MaterialX 1.39.3
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 column 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 CLOSURE_DATA_TYPE;
211extern MX_GENSHADER_API const string CLOSURE_DATA_ARG;
212extern MX_GENSHADER_API const string DIR_N;
213extern MX_GENSHADER_API const string DIR_L;
214extern MX_GENSHADER_API const string DIR_V;
215extern MX_GENSHADER_API const string WORLD_POSITION;
216extern MX_GENSHADER_API const string OCCLUSION;
217
219extern MX_GENSHADER_API const string ATTR_TRANSPARENT;
220
222extern MX_GENSHADER_API const string USER_DATA_LIGHT_SHADERS;
223extern MX_GENSHADER_API const string USER_DATA_BINDING_CONTEXT;
224
226extern MX_GENSHADER_API const TypeDesc ClosureDataType;
227} // namespace HW
228
229namespace Stage
230{
232extern MX_GENSHADER_API const string VERTEX;
233} // namespace Stage
234
235class HwLightShaders;
238
240using HwLightShadersPtr = shared_ptr<class HwLightShaders>;
242using HwShaderGeneratorPtr = shared_ptr<class HwShaderGenerator>;
244using HwResourceBindingContextPtr = shared_ptr<class HwResourceBindingContext>;
245
248class MX_GENSHADER_API HwLightShaders : public GenUserData
249{
250 public:
253 {
254 return std::make_shared<HwLightShaders>();
255 }
256
258 void bind(unsigned int type, ShaderNodePtr shader)
259 {
260 _shaders[type] = shader;
261 }
262
264 void unbind(unsigned int type)
265 {
266 _shaders.erase(type);
267 }
268
270 void clear()
271 {
272 _shaders.clear();
273 }
274
277 const ShaderNode* get(unsigned int type) const
278 {
279 auto it = _shaders.find(type);
280 return it != _shaders.end() ? it->second.get() : nullptr;
281 }
282
284 const std::unordered_map<unsigned int, ShaderNodePtr>& get() const
285 {
286 return _shaders;
287 }
288
289 protected:
290 std::unordered_map<unsigned int, ShaderNodePtr> _shaders;
291};
292
295class MX_GENSHADER_API HwShaderGenerator : public ShaderGenerator
296{
297 public:
299 virtual void addStageLightingUniforms(GenContext& context, ShaderStage& stage) const;
300
302 bool nodeNeedsClosureData(const ShaderNode& node) const override;
303
308 static void bindLightShader(const NodeDef& nodeDef, unsigned int lightTypeId, GenContext& context);
309
311 static void unbindLightShader(unsigned int lightTypeId, GenContext& context);
312
314 static void unbindLightShaders(GenContext& context);
315
317 virtual string getVertexDataPrefix(const VariableBlock& vertexData) const = 0;
318
319 // Note : the order must match the order defined in libraries/pbrlib/genglsl/lib/mx_closure_type.glsl
320 // TODO : investigate build time mechanism for ensuring these stay in sync.
321
324 {
325 DEFAULT,
326 REFLECTION,
327 TRANSMISSION,
328 INDIRECT,
329 EMISSION,
330 LIGHTING,
331 CLOSURE
332 };
333
334 protected:
335 HwShaderGenerator(TypeSystemPtr typeSystem, SyntaxPtr syntax);
336
338 virtual ShaderPtr createShader(const string& name, ElementPtr element, GenContext& context) const;
339};
340
343class MX_GENSHADER_API HwImplementation : public ShaderNodeImpl
344{
345 public:
346 bool isEditable(const ShaderInput& input) const override;
347
348 protected:
349 HwImplementation() { }
350
351 // Integer identifiers for coordinate spaces.
352 // The order must match the order given for the space enum string in stdlib.
353 enum Space
354 {
355 MODEL_SPACE = 0,
356 OBJECT_SPACE = 1,
357 WORLD_SPACE = 2
358 };
359
361 static const string SPACE;
362 static const string INDEX;
363 static const string GEOMPROP;
364};
365
368class MX_GENSHADER_API HwResourceBindingContext : public GenUserData
369{
370 public:
371 virtual ~HwResourceBindingContext() { }
372
373 // Initialize the context before generation starts.
374 virtual void initialize() = 0;
375
376 // Emit directives required for binding support
377 virtual void emitDirectives(GenContext& context, ShaderStage& stage) = 0;
378
379 // Emit uniforms with binding information
380 virtual void emitResourceBindings(GenContext& context, const VariableBlock& uniforms, ShaderStage& stage) = 0;
381
382 // Emit struct uniforms with binding information
383 virtual void emitStructuredResourceBindings(GenContext& context, const VariableBlock& uniforms,
384 ShaderStage& stage, const std::string& structInstanceName,
385 const std::string& arraySuffix = EMPTY_STRING) = 0;
386};
387
388MATERIALX_NAMESPACE_END
389
390#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:242
shared_ptr< class HwResourceBindingContext > HwResourceBindingContextPtr
Shared pointer to a HwResourceBindingContext.
Definition HwShaderGenerator.h:244
shared_ptr< class HwLightShaders > HwLightShadersPtr
Shared pointer to a HwLightShaders.
Definition HwShaderGenerator.h:240
Macros for declaring imported and exported symbols.
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition Library.h:33
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:28
A context class for shader generation.
Definition GenContext.h:30
bool isEditable(const ShaderInput &input) const override
Returns true if an input is editable by users.
static const string SPACE
Internal string constants.
Definition HwShaderGenerator.h:361
Hardware light shader user data.
Definition HwShaderGenerator.h:249
void bind(unsigned int type, ShaderNodePtr shader)
Bind a light shader to a light type id.
Definition HwShaderGenerator.h:258
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:277
const std::unordered_map< unsigned int, ShaderNodePtr > & get() const
Return the map of bound light shaders.
Definition HwShaderGenerator.h:284
static HwLightShadersPtr create()
Create and return a new instance.
Definition HwShaderGenerator.h:252
void clear()
Clear all light shaders previously bound.
Definition HwShaderGenerator.h:270
void unbind(unsigned int type)
Unbind a light shader previously bound to a light type id.
Definition HwShaderGenerator.h:264
Class representing a context for resource binding for hardware resources.
Definition HwShaderGenerator.h:369
Base class for shader generators targeting HW rendering.
Definition HwShaderGenerator.h:296
static void unbindLightShader(unsigned int lightTypeId, GenContext &context)
Unbind a light shader previously bound to the given light type id.
virtual ShaderPtr createShader(const string &name, ElementPtr element, GenContext &context) const
Create and initialize a new HW shader for shader generation.
virtual void addStageLightingUniforms(GenContext &context, ShaderStage &stage) const
Emit code for active light count definitions and uniforms.
static void bindLightShader(const NodeDef &nodeDef, unsigned int lightTypeId, GenContext &context)
Bind a light shader to a light type id, for usage in surface shaders created by the generator.
static void unbindLightShaders(GenContext &context)
Unbind all light shaders previously bound.
bool nodeNeedsClosureData(const ShaderNode &node) const override
Return true if the node needs the ClosureData struct added.
ClosureContextType
Types of closure contexts for HW.
Definition HwShaderGenerator.h:324
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
ShaderGenerator(TypeSystemPtr typeSystem, SyntaxPtr syntax)
Protected constructor.
An input on a ShaderNode.
Definition ShaderNode.h:264
Class representing a node in the shader generation DAG.
Definition ShaderNode.h:320
ShaderNodeImpl()
Protected constructor.
A shader stage, containing the state and resulting source code for the stage.
Definition ShaderStage.h:139
A type descriptor for MaterialX data types.
Definition TypeDesc.h:40
A block of variables in a shader stage.
Definition ShaderStage.h:61
HW specific identifiers.
Definition HwShaderGenerator.h:84
MX_GENSHADER_API const string CLOSURE_DATA_TYPE
Variable names for lighting parameters.
MX_GENSHADER_API const string IN_POSITION
Default names for identifiers.
MX_GENSHADER_API const TypeDesc ClosureDataType
Type Descriptor for closure context data.
MX_GENSHADER_API const string T_IN_POSITION
Token identifiers.
MX_GENSHADER_API const string ATTR_TRANSPARENT
Attribute names.
MX_GENSHADER_API const string VERTEX_INPUTS
Variable blocks names.
MX_GENSHADER_API const string USER_DATA_LIGHT_SHADERS
User data names.