Skip to content

Commit

Permalink
More consistent handling of depth formats across the engine
Browse files Browse the repository at this point in the history
  • Loading branch information
lhog committed Feb 13, 2021
1 parent 4bdbc81 commit 149ea5c
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 50 deletions.
6 changes: 5 additions & 1 deletion rts/Lua/LuaConstPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ bool LuaConstPlatform::PushEntries(lua_State* L)
LuaPushNamedBool(L, "glSupportNonPowerOfTwoTex", globalRendering->supportNonPowerOfTwoTex);
LuaPushNamedBool(L, "glSupportTextureQueryLOD" , globalRendering->supportTextureQueryLOD);
LuaPushNamedBool(L, "glSupportMSAAFrameBuffer" , globalRendering->supportMSAAFrameBuffer);
LuaPushNamedBool(L, "glSupport24bitDepthBuffer", globalRendering->support24bitDepthBuffer);

LuaPushNamedBool(L, "glSupport16bitDepthBuffer", globalRendering->supportDepthBufferBits[1]);
LuaPushNamedBool(L, "glSupport24bitDepthBuffer", globalRendering->supportDepthBufferBits[2]);
LuaPushNamedBool(L, "glSupport32bitDepthBuffer", globalRendering->supportDepthBufferBits[3]);

LuaPushNamedBool(L, "glSupportRestartPrimitive", globalRendering->supportRestartPrimitive);
LuaPushNamedBool(L, "glSupportClipSpaceControl", globalRendering->supportClipSpaceControl);
LuaPushNamedBool(L, "glSupportFragDepthLayout" , globalRendering->supportFragDepthLayout);
Expand Down
3 changes: 2 additions & 1 deletion rts/Lua/LuaTextures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ std::string LuaTextures::Create(const Texture& tex)
if (tex.fboDepth != 0) {
glGenRenderbuffersEXT(1, &fboDepth);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fboDepth);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, tex.xsize, tex.ysize);
GLenum depthFormat = static_cast<GLenum>(CGlobalRendering::DepthBitsToFormat(globalRendering->supportDepthBufferBestBits));
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, tex.xsize, tex.ysize);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fboDepth);
}

Expand Down
6 changes: 4 additions & 2 deletions rts/Rendering/Env/AdvTreeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ void CAdvTreeGenerator::Init()

FBO fbo;
fbo.Bind();
fbo.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, GL_DEPTH_COMPONENT16, 256, 256);
GLenum depthFormat = static_cast<GLenum>(CGlobalRendering::DepthBitsToFormat(globalRendering->supportDepthBufferBestBits));
fbo.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, depthFormat, 256, 256);
fbo.CreateRenderBuffer(GL_COLOR_ATTACHMENT0_EXT, GL_RGBA8, 256, 256);
if (!fbo.CheckStatus("ADVTREE")) {
fbo.Unbind();
Expand Down Expand Up @@ -281,7 +282,8 @@ void CAdvTreeGenerator::CreateFarTex(Shader::IProgramObject* treeShader)
{
FBO fbo;
fbo.Bind();
fbo.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, GL_DEPTH_COMPONENT16, 64, 64);
GLenum depthFormat = static_cast<GLenum>(CGlobalRendering::DepthBitsToFormat(globalRendering->supportDepthBufferBestBits));
fbo.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, depthFormat, 64, 64);
fbo.CreateRenderBuffer(GL_COLOR_ATTACHMENT0_EXT, GL_RGBA8, 64, 64);
if (!fbo.CheckStatus("ADVTREE")) {
FBO::Unbind();
Expand Down
3 changes: 2 additions & 1 deletion rts/Rendering/Env/AdvWater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ CAdvWater::CAdvWater(bool loadShader)

reflectFBO.Bind();
reflectFBO.AttachTexture(reflectTexture, GL_TEXTURE_2D, GL_COLOR_ATTACHMENT0_EXT);
reflectFBO.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, GL_DEPTH_COMPONENT32, 512, 512);
GLenum depthFormat = static_cast<GLenum>(CGlobalRendering::DepthBitsToFormat(globalRendering->supportDepthBufferBestBits));
reflectFBO.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, depthFormat, 512, 512);
bumpFBO.Bind();
bumpFBO.AttachTexture(bumpTexture, GL_TEXTURE_2D, GL_COLOR_ATTACHMENT0_EXT);
FBO::Unbind();
Expand Down
15 changes: 5 additions & 10 deletions rts/Rendering/Env/BumpWater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ CBumpWater::CBumpWater()
anisotropy = configHandler->GetFloat("BumpWaterAnisotropy");
depthCopy = configHandler->GetBool("BumpWaterUseDepthTexture");
depthBits = configHandler->GetInt("BumpWaterDepthBits");
if ((depthBits == 24) && !globalRendering->support24bitDepthBuffer)
depthBits = 16;
depthBits = std::min(depthBits, static_cast<char>(globalRendering->supportDepthBufferBestBits));
blurRefl = configHandler->GetBool("BumpWaterBlurReflection");
shoreWaves = (configHandler->GetBool("BumpWaterShoreWaves")) && waterRendering->shoreWaves;
endlessOcean = (configHandler->GetBool("BumpWaterEndlessOcean")) && waterRendering->hasWaterPlane
Expand Down Expand Up @@ -335,8 +334,9 @@ CBumpWater::CBumpWater()
glBindTexture(target, depthTexture);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GLuint depthFormat = GL_DEPTH_COMPONENT32;
if (!globalRendering->amdHacks) { depthFormat = GL_DEPTH_COMPONENT24; }
//GLuint depthFormat = GL_DEPTH_COMPONENT32;
//if (!globalRendering->amdHacks) { depthFormat = GL_DEPTH_COMPONENT24; }
GLuint depthFormat = CGlobalRendering::DepthBitsToFormat(globalRendering->supportDepthBufferBestBits);
glTexImage2D(target, 0, depthFormat, screenTextureX, screenTextureY, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
}

