Skip to content

Commit 619331f

Browse files
committed
REVIEWED: Issue with depth textures on WebGL #2824
1 parent 445ce51 commit 619331f

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/rlgl.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,8 @@ typedef struct rlglData {
987987
bool vao; // VAO support (OpenGL ES2 could not support VAO extension) (GL_ARB_vertex_array_object)
988988
bool instancing; // Instancing supported (GL_ANGLE_instanced_arrays, GL_EXT_draw_instanced + GL_EXT_instanced_arrays)
989989
bool texNPOT; // NPOT textures full support (GL_ARB_texture_non_power_of_two, GL_OES_texture_npot)
990-
bool texDepth; // Depth textures supported (GL_ARB_depth_texture, GL_WEBGL_depth_texture, GL_OES_depth_texture)
990+
bool texDepth; // Depth textures supported (GL_ARB_depth_texture, GL_OES_depth_texture)
991+
bool texDepthWebGL; // Depth textures supported WebGL specific (GL_WEBGL_depth_texture)
991992
bool texFloat32; // float textures support (32 bit per channel) (GL_OES_texture_float)
992993
bool texCompDXT; // DDS texture compression support (GL_EXT_texture_compression_s3tc, GL_WEBGL_compressed_texture_s3tc, GL_WEBKIT_WEBGL_compressed_texture_s3tc)
993994
bool texCompETC1; // ETC1 texture compression support (GL_OES_compressed_ETC1_RGB8_texture, GL_WEBGL_compressed_texture_etc1)
@@ -2218,11 +2219,12 @@ void rlLoadExtensions(void *loader)
22182219
if (strcmp(extList[i], (const char *)"GL_OES_texture_float") == 0) RLGL.ExtSupported.texFloat32 = true;
22192220

22202221
// Check depth texture support
2221-
if ((strcmp(extList[i], (const char *)"GL_OES_depth_texture") == 0) ||
2222-
(strcmp(extList[i], (const char *)"GL_WEBGL_depth_texture") == 0)) RLGL.ExtSupported.texDepth = true;
2222+
if (strcmp(extList[i], (const char *)"GL_OES_depth_texture") == 0) RLGL.ExtSupported.texDepth = true;
2223+
if (strcmp(extList[i], (const char *)"GL_WEBGL_depth_texture") == 0) RLGL.ExtSupported.texDepthWebGL = true; // WebGL requires unsized internal format
2224+
if (RLGL.ExtSupported.texDepthWebGL) RLGL.ExtSupported.texDepth = true;
22232225

2224-
if (strcmp(extList[i], (const char *)"GL_OES_depth24") == 0) RLGL.ExtSupported.maxDepthBits = 24;
2225-
if (strcmp(extList[i], (const char *)"GL_OES_depth32") == 0) RLGL.ExtSupported.maxDepthBits = 32;
2226+
if (strcmp(extList[i], (const char *)"GL_OES_depth24") == 0) RLGL.ExtSupported.maxDepthBits = 24; // Not available on WebGL
2227+
if (strcmp(extList[i], (const char *)"GL_OES_depth32") == 0) RLGL.ExtSupported.maxDepthBits = 32; // Not available on WebGL
22262228

22272229
// Check texture compression support: DXT
22282230
if ((strcmp(extList[i], (const char *)"GL_EXT_texture_compression_s3tc") == 0) ||
@@ -2975,23 +2977,28 @@ unsigned int rlLoadTexture(const void *data, int width, int height, int format,
29752977
}
29762978

29772979
// Load depth texture/renderbuffer (to be attached to fbo)
2978-
// WARNING: OpenGL ES 2.0 requires GL_OES_depth_texture/WEBGL_depth_texture extensions
2980+
// WARNING: OpenGL ES 2.0 requires GL_OES_depth_texture and WebGL requires WEBGL_depth_texture extensions
29792981
unsigned int rlLoadTextureDepth(int width, int height, bool useRenderBuffer)
29802982
{
29812983
unsigned int id = 0;
29822984

29832985
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
29842986
// In case depth textures not supported, we force renderbuffer usage
29852987
if (!RLGL.ExtSupported.texDepth) useRenderBuffer = true;
2986-
2988+
29872989
// NOTE: We let the implementation to choose the best bit-depth
29882990
// Possible formats: GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT32 and GL_DEPTH_COMPONENT32F
29892991
unsigned int glInternalFormat = GL_DEPTH_COMPONENT;
29902992

29912993
#if defined(GRAPHICS_API_OPENGL_ES2)
2992-
if (RLGL.ExtSupported.maxDepthBits == 32) glInternalFormat = GL_DEPTH_COMPONENT32_OES;
2993-
else if (RLGL.ExtSupported.maxDepthBits == 24) glInternalFormat = GL_DEPTH_COMPONENT24_OES;
2994-
else glInternalFormat = GL_DEPTH_COMPONENT16;
2994+
// WARNING: WebGL platform requires unsized internal format definition (GL_DEPTH_COMPONENT)
2995+
// while other platforms using OpenGL ES 2.0 require/support sized internal formats depending on the GPU capabilities
2996+
if (!RLGL.ExtSupported.texDepthWebGL)
2997+
{
2998+
if (RLGL.ExtSupported.maxDepthBits == 32) glInternalFormat = GL_DEPTH_COMPONENT32_OES;
2999+
else if (RLGL.ExtSupported.maxDepthBits == 24) glInternalFormat = GL_DEPTH_COMPONENT24_OES;
3000+
else glInternalFormat = GL_DEPTH_COMPONENT16;
3001+
}
29953002
#endif
29963003

29973004
if (!useRenderBuffer && RLGL.ExtSupported.texDepth)

0 commit comments

Comments
 (0)