Skip to content

Commit 6198a07

Browse files
committed
OpenGL: no blending in blur, removed redundant BindBuffer and BufferData and ReleaseDC on unequip
1 parent a90304b commit 6198a07

File tree

2 files changed

+33
-61
lines changed

2 files changed

+33
-61
lines changed

src/render/opengl/render_opengl.cpp

+28-59
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ r_init(CmdLine *cmdln)
315315
r_ogl_state->wglCreateContextAttribsARB = (wgl_create_context_attribs_arb*)wglGetProcAddress("wglCreateContextAttribsARB");
316316
r_ogl_state->wglChoosePixelFormatARB = (wgl_choose_pixel_format_arb*)wglGetProcAddress("wglChoosePixelFormatARB");
317317

318-
ReleaseDC(r_ogl_state->fake_window, fake_dc);
318+
r_ogl_state->fake_window_dc = fake_dc;
319319

320320
if(!ok) {
321321
char buffer[256] = {0};
@@ -349,7 +349,7 @@ r_init(CmdLine *cmdln)
349349
#define WGL_SAMPLES_ARB 0x2042
350350

351351
internal void
352-
r_ogl_initialize_window(OS_Handle handle)
352+
r_ogl_initialize_window(OS_Handle handle, R_OGL_Window* window)
353353
{
354354
//- dmylo: map os window handle -> hwnd
355355
HWND hwnd = {0};
@@ -381,19 +381,18 @@ r_ogl_initialize_window(OS_Handle handle)
381381
DescribePixelFormat(window_dc, suggested_format_index, sizeof(PIXELFORMATDESCRIPTOR), &suggested_format);
382382
SetPixelFormat(window_dc, suggested_format_index, &suggested_format);
383383

384-
ReleaseDC(hwnd, window_dc);
384+
window->dc = window_dc;
385385
}
386386

387387
internal void
388-
r_ogl_initialize(OS_Handle handle)
388+
r_ogl_initialize(OS_Handle handle, R_OGL_Window *window)
389389
{
390390
//- dmylo: map os window handle -> hwnd
391391
HWND hwnd = {0};
392392
{
393393
W32_Window *w32_layer_window = w32_window_from_os_window(handle);
394394
hwnd = w32_hwnd_from_window(w32_layer_window);
395395
}
396-
HDC window_dc = GetDC(hwnd);
397396

398397
// Create a modern context with the first window DC
399398
int attribs[] =
@@ -408,9 +407,8 @@ r_ogl_initialize(OS_Handle handle)
408407
0
409408
};
410409

411-
HGLRC glrc = r_ogl_state->wglCreateContextAttribsARB(window_dc, 0, attribs);
412-
bool ok = wglMakeCurrent(window_dc, glrc);
413-
ReleaseDC(hwnd, window_dc);
410+
HGLRC glrc = r_ogl_state->wglCreateContextAttribsARB(window->dc, 0, attribs);
411+
bool ok = wglMakeCurrent(window->dc, glrc);
414412

415413
if(!ok) {
416414
char buffer[256] = {0};
@@ -424,8 +422,10 @@ r_ogl_initialize(OS_Handle handle)
424422

425423
//- dmylo: Now that we have a modern context, cleanup the fake context and the fake window.
426424
wglDeleteContext(r_ogl_state->fake_glrc);
425+
ReleaseDC(r_ogl_state->fake_window, r_ogl_state->fake_window_dc);
427426
DestroyWindow(r_ogl_state->fake_window);
428427
r_ogl_state->fake_glrc = 0;
428+
r_ogl_state->fake_window_dc = 0;
429429
r_ogl_state->fake_window = 0;
430430

431431
//- dmylo: Load function pointers.
@@ -452,7 +452,6 @@ r_ogl_initialize(OS_Handle handle)
452452
// buffers
453453
gl.GenBuffers(1, &r_ogl_state->instance_scratch_buffer_64kb);
454454
gl.BindBuffer(GL_ARRAY_BUFFER, r_ogl_state->instance_scratch_buffer_64kb);
455-
gl.BufferData(GL_ARRAY_BUFFER, KB(64), 0, GL_STREAM_DRAW);
456455

457456
// vao
458457
gl.GenVertexArrays(1, &r_ogl_state->rect_vao);
@@ -465,7 +464,6 @@ r_ogl_initialize(OS_Handle handle)
465464
// uniforms
466465
gl.GenBuffers(1, &r_ogl_state->rect_uniform_buffer);
467466
gl.BindBuffer(GL_UNIFORM_BUFFER, r_ogl_state->rect_uniform_buffer);
468-
gl.BufferData(GL_UNIFORM_BUFFER, sizeof(R_OGL_Uniforms_Rect), 0, GL_STREAM_DRAW);
469467
r_ogl_state->rect_uniform_block_index = gl.GetUniformBlockIndex(r_ogl_state->rect_shader, "Globals");
470468
}
471469

@@ -479,7 +477,6 @@ r_ogl_initialize(OS_Handle handle)
479477
// uniforms
480478
gl.GenBuffers(1, &r_ogl_state->blur_uniform_buffer);
481479
gl.BindBuffer(GL_UNIFORM_BUFFER, r_ogl_state->blur_uniform_buffer);
482-
gl.BufferData(GL_UNIFORM_BUFFER, sizeof(R_OGL_Uniforms_BlurPass), 0, GL_STREAM_DRAW);
483480
r_ogl_state->blur_uniform_block_index = gl.GetUniformBlockIndex(r_ogl_state->blur_shader, "Globals");
484481
r_ogl_state->blur_direction_uniform_location = gl.GetUniformLocation(r_ogl_state->blur_shader, "u_direction");
485482
}
@@ -541,14 +538,14 @@ r_window_equip(OS_Handle handle)
541538
window->generation += 1;
542539
}
543540

