yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

lucy96chenTexture views/shapes tests part 1 (#2179)2aac37007

master
6.7 KiB303 linesraw
1// trivial-copy-textures.slang
2
3typedef uint4 Element;
4
5// UNORDERED ACCESS VIEW
6[shader("compute")]
7[numthreads(1,1,1)]
8void resourceViewTest3DUnordered(
9    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
10    uniform uint width,
11    uniform uint height,
12    uniform uint depth,
13    uniform RWTexture3D<Element> resourceView,
14    uniform RWStructuredBuffer<Element> testResults)
15{
16    uint tid = sv_dispatchThreadID.x;
17
18    uint tmp = tid;
19    uint x = tmp % width; tmp /= width;
20    uint y = tmp % height; tmp /= height;
21    uint z = tmp;
22
23    uint3 coord = uint3(x, y, z);
24
25    // Read from resourceView
26    Element elem = resourceView[coord];
27
28    // Write to resourceView (if possible)
29    resourceView[coord] = Element(1);
30
31    // Write something to testResults
32    testResults[tid] = elem;
33}
34
35[shader("compute")]
36[numthreads(1,1,1)]
37void resourceViewTest2DUnordered(
38    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
39    uniform uint width,
40    uniform uint height,
41    uniform uint depth,
42    uniform RWTexture2D<Element> resourceView,
43    uniform RWStructuredBuffer<Element> testResults)
44{
45    uint tid = sv_dispatchThreadID.x;
46
47    uint tmp = tid;
48    uint x = tmp % width; tmp /= width;
49    uint y = tmp % height; tmp /= height;
50    uint z = tmp;
51
52    uint3 coord = uint3(x, y, z);
53
54    // Read from resourceView
55    Element elem = resourceView[coord.xy];
56
57    // Write to resourceView (if possible)
58    resourceView[coord.xy] = Element(1);
59
60    // Write something to testResults
61    testResults[tid] = elem;
62}
63
64[shader("compute")]
65[numthreads(1,1,1)]
66void resourceViewTest1DUnordered(
67    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
68    uniform uint width,
69    uniform uint height,
70    uniform uint depth,
71    uniform RWTexture1D<Element> resourceView,
72    uniform RWStructuredBuffer<Element> testResults)
73{
74    uint tid = sv_dispatchThreadID.x;
75
76    uint tmp = tid;
77    uint x = tmp % width; tmp /= width;
78    uint y = tmp % height; tmp /= height;
79    uint z = tmp;
80
81    uint3 coord = uint3(x, y, z);
82
83    // Read from resourceView
84    Element elem = resourceView[coord.x];
85
86    // Write to resourceView (if possible)
87    resourceView[coord.x] = Element(1);
88
89    // Write something to testResults
90    testResults[tid] = elem;
91}
92
93// SHADER RESOURCE VIEW
94[shader("compute")]
95[numthreads(1,1,1)]
96void resourceViewTest3DShader(
97    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
98    uniform uint width,
99    uniform uint height,
100    uniform uint depth,
101    uniform Texture3D<Element> resourceView,
102    uniform RWStructuredBuffer<Element> testResults)
103{
104    uint tid = sv_dispatchThreadID.x;
105
106    uint tmp = tid;
107    uint x = tmp % width; tmp /= width;
108    uint y = tmp % height; tmp /= height;
109    uint z = tmp;
110
111    uint3 coord = uint3(x, y, z);
112
113    // Read from resourceView
114    Element elem = resourceView[coord];
115
116    // Write something to testResults
117    testResults[tid] = elem;
118}
119
120[shader("compute")]
121[numthreads(1,1,1)]
122void resourceViewTest2DShader(
123    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
124    uniform uint width,
125    uniform uint height,
126    uniform uint depth,
127    uniform Texture2D<Element> resourceView,
128    uniform RWStructuredBuffer<Element> testResults)
129{
130    uint tid = sv_dispatchThreadID.x;
131
132    uint tmp = tid;
133    uint x = tmp % width; tmp /= width;
134    uint y = tmp % height; tmp /= height;
135    uint z = tmp;
136
137    uint3 coord = uint3(x, y, z);
138
139    // Read from resourceView
140    Element elem = resourceView[coord.xy];
141
142    // Write something to testResults
143    testResults[tid] = elem;
144}
145
146[shader("compute")]
147[numthreads(1,1,1)]
148void resourceViewTest1DShader(
149    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
150    uniform uint width,
151    uniform uint height,
152    uniform uint depth,
153    uniform Texture1D<Element> resourceView,
154    uniform RWStructuredBuffer<Element> testResults)
155{
156    uint tid = sv_dispatchThreadID.x;
157
158    uint tmp = tid;
159    uint x = tmp % width; tmp /= width;
160    uint y = tmp % height; tmp /= height;
161    uint z = tmp;
162
163    uint3 coord = uint3(x, y, z);
164
165    // Read from resourceView
166    Element elem = resourceView[coord.x];
167
168    // Write something to testResults
169    testResults[tid] = elem;
170}
171
172// RENDER TARGET AND DEPTH STENCIL VIEWS
173// Per-vertex attributes to be assembled from bound vertex buffers.
174struct AssembledVertex
175{
176    float3	position : POSITION;
177    float3	color    : COLOR;
178};
179
180// Output of the vertex shader, and input to the fragment shader.
181struct CoarseVertex
182{
183    float3 color;
184};
185
186// Output of the fragment shader
187struct Fragment
188{
189    float4 color;
190};
191
192// Vertex  Shader
193
194struct VertexStageOutput
195{
196    CoarseVertex    coarseVertex    : CoarseVertex;
197    float4          sv_position     : SV_Position;
198};
199
200[shader("vertex")]
201VertexStageOutput vertexMain(
202    AssembledVertex assembledVertex)
203{
204    VertexStageOutput output;
205
206    float3 position = assembledVertex.position;
207    float3 color    = assembledVertex.color;
208
209    output.coarseVertex.color = color;
210    output.sv_position = float4(position, 1.0);
211
212    return output;
213}
214
215// Fragment Shader
216
217[shader("fragment")]
218float4 fragmentMain() : SV_Target
219{
220    return float4(1.0, 2.0, 3.0, 4.0);
221}
222
223
224#if 0
225typedef uint4 Element;
226
227interface ITestCase
228{
229    Element doTest(uint3 coord);
230}
231
232// 1D + array, 2D + array + multisample + multisample array, cube + array
233
234struct UAV_3D : ITestCase
235{
236    uniform RWTexture3D<Element> resourceView,
237
238    Element doTest(uint3 coord)
239    {
240        // Read from resourceView
241        Element elem = resourceView[coord];
242
243        // Write to resourceView (if possible)
244        resourceView[coord] = Element(1);
245
246        return elem;
247    }
248}
249
250struct UAV_2D : ITestCase
251{
252    uniform RWTexture2D<Element> resourceView,
253
254    Element doTest(uint3 coord)
255    {
256        // Read from resourceView
257        Element elem = resourceView[coord.xy];
258
259        // Write to resourceView (if possible)
260        resourceView[coord.xy] = Element(1);
261
262        return elem;
263    }
264}
265
266struct SRV_3D : ITestCase
267{
268    uniform Texture3D<Element> resourceView,
269
270    Element doTest(uint3 coord)
271    {
272        // Read from resourceView
273        Element elem = resourceView[coord];
274
275        return elem;
276    }
277}
278
279// Copy the contents of "src" into "dst". These are for 2D textures containing UINT data.
280[shader("compute")]
281[numthreads(1,1,1)]
282void resourceViewTest<T:ITestCase>(
283    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
284    uniform uint width,
285    uniform uint height,
286    uniform uint depth,
287    uniform T testCase,
288    uniform RWStructuredBuffer<Element> testResults)
289{
290    uint tid = sv_dispatchThreadID.x;
291    
292    uint tmp = tid;
293    uint x = tmp % width; tmp /= width;
294    uint y = tmp % height; tmp /= height;
295    uint z = tmp;
296
297    uint3 coord = uint3(x, y, z);
298    Element elem = testCase.doTest(coord);
299
300    // Write something to testResults
301    testResults[tid] = elem;
302}
303#endif