-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclassify_gpu_enqueue_cs.hlsl
More file actions
149 lines (118 loc) · 4.47 KB
/
classify_gpu_enqueue_cs.hlsl
File metadata and controls
149 lines (118 loc) · 4.47 KB
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
// Copyright (c) 2023-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.
#define TILE_SIZE 16
#if defined(NODE_AGGREGATION) || defined(NODE_THREAD)
#define MAX_ALLOCATIONS 256 // TILE_SIZE*TILE_SIZE
#else
#define MAX_ALLOCATIONS 1 // 1 payload per tile
#endif
typedef vector<uint16_t, 2> uint2_16;
struct InputPayload
{
uint3 grid_size : SV_DispatchGrid;
};
struct OutputPayload
{
#ifdef NODE_DYNAMIC_EXPANSION
uint3 grid_size;
#endif
uint2_16 coord; // tile or pixel coordinates
};
// All shaders must use the same resource bindings (it's the same pipeline)
[[vk::binding(2, 0)]] Texture2D<uint> inMaterial;
[[vk::binding(5, 0)]] Texture2D<float> inDepth;
[[vk::constant_id(2)]] const uint NumMaterials = 0;
#define BACKGROUND_BIT 0
#define MODEL_BIT_BASE 1
// Index 0 is the background. Index 1 and higher are consecutive model materials.
groupshared uint sharedShaderBits[10]; // 10 elements at most
[Shader("node")]
[NodeID("classify")]
[NodeLaunch("broadcasting")]
[NodeIsProgramEntry]
[NodeMaxDispatchGrid(512, 512, 1)]
[NumThreads(TILE_SIZE, TILE_SIZE, 1)]
void main(
const uint svGroupIndex : SV_GroupIndex,
const uint3 svGroupId : SV_GroupID,
const uint3 svGroupThreadId : SV_GroupThreadID,
DispatchNodeInputRecord<InputPayload> in_payload,
[NodeID("compose", 0)]
[NodeArraySize(3)]
[MaxRecords(MAX_ALLOCATIONS)]
NodeOutputArray<OutputPayload> node_out)
{
const int3 coord = int3(TILE_SIZE * svGroupId.xy + svGroupThreadId.xy, 0); // Z is mip level
float depth = inDepth.Load(coord).r;
#if defined(NODE_AGGREGATION) || defined(NODE_THREAD)
uint shaderIndex;
if (depth == 0.0)
{
shaderIndex = 1 << BACKGROUND_BIT;
}
else
{
const uint material = inMaterial.Load(coord).r; // zero-based material index
const uint materialBit = material + MODEL_BIT_BASE;
shaderIndex = 1u << materialBit;
}
shaderIndex -= 1; // background only is shader index 0
const uint recordCount = 1; // one record per invocation
ThreadNodeOutputRecords<OutputPayload> out_payload = node_out[shaderIndex].GetThreadNodeOutputRecords(recordCount);
out_payload[0].coord = uint2_16(coord.xy);
out_payload.OutputComplete();
#else // not NODE_AGGREGATION
if (svGroupIndex == 0)
{
for (int i = 0; i < (NumMaterials + 1); ++i)
{
sharedShaderBits[i] = 0;
}
}
GroupMemoryBarrierWithGroupSync();
if (depth == 0.0)
{
sharedShaderBits[BACKGROUND_BIT] = 1u << BACKGROUND_BIT;
}
else
{
const uint material = inMaterial.Load(coord).r; // zero-based material index
const uint materialBit = material + MODEL_BIT_BASE;
sharedShaderBits[materialBit] = 1u << materialBit;
}
GroupMemoryBarrierWithGroupSync();
uint shaderIndex = 0;
for (int i = 0; i < (NumMaterials + 1); ++i)
{
shaderIndex += sharedShaderBits[i];
}
shaderIndex -= 1; // background only is shader index 0
const uint recordCount = 1; // only one payload
GroupNodeOutputRecords<OutputPayload> out_payload = node_out[shaderIndex].GetGroupNodeOutputRecords(recordCount);
if (svGroupIndex == 0)
{
#ifdef NODE_DYNAMIC_EXPANSION
out_payload[0].grid_size = uint3(1, 1, 1);
#endif
out_payload[0].coord = uint2_16(svGroupId.xy);
}
out_payload.OutputComplete();
#endif // not NODE_AGGREGATION
}