-
Notifications
You must be signed in to change notification settings - Fork 0
/
rjd_gfx_d3d11.h
1644 lines (1356 loc) · 58.4 KB
/
rjd_gfx_d3d11.h
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#define RJD_GFX_D3D11_H 1
#if RJD_IMPL && RJD_GFX_BACKEND_D3D11
#if !RJD_PLATFORM_WINDOWS
#error "D3D11 backend is only supported on Windows."
#endif
#if !RJD_GFX_H
#error "This header depends on rjd_gfx.h"
#endif
// This forward declaration is needed to suppress an order of declarations bug in the d3d headers.
// d3d11shader.h(454): warning C4115: 'ID3D11ModuleInstance': named type definition in parentheses
struct ID3D11ModuleInstance;
#define CINTERFACE
#define COBJMACROS
#include <d3d11_1.h>
#include <dxgi1_6.h>
#include <dxgidebug.h>
#include <d3dcompiler.h>
#undef CINTERFACE
#undef COBJMACROS
////////////////////////////////////////////////////////////////////////////////
// Local helpers
struct rjd_gfx_texture_d3d11
{
ID3D11Texture2D* texture;
ID3D11ShaderResourceView* resource_view;
ID3D11SamplerState* sampler;
struct rjd_strref* debug_name;
};
struct rjd_gfx_shader_d3d11
{
ID3DBlob* bytecode;
ID3D11VertexShader* vertex;
ID3D11PixelShader* pixel;
struct rjd_strref* debug_name;
struct rjd_strref* debug_source;
};
struct rjd_gfx_pipeline_state_d3d11
{
ID3D11InputLayout* vertex_layout;
ID3D11RasterizerState* rasterizer_state;
ID3D11DepthStencilState* depthstencil_state;
struct rjd_gfx_shader shader_vertex;
struct rjd_gfx_shader shader_pixel;
struct rjd_gfx_texture render_target;
struct rjd_gfx_texture depthstencil_target;
enum rjd_gfx_depth_compare depth_compare;
struct rjd_strref* debug_name;
};
struct rjd_gfx_mesh_buffer_d3d11
{
ID3D11Buffer* buffer;
UINT stride;
UINT offset;
UINT slot;
enum rjd_gfx_mesh_buffer_usage_flags usage_flags;
};
struct rjd_gfx_mesh_d3d11
{
struct rjd_gfx_mesh_buffer_d3d11* buffers;
uint32_t count_buffers;
uint32_t count_vertices;
enum rjd_gfx_primitive_type primitive;
};
struct rjd_gfx_command_buffer_d3d11
{
ID3D11DeviceContext1* deferred_context;
ID3D11RenderTargetView* render_target_view;
ID3D11DepthStencilView* depthstencil_view;
};
struct rjd_gfx_context_d3d11
{
IDXGIFactory4* factory;
IDXGIAdapter1* adapter;
ID3D11Device1* device;
ID3D11DeviceContext* context;
IDXGISwapChain1* swapchain;
struct rjd_gfx_texture_d3d11* slotmap_textures;
struct rjd_gfx_shader_d3d11* slotmap_shaders;
struct rjd_gfx_pipeline_state_d3d11* slotmap_pipeline_states;
struct rjd_gfx_mesh_d3d11* slotmap_meshes;
struct rjd_gfx_command_buffer_d3d11* slotmap_command_buffers;
ID3D11DeviceContext1** free_deferred_contexts;
ID3D11Texture2D** depthbuffers;
ID3D11Texture2D** msaa_depthbuffers;
struct rjd_gfx_texture* msaa_backbuffers;
struct rjd_mem_allocator* allocator;
struct rjd_strpool debug_names;
HWND hwnd;
bool is_occluded;
uint8_t num_backbuffers;
uint8_t backbuffer_index;
enum rjd_gfx_format backbuffer_color_format;
enum rjd_gfx_format backbuffer_depth_format;
};
RJD_STATIC_ASSERT(sizeof(struct rjd_gfx_context_d3d11) <= sizeof(struct rjd_gfx_context));
struct rjd_gfx_rgba
{
float v[4];
};
static struct rjd_result rjd_gfx_translate_hresult(HRESULT hr);
static DXGI_FORMAT rjd_gfx_format_to_dxgi(enum rjd_gfx_format format);
static DXGI_FORMAT rjd_gfx_format_to_dxgi_strip_srgb(enum rjd_gfx_format format);
static struct rjd_gfx_rgba rjd_gfx_format_value_to_rgba(struct rjd_gfx_format_value value);
static D3D_PRIMITIVE_TOPOLOGY rjd_gfx_primitive_to_d3d11(enum rjd_gfx_primitive_type primitive);
static D3D11_CULL_MODE rjd_gfx_cull_to_d3d(enum rjd_gfx_cull cull_mode);
static D3D11_COMPARISON_FUNC rjd_gfx_depth_compare_to_d3d11(enum rjd_gfx_depth_compare compare);
static D3D11_USAGE rjd_gfx_texture_access_to_gpu_access(enum rjd_gfx_texture_access access);
static D3D11_CPU_ACCESS_FLAG rjd_gfx_texture_access_to_cpu_access(enum rjd_gfx_texture_access access);
static const char* rjd_gfx_semantic_to_name(enum rjd_gfx_vertex_semantic semantic);
static bool rjd_gfx_texture_isbackbuffer(struct rjd_gfx_texture texture);
static struct rjd_result rjd_gfx_backbuffer_get_msaa_quality_d3d11(const struct rjd_gfx_context_d3d11* context, uint32_t sample_count, DXGI_SAMPLE_DESC* out);
static inline void rjd_gfx_texture_destroy_d3d11(struct rjd_gfx_context_d3d11* context, struct rjd_slot slot);
static inline void rjd_gfx_shader_destroy_d3d11(struct rjd_gfx_context_d3d11* context, struct rjd_slot slot);
static inline void rjd_gfx_pipeline_state_destroy_d3d11(struct rjd_gfx_context_d3d11* context, struct rjd_slot slot);
static inline void rjd_gfx_mesh_destroy_d3d11(struct rjd_gfx_context_d3d11* context, struct rjd_slot slot);
static inline void rjd_gfx_command_buffer_destroy_d3d11(struct rjd_gfx_context_d3d11* context, struct rjd_slot slot);
////////////////////////////////////////////////////////////////////////////////
// Local data
#if RJD_COMPILER_GCC
// The GCC headers have all the correct declarations for these GUIDs, but libdxguid.a are missing the definitions
const GUID IID_IDXGIFactory4 = { 0x1bc6ea02, 0xef36, 0x464f, { 0xbf,0x0c,0x21,0xca,0x39,0xe5,0x16,0x8a } };
const GUID IID_ID3D11Device1 = { 0xa04bfb29, 0x08ef, 0x43d6, { 0xa4,0x9c,0xa9,0xbd,0xbd,0xcb,0xe6,0x86 } };
#endif
////////////////////////////////////////////////////////////////////////////////
// interface implementation
struct rjd_result rjd_gfx_context_create(struct rjd_gfx_context* out, struct rjd_gfx_context_desc desc)
{
RJD_ASSERT(out);
IDXGIFactory4* factory = NULL;
{
UINT flags = 0;
flags |= DXGI_CREATE_FACTORY_DEBUG; // TODO make this optional
HRESULT hr = CreateDXGIFactory2(flags, &IID_IDXGIFactory4, (void**)&factory);
if (!SUCCEEDED(hr)) {
return rjd_gfx_translate_hresult(hr);
}
}
IDXGIAdapter1* adapter_extended = NULL;
struct rjd_result result_adapter = RJD_RESULT("No hardware adapter found. Does this machine have a GPU?");
for (UINT i = 0; DXGI_ERROR_NOT_FOUND != IDXGIFactory1_EnumAdapters1(factory, i, &adapter_extended); ++i) {
DXGI_ADAPTER_DESC1 desc_adapter;
IDXGIAdapter1_GetDesc1(adapter_extended, &desc_adapter);
if (desc_adapter.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) {
adapter_extended = NULL;
continue;
}
// desc_adapter.DedicatedVideoMemory
// desc_adapter.Description // wchar_t* name
result_adapter = RJD_RESULT_OK();
break;
}
if (!rjd_result_isok(result_adapter)) {
return result_adapter;
}
//IDXGIAdapter* adapter = NULL;
//if (adapter_extended) {
// IDXGIAdapter1_QueryInterface(adapter_extended, &IID_IDXGIAdapter, &adapter);
//}
ID3D11Device1* device = NULL;
ID3D11DeviceContext* context = NULL;
{
const UINT flags =
D3D11_CREATE_DEVICE_BGRA_SUPPORT | // enable compatibility with Direct2D.
D3D11_CREATE_DEVICE_DEBUG | // TODO make this optional
0;
const D3D_FEATURE_LEVEL feature_levels[] = {
D3D_FEATURE_LEVEL_11_1,
};
ID3D11Device* device_11_0 = NULL;
const HRESULT hr_device = D3D11CreateDevice(
//(IDXGIAdapter*)adapter, // TODO verify this cast is legit
NULL, // TODO replace this with the specified adapter
D3D_DRIVER_TYPE_HARDWARE,
NULL, // not using a software rasterizer
flags,
feature_levels,
rjd_countof(feature_levels),
D3D11_SDK_VERSION,
&device_11_0,
NULL,
&context);
if (FAILED(hr_device)) {
if (hr_device == DXGI_ERROR_SDK_COMPONENT_MISSING) {
if (flags & D3D11_CREATE_DEVICE_DEBUG) {
return RJD_RESULT("Correct debug layer version not available on this machine. "
"Make sure the DirectX SDK is up to date.");
}
else {
return RJD_RESULT("A SDK component was not available.");
}
}
else {
return rjd_gfx_translate_hresult(hr_device);
}
}
ID3D11Device1_QueryInterface(device_11_0, &IID_ID3D11Device1, (void**)&device);
ID3D11Device1_Release(device_11_0);
if (device == NULL) {
return RJD_RESULT("Unable to create 11.1 device. 11.0 is not supported.");
}
}
// DXGI doesn't support SRGB formats in the swapchain, but if the user wants one, we can still fake it
// by using SRGB views.
DXGI_FORMAT backbuffer_format = rjd_gfx_format_to_dxgi(desc.backbuffer_color_format);
{
if (backbuffer_format != DXGI_FORMAT_B8G8R8A8_UNORM &&
backbuffer_format != DXGI_FORMAT_R8G8B8A8_UNORM &&
backbuffer_format != DXGI_FORMAT_R16G16B16A16_FLOAT &&
backbuffer_format != DXGI_FORMAT_R10G10B10A2_UNORM) {
backbuffer_format = rjd_gfx_format_to_dxgi_strip_srgb(desc.backbuffer_color_format);
}
if (backbuffer_format != DXGI_FORMAT_B8G8R8A8_UNORM &&
backbuffer_format != DXGI_FORMAT_R8G8B8A8_UNORM &&
backbuffer_format != DXGI_FORMAT_R16G16B16A16_FLOAT &&
backbuffer_format != DXGI_FORMAT_R10G10B10A2_UNORM) {
return RJD_RESULT("The backbuffer format must be one of these: "
"DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R16G16B16A16_FLOAT, DXGI_FORMAT_R10G10B10A2_UNORM");
}
}
if (desc.num_backbuffers > 3) {
return RJD_RESULT("You specified more than 3 framebuffers Only 1-3 are supported.");
}
const UINT num_backbuffers = (desc.num_backbuffers == 0) ? 3 : desc.num_backbuffers;
IDXGISwapChain1* swapchain = NULL;
{
DXGI_RATIONAL refresh_rate = {
.Numerator = 0,
.Denominator = 1,
};
// note that the flip model isn't compatible with msaa so we'll have to render to an offscreen msaa
// target to get the same effect
DXGI_SAMPLE_DESC desc_msaa = {
.Count = 1,
.Quality = 0,
};
uint32_t flags = 0;
flags |= DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; // needed to switch between fullscreen/windowed
flags |= DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING; // needed to turn off vsync
DXGI_SWAP_CHAIN_DESC1 desc_swapchain = {
.Width = 0, // auto-detect
.Height = 0, // auto-detect
.Format = backbuffer_format,
.Stereo = false,
.SampleDesc = desc_msaa,
.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT,
.BufferCount = num_backbuffers,
.Scaling = DXGI_SCALING_STRETCH, // Don't stretch the backbuffer to fit the window size. We should handle that. // TODO figure out why DXGI_SCALING_NONE fails
.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD, // flip model has better perf and discard since we don't care to keep it around
.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED,
.Flags = flags,
};
DXGI_SWAP_CHAIN_FULLSCREEN_DESC desc_fullscreen = {
.RefreshRate = refresh_rate,
.Windowed = TRUE, // TODO
};
HRESULT hr_swap = IDXGIFactory2_CreateSwapChainForHwnd(
factory,
(IUnknown*)device,
desc.win32.hwnd,
&desc_swapchain,
&desc_fullscreen,
NULL, // don't restrict to a particular output of the adapter
&swapchain);
if (FAILED(hr_swap)) {
switch (hr_swap) {
case E_OUTOFMEMORY: return RJD_RESULT("Failed to create swapchain: out of memory");
case DXGI_ERROR_INVALID_CALL: RJD_ASSERTFAIL("We should detect this before it gets here."); break;
default: return RJD_RESULT("Failed to create swapchain.");
}
}
}
// Create backing depth buffers
ID3D11Texture2D** depthbuffers = rjd_mem_alloc_array(ID3D11Texture2D*, num_backbuffers, desc.allocator);
{
DXGI_FORMAT depth_format_dxgi = rjd_gfx_format_to_dxgi(desc.backbuffer_depth_format);
DXGI_SWAP_CHAIN_DESC1 swapchain_desc = {0};
HRESULT hr_swap = IDXGISwapChain1_GetDesc1(swapchain, &swapchain_desc);
if (FAILED(hr_swap)) {
return RJD_RESULT("Failed to get swapchain desc to get auto-detected backbuffer size.");
}
D3D11_TEXTURE2D_DESC desc_depthbuffer = {
.Width = swapchain_desc.Width,
.Height = swapchain_desc.Height,
.MipLevels = 1,
.ArraySize = 1,
.Format = depth_format_dxgi,
.SampleDesc.Count = 1,
.SampleDesc.Quality = 0,
.Usage = D3D11_USAGE_DEFAULT,
.BindFlags = D3D11_BIND_DEPTH_STENCIL,
.CPUAccessFlags = 0,
.MiscFlags = 0,
};
for (size_t i = 0; i < num_backbuffers; ++i) {
HRESULT hr = ID3D11Device_CreateTexture2D(device, &desc_depthbuffer, NULL, depthbuffers + i);
if (FAILED(hr)) {
return rjd_gfx_translate_hresult(hr);
}
}
}
memset(out, 0, sizeof(*out));
struct rjd_gfx_context_d3d11* context_d3d11 = (struct rjd_gfx_context_d3d11*)out;
context_d3d11->slotmap_textures = rjd_slotmap_alloc(struct rjd_gfx_texture_d3d11, 64, desc.allocator);
context_d3d11->slotmap_shaders = rjd_slotmap_alloc(struct rjd_gfx_shader_d3d11, 64, desc.allocator);
context_d3d11->slotmap_pipeline_states = rjd_slotmap_alloc(struct rjd_gfx_pipeline_state_d3d11, 64, desc.allocator);
context_d3d11->slotmap_meshes = rjd_slotmap_alloc(struct rjd_gfx_mesh_d3d11, 64, desc.allocator);
context_d3d11->slotmap_command_buffers = rjd_slotmap_alloc(struct rjd_gfx_command_buffer_d3d11, 16, desc.allocator);
context_d3d11->free_deferred_contexts = rjd_array_alloc(ID3D11DeviceContext1*, 1, desc.allocator);
context_d3d11->depthbuffers = depthbuffers;
context_d3d11->msaa_backbuffers = NULL;
context_d3d11->factory = factory;
context_d3d11->adapter = adapter_extended;
context_d3d11->device = device;
context_d3d11->context = context;
context_d3d11->swapchain = swapchain;
context_d3d11->allocator = desc.allocator;
context_d3d11->debug_names = rjd_strpool_init(desc.allocator, 128);
context_d3d11->hwnd = desc.win32.hwnd;
context_d3d11->is_occluded = false;
context_d3d11->num_backbuffers = (uint8_t)num_backbuffers;
context_d3d11->backbuffer_index = 0;
context_d3d11->backbuffer_color_format = desc.backbuffer_color_format;
context_d3d11->backbuffer_depth_format = desc.backbuffer_depth_format;
return RJD_RESULT_OK();
}
void rjd_gfx_context_destroy(struct rjd_gfx_context* context)
{
RJD_ASSERT(context);
struct rjd_gfx_context_d3d11* context_d3d11 = (struct rjd_gfx_context_d3d11*)context;
struct rjd_gfx_texture_d3d11* textures = context_d3d11->slotmap_textures;
struct rjd_gfx_shader_d3d11* shaders = context_d3d11->slotmap_shaders;
struct rjd_gfx_pipeline_state_d3d11* states = context_d3d11->slotmap_pipeline_states;
struct rjd_gfx_mesh_d3d11* meshes = context_d3d11->slotmap_meshes;
struct rjd_gfx_command_buffer_d3d11* commands = context_d3d11->slotmap_command_buffers;
for (struct rjd_slot s = rjd_slotmap_next(textures, NULL); rjd_slot_isvalid(s); s = rjd_slotmap_next(textures, &s)) {
rjd_gfx_texture_destroy_d3d11(context_d3d11, s);
}
for (struct rjd_slot s = rjd_slotmap_next(shaders, NULL); rjd_slot_isvalid(s); s = rjd_slotmap_next(shaders, &s)) {
rjd_gfx_shader_destroy_d3d11(context_d3d11, s);
}
for (struct rjd_slot s = rjd_slotmap_next(states, NULL); rjd_slot_isvalid(s); s = rjd_slotmap_next(states, &s)) {
rjd_gfx_pipeline_state_destroy_d3d11(context_d3d11, s);
}
for (struct rjd_slot s = rjd_slotmap_next(meshes, NULL); rjd_slot_isvalid(s); s = rjd_slotmap_next(meshes, &s)) {
rjd_gfx_mesh_destroy_d3d11(context_d3d11, s);
}
for (struct rjd_slot s = rjd_slotmap_next(commands, NULL); rjd_slot_isvalid(s); s = rjd_slotmap_next(commands, &s)) {
rjd_gfx_command_buffer_destroy_d3d11(context_d3d11, s);
}
rjd_slotmap_free(textures);
rjd_slotmap_free(shaders);
rjd_slotmap_free(states);
rjd_slotmap_free(meshes);
rjd_slotmap_free(commands);
rjd_strpool_free(&context_d3d11->debug_names);
for (size_t i = 0; i < rjd_array_count(context_d3d11->free_deferred_contexts); ++i) {
ID3D11DeviceContext1_Release(context_d3d11->free_deferred_contexts[i]);
}
rjd_array_free(context_d3d11->free_deferred_contexts);
for (size_t i = 0; i < context_d3d11->num_backbuffers; ++i) {
ID3D11Texture2D_Release(context_d3d11->depthbuffers[i]);
if (context_d3d11->msaa_depthbuffers) {
ID3D11Texture2D_Release(context_d3d11->msaa_depthbuffers[i]);
}
}
rjd_mem_free(context_d3d11->depthbuffers);
rjd_mem_free(context_d3d11->msaa_depthbuffers);
// freeing these textures got taken care of in the above resource cleanup
rjd_mem_free(context_d3d11->msaa_backbuffers);
IDXGISwapChain1_Release(context_d3d11->swapchain);
ID3D11DeviceContext_Release(context_d3d11->context);
ID3D11Device_Release(context_d3d11->device);
IDXGIAdapter1_Release(context_d3d11->adapter);
IDXGIFactory4_Release(context_d3d11->factory);
#if RJD_COMPILER_MSVC // GCC doesn't know about IDXGIDebug1
{
IDXGIDebug1* debug = NULL;
HRESULT hr = DXGIGetDebugInterface1(0, &IID_IDXGIDebug, &debug);
if (SUCCEEDED(hr)) {
IDXGIDebug_ReportLiveObjects(debug, DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_SUMMARY);
}
}
#endif
}
uint32_t rjd_gfx_backbuffer_current_index(const struct rjd_gfx_context* context)
{
RJD_ASSERT(context);
struct rjd_gfx_context_d3d11* context_d3d11 = (struct rjd_gfx_context_d3d11*)context;
return context_d3d11->backbuffer_index;
}
struct rjd_result rjd_gfx_backbuffer_msaa_is_count_supported(const struct rjd_gfx_context* context, uint32_t sample_count)
{
RJD_ASSERT(context);
struct rjd_gfx_context_d3d11* context_d3d11 = (struct rjd_gfx_context_d3d11*)context;
DXGI_SAMPLE_DESC unused = {0};
return rjd_gfx_backbuffer_get_msaa_quality_d3d11(context_d3d11, sample_count, &unused);
}
struct rjd_result rjd_gfx_backbuffer_set_msaa_count(struct rjd_gfx_context* context, uint32_t sample_count)
{
RJD_ASSERT(context);
struct rjd_gfx_context_d3d11* context_d3d11 = (struct rjd_gfx_context_d3d11*)context;
DXGI_SAMPLE_DESC sample_desc = {0};
{
struct rjd_result result = rjd_gfx_backbuffer_get_msaa_quality_d3d11(context_d3d11, sample_count, &sample_desc);
if (!rjd_result_isok(result)) {
return result;
}
}
DXGI_SWAP_CHAIN_DESC1 swapchain_desc = {0};
HRESULT hr_swap = IDXGISwapChain1_GetDesc1(context_d3d11->swapchain, &swapchain_desc);
if (FAILED(hr_swap)) {
return RJD_RESULT("Failed to get swapchain desc to check MSAA compatibility.");
}
if (sample_desc.Count != swapchain_desc.SampleDesc.Count) {
const uint32_t num_msaa_backbuffers = (sample_count > 1) ? context_d3d11->num_backbuffers : 0;
if (context_d3d11->msaa_backbuffers) {
for (uint32_t i = 0; i < context_d3d11->num_backbuffers; ++i) {
rjd_gfx_texture_destroy(context, context_d3d11->msaa_backbuffers + i);
ID3D11Texture2D_Release(context_d3d11->msaa_depthbuffers[i]);
}
rjd_mem_free(context_d3d11->msaa_backbuffers);
rjd_mem_free(context_d3d11->msaa_depthbuffers);
context_d3d11->msaa_backbuffers = NULL;
context_d3d11->msaa_depthbuffers = NULL;
}
if (num_msaa_backbuffers > 0) {
// color buffers
context_d3d11->msaa_backbuffers = rjd_mem_alloc_array(struct rjd_gfx_texture, num_msaa_backbuffers, context_d3d11->allocator);
for (size_t i = 0; i < num_msaa_backbuffers; ++i) {
struct rjd_gfx_texture_desc desc = {
.debug_label = "msaa_backbuffer_color",
.data = NULL,
.data_length = 0,
.pixels_width = swapchain_desc.Width,
.pixels_height = swapchain_desc.Height,
.msaa_samples = sample_count,
.format = context_d3d11->backbuffer_color_format,
.access = RJD_GFX_TEXTURE_ACCESS_CPU_NONE_GPU_READWRITE,
.usage = RJD_GFX_TEXTURE_USAGE_RENDERTARGET,
};
struct rjd_result result = rjd_gfx_texture_create(context, context_d3d11->msaa_backbuffers + i, desc);
if (!rjd_result_isok(result)) {
return result;
}
}
// msaa depthbuffers
context_d3d11->msaa_depthbuffers = rjd_mem_alloc_array(ID3D11Texture2D*, num_msaa_backbuffers, context_d3d11->allocator);
for (size_t i = 0; i < num_msaa_backbuffers; ++i) {
DXGI_FORMAT depth_format_dxgi = rjd_gfx_format_to_dxgi(context_d3d11->backbuffer_depth_format);
D3D11_TEXTURE2D_DESC desc_depthbuffer = {
.Width = swapchain_desc.Width,
.Height = swapchain_desc.Height,
.MipLevels = 1,
.ArraySize = 1,
.Format = depth_format_dxgi,
.SampleDesc = sample_desc,
.Usage = D3D11_USAGE_DEFAULT,
.BindFlags = D3D11_BIND_DEPTH_STENCIL,
.CPUAccessFlags = 0,
.MiscFlags = 0,
};
HRESULT hr = ID3D11Device_CreateTexture2D(context_d3d11->device, &desc_depthbuffer, NULL, context_d3d11->msaa_depthbuffers + i);
if (FAILED(hr)) {
return rjd_gfx_translate_hresult(hr);
}
}
}
}
return RJD_RESULT_OK();
}
struct rjd_result rjd_gfx_vsync_set(struct rjd_gfx_context* context, enum RJD_GFX_VSYNC_MODE mode)
{
RJD_UNUSED_PARAM(context);
RJD_UNUSED_PARAM(mode);
return RJD_RESULT("unimplemented");
}
struct rjd_result rjd_gfx_wait_for_frame_begin(struct rjd_gfx_context* context)
{
struct rjd_gfx_context_d3d11* context_d3d11 = (struct rjd_gfx_context_d3d11*)context;
RECT rect_window = {0};
if (GetWindowRect(context_d3d11->hwnd, &rect_window) == FALSE) {
return RJD_RESULT("Failed to get window rect. Check GetLastError() for more information");
}
IDXGIOutput* output = NULL;
IDXGIOutput* selected_output = NULL;
uint32_t selected_intersect_area = 0;
for (UINT i = 0; DXGI_ERROR_NOT_FOUND != IDXGIAdapter1_EnumOutputs(context_d3d11->adapter, i, &output); ++i) {
DXGI_OUTPUT_DESC desc_output = {0};
HRESULT hr = IDXGIOutput_GetDesc(output, &desc_output);
if (SUCCEEDED(hr)) {
RECT rect_intersect = {0};
if (IntersectRect(&rect_intersect, &rect_window, &desc_output.DesktopCoordinates)) {
uint32_t output_intersect_area = (rect_intersect.right - rect_intersect.left) * (rect_intersect.bottom - rect_intersect.top);
if (selected_output == NULL || output_intersect_area > selected_intersect_area) {
selected_output = output;
selected_intersect_area = output_intersect_area;
}
}
}
if (selected_output != output) {
IDXGIOutput_Release(output);
}
}
if (selected_output == NULL) {
return RJD_RESULT("No outputs found. Were all monitors unplugged?");
}
HRESULT hr = IDXGIOutput_WaitForVBlank(selected_output);
IDXGIOutput_Release(selected_output);
struct rjd_result result = rjd_gfx_translate_hresult(hr);
return result;
}
struct rjd_result rjd_gfx_present(struct rjd_gfx_context* context)
{
struct rjd_gfx_context_d3d11* context_d3d11 = (struct rjd_gfx_context_d3d11*)context;
const bool USE_VSYNC = true; // TODO make this configurable
UINT sync_interval = 0;
UINT flags = 0;
if (USE_VSYNC) {
sync_interval = 1;
flags = DXGI_PRESENT_DO_NOT_WAIT;
} else {
sync_interval = 0;
flags = DXGI_PRESENT_DO_NOT_WAIT | DXGI_PRESENT_ALLOW_TEARING;
}
if (context_d3d11->is_occluded) {
flags |= DXGI_PRESENT_TEST;
}
HRESULT hr = IDXGISwapChain_Present(context_d3d11->swapchain, sync_interval, flags);
context_d3d11->is_occluded = (hr == DXGI_STATUS_OCCLUDED);
if (context_d3d11->is_occluded) {
hr = S_OK;
}
context_d3d11->backbuffer_index = (context_d3d11->backbuffer_index + 1) % context_d3d11->num_backbuffers;
return rjd_gfx_translate_hresult(hr);
}
// commands
struct rjd_result rjd_gfx_command_buffer_create(struct rjd_gfx_context* context, struct rjd_gfx_command_buffer* out)
{
struct rjd_gfx_context_d3d11* context_d3d11 = (struct rjd_gfx_context_d3d11*)context;
struct rjd_gfx_command_buffer_d3d11 cmd_buffer_d3d11 = {0};
if (rjd_array_count(context_d3d11->free_deferred_contexts) > 0) {
cmd_buffer_d3d11.deferred_context = rjd_array_pop(context_d3d11->free_deferred_contexts);
} else {
HRESULT hr = ID3D11Device1_CreateDeferredContext1(context_d3d11->device, 0, &cmd_buffer_d3d11.deferred_context);
if (FAILED(hr)) {
return rjd_gfx_translate_hresult(hr);
}
}
rjd_slotmap_insert(context_d3d11->slotmap_command_buffers, cmd_buffer_d3d11, &out->handle);
return RJD_RESULT_OK();
}
struct rjd_result rjd_gfx_command_pass_begin(struct rjd_gfx_context* context, struct rjd_gfx_command_buffer* cmd_buffer, const struct rjd_gfx_pass_begin_desc* command)
{
struct rjd_gfx_context_d3d11* context_d3d11 = (struct rjd_gfx_context_d3d11*)context;
struct rjd_gfx_command_buffer_d3d11* cmd_buffer_d3d11 = rjd_slotmap_get(context_d3d11->slotmap_command_buffers, cmd_buffer->handle);
if (cmd_buffer_d3d11->render_target_view) {
return RJD_RESULT("A render pass has already been started with this command buffer.");
}
// Color buffer
if (rjd_gfx_texture_isbackbuffer(command->render_target)) {
ID3D11Texture2D* texture_backbuffer = NULL;
D3D11_RTV_DIMENSION view_dimension = D3D11_RTV_DIMENSION_TEXTURE2D;
if (context_d3d11->msaa_backbuffers) {
struct rjd_gfx_texture* render_target = context_d3d11->msaa_backbuffers + context_d3d11->backbuffer_index;
struct rjd_gfx_texture_d3d11* render_target_d3d11 = rjd_slotmap_get(context_d3d11->slotmap_textures, render_target->handle);
texture_backbuffer = render_target_d3d11->texture;
view_dimension = D3D11_RTV_DIMENSION_TEXTURE2DMS;
} else {
HRESULT hr = IDXGISwapChain_GetBuffer(context_d3d11->swapchain, 0, &IID_ID3D11Texture2D, (void**)&texture_backbuffer);
if (FAILED(hr)) {
return rjd_gfx_translate_hresult(hr);
}
}
// NOTE: Even if the backbuffer is non-sRGB, we can still create a sRGB view for automatic colorspace conversion if that's what the user wanted the
// backbuffer to be.
D3D11_RENDER_TARGET_VIEW_DESC desc = {
.Format = rjd_gfx_format_to_dxgi(context_d3d11->backbuffer_color_format),
.ViewDimension = view_dimension,
};
HRESULT hr = ID3D11Device_CreateRenderTargetView(context_d3d11->device, (ID3D11Resource*)texture_backbuffer, &desc, &cmd_buffer_d3d11->render_target_view);
if (FAILED(hr)) {
return rjd_gfx_translate_hresult(hr);
}
// Fetching the buffer from the swapchain revs the refcount, so release it here. Getting the buffer from the MSAA render target doesn't, so no need to release.
if (view_dimension == D3D11_RTV_DIMENSION_TEXTURE2D) {
ID3D11Texture2D_Release(texture_backbuffer);
}
} else {
RJD_ASSERTFAIL("non-backbuffer render targets are unimplemented"); // TODO
}
struct rjd_gfx_rgba rgba_backbuffer = rjd_gfx_format_value_to_rgba(command->clear_color);
ID3D11DeviceContext_ClearRenderTargetView(cmd_buffer_d3d11->deferred_context, cmd_buffer_d3d11->render_target_view, rgba_backbuffer.v);
// Depthstencil buffer
if (rjd_gfx_texture_isbackbuffer(command->depthstencil_target)) {
ID3D11Texture2D* texture_depthstencil = NULL;
if (context_d3d11->msaa_depthbuffers) {
texture_depthstencil = context_d3d11->msaa_depthbuffers[context_d3d11->backbuffer_index];
} else {
texture_depthstencil = context_d3d11->depthbuffers[context_d3d11->backbuffer_index];
}
HRESULT hr = ID3D11Device_CreateDepthStencilView(context_d3d11->device, (ID3D11Resource*)texture_depthstencil, NULL, &cmd_buffer_d3d11->depthstencil_view);
if (FAILED(hr)) {
return rjd_gfx_translate_hresult(hr);
}
} else {
RJD_ASSERTFAIL("non-backbuffer depthbuffer targets are unimplemented"); // TODO
}
UINT flags = 0;
float depth_color = 0.0f;
uint8_t stencil_color = 0;
if (rjd_gfx_format_isdepth(command->clear_depthstencil.type)) {
flags |= D3D11_CLEAR_DEPTH;
depth_color = (float)rjd_gfx_format_value_to_depth(command->clear_depthstencil);
}
if (rjd_gfx_format_isstencil(command->clear_depthstencil.type)) {
flags |= D3D11_CLEAR_STENCIL;
stencil_color = rjd_gfx_format_value_to_stencil(command->clear_depthstencil);
}
ID3D11DeviceContext_ClearDepthStencilView(cmd_buffer_d3d11->deferred_context, cmd_buffer_d3d11->depthstencil_view, flags, depth_color, stencil_color);
return RJD_RESULT_OK();
}
struct rjd_result rjd_gfx_command_pass_draw(struct rjd_gfx_context* context, struct rjd_gfx_command_buffer* cmd_buffer, const struct rjd_gfx_pass_draw_desc* command)
{
struct rjd_gfx_context_d3d11* context_d3d11 = (struct rjd_gfx_context_d3d11*)context;
struct rjd_gfx_command_buffer_d3d11* cmd_buffer_d3d11 = rjd_slotmap_get(context_d3d11->slotmap_command_buffers, cmd_buffer->handle);
const D3D11_VIEWPORT viewport =
{
.TopLeftX = 0,
.TopLeftY = 0,
.Width = (float)command->viewport->width,
.Height = (float)command->viewport->height,
.MinDepth = 0,
.MaxDepth = 1.0f,
};
ID3D11DeviceContext1_RSSetViewports(cmd_buffer_d3d11->deferred_context, 1, &viewport);
// pipeline state
struct rjd_gfx_pipeline_state_d3d11* pipeline_state_d3d11 = rjd_slotmap_get(context_d3d11->slotmap_pipeline_states, command->pipeline_state->handle);
ID3D11DeviceContext1_IASetInputLayout(cmd_buffer_d3d11->deferred_context, pipeline_state_d3d11->vertex_layout);
ID3D11DeviceContext1_RSSetState(cmd_buffer_d3d11->deferred_context, pipeline_state_d3d11->rasterizer_state);
ID3D11DeviceContext1_OMSetDepthStencilState(cmd_buffer_d3d11->deferred_context, pipeline_state_d3d11->depthstencil_state, 0);
ID3D11DeviceContext1_OMSetRenderTargets(cmd_buffer_d3d11->deferred_context, 1, &cmd_buffer_d3d11->render_target_view, cmd_buffer_d3d11->depthstencil_view);
struct rjd_gfx_shader_d3d11* shader_vertex_d3d11 = rjd_slotmap_get(context_d3d11->slotmap_shaders, pipeline_state_d3d11->shader_vertex.handle);
struct rjd_gfx_shader_d3d11* shader_pixel_d3d11 = rjd_slotmap_get(context_d3d11->slotmap_shaders, pipeline_state_d3d11->shader_pixel.handle);
if (shader_vertex_d3d11->vertex) {
ID3D11DeviceContext1_VSSetShader(cmd_buffer_d3d11->deferred_context, shader_vertex_d3d11->vertex, NULL, 0);
} else {
return RJD_RESULT("The selected shader did not have a vertex shader.");
}
if (shader_pixel_d3d11->pixel) {
ID3D11DeviceContext1_PSSetShader(cmd_buffer_d3d11->deferred_context, shader_pixel_d3d11->pixel, NULL, 0);
} else {
return RJD_RESULT("The selected shader did not have a pixel shader.");
}
// textures & sampler states
for (size_t i = 0; i < command->count_textures; ++i) {
RJD_ASSERT(command->textures);
RJD_ASSERT(command->texture_indices);
struct rjd_gfx_texture_d3d11* texture_d3d11 = rjd_slotmap_get(context_d3d11->slotmap_textures, command->textures[i].handle);
UINT slot = command->texture_indices[i];
if (D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT <= slot) {
return RJD_RESULT("Not allowed to bind to slots higher than D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT - 1");
}
ID3D11DeviceContext1_PSSetShaderResources(cmd_buffer_d3d11->deferred_context, slot, 1, &texture_d3d11->resource_view);
ID3D11DeviceContext1_PSSetSamplers(cmd_buffer_d3d11->deferred_context, slot, 1, &texture_d3d11->sampler);
}
// draw meshes
for (uint32_t mesh_index = 0; mesh_index < command->count_meshes; ++mesh_index) {
RJD_ASSERT(command->meshes);
const struct rjd_gfx_mesh_d3d11* mesh_d3d11 = rjd_slotmap_get(context_d3d11->slotmap_meshes, command->meshes[mesh_index].handle);
const D3D_PRIMITIVE_TOPOLOGY primitive = rjd_gfx_primitive_to_d3d11(mesh_d3d11->primitive);
ID3D11DeviceContext1_IASetPrimitiveTopology(cmd_buffer_d3d11->deferred_context, primitive);
for (uint32_t buffer_index = 0; buffer_index < mesh_d3d11->count_buffers; ++buffer_index) {
const struct rjd_gfx_mesh_buffer_d3d11* buffer_d3d11 = mesh_d3d11->buffers + buffer_index;
if (buffer_d3d11->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_VERTEX) {
ID3D11DeviceContext1_IASetVertexBuffers(
cmd_buffer_d3d11->deferred_context,
buffer_d3d11->slot,
1,
&buffer_d3d11->buffer,
&buffer_d3d11->stride,
&buffer_d3d11->offset);
}
if (buffer_d3d11->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_VERTEX_CONSTANT ||
buffer_d3d11->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_PIXEL_CONSTANT) {
const struct rjd_gfx_pass_draw_buffer_offset_desc* buffer_offset_desc = NULL;
for (size_t index_constant_desc = 0; index_constant_desc < command->count_constant_descs; ++index_constant_desc) {
if (command->buffer_offset_descs[index_constant_desc].mesh_index == mesh_index &&
command->buffer_offset_descs[index_constant_desc].buffer_index == buffer_index) {
buffer_offset_desc = command->buffer_offset_descs + index_constant_desc;
}
}
RJD_ASSERTMSG(buffer_offset_desc, "Unable to find a rjd_gfx_pass_draw_buffer_offset_desc for mesh %u at buffer index %u.", mesh_index, buffer_index);
const UINT first_constant = buffer_offset_desc->offset_bytes / 16;
const UINT num_constants = buffer_offset_desc->range_bytes / 16;
if (buffer_d3d11->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_VERTEX_CONSTANT) {
ID3D11DeviceContext1_VSSetConstantBuffers1(
cmd_buffer_d3d11->deferred_context,
buffer_d3d11->slot,
1,
&buffer_d3d11->buffer,
&first_constant,
&num_constants);
}
if (buffer_d3d11->usage_flags & RJD_GFX_MESH_BUFFER_USAGE_PIXEL_CONSTANT) {
ID3D11DeviceContext1_PSSetConstantBuffers1(
cmd_buffer_d3d11->deferred_context,
buffer_d3d11->slot,
1,
&buffer_d3d11->buffer,
&first_constant,
&num_constants);
}
}
}
ID3D11DeviceContext1_Draw(cmd_buffer_d3d11->deferred_context, mesh_d3d11->count_vertices, 0);
}
return RJD_RESULT_OK();
}
struct rjd_result rjd_gfx_command_buffer_commit(struct rjd_gfx_context* context, struct rjd_gfx_command_buffer* cmd_buffer)
{
RJD_ASSERT(cmd_buffer);
RJD_ASSERT(context);
RJD_ASSERT(rjd_slot_isvalid(cmd_buffer->handle));
struct rjd_gfx_context_d3d11* context_d3d11 = (struct rjd_gfx_context_d3d11*)context;
struct rjd_gfx_command_buffer_d3d11* cmd_buffer_d3d11 = rjd_slotmap_get(context_d3d11->slotmap_command_buffers, cmd_buffer->handle);
// copy the MSAA render target to the DXGI backbuffer
if (context_d3d11->msaa_backbuffers) {
struct rjd_gfx_texture* source_texture = context_d3d11->msaa_backbuffers + context_d3d11->backbuffer_index;
struct rjd_gfx_texture_d3d11* source_texture_d3d11 = rjd_slotmap_get(context_d3d11->slotmap_textures, source_texture->handle);
ID3D11Resource* swapchain_buffer = NULL;
{
HRESULT hr = IDXGISwapChain_GetBuffer(context_d3d11->swapchain, 0, &IID_ID3D11Texture2D, (void**)&swapchain_buffer);
if (FAILED(hr)) {
return rjd_gfx_translate_hresult(hr);
}
}
ID3D11DeviceContext1_ResolveSubresource(cmd_buffer_d3d11->deferred_context,
swapchain_buffer, 0,
(ID3D11Resource*)source_texture_d3d11->texture, 0,
rjd_gfx_format_to_dxgi(context_d3d11->backbuffer_color_format));
ID3D11Resource_Release(swapchain_buffer);
}
ID3D11CommandList* commandlist = NULL;
HRESULT hr = ID3D11DeviceContext1_FinishCommandList(cmd_buffer_d3d11->deferred_context, FALSE, &commandlist);
if (FAILED(hr)) {
return rjd_gfx_translate_hresult(hr);
}
ID3D11DeviceContext_ExecuteCommandList(context_d3d11->context, commandlist, FALSE);
ID3D11CommandList_Release(commandlist);
ID3D11RenderTargetView_Release(cmd_buffer_d3d11->render_target_view);
ID3D11DepthStencilView_Release(cmd_buffer_d3d11->depthstencil_view);
rjd_array_push(context_d3d11->free_deferred_contexts, cmd_buffer_d3d11->deferred_context);
cmd_buffer_d3d11->deferred_context = NULL;
cmd_buffer_d3d11->render_target_view = NULL;
cmd_buffer_d3d11->depthstencil_view = NULL;
rjd_slotmap_erase(context_d3d11->slotmap_command_buffers, cmd_buffer->handle);
rjd_slot_invalidate(&cmd_buffer->handle);
return RJD_RESULT_OK();
}
struct rjd_result rjd_gfx_texture_create(struct rjd_gfx_context* context, struct rjd_gfx_texture* out, struct rjd_gfx_texture_desc desc)
{
struct rjd_gfx_context_d3d11* context_d3d11 = (struct rjd_gfx_context_d3d11*)context;
DXGI_SAMPLE_DESC sample_desc = {0};
struct rjd_result result = rjd_gfx_backbuffer_get_msaa_quality_d3d11(context_d3d11, desc.msaa_samples, &sample_desc);
if (!rjd_result_isok(result)) {
return result;
}
UINT bind_flags = 0;
bind_flags |= D3D11_BIND_SHADER_RESOURCE;
bind_flags |= (desc.usage == RJD_GFX_TEXTURE_USAGE_RENDERTARGET) ? D3D11_BIND_RENDER_TARGET : 0;
const D3D11_TEXTURE2D_DESC desc_d3d11 = {
.Width = desc.pixels_width,
.Height = desc.pixels_height,
.MipLevels = 1, // TODO mipmap support
.ArraySize = 1,
.Format = rjd_gfx_format_to_dxgi(desc.format),
.SampleDesc = sample_desc,
.Usage = rjd_gfx_texture_access_to_gpu_access(desc.access),
.BindFlags = bind_flags,
.CPUAccessFlags = rjd_gfx_texture_access_to_cpu_access(desc.access),
.MiscFlags = 0,
};
const D3D11_SUBRESOURCE_DATA subresource = {
.pSysMem = desc.data,
.SysMemPitch = desc.pixels_width * rjd_gfx_format_bytesize(desc.format),
.SysMemSlicePitch = 0, // only for 3D textures
};
ID3D11Texture2D* texture = NULL;
{
const D3D11_SUBRESOURCE_DATA* initial_data = desc.data ? &subresource : NULL;
HRESULT hr = ID3D11Device_CreateTexture2D(context_d3d11->device, &desc_d3d11, initial_data, &texture);
if (FAILED(hr)) {
return rjd_gfx_translate_hresult(hr);
}
}
ID3D11ShaderResourceView* resource_view = NULL;
{
D3D11_SHADER_RESOURCE_VIEW_DESC desc_resource_view =
{
.Format = desc_d3d11.Format,
};
if (sample_desc.Count > 1) {
desc_resource_view.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
} else {
desc_resource_view.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
desc_resource_view.Texture2D.MostDetailedMip = 0;
desc_resource_view.Texture2D.MipLevels = desc_d3d11.MipLevels;
}
HRESULT hr = ID3D11Device_CreateShaderResourceView(context_d3d11->device, (ID3D11Resource*)texture, &desc_resource_view, &resource_view);
if (FAILED(hr)) {
return rjd_gfx_translate_hresult(hr);
}
}
ID3D11SamplerState* sampler = NULL;
{
// TODO make this configurable
D3D11_SAMPLER_DESC desc_sampler = {
.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT,
.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP,
.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP,
.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP,
.MipLODBias = 0.0f,
.MaxAnisotropy = 0,
.ComparisonFunc = D3D11_COMPARISON_ALWAYS,
.MinLOD = 0,
.MaxLOD = D3D11_FLOAT32_MAX,
};
HRESULT hr = ID3D11Device_CreateSamplerState(context_d3d11->device, &desc_sampler, &sampler);