-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path012f47c1-31bc-4255-bc90-21c84cbd0eb6.mop
821 lines (821 loc) · 42.8 KB
/
012f47c1-31bc-4255-bc90-21c84cbd0eb6.mop
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
{
"Name": "EnvIS",
"_id": "012f47c1-31bc-4255-bc90-21c84cbd0eb6",
"Namespace": "user.cynic",
"Description": "environment imporance sampling test",
"Inputs": [
{
"Name": "Code",
"MetaInstanceID": "cb9db90d-2a46-4748-af34-9527917b1f02",
"DefaultValue": {
"Type": "Text",
"Value": [
"\r",
"//>>> SumRows\r",
"Texture2D inputImage : register (t0);\r",
"RWTexture2D<float> summedRows : register (u0);\r",
"\r",
"\r",
"[numthreads(1, 128, 1)]\r",
"void ClearSumBuffer(uint3 threadID : SV_DispatchThreadID)\r",
"{\r",
" summedRows[threadID.xy] = 0;\r",
"}\r",
"\r",
"[numthreads(1, 128, 1)]\r",
"void SumRows(uint3 threadID : SV_DispatchThreadID)\r",
"{\r",
" float sum = 0;\r",
" uint width, height;\r",
" inputImage.GetDimensions(width, height);\r",
"\r",
" // first get sum of row\r",
" for (uint x = 0; x < width; ++x)\r",
" {\r",
" sum += inputImage[uint2(x, threadID.y)].r;\r",
" }\r",
" summedRows[uint2(width + 1, threadID.y)] = sum;\r",
" \r",
" float sumReciproc = (sum == 0.0f) ? 0 : 1.0f/sum;\r",
" float summedUp = 0;\r",
" // now sum up, scale by overall sum and store\r",
" for (uint x2 = 0; x2 < width; ++x2)\r",
" {\r",
" summedUp += inputImage[uint2(x2, threadID.y)].r*sumReciproc;\r",
" summedRows[uint2(x2, threadID.y)].r = summedUp;\r",
" }\r",
" summedRows[uint2(width, threadID.y)] = 1.0f;\r}\r",
"//<<< SumRows\r",
"\r",
"//>>> SumColumn\r",
"Texture2D inputSummedRows : register (t1);\r",
"RWTexture2D<float> summedColumn : register (u1);\r",
"\r",
"[numthreads(1, 1, 1)]\r",
"void SumColumn(uint3 threadID : SV_DispatchThreadID)\r",
"{\r",
" float sum = 0;\r",
" uint width, height;\r",
" inputSummedRows.GetDimensions(width, height);\r",
"\r",
" // first get sum of row\r",
" for (uint y = 0; y < height; ++y)\r",
" {\r",
" sum += inputSummedRows[uint2(width - 1, y)].r;\r",
" }\r",
" summedColumn[int2(0, height + 1)] = sum;\r",
" \r",
" float sumReciproc = (sum == 0.0f) ? 0 : 1.0f/sum;\r",
" float summedUp = 0;\r",
" // now sum up, scale by overall sum and store\r",
" for (uint y2 = 0; y2 < height; ++y2)\r",
" {\r",
" summedUp += inputSummedRows[uint2(width - 1, y2)].r*sumReciproc;\r",
" summedColumn[int2(0, y2)].r = summedUp;\r",
" }\r",
" summedColumn[int2(0, height)] = 1.0f;\r",
"}\r",
"//<<< SumColumn\r",
"\r",
"\r",
"//>>> GUVP\r",
"float radicalInverse_VdC(uint bits) \r",
"{\r",
" bits = (bits << 16u) | (bits >> 16u);\r",
" bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);\r",
" bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);\r",
" bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);\r",
" bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);\r",
" return float(bits) * 2.3283064365386963e-10; // / 0x100000000\r",
"}\r",
"\r",
"\r",
"float2 hammersley2d(uint i, uint N) \r",
"{\r",
" return float2(float(i)/float(N), radicalInverse_VdC(i));\r",
"}\r",
"\r",
"int NumSamples;\r",
"Texture2D<float> cdfRows : register (t2);\r",
"Texture2D<float> cdfColumn : register (t3);\r",
"RWByteAddressBuffer outputSampleBuffer : register (u2);\r",
"\r",
"[numthreads(256,1,1)]\r",
"void GenerateUVPositions(uint3 threadID : SV_DispatchThreadID)\r",
"{\r",
" uint rowWidth, columnHeight;\r",
" cdfRows.GetDimensions(rowWidth, columnHeight);\r",
" rowWidth -= 2; columnHeight -= 2;\r",
" float2 prob = hammersley2d(threadID.x, NumSamples);\r",
" // use prob.x to find pos in cdf column\r",
" uint index = columnHeight/2; \r",
" \r",
" uint left = 0;\r",
" uint right = columnHeight;\r",
" uint steps = log2(columnHeight) + 1;\r",
" for (uint j = 0; j < steps; ++j)\r",
" {\r",
" uint middle = left + (right - left)/2;\r",
" float cdfSegStart = cdfColumn[uint2(0, middle)].x;\r",
" float cdfSegEnd = cdfColumn[uint2(0, middle + 1)].x;\r",
" if (!((prob.x >= cdfSegStart) && (prob.x <= cdfSegEnd)))\r",
" {\r",
" if (prob.x < cdfSegStart)\r",
" {\r",
" right = middle;\r",
" }\r",
" else\r",
" {\r",
" left = middle + 1;\r",
" } \r",
" }\r",
" else\r",
" {\r",
" index = middle;\r",
" }\r",
" }\r",
" uint rowIndex = index;\r",
"\r",
" // now search cdf row for x index\r",
" left = 0;\r",
" right = rowWidth;\r",
" steps = log2(rowWidth) + 1;\r",
" for (uint i = 0; i < steps; ++i)\r",
" {\r",
" uint middle = left + (right - left)/2;\r",
" float cdfSegStart = cdfRows[uint2(middle, rowIndex)].x;\r",
" float cdfSegEnd = cdfRows[uint2(middle + 1, rowIndex)].x;\r",
" if (!((prob.y >= cdfSegStart) && (prob.y <= cdfSegEnd)))\r",
" {\r",
" if (prob.y < cdfSegStart)\r",
" {\r",
" right = middle;\r",
" }\r",
" else\r",
" {\r",
" left = middle + 1;\r",
" } \r",
" }\r",
" else\r",
" {\r",
" index = middle;\r",
" }\r",
" }\r",
" uint columnIndex = index;\r",
" \r",
" float2 samplePosInUV = float2(float(columnIndex)/float(columnHeight), float(rowIndex)/float(rowWidth));\r",
" index = threadID.x*8;\r",
" outputSampleBuffer.Store2(index, asint(samplePosInUV));\r",
"\r",
"}\r",
"//<<< GUVP\r",
"\r",
"//>>> GWP\r",
"ByteAddressBuffer inputSampleBuffer : register (t4);\r",
"Texture2D inputMeshPositions : register (t5);\r",
"Texture2D inputMeshNormals : register (t6);\r",
"RWByteAddressBuffer outputWorldPosBuffer : register (u3);\r",
"\r",
"[numthreads(256, 1, 1)]\r",
"void GenerateWorldPositions(uint3 threadID : SV_DispatchThreadID)\r",
"{\r",
" uint width, height;\r",
" inputMeshPositions.GetDimensions(width, height);\r",
"\r",
" uint inputIndex = threadID.x*8; // 2 floats\r",
" float2 pos = asfloat(inputSampleBuffer.Load2(inputIndex));\r",
"\r",
" float3 worldPos = inputMeshPositions[uint2(pos.x*width, (1.0f - pos.y)*height)].xyz;\r",
" float3 worldNormal = inputMeshNormals[uint2(pos.x*width, (1.0f - pos.y)*height)].xyz;\r",
" worldNormal = normalize(worldNormal);\r",
" \r",
" uint outputIndex = threadID.x*64; \r",
" \r",
" float3 up = abs(worldNormal.z) < 0.999 ? float3(0,0,1) : float3(1,0,0); \r",
" float3 xDir = normalize(cross(worldNormal.xyz, up));\r",
" outputWorldPosBuffer.Store4(outputIndex, asint(float4(xDir, 0)));\r",
" outputIndex += 16;\r",
"\r",
" float3 yDir = normalize(cross(worldNormal, xDir));\r",
" outputWorldPosBuffer.Store4(outputIndex, asint(float4(yDir, 0)));\r",
" outputIndex += 16;\r",
"\r",
" outputWorldPosBuffer.Store4(outputIndex, asint(float4(worldNormal, 0)));\r",
" outputIndex += 16;\r",
"\r",
" outputWorldPosBuffer.Store4(outputIndex, asint(float4(worldPos,1))); \r",
"}\r",
"//<<< GWP\r",
"\r",
"\r",
"//>>> VisuSums\r",
"RWTexture2D<float4> Output2 : register (u7);\r",
"\r",
"[numthreads(32, 32, 1)]\r",
"void ClearVisuBuffer(uint3 threadID : SV_DispatchThreadID)\r",
"{\r",
" Output2[threadID.xy] = float4(0,0,0,1);\r",
"}\r",
"\r",
"\r",
"[numthreads(256, 1, 1)]\r",
"void VisualizeSumBuffer(uint3 threadID : SV_DispatchThreadID)\r",
"{\r",
" uint width, height;\r",
" Output2.GetDimensions(width, height);\r",
"\r",
" uint index = threadID.x*8;\r",
" float2 pos = asfloat(inputSampleBuffer.Load2(index));\r",
" Output2[uint2(pos.x*width, pos.y*height)] = float4(0, 0.5, 0, 1);\r",
"}\r",
"//<<< VisuSums\r",
""
]
},
"MetaID": "c522a66e-3260-4692-b3e3-79fd0361fa3d",
"IsMultiInput": "False",
"Relevance": "Optional",
"Description": "",
"Min": "-100000",
"Max": "100000",
"Scale": "0.1",
"ScaleType": "Linear",
"EnumValues": []
},
{
"Name": "Intensity",
"MetaInstanceID": "34e49ad4-9921-4cbe-99fe-928474789538",
"DefaultValue": {
"Type": "Image",
"Value": "Framefield.Core.Image"
},
"MetaID": "9848060d-fd84-45b0-b658-d0d531c61dab",
"IsMultiInput": "False",
"Relevance": "Required",
"Description": "",
"Min": "-100000",
"Max": "100000",
"Scale": "0.1",
"ScaleType": "Linear",
"EnumValues": []
},
{
"Name": "NumSamples",
"MetaInstanceID": "dd1e70fb-0b60-44f1-a517-8d5bf18b97a8",
"DefaultValue": {
"Type": "Float",
"Value": "1000"
},
"MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
"IsMultiInput": "False",
"Relevance": "Optional",
"Description": "",
"Min": "1",
"Max": "1E+07",
"Scale": "1",
"ScaleType": "Logarithmic",
"EnumValues": []
},
{
"Name": "MeshPositions",
"MetaInstanceID": "0bb0032f-7f01-4ce1-82ea-74756546c9ea",
"DefaultValue": {
"Type": "Image",
"Value": "Framefield.Core.Image"
},
"MetaID": "9848060d-fd84-45b0-b658-d0d531c61dab",
"IsMultiInput": "False",
"Relevance": "Required",
"Description": "",
"Min": "-100000",
"Max": "100000",
"Scale": "0.1",
"ScaleType": "Linear",
"EnumValues": []
},
{
"Name": "MeshNormals",
"MetaInstanceID": "1dd1e407-1e0c-4549-84b2-29ca24a34d6e",
"DefaultValue": {
"Type": "Image",
"Value": "Framefield.Core.Image"
},
"MetaID": "9848060d-fd84-45b0-b658-d0d531c61dab",
"IsMultiInput": "False",
"Relevance": "Required",
"Description": "",
"Min": "-100000",
"Max": "100000",
"Scale": "0.1",
"ScaleType": "Linear",
"EnumValues": []
}
],
"Outputs": [
{
"Name": "Image",
"MetaInstanceID": "47c3a297-62ea-4bb0-bd25-9b5000ff920c",
"MetaID": "9848060d-fd84-45b0-b658-d0d531c61dab"
},
{
"Name": "PosBuffer",
"MetaInstanceID": "a65d2823-2262-413b-af91-7c6e968a8216",
"MetaID": "9701d534-b3ff-4889-a250-84aece4a7d76"
}
],
"OperatorParts": [
{
"MetaInstanceID": "1f2c3397-448c-4387-9508-195acc4ffaae",
"MetaID": "72f1fe7a-9983-44ac-ba77-6f7b2d908ae4",
"Name": "EnvISFunc",
"Version": "459f9543-4ec8-4ef7-b9b0-8a8b80399430",
"Type": "Float",
"IsMultiInput": "True",
"Script": [
"//>>> _using",
"using System;",
"using System.Collections.Generic;",
"using System.Linq;",
"using System.Text;",
"using SharpDX;",
"using SharpDX.Direct3D11;",
"using SharpDX.Windows;",
"//<<< _using",
"",
"using System.Runtime.InteropServices;",
"using Framefield.Core.Rendering;",
"using Framefield.Core.Profiling;",
"using SharpDX.Direct3D;",
"using SharpDX.DXGI;",
"using SharpDX.D3DCompiler;",
"using Buffer = SharpDX.Direct3D11.Buffer;",
"using System.CodeDom.Compiler;",
"using System.Diagnostics;",
"using System.Dynamic;",
"",
"namespace Framefield.Core.ID72f1fe7a_9983_44ac_ba77_6f7b2d908ae4",
"{",
" public class Class_EnvIS : FXSourceCodeFunction",
" {",
"",
" //>>> _inputids",
" private enum InputId",
" {",
" Code = 0,",
" Intensity = 1,",
" NumSamples = 2,",
" MeshPositions = 3,",
" MeshNormals = 4",
" }",
" //<<< _inputids",
"",
" //>>> _outputids",
" private enum OutputId",
" {",
" Image = 0,",
" PosBuffer = 1",
" }",
" //<<< _outputids",
"",
" public override void Dispose()",
" {",
" Utilities.DisposeObj(ref _sumTexture);",
" Utilities.DisposeObj(ref _sumTextureUAV);",
" Utilities.DisposeObj(ref _sumTextureSRV);",
" Utilities.DisposeObj(ref _summedColumnTexture);",
" Utilities.DisposeObj(ref _summedColumnTextureUAV);",
" Utilities.DisposeObj(ref _summedColumnTextureSRV);",
" Utilities.DisposeObj(ref _visuTexture);",
" Utilities.DisposeObj(ref _visuTextureUAV);",
" Utilities.DisposeObj(ref _sampleBuffer);",
" Utilities.DisposeObj(ref _sampleBufferUAV);",
" Utilities.DisposeObj(ref _sampleBufferSRV);",
" Utilities.DisposeObj(ref _worldPosBuffer);",
" Utilities.DisposeObj(ref _worldPosBufferUAV);",
" Utilities.DisposeObj(ref _worldPosBufferSRV);",
" Utilities.DisposeObj(ref _constantBuffer);",
" Utilities.DisposeObj(ref _csHistoClear);",
" Utilities.DisposeObj(ref _csSumRows);",
" Utilities.DisposeObj(ref _csSumColumn);",
" Utilities.DisposeObj(ref _csVisu);",
" _queryTimeStampDisjoint.Dispose();",
" _queryTimeStampFrameBegin.Dispose();",
" _queryTimeStampFrameEnd.Dispose();",
" base.Dispose();",
" }",
"",
" protected bool BuildRenderTarget(OperatorPartContext context, Texture2D inputImage)",
" {",
" if (_sumTexture != null && _sumTexture.Description.Height == inputImage.Description.Height)",
" {",
" return false; // nothing to do, buffers have already the right size",
" }",
"",
" // summed up rows texture, has an additional column with the row sum",
" var sumTextureDesc = new Texture2DDescription",
" {",
" BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess,",
" Format = Format.R32_Float,",
" Width = inputImage.Description.Width + 2, // [N] = 1, [N + 1] = Sum of all elements",
" Height = inputImage.Description.Height,",
" MipLevels = 1,",
" SampleDescription = new SampleDescription(1, 0),",
" Usage = ResourceUsage.Default,",
" OptionFlags = ResourceOptionFlags.None,",
" CpuAccessFlags = CpuAccessFlags.None,",
" ArraySize = 1",
" };",
" Utilities.DisposeObj(ref _sumTexture);",
" _sumTexture = new Texture2D(context.D3DDevice, sumTextureDesc);",
" Utilities.DisposeObj(ref _sumTextureUAV);",
" _sumTextureUAV = new UnorderedAccessView(context.D3DDevice, _sumTexture);",
" Utilities.DisposeObj(ref _sumTextureSRV);",
" _sumTextureSRV = new ShaderResourceView(context.D3DDevice, _sumTexture);",
"",
" // summed up column texture, has an additional row with the column sum",
"",
" sumTextureDesc.Width = 1;",
" sumTextureDesc.Height = inputImage.Description.Height + 2; // [N] = 1, [N + 1] = Sum of all elements",
"",
" Utilities.DisposeObj(ref _summedColumnTexture);",
" _summedColumnTexture = new Texture2D(context.D3DDevice, sumTextureDesc);",
" Utilities.DisposeObj(ref _summedColumnTextureUAV);",
" _summedColumnTextureUAV = new UnorderedAccessView(context.D3DDevice, _summedColumnTexture);",
" Utilities.DisposeObj(ref _summedColumnTextureSRV);",
" _summedColumnTextureSRV = new ShaderResourceView(context.D3DDevice, _summedColumnTexture);",
" ",
" // helper texture for visualization",
" var uavVisuDesc = new Texture2DDescription",
" {",
" BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess,",
" Format = Format.R8G8B8A8_UNorm,",
" Width = inputImage.Description.Width,",
" Height = inputImage.Description.Height,",
" MipLevels = 1,",
" SampleDescription = new SampleDescription(1, 0),",
" Usage = ResourceUsage.Default,",
" OptionFlags = ResourceOptionFlags.None,",
" CpuAccessFlags = CpuAccessFlags.None,",
" ArraySize = 1",
" };",
" Utilities.DisposeObj(ref _visuTexture);",
" _visuTexture = new Texture2D(context.D3DDevice, uavVisuDesc);",
" Utilities.DisposeObj(ref _visuTextureUAV);",
" _visuTextureUAV = new UnorderedAccessView(context.D3DDevice, _visuTexture);",
"",
" Utilities.DisposeObj(ref _constantBuffer);",
" _constantBuffer = Buffer.Create(context.D3DDevice, BindFlags.ConstantBuffer, _constantBufferData, 16);",
"",
" return true;",
" }",
"",
"",
" public override OperatorPartContext Eval(OperatorPartContext context, List<OperatorPart> inputs, int outputIdx) ",
" {",
" //>>> _params",
" var Code = inputs[(int)InputId.Code].Eval(context).Text;",
" var Intensity = inputs[(int)InputId.Intensity].Eval(context).Image; // Needs to be checked for null!",
" var NumSamples = inputs[(int)InputId.NumSamples].Eval(context).Value;",
" var MeshPositions = inputs[(int)InputId.MeshPositions].Eval(context).Image; // Needs to be checked for null!",
" var MeshNormals = inputs[(int)InputId.MeshNormals].Eval(context).Image; // Needs to be checked for null!",
" //<<< _params",
" ",
" if (Intensity == null)",
" {",
" Logger.Warn(this, \"No imput image connected\");",
" return context; ",
" }",
" ",
" int inputImageHeight = Intensity.Description.Height;",
" //Logger.Info(this, \"input size: {0}/{1}\", Intensity.Description.Width, inputImageHeight);",
" ",
" if (_csSumRows == null)",
" {",
" Utilities.DisposeObj(ref _csHistoClear);",
" Utilities.DisposeObj(ref _csSumRows);",
" Utilities.DisposeObj(ref _csVisu);",
" var errors = new CompilerErrorCollection();",
" try",
" {",
" using (var bytecode = ShaderBytecode.Compile(GetCode(0), \"ClearVisuBuffer\", \"cs_5_0\", ShaderFlags.Debug))",
" _csHistoClear = new ComputeShader(D3DDevice.Device, bytecode);",
" using (var bytecode = ShaderBytecode.Compile(GetCode(0), \"SumRows\", \"cs_5_0\", ShaderFlags.Debug))",
" _csSumRows = new ComputeShader(D3DDevice.Device, bytecode);",
" using (var bytecode = ShaderBytecode.Compile(GetCode(0), \"SumColumn\", \"cs_5_0\", ShaderFlags.Debug))",
" _csSumColumn = new ComputeShader(D3DDevice.Device, bytecode);",
" using (var bytecode = ShaderBytecode.Compile(GetCode(0), \"GenerateUVPositions\", \"cs_5_0\", ShaderFlags.Debug))",
" _csGenerateUVPositions = new ComputeShader(D3DDevice.Device, bytecode);",
" using (var bytecode = ShaderBytecode.Compile(GetCode(0), \"GenerateWorldPositions\", \"cs_5_0\", ShaderFlags.Debug))",
" _csGenerateWorldPositions = new ComputeShader(D3DDevice.Device, bytecode);",
" using (var bytecode = ShaderBytecode.Compile(GetCode(0), \"VisualizeSumBuffer\", \"cs_5_0\", ShaderFlags.Debug))",
" _csVisu = new ComputeShader(D3DDevice.Device, bytecode);",
" }",
" catch (CompilationException ex)",
" {",
" errors = ErrorsFromString(ex.Message);",
" Logger.Error(this, \"CS compile error: {0}\", ex.Message);",
" }",
" }",
"",
" BuildRenderTarget(context, Intensity);",
"",
" var targetSize = sizeof(float)*2*(int)NumSamples;",
" if (_sampleBuffer == null || _sampleBuffer.Description.SizeInBytes != targetSize)",
" {",
" // create buffer for sample locations",
" Utilities.DisposeObj(ref _sampleBuffer);",
" _sampleBuffer = new Buffer(context.D3DDevice, new BufferDescription()",
" {",
" BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess | BindFlags.VertexBuffer,",
" CpuAccessFlags = CpuAccessFlags.None,",
" OptionFlags = ResourceOptionFlags.BufferAllowRawViews,",
" SizeInBytes = targetSize,",
" Usage = ResourceUsage.Default",
" });",
" ",
" {",
" // create its shader resource view ",
" Utilities.DisposeObj(ref _sampleBufferSRV);",
" var bufferResource = new ShaderResourceViewDescription.ExtendedBufferResource()",
" {",
" ElementCount = _sampleBuffer.Description.SizeInBytes/4,",
" FirstElement = 0,",
" Flags = ShaderResourceViewExtendedBufferFlags.Raw",
" };",
" var srvDesc = new ShaderResourceViewDescription()",
" {",
" Format = Format.R32_Typeless,",
" Dimension = ShaderResourceViewDimension.ExtendedBuffer,",
" BufferEx = bufferResource",
" };",
" _sampleBufferSRV = new ShaderResourceView(context.D3DDevice, _sampleBuffer, srvDesc);",
" }",
" ",
" {",
" // create its unordered access view",
" Utilities.DisposeObj(ref _sampleBufferUAV);",
" ",
" var bufferResource = new UnorderedAccessViewDescription.BufferResource()",
" {",
" ElementCount = _sampleBuffer.Description.SizeInBytes/4,",
" FirstElement = 0,",
" Flags = UnorderedAccessViewBufferFlags.Raw",
" };",
" var uavDesc = new UnorderedAccessViewDescription()",
" {",
" Format = Format.R32_Typeless,",
" Dimension = UnorderedAccessViewDimension.Buffer,",
" Buffer = bufferResource",
" };",
" _sampleBufferUAV = new UnorderedAccessView(context.D3DDevice, _sampleBuffer, uavDesc); ",
" }",
" ",
" ",
" // create buffer for world positions ",
" Utilities.DisposeObj(ref _worldPosBuffer);",
" _worldPosBuffer = new Buffer(context.D3DDevice, new BufferDescription()",
" {",
" BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess | BindFlags.VertexBuffer,",
" CpuAccessFlags = CpuAccessFlags.None,",
" OptionFlags = ResourceOptionFlags.BufferAllowRawViews,",
" SizeInBytes = /*sizeof(float)*3*/64*(int)NumSamples,",
" Usage = ResourceUsage.Default",
" });",
"",
" {",
" // create its shader resource view ",
" Utilities.DisposeObj(ref _worldPosBufferSRV);",
" var bufferResource = new ShaderResourceViewDescription.ExtendedBufferResource()",
" {",
" ElementCount = _worldPosBuffer.Description.SizeInBytes/4,",
" FirstElement = 0,",
" Flags = ShaderResourceViewExtendedBufferFlags.Raw",
" };",
" var srvDesc = new ShaderResourceViewDescription()",
" {",
" Format = Format.R32_Typeless,",
" Dimension = ShaderResourceViewDimension.ExtendedBuffer,",
" BufferEx = bufferResource",
" };",
" _worldPosBufferSRV = new ShaderResourceView(context.D3DDevice, _worldPosBuffer, srvDesc);",
" }",
"",
" {",
" // create its unordered access view",
" Utilities.DisposeObj(ref _worldPosBufferUAV);",
" ",
" var bufferResource = new UnorderedAccessViewDescription.BufferResource()",
" {",
" ElementCount = _worldPosBuffer.Description.SizeInBytes/4,",
" FirstElement = 0,",
" Flags = UnorderedAccessViewBufferFlags.Raw",
" };",
" var uavDesc = new UnorderedAccessViewDescription()",
" {",
" Format = Format.R32_Typeless,",
" Dimension = UnorderedAccessViewDimension.Buffer,",
" Buffer = bufferResource",
" };",
" _worldPosBufferUAV = new UnorderedAccessView(context.D3DDevice, _worldPosBuffer, uavDesc); ",
" }",
"",
" }",
"",
"",
"",
" var deviceContext = context.D3DDevice.ImmediateContext;",
"",
" if (_readyToMeasure)",
" {",
" _queryTimeStampDisjoint.Begin(deviceContext);",
" _queryTimeStampFrameBegin.End(deviceContext);",
" }",
"",
" using (var intensityImageView = new ShaderResourceView(context.D3DDevice, Intensity))",
" using (var meshPosSRV = new ShaderResourceView(context.D3DDevice, MeshPositions))",
" using (var meshNormalsSRV = new ShaderResourceView(context.D3DDevice, MeshNormals))",
" {",
"// deviceContext.ComputeShader.Set(_csHistoClear);",
"// deviceContext.ComputeShader.SetUnorderedAccessView(0, _sumTextureUAV);",
"// deviceContext.Dispatch(1, Image.Description.Height/128, 1);",
"",
" deviceContext.ComputeShader.Set(_csSumRows);",
" deviceContext.ComputeShader.SetUnorderedAccessView(0, _sumTextureUAV);",
" deviceContext.ComputeShader.SetShaderResource(0, intensityImageView);",
" deviceContext.Dispatch(1, Intensity.Description.Height/128, 1);",
" deviceContext.ComputeShader.SetUnorderedAccessView(0, null);",
" ",
" deviceContext.ComputeShader.Set(_csSumColumn);",
" deviceContext.ComputeShader.SetUnorderedAccessView(1, _summedColumnTextureUAV);",
" deviceContext.ComputeShader.SetShaderResource(1, _sumTextureSRV);",
" deviceContext.Dispatch(1, 1, 1);",
" deviceContext.ComputeShader.SetUnorderedAccessView(1, null);",
" deviceContext.ComputeShader.SetShaderResource(1, null);",
"",
"deviceContext.ComputeShader.SetConstantBuffer(0, _constantBuffer);",
"_constantBufferData[0] = (int)NumSamples;",
"deviceContext.UpdateSubresource(_constantBufferData, _constantBuffer);",
"",
" deviceContext.ComputeShader.SetUnorderedAccessView(2, _sampleBufferUAV);",
" deviceContext.ComputeShader.SetShaderResource(2, _sumTextureSRV);",
" deviceContext.ComputeShader.SetShaderResource(3, _summedColumnTextureSRV);",
" deviceContext.ComputeShader.Set(_csGenerateUVPositions);",
" deviceContext.Dispatch((int)NumSamples/256 + 1, 1, 1);",
" deviceContext.ComputeShader.SetShaderResource(2, null);",
" deviceContext.ComputeShader.SetShaderResource(3, null);",
" deviceContext.ComputeShader.SetUnorderedAccessView(2, null);",
" deviceContext.ComputeShader.SetShaderResource(0, null);",
"",
" deviceContext.ComputeShader.SetUnorderedAccessView(3, _worldPosBufferUAV);",
" deviceContext.ComputeShader.SetShaderResource(4, _sampleBufferSRV);",
" deviceContext.ComputeShader.SetShaderResource(5, meshPosSRV);",
" deviceContext.ComputeShader.SetShaderResource(6, meshNormalsSRV);",
" deviceContext.ComputeShader.Set(_csGenerateWorldPositions);",
" deviceContext.Dispatch((int)NumSamples/256 + 1, 1, 1);",
" deviceContext.ComputeShader.SetShaderResource(4, null);",
" deviceContext.ComputeShader.SetShaderResource(5, null);",
" deviceContext.ComputeShader.SetUnorderedAccessView(3, null);",
"",
" deviceContext.ComputeShader.SetUnorderedAccessView(7, _visuTextureUAV);",
" deviceContext.ComputeShader.SetShaderResource(4, _sampleBufferSRV);",
" int width = _visuTexture.Description.Width;",
" int height = _visuTexture.Description.Height;",
" deviceContext.ComputeShader.Set(_csHistoClear);",
" deviceContext.Dispatch(width/32 + 1, height/32 + 1, 1);",
" deviceContext.ComputeShader.Set(_csVisu);",
" deviceContext.Dispatch((int)NumSamples/256 + 1, 1, 1);",
" deviceContext.ComputeShader.SetUnorderedAccessView(7, null);",
" deviceContext.ComputeShader.SetShaderResource(2, null);",
" deviceContext.ComputeShader.SetShaderResource(3, null);",
" deviceContext.ComputeShader.SetShaderResource(0, null);",
"",
"",
" }",
" ",
" if (_readyToMeasure)",
" {",
" _queryTimeStampFrameEnd.End(deviceContext);",
" _queryTimeStampDisjoint.End(deviceContext);",
" _readyToMeasure = false;",
" }",
" else",
" {",
" // check if last measurement is ready",
" QueryDataTimestampDisjoint disjointData;",
" long timeStampframeBegin;",
" long timeStampframeEnd;",
" bool dataFetched = true;",
" dataFetched &= _queryTimeStampDisjoint.GetData(deviceContext, AsynchronousFlags.None, out disjointData);",
" dataFetched &= _queryTimeStampFrameBegin.GetData(deviceContext, AsynchronousFlags.None, out timeStampframeBegin);",
" dataFetched &= _queryTimeStampFrameEnd.GetData(deviceContext, AsynchronousFlags.None, out timeStampframeEnd);",
" ",
" if (dataFetched && !disjointData.Disjoint)",
" {",
" float duration = (float)(timeStampframeEnd - timeStampframeBegin)/disjointData.Frequency;",
" //Logger.Info(this, \"tsfb: {0}\", timeStampframeBegin);",
" //Logger.Info(this, \"tsfe: {0}\", timeStampframeEnd);",
" //Logger.Info(this, \"freq: {0}\", disjointData.Frequency);",
" //Logger.Info(this,\"row calc took: {0}s\", duration);",
" _readyToMeasure = true;",
" }",
" }",
" ",
" switch (outputIdx)",
" {",
" case (int)OutputId.Image:",
" ",
" context.Image = _visuTexture;",
" break;",
" case (int)OutputId.PosBuffer:",
" dynamic obj = new ExpandoObject();",
" obj.PosBuffer = _worldPosBuffer;",
" context.Dynamic = obj;",
" break;",
" default:",
" Logger.Warn(this, \"Output index '{0}' requested, but not existing.\", outputIdx);",
" break;",
" }",
" ",
" return context;",
" }",
"",
" private Texture2D _sumTexture;",
" private UnorderedAccessView _sumTextureUAV;",
" private ShaderResourceView _sumTextureSRV;",
" private Texture2D _summedColumnTexture;",
" private UnorderedAccessView _summedColumnTextureUAV;",
" private ShaderResourceView _summedColumnTextureSRV;",
" private Texture2D _visuTexture;",
" private UnorderedAccessView _visuTextureUAV;",
" private Buffer _sampleBuffer;",
" private ShaderResourceView _sampleBufferSRV;",
" private UnorderedAccessView _sampleBufferUAV;",
" private Buffer _worldPosBuffer;",
" private ShaderResourceView _worldPosBufferSRV;",
" private UnorderedAccessView _worldPosBufferUAV;",
" private Buffer _constantBuffer;",
" private int[] _constantBufferData = new int[4];",
" private ComputeShader _csHistoClear;",
" private ComputeShader _csSumRows;",
" private ComputeShader _csSumColumn;",
" private ComputeShader _csGenerateUVPositions;",
" private ComputeShader _csGenerateWorldPositions;",
" private ComputeShader _csVisu;",
" private GPUQuery _queryTimeStampDisjoint = new GPUQuery(D3DDevice.Device, new QueryDescription() { Type = QueryType.TimestampDisjoint });",
" private GPUQuery _queryTimeStampFrameBegin = new GPUQuery(D3DDevice.Device, new QueryDescription() { Type = QueryType.Timestamp });",
" private GPUQuery _queryTimeStampFrameEnd = new GPUQuery(D3DDevice.Device, new QueryDescription() { Type = QueryType.Timestamp });",
" private bool _readyToMeasure = true;",
" }",
"}",
"",
""
],
"AdditionalAssemblies": []
}
],
"Operators": [],
"Connections": [
{
"SourceOp": "00000000-0000-0000-0000-000000000000",
"SourceOpPart": "1f2c3397-448c-4387-9508-195acc4ffaae",
"TargetOp": "00000000-0000-0000-0000-000000000000",
"TargetOpPart": "47c3a297-62ea-4bb0-bd25-9b5000ff920c"
},
{
"SourceOp": "00000000-0000-0000-0000-000000000000",
"SourceOpPart": "cb9db90d-2a46-4748-af34-9527917b1f02",
"TargetOp": "00000000-0000-0000-0000-000000000000",
"TargetOpPart": "1f2c3397-448c-4387-9508-195acc4ffaae"
},
{
"SourceOp": "00000000-0000-0000-0000-000000000000",
"SourceOpPart": "34e49ad4-9921-4cbe-99fe-928474789538",
"TargetOp": "00000000-0000-0000-0000-000000000000",
"TargetOpPart": "1f2c3397-448c-4387-9508-195acc4ffaae"
},
{
"SourceOp": "00000000-0000-0000-0000-000000000000",
"SourceOpPart": "dd1e70fb-0b60-44f1-a517-8d5bf18b97a8",
"TargetOp": "00000000-0000-0000-0000-000000000000",
"TargetOpPart": "1f2c3397-448c-4387-9508-195acc4ffaae"
},
{
"SourceOp": "00000000-0000-0000-0000-000000000000",
"SourceOpPart": "0bb0032f-7f01-4ce1-82ea-74756546c9ea",
"TargetOp": "00000000-0000-0000-0000-000000000000",
"TargetOpPart": "1f2c3397-448c-4387-9508-195acc4ffaae"
},
{
"SourceOp": "00000000-0000-0000-0000-000000000000",
"SourceOpPart": "1dd1e407-1e0c-4549-84b2-29ca24a34d6e",
"TargetOp": "00000000-0000-0000-0000-000000000000",
"TargetOpPart": "1f2c3397-448c-4387-9508-195acc4ffaae"
},
{
"SourceOp": "00000000-0000-0000-0000-000000000000",
"SourceOpPart": "1f2c3397-448c-4387-9508-195acc4ffaae",
"TargetOp": "00000000-0000-0000-0000-000000000000",
"TargetOpPart": "a65d2823-2262-413b-af91-7c6e968a8216"
}
]
}