forked from microsoft/D3D12TranslationLayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchedContext.cpp
2098 lines (1883 loc) · 98 KB
/
BatchedContext.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) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
namespace D3D12TranslationLayer
{
//----------------------------------------------------------------------------------------------------------------------------------
// Ptr on input points to current command, and on output points to next command
template <typename TCmd> TCmd const& GetCommandData(const void*& pPtrToCommandValue)
{
// Ensure this overall structure is aligned to return pointer to next structure. Internal alignment handled by the compiler.
struct alignas(BatchedContext::BatchPrimitive)Temp { UINT CommandValue; TCmd Command; };
Temp const* pPtr = reinterpret_cast<Temp const*>(pPtrToCommandValue);
pPtrToCommandValue = pPtr + 1;
return pPtr->Command;
}
// Ptr on input points to current command, and on output points to next command
template <typename TCmd, typename TEntryCountType, typename TEntry>
TCmd const& GetCommandDataVariableSize(const void*& pPtrToCommandValue, UINT TEntryCountType::*NumEntries, TEntry const*& entries)
{
// Compiler ensures that pointer to first entry is aligned correctly
struct Temp { UINT CommandValue; TCmd Command; TEntry FirstEntry; };
Temp const* pPtr = reinterpret_cast<Temp const*>(pPtrToCommandValue);
entries = pPtr->Command.*NumEntries ? &pPtr->FirstEntry : nullptr;
// Manually align the pointer to the next command.
pPtrToCommandValue = BatchedContext::AlignPtr(&pPtr->FirstEntry + pPtr->Command.*NumEntries);
return pPtr->Command;
}
template <UINT CommandValue> struct CommandDispatcher;
template <> struct CommandDispatcher<BatchedContext::CmdSetPipelineState::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetPipelineState>(pCommandData);
ImmCtx.SetPipelineState(Data.pPSO);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdDrawInstanced::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdDrawInstanced>(pCommandData);
ImmCtx.DrawInstanced(Data.countPerInstance, Data.instanceCount, Data.vertexStart, Data.instanceStart);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdDrawIndexedInstanced::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdDrawIndexedInstanced>(pCommandData);
ImmCtx.DrawIndexedInstanced(Data.countPerInstance, Data.instanceCount, Data.indexStart, Data.vertexStart, Data.instanceStart);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdDispatch::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdDispatch>(pCommandData);
ImmCtx.Dispatch(Data.x, Data.y, Data.z);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdDrawAuto::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
(void)GetCommandData<BatchedContext::CmdDrawAuto>(pCommandData);
ImmCtx.DrawAuto();
}
};
template <> struct CommandDispatcher<BatchedContext::CmdDrawInstancedIndirect::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdDrawInstancedIndirect>(pCommandData);
ImmCtx.DrawInstancedIndirect(Data.pBuffer, Data.offset);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdDrawIndexedInstancedIndirect::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdDrawIndexedInstancedIndirect>(pCommandData);
ImmCtx.DrawIndexedInstancedIndirect(Data.pBuffer, Data.offset);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdDispatchIndirect::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdDispatchIndirect>(pCommandData);
ImmCtx.DispatchIndirect(Data.pBuffer, Data.offset);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetTopology::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetTopology>(pCommandData);
ImmCtx.IaSetTopology(Data.topology);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetVertexBuffers::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
struct Temp { BatchedContext::CmdSetVertexBuffers Cmd; Resource* pFirstVB; } const* pTemp = reinterpret_cast<Temp const*>(pCommandData);
BatchedContext::CmdSetVertexBuffers const* pCmd = &pTemp->Cmd;
auto ppVBs = &pTemp->pFirstVB;
// Ptr guaranteed to be aligned because alignof(UINT) <= alignof(Resource*)
auto pStrides = reinterpret_cast<UINT const*>(ppVBs + pCmd->numVBs);
auto pOffsets = pStrides + pCmd->numVBs;
// Align pointer to next command
pCommandData = BatchedContext::AlignPtr(pOffsets + pCmd->numVBs);
ImmCtx.IaSetVertexBuffers(pCmd->startSlot, pCmd->numVBs, ppVBs, pStrides, pOffsets);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetIndexBuffer::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetIndexBuffer>(pCommandData);
ImmCtx.IaSetIndexBuffer(Data.pBuffer, Data.format, Data.offset);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetShaderResources::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
SRV* const* ppSRVs = nullptr;
auto& Data = GetCommandDataVariableSize<BatchedContext::CmdSetShaderResources>(pCommandData, &BatchedContext::CmdSetShaderResources::numSRVs, ppSRVs);
switch (Data.stage)
{
case e_VS: ImmCtx.SetShaderResources<e_VS>(Data.startSlot, Data.numSRVs, ppSRVs); break;
case e_PS: ImmCtx.SetShaderResources<e_PS>(Data.startSlot, Data.numSRVs, ppSRVs); break;
case e_GS: ImmCtx.SetShaderResources<e_GS>(Data.startSlot, Data.numSRVs, ppSRVs); break;
case e_HS: ImmCtx.SetShaderResources<e_HS>(Data.startSlot, Data.numSRVs, ppSRVs); break;
case e_DS: ImmCtx.SetShaderResources<e_DS>(Data.startSlot, Data.numSRVs, ppSRVs); break;
case e_CS: ImmCtx.SetShaderResources<e_CS>(Data.startSlot, Data.numSRVs, ppSRVs); break;
}
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetSamplers::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
Sampler* const* ppSamplers = nullptr;
auto& Data = GetCommandDataVariableSize<BatchedContext::CmdSetSamplers>(pCommandData, &BatchedContext::CmdSetSamplers::numSamplers, ppSamplers);
switch (Data.stage)
{
case e_VS: ImmCtx.SetSamplers<e_VS>(Data.startSlot, Data.numSamplers, ppSamplers); break;
case e_PS: ImmCtx.SetSamplers<e_PS>(Data.startSlot, Data.numSamplers, ppSamplers); break;
case e_GS: ImmCtx.SetSamplers<e_GS>(Data.startSlot, Data.numSamplers, ppSamplers); break;
case e_HS: ImmCtx.SetSamplers<e_HS>(Data.startSlot, Data.numSamplers, ppSamplers); break;
case e_DS: ImmCtx.SetSamplers<e_DS>(Data.startSlot, Data.numSamplers, ppSamplers); break;
case e_CS: ImmCtx.SetSamplers<e_CS>(Data.startSlot, Data.numSamplers, ppSamplers); break;
}
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetConstantBuffers::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
struct Temp { BatchedContext::CmdSetConstantBuffers Cmd; Resource* pFirstCB; } const* pTemp = reinterpret_cast<Temp const*>(pCommandData);
BatchedContext::CmdSetConstantBuffers const* pCmd = &pTemp->Cmd;
auto ppCBs = &pTemp->pFirstCB;
// Ptr guaranteed to be aligned because alignof(UINT) <= alignof(Resource*)
auto pFirstConstant = reinterpret_cast<UINT const*>(ppCBs + pCmd->numCBs);
auto pNumConstants = pFirstConstant + pCmd->numCBs;
// Align pointer to next command
pCommandData = BatchedContext::AlignPtr(pNumConstants + pCmd->numCBs);
switch (pCmd->stage)
{
case e_VS: ImmCtx.SetConstantBuffers<e_VS>(pCmd->startSlot, pCmd->numCBs, ppCBs, pFirstConstant, pNumConstants); break;
case e_PS: ImmCtx.SetConstantBuffers<e_PS>(pCmd->startSlot, pCmd->numCBs, ppCBs, pFirstConstant, pNumConstants); break;
case e_GS: ImmCtx.SetConstantBuffers<e_GS>(pCmd->startSlot, pCmd->numCBs, ppCBs, pFirstConstant, pNumConstants); break;
case e_HS: ImmCtx.SetConstantBuffers<e_HS>(pCmd->startSlot, pCmd->numCBs, ppCBs, pFirstConstant, pNumConstants); break;
case e_DS: ImmCtx.SetConstantBuffers<e_DS>(pCmd->startSlot, pCmd->numCBs, ppCBs, pFirstConstant, pNumConstants); break;
case e_CS: ImmCtx.SetConstantBuffers<e_CS>(pCmd->startSlot, pCmd->numCBs, ppCBs, pFirstConstant, pNumConstants); break;
}
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetConstantBuffersNullOffsetSize::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
Resource* const* ppCBs = nullptr;
auto& Data = GetCommandDataVariableSize<BatchedContext::CmdSetConstantBuffersNullOffsetSize>(pCommandData, &BatchedContext::CmdSetConstantBuffersNullOffsetSize::numCBs, ppCBs);
switch (Data.stage)
{
case e_VS: ImmCtx.SetConstantBuffers<e_VS>(Data.startSlot, Data.numCBs, ppCBs, nullptr, nullptr); break;
case e_PS: ImmCtx.SetConstantBuffers<e_PS>(Data.startSlot, Data.numCBs, ppCBs, nullptr, nullptr); break;
case e_GS: ImmCtx.SetConstantBuffers<e_GS>(Data.startSlot, Data.numCBs, ppCBs, nullptr, nullptr); break;
case e_HS: ImmCtx.SetConstantBuffers<e_HS>(Data.startSlot, Data.numCBs, ppCBs, nullptr, nullptr); break;
case e_DS: ImmCtx.SetConstantBuffers<e_DS>(Data.startSlot, Data.numCBs, ppCBs, nullptr, nullptr); break;
case e_CS: ImmCtx.SetConstantBuffers<e_CS>(Data.startSlot, Data.numCBs, ppCBs, nullptr, nullptr); break;
}
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetSOBuffers::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetSOBuffers>(pCommandData);
ImmCtx.SoSetTargets(4, 0, Data.pBuffers, Data.offsets);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetRenderTargets::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetRenderTargets>(pCommandData);
ImmCtx.OMSetRenderTargets(Data.pRTVs, 8, Data.pDSV);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetUAV::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetUAV>(pCommandData);
if (Data.graphics)
{
ImmCtx.OMSetUnorderedAccessViews(Data.slot, 1, &Data.pUAV, &Data.initialCount);
}
else
{
ImmCtx.CsSetUnorderedAccessViews(Data.slot, 1, &Data.pUAV, &Data.initialCount);
}
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetStencilRef::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetStencilRef>(pCommandData);
ImmCtx.OMSetStencilRef(Data.ref);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetBlendFactor::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetBlendFactor>(pCommandData);
ImmCtx.OMSetBlendFactor(Data.factor);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetViewport::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetViewport>(pCommandData);
ImmCtx.SetViewport(Data.slot, &Data.viewport);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetNumViewports::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetNumViewports>(pCommandData);
ImmCtx.SetNumViewports(Data.num);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetScissorRect::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetScissorRect>(pCommandData);
ImmCtx.SetScissorRect(Data.slot, &Data.rect);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetNumScissorRects::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetNumScissorRects>(pCommandData);
ImmCtx.SetNumScissorRects(Data.num);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetScissorEnable::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetScissorEnable>(pCommandData);
ImmCtx.SetScissorRectEnable(Data.enable);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdClearRenderTargetView::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
D3D12_RECT const* pRects = nullptr;
auto& Data = GetCommandDataVariableSize<BatchedContext::CmdClearRenderTargetView>(pCommandData, &BatchedContext::CmdClearRenderTargetView::numRects, pRects);
ImmCtx.ClearRenderTargetView(Data.pView, Data.color, Data.numRects, pRects);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdClearDepthStencilView::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
D3D12_RECT const* pRects = nullptr;
auto& Data = GetCommandDataVariableSize<BatchedContext::CmdClearDepthStencilView>(pCommandData, &BatchedContext::CmdClearDepthStencilView::numRects, pRects);
ImmCtx.ClearDepthStencilView(Data.pView, Data.flags, Data.depth, Data.stencil, Data.numRects, pRects);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdClearUnorderedAccessViewUint::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
D3D12_RECT const* pRects = nullptr;
auto& Data = GetCommandDataVariableSize<BatchedContext::CmdClearUnorderedAccessViewUint>(pCommandData, &BatchedContext::CmdClearUnorderedAccessViewUint::numRects, pRects);
ImmCtx.ClearUnorderedAccessViewUint(Data.pView, Data.color, Data.numRects, pRects);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdClearUnorderedAccessViewFloat::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
D3D12_RECT const* pRects = nullptr;
auto& Data = GetCommandDataVariableSize<BatchedContext::CmdClearUnorderedAccessViewFloat>(pCommandData, &BatchedContext::CmdClearUnorderedAccessViewFloat::numRects, pRects);
ImmCtx.ClearUnorderedAccessViewFloat(Data.pView, Data.color, Data.numRects, pRects);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdClearVideoDecoderOutputView::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
D3D12_RECT const* pRects = nullptr;
auto& Data = GetCommandDataVariableSize<BatchedContext::CmdClearVideoDecoderOutputView>(pCommandData, &BatchedContext::CmdClearVideoDecoderOutputView::numRects, pRects);
ImmCtx.ClearVideoDecoderOutputView(Data.pView, Data.color, Data.numRects, pRects);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdClearVideoProcessorInputView::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
D3D12_RECT const* pRects = nullptr;
auto& Data = GetCommandDataVariableSize<BatchedContext::CmdClearVideoProcessorInputView>(pCommandData, &BatchedContext::CmdClearVideoProcessorInputView::numRects, pRects);
ImmCtx.ClearVideoProcessorInputView(Data.pView, Data.color, Data.numRects, pRects);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdClearVideoProcessorOutputView::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
D3D12_RECT const* pRects = nullptr;
auto& Data = GetCommandDataVariableSize<BatchedContext::CmdClearVideoProcessorOutputView>(pCommandData, &BatchedContext::CmdClearVideoProcessorOutputView::numRects, pRects);
ImmCtx.ClearVideoProcessorOutputView(Data.pView, Data.color, Data.numRects, pRects);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdDiscardView::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
D3D12_RECT const* pRects = nullptr;
auto& Data = GetCommandDataVariableSize<BatchedContext::CmdDiscardView>(pCommandData, &BatchedContext::CmdDiscardView::numRects, pRects);
ImmCtx.DiscardView(Data.pView, pRects, Data.numRects);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdDiscardResource::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
D3D12_RECT const* pRects = nullptr;
auto& Data = GetCommandDataVariableSize<BatchedContext::CmdDiscardResource>(pCommandData, &BatchedContext::CmdDiscardResource::numRects, pRects);
ImmCtx.DiscardResource(Data.pResource, pRects, Data.numRects);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdGenMips::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdGenMips>(pCommandData);
ImmCtx.GenMips(Data.pSRV, Data.filterType);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdFinalizeUpdateSubresources::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdFinalizeUpdateSubresources>(pCommandData);
ImmCtx.FinalizeUpdateSubresources(Data.pDst, Data.Op, nullptr);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdFinalizeUpdateSubresourcesWithLocalPlacement::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdFinalizeUpdateSubresourcesWithLocalPlacement>(pCommandData);
ImmCtx.FinalizeUpdateSubresources(Data.pDst, Data.Op.Base, Data.Op.LocalPlacementDescs);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdRename::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdRename>(pCommandData);
ImmCtx.Rename(Data.pResource, Data.pRenameResource);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdRenameViaCopy::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdRenameViaCopy>(pCommandData);
ImmCtx.RenameViaCopy(Data.pResource, Data.pRenameResource, Data.dirtyPlaneMask);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdQueryBegin::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdQueryBegin>(pCommandData);
ImmCtx.QueryBegin(Data.pQuery);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdQueryEnd::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdQueryEnd>(pCommandData);
ImmCtx.QueryEnd(Data.pQuery);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetPredication::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetPredication>(pCommandData);
ImmCtx.SetPredication(Data.pPredicate, Data.Value);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdResourceCopy::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdResourceCopy>(pCommandData);
ImmCtx.ResourceCopy(Data.pDst, Data.pSrc);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdResolveSubresource::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdResolveSubresource>(pCommandData);
ImmCtx.ResourceResolveSubresource(Data.pDst, Data.DstSubresource, Data.pSrc, Data.SrcSubresource, Data.Format);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdResourceCopyRegion::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdResourceCopyRegion>(pCommandData);
ImmCtx.ResourceCopyRegion(Data.pDst, Data.DstSubresource, Data.DstX, Data.DstY, Data.DstZ, Data.pSrc, Data.SrcSubresource, &Data.SrcBox);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetResourceMinLOD::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetResourceMinLOD>(pCommandData);
ImmCtx.SetResourceMinLOD(Data.pResource, Data.MinLOD);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdCopyStructureCount::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdCopyStructureCount>(pCommandData);
ImmCtx.CopyStructureCount(Data.pDst, Data.DstOffset, Data.pSrc);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdRotateResourceIdentities::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
Resource* const* ppResources = nullptr;
auto& Data = GetCommandDataVariableSize<BatchedContext::CmdRotateResourceIdentities>(pCommandData, &BatchedContext::CmdRotateResourceIdentities::NumResources, ppResources);
ImmCtx.RotateResourceIdentities(ppResources, Data.NumResources);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdExtension::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto pData = reinterpret_cast<BatchedContext::CmdExtension const*>(pCommandData);
const void* pExtensionData = BatchedContext::AlignPtr(pData + 1);
pCommandData = reinterpret_cast<const BYTE*>(pExtensionData) + pData->DataSize;
pData->pExt->Dispatch(ImmCtx, pExtensionData, pData->DataSize);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetHardwareProtection::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetHardwareProtection>(pCommandData);
ImmCtx.SetHardwareProtection(Data.pResource, Data.Value);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetHardwareProtectionState::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdSetHardwareProtectionState>(pCommandData);
ImmCtx.SetHardwareProtectionState(Data.State);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdClearState::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
(void)GetCommandData<BatchedContext::CmdClearState>(pCommandData);
ImmCtx.ClearState();
}
};
template <> struct CommandDispatcher<BatchedContext::CmdUpdateTileMappings::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
struct Temp { BatchedContext::CmdUpdateTileMappings Cmd; D3D12_TILED_RESOURCE_COORDINATE Coords; } const* pTemp = reinterpret_cast<Temp const*>(pCommandData);
static_assert(alignof(D3D12_TILED_RESOURCE_COORDINATE) == alignof(UINT));
static_assert(alignof(D3D12_TILE_REGION_SIZE) == alignof(UINT));
BatchedContext::CmdUpdateTileMappings const* pCmd = &pTemp->Cmd;
// Note: Memory for all arrays is unconditionally allocated, even if null pointers are provided, for pointer math simplicity.
D3D12_TILED_RESOURCE_COORDINATE const* pCoords = &pTemp->Coords;
auto pRegions = reinterpret_cast<D3D12_TILE_REGION_SIZE const*>(pCoords + pCmd->NumTiledResourceRegions);
auto pRangeFlags = reinterpret_cast<ImmediateContext::TILE_RANGE_FLAG const*>(pRegions + pCmd->NumTiledResourceRegions);
auto pTilePoolStartOffsets = reinterpret_cast<const UINT*>(pRangeFlags + pCmd->NumRanges);
auto pRangeTileCounts = pTilePoolStartOffsets + pCmd->NumRanges;
pCommandData = BatchedContext::AlignPtr(pRangeTileCounts + pCmd->NumRanges);
ImmCtx.UpdateTileMappings(pCmd->pTiledResource,
pCmd->NumTiledResourceRegions,
pCoords,
pCmd->bTiledResourceRegionSizesPresent ? pRegions : nullptr,
pCmd->pTilePool,
pCmd->NumRanges,
pCmd->bRangeFlagsPresent ? pRangeFlags : nullptr,
pCmd->bTilePoolStartOffsetsPresent ? pTilePoolStartOffsets : nullptr,
pCmd->bRangeTileCountsPresent ? pRangeTileCounts : nullptr,
pCmd->Flags);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdCopyTileMappings::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdCopyTileMappings>(pCommandData);
ImmCtx.CopyTileMappings(Data.pDstTiledResource, &Data.DstStartCoords, Data.pSrcTiledResource, &Data.SrcStartCoords, &Data.TileRegion, Data.Flags);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdCopyTiles::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdCopyTiles>(pCommandData);
ImmCtx.CopyTiles(Data.pResource, &Data.StartCoords, &Data.TileRegion, Data.pBuffer, Data.BufferOffset, Data.Flags);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdTiledResourceBarrier::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdTiledResourceBarrier>(pCommandData);
ImmCtx.TiledResourceBarrier(Data.pBefore, Data.pAfter);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdResizeTilePool::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdResizeTilePool>(pCommandData);
ImmCtx.ResizeTilePool(Data.pTilePool, Data.NewSize);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdExecuteNestedBatch::CmdValue>
{
static void Execute(ImmediateContext&, const void*& pCommandData)
{
auto& Data = GetCommandData<BatchedContext::CmdExecuteNestedBatch>(pCommandData);
// Note: The D3D11 runtime also recursively executes nested command lists, so this is probably safe to do.
Data.pThis->ProcessBatchImpl(Data.pBatch);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdSetMarker::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
const wchar_t* name = nullptr;
(void)GetCommandDataVariableSize<BatchedContext::CmdSetMarker>(pCommandData, &BatchedContext::CmdSetMarker::NumChars, name);
ImmCtx.SetMarker(name);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdBeginEvent::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
const wchar_t* name = nullptr;
(void)GetCommandDataVariableSize<BatchedContext::CmdBeginEvent>(pCommandData, &BatchedContext::CmdBeginEvent::NumChars, name);
ImmCtx.BeginEvent(name);
}
};
template <> struct CommandDispatcher<BatchedContext::CmdEndEvent::CmdValue>
{
static void Execute(ImmediateContext& ImmCtx, const void*& pCommandData)
{
(void)GetCommandData<BatchedContext::CmdEndEvent>(pCommandData);
ImmCtx.EndEvent();
}
};
//----------------------------------------------------------------------------------------------------------------------------------
// These structs generate an array consisting of the functions defined above,
// in order of their command value, so it can be indexed by value as a dispatch table.
template <int N, int... Rest>
struct DispatchArrayImpl
{
// Generic case, just grab value from below.
// This is recursive, so Rest starts from empty, to N, to (N-1, N), etc, until 0 is the first value.
// Then it triggers the base.
static constexpr auto& value = DispatchArrayImpl<N - 1, N, Rest...>::value;
};
template <int... Rest>
struct DispatchArrayImpl<0, Rest...>
{
// Base case, define array.
static constexpr BatchedContext::DispatcherFunction value[] = { &CommandDispatcher<0>::Execute, &CommandDispatcher<Rest>::Execute... };
};
// Statics need to be explicitly instantiated.
template <int... Rest>
constexpr BatchedContext::DispatcherFunction DispatchArrayImpl<0, Rest...>::value[];
// Instantiate with the correct number of commands. Note that the array generated is inclusive, not exclusive.
constexpr auto& DispatchArray = DispatchArrayImpl<BatchedContext::c_LastCommand>::value;
//----------------------------------------------------------------------------------------------------------------------------------
BatchedContext::BatchedContext(ImmediateContext& ImmCtx, CreationArgs args, Callbacks const& callbacks)
: m_ImmCtx(ImmCtx) // WARNING: ImmCtx might not be initialized yet, avoid any access to it during this constructor.
, m_CreationArgs(args)
, m_DispatchTable(DispatchArray)
, m_Callbacks(callbacks)
{
if (args.SubmitBatchesToWorkerThread)
{
m_BatchSubmittedSemaphore.m_h = CreateSemaphore(nullptr, 0, c_MaxOutstandingBatches, nullptr);
ThrowIfHandleNull(m_BatchSubmittedSemaphore);
m_BatchConsumedSemaphore.m_h = CreateSemaphore(nullptr, 0, c_MaxOutstandingBatches, nullptr);
ThrowIfHandleNull(m_BatchConsumedSemaphore);
m_BatchThread.m_h = CreateThread(
nullptr, 0,
[](void* pContext) -> DWORD
{
reinterpret_cast<BatchedContext*>(pContext)->BatchThread();
return 0;
}, this, CREATE_SUSPENDED, nullptr);
ThrowIfHandleNull(m_BatchThread);
ResumeThread(m_BatchThread.m_h);
}
}
//----------------------------------------------------------------------------------------------------------------------------------
BatchedContext::~BatchedContext()
{
assert(!IsBatchThread());
// Batch producing contexts shouldn't flush on their own.
if (!m_CreationArgs.pParentContext)
{
ProcessBatch();
}
if (m_CreationArgs.SubmitBatchesToWorkerThread)
{
assert(m_NumOutstandingBatches == 0 && m_QueuedBatches.empty());
// When the batch thread wakes up after consuming a semaphore value, and
// sees that the queue is empty, it will exit.
BOOL value = ReleaseSemaphore(m_BatchSubmittedSemaphore, 1, nullptr);
assert(value == TRUE);
UNREFERENCED_PARAMETER(value);
// Wait for it to exit.
WaitForSingleObject(m_BatchThread, INFINITE);
}
}
//----------------------------------------------------------------------------------------------------------------------------------
template <typename TCmd>
void BatchedContext::AddToBatch(BatchStorage& CurrentBatch, TCmd const& command)
{
assert(!IsBatchThread());
static_assert(std::is_trivially_destructible<TCmd>::value, "Destructors don't get called on batched commands.");
struct alignas(BatchPrimitive)Temp { UINT CommandValue; TCmd Command; };
if (!CurrentBatch.reserve_contiguous(sizeof(Temp) / sizeof(BatchPrimitive)))
{
throw std::bad_alloc();
}
Temp* pPtr = reinterpret_cast<Temp*>(CurrentBatch.append_contiguous_manually(sizeof(Temp) / sizeof(BatchPrimitive)));
pPtr->CommandValue = TCmd::CmdValue;
pPtr->Command = command;
}
//----------------------------------------------------------------------------------------------------------------------------------
template <typename TCmd, typename TEntry>
void BatchedContext::AddToBatchVariableSize(TCmd const& command, UINT NumEntries, TEntry const* entries)
{
assert(!IsBatchThread());
auto Lock = m_RecordingLock.TakeLock();
static_assert(std::is_trivially_destructible<TCmd>::value, "Destructors don't get called on batched commands.");
struct Temp { UINT CommandValue; TCmd Command; TEntry FirstEntry; };
const size_t TotalSizeInBytes = Align(offsetof(Temp, FirstEntry) + sizeof(TEntry) * (NumEntries), sizeof(BatchPrimitive));
const size_t TotalSizeInElements = TotalSizeInBytes / sizeof(BatchPrimitive);
if (!m_CurrentBatch.reserve_contiguous(TotalSizeInElements))
{
throw std::bad_alloc();
}
Temp* pPtr = reinterpret_cast<Temp*>(m_CurrentBatch.append_contiguous_manually(TotalSizeInElements));
pPtr->CommandValue = TCmd::CmdValue;
pPtr->Command = command;
std::copy(entries, entries + NumEntries, &pPtr->FirstEntry);
++m_CurrentCommandCount;
SubmitBatchIfIdle();
}
//----------------------------------------------------------------------------------------------------------------------------------
template <typename TCmd, typename... Args>
void BatchedContext::EmplaceInBatch(Args&&... args)
{
assert(!IsBatchThread());
auto Lock = m_RecordingLock.TakeLock();
static_assert(std::is_trivially_destructible<TCmd>::value, "Destructors don't get called on batched commands.");
const size_t CommandSize = TCmd::GetCommandSize(std::forward<Args>(args)...);
// GetCommandSize must ensure size is aligned correctly.
assert(CommandSize % sizeof(BatchPrimitive) == 0);
if (!m_CurrentBatch.reserve_contiguous(CommandSize / sizeof(BatchPrimitive)))
{
throw std::bad_alloc();
}
void* pPtr = m_CurrentBatch.append_contiguous_manually(CommandSize / sizeof(BatchPrimitive));
new (pPtr) TCmd(std::forward<Args>(args)...);
++m_CurrentCommandCount;
SubmitBatchIfIdle();
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::SetPipelineState(PipelineState* pPSO)
{
AddToBatch(CmdSetPipelineState{ pPSO });
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::DrawInstanced(UINT countPerInstance, UINT instanceCount, UINT vertexStart, UINT instanceStart)
{
AddToBatch(CmdDrawInstanced{ countPerInstance, instanceCount, vertexStart, instanceStart });
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::DrawIndexedInstanced(UINT countPerInstance, UINT instanceCount, UINT indexStart, INT vertexStart, UINT instanceStart)
{
AddToBatch(CmdDrawIndexedInstanced{ countPerInstance, instanceCount, indexStart, vertexStart, instanceStart });
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::DrawAuto()
{
AddToBatch(CmdDrawAuto{});
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::DrawIndexedInstancedIndirect(Resource* pBuffer, UINT offset)
{
AddToBatch(CmdDrawIndexedInstancedIndirect{ pBuffer, offset });
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::DrawInstancedIndirect(Resource* pBuffer, UINT offset)
{
AddToBatch(CmdDrawInstancedIndirect{ pBuffer, offset });
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::Dispatch(UINT x, UINT y, UINT z)
{
AddToBatch(CmdDispatch{ x, y, z });
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::DispatchIndirect(Resource* pBuffer, UINT offset)
{
AddToBatch(CmdDispatchIndirect{ pBuffer, offset });
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::IaSetTopology(D3D12_PRIMITIVE_TOPOLOGY topology)
{
AddToBatch(CmdSetTopology{ topology });
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::IaSetVertexBuffers(UINT StartSlot, __in_range(0, D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT) UINT NumBuffers, Resource** pVBs, const UINT*pStrides, const UINT* pOffsets)
{
EmplaceInBatch<CmdSetVertexBuffers>(StartSlot, NumBuffers, pVBs, pStrides, pOffsets);
}
BatchedContext::CmdSetVertexBuffers::CmdSetVertexBuffers(UINT _startSlot, UINT _numVBs, Resource* const* _ppVBs, UINT const* _pStrides, UINT const* _pOffsets)
: startSlot(_startSlot)
, numVBs(_numVBs)
{
struct Temp { BatchedContext::CmdSetVertexBuffers Cmd; Resource* pFirstVB; } *pTemp = reinterpret_cast<Temp*>(this);
auto ppVBs = &pTemp->pFirstVB;
// Ptr guaranteed to be aligned because alignof(UINT) <= alignof(Resource*)
auto pStrides = reinterpret_cast<UINT*>(ppVBs + numVBs);
auto pOffsets = pStrides + numVBs;
std::copy(_ppVBs, _ppVBs + numVBs, ppVBs);
std::copy(_pStrides, _pStrides + numVBs, pStrides);
std::copy(_pOffsets, _pOffsets + numVBs, pOffsets);
}
size_t BatchedContext::CmdSetVertexBuffers::GetCommandSize(UINT, UINT _numVBs, Resource* const*, UINT const*, UINT const*)
{
struct Temp { BatchedContext::CmdSetVertexBuffers Cmd; Resource* pFirstVB; };
return Align(offsetof(Temp, pFirstVB) + (sizeof(Resource*) + sizeof(UINT) * 2) * _numVBs, sizeof(BatchPrimitive));
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::IaSetIndexBuffer(Resource* pBuffer, DXGI_FORMAT format, UINT offset)
{
AddToBatch(CmdSetIndexBuffer{ pBuffer, format, offset });
}
//----------------------------------------------------------------------------------------------------------------------------------
template <EShaderStage ShaderStage>
void TRANSLATION_API BatchedContext::SetShaderResources(UINT StartSlot, __in_range(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT) UINT NumSRVs, SRV* const* ppSRVs)
{
AddToBatchVariableSize(CmdSetShaderResources{ ShaderStage, StartSlot, NumSRVs }, NumSRVs, ppSRVs);
}
template void TRANSLATION_API BatchedContext::SetShaderResources<e_VS>(UINT, UINT, SRV* const*);
template void TRANSLATION_API BatchedContext::SetShaderResources<e_PS>(UINT, UINT, SRV* const*);
template void TRANSLATION_API BatchedContext::SetShaderResources<e_GS>(UINT, UINT, SRV* const*);
template void TRANSLATION_API BatchedContext::SetShaderResources<e_HS>(UINT, UINT, SRV* const*);
template void TRANSLATION_API BatchedContext::SetShaderResources<e_DS>(UINT, UINT, SRV* const*);
template void TRANSLATION_API BatchedContext::SetShaderResources<e_CS>(UINT, UINT, SRV* const*);
//----------------------------------------------------------------------------------------------------------------------------------
template <EShaderStage ShaderStage>
void TRANSLATION_API BatchedContext::SetSamplers(UINT StartSlot, __in_range(0, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT) UINT NumSamplers, Sampler** ppSamplers)
{
AddToBatchVariableSize(CmdSetSamplers{ ShaderStage, StartSlot, NumSamplers }, NumSamplers, ppSamplers );
}
template void TRANSLATION_API BatchedContext::SetSamplers<e_VS>(UINT, UINT, Sampler**);
template void TRANSLATION_API BatchedContext::SetSamplers<e_PS>(UINT, UINT, Sampler**);
template void TRANSLATION_API BatchedContext::SetSamplers<e_GS>(UINT, UINT, Sampler**);
template void TRANSLATION_API BatchedContext::SetSamplers<e_HS>(UINT, UINT, Sampler**);
template void TRANSLATION_API BatchedContext::SetSamplers<e_DS>(UINT, UINT, Sampler**);
template void TRANSLATION_API BatchedContext::SetSamplers<e_CS>(UINT, UINT, Sampler**);
//----------------------------------------------------------------------------------------------------------------------------------
template <EShaderStage ShaderStage>
void TRANSLATION_API BatchedContext::SetConstantBuffers(UINT StartSlot, __in_range(0, D3D11_COMMONSHADER_CONSTANT_BUFFER_HW_SLOT_COUNT) UINT NumBuffers, Resource** ppCBs, __in_ecount_opt(NumBuffers) CONST UINT* pFirstConstant, __in_ecount_opt(NumBuffers) CONST UINT* pNumConstants)
{
if (pFirstConstant)
{
EmplaceInBatch<CmdSetConstantBuffers>(ShaderStage, StartSlot, NumBuffers, ppCBs, pFirstConstant, pNumConstants);
}
else
{
AddToBatchVariableSize(CmdSetConstantBuffersNullOffsetSize{ ShaderStage, StartSlot, NumBuffers }, NumBuffers, ppCBs);
}
}
template void TRANSLATION_API BatchedContext::SetConstantBuffers<e_VS>(UINT, UINT, Resource** ppCBs, CONST UINT* pFirstConstant, CONST UINT* pNumConstants);
template void TRANSLATION_API BatchedContext::SetConstantBuffers<e_PS>(UINT, UINT, Resource** ppCBs, CONST UINT* pFirstConstant, CONST UINT* pNumConstants);
template void TRANSLATION_API BatchedContext::SetConstantBuffers<e_GS>(UINT, UINT, Resource** ppCBs, CONST UINT* pFirstConstant, CONST UINT* pNumConstants);
template void TRANSLATION_API BatchedContext::SetConstantBuffers<e_HS>(UINT, UINT, Resource** ppCBs, CONST UINT* pFirstConstant, CONST UINT* pNumConstants);
template void TRANSLATION_API BatchedContext::SetConstantBuffers<e_DS>(UINT, UINT, Resource** ppCBs, CONST UINT* pFirstConstant, CONST UINT* pNumConstants);
template void TRANSLATION_API BatchedContext::SetConstantBuffers<e_CS>(UINT, UINT, Resource** ppCBs, CONST UINT* pFirstConstant, CONST UINT* pNumConstants);
BatchedContext::CmdSetConstantBuffers::CmdSetConstantBuffers(EShaderStage _stage, UINT _startSlot, UINT _numCBs, Resource* const* _ppCBs, UINT const* _pFirstConstant, UINT const* _pNumConstants)
: stage(_stage)
, startSlot(_startSlot)
, numCBs(_numCBs)
{
struct Temp { BatchedContext::CmdSetConstantBuffers Cmd; Resource* pFirstCB; } *pTemp = reinterpret_cast<Temp*>(this);
auto ppCBs = &pTemp->pFirstCB;
// Ptr guaranteed to be aligned because alignof(UINT) <= alignof(Resource*)
auto pFirstConstant = reinterpret_cast<UINT*>(ppCBs + numCBs);
auto pNumConstants = pFirstConstant + numCBs;
std::copy(_ppCBs, _ppCBs + numCBs, ppCBs);
std::copy(_pFirstConstant, _pFirstConstant + numCBs, pFirstConstant);
std::copy(_pNumConstants, _pNumConstants + numCBs, pNumConstants);
}
size_t BatchedContext::CmdSetConstantBuffers::GetCommandSize(EShaderStage, UINT, UINT numCBs, Resource* const*, UINT const*, UINT const*)
{
struct Temp { BatchedContext::CmdSetConstantBuffers Cmd; Resource* pFirstCB; };
return Align(offsetof(Temp, pFirstCB) + (sizeof(Resource*) + sizeof(UINT) * 2) * numCBs, sizeof(BatchPrimitive));
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::SoSetTargets(_In_range_(0, 4) UINT NumTargets, _In_range_(0, 4) UINT, _In_reads_(NumTargets) Resource** pBuffers, _In_reads_(NumTargets) const UINT* offsets)
{
CmdSetSOBuffers command = {};
std::copy(pBuffers, pBuffers + NumTargets, command.pBuffers);
std::copy(offsets, offsets + NumTargets, command.offsets);
AddToBatch(command);
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::OMSetRenderTargets(__in_ecount(NumRTVs) RTV** ppRTVs, __in_range(0, 8) UINT NumRTVs, __in_opt DSV *pDSV, __in_ecount(NumUavs) UAV** ppUavs, CONST UINT* pInitialCounts, UINT UAVStartSlot, __in_range(0, D3D11_1_UAV_SLOT_COUNT) UINT NumUavs)
{
CmdSetRenderTargets command = {};
std::copy(ppRTVs, ppRTVs + NumRTVs, command.pRTVs);
command.pDSV = pDSV;
AddToBatch(command);
for (UINT i = 0; i < D3D11_1_UAV_SLOT_COUNT; ++i)
{
UINT slot = i;
UINT inputIndex = slot - UAVStartSlot;
bool bValidUAVSlot = slot >= NumRTVs && inputIndex < NumUavs;
UAV* pUAV = bValidUAVSlot ? ppUavs[inputIndex] : nullptr;
if (m_UAVs.UpdateBinding(slot, pUAV) || (pUAV && pInitialCounts[inputIndex] != UINT_MAX))
{
AddToBatch(CmdSetUAV{ true, slot, pUAV, pUAV ? pInitialCounts[inputIndex] : UINT_MAX });
}
}
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::CsSetUnorderedAccessViews(UINT Start, __in_range(0, D3D11_1_UAV_SLOT_COUNT) UINT NumViews, __in_ecount(NumViews) UAV** ppUAVs, __in_ecount(NumViews) CONST UINT* pInitialCounts)
{
for (UINT i = 0; i < NumViews; ++i)
{
AddToBatch(CmdSetUAV{ false, i + Start, ppUAVs[i], pInitialCounts ? pInitialCounts[i] : UINT_MAX });
}
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::OMSetStencilRef(UINT StencilRef)
{
AddToBatch(CmdSetStencilRef{ StencilRef });
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::OMSetBlendFactor(const FLOAT BlendFactor[4])
{
AddToBatch(CmdSetBlendFactor{ {BlendFactor[0], BlendFactor[1], BlendFactor[2], BlendFactor[3]} });
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::SetViewports(UINT NumViewports, const D3D12_VIEWPORT* pViewports)
{
for (UINT i = 0; i < NumViewports; ++i)
{
if (memcmp(&pViewports[i], &m_Viewports[i], sizeof(D3D12_VIEWPORT)) != 0)
{
m_Viewports[i] = pViewports[i];
AddToBatch(CmdSetViewport{ i, pViewports[i] });
}
}
if (m_NumViewports != NumViewports)
{
AddToBatch(CmdSetNumViewports{ NumViewports });
m_NumViewports = NumViewports;
}
}
//----------------------------------------------------------------------------------------------------------------------------------
void TRANSLATION_API BatchedContext::SetScissorRects(UINT NumRects, const D3D12_RECT* pRects)
{
for (UINT i = 0; i < NumRects; ++i)
{
if (memcmp(&pRects[i], &m_Scissors[i], sizeof(D3D12_RECT)) != 0)
{
m_Scissors[i] = pRects[i];
AddToBatch(CmdSetScissorRect{ i, pRects[i] });
}
}
if (m_NumScissors != NumRects)
{
AddToBatch(CmdSetNumScissorRects{ NumRects });
m_NumScissors = NumRects;
}
}