1# RTSS: Run Time Shader System {#rtss}
2
3@tableofcontents
4
5# Core features of the system {#core-feats}
6* Runtime shader generation synchronized with scene state. Each time scene settings change, a new set of shaders is generated.
7* Full Fixed Function Pipeline (FFP) emulation. This feature is most useful combined with render system that doesn't provide any FFP functionality (OpenGL ES 2.0, D3D11 etc).
8* Shader language independent interface: the logic representation of the shader programs is completely independent from the target shader language. You can generate code for different shader languages from the same program.
9* Pluggable interface allows extending the target shader languages set.
10* Pluggable interface allows adding new shader based effects to the system in a seamless way. Each effect code will be automatically combined with the rest of the shader code.
11* Smart program management: each shader program is created only once and may be used by multiple passes.
12* Automatic vertex shader compacting mechanism: no more compacting variables by hand. In case the amount of used vertex shader output registers exceeds the maximum allowed (12 to 32, depending on [D3DPSHADERCAPS2_0.NumTemps](http://msdn.microsoft.com/en-us/library/bb172918%28v=VS.85%29.aspx)), a compacting algorithm packs the vertex shader outputs and adds unpack code in the fragment shader side.
13* Material script support, for both export and import.
14
15# System overview {#rtss_overview}
16
17The RTSS manages a set of opaque isolated components (SubRenderStates) where each implements a specific effect.
18These "effects" include Fixed Function transformation and lighting. At the core these components are plain shader files providing a set of functions; e.g. @ref FFP_FUNC_LIGHT_DIRECTIONAL_DIFFUSE, @ref FFP_FUNC_LIGHT_POINT_DIFFUSE.
19
20Correctly ordering these functions, providing them with the right input values and interconnecting them is the main purpose of the RTSS.
21
22To this end the RTSS defines a set of stages; e.g Ogre::RTShader::FFP_VS_TRANSFORM, Ogre::RTShader::FFP_PS_TEXTURING.
23It then queries each registered Ogre::RTShader::SubRenderState to attach its functions to these stages. Then it generates the entry function (e.g. `main()` for GLSL) by sequentially calling these functions.
24
25You can think of stages as a way to group shader "effects" inside a Ogre::Pass - similarly how a Ogre::RenderQueueGroup groups [renderables](@ref Ogre::Renderable) for rendering.
26
27Basically it performs the following (simplified) transformation, given
28```cpp
29// GLOBAL PARAMETERS
30$global_parameters
31// FUNCTION
32$input_parameters
33$output_parameters
34void main() {
35	$local_parameters
36	$FFP_VS_TRANSFORM
37	(...)
38	$FFP_VS_TEXTURING
39}
40```
41and `$FFP_VS_TRANSFORM = [FFP_FUNC_TRANSFORM]`, `$FFP_VS_TEXTURING = [FFP_FUNC_TRANSFORM_TEXCOORD]`, it generates
42
43```cpp
44// FORWARD DECLARATIONS
45void FFP_Transform(in mat4, in vec4, out vec4);
46void FFP_TransformTexCoord(in mat4, in vec2, out vec2);
47// GLOBAL PARAMETERS
48uniform	mat4	worldviewproj_matrix;
49uniform	mat4	texture_matrix1;
50// FUNCTION
51in	vec4	vertex;
52in	vec4	uv0;
53out	vec4	oTexcoord4_0;
54void main() {
55	FFP_Transform(worldviewproj_matrix, vertex, gl_Position);
56	FFP_TransformTexCoord(texture_matrix1, uv0.xy, oTexcoord4_0.xy);
57}
58```
59
60As you can see the RTSS also resolved the required parameters and routed them into the correct functions. See @ref creating-extensions for details about parameter resolution.
61
62Now that you know what the RTSS does, you are probably wondering how to change which functions are emitted per stage. Lets say, change the lighting from the FFP style per-vertex lighting to per-pixel lighting.
63
64The RTSS is flexible enough to "just" move the according calculations from the vertex shader to the pixel shader.
65
66## Customising the RTSS using the API {#rtss_custom_api}
67
68The first option is to globally enforce per-pixel lighting, you can do the following
69
70@snippet Components/Bites/src/OgreAdvancedRenderControls.cpp rtss_per_pixel
71
72any non FFP SRS will automatically override the default SRS for the same stage. Ogre::RTShader::FFP_LIGHTING in this case.
73
74## Customizing the RTSS using Material Scripts {#rtss_custom_mat}
75
76Alternatively you can enable per-pixel lighting for one material only, by adding a `rtshader_system` section to the pass as following
77
78@snippet Samples/Media/RTShaderLib/materials/RTShaderSystem.material rtss_per_pixel
79
80for more examples see `Samples/Media/RTShaderLib/materials/RTShaderSystem.material`.
81
82Here are the attributes you can use in a `rtshader_system` section of a .material script:
83
84- [lighting_stage](#lighting_stage)
85- [fog_stage](#fog_stage)
86- [light_count](#light_count)
87- [triplanarTexturing](#triplanarTexturing)
88- [integrated_pssm4](#integrated_pssm4)
89- [layered_blend](#layered_blend)
90- [source_modifier](#source_modifier)
91
92<a name="lighting_stage"></a>
93
94### lighting_stage
95
96Force a specific lighting model.
97
98@par
99Format1: `lighting_stage <ffp|per_pixel> [normalised]`
100@par
101Format2: `lighting_stage normal_map <texturename> [tangent_space|object_space] [coordinateIndex] [samplerName]`
102@par
103Example: `lighting_stage normal_map Panels_Normal_Tangent.png tangent_space 0 SamplerToUse`
104
105@param normalised @copybrief Ogre::RTShader::FFPLighting::setNormaliseEnabled @copydetails Ogre::RTShader::FFPLighting::setNormaliseEnabled
106
107@see Ogre::RTShader::NormalMapLighting::NormalMapSpace
108@see @ref Samplers
109
110<a name="fog_stage"></a>
111
112### fog_stage
113
114Force a specific fog calculation
115
116@par
117Format: `fog_stage ffp <per_vertex|per_pixel>`
118@par
119Example: `fog_stage ffp per_pixel`
120
121<a name="light_count"></a>
122
123### light_count
124
125Override dynamic light count. Allows to customize which lights the RTSS will consider.
126@par
127Format: `light_count <pointLights> <directionalLights> <spotLights>`
128
129<a name="triplanarTexturing"></a>
130
131### triplanarTexturing
132
133Force [triplanar texturing](https://www.volume-gfx.com/volume-rendering/triplanar-texturing/)
134@par
135Format: `triplanarTexturing <textureScale> <plateauSize> <transitionSpeed> <textureFromX> <textureFromY> <textureFromZ>`
136@par
137Example: `triplanarTexturing 0.05 0.2 4.0 BumpyMetal.jpg egyptrockyfull.jpg MtlPlat2.jpg`
138
139@param textureScale texture coordinates are multiplied by this.
140@param plateauSize plateau on which small components of the normal have no influence.
141@param transitionSpeed transitions speed between the three textures
142Valid values are [0; 0.57] not bigger to avoid division by zero
143@param textureFromX Texture for the x-direction planar mapping
144@param textureFromY Texture for the y-direction planar mapping
145@param textureFromZ Texture for the z-direction planar mapping
146
147<a name="integrated_pssm4"></a>
148
149### integrated_pssm4
150Integrated PSSM shadow receiver with 2 splits. Custom split points.
151@par
152Format: `integrated_pssm4 <znear> <sp0> <sp1> <zfar>`
153
154<a name="layered_blend"></a>
155
156### layered_blend
157
158Apply photoshop-like blend effects to texture layers
159@par
160Format: `layered_blend <effect>`
161@par
162Example: layered_blend luminosity
163
164@note only applicable inside a texture_unit section
165
166@param effect one of `default, normal, lighten, darken, multiply, average, add, subtract, difference, negation, exclusion, screen, overlay, hard_light, soft_light, color_dodge, color_burn, linear_dodge, linear_burn, linear_light, vivid_light, pin_light, hard_mix, reflect, glow, phoenix, saturation, color, luminosity`
167
168
169<a name="source_modifier"></a>
170
171### source_modifier
172
173Apply custom modulate effect to texture layer
174@par
175Format: `source_modifier <operation> custom <parameterNum>`
176@par
177Example: `source_modifier src1_inverse_modulate custom 2`
178
179@note only applicable inside a texture_unit section
180
181@param operation one of `src1_modulate, src2_modulate, src1_inverse_modulate, src2_inverse_modulate`
182@param parameterNum number of the custom shader parameter that controls the operation
183
184# The RTSS in Depth {#rtss_indepth}
185
186When the user asks the system to generate shaders for a given technique it has to provide the system a name for the target technique scheme. The system in turn, then creates a new technique based on the source technique but with a different scheme name.
187__Note:__ In order to avoid clashes the source technique must NOT contain any shaders otherwise this step will fail.
188
189The idea behind this concept is to use Ogre's built in mechanism of material schemes, so all the user has to do in order to use the new technique is to change the material scheme of his viewport(s).
190
191Before each viewport update, the system performs a validation step of all associated shader based techniques it created. This step includes automatic synchronization with the scene lights and fog states. When the system detects that a scheme is out of date it generates the appropriate shaders for each technique new.
192
193The following steps are executed in order to generate shaders for a given technique:
194
195* For each pass in the technique the system builds a set of sub render states that describe the logic process of the rendering pipeline from the draw call submission until the final pixel color.
196* Each render state is translated into a set of logic shader programs (currently only pixel and vertex shader).
197The logic programs are then sent to specific shader language writers that produce source code for the respective shader language. The source code is used to create the GPU programs that are applied to the destination pass.
198Before rendering of an object that uses generated shaders the system allows each sub render state to update the GPU constants associated with it.
199
200## Initializing the system
201
202@note If you are using the OgreBites::ApplicationContext, the following steps will be taken automatically for you.
203
204Initializing the system is composed of the following steps:
205* Create the internal managers and structures via the `Ogre::RTShader::ShaderGenerator::initialize()` method.
206* Set the target cache path. This is the place on your disk where the output shaders will be written to or will be read from in case they were generated by previous runs of your application.
207* Verify that the location of the shader libs needed by the system is added to the ResourceGroupManager via the `Ogre::ResourceGroupManager::addResourceLocation()` method.
208* Assign the target scene manager to the shader generator.
209* Add one or more specialized sub-render states that are to be shared among all materials (per pixel lighting, textured fog, etc...).
210
211```cpp
212if (Ogre::RTShader::ShaderGenerator::initialize())
213{
214	// Grab the shader generator pointer.
215	mShaderGenerator = Ogre::RTShader::ShaderGenerator::getSingletonPtr();
216
217	// Add the shader libs resource location. a sample shader lib can be found in Samples\Media\RTShaderLib
218	Ogre::ResourceGroupManager::getSingleton().addResourceLocation(shaderLibPath, "FileSystem");
219
220	// Set shader cache path.
221	mShaderGenerator->setShaderCachePath(shaderCachePath);
222
223	// Set the scene manager.
224	mShaderGenerator->addSceneManager(sceneMgr);
225
226	return true;
227}
228```
229
230## Creating shader based technique
231This step will associate the given technique with a destination shader generated based technique. Calling the `Ogre::RTShader::ShaderGenerator::createShaderBasedTechnique()` will cause the system to generate internal data structures associated with the source technique and will add new technique to the source material. This new technique will have the scheme name that was passed as an argument to this method and all its passes will contain shaders that the system will generate and update during the application runtime.
232
233To use the generated technique set the change material scheme of your viewport(s) to the same scheme name you passed as argument to this method.
234
235Note that you can automate the shader generation process for all materials. First set the viewport scheme to the destination scheme of the RTSS shaders. Second register to the `Ogre::MaterialManager::Listener` and implement the `handleSchemeNotFound()` function. If the function requests a scheme for the RTSS, generate it based on functions parameters.
236
237```cpp
238// Create shader based technique from the default technique of the given material.
239mShaderGenerator->createShaderBasedTechnique("Examples/BeachStones", Ogre::MaterialManager::DEFAULT_SCHEME_NAME, Ogre::RTShader::ShaderGenerator::DEFAULT_SCHEME_NAME);
240
241// Apply the shader generated based techniques.
242mViewport->setMaterialScheme(Ogre::RTShader::ShaderGenerator::DEFAULT_SCHEME_NAME);
243```
244![](CreateShaderBasedTech.svg)
245
246## Runtime shader generation
247During the application runtime the ShaderGenerator instance receives notifications on per frame basis from its target SceneManager.
248At this point it checks the material scheme in use. In case the current scheme has representations in the manager, it executes its validate method.
249The SGScheme validation includes synchronization with scene light and fog settings. In case it is out of date it will rebuild all shader generated techniques.
250The first step is to loop over every SGTechnique associated with this SGScheme and build its RenderStates - one for each pass. Each RenderState has its own hash code and it is cached at the ShaderGenerator. The same RenderState can be shared by multiple SGPasses.
251The second step is to loop again on every SGTechnique and acquire a program set for each SGPass. The actual acquiring process is done by the ProgramManager that generates CPU program representation, send them to a matching ProgramWriter that is chosen by the active target language, the writer generates source code that is the basis for the GPU programs.
252The result of this entire process is that each technique associated with the SGScheme has vertex and pixel shaders applied to all its passes. These shaders are synchronized with scene lights and fog settings.
253
254![](RuntimeShaderGeneration.svg)
255
256## Main components {#rtss__components}
257The following is an partial list of components within the RTSS. These components are listed as they have great importance in understanding controlling and later extending the RTSS system.
258
259@par ShaderGenerator
260The ShaderGenerator is the main interface to the RTSS system. Through it you can request to generate and destroy the shaders, influence from what parts to create the shaders, and control general system settings such as the shading language and shader caching.
261
262@par RenderState classes
263A render state describes the different components that a shader will be created from. These components are referred to as SubRenderStates.
264@par
265RenderStates exist on 2 levels: scheme and pass. Scheme RenderStates describe the SubRenderStates that will be used when creating a shader for a given material scheme. Pass RenderState describe the SubRenderStates that will be used when creating a specific pass of a specific material. When a shader is generated for a given material the system combines the SubRenderStates from both RenderStates to create a shader specific for a material pass in a specific scheme.
266
267@par SubRenderState classes
268Sub-render states (SRS) are components designed to generate the code of the RTSS shaders. Each SRS usually has a specific role to fill within the shader's construction. These components can be combined in different combinations to create shaders with different capabilities.
269@par
270There are 5 basic SRSs. These are used to recreate the functionality provided by the fixed pipeline and are added by default to every scheme RenderState:
271* Ogre::RTShader::FFPTransform - responsible for adding code to the vertex shader which computes the position of the vertex in projection space
272* Ogre::RTShader::FFPColour - responsible for adding code to the shaders that calculate the base diffuse and specular color of the object regardless of lights or textures. The color is calculated based on the ambient, diffuse, specular and emissive properties of the object and scene, color tracking and the specified hardware buffer color.
273* Ogre::RTShader::FFPLighting - responsible for adding code to the shaders that calculate the luminescence added to the object by light. Then add that value to the color calculated by the color SRS stage.
274* Ogre::RTShader::FFPTexturing - responsible for adding code that modulates the color of the pixels based on textures assigned to the material.
275* Ogre::RTShader::FFPFog - responsible for adding code that modulates the color of a pixel based on the scene or object fog parameters.
276@par
277There are many more sub render states that already exist in the Ogre system and new ones can be added. Some of the existing SRSs include capabilities such as: per-pixel lighting, texture atlas, advanced texture blend, bump mapping, efficient multiple lights (sample), textured fog (sample), etc...
278
279@par SubRenderStateFactory
280As the name suggests, sub render state factories are factories that produce sub render states. Each factory generates a specific SRS.
281@par
282These type of components are note worthy for 2 reason. The first and obvious one is that they allow the system to generate new SRSs for the materials it is asked to generate. The second reason is that they perform as script readers and writers allowing the system to create specific or specialized SRSs per material.
283
284## Creating custom shader extensions {#creating-extensions}
285Although the system implements some common shader based effects such as per pixel lighting, normal map, etc., you may find it useful to write your own shader extensions.
286
287In order to extend the system with your own shader effects you'll have to follow these steps:
288* Implement the SubRenderState interface - This is the main class that is responsible for the actual effect processing such as preparing the destination pass, updating the CPU shader programs, updating the GPU shader parameters etc.
289* Implement the SubRenderStateFactory interface: This class will allow the RTSS to create instances of the previous class via code or script as well as export it to material script file.
290* Register the factory to the RTSS using the Ogre::RTShader::ShaderGenerator::addSubRenderStateFactory method.
291* Add shader files that will supply all the actual shader functions your SubRenderState needs. In order to support multiple shader languages you should supply code for your entire desired target shading languages (CG, HLSL, GLSL etc). These files should be placed in a way that the resource manager could access them. This can be done by placing them in a valid resource location or by dynamically adding resource location.
292
293Implementing the SubRenderState requires overriding the pure methods of the base class.
294* Ogre::RTShader::SubRenderState::getType() should return unique string that identify the sub class implementation. That value is shared among all instances and can be stored in a static string variable. It uses to system to match between SubRenderState instance and the factory to should destroy it.
295* Ogre::RTShader::SubRenderState::getExecutionOrder() should return integer value that will use the system to sort all SubRenderState instances of the same render state before each one of them will create its part in the CPU shader programs. Note that:
296 * The execution order does not imply the order of the parameter definitions and function calls within the generated shader.
297 * If an execution number is set to be the same as one of the basic fixed pipeline SRSs. Than that SRS will be built __instead__ of the fixed pipeline SRS.
298* Ogre::RTShader::SubRenderState::copyFrom() a simple copy method that uses the system when coping one instance to another. **Note:** Only configuration data attributes should be copy here.
299* Ogre::RTShader::SubRenderState::createCpuSubPrograms - This is the heart of this interface. This method should update the CPU shader programs with the specific details of the overriding class.
300
301The SubRenderState supply default implementation for this method which break down this method into three stages:
302
303@par Resolving parameters
304this stage should grab all the needed parameters for this SubRenderState. Typically there several SubRenderStates working on a common set of Parameters - either to cooperate or because they use the same inputs.
305Therefore parameters are not resolved by name (except for local variables), but rather by symbolic constants. These can either be of Ogre::GpuProgramParameters::AutoConstantType, which should already be familiar to you or of Ogre::RTShader::Parameter::Content.
306@par
307You can think of the latter as an extension of the Cg/ HLSL Semantics to the actual content of the parameter.
308@par
309In case of the Ogre::RTShader::FFPTransform wee nned the world view projection matrix and vertex shader input and output position parameters.
310@par
311@snippet Components/RTShaderSystem/src/OgreShaderFFPTransform.cpp param_resolve
312
313@par Resolving dependencies
314this stage should provide the name of the external shader library files that contains the actual shader code needed by this SubRenderState.
315In case of the Ogre::RTShader::FFPTexturing  it will add the common and texturing library for both vertex and pixel shader program.
316@par
317@snippet Components/RTShaderSystem/src/OgreShaderFFPTexturing.cpp deps_resolve
318
319@par Adding function invocations
320this stage creates the function calls within this SubRenderState requires. To add function invocations, you first need to obtain a Ogre::RTShader::FunctionStageRef for the respective stage.
321In case of the Ogre::RTShader::FFPFog it will add vertex depth calculation to the vertex shader program.
322@par
323@snippet Components/RTShaderSystem/src/OgreShaderFFPFog.cpp func_invoc
324@par
325The arguments to the function are the ones you resolved in the first step and the function name must be available in one of the libraries you provided in the second step.
326You can add call as many functions as you need. The calls will appear in the same order in the generates shader source code.
327@note
328* The ordering of the function invocation is crucial. Use the Ogre::RTShader::FFPVertexShaderStage and Ogre::RTShader::FFPFragmentShaderStage enumarations to place your invocations in the desired global order.
329* Make sure the parameter semantic (in/out) in the SubRenderState code matches to your shader code implementation you supplied in the library file. GLSL will fail to link to libray functions if it won't be able to find a perfect function declaration match.
330* Ogre::RTShader::SubRenderState::updateGpuProgramsParams - As the name suggest this method should be overridden only in case your SubRenderState should update some parameter it created before.
331* Ogre::RTShader::SubRenderState::preAddToRenderState(): this method called before adding this SubRenderState to a parent RenderState instances. It allows this SubRenderState to exclude itself from the list in case the source pass is not matching. I.E in case of SubRenderState that perform lighting calculations it can return false when the given source pass specifies that lighting calculations disabled for it.
332@snippet Components/RTShaderSystem/src/OgreShaderFFPLighting.cpp disable
333This method also let the SubRenderState to opportunity to modify the destination pass. I.E the Ogre::RTShader::NormalMapLighting instance adds the normal map texture unit in this context.
334
335Implementing the Ogre::RTShader::SubRenderStateFactory is much simpler and involves implementing the following methods
336* Ogre::RTShader::SubRenderStateFactory::createInstanceImpl(): This method should return instance for the SubRenderState sub class.
337* Ogre::RTShader::SubRenderStateFactory::createInstance(): This method should return instasnce for the SubRenderState sub class using the given script compiler parameters. Implemet this method if you want to be able to creat your custom shader extension from material script.
338* Ogre::RTShader::SubRenderStateFactory::writeInstance(): This method should write down the parameters of a given SubRenderState instance to material script file. Implement this method if you want to be able to export a material that contains your custom shader extension.
339
340## Tips for debugging shaders {#debugging}
341A couple of notes on debugging shaders coming from the RTSS:
342* Call OgreBites::ApplicationContext::setRTSSWriteShadersToDisk. This will cache the generated shaders onto the disk under the directory [OGRE_MEDIA_DIR](@ref cmake)`/RTShaderLib/cache`. This is important for 2 reasons:
343  * It will make compilation problems easier to detect.
344  * Once a shader is written to the disk, as long as you don't change the code behind it, the same shader will be picked up in the next application run even if its content has changed. If you have compilation or visual problems with the shader you can try to manually tinker with it without compiling the code again and again.
345* Add a breakpoint in OgreShaderProgramManager.cpp at
346@snippetlineno Components/RTShaderSystem/src/OgreShaderProgramManager.cpp debug_break
347If a shader will fail to compile it will usually fail there. Once that happens you can find the shader name under the `programName` parameter, then look for it in the cache directory you created.
348* Other common problems with creating shaders in RTSS usually occur from defining vertex shader parameters and using them in the pixel shader and vice versa. so watch out for those.