forked from doitsujin/dxvk
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathd3d11_context.cpp
4342 lines (3395 loc) · 147 KB
/
d3d11_context.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
#include <cstring>
#include "d3d11_context.h"
#include "d3d11_device.h"
#include "d3d11_query.h"
#include "d3d11_texture.h"
#include "../dxbc/dxbc_util.h"
namespace dxvk {
D3D11DeviceContext::D3D11DeviceContext(
D3D11Device* pParent,
const Rc<DxvkDevice>& Device,
DxvkCsChunkFlags CsFlags)
: D3D11DeviceChild<ID3D11DeviceContext4>(pParent),
m_contextExt(this),
m_annotation(this),
m_multithread(this, false),
m_device (Device),
m_csFlags (CsFlags),
m_csChunk (AllocCsChunk()),
m_cmdData (nullptr) {
}
D3D11DeviceContext::~D3D11DeviceContext() {
}
HRESULT STDMETHODCALLTYPE D3D11DeviceContext::QueryInterface(REFIID riid, void** ppvObject) {
if (ppvObject == nullptr)
return E_POINTER;
*ppvObject = nullptr;
if (riid == __uuidof(IUnknown)
|| riid == __uuidof(ID3D11DeviceChild)
|| riid == __uuidof(ID3D11DeviceContext)
|| riid == __uuidof(ID3D11DeviceContext1)
|| riid == __uuidof(ID3D11DeviceContext2)
|| riid == __uuidof(ID3D11DeviceContext3)
|| riid == __uuidof(ID3D11DeviceContext4)) {
*ppvObject = ref(this);
return S_OK;
}
if (riid == __uuidof(ID3D11VkExtContext)) {
*ppvObject = ref(&m_contextExt);
return S_OK;
}
if (riid == __uuidof(ID3DUserDefinedAnnotation)) {
*ppvObject = ref(&m_annotation);
return S_OK;
}
if (riid == __uuidof(ID3D10Multithread)) {
*ppvObject = ref(&m_multithread);
return S_OK;
}
Logger::warn("D3D11DeviceContext::QueryInterface: Unknown interface query");
Logger::warn(str::format(riid));
return E_NOINTERFACE;
}
void STDMETHODCALLTYPE D3D11DeviceContext::DiscardResource(ID3D11Resource* pResource) {
D3D10DeviceLock lock = LockContext();
if (!pResource)
return;
// We don't support the Discard API for images
D3D11_RESOURCE_DIMENSION resType = D3D11_RESOURCE_DIMENSION_UNKNOWN;
pResource->GetType(&resType);
if (resType == D3D11_RESOURCE_DIMENSION_BUFFER) {
DiscardBuffer(pResource);
} else {
auto texture = GetCommonTexture(pResource);
for (uint32_t i = 0; i < texture->CountSubresources(); i++)
DiscardTexture(pResource, i);
}
}
void STDMETHODCALLTYPE D3D11DeviceContext::DiscardView(ID3D11View* pResourceView) {
DiscardView1(pResourceView, nullptr, 0);
}
void STDMETHODCALLTYPE D3D11DeviceContext::DiscardView1(
ID3D11View* pResourceView,
const D3D11_RECT* pRects,
UINT NumRects) {
D3D10DeviceLock lock = LockContext();
// We don't support discarding individual rectangles
if (!pResourceView || (NumRects && pRects))
return;
// ID3D11View has no methods to query the exact type of
// the view, so we'll have to check each possible class
auto dsv = dynamic_cast<D3D11DepthStencilView*>(pResourceView);
auto rtv = dynamic_cast<D3D11RenderTargetView*>(pResourceView);
auto uav = dynamic_cast<D3D11UnorderedAccessView*>(pResourceView);
Rc<DxvkImageView> view;
if (dsv) view = dsv->GetImageView();
if (rtv) view = rtv->GetImageView();
if (uav) view = uav->GetImageView();
if (view == nullptr)
return;
// Get information about underlying resource
Com<ID3D11Resource> resource;
pResourceView->GetResource(&resource);
uint32_t mipCount = GetCommonTexture(resource.ptr())->Desc()->MipLevels;
// Discard mip levels one by one
VkImageSubresourceRange sr = view->subresources();
for (uint32_t layer = 0; layer < sr.layerCount; layer++) {
for (uint32_t mip = 0; mip < sr.levelCount; mip++) {
DiscardTexture(resource.ptr(), D3D11CalcSubresource(
sr.baseMipLevel + mip, sr.baseArrayLayer + layer, mipCount));
}
}
}
void STDMETHODCALLTYPE D3D11DeviceContext::ClearState() {
D3D10DeviceLock lock = LockContext();
// Default shaders
m_state.vs.shader = nullptr;
m_state.hs.shader = nullptr;
m_state.ds.shader = nullptr;
m_state.gs.shader = nullptr;
m_state.ps.shader = nullptr;
m_state.cs.shader = nullptr;
// Default constant buffers
for (uint32_t i = 0; i < D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT; i++) {
m_state.vs.constantBuffers[i] = { nullptr, 0, 0 };
m_state.hs.constantBuffers[i] = { nullptr, 0, 0 };
m_state.ds.constantBuffers[i] = { nullptr, 0, 0 };
m_state.gs.constantBuffers[i] = { nullptr, 0, 0 };
m_state.ps.constantBuffers[i] = { nullptr, 0, 0 };
m_state.cs.constantBuffers[i] = { nullptr, 0, 0 };
}
// Default samplers
for (uint32_t i = 0; i < D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT; i++) {
m_state.vs.samplers[i] = nullptr;
m_state.hs.samplers[i] = nullptr;
m_state.ds.samplers[i] = nullptr;
m_state.gs.samplers[i] = nullptr;
m_state.ps.samplers[i] = nullptr;
m_state.cs.samplers[i] = nullptr;
}
// Default shader resources
for (uint32_t i = 0; i < D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; i++) {
m_state.vs.shaderResources.views[i] = nullptr;
m_state.hs.shaderResources.views[i] = nullptr;
m_state.ds.shaderResources.views[i] = nullptr;
m_state.gs.shaderResources.views[i] = nullptr;
m_state.ps.shaderResources.views[i] = nullptr;
m_state.cs.shaderResources.views[i] = nullptr;
}
m_state.vs.shaderResources.hazardous.clear();
m_state.hs.shaderResources.hazardous.clear();
m_state.ds.shaderResources.hazardous.clear();
m_state.gs.shaderResources.hazardous.clear();
m_state.ps.shaderResources.hazardous.clear();
m_state.cs.shaderResources.hazardous.clear();
// Default UAVs
for (uint32_t i = 0; i < D3D11_1_UAV_SLOT_COUNT; i++) {
m_state.ps.unorderedAccessViews[i] = nullptr;
m_state.cs.unorderedAccessViews[i] = nullptr;
}
m_state.cs.uavMask.clear();
// Default ID state
m_state.id.argBuffer = nullptr;
// Default IA state
m_state.ia.inputLayout = nullptr;
m_state.ia.primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED;
for (uint32_t i = 0; i < D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT; i++) {
m_state.ia.vertexBuffers[i].buffer = nullptr;
m_state.ia.vertexBuffers[i].offset = 0;
m_state.ia.vertexBuffers[i].stride = 0;
}
m_state.ia.indexBuffer.buffer = nullptr;
m_state.ia.indexBuffer.offset = 0;
m_state.ia.indexBuffer.format = DXGI_FORMAT_UNKNOWN;
// Default OM State
for (uint32_t i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
m_state.om.renderTargetViews[i] = nullptr;
m_state.om.depthStencilView = nullptr;
m_state.om.cbState = nullptr;
m_state.om.dsState = nullptr;
for (uint32_t i = 0; i < 4; i++)
m_state.om.blendFactor[i] = 1.0f;
m_state.om.sampleMask = D3D11_DEFAULT_SAMPLE_MASK;
m_state.om.stencilRef = D3D11_DEFAULT_STENCIL_REFERENCE;
m_state.om.maxRtv = 0;
m_state.om.maxUav = 0;
// Default RS state
m_state.rs.state = nullptr;
m_state.rs.numViewports = 0;
m_state.rs.numScissors = 0;
for (uint32_t i = 0; i < D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; i++) {
m_state.rs.viewports[i] = D3D11_VIEWPORT { };
m_state.rs.scissors [i] = D3D11_RECT { };
}
// Default SO state
for (uint32_t i = 0; i < D3D11_SO_BUFFER_SLOT_COUNT; i++) {
m_state.so.targets[i].buffer = nullptr;
m_state.so.targets[i].offset = 0;
}
// Default predication
m_state.pr.predicateObject = nullptr;
m_state.pr.predicateValue = FALSE;
// Make sure to apply all state
ResetState();
}
void STDMETHODCALLTYPE D3D11DeviceContext::SetPredication(
ID3D11Predicate* pPredicate,
BOOL PredicateValue) {
D3D10DeviceLock lock = LockContext();
auto predicate = D3D11Query::FromPredicate(pPredicate);
m_state.pr.predicateObject = predicate;
m_state.pr.predicateValue = PredicateValue;
static bool s_errorShown = false;
if (pPredicate && !std::exchange(s_errorShown, true))
Logger::err("D3D11DeviceContext::SetPredication: Stub");
}
void STDMETHODCALLTYPE D3D11DeviceContext::GetPredication(
ID3D11Predicate** ppPredicate,
BOOL* pPredicateValue) {
D3D10DeviceLock lock = LockContext();
if (ppPredicate)
*ppPredicate = D3D11Query::AsPredicate(m_state.pr.predicateObject.ref());
if (pPredicateValue)
*pPredicateValue = m_state.pr.predicateValue;
}
void STDMETHODCALLTYPE D3D11DeviceContext::CopySubresourceRegion(
ID3D11Resource* pDstResource,
UINT DstSubresource,
UINT DstX,
UINT DstY,
UINT DstZ,
ID3D11Resource* pSrcResource,
UINT SrcSubresource,
const D3D11_BOX* pSrcBox) {
CopySubresourceRegion1(
pDstResource, DstSubresource, DstX, DstY, DstZ,
pSrcResource, SrcSubresource, pSrcBox, 0);
}
void STDMETHODCALLTYPE D3D11DeviceContext::CopySubresourceRegion1(
ID3D11Resource* pDstResource,
UINT DstSubresource,
UINT DstX,
UINT DstY,
UINT DstZ,
ID3D11Resource* pSrcResource,
UINT SrcSubresource,
const D3D11_BOX* pSrcBox,
UINT CopyFlags) {
D3D10DeviceLock lock = LockContext();
if (!pDstResource || !pSrcResource)
return;
D3D11_RESOURCE_DIMENSION dstResourceDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
D3D11_RESOURCE_DIMENSION srcResourceDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
pDstResource->GetType(&dstResourceDim);
pSrcResource->GetType(&srcResourceDim);
// Copying 2D image slices to 3D images and vice versa is legal
const bool copy2Dto3D = dstResourceDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D
&& srcResourceDim == D3D11_RESOURCE_DIMENSION_TEXTURE2D;
const bool copy3Dto2D = dstResourceDim == D3D11_RESOURCE_DIMENSION_TEXTURE2D
&& srcResourceDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D;
if (dstResourceDim != srcResourceDim && !copy2Dto3D && !copy3Dto2D) {
Logger::err(str::format(
"D3D11: CopySubresourceRegion: Incompatible resources",
"\n Dst resource type: ", dstResourceDim,
"\n Src resource type: ", srcResourceDim));
return;
}
if (dstResourceDim == D3D11_RESOURCE_DIMENSION_BUFFER) {
auto dstBuffer = static_cast<D3D11Buffer*>(pDstResource)->GetBufferSlice();
auto srcBuffer = static_cast<D3D11Buffer*>(pSrcResource)->GetBufferSlice();
VkDeviceSize dstOffset = DstX;
VkDeviceSize srcOffset = 0;
VkDeviceSize regLength = srcBuffer.length();
if (dstOffset >= dstBuffer.length())
return;
if (pSrcBox != nullptr) {
if (pSrcBox->left >= pSrcBox->right)
return; // no-op, but legal
srcOffset = pSrcBox->left;
regLength = pSrcBox->right - pSrcBox->left;
if (srcOffset >= srcBuffer.length())
return;
}
// Clamp copy region to prevent out-of-bounds access
regLength = std::min(regLength, srcBuffer.length() - srcOffset);
regLength = std::min(regLength, dstBuffer.length() - dstOffset);
EmitCs([
cDstSlice = dstBuffer.subSlice(dstOffset, regLength),
cSrcSlice = srcBuffer.subSlice(srcOffset, regLength)
] (DxvkContext* ctx) {
bool sameResource = cDstSlice.buffer() == cSrcSlice.buffer();
if (!sameResource) {
ctx->copyBuffer(
cDstSlice.buffer(),
cDstSlice.offset(),
cSrcSlice.buffer(),
cSrcSlice.offset(),
cSrcSlice.length());
} else {
ctx->copyBufferRegion(
cDstSlice.buffer(),
cDstSlice.offset(),
cSrcSlice.offset(),
cSrcSlice.length());
}
});
} else {
const D3D11CommonTexture* dstTextureInfo = GetCommonTexture(pDstResource);
const D3D11CommonTexture* srcTextureInfo = GetCommonTexture(pSrcResource);
const Rc<DxvkImage> dstImage = dstTextureInfo->GetImage();
const Rc<DxvkImage> srcImage = srcTextureInfo->GetImage();
const DxvkFormatInfo* dstFormatInfo = imageFormatInfo(dstImage->info().format);
const DxvkFormatInfo* srcFormatInfo = imageFormatInfo(srcImage->info().format);
const VkImageSubresource dstSubresource = dstTextureInfo->GetSubresourceFromIndex(dstFormatInfo->aspectMask, DstSubresource);
const VkImageSubresource srcSubresource = srcTextureInfo->GetSubresourceFromIndex(srcFormatInfo->aspectMask, SrcSubresource);
if (DstSubresource >= dstTextureInfo->CountSubresources()
|| SrcSubresource >= srcTextureInfo->CountSubresources())
return;
// Copies are only supported on size-compatible formats
if (dstFormatInfo->elementSize != srcFormatInfo->elementSize) {
Logger::err(str::format(
"D3D11: CopySubresourceRegion: Incompatible texel size"
"\n Dst texel size: ", dstFormatInfo->elementSize,
"\n Src texel size: ", srcFormatInfo->elementSize));
return;
}
// Copies are only supported if the sample count matches
if (dstImage->info().sampleCount != srcImage->info().sampleCount) {
Logger::err(str::format(
"D3D11: CopySubresourceRegion: Incompatible sample count",
"\n Dst sample count: ", dstImage->info().sampleCount,
"\n Src sample count: ", srcImage->info().sampleCount));
return;
}
VkOffset3D srcOffset = { 0, 0, 0 };
VkOffset3D dstOffset = { int32_t(DstX), int32_t(DstY), int32_t(DstZ) };
VkExtent3D srcExtent = srcImage->mipLevelExtent(srcSubresource.mipLevel);
VkExtent3D dstExtent = dstImage->mipLevelExtent(dstSubresource.mipLevel);
VkExtent3D regExtent = srcExtent;
if (uint32_t(dstOffset.x) >= dstExtent.width
|| uint32_t(dstOffset.y) >= dstExtent.height
|| uint32_t(dstOffset.z) >= dstExtent.depth)
return;
if (pSrcBox != nullptr) {
if (pSrcBox->left >= pSrcBox->right
|| pSrcBox->top >= pSrcBox->bottom
|| pSrcBox->front >= pSrcBox->back)
return; // no-op, but legal
srcOffset.x = pSrcBox->left;
srcOffset.y = pSrcBox->top;
srcOffset.z = pSrcBox->front;
regExtent.width = pSrcBox->right - pSrcBox->left;
regExtent.height = pSrcBox->bottom - pSrcBox->top;
regExtent.depth = pSrcBox->back - pSrcBox->front;
if (uint32_t(srcOffset.x) >= srcExtent.width
|| uint32_t(srcOffset.y) >= srcExtent.height
|| uint32_t(srcOffset.z) >= srcExtent.depth)
return;
}
VkImageSubresourceLayers dstLayers = {
dstSubresource.aspectMask,
dstSubresource.mipLevel,
dstSubresource.arrayLayer, 1 };
VkImageSubresourceLayers srcLayers = {
srcSubresource.aspectMask,
srcSubresource.mipLevel,
srcSubresource.arrayLayer, 1 };
// Copying multiple slices does not
// seem to be supported in D3D11
if (copy2Dto3D || copy3Dto2D) {
regExtent.depth = 1;
dstLayers.layerCount = 1;
srcLayers.layerCount = 1;
}
// Don't perform the copy if the offsets aren't aligned
if (!util::isBlockAligned(srcOffset, srcFormatInfo->blockSize)
|| !util::isBlockAligned(dstOffset, dstFormatInfo->blockSize)) {
Logger::err(str::format(
"D3D11: CopySubresourceRegion: Unaligned block offset",
"\n Src offset: (", srcOffset.x, ",", srcOffset.y, ",", srcOffset.z, ")",
"\n Src block size: (", srcFormatInfo->blockSize.width, "x", srcFormatInfo->blockSize.height, "x", srcFormatInfo->blockSize.depth, ")",
"\n Dst offset: (", dstOffset.x, ",", dstOffset.y, ",", dstOffset.z, ")",
"\n Dst block size: (", dstFormatInfo->blockSize.width, "x", dstFormatInfo->blockSize.height, "x", dstFormatInfo->blockSize.depth, ")"));
return;
}
// Clamp the image region in order to avoid out-of-bounds access
VkExtent3D regBlockCount = util::computeBlockCount(regExtent, srcFormatInfo->blockSize);
VkExtent3D dstBlockCount = util::computeMaxBlockCount(dstOffset, dstExtent, dstFormatInfo->blockSize);
VkExtent3D srcBlockCount = util::computeMaxBlockCount(srcOffset, srcExtent, srcFormatInfo->blockSize);
regBlockCount = util::minExtent3D(regBlockCount, dstBlockCount);
regBlockCount = util::minExtent3D(regBlockCount, srcBlockCount);
regExtent = util::minExtent3D(regExtent, util::computeBlockExtent(regBlockCount, srcFormatInfo->blockSize));
// Don't perform the copy if the image extent is not aligned and
// if it does not touch the image border for unaligned dimensons
if (!util::isBlockAligned(srcOffset, regExtent, srcFormatInfo->blockSize, srcExtent)) {
Logger::err(str::format(
"D3D11: CopySubresourceRegion: Unaligned block size",
"\n Src offset: (", srcOffset.x, ",", srcOffset.y, ",", srcOffset.z, ")",
"\n Src extent: (", srcExtent.width, "x", srcExtent.height, "x", srcExtent.depth, ")",
"\n Src block size: (", srcFormatInfo->blockSize.width, "x", srcFormatInfo->blockSize.height, "x", srcFormatInfo->blockSize.depth, ")",
"\n Dst offset: (", dstOffset.x, ",", dstOffset.y, ",", dstOffset.z, ")",
"\n Dst extent: (", dstExtent.width, "x", dstExtent.height, "x", dstExtent.depth, ")",
"\n Dst block size: (", dstFormatInfo->blockSize.width, "x", dstFormatInfo->blockSize.height, "x", dstFormatInfo->blockSize.depth, ")",
"\n Region extent: (", regExtent.width, "x", regExtent.height, "x", regExtent.depth, ")"));
return;
}
EmitCs([
cDstImage = dstImage,
cSrcImage = srcImage,
cDstLayers = dstLayers,
cSrcLayers = srcLayers,
cDstOffset = dstOffset,
cSrcOffset = srcOffset,
cExtent = regExtent
] (DxvkContext* ctx) {
bool sameSubresource = cDstImage == cSrcImage
&& cDstLayers == cSrcLayers;
if (!sameSubresource) {
ctx->copyImage(
cDstImage, cDstLayers, cDstOffset,
cSrcImage, cSrcLayers, cSrcOffset,
cExtent);
} else {
ctx->copyImageRegion(
cDstImage, cDstLayers,
cDstOffset, cSrcOffset,
cExtent);
}
});
if (dstTextureInfo->CanUpdateMappedBufferEarly())
UpdateMappedBuffer(dstTextureInfo, dstSubresource);
}
}
void STDMETHODCALLTYPE D3D11DeviceContext::CopyResource(
ID3D11Resource* pDstResource,
ID3D11Resource* pSrcResource) {
D3D10DeviceLock lock = LockContext();
if (!pDstResource || !pSrcResource || (pDstResource == pSrcResource))
return;
D3D11_RESOURCE_DIMENSION dstResourceDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
D3D11_RESOURCE_DIMENSION srcResourceDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
pDstResource->GetType(&dstResourceDim);
pSrcResource->GetType(&srcResourceDim);
if (dstResourceDim != srcResourceDim) {
Logger::err(str::format(
"D3D11: CopyResource: Incompatible resources",
"\n Dst resource type: ", dstResourceDim,
"\n Src resource type: ", srcResourceDim));
return;
}
if (dstResourceDim == D3D11_RESOURCE_DIMENSION_BUFFER) {
auto dstBuffer = static_cast<D3D11Buffer*>(pDstResource)->GetBufferSlice();
auto srcBuffer = static_cast<D3D11Buffer*>(pSrcResource)->GetBufferSlice();
if (dstBuffer.length() != srcBuffer.length()) {
Logger::err(str::format(
"D3D11: CopyResource: Mismatched buffer size",
"\n Dst buffer size: ", dstBuffer.length(),
"\n Src buffer size: ", srcBuffer.length()));
return;
}
EmitCs([
cDstBuffer = std::move(dstBuffer),
cSrcBuffer = std::move(srcBuffer)
] (DxvkContext* ctx) {
ctx->copyBuffer(
cDstBuffer.buffer(),
cDstBuffer.offset(),
cSrcBuffer.buffer(),
cSrcBuffer.offset(),
cSrcBuffer.length());
});
} else {
auto dstTexture = GetCommonTexture(pDstResource);
auto srcTexture = GetCommonTexture(pSrcResource);
const Rc<DxvkImage> dstImage = dstTexture->GetImage();
const Rc<DxvkImage> srcImage = srcTexture->GetImage();
const DxvkFormatInfo* dstFormatInfo = imageFormatInfo(dstImage->info().format);
const DxvkFormatInfo* srcFormatInfo = imageFormatInfo(srcImage->info().format);
// Copies are only supported on size-compatible formats
if (dstFormatInfo->elementSize != srcFormatInfo->elementSize) {
Logger::err(str::format(
"D3D11: CopyResource: Incompatible texel size"
"\n Dst texel size: ", dstFormatInfo->elementSize,
"\n Src texel size: ", srcFormatInfo->elementSize));
return;
}
// Layer count, mip level count, and sample count must match
if (srcImage->info().numLayers != dstImage->info().numLayers
|| srcImage->info().mipLevels != dstImage->info().mipLevels
|| srcImage->info().sampleCount != dstImage->info().sampleCount) {
Logger::err(str::format(
"D3D11: CopyResource: Incompatible images"
"\n Dst: (", dstImage->info().numLayers,
",", dstImage->info().mipLevels,
",", dstImage->info().sampleCount, ")",
"\n Src: (", srcImage->info().numLayers,
",", srcImage->info().mipLevels,
",", srcImage->info().sampleCount, ")"));
return;
}
for (uint32_t i = 0; i < srcImage->info().mipLevels; i++) {
VkImageSubresourceLayers dstLayers = { dstFormatInfo->aspectMask, i, 0, dstImage->info().numLayers };
VkImageSubresourceLayers srcLayers = { srcFormatInfo->aspectMask, i, 0, srcImage->info().numLayers };
VkExtent3D extent = srcImage->mipLevelExtent(i);
EmitCs([
cDstImage = dstImage,
cSrcImage = srcImage,
cDstLayers = dstLayers,
cSrcLayers = srcLayers,
cExtent = extent
] (DxvkContext* ctx) {
ctx->copyImage(
cDstImage, cDstLayers, VkOffset3D { 0, 0, 0 },
cSrcImage, cSrcLayers, VkOffset3D { 0, 0, 0 },
cExtent);
});
if (dstTexture->CanUpdateMappedBufferEarly()) {
for (uint32_t j = 0; j < dstImage->info().numLayers; j++)
UpdateMappedBuffer(dstTexture, { dstLayers.aspectMask, i, j });
}
}
}
}
void STDMETHODCALLTYPE D3D11DeviceContext::CopyStructureCount(
ID3D11Buffer* pDstBuffer,
UINT DstAlignedByteOffset,
ID3D11UnorderedAccessView* pSrcView) {
D3D10DeviceLock lock = LockContext();
auto buf = static_cast<D3D11Buffer*>(pDstBuffer);
auto uav = static_cast<D3D11UnorderedAccessView*>(pSrcView);
if (!buf || !uav)
return;
auto counterSlice = uav->GetCounterSlice();
if (!counterSlice.defined())
return;
EmitCs([
cDstSlice = buf->GetBufferSlice(DstAlignedByteOffset),
cSrcSlice = std::move(counterSlice)
] (DxvkContext* ctx) {
ctx->copyBuffer(
cDstSlice.buffer(),
cDstSlice.offset(),
cSrcSlice.buffer(),
cSrcSlice.offset(),
sizeof(uint32_t));
});
}
void STDMETHODCALLTYPE D3D11DeviceContext::CopyTiles(
ID3D11Resource* pTiledResource,
const D3D11_TILED_RESOURCE_COORDINATE* pTileRegionStartCoordinate,
const D3D11_TILE_REGION_SIZE* pTileRegionSize,
ID3D11Buffer* pBuffer,
UINT64 BufferStartOffsetInBytes,
UINT Flags) {
static bool s_errorShown = false;
if (!std::exchange(s_errorShown, true))
Logger::err("D3D11DeviceContext::CopyTiles: Not implemented");
}
HRESULT STDMETHODCALLTYPE D3D11DeviceContext::CopyTileMappings(
ID3D11Resource* pDestTiledResource,
const D3D11_TILED_RESOURCE_COORDINATE* pDestRegionStartCoordinate,
ID3D11Resource* pSourceTiledResource,
const D3D11_TILED_RESOURCE_COORDINATE* pSourceRegionStartCoordinate,
const D3D11_TILE_REGION_SIZE* pTileRegionSize,
UINT Flags) {
static bool s_errorShown = false;
if (!std::exchange(s_errorShown, true))
Logger::err("D3D11DeviceContext::CopyTileMappings: Not implemented");
return DXGI_ERROR_INVALID_CALL;
}
HRESULT STDMETHODCALLTYPE D3D11DeviceContext::ResizeTilePool(
ID3D11Buffer* pTilePool,
UINT64 NewSizeInBytes) {
static bool s_errorShown = false;
if (!std::exchange(s_errorShown, true))
Logger::err("D3D11DeviceContext::ResizeTilePool: Not implemented");
return DXGI_ERROR_INVALID_CALL;
}
void STDMETHODCALLTYPE D3D11DeviceContext::TiledResourceBarrier(
ID3D11DeviceChild* pTiledResourceOrViewAccessBeforeBarrier,
ID3D11DeviceChild* pTiledResourceOrViewAccessAfterBarrier) {
}
void STDMETHODCALLTYPE D3D11DeviceContext::ClearRenderTargetView(
ID3D11RenderTargetView* pRenderTargetView,
const FLOAT ColorRGBA[4]) {
D3D10DeviceLock lock = LockContext();
auto rtv = static_cast<D3D11RenderTargetView*>(pRenderTargetView);
if (!rtv)
return;
auto view = rtv->GetImageView();
auto color = ConvertColorValue(ColorRGBA, view->formatInfo());
EmitCs([
cClearValue = color,
cImageView = std::move(view)
] (DxvkContext* ctx) {
ctx->clearRenderTarget(
cImageView,
VK_IMAGE_ASPECT_COLOR_BIT,
cClearValue);
});
}
void STDMETHODCALLTYPE D3D11DeviceContext::ClearUnorderedAccessViewUint(
ID3D11UnorderedAccessView* pUnorderedAccessView,
const UINT Values[4]) {
D3D10DeviceLock lock = LockContext();
auto uav = static_cast<D3D11UnorderedAccessView*>(pUnorderedAccessView);
if (!uav)
return;
// Gather UAV format info. We'll use this to determine
// whether we need to create a temporary view or not.
D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc;
uav->GetDesc(&uavDesc);
VkFormat uavFormat = m_parent->LookupFormat(uavDesc.Format, DXGI_VK_FORMAT_MODE_ANY).Format;
VkFormat rawFormat = m_parent->LookupFormat(uavDesc.Format, DXGI_VK_FORMAT_MODE_RAW).Format;
if (uavFormat != rawFormat && rawFormat == VK_FORMAT_UNDEFINED) {
Logger::err(str::format("D3D11: ClearUnorderedAccessViewUint: No raw format found for ", uavFormat));
return;
}
// Set up clear color struct
VkClearValue clearValue;
clearValue.color.uint32[0] = Values[0];
clearValue.color.uint32[1] = Values[1];
clearValue.color.uint32[2] = Values[2];
clearValue.color.uint32[3] = Values[3];
// This is the only packed format that has UAV support
if (uavFormat == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
clearValue.color.uint32[0] = ((Values[0] & 0x7FF) << 0)
| ((Values[1] & 0x7FF) << 11)
| ((Values[2] & 0x3FF) << 22);
}
if (uav->GetResourceType() == D3D11_RESOURCE_DIMENSION_BUFFER) {
// In case of raw and structured buffers as well as typed
// buffers that can be used for atomic operations, we can
// use the fast Vulkan buffer clear function.
Rc<DxvkBufferView> bufferView = uav->GetBufferView();
if (bufferView->info().format == VK_FORMAT_R32_UINT
|| bufferView->info().format == VK_FORMAT_R32_SINT
|| bufferView->info().format == VK_FORMAT_R32_SFLOAT) {
EmitCs([
cClearValue = Values[0],
cDstSlice = bufferView->slice()
] (DxvkContext* ctx) {
ctx->clearBuffer(
cDstSlice.buffer(),
cDstSlice.offset(),
cDstSlice.length(),
cClearValue);
});
} else {
// Create a view with an integer format if necessary
if (uavFormat != rawFormat) {
DxvkBufferViewCreateInfo info = bufferView->info();
info.format = rawFormat;
bufferView = m_device->createBufferView(
bufferView->buffer(), info);
}
EmitCs([
cClearValue = clearValue,
cDstView = bufferView
] (DxvkContext* ctx) {
ctx->clearBufferView(
cDstView, 0,
cDstView->elementCount(),
cClearValue.color);
});
}
} else {
// Create a view with an integer format if necessary
Rc<DxvkImageView> imageView = uav->GetImageView();
if (uavFormat != rawFormat) {
DxvkImageViewCreateInfo info = imageView->info();
info.format = rawFormat;
imageView = m_device->createImageView(
imageView->image(), info);
}
EmitCs([
cClearValue = clearValue,
cDstView = imageView
] (DxvkContext* ctx) {
ctx->clearImageView(cDstView,
VkOffset3D { 0, 0, 0 },
cDstView->mipLevelExtent(0),
VK_IMAGE_ASPECT_COLOR_BIT,
cClearValue);
});
}
}
void STDMETHODCALLTYPE D3D11DeviceContext::ClearUnorderedAccessViewFloat(
ID3D11UnorderedAccessView* pUnorderedAccessView,
const FLOAT Values[4]) {
D3D10DeviceLock lock = LockContext();
auto uav = static_cast<D3D11UnorderedAccessView*>(pUnorderedAccessView);
if (!uav)
return;
auto imgView = uav->GetImageView();
auto bufView = uav->GetBufferView();
const DxvkFormatInfo* info = nullptr;
if (imgView != nullptr) info = imgView->formatInfo();
if (bufView != nullptr) info = bufView->formatInfo();
if (!info || info->flags.any(DxvkFormatFlag::SampledSInt, DxvkFormatFlag::SampledUInt))
return;
VkClearValue clearValue;
clearValue.color.float32[0] = Values[0];
clearValue.color.float32[1] = Values[1];
clearValue.color.float32[2] = Values[2];
clearValue.color.float32[3] = Values[3];
if (uav->GetResourceType() == D3D11_RESOURCE_DIMENSION_BUFFER) {
EmitCs([
cClearValue = clearValue,
cDstView = std::move(bufView)
] (DxvkContext* ctx) {
ctx->clearBufferView(
cDstView, 0,
cDstView->elementCount(),
cClearValue.color);
});
} else {
EmitCs([
cClearValue = clearValue,
cDstView = std::move(imgView)
] (DxvkContext* ctx) {
ctx->clearImageView(cDstView,
VkOffset3D { 0, 0, 0 },
cDstView->mipLevelExtent(0),
VK_IMAGE_ASPECT_COLOR_BIT,
cClearValue);
});
}
}
void STDMETHODCALLTYPE D3D11DeviceContext::ClearDepthStencilView(
ID3D11DepthStencilView* pDepthStencilView,
UINT ClearFlags,
FLOAT Depth,
UINT8 Stencil) {
D3D10DeviceLock lock = LockContext();
auto dsv = static_cast<D3D11DepthStencilView*>(pDepthStencilView);
if (!dsv)
return;
// Figure out which aspects to clear based on
// the image view properties and clear flags.
VkImageAspectFlags aspectMask = 0;
if (ClearFlags & D3D11_CLEAR_DEPTH)
aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT;
if (ClearFlags & D3D11_CLEAR_STENCIL)
aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
aspectMask &= dsv->GetWritableAspectMask();
if (!aspectMask)
return;
VkClearValue clearValue;
clearValue.depthStencil.depth = Depth;
clearValue.depthStencil.stencil = Stencil;
EmitCs([
cClearValue = clearValue,
cAspectMask = aspectMask,
cImageView = dsv->GetImageView()
] (DxvkContext* ctx) {
ctx->clearRenderTarget(
cImageView,
cAspectMask,
cClearValue);
});
}
void STDMETHODCALLTYPE D3D11DeviceContext::ClearView(
ID3D11View* pView,
const FLOAT Color[4],
const D3D11_RECT* pRect,
UINT NumRects) {
D3D10DeviceLock lock = LockContext();
if (NumRects && !pRect)
return;
// ID3D11View has no methods to query the exact type of
// the view, so we'll have to check each possible class
auto dsv = dynamic_cast<D3D11DepthStencilView*>(pView);
auto rtv = dynamic_cast<D3D11RenderTargetView*>(pView);
auto uav = dynamic_cast<D3D11UnorderedAccessView*>(pView);
// Retrieve underlying resource view
Rc<DxvkBufferView> bufView;
Rc<DxvkImageView> imgView;
if (dsv != nullptr)
imgView = dsv->GetImageView();
if (rtv != nullptr)
imgView = rtv->GetImageView();
if (uav != nullptr) {
bufView = uav->GetBufferView();
imgView = uav->GetImageView();
}
// 3D views are unsupported
if (imgView != nullptr
&& imgView->info().type == VK_IMAGE_VIEW_TYPE_3D)
return;
// Query the view format. We'll have to convert
// the clear color based on the format's data type.
VkFormat format = VK_FORMAT_UNDEFINED;
if (bufView != nullptr)
format = bufView->info().format;
if (imgView != nullptr)
format = imgView->info().format;
if (format == VK_FORMAT_UNDEFINED)
return;
// We'll need the format info to determine the buffer
// element size, and we also need it for depth images.
const DxvkFormatInfo* formatInfo = imageFormatInfo(format);
// Convert the clear color format. ClearView takes
// the clear value for integer formats as a set of
// integral floats, so we'll have to convert.
VkClearValue clearValue = ConvertColorValue(Color, formatInfo);
VkImageAspectFlags clearAspect = formatInfo->aspectMask & (VK_IMAGE_ASPECT_COLOR_BIT | VK_IMAGE_ASPECT_DEPTH_BIT);
// Clear all the rectangles that are specified