544-
r_ogl_initialize_window(handle);
541+
r_ogl_initialize_window(handle, window);
545542

546543
//- dmylo: we delay OpenGL state initialization to the first time a window
547544
// is created, because we need a window with a modern context to load
548545
// the functions we need.
549546
if(!r_ogl_state->initialized)
550547
{
551-
r_ogl_initialize(handle);
548+
r_ogl_initialize(handle, window);
552549
r_ogl_state->initialized = true;
553550
just_initialized = true;
554551
}
@@ -572,13 +569,21 @@ r_window_equip(OS_Handle handle)
572569
}
573570

574571
r_hook void
575-
r_window_unequip(OS_Handle window, R_Handle equip_handle)
572+
r_window_unequip(OS_Handle window_handle, R_Handle equip_handle)
576573
{
577574
ProfBeginFunction();
578575
OS_MutexScopeW(r_ogl_state->device_rw_mutex)
579576
{
577+
//- dmylo: map os window handle -> hwnd
578+
HWND hwnd = {0};
579+
{
580+
W32_Window *w32_layer_window = w32_window_from_os_window(window_handle);
581+
hwnd = w32_hwnd_from_window(w32_layer_window);
582+
}
583+
580584
R_OGL_Window *window = r_ogl_window_from_handle(equip_handle);
581585
window->generation += 1;
586+
ReleaseDC(hwnd, window->dc);
582587
SLLStackPush(r_ogl_state->first_free_window, window);
583588
}
584589
ProfEnd();
@@ -840,9 +845,7 @@ r_window_begin_frame(OS_Handle window_handle, R_Handle window_equip)
840845
}
841846

842847
//- dmylo: bind main context to the window.
843-
HDC window_dc = GetDC(hwnd);
844-
bool ok = wglMakeCurrent(window_dc, r_ogl_state->glrc);
845-
ReleaseDC(hwnd, window_dc);
848+
bool ok = wglMakeCurrent(window->dc, r_ogl_state->glrc);
846849

