Skip to content

Commit 90a5af6

Browse files
authored
Merge pull request #1186 from asarium/fix/976
Fix wrong usage of cockpit depth buffer
2 parents ce519f0 + 5d7f10d commit 90a5af6

File tree

12 files changed

+133
-46
lines changed

12 files changed

+133
-46
lines changed

code/graphics/opengl/gropengl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,11 @@ void gr_opengl_pop_debug_group() {
16581658
glPopDebugGroup();
16591659
}
16601660
}
1661+
void opengl_set_object_label(GLenum type, GLuint handle, const SCP_string& name) {
1662+
if (GLAD_GL_KHR_debug) {
1663+
glObjectLabelKHR(type, handle, (GLsizei) name.size(), name.c_str());
1664+
}
1665+
}
16611666

16621667
uint opengl_data_type_size(GLenum data_type)
16631668
{

code/graphics/opengl/gropengl.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ int opengl_check_for_errors(const char *err_at = NULL);
2424
bool gr_opengl_is_capable(gr_capability capability);
2525
void gr_opengl_push_debug_group(const char* name);
2626
void gr_opengl_pop_debug_group();
27+
28+
/**
29+
* @brief Assigns a string name to the specified handle
30+
* @details This uses @c GL_KHR_debug for assigning a human readable name to an OpenGL object. This can help with debugging
31+
* since it makes it easier to identify which object is currently being used.
32+
* @param type The type of the handle, e.g. GL_FRAMEBUFFER
33+
* @param handle The handle of the object
34+
* @param name The name of the object
35+
*/
36+
void opengl_set_object_label(GLenum type, GLuint handle, const SCP_string& name);
37+
2738
uint opengl_data_type_size(GLenum data_type);
2839

2940
#ifndef NDEBUG

code/graphics/opengl/gropengldraw.cpp

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,8 @@ void opengl_setup_scene_textures()
15761576

15771577
// create framebuffer
15781578
glGenFramebuffers(1, &Scene_framebuffer);
1579-
glBindFramebuffer(GL_FRAMEBUFFER, Scene_framebuffer);
1579+
GL_state.BindFrameBuffer(Scene_framebuffer);
1580+
opengl_set_object_label(GL_FRAMEBUFFER, Scene_framebuffer, "Scene framebuffer");
15801581

15811582
// setup main render texture
15821583

@@ -1596,6 +1597,7 @@ void opengl_setup_scene_textures()
15961597
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, Scene_texture_width, Scene_texture_height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
15971598

15981599
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Scene_color_texture, 0);
1600+
opengl_set_object_label(GL_TEXTURE, Scene_color_texture, "Scene color texture");
15991601

16001602
// setup low dynamic range color texture
16011603
glGenTextures(1, &Scene_ldr_texture);
@@ -1611,6 +1613,7 @@ void opengl_setup_scene_textures()
16111613
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
16121614

16131615
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, Scene_texture_width, Scene_texture_height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
1616+
opengl_set_object_label(GL_TEXTURE, Scene_ldr_texture, "Scene LDR texture");
16141617

16151618
// setup position render texture
16161619
glGenTextures(1, &Scene_position_texture);
@@ -1626,6 +1629,7 @@ void opengl_setup_scene_textures()
16261629
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
16271630

16281631
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, Scene_texture_width, Scene_texture_height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
1632+
opengl_set_object_label(GL_TEXTURE, Scene_position_texture, "Scene Position texture");
16291633

16301634
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, Scene_position_texture, 0);
16311635

@@ -1643,6 +1647,7 @@ void opengl_setup_scene_textures()
16431647
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
16441648

16451649
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, Scene_texture_width, Scene_texture_height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
1650+
opengl_set_object_label(GL_TEXTURE, Scene_normal_texture, "Scene Normal texture");
16461651

16471652
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, Scene_normal_texture, 0);
16481653

@@ -1660,6 +1665,7 @@ void opengl_setup_scene_textures()
16601665
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
16611666

