-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvismtv_inbuilt_renderergpudx.cpp
887 lines (734 loc) · 31.2 KB
/
vismtv_inbuilt_renderergpudx.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
#include "vismtv_inbuilt_renderergpudx.h"
//#include "VXDX11Helper.h"
#include <d2d1_1.h>
#include <dwrite.h>
#include "RendererHeader.h"
#include <iostream>
//GPU Direct3D Renderer - (c)DongJoon Kim
#define MODULEDEFINEDSPECIFIER "GPU Direct3D Renderer - (c)DongJoon Kim"
//#define RELEASE_MODE
double g_dProgress = 0;
double g_dRunTimeVRs = 0;
grd_helper::GpuDX11CommonParameters g_vmCommonParams;
LocalProgress g_LocalProgress;
VmGpuManager* g_pCGpuManager = NULL;
ID2D1Factory1* g_pDirect2dFactory = NULL;
IDWriteFactory* g_pDWriteFactory = NULL;
struct D2DRes {
IDXGISurface* pDxgiSurface = NULL;
ID2D1RenderTarget* pRenderTarget = NULL;
ID2D1SolidColorBrush* pSolidBrush = NULL;
std::map<void*, ID3D11ShaderResourceView*> mapDevSharedSRVs;
ID3D11Texture2D* pTex2DRT = NULL; // do not release this!
void ReleaseD2DRes() {
VMSAFE_RELEASE(pSolidBrush);
VMSAFE_RELEASE(pRenderTarget);
VMSAFE_RELEASE(pDxgiSurface);
for (auto& kv : mapDevSharedSRVs) {
VMSAFE_RELEASE(kv.second);
}
mapDevSharedSRVs.clear();
}
};
map<int, D2DRes> g_d2dResMap;
map<string, ID2D1StrokeStyle*> g_d2dStrokeStyleMap;
map<string, IDWriteTextFormat*> g_d2dTextFormatMap;
//std::atomic_int g_state = 0;
bool CheckModuleParameters(fncontainer::VmFnContainer& _fncontainer)
{
/////////////////////////////////////////////////
// Check whether the module inputs are correct //
// vector<VmObject*> vtrInputVolumes;
// _fncontainer.GetVmObjectList(&vtrInputVolumes, VmObjKey(ObjectTypeVOLUME, true));
// vector<VmObject*> vtrInputOTFs;
// _fncontainer.GetVmObjectList(&vtrInputOTFs, VmObjKey(ObjectTypeTMAP, true));
// vector<VmObject*> vtrOutputIPs;
// _fncontainer.GetVmObjectList(&vtrOutputIPs, VmObjKey(ObjectTypeIMAGEPLANE, false));
//
// if(vtrInputVolumes.size() == 0 || vtrInputOTFs.size() == 0 || vtrOutputIPs.size() == 0)
// return false;
g_dProgress = 0;
return true;
}
static CRITICAL_SECTION cs;
bool InitModule(fncontainer::VmFnContainer& _fncontainer)
{
InitializeCriticalSection(&cs);
if(g_pCGpuManager == NULL)
g_pCGpuManager = new VmGpuManager(GpuSdkTypeDX11, __DLLNAME);
if (grd_helper::InitializePresettings(g_pCGpuManager, &g_vmCommonParams) == -1)
{
vmlog::LogErr("failure new initializer!");
DeInitModule(fncontainer::VmFnContainer());
return false;
}
vmlog::LogInfo(string("Plugin: GPU DX11 Renderer using Core v(") + __VERSION + ")");
// TEST //
//return false;
// Create a Direct2D factory.
if (g_pDirect2dFactory == NULL) {
if (D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_pDirect2dFactory) != S_OK)
vmlog::LogErr("Failure D2D1CreateFactory!!");
// IDWriteFactory는 DWriteCreateFactory 함수 호출 생성.
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(g_pDWriteFactory), reinterpret_cast<IUnknown**>(&g_pDWriteFactory));
ID2D1StrokeStyle* pStrokeStyle = NULL;
HRESULT hr = g_pDirect2dFactory->CreateStrokeStyle(
D2D1::StrokeStyleProperties(
D2D1_CAP_STYLE_ROUND,
D2D1_CAP_STYLE_ROUND,
D2D1_CAP_STYLE_ROUND,
D2D1_LINE_JOIN_ROUND,
10.0f,
D2D1_DASH_STYLE_SOLID,
0.0f),
NULL,
0,
&pStrokeStyle
);
g_d2dStrokeStyleMap["DEFAULT"] = pStrokeStyle;
IDWriteTextFormat* pTextFormat = NULL;
g_pDWriteFactory->CreateTextFormat(
L"Comic Sans MS", // 폰트 패밀리 이름의 문자열
NULL, // 폰트 컬렉션 객체, NULL=시스템 폰트 컬렉션
DWRITE_FONT_WEIGHT_NORMAL, // 폰트 굵기. LIGHT, NORMAL, BOLD 등.
DWRITE_FONT_STYLE_NORMAL, // 폰트 스타일. NORMAL, OBLIQUE, ITALIC.
DWRITE_FONT_STRETCH_NORMAL, // 폰트 간격. CONDENSED, NORMAL, MEDIUM, EXTEXDED 등.
50.f, // 폰트 크기.
L"", // 로케일을 문자열로 명시. 영어-미국=L"en-us", 한국어-한국=L"ko-kr"
&pTextFormat
);
g_d2dTextFormatMap["DEFAULT"] = pTextFormat;
if (hr != S_OK)
vmlog::LogErr("D2D Device Setting Error!");
}
return true;
}
#ifdef THREAD_SAFE_CODE
static std::mutex queue_locker;
#endif
bool DoModule(fncontainer::VmFnContainer& _fncontainer)
{
#ifdef THREAD_SAFE_CODE
std::scoped_lock lock(queue_locker);
#endif
if(g_pCGpuManager == NULL)
{
return false;
}
if (g_vmCommonParams.dx11Device == NULL || g_vmCommonParams.dx11DeviceImmContext == NULL)
{
if (grd_helper::InitializePresettings(g_pCGpuManager, &g_vmCommonParams) == -1)
{
DeInitModule(fncontainer::VmFnContainer());
return false;
}
}
#ifdef DX11_3
if (g_vmCommonParams.dx11_featureLevel < 0xb100) {
_fncontainer.fnParams.SetParam("_bool_UseSpinLock", true);
}
#else
_fncontainer.fnParams.SetParam("_bool_UseSpinLock", true);
#endif
#ifdef DX10_0
_fncontainer.fnParams.SetParam("_int_OitMode", (int)MFR_MODE::DYNAMIC_FB);
#endif
g_LocalProgress.start = 0;
g_LocalProgress.range = 100;
g_LocalProgress.progress_ptr = &g_dProgress;
VmIObject* iobj = _fncontainer.fnParams.GetParam("_VmIObject*_RenderOut", (VmIObject*)NULL);
if(iobj == NULL)
{
VMERRORMESSAGE("VisMotive Renderer needs at least one IObject as output!");
return false;
}
if(iobj->GetCameraObject() == NULL)
{
VMERRORMESSAGE("VisMotive Renderer needs Camera Initializeation!");
return false;
}
EnterCriticalSection(&cs);
#pragma region GPU/CPU Pre Setting
//float sizeGpuResourceForVolume = _fncontainer.fnParams.GetParam("_float_SizeGpuResourceForVolume", 80.0f);
//// 100 means 50%
//float resourceRatioForVolume = sizeGpuResourceForVolume * 0.5f * 0.01f;
//uint uiDedicatedGpuMemoryKB =
// (uint)(g_vmCommonParams.dx11_adapter.DedicatedVideoMemory / 1024);
//float halfCriterionKB = (float)uiDedicatedGpuMemoryKB * resourceRatioForVolume;
//float halfCriterionKB = _fncontainer.fnParams.GetParam("_float_GpuVolumeMaxSizeKB", 256.f * 1024.f);
// In CPU VR mode, Recommend to set dHalfCriterionKB = 16;
//vector<VmObject*> vtrInputVolumes;
//_fncontainer.GetVmObjectList(&vtrInputVolumes, VmObjKey(ObjectTypeVOLUME, true));
//for (int i = 0; i < (int)vtrInputVolumes.size(); i++)
// vtrInputVolumes.at(i)->RegisterCustomParameter("_float_ForcedHalfCriterionKB", halfCriterionKB);
#pragma endregion
float maxTextureResoulution = _fncontainer.fnParams.GetParam("_float_MaxTextureResoulution", 2048.f);
float maxVolumeSizeKB = _fncontainer.fnParams.GetParam("_float_MaxVolumeSizeKB", 1024.f * 1024.f);
grd_helper::SetUserCapacity(maxVolumeSizeKB, maxTextureResoulution);
bool is_shadow = _fncontainer.fnParams.GetParam("_bool_IsShadow", false);
bool curved_slicer = _fncontainer.fnParams.GetParam("_bool_IsNonlinear", false);
string strRendererSource = _fncontainer.fnParams.GetParam("_string_RenderingSourceType", string("MESH"));
bool is_sectional = strRendererSource == "SECTIONAL_MESH" || strRendererSource == "SECTIONAL_VOLUME";
bool is_final_render_out = false;
float planeThickness = _fncontainer.fnParams.GetParam("_float_PlaneThickness", -1.f);
bool is_picking_routine = _fncontainer.fnParams.GetParam("_bool_IsPickingRoutine", false);
bool is_first_renderer = _fncontainer.fnParams.GetParam("_bool_IsFirstRenderer", true);
bool is_last_renderer = _fncontainer.fnParams.GetParam("_bool_IsFinalRenderer", true);
g_vmCommonParams.gpu_profile = _fncontainer.fnParams.GetParam("_bool_GpuProfile", false);
map<string, vmint2>& profile_map = g_vmCommonParams.profile_map;
if (g_vmCommonParams.gpu_profile)
{
int gpu_profilecount = (int)profile_map.size();
g_vmCommonParams.dx11DeviceImmContext->Begin(g_vmCommonParams.dx11qr_disjoint);
//gpu_profilecount++;
}
if (!is_sectional && is_shadow) {
// to do //
}
if (strRendererSource == "SECTIONAL_VOLUME" && !curved_slicer)
strRendererSource = "VOLUME";
bool is_vr = false;
const bool gpu_query_synch = false;
if (is_first_renderer && !is_picking_routine && gpu_query_synch)
{
g_vmCommonParams.dx11DeviceImmContext->Begin(g_vmCommonParams.dx11qr_disjoint);
//g_vmCommonParams.dx11DeviceImmContext->End(g_vmCommonParams.dx11qr_timestamps[0]);
}
if (strRendererSource == "VOLUME")
{
double dRuntime = 0;
uint32_t vrSlot = _fncontainer.fnParams.GetParam("DVR_CUSTOM_SLOT", 0u);
switch (vrSlot)
{
case 1:
RenderVrDLS1(&_fncontainer, g_pCGpuManager, &g_vmCommonParams, &g_LocalProgress, &dRuntime);
break;
case 2:
RenderVrDLS2(&_fncontainer, g_pCGpuManager, &g_vmCommonParams, &g_LocalProgress, &dRuntime);
break;
default:
case 0:
RenderVrDLS(&_fncontainer, g_pCGpuManager, &g_vmCommonParams, &g_LocalProgress, &dRuntime);
break;
}
g_dRunTimeVRs += dRuntime;
is_final_render_out = true;
is_vr = true;
//g_state = 1;
}
else if (strRendererSource == "MESH")
{
//g_state = 0;
double dRuntime = 0;
RenderPrimitives(&_fncontainer, g_pCGpuManager, &g_vmCommonParams, &g_LocalProgress, &dRuntime);
g_dRunTimeVRs += dRuntime;
if (is_last_renderer) is_final_render_out = true;
}
else if (strRendererSource == "SECTIONAL_VOLUME")
{
assert(curved_slicer == true);
double dRuntime = 0;
RenderVrCurvedSlicer(&_fncontainer, g_pCGpuManager, &g_vmCommonParams, &g_LocalProgress, &dRuntime);
g_dRunTimeVRs += dRuntime;
is_final_render_out = true;
is_vr = true;
}
else if (strRendererSource == "SECTIONAL_MESH")
{
double dRuntime = 0;
RenderSrSlicer(&_fncontainer, g_pCGpuManager, &g_vmCommonParams, &g_LocalProgress, &dRuntime);
g_dRunTimeVRs += dRuntime;
if (is_last_renderer || planeThickness <= 0.f) is_final_render_out = true;
}
auto RenderOut = [&iobj, &is_last_renderer, &planeThickness, &_fncontainer, &is_vr]() {
g_vmCommonParams.GpuProfile("Copyback");
VmGpuManager* gpu_manager = g_pCGpuManager;
__ID3D11Device* dx11Device = g_vmCommonParams.dx11Device;
__ID3D11DeviceContext* dx11DeviceImmContext = g_vmCommonParams.dx11DeviceImmContext;
HWND hWnd = (HWND)_fncontainer.fnParams.GetParam("_hwnd_WindowHandle", (HWND)NULL);
bool is_rgba = _fncontainer.fnParams.GetParam("_bool_IsRGBA", false);
vmint2 fb_size_cur;
iobj->GetFrameBufferInfo(&fb_size_cur);
int count_call_render = iobj->GetObjParam("_int_NumCallRenders", 0);
GpuRes gres_fb_rgba, gres_fb_depthcs;
GpuRes gres_fb_sys_rgba, gres_fb_sys_depthcs;
#ifdef DX10_0
grd_helper::UpdateFrameBuffer(gres_fb_rgba, iobj, is_vr? "RENDER_OUT_RGBA_1" : "RENDER_OUT_RGBA_0", RTYPE_TEXTURE2D,
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE, DXGI_FORMAT_R8G8B8A8_UNORM, 0);
grd_helper::UpdateFrameBuffer(gres_fb_depthcs, iobj, is_vr? "RENDER_OUT_DEPTH_1" : "RENDER_OUT_DEPTH_0", RTYPE_TEXTURE2D,
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE, DXGI_FORMAT_R32_FLOAT, 0);
#else
grd_helper::UpdateFrameBuffer(gres_fb_rgba, iobj, "RENDER_OUT_RGBA_0", RTYPE_TEXTURE2D,
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS, DXGI_FORMAT_R8G8B8A8_UNORM, 0);
grd_helper::UpdateFrameBuffer(gres_fb_depthcs, iobj, "RENDER_OUT_DEPTH_0", RTYPE_TEXTURE2D,
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS, DXGI_FORMAT_R32_FLOAT, 0);
#endif
grd_helper::UpdateFrameBuffer(gres_fb_sys_rgba, iobj, "SYSTEM_OUT_RGBA", RTYPE_TEXTURE2D, NULL, DXGI_FORMAT_R8G8B8A8_UNORM, UPFB_SYSOUT);
grd_helper::UpdateFrameBuffer(gres_fb_sys_depthcs, iobj, "SYSTEM_OUT_DEPTH", RTYPE_TEXTURE2D, NULL, DXGI_FORMAT_R32_FLOAT, UPFB_SYSOUT);
if (hWnd && is_last_renderer)
{
// APPLY HWND MODE
ID3D11Texture2D* pTex2dHwndRT = NULL;
ID3D11RenderTargetView* pHwndRTV = NULL;
gpu_manager->UpdateDXGI((void**)&pTex2dHwndRT, (void**)&pHwndRTV, hWnd, fb_size_cur.x, fb_size_cur.y);
dx11DeviceImmContext->CopyResource(pTex2dHwndRT, (ID3D11Texture2D*)gres_fb_rgba.alloc_res_ptrs[DTYPE_RES]);
}
else {
if (hWnd) gpu_manager->ReleaseDXGI(hWnd);
int outIndex = planeThickness == 0.f && !is_last_renderer ? 1 : 0; // just for SlicerSR combined with CPU MPR
FrameBuffer* fb_rout = (FrameBuffer*)iobj->GetFrameBuffer(FrameBufferUsageRENDEROUT, outIndex);
FrameBuffer* fb_dout = (FrameBuffer*)iobj->GetFrameBuffer(FrameBufferUsageDEPTH, 0);
if (count_call_render == 0) // this means that there is no valid rendering pass
{
vmbyte4* rgba_buf = (vmbyte4*)fb_rout->fbuffer;
float* depth_buf = (float*)fb_dout->fbuffer;
memset(rgba_buf, 0, fb_size_cur.x * fb_size_cur.y * sizeof(vmbyte4));
memset(depth_buf, 0x77, fb_size_cur.x * fb_size_cur.y * sizeof(float));
}
else
{
#pragma region // Copy GPU to CPU
dx11DeviceImmContext->CopyResource((ID3D11Texture2D*)gres_fb_sys_rgba.alloc_res_ptrs[DTYPE_RES],
(ID3D11Texture2D*)gres_fb_rgba.alloc_res_ptrs[DTYPE_RES]);
dx11DeviceImmContext->CopyResource((ID3D11Texture2D*)gres_fb_sys_depthcs.alloc_res_ptrs[DTYPE_RES],
(ID3D11Texture2D*)gres_fb_depthcs.alloc_res_ptrs[DTYPE_RES]);
vmbyte4* rgba_sys_buf = (vmbyte4*)fb_rout->fbuffer;
float* depth_sys_buf = (float*)fb_dout->fbuffer;
D3D11_MAPPED_SUBRESOURCE mappedResSysRGBA;
D3D11_MAPPED_SUBRESOURCE mappedResSysDepth;
HRESULT hr = dx11DeviceImmContext->Map((ID3D11Texture2D*)gres_fb_sys_rgba.alloc_res_ptrs[DTYPE_RES], 0, D3D11_MAP_READ, NULL, &mappedResSysRGBA);
hr |= dx11DeviceImmContext->Map((ID3D11Texture2D*)gres_fb_sys_depthcs.alloc_res_ptrs[DTYPE_RES], 0, D3D11_MAP_READ, NULL, &mappedResSysDepth);
vmbyte4* rgba_gpu_buf = (vmbyte4*)mappedResSysRGBA.pData;
float* depth_gpu_buf = (float*)mappedResSysDepth.pData;
if (rgba_gpu_buf == NULL || depth_gpu_buf == NULL)
{
#ifdef __DX_DEBUG_QUERY
g_vmCommonParams.debug_info_queue->PushEmptyStorageFilter();
UINT64 message_count = g_vmCommonParams.debug_info_queue->GetNumStoredMessages();
for (UINT64 i = 0; i < message_count; i++) {
SIZE_T message_size = 0;
g_vmCommonParams.debug_info_queue->GetMessage(i, nullptr, &message_size); //get the size of the message
D3D11_MESSAGE* message = (D3D11_MESSAGE*)malloc(message_size); //allocate enough space
g_vmCommonParams.debug_info_queue->GetMessage(i, message, &message_size); //get the actual message
//do whatever you want to do with it
printf("Directx11: %.*s", message->DescriptionByteLength, message->pDescription);
free(message);
}
g_vmCommonParams.debug_info_queue->ClearStoredMessages();
#endif
cout << ("VR ERROR -- OUT") << endl;
cout << ("screen : " + to_string(fb_size_cur.x) + " x " + to_string(fb_size_cur.y)) << endl;
//cout << ("v_thickness : " + to_string(v_thickness)) <<endl;
//cout << ("k_value : " + to_string(k_value)) <<endl;
//cout << ("grid width and height : " + to_string(num_grid_x) + " x " + to_string(num_grid_y))<<endl;
}
int buf_row_pitch = mappedResSysRGBA.RowPitch / 4;
//is_rgba = is_rgba | !is_final_renderer;
#ifdef PPL_USE
int count = fb_size_cur.y;
parallel_for(int(0), count, [&](int i) // is_rgba, fb_size_cur, rgba_sys_buf, depth_sys_buf, rgba_gpu_buf, depth_gpu_buf, buf_row_pitch
#else
#pragma omp parallel for
for (int i = 0; i < fb_size_cur.y; i++)
#endif
{
for (int j = 0; j < fb_size_cur.x; j++)
{
vmbyte4 rgba = rgba_gpu_buf[j + i * buf_row_pitch];
// __PS_MERGE_LAYERS_TO_RENDEROUT 에서 INT -> FLOAT4 로 되어 배열된 color 요소가 들어 옴. //
// BGRA
if (is_rgba)
rgba_sys_buf[i * fb_size_cur.x + j] = vmbyte4(rgba.x, rgba.y, rgba.z, rgba.w);
else
rgba_sys_buf[i * fb_size_cur.x + j] = vmbyte4(rgba.z, rgba.y, rgba.x, rgba.w);
int iAddr = i * fb_size_cur.x + j;
if (rgba.w > 0)
depth_sys_buf[iAddr] = depth_gpu_buf[j + i * buf_row_pitch];
else
depth_sys_buf[iAddr] = FLT_MAX;
}
#ifdef PPL_USE
});
#else
}
#endif
dx11DeviceImmContext->Unmap((ID3D11Texture2D*)gres_fb_sys_rgba.alloc_res_ptrs[DTYPE_RES], 0);
dx11DeviceImmContext->Unmap((ID3D11Texture2D*)gres_fb_sys_depthcs.alloc_res_ptrs[DTYPE_RES], 0);
}
}
g_vmCommonParams.GpuProfile("Copyback", true);
};
if (is_final_render_out && !is_picking_routine)
{
const uint rtbind = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
GpuRes gres_fb_rgba;
grd_helper::UpdateFrameBuffer(gres_fb_rgba, iobj, "RENDER_OUT_RGBA_0", RTYPE_TEXTURE2D, rtbind, DXGI_FORMAT_R8G8B8A8_UNORM, 0);
#ifndef DX10_0
// here, compose the 2nd layer rendering target texture onto the final render out texture...
auto Blend2ndLayer = [&gres_fb_rgba, &iobj, &_fncontainer, &rtbind]() {
vmint2 fb_size_cur;
iobj->GetFrameBufferInfo(&fb_size_cur);
GpuRes gres_fb_depthcs;
grd_helper::UpdateFrameBuffer(gres_fb_depthcs, iobj, "RENDER_OUT_DEPTH_0", RTYPE_TEXTURE2D, rtbind, DXGI_FORMAT_R32_FLOAT, 0);
GpuRes gres_fb_2ndlayer_rgba, gres_fb_2ndlayer_depthcs;
grd_helper::UpdateFrameBuffer(gres_fb_2ndlayer_rgba, iobj, "RENDER_OUT_RGBA_1", RTYPE_TEXTURE2D, rtbind, DXGI_FORMAT_R8G8B8A8_UNORM, 0);
grd_helper::UpdateFrameBuffer(gres_fb_2ndlayer_depthcs, iobj, "RENDER_OUT_DEPTH_2", RTYPE_TEXTURE2D, rtbind, DXGI_FORMAT_R8G8B8A8_UNORM, 0);
ID3D11UnorderedAccessView* dx11UAVs[4] = {
(ID3D11UnorderedAccessView*)gres_fb_rgba.alloc_res_ptrs[DTYPE_UAV]
, (ID3D11UnorderedAccessView*)gres_fb_depthcs.alloc_res_ptrs[DTYPE_UAV]
, (ID3D11UnorderedAccessView*)gres_fb_2ndlayer_rgba.alloc_res_ptrs[DTYPE_UAV]
, (ID3D11UnorderedAccessView*)gres_fb_2ndlayer_depthcs.alloc_res_ptrs[DTYPE_UAV]
};
__ID3D11Device* dx11Device = g_vmCommonParams.dx11Device;
__ID3D11DeviceContext* dx11DeviceImmContext = g_vmCommonParams.dx11DeviceImmContext;
int dotSize = _fncontainer.fnParams.GetParam("_int_2ndLayerPatternInterval", (int)3);
float blendingW = _fncontainer.fnParams.GetParam("_float_2ndLayerBlendingW", 0.2f);
ID3D11Buffer* cbuf_cam_state = g_vmCommonParams.get_cbuf("CB_CameraState");
D3D11_MAPPED_SUBRESOURCE mappedResCamState;
dx11DeviceImmContext->Map(cbuf_cam_state, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResCamState);
CB_CameraState* cbCamStateData = (CB_CameraState*)mappedResCamState.pData;
cbCamStateData->rt_width = fb_size_cur.x;
cbCamStateData->rt_height = fb_size_cur.y;
cbCamStateData->iSrCamDummy__0 = dotSize | ((int)(blendingW * 100) << 8);
dx11DeviceImmContext->Unmap(cbuf_cam_state, 0);
dx11DeviceImmContext->CSSetConstantBuffers(0, 1, &cbuf_cam_state);
ID3D11ComputeShader* dx11CShader = g_vmCommonParams.get_cshader("CS_Blend2ndLayer_cs_5_0");
dx11DeviceImmContext->CSSetShader(dx11CShader, NULL, 0);
dx11DeviceImmContext->CSSetUnorderedAccessViews(1, 4, dx11UAVs, (UINT*)(&dx11UAVs));
int __BLOCKSIZE = _fncontainer.fnParams.GetParam("_int_GpuThreadBlockSize", (int)4);
uint num_grid_x = (uint)ceil(fb_size_cur.x / (float)__BLOCKSIZE);
uint num_grid_y = (uint)ceil(fb_size_cur.y / (float)__BLOCKSIZE);
dx11DeviceImmContext->Dispatch(num_grid_x, num_grid_y, 1);
// Set NULL States //
ID3D11UnorderedAccessView* dx11UAVs_NULL[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
dx11DeviceImmContext->CSSetUnorderedAccessViews(1, 4, dx11UAVs_NULL, (UINT*)(&dx11UAVs_NULL));
};
if (iobj->GetObjParam("NUM_SECOND_LAYER_OBJECTS", (int)0) > 0) {
Blend2ndLayer();
}
#endif
ID3D11Texture2D* rtTex2D = (ID3D11Texture2D*)gres_fb_rgba.alloc_res_ptrs[DTYPE_RES];
// check release or not
D2DRes* res2d = NULL;
bool reGenRes = false;
auto it = g_d2dResMap.find(iobj->GetObjectID());
if (it == g_d2dResMap.end()) {
g_d2dResMap[iobj->GetObjectID()] = D2DRes();
res2d = &g_d2dResMap[iobj->GetObjectID()];
reGenRes = true;
}
else {
res2d = &it->second;
if (res2d->pTex2DRT != rtTex2D) {
res2d->ReleaseD2DRes(); // clean also shared res views
reGenRes = true;
}
}
if (reGenRes) {
// https://learn.microsoft.com/en-us/windows/win32/direct2d/direct2d-quickstart
// https://learn.microsoft.com/en-us/windows/win32/direct2d/direct2d-and-direct3d-interoperation-overview
IDXGISurface* pDxgiSurface = NULL;
HRESULT hr = rtTex2D->QueryInterface(&pDxgiSurface);
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED), 0, 0);
ID2D1RenderTarget* pRenderTarget = NULL;
hr = g_pDirect2dFactory->CreateDxgiSurfaceRenderTarget(pDxgiSurface, &props, &pRenderTarget);
//D2D1_BITMAP_PROPERTIES1 bitmapProperties =
// D2D1::BitmapProperties1(
// D2D1_BITMAP_OPTIONS_TARGET,
// D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE),
// 0,
// 0
// );
//ID2D1Bitmap1* pRenderTarget = NULL;
//hr = g_d2dDeviceContext->CreateBitmapFromDxgiSurface(pDxgiSurface, &bitmapProperties, &pRenderTarget);
ID2D1SolidColorBrush* pSolidBrush = NULL;
hr = pRenderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::White),
&pSolidBrush
);
res2d->pTex2DRT = rtTex2D;
res2d->pDxgiSurface = pDxgiSurface;
res2d->pRenderTarget = pRenderTarget;
res2d->pSolidBrush = pSolidBrush;
}
// drawing //
// Now we can set the Direct2D render target.
//g_d2dDeviceContext->SetTarget(res2d->pRenderTarget);
res2d->pRenderTarget->BeginDraw();
res2d->pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
if (0)
{
res2d->pRenderTarget->DrawLine(
D2D1::Point2F(100.f, 100.f),
D2D1::Point2F(500.f, 500.f),
res2d->pSolidBrush,
1.5f, g_d2dStrokeStyleMap["DEFAULT"]
);
}
vector<TextItem>* textItems = (vector<TextItem>*)_fncontainer.fnParams.GetParam("_vector<TextItem>*_TextItems", (void*)NULL);
if (textItems) {
// IDWriteTextFormat 객체 생성.
// https://learn.microsoft.com/en-us/windows/win32/Direct2D/how-to--draw-text
D2D1_SIZE_F rSize = res2d->pRenderTarget->GetSize();
IDWriteTextFormat* textFormat = g_d2dTextFormatMap["DEFAULT"];
vector<TextItem>& _textItems = *textItems;
for (TextItem& titem : _textItems) {
std::wstring fontName_w;
fontName_w.assign(titem.font.begin(), titem.font.end());
std::wstring text_w;
text_w.assign(titem.textStr.begin(), titem.textStr.end());
IDWriteTextFormat* pDynamicTextFormat = nullptr;
if (g_pDWriteFactory->CreateTextFormat(
fontName_w.c_str(), nullptr,
(DWRITE_FONT_WEIGHT)(titem.fontWeight * 100),
titem.isItalic? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL, titem.fontSize, L"", &pDynamicTextFormat) != S_OK)
continue;
if (titem.alignment == "CENTER") {
pDynamicTextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
}
else if (titem.alignment == "RIGHT") {
pDynamicTextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_TRAILING);
}
res2d->pSolidBrush->SetColor(D2D1::ColorF(titem.iColor, titem.alpha)); // D2D1::ColorF::Black
const D2D1_RECT_F rectangle1 = D2D1::RectF(titem.posScreenX, titem.posScreenY, rSize.width, rSize.height);
res2d->pRenderTarget->DrawText(
text_w.c_str(),
text_w.length(),
pDynamicTextFormat,
&rectangle1,
res2d->pSolidBrush
);
pDynamicTextFormat->Release();
}
}
//= D2D1::RectF(0, 0, rSize.width, rSize.height);
if (res2d->pRenderTarget->EndDraw() != S_OK)
vmlog::LogErr("D2D Draw Error!");
// https://learn.microsoft.com/en-us/windows/win32/direct2d/path-geometries-overview
/*
if (pvtrLineList)
{
int num_lines = ((int)pvtrLineList->size() - prev_lines_count) / 4;
if (num_lines) {
vtrLineListObjColor.push_back(vmfloat3(actor->color));
vtrLineListObjNum.push_back(num_lines);
vtrLineListObjID.push_back(pobj->GetObjectID());
prev_lines_count = (int)pvtrLineList->size();
}
}
/**/
bool isSkipSysFbBupdate = _fncontainer.fnParams.GetParam("_bool_SkipSysFBUpdate", false);
if (!isSkipSysFbBupdate) RenderOut();
}
if (is_last_renderer && !is_picking_routine && gpu_query_synch)
{
//g_vmCommonParams.dx11DeviceImmContext->End(g_vmCommonParams.dx11qr_timestamps[1]);
g_vmCommonParams.dx11DeviceImmContext->End(g_vmCommonParams.dx11qr_disjoint);
// Wait for data to be available
static int testConsoleOut = 0;
testConsoleOut++;
int sleepCount = 0;
while (g_vmCommonParams.dx11DeviceImmContext->GetData(g_vmCommonParams.dx11qr_disjoint, NULL, 0, 0) == S_FALSE)
{
Sleep(1); // Wait a bit, but give other threads a chance to run
//sleepCount++;
}
if (testConsoleOut % 100 == 0)
{
//vmlog::LogInfo("Sleep Count : " + std::to_string(sleepCount));
// Check whether timestamps were disjoint during the last frame
D3D10_QUERY_DATA_TIMESTAMP_DISJOINT tsDisjoint;
g_vmCommonParams.dx11DeviceImmContext->GetData(g_vmCommonParams.dx11qr_disjoint, &tsDisjoint, sizeof(tsDisjoint), 0);
if (!tsDisjoint.Disjoint)
{
/*
auto DisplayDuration = [&tsDisjoint](UINT64 tsS, UINT64 tsE, const string& _test)
{
if (tsS == 0 || tsE == 0) return;
cout << _test << " : " << float(tsE - tsS) / float(tsDisjoint.Frequency) * 1000.0f << " ms" << endl;
cout << _test << "ts : " << tsS << endl;
cout << _test << "te : " << tsE << endl;
};
UINT64 ts, te;
while (g_vmCommonParams.dx11DeviceImmContext->GetData(g_vmCommonParams.dx11qr_timestamps[0], &ts, sizeof(UINT64), 0) == S_FALSE)
{
Sleep(1); // Wait a bit, but give other threads a chance to run
sleepCount++;
}
while (g_vmCommonParams.dx11DeviceImmContext->GetData(g_vmCommonParams.dx11qr_timestamps[1], &te, sizeof(UINT64), 0) == S_FALSE)
{
Sleep(1); // Wait a bit, but give other threads a chance dx11qr_timestamps[0]to run
sleepCount++;
}
vmlog::LogInfo("Sleep Count2 : " + std::to_string(sleepCount));
//g_vmCommonParams.dx11DeviceImmContext->GetData(g_vmCommonParams.dx11qr_timestamps[0], &ts, sizeof(UINT64), 0);
//g_vmCommonParams.dx11DeviceImmContext->GetData(g_vmCommonParams.dx11qr_timestamps[1], &te, sizeof(UINT64), 0);
DisplayDuration(ts, te, "TIME : ");
/**/
}
else {
vmlog::LogErr("ERROR for QUERY-based SYNCH");
}
}
}
g_dProgress = 100;
if (g_vmCommonParams.gpu_profile)
{
g_vmCommonParams.dx11DeviceImmContext->End(g_vmCommonParams.dx11qr_disjoint);
// Wait for data to be available
while (g_vmCommonParams.dx11DeviceImmContext->GetData(g_vmCommonParams.dx11qr_disjoint, NULL, 0, 0) == S_FALSE)
{
Sleep(1); // Wait a bit, but give other threads a chance to run
}
// Check whether timestamps were disjoint during the last frame
D3D10_QUERY_DATA_TIMESTAMP_DISJOINT tsDisjoint;
g_vmCommonParams.dx11DeviceImmContext->GetData(g_vmCommonParams.dx11qr_disjoint, &tsDisjoint, sizeof(tsDisjoint), 0);
if (!tsDisjoint.Disjoint)
{
auto DisplayDuration = [&tsDisjoint](UINT64 tsS, UINT64 tsE, const string& _test)
{
if (tsS == 0 || tsE == 0) return;
cout << _test << " : " << float(tsE - tsS) / float(tsDisjoint.Frequency) * 1000.0f << " ms" << endl;
};
for (auto& it : profile_map) {
UINT64 ts, te;
g_vmCommonParams.dx11DeviceImmContext->GetData(g_vmCommonParams.dx11qr_timestamps[it.second.x], &ts, sizeof(UINT64), 0);
g_vmCommonParams.dx11DeviceImmContext->GetData(g_vmCommonParams.dx11qr_timestamps[it.second.y], &te, sizeof(UINT64), 0);
DisplayDuration(ts, te, it.first);
}
//if (test_fps_profiling)
//{
// auto it = profile_map.find("SR Render");
// UINT64 ts, te;
// dx11DeviceImmContext->GetData(dx11CommonParams->dx11qr_timestamps[it->second.x], &ts, sizeof(UINT64), 0);
// dx11DeviceImmContext->GetData(dx11CommonParams->dx11qr_timestamps[it->second.y], &te, sizeof(UINT64), 0);
// ofstream file_rendertime;
// file_rendertime.open(".\\data\\frames_profile_rendertime.txt", std::ios_base::app);
// file_rendertime << float(te - ts) / float(tsDisjoint.Frequency) * 1000.0f << endl;
// file_rendertime.close();
//}
}
}
LeaveCriticalSection(&cs);
return true;
}
void DeInitModule(fncontainer::VmFnContainer& _fncontainer)
{
DeleteCriticalSection(&cs);
for (auto it = g_d2dResMap.begin(); it != g_d2dResMap.end(); it++) {
it->second.ReleaseD2DRes();
}
g_d2dResMap.clear();
for (auto it = g_d2dStrokeStyleMap.begin(); it != g_d2dStrokeStyleMap.end(); it++) {
VMSAFE_RELEASE(it->second);
}
g_d2dStrokeStyleMap.clear();
for (auto it = g_d2dTextFormatMap.begin(); it != g_d2dTextFormatMap.end(); it++) {
VMSAFE_RELEASE(it->second);
}
g_d2dTextFormatMap.clear();
VMSAFE_RELEASE(g_pDWriteFactory);
VMSAFE_RELEASE(g_pDirect2dFactory);
// ORDER!!
grd_helper::DeinitializePresettings();
VMSAFE_DELETE(g_pCGpuManager);
}
int GetProgress(std::string& progressTag)
{
return ((int)g_dProgress % 100);
}
void GetModuleSpecification(std::vector<std::string>& requirements)
{
requirements.push_back("DX11");
requirements.push_back("WINDOWS");
requirements.push_back("GPUMANAGER");
}
void InteropCustomWork(fncontainer::VmFnContainer& _fncontainer)
{
if (g_pCGpuManager == NULL) // for module initialization
g_pCGpuManager = new VmGpuManager(GpuSdkTypeDX11, __DLLNAME);
_fncontainer.fnParams.SetParam("_string_CoreVersion", string(__VERSION));
_fncontainer.fnParams.SetParam("_VmGpuManager*_", g_pCGpuManager);
}
bool GetSharedShaderResView(const int iobjId, const void* dx11devPtr, void** sharedSRV)
{
//if (g_state != 1)
// assert(0);
*sharedSRV = nullptr;
if (g_pCGpuManager == NULL) {
vmlog::LogErr("No GPU Manager is assigned!");
return false;
}
GpuRes gres_fb;
gres_fb.vm_src_id = iobjId;
gres_fb.res_name = "RENDER_OUT_RGBA_0";
if (!g_pCGpuManager->UpdateGpuResource(gres_fb)) {
vmlog::LogErr("No GPU Rendertarget is assigned! (" + std::to_string(iobjId) + ")");
return false;
}
ID3D11Texture2D* rtTex2D = (ID3D11Texture2D*)gres_fb.alloc_res_ptrs[DTYPE_RES];
ID3D11ShaderResourceView** ppsharedSRV = NULL;
D2DRes* res2d = NULL;
auto it = g_d2dResMap.find(iobjId);
if (it == g_d2dResMap.end()) {
vmlog::LogErr("No GPU Rendering is called! (" + std::to_string(iobjId) + ")");
return false;
}
else {
res2d = &it->second;
auto it = res2d->mapDevSharedSRVs.find((void*)dx11devPtr);
if (it != res2d->mapDevSharedSRVs.end()) {
*sharedSRV = it->second;
return true;
}
ppsharedSRV = &res2d->mapDevSharedSRVs[(void*)dx11devPtr];
}
// QI IDXGIResource interface to synchronized shared surface.
IDXGIResource* pDXGIResource = NULL;
rtTex2D->QueryInterface(__uuidof(IDXGIResource), (LPVOID*)&pDXGIResource);
// obtain handle to IDXGIResource object.
HANDLE sharedHandle;
// this code snippet is only for dx11.0
// for dx11.1 or higher, refer to
// https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgiresource-getsharedhandle
pDXGIResource->GetSharedHandle(&sharedHandle);
pDXGIResource->Release();
ID3D11Device* pdx11AnotherDev = (ID3D11Device*)dx11devPtr;
if (sharedHandle == NULL) {
vmlog::LogErr("Not Allowed for Shared Resource! (" + std::to_string(iobjId) + ")");
return false;
}
ID3D11Texture2D* rtTex2;
pdx11AnotherDev->OpenSharedResource(sharedHandle, __uuidof(ID3D11Texture2D), (LPVOID*)&rtTex2);
// Create texture view
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
ZeroMemory(&srvDesc, sizeof(srvDesc));
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = 1;
srvDesc.Texture2D.MostDetailedMip = 0;
pdx11AnotherDev->CreateShaderResourceView(rtTex2, &srvDesc, ppsharedSRV);
rtTex2->Release();
*sharedSRV = *ppsharedSRV;
return true;
}
bool GetRendererDevice(
vmobjects::VmParamMap<std::string, std::any>& ioResObjs,
vmobjects::VmParamMap<std::string, std::any>& ioActors,
vmobjects::VmParamMap<std::string, std::any>& ioParams)
{
if (g_pCGpuManager == NULL) {
vmlog::LogErr("No GPU Manager is assigned!");
return false;
}
__ID3D11Device* device;
#ifdef DX11_3
g_pCGpuManager->GetDeviceInformation((void*)(&device), "DEVICE_POINTER_3");
#else
g_pCGpuManager->GetDeviceInformation((void*)(&device), "DEVICE_POINTER");
#endif
ioParams.SetParam("DirectX11Device", (void*)device);
return true;
}