-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShaderSource.h
285 lines (244 loc) · 9.79 KB
/
ShaderSource.h
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
/**********************************************************************
Copyright (c) 2024 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
********************************************************************/
#pragma once
// This file contains the entire work graph HLSL source code as a single resource string
// The shader code will be compiled twice:
// - with target lib_6_9 for all work graph nodes, including the two mesh nodes for drawing
// - with target ps_6_9 for the pixel shader. Pixel shader cannot be included in the library object and need to be compiled separately.
namespace shader {
static const char* workGraphSource = R"(
// =========================
// Work graph record structs
// Record used for recursively generating & drawing lines
struct LineRecord
{
float2 start;
float2 end;
};
// Record used to draw a single triangle
struct TriangleDrawRecord
{
float2 verts[3];
uint depth;
};
// Number of Koch iterations
static const uint maxSnowflakeRecursions = 3;
// This node creates the triangle base for the Koch snowflake.
[Shader("node")]
[NodeIsProgramEntry]
[NodeLaunch("thread")]
void EntryNode(
// Start recursive Koch fractal on each of the three sides of the triangle
[MaxRecords(3)]NodeOutput<LineRecord> SnowflakeNode,
// Fill triangle
[MaxRecords(1)]NodeOutput<TriangleDrawRecord> TriangleMeshNode)
{
ThreadNodeOutputRecords<LineRecord> snowflakeRecords = SnowflakeNode.GetThreadNodeOutputRecords(3);
ThreadNodeOutputRecords<TriangleDrawRecord> drawRecords = TriangleMeshNode.GetThreadNodeOutputRecords(1);
const float2 v0 = float2(0., .9);
const float2 v1 = float2(+sqrt(3) * .45, -.45);
const float2 v2 = float2(-sqrt(3) * .45, -.45);
// Line v0 -> v1
snowflakeRecords.Get(0).start = v0;
snowflakeRecords.Get(0).end = v1;
// Line v1 -> v2
snowflakeRecords.Get(1).start = v1;
snowflakeRecords.Get(1).end = v2;
// Line v2 -> v0
snowflakeRecords.Get(2).start = v2;
snowflakeRecords.Get(2).end = v0;
// Triangle record
drawRecords.Get(0).depth = 0;
drawRecords.Get(0).verts[0] = v0;
drawRecords.Get(0).verts[1] = v1;
drawRecords.Get(0).verts[2] = v2;
snowflakeRecords.OutputComplete();
drawRecords.OutputComplete();
};
[Shader("node")]
[NodeLaunch("thread")]
[NodeMaxRecursionDepth(maxSnowflakeRecursions)]
void SnowflakeNode(
ThreadNodeInputRecord<LineRecord> record,
// Koch fractal recursively splits line into 4 new line segments
[MaxRecords(4)]NodeOutput<LineRecord> SnowflakeNode,
// Two of the recursive lines form edges of a triangles, which needs to be filled
[MaxRecords(1)]NodeOutput<TriangleDrawRecord> TriangleMeshNode,
// If recursion is not possible, draw a single line
[MaxRecords(1)]NodeOutput<LineRecord> LineMeshNode
) {
const float2 start = record.Get().start;
const float2 end = record.Get().end;
const bool hasOutput = GetRemainingRecursionLevels() != 0;
ThreadNodeOutputRecords<LineRecord> snowflakeRecords = SnowflakeNode.GetThreadNodeOutputRecords(hasOutput * 4);
ThreadNodeOutputRecords<TriangleDrawRecord> triRecord = TriangleMeshNode.GetThreadNodeOutputRecords(hasOutput);
ThreadNodeOutputRecords<LineRecord> lineRecord = LineMeshNode.GetThreadNodeOutputRecords(!hasOutput);
if (hasOutput) {
const float2 perpendicular = float2(start.y - end.y, end.x - start.x) * sqrt(3) / 6;
const float2 triangleLeft = lerp(start, end, 1./3.);
const float2 triangleMid = lerp(start, end, .5) + perpendicular;
const float2 triangleRight = lerp(start, end, 2./3.);
snowflakeRecords.Get(0).start = start;
snowflakeRecords.Get(0).end = triangleLeft;
snowflakeRecords.Get(1).start = triangleLeft;
snowflakeRecords.Get(1).end = triangleMid;
snowflakeRecords.Get(2).start = triangleMid;
snowflakeRecords.Get(2).end = triangleRight;
snowflakeRecords.Get(3).start = triangleRight;
snowflakeRecords.Get(3).end = end;
triRecord.Get(0).depth = 1 + (maxSnowflakeRecursions - GetRemainingRecursionLevels());
triRecord.Get(0).verts[0] = triangleLeft;
triRecord.Get(0).verts[1] = triangleMid;
triRecord.Get(0).verts[2] = triangleRight;
} else {
lineRecord.Get(0).start = start;
lineRecord.Get(0).end = end;
}
snowflakeRecords.OutputComplete();
lineRecord.OutputComplete();
triRecord.OutputComplete();
}
// =======================================================
// Vertex and primitive attribute structs for mesh shaders
struct Vertex
{
float4 position : SV_POSITION;
};
struct Primitive {
float4 color : COLOR0;
};
// ==========
// Mesh Nodes
// Mesh shader to draw a line between a start and end position.
// As lines X degree angles, we cannot draw lines a simple 2D boxes.
//
//
// v2----------v3
// / \
// v1 v4
// \ /
// v0-----------v5
//
// Triangulation:
// - v0 -> v1 -> v2
// - v0 -> v2 -> v3
// - v0 -> v3 -> v4
// - v0 -> v4 -> v5
[Shader("node")]
// Indicate that we are defining a mesh node
[NodeLaunch("mesh")]
// Mesh nodes do not automatically use the function name of the node as their node id.
// If we want to automatically add the generic program created with this mesh node to the work graph,
// we need to explicitly define a node id for it.
[NodeId("LineMeshNode", 0)]
// Mesh nodes can use [NodeDispatchGrid(...)] and [NodeMaxDispatchGrid(...)] in combination with SV_DispatchGrid.
[NodeDispatchGrid(1, 1, 1)]
// The rest of the attributes are the same as for "normal" mesh shaders.
[NumThreads(32, 1, 1)]
[OutputTopology("triangle")]
void LineMeshShader(
uint gtid : SV_GroupThreadID,
DispatchNodeInputRecord<LineRecord> inputRecord,
out indices uint3 triangles[4],
out primitives Primitive prims[4],
out vertices Vertex verts[6])
{
const LineRecord record = inputRecord.Get();
SetMeshOutputCounts(6, 4);
// Output triangles based on triangulation above
if (gtid < 4)
{
triangles[gtid] = uint3(0, gtid + 1, gtid + 2);
prims[gtid].color = float4(0.03, 0.19, 0.42, 1.0);
}
// Output vertices
if (gtid < 6) {
const float2 direction = normalize(record.end - record.start);
const float2 perpendicular = float2(direction.y, -direction.x);
const float lineWidth = 0.0075;
// Offsets for outer triangle shape
//
// offsets[2] ---- ...
// /
// offsets[1]
// \
// offsets[0] ---- ...
//
// direction <---+
// |
// v
// prependicular
//
const float2 offsets[3] = {
perpendicular,
direction * sqrt(3) / 3.0,
-perpendicular,
};
// Shift entire line end outwards by sqrt(3) / 3.0 to align with connecting line
const float2 offset = (direction * sqrt(3) / 3.0) + offsets[gtid % 3];
const float2 position = (gtid < 3)? record.start - offset * lineWidth
: record.end + offset * lineWidth;
verts[gtid].position = float4(position, 0.25, 1.0);
}
}
// Color palette for different depth levels
float4 GetTriangleColor(in uint depth) {
switch (depth % 4) {
case 0: return float4(0.13, 0.44, 0.71, 1.0); // Triangle Recursion 0
case 1: return float4(0.42, 0.68, 0.84, 1.0); // Triangle Recursion 1
case 2: return float4(0.74, 0.84, 0.91, 1.0); // Triangle Recursion 2
case 3: return float4(0.94, 0.95, 1.00, 1.0); // Triangle Recursion 3
default: return 0;
}
}
[Shader("node")]
[NodeLaunch("mesh")]
// To demonstrate how to override a mesh node id when creating the work graph, we don't specify a node id for this mesh shader.
// The node id will be set using a mesh node launch override when creating the work graph state object (see HelloMeshNodes.cpp:160)
// [NodeId("TriangleMeshNode", 0)]
[NodeDispatchGrid(1, 1, 1)]
[NumThreads(3, 1, 1)]
[OutputTopology("triangle")]
void TriangleMeshShader(
uint gtid : SV_GroupThreadID,
DispatchNodeInputRecord<TriangleDrawRecord> inputRecord,
out indices uint3 triangles[1],
out primitives Primitive prims[1],
out vertices Vertex verts[3])
{
const TriangleDrawRecord record = inputRecord.Get();
SetMeshOutputCounts(3, 1);
if (gtid < 1)
{
triangles[0] = uint3(0, 1, 2);
prims[0].color = GetTriangleColor(record.depth);
}
if (gtid < 3)
{
verts[gtid].position = float4(record.verts[gtid], 0.5, 1);
}
}
// ================================
// Pixel Shader for both mesh nodes
float4 MeshNodePixelShader(in float4 color : COLOR0) : SV_TARGET
{
return color;
}
)";
}