yum-mirror/slang

Making it easier to work with shaders

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

skallweitNVShader cache improvements (#2564)5ce8d4c14

master
2.2 KiB68 linesraw
1// shader-cache-specialization.slang
2
3// This is a copy of `shader-object.slang` in `shader-object` example
4// for use by compute-smoke gfx unit test.
5
6// This file implements a simple compute shader that transforms
7// input floating point numbers stored in a `RWStructuredBuffer`.
8// Specifically, for each number x from input buffer, compute
9// f(x) and store the result back in the same buffer.
10
11// The compute shader supports multiple transformation functions,
12// such add(x, c) which returns x+c, or mul(x, c) which returns x*c.
13// This functions are implemented as types that conforms to the
14// `ITransformer` interface.
15
16// The main entry point function takes a parameter of `ITransformer`
17// type, and applies the transformation to numbers in the input
18// buffer. By defining the shader parameter using interfaces,
19// we enable the flexiblity to generate either specialized compute
20// kernels that performs specific transformation or a general
21// kernel that can perform any transformations encoded by the
22// parameter at run-time, without changing any shader code or
23// host-application logic for setting and preparing shader parameters.
24
25// Defines the transformer interface, which implements a single
26// `transform` operation.
27interface ITransformer
28{
29    float transform(float x);
30}
31
32// Represents a transform function f(x) = x + c.
33struct AddTransformer : ITransformer
34{
35    float c;
36    float transform(float x) { return x + c; }
37};
38
39// Represents a transform function f(x) = x * c.
40struct MulTransformer : ITransformer
41{
42    float c;
43    float transform(float x) { return x * c; }
44};
45
46// Represents a composite function f(x) = f0(f1(x));
47struct CompositeTransformer : ITransformer
48{
49    ITransformer func0;
50    ITransformer func1;
51    float transform(float x)
52    {
53        return func0.transform(func1.transform(x));
54    }
55};
56
57// Main entry-point. Applies the transformation encoded by `transformer`
58// to all elements in `buffer`.
59[shader("compute")]
60[numthreads(4,1,1)]
61void computeMain(
62    uint3 sv_dispatchThreadID : SV_DispatchThreadID,
63    uniform RWStructuredBuffer<float> buffer,
64    uniform ITransformer transformer)
65{
66    var input = buffer[sv_dispatchThreadID.x];
67    buffer[sv_dispatchThreadID.x] = transformer.transform(input);
68}