Expand All @@ -362,12 +362,7 @@ CBumpWater::CBumpWater()

// CREATE FBOs
if (FBO::IsSupported()) {
GLuint depthRBOFormat = GL_DEPTH_COMPONENT;
switch (depthBits) {
case 16: depthRBOFormat = GL_DEPTH_COMPONENT16; break;
case 24: depthRBOFormat = GL_DEPTH_COMPONENT24; break;
case 32: depthRBOFormat = GL_DEPTH_COMPONENT32; break;
}
GLuint depthRBOFormat = static_cast<GLuint>(CGlobalRendering::DepthBitsToFormat(depthBits));

if (reflection>0) {
reflectFBO.Bind();
Expand Down
3 changes: 2 additions & 1 deletion rts/Rendering/Env/Decals/DecalsDrawerGL4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,8 @@ void CDecalsDrawerGL4::ViewResize()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, globalRendering->viewSizeX, globalRendering->viewSizeY, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
GLint depthFormat = static_cast<GLint>(CGlobalRendering::DepthBitsToFormat(globalRendering->supportDepthBufferBestBits));
glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, globalRendering->viewSizeX, globalRendering->viewSizeY, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
}


Expand Down
6 changes: 4 additions & 2 deletions rts/Rendering/Env/DynWater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,14 @@ CDynWater::CDynWater()

glGenFramebuffersEXT(1, &frameBuffer);

GLenum depthFormat = static_cast<GLenum>(CGlobalRendering::DepthBitsToFormat(globalRendering->supportDepthBufferBestBits));

reflectFBO.Bind();
reflectFBO.AttachTexture(reflectTexture, GL_TEXTURE_2D, GL_COLOR_ATTACHMENT0_EXT);
reflectFBO.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, GL_DEPTH_COMPONENT32, 512, 512);
reflectFBO.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, depthFormat, 512, 512);
refractFBO.Bind();
refractFBO.AttachTexture(refractTexture, GL_TEXTURE_2D, GL_COLOR_ATTACHMENT0_EXT);
refractFBO.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, GL_DEPTH_COMPONENT32, refractSize, refractSize);
refractFBO.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, depthFormat, refractSize, refractSize);
FBO::Unbind();

if (!reflectFBO.IsValid() || !refractFBO.IsValid()) {
Expand Down
4 changes: 3 additions & 1 deletion rts/Rendering/Env/GrassDrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,9 +893,11 @@ void CGrassDrawer::CreateFarTex()
fboTex.AttachTexture(farTex);
fboTex.CheckStatus("GRASSDRAWER1");

GLenum depthFormat = static_cast<GLenum>(CGlobalRendering::DepthBitsToFormat(globalRendering->supportDepthBufferBestBits));

FBO fbo;
fbo.Bind();
fbo.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, GL_DEPTH_COMPONENT16, texSizeX * sizeMod, texSizeY * sizeMod);
fbo.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, depthFormat, texSizeX * sizeMod, texSizeY * sizeMod);
fbo.CreateRenderBuffer(GL_COLOR_ATTACHMENT0_EXT, GL_RGBA8, texSizeX * sizeMod, texSizeY * sizeMod);
fbo.CheckStatus("GRASSDRAWER2");

