Skip to content

Commit

Permalink
implement new gl uniform system
Browse files Browse the repository at this point in the history
  • Loading branch information
aap committed Feb 19, 2021
1 parent 8b2caf8 commit 276ac82
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 147 deletions.
193 changes: 76 additions & 117 deletions src/gl/gl3device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ struct UniformObject
RGBAf lightColor[MAX_LIGHTS];
};

struct GLShaderState
{
RGBA matColor;
SurfaceProperties surfProps;
float extraSurfProp;
};

const char *shaderDecl120 =
"#version 120\n"
"#define GL2\n"
Expand Down Expand Up @@ -120,16 +113,11 @@ static GLuint whitetex;
static UniformState uniformState;
static UniformScene uniformScene;
static UniformObject uniformObject;
static GLShaderState shaderState;

#ifndef RW_GL_USE_UBOS
// State
int32 u_alphaRef;
int32 u_fogData;
//int32 u_fogStart;
//int32 u_fogEnd;
//int32 u_fogRange;
//int32 u_fogDisable;
int32 u_fogColor;

// Scene
Expand Down Expand Up @@ -405,6 +393,8 @@ getAlphaBlend(void)
return rwStateCache.blendEnable;
}

bool32 getAlphaTest(void) { return rwStateCache.alphaTestEnable; }

static void
setDepthTest(bool32 enable)
{
Expand Down Expand Up @@ -980,6 +970,7 @@ void
setWorldMatrix(Matrix *mat)
{
convMatrix(&uniformObject.world, mat);
setUniform(u_world, &uniformObject.world);
objectDirty = 1;
}

Expand Down Expand Up @@ -1045,6 +1036,12 @@ setLights(WorldLights *lightData)
}

uniformObject.lightParams[n].type = 0.0f;

setUniform(u_ambLight, &uniformObject.ambLight);
setUniform(u_lightParams, uniformObject.lightParams);
setUniform(u_lightPosition, uniformObject.lightPosition);
setUniform(u_lightDirection, uniformObject.lightDirection);
setUniform(u_lightColor, uniformObject.lightColor);
out:
objectDirty = 1;
return bits;
Expand All @@ -1054,13 +1051,15 @@ void
setProjectionMatrix(float32 *mat)
{
memcpy(&uniformScene.proj, mat, 64);
setUniform(u_proj, uniformScene.proj);
sceneDirty = 1;
}

void
setViewMatrix(float32 *mat)
{
memcpy(&uniformScene.view, mat, 64);
setUniform(u_view, uniformScene.view);
sceneDirty = 1;
}

Expand All @@ -1071,27 +1070,16 @@ Shader *lastShaderUploaded;
void
setMaterial(const RGBA &color, const SurfaceProperties &surfaceprops, float extraSurfProp)
{
bool force = lastShaderUploaded != currentShader;
if(force || !equal(shaderState.matColor, color)){
rw::RGBAf col;
convColor(&col, &color);
glUniform4fv(U(u_matColor), 1, (GLfloat*)&col);
shaderState.matColor = color;
}

if(force ||
shaderState.surfProps.ambient != surfaceprops.ambient ||
shaderState.surfProps.specular != surfaceprops.specular ||
shaderState.surfProps.diffuse != surfaceprops.diffuse ||
shaderState.extraSurfProp != extraSurfProp){
float surfProps[4];
surfProps[0] = surfaceprops.ambient;
surfProps[1] = surfaceprops.specular;
surfProps[2] = surfaceprops.diffuse;
surfProps[3] = extraSurfProp;
glUniform4fv(U(u_surfProps), 1, surfProps);
shaderState.surfProps = surfaceprops;
}
rw::RGBAf col;
convColor(&col, &color);
setUniform(u_matColor, &col);

float surfProps[4];
surfProps[0] = surfaceprops.ambient;
surfProps[1] = surfaceprops.specular;
surfProps[2] = surfaceprops.diffuse;
surfProps[3] = extraSurfProp;
setUniform(u_surfProps, surfProps);
}

void
Expand All @@ -1101,80 +1089,54 @@ flushCache(void)

#ifndef RW_GL_USE_UBOS

