-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompose_material_map_gpu_enqueue_cs.hlsl
More file actions
147 lines (131 loc) · 4.93 KB
/
compose_material_map_gpu_enqueue_cs.hlsl
File metadata and controls
147 lines (131 loc) · 4.93 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
// 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.
#include "common.hlsl"
#define TILE_SIZE 16
[[vk::constant_id(4)]] const uint ShaderPermutation = 0;
[[vk::constant_id(5)]] const uint AluComplexity = 0;
typedef vector<uint16_t, 2> uint2_16;
struct InputPayload
{
#ifdef NODE_DYNAMIC_EXPANSION
uint3 grid_size : SV_DispatchGrid;
#endif
uint2_16 coord; // tile or pixel coordinates
};
// All shaders must use the same resource bindings (it's the same pipeline)
[[vk::binding(0, 0)]]
cbuffer cbUbo
{
UboData ubo;
};
[[vk::binding(1, 0)]] [[vk::image_format("rgba8")]] RWTexture2D<float4> outImage;
[[vk::binding(2, 0)]] Texture2D<uint> inMaterial;
[Shader("node")]
[NodeID("compose")]
#ifdef NODE_AGGREGATION
[NodeLaunch("coalescing")]
#else
[NodeLaunch("broadcasting")]
#ifdef NODE_DYNAMIC_EXPANSION
[NodeMaxDispatchGrid(1, 1, 1)]
#else
[NodeDispatchGrid(1, 1, 1)]
#endif
#endif
[NumThreads(TILE_SIZE, TILE_SIZE, 1)]
void main(
const uint svGroupIndex : SV_GroupIndex,
const uint3 svGroupThreadId : SV_GroupThreadID,
#ifdef NODE_AGGREGATION
[MaxRecords(TILE_SIZE * TILE_SIZE)] GroupNodeInputRecords<InputPayload> in_payload
#else
DispatchNodeInputRecord<InputPayload> in_payload
#endif
)
{
#ifdef NODE_AGGREGATION
if (svGroupIndex < in_payload.Count())
#endif
{
#ifdef NODE_AGGREGATION
const int2 coord = int2(in_payload[svGroupIndex].coord);
#else
const int2 coord = int2(TILE_SIZE * in_payload.Get().coord + svGroupThreadId.xy);
#endif
// Display the original material map (albeit with different color palette).
const uint material = inMaterial.Load(int3(coord, 0)).r; // Z is mip level
float3 finalColor = getMaterialColor(material).rgb;
// Active branches -- should be compiled out if not needed based on the specialization constant.
if (ShaderPermutation < 256u)
{
const float complexity = AluComplexity / 8.0;
float noise = 0.0;
if ((ShaderPermutation & 1u) != 0) // bit 0
{
noise += abs(iterateNoise(float3(coord, 0.1), complexity));
}
if ((ShaderPermutation & 2u) != 0) // bit 1
{
noise += abs(iterateNoise(float3(coord, 0.12), complexity));
}
if ((ShaderPermutation & 4u) != 0) // bit 2
{
noise += abs(iterateNoise(float3(coord, 0.45), complexity));
}
if ((ShaderPermutation & 8u) != 0) // bit 3
{
noise += abs(iterateNoise(float3(coord, 0.75), complexity));
}
if ((ShaderPermutation & 16u) != 0) // bit 4
{
noise += abs(iterateNoise(float3(coord, 0.98), complexity));
}
if ((ShaderPermutation & 32u) != 0) // bit 5
{
noise += abs(iterateNoise(float3(coord, 0.27), complexity));
}
if ((ShaderPermutation & 64u) != 0) // bit 6
{
noise += abs(iterateNoise(float3(coord, 0.63), complexity));
}
if ((ShaderPermutation & 128u) != 0) // bit 7
{
noise += abs(iterateNoise(float3(coord, 0.32), complexity));
}
finalColor *= 0.95 + 0.05 * abs(sin(noise));
// Visual feedback for a specialized shader
if (ShaderPermutation == ubo.highlightedShaderPermutation)
{
finalColor.g = 1.0;
}
}
else
{
// ERROR, we're handling only up to 256 permutations
finalColor = float3(1, 0, 0);
}
// Visual feedback for a specialized shader
if (ShaderPermutation == ubo.highlightedShaderPermutation)
{
finalColor.g = 1.0;
}
outImage[coord] = float4(toSrgb(finalColor), 1.0);
}
}