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
|
/*
* Lightmap generation D3D11 shader file
*
* This file is part of the "SoftPixel Engine" (Copyright (c) 2008 by Lukas Hermanns)
* See "SoftPixelEngine.hpp" for license information.
*/
//****INFO****: Diese Macros dienen nur dazu, um Teile des Codes mit GLSL kompativel zu halten.
#define MUL(m, v) mul(m, v)
#define CAST(t, v) ((t)(v))
/* === Macros === */
#define ID_NONE 0xFFFFFFFF
#define EPSILON 0.0001
#define PLANE_NORMAL(Plane) ((Plane).xyz)
#define PLANE_DISTANCE(Plane) ((Plane).w)
#define SPHERE_POINT(Sphere) ((Sphere).xyz)
#define SPHERE_RADIUS(Sphere) ((Sphere).w)
#define SPlane float4
#define MAX_STACK_SIZE 64
#define LIGHT_DIRECTIONAL 0
#define LIGHT_POINT 1
#define LIGHT_SPOT 2
#define KDTREE_XAXIS 0
#define KDTREE_YAXIS 1
#define KDTREE_ZAXIS 2
/* === Structures === */
struct STriangle
{
float3 A, B, C;
};
struct SRay
{
float3 Origin, Direction;
};
struct SLine
{
float3 Start, End;
};
struct SKDTreeNode
{
/* Node construction */
int Axis;
float Distance;
/* Content */
uint TriangleStart; //!< Index to 'TriangleIdList' or ID_NONE
uint NumTriangles; //!< Length in 'TriangleIdList' or 0
/* Children pointers */
uint ChildIds[2]; //!< Index to 'NodeList' or ID_NONE
};
struct SLightSource
{
int Type;
float4 Sphere;
float3 Color;
float3 Direction;
float SpotTheta;
float SpotPhiMinusTheta;
};
struct SLightmapTexel
{
float3 WorldPos;
float3 Normal;
float3 Tangent;
};
struct SIdStack
{
uint Data[MAX_STACK_SIZE];
uint StackPointer;
};
/* === Uniforms === */
cbuffer BufferMain : register(b0)
{
float4x4 WVPMatrix : packoffset(c0);
float4 AmbientColor : packoffset(c4);
uint NumLights : packoffset(c5);
uint2 LightmapSize : packoffset(c5.y);
};
cbuffer BufferRadiositySetup : register(b1)
{
uint NumRadiosityRays : packoffset(c0);
float RadiosityFactor : packoffset(c0.y); // (1.0 / NumRadiosityRays) * Factor
}
cbuffer BufferRadiosityRays : register(b2)
{
float4 RadiosityDirections[4096];
};
/*
* ======= Compute shader: =======
*/
/* === Uniforms === */
StructuredBuffer<SLightSource> LightList : register(t0);
StructuredBuffer<SLightmapTexel> Lightmap : register(t1); // Active lightmap texels (one draw-call for every lightmap texture)
//****INFO****: Hatte ich zuvor ohne das "RW" aber dann könnte ich das "allow_uav_condition" Attribut nicht verwenden.
// cbuffer nutzen mir hier leider auch nichts.
RWStructuredBuffer<STriangle> TriangleList : register(u2);
RWStructuredBuffer<SKDTreeNode> NodeList : register(u3);
RWBuffer<uint> TriangleIdList : register(u4);
RWTexture2D<float4> OutputLightmap : register(u0);
/* === Functions === */
bool StackEmpty(SIdStack Stack)
{
return Stack.StackPointer == 0;
}
void StackPush(SIdStack Stack, inout uint StackPointer, uint Id)
{
Stack.Data[StackPointer] = Id;
++StackPointer;
}
uint StackPop(SIdStack Stack, inout uint StackPointer)
{
--StackPointer;
return Stack.Data[StackPointer];
}
SPlane BuildPlane(STriangle Tri)
{
SPlane Plane;
float3 U = Tri.B - Tri.A;
float3 V = Tri.C - Tri.A;
PLANE_NORMAL(Plane) = normalize(cross(U, V));
PLANE_DISTANCE(Plane) = dot(PLANE_NORMAL(Plane), Tri.A);
return Plane;
}
bool IntersectionLinePlane(SPlane Plane, SLine Line, out float3 Intersection)
{
float3 Dir = Line.End - Line.Start;
float t = (PLANE_DISTANCE(Plane) - dot(PLANE_NORMAL(Plane), Line.Start)) / dot(PLANE_NORMAL(Plane), Dir);
if (t >= 0.0 && t <= 1.0)
{
Intersection = Line.Start + Dir * CAST(float3, t);
return true;
}
return false;
}
bool IntersectionLineTriangle(STriangle Tri, SLine Line, out float3 Intersection)
{
float3 pq = Line.End - Line.Start;
float3 pa = Tri.A - Line.Start;
float3 pb = Tri.B - Line.Start;
float3 pc = Tri.C - Line.Start;
/* Check if pq is inside the edges bc, ca and ab */
Intersection.x = dot(pb, cross(pq, pc));
if (Intersection.x < 0.0)
return false;
Intersection.y = dot(pc, cross(pq, pa));
if (Intersection.y < 0.0)
return false;
Intersection.z = dot(pa, cross(pq, pb));
if (Intersection.z < 0.0)
return false;
return IntersectionLinePlane(BuildPlane(Tri), Line, Intersection);
}
bool OverlapLineTriangle(STriangle Tri, SLine Line)
{
float3 Intersection = CAST(float3, 0.0);
if (IntersectionLineTriangle(Tri, Line, Intersection))
{
return
distance(Intersection, Line.Start) < EPSILON &&
distance(Intersection, Line.End) < EPSILON;
}
return false;
}
void StackPushNodeChildren(SIdStack Stack, inout uint StackPointer, SKDTreeNode Node, SLine Line)
{
/* Get line segment in other representation */
float3 Vec = Line.End - Line.Start;
float tmax = length(Vec);
Vec = normalize(Vec);
int Axis = Node.Axis;
int First = (Line.Start[Axis] > Node.Distance);
if (Vec[Axis] == 0.0)
{
/* Line segment parallel to splitting plane, visit near side only */
StackPush(Stack, StackPointer, Node.ChildIds[First]);
}
else
{
float t = (Node.Distance - Line.Start[Axis]) / Vec[Axis];
/* Check if line segment straddles splitting plane */
if (t >= 0.0 && t <= tmax)
{
/* Traverse near side first, then far side */
StackPush(Stack, StackPointer, Node.ChildIds[First]);
StackPush(Stack, StackPointer, Node.ChildIds[First ^ 1]);
}
else
{
/* Just traverse near side */
StackPush(Stack, StackPointer, Node.ChildIds[First]);
}
}
}
float GetAngle(float3 a, float3 b)
{
return acos(dot(a, b));
}
float GetSpotLightIntensity(float3 LightDir, SLightSource Light)
{
/* Compute spot light cone */
float Angle = GetAngle(LightDir, Light.Direction);
float ConeAngleLerp = (Angle - Light.SpotTheta) / Light.SpotPhiMinusTheta;
return saturate(1.0 - ConeAngleLerp);
}
void ComputeLightShading(inout float3 Color, SLightSource Light, SLightmapTexel Texel)
{
/* Compute light direction vector */
float3 LightDir = CAST(float3, 0.0);
if (Light.Type != LIGHT_DIRECTIONAL)
LightDir = normalize(Texel.WorldPos - SPHERE_POINT(Light.Sphere));
else
LightDir = Light.Direction;
/* Compute phong shading */
float NdotL = max(0.0, -dot(Texel.Normal, LightDir));
/* Compute light attenuation */
float Distance = distance(Texel.WorldPos, SPHERE_POINT(Light.Sphere));
float AttnLinear = Distance * SPHERE_RADIUS(Light.Sphere);
float AttnQuadratic = AttnLinear * Distance;
float Intensity = 1.0 / (1.0 + AttnLinear + AttnQuadratic);
if (Light.Type == LIGHT_SPOT)
Intensity *= GetSpotLightIntensity(LightDir, Light);
/* Compute diffuse color */
Color += Light.Color * CAST(float3, Intensity * NdotL);
}
bool TexelVisibleFromLight(SLightSource Light, SLightmapTexel Texel)
{
/* Setup line segment */
SLine Line;
Line.Start = SPHERE_POINT(Light.Sphere);
Line.End = Texel.WorldPos;
/* Initialize node ID stack */
SIdStack Stack;
for (uint i = 0; i < MAX_STACK_SIZE; ++i)
Stack.Data[i] = 0;
Stack.StackPointer = 0;
uint Id = 0;
StackPush(Stack, Stack.StackPointer, Id);
/* Iterate over all affected tree nodes */
[allow_uav_condition]
while (!StackEmpty(Stack))
{
/* Get next tree node */
Id = StackPop(Stack, Stack.StackPointer);
SKDTreeNode Node = NodeList[Id];
/* Check if this is a node leaf */
if (Node.TriangleStart != ID_NONE)
{
/* Iterate over all triangles inside the tree node */
[allow_uav_condition]
//****INFO****: Wenn ich hier nicht "[allow_uav_condition]" davor schreibe,
//beschwert sich der Compiler, dass die Abbruchbedingung nicht von einem UAV
//abhängen darf. Mit diesem Attribut kommt aber wieder der Fehler "cannot unroll loop".
for (uint i = Node.TriangleStart, n = i + Node.NumTriangles; i < n; ++i)
{
/* Get current triangle */
uint TriIndex = TriangleIdList[i];
STriangle Tri = TriangleList[TriIndex];
/* Make intersection tests */
if (OverlapLineTriangle(Tri, Line))
return false;
}
}
else
{
/* Push child nodes which are affected by the line segment */
//****INFO****: Wenn ich diese Zeile entferne läuft alles, allerdings wird dann halt auch fast alles weg-optimiert.
StackPushNodeChildren(Stack, Stack.StackPointer, Node, Line);
}
}
return true;
}
float3 GetRandomRayDirection(float3x3 NormalMatrix, uint Index)
{
return MUL(NormalMatrix, RadiosityDirections[Index].xyz);
}
bool SampleLightEnergy(SRay Ray, out float3 Color, out float Distance)
{
Color = CAST(float3, 0.0);
Distance = 0.0;
//todo...
return false;
}
void ComputeRadiosityShading(inout float3 Color, SRay Ray, float3 TexelNormal)
{
/* Sampel light energy along specified ray */
float3 SampleColor = CAST(float3, 0.0);
float SampleDistance = 0.0;
if (SampleLightEnergy(Ray, SampleColor, SampleDistance))
{
/* Add light energy to final texel color */
float NdotL = max(0.0, dot(TexelNormal, Ray.Direction));
Color += SampleColor * CAST(float3, NdotL * RadiosityFactor);
}
}
void GenerateLightmapTexel(inout float3 Color, SLightSource Light, SLightmapTexel Texel)
{
/* Compute direct illumination */
if (TexelVisibleFromLight(Light, Texel))
ComputeLightShading(Color, Light, Texel);
/* Compute radiosity */
if (NumRadiosityRays > 0)
{
/* Generate normal matrix (tangent space) */
float3x3 NormalMatrix = float3x3(
Texel.Tangent,
cross(Texel.Normal, Texel.Tangent),
Texel.Normal
);
/* Sample radiosity rays */
SRay Ray;
Ray.Origin = Texel.WorldPos;
for (uint i = 0; i < NumRadiosityRays; ++i)
{
Ray.Direction = GetRandomRayDirection(NormalMatrix, i);
ComputeRadiosityShading(Color, Ray, Texel.Normal);
}
}
}
[numthreads(8, 8, 1)]
void ComputeMain(
uint3 GroupId : SV_GroupID,
uint3 LocalId : SV_GroupThreadID,
uint3 GlobalId : SV_DispatchThreadID)
{
float4 Color = AmbientColor;
/* Get current lightmap texel */
uint2 TexelPos = GlobalId.xy;
SLightmapTexel Texel = Lightmap[TexelPos.y * LightmapSize.x + TexelPos.x];
/* Generate lightmap texel for each light source */
[allow_uav_condition]
//****INFO****: Hier kommt der Fehler "forced to unroll loop, but unrolling failed.".
//Diese Schleife muss aber Dynamisch bleiben.
for (uint i = 0; i < NumLights; ++i)
GenerateLightmapTexel(Color.rgb, LightList[i], Texel);
OutputLightmap[TexelPos] = Color;
}
|