summaryrefslogtreecommitdiff
path: root/examples/heterogeneous-hello-world/shader.slang
blob: d67fd5582aa805b5a32f85e56724d5e9e59058a3 (plain)
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
// shader.slang

//TEST_INPUT:ubuffer(random(float, 4096, -1.0, 1.0), stride=4):name=ioBuffer
RWStructuredBuffer<float> convertBuffer(Ptr<gfx::BufferResource> x);

[shader("compute")]
[numthreads(4, 1, 1)]
void computeMain(uniform RWStructuredBuffer<float> ioBuffer, uint3 dispatchThreadID : SV_DispatchThreadID)
{
    uint tid = dispatchThreadID.x;

    float i = ioBuffer[tid];
    float o = i < 0.5 ? (i + i) : sqrt(i);

    ioBuffer[tid] = o;
}

// Forward declarations of gfx types
//
namespace gfx {
    struct Device{};
    struct BufferResource{};
    struct ResourceView{};
    struct TransientResourceHeap{};
    struct PipelineState{};
    struct ShaderProgram{};
}

// Forward declarations of cpp functions
//
Ptr<gfx::Device> createDevice();
Ptr<gfx::ShaderProgram> loadShaderProgram(Ptr<gfx::Device> device, String entryPoint, String module);
Ptr<gfx::BufferResource> createStructuredBuffer(
    Ptr<gfx::Device> device,
    float[4] initialData);
Ptr<gfx::ResourceView> createBufferView(
    Ptr<gfx::Device> device,
    Ptr<gfx::BufferResource> buffer);
Ptr<gfx::TransientResourceHeap> buildTransientHeap(
    Ptr<gfx::Device> device);
Ptr<gfx::PipelineState> buildPipelineState(
    Ptr<gfx::Device> device,
    Ptr<gfx::ShaderProgram> shaderProgram);
void printInitialValues(float[4] initialArray, int length);
void dispatchComputation(
    Ptr<gfx::Device> device,
    Ptr<gfx::TransientResourceHeap> transientHeap,
    Ptr<gfx::PipelineState> pipelineState,
    Ptr<gfx::ResourceView> bufferView);
bool printOutputValues(
    Ptr<gfx::Device> device,
    Ptr<gfx::BufferResource> buffer,
    int length);

public bool executeComputation() {
    // We will hard-code the size of our initial array.
    //
    float   initialArray[4] = { 3.0f, -20.0f, -6.0f, 8.0f };

    // Declare functions
    let device = createDevice();
    let structuredBuffer = createStructuredBuffer(device, initialArray);
    let bufferView = createBufferView(device, structuredBuffer);
    __GPU_FOREACH(device, uint3(4, 1, 1), LAMBDA(uint3 dispatchThreadID)
        { computeMain(convertBuffer(structuredBuffer), dispatchThreadID) ; });
    printInitialValues(initialArray, 4);
    printOutputValues(device, structuredBuffer, 4);


    return true;
}