Skip to content

Commit

Permalink
renderer: split generic and generic3D
Browse files Browse the repository at this point in the history
  • Loading branch information
illwieckz committed Nov 8, 2024
1 parent c750866 commit dbe52d3
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 168 deletions.
62 changes: 31 additions & 31 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,42 +214,42 @@ void UpdateSurfaceDataGeneric3D( uint32_t* materials, Material& material, drawSu
}
drawSurf->initialized[stage] = true;

gl_genericShaderMaterial->BindProgram( material.deformIndex );
gl_generic3DShaderMaterial->BindProgram( material.deformIndex );

gl_genericShaderMaterial->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
gl_genericShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
gl_generic3DShaderMaterial->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
gl_generic3DShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );

// u_AlphaThreshold
gl_genericShaderMaterial->SetUniform_AlphaTest( pStage->stateBits );
gl_generic3DShaderMaterial->SetUniform_AlphaTest( pStage->stateBits );

// u_ColorModulate
colorGen_t rgbGen = SetRgbGen( pStage );
alphaGen_t alphaGen = SetAlphaGen( pStage );

bool mayUseVertexOverbright = pStage->type == stageType_t::ST_COLORMAP && drawSurf->bspSurface;
gl_genericShaderMaterial->SetUniform_ColorModulate( rgbGen, alphaGen, mayUseVertexOverbright );
gl_generic3DShaderMaterial->SetUniform_ColorModulate( rgbGen, alphaGen, mayUseVertexOverbright );

Tess_ComputeColor( pStage );
gl_genericShaderMaterial->SetUniform_Color( tess.svars.color );
gl_generic3DShaderMaterial->SetUniform_Color( tess.svars.color );

Tess_ComputeTexMatrices( pStage );
gl_genericShaderMaterial->SetUniform_TextureMatrix( tess.svars.texMatrices[TB_COLORMAP] );
gl_generic3DShaderMaterial->SetUniform_TextureMatrix( tess.svars.texMatrices[TB_COLORMAP] );

// bind u_ColorMap
if ( pStage->type == stageType_t::ST_STYLELIGHTMAP ) {
gl_genericShaderMaterial->SetUniform_ColorMapBindless(
gl_generic3DShaderMaterial->SetUniform_ColorMapBindless(
GL_BindToTMU( 0, GetLightMap( drawSurf ) )
);
} else {
gl_genericShaderMaterial->SetUniform_ColorMapBindless( BindAnimatedImage( 0, &pStage->bundle[TB_COLORMAP] ) );
gl_generic3DShaderMaterial->SetUniform_ColorMapBindless( BindAnimatedImage( 0, &pStage->bundle[TB_COLORMAP] ) );
}

bool hasDepthFade = pStage->hasDepthFade;
if ( hasDepthFade ) {
gl_genericShaderMaterial->SetUniform_DepthScale( pStage->depthFadeValue );
gl_generic3DShaderMaterial->SetUniform_DepthScale( pStage->depthFadeValue );
}

gl_genericShaderMaterial->WriteUniformsToBuffer( materials );
gl_generic3DShaderMaterial->WriteUniformsToBuffer( materials );
}

