1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500 | int Win32OpenGLAttribs[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
WGL_CONTEXT_MINOR_VERSION_ARB, 0,
WGL_CONTEXT_FLAGS_ARB, 0 // WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
#if BLACKBOX_INTERNAL
|WGL_CONTEXT_DEBUG_BIT_ARB
#endif
,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
0,
};
internal win32_thread_startup
Win32GetThreadStartupForGL(HDC OpenGLDC, HGLRC OpenGLRC)
{
win32_thread_startup Result = {};
Result.OpenGLDC = OpenGLDC;
if(wglCreateContextAttribsARB)
{
Result.OpenGLRC = wglCreateContextAttribsARB(OpenGLDC, OpenGLRC, Win32OpenGLAttribs);
}
return Result;
}
internal void
Win32SetPixelFormat(HDC WindowDC)
{
int SuggestedPixelFormatIndex = 0;
GLuint ExtendedPick = 0;
if(wglChoosePixelFormatARB)
{
int IntAttribList[] =
{
WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, // 0
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB, // 1
WGL_SUPPORT_OPENGL_ARB, GL_TRUE, // 2
#if HANDMADE_STREAMING
WGL_DOUBLE_BUFFER_ARB, GL_FALSE, // 3
#else
WGL_DOUBLE_BUFFER_ARB, GL_TRUE, // 3
#endif
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, // 4
WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, GL_TRUE, // 5
0,
};
if(!OpenGL.SupportsSRGBFramebuffer)
{
IntAttribList[10] = 0;
}
wglChoosePixelFormatARB(WindowDC, IntAttribList, 0, 1,
&SuggestedPixelFormatIndex, &ExtendedPick);
}
if(!ExtendedPick)
{
// TODO(casey): Hey Raymond Chen - what's the deal here?
// Is cColorBits ACTUALLY supposed to exclude the alpha bits, like MSDN says, or not?
PIXELFORMATDESCRIPTOR DesiredPixelFormat = {};
DesiredPixelFormat.nSize = sizeof(DesiredPixelFormat);
DesiredPixelFormat.nVersion = 1;
DesiredPixelFormat.iPixelType = PFD_TYPE_RGBA;
#if HANDMADE_STREAMING
// NOTE(casey): PFD_DOUBLEBUFFER appears to prevent OBS from reliably streaming the window
DesiredPixelFormat.dwFlags = PFD_SUPPORT_OPENGL|PFD_DRAW_TO_WINDOW;
#else
DesiredPixelFormat.dwFlags = PFD_SUPPORT_OPENGL|PFD_DRAW_TO_WINDOW|PFD_DOUBLEBUFFER;
#endif
DesiredPixelFormat.cColorBits = 32;
DesiredPixelFormat.cAlphaBits = 8;
DesiredPixelFormat.cDepthBits = 24;
DesiredPixelFormat.iLayerType = PFD_MAIN_PLANE;
SuggestedPixelFormatIndex = ChoosePixelFormat(WindowDC, &DesiredPixelFormat);
}
PIXELFORMATDESCRIPTOR SuggestedPixelFormat;
// NOTE(casey): Technically you do not need to call DescribePixelFormat here,
// as SetPixelFormat doesn't actually need it to be filled out properly.
DescribePixelFormat(WindowDC, SuggestedPixelFormatIndex,
sizeof(SuggestedPixelFormat), &SuggestedPixelFormat);
SetPixelFormat(WindowDC, SuggestedPixelFormatIndex, &SuggestedPixelFormat);
}
internal void
Win32LoadWGLExtensions(void)
{
WNDCLASSA WindowClass = {};
WindowClass.lpfnWndProc = DefWindowProcA;
WindowClass.hInstance = GetModuleHandle(0);
WindowClass.lpszClassName = "HandmadeWGLLoader";
if(RegisterClassA(&WindowClass))
{
HWND Window = CreateWindowExA(
0,
WindowClass.lpszClassName,
"Handmade Hero",
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
WindowClass.hInstance,
0);
HDC WindowDC = GetDC(Window);
Win32SetPixelFormat(WindowDC);
HGLRC OpenGLRC = wglCreateContext(WindowDC);
if(wglMakeCurrent(WindowDC, OpenGLRC))
{
wglChoosePixelFormatARB =
(wgl_choose_pixel_format_arb *)wglGetProcAddress("wglChoosePixelFormatARB");
wglCreateContextAttribsARB =
(wgl_create_context_attribs_arb *)wglGetProcAddress("wglCreateContextAttribsARB");
wglSwapIntervalEXT = (wgl_swap_interval_ext *)wglGetProcAddress("wglSwapIntervalEXT");
wglGetExtensionsStringEXT = (wgl_get_extensions_string_ext *)wglGetProcAddress("wglGetExtensionsStringEXT");
if(wglGetExtensionsStringEXT)
{
char *Extensions = (char *)wglGetExtensionsStringEXT();
char *At = Extensions;
while(*At)
{
while(IsWhitespace(*At)) {++At;}
char *End = At;
while(*End && !IsWhitespace(*End)) {++End;}
umm Count = End - At;
if(0) {}
else if(StringsAreEqual(Count, At, "WGL_EXT_framebuffer_sRGB")) {OpenGL.SupportsSRGBFramebuffer = true;}
else if(StringsAreEqual(Count, At, "WGL_ARB_framebuffer_sRGB")) {OpenGL.SupportsSRGBFramebuffer = true;}
At = End;
}
}
wglMakeCurrent(0, 0);
}
wglDeleteContext(OpenGLRC);
ReleaseDC(Window, WindowDC);
DestroyWindow(Window);
}
}
internal HGLRC
Win32InitOpenGL(HDC WindowDC)
{
Win32LoadWGLExtensions();
b32 ModernContext = true;
HGLRC OpenGLRC = 0;
if(wglCreateContextAttribsARB)
{
Win32SetPixelFormat(WindowDC);
OpenGLRC = wglCreateContextAttribsARB(WindowDC, 0, Win32OpenGLAttribs);
}
if(!OpenGLRC)
{
ModernContext = false;
OpenGLRC = wglCreateContext(WindowDC);
}
if(wglMakeCurrent(WindowDC, OpenGLRC))
{
opengl_info Info = OpenGLGetInfo(ModernContext);
if(Info.GL_ARB_framebuffer_object)
{
}
glBindFramebuffer = (gl_bind_framebuffer *)wglGetProcAddress("glBindFramebuffer");
glGenFramebuffers = (gl_gen_framebuffers *)wglGetProcAddress("glGenFramebuffers");
glFramebufferTexture2D = (gl_framebuffer_texture_2D *)wglGetProcAddress("glFramebufferTexture2D");
glCheckFramebufferStatus = (gl_check_framebuffer_status *)wglGetProcAddress("glCheckFramebufferStatus");
glUniform1f = (gl_uniform1f *)wglGetProcAddress("glUniform1f");
glUniform2f = (gl_uniform2f *)wglGetProcAddress("glUniform2f");
glUniform3f = (gl_uniform3f *)wglGetProcAddress("glUniform3f");
glUniform4f = (gl_uniform4f *)wglGetProcAddress("glUniform4f");
glValidateProgram = (gl_validate_program *)wglGetProcAddress("glValidateProgram");
glGetProgramInfoLog = (gl_get_program_info_log *)wglGetProcAddress("glGetProgramInfoLog");
glGetProgramiv = (gl_get_programiv *)wglGetProcAddress("glGetProgramiv");
glGetShaderInfoLog = (gl_get_shader_info_log *)wglGetProcAddress("glGetShaderInfoLog");
glAttachShader = (gl_attach_shader *)wglGetProcAddress("glAttachShader");
glCompileShader = (gl_compile_shader *)wglGetProcAddress("glCompileShader");
glCreateProgram = (gl_create_program *)wglGetProcAddress("glCreateProgram");
glCreateShader = (gl_create_shader *)wglGetProcAddress("glCreateShader");
glLinkProgram = (gl_link_program *)wglGetProcAddress("glLinkProgram");
glShaderSource = (gl_shader_source *)wglGetProcAddress("glShaderSource");
glUseProgram = (gl_use_program *)wglGetProcAddress("glUseProgram");
glGetUniformLocation = (gl_get_uniform_location *)wglGetProcAddress("glGetUniformLocation");
glUniform4iv = (gl_uniform4iv *)wglGetProcAddress("glUniform4iv");
glUniformMatrix4fv = (gl_uniform_matrix4fv *)wglGetProcAddress("glUniformMatrix4fv");
glBindBuffer = (gl_bind_buffer *)wglGetProcAddress("glBindBuffer");
glGenBuffers = (gl_gen_buffers *)wglGetProcAddress("glGenBuffers");
glBufferData = (gl_buffer_data *)wglGetProcAddress("glBufferData");
glVertexAttribPointer = (gl_vertex_attrib_pointer *)wglGetProcAddress("glVertexAttribPointer");
glEnableVertexAttribArray = (gl_enable_vertex_attrib_array *)wglGetProcAddress("glEnableVertexAttribArray");
glGenVertexArrays = (gl_gen_vertex_arrays *)wglGetProcAddress("glGenVertexArrays");
glBindVertexArray = (gl_bind_vertex_array *)wglGetProcAddress("glBindVertexArray");
glGetAttribLocation = (gl_get_attrib_location *)wglGetProcAddress("glGetAttribLocation");
glGetUniformfv = (gl_get_uniformfv *)wglGetProcAddress("glGetUniformfv");
glGetUniformiv = (gl_get_uniformiv *)wglGetProcAddress("glGetUniformiv");
glGenFramebuffers = (gl_gen_framebuffers *)wglGetProcAddress("glGenFramebuffers");
if(wglSwapIntervalEXT)
{
wglSwapIntervalEXT(1);
}
OpenGLInit(Info, OpenGL.SupportsSRGBFramebuffer);
}
return(OpenGLRC);
}
internal GLuint
OpenGLCreateProgram(char *HeaderCode, char *VertexCode, char *FragmentCode)
{
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLchar *VertexShaderCode[] =
{
HeaderCode,
VertexCode,
};
glShaderSource(VertexShaderID, ArrayCount(VertexShaderCode), VertexShaderCode, 0);
glCompileShader(VertexShaderID);
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
GLchar *FragmentShaderCode[] =
{
HeaderCode,
FragmentCode,
};
glShaderSource(VertexShaderID, ArrayCount(FragmentShaderCode), FragmentShaderCode, 0);
glCompileShader(FragmentShaderID);
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
glValidateProgram(ProgramID);
GLint Link = false;
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Link);
if(!Link)
{
GLsizei Ignored;
char VertexErrors[4096];
char FragmentErrors[4096];
char ProgramErrors[4096];
glGetShaderInfoLog(VertexShaderID, sizeof(VertexErrors), &Ignored, VertexErrors);
glGetShaderInfoLog(FragmentShaderID, sizeof(FragmentErrors), &Ignored, FragmentErrors);
glGetShaderInfoLog(ProgramID, sizeof(ProgramErrors), &Ignored, ProgramErrors);
Assert(!"Shader Validation Failed!!!!!");
}
return ProgramID;
}
internal void
OpenGLInit(b32 ModernContext)
{
OpenGLDefaultInternalTextureFormat = GL_RGBA8;
opengl_info Info = OpenGLGetInfo(ModernContext);
if(Info.GL_EXT_texture_sRGB)
{
OpenGLDefaultInternalTextureFormat = GL_SRGB8_ALPHA8;
}
if(Info.GL_EXT_framebuffer_sRGB)
{
//glEnable(GL_FRAMEBUFFER_SRGB);
}
}
internal void
OpenGLDrawRect(v2 Min, v2 Dim, v4 Color)
{
glBegin(GL_TRIANGLES);
glColor4f(Color.r, Color.g, Color.b, Color.a);
v2 Max = Min + Dim;
glTexCoord2f(0.0f, 0.0f);
glVertex2f(Min.x, Min.y);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(Max.x, Min.y);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(Max.x, Max.y);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(Max.x, Max.y);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(Min.x, Max.y);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(Min.x, Min.y);
glEnd();
}
internal void
OpenGLSetScreenspace(s32 Width, s32 Height)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
f32 A = SafeRatio1(2.0f, (f32)Width);
f32 B = SafeRatio1(2.0f, (f32)Height);
f32 Proj[] =
{
A, 0, 0, 0,
0, B, 0, 0,
0, 0, 1, 0,
-1, -1, 0, 1
};
glLoadMatrixf(Proj);
}
internal void
OpenGLRenderCommands(render_commands *Commands, s32 WindowWidth, s32 WindowHeight, rect2i DrawRegion)
{
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
f32 A = SafeRatio1(2.0f, (f32)WindowWidth);
f32 B = SafeRatio1(2.0f, (f32)WindowHeight);
f32 Proj[] =
{
A, 0, 0, 0,
0, B, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
glLoadMatrixf(Proj);
glViewport(0, 0, 2560, 1440);
GLint ViewData[4] = {};
glGetIntegerv(GL_VIEWPORT, ViewData);
for(u32 BaseAddress = 0;
BaseAddress < Commands->PushBufferSize;
)
{
render_group_entry_header *Header = (render_group_entry_header *)(Commands->PushBufferBase + BaseAddress);
BaseAddress += sizeof(*Header);
void *Data = Header + 1;
switch(Header->Type)
{
case RenderGroupEntryType_render_entry_bitmap:
{
render_entry_bitmap *Entry = (render_entry_bitmap *)Data;
v2 XAxis = { 1, 0 };
v2 YAxis = { 0, 1 };
v2 MinP = Entry->P;
v2 MaxP = MinP + Entry->Size.x*XAxis + Entry->Size.y*XAxis;
glBindTexture(GL_TEXTURE_2D, (GLuint)Entry->Bitmap->TextureHandle);
glUseProgram(OpenGL.BasicZBiasProg);
//glUniform4f(OpenGL.TransformID, 1, GL_FALSE, Proj);
OpenGLDrawRect(Entry->P, Entry->Size, Entry->Color);
glUseProgram(0);
} break;
case RenderGroupEntryType_render_entry_clear:
{
render_entry_clear *Entry = (render_entry_clear *)Data;
glDisable(GL_TEXTURE_2D);
OpenGLDrawRect(V2(0.0f, 0.0f), Entry->Dim, Entry->Color);
glEnable(GL_TEXTURE_2D);
BaseAddress += sizeof(*Entry);
} break;
case RenderGroupEntryType_render_entry_rect:
{
render_entry_rect *Entry = (render_entry_rect *)Data;
glDisable(GL_TEXTURE_2D);
//glUseProgram(OpenGL.BasicZBiasProg);
//glUniformMatrix4fv(OpenGL.TransformID, 1, GL_FALSE, Proj);
OpenGLDrawRect(Entry->P, Entry->Dim, Entry->Color);
glUseProgram(0);
glEnable(GL_TEXTURE_2D);
BaseAddress += sizeof(*Entry);
} break;
//InvalidDefaultCase;
}
}
#if 0
glBindFramebuffer(GL_READ_FRAMEBUFFER, GlobalFramebufferHandles);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glViewport(DrawRegion.Min.x, DrawRegion.Min.y, WindowWidth, WindowHeight);
glBlitFramebuffer(0, 0, GetWidth(DrawRegion), GetHeight(DrawRegion),
DrawRegion.Min.x, DrawRegion.Min.y,
DrawRegion.Max.x, DrawRegion.Max.y,
GL_COLOR_BUFFER_BIT,
GL_LINEAR);
#endif
}
internal opengl_info
OpenGLInit(opengl_info Info, b32 FramebufferSupportsSRGB)
{
glGenTextures(1, &OpenGL.ReservedBlitTexture);
// NOTE(casey): If we believe we can do full sRGB on the texture side
// and the framebuffer side, then we can enable it, otherwise it is
// safer for us to pass it straight through.
OpenGL.DefaultInternalTextureFormat = GL_RGBA8;
if(FramebufferSupportsSRGB && Info.GL_EXT_texture_sRGB && Info.GL_EXT_framebuffer_sRGB)
{
OpenGL.DefaultInternalTextureFormat = GL_SRGB8_ALPHA8;
//glEnable(GL_FRAMEBUFFER_SRGB);
}
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
char *HeaderCode = R"FOO(
// Header code
#version 330
)FOO";
char *VertexCode = R"FOO(
// Vertex code
uniform mat4x4 Transform;
void main()
{
gl_Position = Transform*gl_Position;
}
)FOO";
char *FragmentCode = R"FOO(
// Fragment code
out vec4 Color;
void main()
{
Color = vec4(1.0, 0.0, 0.0, 1.0);
}
)FOO";
OpenGL.BasicZBiasProg = OpenGLCreateProgram(HeaderCode, VertexCode, FragmentCode);
OpenGL.TransformID = glGetUniformLocation(OpenGL.BasicZBiasProg, "Transform");
return(Info);
}
|