-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathd3d11-window.cpp
888 lines (749 loc) · 24.3 KB
/
d3d11-window.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
#include "framework.h"
// Include shader binaries generated by build
#include "fullscreen_vs.h"
#include "rect_vs.h"
#include "copy_ps.h"
#include "lines_vs.h"
#include "lines_ps.h"
namespace Framework
{
static const int s_lineVerticesPerDraw = 1024;
static LRESULT CALLBACK StaticMsgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
D3D11Window::D3D11Window()
: m_hInstance(nullptr),
m_hWnd(nullptr),
m_dims(0),
m_hasDepthBuffer(true)
{
}
void D3D11Window::Init(
const char * windowClassName,
const char * windowTitle,
HINSTANCE hInstance)
{
LOG("Initialization started");
m_hInstance = hInstance;
// Register window class
WNDCLASS wc =
{
0,
&StaticMsgProc,
0,
0,
hInstance,
LoadIcon(nullptr, IDI_APPLICATION),
LoadCursor(nullptr, IDC_ARROW),
nullptr,
nullptr,
windowClassName,
};
CHECK_ERR(RegisterClass(&wc));
// Create the window
m_hWnd = CreateWindow(
windowClassName,
windowTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
nullptr,
nullptr,
hInstance,
this);
ASSERT_ERR(m_hWnd);
// Take a look at the adaptors and outputs on the system, just for kicks
comptr<IDXGIFactory> pFactory;
CHECK_D3D(CreateDXGIFactory(__uuidof(IDXGIFactory), (void **)&pFactory));
for (int iAdapter = 0; ; ++iAdapter)
{
comptr<IDXGIAdapter> pAdapter;
HRESULT hr = pFactory->EnumAdapters(iAdapter, &pAdapter);
if (hr == DXGI_ERROR_NOT_FOUND)
break;
ASSERT_ERR(SUCCEEDED(hr));
DXGI_ADAPTER_DESC adapterDesc;
CHECK_D3D(pAdapter->GetDesc(&adapterDesc));
if (adapterDesc.DedicatedVideoMemory > 0)
LOG("Adapter %d: %ls (%dMB VRAM)", iAdapter, adapterDesc.Description, adapterDesc.DedicatedVideoMemory / 1048576);
else
LOG("Adapter %d: %ls (%dMB shared RAM)", iAdapter, adapterDesc.Description, adapterDesc.SharedSystemMemory / 1048576);
int iOutput = 0;
for (; ; ++iOutput)
{
comptr<IDXGIOutput> pOutput;
hr = pAdapter->EnumOutputs(iOutput, &pOutput);
if (hr == DXGI_ERROR_NOT_FOUND)
break;
ASSERT_ERR(SUCCEEDED(hr));
DXGI_OUTPUT_DESC outputDesc;
CHECK_D3D(pOutput->GetDesc(&outputDesc));
DISPLAY_DEVICEW displayDevice = { sizeof(displayDevice), };
CHECK_ERR(EnumDisplayDevicesW(outputDesc.DeviceName, 0, &displayDevice, 0));
LOG(" Output %d: %ls (%dx%d)",
iOutput, displayDevice.DeviceString,
outputDesc.DesktopCoordinates.right - outputDesc.DesktopCoordinates.left,
outputDesc.DesktopCoordinates.bottom - outputDesc.DesktopCoordinates.top);
}
if (iOutput == 0)
LOG(" No outputs");
}
// Initialize D3D11
UINT flags = 0;
#ifdef _DEBUG
flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
DXGI_SWAP_CHAIN_DESC swapChainDesc =
{
{ 1, 1, {}, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, },
{ 1, 0, },
DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_BACK_BUFFER,
2,
m_hWnd,
true,
DXGI_SWAP_EFFECT_DISCARD,
};
D3D_FEATURE_LEVEL featureLevel;
CHECK_D3D(D3D11CreateDeviceAndSwapChain(
nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr,
flags, nullptr, 0, D3D11_SDK_VERSION,
&swapChainDesc,
&m_pSwapChain, &m_pDevice,
&featureLevel, &m_pCtx));
#ifdef _DEBUG
// Set up D3D11 debug layer settings
comptr<ID3D11InfoQueue> pInfoQueue;
if (SUCCEEDED(m_pDevice->QueryInterface<ID3D11InfoQueue>(&pInfoQueue)))
{
// Break in the debugger when an error or warning is issued
pInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true);
pInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true);
pInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_WARNING, true);
// Disable warning about setting private data (i.e. debug names of resources)
D3D11_MESSAGE_ID aMsgToFilter[] =
{
D3D11_MESSAGE_ID_SETPRIVATEDATA_CHANGINGPARAMS,
};
D3D11_INFO_QUEUE_FILTER filter = {};
filter.DenyList.NumIDs = dim(aMsgToFilter);
filter.DenyList.pIDList = aMsgToFilter;
pInfoQueue->AddStorageFilterEntries(&filter);
}
#endif
// Set up commonly used state blocks
D3D11_RASTERIZER_DESC rssDesc =
{
D3D11_FILL_SOLID,
D3D11_CULL_BACK,
true, // FrontCounterClockwise
0, 0, 0, // depth bias
true, // DepthClipEnable
false, // ScissorEnable
true, // MultisampleEnable
};
CHECK_D3D(m_pDevice->CreateRasterizerState(&rssDesc, &m_pRsDefault));
rssDesc.CullMode = D3D11_CULL_NONE;
CHECK_D3D(m_pDevice->CreateRasterizerState(&rssDesc, &m_pRsDoubleSided));
D3D11_DEPTH_STENCIL_DESC dssDesc =
{
true, // DepthEnable
D3D11_DEPTH_WRITE_MASK_ALL,
D3D11_COMPARISON_LESS_EQUAL,
};
CHECK_D3D(m_pDevice->CreateDepthStencilState(&dssDesc, &m_pDssDepthTest));
dssDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
CHECK_D3D(m_pDevice->CreateDepthStencilState(&dssDesc, &m_pDssNoDepthWrite));
dssDesc.DepthEnable = false;
CHECK_D3D(m_pDevice->CreateDepthStencilState(&dssDesc, &m_pDssNoDepthTest));
D3D11_BLEND_DESC bsAdditiveDesc =
{
false, false,
{
true,
D3D11_BLEND_ONE,
D3D11_BLEND_ONE,
D3D11_BLEND_OP_ADD,
D3D11_BLEND_ONE,
D3D11_BLEND_ONE,
D3D11_BLEND_OP_ADD,
D3D11_COLOR_WRITE_ENABLE_ALL,
},
};
CHECK_D3D(m_pDevice->CreateBlendState(&bsAdditiveDesc, &m_pBsAdditive));
D3D11_BLEND_DESC bsAlphaBlendDesc =
{
false, false,
{
true,
D3D11_BLEND_SRC_ALPHA,
D3D11_BLEND_INV_SRC_ALPHA,
D3D11_BLEND_OP_ADD,
D3D11_BLEND_SRC_ALPHA,
D3D11_BLEND_INV_SRC_ALPHA,
D3D11_BLEND_OP_ADD,
D3D11_COLOR_WRITE_ENABLE_ALL,
},
};
CHECK_D3D(m_pDevice->CreateBlendState(&bsAlphaBlendDesc, &m_pBsAlphaBlend));
// Set up commonly used samplers
D3D11_SAMPLER_DESC sampDesc =
{
D3D11_FILTER_MIN_MAG_MIP_POINT,
D3D11_TEXTURE_ADDRESS_CLAMP,
D3D11_TEXTURE_ADDRESS_CLAMP,
D3D11_TEXTURE_ADDRESS_CLAMP,
0.0f,
1,
D3D11_COMPARISON_FUNC(0),
{ 0.0f, 0.0f, 0.0f, 0.0f },
0.0f,
FLT_MAX,
};
CHECK_D3D(m_pDevice->CreateSamplerState(&sampDesc, &m_pSsPointClamp));
sampDesc.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
CHECK_D3D(m_pDevice->CreateSamplerState(&sampDesc, &m_pSsBilinearClamp));
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
CHECK_D3D(m_pDevice->CreateSamplerState(&sampDesc, &m_pSsTrilinearRepeat));
sampDesc.Filter = D3D11_FILTER_ANISOTROPIC;
sampDesc.MaxAnisotropy = 16;
CHECK_D3D(m_pDevice->CreateSamplerState(&sampDesc, &m_pSsTrilinearRepeatAniso));
// PCF shadow comparison filter, with border color set to 1.0 so areas outside
// the shadow map will be unshadowed
sampDesc.Filter = D3D11_FILTER_COMPARISON_ANISOTROPIC;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_BORDER;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_BORDER;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_BORDER;
sampDesc.MaxAnisotropy = 16;
sampDesc.ComparisonFunc = D3D11_COMPARISON_LESS_EQUAL;
sampDesc.BorderColor[0] = 1.0f;
sampDesc.BorderColor[1] = 1.0f;
sampDesc.BorderColor[2] = 1.0f;
sampDesc.BorderColor[3] = 1.0f;
CHECK_D3D(m_pDevice->CreateSamplerState(&sampDesc, &m_pSsPCF));
// Set up commonly used shaders
CHECK_D3D(m_pDevice->CreateVertexShader(fullscreen_vs_bytecode, dim(fullscreen_vs_bytecode), nullptr, &m_pVsFullscreen));
CHECK_D3D(m_pDevice->CreateVertexShader(rect_vs_bytecode, dim(rect_vs_bytecode), nullptr, &m_pVsRect));
CHECK_D3D(m_pDevice->CreatePixelShader(copy_ps_bytecode, dim(copy_ps_bytecode), nullptr, &m_pPsCopy));
CHECK_D3D(m_pDevice->CreateVertexShader(lines_vs_bytecode, dim(lines_vs_bytecode), nullptr, &m_pVsLines));
CHECK_D3D(m_pDevice->CreatePixelShader(lines_ps_bytecode, dim(lines_ps_bytecode), nullptr, &m_pPsLines));
// Init CB for blits and fullscreen passes
m_cbBlit.Init(m_pDevice);
// Init vertex buffer for debug lines
D3D11_BUFFER_DESC bufDesc =
{
s_lineVerticesPerDraw * sizeof(LineVertex),
D3D11_USAGE_DYNAMIC,
D3D11_BIND_VERTEX_BUFFER,
D3D11_CPU_ACCESS_WRITE,
};
CHECK_D3D(m_pDevice->CreateBuffer(&bufDesc, nullptr, &m_pBufLineVertices));
// Init input layout for debug lines
D3D11_INPUT_ELEMENT_DESC aInputDescs[] =
{
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, UINT(offsetof(LineVertex, m_rgba)), D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "SV_Position", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, UINT(offsetof(LineVertex, m_posClip)), D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
CHECK_D3D(m_pDevice->CreateInputLayout(
aInputDescs, dim(aInputDescs),
lines_vs_bytecode, dim(lines_vs_bytecode),
&m_pInputLayoutLines));
}
void D3D11Window::Shutdown()
{
LOG("Shutting down");
if (m_pSwapChain)
{
CHECK_D3D(m_pSwapChain->SetFullscreenState(false, nullptr));
}
m_pCtx.release();
m_pTexBackBuffer.release();
m_pRtvSRGB.release();
m_pRtvRaw.release();
m_pTexDepth.release();
m_pDsv.release();
m_pSrvDepth.release();
m_pRsDefault.release();
m_pRsDoubleSided.release();
m_pDssDepthTest.release();
m_pDssNoDepthWrite.release();
m_pDssNoDepthTest.release();
m_pBsAdditive.release();
m_pBsAlphaBlend.release();
m_pSsPointClamp.release();
m_pSsBilinearClamp.release();
m_pSsTrilinearRepeat.release();
m_pSsTrilinearRepeatAniso.release();
m_pSsPCF.release();
m_pVsFullscreen.release();
m_pVsRect.release();
m_pPsCopy.release();
m_cbBlit.Reset();
m_pBufLineVertices.release();
m_pInputLayoutLines.release();
m_pVsLines.release();
m_pPsLines.release();
// Note that all D3D objects must be released before the device and swap chain,
// or it's possible to end up with dangling objects.
m_pDevice.release();
m_pSwapChain.release();
if (m_hWnd)
{
DestroyWindow(m_hWnd);
m_hWnd = nullptr;
}
}
void D3D11Window::MainLoop(int nShowCmd)
{
// Show the window. This sends the initial WM_SIZE message which results in
// calling OnRender(); we don't want to do this until all initialization
// (including subclass init) is done, so it's here instead of in Init().
ShowWindow(m_hWnd, nShowCmd);
LOG("Main loop started");
MSG msg;
for (;;)
{
// Handle any messages
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Quit if the window has been destroyed
if (!m_hWnd)
break;
// Render a new frame
OnRender();
}
}
static LRESULT CALLBACK StaticMsgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
D3D11Window * pWindow;
if (message == WM_CREATE)
{
// On creation, stash pointer to the D3D11Window object in the window data
CREATESTRUCT * pCreate = (CREATESTRUCT *)lParam;
pWindow = (D3D11Window *)pCreate->lpCreateParams;
SetWindowLongPtr(hWnd, GWLP_USERDATA, LONG_PTR(pWindow));
}
else
{
// Retrieve the D3D11Window object from the window data
pWindow = (D3D11Window *)GetWindowLongPtr(hWnd, GWLP_USERDATA);
// If it's not there yet (due to messages prior to WM_CREATE),
// just fall back to DefWindowProc
if (!pWindow)
return DefWindowProc(hWnd, message, wParam, lParam);
}
// Pass the message to the D3D11Window object
return pWindow->MsgProc(hWnd, message, wParam, lParam);
}
LRESULT D3D11Window::MsgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CLOSE:
Shutdown();
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_SIZE:
{
int2 dimsNew = { LOWORD(lParam), HIWORD(lParam) };
if (all(dimsNew > 0) && any(dimsNew != m_dims))
{
OnResize(dimsNew);
// Note: not calling OnRender here because it causes problems with startup/shutdown,
// during which we may get WM_SIZE messages while we're in a partially-inited state.
}
return 0;
}
case WM_SIZING:
{
RECT * pRectNew = (RECT *)lParam;
int2 dimsNew = { pRectNew->right - pRectNew->left, pRectNew->bottom - pRectNew->top };
// The rect contains the whole window, including borders and title bar, so calculate the delta
// between that and the client area.
RECT rectEmpty = {};
AdjustWindowRect(&rectEmpty, GetWindowLong(m_hWnd, GWL_STYLE), (GetMenu(m_hWnd) != nullptr));
int2 deltaClientToWindow = { rectEmpty.right - rectEmpty.left, rectEmpty.bottom - rectEmpty.top };
dimsNew -= deltaClientToWindow;
if (all(dimsNew > 0) && any(dimsNew != m_dims))
{
OnResize(dimsNew);
OnRender();
}
return 0;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
void D3D11Window::OnResize(int2 dimsNew)
{
LOG("Resizing swap chain to %d x %d", dimsNew.x, dimsNew.y);
m_dims = dimsNew;
// Have to release old render target views before swap chain can be resized
m_pTexBackBuffer.release();
m_pRtvSRGB.release();
m_pRtvRaw.release();
m_pTexDepth.release();
m_pDsv.release();
m_pSrvDepth.release();
// Resize the swap chain to fit the window again
ASSERT_ERR(m_pSwapChain);
CHECK_D3D(m_pSwapChain->ResizeBuffers(0, dimsNew.x, dimsNew.y, DXGI_FORMAT_UNKNOWN, 0));
{
// Retrieve the back buffer
CHECK_D3D(m_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void **)&m_pTexBackBuffer));
// Create render target views in sRGB and raw formats
D3D11_RENDER_TARGET_VIEW_DESC rtvDesc =
{
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
D3D11_RTV_DIMENSION_TEXTURE2D,
};
CHECK_D3D(m_pDevice->CreateRenderTargetView(m_pTexBackBuffer, &rtvDesc, &m_pRtvSRGB));
rtvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
CHECK_D3D(m_pDevice->CreateRenderTargetView(m_pTexBackBuffer, &rtvDesc, &m_pRtvRaw));
}
if (m_hasDepthBuffer)
{
// Create depth buffer and its views
D3D11_TEXTURE2D_DESC texDesc =
{
UINT(dimsNew.x), UINT(dimsNew.y), 1, 1,
DXGI_FORMAT_R32_TYPELESS,
{ 1, 0 },
D3D11_USAGE_DEFAULT,
D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE,
};
CHECK_D3D(m_pDevice->CreateTexture2D(&texDesc, nullptr, &m_pTexDepth));
D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc =
{
DXGI_FORMAT_D32_FLOAT,
D3D11_DSV_DIMENSION_TEXTURE2D,
};
CHECK_D3D(m_pDevice->CreateDepthStencilView(m_pTexDepth, &dsvDesc, &m_pDsv));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc =
{
DXGI_FORMAT_R32_FLOAT,
D3D11_SRV_DIMENSION_TEXTURE2D,
};
srvDesc.Texture2D.MipLevels = 1;
CHECK_D3D(m_pDevice->CreateShaderResourceView(m_pTexDepth, &srvDesc, &m_pSrvDepth));
}
}
// Utility methods
void D3D11Window::BindSRGBBackBuffer(ID3D11DeviceContext * pCtx)
{
pCtx->OMSetRenderTargets(1, &m_pRtvSRGB, m_pDsv);
D3D11_VIEWPORT viewport = { 0.0f, 0.0f, float(m_dims.x), float(m_dims.y), 0.0f, 1.0f, };
pCtx->RSSetViewports(1, &viewport);
}
void D3D11Window::BindRawBackBuffer(ID3D11DeviceContext * pCtx)
{
pCtx->OMSetRenderTargets(1, &m_pRtvRaw, m_pDsv);
D3D11_VIEWPORT viewport = { 0.0f, 0.0f, float(m_dims.x), float(m_dims.y), 0.0f, 1.0f, };
pCtx->RSSetViewports(1, &viewport);
}
void D3D11Window::SetViewport(ID3D11DeviceContext * pCtx, int2 dims)
{
D3D11_VIEWPORT vp =
{
0.0f, 0.0f,
float(dims.x), float(dims.y),
0.0f, 1.0f,
};
pCtx->RSSetViewports(1, &vp);
}
void D3D11Window::SetViewport(ID3D11DeviceContext * pCtx, box2 viewport)
{
D3D11_VIEWPORT vp =
{
viewport.mins.x, viewport.mins.y,
viewport.maxs.x - viewport.mins.x, viewport.maxs.y - viewport.mins.y,
0.0f, 1.0f,
};
pCtx->RSSetViewports(1, &vp);
}
void D3D11Window::SetViewport(ID3D11DeviceContext * pCtx, box3 viewport)
{
D3D11_VIEWPORT vp =
{
viewport.mins.x, viewport.mins.y,
viewport.maxs.x - viewport.mins.x, viewport.maxs.y - viewport.mins.y,
viewport.mins.z, viewport.maxs.z,
};
pCtx->RSSetViewports(1, &vp);
}
void D3D11Window::DrawFullscreenPass(
ID3D11DeviceContext * pCtx,
box2 boxSrc /*= { 0, 0, 1, 1 }*/)
{
CBBlit cbBlit =
{
boxSrc,
{ 0, 0, 1, 1 },
};
m_cbBlit.Update(pCtx, &cbBlit);
pCtx->IASetInputLayout(nullptr);
pCtx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
pCtx->VSSetShader(m_pVsFullscreen, nullptr, 0);
pCtx->VSSetConstantBuffers(0, 1, &m_cbBlit.m_pBuf);
pCtx->Draw(3, 0);
}
void D3D11Window::DrawRectPass(
ID3D11DeviceContext * pCtx,
box2 boxSrc,
box2 boxDst)
{
CBBlit cbBlit =
{
boxSrc,
boxDst,
};
m_cbBlit.Update(pCtx, &cbBlit);
pCtx->IASetInputLayout(nullptr);
pCtx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
pCtx->VSSetShader(m_pVsRect, nullptr, 0);
pCtx->VSSetConstantBuffers(0, 1, &m_cbBlit.m_pBuf);
pCtx->Draw(6, 0);
}
void D3D11Window::BlitFullscreen(
ID3D11DeviceContext * pCtx,
ID3D11ShaderResourceView * pSrvSrc,
ID3D11SamplerState * pSampSrc,
box2 boxSrc /*= { 0, 0, 1, 1 }*/)
{
CBBlit cbBlit =
{
boxSrc,
{ 0, 0, 1, 1 },
};
m_cbBlit.Update(pCtx, &cbBlit);
pCtx->IASetInputLayout(nullptr);
pCtx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
pCtx->VSSetShader(m_pVsFullscreen, nullptr, 0);
pCtx->VSSetConstantBuffers(0, 1, &m_cbBlit.m_pBuf);
pCtx->PSSetShader(m_pPsCopy, nullptr, 0);
pCtx->PSSetShaderResources(0, 1, &pSrvSrc);
pCtx->PSSetSamplers(0, 1, &pSampSrc);
pCtx->Draw(3, 0);
}
void D3D11Window::Blit(
ID3D11DeviceContext * pCtx,
ID3D11ShaderResourceView * pSrvSrc,
ID3D11SamplerState * pSampSrc,
box2 boxSrc,
box2 boxDst)
{
CBBlit cbBlit =
{
boxSrc,
boxDst,
};
m_cbBlit.Update(pCtx, &cbBlit);
pCtx->IASetInputLayout(nullptr);
pCtx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
pCtx->VSSetShader(m_pVsRect, nullptr, 0);
pCtx->VSSetConstantBuffers(0, 1, &m_cbBlit.m_pBuf);
pCtx->PSSetShader(m_pPsCopy, nullptr, 0);
pCtx->PSSetShaderResources(0, 1, &pSrvSrc);
pCtx->PSSetSamplers(0, 1, &pSampSrc);
pCtx->Draw(6, 0);
}
// Methods for debug lines
void D3D11Window::AddDebugLine(float2 p0, float2 p1, rgba rgba)
{
LineVertex verts[2] =
{
{ rgba, { p0.x, p0.y, 0.0f, 1.0f }, },
{ rgba, { p1.x, p1.y, 0.0f, 1.0f }, },
};
m_lineVertices.insert(m_lineVertices.end(), &verts[0], &verts[dim(verts)]);
}
void D3D11Window::AddDebugLine(float2 p0, float2 p1, rgba rgba, affine2 const & xfm)
{
LineVertex verts[2] =
{
{ rgba, float4(xfmPoint(p0, xfm), 0.0f, 1.0f), },
{ rgba, float4(xfmPoint(p1, xfm), 0.0f, 1.0f), },
};
m_lineVertices.insert(m_lineVertices.end(), &verts[0], &verts[dim(verts)]);
}
void D3D11Window::AddDebugLine(float4 p0, float4 p1, rgba rgba)
{
LineVertex verts[2] =
{
{ rgba, p0, },
{ rgba, p1, },
};
m_lineVertices.insert(m_lineVertices.end(), &verts[0], &verts[dim(verts)]);
}
void D3D11Window::AddDebugLine(float4 p0, float4 p1, rgba rgba, float4x4 const & xfm)
{
LineVertex verts[2] =
{
{ rgba, p0 * xfm, },
{ rgba, p1 * xfm, },
};
m_lineVertices.insert(m_lineVertices.end(), &verts[0], &verts[dim(verts)]);
}
void D3D11Window::AddDebugLineStrip(const float2 * pPoints, int numPoints, rgba rgba)
{
if (numPoints < 2)
return;
ASSERT_ERR(pPoints);
int base = int(m_lineVertices.size());
m_lineVertices.resize(base + 2 * numPoints - 2);
const float2 * pPoint = pPoints;
LineVertex * pVtx = &m_lineVertices[base];
// Store the first vertex
pVtx->m_rgba = rgba;
pVtx->m_posClip = { pPoint->x, pPoint->y, 0.0f, 1.0f };
++pPoint;
++pVtx;
// Store the middle vertices, repeating twice each
for (int i = 1; i < numPoints - 1; ++i)
{
pVtx->m_rgba = rgba;
pVtx->m_posClip = { pPoint->x, pPoint->y, 0.0f, 1.0f };
pVtx[1] = pVtx[0];
++pPoint;
pVtx += 2;
}
// Store the last vertex
pVtx->m_rgba = rgba;
pVtx->m_posClip = { pPoint->x, pPoint->y, 0.0f, 1.0f };
++pPoint;
++pVtx;
ASSERT_ERR(pPoint == pPoints + numPoints);
ASSERT_ERR(pVtx == &m_lineVertices[0] + m_lineVertices.size());
}
void D3D11Window::AddDebugLineStrip(const float2 * pPoints, int numPoints, rgba rgba, affine2 const & xfm)
{
if (numPoints < 2)
return;
ASSERT_ERR(pPoints);
int base = int(m_lineVertices.size());
m_lineVertices.resize(base + 2 * numPoints - 2);
const float2 * pPoint = pPoints;
LineVertex * pVtx = &m_lineVertices[base];
// Store the first vertex
pVtx->m_rgba = rgba;
pVtx->m_posClip = float4(xfmPoint(*pPoint, xfm), 0.0f, 1.0f);
++pPoint;
++pVtx;
// Store the middle vertices, repeating twice each
for (int i = 1; i < numPoints - 1; ++i)
{
pVtx->m_rgba = rgba;
pVtx->m_posClip = float4(xfmPoint(*pPoint, xfm), 0.0f, 1.0f);
pVtx[1] = pVtx[0];
++pPoint;
pVtx += 2;
}
// Store the last vertex
pVtx->m_rgba = rgba;
pVtx->m_posClip = float4(xfmPoint(*pPoint, xfm), 0.0f, 1.0f);
++pPoint;
++pVtx;
ASSERT_ERR(pPoint == pPoints + numPoints);
ASSERT_ERR(pVtx == &m_lineVertices[0] + m_lineVertices.size());
}
void D3D11Window::AddDebugLineStrip(const float4 * pPoints, int numPoints, rgba rgba)
{
if (numPoints < 2)
return;
ASSERT_ERR(pPoints);
int base = int(m_lineVertices.size());
m_lineVertices.resize(base + 2 * numPoints - 2);
const float4 * pPoint = pPoints;
LineVertex * pVtx = &m_lineVertices[base];
// Store the first vertex
pVtx->m_rgba = rgba;
pVtx->m_posClip = *pPoint;
++pPoint;
++pVtx;
// Store the middle vertices, repeating twice each
for (int i = 1; i < numPoints - 1; ++i)
{
pVtx->m_rgba = rgba;
pVtx->m_posClip = *pPoint;
pVtx[1] = pVtx[0];
++pPoint;
pVtx += 2;
}
// Store the last vertex
pVtx->m_rgba = rgba;
pVtx->m_posClip = *pPoint;
++pPoint;
++pVtx;
ASSERT_ERR(pPoint == pPoints + numPoints);
ASSERT_ERR(pVtx == &m_lineVertices[0] + m_lineVertices.size());
}
void D3D11Window::AddDebugLineStrip(const float4 * pPoints, int numPoints, rgba rgba, float4x4 const & xfm)
{
if (numPoints < 2)
return;
ASSERT_ERR(pPoints);
int base = int(m_lineVertices.size());
m_lineVertices.resize(base + 2 * numPoints - 2);
const float4 * pPoint = pPoints;
LineVertex * pVtx = &m_lineVertices[base];
// Store the first vertex
pVtx->m_rgba = rgba;
pVtx->m_posClip = *pPoint * xfm;
++pPoint;
++pVtx;
// Store the middle vertices, repeating twice each
for (int i = 1; i < numPoints - 1; ++i)
{
pVtx->m_rgba = rgba;
pVtx->m_posClip = *pPoint * xfm;
pVtx[1] = pVtx[0];
++pPoint;
pVtx += 2;
}
// Store the last vertex
pVtx->m_rgba = rgba;
pVtx->m_posClip = *pPoint * xfm;
++pPoint;
++pVtx;
ASSERT_ERR(pPoint == pPoints + numPoints);
ASSERT_ERR(pVtx == &m_lineVertices[0] + m_lineVertices.size());
}
void D3D11Window::DrawDebugLines(ID3D11DeviceContext * pCtx)
{
// Batch up into draw calls based on how many vertices the buffer can hold
if (m_lineVertices.empty())
return;
pCtx->IASetInputLayout(m_pInputLayoutLines);
pCtx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
UINT stride = sizeof(LineVertex), zero = 0;
pCtx->IASetVertexBuffers(0, 1, &m_pBufLineVertices, &stride, &zero);
pCtx->IASetIndexBuffer(nullptr, DXGI_FORMAT_UNKNOWN, 0);
pCtx->VSSetShader(m_pVsLines, nullptr, 0);
pCtx->PSSetShader(m_pPsLines, nullptr, 0);
int numVerts = int(m_lineVertices.size());
for (int baseVert = 0; baseVert < numVerts; baseVert += s_lineVerticesPerDraw)
{
int numVertsThisDraw = min(numVerts - baseVert, s_lineVerticesPerDraw);
// Update the vertex buffer
D3D11_MAPPED_SUBRESOURCE mapped = {};
CHECK_D3D_WARN(m_pCtx->Map(m_pBufLineVertices, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped));
memcpy(mapped.pData, &m_lineVertices[baseVert], numVertsThisDraw * sizeof(LineVertex));
m_pCtx->Unmap(m_pBufLineVertices, 0);
m_pCtx->Draw(numVertsThisDraw, 0);
}
m_lineVertices.clear();
}
}
// Magic incantation to make the app run on the NV GPU on Optimus laptops
extern "C"
{
_declspec(dllexport) DWORD NvOptimusEnablement = 1;
}