847850
//- dmylo: get resolution
848851
Rng2F32 client_rect = os_client_rect_from_window(window_handle);
@@ -928,7 +931,7 @@ r_window_end_frame(OS_Handle window_handle, R_Handle window_equip)
928931
ProfBeginFunction();
929932
OS_MutexScopeW(r_ogl_state->device_rw_mutex)
930933
{
931-
R_OGL_Window *wnd = r_ogl_window_from_handle(window_equip);
934+
R_OGL_Window *window = r_ogl_window_from_handle(window_equip);
932935

933936
////////////////////////////
934937
//- dmylo: finalize, by writing staging buffer out to window framebuffer
@@ -942,28 +945,20 @@ r_window_end_frame(OS_Handle window_handle, R_Handle window_equip)
942945
gl.Disable(GL_CULL_FACE);
943946
gl.Disable(GL_BLEND);
944947

945-
Vec2S32 resolution = wnd->last_resolution;
948+
Vec2S32 resolution = window->last_resolution;
946949
gl.Viewport(0, 0, (F32)resolution.x, (F32)resolution.y);
947950

948951
gl.UseProgram(r_ogl_state->finalize_shader);
949952

950953
gl.ActiveTexture(GL_TEXTURE0);
951-
gl.BindTexture(GL_TEXTURE_2D, wnd->stage_color);
954+
gl.BindTexture(GL_TEXTURE_2D, window->stage_color);
952955
gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
953956
gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
954957

955958
gl.DrawArrays(GL_TRIANGLE_STRIP, 0, 4);
956959
}
957960

958-
HWND hwnd = {0};
959-
{
960-
W32_Window *w32_layer_window = w32_window_from_os_window(window_handle);
961-
hwnd = w32_hwnd_from_window(w32_layer_window);
962-
}
963-
HDC window_dc = GetDC(hwnd);
964-
965-
SwapBuffers(window_dc);
966-
ReleaseDC(hwnd, window_dc);
961+
SwapBuffers(window->dc);
967962
}
968963
ProfEnd();
969964
}
@@ -1045,19 +1040,6 @@ r_window_submit(OS_Handle window, R_Handle window_equip, R_PassList *passes)
10451040
gl.BufferData(GL_ARRAY_BUFFER, batches->byte_count, dst_ptr, GL_STREAM_DRAW);
10461041

10471042
temp_end(temp);
1048-
1049-
//- dmylo: Equivalent mapping code, much slower on nvidia driver, likely forcing GPU synchronization
1050-
// instead of providing a new driver-managed buffer for some reason.
1051-
//
1052-
// gl.BindBuffer(GL_ARRAY_BUFFER, buffer);
1053-
// U8* dst_ptr = (U8*)gl.MapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
1054-
// U64 off = 0;
1055-
// for(R_BatchNode *batch_n = batches->first; batch_n != 0; batch_n = batch_n->next)
1056-
// {
1057-
// MemoryCopy(dst_ptr+off, batch_n->v.v, batch_n->v.byte_count);
1058-
// off += batch_n->v.byte_count;
1059-
// }
1060-
// gl.UnmapBuffer(GL_ARRAY_BUFFER);
10611043
}
10621044

10631045
for(U32 i = 0; i < 8; i++) {
@@ -1109,22 +1091,13 @@ r_window_submit(OS_Handle window, R_Handle window_equip, R_PassList *passes)
11091091
uniforms.xform_scale.y = length_2f32(xform_2x2_col1);
11101092
}
11111093

