Skip to content

Commit

Permalink
Fix depthMap binding in cull/depthReduction shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
VReaperV committed Aug 28, 2024
1 parent 72c438c commit abe2d74
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1851,7 +1851,7 @@ void MaterialSystem::DepthReduction() {
uint32_t globalWorkgroupX = ( width + 7 ) / 8;
uint32_t globalWorkgroupY = ( height + 7 ) / 8;

GL_Bind( tr.currentDepthImage );
gl_depthReductionShader->SetUniform_DepthMapBindless( GL_BindToTMU( 0, tr.currentDepthImage ) );
glBindImageTexture( 2, depthImage->texnum, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F );

gl_depthReductionShader->SetUniform_InitialDepthLevel( true );
Expand Down Expand Up @@ -1915,7 +1915,7 @@ void MaterialSystem::CullSurfaces() {
gl_cullShader->BindProgram( 0 );
uint32_t globalWorkGroupX = totalDrawSurfs % MAX_COMMAND_COUNTERS == 0 ?
totalDrawSurfs / MAX_COMMAND_COUNTERS : totalDrawSurfs / MAX_COMMAND_COUNTERS + 1;
GL_Bind( depthImage );
gl_cullShader->SetUniform_DepthMapBindless( GL_BindToTMU( 0, depthImage ) );
gl_cullShader->SetUniform_Frame( nextFrame );
gl_cullShader->SetUniform_ViewID( view );
gl_cullShader->SetUniform_TotalDrawSurfs( totalDrawSurfs );
Expand Down
10 changes: 6 additions & 4 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,10 @@ static std::string GenComputeHeader() {
AddDefine( str, "MAX_SURFACE_COMMAND_BATCHES", MAX_SURFACE_COMMAND_BATCHES );
AddDefine( str, "MAX_COMMAND_COUNTERS", MAX_COMMAND_COUNTERS );

if ( glConfig2.bindlessTexturesAvailable ) {
str += "layout(bindless_sampler) uniform;\n";
}

return str;
}

Expand Down Expand Up @@ -2913,6 +2917,7 @@ void GLShader_fxaa::SetShaderProgramUniforms( shaderProgram_t *shaderProgram )

GLShader_cull::GLShader_cull( GLShaderManager* manager ) :
GLShader( "cull", ATTR_POSITION, manager, false, false, true ),
u_DepthMap( this ),
u_Frame( this ),
u_ViewID( this ),
u_TotalDrawSurfs( this ),
Expand All @@ -2932,15 +2937,12 @@ GLShader_cull::GLShader_cull( GLShaderManager* manager ) :

GLShader_depthReduction::GLShader_depthReduction( GLShaderManager* manager ) :
GLShader( "depthReduction", ATTR_POSITION, manager, false, false, true ),
u_DepthMap( this ),
u_ViewWidth( this ),
u_ViewHeight( this ),
u_InitialDepthLevel( this ) {
}

void GLShader_depthReduction::SetShaderProgramUniforms( shaderProgram_t* shaderProgram ) {
glUniform1i( glGetUniformLocation( shaderProgram->program, "depthTextureInitial" ), 0 );
}

GLShader_clearSurfaces::GLShader_clearSurfaces( GLShaderManager* manager ) :
GLShader( "clearSurfaces", ATTR_POSITION, manager, false, false, true ),
u_Frame( this ) {
Expand Down
3 changes: 2 additions & 1 deletion src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4695,6 +4695,7 @@ class GLShader_fxaa :

class GLShader_cull :
public GLShader,
public u_DepthMap,
public u_Frame,
public u_ViewID,
public u_TotalDrawSurfs,
Expand All @@ -4716,12 +4717,12 @@ class GLShader_cull :

class GLShader_depthReduction :
public GLShader,
public u_DepthMap,
public u_ViewWidth,
public u_ViewHeight,
public u_InitialDepthLevel {
public:
GLShader_depthReduction( GLShaderManager* manager );
void SetShaderProgramUniforms( shaderProgram_t* shaderProgram ) override;
};

class GLShader_clearSurfaces :
Expand Down
10 changes: 5 additions & 5 deletions src/engine/renderer/glsl_source/cull_cp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Keep this to 64 because we don't want extra shared mem etc. to be allocated, and to minimize wasted lanes
layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in;

layout(binding = 0) uniform sampler2D depthImage;
uniform sampler2D u_DepthMap;

struct BoundingSphere {
vec3 origin;
Expand Down Expand Up @@ -168,10 +168,10 @@ bool CullSurface( in BoundingSphere boundingSphere ) {
depthCoords.w = clamp( depthCoords.w, 0, int( ( u_ViewHeight >> level ) - 1 ) );

vec4 depthValues;
depthValues.x = texelFetch( depthImage, depthCoords.xy, level ).r;
depthValues.y = texelFetch( depthImage, depthCoords.zy, level ).r;
depthValues.z = texelFetch( depthImage, depthCoords.xw, level ).r;
depthValues.w = texelFetch( depthImage, depthCoords.zw, level ).r;
depthValues.x = texelFetch( u_DepthMap, depthCoords.xy, level ).r;
depthValues.y = texelFetch( u_DepthMap, depthCoords.zy, level ).r;
depthValues.z = texelFetch( u_DepthMap, depthCoords.xw, level ).r;
depthValues.w = texelFetch( u_DepthMap, depthCoords.zw, level ).r;

const float surfaceDepth = max( max( max( depthValues.x, depthValues.y ), depthValues.z ), depthValues.w );

Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/glsl_source/depthReduction_cp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Keep this to 8x8 because we don't want extra shared mem etc. to be allocated, and to minimize wasted lanes
layout (local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

layout(binding = 0) uniform sampler2D depthTextureInitial;
uniform sampler2D u_DepthMap;
layout(r32f, binding = 1) uniform readonly image2D depthImageIn;
layout(r32f, binding = 2) uniform writeonly image2D depthImageOut;

Expand All @@ -57,7 +57,7 @@ void main() {

// Depth buffer uses a packed D24S8 format, so we have to copy it over to an r32f image first
if( u_InitialDepthLevel ) {
vec4 depthOut = texelFetch( depthTextureInitial, position, 0 );
vec4 depthOut = texelFetch( u_DepthMap, position, 0 );
imageStore( depthImageOut, position, depthOut );
} else {
float depth[4];
Expand Down

0 comments on commit abe2d74

Please sign in to comment.