Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove bindless layout declaration from compute header #1266

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,9 @@ void MaterialSystem::GenerateDepthImages( const int width, const int height, ima
mipmapWidth = mipmapWidth > 1 ? mipmapWidth >> 1 : 1;
mipmapHeight = mipmapHeight > 1 ? mipmapHeight >> 1 : 1;
}

depthImage->texture->GenBindlessImageHandle();
depthImage->texture->MakeImagesResident( GL_READ_WRITE );
}

void BindShaderNONE( Material* ) {
Expand Down Expand Up @@ -1596,8 +1599,12 @@ void MaterialSystem::DepthReduction() {
uint32_t globalWorkgroupX = ( width + 7 ) / 8;
uint32_t globalWorkgroupY = ( height + 7 ) / 8;

GL_Bind( tr.currentDepthImage );
glBindImageTexture( 2, depthImage->texnum, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F );
gl_depthReductionShader->SetUniform_DepthMapBindless( GL_BindToTMU( 0, tr.currentDepthImage ) );
// glBindImageTexture( 2, depthImage->texnum, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F );
// glUniformHandleui64ARB( 1, depthImage->texture->bindlessImageHandles[0]
GL_CheckErrors();
glUniform2uiv( 1, 1, ( const GLuint* ) &depthImage->texture->bindlessImageHandles[0] );
GL_CheckErrors();

gl_depthReductionShader->SetUniform_InitialDepthLevel( true );
gl_depthReductionShader->SetUniform_ViewWidth( width );
Expand All @@ -1611,8 +1618,15 @@ void MaterialSystem::DepthReduction() {
globalWorkgroupX = ( width + 7 ) / 8;
globalWorkgroupY = ( height + 7 ) / 8;

glBindImageTexture( 1, depthImage->texnum, i, GL_FALSE, 0, GL_READ_ONLY, GL_R32F );
glBindImageTexture( 2, depthImage->texnum, i + 1, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F );
// glBindImageTexture( 1, depthImage->texnum, i, GL_FALSE, 0, GL_READ_ONLY, GL_R32F );
// glBindImageTexture( 2, depthImage->texnum, i + 1, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F );
// glUniformHandleui64ARB( 0, depthImage->texture->bindlessImageHandles[i] );
// glUniformHandleui64ARB( 1, depthImage->texture->bindlessImageHandles[i + 1] );
GL_CheckErrors();
glUniform2uiv( 0, 1, ( const GLuint* ) &depthImage->texture->bindlessImageHandles[i] );
GL_CheckErrors();
glUniform2uiv( 1, 1, ( const GLuint* ) &depthImage->texture->bindlessImageHandles[i + 1] );
GL_CheckErrors();

gl_depthReductionShader->SetUniform_InitialDepthLevel( false );
gl_depthReductionShader->SetUniform_ViewWidth( width );
Expand Down Expand Up @@ -1661,7 +1675,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 Expand Up @@ -1829,6 +1843,8 @@ void MaterialSystem::Free() {
}
}

depthImage->texture->MakeImagesNonResident();

currentFrame = 0;
nextFrame = 1;
maxStages = 0;
Expand Down
33 changes: 33 additions & 0 deletions src/engine/renderer/TextureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,34 @@ bool Texture::IsResident() const {
return bindlessTextureResident;
}

bool Texture::AreImagesResident() const {
return bindlessImagesResident;
}

void Texture::MakeResident() {
glMakeTextureHandleResidentARB( bindlessTextureHandle );
bindlessTextureResident = true;
}

void Texture::MakeImagesResident( const GLenum access ) {
for ( uint8_t i = 0; i < mipLevels; i++ ) {
glMakeImageHandleResidentARB( bindlessImageHandles[i], access );
}
bindlessImagesResident = true;
}

void Texture::MakeNonResident() {
glMakeTextureHandleNonResidentARB( bindlessTextureHandle );
bindlessTextureResident = false;
}

void Texture::MakeImagesNonResident() {
for ( uint8_t i = 0; i < mipLevels; i++ ) {
glMakeImageHandleNonResidentARB( bindlessImageHandles[i] );
}
bindlessImagesResident = false;
}

void Texture::GenBindlessHandle() {
bindlessTextureHandle = glGetTextureHandleARB( textureHandle );

Expand All @@ -68,6 +86,21 @@ void Texture::GenBindlessHandle() {
hasBindlessHandle = true;
}

void Texture::GenBindlessImageHandle() {
if ( target != GL_TEXTURE_2D ) {
Sys::Drop( "Bindless image handles for layered textures are currently unsupported" );
}

for ( uint8_t level = 0; level < mipLevels; level++ ) {
bindlessImageHandles[level] = glGetImageHandleARB( textureHandle, level, GL_FALSE, 0, format );
if ( bindlessImageHandles[level] == 0 ) {
Sys::Drop( "Failed to generate bindless image handle" );
}
}

hasBindlessImageHandles = true;
}

TextureManager::TextureManager() = default;
TextureManager::~TextureManager() = default;

Expand Down
12 changes: 11 additions & 1 deletion src/engine/renderer/TextureManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,28 @@ class Texture {
bool hasBindlessHandle = false;

GLenum target = GL_TEXTURE_2D;
GLenum format = GL_RGBA8;

bool hasBindlessImageHandles = false;
uint8_t mipLevels = 0;
GLuint64 bindlessImageHandles[16]; // Enough for all mipmaps of a 32768x32768 texture, which is the current maximum

Texture();
~Texture();

bool IsResident() const;
bool AreImagesResident() const;
void MakeResident();
void MakeImagesResident( const GLenum access );
void MakeNonResident();
void MakeImagesNonResident();

void GenBindlessHandle();;
void GenBindlessHandle();
void GenBindlessImageHandle();

private:
bool bindlessTextureResident = false;
bool bindlessImagesResident = false;
};

class TextureManager {
Expand Down
7 changes: 3 additions & 4 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ static std::string GenComputeHeader() {
AddDefine( str, "MAX_COMMAND_COUNTERS", MAX_COMMAND_COUNTERS );

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

Expand Down Expand Up @@ -2937,6 +2938,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 @@ -2956,15 +2958,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 @@ -4685,6 +4685,7 @@ class GLShader_fxaa :

class GLShader_cull :
public GLShader,
public u_DepthMap,
public u_Frame,
public u_ViewID,
public u_TotalDrawSurfs,
Expand All @@ -4706,12 +4707,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 @@ -39,7 +39,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;
/* layout(binding = 0) */ uniform sampler2D u_DepthMap;

layout(std430, binding = 1) readonly restrict buffer surfaceDescriptorsSSBO {
SurfaceDescriptor surfaces[];
Expand Down Expand Up @@ -139,10 +139,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
14 changes: 10 additions & 4 deletions src/engine/renderer/glsl_source/depthReduction_cp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ 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;
layout(r32f, binding = 1) uniform readonly image2D depthImageIn;
layout(r32f, binding = 2) uniform writeonly image2D depthImageOut;
/* layout(binding = 0) */ uniform sampler2D u_DepthMap;
// layout(r32f, binding = 1) uniform readonly image2D depthImageIn;
// layout(r32f, binding = 2) uniform writeonly image2D depthImageOut;

layout(location = 0) uniform uvec2 depthInTest;
layout(location = 1) uniform uvec2 depthOutTest;

uniform uint u_ViewWidth;
uniform uint u_ViewHeight;
Expand All @@ -55,9 +58,12 @@ void main() {
return;
};

layout(r32f) image2D depthImageIn = layout(r32f) image2D( depthInTest );
layout(r32f) image2D depthImageOut = layout(r32f) image2D( depthOutTest );

// 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
3 changes: 3 additions & 0 deletions src/engine/renderer/tr_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,9 @@ void R_UploadImage( const byte **dataArray, int numLayers, int numMips, image_t
image->bits |= IF_ALPHA;
}

image->texture->format = internalFormat;
image->texture->mipLevels = log2f( std::max( image->uploadWidth, image->uploadHeight ) ) + 1;

GL_Unbind( image );
}

Expand Down