MaterialX 1.38.10
Loading...
Searching...
No Matches
HwShaderGenerator.h
Go to the documentation of this file.
1//
2// Copyright Contributors to the MaterialX Project
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef MATERIALX_HWSHADERGENERATOR_H
7#define MATERIALX_HWSHADERGENERATOR_H
8
11
13
16
17MATERIALX_NAMESPACE_BEGIN
18
19/*
20The HW shader generators have a number of predefined variables (inputs and uniforms) with binding rules.
21When these are used by a shader the application must bind them to the expected data. The following table is
22a listing of the variables with a description of what data they should be bound to.
23
24However, different renderers can have different requirements on naming conventions for these variables.
25In order to facilitate this the generators will use token substitution for naming the variables. The
26first colum below shows the token names that should be used in source code before the token substitution
27is done. The second row shows the real identifier names that will be used by default after substitution.
28An generator can override these identifier names in order to use a custom naming convention for these.
29Overriding identifier names is done by changing the entries in the identifiers map given to the function
30replaceIdentifiers(), which is handling the token substitution on a shader stage.
31
32----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
33 TOKEN NAME DEFAULT IDENTIFIER NAME TYPE BINDING
34----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
35
36Vertex input variables :
37 $inPosition i_position vec3 Vertex position in object space
38 $inNormal i_normal vec3 Vertex normal in object space
39 $inTangent i_tangent vec3 Vertex tangent in object space
40 $inBitangent i_bitangent vec3 Vertex bitangent in object space
41 $inTexcoord_N i_texcoord_N vec2 Vertex texture coordinate for the N:th uv set
42 $inColor_N i_color_N vec4 Vertex color for the N:th color set (RGBA)
43
44Uniform variables :
45 $worldMatrix u_worldMatrix mat4 World transformation
46 $worldInverseMatrix u_worldInverseMatrix mat4 World transformation, inverted
47 $worldTransposeMatrix u_worldTransposeMatrix mat4 World transformation, transposed
48 $worldInverseTransposeMatrix u_worldInverseTransposeMatrix mat4 World transformation, inverted and transposed
49 $viewMatrix u_viewMatrix mat4 View transformation
50 $viewInverseMatrix u_viewInverseMatrix mat4 View transformation, inverted
51 $viewTransposeMatrix u_viewTransposeMatrix mat4 View transformation, transposed
52 $viewInverseTransposeMatrix u_viewInverseTransposeMatrix mat4 View transformation, inverted and transposed
53 $projectionMatrix u_projectionMatrix mat4 Projection transformation
54 $projectionInverseMatrix u_projectionInverseMatrix mat4 Projection transformation, inverted
55 $projectionTransposeMatrix u_projectionTransposeMatrix mat4 Projection transformation, transposed
56 $projectionInverseTransposeMatrix u_projectionInverseTransposeMatrix mat4 Projection transformation, inverted and transposed
57 $worldViewMatrix u_worldViewMatrix mat4 World-view transformation
58 $viewProjectionMatrix u_viewProjectionMatrix mat4 View-projection transformation
59 $worldViewProjectionMatrix u_worldViewProjectionMatrix mat4 World-view-projection transformation
60 $viewPosition u_viewPosition vec3 World-space position of the view (camera)
61 $viewDirection u_viewDirection vec3 World-space direction of the view (camera)
62 $frame u_frame float The current frame number as defined by the host application
63 $time u_time float The current time in seconds
64 $geomprop_<name> u_geomprop_<name> <type> A named property of given <type> where <name> is the name of the variable on the geometry
65 $numActiveLightSources u_numActiveLightSources int The number of currently active light sources. Note that in shader this is clamped against
66 the maximum allowed number of lights sources. The maximum number is set by the generation
67 option GenOptions.hwMaxActiveLightSources.
68 $lightData[] u_lightData[] struct Array of struct LightData holding parameters for active light sources.
69 The LightData struct is built dynamically depending on requirements for
70 bound light shaders.
71 $envMatrix u_envMatrix mat4 Rotation matrix for the environment.
72 $envIrradiance u_envIrradiance sampler2D Sampler for the texture used for diffuse environment lighting.
73 $envRadiance u_envRadiance sampler2D Sampler for the texture used for specular environment lighting.
74 $envLightIntensity u_envLightIntensity float Linear multiplier for environment lighting
75 $envRadianceMips u_envRadianceMips int Number of mipmaps used on the specular environment texture.
76 $envRadianceSamples u_envRadianceSamples int Samples to use if Filtered Importance Sampling is used for specular environment lighting.
77
78----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
79----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
80*/
81
83namespace HW
84{
86extern MX_GENSHADER_API const string T_IN_POSITION;
87extern MX_GENSHADER_API const string T_IN_NORMAL;
88extern MX_GENSHADER_API const string T_IN_TANGENT;
89extern MX_GENSHADER_API const string T_IN_BITANGENT;
90extern MX_GENSHADER_API const string T_IN_TEXCOORD;
91extern MX_GENSHADER_API const string T_IN_GEOMPROP;
92extern MX_GENSHADER_API const string T_IN_COLOR;
93extern MX_GENSHADER_API const string T_POSITION_WORLD;
94extern MX_GENSHADER_API const string T_NORMAL_WORLD;
95extern MX_GENSHADER_API const string T_TANGENT_WORLD;
96extern MX_GENSHADER_API const string T_BITANGENT_WORLD;
97extern MX_GENSHADER_API const string T_POSITION_OBJECT;
98extern MX_GENSHADER_API const string T_NORMAL_OBJECT;
99extern MX_GENSHADER_API const string T_TANGENT_OBJECT;
100extern MX_GENSHADER_API const string T_BITANGENT_OBJECT;
101extern MX_GENSHADER_API const string T_TEXCOORD;
102extern MX_GENSHADER_API const string T_COLOR;
103extern MX_GENSHADER_API const string T_WORLD_MATRIX;
104extern MX_GENSHADER_API const string T_WORLD_INVERSE_MATRIX;
105extern MX_GENSHADER_API const string T_WORLD_TRANSPOSE_MATRIX;
106extern MX_GENSHADER_API const string T_WORLD_INVERSE_TRANSPOSE_MATRIX;
107extern MX_GENSHADER_API const string T_VIEW_MATRIX;
108extern MX_GENSHADER_API const string T_VIEW_INVERSE_MATRIX;
109extern MX_GENSHADER_API const string T_VIEW_TRANSPOSE_MATRIX;
110extern MX_GENSHADER_API const string T_VIEW_INVERSE_TRANSPOSE_MATRIX;
111extern MX_GENSHADER_API const string T_PROJ_MATRIX;
112extern MX_GENSHADER_API const string T_PROJ_INVERSE_MATRIX;
113extern MX_GENSHADER_API const string T_PROJ_TRANSPOSE_MATRIX;
114extern MX_GENSHADER_API const string T_PROJ_INVERSE_TRANSPOSE_MATRIX;
115extern MX_GENSHADER_API const string T_WORLD_VIEW_MATRIX;
116extern MX_GENSHADER_API const string T_VIEW_PROJECTION_MATRIX;
117extern MX_GENSHADER_API const string T_WORLD_VIEW_PROJECTION_MATRIX;
118extern MX_GENSHADER_API const string T_VIEW_POSITION;
119extern MX_GENSHADER_API const string T_VIEW_DIRECTION;
120extern MX_GENSHADER_API const string T_FRAME;
121extern MX_GENSHADER_API const string T_TIME;
122extern MX_GENSHADER_API const string T_GEOMPROP;
123extern MX_GENSHADER_API const string T_ALPHA_THRESHOLD;
124extern MX_GENSHADER_API const string T_NUM_ACTIVE_LIGHT_SOURCES;
125extern MX_GENSHADER_API const string T_ENV_MATRIX;
126extern MX_GENSHADER_API const string T_ENV_RADIANCE;
127extern MX_GENSHADER_API const string T_ENV_RADIANCE_MIPS;
128extern MX_GENSHADER_API const string T_ENV_RADIANCE_SAMPLES;
129extern MX_GENSHADER_API const string T_ENV_IRRADIANCE;
130extern MX_GENSHADER_API const string T_ENV_LIGHT_INTENSITY;
131extern MX_GENSHADER_API const string T_ENV_PREFILTER_MIP;
132extern MX_GENSHADER_API const string T_REFRACTION_TWO_SIDED;
133extern MX_GENSHADER_API const string T_ALBEDO_TABLE;
134extern MX_GENSHADER_API const string T_ALBEDO_TABLE_SIZE;
135extern MX_GENSHADER_API const string T_AMB_OCC_MAP;
136extern MX_GENSHADER_API const string T_AMB_OCC_GAIN;
137extern MX_GENSHADER_API const string T_SHADOW_MAP;
138extern MX_GENSHADER_API const string T_SHADOW_MATRIX;
139extern MX_GENSHADER_API const string T_VERTEX_DATA_INSTANCE;
140extern MX_GENSHADER_API const string T_LIGHT_DATA_INSTANCE;
141
144extern MX_GENSHADER_API const string IN_POSITION;
145extern MX_GENSHADER_API const string IN_NORMAL;
146extern MX_GENSHADER_API const string IN_TANGENT;
147extern MX_GENSHADER_API const string IN_BITANGENT;
148extern MX_GENSHADER_API const string IN_TEXCOORD;
149extern MX_GENSHADER_API const string IN_GEOMPROP;
150extern MX_GENSHADER_API const string IN_COLOR;
151extern MX_GENSHADER_API const string POSITION_WORLD;
152extern MX_GENSHADER_API const string NORMAL_WORLD;
153extern MX_GENSHADER_API const string TANGENT_WORLD;
154extern MX_GENSHADER_API const string BITANGENT_WORLD;
155extern MX_GENSHADER_API const string POSITION_OBJECT;
156extern MX_GENSHADER_API const string NORMAL_OBJECT;
157extern MX_GENSHADER_API const string TANGENT_OBJECT;
158extern MX_GENSHADER_API const string BITANGENT_OBJECT;
159extern MX_GENSHADER_API const string TEXCOORD;
160extern MX_GENSHADER_API const string COLOR;
161extern MX_GENSHADER_API const string WORLD_MATRIX;
162extern MX_GENSHADER_API const string WORLD_INVERSE_MATRIX;
163extern MX_GENSHADER_API const string WORLD_TRANSPOSE_MATRIX;
164extern MX_GENSHADER_API const string WORLD_INVERSE_TRANSPOSE_MATRIX;
165extern MX_GENSHADER_API const string VIEW_MATRIX;
166extern MX_GENSHADER_API const string VIEW_INVERSE_MATRIX;
167extern MX_GENSHADER_API const string VIEW_TRANSPOSE_MATRIX;
168extern MX_GENSHADER_API const string VIEW_INVERSE_TRANSPOSE_MATRIX;
169extern MX_GENSHADER_API const string PROJ_MATRIX;
170extern MX_GENSHADER_API const string PROJ_INVERSE_MATRIX;
171extern MX_GENSHADER_API const string PROJ_TRANSPOSE_MATRIX;
172extern MX_GENSHADER_API const string PROJ_INVERSE_TRANSPOSE_MATRIX;
173extern MX_GENSHADER_API const string WORLD_VIEW_MATRIX;
174extern MX_GENSHADER_API const string VIEW_PROJECTION_MATRIX;
175extern MX_GENSHADER_API const string WORLD_VIEW_PROJECTION_MATRIX;
176extern MX_GENSHADER_API const string VIEW_POSITION;
177extern MX_GENSHADER_API const string VIEW_DIRECTION;
178extern MX_GENSHADER_API const string FRAME;
179extern MX_GENSHADER_API const string TIME;
180extern MX_GENSHADER_API const string GEOMPROP;
181extern MX_GENSHADER_API const string ALPHA_THRESHOLD;
182extern MX_GENSHADER_API const string NUM_ACTIVE_LIGHT_SOURCES;
183extern MX_GENSHADER_API const string ENV_MATRIX;
184extern MX_GENSHADER_API const string ENV_RADIANCE;
185extern MX_GENSHADER_API const string ENV_RADIANCE_MIPS;
186extern MX_GENSHADER_API const string ENV_RADIANCE_SAMPLES;
187extern MX_GENSHADER_API const string ENV_IRRADIANCE;
188extern MX_GENSHADER_API const string ENV_LIGHT_INTENSITY;
189extern MX_GENSHADER_API const string ENV_PREFILTER_MIP;
190extern MX_GENSHADER_API const string REFRACTION_TWO_SIDED;
191extern MX_GENSHADER_API const string ALBEDO_TABLE;
192extern MX_GENSHADER_API const string ALBEDO_TABLE_SIZE;
193extern MX_GENSHADER_API const string AMB_OCC_MAP;
194extern MX_GENSHADER_API const string AMB_OCC_GAIN;
195extern MX_GENSHADER_API const string SHADOW_MAP;
196extern MX_GENSHADER_API const string SHADOW_MATRIX;
197extern MX_GENSHADER_API const string VERTEX_DATA_INSTANCE;
198extern MX_GENSHADER_API const string LIGHT_DATA_INSTANCE;
199extern MX_GENSHADER_API const string LIGHT_DATA_MAX_LIGHT_SOURCES;
200
202extern MX_GENSHADER_API const string VERTEX_INPUTS; // Geometric inputs for vertex stage.
203extern MX_GENSHADER_API const string VERTEX_DATA; // Connector block for data transfer from vertex stage to pixel stage.
204extern MX_GENSHADER_API const string PRIVATE_UNIFORMS; // Uniform inputs set privately by application.
205extern MX_GENSHADER_API const string PUBLIC_UNIFORMS; // Uniform inputs visible in UI and set by user.
206extern MX_GENSHADER_API const string LIGHT_DATA; // Uniform inputs for light sources.
207extern MX_GENSHADER_API const string PIXEL_OUTPUTS; // Outputs from the main/pixel stage.
208
210extern MX_GENSHADER_API const string DIR_N;
211extern MX_GENSHADER_API const string DIR_L;
212extern MX_GENSHADER_API const string DIR_V;
213extern MX_GENSHADER_API const string WORLD_POSITION;
214extern MX_GENSHADER_API const string OCCLUSION;
215
217extern MX_GENSHADER_API const string ATTR_TRANSPARENT;
218
220extern MX_GENSHADER_API const string USER_DATA_LIGHT_SHADERS;
221extern MX_GENSHADER_API const string USER_DATA_BINDING_CONTEXT;
222} // namespace HW
223
224namespace Stage
225{
227extern MX_GENSHADER_API const string VERTEX;
228} // namespace Stage
229
230class HwLightShaders;
233
235using HwLightShadersPtr = shared_ptr<class HwLightShaders>;
237using HwShaderGeneratorPtr = shared_ptr<class HwShaderGenerator>;
239using HwResourceBindingContextPtr = shared_ptr<class HwResourceBindingContext>;
240
243class MX_GENSHADER_API HwLightShaders : public GenUserData
244{
245 public:
248 {
249 return std::make_shared<HwLightShaders>();
250 }
251
253 void bind(unsigned int type, ShaderNodePtr shader)
254 {
255 _shaders[type] = shader;
256 }
257
259 void unbind(unsigned int type)
260 {
261 _shaders.erase(type);
262 }
263
265 void clear()
266 {
267 _shaders.clear();
268 }
269
272 const ShaderNode* get(unsigned int type) const
273 {
274 auto it = _shaders.find(type);
275 return it != _shaders.end() ? it->second.get() : nullptr;
276 }
277
279 const std::unordered_map<unsigned int, ShaderNodePtr>& get() const
280 {
281 return _shaders;
282 }
283
284 protected:
285 std::unordered_map<unsigned int, ShaderNodePtr> _shaders;
286};
287
290class MX_GENSHADER_API HwShaderGenerator : public ShaderGenerator
291{
292 public:
294 void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const override;
295
297 virtual void addStageLightingUniforms(GenContext& context, ShaderStage& stage) const;
298
300 void getClosureContexts(const ShaderNode& node, vector<ClosureContext*>& cct) const override;
301
306 static void bindLightShader(const NodeDef& nodeDef, unsigned int lightTypeId, GenContext& context);
307
309 static void unbindLightShader(unsigned int lightTypeId, GenContext& context);
310
312 static void unbindLightShaders(GenContext& context);
313
315 virtual string getVertexDataPrefix(const VariableBlock& vertexData) const = 0;
316
319 {
320 DEFAULT,
321 REFLECTION,
322 TRANSMISSION,
323 INDIRECT,
324 EMISSION
325 };
326
329 static const string CLOSURE_CONTEXT_SUFFIX_TRANSMISSION;
330 static const string CLOSURE_CONTEXT_SUFFIX_INDIRECT;
331
332 protected:
334
336 virtual ShaderPtr createShader(const string& name, ElementPtr element, GenContext& context) const;
337
340 mutable ClosureContext _defReflection;
341 mutable ClosureContext _defTransmission;
342 mutable ClosureContext _defIndirect;
343 mutable ClosureContext _defEmission;
344};
345
348class MX_GENSHADER_API HwImplementation : public ShaderNodeImpl
349{
350 public:
351 bool isEditable(const ShaderInput& input) const override;
352
353 protected:
354 HwImplementation() { }
355
356 // Integer identifiers for coordinate spaces.
357 // The order must match the order given for the space enum string in stdlib.
358 enum Space
359 {
360 MODEL_SPACE = 0,
361 OBJECT_SPACE = 1,
362 WORLD_SPACE = 2
363 };
364
366 static const string SPACE;
367 static const string INDEX;
368 static const string GEOMPROP;
369};
370
373class MX_GENSHADER_API HwResourceBindingContext : public GenUserData
374{
375 public:
376 virtual ~HwResourceBindingContext() { }
377
378 // Initialize the context before generation starts.
379 virtual void initialize() = 0;
380
381 // Emit directives required for binding support
382 virtual void emitDirectives(GenContext& context, ShaderStage& stage) = 0;
383
384 // Emit uniforms with binding information
385 virtual void emitResourceBindings(GenContext& context, const VariableBlock& uniforms, ShaderStage& stage) = 0;
386
387 // Emit struct uniforms with binding information
388 virtual void emitStructuredResourceBindings(GenContext& context, const VariableBlock& uniforms,
389 ShaderStage& stage, const std::string& structInstanceName,
390 const std::string& arraySuffix = EMPTY_STRING) = 0;
391};
392
393MATERIALX_NAMESPACE_END
394
395#endif
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
Context classes for shader generation.
shared_ptr< class HwShaderGenerator > HwShaderGeneratorPtr
Shared pointer to a HwShaderGenerator.
Definition: HwShaderGenerator.h:237
shared_ptr< class HwResourceBindingContext > HwResourceBindingContextPtr
Shared pointer to a HwResourceBindingContext.
Definition: HwShaderGenerator.h:239
shared_ptr< class HwLightShaders > HwLightShadersPtr
Shared pointer to a HwLightShaders.
Definition: HwShaderGenerator.h:235
Macros for declaring imported and exported symbols.
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition: Library.h:34
Base shader generator class.
shared_ptr< class ShaderNode > ShaderNodePtr
Shared pointer to a ShaderNode.
Definition: ShaderNode.h:35
shared_ptr< Syntax > SyntaxPtr
Shared pointer to a Syntax.
Definition: Syntax.h:26
Class representing a context for closure evaluation.
Definition: GenContext.h:248
A context class for shader generation.
Definition: GenContext.h:31
Base class for custom user data needed during shader generation.
Definition: GenUserData.h:27
Definition: HwShaderGenerator.h:349
static const string SPACE
Internal string constants.
Definition: HwShaderGenerator.h:366
Hardware light shader user data.
Definition: HwShaderGenerator.h:244
void bind(unsigned int type, ShaderNodePtr shader)
Bind a light shader to a light type id.
Definition: HwShaderGenerator.h:253
const ShaderNode * get(unsigned int type) const
Return the light shader bound to the given light type, or nullptr if not light shader is bound to thi...
Definition: HwShaderGenerator.h:272
const std::unordered_map< unsigned int, ShaderNodePtr > & get() const
Return the map of bound light shaders.
Definition: HwShaderGenerator.h:279
static HwLightShadersPtr create()
Create and return a new instance.
Definition: HwShaderGenerator.h:247
void clear()
Clear all light shaders previously bound.
Definition: HwShaderGenerator.h:265
void unbind(unsigned int type)
Unbind a light shader previously bound to a light type id.
Definition: HwShaderGenerator.h:259
Class representing a context for resource binding for hardware resources.
Definition: HwShaderGenerator.h:374
Base class for shader generators targeting HW rendering.
Definition: HwShaderGenerator.h:291
static const string CLOSURE_CONTEXT_SUFFIX_REFLECTION
String constants for closure context suffixes.
Definition: HwShaderGenerator.h:328
ClosureContext _defDefault
Closure contexts for defining closure functions.
Definition: HwShaderGenerator.h:339
ClosureContextType
Types of closure contexts for HW.
Definition: HwShaderGenerator.h:319
virtual string getVertexDataPrefix(const VariableBlock &vertexData) const =0
Determine the prefix of vertex data variables.
A node definition element within a Document.
Definition: Definition.h:82
Base class for shader generators All third-party shader generators should derive from this class.
Definition: ShaderGenerator.h:31
An input on a ShaderNode.
Definition: ShaderNode.h:264
Class representing a node in the shader generation DAG.
Definition: ShaderNode.h:326
Class handling the shader generation implementation for a node.
Definition: ShaderNodeImpl.h:31
A shader stage, containing the state and resulting source code for the stage.
Definition: ShaderStage.h:139
A block of variables in a shader stage.
Definition: ShaderStage.h:61
HW specific identifiers.
Definition: HwShaderGenerator.cpp:34
const string IN_POSITION
Default names for identifiers.
Definition: HwShaderGenerator.cpp:92
const string T_IN_POSITION
Token identifiers.
Definition: HwShaderGenerator.cpp:36
const string ATTR_TRANSPARENT
Attribute names.
Definition: HwShaderGenerator.cpp:160
const string VERTEX_INPUTS
Variable blocks names.
Definition: HwShaderGenerator.cpp:149
const string DIR_N
Variable names for lighting parameters.
Definition: HwShaderGenerator.cpp:155
const string USER_DATA_LIGHT_SHADERS
User data names.
Definition: HwShaderGenerator.cpp:162