-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex.cpp
More file actions
1093 lines (908 loc) · 38.1 KB
/
ex.cpp
File metadata and controls
1093 lines (908 loc) · 38.1 KB
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
```cpp
// Day1 Harness Skeleton: Instanced vs Naive (10k)
// Single-file style pseudo-implementation focusing on *mental model contracts*.
// Assumes: device, queue, swapchain, backbuffers, RTVs, viewport/scissor created.
#include <windows.h>
#include <wrl.h>
#include <d3d12.h>
#include <dxgi1_6.h>
#include <d3dcompiler.h>
#include <cstdint>
#include <vector>
#include <string>
#include <cassert>
#include <cstdio>
using Microsoft::WRL::ComPtr;
static void ThrowIfFailed(HRESULT hr) { if (FAILED(hr)) { __debugbreak(); } }
static constexpr UINT kFrameCount = 3;
static constexpr UINT kInstanceCount = 10000;
// ------------------------------
// (A) ABI: RootParam indices are YOUR ABI. Use names.
// ------------------------------
enum RootParam : UINT
{
RP_FrameCB = 0, // b0 space0
RP_TransformsTable = 1, // SRV t0 space0 (descriptor table)
RP_Count
};
// ------------------------------
// Shader (mailboxes): b0 space0, t0 space0
// ------------------------------
static const char* kHlslVS = R"(
cbuffer FrameCB : register(b0, space0)
{
float4x4 ViewProj;
};
StructuredBuffer<float4x4> Transforms : register(t0, space0);
struct VSIn { float3 Pos : POSITION; };
struct VSOut { float4 Pos : SV_Position; };
VSOut VSMain(VSIn vin, uint iid : SV_InstanceID)
{
VSOut o;
// (D) Instance correctness: iid must index the same buffer you think is bound to t0.
float4x4 M = Transforms[iid];
float4 wpos = mul(float4(vin.Pos, 1.0), M);
o.Pos = mul(wpos, ViewProj);
return o;
}
)";
static const char* kHlslPS = R"(
float4 PSMain() : SV_Target
{
return float4(1,1,1,1);
}
)";
// ------------------------------
// Per-frame ownership (B) Lifetime contract:
// "After submit, treat cmd + referenced resources read-only until fence passes."
// ------------------------------
struct FrameContext
{
ComPtr<ID3D12CommandAllocator> alloc;
UINT64 fenceValue = 0;
// Per-frame CB (upload heap): safe because we fence-gate reuse
ComPtr<ID3D12Resource> frameCB; // upload heap buffer holding FrameCB
void* frameCBMapped = nullptr;
// Per-frame transforms buffers: upload -> default
ComPtr<ID3D12Resource> transformsUpload; // upload heap
void* transformsUploadMapped = nullptr;
ComPtr<ID3D12Resource> transformsDefault; // default heap (GPU reads as SRV)
// Per-frame SRV descriptor slot index in a shader-visible heap
UINT srvSlot = 0;
// GPU timestamp query indices (2 per frame)
UINT queryBegin = 0;
UINT queryEnd = 0;
};
struct App
{
ComPtr<ID3D12Device> device;
ComPtr<ID3D12CommandQueue> queue;
ComPtr<IDXGISwapChain3> swap;
UINT backIndex = 0;
ComPtr<ID3D12GraphicsCommandList> cmd;
ComPtr<ID3D12Fence> fence;
HANDLE fenceEvent = nullptr;
UINT64 fenceCounter = 0;
FrameContext frames[kFrameCount];
// Descriptor heap for CBV/SRV/UAV (shader-visible)
ComPtr<ID3D12DescriptorHeap> cbvSrvUavHeap;
UINT descInc = 0;
// RootSig + PSO
ComPtr<ID3D12RootSignature> rootSig;
ComPtr<ID3D12PipelineState> pso;
// Timestamp queries
ComPtr<ID3D12QueryHeap> queryHeap;
ComPtr<ID3D12Resource> queryReadback; // readback heap buffer (uint64 results)
UINT64* queryReadbackMapped = nullptr;
// Toggles for microtests (proof levers)
bool modeInstanced = true;
bool break_RPIndexSwap = false; // bind table to wrong RP -> deterministic break
bool break_MailboxShift = false; // build rootSig SRV as t1 but shader uses t0 -> deterministic break
bool break_OmitSetHeaps = false; // omit SetDescriptorHeaps -> deterministic break
bool stomp_Lifetime = false; // intentionally reuse frame resources too early -> flicker/garbage
bool sentinel_Instance0 = true; // Transforms[0] huge translate -> only instance 0 moves
// Basic timing
LARGE_INTEGER qpcFreq{};
// Window / swapchain config
HWND hwnd = nullptr;
UINT width = 1280;
UINT height = 720;
DXGI_FORMAT backbufferFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
// Backbuffers + RTV heap
ComPtr<ID3D12DescriptorHeap> rtvHeap;
UINT rtvInc = 0;
ComPtr<ID3D12Resource> backBuffers[kFrameCount];
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandles[kFrameCount]{};
// Viewport/scissor
D3D12_VIEWPORT viewport{};
D3D12_RECT scissor{};
// Geometry: a cube (VB/IB in DEFAULT heap)
ComPtr<ID3D12Resource> vbDefault, ibDefault;
D3D12_VERTEX_BUFFER_VIEW vbv{};
D3D12_INDEX_BUFFER_VIEW ibv{};
UINT indexCount = 36;
};
// -------------------------------------------
// Helpers: descriptor CPU/GPU handles
// -------------------------------------------
static D3D12_CPU_DESCRIPTOR_HANDLE CpuHandleAt(ID3D12DescriptorHeap* heap, UINT slot, UINT inc)
{
D3D12_CPU_DESCRIPTOR_HANDLE h = heap->GetCPUDescriptorHandleForHeapStart();
h.ptr += UINT64(slot) * inc;
return h;
}
static D3D12_GPU_DESCRIPTOR_HANDLE GpuHandleAt(ID3D12DescriptorHeap* heap, UINT slot, UINT inc)
{
D3D12_GPU_DESCRIPTOR_HANDLE h = heap->GetGPUDescriptorHandleForHeapStart();
h.ptr += UINT64(slot) * inc;
return h;
}
// -------------------------------------------
// (B) Fence gate: reuse frame resources only after GPU done.
// -------------------------------------------
static void WaitForFence(App& a, UINT64 v)
{
if (a.fence->GetCompletedValue() >= v) return;
ThrowIfFailed(a.fence->SetEventOnCompletion(v, a.fenceEvent));
WaitForSingleObject(a.fenceEvent, INFINITE);
}
static FrameContext& BeginFrame(App& a)
{
a.backIndex = a.swap->GetCurrentBackBufferIndex();
FrameContext& fc = a.frames[a.backIndex % kFrameCount];
// If stomping lifetime, intentionally skip waiting (microtest)
if (!a.stomp_Lifetime && fc.fenceValue != 0)
WaitForFence(a, fc.fenceValue);
ThrowIfFailed(fc.alloc->Reset());
ThrowIfFailed(a.cmd->Reset(fc.alloc.Get(), a.pso.Get()));
return fc;
}
static void EndFrame(App& a, FrameContext& fc)
{
ThrowIfFailed(a.cmd->Close());
ID3D12CommandList* lists[] = { a.cmd.Get() };
a.queue->ExecuteCommandLists(1, lists);
// Signal fence for this frame context
a.fenceCounter++;
ThrowIfFailed(a.queue->Signal(a.fence.Get(), a.fenceCounter));
fc.fenceValue = a.fenceCounter;
}
// -------------------------------------------
// (1) Create Root Signature (ABI bridge)
// -------------------------------------------
static void CreateRootSig(App& a)
{
D3D12_DESCRIPTOR_RANGE1 srvRange = {};
srvRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
srvRange.NumDescriptors = 1;
srvRange.BaseShaderRegister = a.break_MailboxShift ? 1 : 0; // t1 if broken, else t0
srvRange.RegisterSpace = 0; // space0
srvRange.Flags = D3D12_DESCRIPTOR_RANGE_FLAG_DATA_STATIC; // OK if descriptor not rewritten mid-flight
srvRange.OffsetInDescriptorsFromTableStart = 0;
D3D12_ROOT_PARAMETER1 rp[RP_Count] = {};
// RP0: Root CBV -> b0 space0
rp[RP_FrameCB].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rp[RP_FrameCB].Descriptor.ShaderRegister = 0; // b0
rp[RP_FrameCB].Descriptor.RegisterSpace = 0;
rp[RP_FrameCB].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
// RP1: Descriptor Table -> SRV range t0 space0 (or t1 if broken)
D3D12_ROOT_DESCRIPTOR_TABLE1 table = {};
table.NumDescriptorRanges = 1;
table.pDescriptorRanges = &srvRange;
rp[RP_TransformsTable].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
rp[RP_TransformsTable].DescriptorTable = table;
rp[RP_TransformsTable].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
D3D12_VERSIONED_ROOT_SIGNATURE_DESC rsd = {};
rsd.Version = D3D_ROOT_SIGNATURE_VERSION_1_1;
rsd.Desc_1_1.NumParameters = RP_Count;
rsd.Desc_1_1.pParameters = rp;
rsd.Desc_1_1.NumStaticSamplers = 0;
rsd.Desc_1_1.pStaticSamplers = nullptr;
rsd.Desc_1_1.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
ComPtr<ID3DBlob> blob, err;
ThrowIfFailed(D3D12SerializeVersionedRootSignature(&rsd, &blob, &err));
ThrowIfFailed(a.device->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(),
IID_PPV_ARGS(&a.rootSig)));
}
// -------------------------------------------
// (3) Measurement: GPU timestamp setup (2 queries per frame)
// -------------------------------------------
static void CreateTimestamps(App& a)
{
D3D12_QUERY_HEAP_DESC qh = {};
qh.Type = D3D12_QUERY_HEAP_TYPE_TIMESTAMP;
qh.Count = kFrameCount * 2;
ThrowIfFailed(a.device->CreateQueryHeap(&qh, IID_PPV_ARGS(&a.queryHeap)));
// Readback buffer for uint64 timestamps
D3D12_RESOURCE_DESC rb = {};
rb.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
rb.Width = sizeof(UINT64) * qh.Count;
rb.Height = 1;
rb.DepthOrArraySize = 1;
rb.MipLevels = 1;
rb.SampleDesc.Count = 1;
rb.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
D3D12_HEAP_PROPERTIES hp = {};
hp.Type = D3D12_HEAP_TYPE_READBACK;
ThrowIfFailed(a.device->CreateCommittedResource(
&hp, D3D12_HEAP_FLAG_NONE, &rb,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&a.queryReadback)));
D3D12_RANGE r = {0, 0};
ThrowIfFailed(a.queryReadback->Map(0, &r, (void**)&a.queryReadbackMapped));
}
// -------------------------------------------
// (B)(D) Create per-frame resources (CB, upload, default, SRV slots)
// -------------------------------------------
static void CreatePerFrameResources(App& a)
{
a.descInc = a.device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
// Heap: enough SRVs for kFrameCount + (optional) extra
D3D12_DESCRIPTOR_HEAP_DESC hd = {};
hd.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
hd.NumDescriptors = kFrameCount;
hd.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
ThrowIfFailed(a.device->CreateDescriptorHeap(&hd, IID_PPV_ARGS(&a.cbvSrvUavHeap)));
// Allocate per-frame SRV slots deterministically: slot = frameIndex
for (UINT i = 0; i < kFrameCount; ++i)
a.frames[i].srvSlot = i;
// FrameCB (upload heap), TransformsUpload (upload heap), TransformsDefault (default heap)
const UINT64 cbSize = (sizeof(float) * 16 + 255) & ~255ULL; // (CBV alignment teaching hook)
const UINT64 transformsBytes = UINT64(kInstanceCount) * sizeof(float) * 16; // float4x4
for (UINT i = 0; i < kFrameCount; ++i)
{
FrameContext& fc = a.frames[i];
// Allocator
ThrowIfFailed(a.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&fc.alloc)));
// FrameCB upload
{
D3D12_HEAP_PROPERTIES hp = {}; hp.Type = D3D12_HEAP_TYPE_UPLOAD;
D3D12_RESOURCE_DESC rd = {};
rd.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
rd.Width = cbSize;
rd.Height = 1; rd.DepthOrArraySize = 1; rd.MipLevels = 1;
rd.SampleDesc.Count = 1;
rd.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
ThrowIfFailed(a.device->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &rd,
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&fc.frameCB)));
D3D12_RANGE r = {0, 0};
ThrowIfFailed(fc.frameCB->Map(0, &r, &fc.frameCBMapped));
}
// TransformsUpload (upload heap)
{
D3D12_HEAP_PROPERTIES hp = {}; hp.Type = D3D12_HEAP_TYPE_UPLOAD;
D3D12_RESOURCE_DESC rd = {};
rd.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
rd.Width = transformsBytes;
rd.Height = 1; rd.DepthOrArraySize = 1; rd.MipLevels = 1;
rd.SampleDesc.Count = 1;
rd.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
ThrowIfFailed(a.device->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &rd,
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&fc.transformsUpload)));
D3D12_RANGE r = {0, 0};
ThrowIfFailed(fc.transformsUpload->Map(0, &r, &fc.transformsUploadMapped));
}
// TransformsDefault (default heap) - GPU reads as SRV, also copy dest
{
D3D12_HEAP_PROPERTIES hp = {}; hp.Type = D3D12_HEAP_TYPE_DEFAULT;
D3D12_RESOURCE_DESC rd = {};
rd.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
rd.Width = transformsBytes;
rd.Height = 1; rd.DepthOrArraySize = 1; rd.MipLevels = 1;
rd.SampleDesc.Count = 1;
rd.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
ThrowIfFailed(a.device->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &rd,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&fc.transformsDefault)));
}
// Create SRV in this frame's slot, pointing at THIS frame's default buffer.
// (B) Lifetime: we never overwrite descriptors for an in-flight frame (slot per frame).
{
D3D12_SHADER_RESOURCE_VIEW_DESC sd = {};
sd.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
sd.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
sd.Format = DXGI_FORMAT_UNKNOWN;
sd.Buffer.FirstElement = 0;
sd.Buffer.NumElements = kInstanceCount;
sd.Buffer.StructureByteStride = sizeof(float) * 16; // float4x4 = 64 bytes
sd.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_NONE;
auto cpu = CpuHandleAt(a.cbvSrvUavHeap.Get(), fc.srvSlot, a.descInc);
a.device->CreateShaderResourceView(fc.transformsDefault.Get(), &sd, cpu);
}
// Timestamp query indices for this frame
fc.queryBegin = i * 2;
fc.queryEnd = i * 2 + 1;
}
}
// -------------------------------------------
// Update transforms (D): build a deterministic grid, with sentinel proof.
// -------------------------------------------
static void WriteTransforms(FrameContext& fc, bool sentinel)
{
// Row-major float4x4 in memory here; the HLSL mul order must match your convention.
// If your matrices appear transposed, this is the exact seam to investigate.
float* out = (float*)fc.transformsUploadMapped;
const int grid = 100; // 100 x 100 = 10,000
for (int y = 0; y < grid; ++y)
{
for (int x = 0; x < grid; ++x)
{
int i = y * grid + x;
float tx = (float)x * 2.0f;
float ty = 0.0f;
float tz = (float)y * 2.0f;
if (sentinel && i == 0)
{
tx = 9999.0f; tz = 9999.0f; // sentinel: only instance 0 should fly away
}
// Identity with translation (simple)
// [ 1 0 0 0
// 0 1 0 0
// 0 0 1 0
// tx ty tz 1 ] (row-major layout example)
// You must match your shader's mul conventions.
float M[16] = {
1,0,0,0,
0,1,0,0,
0,0,1,0,
tx,ty,tz,1
};
memcpy(out + i * 16, M, sizeof(M));
}
}
}
// -------------------------------------------
// Record draw: this is where (1)(2)(3)(4) meet.
// -------------------------------------------
static void RecordDraw(App& a, FrameContext& fc,
D3D12_GPU_VIRTUAL_ADDRESS frameCBGpuVA,
D3D12_GPU_DESCRIPTOR_HANDLE transformsTableStartGPU,
UINT indexCount, UINT instanceCount)
{
// (3) GPU timestamps: measure GPU execution window of "draw-ish" region
a.cmd->EndQuery(a.queryHeap.Get(), D3D12_QUERY_TYPE_TIMESTAMP, fc.queryBegin);
// (1) Ritual order: PSO -> RootSig -> Heaps -> Root bindings -> Draw
a.cmd->SetPipelineState(a.pso.Get());
a.cmd->SetGraphicsRootSignature(a.rootSig.Get());
if (!a.break_OmitSetHeaps)
{
ID3D12DescriptorHeap* heaps[] = { a.cbvSrvUavHeap.Get() };
a.cmd->SetDescriptorHeaps(1, heaps);
}
// Root bindings:
a.cmd->SetGraphicsRootConstantBufferView(RP_FrameCB, frameCBGpuVA);
// (1) RP-index lever: if you bind the table to the wrong RP, it breaks deterministically.
UINT rpForTable = a.break_RPIndexSwap ? RP_FrameCB : RP_TransformsTable;
a.cmd->SetGraphicsRootDescriptorTable(rpForTable, transformsTableStartGPU);
// Draw (instanced = one call; naive = many calls)
if (a.modeInstanced)
{
a.cmd->DrawIndexedInstanced(indexCount, instanceCount, 0, 0, 0);
}
else
{
for (UINT i = 0; i < instanceCount; ++i)
{
a.cmd->DrawIndexedInstanced(indexCount, 1, 0, 0, i); // <-- StartInstanceLocation = i
}
}
a.cmd->EndQuery(a.queryHeap.Get(), D3D12_QUERY_TYPE_TIMESTAMP, fc.queryEnd);
// Resolve timestamps into readback for this frame
a.cmd->ResolveQueryData(a.queryHeap.Get(), D3D12_QUERY_TYPE_TIMESTAMP,
fc.queryBegin, 2,
a.queryReadback.Get(),
sizeof(UINT64) * fc.queryBegin);
}
// -------------------------------------------
// One frame tick: update per-frame resources, record, submit, present, log.
// -------------------------------------------
static void Tick(App& a)
{
FrameContext& fc = BeginFrame(a);
// CPU record timing (3): what instancing should reduce
LARGE_INTEGER t0, t1;
QueryPerformanceCounter(&t0);
// (D) Update transform data in upload memory
WriteTransforms(fc, a.sentinel_Instance0);
// Copy upload -> default, and transition states so VS can read SRV.
// (B) Lifetime: these resources must remain valid until fence passes.
{
// default: COPY_DEST -> (copy) -> NON_PIXEL_SHADER_RESOURCE
D3D12_RESOURCE_BARRIER b0 = {};
b0.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
b0.Transition.pResource = fc.transformsDefault.Get();
b0.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
b0.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
b0.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST; // stays, but explicit shows intent
a.cmd->ResourceBarrier(1, &b0);
a.cmd->CopyBufferRegion(fc.transformsDefault.Get(), 0, fc.transformsUpload.Get(), 0,
UINT64(kInstanceCount) * sizeof(float) * 16);
D3D12_RESOURCE_BARRIER b1 = {};
b1.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
b1.Transition.pResource = fc.transformsDefault.Get();
b1.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
b1.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
b1.Transition.StateAfter = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
a.cmd->ResourceBarrier(1, &b1);
}
// FrameCB write (upload mapped) – fill ViewProj
// (Keep omitted; the point is: RP0 root CBV points at this GPUVA)
// memcpy(fc.frameCBMapped, &ViewProj, sizeof(ViewProj));
const D3D12_GPU_VIRTUAL_ADDRESS frameCBGpuVA = fc.frameCB->GetGPUVirtualAddress();
// Descriptor table base GPU handle for this frame’s SRV slot
const D3D12_GPU_DESCRIPTOR_HANDLE transformsTableStartGPU =
GpuHandleAt(a.cbvSrvUavHeap.Get(), fc.srvSlot, a.descInc);
// (1)(2)(3)(4) meet here:
// - (1) Binding ABI: RP indices + SRV table base must match shader mailboxes
// - (2) Lifetime: descriptors/resources must survive until fence
// - (3) Measurement: CPU record and GPU timestamps are separate
// - (4) Instance correctness: iid indexes transforms buffer layout
RecordDraw(a, fc, frameCBGpuVA, transformsTableStartGPU,
/*indexCount=*/36, /*instanceCount=*/kInstanceCount);
QueryPerformanceCounter(&t1);
double cpuRecordMs = 1000.0 * double(t1.QuadPart - t0.QuadPart) / double(a.qpcFreq.QuadPart);
// Close/submit + fence
EndFrame(a, fc);
// Present (not shown: transitions backbuffer -> PRESENT etc)
ThrowIfFailed(a.swap->Present(1, 0));
// Read GPU timestamps for this frame (will be meaningful only after fence completes).
// For teaching: read last completed frame’s timestamps to avoid stalling.
UINT prev = (a.backIndex + kFrameCount - 1) % kFrameCount;
FrameContext& prevFc = a.frames[prev];
if (a.fence->GetCompletedValue() >= prevFc.fenceValue && prevFc.fenceValue != 0)
{
UINT64 tBegin = a.queryReadbackMapped[prevFc.queryBegin];
UINT64 tEnd = a.queryReadbackMapped[prevFc.queryEnd];
// Need timestamp frequency to convert ticks -> ms
UINT64 freq = 0;
a.queue->GetTimestampFrequency(&freq);
double gpuMs = (tEnd > tBegin && freq) ? (1000.0 * double(tEnd - tBegin) / double(freq)) : 0.0;
// Log = proof artifact (3)
// mode, drawCalls, cpu_record_ms, gpu_ms, fenceCompleted
UINT drawCalls = a.modeInstanced ? 1u : kInstanceCount;
UINT64 completed = a.fence->GetCompletedValue();
printf("mode=%s draws=%u cpu_record_ms=%.3f gpu_ms=%.3f fence_done=%llu\n",
a.modeInstanced ? "instanced" : "naive",
drawCalls, cpuRecordMs, gpuMs,
(unsigned long long)completed);
}
}
// ---------------------------------------------------------
// Minimal Win32 + DX12 init to make your skeleton runnable.
// ---------------------------------------------------------
static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_DESTROY) { PostQuitMessage(0); return 0; }
return DefWindowProc(hWnd, msg, wParam, lParam);
}
static ComPtr<IDXGIFactory6> CreateFactory(bool enableDebug)
{
UINT flags = 0;
#if defined(_DEBUG)
if (enableDebug) flags |= DXGI_CREATE_FACTORY_DEBUG;
#endif
ComPtr<IDXGIFactory6> f;
ThrowIfFailed(CreateDXGIFactory2(flags, IID_PPV_ARGS(&f)));
return f;
}
static ComPtr<IDXGIAdapter1> PickAdapter(IDXGIFactory6* factory)
{
// Prefer high-performance adapters (DXGI 1.6)
ComPtr<IDXGIAdapter1> best;
SIZE_T bestVram = 0;
for (UINT i = 0;; ++i)
{
ComPtr<IDXGIAdapter1> ad;
if (factory->EnumAdapters1(i, &ad) == DXGI_ERROR_NOT_FOUND) break;
DXGI_ADAPTER_DESC1 desc{};
ad->GetDesc1(&desc);
if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) continue;
if (desc.DedicatedVideoMemory > bestVram)
{
bestVram = desc.DedicatedVideoMemory;
best = ad;
}
}
return best;
}
static void EnableDebugLayer()
{
#if defined(_DEBUG)
ComPtr<ID3D12Debug> dbg;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&dbg))))
{
dbg->EnableDebugLayer();
// If you later want GPU-based validation, you can query ID3D12Debug1 and SetEnableGPUBasedValidation(TRUE).
}
#endif
}
static void CreateDeviceQueueSwapchainRTV(App& a)
{
EnableDebugLayer();
auto factory = CreateFactory(true);
auto adapter = PickAdapter(factory.Get());
ThrowIfFailed(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&a.device)));
// Queue
D3D12_COMMAND_QUEUE_DESC qd{};
qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
qd.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
ThrowIfFailed(a.device->CreateCommandQueue(&qd, IID_PPV_ARGS(&a.queue)));
// Swapchain
DXGI_SWAP_CHAIN_DESC1 sd{};
sd.Width = a.width;
sd.Height = a.height;
sd.Format = a.backbufferFormat;
sd.BufferCount = kFrameCount;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
sd.SampleDesc.Count = 1;
ComPtr<IDXGISwapChain1> sc1;
ThrowIfFailed(factory->CreateSwapChainForHwnd(
a.queue.Get(), a.hwnd, &sd, nullptr, nullptr, &sc1));
ThrowIfFailed(sc1.As(&a.swap));
a.backIndex = a.swap->GetCurrentBackBufferIndex();
// RTV heap + RTVs
a.rtvInc = a.device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
D3D12_DESCRIPTOR_HEAP_DESC rh{};
rh.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
rh.NumDescriptors = kFrameCount;
rh.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
ThrowIfFailed(a.device->CreateDescriptorHeap(&rh, IID_PPV_ARGS(&a.rtvHeap)));
auto rtvStart = a.rtvHeap->GetCPUDescriptorHandleForHeapStart();
for (UINT i = 0; i < kFrameCount; ++i)
{
ThrowIfFailed(a.swap->GetBuffer(i, IID_PPV_ARGS(&a.backBuffers[i])));
a.rtvHandles[i] = rtvStart;
a.device->CreateRenderTargetView(a.backBuffers[i].Get(), nullptr, a.rtvHandles[i]);
rtvStart.ptr += UINT64(a.rtvInc);
}
// Viewport/scissor
a.viewport = { 0.0f, 0.0f, float(a.width), float(a.height), 0.0f, 1.0f };
a.scissor = { 0, 0, (LONG)a.width, (LONG)a.height };
}
static void CreateFenceAndEvent(App& a)
{
ThrowIfFailed(a.device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&a.fence)));
a.fenceCounter = 0;
a.fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (!a.fenceEvent) __debugbreak();
}
static ComPtr<ID3DBlob> Compile(const char* src, const char* entry, const char* target)
{
UINT flags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined(_DEBUG)
flags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
#endif
ComPtr<ID3DBlob> blob, err;
HRESULT hr = D3DCompile(src, strlen(src), nullptr, nullptr, nullptr,
entry, target, flags, 0, &blob, &err);
if (FAILED(hr)) __debugbreak();
return blob;
}
static void CreatePSO(App& a)
{
auto vs = Compile(kHlslVS, "VSMain", "vs_5_1");
auto ps = Compile(kHlslPS, "PSMain", "ps_5_1");
D3D12_INPUT_ELEMENT_DESC il[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,
D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }
};
// Default-ish rasterizer
D3D12_RASTERIZER_DESC rast{};
rast.FillMode = D3D12_FILL_MODE_SOLID;
rast.CullMode = D3D12_CULL_MODE_BACK;
rast.FrontCounterClockwise = FALSE;
rast.DepthBias = D3D12_DEFAULT_DEPTH_BIAS;
rast.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP;
rast.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS;
rast.DepthClipEnable = TRUE;
// Default-ish blend
D3D12_BLEND_DESC blend{};
blend.AlphaToCoverageEnable = FALSE;
blend.IndependentBlendEnable = FALSE;
blend.RenderTarget[0].BlendEnable = FALSE;
blend.RenderTarget[0].LogicOpEnable = FALSE;
blend.RenderTarget[0].SrcBlend = D3D12_BLEND_ONE;
blend.RenderTarget[0].DestBlend = D3D12_BLEND_ZERO;
blend.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD;
blend.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_ONE;
blend.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_ZERO;
blend.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD;
blend.RenderTarget[0].LogicOp = D3D12_LOGIC_OP_NOOP;
blend.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
D3D12_DEPTH_STENCIL_DESC ds{};
ds.DepthEnable = FALSE;
ds.StencilEnable = FALSE;
D3D12_GRAPHICS_PIPELINE_STATE_DESC p{};
p.pRootSignature = a.rootSig.Get(); // ✅ PSO must reference this RootSig
p.VS = { vs->GetBufferPointer(), vs->GetBufferSize() };
p.PS = { ps->GetBufferPointer(), ps->GetBufferSize() };
p.InputLayout = { il, _countof(il) };
p.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
p.RasterizerState = rast;
p.BlendState = blend;
p.DepthStencilState = ds;
p.SampleMask = UINT_MAX;
p.NumRenderTargets = 1;
p.RTVFormats[0] = a.backbufferFormat;
p.SampleDesc.Count = 1;
ThrowIfFailed(a.device->CreateGraphicsPipelineState(&p, IID_PPV_ARGS(&a.pso)));
}
static void OneShotUploadBuffer(
App& a,
ID3D12Resource* dstDefault, UINT64 dstOffset,
const void* srcData, UINT64 numBytes,
D3D12_RESOURCE_STATES afterState)
{
// Create upload buffer
ComPtr<ID3D12Resource> upload;
D3D12_HEAP_PROPERTIES hpU{}; hpU.Type = D3D12_HEAP_TYPE_UPLOAD;
D3D12_RESOURCE_DESC rd{};
rd.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
rd.Width = numBytes;
rd.Height = 1;
rd.DepthOrArraySize = 1;
rd.MipLevels = 1;
rd.SampleDesc.Count = 1;
rd.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
ThrowIfFailed(a.device->CreateCommittedResource(
&hpU, D3D12_HEAP_FLAG_NONE, &rd,
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
IID_PPV_ARGS(&upload)));
// Copy CPU->upload
void* mapped = nullptr;
D3D12_RANGE r{0, 0};
ThrowIfFailed(upload->Map(0, &r, &mapped));
memcpy(mapped, srcData, (size_t)numBytes);
upload->Unmap(0, nullptr);
// Record copy on a temporary cmdlist
ComPtr<ID3D12CommandAllocator> alloc;
ThrowIfFailed(a.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&alloc)));
ComPtr<ID3D12GraphicsCommandList> cl;
ThrowIfFailed(a.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, alloc.Get(), nullptr, IID_PPV_ARGS(&cl)));
cl->CopyBufferRegion(dstDefault, dstOffset, upload.Get(), 0, numBytes);
D3D12_RESOURCE_BARRIER b{};
b.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
b.Transition.pResource = dstDefault;
b.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
b.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
b.Transition.StateAfter = afterState;
cl->ResourceBarrier(1, &b);
ThrowIfFailed(cl->Close());
ID3D12CommandList* lists[] = { cl.Get() };
a.queue->ExecuteCommandLists(1, lists);
// Wait
a.fenceCounter++;
ThrowIfFailed(a.queue->Signal(a.fence.Get(), a.fenceCounter));
WaitForFence(a, a.fenceCounter);
}
static void CreateCubeGeometry(App& a)
{
struct V { float x, y, z; };
// A tiny cube (8 verts) + 36 indices (12 triangles)
const V verts[] = {
{-1,-1,-1},{-1, 1,-1},{ 1, 1,-1},{ 1,-1,-1},
{-1,-1, 1},{-1, 1, 1},{ 1, 1, 1},{ 1,-1, 1},
};
const uint16_t idx[] = {
0,1,2, 0,2,3, // -Z
4,6,5, 4,7,6, // +Z
4,5,1, 4,1,0, // -X
3,2,6, 3,6,7, // +X
1,5,6, 1,6,2, // +Y
4,0,3, 4,3,7 // -Y
};
a.indexCount = (UINT)_countof(idx);
const UINT64 vbBytes = sizeof(verts);
const UINT64 ibBytes = sizeof(idx);
// VB default
{
D3D12_HEAP_PROPERTIES hpD{}; hpD.Type = D3D12_HEAP_TYPE_DEFAULT;
D3D12_RESOURCE_DESC rd{};
rd.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
rd.Width = vbBytes;
rd.Height = 1;
rd.DepthOrArraySize = 1;
rd.MipLevels = 1;
rd.SampleDesc.Count = 1;
rd.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
ThrowIfFailed(a.device->CreateCommittedResource(
&hpD, D3D12_HEAP_FLAG_NONE, &rd,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&a.vbDefault)));
OneShotUploadBuffer(a, a.vbDefault.Get(), 0, verts, vbBytes, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
a.vbv.BufferLocation = a.vbDefault->GetGPUVirtualAddress();
a.vbv.SizeInBytes = (UINT)vbBytes;
a.vbv.StrideInBytes = sizeof(V);
}
// IB default
{
D3D12_HEAP_PROPERTIES hpD{}; hpD.Type = D3D12_HEAP_TYPE_DEFAULT;
D3D12_RESOURCE_DESC rd{};
rd.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
rd.Width = ibBytes;
rd.Height = 1;
rd.DepthOrArraySize = 1;
rd.MipLevels = 1;
rd.SampleDesc.Count = 1;
rd.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
ThrowIfFailed(a.device->CreateCommittedResource(
&hpD, D3D12_HEAP_FLAG_NONE, &rd,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&a.ibDefault)));
OneShotUploadBuffer(a, a.ibDefault.Get(), 0, idx, ibBytes, D3D12_RESOURCE_STATE_INDEX_BUFFER);
a.ibv.BufferLocation = a.ibDefault->GetGPUVirtualAddress();
a.ibv.SizeInBytes = (UINT)ibBytes;
a.ibv.Format = DXGI_FORMAT_R16_UINT;
}
}
static void CreateMainCommandList(App& a)
{
// Create one command list object; reset it per-frame using per-frame allocators.
// Needs at least one allocator to start with.
ThrowIfFailed(a.device->CreateCommandList(
0, D3D12_COMMAND_LIST_TYPE_DIRECT,
a.frames[0].alloc.Get(), a.pso.Get(),
IID_PPV_ARGS(&a.cmd)));
ThrowIfFailed(a.cmd->Close()); // start closed
}
static void InitApp(App& a, HWND hwnd, UINT w, UINT h)
{
a.hwnd = hwnd;
a.width = w;
a.height = h;
QueryPerformanceFrequency(&a.qpcFreq);
// Create swapchain/device/rtv first
CreateDeviceQueueSwapchainRTV(a);
CreateFenceAndEvent(a);
// Create RootSig + PSO
CreateRootSig(a);
CreatePSO(a);
// Create per-frame resources (allocators/CB/transforms/srv slots)
CreatePerFrameResources(a);
// Now that allocators exist, create the main command list object
CreateMainCommandList(a);
// Timestamp heap/readback
CreateTimestamps(a);
// Geometry
CreateCubeGeometry(a);
}
// ---------------------------------------------------------
// You must patch Tick() slightly to bind RTV + IA + barriers.
// (This is not "main omitted", but without it you'll draw nothing.)
// ---------------------------------------------------------
static void Tick_Patched(App& a)
{
FrameContext& fc = BeginFrame(a);
LARGE_INTEGER t0, t1;
QueryPerformanceCounter(&t0);
// Update transforms (upload mapped)
WriteTransforms(fc, a.sentinel_Instance0);
// Backbuffer: PRESENT -> RENDER_TARGET
auto* bb = a.backBuffers[a.backIndex].Get();
{
D3D12_RESOURCE_BARRIER b{};
b.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
b.Transition.pResource = bb;
b.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
b.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
b.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
a.cmd->ResourceBarrier(1, &b);
}
// Bind RTV + clear
auto rtv = a.rtvHandles[a.backIndex];
a.cmd->OMSetRenderTargets(1, &rtv, FALSE, nullptr);
const float clear[4] = { 0.05f, 0.07f, 0.10f, 1.0f };
a.cmd->ClearRenderTargetView(rtv, clear, 0, nullptr);
// IA / viewport
a.cmd->RSSetViewports(1, &a.viewport);
a.cmd->RSSetScissorRects(1, &a.scissor);
a.cmd->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
a.cmd->IASetVertexBuffers(0, 1, &a.vbv);
a.cmd->IASetIndexBuffer(&a.ibv);
// Copy upload -> default for transforms (FIXED barrier pattern)
{
// If you're updating every frame, you must go SRV -> COPY_DEST first.
D3D12_RESOURCE_BARRIER b0{};
b0.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
b0.Transition.pResource = fc.transformsDefault.Get();
b0.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
b0.Transition.StateBefore = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
b0.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
a.cmd->ResourceBarrier(1, &b0);
a.cmd->CopyBufferRegion(fc.transformsDefault.Get(), 0, fc.transformsUpload.Get(), 0,
UINT64(kInstanceCount) * sizeof(float) * 16);
D3D12_RESOURCE_BARRIER b1{};
b1.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
b1.Transition.pResource = fc.transformsDefault.Get();
b1.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
b1.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
b1.Transition.StateAfter = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
a.cmd->ResourceBarrier(1, &b1);
}
const D3D12_GPU_VIRTUAL_ADDRESS frameCBGpuVA = fc.frameCB->GetGPUVirtualAddress();
const D3D12_GPU_DESCRIPTOR_HANDLE transformsTableStartGPU =
GpuHandleAt(a.cbvSrvUavHeap.Get(), fc.srvSlot, a.descInc);
RecordDraw(a, fc, frameCBGpuVA, transformsTableStartGPU,
/*indexCount=*/a.indexCount, /*instanceCount=*/kInstanceCount);
// Backbuffer: RENDER_TARGET -> PRESENT