Skip to content

Commit 2da60d4

Browse files
committed
- added renderer shader constant lookup
1 parent 76c99c4 commit 2da60d4

2 files changed

Lines changed: 101 additions & 2 deletions

File tree

DriverLevelTool/viewer/gl_renderer.cpp

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ GLint u_View; // view transform in the world
3838
GLint u_Projection; // projection
3939
GLint u_WorldViewProj;
4040

41+
void* s_uniformFuncs[CONSTANT_TYPE_COUNT] = {};
42+
4143
TextureID g_whiteTexture;
4244

4345
static const GLenum glPrimitiveType[] = {
@@ -171,6 +173,22 @@ int GR_InitGL()
171173
const char* glslVersionStr = (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
172174
Msg("*GLSL version: %s\n", glslVersionStr);
173175

176+
s_uniformFuncs[CONSTANT_FLOAT] = (void*)glUniform1fv;
177+
s_uniformFuncs[CONSTANT_VECTOR2D] = (void*)glUniform2fv;
178+
s_uniformFuncs[CONSTANT_VECTOR3D] = (void*)glUniform3fv;
179+
s_uniformFuncs[CONSTANT_VECTOR4D] = (void*)glUniform4fv;
180+
s_uniformFuncs[CONSTANT_INT] = (void*)glUniform1iv;
181+
s_uniformFuncs[CONSTANT_IVECTOR2D] = (void*)glUniform2iv;
182+
s_uniformFuncs[CONSTANT_IVECTOR3D] = (void*)glUniform3iv;
183+
s_uniformFuncs[CONSTANT_IVECTOR4D] = (void*)glUniform4iv;
184+
s_uniformFuncs[CONSTANT_BOOL] = (void*)glUniform1iv;
185+
s_uniformFuncs[CONSTANT_BVECTOR2D] = (void*)glUniform2iv;
186+
s_uniformFuncs[CONSTANT_BVECTOR3D] = (void*)glUniform3iv;
187+
s_uniformFuncs[CONSTANT_BVECTOR4D] = (void*)glUniform4iv;
188+
s_uniformFuncs[CONSTANT_MATRIX2x2] = (void*)glUniformMatrix2fv;
189+
s_uniformFuncs[CONSTANT_MATRIX3x3] = (void*)glUniformMatrix3fv;
190+
s_uniformFuncs[CONSTANT_MATRIX4x4] = (void*)glUniformMatrix4fv;
191+
174192
return 1;
175193
}
176194

@@ -258,14 +276,18 @@ ShaderID GR_CompileShader(const char* source)
258276
"#define VERTEX\n"
259277
"#define varying out\n"
260278
"#define attribute in\n"
261-
"#define texture2D texture\n";
279+
"#define texture2D texture\n"
280+
"#define saturate(x) clamp(x,0.0,1.0)\r\n"
281+
"#define lerp mix\r\n";
262282

263283
const char* GLSL_HEADER_FRAG =
264284
"#version 300 es\n"
265285
"precision lowp int;\n"
266286
"precision highp float;\n"
267287
"#define varying in\n"
268288
"#define texture2D texture\n"
289+
"#define saturate(x) clamp(x,0.0,1.0)\r\n"
290+
"#define lerp mix\r\n";
269291
"out vec4 fragColor;\n";
270292
#else
271293
const char* GLSL_HEADER_VERT =
@@ -275,14 +297,18 @@ ShaderID GR_CompileShader(const char* source)
275297
"#define VERTEX\n"
276298
"#define varying out\n"
277299
"#define attribute in\n"
278-
"#define texture2D texture\n";
300+
"#define texture2D texture\n"
301+
"#define saturate(x) clamp(x,0.0,1.0)\r\n"
302+
"#define lerp mix\r\n";
279303

280304
const char* GLSL_HEADER_FRAG =
281305
"#version 330\n"
282306
"precision lowp int;\n"
283307
"precision highp float;\n"
284308
"#define varying in\n"
285309
"#define texture2D texture\n"
310+
"#define saturate(x) clamp(x,0.0,1.0)\r\n"
311+
"#define lerp mix\r\n"
286312
"out vec4 fragColor;\n";
287313
#endif
288314

@@ -336,6 +362,47 @@ void GR_SetShader(const ShaderID& shader)
336362
GR_GetWVPUniforms(shader);
337363
}
338364

