MaterialX 1.38.9
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 $envRadianceMips u_envRadianceMips int Number of mipmaps used on the specular environment texture.
75 $envRadianceSamples u_envRadianceSamples int Samples to use if Filtered Importance Sampling is used for specular environment lighting.
76
77----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
78----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
79*/
80
82namespace HW
83{
85extern MX_GENSHADER_API const string T_IN_POSITION;
86extern MX_GENSHADER_API const string T_IN_NORMAL;
87extern MX_GENSHADER_API const string T_IN_TANGENT;
88extern MX_GENSHADER_API const string T_IN_BITANGENT;
89extern MX_GENSHADER_API const string T_IN_TEXCOORD;
90extern MX_GENSHADER_API const string T_IN_GEOMPROP;
91extern MX_GENSHADER_API const string T_IN_COLOR;
92extern MX_GENSHADER_API const string T_POSITION_WORLD;
93extern MX_GENSHADER_API const string T_NORMAL_WORLD;
94extern MX_GENSHADER_API const string T_TANGENT_WORLD;
95extern MX_GENSHADER_API const string T_BITANGENT_WORLD;
96extern MX_GENSHADER_API const string T_POSITION_OBJECT;
97extern MX_GENSHADER_API const string T_NORMAL_OBJECT;
98extern MX_GENSHADER_API const string T_TANGENT_OBJECT;
99extern MX_GENSHADER_API const string T_BITANGENT_OBJECT;
100extern MX_GENSHADER_API const string T_TEXCOORD;
101extern MX_GENSHADER_API const string T_COLOR;
102extern MX_GENSHADER_API const string T_WORLD_MATRIX;
103extern MX_GENSHADER_API const string T_WORLD_INVERSE_MATRIX;
104extern MX_GENSHADER_API const string T_WORLD_TRANSPOSE_MATRIX;
105extern MX_GENSHADER_API const string T_WORLD_INVERSE_TRANSPOSE_MATRIX;
106extern MX_GENSHADER_API const string T_VIEW_MATRIX;
107extern MX_GENSHADER_API const string T_VIEW_INVERSE_MATRIX;
108extern MX_GENSHADER_API const string T_VIEW_TRANSPOSE_MATRIX;
109extern MX_GENSHADER_API const string T_VIEW_INVERSE_TRANSPOSE_MATRIX;
110extern MX_GENSHADER_API const string T_PROJ_MATRIX;
111extern MX_GENSHADER_API const string T_PROJ_INVERSE_MATRIX;
112extern MX_GENSHADER_API const string T_PROJ_TRANSPOSE_MATRIX;
113extern MX_GENSHADER_API const string T_PROJ_INVERSE_TRANSPOSE_MATRIX;
114extern MX_GENSHADER_API const string T_WORLD_VIEW_MATRIX;
115extern MX_GENSHADER_API const string T_VIEW_PROJECTION_MATRIX;
116extern MX_GENSHADER_API const string T_WORLD_VIEW_PROJECTION_MATRIX;
117extern MX_GENSHADER_API const string T_VIEW_POSITION;
118extern MX_GENSHADER_API const string T_VIEW_DIRECTION;
119extern MX_GENSHADER_API const string T_FRAME;
120extern MX_GENSHADER_API const string T_TIME;
121extern MX_GENSHADER_API const string T_GEOMPROP;
122extern MX_GENSHADER_API const string T_ALPHA_THRESHOLD;
123extern MX_GENSHADER_API const string T_NUM_ACTIVE_LIGHT_SOURCES;
124extern MX_GENSHADER_API const string T_ENV_MATRIX;
125extern MX_GENSHADER_API const string T_ENV_RADIANCE;
126extern MX_GENSHADER_API const string T_ENV_RADIANCE_MIPS;
127extern MX_GENSHADER_API const string T_ENV_RADIANCE_SAMPLES;
128extern MX_GENSHADER_API const string T_ENV_IRRADIANCE;
129extern MX_GENSHADER_API const string T_ENV_PREFILTER_MIP;
130extern MX_GENSHADER_API const string T_REFRACTION_TWO_SIDED;
131extern MX_GENSHADER_API const string T_ALBEDO_TABLE;
132extern MX_GENSHADER_API const string T_ALBEDO_TABLE_SIZE;
133extern MX_GENSHADER_API const string T_AMB_OCC_MAP;
134extern MX_GENSHADER_API const string T_AMB_OCC_GAIN;
135extern MX_GENSHADER_API const string T_SHADOW_MAP;
136extern MX_GENSHADER_API const string T_SHADOW_MATRIX;
137extern MX_GENSHADER_API const string T_VERTEX_DATA_INSTANCE;
138extern MX_GENSHADER_API const string T_LIGHT_DATA_INSTANCE;
139
142extern MX_GENSHADER_API const string IN_POSITION;
143extern MX_GENSHADER_API const string IN_NORMAL;
144extern MX_GENSHADER_API const string IN_TANGENT;
145extern MX_GENSHADER_API const string IN_BITANGENT;
146extern MX_GENSHADER_API const string IN_TEXCOORD;
147extern MX_GENSHADER_API const string IN_GEOMPROP;
148extern MX_GENSHADER_API const string IN_COLOR;
149extern MX_GENSHADER_API const string POSITION_WORLD;
150extern MX_GENSHADER_API const string NORMAL_WORLD;
151extern MX_GENSHADER_API const string TANGENT_WORLD;
152extern MX_GENSHADER_API const string BITANGENT_WORLD;
153extern MX_GENSHADER_API const string POSITION_OBJECT;
154extern MX_GENSHADER_API const string NORMAL_OBJECT;
155extern MX_GENSHADER_API const string TANGENT_OBJECT;
156extern MX_GENSHADER_API const string BITANGENT_OBJECT;
157extern MX_GENSHADER_API const string TEXCOORD;
158extern MX_GENSHADER_API const string COLOR;
159extern MX_GENSHADER_API const string WORLD_MATRIX;
160extern MX_GENSHADER_API const string WORLD_INVERSE_MATRIX;
161extern MX_GENSHADER_API const string WORLD_TRANSPOSE_MATRIX;
162extern MX_GENSHADER_API const string WORLD_INVERSE_TRANSPOSE_MATRIX;
163extern MX_GENSHADER_API const string VIEW_MATRIX;
164extern MX_GENSHADER_API const string VIEW_INVERSE_MATRIX;
165extern MX_GENSHADER_API const string VIEW_TRANSPOSE_MATRIX;
166extern MX_GENSHADER_API const string VIEW_INVERSE_TRANSPOSE_MATRIX;
167extern MX_GENSHADER_API const string PROJ_MATRIX;
168extern MX_GENSHADER_API const string PROJ_INVERSE_MATRIX;
169extern MX_GENSHADER_API const string PROJ_TRANSPOSE_MATRIX;
170extern MX_GENSHADER_API const string PROJ_INVERSE_TRANSPOSE_MATRIX;
171extern MX_GENSHADER_API const string WORLD_VIEW_MATRIX;
172extern MX_GENSHADER_API const string VIEW_PROJECTION_MATRIX;
173extern MX_GENSHADER_API const string WORLD_VIEW_PROJECTION_MATRIX;
174extern MX_GENSHADER_API const string VIEW_POSITION;
175extern MX_GENSHADER_API const string VIEW_DIRECTION;
176extern MX_GENSHADER_API const string FRAME;
177extern MX_GENSHADER_API const string TIME;
178extern MX_GENSHADER_API const string GEOMPROP;
179extern MX_GENSHADER_API const string ALPHA_THRESHOLD;
180extern MX_GENSHADER_API const string NUM_ACTIVE_LIGHT_SOURCES;
181extern MX_GENSHADER_API const string ENV_MATRIX;
182extern MX_GENSHADER_API const string ENV_RADIANCE;
183extern MX_GENSHADER_API const string ENV_RADIANCE_MIPS;
184extern MX_GENSHADER_API const string ENV_RADIANCE_SAMPLES;
185extern MX_GENSHADER_API const string ENV_IRRADIANCE;
186extern MX_GENSHADER_API const string ENV_PREFILTER_MIP;
187extern MX_GENSHADER_API const string REFRACTION_TWO_SIDED;
188extern MX_GENSHADER_API const string ALBEDO_TABLE;
189extern MX_GENSHADER_API const string ALBEDO_TABLE_SIZE;
190extern MX_GENSHADER_API const string AMB_OCC_MAP;
191extern MX_GENSHADER_API const string AMB_OCC_GAIN;
192extern MX_GENSHADER_API const string SHADOW_MAP;
193extern MX_GENSHADER_API const string SHADOW_MATRIX;
194extern MX_GENSHADER_API const string VERTEX_DATA_INSTANCE;
195extern MX_GENSHADER_API const string LIGHT_DATA_INSTANCE;
196extern MX_GENSHADER_API const string LIGHT_DATA_MAX_LIGHT_SOURCES;
197
199extern MX_GENSHADER_API const string VERTEX_INPUTS; // Geometric inputs for vertex stage.
200extern MX_GENSHADER_API const string VERTEX_DATA; // Connector block for data transfer from vertex stage to pixel stage.
201extern MX_GENSHADER_API const string PRIVATE_UNIFORMS; // Uniform inputs set privately by application.
202extern MX_GENSHADER_API const string PUBLIC_UNIFORMS; // Uniform inputs visible in UI and set by user.
203extern MX_GENSHADER_API const string LIGHT_DATA; // Uniform inputs for light sources.
204extern MX_GENSHADER_API const string PIXEL_OUTPUTS; // Outputs from the main/pixel stage.
205
207extern MX_GENSHADER_API const string DIR_N;
208extern MX_GENSHADER_API const string DIR_L;
209extern MX_GENSHADER_API const string DIR_V;
210extern MX_GENSHADER_API const string WORLD_POSITION;
211extern MX_GENSHADER_API const string OCCLUSION;
212
214extern MX_GENSHADER_API const string ATTR_TRANSPARENT;
215
217extern MX_GENSHADER_API const string USER_DATA_LIGHT_SHADERS;
218extern MX_GENSHADER_API const string USER_DATA_BINDING_CONTEXT;
219} // namespace HW
220
221namespace Stage
222{
224extern MX_GENSHADER_API const string VERTEX;
225} // namespace Stage
226
227class HwLightShaders;
230
232using HwLightShadersPtr = shared_ptr<class HwLightShaders>;
234using HwShaderGeneratorPtr = shared_ptr<class HwShaderGenerator>;
236using HwResourceBindingContextPtr = shared_ptr<class HwResourceBindingContext>;
237
240class MX_GENSHADER_API HwLightShaders : public GenUserData
241{
242 public:
245 {
246 return std::make_shared<HwLightShaders>();
247 }
248
250 void bind(unsigned int type, ShaderNodePtr shader)
251 {
252 _shaders[type] = shader;
253 }
254
256 void unbind(unsigned int type)
257 {
258 _shaders.erase(type);
259 }
260
262 void clear()
263 {
264 _shaders.clear();
265 }
266
269 const ShaderNode* get(unsigned int type) const
270 {
271 auto it = _shaders.find(type);
272 return it != _shaders.end() ? it->second.get() : nullptr;
273 }
274
276 const std::unordered_map<unsigned int, ShaderNodePtr>& get() const
277 {
278 return _shaders;
279 }
280
281 protected:
282 std::unordered_map<unsigned int, ShaderNodePtr> _shaders;
283};
284
287class MX_GENSHADER_API HwShaderGenerator : public ShaderGenerator
288{
289 public:
291 void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const override;
292
294 virtual void addStageLightingUniforms(GenContext& context, ShaderStage& stage) const;
295
297 void getClosureContexts(const ShaderNode& node, vector<ClosureContext*>& cct) const override;
298
303 static void bindLightShader(const NodeDef& nodeDef, unsigned int lightTypeId, GenContext& context);
304
306 static void unbindLightShader(unsigned int lightTypeId, GenContext& context);
307
309 static void unbindLightShaders(GenContext& context);
310
312 virtual string getVertexDataPrefix(const VariableBlock& vertexData) const = 0;
313
316 {
317 DEFAULT,
318 REFLECTION,
319 TRANSMISSION,
320 INDIRECT,
321 EMISSION
322 };
323
326 static const string CLOSURE_CONTEXT_SUFFIX_TRANSMISSION;
327 static const string CLOSURE_CONTEXT_SUFFIX_INDIRECT;
328
329 protected:
331
333 virtual ShaderPtr createShader(const string& name, ElementPtr element, GenContext& context) const;
334
337 mutable ClosureContext _defReflection;
338 mutable ClosureContext _defTransmission;
339 mutable ClosureContext _defIndirect;
340 mutable ClosureContext _defEmission;
341};
342
345class MX_GENSHADER_API HwImplementation : public ShaderNodeImpl
346{
347 public:
348 bool isEditable(const ShaderInput& input) const override;
349
350 protected:
351 HwImplementation() { }
352
353 // Integer identifiers for coordinate spaces.
354 // The order must match the order given for the space enum string in stdlib.
355 enum Space
356 {
357 MODEL_SPACE = 0,
358 OBJECT_SPACE = 1,
359 WORLD_SPACE = 2
360 };
361
363 static const string SPACE;
364 static const string INDEX;
365 static const string GEOMPROP;
366};
367
370class MX_GENSHADER_API HwResourceBindingContext : public GenUserData
371{
372 public:
373 virtual ~HwResourceBindingContext() { }
374
375 // Initialize the context before generation starts.
376 virtual void initialize() = 0;
377
378 // Emit directives required for binding support
379 virtual void emitDirectives(GenContext& context, ShaderStage& stage) = 0;
380
381 // Emit uniforms with binding information
382 virtual void emitResourceBindings(GenContext& context, const VariableBlock& uniforms, ShaderStage& stage) = 0;
383
384 // Emit struct uniforms with binding information
385 virtual void emitStructuredResourceBindings(GenContext& context, const VariableBlock& uniforms,
386 ShaderStage& stage, const std::string& structInstanceName,
387 const std::string& arraySuffix = EMPTY_STRING) = 0;
388};
389
390MATERIALX_NAMESPACE_END
391
392#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:234
shared_ptr< class HwResourceBindingContext > HwResourceBindingContextPtr
Shared pointer to a HwResourceBindingContext.
Definition: HwShaderGenerator.h:236
shared_ptr< class HwLightShaders > HwLightShadersPtr
Shared pointer to a HwLightShaders.
Definition: HwShaderGenerator.h:232
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:346
static const string SPACE
Internal string constants.
Definition: HwShaderGenerator.h:363
Hardware light shader user data.
Definition: HwShaderGenerator.h:241
void bind(unsigned int type, ShaderNodePtr shader)
Bind a light shader to a light type id.
Definition: HwShaderGenerator.h:250
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:269
const std::unordered_map< unsigned int, ShaderNodePtr > & get() const
Return the map of bound light shaders.
Definition: HwShaderGenerator.h:276
static HwLightShadersPtr create()
Create and return a new instance.
Definition: HwShaderGenerator.h:244
void clear()
Clear all light shaders previously bound.
Definition: HwShaderGenerator.h:262
void unbind(unsigned int type)
Unbind a light shader previously bound to a light type id.
Definition: HwShaderGenerator.h:256
Class representing a context for resource binding for hardware resources.
Definition: HwShaderGenerator.h:371
Base class for shader generators targeting HW rendering.
Definition: HwShaderGenerator.h:288
static const string CLOSURE_CONTEXT_SUFFIX_REFLECTION
String constants for closure context suffixes.
Definition: HwShaderGenerator.h:325
ClosureContext _defDefault
Closure contexts for defining closure functions.
Definition: HwShaderGenerator.h:336
ClosureContextType
Types of closure contexts for HW.
Definition: HwShaderGenerator.h:316
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:91
const string T_IN_POSITION
Token identifiers.
Definition: HwShaderGenerator.cpp:36
const string ATTR_TRANSPARENT
Attribute names.
Definition: HwShaderGenerator.cpp:158
const string VERTEX_INPUTS
Variable blocks names.
Definition: HwShaderGenerator.cpp:147
const string DIR_N
Variable names for lighting parameters.
Definition: HwShaderGenerator.cpp:153
const string USER_DATA_LIGHT_SHADERS
User data names.
Definition: HwShaderGenerator.cpp:160