summaryrefslogtreecommitdiffstats
path: root/examples/heterogeneous-hello-world/main.cpp
blob: 47df20dc57ea4f2160c99372db74bd4d8ba5e63b (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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// main.cpp

// This file implements an extremely simple example of loading and
// executing a Slang shader program. This is primarily an example
// of how to use Slang as a "drop-in" replacement for an existing
// HLSL compiler like the `D3DCompile` API. More advanced usage
// of advanced Slang language and API features is left to the
// next example.
//
// The comments in the file will attempt to explain concepts as
// they are introduced.
//
// Of course, in order to use the Slang API, we need to include
// its header. We have set up the build options for this project
// so that it is as simple as:
//
#include <slang.h>
//
// Other build setups are possible, and Slang doesn't assume that
// its include directory must be added to your global include
// path.

// For the purposes of keeping the demo code as simple as possible,
// while still retaining some level of portability, our examples
// make use of a small platform and graphics API abstraction layer,
// which is included in the Slang source distribution under the
// `tools/` directory.
//
// Applications can of course use Slang without ever touching this
// abstraction layer, so we will not focus on it when explaining
// examples, except in places where best practices for interacting
// with Slang may depend on an application/engine making certain
// design choices in their abstraction layer.
//
#include "gfx/render.h"
#include "gfx/d3d11/render-d3d11.h"
#include "gfx/window.h"
#include "../../prelude/slang-cpp-types.h";
using namespace gfx;

// We create global ref pointers to avoid dereferencing values
//
RefPtr<gfx::ShaderProgram>         gShaderProgram;
RefPtr<gfx::ApplicationContext>    gAppContext;
RefPtr<gfx::Renderer>              gRenderer;

RefPtr<gfx::BufferResource>        gStructuredBuffer;

RefPtr<gfx::PipelineLayout>        gPipelineLayout;
RefPtr<gfx::PipelineState>         gPipelineState;
RefPtr<gfx::DescriptorSetLayout>   gDescriptorSetLayout;
RefPtr<gfx::DescriptorSet>         gDescriptorSet;

// Boilerplate types to help the slan-generated file
//
struct gfx_Window_0;
struct gfx_Renderer_0;
struct gfx_BufferResource_0;
struct gfx_ShaderProgram_0;
struct gfx_DescriptorSetLayout_0;
struct gfx_PipelineLayout_0;
struct gfx_DescriptorSet_0;
struct gfx_PipelineState_0;

bool executeComputation_0();
extern unsigned char __computeMain[];
extern size_t __computeMainSize;

gfx::ShaderProgram* loadShaderProgram(gfx::Renderer* renderer)
{
    // We extract the begin/end pointers to the output code buffers directly
    //
    char unsigned const* computeCode = __computeMain;
    char unsigned const* computeCodeEnd = computeCode + __computeMainSize;

    // Now we use the operations of the example graphics API abstraction
    // layer to load shader code into the underlying API.
    //
    // Reminder: this section does not involve the Slang API at all.
    //

    gfx::ShaderProgram::KernelDesc kernelDescs[] =
    {
        { gfx::StageType::Compute,    computeCode,     computeCodeEnd },
    };

    gfx::ShaderProgram::Desc programDesc;
    programDesc.pipelineType = gfx::PipelineType::Compute;
    programDesc.kernels = &kernelDescs[0];
    programDesc.kernelCount = 2;

    gShaderProgram = renderer->createProgram(programDesc);

    return gShaderProgram;
}

// Now that we've covered the function that actually loads and
// compiles our Slang shade code, we can go through the rest
// of the application code without as much commentary.
//
gfx::Window* createWindow(int windowWidth, int windowHeight)
{
    // Create a window for our application to render into.
    //
    WindowDesc windowDesc;
    windowDesc.title = "Hello, World!";
    windowDesc.width = windowWidth;
    windowDesc.height = windowHeight;
    return createWindow(windowDesc);
    //return globalWindow;
}

gfx::Renderer* createRenderer(
    int windowWidth,
    int windowHeight,
    gfx::Window* window)
{
    // Initialize the rendering layer.
    //
    // Note: for now we are hard-coding logic to use the
    // Direct3D11 back-end for the graphics API abstraction.
    // A future version of this example may support multiple
    // platforms/APIs.
    //
    gRenderer = createD3D11Renderer();
    Renderer::Desc rendererDesc;
    rendererDesc.width = windowWidth;
    rendererDesc.height = windowHeight;
    {
        Result res = gRenderer->initialize(rendererDesc, getPlatformWindowHandle(window));
        if (SLANG_FAILED(res)) return nullptr;
    }
    return gRenderer;
}

gfx::BufferResource* createStructuredBuffer(gfx::Renderer* renderer, float* initialArray)
{
    // Create a structured buffer for storing the data for computation
    //
    int structuredBufferSize = 4 * sizeof(float);

    BufferResource::Desc structuredBufferDesc;
    structuredBufferDesc.init(structuredBufferSize);
    structuredBufferDesc.setDefaults(Resource::Usage::UnorderedAccess);
    structuredBufferDesc.elementSize = 4;
    structuredBufferDesc.cpuAccessFlags = Resource::AccessFlag::Read;

    gStructuredBuffer = renderer->createBufferResource(
        Resource::Usage::UnorderedAccess,
        structuredBufferDesc,
        initialArray);
    return gStructuredBuffer;
}

gfx::DescriptorSetLayout* buildDescriptorSetLayout(gfx::Renderer* renderer)
{
    // Our example graphics API usess a "modern" D3D12/Vulkan style
    // of resource binding, so now we will dive into describing and
    // allocating "descriptor sets."
    //
    // First, we need to construct a descriptor set *layout*.
    //
    DescriptorSetLayout::SlotRangeDesc slotRanges[] =
    {
        DescriptorSetLayout::SlotRangeDesc(DescriptorSlotType::StorageBuffer),
    };
    DescriptorSetLayout::Desc descriptorSetLayoutDesc;
    descriptorSetLayoutDesc.slotRangeCount = 1;
    descriptorSetLayoutDesc.slotRanges = &slotRanges[0];
    gDescriptorSetLayout = renderer->createDescriptorSetLayout(descriptorSetLayoutDesc);
    return gDescriptorSetLayout;
}

gfx::PipelineLayout* buildPipeline(gfx::Renderer* renderer, gfx::DescriptorSetLayout* descriptorSetLayout)
{
    // Next we will allocate a pipeline layout, which specifies
    // that we will render with only a single descriptor set bound.
    //

    PipelineLayout::DescriptorSetDesc descriptorSets[] =
    {
        PipelineLayout::DescriptorSetDesc(descriptorSetLayout),
    };
    PipelineLayout::Desc pipelineLayoutDesc;
    pipelineLayoutDesc.renderTargetCount = 1;
    pipelineLayoutDesc.descriptorSetCount = 1;
    pipelineLayoutDesc.descriptorSets = &descriptorSets[0];
    gPipelineLayout = renderer->createPipelineLayout(pipelineLayoutDesc);

    return gPipelineLayout;
}

gfx::DescriptorSet* buildDescriptorSet(
    gfx::Renderer* renderer,
    gfx::DescriptorSetLayout* descriptorSetLayout,
    gfx::BufferResource* structuredBuffer)
{
    // Once we have the descriptor set layout, we can allocate
    // and fill in a descriptor set to hold our parameters.
    //
    gDescriptorSet = renderer->createDescriptorSet(descriptorSetLayout);
    if(!gDescriptorSet) return nullptr;

    // Once we have the bufferResource created, we can fill in
    // a descriptor set for creating a structured buffer
    //
    ResourceView::Desc resourceViewDesc;
    resourceViewDesc.type = ResourceView::Type::UnorderedAccess;
    auto resourceView = renderer->createBufferView(structuredBuffer, resourceViewDesc);
    gDescriptorSet->setResource(0, 0, resourceView);

    return gDescriptorSet;
}

gfx::PipelineState* buildPipelineState(
    gfx::ShaderProgram* shaderProgram,
    gfx::Renderer* renderer,
    gfx::PipelineLayout* pipelineLayout)
{
    // Following the D3D12/Vulkan style of API, we need a pipeline state object
    // (PSO) to encapsulate the configuration of the overall graphics pipeline.
    //
    ComputePipelineStateDesc desc;
    desc.pipelineLayout = pipelineLayout;
    desc.program = shaderProgram;
    gPipelineState = renderer->createComputePipelineState(desc);
    return gPipelineState;
}

void printInitialValues(float* initialArray, int length)
{
    // Print out the values before the computation
    printf("Before:\n");
    for (int i = 0; i < length; i++)
    {
        printf("%f, ", initialArray[i]);
    }
    printf("\n");
}

void dispatchComputation(
    gfx::Renderer* gRenderer,
    gfx::PipelineState* gPipelineState,
    gfx::PipelineLayout* gPipelineLayout,
    gfx::DescriptorSet* gDescriptorSet)
{

    gRenderer->setPipelineState(PipelineType::Compute, gPipelineState);
    gRenderer->setDescriptorSet(PipelineType::Compute, gPipelineLayout, 0, gDescriptorSet);

    gRenderer->dispatchCompute(4, 1, 1);
}

void print_output(
    gfx::Renderer* renderer,
    gfx::BufferResource* structuredBuffer,
    int length)
{
    if (float* outputData = (float*)renderer->map(structuredBuffer, MapFlavor::HostRead))
    {
        // Print out the values the the kernel produced
        printf("After: \n");
        for (int i = 0; i < 4; i++)
        {
            printf("%f, ", outputData[i]);
        }
        printf("\n");

        renderer->unmap(structuredBuffer);
    }
}

// Boilerplate functions to help the slang-generated file and types
gfx_Window_0* createWindow_0(int32_t _0, int32_t _1)
{
    return (gfx_Window_0*)createWindow(_0, _1);
}

gfx_Renderer_0* createRenderer_0(int32_t _0, int32_t _1, gfx_Window_0* _2)
{
    return (gfx_Renderer_0*)createRenderer(_0, _1, (gfx::Window*)_2);
}

gfx_BufferResource_0* createStructuredBuffer_0(gfx_Renderer_0* _0, FixedArray<float, 4> _1)
{
    return (gfx_BufferResource_0*)createStructuredBuffer((gfx::Renderer*)_0, (float*)&_1);
}

gfx_ShaderProgram_0* loadShaderProgram_0(gfx_Renderer_0* _0)
{
    return (gfx_ShaderProgram_0*)loadShaderProgram((gfx::Renderer*)_0);
}

gfx_DescriptorSetLayout_0* buildDescriptorSetLayout_0(gfx_Renderer_0* _0)
{
    return (gfx_DescriptorSetLayout_0*)buildDescriptorSetLayout((gfx::Renderer*)_0);
}

gfx_PipelineLayout_0* buildPipeline_0(gfx_Renderer_0* _0, gfx_DescriptorSetLayout_0* _1)
{
    return (gfx_PipelineLayout_0*)buildPipeline((gfx::Renderer*)_0, (gfx::DescriptorSetLayout*)_1);
}

gfx_DescriptorSet_0* buildDescriptorSet_0(gfx_Renderer_0* _0, gfx_DescriptorSetLayout_0* _1, gfx_BufferResource_0* _2)
{
    return (gfx_DescriptorSet_0*)buildDescriptorSet(
        (gfx::Renderer*)_0,
        (gfx::DescriptorSetLayout*)_1,
        (gfx::BufferResource*)_2);
}

gfx_PipelineState_0* buildPipelineState_0(gfx_ShaderProgram_0* _0, gfx_Renderer_0* _1, gfx_PipelineLayout_0* _2)
{
    return (gfx_PipelineState_0*)buildPipelineState(
        (gfx::ShaderProgram*)_0,
        (gfx::Renderer*)_1,
        (gfx::PipelineLayout*)_2);
}

void printInitialValues_0(FixedArray<float, 4> _0, int32_t _1)
{
    printInitialValues((float*)&_0, _1);
}

void dispatchComputation_0(gfx_Renderer_0* _0, gfx_PipelineState_0* _1, gfx_PipelineLayout_0* _2, gfx_DescriptorSet_0* _3)
{
    dispatchComputation(
        (gfx::Renderer*)_0,
        (gfx::PipelineState*)_1,
        (gfx::PipelineLayout*)_2,
        (gfx::DescriptorSet*)_3);
}

void print_output_0(gfx_Renderer_0* _0, gfx_BufferResource_0* _1, int32_t _2)
{
    print_output((gfx::Renderer*)_0, (gfx::BufferResource*)_1, _2);
}

// This "inner" main function is used by the platform abstraction
// layer to deal with differences in how an entry point needs
// to be defined for different platforms.
//
void innerMain(ApplicationContext* context)
{
    // We construct an instance of our example application
    // `struct` type, and then walk through the lifecyle
    // of the application.

    if (!(executeComputation_0()))
    {
        return exitApplication(context, 1);
    }
}

// This macro instantiates an appropriate main function to
// invoke the `innerMain` above.
//
GFX_CONSOLE_MAIN(innerMain)