Skylicht Engine
Loading...
Searching...
No Matches
CShader.h
1/*
2!@
3MIT License
4
5Copyright (c) 2019 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "CBaseShaderCallback.h"
28#include "Instancing/IShaderInstancing.h"
29
30namespace Skylicht
31{
32 class CShader;
33
39 {
40 VIEW_PROJECTION,
41 WORLD_VIEW_PROJECTION,
42 VIEW,
43 WORLD,
44 WORLD_INVERSE,
45 WORLD_INVERSE_TRANSPOSE,
46 WORLD_TRANSPOSE,
47 BONE_MATRIX,
48 BONE_COUNT,
49 SHADOW_MAP_MATRIX,
50 SHADOW_MAP_DISTANCE,
51 SHADOW_BIAS,
52 WORLD_CAMERA_POSITION,
53 LIGHT_COLOR,
54 LIGHT_AMBIENT,
55 WORLD_LIGHT_DIRECTION,
56 POINT_LIGHT_COLOR,
57 POINT_LIGHT_POSITION,
58 POINT_LIGHT_ATTENUATION,
59 SPOT_LIGHT_COLOR,
60 SPOT_LIGHT_DIRECTION,
61 SPOT_LIGHT_POSITION,
62 SPOT_LIGHT_ATTENUATION,
63 AREA_LIGHT_POSITION,
64 AREA_LIGHT_DIR_X,
65 AREA_LIGHT_DIR_Y,
66 AREA_LIGHT_SIZE,
67 AREA_LIGHT_COLOR,
68 AREA_LIGHT_SHADOW_MATRIX,
69 MATERIAL_PARAM,
70 DEFAULT_VALUE,
71 SHADER_VEC2,
72 SHADER_VEC3,
73 SHADER_VEC4,
74 SH_CONST,
75 CUSTOM_VALUE,
76 TEXTURE_MIPMAP_COUNT,
77 TEXTURE_WIDTH_HEIGHT,
78 DEFERRED_VIEW,
79 DEFERRED_PROJECTION,
80 DEFERRED_VIEW_PROJECTION,
81 TIME_STEP,
82 PARTICLE_VIEW_UP,
83 PARTICLE_VIEW_LOOK,
84 PARTICLE_ORIENTATION_UP,
85 PARTICLE_ORIENTATION_NORMAL,
86 LIGHTMAP_INDEX,
87 TIME,
88 COLOR_INTENSITY,
89 RENDER_TEXTURE_MATRIX,
90 NUM_SHADER_TYPE,
91 };
92
97 struct SUniform
98 {
100 std::string Name;
101
104
107
109 float Value[16];
110
113
116
119
122
125
127 bool OpenGL;
128
133
136
138 float Min;
139
141 float Max;
142
147 {
148 OpenGL = true;
149 DirectX = true;
150 FloatSize = 1;
151 ArraySize = 1;
152 IsMatrix = false;
153 IsNormal = false;
154
155 ValueIndex = -1;
156 memset(Value, 0, sizeof(float) * 16);
157 UniformShaderID = -1;
158 SizeOfUniform = 0;
159
160 Type = NUM_SHADER_TYPE;
161 Min = -FLT_MAX;
162 Max = FLT_MAX;
163 }
164 };
165
169 class SKYLICHT_API IShaderCallback
170 {
171 public:
172 IShaderCallback()
173 {
174
175 }
176
177 virtual ~IShaderCallback()
178 {
179
180 }
181
189 virtual void OnSetConstants(CShader* shader, SUniform* uniform, IMaterialRenderer* matRender, bool vertexShader) = 0;
190 };
191
197 class SKYLICHT_API CShader : public CBaseShaderCallback
198 {
199 public:
204 {
205 UITexture = 0,
206 UIColor,
207 UIFloat,
208 UIFloat2,
209 UIFloat3,
210 UIFloat4,
211 UIGroup,
212 UINone,
213 UICount
214 };
215
220 {
221 Texture = 0,
222 CubeTexture,
223 ReflectionProbe,
224 ShadowMap,
225 TransformTexture,
226 VertexPositionTexture,
227 VertexNormalTexture,
228 LastFrame,
229 RTT0,
230 RTT1,
231 RTT2,
232 RTT3,
233 RTT4,
234 RTT5,
235 RTT6,
236 RTT7,
237 ResourceCount
238 };
239
245 {
248
250 std::string Name;
251
253 std::vector<std::string> ElementName;
254
256 std::vector<std::string> AutoReplace;
257
259 float Min;
260
262 float Max;
263
266
269
272
275
281 {
282 UniformInfo = NULL;
283 Shader = shader;
284 Parent = NULL;
285 Min = -FLT_MAX;
286 Max = FLT_MAX;
287 ControlType = UINone;
288 }
289
294 {
295 for (u32 i = 0, n = Childs.size(); i < n; i++)
296 {
297 delete Childs[i];
298 }
299 }
300 };
301
306 {
307 std::string Name;
308 std::string Path;
309 EResourceType Type;
310 };
311
315 struct SShader
316 {
318 std::string VertexShader;
319
321 std::string FragmentShader;
322 };
323
327 struct SShaderInstancing
328 {
331
333 std::string ShaderName;
334
337 std::string InstancingVertex;
338
341
344
345 SShaderInstancing()
346 {
348 Instancing = NULL;
349 InstancingShader = NULL;
350 }
351 };
352
357 {
359 std::string UniformName;
360
362 std::string AttributeName;
363 };
364
365 protected:
367 std::string m_name;
368
371
374
377
380
383
385 std::vector<IShaderCallback*> m_callbacks;
386
388 std::vector<std::string> m_dependents;
389
392
395
398
401
404
407
410
413
416
419
422
425
428
431
434
437
440
442 std::string m_source;
443 public:
448
452 virtual ~CShader();
453
457 void clear();
458
465 void initShader(io::IXMLReader* xmlReader, const char* source, const char* shaderFolder);
466
467 protected:
472 void parseUniform(io::IXMLReader* xmlReader);
473
479
485
491 void parseUI(io::IXMLReader* xmlReader, SUniformUI* parent);
492
493 public:
498 inline const std::string& getName()
499 {
500 return m_name;
501 }
502
507 inline const std::string& getSource()
508 {
509 return m_source;
510 }
511
516 inline const std::string& getSoftwareSkinningName()
517 {
519 }
520
524 inline bool isSkinning()
525 {
526 return m_skinning;
527 }
528
533 {
534 return !m_shadowDepthShaderName.empty();
535 }
536
541 {
542 return !m_shadowDistanceShaderName.empty();
543 }
544
550
557
564
571
578
585
590 void setShadowDepthWriteShader(const char* name);
591
596 void setShadowDistanceWriteShader(const char* name);
597
603
609
615 {
616 return (int)m_attributeMapping.size();
617 }
618
625 {
626 return m_attributeMapping[id];
627 }
628
633 inline int getNumResource()
634 {
635 return (int)m_resources.size();
636 }
637
642 inline int getNumVS()
643 {
644 return (int)m_vsUniforms.size();
645 }
646
651 inline int getNumFS()
652 {
653 return (int)m_fsUniforms.size();
654 }
655
661 inline SResource* getResouceInfo(int id)
662 {
663 return m_resources[id];
664 }
665
671 inline SUniform* getVSUniformID(int id)
672 {
673 return &m_listVSUniforms[id];
674 }
675
681 inline SUniform* getFSUniformID(int id)
682 {
683 return &m_listFSUniforms[id];
684 }
685
690 inline int getNumUI()
691 {
692 return (int)m_ui.size();
693 }
694
700 inline SUniformUI* getUniformUI(int id)
701 {
702 return m_ui[id];
703 }
704
709 inline std::vector<std::string>& getDependents()
710 {
711 return m_dependents;
712 }
713
719 SUniformUI* getUniformUIByName(const char* name);
720
727 SUniformUI* getUniformUIByName(const char* name, SUniformUI* group);
728
734 SUniform* getVSUniform(const char* name);
735
741 SUniform* getFSUniform(const char* name);
742
746 inline bool isDeferred()
747 {
748 return m_deferred;
749 }
750
754 inline bool isOpaque()
755 {
756 return m_baseShader == EMT_SOLID;
757 }
758
764 {
765 return m_baseShader;
766 }
767
768 public:
769
776 virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData, bool updateTransform);
777
783 template<class T>
785 {
786 T* newCB = new T();
787 IShaderCallback* shaderCallback = dynamic_cast<IShaderCallback*>(newCB);
788 if (shaderCallback == NULL)
789 {
790 char exceptionInfo[512];
791 sprintf(exceptionInfo, "CShader::addCallback %s must inherit IShaderCallback", typeid(T).name());
792 os::Printer::log(exceptionInfo);
793
794 delete newCB;
795 return NULL;
796 }
797
798 m_callbacks.push_back(newCB);
799 return newCB;
800 }
801
807 template<class T>
809 {
810 for (IShaderCallback* cb : m_callbacks)
811 {
812 if (typeid(T) == typeid(*cb))
813 {
814 return (T*)cb;
815 }
816 }
817
818 return NULL;
819 }
820
825
830
834 inline bool isDrawDepthShadow()
835 {
836 return m_shadow;
837 }
838
839 protected:
846
852 EUniformType getUniformType(const char* name);
853
858 std::string getVSShaderFileName();
859
864 std::string getFSShaderFileName();
865
871
878
887 bool setUniform(SUniform& uniform, IMaterialRenderer* matRender, bool vertexShader, bool updateTransform);
888
893
898 };
899
900}
Represents a programmable shader, including uniforms, UI, resources, instancing and platform-specific...
Definition CShader.h:198
void parseUniform(io::IXMLReader *xmlReader)
Parse uniform definitions from XML.
SShader m_glsl
GLSL shader source.
Definition CShader.h:403
void buildShader()
Build the shader by compiling vertex and fragment sources and registering with the engine.
CShader * getShadowDistanceWriteShader()
Get the custom shadow distance write shader.
int getAttributeMappingCount()
Get the number of attribute mappings for editor integration.
Definition CShader.h:614
std::string getFSShaderFileName()
Get the full path to the fragment/pixel shader source file (GLSL/HLSL).
virtual ~CShader()
Destructor. Releases resources, UI, callbacks and instancing shaders.
core::array< SUniformUI * > m_ui
UI elements.
Definition CShader.h:376
void deleteAllUI()
Delete all UI elements.
SUniform * m_listVSUniforms
Pointer to VS uniforms (m_vsUniforms).
Definition CShader.h:391
void initShader(io::IXMLReader *xmlReader, const char *source, const char *shaderFolder)
Initialize shader from XML definition.
const std::string & getSource()
Get the source file path of the shader.
Definition CShader.h:507
SUniform * getVSUniformID(int id)
Get a vertex shader uniform by index.
Definition CShader.h:671
CShader * getShadowDepthWriteShader()
Get the custom shadow depth write shader.
void parseUI(io::IXMLReader *xmlReader, SUniformUI *parent)
Parse UI group hierarchy from XML.
bool isUniformAvaiable(SUniform &uniform)
Check if a uniform is available on the current graphics API.
CShader * m_shadowDepthShader
Custom shadow depth shader.
Definition CShader.h:433
int getNumVS()
Get the number of vertex shader uniforms.
Definition CShader.h:642
std::string getInstancingVertex(video::E_VERTEX_TYPE vtxType)
Get the vertex type name for instancing for a specific vertex type.
int getNumResource()
Get the number of texture resources bind in shader.
Definition CShader.h:633
CShader * getInstancingShader(video::E_VERTEX_TYPE vtxType)
Get the instancing shader for a specific vertex type.
void buildUIUniform()
Build UI uniform bindings for editor integration.
void parseUniformUI(io::IXMLReader *xmlReader)
Parse UI uniform definitions from XML.
EUIControlType
Enum for UI control types in material editor.
Definition CShader.h:204
core::array< SUniform > m_fsUniforms
Pixel / fragment shader uniforms.
Definition CShader.h:373
const std::string & getName()
Get the name of the shader.
Definition CShader.h:498
bool setUniform(SUniform &uniform, IMaterialRenderer *matRender, bool vertexShader, bool updateTransform)
Try to set a uniform value for rendering.
int m_numFSUniform
Count of FS uniforms.
Definition CShader.h:400
bool isDeferred()
Returns true if this shader is used for deferred rendering.
Definition CShader.h:746
E_MATERIAL_TYPE getBaseMaterial()
Get the base material type of this shader.
Definition CShader.h:763
bool m_skinning
Is shader for skinning.
Definition CShader.h:415
int m_numVSUniform
Count of VS uniforms.
Definition CShader.h:397
CShader * m_softwareSkinningShader
Fallback software skinning shader.
Definition CShader.h:424
std::vector< std::string > & getDependents()
Get the list of dependent shader paths.
Definition CShader.h:709
EUniformType getUniformType(const char *name)
Get the uniform type enum from its name string.
std::string m_name
Shader name.
Definition CShader.h:367
std::string getVSShaderFileName()
Get the full path to the vertex shader source file (GLSL/HLSL).
SUniform * getFSUniformID(int id)
Get a fragment shader uniform by index.
Definition CShader.h:681
E_MATERIAL_TYPE getBaseShaderByName(const char *name)
Get the base shader type by its name string (SOLID, TRANSPARENT, etc.).
CShader()
Constructor. Initializes internal structures and built-in callbacks.
SUniformUI * getUniformUIByName(const char *name, SUniformUI *group)
Get a UI element by uniform name within a UI group.
void setShadowDepthWriteShader(const char *name)
Set the custom shadow depth write shader by name.
void setShadowDistanceWriteShader(const char *name)
Set the custom shadow distance write shader by name.
std::string m_shadowDistanceShaderName
Custom shadow distance shader name for point light.
Definition CShader.h:430
bool isOpaque()
Returns true if this shader is an opaque base shader.
Definition CShader.h:754
bool m_shadow
Is shader for shadow rendering.
Definition CShader.h:418
bool isSupportInstancing(video::E_VERTEX_TYPE vtxType)
Returns true if instancing is supported for the given vertex type.
std::string m_shadowDepthShaderName
Custom shadow depth shader name for directional light.
Definition CShader.h:427
void deleteAllResource()
Delete all shader resources.
T * getCallback()
Get a custom callback of a specific type.
Definition CShader.h:808
IShaderCallback * addCallback()
Add a custom callback to the shader for setting constants.
Definition CShader.h:784
SUniform * m_listFSUniforms
Pointer to FS uniforms (m_fsUniforms).
Definition CShader.h:394
std::string m_source
Source file path.
Definition CShader.h:442
E_MATERIAL_TYPE m_baseShader
Base material type(opaque, transparent, etc.).
Definition CShader.h:409
core::array< SResource * > m_resources
Shader texture resources.
Definition CShader.h:379
SUniformUI * getUniformUI(int id)
Get a UI element by index.
Definition CShader.h:700
virtual void OnSetConstants(video::IMaterialRendererServices *services, s32 userData, bool updateTransform)
Callback to set shader constants (uniforms) for rendering.
SResource * getResouceInfo(int id)
Get a shader resource info by index.
Definition CShader.h:661
bool isCustomShadowDistanceWrite()
Returns true if a custom shadow distance write shader is defined.
Definition CShader.h:540
bool m_deferred
Is shader for deferred rendering.
Definition CShader.h:412
CShader * getSoftwareSkinningShader()
Get the software skinning fallback shader.
bool isDrawDepthShadow()
Returns true if this shader renders depth for shadows.
Definition CShader.h:834
EResourceType
Enum for resource types used in shaders.
Definition CShader.h:220
bool isSkinning()
Returns true if the shader supports skinning.
Definition CShader.h:524
SShaderInstancing * m_shaderInstancing[video::EVT_UNKNOWN+1]
Instancing shader info by vertex type.
Definition CShader.h:439
bool isCustomShadowDepthWrite()
Returns true if a custom shadow depth write shader is defined.
Definition CShader.h:532
IShaderInstancing * getInstancing(video::E_VERTEX_TYPE vtxType)
Get the custom instancing logic for a specific vertex type.
SUniformUI * getUniformUIByName(const char *name)
Get a UI element by uniform name.
std::string m_softwareSkinningShaderName
Fallback software skinning shader name.
Definition CShader.h:421
std::vector< std::string > m_dependents
Dependent shader paths.
Definition CShader.h:388
const std::string & getSoftwareSkinningName()
Get the name of the software skinning fallback shader.
Definition CShader.h:516
core::array< SUniform > m_vsUniforms
Vertex shader uniforms.
Definition CShader.h:370
int getNumUI()
Get the number of UI elements.
Definition CShader.h:690
SShader m_hlsl
HLSL shader source.
Definition CShader.h:406
SAttributeMapping & getAttributeMapping(int id)
Get an attribute mapping by index.
Definition CShader.h:624
std::vector< IShaderCallback * > m_callbacks
Shader callbacks.
Definition CShader.h:385
int getNumFS()
Get the number of fragment shader uniforms.
Definition CShader.h:651
void setInstancing(video::E_VERTEX_TYPE vtxType, IShaderInstancing *instancing)
Set a custom instancing logic for a specific vertex type.
SUniform * getFSUniform(const char *name)
Get a fragment shader uniform by name.
core::array< SAttributeMapping > m_attributeMapping
UI to uniform mapping.
Definition CShader.h:382
void buildUIUniform(SUniformUI *ui)
Recursively build UI uniform bindings for a UI element.
CShader * m_shadowDistanceShader
Custom shadow distance shader.
Definition CShader.h:436
void parseResources(io::IXMLReader *xmlReader)
Parse resource definitions from XML.
SUniform * getVSUniform(const char *name)
Get a vertex shader uniform by name.
void clear()
Clear all internal data (resources, uniforms, UI, etc.).
Interface for shader callbacks, used to set constants in user-extended shaders.
Definition CShader.h:170
virtual void OnSetConstants(CShader *shader, SUniform *uniform, IMaterialRenderer *matRender, bool vertexShader)=0
Called to set uniform constants for the shader.
Abstract base class for GPU instancing logic in Skylicht-Engine.
Definition IShaderInstancing.h:45
Self reallocating template array (like stl vector) with additional features.
Definition irrArray.h:23
Interface for material rendering.
Definition IMaterialRenderer.h:27
Interface providing some methods for changing advanced, internal states of a IVideoDriver.
Definition IMaterialRendererServices.h:21
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
EUniformType
Enum for all supported uniform types in shaders. See documentation page: Shader information structure...
Definition CShader.h:39
IIrrXMLReader< wchar_t, IReferenceCounted > IXMLReader
An xml reader for wide characters, derived from IReferenceCounted.
Definition IXMLReader.h:19
E_VERTEX_TYPE
Enumeration for all vertex types there are.
Definition S3DVertex.h:19
@ EVT_UNKNOWN
Null.
Definition S3DVertex.h:37
E_MATERIAL_TYPE
Abstracted and easy to use fixed function/programmable pipeline material modes.
Definition EMaterialTypes.h:15
@ EMT_SOLID
Standard solid material.
Definition EMaterialTypes.h:19
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58
signed int s32
32 bit signed variable.
Definition irrTypes.h:66
Structure for mapping UI attributes to uniforms.
Definition CShader.h:357
std::string AttributeName
Associated editor attribute name.
Definition CShader.h:362
std::string UniformName
Name of the uniform.
Definition CShader.h:359
Structure describing a shader resource (texture, cube map, etc.).
Definition CShader.h:306
Structure for storing shader source file names.
Definition CShader.h:316
std::string FragmentShader
Fragment / pixel shader file path.
Definition CShader.h:321
std::string VertexShader
Vertex shader file path.
Definition CShader.h:318
Structure for instancing shader information.
Definition CShader.h:328
video::E_VERTEX_TYPE VertexType
Vertex type for instancing.
Definition CShader.h:330
std::string InstancingVertex
Definition CShader.h:337
std::string ShaderName
Name of instancing shader.
Definition CShader.h:333
IShaderInstancing * Instancing
Custom instancing logic.
Definition CShader.h:340
CShader * InstancingShader
Pointer to instancing shader.
Definition CShader.h:343
Structure for describing a UI element for a uniform. Used for editor integration and presentation.
Definition CShader.h:245
SUniform * UniformInfo
Pointer to uniform info.
Definition CShader.h:265
std::vector< std::string > AutoReplace
Auto - replace patterns for textures.
Definition CShader.h:256
std::vector< std::string > ElementName
Element names for vector types.
Definition CShader.h:253
SUniformUI * Parent
Parent UI group.
Definition CShader.h:271
float Max
Maximum value for UI.
Definition CShader.h:262
float Min
Minimum value for UI.
Definition CShader.h:259
core::array< SUniformUI * > Childs
Child UI elements (for groups).
Definition CShader.h:274
CShader * Shader
Pointer to owning shader.
Definition CShader.h:268
SUniformUI(CShader *shader)
Constructor.
Definition CShader.h:280
EUIControlType ControlType
Type of UI control.
Definition CShader.h:247
std::string Name
Uniform name.
Definition CShader.h:250
~SUniformUI()
Destructor. Cleans up child UI elements.
Definition CShader.h:293
Structure describing a shader uniform. Holds name, type, values, binding info and platform specifics.
Definition CShader.h:98
EUniformType Type
Uniform type (see EUniformType).
Definition CShader.h:103
int ArraySize
Array size(for bone matrices etc.).
Definition CShader.h:115
std::string Name
Uniform name (as in shader source).
Definition CShader.h:100
float Value[16]
Default or set values.
Definition CShader.h:109
bool IsNormal
True if uniform is a normal map texture.
Definition CShader.h:121
int UniformShaderID
Internal shader variable ID.
Definition CShader.h:135
float Max
Maximum allowed value(for UI).
Definition CShader.h:141
int ValueIndex
Index for parameter arrays.
Definition CShader.h:106
bool DirectX
Definition CShader.h:132
int FloatSize
Number of floats used.
Definition CShader.h:112
bool IsMatrix
True if uniform is matrix type.
Definition CShader.h:118
float Min
Minimum allowed value(for UI).
Definition CShader.h:138
SUniform()
Default constructor initializing values.
Definition CShader.h:146
int SizeOfUniform
Total size in floats.
Definition CShader.h:124
bool OpenGL
Supported on OpenGL.
Definition CShader.h:127