Expand Down
4 changes: 3 additions & 1 deletion rts/Rendering/Env/Particles/ProjectileDrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ void CProjectileDrawer::ViewResize()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); //might break something else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, globalRendering->viewSizeX, globalRendering->viewSizeY, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);

GLint depthFormat = static_cast<GLint>(CGlobalRendering::DepthBitsToFormat(globalRendering->supportDepthBufferBestBits));
glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, globalRendering->viewSizeX, globalRendering->viewSizeY, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);

glBindTexture(GL_TEXTURE_2D, 0);

Expand Down
2 changes: 2 additions & 0 deletions rts/Rendering/FarTextureHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ void CFarTextureHandler::CreateFarTexture(const CSolidObject* obj)
if (!CheckResizeAtlas())
return;

GLenum depthFormat = static_cast<GLenum>(CGlobalRendering::DepthBitsToFormat(globalRendering->supportDepthBufferBestBits));

fbo.Bind();
fbo.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, GL_DEPTH_COMPONENT16, texSize.x, texSize.y);
fbo.CheckStatus("FARTEXTURE");
Expand Down
66 changes: 45 additions & 21 deletions rts/Rendering/GlobalRendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ CR_REG_METADATA(CGlobalRendering, (
CR_IGNORED(supportNonPowerOfTwoTex),
CR_IGNORED(supportTextureQueryLOD),
CR_IGNORED(supportMSAAFrameBuffer),
CR_IGNORED(support24bitDepthBuffer),
CR_IGNORED(supportDepthBufferBestBits),
CR_IGNORED(supportDepthBufferBits),
CR_IGNORED(supportRestartPrimitive),
CR_IGNORED(supportClipSpaceControl),
CR_IGNORED(supportSeamlessCubeMaps),
Expand Down Expand Up @@ -244,7 +245,7 @@ CGlobalRendering::CGlobalRendering()
, supportNonPowerOfTwoTex(false)
, supportTextureQueryLOD(false)
, supportMSAAFrameBuffer(false)
, support24bitDepthBuffer(false)
, supportDepthBufferBestBits(0)
, supportRestartPrimitive(false)
, supportClipSpaceControl(false)
, supportSeamlessCubeMaps(false)
Expand Down Expand Up @@ -716,26 +717,33 @@ void CGlobalRendering::SetGLSupportFlags()
supportFragDepthLayout = ((globalRenderingInfo.glContextVersion.x * 10 + globalRenderingInfo.glContextVersion.y) >= 42);
supportMSAAFrameBuffer &= ((globalRenderingInfo.glContextVersion.x * 10 + globalRenderingInfo.glContextVersion.y) >= 32);

#if 0
{
// detect if GL_DEPTH_COMPONENT24 is supported (many ATIs don't;
// they seem to support GL_DEPTH_COMPONENT24 for static textures
// but those can't be rendered to)
GLint state = 0;
glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, 16, 16, 0, GL_LUMINANCE, GL_FLOAT, nullptr);
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &state);
support24bitDepthBuffer = (state > 0);
int iter = 0;
for (const int bits : {0, 16, 24, 32}) {
bool supported = false;
if (FBO::IsSupported()) {
FBO fbo;
fbo.Bind();
fbo.CreateRenderBuffer(GL_COLOR_ATTACHMENT0_EXT, GL_RGBA8, 16, 16);
const GLint format = DepthBitsToFormat(bits);
fbo.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, format, 16, 16);
supported = (fbo.GetStatus() == GL_FRAMEBUFFER_COMPLETE_EXT);
fbo.Unbind();
}

if (supported)
supportDepthBufferBestBits = std::max(supportDepthBufferBestBits, bits);

supportDepthBufferBits[iter] = supported;

++iter;
}
#else
if (FBO::IsSupported() && !amdHacks) {
FBO fbo;
fbo.Bind();
fbo.CreateRenderBuffer(GL_COLOR_ATTACHMENT0_EXT, GL_RGBA8, 16, 16);
fbo.CreateRenderBuffer(GL_DEPTH_ATTACHMENT_EXT, GL_DEPTH_COMPONENT24, 16, 16);
support24bitDepthBuffer = (fbo.GetStatus() == GL_FRAMEBUFFER_COMPLETE_EXT);
fbo.Unbind();

//TODO figure out if needed
if (globalRendering->amdHacks) {
supportDepthBufferBits[3] = false; //32
supportDepthBufferBits[2] = false; //24
supportDepthBufferBestBits = 16;
}
#endif
}

