Skip to content

Commit

Permalink
Fix spot lights for GLES
Browse files Browse the repository at this point in the history
  • Loading branch information
emileb authored and madame-rachelle committed Sep 20, 2021
1 parent e361ff1 commit 76875f0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
31 changes: 20 additions & 11 deletions src/common/rendering/gles/gles_renderstate.cpp
Expand Up @@ -92,11 +92,16 @@ bool FGLRenderState::ApplyShader()
{
static const float nulvec[] = { 0.f, 0.f, 0.f, 0.f };

ShaderFlavourData flavour;

// Need to calc light data now in order to select correct shader
float* lightPtr = NULL;
int modLights = 0;
int subLights = 0;
int addLights = 0;
int totalLights = 0;

flavour.hasSpotLight = false;

if (mLightIndex >= 0)
{
Expand All @@ -119,13 +124,25 @@ bool FGLRenderState::ApplyShader()

if (modLights + subLights + addLights > gles.maxlights)
addLights = gles.maxlights - modLights - subLights;


totalLights = modLights + subLights + addLights;

// Skip passed the first 4 floats so the upload below only contains light data
lightPtr += 4;

float* findSpotsPtr = lightPtr + 11; // The 11th float contains '1' if the light is a spot light, see hw_dynlightdata.cpp

for (int n = 0; n < totalLights; n++)
{
if (*findSpotsPtr > 0) // This is a spot light
{
flavour.hasSpotLight = true;
break;
}
findSpotsPtr += LIGHT_VEC4_NUM * 4;
}
}

ShaderFlavourData flavour;

flavour.textureMode = (mTextureMode == TM_NORMAL && mTempTM == TM_OPAQUE ? TM_OPAQUE : mTextureMode) & 0xff;
if (mTextureClamp && flavour.textureMode == TM_NORMAL) flavour.textureMode = 5; // fixme. Clamp can now be combined with all modes.
Expand Down Expand Up @@ -199,14 +216,10 @@ bool FGLRenderState::ApplyShader()

if (mHwUniforms)
{
//matrixToGL(mHwUniforms->mProjectionMatrix, activeShader->cur->ProjectionMatrix_index);
activeShader->cur->muProjectionMatrix.Set(&mHwUniforms->mProjectionMatrix);
activeShader->cur->muViewMatrix.Set(&mHwUniforms->mViewMatrix);
activeShader->cur->muNormalViewMatrix.Set(&mHwUniforms->mNormalViewMatrix);

//matrixToGL(mHwUniforms->mViewMatrix, activeShader->cur->ViewMatrix_index);
//matrixToGL(mHwUniforms->mNormalViewMatrix, activeShader->cur->NormalViewMatrix_index);

activeShader->cur->muCameraPos.Set(&mHwUniforms->mCameraPos.X);
activeShader->cur->muClipLine.Set(&mHwUniforms->mClipLine.X);

Expand Down Expand Up @@ -318,13 +331,9 @@ bool FGLRenderState::ApplyShader()
// Upload the light data
if (mLightIndex >= 0)
{
int totalLights = modLights + subLights + addLights;

// Calculate the total number of vec4s we need
int totalVectors = totalLights * LIGHT_VEC4_NUM;

// TODO!!! If there are too many lights we need to remove some of the lights and modify the data
// At the moment the shader will just try to read off the end of the array...

if (totalVectors > gles.numlightvectors)
totalVectors = gles.numlightvectors;

Expand Down
31 changes: 17 additions & 14 deletions src/common/rendering/gles/gles_shader.cpp
Expand Up @@ -633,11 +633,11 @@ bool FShader::Load(const char * name, const char * vert_prog_lump_, const char *
char stringbuf[20];
mysnprintf(stringbuf, 20, "texture%d", i);
int tempindex = glGetUniformLocation(shaderData->hShader, stringbuf);
if (tempindex > 0) glUniform1i(tempindex, i - 1);
if (tempindex >= 0) glUniform1i(tempindex, i - 1);
}

int shadowmapindex = glGetUniformLocation(shaderData->hShader, "ShadowMap");
if (shadowmapindex > 0) glUniform1i(shadowmapindex, 16);
if (shadowmapindex >= 0) glUniform1i(shadowmapindex, 16);

glUseProgram(0);

Expand All @@ -654,13 +654,17 @@ bool FShader::Load(const char * name, const char * vert_prog_lump_, const char *

FShader::~FShader()
{
/*
glDeleteProgram(hShader);
if (hVertProg != 0)
glDeleteShader(hVertProg);
if (hFragProg != 0)
glDeleteShader(hFragProg);
*/
std::map<uint32_t, ShaderVariantData*>::iterator it = variants.begin();

while (it != variants.end())
{
glDeleteProgram(it->second->hShader);
if (it->second->hVertProg != 0)
glDeleteShader(it->second->hVertProg);
if (it->second->hFragProg != 0)
glDeleteShader(it->second->hFragProg);
it++;
}
}


Expand Down Expand Up @@ -710,14 +714,13 @@ bool FShader::Bind(ShaderFlavourData& flavour)
variantConfig.AppendFormat("#define DEF_NPOT_EMULATION %d\n", flavour.npotEmulation);
#endif

// Printf("Shader: %s, %08x %s", mFragProg2.GetChars(), tag, variantConfig.GetChars());
variantConfig.AppendFormat("#define DEF_HAS_SPOTLIGHT %d\n", flavour.hasSpotLight);

//Printf("Shader: %s, %08x %s", mFragProg2.GetChars(), tag, variantConfig.GetChars());

Load(mName.GetChars(), mVertProg, mFragProg, mFragProg2, mLightProg, mDefinesBase + variantConfig);

if (variants.insert(std::make_pair(tag, cur)).second == false)
{
Printf("ERROR INSERTING");
}
variants.insert(std::make_pair(tag, cur));
}
else
{
Expand Down
5 changes: 5 additions & 0 deletions src/common/rendering/gles/gles_shader.h
Expand Up @@ -275,6 +275,8 @@ class ShaderFlavourData
#ifdef NPOT_EMULATION
bool npotEmulation;
#endif

bool hasSpotLight;
};

class FShader
Expand Down Expand Up @@ -417,6 +419,9 @@ public: class ShaderVariantData
#ifdef NPOT_EMULATION
tag |= (flavour.npotEmulation & 1) << 22;
#endif

tag |= (flavour.hasSpotLight & 1) << 23;

return tag;
}

Expand Down
3 changes: 0 additions & 3 deletions wadsrc/static/shaders_gles/glsl/main.vp
Expand Up @@ -127,8 +127,5 @@ void main()
ClipDistanceA = vec4(ClipDistance0, ClipDistance1, ClipDistance2, ClipDistance3);
ClipDistanceB = vec4(ClipDistance4, 0.0, 0.0, 0.0);


//gl_PointSize = 1.0;

gl_Position = ProjectionMatrix * eyeCoordPos;
}
10 changes: 8 additions & 2 deletions wadsrc/static/shaders_gles/glsl/material_normal.fp
Expand Up @@ -19,7 +19,13 @@ vec3 lightContribution(int i, vec3 normal)
float attenuation = clamp((lightpos.w - lightdistance) / lightpos.w, 0.0, 1.0);


// NOTE, all spot light stuff gone
#if (DEF_HAS_SPOTLIGHT == 1) // Only perform test below if there are ANY spot lights on this surface.

if (lightspot1.w == 1.0)
attenuation *= spotLightAttenuation(lightpos, lightspot1.xyz, lightspot2.x, lightspot2.y);

#endif

return lightcolor.rgb * attenuation;

/*
Expand Down Expand Up @@ -55,7 +61,7 @@ vec3 ProcessMaterialLight(Material material, vec3 color)
// modulated lights

// Some very old GLES2 hardward does not allow non-constants in a for-loop expression because it can not unroll it.
// However they do allow 'break' and use stupid hack
// However they do allow 'break', so use stupid hack
#if (USE_GLSL_V100 == 1)

for(int i = 0; i < 8; i++) // Max 8 lights
Expand Down

0 comments on commit 76875f0

Please sign in to comment.