void UpdateSurfaceDataLightMapping( uint32_t* materials, Material& material, drawSurf_t* drawSurf, const uint32_t stage ) {
Expand Down Expand Up @@ -994,30 +994,30 @@ void BindShaderNOP( Material* ) {

void BindShaderGeneric3D( Material* material ) {
// Select shader permutation.
gl_genericShaderMaterial->SetTCGenEnvironment( material->tcGenEnvironment );
gl_genericShaderMaterial->SetTCGenLightmap( material->tcGen_Lightmap );
gl_genericShaderMaterial->SetDepthFade( material->hasDepthFade );
gl_generic3DShaderMaterial->SetTCGenEnvironment( material->tcGenEnvironment );
gl_generic3DShaderMaterial->SetTCGenLightmap( material->tcGen_Lightmap );
gl_generic3DShaderMaterial->SetDepthFade( material->hasDepthFade );

// Bind shader program.
gl_genericShaderMaterial->BindProgram( material->deformIndex );
gl_generic3DShaderMaterial->BindProgram( material->deformIndex );

// Set shader uniforms.
if ( material->tcGenEnvironment ) {
gl_genericShaderMaterial->SetUniform_ViewOrigin( backEnd.orientation.viewOrigin );
gl_genericShaderMaterial->SetUniform_ViewUp( backEnd.orientation.axis[2] );
gl_generic3DShaderMaterial->SetUniform_ViewOrigin( backEnd.orientation.viewOrigin );
gl_generic3DShaderMaterial->SetUniform_ViewUp( backEnd.orientation.axis[2] );
}

gl_genericShaderMaterial->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
gl_genericShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
gl_generic3DShaderMaterial->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
gl_generic3DShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );

gl_genericShaderMaterial->SetUniform_DepthMapBindless( GL_BindToTMU( 1, tr.currentDepthImage ) );
gl_generic3DShaderMaterial->SetUniform_DepthMapBindless( GL_BindToTMU( 1, tr.currentDepthImage ) );

// u_DeformGen
gl_genericShaderMaterial->SetUniform_Time( backEnd.refdef.floatTime - backEnd.currentEntity->e.shaderTime );
gl_generic3DShaderMaterial->SetUniform_Time( backEnd.refdef.floatTime - backEnd.currentEntity->e.shaderTime );

if ( r_profilerRenderSubGroups.Get() ) {
gl_genericShaderMaterial->SetUniform_ProfilerZero();
gl_genericShaderMaterial->SetUniform_ProfilerRenderSubGroups( GetShaderProfilerRenderSubGroupsMode( material->stateBits ) );
gl_generic3DShaderMaterial->SetUniform_ProfilerZero();
gl_generic3DShaderMaterial->SetUniform_ProfilerRenderSubGroups( GetShaderProfilerRenderSubGroupsMode( material->stateBits ) );
}
}

Expand Down Expand Up @@ -1264,7 +1264,7 @@ void ProcessMaterialNOP( Material*, shaderStage_t*, drawSurf_t* ) {
// ProcessMaterial*() are essentially same as BindShader*(), but only set the GL program id to the material,
// without actually binding it
void ProcessMaterialGeneric3D( Material* material, shaderStage_t* pStage, drawSurf_t* ) {
material->shader = gl_genericShaderMaterial;
material->shader = gl_generic3DShaderMaterial;

material->tcGenEnvironment = pStage->tcGen_Environment;
material->tcGen_Lightmap = pStage->tcGen_Lightmap;
Expand All @@ -1276,14 +1276,14 @@ void ProcessMaterialGeneric3D( Material* material, shaderStage_t* pStage, drawSu
material->useAttrColor = rgbGen == colorGen_t::CGEN_VERTEX || rgbGen == colorGen_t::CGEN_ONE_MINUS_VERTEX
|| alphaGen == alphaGen_t::AGEN_VERTEX || alphaGen == alphaGen_t::AGEN_ONE_MINUS_VERTEX;

gl_genericShaderMaterial->SetTCGenEnvironment( pStage->tcGen_Environment );
gl_genericShaderMaterial->SetTCGenLightmap( pStage->tcGen_Lightmap );
gl_generic3DShaderMaterial->SetTCGenEnvironment( pStage->tcGen_Environment );
gl_generic3DShaderMaterial->SetTCGenLightmap( pStage->tcGen_Lightmap );

bool hasDepthFade = pStage->hasDepthFade;
material->hasDepthFade = hasDepthFade;
gl_genericShaderMaterial->SetDepthFade( hasDepthFade );
gl_generic3DShaderMaterial->SetDepthFade( hasDepthFade );

material->program = gl_genericShaderMaterial->GetProgram( pStage->deformIndex );
material->program = gl_generic3DShaderMaterial->GetProgram( pStage->deformIndex );
}

void ProcessMaterialLightMapping( Material* material, shaderStage_t* pStage, drawSurf_t* drawSurf ) {
Expand Down Expand Up @@ -2286,7 +2286,7 @@ void MaterialSystem::RenderMaterial( Material& material, const uint32_t viewID )
if ( material.shaderBinder == BindShaderLightMapping ) {
gl_lightMappingShaderMaterial->SetUniform_MaterialColour( color );
} else if ( material.shaderBinder == BindShaderGeneric3D ) {
gl_genericShaderMaterial->SetUniform_MaterialColour( color );
gl_generic3DShaderMaterial->SetUniform_MaterialColour( color );
}
}

Expand All @@ -2313,7 +2313,7 @@ void MaterialSystem::RenderMaterial( Material& material, const uint32_t viewID )
if ( material.shaderBinder == &BindShaderLightMapping ) {
gl_lightMappingShaderMaterial->SetUniform_ShowTris( 1 );
} else if ( material.shaderBinder == &BindShaderGeneric3D ) {
gl_genericShaderMaterial->SetUniform_ShowTris( 1 );
gl_generic3DShaderMaterial->SetUniform_ShowTris( 1 );
}

GL_State( GLS_DEPTHTEST_DISABLE );
Expand All @@ -2322,7 +2322,7 @@ void MaterialSystem::RenderMaterial( Material& material, const uint32_t viewID )
if ( material.shaderBinder == &BindShaderLightMapping ) {
gl_lightMappingShaderMaterial->SetUniform_ShowTris( 0 );
} else if ( material.shaderBinder == &BindShaderGeneric3D ) {
gl_genericShaderMaterial->SetUniform_ShowTris( 0 );
gl_generic3DShaderMaterial->SetUniform_ShowTris( 0 );
}
}

Expand Down
46 changes: 38 additions & 8 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ ShaderKind shaderKind = ShaderKind::Unknown;

// *INDENT-OFF*

GLShader_generic2D *gl_generic2DShader = nullptr;
GLShader_generic *gl_genericShader = nullptr;
GLShader_genericMaterial *gl_genericShaderMaterial = nullptr;
GLShader_generic2D *gl_generic2DShader = nullptr;
GLShader_generic3D *gl_generic3DShader = nullptr;
GLShader_generic3DMaterial *gl_generic3DShaderMaterial = nullptr;
GLShader_cull *gl_cullShader = nullptr;
GLShader_depthReduction *gl_depthReductionShader = nullptr;
GLShader_clearSurfaces *gl_clearSurfacesShader = nullptr;
Expand Down Expand Up @@ -2173,6 +2174,25 @@ void GLShader::WriteUniformsToBuffer( uint32_t* buffer ) {
}
}

GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
GLShader( "generic", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
u_ColorMap( this ),
u_DepthMap( this ),
u_TextureMatrix( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_ColorModulate( this ),
u_Color( this ),
GLDeformStage( this )
{
}

void GLShader_generic::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
{
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap" ), 0 );
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_DepthMap" ), 1 );
}

GLShader_generic2D::GLShader_generic2D( GLShaderManager *manager ) :
GLShader( "generic2D", "generic", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
u_ColorMap( this ),
Expand Down Expand Up @@ -2200,8 +2220,8 @@ void GLShader_generic2D::SetShaderProgramUniforms( shaderProgram_t *shaderProgra
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_DepthMap" ), 1 );
}

GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
GLShader( "generic", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
GLShader_generic3D::GLShader_generic3D( GLShaderManager *manager ) :
GLShader( "generic3D", "generic", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
u_ColorMap( this ),
u_DepthMap( this ),
u_TextureMatrix( this ),
Expand All @@ -2226,14 +2246,19 @@ GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
{
}

void GLShader_generic::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
void GLShader_generic3D::BuildShaderCompileMacros( std::string& compileMacros )
{
compileMacros += "GENERIC_3D ";
}

void GLShader_generic3D::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )
{
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap" ), 0 );
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_DepthMap" ), 1 );
}

GLShader_genericMaterial::GLShader_genericMaterial( GLShaderManager* manager ) :
GLShader( "genericMaterial", "generic", true, ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
GLShader_generic3DMaterial::GLShader_generic3DMaterial( GLShaderManager* manager ) :
GLShader( "generic3DMaterial", "generic", true, ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
u_ColorMap( this ),
u_DepthMap( this ),
u_TextureMatrix( this ),
Expand All @@ -2255,7 +2280,12 @@ GLShader_genericMaterial::GLShader_genericMaterial( GLShaderManager* manager ) :
GLCompileMacro_USE_DEPTH_FADE( this ) {
}

void GLShader_genericMaterial::SetShaderProgramUniforms( shaderProgram_t* shaderProgram ) {
void GLShader_generic3DMaterial::BuildShaderCompileMacros( std::string& compileMacros )
{
compileMacros += "GENERIC_3D ";
}

void GLShader_generic3DMaterial::SetShaderProgramUniforms( shaderProgram_t* shaderProgram ) {
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_ColorMap" ), 0 );
glUniform1i( glGetUniformLocation( shaderProgram->program, "u_DepthMap" ), 1 );
}
Expand Down
37 changes: 28 additions & 9 deletions src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3906,10 +3906,24 @@ class u_Lights :
}
};

// This is just a copy of the GLShader_generic, but with a special
// define for RmlUI transforms. It probably has a lot of unnecessary
// code that could be pruned.
// TODO: Write a more minimal 2D rendering shader.
// Mostly used by debug tools and things only requiring a very simple shader.
class GLShader_generic :
public GLShader,
public u_ColorMap,
public u_DepthMap,
public u_TextureMatrix,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_ColorModulate,
public u_Color,
public GLDeformStage
{
public:
GLShader_generic( GLShaderManager *manager );
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
};

// Used for 2D UI elements, it has special code for RmlUi transforms.
class GLShader_generic2D :
public GLShader,
public u_ColorMap,
Expand All @@ -3930,7 +3944,8 @@ class GLShader_generic2D :
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
};

class GLShader_generic :
// Full-featured shader for stages with a single texture.
class GLShader_generic3D :
public GLShader,
public u_ColorMap,
public u_DepthMap,
Expand All @@ -3955,11 +3970,12 @@ class GLShader_generic :
public GLCompileMacro_USE_DEPTH_FADE
{
public:
GLShader_generic( GLShaderManager *manager );
GLShader_generic3D( GLShaderManager *manager );
void BuildShaderCompileMacros( std::string& compileMacros ) override;
void SetShaderProgramUniforms( shaderProgram_t *shaderProgram ) override;
};

class GLShader_genericMaterial :
class GLShader_generic3DMaterial :
public GLShader,
public u_ColorMap,
public u_DepthMap,
Expand All @@ -3981,10 +3997,12 @@ class GLShader_genericMaterial :
public GLCompileMacro_USE_TCGEN_LIGHTMAP,
public GLCompileMacro_USE_DEPTH_FADE {
public:
GLShader_genericMaterial( GLShaderManager* manager );
GLShader_generic3DMaterial( GLShaderManager* manager );
void BuildShaderCompileMacros( std::string& compileMacros ) override;
void SetShaderProgramUniforms( shaderProgram_t* shaderProgram ) override;
};

// Full-featured shader for multi-textured stages.
class GLShader_lightMapping :
public GLShader,
public u_DiffuseMap,
Expand Down Expand Up @@ -4716,8 +4734,9 @@ std::string GetShaderPath();
extern ShaderKind shaderKind;

extern GLShader_generic2D *gl_generic2DShader;
extern GLShader_generic3D *gl_generic3DShader;
extern GLShader_generic3DMaterial *gl_generic3DShaderMaterial;
extern GLShader_generic *gl_genericShader;
extern GLShader_genericMaterial *gl_genericShaderMaterial;
extern GLShader_cull *gl_cullShader;
extern GLShader_depthReduction *gl_depthReductionShader;
extern GLShader_clearSurfaces *gl_clearSurfacesShader;
Expand Down
7 changes: 6 additions & 1 deletion src/engine/renderer/glsl_source/generic_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#define GENERIC_GLSL

uniform sampler2D u_ColorMap;
uniform float u_AlphaThreshold;

#if defined(GENERIC_2D) || defined(GENERIC_3D)
uniform float u_AlphaThreshold;
#endif

#if defined(USE_MATERIAL_SYSTEM)
uniform bool u_ShowTris;
Expand Down Expand Up @@ -57,11 +60,13 @@ void main()

vec4 color = texture2D(u_ColorMap, var_TexCoords);

#if defined(GENERIC_2D) || defined(GENERIC_3D)
if( abs(color.a + u_AlphaThreshold) <= 1.0 )
{
discard;
return;
}
#endif

#if defined(USE_DEPTH_FADE)
float depth = texture2D(u_DepthMap, gl_FragCoord.xy / r_FBufSize).x;
Expand Down
Loading

0 comments on commit dbe52d3

Please sign in to comment.