void CGlobalRendering::QueryGLMaxVals()
Expand Down Expand Up @@ -827,7 +835,9 @@ void CGlobalRendering::LogVersionInfo(const char* sdlVersionStr, const char* glV
LOG("\tS3TC/DXT1 texture support : %i/%i", glewIsExtensionSupported("GL_EXT_texture_compression_s3tc"), glewIsExtensionSupported("GL_EXT_texture_compression_dxt1"));
LOG("\ttexture query-LOD support : %i (%i)", supportTextureQueryLOD, glewIsExtensionSupported("GL_ARB_texture_query_lod"));
LOG("\tMSAA frame-buffer support : %i (%i)", supportMSAAFrameBuffer, glewIsExtensionSupported("GL_EXT_framebuffer_multisample"));
LOG("\t24-bit Z-buffer support : %i (-)", support24bitDepthBuffer);
LOG("\t16-bit Z-buffer support : %i (-)", supportDepthBufferBits[1]);
LOG("\t24-bit Z-buffer support : %i (-)", supportDepthBufferBits[2]);
LOG("\t32-bit Z-buffer support : %i (-)", supportDepthBufferBits[3]);
LOG("\tprimitive-restart support : %i (%i)", supportRestartPrimitive, glewIsExtensionSupported("GL_NV_primitive_restart"));
LOG("\tclip-space control support: %i (%i)", supportClipSpaceControl, glewIsExtensionSupported("GL_ARB_clip_control"));
LOG("\tseamless cube-map support : %i (%i)", supportSeamlessCubeMaps, glewIsExtensionSupported("GL_ARB_seamless_cube_map"));
Expand Down Expand Up @@ -1156,6 +1166,20 @@ void CGlobalRendering::InitGLState()
LogDisplayMode(sdlWindows[0]);
}

int CGlobalRendering::DepthBitsToFormat(int bits)
{
switch (bits)
{
case 16:
return GL_DEPTH_COMPONENT16;
case 24:
return GL_DEPTH_COMPONENT24;
case 32:
return GL_DEPTH_COMPONENT32;
}
return GL_DEPTH_COMPONENT;
}

/**
* @brief multisample verify
* @return whether verification passed
Expand Down
13 changes: 6 additions & 7 deletions rts/Rendering/GlobalRendering.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,9 @@ class CGlobalRendering {
bool supportTextureQueryLOD;

bool supportMSAAFrameBuffer;
/**
* @brief support24bitDepthBuffer
*
* if GL_DEPTH_COMPONENT24 is supported (many ATIs don't)
*/
bool support24bitDepthBuffer;

int supportDepthBufferBestBits;
bool supportDepthBufferBits[4];

bool supportRestartPrimitive;
bool supportClipSpaceControl;
Expand Down Expand Up @@ -309,7 +306,9 @@ class CGlobalRendering {
// [0] := primary, [1] := secondary (hidden)
SDL_Window* sdlWindows[2];
SDL_GLContext glContexts[2];

public:
//helper function
static int DepthBitsToFormat(int bits);
public:
/**
* @brief max view range in elmos
Expand Down
9 changes: 7 additions & 2 deletions rts/Rendering/ShadowHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,10 @@ bool CShadowHandler::InitDepthTarget()
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
} else {
GLint depthFormat = static_cast<GLint>(CGlobalRendering::DepthBitsToFormat(globalRendering->supportDepthBufferBestBits));

glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, shadowMapSize, shadowMapSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, shadowMapSize, shadowMapSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);

// Mesa complains about an incomplete FBO if calling Bind before TexImage (?)
shadowMapFBO.Bind();
Expand Down Expand Up @@ -331,7 +333,10 @@ bool CShadowHandler::WorkaroundUnsupportedFboRenderTargets()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
const GLint texFormat = globalRendering->support24bitDepthBuffer? GL_DEPTH_COMPONENT24: GL_DEPTH_COMPONENT16;

const int depthBits = std::min(globalRendering->supportDepthBufferBestBits, 24);
const GLint texFormat = CGlobalRendering::DepthBitsToFormat(depthBits);

glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
glTexImage2D(GL_TEXTURE_2D, 0, texFormat, shadowMapSize, shadowMapSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);

Expand Down

0 comments on commit 149ea5c

Please sign in to comment.