Skip to content

Commit

Permalink
Make some uniforms global
Browse files Browse the repository at this point in the history
  • Loading branch information
VReaperV committed Sep 25, 2024
1 parent ec3018b commit c28370b
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 26 deletions.
104 changes: 103 additions & 1 deletion src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,8 @@ void BindShaderLightMapping( Material* material ) {
gl_lightMappingShaderMaterial->SetGridDeluxeMapping( material->enableGridDeluxeMapping );
gl_lightMappingShaderMaterial->SetHeightMapInNormalMap( material->hasHeightMapInNormalMap );
gl_lightMappingShaderMaterial->SetReliefMapping( material->enableReliefMapping );
gl_lightMappingShaderMaterial->SetReflectiveSpecular( material->enableNormalMapping && tr.cubeHashTable != nullptr );
gl_lightMappingShaderMaterial->SetReflectiveSpecular( material->enableNormalMapping && tr.cubeHashTable != nullptr
&& r_reflectionMapping->integer );
gl_lightMappingShaderMaterial->SetPhysicalShading( material->enablePhysicalMapping );

// Bind shader program.
Expand All @@ -1104,6 +1105,107 @@ void BindShaderLightMapping( Material* material ) {
}
// FIXME: else

// bind u_LightGrid1
if ( material->enableGridLighting ) {
gl_lightMappingShaderMaterial->SetUniform_LightGrid1Bindless( GL_BindToTMU( BIND_LIGHTMAP, tr.lightGrid1Image ) );
}

// bind u_LightGrid2
if ( material->enableGridDeluxeMapping ) {
gl_lightMappingShaderMaterial->SetUniform_LightGrid2Bindless( GL_BindToTMU( BIND_DELUXEMAP, tr.lightGrid2Image ) );
}

if ( glConfig2.realtimeLighting ) {
gl_lightMappingShaderMaterial->SetUniformBlock_Lights( tr.dlightUBO );

// bind u_LightTilesInt
if ( r_realtimeLightingRenderer.Get() == Util::ordinal( realtimeLightingRenderer_t::TILED ) ) {
gl_lightMappingShaderMaterial->SetUniform_LightTilesIntBindless(
GL_BindToTMU( BIND_LIGHTTILES, tr.lighttileRenderImage )
);
}
}

if ( material->enableNormalMapping && tr.cubeHashTable != nullptr && r_reflectionMapping->integer ) {
cubemapProbe_t* cubeProbeNearest;
cubemapProbe_t* cubeProbeSecondNearest;

image_t* cubeMap0 = nullptr;
image_t* cubeMap1 = nullptr;

float interpolation = 0.0;

bool isWorldEntity = backEnd.currentEntity == &tr.worldEntity;

if ( backEnd.currentEntity && !isWorldEntity ) {
R_FindTwoNearestCubeMaps( backEnd.currentEntity->e.origin, &cubeProbeNearest, &cubeProbeSecondNearest );
} else {
// FIXME position
R_FindTwoNearestCubeMaps( backEnd.viewParms.orientation.origin, &cubeProbeNearest, &cubeProbeSecondNearest );
}

if ( cubeProbeNearest == nullptr && cubeProbeSecondNearest == nullptr ) {
GLimp_LogComment( "cubeProbeNearest && cubeProbeSecondNearest == NULL\n" );

cubeMap0 = tr.whiteCubeImage;
cubeMap1 = tr.whiteCubeImage;
} else if ( cubeProbeNearest == nullptr ) {
GLimp_LogComment( "cubeProbeNearest == NULL\n" );

cubeMap0 = cubeProbeSecondNearest->cubemap;
} else if ( cubeProbeSecondNearest == nullptr ) {
GLimp_LogComment( "cubeProbeSecondNearest == NULL\n" );

cubeMap0 = cubeProbeNearest->cubemap;
} else {
float cubeProbeNearestDistance, cubeProbeSecondNearestDistance;

if ( backEnd.currentEntity && !isWorldEntity ) {
cubeProbeNearestDistance = Distance( backEnd.currentEntity->e.origin, cubeProbeNearest->origin );
cubeProbeSecondNearestDistance = Distance( backEnd.currentEntity->e.origin, cubeProbeSecondNearest->origin );
} else {
// FIXME position
cubeProbeNearestDistance = Distance( backEnd.viewParms.orientation.origin, cubeProbeNearest->origin );
cubeProbeSecondNearestDistance = Distance( backEnd.viewParms.orientation.origin, cubeProbeSecondNearest->origin );
}

interpolation = cubeProbeNearestDistance / ( cubeProbeNearestDistance + cubeProbeSecondNearestDistance );

if ( r_logFile->integer ) {
GLimp_LogComment( va( "cubeProbeNearestDistance = %f, cubeProbeSecondNearestDistance = %f, interpolation = %f\n",
cubeProbeNearestDistance, cubeProbeSecondNearestDistance, interpolation ) );
}

cubeMap0 = cubeProbeNearest->cubemap;
cubeMap1 = cubeProbeSecondNearest->cubemap;
}

/* TODO: Check why it is required to test for this, why
cubeProbeNearest->cubemap and cubeProbeSecondNearest->cubemap
can be nullptr while cubeProbeNearest and cubeProbeSecondNearest
are not. Maybe this is only required while cubemaps are building. */
if ( cubeMap0 == nullptr ) {
cubeMap0 = tr.whiteCubeImage;
}

if ( cubeMap1 == nullptr ) {
cubeMap1 = tr.whiteCubeImage;
}

// bind u_EnvironmentMap0
gl_lightMappingShaderMaterial->SetUniform_EnvironmentMap0Bindless(
GL_BindToTMU( BIND_ENVIRONMENTMAP0, cubeMap0 )
);

// bind u_EnvironmentMap1
gl_lightMappingShaderMaterial->SetUniform_EnvironmentMap1Bindless(
GL_BindToTMU( BIND_ENVIRONMENTMAP1, cubeMap1 )
);

// bind u_EnvironmentInterpolation
gl_lightMappingShaderMaterial->SetUniform_EnvironmentInterpolation( interpolation );
}

gl_lightMappingShaderMaterial->SetUniform_ViewOrigin( backEnd.orientation.viewOrigin );
gl_lightMappingShaderMaterial->SetUniform_numLights( backEnd.refdef.numLights );
gl_lightMappingShaderMaterial->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
Expand Down
36 changes: 18 additions & 18 deletions src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,9 @@ class GLUniform

class GLUniformSampler : protected GLUniform {
protected:
GLUniformSampler( GLShader* shader, const char* name, const char* type, const GLuint size ) :
GLUniformSampler( GLShader* shader, const char* name, const char* type, const GLuint size, const bool global = false ) :
GLUniform( shader, name, type, glConfig2.bindlessTexturesAvailable ? size * 2 : size,
glConfig2.bindlessTexturesAvailable ? size * 2 : size, false, 0, true ) {
glConfig2.bindlessTexturesAvailable ? size * 2 : size, global, 0, true ) {
}

inline GLint GetLocation() {
Expand Down Expand Up @@ -588,8 +588,8 @@ class GLUniformSampler2D : protected GLUniformSampler {

class GLUniformSampler3D : protected GLUniformSampler {
protected:
GLUniformSampler3D( GLShader* shader, const char* name ) :
GLUniformSampler( shader, name, "sampler3D", 1 ) {
GLUniformSampler3D( GLShader* shader, const char* name, const bool global = false ) :
GLUniformSampler( shader, name, "sampler3D", 1, global ) {
}

inline GLint GetLocation() {
Expand All @@ -610,8 +610,8 @@ class GLUniformSampler3D : protected GLUniformSampler {

class GLUniformUSampler3D : protected GLUniformSampler {
protected:
GLUniformUSampler3D( GLShader* shader, const char* name ) :
GLUniformSampler( shader, name, "usampler3D", 1 ) {
GLUniformUSampler3D( GLShader* shader, const char* name, const bool global = false ) :
GLUniformSampler( shader, name, "usampler3D", 1, global ) {
}

inline GLint GetLocation() {
Expand All @@ -632,8 +632,8 @@ class GLUniformUSampler3D : protected GLUniformSampler {

class GLUniformSamplerCube : protected GLUniformSampler {
protected:
GLUniformSamplerCube( GLShader* shader, const char* name ) :
GLUniformSampler( shader, name, "samplerCube", 1 ) {
GLUniformSamplerCube( GLShader* shader, const char* name, const bool global = false ) :
GLUniformSampler( shader, name, "samplerCube", 1, global ) {
}

inline GLint GetLocation() {
Expand Down Expand Up @@ -813,8 +813,8 @@ class GLUniform1Bool : protected GLUniform {
class GLUniform1f : protected GLUniform
{
protected:
GLUniform1f( GLShader *shader, const char *name ) :
GLUniform( shader, name, "float", 1, 1, false )
GLUniform1f( GLShader *shader, const char *name, const bool global = false ) :
GLUniform( shader, name, "float", 1, 1, global )
{
}

Expand Down Expand Up @@ -2449,7 +2449,7 @@ class u_LightTilesInt :
GLUniformUSampler3D {
public:
u_LightTilesInt( GLShader* shader ) :
GLUniformUSampler3D( shader, "u_LightTilesInt" ) {
GLUniformUSampler3D( shader, "u_LightTilesInt", true ) {
}

void SetUniform_LightTilesIntBindless( GLuint64 bindlessHandle ) {
Expand All @@ -2465,7 +2465,7 @@ class u_LightGrid1 :
GLUniformSampler3D {
public:
u_LightGrid1( GLShader* shader ) :
GLUniformSampler3D( shader, "u_LightGrid1" ) {
GLUniformSampler3D( shader, "u_LightGrid1", true ) {
}

void SetUniform_LightGrid1Bindless( GLuint64 bindlessHandle ) {
Expand All @@ -2481,7 +2481,7 @@ class u_LightGrid2 :
GLUniformSampler3D {
public:
u_LightGrid2( GLShader* shader ) :
GLUniformSampler3D( shader, "u_LightGrid2" ) {
GLUniformSampler3D( shader, "u_LightGrid2", true ) {
}

void SetUniform_LightGrid2Bindless( GLuint64 bindlessHandle ) {
Expand All @@ -2497,7 +2497,7 @@ class u_EnvironmentMap0 :
GLUniformSamplerCube {
public:
u_EnvironmentMap0( GLShader* shader ) :
GLUniformSamplerCube( shader, "u_EnvironmentMap0" ) {
GLUniformSamplerCube( shader, "u_EnvironmentMap0", true ) {
}

void SetUniform_EnvironmentMap0Bindless( GLuint64 bindlessHandle ) {
Expand All @@ -2513,7 +2513,7 @@ class u_EnvironmentMap1 :
GLUniformSamplerCube {
public:
u_EnvironmentMap1( GLShader* shader ) :
GLUniformSamplerCube( shader, "u_EnvironmentMap1" ) {
GLUniformSamplerCube( shader, "u_EnvironmentMap1", true ) {
}

void SetUniform_EnvironmentMap1Bindless( GLuint64 bindlessHandle ) {
Expand Down Expand Up @@ -3496,7 +3496,7 @@ class u_VertexInterpolation :
{
public:
u_VertexInterpolation( GLShader *shader ) :
GLUniform1f( shader, "u_VertexInterpolation" )
GLUniform1f( shader, "u_VertexInterpolation", true )
{
}

Expand Down Expand Up @@ -3571,7 +3571,7 @@ class u_EnvironmentInterpolation :
{
public:
u_EnvironmentInterpolation( GLShader *shader ) :
GLUniform1f( shader, "u_EnvironmentInterpolation" )
GLUniform1f( shader, "u_EnvironmentInterpolation", true )
{
}

Expand All @@ -3586,7 +3586,7 @@ class u_Time :
{
public:
u_Time( GLShader *shader ) :
GLUniform1f( shader, "u_Time" )
GLUniform1f( shader, "u_Time", true ) // Source this from a buffer when entity support is added to the material system
{
}

Expand Down
10 changes: 3 additions & 7 deletions src/engine/renderer/glsl_source/material_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifdef HAVE_ARB_bindless_texture

#if defined(COMPUTELIGHT_GLSL)
#if defined(USE_REFLECTIVE_SPECULAR)
// TODO: Source this from an entity buffer once entities are supported by the material system
/* #if defined(USE_REFLECTIVE_SPECULAR)
samplerCube u_EnvironmentMap0 = samplerCube( u_EnvironmentMap0_initial );
samplerCube u_EnvironmentMap1 = samplerCube( u_EnvironmentMap1_initial );
#endif // !USE_REFLECTIVE_SPECULAR
usampler3D u_LightTilesInt = usampler3D( u_LightTilesInt_initial );
#endif // !USE_REFLECTIVE_SPECULAR */
#endif // !COMPUTELIGHT_GLSL

#if defined(FOGQUAKE3_GLSL)
Expand All @@ -80,17 +80,13 @@ sampler2D u_DiffuseMap = sampler2D( u_DiffuseMap_initial );
sampler2D u_MaterialMap = sampler2D( u_MaterialMap_initial );
sampler2D u_GlowMap = sampler2D( u_GlowMap_initial );
sampler2D u_LightMap = sampler2D( u_LightMap_initial );
sampler3D u_LightGrid1 = sampler3D( u_LightGrid1_initial );
sampler2D u_DeluxeMap = sampler2D( u_DeluxeMap_initial );
sampler3D u_LightGrid2 = sampler3D( u_LightGrid2_initial );
#endif // !LIGHTMAPPING_GLSL

#if defined(LIQUID_GLSL)
sampler2D u_CurrentMap = sampler2D( u_CurrentMap_initial );
sampler2D u_PortalMap = sampler2D( u_PortalMap_initial );
sampler2D u_DepthMap = sampler2D( u_DepthMap_initial );
sampler3D u_LightGrid1 = sampler3D( u_LightGrid1_initial );
sampler3D u_LightGrid2 = sampler3D( u_LightGrid2_initial );
#endif // !LIQUID_GLSL

#if defined(REFLECTION_CB_GLSL)
Expand Down

0 comments on commit c28370b

Please sign in to comment.