Skip to content

Commit f834f25

Browse files
authored
COM-ify all slang-gfx interfaces. (shader-slang#1656)
* COM-ify all slang-gfx interfaces.
1 parent ac76997 commit f834f25

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2221
-1660
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*.ilk
99
*.obj
1010
*.slang-module
11+
*.zip
1112
.clang-format
1213
bin/
1314
intermediate/

build/visual-studio/gfx/gfx.vcxproj

+4
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@
184184
<ClInclude Include="..\..\..\tools\gfx\open-gl\render-gl.h" />
185185
<ClInclude Include="..\..\..\tools\gfx\render-graphics-common.h" />
186186
<ClInclude Include="..\..\..\tools\gfx\render.h" />
187+
<ClInclude Include="..\..\..\tools\gfx\renderer-shared.h" />
187188
<ClInclude Include="..\..\..\tools\gfx\shader-cursor.h" />
189+
<ClInclude Include="..\..\..\tools\gfx\slang-gfx-helper.h" />
188190
<ClInclude Include="..\..\..\tools\gfx\surface.h" />
189191
<ClInclude Include="..\..\..\tools\gfx\vulkan\render-vk.h" />
190192
<ClInclude Include="..\..\..\tools\gfx\vulkan\vk-api.h" />
@@ -206,7 +208,9 @@
206208
<ClCompile Include="..\..\..\tools\gfx\open-gl\render-gl.cpp" />
207209
<ClCompile Include="..\..\..\tools\gfx\render-graphics-common.cpp" />
208210
<ClCompile Include="..\..\..\tools\gfx\render.cpp" />
211+
<ClCompile Include="..\..\..\tools\gfx\renderer-shared.cpp" />
209212
<ClCompile Include="..\..\..\tools\gfx\shader-cursor.cpp" />
213+
<ClCompile Include="..\..\..\tools\gfx\slang-gfx-helper.cpp" />
210214
<ClCompile Include="..\..\..\tools\gfx\surface.cpp" />
211215
<ClCompile Include="..\..\..\tools\gfx\vulkan\render-vk.cpp" />
212216
<ClCompile Include="..\..\..\tools\gfx\vulkan\vk-api.cpp" />

build/visual-studio/gfx/gfx.vcxproj.filters

+12
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,15 @@
4848
<ClInclude Include="..\..\..\tools\gfx\render.h">
4949
<Filter>Header Files</Filter>
5050
</ClInclude>
51+
<ClInclude Include="..\..\..\tools\gfx\renderer-shared.h">
52+
<Filter>Header Files</Filter>
53+
</ClInclude>
5154
<ClInclude Include="..\..\..\tools\gfx\shader-cursor.h">
5255
<Filter>Header Files</Filter>
5356
</ClInclude>
57+
<ClInclude Include="..\..\..\tools\gfx\slang-gfx-helper.h">
58+
<Filter>Header Files</Filter>
59+
</ClInclude>
5460
<ClInclude Include="..\..\..\tools\gfx\surface.h">
5561
<Filter>Header Files</Filter>
5662
</ClInclude>
@@ -110,9 +116,15 @@
110116
<ClCompile Include="..\..\..\tools\gfx\render.cpp">
111117
<Filter>Source Files</Filter>
112118
</ClCompile>
119+
<ClCompile Include="..\..\..\tools\gfx\renderer-shared.cpp">
120+
<Filter>Source Files</Filter>
121+
</ClCompile>
113122
<ClCompile Include="..\..\..\tools\gfx\shader-cursor.cpp">
114123
<Filter>Source Files</Filter>
115124
</ClCompile>
125+
<ClCompile Include="..\..\..\tools\gfx\slang-gfx-helper.cpp">
126+
<Filter>Source Files</Filter>
127+
</ClCompile>
116128
<ClCompile Include="..\..\..\tools\gfx\surface.cpp">
117129
<Filter>Source Files</Filter>
118130
</ClCompile>

examples/gpu-printing/main.cpp

+20-19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ using Slang::ComPtr;
88
#include "gfx/render.h"
99
#include "gfx/d3d11/render-d3d11.h"
1010
#include "tools/graphics-app-framework/window.h"
11+
#include "source/core/slang-basic.h"
1112
using namespace gfx;
1213

1314
#include <string>
@@ -69,17 +70,17 @@ ComPtr<gfx::IRenderer> gRenderer;
6970

7071
ComPtr<slang::ISession> gSlangSession;
7172
ComPtr<slang::IModule> gSlangModule;
72-
RefPtr<gfx::ShaderProgram> gProgram;
73+
ComPtr<gfx::IShaderProgram> gProgram;
7374

74-
RefPtr<gfx::PipelineLayout> gPipelineLayout;
75-
RefPtr<gfx::PipelineState> gPipelineState;
76-
RefPtr<gfx::DescriptorSet> gDescriptorSet;
75+
ComPtr<gfx::IPipelineLayout> gPipelineLayout;
76+
ComPtr<gfx::IPipelineState> gPipelineState;
77+
ComPtr<gfx::IDescriptorSet> gDescriptorSet;
7778

78-
Dictionary<int, std::string> gHashedStrings;
79+
Slang::Dictionary<int, std::string> gHashedStrings;
7980

8081
GPUPrinting gGPUPrinting;
8182

82-
RefPtr<gfx::ShaderProgram> loadComputeProgram(slang::IModule* slangModule, char const* entryPointName)
83+
ComPtr<gfx::IShaderProgram> loadComputeProgram(slang::IModule* slangModule, char const* entryPointName)
8384
{
8485
ComPtr<slang::IEntryPoint> entryPoint;
8586
slangModule->findEntryPointByName(entryPointName, entryPoint.writeRef());
@@ -95,12 +96,12 @@ RefPtr<gfx::ShaderProgram> loadComputeProgram(slang::IModule* slangModule, char
9596
char const* code = (char const*) codeBlob->getBufferPointer();
9697
char const* codeEnd = code + codeBlob->getBufferSize();
9798

98-
gfx::ShaderProgram::KernelDesc kernelDescs[] =
99+
gfx::IShaderProgram::KernelDesc kernelDescs[] =
99100
{
100101
{ gfx::StageType::Compute, code, codeEnd },
101102
};
102103

103-
gfx::ShaderProgram::Desc programDesc;
104+
gfx::IShaderProgram::Desc programDesc;
104105
programDesc.pipelineType = gfx::PipelineType::Compute;
105106
programDesc.kernels = &kernelDescs[0];
106107
programDesc.kernelCount = 2;
@@ -133,21 +134,21 @@ Result execute()
133134
gProgram = loadComputeProgram(gSlangModule, "computeMain");
134135
if(!gProgram) return SLANG_FAIL;
135136

136-
DescriptorSetLayout::SlotRangeDesc slotRanges[] =
137+
IDescriptorSetLayout::SlotRangeDesc slotRanges[] =
137138
{
138-
DescriptorSetLayout::SlotRangeDesc(DescriptorSlotType::StorageBuffer),
139+
IDescriptorSetLayout::SlotRangeDesc(DescriptorSlotType::StorageBuffer),
139140
};
140-
DescriptorSetLayout::Desc descriptorSetLayoutDesc;
141+
IDescriptorSetLayout::Desc descriptorSetLayoutDesc;
141142
descriptorSetLayoutDesc.slotRangeCount = 1;
142143
descriptorSetLayoutDesc.slotRanges = &slotRanges[0];
143144
auto descriptorSetLayout = gRenderer->createDescriptorSetLayout(descriptorSetLayoutDesc);
144145
if(!descriptorSetLayout) return SLANG_FAIL;
145146

146-
PipelineLayout::DescriptorSetDesc descriptorSets[] =
147+
IPipelineLayout::DescriptorSetDesc descriptorSets[] =
147148
{
148-
PipelineLayout::DescriptorSetDesc( descriptorSetLayout ),
149+
IPipelineLayout::DescriptorSetDesc( descriptorSetLayout ),
149150
};
150-
PipelineLayout::Desc pipelineLayoutDesc;
151+
IPipelineLayout::Desc pipelineLayoutDesc;
151152
pipelineLayoutDesc.renderTargetCount = 1;
152153
pipelineLayoutDesc.descriptorSetCount = 1;
153154
pipelineLayoutDesc.descriptorSets = &descriptorSets[0];
@@ -176,14 +177,14 @@ Result execute()
176177

177178
size_t printBufferSize = 4 * 1024; // use a small-ish (4KB) buffer for print output
178179

179-
BufferResource::Desc printBufferDesc;
180+
IBufferResource::Desc printBufferDesc;
180181
printBufferDesc.init(printBufferSize);
181182
printBufferDesc.elementSize = sizeof(uint32_t);
182-
printBufferDesc.cpuAccessFlags = Resource::AccessFlag::Read; // | Resource::AccessFlag::Write;
183-
auto printBuffer = gRenderer->createBufferResource(Resource::Usage::UnorderedAccess, printBufferDesc);
183+
printBufferDesc.cpuAccessFlags = IResource::AccessFlag::Read; // | Resource::AccessFlag::Write;
184+
auto printBuffer = gRenderer->createBufferResource(IResource::Usage::UnorderedAccess, printBufferDesc);
184185

185-
ResourceView::Desc printBufferViewDesc;
186-
printBufferViewDesc.type = ResourceView::Type::UnorderedAccess;
186+
IResourceView::Desc printBufferViewDesc;
187+
printBufferViewDesc.type = IResourceView::Type::UnorderedAccess;
187188
auto printBufferView = gRenderer->createBufferView(printBuffer, printBufferViewDesc);
188189

189190
// TODO: need to copy a zero into the start of the print buffer!

examples/hello-world/main.cpp

+26-24
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
#include "gfx/d3d11/render-d3d11.h"
3737
#include "tools/graphics-app-framework/window.h"
3838
#include "slang-com-ptr.h"
39+
#include "source/core/slang-basic.h"
3940

4041
using namespace gfx;
42+
using namespace Slang;
4143

4244
// For the purposes of a small example, we will define the vertex data for a
4345
// single triangle directly in the source file. It should be easy to extend
@@ -72,7 +74,7 @@ struct HelloWorld
7274
// Slang API. This function is representative of code that a user
7375
// might write to integrate Slang into their renderer/engine.
7476
//
75-
RefPtr<gfx::ShaderProgram> loadShaderProgram(gfx::IRenderer* renderer)
77+
ComPtr<gfx::IShaderProgram> loadShaderProgram(gfx::IRenderer* renderer)
7678
{
7779
// First, we need to create a "session" for interacting with the Slang
7880
// compiler. This scopes all of our application's interactions
@@ -183,13 +185,13 @@ RefPtr<gfx::ShaderProgram> loadShaderProgram(gfx::IRenderer* renderer)
183185
// Reminder: this section does not involve the Slang API at all.
184186
//
185187

186-
gfx::ShaderProgram::KernelDesc kernelDescs[] =
188+
gfx::IShaderProgram::KernelDesc kernelDescs[] =
187189
{
188190
{ gfx::StageType::Vertex, vertexCode, vertexCodeEnd },
189191
{ gfx::StageType::Fragment, fragmentCode, fragmentCodeEnd },
190192
};
191193

192-
gfx::ShaderProgram::Desc programDesc;
194+
gfx::IShaderProgram::Desc programDesc;
193195
programDesc.pipelineType = gfx::PipelineType::Graphics;
194196
programDesc.kernels = &kernelDescs[0];
195197
programDesc.kernelCount = 2;
@@ -230,19 +232,19 @@ int gWindowHeight = 768;
230232
gfx::ApplicationContext* gAppContext;
231233
gfx::Window* gWindow;
232234
Slang::ComPtr<gfx::IRenderer> gRenderer;
233-
RefPtr<gfx::BufferResource> gConstantBuffer;
235+
ComPtr<gfx::IBufferResource> gConstantBuffer;
234236

235-
RefPtr<gfx::PipelineLayout> gPipelineLayout;
236-
RefPtr<gfx::PipelineState> gPipelineState;
237-
RefPtr<gfx::DescriptorSet> gDescriptorSet;
237+
ComPtr<gfx::IPipelineLayout> gPipelineLayout;
238+
ComPtr<gfx::IPipelineState> gPipelineState;
239+
ComPtr<gfx::IDescriptorSet> gDescriptorSet;
238240

239-
RefPtr<gfx::BufferResource> gVertexBuffer;
241+
ComPtr<gfx::IBufferResource> gVertexBuffer;
240242

241243
// Now that we've covered the function that actually loads and
242244
// compiles our Slang shade code, we can go through the rest
243245
// of the application code without as much commentary.
244246
//
245-
Result initialize()
247+
Slang::Result initialize()
246248
{
247249
// Create a window for our application to render into.
248250
//
@@ -264,7 +266,7 @@ Result initialize()
264266
rendererDesc.width = gWindowWidth;
265267
rendererDesc.height = gWindowHeight;
266268
{
267-
Result res = gRenderer->initialize(rendererDesc, getPlatformWindowHandle(gWindow));
269+
gfx::Result res = gRenderer->initialize(rendererDesc, getPlatformWindowHandle(gWindow));
268270
if(SLANG_FAILED(res)) return res;
269271
}
270272

@@ -278,13 +280,13 @@ Result initialize()
278280
//
279281
int constantBufferSize = 16 * sizeof(float);
280282

281-
BufferResource::Desc constantBufferDesc;
283+
IBufferResource::Desc constantBufferDesc;
282284
constantBufferDesc.init(constantBufferSize);
283-
constantBufferDesc.setDefaults(Resource::Usage::ConstantBuffer);
284-
constantBufferDesc.cpuAccessFlags = Resource::AccessFlag::Write;
285+
constantBufferDesc.setDefaults(IResource::Usage::ConstantBuffer);
286+
constantBufferDesc.cpuAccessFlags = IResource::AccessFlag::Write;
285287

286288
gConstantBuffer = gRenderer->createBufferResource(
287-
Resource::Usage::ConstantBuffer,
289+
IResource::Usage::ConstantBuffer,
288290
constantBufferDesc);
289291
if(!gConstantBuffer) return SLANG_FAIL;
290292

@@ -305,19 +307,19 @@ Result initialize()
305307
// Next we allocate a vertex buffer for our pre-initialized
306308
// vertex data.
307309
//
308-
BufferResource::Desc vertexBufferDesc;
310+
IBufferResource::Desc vertexBufferDesc;
309311
vertexBufferDesc.init(kVertexCount * sizeof(Vertex));
310-
vertexBufferDesc.setDefaults(Resource::Usage::VertexBuffer);
312+
vertexBufferDesc.setDefaults(IResource::Usage::VertexBuffer);
311313
gVertexBuffer = gRenderer->createBufferResource(
312-
Resource::Usage::VertexBuffer,
314+
IResource::Usage::VertexBuffer,
313315
vertexBufferDesc,
314316
&kVertexData[0]);
315317
if(!gVertexBuffer) return SLANG_FAIL;
316318

317319
// Now we will use our `loadShaderProgram` function to load
318320
// the code from `shaders.slang` into the graphics API.
319321
//
320-
RefPtr<ShaderProgram> shaderProgram = loadShaderProgram(gRenderer);
322+
ComPtr<IShaderProgram> shaderProgram = loadShaderProgram(gRenderer);
321323
if(!shaderProgram) return SLANG_FAIL;
322324

323325
// Our example graphics API usess a "modern" D3D12/Vulkan style
@@ -326,11 +328,11 @@ Result initialize()
326328
//
327329
// First, we need to construct a descriptor set *layout*.
328330
//
329-
DescriptorSetLayout::SlotRangeDesc slotRanges[] =
331+
IDescriptorSetLayout::SlotRangeDesc slotRanges[] =
330332
{
331-
DescriptorSetLayout::SlotRangeDesc(DescriptorSlotType::UniformBuffer),
333+
IDescriptorSetLayout::SlotRangeDesc(DescriptorSlotType::UniformBuffer),
332334
};
333-
DescriptorSetLayout::Desc descriptorSetLayoutDesc;
335+
IDescriptorSetLayout::Desc descriptorSetLayoutDesc;
334336
descriptorSetLayoutDesc.slotRangeCount = 1;
335337
descriptorSetLayoutDesc.slotRanges = &slotRanges[0];
336338
auto descriptorSetLayout = gRenderer->createDescriptorSetLayout(descriptorSetLayoutDesc);
@@ -340,11 +342,11 @@ Result initialize()
340342
// that we will render with only a single descriptor set bound.
341343
//
342344

343-
PipelineLayout::DescriptorSetDesc descriptorSets[] =
345+
IPipelineLayout::DescriptorSetDesc descriptorSets[] =
344346
{
345-
PipelineLayout::DescriptorSetDesc( descriptorSetLayout ),
347+
IPipelineLayout::DescriptorSetDesc( descriptorSetLayout ),
346348
};
347-
PipelineLayout::Desc pipelineLayoutDesc;
349+
IPipelineLayout::Desc pipelineLayoutDesc;
348350
pipelineLayoutDesc.renderTargetCount = 1;
349351
pipelineLayoutDesc.descriptorSetCount = 1;
350352
pipelineLayoutDesc.descriptorSets = &descriptorSets[0];

0 commit comments

Comments
 (0)