Skip to content

Commit 46045a1

Browse files
authored
Fix FBO texture format in GLCore driver when using Cg/GLSL shaders (#18158)
1 parent e4f1f25 commit 46045a1

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

gfx/drivers/gl3.c

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ typedef struct gl3
123123
const float *vertex_ptr;
124124
bool active;
125125
bool mipmap_active;
126+
bool fp_fbo_supported;
127+
bool srgb_fbo_supported;
128+
bool force_srgb_disable;
126129
} chain;
127130

128131
#ifdef HAVE_SLANG
@@ -2342,6 +2345,8 @@ static void gl3_create_fbo_texture(gl3_t *gl,
23422345
GLint mag_filter;
23432346
GLint min_filter;
23442347
bool smooth;
2348+
bool fp_fbo = false;
2349+
bool srgb_fbo = false;
23452350

23462351
if (!gl->chain.shader->filter_type(gl->chain.shader_data, pass + 2, &smooth))
23472352
smooth = gl->video_info.smooth;
@@ -2352,11 +2357,40 @@ static void gl3_create_fbo_texture(gl3_t *gl,
23522357
: mag_filter;
23532358

23542359
glBindTexture(GL_TEXTURE_2D, texture);
2360+
23552361
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
23562362
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
23572363
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
23582364
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
2359-
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, gl->chain.fbo_rect[pass].width, gl->chain.fbo_rect[pass].height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2365+
2366+
fp_fbo = gl->chain.fbo_scale[pass].flags & FBO_SCALE_FLAG_FP_FBO;
2367+
if (fp_fbo && !gl->chain.fp_fbo_supported)
2368+
{
2369+
RARCH_ERR("[GLCore] Floating-point FBO was requested, but is not supported. Falling back to UNORM. Result may band/clip/etc.!\n");
2370+
fp_fbo = false;
2371+
}
2372+
if (fp_fbo)
2373+
{
2374+
RARCH_LOG("[GLCore] FBO pass #%d is floating-point.\n", pass);
2375+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, gl->chain.fbo_rect[pass].width, gl->chain.fbo_rect[pass].height, 0, GL_RGBA, GL_FLOAT, NULL);
2376+
}
2377+
else
2378+
{
2379+
srgb_fbo = gl->chain.fbo_scale[pass].flags & FBO_SCALE_FLAG_SRGB_FBO;
2380+
if (srgb_fbo && !gl->chain.srgb_fbo_supported)
2381+
{
2382+
RARCH_ERR("[GLCore] sRGB FBO was requested, but is not supported. Falling back to UNORM. Result may band/clip/etc.!\n");
2383+
srgb_fbo = false;
2384+
}
2385+
if (srgb_fbo && !gl->chain.force_srgb_disable)
2386+
{
2387+
RARCH_LOG("[GLCore] FBO pass #%d is sRGB.\n", pass);
2388+
glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, gl->chain.fbo_rect[pass].width, gl->chain.fbo_rect[pass].height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2389+
}
2390+
else
2391+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, gl->chain.fbo_rect[pass].width, gl->chain.fbo_rect[pass].height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2392+
}
2393+
23602394
glBindTexture(GL_TEXTURE_2D, 0);
23612395
}
23622396

@@ -2367,6 +2401,9 @@ static bool gl3_create_fbo_targets(gl3_t *gl)
23672401

23682402
gl->chain.active = true;
23692403
gl->chain.mipmap_active = gl->chain.shader->mipmap_input(gl->chain.shader_data, 1);
2404+
gl->chain.fp_fbo_supported = gl_check_capability(GL_CAPS_FP_FBO);
2405+
gl->chain.srgb_fbo_supported = gl_check_capability(GL_CAPS_SRGB_FBO);
2406+
gl->chain.force_srgb_disable = config_get_ptr()->bools.video_force_srgb_disable;
23702407
gl->chain.fbo_feedback = 0;
23712408
gl->chain.fbo_feedback_pass = 0;
23722409
gl->chain.fbo_feedback_texture = 0;
@@ -3540,7 +3577,11 @@ static void gl3_renderchain_start_render(
35403577
* We will "flip" it in place on last pass. */
35413578
gl->chain.coords.vertex = fbo_vertexes;
35423579

3543-
glEnable(GL_FRAMEBUFFER_SRGB);
3580+
#ifndef HAVE_OPENGLES
3581+
if (gl->chain.srgb_fbo_supported)
3582+
glEnable(GL_FRAMEBUFFER_SRGB);
3583+
#endif
3584+
35443585
glBindTexture(GL_TEXTURE_2D, 0);
35453586
}
35463587

@@ -3630,7 +3671,10 @@ static void gl3_renderchain_render(
36303671
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
36313672
}
36323673

3633-
glDisable(GL_FRAMEBUFFER_SRGB);
3674+
#ifndef HAVE_OPENGLES
3675+
if (gl->chain.srgb_fbo_supported)
3676+
glDisable(GL_FRAMEBUFFER_SRGB);
3677+
#endif
36343678

36353679
/* Render our last FBO texture directly to screen. */
36363680
prev_rect = &gl->chain.fbo_rect[gl->chain.num_fbo_passes - 1];
@@ -3806,7 +3850,7 @@ static bool gl3_frame(void *data, const void *frame,
38063850

38073851
/* No point regenerating mipmaps
38083852
* if there are no new frames. */
3809-
if (gl->chain.mipmap_active)
3853+
if (gl->chain.active && gl->chain.mipmap_active)
38103854
{
38113855
glBindTexture(GL_TEXTURE_2D, texture.image);
38123856
glGenerateMipmap(GL_TEXTURE_2D);

0 commit comments

Comments
 (0)