forked from GamebabyRockSun/GRSD3D12Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path27-IBL-With-Material-Texture.cpp
5819 lines (4855 loc) · 276 KB
/
27-IBL-With-Material-Texture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <SDKDDKVer.h>
#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include <windows.h>
#include <tchar.h>
#include <commdlg.h>
#include <wrl.h> //添加WTL支持 方便使用COM
#include <strsafe.h>
#include <atlbase.h>
#include <atlcoll.h>
#include <atlchecked.h>
#include <atlstr.h>
#include <atlconv.h>
#include <atlexcept.h>
#include <DirectXMath.h>
#include <dxgi1_6.h>
#include <d3d12.h> //for d3d12
#include <d3dcompiler.h>
#if defined(_DEBUG)
#include <dxgidebug.h>
#endif
using namespace DirectX;
using namespace Microsoft;
using namespace Microsoft::WRL;
#include "../Commons/GRS_Def.h"
#include "../Commons/GRS_Mem.h"
#include "../Commons/GRS_D3D12_Utility.h"
#include "../Commons/GRS_Texture_Loader.h"
#include "../Commons/CGRSD3DCompilerInclude.h"
#include "../Commons/GRS_Mesh_Load_Txt.h"
#include "../Commons/GRS_Assimp_Loader.h"
#define STB_IMAGE_IMPLEMENTATION
#include "../stb/stb_image.h" // for HDR Image File
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3d12.lib")
#pragma comment(lib, "d3dcompiler.lib")
#define GRS_WND_CLASS _T("GRS Game Window Class")
#define GRS_WND_TITLE _T("GRS DirectX12 Sample: PBR IBL With Texture")
// 定义该宏 就使用文件打开对话框选择需要加载的资源
// 否则就使用默认的资源展示效果
#define GRS_USE_DIR_DIALOG
const UINT g_nFrameBackBufCount = 3u;
struct ST_GRS_D3D12_DEVICE
{
DWORD m_dwWndStyle;
INT m_iWndWidth;
INT m_iWndHeight;
HWND m_hWnd;
RECT m_rtWnd;
RECT m_rtWndClient;
const float m_faClearColor[4] = { 0.17647f, 0.549f, 0.941176f, 1.0f };
D3D_FEATURE_LEVEL m_emFeatureLevel = D3D_FEATURE_LEVEL_12_1;
ComPtr<IDXGIFactory6> m_pIDXGIFactory6;
ComPtr<ID3D12Device4> m_pID3D12Device4;
ComPtr<ID3D12CommandQueue> m_pIMainCMDQueue;
ComPtr<ID3D12CommandAllocator> m_pIMainCMDAlloc;
ComPtr<ID3D12GraphicsCommandList> m_pIMainCMDList;
BOOL m_bFullScreen;
BOOL m_bSupportTearing;
UINT m_nFrameIndex;
DXGI_FORMAT m_emRenderTargetFormat;
DXGI_FORMAT m_emDepthStencilFormat;
UINT m_nRTVDescriptorSize;
UINT m_nDSVDescriptorSize;
UINT m_nSamplerDescriptorSize;
UINT m_nCBVSRVDescriptorSize;
UINT64 m_n64FenceValue;
HANDLE m_hEventFence;
D3D12_VIEWPORT m_stViewPort;
D3D12_RECT m_stScissorRect;
ComPtr<ID3D12Fence> m_pIFence;
ComPtr<IDXGISwapChain3> m_pISwapChain3;
ComPtr<ID3D12DescriptorHeap> m_pIRTVHeap;
ComPtr<ID3D12Resource> m_pIARenderTargets[g_nFrameBackBufCount];
ComPtr<ID3D12DescriptorHeap> m_pIDSVHeap;
ComPtr<ID3D12Resource> m_pIDepthStencilBuffer;
};
// 利用GS 一次渲染Cube Map 的 6 个面 所以需要使用 GS Render Target Array 技术
#define GRS_CUBE_MAP_FACE_CNT 6u
#define GRS_LIGHT_COUNT 4u
#define GRS_RTA_DEFAULT_SIZE_W 2048
#define GRS_RTA_DEFAULT_SIZE_H 2048
#define GRS_RTA_IBL_DIFFUSE_MAP_SIZE_W 256
#define GRS_RTA_IBL_DIFFUSE_MAP_SIZE_H 256
#define GRS_RTA_IBL_SPECULAR_MAP_SIZE_W 512
#define GRS_RTA_IBL_SPECULAR_MAP_SIZE_H 512
#define GRS_RTA_IBL_SPECULAR_MAP_MIP 5
#define GRS_RTA_IBL_LUT_SIZE_W 512
#define GRS_RTA_IBL_LUT_SIZE_H 512
#define GRS_RTA_IBL_LUT_FORMAT DXGI_FORMAT_R32G32_FLOAT
// Geometry Shader Render Target Array Struct
struct ST_GRS_RENDER_TARGET_ARRAY_DATA
{
UINT m_nRTWidth;
UINT m_nRTHeight;
D3D12_VIEWPORT m_stViewPort;
D3D12_RECT m_stScissorRect;
DXGI_FORMAT m_emRTAFormat;
D3D12_RESOURCE_BARRIER m_stRTABarriers;
ComPtr<ID3D12DescriptorHeap> m_pIRTAHeap;
ComPtr<ID3D12Resource> m_pITextureRTA;
ComPtr<ID3D12Resource> m_pITextureRTADownload;
};
struct ST_GRS_PSO
{
UINT m_nCnt;
UINT m_nCBVCnt;
UINT m_nSRVCnt;
UINT m_nSmpCnt;
ComPtr<ID3D12RootSignature> m_pIRS;
ComPtr<ID3D12PipelineState> m_pIPSO;
ComPtr<ID3D12CommandAllocator> m_pICmdAlloc;
ComPtr<ID3D12GraphicsCommandList> m_pICmdBundles;
ComPtr<ID3D12CommandSignature> m_pICmdSignature;
};
struct ST_GRS_DESCRIPTOR_HEAP
{
UINT m_nSRVOffset; // SRV Offset
//UINT m_nCnt;
//UINT m_nCBVCnt;
//UINT m_nSRVCnt;
//UINT m_nSmpCnt;
ComPtr<ID3D12DescriptorHeap> m_pICBVSRVHeap; // CBV SRV Heap
ComPtr<ID3D12DescriptorHeap> m_pISAPHeap; // Sample Heap
};
//#pragma pack()
//#pragma pack(push,4)
//#pragma pack(show)
// Scene Matrix
struct ST_GRS_CB_SCENE_MATRIX
{
XMMATRIX m_mxWorld;
XMMATRIX m_mxView;
XMMATRIX m_mxProj;
XMMATRIX m_mxWorldView;
XMMATRIX m_mxWVP;
XMMATRIX m_mxInvWVP;
XMMATRIX m_mxVP;
XMMATRIX m_mxInvVP;
};
// GS 6 Direction Views
struct ST_GRS_CB_GS_VIEW_MATRIX
{
XMMATRIX m_mxGSCubeView[GRS_CUBE_MAP_FACE_CNT]; // Cube Map 6个 视方向的矩阵
};
// Material
struct ST_GRS_PBR_MATERIAL
{
float m_fMetallic; // 金属度
float m_fRoughness; // 粗糙度
float m_fAO; // 环境遮挡系数
XMVECTOR m_v4Albedo; // 反射率
};
// Camera
struct ST_GRS_SCENE_CAMERA
{
XMVECTOR m_v4EyePos;
XMVECTOR m_v4LookAt;
XMVECTOR m_v4UpDir;
};
// lights
struct ST_GRS_SCENE_LIGHTS
{
XMVECTOR m_v4LightPos[GRS_LIGHT_COUNT];
XMVECTOR m_v4LightColors[GRS_LIGHT_COUNT];
};
struct ST_GRS_VERTEX_CUBE_BOX
{
XMFLOAT4 m_v4Position;
};
struct ST_GRS_VERTEX_QUAD
{// 注意这个顶点结构保留Color的意思
// 是为了方便UI渲染时直接渲染一个颜色块出来,
// 这样就可以兼容颜色块或Texture,而不用再定义额外的结构体
// 也是向古老的HGE引擎致敬学习
XMFLOAT4 m_v4Position; //Position
XMFLOAT4 m_v4Color; //Color
XMFLOAT2 m_v2UV; //Texcoord
};
struct ST_GRS_VERTEX_QUAD_SIMPLE
{
XMFLOAT4 m_v4Position; //Position
XMFLOAT2 m_v2UV; //Texcoord
};
//#pragma pack(pop)
//#pragma pack(show)
struct ST_GRS_SCENE_CONST_DATA
{
ST_GRS_CB_SCENE_MATRIX* m_pstSceneMatrix;
ST_GRS_CB_GS_VIEW_MATRIX* m_pstGSViewMatrix;
ST_GRS_SCENE_CAMERA* m_pstSceneCamera;
ST_GRS_SCENE_LIGHTS* m_pstSceneLights;
ST_GRS_PBR_MATERIAL* m_pstPBRMaterial;
ComPtr<ID3D12Resource> m_pISceneMatrixBuffer;
ComPtr<ID3D12Resource> m_pISceneCameraBuffer;
ComPtr<ID3D12Resource> m_pISceneLightsBuffer;
ComPtr<ID3D12Resource> m_pIGSMatrixBuffer;
ComPtr<ID3D12Resource> m_pIPBRMaterialBuffer;
};
struct ST_GRS_TRIANGLE_LIST_DATA
{
UINT m_nVertexCount;
UINT m_nIndexCount;
D3D12_VERTEX_BUFFER_VIEW m_stVBV = {};
D3D12_INDEX_BUFFER_VIEW m_stIBV = {};
ComPtr<ID3D12Resource> m_pIVB;
ComPtr<ID3D12Resource> m_pIIB;
ComPtr<ID3D12Resource> m_pITexture;
};
struct ST_GRS_TRIANGLE_STRIP_DATA
{
UINT m_nVertexCount;
D3D12_VERTEX_BUFFER_VIEW m_stVertexBufferView;
ComPtr<ID3D12Resource> m_pIVertexBuffer;
ComPtr<ID3D12Resource> m_pITexture;
ComPtr<ID3D12Resource> m_pITextureUpload;
};
#define GRS_INSTANCE_DATA_SLOT_CNT 2
struct ST_GRS_TRIANGLE_STRIP_DATA_WITH_INSTANCE
{
UINT m_nVertexCount;
UINT m_nInstanceCount;
D3D12_VERTEX_BUFFER_VIEW m_pstVertexBufferView[GRS_INSTANCE_DATA_SLOT_CNT];
ComPtr<ID3D12Resource> m_ppIVertexBuffer[GRS_INSTANCE_DATA_SLOT_CNT];
CAtlArray<ComPtr<ID3D12Resource>> m_ppITexture;
};
struct ST_GRS_TRIANGLE_LIST_DATA_WITH_INSTANCE
{
UINT m_nIndexCount;
UINT m_nInstanceCount;
D3D12_INDEX_BUFFER_VIEW m_stIBV;
ComPtr<ID3D12Resource> m_pIIB;
D3D12_VERTEX_BUFFER_VIEW m_pstVBV[GRS_INSTANCE_DATA_SLOT_CNT];
ComPtr<ID3D12Resource> m_ppIVB[GRS_INSTANCE_DATA_SLOT_CNT];
CAtlArray<ComPtr<ID3D12Resource>> m_ppITexture;
};
struct ST_GRS_MESH_DATA_MULTI_SLOT
{
UINT m_nIndexCount;
DXGI_FORMAT m_emIndexType;
D3D12_INDEX_BUFFER_VIEW m_stIBV;
ComPtr<ID3D12Resource> m_pIIB;
CAtlArray<D3D12_VERTEX_BUFFER_VIEW> m_arVBV;
CAtlArray<ComPtr<ID3D12Resource>> m_arIVB;
};
struct ST_GRS_PER_QUAD_INSTANCE_DATA
{
XMMATRIX m_mxQuad2World;
UINT m_nTextureIndex;
};
struct ST_GRS_HDR_TEXTURE_DATA
{
TCHAR m_pszHDRFile[MAX_PATH];
INT m_iHDRWidth;
INT m_iHDRHeight;
INT m_iHDRPXComponents;
FLOAT* m_pfHDRData;
DXGI_FORMAT m_emHDRFormat;
ComPtr<ID3D12Resource> m_pIHDRTextureRaw;
ComPtr<ID3D12Resource> m_pIHDRTextureRawUpload;
};
#define GRS_MODEL_DATA_CB_CNT 1
#define GRS_PBR_MATERIAL_TEXTURE_CNT 6
struct ST_GRS_MODEL_DATA
{
XMMATRIX m_mxModel2World;
};
struct ST_GRS_SCENE_PBR_MATERIAL_MODEL_DATA
{
union
{
struct
{
TCHAR m_pszAlbedo[MAX_PATH];
TCHAR m_pszNormal[MAX_PATH];
TCHAR m_pszDisplacement[MAX_PATH];
TCHAR m_pszMetallic[MAX_PATH];
TCHAR m_pszRoughness[MAX_PATH];
TCHAR m_pszAO[MAX_PATH];
};
TCHAR m_pszPBRMaterialPathName[GRS_PBR_MATERIAL_TEXTURE_CNT][MAX_PATH];
};
ST_GRS_MODEL_DATA* m_pstModelData;
ComPtr<ID3D12DescriptorHeap> m_pIMaterialHeap;
CAtlArray<ComPtr<ID3D12Resource>> m_arTexMaterial;
CAtlArray<ComPtr<ID3D12Resource>> m_arTexMaterialUpload;
ComPtr<ID3D12Resource> m_pICBModelData;
};
struct ST_GRS_CAMERA
{
XMVECTOR m_v4EyePos;
XMVECTOR m_v4LookDir;
XMVECTOR m_v4UpDir;
float m_fNear;
float m_fFar;
};
struct ST_GRS_CAMERA_CONTROL
{
float m_fRadius;
XMVECTOR m_qCameraStart;
XMVECTOR m_qCameraMoves;
XMVECTOR m_vCameraPosStart;
XMVECTOR m_vCameraPosMoves;
};
ST_GRS_CAMERA g_stCamera = {};
ST_GRS_CAMERA_CONTROL g_stCameraControl = {};
BOOL g_bWorldRotate = TRUE;
// 场景的旋转角速度(弧度/s)
float g_fPalstance = 15.0f;
// 场景旋转的角度:旋转角
float g_fWorldRotateAngle = 0;
BOOL g_bShowRTA = TRUE;
BOOL g_bUseLight = FALSE;
// 常见材质的线性反射率数组
XMFLOAT4 g_v4Albedo[]
= { {0.02f, 0.02f, 0.02f,1.0f}, //水
{0.03f, 0.03f, 0.03f,1.0f}, //塑料 / 玻璃(低)
{0.05f, 0.05f, 0.05f,1.0f}, //塑料(高)
{0.08f, 0.08f, 0.08f,1.0f}, //玻璃(高) / 红宝石
{0.17f, 0.17f, 0.17f,1.0f}, //钻石
{0.56f, 0.57f, 0.58f,1.0f}, //铁
{0.95f, 0.64f, 0.54f,1.0f}, //铜
{1.00f, 0.71f, 0.29f,1.0f}, //金
{0.91f, 0.92f, 0.92f,1.0f}, //铝
{0.95f, 0.93f, 0.88f,1.0f} //银
};
float g_fScale = 1.0f; // 加载物体的缩放比例 Z键放大 C键缩小
D3D12_HEAP_PROPERTIES g_stDefaultHeapProps = {};
D3D12_HEAP_PROPERTIES g_stUploadHeapProps = {};
D3D12_RESOURCE_DESC g_stBufferResSesc = {};
ST_GRS_D3D12_DEVICE g_stD3DDevice = {};
ST_GRS_SCENE_CONST_DATA g_stWorldConstData = {};
ST_GRS_SCENE_CONST_DATA g_stQuadConstData = {};
ST_GRS_HDR_TEXTURE_DATA g_stHDRData = {};
ST_GRS_RENDER_TARGET_ARRAY_DATA g_st1TimesGSRTAStatus = {};
ST_GRS_DESCRIPTOR_HEAP g_st1TimesGSHeap = {};
ST_GRS_PSO g_st1TimesGSPSO = {};
ST_GRS_TRIANGLE_STRIP_DATA g_stBoxData_Strip = {};
ST_GRS_RENDER_TARGET_ARRAY_DATA g_st6TimesRTAStatus = {};
ST_GRS_DESCRIPTOR_HEAP g_stHDR2CubemapHeap = {};
ST_GRS_PSO g_stHDR2CubemapPSO = {};
ST_GRS_TRIANGLE_LIST_DATA g_stBoxData_Index = {};
// IBL 慢反射预积分计算需要的数据结构
ST_GRS_RENDER_TARGET_ARRAY_DATA g_stIBLDiffusePreIntRTA = {};
ST_GRS_PSO g_stIBLDiffusePreIntPSO = {};
// IBL 镜面反射预积分计算需要的数据结构
ST_GRS_RENDER_TARGET_ARRAY_DATA g_stIBLSpecularPreIntRTA = {};
ST_GRS_PSO g_stIBLSpecularPreIntPSO = {};
// IBL 镜面反射LUT计算需要的数据结构
ST_GRS_RENDER_TARGET_ARRAY_DATA g_stIBLLutRTA = {};
ST_GRS_PSO g_stIBLLutPSO = {};
ST_GRS_TRIANGLE_STRIP_DATA g_stIBLLutData = {};
ST_GRS_DESCRIPTOR_HEAP g_stIBLHeap = {};
ST_GRS_PSO g_stIBLPSO = {};
ST_GRS_MESH_DATA_MULTI_SLOT g_stMultiMeshData = {};
CAtlArray<ST_GRS_SCENE_PBR_MATERIAL_MODEL_DATA> g_arPBRMaterial;
ST_GRS_DESCRIPTOR_HEAP g_stSkyBoxHeap = {};
ST_GRS_PSO g_stSkyBoxPSO = {};
ST_GRS_TRIANGLE_STRIP_DATA g_stSkyBoxData = {};
ST_GRS_DESCRIPTOR_HEAP g_stQuadHeap = {};
ST_GRS_PSO g_stQuadPSO = {};
ST_GRS_TRIANGLE_STRIP_DATA_WITH_INSTANCE g_stQuadData = {};
TCHAR g_pszAppPath[MAX_PATH] = {};
TCHAR g_pszShaderPath[MAX_PATH] = {};
TCHAR g_pszHDRFilePath[MAX_PATH] = {};
void OnSize(UINT width, UINT height, bool minimized);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
USES_CONVERSION;
try
{
::CoInitialize(nullptr); //for WIC & COM
// 0-1、初始化全局参数
{
g_stD3DDevice.m_iWndWidth = 1280;
g_stD3DDevice.m_iWndHeight = 800;
g_stD3DDevice.m_hWnd = nullptr;
g_stD3DDevice.m_nFrameIndex = 0;
g_stD3DDevice.m_emRenderTargetFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
g_stD3DDevice.m_emDepthStencilFormat = DXGI_FORMAT_D32_FLOAT;
g_stD3DDevice.m_nRTVDescriptorSize = 0U;
g_stD3DDevice.m_nDSVDescriptorSize = 0U;
g_stD3DDevice.m_nSamplerDescriptorSize = 0u;
g_stD3DDevice.m_nCBVSRVDescriptorSize = 0u;
g_stD3DDevice.m_stViewPort = { 0.0f, 0.0f, static_cast<float>(g_stD3DDevice.m_iWndWidth), static_cast<float>(g_stD3DDevice.m_iWndHeight), D3D12_MIN_DEPTH, D3D12_MAX_DEPTH };
g_stD3DDevice.m_stScissorRect = { 0, 0, static_cast<LONG>(g_stD3DDevice.m_iWndWidth), static_cast<LONG>(g_stD3DDevice.m_iWndHeight) };
g_stD3DDevice.m_n64FenceValue = 0ui64;
g_stD3DDevice.m_hEventFence = nullptr;
g_stDefaultHeapProps.Type = D3D12_HEAP_TYPE_DEFAULT;
g_stDefaultHeapProps.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
g_stDefaultHeapProps.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
g_stDefaultHeapProps.CreationNodeMask = 0;
g_stDefaultHeapProps.VisibleNodeMask = 0;
g_stUploadHeapProps.Type = D3D12_HEAP_TYPE_UPLOAD;
g_stUploadHeapProps.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
g_stUploadHeapProps.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
g_stUploadHeapProps.CreationNodeMask = 0;
g_stUploadHeapProps.VisibleNodeMask = 0;
g_stBufferResSesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
g_stBufferResSesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
g_stBufferResSesc.Flags = D3D12_RESOURCE_FLAG_NONE;
g_stBufferResSesc.Format = DXGI_FORMAT_UNKNOWN;
g_stBufferResSesc.Width = 0;
g_stBufferResSesc.Height = 1;
g_stBufferResSesc.DepthOrArraySize = 1;
g_stBufferResSesc.MipLevels = 1;
g_stBufferResSesc.SampleDesc.Count = 1;
g_stBufferResSesc.SampleDesc.Quality = 0;
}
// 0-2、得到当前的工作目录,方便我们使用相对路径来访问各种资源文件
{
if (0 == ::GetModuleFileName(nullptr, g_pszAppPath, MAX_PATH))
{
GRS_THROW_IF_FAILED(HRESULT_FROM_WIN32(GetLastError()));
}
WCHAR* lastSlash = _tcsrchr(g_pszAppPath, _T('\\'));
if (lastSlash)
{//删除Exe文件名
*(lastSlash) = _T('\0');
}
lastSlash = _tcsrchr(g_pszAppPath, _T('\\'));
if (lastSlash)
{//删除x64路径
*(lastSlash) = _T('\0');
}
lastSlash = _tcsrchr(g_pszAppPath, _T('\\'));
if (lastSlash)
{//删除Debug 或 Release路径
*(lastSlash + 1) = _T('\0');
}
::StringCchPrintf(g_pszShaderPath
, MAX_PATH
, _T("%s27-IBL-With-Material-Texture\\Shader")
, g_pszAppPath);
}
// 0-3、准备编程Shader时处理包含文件的类及路径
TCHAR pszPublicShaderPath[MAX_PATH] = {};
::StringCchPrintf(pszPublicShaderPath
, MAX_PATH
, _T("%sShader")
, g_pszAppPath);
CGRSD3DCompilerInclude grsD3DCompilerInclude(pszPublicShaderPath);
grsD3DCompilerInclude.AddDir(g_pszShaderPath);
// 0-4、初始化摄像机参数
{
g_stCamera.m_v4EyePos = XMVectorSet(0.0f, 1.0f, -10.0f, 1.0f); // 位置
g_stCamera.m_v4LookDir = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); // 朝向
g_stCamera.m_v4UpDir = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); // 上方
g_stCamera.m_fNear = 0.1f;
g_stCamera.m_fFar = 200000.0f; //这个超大的值是为了 skybox
// 设置虚拟控制球半径
g_stCameraControl.m_fRadius = 100.0f;
// 初始化鼠标旋转控制四元数
g_stCameraControl.m_qCameraStart = XMQuaternionIdentity();
g_stCameraControl.m_qCameraMoves = g_stCameraControl.m_qCameraStart;
}
// 1-1、创建窗口
{
WNDCLASSEX wcex = {};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_GLOBALCLASS;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH); //防止无聊的背景重绘
wcex.lpszClassName = GRS_WND_CLASS;
::RegisterClassEx(&wcex);
g_stD3DDevice.m_dwWndStyle = WS_OVERLAPPEDWINDOW;
RECT rtWnd = { 0, 0, g_stD3DDevice.m_iWndWidth, g_stD3DDevice.m_iWndHeight };
::AdjustWindowRect(&rtWnd, g_stD3DDevice.m_dwWndStyle, FALSE);
// 计算窗口居中的屏幕坐标
INT posX = (::GetSystemMetrics(SM_CXSCREEN) - rtWnd.right - rtWnd.left) / 2;
INT posY = (::GetSystemMetrics(SM_CYSCREEN) - rtWnd.bottom - rtWnd.top) / 2;
g_stD3DDevice.m_hWnd = ::CreateWindowW(GRS_WND_CLASS
, GRS_WND_TITLE
, g_stD3DDevice.m_dwWndStyle
, posX
, posY
, rtWnd.right - rtWnd.left
, rtWnd.bottom - rtWnd.top
, nullptr
, nullptr
, hInstance
, nullptr);
if (!g_stD3DDevice.m_hWnd)
{
GRS_THROW_IF_FAILED(HRESULT_FROM_WIN32(GetLastError()));
}
}
// 2-1、创建DXGI Factory对象
{
UINT nDXGIFactoryFlags = 0U;
#if defined(_DEBUG)
// 打开显示子系统的调试支持
ComPtr<ID3D12Debug> debugController;
if (SUCCEEDED(::D3D12GetDebugInterface(IID_PPV_ARGS(&debugController))))
{
debugController->EnableDebugLayer();
// 打开附加的调试支持
nDXGIFactoryFlags |= DXGI_CREATE_FACTORY_DEBUG;
}
#endif
ComPtr<IDXGIFactory5> pIDXGIFactory5;
GRS_THROW_IF_FAILED(::CreateDXGIFactory2(nDXGIFactoryFlags, IID_PPV_ARGS(&pIDXGIFactory5)));
GRS_SET_DXGI_DEBUGNAME_COMPTR(pIDXGIFactory5);
//获取IDXGIFactory6接口
GRS_THROW_IF_FAILED(pIDXGIFactory5.As(&g_stD3DDevice.m_pIDXGIFactory6));
GRS_SET_DXGI_DEBUGNAME_COMPTR(g_stD3DDevice.m_pIDXGIFactory6);
// 检测Tearing支持
HRESULT hr = g_stD3DDevice.m_pIDXGIFactory6->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING
, &g_stD3DDevice.m_bSupportTearing
, sizeof(g_stD3DDevice.m_bSupportTearing));
g_stD3DDevice.m_bSupportTearing = SUCCEEDED(hr) && g_stD3DDevice.m_bSupportTearing;
}
// 2-2、枚举适配器创建设备
{ //选择高性能的显卡来创建3D设备对象
ComPtr<IDXGIAdapter1> pIAdapter1;
GRS_THROW_IF_FAILED(g_stD3DDevice.m_pIDXGIFactory6->EnumAdapterByGpuPreference(0
, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE
, IID_PPV_ARGS(&pIAdapter1)));
GRS_SET_DXGI_DEBUGNAME_COMPTR(pIAdapter1);
// 创建D3D12.1的设备
GRS_THROW_IF_FAILED(D3D12CreateDevice(pIAdapter1.Get(), D3D_FEATURE_LEVEL_12_1, IID_PPV_ARGS(&g_stD3DDevice.m_pID3D12Device4)));
GRS_SET_D3D12_DEBUGNAME_COMPTR(g_stD3DDevice.m_pID3D12Device4);
DXGI_ADAPTER_DESC1 stAdapterDesc = {};
TCHAR pszWndTitle[MAX_PATH] = {};
GRS_THROW_IF_FAILED(pIAdapter1->GetDesc1(&stAdapterDesc));
::GetWindowText(g_stD3DDevice.m_hWnd, pszWndTitle, MAX_PATH);
StringCchPrintf(pszWndTitle
, MAX_PATH
, _T("%s(GPU[0]:%s)")
, pszWndTitle
, stAdapterDesc.Description);
::SetWindowText(g_stD3DDevice.m_hWnd, pszWndTitle);
}
// 2-3、得到每种描述符元素的大小
{
g_stD3DDevice.m_nRTVDescriptorSize
= g_stD3DDevice.m_pID3D12Device4->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
g_stD3DDevice.m_nDSVDescriptorSize
= g_stD3DDevice.m_pID3D12Device4->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
g_stD3DDevice.m_nSamplerDescriptorSize
= g_stD3DDevice.m_pID3D12Device4->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
g_stD3DDevice.m_nCBVSRVDescriptorSize
= g_stD3DDevice.m_pID3D12Device4->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
}
// 2-4、创建命令队列&命令列表
{
D3D12_COMMAND_QUEUE_DESC stQueueDesc = {};
stQueueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
GRS_THROW_IF_FAILED(g_stD3DDevice.m_pID3D12Device4->CreateCommandQueue(&stQueueDesc, IID_PPV_ARGS(&g_stD3DDevice.m_pIMainCMDQueue)));
GRS_SET_D3D12_DEBUGNAME_COMPTR(g_stD3DDevice.m_pIMainCMDQueue);
GRS_THROW_IF_FAILED(g_stD3DDevice.m_pID3D12Device4->CreateCommandAllocator(
D3D12_COMMAND_LIST_TYPE_DIRECT
, IID_PPV_ARGS(&g_stD3DDevice.m_pIMainCMDAlloc)));
GRS_SET_D3D12_DEBUGNAME_COMPTR(g_stD3DDevice.m_pIMainCMDAlloc);
// 创建图形命令列表
GRS_THROW_IF_FAILED(g_stD3DDevice.m_pID3D12Device4->CreateCommandList(
0
, D3D12_COMMAND_LIST_TYPE_DIRECT
, g_stD3DDevice.m_pIMainCMDAlloc.Get()
, nullptr
, IID_PPV_ARGS(&g_stD3DDevice.m_pIMainCMDList)));
GRS_SET_D3D12_DEBUGNAME_COMPTR(g_stD3DDevice.m_pIMainCMDList);
}
// 2-5、创建交换链
{
DXGI_SWAP_CHAIN_DESC1 stSwapChainDesc = {};
stSwapChainDesc.BufferCount = g_nFrameBackBufCount;
stSwapChainDesc.Width = g_stD3DDevice.m_iWndWidth;
stSwapChainDesc.Height = g_stD3DDevice.m_iWndHeight;
stSwapChainDesc.Format = g_stD3DDevice.m_emRenderTargetFormat;
stSwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
stSwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
stSwapChainDesc.SampleDesc.Count = 1;
stSwapChainDesc.Scaling = DXGI_SCALING_NONE;
// 检测并启用Tearing支持
stSwapChainDesc.Flags = g_stD3DDevice.m_bSupportTearing ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0;
ComPtr<IDXGISwapChain1> pISwapChain1;
GRS_THROW_IF_FAILED(g_stD3DDevice.m_pIDXGIFactory6->CreateSwapChainForHwnd(
g_stD3DDevice.m_pIMainCMDQueue.Get(), // 交换链需要命令队列,Present命令要执行
g_stD3DDevice.m_hWnd,
&stSwapChainDesc,
nullptr,
nullptr,
&pISwapChain1
));
GRS_SET_DXGI_DEBUGNAME_COMPTR(pISwapChain1);
GRS_THROW_IF_FAILED(pISwapChain1.As(&g_stD3DDevice.m_pISwapChain3));
GRS_SET_DXGI_DEBUGNAME_COMPTR(g_stD3DDevice.m_pISwapChain3);
//得到当前后缓冲区的序号,也就是下一个将要呈送显示的缓冲区的序号
//注意此处使用了高版本的SwapChain接口的函数
g_stD3DDevice.m_nFrameIndex = g_stD3DDevice.m_pISwapChain3->GetCurrentBackBufferIndex();
//创建RTV(渲染目标视图)描述符堆(这里堆的含义应当理解为数组或者固定大小元素的固定大小显存池)
{
D3D12_DESCRIPTOR_HEAP_DESC stRTVHeapDesc = {};
stRTVHeapDesc.NumDescriptors = g_nFrameBackBufCount;
stRTVHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
stRTVHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
GRS_THROW_IF_FAILED(g_stD3DDevice.m_pID3D12Device4->CreateDescriptorHeap(
&stRTVHeapDesc
, IID_PPV_ARGS(&g_stD3DDevice.m_pIRTVHeap)));
GRS_SET_D3D12_DEBUGNAME_COMPTR(g_stD3DDevice.m_pIRTVHeap);
}
//创建RTV的描述符
{
D3D12_CPU_DESCRIPTOR_HANDLE stRTVHandle
= g_stD3DDevice.m_pIRTVHeap->GetCPUDescriptorHandleForHeapStart();
for (UINT i = 0; i < g_nFrameBackBufCount; i++)
{
GRS_THROW_IF_FAILED(g_stD3DDevice.m_pISwapChain3->GetBuffer(i
, IID_PPV_ARGS(&g_stD3DDevice.m_pIARenderTargets[i])));
GRS_SET_D3D12_DEBUGNAME_INDEXED_COMPTR(g_stD3DDevice.m_pIARenderTargets, i);
g_stD3DDevice.m_pID3D12Device4->CreateRenderTargetView(g_stD3DDevice.m_pIARenderTargets[i].Get()
, nullptr
, stRTVHandle);
stRTVHandle.ptr += g_stD3DDevice.m_nRTVDescriptorSize;
}
}
if (g_stD3DDevice.m_bSupportTearing)
{// 如果支持Tearing,那么就关闭系统的ALT + Entery全屏键支持,而改用我们自己处理
GRS_THROW_IF_FAILED(g_stD3DDevice.m_pIDXGIFactory6->MakeWindowAssociation(g_stD3DDevice.m_hWnd, DXGI_MWA_NO_ALT_ENTER));
}
}
// 2-6、创建深度缓冲及深度缓冲描述符堆
{
D3D12_DEPTH_STENCIL_VIEW_DESC stDepthStencilDesc = {};
D3D12_CLEAR_VALUE stDepthOptimizedClearValue = {};
D3D12_RESOURCE_DESC stDSTex2DDesc = {};
D3D12_DESCRIPTOR_HEAP_DESC stDSVHeapDesc = {};
stDepthStencilDesc.Format = g_stD3DDevice.m_emDepthStencilFormat;
stDepthStencilDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
stDepthStencilDesc.Flags = D3D12_DSV_FLAG_NONE;
stDepthOptimizedClearValue.Format = g_stD3DDevice.m_emDepthStencilFormat;
stDepthOptimizedClearValue.DepthStencil.Depth = 1.0f;
stDepthOptimizedClearValue.DepthStencil.Stencil = 0;
stDSTex2DDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
stDSTex2DDesc.Alignment = 0;
stDSTex2DDesc.Width = g_stD3DDevice.m_iWndWidth;
stDSTex2DDesc.Height = g_stD3DDevice.m_iWndHeight;
stDSTex2DDesc.DepthOrArraySize = 1;
stDSTex2DDesc.MipLevels = 0;
stDSTex2DDesc.Format = g_stD3DDevice.m_emDepthStencilFormat;
stDSTex2DDesc.SampleDesc.Count = 1;
stDSTex2DDesc.SampleDesc.Quality = 0;
stDSTex2DDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
stDSTex2DDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
stDSVHeapDesc.NumDescriptors = 1;
stDSVHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
stDSVHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
//使用隐式默认堆创建一个深度蜡板缓冲区,
//因为基本上深度缓冲区会一直被使用,重用的意义不大,所以直接使用隐式堆,图方便
GRS_THROW_IF_FAILED(g_stD3DDevice.m_pID3D12Device4->CreateCommittedResource(
&g_stDefaultHeapProps
, D3D12_HEAP_FLAG_NONE
, &stDSTex2DDesc
, D3D12_RESOURCE_STATE_DEPTH_WRITE
, &stDepthOptimizedClearValue
, IID_PPV_ARGS(&g_stD3DDevice.m_pIDepthStencilBuffer)
));
GRS_SET_D3D12_DEBUGNAME_COMPTR(g_stD3DDevice.m_pIDepthStencilBuffer);
//==============================================================================================
// 创建 DSV Heap
GRS_THROW_IF_FAILED(g_stD3DDevice.m_pID3D12Device4->CreateDescriptorHeap(&stDSVHeapDesc, IID_PPV_ARGS(&g_stD3DDevice.m_pIDSVHeap)));
GRS_SET_D3D12_DEBUGNAME_COMPTR(g_stD3DDevice.m_pIDSVHeap);
// 创建 DSV
g_stD3DDevice.m_pID3D12Device4->CreateDepthStencilView(g_stD3DDevice.m_pIDepthStencilBuffer.Get()
, &stDepthStencilDesc
, g_stD3DDevice.m_pIDSVHeap->GetCPUDescriptorHandleForHeapStart());
}
// 2-7、创建围栏
{
GRS_THROW_IF_FAILED(g_stD3DDevice.m_pID3D12Device4->CreateFence(0
, D3D12_FENCE_FLAG_NONE
, IID_PPV_ARGS(&g_stD3DDevice.m_pIFence)));
GRS_SET_D3D12_DEBUGNAME_COMPTR(g_stD3DDevice.m_pIFence);
// 初始化 Fence 计数为 1
g_stD3DDevice.m_n64FenceValue = 1;
// 创建一个Event同步对象,用于等待围栏事件通知
// 本质上是为了 CPU 与 GPU 的同步
g_stD3DDevice.m_hEventFence = CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (g_stD3DDevice.m_hEventFence == nullptr)
{
GRS_THROW_IF_FAILED(HRESULT_FROM_WIN32(GetLastError()));
}
}
// 2-8、检测根签名版本,现在只支持1.1以上的,低版本的就丢弃退出了,不然兼容的代码太啰嗦了也毫无营养
{
D3D12_FEATURE_DATA_ROOT_SIGNATURE stFeatureData = {};
// 检测是否支持V1.1版本的根签名
stFeatureData.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_1;
if (FAILED(g_stD3DDevice.m_pID3D12Device4->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE
, &stFeatureData
, sizeof(stFeatureData))))
{ // 1.0版 直接丢异常退出了
ATLTRACE("系统不支持V1.1的跟签名,直接退出了,需要运行自行修改创建跟签名的代码回1.0版。");
GRS_THROW_IF_FAILED(E_NOTIMPL);
}
}
//---------------------------------------------------------------------------------------
// 3、使用HDR 等距柱面环境贴图生成 立方体贴图的六个面
// 第一种方式: 使用GS Render Target Array方法一次渲染到六个面
{}
// 3-1-0、初始化Render Target参数
{
g_st1TimesGSRTAStatus.m_nRTWidth = GRS_RTA_DEFAULT_SIZE_W;
g_st1TimesGSRTAStatus.m_nRTHeight = GRS_RTA_DEFAULT_SIZE_H;
g_st1TimesGSRTAStatus.m_stViewPort = { 0.0f
, 0.0f
, static_cast<float>(g_st1TimesGSRTAStatus.m_nRTWidth)
, static_cast<float>(g_st1TimesGSRTAStatus.m_nRTHeight)
, D3D12_MIN_DEPTH, D3D12_MAX_DEPTH };
g_st1TimesGSRTAStatus.m_stScissorRect = { 0
, 0
, static_cast<LONG>(g_st1TimesGSRTAStatus.m_nRTWidth)
, static_cast<LONG>(g_st1TimesGSRTAStatus.m_nRTHeight) };
g_st1TimesGSRTAStatus.m_emRTAFormat = DXGI_FORMAT_R32G32B32A32_FLOAT;
}
// 3-1-1、创建利用 Geometry shader 渲染到Cube Map的PSO
{
g_st1TimesGSPSO.m_nCBVCnt = 2;
g_st1TimesGSPSO.m_nSRVCnt = 1;
g_st1TimesGSPSO.m_nSmpCnt = 1;
g_st1TimesGSPSO.m_nCnt = g_st1TimesGSPSO.m_nCBVCnt + g_st1TimesGSPSO.m_nSRVCnt;
D3D12_DESCRIPTOR_RANGE1 stDSPRanges[3] = {};
// 2 Const Buffer ( MVP Matrix + Cube Map 6 View Matrix)
stDSPRanges[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;
stDSPRanges[0].NumDescriptors = g_st1TimesGSPSO.m_nCBVCnt;
stDSPRanges[0].BaseShaderRegister = 0;
stDSPRanges[0].RegisterSpace = 0;
stDSPRanges[0].Flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE;
stDSPRanges[0].OffsetInDescriptorsFromTableStart = 0;
// 1 Texture ( HDR Texture )
stDSPRanges[1].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
stDSPRanges[1].NumDescriptors = g_st1TimesGSPSO.m_nSRVCnt;
stDSPRanges[1].BaseShaderRegister = 0;
stDSPRanges[1].RegisterSpace = 0;
stDSPRanges[1].Flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE;
stDSPRanges[1].OffsetInDescriptorsFromTableStart = 0;
// 1 Sampler ( Linear Sampler )
stDSPRanges[2].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER;
stDSPRanges[2].NumDescriptors = g_st1TimesGSPSO.m_nSmpCnt;
stDSPRanges[2].BaseShaderRegister = 0;
stDSPRanges[2].RegisterSpace = 0;
stDSPRanges[2].Flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE;
stDSPRanges[2].OffsetInDescriptorsFromTableStart = 0;
D3D12_ROOT_PARAMETER1 stRootParameters[3] = {};
stRootParameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
stRootParameters[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;//CBV是所有Shader可见
stRootParameters[0].DescriptorTable.NumDescriptorRanges = 1;
stRootParameters[0].DescriptorTable.pDescriptorRanges = &stDSPRanges[0];
stRootParameters[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
stRootParameters[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;//SRV仅PS可见
stRootParameters[1].DescriptorTable.NumDescriptorRanges = 1;
stRootParameters[1].DescriptorTable.pDescriptorRanges = &stDSPRanges[1];
stRootParameters[2].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
stRootParameters[2].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;//SAMPLE仅PS可见
stRootParameters[2].DescriptorTable.NumDescriptorRanges = 1;
stRootParameters[2].DescriptorTable.pDescriptorRanges = &stDSPRanges[2];
D3D12_VERSIONED_ROOT_SIGNATURE_DESC stRootSignatureDesc = {};
stRootSignatureDesc.Version = D3D_ROOT_SIGNATURE_VERSION_1_1;
stRootSignatureDesc.Desc_1_1.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
stRootSignatureDesc.Desc_1_1.NumParameters = _countof(stRootParameters);
stRootSignatureDesc.Desc_1_1.pParameters = stRootParameters;
stRootSignatureDesc.Desc_1_1.NumStaticSamplers = 0;
stRootSignatureDesc.Desc_1_1.pStaticSamplers = nullptr;
ComPtr<ID3DBlob> pISignatureBlob;
ComPtr<ID3DBlob> pIErrorBlob;
GRS_THROW_IF_FAILED(D3D12SerializeVersionedRootSignature(&stRootSignatureDesc
, &pISignatureBlob
, &pIErrorBlob));
GRS_THROW_IF_FAILED(g_stD3DDevice.m_pID3D12Device4->CreateRootSignature(0
, pISignatureBlob->GetBufferPointer()
, pISignatureBlob->GetBufferSize()
, IID_PPV_ARGS(&g_st1TimesGSPSO.m_pIRS)));
GRS_SET_D3D12_DEBUGNAME_COMPTR(g_st1TimesGSPSO.m_pIRS);
//--------------------------------------------------------------------------------------------------------------------------------
UINT nCompileFlags = 0;
#if defined(_DEBUG)
// Enable better shader debugging with the graphics debugging tools.
nCompileFlags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
#endif
//编译为行矩阵形式
nCompileFlags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
TCHAR pszShaderFileName[MAX_PATH] = {};
ComPtr<ID3DBlob> pIVSCode;
ComPtr<ID3DBlob> pIGSCode;
ComPtr<ID3DBlob> pIPSCode;
ComPtr<ID3DBlob> pIErrorMsg;
::StringCchPrintf(pszShaderFileName
, MAX_PATH
, _T("%s\\1-2 GRS_1Times_GS_HDR_2_CubeMap_VS_GS.hlsl"), g_pszShaderPath);
HRESULT hr = D3DCompileFromFile(pszShaderFileName, nullptr, &grsD3DCompilerInclude
, "VSMain", "vs_5_0", nCompileFlags, 0, &pIVSCode, &pIErrorMsg);
if (FAILED(hr))
{
ATLTRACE("编译 Vertex Shader:\"%s\" 发生错误:%s\n"
, T2A(pszShaderFileName)
, pIErrorMsg ? pIErrorMsg->GetBufferPointer() : "无法读取文件!");
GRS_THROW_IF_FAILED(hr);
}
pIErrorMsg.Reset();
hr = ::D3DCompileFromFile(pszShaderFileName, nullptr, &grsD3DCompilerInclude
, "GSMain", "gs_5_0", nCompileFlags, 0, &pIGSCode, &pIErrorMsg);
if (FAILED(hr))
{
ATLTRACE("编译 Geometry Shader:\"%s\" 发生错误:%s\n"
, T2A(pszShaderFileName)
, pIErrorMsg ? pIErrorMsg->GetBufferPointer() : "无法读取文件!");
GRS_THROW_IF_FAILED(hr);
}
pIErrorMsg.Reset();
::StringCchPrintf(pszShaderFileName
, MAX_PATH
, _T("%s\\1-3 GRS_HDR_Spherical_Map_2_Cubemap_PS.hlsl")
, g_pszShaderPath);
hr = D3DCompileFromFile(pszShaderFileName, nullptr, &grsD3DCompilerInclude
, "PSMain", "ps_5_0", nCompileFlags, 0, &pIPSCode, &pIErrorMsg);
if (FAILED(hr))
{
ATLTRACE("编译 Pixel Shader:\"%s\" 发生错误:%s\n"
, T2A(pszShaderFileName)
, pIErrorMsg ? pIErrorMsg->GetBufferPointer() : "无法读取文件!");
GRS_THROW_IF_FAILED(hr);
}
pIErrorMsg.Reset();