forked from crosire/reshade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime_d3d12.cpp
1716 lines (1437 loc) · 65.3 KB
/
runtime_d3d12.cpp
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
/*
* Copyright (C) 2014 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/reshade#license
*/
#include "dll_log.hpp"
#include "dll_resources.hpp"
#include "hook_manager.hpp"
#include "runtime_d3d12.hpp"
#include "runtime_config.hpp"
#include "runtime_objects.hpp"
#include "dxgi/format_utils.hpp"
#include <imgui.h>
#include <imgui_internal.h>
#include <d3dcompiler.h>
#define D3D12_RESOURCE_STATE_SHADER_RESOURCE \
(D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE)
namespace reshade::d3d12
{
struct d3d12_tex_data
{
com_ptr<ID3D12Resource> resource;
com_ptr<ID3D12DescriptorHeap> descriptors;
};
struct d3d12_pass_data
{
com_ptr<ID3D12PipelineState> pipeline;
UINT num_render_targets;
D3D12_CPU_DESCRIPTOR_HANDLE render_targets;
};
struct d3d12_effect_data
{
com_ptr<ID3D12Resource> cb;
com_ptr<ID3D12RootSignature> signature;
com_ptr<ID3D12DescriptorHeap> srv_heap;
com_ptr<ID3D12DescriptorHeap> rtv_heap;
com_ptr<ID3D12DescriptorHeap> sampler_heap;
D3D12_GPU_VIRTUAL_ADDRESS cbv_gpu_address;
D3D12_CPU_DESCRIPTOR_HANDLE srv_cpu_base;
D3D12_GPU_DESCRIPTOR_HANDLE srv_gpu_base;
D3D12_CPU_DESCRIPTOR_HANDLE rtv_cpu_base;
D3D12_CPU_DESCRIPTOR_HANDLE sampler_cpu_base;
D3D12_GPU_DESCRIPTOR_HANDLE sampler_gpu_base;
D3D12_CPU_DESCRIPTOR_HANDLE depth_texture_binding = {};
};
struct d3d12_technique_data
{
std::vector<d3d12_pass_data> passes;
};
static void transition_state(
const com_ptr<ID3D12GraphicsCommandList> &list,
const com_ptr<ID3D12Resource> &res,
D3D12_RESOURCE_STATES from, D3D12_RESOURCE_STATES to,
UINT subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES)
{
D3D12_RESOURCE_BARRIER transition = { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION };
transition.Transition.pResource = res.get();
transition.Transition.Subresource = subresource;
transition.Transition.StateBefore = from;
transition.Transition.StateAfter = to;
list->ResourceBarrier(1, &transition);
}
}
reshade::d3d12::runtime_d3d12::runtime_d3d12(ID3D12Device *device, ID3D12CommandQueue *queue, IDXGISwapChain3 *swapchain) :
_device(device), _commandqueue(queue), _swapchain(swapchain)
{
assert(queue != nullptr);
assert(device != nullptr);
_renderer_id = D3D_FEATURE_LEVEL_12_0;
if (com_ptr<IDXGIFactory4> factory;
swapchain != nullptr && SUCCEEDED(swapchain->GetParent(IID_PPV_ARGS(&factory))))
{
const LUID luid = device->GetAdapterLuid();
if (com_ptr<IDXGIAdapter> dxgi_adapter;
SUCCEEDED(factory->EnumAdapterByLuid(luid, IID_PPV_ARGS(&dxgi_adapter))))
{
DXGI_ADAPTER_DESC desc;
if (SUCCEEDED(dxgi_adapter->GetDesc(&desc)))
_vendor_id = desc.VendorId, _device_id = desc.DeviceId;
}
}
#if RESHADE_GUI && RESHADE_DEPTH
subscribe_to_ui("DX12", [this]() {
assert(_buffer_detection != nullptr);
draw_depth_debug_menu(*_buffer_detection);
});
#endif
#if RESHADE_DEPTH
subscribe_to_load_config([this](const ini_file &config) {
config.get("DX12_BUFFER_DETECTION", "DepthBufferRetrievalMode", _preserve_depth_buffers);
config.get("DX12_BUFFER_DETECTION", "DepthBufferClearingNumber", _depth_clear_index_override);
config.get("DX12_BUFFER_DETECTION", "UseAspectRatioHeuristics", _filter_aspect_ratio);
if (_depth_clear_index_override == 0)
// Zero is not a valid clear index, since it disables depth buffer preservation
_depth_clear_index_override = std::numeric_limits<UINT>::max();
});
subscribe_to_save_config([this](ini_file &config) {
config.set("DX12_BUFFER_DETECTION", "DepthBufferRetrievalMode", _preserve_depth_buffers);
config.set("DX12_BUFFER_DETECTION", "DepthBufferClearingNumber", _depth_clear_index_override);
config.set("DX12_BUFFER_DETECTION", "UseAspectRatioHeuristics", _filter_aspect_ratio);
});
#endif
}
reshade::d3d12::runtime_d3d12::~runtime_d3d12()
{
if (_d3d_compiler != nullptr)
FreeLibrary(_d3d_compiler);
}
bool reshade::d3d12::runtime_d3d12::on_init(const DXGI_SWAP_CHAIN_DESC &swap_desc
#if RESHADE_D3D12ON7
, ID3D12Resource *backbuffer
#endif
)
{
RECT window_rect = {};
GetClientRect(swap_desc.OutputWindow, &window_rect);
_width = swap_desc.BufferDesc.Width;
_height = swap_desc.BufferDesc.Height;
_window_width = window_rect.right - window_rect.left;
_window_height = window_rect.bottom - window_rect.top;
_color_bit_depth = dxgi_format_color_depth(swap_desc.BufferDesc.Format);
_backbuffer_format = swap_desc.BufferDesc.Format;
_srv_handle_size = _device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
_rtv_handle_size = _device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
_dsv_handle_size = _device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
_sampler_handle_size = _device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
#if RESHADE_D3D12ON7
if (backbuffer != nullptr)
{
_backbuffers.resize(1);
_backbuffers[0] = backbuffer;
assert(swap_desc.BufferCount == 1);
}
#endif
// Create multiple command allocators to buffer for multiple frames
_cmd_alloc.resize(swap_desc.BufferCount);
for (UINT i = 0; i < swap_desc.BufferCount; ++i)
if (FAILED(_device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&_cmd_alloc[i]))))
return false;
if (FAILED(_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, _cmd_alloc[0].get(), nullptr, IID_PPV_ARGS(&_cmd_list))))
return false;
_cmd_list->Close(); // Immediately close since it will be reset on first use
// Create auto-reset event and fences for synchronization
_fence_event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (_fence_event == nullptr)
return false;
_fence.resize(swap_desc.BufferCount);
_fence_value.resize(swap_desc.BufferCount);
for (UINT i = 0; i < swap_desc.BufferCount; ++i)
if (FAILED(_device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&_fence[i]))))
return false;
// Allocate descriptor heaps
{ D3D12_DESCRIPTOR_HEAP_DESC desc = { D3D12_DESCRIPTOR_HEAP_TYPE_RTV };
desc.NumDescriptors = swap_desc.BufferCount * 2;
if (FAILED(_device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&_backbuffer_rtvs))))
return false;
}
{ D3D12_DESCRIPTOR_HEAP_DESC desc = { D3D12_DESCRIPTOR_HEAP_TYPE_DSV };
desc.NumDescriptors = 1;
if (FAILED(_device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&_depthstencil_dsvs))))
return false;
}
// Get back buffer textures
_backbuffers.resize(swap_desc.BufferCount);
D3D12_CPU_DESCRIPTOR_HANDLE rtv_handle = _backbuffer_rtvs->GetCPUDescriptorHandleForHeapStart();
for (unsigned int i = 0; i < swap_desc.BufferCount; ++i)
{
if (_swapchain != nullptr && FAILED(_swapchain->GetBuffer(i, IID_PPV_ARGS(&_backbuffers[i]))))
return false;
assert(_backbuffers[i] != nullptr);
#ifndef NDEBUG
_backbuffers[i]->SetName(L"Back buffer");
#endif
for (int srgb_write_enable = 0; srgb_write_enable < 2; ++srgb_write_enable, rtv_handle.ptr += _rtv_handle_size)
{
D3D12_RENDER_TARGET_VIEW_DESC rtv_desc = {};
rtv_desc.Format = srgb_write_enable ?
make_dxgi_format_srgb(_backbuffer_format) :
make_dxgi_format_normal(_backbuffer_format);
rtv_desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
_device->CreateRenderTargetView(_backbuffers[i].get(), &rtv_desc, rtv_handle);
}
}
// Create back buffer shader texture
{ D3D12_RESOURCE_DESC desc = { D3D12_RESOURCE_DIMENSION_TEXTURE2D };
desc.Width = _width;
desc.Height = _height;
desc.DepthOrArraySize = 1;
desc.MipLevels = 1;
desc.Format = make_dxgi_format_typeless(_backbuffer_format);
desc.SampleDesc = { 1, 0 };
D3D12_HEAP_PROPERTIES props = { D3D12_HEAP_TYPE_DEFAULT };
if (FAILED(_device->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_SHADER_RESOURCE, nullptr, IID_PPV_ARGS(&_backbuffer_texture))))
return false;
#ifndef NDEBUG
_backbuffer_texture->SetName(L"ReShade back buffer");
#endif
}
// Create effect stencil resource
{ D3D12_RESOURCE_DESC desc = { D3D12_RESOURCE_DIMENSION_TEXTURE2D };
desc.Width = _width;
desc.Height = _height;
desc.DepthOrArraySize = 1;
desc.MipLevels = 1;
desc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
desc.SampleDesc = { 1, 0 };
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
D3D12_HEAP_PROPERTIES props = { D3D12_HEAP_TYPE_DEFAULT };
D3D12_CLEAR_VALUE clear_value = {};
clear_value.Format = desc.Format;
clear_value.DepthStencil = { 1.0f, 0x0 };
if (FAILED(_device->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_DEPTH_WRITE, &clear_value, IID_PPV_ARGS(&_effect_stencil))))
return false;
#ifndef NDEBUG
_effect_stencil->SetName(L"ReShade stencil buffer");
#endif
_device->CreateDepthStencilView(_effect_stencil.get(), nullptr, _depthstencil_dsvs->GetCPUDescriptorHandleForHeapStart());
}
// Create mipmap generation states
{ D3D12_DESCRIPTOR_RANGE srv_range = {};
srv_range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
srv_range.NumDescriptors = 1;
srv_range.BaseShaderRegister = 0; // t0
D3D12_DESCRIPTOR_RANGE uav_range = {};
uav_range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
uav_range.NumDescriptors = 1;
uav_range.BaseShaderRegister = 0; // u0
D3D12_ROOT_PARAMETER params[3] = {};
params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
params[0].Constants.ShaderRegister = 0; // b0
params[0].Constants.Num32BitValues = 2;
params[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
params[1].DescriptorTable.NumDescriptorRanges = 1;
params[1].DescriptorTable.pDescriptorRanges = &srv_range;
params[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
params[2].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
params[2].DescriptorTable.NumDescriptorRanges = 1;
params[2].DescriptorTable.pDescriptorRanges = &uav_range;
params[2].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
D3D12_STATIC_SAMPLER_DESC samplers[1] = {};
samplers[0].Filter = D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT;
samplers[0].AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
samplers[0].AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
samplers[0].AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
samplers[0].ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
samplers[0].ShaderRegister = 0; // s0
samplers[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
D3D12_ROOT_SIGNATURE_DESC desc = {};
desc.NumParameters = ARRAYSIZE(params);
desc.pParameters = params;
desc.NumStaticSamplers = ARRAYSIZE(samplers);
desc.pStaticSamplers = samplers;
_mipmap_signature = create_root_signature(desc);
}
{ D3D12_COMPUTE_PIPELINE_STATE_DESC pso_desc = {};
pso_desc.pRootSignature = _mipmap_signature.get();
const resources::data_resource cs = resources::load_data_resource(IDR_MIPMAP_CS);
pso_desc.CS = { cs.data, cs.data_size };
if (FAILED(_device->CreateComputePipelineState(&pso_desc, IID_PPV_ARGS(&_mipmap_pipeline))))
return false;
}
#if RESHADE_GUI
if (!init_imgui_resources())
return false;
#endif
return runtime::on_init(swap_desc.OutputWindow);
}
void reshade::d3d12::runtime_d3d12::on_reset()
{
runtime::on_reset();
// Make sure none of the resources below are currently in use (provided the runtime was initialized previously)
if (!_fence.empty() && !_fence_value.empty())
wait_for_command_queue();
_cmd_list.reset();
_cmd_alloc.clear();
CloseHandle(_fence_event);
_fence.clear();
_fence_value.clear();
_backbuffers.clear();
_backbuffer_rtvs.reset();
_backbuffer_texture.reset();
_depthstencil_dsvs.reset();
_mipmap_pipeline.reset();
_mipmap_signature.reset();
_effect_stencil.reset();
#if RESHADE_GUI
_imgui.pipeline.reset();
_imgui.signature.reset();
for (unsigned int i = 0; i < NUM_IMGUI_BUFFERS; ++i)
{
_imgui.indices[i].reset();
_imgui.vertices[i].reset();
_imgui.num_indices[i] = 0;
_imgui.num_vertices[i] = 0;
}
#endif
#if RESHADE_DEPTH
_depth_texture.reset();
_has_depth_texture = false;
_depth_texture_override = nullptr;
#endif
}
void reshade::d3d12::runtime_d3d12::on_present()
{
if (!_is_initialized)
return;
assert(_buffer_detection != nullptr);
_vertices = _buffer_detection->total_vertices();
_drawcalls = _buffer_detection->total_drawcalls();
// There is no swap chain for d3d12on7
if (_swapchain != nullptr)
_swap_index = _swapchain->GetCurrentBackBufferIndex();
// Make sure all commands for this command allocator have finished executing before reseting it
if (_fence[_swap_index]->GetCompletedValue() < _fence_value[_swap_index])
{
if (SUCCEEDED(_fence[_swap_index]->SetEventOnCompletion(_fence_value[_swap_index], _fence_event)))
WaitForSingleObject(_fence_event, INFINITE); // Event is automatically reset after this wait is released
}
// Reset command allocator before using it this frame again
_cmd_alloc[_swap_index]->Reset();
if (!begin_command_list())
return;
#if RESHADE_DEPTH
assert(_depth_clear_index_override != 0);
update_depth_texture_bindings(_buffer_detection->update_depth_texture(
_commandqueue.get(), _cmd_list.get(), _filter_aspect_ratio ? _width : 0, _height, _depth_texture_override, _preserve_depth_buffers ? _depth_clear_index_override : 0));
#endif
transition_state(_cmd_list, _backbuffers[_swap_index], D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET);
update_and_render_effects();
runtime::on_present();
// Potentially have to restart command list here because a screenshot was taken
if (!begin_command_list())
return;
transition_state(_cmd_list, _backbuffers[_swap_index], D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT);
execute_command_list();
if (const UINT64 sync_value = _fence_value[_swap_index] + 1;
SUCCEEDED(_commandqueue->Signal(_fence[_swap_index].get(), sync_value)))
_fence_value[_swap_index] = sync_value;
}
bool reshade::d3d12::runtime_d3d12::capture_screenshot(uint8_t *buffer) const
{
const uint32_t data_pitch = _width * 4;
const uint32_t download_pitch = (data_pitch + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u);
D3D12_RESOURCE_DESC desc = { D3D12_RESOURCE_DIMENSION_BUFFER };
desc.Width = _height * download_pitch;
desc.Height = 1;
desc.DepthOrArraySize = 1;
desc.MipLevels = 1;
desc.SampleDesc = { 1, 0 };
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
D3D12_HEAP_PROPERTIES props = { D3D12_HEAP_TYPE_READBACK };
com_ptr<ID3D12Resource> intermediate;
if (FAILED(_device->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&intermediate))))
{
LOG(ERROR) << "Failed to create system memory texture for screenshot capture!";
return false;
}
#ifndef NDEBUG
intermediate->SetName(L"ReShade screenshot texture");
#endif
if (!begin_command_list())
return false;
// Was transitioned to D3D12_RESOURCE_STATE_RENDER_TARGET in 'on_present' already
transition_state(_cmd_list, _backbuffers[_swap_index], D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE, 0);
{
D3D12_TEXTURE_COPY_LOCATION src_location = { _backbuffers[_swap_index].get() };
src_location.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
src_location.SubresourceIndex = 0;
D3D12_TEXTURE_COPY_LOCATION dst_location = { intermediate.get() };
dst_location.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
dst_location.PlacedFootprint.Footprint.Width = _width;
dst_location.PlacedFootprint.Footprint.Height = _height;
dst_location.PlacedFootprint.Footprint.Depth = 1;
dst_location.PlacedFootprint.Footprint.Format = make_dxgi_format_normal(_backbuffer_format);
dst_location.PlacedFootprint.Footprint.RowPitch = download_pitch;
_cmd_list->CopyTextureRegion(&dst_location, 0, 0, 0, &src_location, nullptr);
}
transition_state(_cmd_list, _backbuffers[_swap_index], D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET, 0);
// Execute and wait for completion
if (!wait_for_command_queue())
return false;
// Copy data from system memory texture into output buffer
uint8_t *mapped_data;
if (FAILED(intermediate->Map(0, nullptr, reinterpret_cast<void **>(&mapped_data))))
return false;
for (uint32_t y = 0; y < _height; y++, buffer += data_pitch, mapped_data += download_pitch)
{
if (_color_bit_depth == 10)
{
for (uint32_t x = 0; x < data_pitch; x += 4)
{
const uint32_t rgba = *reinterpret_cast<const uint32_t *>(mapped_data + x);
// Divide by 4 to get 10-bit range (0-1023) into 8-bit range (0-255)
buffer[x + 0] = ((rgba & 0x3FF) / 4) & 0xFF;
buffer[x + 1] = (((rgba & 0xFFC00) >> 10) / 4) & 0xFF;
buffer[x + 2] = (((rgba & 0x3FF00000) >> 20) / 4) & 0xFF;
buffer[x + 3] = 0xFF;
}
}
else
{
std::memcpy(buffer, mapped_data, data_pitch);
for (uint32_t x = 0; x < data_pitch; x += 4)
buffer[x + 3] = 0xFF; // Clear alpha channel
}
}
intermediate->Unmap(0, nullptr);
return true;
}
bool reshade::d3d12::runtime_d3d12::init_effect(size_t index)
{
if (_d3d_compiler == nullptr)
_d3d_compiler = LoadLibraryW(L"d3dcompiler_47.dll");
if (_d3d_compiler == nullptr)
{
LOG(ERROR) << "Unable to load HLSL compiler (\"d3dcompiler_47.dll\").";
return false;
}
effect &effect = _effects[index];
const auto D3DCompile = reinterpret_cast<pD3DCompile>(GetProcAddress(_d3d_compiler, "D3DCompile"));
const auto D3DDisassemble = reinterpret_cast<pD3DDisassemble>(GetProcAddress(_d3d_compiler, "D3DDisassemble"));
const std::string hlsl = effect.preamble + effect.module.hlsl;
std::unordered_map<std::string, com_ptr<ID3DBlob>> entry_points;
// Compile the generated HLSL source code to DX byte code
for (const reshadefx::entry_point &entry_point : effect.module.entry_points)
{
com_ptr<ID3DBlob> d3d_errors;
const HRESULT hr = D3DCompile(
hlsl.c_str(), hlsl.size(),
nullptr, nullptr, nullptr,
entry_point.name.c_str(),
entry_point.is_pixel_shader ? "ps_5_0" : "vs_5_0",
D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_OPTIMIZATION_LEVEL3, 0,
&entry_points[entry_point.name], &d3d_errors);
if (d3d_errors != nullptr) // Append warnings to the output error string as well
effect.errors.append(static_cast<const char *>(d3d_errors->GetBufferPointer()), d3d_errors->GetBufferSize() - 1); // Subtracting one to not append the null-terminator as well
// No need to setup resources if any of the shaders failed to compile
if (FAILED(hr))
return false;
if (com_ptr<ID3DBlob> d3d_disassembled; SUCCEEDED(D3DDisassemble(entry_points[entry_point.name]->GetBufferPointer(), entry_points[entry_point.name]->GetBufferSize(), 0, nullptr, &d3d_disassembled)))
effect.assembly[entry_point.name] = std::string(static_cast<const char *>(d3d_disassembled->GetBufferPointer()));
}
if (index >= _effect_data.size())
_effect_data.resize(index + 1);
d3d12_effect_data &effect_data = _effect_data[index];
{ D3D12_DESCRIPTOR_RANGE srv_range = {};
srv_range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
srv_range.NumDescriptors = effect.module.num_texture_bindings;
srv_range.BaseShaderRegister = 0;
D3D12_DESCRIPTOR_RANGE sampler_range = {};
sampler_range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER;
sampler_range.NumDescriptors = effect.module.num_sampler_bindings;
sampler_range.BaseShaderRegister = 0;
D3D12_ROOT_PARAMETER params[3] = {};
params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
params[0].Descriptor.ShaderRegister = 0; // b0 (global constant buffer)
params[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
params[1].DescriptorTable.NumDescriptorRanges = 1;
params[1].DescriptorTable.pDescriptorRanges = &srv_range;
params[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
params[2].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
params[2].DescriptorTable.NumDescriptorRanges = 1;
params[2].DescriptorTable.pDescriptorRanges = &sampler_range;
params[2].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
D3D12_ROOT_SIGNATURE_DESC desc = {};
desc.NumParameters = ARRAYSIZE(params);
desc.pParameters = params;
effect_data.signature = create_root_signature(desc);
}
if (!effect.uniform_data_storage.empty())
{
D3D12_RESOURCE_DESC desc = { D3D12_RESOURCE_DIMENSION_BUFFER };
desc.Width = effect.uniform_data_storage.size();
desc.Height = 1;
desc.DepthOrArraySize = 1;
desc.MipLevels = 1;
desc.SampleDesc = { 1, 0 };
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
D3D12_HEAP_PROPERTIES props = { D3D12_HEAP_TYPE_UPLOAD };
if (FAILED(_device->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&effect_data.cb))))
return false;
#ifndef NDEBUG
effect_data.cb->SetName(L"ReShade constant buffer");
#endif
effect_data.cbv_gpu_address = effect_data.cb->GetGPUVirtualAddress();
}
{ D3D12_DESCRIPTOR_HEAP_DESC desc = { D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV };
desc.NumDescriptors = effect.module.num_texture_bindings;
desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
if (FAILED(_device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&effect_data.srv_heap))))
return false;
effect_data.srv_cpu_base = effect_data.srv_heap->GetCPUDescriptorHandleForHeapStart();
effect_data.srv_gpu_base = effect_data.srv_heap->GetGPUDescriptorHandleForHeapStart();
}
{ D3D12_DESCRIPTOR_HEAP_DESC desc = { D3D12_DESCRIPTOR_HEAP_TYPE_RTV };
for (const reshadefx::technique_info &info : effect.module.techniques)
desc.NumDescriptors += static_cast<UINT>(8 * info.passes.size());
if (FAILED(_device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&effect_data.rtv_heap))))
return false;
effect_data.rtv_cpu_base = effect_data.rtv_heap->GetCPUDescriptorHandleForHeapStart();
}
{ D3D12_DESCRIPTOR_HEAP_DESC desc = { D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER };
desc.NumDescriptors = effect.module.num_sampler_bindings;
desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
if (FAILED(_device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&effect_data.sampler_heap))))
return false;
effect_data.sampler_cpu_base = effect_data.sampler_heap->GetCPUDescriptorHandleForHeapStart();
effect_data.sampler_gpu_base = effect_data.sampler_heap->GetGPUDescriptorHandleForHeapStart();
}
UINT16 sampler_list = 0;
for (const reshadefx::sampler_info &info : effect.module.samplers)
{
if (info.binding >= D3D12_COMMONSHADER_SAMPLER_SLOT_COUNT)
{
LOG(ERROR) << "Cannot bind sampler '" << info.unique_name << "' since it exceeds the maximum number of allowed sampler slots in D3D12 (" << info.binding << ", allowed are up to " << D3D12_COMMONSHADER_SAMPLER_SLOT_COUNT << ").";
return false;
}
if (info.texture_binding >= D3D12_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT)
{
LOG(ERROR) << "Cannot bind texture '" << info.texture_name << "' since it exceeds the maximum number of allowed resource slots in D3D12 (" << info.texture_binding << ", allowed are up to " << D3D12_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT << ").";
return false;
}
D3D12_CPU_DESCRIPTOR_HANDLE srv_handle = effect_data.srv_cpu_base;
srv_handle.ptr += info.texture_binding * _srv_handle_size;
const auto existing_texture = std::find_if(_textures.begin(), _textures.end(),
[&texture_name = info.texture_name](const auto &item) {
return item.unique_name == texture_name && item.impl != nullptr;
});
assert(existing_texture != _textures.end());
com_ptr<ID3D12Resource> resource;
switch (existing_texture->impl_reference)
{
case texture_reference::back_buffer:
resource = _backbuffer_texture;
break;
case texture_reference::depth_buffer:
#if RESHADE_DEPTH
resource = _depth_texture; // Note: This can be a "nullptr"
#endif
// Keep track of the depth buffer texture descriptor to simplify updating it
effect_data.depth_texture_binding = srv_handle;
break;
default:
resource = static_cast<d3d12_tex_data *>(existing_texture->impl)->resource;
break;
}
if (resource != nullptr)
{
D3D12_SHADER_RESOURCE_VIEW_DESC desc = {};
desc.Format = info.srgb ?
make_dxgi_format_srgb(resource->GetDesc().Format) :
make_dxgi_format_normal(resource->GetDesc().Format);
desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
desc.Texture2D.MipLevels = existing_texture->levels;
_device->CreateShaderResourceView(resource.get(), &desc, srv_handle);
}
// Only initialize sampler if it has not been created before
if (0 == (sampler_list & (1 << info.binding)))
{
sampler_list |= (1 << info.binding); // D3D12_COMMONSHADER_SAMPLER_SLOT_COUNT is 16, so a 16-bit integer is enough to hold all bindings
D3D12_SAMPLER_DESC desc = {};
desc.Filter = static_cast<D3D12_FILTER>(info.filter);
desc.AddressU = static_cast<D3D12_TEXTURE_ADDRESS_MODE>(info.address_u);
desc.AddressV = static_cast<D3D12_TEXTURE_ADDRESS_MODE>(info.address_v);
desc.AddressW = static_cast<D3D12_TEXTURE_ADDRESS_MODE>(info.address_w);
desc.MipLODBias = info.lod_bias;
desc.MaxAnisotropy = 1;
desc.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
desc.MinLOD = info.min_lod;
desc.MaxLOD = info.max_lod;
D3D12_CPU_DESCRIPTOR_HANDLE sampler_handle = effect_data.sampler_cpu_base;
sampler_handle.ptr += info.binding * _sampler_handle_size;
_device->CreateSampler(&desc, sampler_handle);
}
}
for (technique &technique : _techniques)
{
if (technique.impl != nullptr || technique.effect_index != index)
continue;
auto impl = new d3d12_technique_data();
technique.impl = impl;
impl->passes.resize(technique.passes.size());
for (size_t pass_index = 0; pass_index < technique.passes.size(); ++pass_index)
{
d3d12_pass_data &pass_data = impl->passes[pass_index];
reshadefx::pass_info &pass_info = technique.passes[pass_index];
D3D12_GRAPHICS_PIPELINE_STATE_DESC pso_desc = {};
pso_desc.pRootSignature = effect_data.signature.get();
const auto &VS = entry_points.at(pass_info.vs_entry_point);
pso_desc.VS = { VS->GetBufferPointer(), VS->GetBufferSize() };
const auto &PS = entry_points.at(pass_info.ps_entry_point);
pso_desc.PS = { PS->GetBufferPointer(), PS->GetBufferSize() };
pso_desc.NumRenderTargets = 1;
pso_desc.RTVFormats[0] = pass_info.srgb_write_enable ?
make_dxgi_format_srgb(_backbuffer_format) :
make_dxgi_format_normal(_backbuffer_format);
D3D12_CPU_DESCRIPTOR_HANDLE rtv_handle = effect_data.rtv_cpu_base;
rtv_handle.ptr += pass_index * 8 * _rtv_handle_size;
// Keep track of base handle, which is followed by a contiguous range of render target descriptors
pass_data.render_targets = rtv_handle;
for (UINT k = 0; k < 8 && !pass_info.render_target_names[k].empty(); ++k)
{
const auto texture_impl = static_cast<d3d12_tex_data *>(std::find_if(_textures.begin(), _textures.end(),
[&render_target = pass_info.render_target_names[k]](const auto &item) {
return item.unique_name == render_target;
})->impl);
assert(texture_impl != nullptr);
D3D12_RENDER_TARGET_VIEW_DESC desc = {};
desc.Format = pass_info.srgb_write_enable ?
make_dxgi_format_srgb(texture_impl->resource->GetDesc().Format) :
make_dxgi_format_normal(texture_impl->resource->GetDesc().Format);
desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
_device->CreateRenderTargetView(texture_impl->resource.get(), &desc, rtv_handle);
pso_desc.RTVFormats[k] = desc.Format;
pso_desc.NumRenderTargets = pass_data.num_render_targets = k + 1;
// Increment handle to next descriptor position
rtv_handle.ptr += _rtv_handle_size;
}
if (pass_info.render_target_names[0].empty())
{
pass_info.viewport_width = _width;
pass_info.viewport_height = _height;
}
pso_desc.NodeMask = 1;
pso_desc.DSVFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
pso_desc.SampleMask = UINT_MAX;
pso_desc.SampleDesc = { 1, 0 };
switch (pass_info.topology)
{
case reshadefx::primitive_topology::point_list:
pso_desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT;
break;
case reshadefx::primitive_topology::line_list:
case reshadefx::primitive_topology::line_strip:
pso_desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE;
break;
case reshadefx::primitive_topology::triangle_list:
case reshadefx::primitive_topology::triangle_strip:
pso_desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
break;
}
{ D3D12_BLEND_DESC &desc = pso_desc.BlendState;
desc.RenderTarget[0].BlendEnable = pass_info.blend_enable;
const auto convert_blend_op = [](reshadefx::pass_blend_op value) {
switch (value)
{
default:
case reshadefx::pass_blend_op::add: return D3D12_BLEND_OP_ADD;
case reshadefx::pass_blend_op::subtract: return D3D12_BLEND_OP_SUBTRACT;
case reshadefx::pass_blend_op::rev_subtract: return D3D12_BLEND_OP_REV_SUBTRACT;
case reshadefx::pass_blend_op::min: return D3D12_BLEND_OP_MIN;
case reshadefx::pass_blend_op::max: return D3D12_BLEND_OP_MAX;
}
};
const auto convert_blend_func = [](reshadefx::pass_blend_func value) {
switch (value) {
default:
case reshadefx::pass_blend_func::one: return D3D12_BLEND_ONE;
case reshadefx::pass_blend_func::zero: return D3D12_BLEND_ZERO;
case reshadefx::pass_blend_func::src_alpha: return D3D12_BLEND_SRC_ALPHA;
case reshadefx::pass_blend_func::src_color: return D3D12_BLEND_SRC_COLOR;
case reshadefx::pass_blend_func::inv_src_color: return D3D12_BLEND_INV_SRC_COLOR;
case reshadefx::pass_blend_func::inv_src_alpha: return D3D12_BLEND_INV_SRC_ALPHA;
case reshadefx::pass_blend_func::dst_color: return D3D12_BLEND_DEST_COLOR;
case reshadefx::pass_blend_func::dst_alpha: return D3D12_BLEND_DEST_ALPHA;
case reshadefx::pass_blend_func::inv_dst_color: return D3D12_BLEND_INV_DEST_COLOR;
case reshadefx::pass_blend_func::inv_dst_alpha: return D3D12_BLEND_INV_DEST_ALPHA;
}
};
desc.RenderTarget[0].SrcBlend = convert_blend_func(pass_info.src_blend);
desc.RenderTarget[0].DestBlend = convert_blend_func(pass_info.dest_blend);
desc.RenderTarget[0].BlendOp = convert_blend_op(pass_info.blend_op);
desc.RenderTarget[0].SrcBlendAlpha = convert_blend_func(pass_info.src_blend_alpha);
desc.RenderTarget[0].DestBlendAlpha = convert_blend_func(pass_info.dest_blend_alpha);
desc.RenderTarget[0].BlendOpAlpha = convert_blend_op(pass_info.blend_op_alpha);
desc.RenderTarget[0].RenderTargetWriteMask = pass_info.color_write_mask;
}
{ D3D12_RASTERIZER_DESC &desc = pso_desc.RasterizerState;
desc.FillMode = D3D12_FILL_MODE_SOLID;
desc.CullMode = D3D12_CULL_MODE_NONE;
desc.DepthClipEnable = true;
}
{ D3D12_DEPTH_STENCIL_DESC &desc = pso_desc.DepthStencilState;
desc.DepthEnable = FALSE;
desc.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO;
desc.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;
const auto convert_stencil_op = [](reshadefx::pass_stencil_op value) {
switch (value) {
default:
case reshadefx::pass_stencil_op::keep: return D3D12_STENCIL_OP_KEEP;
case reshadefx::pass_stencil_op::zero: return D3D12_STENCIL_OP_ZERO;
case reshadefx::pass_stencil_op::invert: return D3D12_STENCIL_OP_INVERT;
case reshadefx::pass_stencil_op::replace: return D3D12_STENCIL_OP_REPLACE;
case reshadefx::pass_stencil_op::incr: return D3D12_STENCIL_OP_INCR;
case reshadefx::pass_stencil_op::incr_sat: return D3D12_STENCIL_OP_INCR_SAT;
case reshadefx::pass_stencil_op::decr: return D3D12_STENCIL_OP_DECR;
case reshadefx::pass_stencil_op::decr_sat: return D3D12_STENCIL_OP_DECR_SAT;
}
};
const auto convert_stencil_func = [](reshadefx::pass_stencil_func value) {
switch (value)
{
default:
case reshadefx::pass_stencil_func::always: return D3D12_COMPARISON_FUNC_ALWAYS;
case reshadefx::pass_stencil_func::never: return D3D12_COMPARISON_FUNC_NEVER;
case reshadefx::pass_stencil_func::equal: return D3D12_COMPARISON_FUNC_EQUAL;
case reshadefx::pass_stencil_func::not_equal: return D3D12_COMPARISON_FUNC_NOT_EQUAL;
case reshadefx::pass_stencil_func::less: return D3D12_COMPARISON_FUNC_LESS;
case reshadefx::pass_stencil_func::less_equal: return D3D12_COMPARISON_FUNC_LESS_EQUAL;
case reshadefx::pass_stencil_func::greater: return D3D12_COMPARISON_FUNC_GREATER;
case reshadefx::pass_stencil_func::greater_equal: return D3D12_COMPARISON_FUNC_GREATER_EQUAL;
}
};
desc.StencilEnable = pass_info.stencil_enable;
desc.StencilReadMask = pass_info.stencil_read_mask;
desc.StencilWriteMask = pass_info.stencil_write_mask;
desc.FrontFace.StencilFailOp = convert_stencil_op(pass_info.stencil_op_fail);
desc.FrontFace.StencilDepthFailOp = convert_stencil_op(pass_info.stencil_op_depth_fail);
desc.FrontFace.StencilPassOp = convert_stencil_op(pass_info.stencil_op_pass);
desc.FrontFace.StencilFunc = convert_stencil_func(pass_info.stencil_comparison_func);
desc.BackFace = desc.FrontFace;
}
if (HRESULT hr = _device->CreateGraphicsPipelineState(&pso_desc, IID_PPV_ARGS(&pass_data.pipeline)); FAILED(hr))
{
LOG(ERROR) << "Failed to create pipeline for pass " << pass_index << " in technique '" << technique.name << "'! "
"HRESULT is " << hr << '.';
return false;
}
}
}
return true;
}
void reshade::d3d12::runtime_d3d12::unload_effect(size_t index)
{
// Make sure no effect resources are currently in use
wait_for_command_queue();
for (technique &tech : _techniques)
{
if (tech.effect_index != index)
continue;
delete static_cast<d3d12_technique_data *>(tech.impl);
tech.impl = nullptr;
}
runtime::unload_effect(index);
if (index < _effect_data.size())
{
d3d12_effect_data &effect_data = _effect_data[index];
effect_data.cb.reset();
effect_data.signature.reset();
effect_data.srv_heap.reset();
effect_data.rtv_heap.reset();
effect_data.sampler_heap.reset();
effect_data.depth_texture_binding = { 0 };
}
}
void reshade::d3d12::runtime_d3d12::unload_effects()
{
// Make sure no effect resources are currently in use
wait_for_command_queue();
for (technique &tech : _techniques)
{
delete static_cast<d3d12_technique_data *>(tech.impl);
tech.impl = nullptr;
}
runtime::unload_effects();
_effect_data.clear();
}
bool reshade::d3d12::runtime_d3d12::init_texture(texture &texture)
{
auto impl = new d3d12_tex_data();
texture.impl = impl;
// Do not create resource if it is a reference, it is set in 'render_technique'
if (texture.impl_reference != texture_reference::none)
return true;
D3D12_RESOURCE_DESC desc = { D3D12_RESOURCE_DIMENSION_TEXTURE2D };
desc.Width = texture.width;
desc.Height = texture.height;
desc.DepthOrArraySize = 1;
desc.MipLevels = static_cast<UINT16>(texture.levels);
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc = { 1, 0 };
desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; // Textures may be bound as render target
if (texture.levels > 1) // Need UAV for mipmap generation
desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
switch (texture.format)
{
case reshadefx::texture_format::r8:
desc.Format = DXGI_FORMAT_R8_UNORM;
break;
case reshadefx::texture_format::r16f:
desc.Format = DXGI_FORMAT_R16_FLOAT;
break;
case reshadefx::texture_format::r32f:
desc.Format = DXGI_FORMAT_R32_FLOAT;
break;
case reshadefx::texture_format::rg8:
desc.Format = DXGI_FORMAT_R8G8_UNORM;
break;
case reshadefx::texture_format::rg16:
desc.Format = DXGI_FORMAT_R16G16_UNORM;
break;
case reshadefx::texture_format::rg16f:
desc.Format = DXGI_FORMAT_R16G16_FLOAT;
break;
case reshadefx::texture_format::rg32f:
desc.Format = DXGI_FORMAT_R32G32_FLOAT;
break;
case reshadefx::texture_format::rgba8:
desc.Format = DXGI_FORMAT_R8G8B8A8_TYPELESS;
break;
case reshadefx::texture_format::rgba16:
desc.Format = DXGI_FORMAT_R16G16B16A16_UNORM;
break;
case reshadefx::texture_format::rgba16f:
desc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
break;
case reshadefx::texture_format::rgba32f:
desc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
break;
case reshadefx::texture_format::rgb10a2:
desc.Format = DXGI_FORMAT_R10G10B10A2_UNORM;
break;
}
// Render targets are always either cleared to zero or not cleared at all (see 'ClearRenderTargets' pass state), so can set the optimized clear value here to zero
D3D12_CLEAR_VALUE clear_value = {};
clear_value.Format = make_dxgi_format_normal(desc.Format);
D3D12_HEAP_PROPERTIES props = { D3D12_HEAP_TYPE_DEFAULT };
if (HRESULT hr = _device->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_SHADER_RESOURCE, &clear_value, IID_PPV_ARGS(&impl->resource)); FAILED(hr))
{
LOG(ERROR) << "Failed to create texture '" << texture.unique_name << "' ("
"Width = " << desc.Width << ", "
"Height = " << desc.Height << ", "
"Levels = " << desc.MipLevels << ", "
"Format = " << desc.Format << ")! "
"HRESULT is " << hr << '.';
return false;
}
#ifndef NDEBUG