-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlockGen.fxh
executable file
·492 lines (419 loc) · 16.3 KB
/
BlockGen.fxh
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
//--------------------------------------------------------------------------------------
// File: BlockGen.fxh
//--------------------------------------------------------------------------------------
#include "Tables.fxh"
#include "Density.fxh"
cbuffer cb1 {
uint VoxelDim = 33;
uint VoxelDimMinusOne = 32;
float2 InvVoxelDim = float2(1.0/33.0, 0);
float2 InvVoxelDimMinusOne = float2(1.0/32.0, 0);
uint Margin = 2;
uint VoxelDimWithMargins = 37;
uint VoxelDimWithMarginsMinusOne = 36;
float2 InvVoxelDimWithMargins = float2(1.0/37.0, 0);
float2 InvVoxelDimWithMarginsMinusOne = float2(1.0/36.0, 0);
float BlockSize = 1.0;
}
struct VS_DENSITY_INPUT {
float2 Position : POSITION;
float2 Tex : TEXCOORD;
uint InstanceID : SV_InstanceID;
};
struct VS_DENSITY_OUTPUT {
float4 Position : POSITION;
float4 WorldPosition : WORLDPOSITION;
float3 BlockPosition : BLOCKPOSITION;
uint InstanceID : INSTANCEID;
};
struct GS_DENSITY_OUTPUT {
float4 Position : SV_Position;
float4 WorldPosition : WORLDPOSITION;
float3 BlockPosition : BLOCKPOSITION;
uint RTIndex : SV_RenderTargetArrayIndex;
};
VS_DENSITY_OUTPUT Density_VS(VS_DENSITY_INPUT Input) {
VS_DENSITY_OUTPUT Output;
Output.Position = float4(Input.Position.xy, 0.5, 1);
float3 vVolumePos = float3(Input.Tex.xy, Input.InstanceID * InvVoxelDimWithMarginsMinusOne.x);
// Texel (0,0) is half a texel away from "real" position of vertex at (-1,-1), so compensate for that:
vVolumePos.xy = (vVolumePos - float2(0.5, 0.5)) * (1 + InvVoxelDimMinusOne.x) + float2(0.5, 0.5);
Output.BlockPosition.xy = (vVolumePos.xy * VoxelDimWithMargins.x - Margin.xx) * InvVoxelDim.x;
Output.BlockPosition.z = (vVolumePos.z * VoxelDimWithMarginsMinusOne.x - Margin) * InvVoxelDimMinusOne.x;
Output.WorldPosition = float4(g_vBlockOffset + Output.BlockPosition * BlockSize, 1);
Output.InstanceID = Input.InstanceID;
return Output;
}
[MaxVertexCount(3)]
void Density_GS(triangle VS_DENSITY_OUTPUT Input[3],
inout TriangleStream<GS_DENSITY_OUTPUT> Stream) {
GS_DENSITY_OUTPUT Output;
[unroll] for (uint i = 0; i < 3; ++i) {
Output.Position = Input[i].Position;
Output.WorldPosition = Input[i].WorldPosition;
Output.BlockPosition = Input[i].BlockPosition;
Output.RTIndex = Input[i].InstanceID;
Stream.Append(Output);
}
Stream.RestartStrip();
}
float Density_PS(GS_DENSITY_OUTPUT Input) : SV_Target {
return DENSITY(Input.WorldPosition.xyz);
}
struct VS_LISTTRIS_INPUT {
uint2 Position : POSITION;
uint InstanceID : SV_InstanceID;
};
struct VS_LISTTRIS_OUTPUT {
uint3 Position : POSITION;
uint Case : CASE;
};
struct GS_LISTTRIS_OUTPUT {
uint Marker : MARKER; // z6_y6_x6_edge1_edge2_edge3
};
Texture3D g_tDensityVolume;
VS_LISTTRIS_OUTPUT ListTris_VS(VS_LISTTRIS_INPUT Input) {
int4 pos = int4(int3(Input.Position.xy, Input.InstanceID) + Margin.xxx, 0);
float4 density0123;
float4 density4567;
density0123.x = g_tDensityVolume.Load(pos + int4(0, 0, 0, 0), 0);
density0123.y = g_tDensityVolume.Load(pos + int4(0, 1, 0, 0), 0);
density0123.z = g_tDensityVolume.Load(pos + int4(1, 1, 0, 0), 0);
density0123.w = g_tDensityVolume.Load(pos + int4(1, 0, 0, 0), 0);
density4567.x = g_tDensityVolume.Load(pos + int4(0, 0, 1, 0), 0);
density4567.y = g_tDensityVolume.Load(pos + int4(0, 1, 1, 0), 0);
density4567.z = g_tDensityVolume.Load(pos + int4(1, 1, 1, 0), 0);
density4567.w = g_tDensityVolume.Load(pos + int4(1, 0, 1, 0), 0);
uint4 i0123 = (uint4)saturate(density0123*99999);
uint4 i4567 = (uint4)saturate(density4567*99999);
VS_LISTTRIS_OUTPUT Output;
Output.Case = (i0123.x << 0) | (i0123.y << 1) | (i0123.z << 2) | (i0123.w << 3) |
(i4567.x << 4) | (i4567.y << 5) | (i4567.z << 6) | (i4567.w << 7);
Output.Position = int3(Input.Position.xy, Input.InstanceID);
return Output;
}
[MaxVertexCount(5)]
void ListTris_GS(point VS_LISTTRIS_OUTPUT Input[1],
inout PointStream<GS_LISTTRIS_OUTPUT> Stream) {
const uint nTris = numTris[Input[0].Case];
const int3 vBlockPos = Input[0].Position;
GS_LISTTRIS_OUTPUT Output;
for (uint i = 0; i < nTris; ++i) {
const int3 edges = triTable[Input[0].Case][i];
// Warning: This only works with VoxelDim 33 (or less)!
Output.Marker = ((Input[0].Position.z & 0x3F) << 26) |
((Input[0].Position.y & 0x3F) << 20) |
((Input[0].Position.x & 0x3F) << 14) |
((edges[0] & 0x0F) << 8) |
((edges[1] & 0x0F) << 4) |
((edges[2] & 0x0F) << 0);
Stream.Append(Output);
}
}
struct VS_GENTRIS_INPUT {
uint Marker : MARKER; // z6_y6_x6_edge1_edge2_edge3
};
struct VS_GENTRIS_OUTPUT {
float3 Position1 : POSITION1;
float3 Normal1 : NORMAL1;
float3 Position2 : POSITION2;
float3 Normal2 : NORMAL2;
float3 Position3 : POSITION3;
float3 Normal3 : NORMAL3;
};
struct GS_GENTRIS_OUTPUT {
float3 Position : POSITION;
float3 Normal : NORMAL;
};
float3 GetEdgeOffset(int3 pos, int edge) {
pos += Margin.xxx;
float d1 = g_tDensityVolume.Load(int4(pos + edgeStartCorner[edge], 0));
float d2 = g_tDensityVolume.Load(int4(pos + edgeStartCorner[edge] + edgeDirection[edge], 0));
return (edgeStartCorner[edge] + (d1/(d1-d2))*edgeDirection[edge]);
}
float3 GetNormal(float3 vVolumePos) {
float3 grad;
grad.x = g_tDensityVolume.SampleLevel(ssTrilinearClamp, vVolumePos + InvVoxelDimWithMargins.xyy, 0) -
g_tDensityVolume.SampleLevel(ssTrilinearClamp, vVolumePos - InvVoxelDimWithMargins.xyy, 0);
grad.y = g_tDensityVolume.SampleLevel(ssTrilinearClamp, vVolumePos + InvVoxelDimWithMargins.yxy, 0) -
g_tDensityVolume.SampleLevel(ssTrilinearClamp, vVolumePos - InvVoxelDimWithMargins.yxy, 0);
grad.z = g_tDensityVolume.SampleLevel(ssTrilinearClamp, vVolumePos + InvVoxelDimWithMargins.yyx, 0) -
g_tDensityVolume.SampleLevel(ssTrilinearClamp, vVolumePos - InvVoxelDimWithMargins.yyx, 0);
return -normalize(grad);
}
void CreateVertex(int3 vCellPos, int nEdge, out float3 vPosition, out float3 vNormal) {
float3 vEdgePos = (vCellPos + GetEdgeOffset(vCellPos, nEdge));
float3 vVolumePos = (vEdgePos + Margin.xxx) * InvVoxelDimWithMargins.x;
vPosition = g_vBlockOffset + vEdgePos * InvVoxelDimMinusOne.x * BlockSize;
vNormal = GetNormal(vVolumePos);
}
VS_GENTRIS_OUTPUT GenTris_VS(VS_GENTRIS_INPUT Input) {
VS_GENTRIS_OUTPUT Output;
const int3 vCellPos = int3((Input.Marker >> 14) & 0x3F,
(Input.Marker >> 20) & 0x3F,
(Input.Marker >> 26) & 0x3F);
CreateVertex(vCellPos, (Input.Marker >> 8) & 0x0F, Output.Position1, Output.Normal1);
CreateVertex(vCellPos, (Input.Marker >> 4) & 0x0F, Output.Position2, Output.Normal2);
CreateVertex(vCellPos, (Input.Marker >> 0) & 0x0F, Output.Position3, Output.Normal3);
return Output;
}
[MaxVertexCount(3)]
void GenTris_GS(point VS_GENTRIS_OUTPUT Input[1],
inout TriangleStream<GS_GENTRIS_OUTPUT> Stream) {
GS_GENTRIS_OUTPUT Output;
Output.Position = Input[0].Position1;
Output.Normal = Input[0].Normal1;
Stream.Append(Output);
Output.Position = Input[0].Position2;
Output.Normal = Input[0].Normal2;
Stream.Append(Output);
Output.Position = Input[0].Position3;
Output.Normal = Input[0].Normal3;
Stream.Append(Output);
Stream.RestartStrip();
}
DepthStencilState dssDisableDepthStencil {
DepthEnable = FALSE;
StencilEnable = FALSE;
};
technique10 GenBlock {
pass build_densities {
SetVertexShader(CompileShader(vs_4_0, Density_VS()));
SetGeometryShader(CompileShader(gs_4_0, Density_GS()));
SetPixelShader(CompileShader(ps_4_0, Density_PS()));
SetDepthStencilState(dssDisableDepthStencil, 0);
SetBlendState(NULL, float4(0, 0, 0, 0), 0xFFFFFFFF);
SetRasterizerState(NULL);
}
pass list_triangles {
SetVertexShader(CompileShader(vs_4_0, ListTris_VS()));
SetGeometryShader(ConstructGSWithSO(CompileShader(gs_4_0, ListTris_GS()), "MARKER.x"));
SetPixelShader(NULL);
}
pass gen_vertices {
SetVertexShader(CompileShader(vs_4_0, GenTris_VS()));
SetGeometryShader(ConstructGSWithSO(CompileShader(gs_4_0, GenTris_GS()), "POSITION.xyz; NORMAL.xyz"));
SetPixelShader(NULL);
}
}
struct VS_LISTCELLS_INPUT {
uint2 Position : POSITION;
uint InstanceID : SV_InstanceID;
};
struct VS_LISTCELLS_OUTPUT {
uint Cell : CELL; // z8_y8_x8_case8
};
struct GS_LISTCELLS_OUTPUT {
uint Cell : CELL; // z8_y8_x8_case8
};
VS_LISTCELLS_OUTPUT ListCells_VS(VS_LISTCELLS_INPUT Input) {
int4 pos = int4(int3(Input.Position.xy, Input.InstanceID) + Margin.xxx, 0);
float4 density0123;
float4 density4567;
density0123.x = g_tDensityVolume.Load(pos + int4(0, 0, 0, 0), 0);
density0123.y = g_tDensityVolume.Load(pos + int4(0, 1, 0, 0), 0);
density0123.z = g_tDensityVolume.Load(pos + int4(1, 1, 0, 0), 0);
density0123.w = g_tDensityVolume.Load(pos + int4(1, 0, 0, 0), 0);
density4567.x = g_tDensityVolume.Load(pos + int4(0, 0, 1, 0), 0);
density4567.y = g_tDensityVolume.Load(pos + int4(0, 1, 1, 0), 0);
density4567.z = g_tDensityVolume.Load(pos + int4(1, 1, 1, 0), 0);
density4567.w = g_tDensityVolume.Load(pos + int4(1, 0, 1, 0), 0);
uint4 i0123 = (uint4)saturate(density0123*99999);
uint4 i4567 = (uint4)saturate(density4567*99999);
VS_LISTCELLS_OUTPUT Output;
uint nCase = (i0123.x << 0) | (i0123.y << 1) | (i0123.z << 2) | (i0123.w << 3) |
(i4567.x << 4) | (i4567.y << 5) | (i4567.z << 6) | (i4567.w << 7);
int3 vPosition = int3(Input.Position.xy, Input.InstanceID);
Output.Cell = ((vPosition.z & 0xFF) << 24) |
((vPosition.y & 0xFF) << 16) |
((vPosition.x & 0xFF) << 8) |
nCase;
return Output;
}
[MaxVertexCount(1)]
void ListCells_GS(point VS_LISTCELLS_OUTPUT Input[1],
inout PointStream<GS_LISTCELLS_OUTPUT> Stream) {
if (numTris[Input[0].Cell & 0xFF] > 0) {
Stream.Append(Input[0]);
}
}
struct VS_LISTVERTS_INPUT {
uint Cell : CELL; // z8_y8_x8_case8
};
struct VS_LISTVERTS_OUTPUT {
uint Cell : CELL; // z8_y8_x8_edgeflags8
};
const uint EDGE0 = 0x02; // 00010b
const uint EDGE3 = 0x08; // 01000b
const uint EDGE8 = 0x10; // 10000b
struct GS_LISTVERTS_OUTPUT {
uint Edge : EDGE; // z8_y8_x8_null4_edge4
};
VS_LISTVERTS_OUTPUT ListVerts_VS(VS_LISTVERTS_INPUT Input) {
uint edgeFlags = (Input.Cell & 0xFF) ^ ((Input.Cell & 1) * (EDGE0 | EDGE3 | EDGE8));
Input.Cell = (Input.Cell & 0xFFFFFF00) | (edgeFlags & 0xFF);
return Input;
}
[MaxVertexCount(3)]
void ListVerts_GS(point VS_LISTVERTS_OUTPUT Input[1],
inout PointStream<GS_LISTVERTS_OUTPUT> Stream) {
GS_LISTVERTS_OUTPUT Output;
uint pos = Input[0].Cell & 0xFFFFFF00;
if (Input[0].Cell & EDGE3) {
Output.Edge = pos | 3;
Stream.Append(Output);
}
if (Input[0].Cell & EDGE0) {
Output.Edge = pos | 0;
Stream.Append(Output);
}
if (Input[0].Cell & EDGE8) {
Output.Edge = pos | 8;
Stream.Append(Output);
}
}
struct VS_GENVERTS_INPUT {
uint Edge : EDGE; // z8_y8_x8_null4_edge4
};
struct VS_GENVERTS_OUTPUT {
float3 Position : POSITION;
float3 Normal : NORMAL;
};
struct GS_GENVERTS_OUTPUT {
float3 Position : POSITION;
float3 Normal : NORMAL;
};
VS_GENVERTS_OUTPUT GenVerts_VS(VS_GENVERTS_INPUT Input) {
VS_GENVERTS_OUTPUT Output;
const int3 vCellPos = int3((Input.Edge >> 8) & 0xFF,
(Input.Edge >> 16) & 0xFF,
(Input.Edge >> 24) & 0xFF);
CreateVertex(vCellPos, Input.Edge & 0x0F, Output.Position, Output.Normal);
return Output;
}
[MaxVertexCount(1)]
void GenVerts_GS(point VS_GENVERTS_OUTPUT Input[1],
inout PointStream<GS_GENVERTS_OUTPUT> Stream) {
Stream.Append(Input[0]);
}
technique10 GenBlock3 {
pass build_densities {
SetVertexShader(CompileShader(vs_4_0, Density_VS()));
SetGeometryShader(CompileShader(gs_4_0, Density_GS()));
SetPixelShader(CompileShader(ps_4_0, Density_PS()));
SetDepthStencilState(dssDisableDepthStencil, 0);
SetBlendState(NULL, float4(0, 0, 0, 0), 0xFFFFFFFF);
SetRasterizerState(NULL);
}
pass list_nonempty_cells {
SetVertexShader(CompileShader(vs_4_0, ListCells_VS()));
SetGeometryShader(ConstructGSWithSO(CompileShader(gs_4_0, ListCells_GS()), "CELL.x"));
SetPixelShader(NULL);
}
pass list_verts_to_generate {
SetVertexShader(CompileShader(vs_4_0, ListVerts_VS()));
SetGeometryShader(ConstructGSWithSO(CompileShader(gs_4_0, ListVerts_GS()), "EDGE.x"));
SetPixelShader(NULL);
}
pass gen_vertices {
SetVertexShader(CompileShader(vs_4_0, GenVerts_VS()));
SetGeometryShader(ConstructGSWithSO(CompileShader(gs_4_0, GenVerts_GS()), "POSITION.xyz; NORMAL.xyz"));
SetPixelShader(NULL);
}
}
struct VS_SPLATIDS_INPUT {
uint Edge : EDGE; // z8_y8_x8_null4_edge4
uint VertexID : SV_VertexID;
};
struct VS_SPLATIDS_OUTPUT {
float4 Position : POSITION;
uint VertexID : VERTEXID;
uint RTIndex : RTINDEX;
};
struct GS_SPLATIDS_OUTPUT {
float4 Position : SV_Position;
uint VertexID : VERTEXID;
uint RTIndex : SV_RenderTargetArrayIndex;
};
VS_SPLATIDS_OUTPUT SplatIDs_VS(VS_SPLATIDS_INPUT Input) {
VS_SPLATIDS_OUTPUT Output;
int3 vCellPos = int3((Input.Edge >> 8) & 0xFF,
(Input.Edge >> 16) & 0xFF,
(Input.Edge >> 24) & 0xFF);
const int nEdge = Input.Edge & 0x0F;
vCellPos.x *= 3;
switch (nEdge) {
case 3: vCellPos.x += 0; break;
case 0: vCellPos.x += 1; break;
case 8: vCellPos.x += 2; break;
}
float2 xy = vCellPos.xy;
xy.x += 0.5;
xy.y += 0.5;
xy.x *= InvVoxelDim.x/3.0;
xy.y *= InvVoxelDim.x;
Output.Position = float4(xy, 0.5, 1);
Output.Position.xy = Output.Position.xy * 2 - 1; // [-1..1] range
Output.Position.y *= -1; // flip y
Output.VertexID = Input.VertexID;
Output.RTIndex = vCellPos.z;
return Output;
}
[MaxVertexCount(1)]
void SplatIDs_GS(point VS_SPLATIDS_OUTPUT Input[1],
inout PointStream<GS_SPLATIDS_OUTPUT> Stream) {
Stream.Append(Input[0]);
}
uint SplatIDs_PS(GS_SPLATIDS_OUTPUT Input) : SV_Target {
return Input.VertexID;
}
struct VS_GENINDICES_INPUT {
uint Cell : CELL; // z8_y8_x8_case8
};
struct VS_GENINDICES_OUTPUT {
uint Cell : CELL; // z8_y8_x8_case8
};
struct GS_GENINDICES_OUTPUT {
uint Index : INDEX;
};
Texture3D<uint> g_tIndicesVolume;
VS_GENINDICES_OUTPUT GenIndices_VS(VS_GENINDICES_INPUT Input) {
return Input;
}
[MaxVertexCount(15)]
void GenIndices_GS(point VS_GENINDICES_OUTPUT Input[1],
inout PointStream<GS_GENINDICES_OUTPUT> Stream) {
const uint nCase = Input[0].Cell & 0xFF;
const uint nTris = numTris[nCase];
const uint3 vCellPos = int3((Input[0].Cell >> 8) & 0xFF,
(Input[0].Cell >> 16) & 0xFF,
(Input[0].Cell >> 24) & 0xFF);
if (vCellPos.x == VoxelDimMinusOne.x || vCellPos.y == VoxelDimMinusOne.x || vCellPos.z == VoxelDimMinusOne.x) return;
GS_GENINDICES_OUTPUT Output;
int3 vEdgePos;
for (uint i = 0; i < nTris; ++i) {
const int3 edges = triTable[nCase][i];
[unroll(3)]
for (uint j = 0; j < 3; ++j) {
vEdgePos = vCellPos + edgeStartCorner[edges[j]];
vEdgePos.x = vEdgePos.x*3 + edgeAxis[edges[j]];
Output.Index = g_tIndicesVolume.Load(int4(vEdgePos, 0));
Stream.Append(Output);
}
}
}
technique10 GenIndices {
pass splat_vertex_ids {
SetVertexShader(CompileShader(vs_4_0, SplatIDs_VS()));
SetGeometryShader(CompileShader(gs_4_0, SplatIDs_GS()));
SetPixelShader(CompileShader(ps_4_0, SplatIDs_PS()));
SetDepthStencilState(dssDisableDepthStencil, 0);
SetBlendState(NULL, float4(0, 0, 0, 0), 0xFFFFFFFF);
SetRasterizerState(NULL);
}
pass gen_indices {
SetVertexShader(CompileShader(vs_4_0, GenIndices_VS()));
SetGeometryShader(ConstructGSWithSO(CompileShader(gs_4_0, GenIndices_GS()), "INDEX.x"));
SetPixelShader(NULL);
}
}