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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
|
#include <initguid.h>
#include <d3d11_1.h>
#include <dxgi1_3.h>
#include <d3dcompiler.h>
#if !SHIP_MODE
#include <dxgidebug.h>
IDXGIDebug1* dxgi_debug;
#endif
#include "4ed_dx11_render.cpp"
internal LRESULT CALL_CONVENTION
win32_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
internal b32
win32_gl_create_window(HWND *wnd_out, DWORD style, RECT rect){
local_persist b32 srgb_support = false;
local_persist b32 first_call = true;
b32 result = false;
*wnd_out = 0;
Assert( g_dx11.initialized == 0 );
Assert( g_dx11.device == 0 );
Assert( g_dx11.context == 0 );
Assert( g_dx11.swap_chain == 0 );
Assert( g_dx11.sampler == 0 );
Assert( g_dx11.render_target_view == 0 );
Assert( g_dx11.gpu_program.vertex == 0 );
Assert( g_dx11.gpu_program.layout == 0 );
Assert( g_dx11.gpu_program.pixel == 0 );
Assert( g_dx11.gpu_program.valid == 0 );
Assert( g_dx11.vertex_buffer == 0 );
Assert( g_dx11.constants_buffer == 0 );
Assert( g_dx11.texture_count == 0 );
g_dx11 = { };
HINSTANCE this_instance = GetModuleHandle(0);
HWND wnd = 0;
ID3D11Device* base_device = 0;
ID3D11DeviceContext* base_device_context = 0;
IDXGIDevice1* dxgi_device = 0;
IDXGIAdapter* dxgi_adapter = 0;
IDXGIFactory2* dxgi_factory = 0;
ID3D11BlendState* blend_state = 0;
ID3D11RasterizerState1* rasterizer_state = 0;
do {
// NOTE(simon, 28/02/24): There is nothing in the code suggesting that this function could
// be called several time. If it is called several time, we would need to make sure that
// we cleaned up previous dx11 resources. The reason this function could be called twice
// would be if it failed previously, and in that case we clean up everything before
// exiting the function so we should be good. Still we assume it's only ever call once.
Assert( first_call );
if (first_call){
first_call = false;
log_os( " Registering graphics class...\n" );
WNDCLASSW wndclass = {};
wndclass.style = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
wndclass.lpfnWndProc = win32_proc;
wndclass.hIcon = LoadIconW(GetModuleHandle(0), L"main");
wndclass.hInstance = this_instance;
wndclass.lpszClassName = L"GRAPHICS-WINDOW-NAME";
if (RegisterClassW(&wndclass) == 0){
log_os(" Failed.\n");
break;
}
}
log_os( " Creating graphics window...\n" );
wnd = CreateWindowExW(0, L"GRAPHICS-WINDOW-NAME", L"GRAPHICS", style,
CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top,
0, 0, this_instance, 0);
if (wnd == 0) {
log_os( " Failed.\n" );
break;
}
log_os( " Creating a d3d11 hardware device and context...\n" );
// NOTE(simon, 28/02/24): We are creating a directx 11.1 device and context (supported
// since windows 8).
D3D_FEATURE_LEVEL feature_levels[ ] = { D3D_FEATURE_LEVEL_11_1 };
u32 device_flags = 0;
// device_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#if !SHIP_MODE
device_flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
HRESULT hr = D3D11CreateDevice( 0, D3D_DRIVER_TYPE_HARDWARE, 0, device_flags, feature_levels, ArrayCount( feature_levels ), D3D11_SDK_VERSION, &base_device, 0, &base_device_context );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
log_os( " Creating a d3d11 software (WARP) device and context...\n" );
// NOTE(simon, 28/02/24): Try creating a high performance software device as a fallback.
hr = D3D11CreateDevice( 0, D3D_DRIVER_TYPE_WARP, 0, device_flags, feature_levels, ArrayCount( feature_levels ), D3D11_SDK_VERSION, &base_device, 0, &base_device_context );
}
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
break;
}
log_os( " Creating a ID3D11Device1...\n" );
hr = base_device->QueryInterface( __uuidof( ID3D11Device1 ), ( void** ) &g_dx11.device );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
break;
}
log_os( " Creating a ID3D11DeviceContext1...\n" );
hr = base_device_context->QueryInterface( __uuidof( ID3D11DeviceContext1 ), ( void** ) &g_dx11.context );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
break;
}
ID3D11Device1* device = g_dx11.device;
ID3D11DeviceContext1* context = g_dx11.context;
#if !SHIP_MODE
log_os( " Getting ID3D11InfoQueue. This is not important if you're not debugging graphics...\n" );
ID3D11InfoQueue* info;
hr = device->QueryInterface( __uuidof( ID3D11InfoQueue ), ( void** ) &info );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
} else {
info->SetBreakOnSeverity( D3D11_MESSAGE_SEVERITY_CORRUPTION, TRUE );
info->SetBreakOnSeverity( D3D11_MESSAGE_SEVERITY_ERROR, TRUE );
info->Release( );
}
log_os( " Getting IDXGIDebug1. This is not important if you're not debugging graphics...\n" );
hr = DXGIGetDebugInterface1( 0, __uuidof( IDXGIDebug1 ), ( void** ) &dxgi_debug );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
}
#endif
// NOTE(simon, 28/02/24): sRGB should be supported by any hardware now, but there was a
// check so I added one. The OpenGL version never enables sRGB.
DXGI_FORMAT back_buffer_format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
UINT format_support = 0;
if ( SUCCEEDED( device->CheckFormatSupport( back_buffer_format, &format_support ) ) ) {
u32 required = D3D11_FORMAT_SUPPORT_RENDER_TARGET | D3D11_FORMAT_SUPPORT_DISPLAY;
srgb_support = ( ( format_support & required ) == required );
}
if ( !srgb_support ) {
log_os( " sRBG back buffer not supported.\n" );
back_buffer_format = DXGI_FORMAT_R8G8B8A8_UNORM;
} else {
log_os( " sRBG back buffer supported.\n" );
}
log_os( " Getting a IDXGIFactory2...\n" );
log_os( " Qurey for the IDXGIDevice1...\n" );
hr = device->QueryInterface( __uuidof( IDXGIDevice1 ), ( void** ) &dxgi_device );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
break;
}
log_os( " Getting the IDXGIAdapter...\n" );
dxgi_device->GetAdapter( &dxgi_adapter );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
break;
}
log_os( " Getting the IDXGIFactor2...\n" );
dxgi_adapter->GetParent( __uuidof( IDXGIFactory2 ), ( void** ) &dxgi_factory );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
break;
}
DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = { };
// NOTE(simon, 28/02/24): Can't request sRGB format here when using FLIP_* swap chain. It's
// requested when creating the render target view.
swap_chain_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swap_chain_desc.SampleDesc.Count = 1;
swap_chain_desc.SampleDesc.Quality = 0;
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swap_chain_desc.BufferCount = 2;
swap_chain_desc.Scaling = DXGI_SCALING_NONE;
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
log_os( " Creating a IDXGISwapChain1...\n" );
hr = dxgi_factory->CreateSwapChainForHwnd( device, wnd, &swap_chain_desc, 0, 0, &g_dx11.swap_chain );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
break;
}
log_os( " Prevent DXGI handling ALT + ENTER...\n" );
hr = dxgi_factory->MakeWindowAssociation( wnd, DXGI_MWA_NO_ALT_ENTER );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
}
// NOTE(simon, 28/02/24): We setup alpha blending here as it's always on in 4coder.
D3D11_BLEND_DESC blend_state_desc = { };
blend_state_desc.RenderTarget[ 0 ].BlendEnable = TRUE;
blend_state_desc.RenderTarget[ 0 ].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blend_state_desc.RenderTarget[ 0 ].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blend_state_desc.RenderTarget[ 0 ].BlendOp = D3D11_BLEND_OP_ADD;
blend_state_desc.RenderTarget[ 0 ].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
blend_state_desc.RenderTarget[ 0 ].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
blend_state_desc.RenderTarget[ 0 ].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blend_state_desc.RenderTarget[ 0 ].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
log_os( " Creating a blend state...\n" );
hr = device->CreateBlendState( &blend_state_desc, &blend_state );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
break;
}
context->OMSetBlendState( blend_state, 0, 0xffffffff );
// NOTE(simon, 28/02/24): Enable scissor and disable backface culling.
D3D11_RASTERIZER_DESC1 rasterizer_desc = { };
rasterizer_desc.FillMode = D3D11_FILL_SOLID;
rasterizer_desc.CullMode = D3D11_CULL_NONE;
rasterizer_desc.DepthClipEnable = TRUE;
rasterizer_desc.ScissorEnable = TRUE;
log_os( " Creating a rasterizer state...\n" );
hr = device->CreateRasterizerState1( &rasterizer_desc, &rasterizer_state );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
break;
}
context->RSSetState( rasterizer_state );
// NOTE(simon, 28/02/24): Not setting depth stencil as 4coder doesn't use it.
// NOTE(simon, 28/02/24): Swap interval is a parameter of swap_chain->present.
D3D11_SAMPLER_DESC linear_desc = { };
linear_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
linear_desc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
linear_desc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
linear_desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
linear_desc.ComparisonFunc = D3D11_COMPARISON_NEVER;
linear_desc.MinLOD = 0;
linear_desc.MaxLOD = D3D11_FLOAT32_MAX;
log_os( " Creating a sampler state...\n" );
hr = device->CreateSamplerState( &linear_desc, &g_dx11.sampler );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
break;
}
// NOTE(simon, 28/02/24): We create the vertex buffer, constants buffers and shader here
// because if we can't create them we won't be able to render anything and so we should
// just exit the program.
D3D11_BUFFER_DESC vertex_buffer_desc = { };
// NOTE(simon, 28/02/24): Reserving 400K vertices which is about 11 megabytes and would
// allow 100K characters. On a 1080p monitor, with 4 by 10 pixels characters we would need
// (1920/4)*(1080/10) = 51840 characters to fill the screen.
vertex_buffer_desc.ByteWidth = 400000 * sizeof( Render_Vertex );
vertex_buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
vertex_buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertex_buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
log_os( " Creating a vertex buffer...\n" );
hr = device->CreateBuffer( &vertex_buffer_desc, 0, &g_dx11.vertex_buffer );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
break;
}
D3D11_BUFFER_DESC constants_buffer_desc = { };
// NOTE(simon, 28/02/24): constants buffer size needs to be a multiple of 16.
// NOTE(simon, 28/02/24): The layout is explained where we set the values in the buffer in
// gl_render.
constants_buffer_desc.ByteWidth = 32;
constants_buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
constants_buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constants_buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
log_os( " Creating a constants buffer...\n" );
hr = device->CreateBuffer( &constants_buffer_desc, 0, &g_dx11.constants_buffer );
if ( FAILED( hr ) ) {
log_os( " Failed.\n" );
break;
}
g_dx11.gpu_program = gl__make_program( gl__vertex, gl__fragment );
if ( !g_dx11.gpu_program.valid ) {
break;
}
*wnd_out = wnd;
// NOTE(simon, 28/02/24): Reserve the first texture slot as a invalid/unbind texture.
g_dx11.texture_count = 1;
g_dx11.initialized = true;
result = true;
} while ( 0 );
if ( !result ) {
if ( wnd ) {
DestroyWindow( wnd );
( *wnd_out ) = 0;
}
if ( g_dx11.context ) {
g_dx11.context->OMSetBlendState( 0, 0, 0xffffffff );
g_dx11.context->RSSetState( 0 );
}
if ( g_dx11.constants_buffer ) {
g_dx11.constants_buffer->Release( );
g_dx11.constants_buffer = 0;
}
if ( g_dx11.vertex_buffer ) {
g_dx11.vertex_buffer->Release( );
g_dx11.vertex_buffer = 0;
}
if ( g_dx11.gpu_program.valid ) {
if ( g_dx11.gpu_program.vertex ) {
g_dx11.gpu_program.vertex->Release( );
g_dx11.gpu_program.vertex = 0;
}
if ( g_dx11.gpu_program.layout ) {
g_dx11.gpu_program.layout->Release( );
g_dx11.gpu_program.layout = 0;
}
if ( g_dx11.gpu_program.pixel ) {
g_dx11.gpu_program.pixel->Release( );
g_dx11.gpu_program.pixel = 0;
}
}
// NOTE(simon, 28/02/24): No render target view at this point as it's created in the
// WM_SIZE message.
if ( g_dx11.sampler ) {
g_dx11.sampler->Release( );
g_dx11.sampler = 0;
}
if ( g_dx11.swap_chain ) {
g_dx11.swap_chain->Release( );
g_dx11.swap_chain = 0;
}
if ( g_dx11.context ) {
g_dx11.context->Release( );
g_dx11.context = 0;
}
if ( g_dx11.device ) {
g_dx11.device->Release( );
g_dx11.device = 0;
}
g_dx11.initialized = false;
#if SHIP_MODE
os_popup_error( "Error", "Window creation failed.");
#endif
}
if( base_device ) {
base_device->Release( );
base_device = 0;
}
if ( base_device_context ) {
base_device_context->Release( );
base_device_context = 0;
}
if ( dxgi_device ) {
dxgi_device->Release( );
dxgi_device = 0;
}
if ( dxgi_adapter ) {
dxgi_adapter->Release( );
dxgi_adapter = 0;
}
if ( dxgi_factory ) {
dxgi_factory->Release( );
dxgi_factory = 0;
}
if ( blend_state ) {
blend_state->Release( );
blend_state = 0;
}
if ( rasterizer_state ) {
rasterizer_state->Release( );
rasterizer_state = 0;
}
return(result);
}
#if !SHIP_MODE
// NOTE(simon, 28/02/24): Only call this when working on 4coder, to make sure we don't do something
// stupid. In SHIP_MODE we let the os clean up resources.
internal void
win32_gl_cleanup( void ) {
if ( dxgi_debug && g_dx11.initialized ) {
OutputDebugString( L"win32_gl_cleanup start report...\n" );
dxgi_debug->ReportLiveObjects( DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL );
g_dx11.initialized = false;
if ( g_dx11.context ) {
g_dx11.context->OMSetBlendState( 0, 0, 0xffffffff );
g_dx11.context->RSSetState( 0 );
}
for ( u32 i = 1; i < g_dx11.texture_count; i++ ) {
DX11Texture* texture = g_dx11.textures + i;
if ( texture->view ) {
texture->view->Release( );
texture->view = 0;
}
if ( texture->pointer ) {
texture->pointer->Release( );
texture->pointer = 0;
}
}
g_dx11.texture_count = 0;
if ( g_dx11.constants_buffer ) {
g_dx11.constants_buffer->Release( );
g_dx11.constants_buffer = 0;
}
if ( g_dx11.vertex_buffer ) {
g_dx11.vertex_buffer->Release( );
g_dx11.vertex_buffer = 0;
}
if ( g_dx11.gpu_program.valid ) {
if ( g_dx11.gpu_program.vertex ) {
g_dx11.gpu_program.vertex->Release( );
g_dx11.gpu_program.vertex = 0;
}
if ( g_dx11.gpu_program.layout ) {
g_dx11.gpu_program.layout->Release( );
g_dx11.gpu_program.layout = 0;
}
if ( g_dx11.gpu_program.pixel ) {
g_dx11.gpu_program.pixel->Release( );
g_dx11.gpu_program.pixel = 0;
}
}
if ( g_dx11.render_target_view ) {
g_dx11.render_target_view->Release( );
g_dx11.render_target_view = 0;
}
if ( g_dx11.sampler ) {
g_dx11.sampler->Release( );
g_dx11.sampler = 0;
}
if ( g_dx11.swap_chain ) {
g_dx11.swap_chain->Release( );
g_dx11.swap_chain = 0;
}
if ( g_dx11.context ) {
g_dx11.context->Release( );
g_dx11.context = 0;
}
if ( g_dx11.device ) {
g_dx11.device->Release( );
g_dx11.device = 0;
}
OutputDebugString( L"win32_gl_cleanup end report (nothing printed after this line means everything is OK)...\n" );
dxgi_debug->ReportLiveObjects( DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL );
}
}
#endif
|