16621667
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, Scene_texture_width, Scene_texture_height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
1668+
opengl_set_object_label(GL_TEXTURE, Scene_specular_texture, "Scene Specular texture");
16631669

16641670
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, Scene_specular_texture, 0);
16651671

@@ -1678,6 +1684,7 @@ void opengl_setup_scene_textures()
16781684
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
16791685

16801686
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, Scene_texture_width, Scene_texture_height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
1687+
opengl_set_object_label(GL_TEXTURE, Scene_luminance_texture, "Scene Luminance texture");
16811688

16821689
// setup effect texture
16831690

@@ -1694,6 +1701,7 @@ void opengl_setup_scene_textures()
16941701
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
16951702

16961703
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, Scene_texture_width, Scene_texture_height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
1704+
opengl_set_object_label(GL_TEXTURE, Scene_effect_texture, "Scene Effect texture");
16971705

16981706
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, GL_TEXTURE_2D, Scene_effect_texture, 0);
16991707

@@ -1712,6 +1720,8 @@ void opengl_setup_scene_textures()
17121720
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
17131721

17141722
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, Scene_texture_width, Scene_texture_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
1723+
opengl_set_object_label(GL_TEXTURE, Cockpit_depth_texture, "Cockpit depth texture");
1724+
17151725
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, Cockpit_depth_texture, 0);
17161726
gr_zbuffer_set(GR_ZBUFF_FULL);
17171727
glClear(GL_DEPTH_BUFFER_BIT);
@@ -1731,6 +1741,7 @@ void opengl_setup_scene_textures()
17311741
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
17321742

17331743
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, Scene_texture_width, Scene_texture_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
1744+
opengl_set_object_label(GL_TEXTURE, Scene_depth_texture, "Scene depth texture");
17341745
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, Scene_depth_texture, 0);
17351746

17361747
//setup main stencil buffer
@@ -1742,7 +1753,7 @@ void opengl_setup_scene_textures()
17421753
glReadBuffer(GL_COLOR_ATTACHMENT0);
17431754

