-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathmain.cpp
162 lines (123 loc) · 5.09 KB
/
main.cpp
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
// main.cpp
#include <string>
#include <slang.h>
#include <slang-com-ptr.h>
using Slang::ComPtr;
#include "gpu-printing.h"
#include "slang-gfx.h"
#include "gfx-util/shader-cursor.h"
#include "tools/platform/window.h"
#include "source/core/slang-basic.h"
#include "examples/example-base/example-base.h"
using namespace gfx;
static const ExampleResources resourceBase("gpu-printing");
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::IPipelineState> gPipelineState;
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());
gfx::IShaderProgram::Desc programDesc = {};
programDesc.slangGlobalScope = linkedProgram;
auto shaderProgram = gDevice->createProgram(programDesc);
return shaderProgram;
}
Result execute()
{
IDevice::Desc deviceDesc;
Result res = gfxCreateDevice(&deviceDesc, gDevice.writeRef());
if(SLANG_FAILED(res)) return res;
Slang::String path = resourceBase.resolveResource("kernels.slang");
gSlangSession = createSlangSession(gDevice);
gSlangModule = compileShaderModuleFromFile(gSlangSession, path.getBuffer());
if(!gSlangModule)
return SLANG_FAIL;
gProgram = loadComputeProgram(gSlangModule, "computeMain");
if(!gProgram)
return SLANG_FAIL;
ComputePipelineStateDesc desc;
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.type = IResource::Type::Buffer;
printBufferDesc.sizeInBytes = printBufferSize;
printBufferDesc.elementSize = sizeof(uint32_t);
printBufferDesc.defaultState = ResourceState::UnorderedAccess;
printBufferDesc.allowedStates = ResourceStateSet(
ResourceState::CopySource, ResourceState::CopyDestination, ResourceState::UnorderedAccess);
printBufferDesc.memoryType = MemoryType::DeviceLocal;
auto printBuffer = gDevice->createBufferResource(printBufferDesc);
IResourceView::Desc printBufferViewDesc = {};
printBufferViewDesc.type = IResourceView::Type::UnorderedAccess;
printBufferViewDesc.format = Format::Unknown;
auto printBufferView = gDevice->createBufferView(printBuffer, nullptr, printBufferViewDesc);
ITransientResourceHeap::Desc transientResourceHeapDesc = {};
transientResourceHeapDesc.constantBufferSize = 256;
auto transientHeap = gDevice->createTransientResourceHeap(transientResourceHeapDesc);
ICommandQueue::Desc queueDesc = {ICommandQueue::QueueType::Graphics};
auto queue = gDevice->createCommandQueue(queueDesc);
auto commandBuffer = transientHeap->createCommandBuffer();
auto encoder = commandBuffer->encodeComputeCommands();
auto rootShaderObject = encoder->bindPipeline(gPipelineState);
auto cursor = ShaderCursor(rootShaderObject);
cursor["gPrintBuffer"].setResource(printBufferView);
encoder->dispatchCompute(1, 1, 1);
encoder->bufferBarrier(printBuffer, ResourceState::UnorderedAccess, ResourceState::CopySource);
encoder->endEncoding();
commandBuffer->close();
queue->executeCommandBuffer(commandBuffer);
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;
}