// TODO: this is probably a stupid way to do it without UBOs
if(lastShaderUploaded != currentShader){
lastShaderUploaded = currentShader;
objectDirty = 1;
sceneDirty = 1;
stateDirty = 1;
// what's this doing here??
uniformState.fogDisable = rwStateCache.fogEnable ? 0.0f : 1.0f;
uniformState.fogStart = rwStateCache.fogStart;
uniformState.fogEnd = rwStateCache.fogEnd;
uniformState.fogRange = 1.0f/(rwStateCache.fogStart - rwStateCache.fogEnd);

int i;
for(i = 0; i < RWGL_NUM_STATES; i++)
uniformStateDirty[i] = true;
if(uniformStateDirty[RWGL_ALPHAFUNC] || uniformStateDirty[RWGL_ALPHAREF]){
float alphaTest[4];
switch(alphaFunc){
case ALPHAALWAYS:
default:
alphaTest[0] = -1000.0f;
alphaTest[1] = 1000.0f;
break;
case ALPHAGREATEREQUAL:
alphaTest[0] = alphaRef;
alphaTest[1] = 1000.0f;
break;
case ALPHALESS:
alphaTest[0] = -1000.0f;
alphaTest[1] = alphaRef;
break;
}
setUniform(u_alphaRef, alphaTest);
uniformStateDirty[RWGL_ALPHAFUNC] = false;
uniformStateDirty[RWGL_ALPHAREF] = false;
}

if(sceneDirty){
glUniformMatrix4fv(U(u_proj), 1, 0, uniformScene.proj);
glUniformMatrix4fv(U(u_view), 1, 0, uniformScene.view);
sceneDirty = 0;
if(uniformStateDirty[RWGL_FOG] ||
uniformStateDirty[RWGL_FOGSTART] ||
uniformStateDirty[RWGL_FOGEND]){
float fogData[4] = {
uniformState.fogStart,
uniformState.fogEnd,
uniformState.fogRange,
uniformState.fogDisable
};
setUniform(u_fogData, fogData);
uniformStateDirty[RWGL_FOG] = false;
uniformStateDirty[RWGL_FOGSTART] = false;
uniformStateDirty[RWGL_FOGEND] = false;
}

if(objectDirty){
glUniformMatrix4fv(U(u_world), 1, 0, (float*)&uniformObject.world);
glUniform4fv(U(u_ambLight), 1, (float*)&uniformObject.ambLight);
glUniform4fv(U(u_lightParams), MAX_LIGHTS, (float*)uniformObject.lightParams);
glUniform4fv(U(u_lightPosition), MAX_LIGHTS, (float*)uniformObject.lightPosition);
glUniform4fv(U(u_lightDirection), MAX_LIGHTS, (float*)uniformObject.lightDirection);
glUniform4fv(U(u_lightColor), MAX_LIGHTS, (float*)uniformObject.lightColor);
objectDirty = 0;
if(uniformStateDirty[RWGL_FOGCOLOR]){
setUniform(u_fogColor, &uniformState.fogColor);
uniformStateDirty[RWGL_FOGCOLOR] = false;
}

// if(stateDirty){

uniformState.fogDisable = rwStateCache.fogEnable ? 0.0f : 1.0f;
uniformState.fogStart = rwStateCache.fogStart;
uniformState.fogEnd = rwStateCache.fogEnd;
uniformState.fogRange = 1.0f/(rwStateCache.fogStart - rwStateCache.fogEnd);

if(uniformStateDirty[RWGL_ALPHAFUNC] || uniformStateDirty[RWGL_ALPHAREF]){
switch(alphaFunc){
case ALPHAALWAYS:
default:
glUniform2f(U(u_alphaRef), -1000.0f, 1000.0f);
break;
case ALPHAGREATEREQUAL:
glUniform2f(U(u_alphaRef), alphaRef, 1000.0f);
break;
case ALPHALESS:
glUniform2f(U(u_alphaRef), -1000.0f, alphaRef);
break;
}
uniformStateDirty[RWGL_ALPHAFUNC] = false;
uniformStateDirty[RWGL_ALPHAREF] = false;
}

if(uniformStateDirty[RWGL_FOG] ||
uniformStateDirty[RWGL_FOGSTART] ||
uniformStateDirty[RWGL_FOGEND]){
float fogData[4] = {
uniformState.fogStart,
uniformState.fogEnd,
uniformState.fogRange,
uniformState.fogDisable
};
glUniform4fv(U(u_fogData), 1, fogData);
uniformStateDirty[RWGL_FOG] = false;
uniformStateDirty[RWGL_FOGSTART] = false;
uniformStateDirty[RWGL_FOGEND] = false;
}

if(uniformStateDirty[RWGL_FOGCOLOR]){
glUniform4fv(U(u_fogColor), 1, (float*)&uniformState.fogColor);
uniformStateDirty[RWGL_FOGCOLOR] = false;
}

// stateDirty = 0;
// }
#else
if(objectDirty){
glBindBuffer(GL_UNIFORM_BUFFER, ubo_object);
Expand Down Expand Up @@ -1214,6 +1176,7 @@ flushCache(void)
stateDirty = 0;
}
#endif
flushUniforms();
}

static void
Expand Down Expand Up @@ -1796,29 +1759,25 @@ initOpenGL(void)
}

#ifndef RW_GL_USE_UBOS
u_alphaRef = registerUniform("u_alphaRef");
u_fogData = registerUniform("u_fogData");
// u_fogStart = registerUniform("u_fogStart");
// u_fogEnd = registerUniform("u_fogEnd");
// u_fogRange = registerUniform("u_fogRange");
// u_fogDisable = registerUniform("u_fogDisable");
u_fogColor = registerUniform("u_fogColor");
u_proj = registerUniform("u_proj");
u_view = registerUniform("u_view");
u_world = registerUniform("u_world");
u_ambLight = registerUniform("u_ambLight");
u_lightParams = registerUniform("u_lightParams");
u_lightPosition = registerUniform("u_lightPosition");
u_lightDirection = registerUniform("u_lightDirection");
u_lightColor = registerUniform("u_lightColor");
u_alphaRef = registerUniform("u_alphaRef", UNIFORM_VEC4);
u_fogData = registerUniform("u_fogData", UNIFORM_VEC4);
u_fogColor = registerUniform("u_fogColor", UNIFORM_VEC4);
u_proj = registerUniform("u_proj", UNIFORM_MAT4);
u_view = registerUniform("u_view", UNIFORM_MAT4);
u_world = registerUniform("u_world", UNIFORM_MAT4);
u_ambLight = registerUniform("u_ambLight", UNIFORM_VEC4);
u_lightParams = registerUniform("u_lightParams", UNIFORM_VEC4, MAX_LIGHTS);
u_lightPosition = registerUniform("u_lightPosition", UNIFORM_VEC4, MAX_LIGHTS);
u_lightDirection = registerUniform("u_lightDirection", UNIFORM_VEC4, MAX_LIGHTS);
u_lightColor = registerUniform("u_lightColor", UNIFORM_VEC4, MAX_LIGHTS);
lastShaderUploaded = nil;
#else
registerBlock("Scene");
registerBlock("Object");
registerBlock("State");
#endif
u_matColor = registerUniform("u_matColor");
u_surfProps = registerUniform("u_surfProps");
u_matColor = registerUniform("u_matColor", UNIFORM_VEC4);
u_surfProps = registerUniform("u_surfProps", UNIFORM_VEC4);

glClearColor(0.25, 0.25, 0.25, 1.0);

Expand Down
5 changes: 3 additions & 2 deletions src/gl/gl3immed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static int primTypeMap[] = {
void
openIm2D(void)
{
u_xform = registerUniform("u_xform");
u_xform = registerUniform("u_xform", UNIFORM_VEC4);

#include "shaders/im2d_gl.inc"
#include "shaders/simple_fs_gl.inc"
Expand Down Expand Up @@ -120,7 +120,8 @@ im2DSetXform(void)
xform[1] = -2.0f/cam->frameBuffer->height;
xform[2] = -1.0f;
xform[3] = 1.0f;
glUniform4fv(currentShader->uniformLocations[u_xform], 1, xform);
setUniform(u_xform, xform);
// glUniform4fv(currentShader->uniformLocations[u_xform], 1, xform);
}

void
Expand Down
19 changes: 9 additions & 10 deletions src/gl/gl3matfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ namespace gl3 {

#ifdef RW_OPENGL

#define U(i) currentShader->uniformLocations[i]

static Shader *envShader;
static int32 u_texMatrix;
static int32 u_fxparams;
Expand Down Expand Up @@ -77,7 +75,7 @@ uploadEnvMatrix(Frame *frame)
invMtx.pos.set(0.0f, 0.0f, 0.0f);
RawMatrix::mult(&envMtx, &invMtx, &normal2texcoord);
}
glUniformMatrix4fv(U(u_texMatrix), 1, GL_FALSE, (float*)&envMtx);
setUniform(u_texMatrix, &envMtx);
}

void
Expand All @@ -99,18 +97,19 @@ matfxEnvRender(InstanceDataHeader *header, InstanceData *inst, uint32 flags, Mat

setMaterial(flags, m->color, m->surfaceProps);

float fxparams[2];
float fxparams[4];
fxparams[0] = env->coefficient;
fxparams[1] = env->fbAlpha ? 0.0f : 1.0f;
fxparams[2] = fxparams[3] = 0.0f;

glUniform2fv(U(u_fxparams), 1, fxparams);
setUniform(u_fxparams, fxparams);
static float zero[4];
static float one[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
// This clamps the vertex color below. With it we can achieve both PC and PS2 style matfx
if(MatFX::modulateEnvMap)
glUniform4fv(U(u_colorClamp), 1, zero);
setUniform(u_colorClamp, zero);
else
glUniform4fv(U(u_colorClamp), 1, one);
setUniform(u_colorClamp, one);

rw::SetRenderState(VERTEXALPHA, 1);
rw::SetRenderState(SRCBLEND, BLENDONE);
Expand Down Expand Up @@ -193,9 +192,9 @@ matfxClose(void *o, int32, int32)
void
initMatFX(void)
{
u_texMatrix = registerUniform("u_texMatrix");
u_fxparams = registerUniform("u_fxparams");
u_colorClamp = registerUniform("u_colorClamp");
u_texMatrix = registerUniform("u_texMatrix", UNIFORM_MAT4);
u_fxparams = registerUniform("u_fxparams", UNIFORM_VEC4);
u_colorClamp = registerUniform("u_colorClamp", UNIFORM_VEC4);

Driver::registerPlugin(PLATFORM_GL3, 0, ID_MATFX,
matfxOpen, matfxClose);
Expand Down
Loading

0 comments on commit 276ac82

Please sign in to comment.