17441755
if ( opengl_check_framebuffer() ) {
1745-
glBindFramebuffer(GL_FRAMEBUFFER, 0);
1756+
GL_state.BindFrameBuffer(0);
17461757
glDeleteFramebuffers(1, &Scene_framebuffer);
17471758
Scene_framebuffer = 0;
17481759

@@ -1779,7 +1790,7 @@ void opengl_setup_scene_textures()
17791790
if (Cmdline_fb_thrusters || Cmdline_fb_explosions)
17801791
{
17811792
glGenFramebuffers(1, &Distortion_framebuffer);
1782-
glBindFramebuffer(GL_FRAMEBUFFER, Distortion_framebuffer);
1793+
GL_state.BindFrameBuffer(Distortion_framebuffer);
17831794

17841795
glGenTextures(2, Distortion_texture);
17851796

@@ -1810,7 +1821,7 @@ void opengl_setup_scene_textures()
18101821

18111822

18121823
if ( opengl_check_framebuffer() ) {
1813-
glBindFramebuffer(GL_FRAMEBUFFER, 0);
1824+
GL_state.BindFrameBuffer(0);
18141825
glDeleteFramebuffers(1, &Distortion_framebuffer);
18151826
Distortion_framebuffer = 0;
18161827

@@ -1829,7 +1840,7 @@ void opengl_setup_scene_textures()
18291840
return;
18301841
}
18311842

1832-
glBindFramebuffer(GL_FRAMEBUFFER, 0);
1843+
GL_state.BindFrameBuffer(0);
18331844

18341845
Scene_texture_initialized = 1;
18351846
Scene_framebuffer_in_frame = false;
@@ -1900,7 +1911,11 @@ void gr_opengl_scene_texture_begin()
19001911
return;
19011912
}
19021913

1903-
glBindFramebuffer(GL_FRAMEBUFFER, Scene_framebuffer);
1914+
GR_DEBUG_SCOPE("Begin scene texture");
1915+
1916+
GL_state.PushFramebufferState();
1917+
GL_state.BindFrameBuffer(Scene_framebuffer);
1918+
//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, Scene_depth_texture, 0);
19041919

19051920
if (GL_rendering_to_texture)
19061921
{
@@ -1965,7 +1980,7 @@ void gr_opengl_scene_texture_end()
19651980
GLboolean blend = GL_state.Blend(GL_FALSE);
19661981
GLboolean cull = GL_state.CullFace(GL_FALSE);
19671982

1968-
glBindFramebuffer(GL_FRAMEBUFFER, opengl_get_rtt_framebuffer());
1983+
GL_state.PopFramebufferState();
19691984

19701985
GL_state.Texture.SetActiveUnit(0);
19711986
GL_state.Texture.SetTarget(GL_TEXTURE_2D);
@@ -2016,6 +2031,8 @@ void gr_opengl_copy_effect_texture()
20162031

20172032
void opengl_clear_deferred_buffers()
20182033
{
2034+
GR_DEBUG_SCOPE("Clear deferred buffers");
2035+
20192036
GLboolean depth = GL_state.DepthTest(GL_FALSE);
20202037
GLboolean depth_mask = GL_state.DepthMask(GL_FALSE);
20212038
GLboolean blend = GL_state.Blend(GL_FALSE);
@@ -2042,6 +2059,8 @@ void gr_opengl_deferred_lighting_begin()
20422059
if ( Cmdline_no_deferred_lighting)
20432060
return;
20442061

2062+
GR_DEBUG_SCOPE("Deferred lighting begin");
2063+
20452064
Deferred_lighting = true;
20462065
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
20472066

@@ -2053,6 +2072,9 @@ void gr_opengl_deferred_lighting_end()
20532072
{
20542073
if(!Deferred_lighting)
20552074
return;
2075+
2076+
GR_DEBUG_SCOPE("Deferred lighting end");
2077+
20562078
Deferred_lighting = false;
20572079
glDrawBuffer(GL_COLOR_ATTACHMENT0);
20582080

@@ -2067,6 +2089,7 @@ extern float static_tube_factor;
20672089

20682090
void gr_opengl_deferred_lighting_finish()
20692091
{
2092+
GR_DEBUG_SCOPE("Deferred lighting finish");
20702093
TRACE_SCOPE(tracing::ApplyLights);
20712094

20722095
if ( Cmdline_no_deferred_lighting ) {
@@ -2118,6 +2141,8 @@ void gr_opengl_deferred_lighting_finish()
21182141

21192142
for(int i = 0; i < Num_lights; ++i)
21202143
{
2144+
GR_DEBUG_SCOPE("Deferred apply single light");
2145+
21212146
light *l = &lights_copy[i];
21222147
Current_shader->program->Uniforms.setUniformi( "lightType", 0 );
21232148
switch(l->type)
@@ -2205,6 +2230,7 @@ void gr_opengl_deferred_lighting_finish()
22052230
GL_state.Texture.Enable(Scene_luminance_texture);
22062231

22072232
GL_state.SetAlphaBlendMode( ALPHA_BLEND_ADDITIVE );
2233+
GL_state.DepthMask(GL_FALSE);
22082234

22092235
opengl_draw_textured_quad(0.0f, 0.0f, 0.0f, Scene_texture_v_scale, (float)gr_screen.max_w, (float)gr_screen.max_h, Scene_texture_u_scale, 0.0f);
22102236

@@ -2266,7 +2292,9 @@ void gr_opengl_update_distortion()
22662292
GLboolean cull = GL_state.CullFace(GL_FALSE);
22672293

22682294
opengl_shader_set_passthrough(true, false);
2269-
glBindFramebuffer(GL_FRAMEBUFFER, Distortion_framebuffer);
2295+
2296+
GL_state.PushFramebufferState();
2297+
GL_state.BindFrameBuffer(Distortion_framebuffer);
22702298
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Distortion_texture[!Distortion_switch], 0);
22712299
glDrawBuffer(GL_COLOR_ATTACHMENT0);
22722300

@@ -2335,7 +2363,7 @@ void gr_opengl_update_distortion()
23352363
Distortion_switch = !Distortion_switch;
23362364

23372365
// reset state
2338-
glBindFramebuffer(GL_FRAMEBUFFER, Scene_framebuffer);
2366+
GL_state.PopFramebufferState();
23392367

23402368
glViewport(0,0,gr_screen.max_w,gr_screen.max_h);
23412369

@@ -2347,6 +2375,8 @@ void gr_opengl_update_distortion()
23472375

23482376
void opengl_render_primitives(primitive_type prim_type, vertex_layout* layout, int n_verts, int buffer_handle, size_t vert_offset, size_t byte_offset)
23492377
{
2378+
GR_DEBUG_SCOPE("Render primitives");
2379+
23502380
if ( buffer_handle >= 0 ) {
23512381
opengl_bind_buffer_object(buffer_handle);
23522382
} else {

code/graphics/opengl/gropengldraw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ inline void opengl_draw_textured_quad(
109109
GLfloat x1, GLfloat y1, GLfloat u1, GLfloat v1,
110110
GLfloat x2, GLfloat y2, GLfloat u2, GLfloat v2 )
111111
{
112+
GR_DEBUG_SCOPE("Draw textured quad");
113+
112114
GLfloat glVertices[4][4] = {
113115
{ x1, y1, u1, v1 },
114116
{ x1, y2, u1, v2 },

code/graphics/opengl/gropenglpostprocessing.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ void opengl_post_pass_bloom()
103103
// we need the scissor test disabled
104104
GLboolean scissor_test = GL_state.ScissorTest(GL_FALSE);
105105

106+
GL_state.PushFramebufferState();
107+
106108
// ------ begin bright pass ------
107109
int width, height;
108110
{
109111
GR_DEBUG_SCOPE("Bloom bright pass");
110112
TRACE_SCOPE(tracing::BloomBrightPass);
111113

112-
glBindFramebuffer(GL_FRAMEBUFFER, Bloom_framebuffer);
114+
GL_state.BindFrameBuffer(Bloom_framebuffer);
113115
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Bloom_textures[0], 0);
114116

115117
// width and height are 1/2 for the bright pass
@@ -224,7 +226,8 @@ void gr_opengl_post_process_begin()
224226
return;
225227
}
226228

227-
glBindFramebuffer(GL_FRAMEBUFFER, Post_framebuffer_id[0]);
229+
GL_state.PushFramebufferState();
230+
GL_state.BindFrameBuffer(Post_framebuffer_id[0]);
228231

229232
glDrawBuffer(GL_COLOR_ATTACHMENT0);
230233

@@ -383,6 +386,8 @@ void gr_opengl_post_process_end()
383386
// do tone mapping
384387
opengl_post_pass_tonemap();
385388

389+
GL_state.PopFramebufferState();
390+
386391
// Do FXAA
387392
if (Cmdline_fxaa && !fxaa_unavailable && !GL_rendering_to_texture) {
388393
opengl_post_pass_fxaa();
@@ -394,8 +399,8 @@ void gr_opengl_post_process_end()
394399
GR_DEBUG_SCOPE("Draw post effects");
395400
TRACE_SCOPE(tracing::DrawPostEffects);
396401

397-
// now write to the on-screen buffer
398-
glBindFramebuffer(GL_FRAMEBUFFER, opengl_get_rtt_framebuffer());
402+
// now write to the previous buffer
403+
GL_state.PopFramebufferState();
399404

400405
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
401406
glClear(GL_COLOR_BUFFER_BIT);
@@ -432,7 +437,7 @@ void gr_opengl_post_process_end()
432437
}
433438

434439
// now render it to the screen ...
435-
glBindFramebuffer(GL_FRAMEBUFFER,0);
440+
GL_state.BindFrameBuffer(0);
436441
GL_state.Texture.SetActiveUnit(0);
437442
GL_state.Texture.SetTarget(GL_TEXTURE_2D);
438443
//GL_state.Texture.Enable(Scene_color_texture);
@@ -585,6 +590,7 @@ void gr_opengl_post_process_set_defaults()
585590
extern GLuint Cockpit_depth_texture;
586591
void gr_opengl_post_process_save_zbuffer()
587592
{
593+
GR_DEBUG_SCOPE("Save z-Buffer");
588594
if (Post_initialized)
589595
{
590596
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, Cockpit_depth_texture, 0);
@@ -870,7 +876,7 @@ void opengl_setup_bloom_textures()
870876
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, MAX_MIP_BLUR_LEVELS-1);
871877
}
872878

873-
glBindFramebuffer(GL_FRAMEBUFFER, 0);
879+
GL_state.BindFrameBuffer(0);
874880
}
875881

876882
// generate and test the framebuffer and textures that we are going to use
@@ -896,7 +902,7 @@ static bool opengl_post_init_framebuffer()
896902
int size = (Cmdline_shadow_quality == 2 ? 1024 : 512);
897903

898904
glGenFramebuffers(1, &Post_shadow_framebuffer_id);
899-
glBindFramebuffer(GL_FRAMEBUFFER, Post_shadow_framebuffer_id);
905+
GL_state.BindFrameBuffer(Post_shadow_framebuffer_id);
900906

901907
glGenTextures(1, &Post_shadow_texture_id);
902908

@@ -947,7 +953,7 @@ static bool opengl_post_init_framebuffer()
947953
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, Post_shadow_depth_texture_id, 0);
948954
}
949955

950-
glBindFramebuffer(GL_FRAMEBUFFER, 0);
956+
GL_state.BindFrameBuffer(0);
951957

952958
rval = true;
953959

code/graphics/opengl/gropenglshader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ opengl_shader_t *Current_shader = NULL;
227227
void opengl_shader_set_current(opengl_shader_t *shader_obj)
228228
{
229229
if (Current_shader != shader_obj) {
230+
GR_DEBUG_SCOPE("Set shader");
231+
230232
GL_state.Array.ResetVertexAttribs();
231233

232234
if(shader_obj) {

code/graphics/opengl/gropenglstate.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ void opengl_state::init()
180180

181181
current_program = 0;
182182
glUseProgram(0);
183+
184+
current_framebuffer = 0;
185+
glBindFramebuffer(GL_FRAMEBUFFER, 0);
186+
187+
framebuffer_stack.clear();
183188
}
184189

185190
GLboolean opengl_state::Blend(GLint state)
@@ -508,6 +513,23 @@ void opengl_state::UseProgram(GLuint program)
508513
bool opengl_state::IsCurrentProgram(GLuint program) {
509514
return current_program == program;
510515
}
516+
void opengl_state::BindFrameBuffer(GLuint name) {
517+
if (current_framebuffer != name) {
518+
glBindFramebuffer(GL_FRAMEBUFFER, name);
519+
current_framebuffer = name;
520+
}
521+
}
522+
void opengl_state::PushFramebufferState() {
523+
framebuffer_stack.push_back(current_framebuffer);
524+
}
525+
void opengl_state::PopFramebufferState() {
526+
Assertion(framebuffer_stack.size() > 0, "Tried to pop the framebuffer state stack while it was empty!");
527+
528+
auto restoreBuffer = framebuffer_stack.back();
529+
framebuffer_stack.pop_back();
530+
531+
BindFrameBuffer(restoreBuffer);
532+
}
511533

512534
opengl_array_state::~opengl_array_state()
513535
{

0 commit comments

Comments
 (0)