1094+
// dmylo: uniform
11121095
GLuint uniform_buffer = r_ogl_state->rect_uniform_buffer;
11131096
{
1114-
gl.BindBuffer(GL_UNIFORM_BUFFER, uniform_buffer);
1097+
gl.BindBufferBase(GL_UNIFORM_BUFFER, 0, uniform_buffer);
11151098
gl.BufferData(GL_UNIFORM_BUFFER, sizeof(uniforms), &uniforms, GL_STREAM_DRAW);
1116-
1117-
// dmylo: Equivalent mapping code, much slower on nvidia driver, likely forcing GPU synchronization
1118-
// instead of providing a new driver-managed buffer for some reason.
1119-
//
1120-
// U8* dst_ptr = (U8*)gl.MapBuffer(GL_UNIFORM_BUFFER, GL_WRITE_ONLY);
1121-
// MemoryCopy((U8 *)dst_ptr, &uniforms, sizeof(uniforms));
1122-
// gl.UnmapBuffer(GL_UNIFORM_BUFFER);
11231099
}
1124-
1125-
// dmylo: bind uniform buffer
11261100
gl.UniformBlockBinding(r_ogl_state->rect_shader, r_ogl_state->rect_uniform_block_index, 0);
1127-
gl.BindBufferBase(GL_UNIFORM_BUFFER, 0, uniform_buffer);
11281101

11291102
// dmylo: activate and bind texture
11301103
gl.ActiveTexture(GL_TEXTURE0);
@@ -1194,10 +1167,7 @@ r_window_submit(OS_Handle window, R_Handle window_equip, R_PassList *passes)
11941167
gl.Disable(GL_STENCIL_TEST);
11951168
gl.Disable(GL_SCISSOR_TEST);
11961169
gl.Disable(GL_CULL_FACE);
1197-
1198-
// dmylo: blending
1199-
gl.Enable(GL_BLEND);
1200-
gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1170+
gl.Disable(GL_BLEND);
12011171

12021172
Vec2S32 resolution = wnd->last_resolution;
12031173
gl.Viewport(0, 0, (F32)resolution.x, (F32)resolution.y);
@@ -1223,10 +1193,9 @@ r_window_submit(OS_Handle window, R_Handle window_equip, R_PassList *passes)
12231193

12241194
GLuint uniform_buffer = r_ogl_state->blur_uniform_buffer;
12251195
{
1226-
gl.BindBuffer(GL_UNIFORM_BUFFER, uniform_buffer);
1196+
gl.BindBufferBase(GL_UNIFORM_BUFFER, 0, uniform_buffer);
12271197
gl.BufferData(GL_UNIFORM_BUFFER, sizeof(uniforms), &uniforms, GL_STREAM_DRAW);
12281198
gl.UniformBlockBinding(r_ogl_state->blur_shader, r_ogl_state->blur_uniform_block_index, 0);
1229-
gl.BindBufferBase(GL_UNIFORM_BUFFER, 0, uniform_buffer);
12301199
}
12311200

12321201
// dmylo: Horizontal pass

src/render/opengl/render_opengl.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ struct R_OGL_Window
9999
GLuint geo3d_depth;
100100

101101
Vec2S32 last_resolution;
102+
103+
HDC dc;
102104
};
103105

104106
struct R_OGL_FlushBuffer
@@ -139,6 +141,7 @@ struct R_OGL_State
139141
wgl_choose_pixel_format_arb *wglChoosePixelFormatARB;
140142
HGLRC glrc;
141143
HWND fake_window;
144+
HDC fake_window_dc;
142145
HGLRC fake_glrc;
143146

144147
// dmylo: state
@@ -208,8 +211,8 @@ internal R_Handle r_ogl_handle_from_buffer(R_OGL_Buffer *buffer);
208211
internal GLuint r_ogl_instance_buffer_from_size(U64 size);
209212
internal GLuint r_ogl_compile_shader(String8 common, String8 src, GLenum kind);
210213
internal GLuint r_ogl_link_shaders(GLuint vs, GLuint fs);
211-
internal void r_ogl_initialize_window(OS_Handle handle);
212-
internal void r_ogl_initialize(OS_Handle handle);
214+
internal void r_ogl_initialize_window(OS_Handle handle, R_OGL_Window* window);
215+
internal void r_ogl_initialize(OS_Handle handle, R_OGL_Window* window);
213216
internal void r_ogl_upload_buffer(R_OGL_Buffer *buffer);
214217
internal void r_ogl_upload_texture(R_OGL_Tex2D *texture);
215218
internal void r_ogl_fill_tex2d_region(R_OGL_Tex2D *texture);

0 commit comments

Comments
 (0)