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
|
// main.cpp
#include <slang.h>
#include <slang-com-ptr.h>
using Slang::ComPtr;
#include "slang-gfx.h"
#include "tools/platform/window.h"
#include "source/core/slang-basic.h"
using namespace gfx;
#include <string>
#include "gpu-printing.h"
ComPtr<slang::ISession> createSlangSession(gfx::IDevice* device)
{
ComPtr<slang::ISession> slangSession = device->getSlangSession();
return slangSession;
}
ComPtr<slang::IModule> compileShaderModuleFromFile(slang::ISession* slangSession, char const* filePath)
{
SlangCompileRequest* slangRequest = nullptr;
slangSession->createCompileRequest(&slangRequest);
int translationUnitIndex = spAddTranslationUnit(slangRequest, SLANG_SOURCE_LANGUAGE_SLANG, filePath);
spAddTranslationUnitSourceFile(slangRequest, translationUnitIndex, filePath);
const SlangResult compileRes = spCompile(slangRequest);
if(auto diagnostics = spGetDiagnosticOutput(slangRequest))
{
printf("%s", diagnostics);
}
if(SLANG_FAILED(compileRes))
{
spDestroyCompileRequest(slangRequest);
return ComPtr<slang::IModule>();
}
ComPtr<slang::IModule> slangModule;
spCompileRequest_getModule(slangRequest, translationUnitIndex, slangModule.writeRef());
return slangModule;
}
struct ExampleProgram
{
int gWindowWidth = 640;
int gWindowHeight = 480;
ComPtr<gfx::IDevice> gDevice;
ComPtr<slang::ISession> gSlangSession;
ComPtr<slang::IModule> gSlangModule;
ComPtr<gfx::IShaderProgram> gProgram;
ComPtr<gfx::IPipelineLayout> gPipelineLayout;
ComPtr<gfx::IPipelineState> gPipelineState;
ComPtr<gfx::IDescriptorSet> gDescriptorSet;
Slang::Dictionary<int, std::string> gHashedStrings;
GPUPrinting gGPUPrinting;
ComPtr<gfx::IShaderProgram> loadComputeProgram(slang::IModule* slangModule, char const* entryPointName)
{
ComPtr<slang::IEntryPoint> entryPoint;
slangModule->findEntryPointByName(entryPointName, entryPoint.writeRef());
ComPtr<slang::IComponentType> linkedProgram;
entryPoint->link(linkedProgram.writeRef());
gGPUPrinting.loadStrings(linkedProgram->getLayout());
ComPtr<ISlangBlob> codeBlob;
linkedProgram->getEntryPointCode(0, 0, codeBlob.writeRef());
char const* code = (char const*) codeBlob->getBufferPointer();
char const* codeEnd = code + codeBlob->getBufferSize();
gfx::IShaderProgram::KernelDesc kernelDescs[] =
{
{ gfx::StageType::Compute, code, codeEnd },
};
gfx::IShaderProgram::Desc programDesc = {};
programDesc.pipelineType = gfx::PipelineType::Compute;
programDesc.kernels = &kernelDescs[0];
programDesc.kernelCount = 2;
auto shaderProgram = gDevice->createProgram(programDesc);
return shaderProgram;
}
Result execute()
{
IDevice::Desc deviceDesc;
Result res = gfxCreateDevice(&deviceDesc, gDevice.writeRef());
if(SLANG_FAILED(res)) return res;
gSlangSession = createSlangSession(gDevice);
gSlangModule = compileShaderModuleFromFile(gSlangSession, "kernels.slang");
gProgram = loadComputeProgram(gSlangModule, "computeMain");
if(!gProgram) return SLANG_FAIL;
IDescriptorSetLayout::SlotRangeDesc slotRanges[] =
{
IDescriptorSetLayout::SlotRangeDesc(DescriptorSlotType::StorageBuffer),
};
IDescriptorSetLayout::Desc descriptorSetLayoutDesc;
descriptorSetLayoutDesc.slotRangeCount = 1;
descriptorSetLayoutDesc.slotRanges = &slotRanges[0];
auto descriptorSetLayout = gDevice->createDescriptorSetLayout(descriptorSetLayoutDesc);
if(!descriptorSetLayout) return SLANG_FAIL;
IPipelineLayout::DescriptorSetDesc descriptorSets[] =
{
IPipelineLayout::DescriptorSetDesc( descriptorSetLayout ),
};
IPipelineLayout::Desc pipelineLayoutDesc;
pipelineLayoutDesc.renderTargetCount = 1;
pipelineLayoutDesc.descriptorSetCount = 1;
pipelineLayoutDesc.descriptorSets = &descriptorSets[0];
auto pipelineLayout = gDevice->createPipelineLayout(pipelineLayoutDesc);
if(!pipelineLayout) return SLANG_FAIL;
gPipelineLayout = pipelineLayout;
// Once we have the descriptor set layout, we can allocate
// and fill in a descriptor set to hold our parameters.
//
auto descriptorSet =
gDevice->createDescriptorSet(descriptorSetLayout, IDescriptorSet::Flag::Transient);
if(!descriptorSet) return SLANG_FAIL;
gDescriptorSet = descriptorSet;
ComputePipelineStateDesc desc;
desc.pipelineLayout = gPipelineLayout;
desc.program = gProgram;
auto pipelineState = gDevice->createComputePipelineState(desc);
if(!pipelineState) return SLANG_FAIL;
gPipelineState = pipelineState;
size_t printBufferSize = 4 * 1024; // use a small-ish (4KB) buffer for print output
IBufferResource::Desc printBufferDesc;
printBufferDesc.init(printBufferSize);
printBufferDesc.elementSize = sizeof(uint32_t);
printBufferDesc.cpuAccessFlags = IResource::AccessFlag::Read; // | Resource::AccessFlag::Write;
auto printBuffer =
gDevice->createBufferResource(IResource::Usage::UnorderedAccess, printBufferDesc);
IResourceView::Desc printBufferViewDesc;
printBufferViewDesc.type = IResourceView::Type::UnorderedAccess;
auto printBufferView = gDevice->createBufferView(printBuffer, printBufferViewDesc);
ICommandQueue::Desc queueDesc = {ICommandQueue::QueueType::Graphics};
auto queue = gDevice->createCommandQueue(queueDesc);
auto commandBuffer = queue->createCommandBuffer();
auto encoder = commandBuffer->encodeComputeCommands();
// TODO: need to copy a zero into the start of the print buffer!
gDescriptorSet->setResource(0, 0, printBufferView);
encoder->setDescriptorSet(gPipelineLayout, 0, gDescriptorSet);
encoder->setPipelineState(gPipelineState);
encoder->dispatchCompute(1, 1, 1);
encoder->endEncoding();
commandBuffer->close();
queue->executeCommandBuffer(commandBuffer);
// TODO: need to copy from the print buffer to a staging buffer...
ComPtr<ISlangBlob> blob;
gDevice->readBufferResource(printBuffer, 0, printBufferSize, blob.writeRef());
gGPUPrinting.processGPUPrintCommands(blob->getBufferPointer(), printBufferSize);
return SLANG_OK;
}
};
int main()
{
ExampleProgram app;
if (SLANG_FAILED(app.execute()))
{
return -1;
}
return 0;
}
|