yum-mirror/slang

Making it easier to work with shaders

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

lucy96chenExpanded gfx::Format to include additional formats (#1982)dcc2b854a

master
7.3 KiB220 linesraw
1// format-test-shaders.slang - Shaders used by the unit tests in format-uint-tests.cpp
2
3// Copy the contents of "tex" into "buffer". These are for textures containing UINT data.
4[shader("compute")]
5[numthreads(4,1,1)]
6void copyTexUint4(
7    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
8    uniform Texture2D<uint4> tex,
9    uniform RWStructuredBuffer<uint> buffer)
10{
11    uint width;
12    uint height;
13    tex.GetDimensions(width, height);
14    uint4 result = tex[uint2(sv_dispatchThreadID.x % width, sv_dispatchThreadID.x / width)];
15    buffer[sv_dispatchThreadID.x * 4] = result.r;
16    buffer[sv_dispatchThreadID.x * 4 + 1] = result.g;
17    buffer[sv_dispatchThreadID.x * 4 + 2] = result.b;
18    buffer[sv_dispatchThreadID.x * 4 + 3] = result.a;
19}
20
21[shader("compute")]
22[numthreads(4,1,1)]
23void copyTexUint3(
24    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
25    uniform Texture2D<uint4> tex,
26    uniform RWStructuredBuffer<uint> buffer)
27{
28    uint width;
29    uint height;
30    tex.GetDimensions(width, height);
31    uint4 result = tex[uint2(sv_dispatchThreadID.x % width, sv_dispatchThreadID.x / width)];
32    buffer[sv_dispatchThreadID.x * 3] = result.r;
33    buffer[sv_dispatchThreadID.x * 3 + 1] = result.g;
34    buffer[sv_dispatchThreadID.x * 3 + 2] = result.b;
35}
36
37[shader("compute")]
38[numthreads(4,1,1)]
39void copyTexUint2(
40    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
41    uniform Texture2D<uint2> tex,
42    uniform RWStructuredBuffer<uint> buffer)
43{
44    uint width;
45    uint height;
46    tex.GetDimensions(width, height);
47    uint2 result = tex[uint2(sv_dispatchThreadID.x % width, sv_dispatchThreadID.x / width)];
48    buffer[sv_dispatchThreadID.x * 2] = result.r;
49    buffer[sv_dispatchThreadID.x * 2 + 1] = result.g;
50}
51
52[shader("compute")]
53[numthreads(4,1,1)]
54void copyTexUint(
55    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
56    uniform Texture2D<uint> tex,
57    uniform RWStructuredBuffer<uint> buffer)
58{
59    uint width;
60    uint height;
61    tex.GetDimensions(width, height);
62    buffer[sv_dispatchThreadID.x] = tex[uint2(sv_dispatchThreadID.x % width, sv_dispatchThreadID.x / width)];
63}
64
65// Copy the contents of "tex" into "buffer". These are for textures containing SINT data.
66[shader("compute")]
67[numthreads(4,1,1)]
68void copyTexInt4(
69    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
70    uniform Texture2D<int4> tex,
71    uniform RWStructuredBuffer<int> buffer)
72{
73    uint width;
74    uint height;
75    tex.GetDimensions(width, height);
76    int4 result = tex[uint2(sv_dispatchThreadID.x % width, sv_dispatchThreadID.x / width)];
77    buffer[sv_dispatchThreadID.x * 4] = result.r;
78    buffer[sv_dispatchThreadID.x * 4 + 1] = result.g;
79    buffer[sv_dispatchThreadID.x * 4 + 2] = result.b;
80    buffer[sv_dispatchThreadID.x * 4 + 3] = result.a;
81}
82
83[shader("compute")]
84[numthreads(4,1,1)]
85void copyTexInt3(
86    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
87    uniform Texture2D<int4> tex,
88    uniform RWStructuredBuffer<int> buffer)
89{
90    uint width;
91    uint height;
92    tex.GetDimensions(width, height);
93    int4 result = tex[uint2(sv_dispatchThreadID.x % width, sv_dispatchThreadID.x / width)];
94    buffer[sv_dispatchThreadID.x * 3] = result.r;
95    buffer[sv_dispatchThreadID.x * 3 + 1] = result.g;
96    buffer[sv_dispatchThreadID.x * 3 + 2] = result.b;
97}
98
99[shader("compute")]
100[numthreads(4,1,1)]
101void copyTexInt2(
102    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
103    uniform Texture2D<int2> tex,
104    uniform RWStructuredBuffer<int> buffer)
105{
106    uint width;
107    uint height;
108    tex.GetDimensions(width, height);
109    int2 result = tex[uint2(sv_dispatchThreadID.x % width, sv_dispatchThreadID.x / width)];
110    buffer[sv_dispatchThreadID.x * 2] = result.r;
111    buffer[sv_dispatchThreadID.x * 2 + 1] = result.g;
112}
113
114[shader("compute")]
115[numthreads(4,1,1)]
116void copyTexInt(
117    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
118    uniform Texture2D<int> tex,
119    uniform RWStructuredBuffer<int> buffer)
120{
121    uint width;
122    uint height;
123    tex.GetDimensions(width, height);
124    buffer[sv_dispatchThreadID.x] = tex[uint2(sv_dispatchThreadID.x % width, sv_dispatchThreadID.x / width)];
125}
126
127// Copy the contents of "tex" into "buffer". These are for textures containing FLOAT data.
128[shader("compute")]
129[numthreads(4,1,1)]
130void copyTexFloat4(
131    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
132    uniform Texture2D<float4> tex,
133    uniform RWStructuredBuffer<float> buffer)
134{
135    uint width;
136    uint height;
137    tex.GetDimensions(width, height);
138    float4 result = tex[uint2(sv_dispatchThreadID.x % width, sv_dispatchThreadID.x / width)];
139    buffer[sv_dispatchThreadID.x * 4] = result.r;
140    buffer[sv_dispatchThreadID.x * 4 + 1] = result.g;
141    buffer[sv_dispatchThreadID.x * 4 + 2] = result.b;
142    buffer[sv_dispatchThreadID.x * 4 + 3] = result.a;
143}
144
145[shader("compute")]
146[numthreads(4,1,1)]
147void copyTexFloat3(
148    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
149    uniform Texture2D<float4> tex,
150    uniform RWStructuredBuffer<float> buffer)
151{
152    uint width;
153    uint height;
154    tex.GetDimensions(width, height);
155    float4 result = tex[uint2(sv_dispatchThreadID.x % width, sv_dispatchThreadID.x / width)];
156    buffer[sv_dispatchThreadID.x * 3] = result.r;
157    buffer[sv_dispatchThreadID.x * 3 + 1] = result.g;
158    buffer[sv_dispatchThreadID.x * 3 + 2] = result.b;
159}
160
161[shader("compute")]
162[numthreads(4,1,1)]
163void copyTexFloat2(
164    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
165    uniform Texture2D<float2> tex,
166    uniform RWStructuredBuffer<float> buffer)
167{
168    uint width;
169    uint height;
170    tex.GetDimensions(width, height);
171    float2 result = tex[uint2(sv_dispatchThreadID.x % width, sv_dispatchThreadID.x / width)];
172    buffer[sv_dispatchThreadID.x * 2] = result.r;
173    buffer[sv_dispatchThreadID.x * 2 + 1] = result.g;
174}
175
176[shader("compute")]
177[numthreads(4,1,1)]
178void copyTexFloat(
179    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
180    uniform Texture2D<float> tex,
181    uniform RWStructuredBuffer<float> buffer)
182{
183    uint width;
184    uint height;
185    tex.GetDimensions(width, height);
186    buffer[sv_dispatchThreadID.x] = tex[uint2(sv_dispatchThreadID.x % width, sv_dispatchThreadID.x / width)];
187}
188
189// Sample from "tex" at texture coordinates (0.5, 0.5) and save the results in "buffer".
190[shader("compute")]
191[numthreads(4,1,1)]
192void sampleTex(
193    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
194    uniform Texture2D<float4> tex,
195    uniform SamplerState sampler,
196    uniform RWStructuredBuffer<float> buffer)
197{
198    float4 result = tex.SampleLevel(sampler, float2(0.5, 0.5), 0);
199    buffer[sv_dispatchThreadID.x * 4] = result.r;
200    buffer[sv_dispatchThreadID.x * 4 + 1] = result.g;
201    buffer[sv_dispatchThreadID.x * 4 + 2] = result.b;
202    buffer[sv_dispatchThreadID.x * 4 + 3] = result.a;
203}
204
205// Sample from "tex" at texture coordinates (0.5, 0.5) and save the resuls in "buffer".
206// This should only be used with textures containing two mip levels.
207[shader("compute")]
208[numthreads(2,1,1)]
209void sampleMips(
210    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
211    uniform Texture2D<float4> tex,
212    uniform SamplerState sampler,
213    uniform RWStructuredBuffer<float> buffer)
214{
215    float4 result = tex.SampleLevel(sampler, float2(0.5, 0.5), float(sv_dispatchThreadID.x));
216    buffer[sv_dispatchThreadID.x * 4] = result.r;
217    buffer[sv_dispatchThreadID.x * 4 + 1] = result.g;
218    buffer[sv_dispatchThreadID.x * 4 + 2] = result.b;
219    buffer[sv_dispatchThreadID.x * 4 + 3] = result.a;
220}