forked from crosire/reshade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime_d3d12.cpp
1598 lines (1332 loc) · 63.7 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 "runtime_d3d12.hpp"
#include "runtime_d3d12_objects.hpp"
#include "dxgi/format_utils.hpp"
#include <CoreWindow.h>
#include <d3dcompiler.h>
#define D3D12_RESOURCE_STATE_SHADER_RESOURCE \
(D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE)
com_ptr<ID3D12RootSignature> create_root_signature(ID3D12Device *device, const D3D12_ROOT_SIGNATURE_DESC &desc)
{
com_ptr<ID3DBlob> signature_blob;
com_ptr<ID3D12RootSignature> signature;
if (SUCCEEDED(D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &signature_blob, nullptr)))
device->CreateRootSignature(0, signature_blob->GetBufferPointer(), signature_blob->GetBufferSize(), IID_PPV_ARGS(&signature));
return signature;
}
reshade::d3d12::runtime_impl::runtime_impl(device_impl *device, command_queue_impl *queue, IDXGISwapChain3 *swapchain) :
api_object_impl(swapchain),
_device(device->_orig),
_cmd_queue(queue->_orig),
_device_impl(device),
_cmd_queue_impl(queue),
_cmd_impl(static_cast<command_list_immediate_impl *>(queue->get_immediate_command_list()))
{
_renderer_id = D3D_FEATURE_LEVEL_12_0;
// There is no swap chain in d3d12on7
if (com_ptr<IDXGIFactory4> factory;
_orig != nullptr && SUCCEEDED(_orig->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))))
{
if (DXGI_ADAPTER_DESC desc; SUCCEEDED(dxgi_adapter->GetDesc(&desc)))
{
_vendor_id = desc.VendorId;
_device_id = desc.DeviceId;
LOG(INFO) << "Running on " << desc.Description;
}
}
}
if (_orig != nullptr && !on_init())
LOG(ERROR) << "Failed to initialize Direct3D 12 runtime environment on runtime " << this << '!';
}
reshade::d3d12::runtime_impl::~runtime_impl()
{
on_reset();
if (_d3d_compiler != nullptr)
FreeLibrary(_d3d_compiler);
}
bool reshade::d3d12::runtime_impl::on_init()
{
assert(_orig != nullptr);
DXGI_SWAP_CHAIN_DESC swap_desc;
// Get description from IDXGISwapChain interface, since later versions are slightly different
if (FAILED(_orig->GetDesc(&swap_desc)))
return false;
// Update window handle in swap chain description for UWP applications
if (HWND hwnd = nullptr; SUCCEEDED(_orig->GetHwnd(&hwnd)))
swap_desc.OutputWindow = hwnd;
else if (com_ptr<ICoreWindowInterop> window_interop; // Get window handle of the core window
SUCCEEDED(_orig->GetCoreWindow(IID_PPV_ARGS(&window_interop))) && SUCCEEDED(window_interop->get_WindowHandle(&hwnd)))
swap_desc.OutputWindow = hwnd;
return on_init(swap_desc);
}
bool reshade::d3d12::runtime_impl::on_init(const DXGI_SWAP_CHAIN_DESC &swap_desc)
{
if (swap_desc.SampleDesc.Count > 1)
return false; // Multisampled swap chains are not currently supported
_width = _window_width = swap_desc.BufferDesc.Width;
_height = _window_height = swap_desc.BufferDesc.Height;
_color_bit_depth = dxgi_format_color_depth(swap_desc.BufferDesc.Format);
_backbuffer_format = swap_desc.BufferDesc.Format;
if (swap_desc.OutputWindow != nullptr)
{
RECT window_rect = {};
GetClientRect(swap_desc.OutputWindow, &window_rect);
_window_width = window_rect.right;
_window_height = window_rect.bottom;
}
// 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;
_backbuffer_rtvs->SetName(L"ReShade RTV heap");
}
{ 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;
_depthstencil_dsvs->SetName(L"ReShade DSV heap");
}
// Get back buffer textures (skip on d3d12on7 devices, since there is no swap chain there)
_backbuffers.resize(swap_desc.BufferCount);
if (_orig != nullptr)
{
D3D12_CPU_DESCRIPTOR_HANDLE rtv_handle = _backbuffer_rtvs->GetCPUDescriptorHandleForHeapStart();
for (unsigned int i = 0; i < swap_desc.BufferCount; ++i)
{
if (FAILED(_orig->GetBuffer(i, IID_PPV_ARGS(&_backbuffers[i]))))
return false;
for (int srgb_write_enable = 0; srgb_write_enable < 2; ++srgb_write_enable, rtv_handle.ptr += _device_impl->_descriptor_handle_size[D3D12_DESCRIPTOR_HEAP_TYPE_RTV])
{
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;
_backbuffer_texture->SetName(L"ReShade back buffer");
}
// 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;
_effect_stencil->SetName(L"ReShade stencil buffer");
_device->CreateDepthStencilView(_effect_stencil.get(), nullptr, _depthstencil_dsvs->GetCPUDescriptorHandleForHeapStart());
}
// Create mipmap generation states
if (_mipmap_signature == nullptr)
{
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(_device.get(), desc);
if (_mipmap_signature == nullptr)
return false;
}
if (_mipmap_pipeline == nullptr)
{
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_impl::on_reset()
{
runtime::on_reset();
// Make sure none of the resources below are currently in use (provided the runtime was initialized previously)
_cmd_impl->flush_and_wait(_cmd_queue.get());
_backbuffers.clear();
_backbuffer_rtvs.reset();
_backbuffer_texture.reset();
_depthstencil_dsvs.reset();
_effect_stencil.reset();
#if RESHADE_GUI
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
}
void reshade::d3d12::runtime_impl::on_present()
{
if (!_is_initialized)
return;
// There is no swap chain in d3d12on7
if (_orig != nullptr)
_swap_index = _orig->GetCurrentBackBufferIndex();
ID3D12GraphicsCommandList *const cmd_list = _cmd_impl->begin_commands();
D3D12_RESOURCE_BARRIER transition = { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION };
transition.Transition.pResource = _backbuffers[_swap_index].get();
transition.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
transition.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
transition.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
cmd_list->ResourceBarrier(1, &transition);
update_and_render_effects();
runtime::on_present();
std::swap(transition.Transition.StateBefore, transition.Transition.StateAfter);
cmd_list->ResourceBarrier(1, &transition);
_cmd_impl->flush(_cmd_queue.get());
}
bool reshade::d3d12::runtime_impl::on_present(ID3D12Resource *source, HWND hwnd)
{
assert(source != nullptr);
// Reinitialize runtime when the source texture dimensions changes
const D3D12_RESOURCE_DESC source_desc = source->GetDesc();
if (source_desc.Width != _width || source_desc.Height != _height || source_desc.Format != _backbuffer_format)
{
on_reset();
DXGI_SWAP_CHAIN_DESC swap_desc = {};
swap_desc.BufferDesc.Width = static_cast<UINT>(source_desc.Width);
swap_desc.BufferDesc.Height = source_desc.Height;
swap_desc.BufferDesc.Format = source_desc.Format;
swap_desc.SampleDesc = source_desc.SampleDesc;
swap_desc.BufferCount = 3; // Cycle between three fake back buffers
swap_desc.OutputWindow = hwnd;
if (!on_init(swap_desc))
{
LOG(ERROR) << "Failed to initialize Direct3D 12 runtime environment on runtime " << this << '!';
return false;
}
}
_swap_index = (_swap_index + 1) % 3;
// Update source texture render target view
assert(_backbuffers.size() == 3);
if (_backbuffers[_swap_index] != source)
{
_backbuffers[_swap_index] = source;
D3D12_CPU_DESCRIPTOR_HANDLE rtv_handle = _backbuffer_rtvs->GetCPUDescriptorHandleForHeapStart();
rtv_handle.ptr += _device_impl->_descriptor_handle_size[D3D12_DESCRIPTOR_HEAP_TYPE_RTV] * 2 * _swap_index;
for (int srgb_write_enable = 0; srgb_write_enable < 2; ++srgb_write_enable, rtv_handle.ptr += _device_impl->_descriptor_handle_size[D3D12_DESCRIPTOR_HEAP_TYPE_RTV])
{
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(source, &rtv_desc, rtv_handle);
}
}
on_present();
return true;
}
bool reshade::d3d12::runtime_impl::on_present(ID3D12Resource *source, const D3D12_BOX ®ion, HWND hwnd)
{
assert(source != nullptr);
D3D12_RESOURCE_DESC source_desc = source->GetDesc();
const UINT region_width = region.right - region.left;
const UINT region_height = region.bottom - region.top;
assert((region.back - region.front) == 1);
if (region_width != _width || region_height != _height || source_desc.Format != _backbuffer_format)
{
on_reset();
DXGI_SWAP_CHAIN_DESC swap_desc = {};
swap_desc.BufferDesc.Width = region_width;
swap_desc.BufferDesc.Height = region_height;
swap_desc.BufferDesc.Format = source_desc.Format;
swap_desc.SampleDesc = source_desc.SampleDesc;
swap_desc.BufferCount = 1;
swap_desc.OutputWindow = hwnd;
if (!on_init(swap_desc))
{
LOG(ERROR) << "Failed to initialize Direct3D 12 runtime environment on runtime " << this << '!';
return false;
}
assert(_backbuffers.size() == 1);
source_desc.Width = region_width;
source_desc.Height = region_height;
source_desc.Format = make_dxgi_format_typeless(source_desc.Format);
const D3D12_HEAP_PROPERTIES heap_props = { D3D12_HEAP_TYPE_DEFAULT };
if (HRESULT hr = _device->CreateCommittedResource(&heap_props, D3D12_HEAP_FLAG_NONE, &source_desc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&_backbuffers[0])); FAILED(hr))
{
LOG(ERROR) << "Failed to create region texture! HRESULT is " << hr << '.';
return false;
}
D3D12_CPU_DESCRIPTOR_HANDLE rtv_handle = _backbuffer_rtvs->GetCPUDescriptorHandleForHeapStart();
for (int srgb_write_enable = 0; srgb_write_enable < 2; ++srgb_write_enable, rtv_handle.ptr += _device_impl->_descriptor_handle_size[D3D12_DESCRIPTOR_HEAP_TYPE_RTV])
{
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[0].get(), &rtv_desc, rtv_handle);
}
}
ID3D12GraphicsCommandList *const cmd_list = _cmd_impl->begin_commands();
D3D12_RESOURCE_BARRIER transitions[2];
transitions[0] = { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION };
transitions[0].Transition.pResource = source;
transitions[0].Transition.Subresource = 0;
transitions[1] = { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION };
transitions[1].Transition.pResource = _backbuffers[0].get();
transitions[1].Transition.Subresource = 0;
transitions[0].Transition.StateBefore = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
transitions[0].Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE;
cmd_list->ResourceBarrier(1, &transitions[0]);
// Copy region of the source texture
const D3D12_TEXTURE_COPY_LOCATION src_location = { source, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX };
const D3D12_TEXTURE_COPY_LOCATION dest_location = { _backbuffers[0].get(), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX };
cmd_list->CopyTextureRegion(&dest_location, 0, 0, 0, &src_location, ®ion);
transitions[1].Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
transitions[1].Transition.StateAfter = D3D12_RESOURCE_STATE_COMMON;
cmd_list->ResourceBarrier(1, &transitions[1]);
on_present();
transitions[0].Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_SOURCE;
transitions[0].Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
transitions[1].Transition.StateBefore = D3D12_RESOURCE_STATE_COMMON;
transitions[1].Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE;
cmd_list->ResourceBarrier(2, &transitions[0]);
// Copy results back into the source texture
cmd_list->CopyTextureRegion(&src_location, region.left, region.top, region.front, &dest_location, nullptr);
transitions[0].Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
transitions[0].Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
transitions[1].Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_SOURCE;
transitions[1].Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
cmd_list->ResourceBarrier(2, &transitions[0]);
return true;
}
bool reshade::d3d12::runtime_impl::capture_screenshot(uint8_t *buffer) const
{
if (_color_bit_depth != 8 && _color_bit_depth != 10)
{
if (const char *format_string = format_to_string(_backbuffer_format); format_string != nullptr)
LOG(ERROR) << "Screenshots are not supported for back buffer format " << format_string << '!';
else
LOG(ERROR) << "Screenshots are not supported for back buffer format " << _backbuffer_format << '!';
return false;
}
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 (HRESULT hr = _device->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&intermediate)); FAILED(hr))
{
LOG(ERROR) << "Failed to create system memory texture for screenshot capture! HRESULT is " << hr << '.';
LOG(DEBUG) << "> Details: Width = " << desc.Width;
return false;
}
intermediate->SetName(L"ReShade screenshot texture");
ID3D12GraphicsCommandList *const cmd_list = _cmd_impl->begin_commands();
// Was transitioned to D3D12_RESOURCE_STATE_RENDER_TARGET in 'on_present' already
D3D12_RESOURCE_BARRIER transition = { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION };
transition.Transition.pResource = _backbuffers[_swap_index].get();
transition.Transition.Subresource = 0;
transition.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
transition.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE;
cmd_list->ResourceBarrier(1, &transition);
{
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);
}
std::swap(transition.Transition.StateBefore, transition.Transition.StateAfter);
cmd_list->ResourceBarrier(1, &transition);
// Execute and wait for completion
if (!_cmd_impl->flush_and_wait(_cmd_queue.get()))
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 & 0x000003FF) / 4) & 0xFF;
buffer[x + 1] = (((rgba & 0x000FFC00) >> 10) / 4) & 0xFF;
buffer[x + 2] = (((rgba & 0x3FF00000) >> 20) / 4) & 0xFF;
buffer[x + 3] = (((rgba & 0xC0000000) >> 30) * 85) & 0xFF;
}
}
else
{
std::memcpy(buffer, mapped_data, data_pitch);
if (_backbuffer_format == DXGI_FORMAT_B8G8R8A8_UNORM ||
_backbuffer_format == DXGI_FORMAT_B8G8R8A8_UNORM_SRGB)
{
// Format is BGRA, but output should be RGBA, so flip channels
for (uint32_t x = 0; x < data_pitch; x += 4)
std::swap(buffer[x + 0], buffer[x + 2]);
}
}
}
intermediate->Unmap(0, nullptr);
return true;
}
bool reshade::d3d12::runtime_impl::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, std::vector<char>> entry_points;
// Compile the generated HLSL source code to DX byte code
for (const reshadefx::entry_point &entry_point : effect.module.entry_points)
{
HRESULT hr = E_FAIL;
std::string profile;
switch (entry_point.type)
{
case reshadefx::shader_type::vs:
profile = "vs_5_0";
break;
case reshadefx::shader_type::ps:
profile = "ps_5_0";
break;
case reshadefx::shader_type::cs:
profile = "cs_5_0";
break;
}
UINT compile_flags = D3DCOMPILE_ENABLE_STRICTNESS;
compile_flags |= (_performance_mode ? D3DCOMPILE_OPTIMIZATION_LEVEL3 : D3DCOMPILE_OPTIMIZATION_LEVEL1);
#ifndef NDEBUG
compile_flags |= D3DCOMPILE_DEBUG;
#endif
std::string attributes;
attributes += "entrypoint=" + entry_point.name + ';';
attributes += "profile=" + profile + ';';
attributes += "flags=" + std::to_string(compile_flags) + ';';
const size_t hash = std::hash<std::string_view>()(attributes) ^ std::hash<std::string_view>()(hlsl);
std::vector<char> &cso = entry_points[entry_point.name];
if (!load_effect_cache(effect.source_file, entry_point.name, hash, cso, effect.assembly[entry_point.name]))
{
com_ptr<ID3DBlob> d3d_compiled, d3d_errors;
hr = D3DCompile(
hlsl.data(), hlsl.size(),
nullptr, nullptr, nullptr,
entry_point.name.c_str(),
profile.c_str(),
compile_flags, 0,
&d3d_compiled, &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;
cso.resize(d3d_compiled->GetBufferSize());
std::memcpy(cso.data(), d3d_compiled->GetBufferPointer(), cso.size());
if (com_ptr<ID3DBlob> d3d_disassembled; SUCCEEDED(D3DDisassemble(cso.data(), cso.size(), 0, nullptr, &d3d_disassembled)))
effect.assembly[entry_point.name].assign(static_cast<const char *>(d3d_disassembled->GetBufferPointer()), d3d_disassembled->GetBufferSize() - 1);
save_effect_cache(effect.source_file, entry_point.name, hash, cso, effect.assembly[entry_point.name]);
}
}
if (index >= _effect_data.size())
_effect_data.resize(index + 1);
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_DESCRIPTOR_RANGE uav_range = {};
uav_range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
uav_range.NumDescriptors = effect.module.num_storage_bindings;
uav_range.BaseShaderRegister = 0;
D3D12_ROOT_PARAMETER params[4] = {};
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;
params[3].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
params[3].DescriptorTable.NumDescriptorRanges = 1;
params[3].DescriptorTable.pDescriptorRanges = &uav_range;
params[3].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
D3D12_ROOT_SIGNATURE_DESC desc = {};
desc.NumParameters = effect.module.num_storage_bindings == 0 ? 3 : 4;
desc.pParameters = params;
effect_data.signature = create_root_signature(_device.get(), desc);
if (effect_data.signature == nullptr)
{
LOG(ERROR) << "Failed to create root signature for effect file '" << effect.source_file << "'!";
return false;
}
}
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 (HRESULT hr = _device->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&effect_data.cb)); FAILED(hr))
{
LOG(ERROR) << "Failed to create constant buffer for effect file '" << effect.source_file << "'! HRESULT is " << hr << '.';
LOG(DEBUG) << "> Details: Width = " << desc.Width;
return false;
}
effect_data.cb->SetName(L"ReShade constant buffer");
effect_data.cbv_gpu_address = effect_data.cb->GetGPUVirtualAddress();
}
{ 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_heap->SetName(L"ReShade effect RTV heap");
effect_data.rtv_cpu_base = effect_data.rtv_heap->GetCPUDescriptorHandleForHeapStart();
}
UINT num_passes = 0;
for (const reshadefx::technique_info &info : effect.module.techniques)
num_passes += static_cast<UINT>(info.passes.size());
{ D3D12_DESCRIPTOR_HEAP_DESC desc = { D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV };
desc.NumDescriptors = (effect.module.num_texture_bindings + effect.module.num_storage_bindings) * num_passes;
desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
if (FAILED(_device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&effect_data.srv_uav_heap))))
return false;
effect_data.srv_uav_heap->SetName(L"ReShade effect SRV heap");
effect_data.srv_cpu_base = effect_data.srv_uav_heap->GetCPUDescriptorHandleForHeapStart();
effect_data.srv_gpu_base = effect_data.srv_uav_heap->GetGPUDescriptorHandleForHeapStart();
effect_data.uav_cpu_base = effect_data.srv_cpu_base;
effect_data.uav_cpu_base.ptr += effect.module.num_texture_bindings * num_passes * _device_impl->_descriptor_handle_size[desc.Type];
effect_data.uav_gpu_base = effect_data.srv_gpu_base;
effect_data.uav_gpu_base.ptr += effect.module.num_texture_bindings * num_passes * _device_impl->_descriptor_handle_size[desc.Type];
}
{ 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_heap->SetName(L"ReShade effect sampler heap");
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;
}
// 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;
std::memset(desc.BorderColor, 0, sizeof(desc.BorderColor));
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 * _device_impl->_descriptor_handle_size[D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER];
_device->CreateSampler(&desc, sampler_handle);
}
}
// The render target descriptor table is shared across all techniques in the effect
D3D12_GPU_DESCRIPTOR_HANDLE srv_gpu_base = effect_data.srv_gpu_base;
D3D12_CPU_DESCRIPTOR_HANDLE srv_cpu_base = effect_data.srv_cpu_base;
D3D12_GPU_DESCRIPTOR_HANDLE uav_gpu_base = effect_data.uav_gpu_base;
D3D12_CPU_DESCRIPTOR_HANDLE uav_cpu_base = effect_data.uav_cpu_base;
D3D12_CPU_DESCRIPTOR_HANDLE rtv_cpu_base = effect_data.rtv_cpu_base;
for (technique &technique : _techniques)
{
if (technique.impl != nullptr || technique.effect_index != index)
continue;
auto impl = new technique_data();
technique.impl = impl;
impl->passes.resize(technique.passes.size());
for (size_t pass_index = 0; pass_index < technique.passes.size(); ++pass_index)
{
pass_data &pass_data = impl->passes[pass_index];
reshadefx::pass_info &pass_info = technique.passes[pass_index];
if (!pass_info.cs_entry_point.empty())
{
impl->has_compute_passes = true;
D3D12_COMPUTE_PIPELINE_STATE_DESC pso_desc = {};
pso_desc.pRootSignature = effect_data.signature.get();
const auto &CS = entry_points.at(pass_info.cs_entry_point);
pso_desc.CS = { CS.data(), CS.size() };
pso_desc.NodeMask = 1;
if (HRESULT hr = _device->CreateComputePipelineState(&pso_desc, IID_PPV_ARGS(&pass_data.pipeline)); FAILED(hr))
{
LOG(ERROR) << "Failed to create compute pipeline for pass " << pass_index << " in technique '" << technique.name << "'! HRESULT is " << hr << '.';
return false;
}
}
else
{
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.data(), VS.size() };
const auto &PS = entry_points.at(pass_info.ps_entry_point);
pso_desc.PS = { PS.data(), PS.size() };
// Keep track of the base handle, which is followed by a contiguous range of render target descriptors
pass_data.render_targets = rtv_cpu_base;
rtv_cpu_base.ptr += 8 * _device_impl->_descriptor_handle_size[D3D12_DESCRIPTOR_HEAP_TYPE_RTV];
for (UINT k = 0; k < 8 && !pass_info.render_target_names[k].empty(); ++k)
{
tex_data *const tex_impl = static_cast<tex_data *>(
look_up_texture_by_name(pass_info.render_target_names[k]).impl);
pass_data.modified_resources.push_back(tex_impl);
const D3D12_RESOURCE_DESC desc = tex_impl->resource->GetDesc();
D3D12_CPU_DESCRIPTOR_HANDLE rtv_handle = pass_data.render_targets;
rtv_handle.ptr += k * _device_impl->_descriptor_handle_size[D3D12_DESCRIPTOR_HEAP_TYPE_RTV];
D3D12_RENDER_TARGET_VIEW_DESC rtv_desc = {};
rtv_desc.Format = pass_info.srgb_write_enable ?
make_dxgi_format_srgb(desc.Format) :
make_dxgi_format_normal(desc.Format);
rtv_desc.ViewDimension = desc.SampleDesc.Count > 1 ? D3D12_RTV_DIMENSION_TEXTURE2DMS : D3D12_RTV_DIMENSION_TEXTURE2D;
_device->CreateRenderTargetView(tex_impl->resource.get(), &rtv_desc, rtv_handle);
pso_desc.NumRenderTargets = pass_data.num_render_targets = k + 1;
pso_desc.RTVFormats[k] = rtv_desc.Format;
}
if (pass_info.render_target_names[0].empty())
{
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);
pass_info.viewport_width = _width;
pass_info.viewport_height = _height;
}
pso_desc.DSVFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
pso_desc.SampleMask = UINT_MAX;
pso_desc.SampleDesc = { 1, 0 };
pso_desc.NodeMask = 1;
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.AlphaToCoverageEnable = FALSE;
desc.IndependentBlendEnable = FALSE;
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) {
case reshadefx::pass_blend_func::zero: return D3D12_BLEND_ZERO;
default:
case reshadefx::pass_blend_func::one: return D3D12_BLEND_ONE;
case reshadefx::pass_blend_func::src_color: return D3D12_BLEND_SRC_COLOR;
case reshadefx::pass_blend_func::src_alpha: return D3D12_BLEND_SRC_ALPHA;
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) {
case reshadefx::pass_stencil_op::zero: return D3D12_STENCIL_OP_ZERO;
default:
case reshadefx::pass_stencil_op::keep: return D3D12_STENCIL_OP_KEEP;
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)
{
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;
default:
case reshadefx::pass_stencil_func::always: return D3D12_COMPARISON_FUNC_ALWAYS;
}
};
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 graphics pipeline for pass " << pass_index << " in technique '" << technique.name << "'! HRESULT is " << hr << '.';
return false;
}
}
pass_data.srv_handle = srv_gpu_base;
for (const reshadefx::sampler_info &info : pass_info.samplers)
{
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;
}
const texture &texture = look_up_texture_by_name(info.texture_name);
D3D12_CPU_DESCRIPTOR_HANDLE srv_handle = srv_cpu_base;
srv_handle.ptr += info.texture_binding * _device_impl->_descriptor_handle_size[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV];
com_ptr<ID3D12Resource> resource;
if (texture.semantic == "COLOR")
{
resource = _backbuffer_texture;
}
else if (!texture.semantic.empty())
{
if (const auto it = _texture_semantic_bindings.find(texture.semantic);
it != _texture_semantic_bindings.end())