365+
typedef GLvoid(APIENTRY* UNIFORM_FUNC)(GLint location, GLsizei count, const void* value);
366+
typedef GLvoid(APIENTRY* UNIFORM_MAT_FUNC)(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
367+
368+
int GR_GetShaderConstantIndex(ShaderID shaderId, char* name)
369+
{
370+
return glGetUniformLocation(shaderId, name);
371+
}
372+
373+
void GR_SetShaderConstatntvi(int index, GR_ConstantType constantType, int count, float* value)
374+
{
375+
if (constantType >= CONSTANT_MATRIX2x2)
376+
((UNIFORM_MAT_FUNC)s_uniformFuncs[constantType])(index, count, GL_TRUE, value);
377+
else
378+
((UNIFORM_FUNC)s_uniformFuncs[constantType])(index, count, value);
379+
}
380+
381+
void GR_SetShaderConstatntFloat(int index, float value)
382+
{
383+
GR_SetShaderConstatntvi(index, CONSTANT_FLOAT, 1, &value);
384+
}
385+
386+
void GR_SetShaderConstatntVector3D(int index, const Vector3D& value)
387+
{
388+
GR_SetShaderConstatntvi(index, CONSTANT_VECTOR3D, 1, (float*)&value);
389+
}
390+
391+
void GR_SetShaderConstatntVector4D(int index, const Vector4D& value)
392+
{
393+
GR_SetShaderConstatntvi(index, CONSTANT_VECTOR4D, 1, (float*)&value);
394+
}
395+
396+
void GR_SetShaderConstatntMatrix3x3(int index, const Matrix3x3& value)
397+
{
398+
GR_SetShaderConstatntvi(index, CONSTANT_MATRIX3x3, 1, (float*)&value);
399+
}
400+
401+
void GR_SetShaderConstatntMatrix4x4(int index, const Matrix4x4& value)
402+
{
403+
GR_SetShaderConstatntvi(index, CONSTANT_MATRIX4x4, 1, (float*)&value);
404+
}
405+
339406
//----------------------------------------------------------------
340407

341408
void GR_SetMatrix(GR_MatrixMode mode, const Matrix4x4& matrix)

DriverLevelTool/viewer/gl_renderer.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ enum GR_PrimitiveType
6666
PRIM_POINTS,
6767
};
6868

69+
// Reserved, used for shaders only
70+
enum GR_ConstantType
71+
{
72+
CONSTANT_FLOAT,
73+
CONSTANT_VECTOR2D,
74+
CONSTANT_VECTOR3D,
75+
CONSTANT_VECTOR4D,
76+
CONSTANT_INT,
77+
CONSTANT_IVECTOR2D,
78+
CONSTANT_IVECTOR3D,
79+
CONSTANT_IVECTOR4D,
80+
CONSTANT_BOOL,
81+
CONSTANT_BVECTOR2D,
82+
CONSTANT_BVECTOR3D,
83+
CONSTANT_BVECTOR4D,
84+
CONSTANT_MATRIX2x2,
85+
CONSTANT_MATRIX3x3,
86+
CONSTANT_MATRIX4x4,
87+
88+
CONSTANT_TYPE_COUNT
89+
};
90+
6991
int GR_Init(char* windowName, int width, int height, int fullscreen);
7092
void GR_Shutdown();
7193

@@ -98,6 +120,16 @@ void GR_DestroyVAO(GrVAO* vaoPtr);
98120
//--------------------------------------------------
99121

100122
void GR_SetShader(const ShaderID& shader);
123+
int GR_GetShaderConstantIndex(ShaderID shaderId, char* name);
124+
125+
void GR_SetShaderConstatntvi(int index, GR_ConstantType constantType, int count, float* value);
126+
127+
void GR_SetShaderConstatntFloat(int index, float value);
128+
void GR_SetShaderConstatntVector3D(int index, const Vector3D& value);
129+
void GR_SetShaderConstatntVector4D(int index, const Vector4D& value);
130+
void GR_SetShaderConstatntMatrix3x3(int index, const Matrix3x3& value);
131+
void GR_SetShaderConstatntMatrix4x4(int index, const Matrix4x4& value);
132+
101133
void GR_SetVAO(GrVAO* vaoPtr);
102134
void GR_SetTexture(TextureID texture);
103135
void GR_SetMatrix(GR_MatrixMode mode, const Matrix4x4& matrix);

0 commit comments

Comments
 (0)