summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorDavid Siher <32305650+dsiher@users.noreply.github.com>2022-02-03 19:16:54 -0800
committerGitHub <noreply@github.com>2022-02-03 19:16:54 -0800
commit5eb835f0332868fd56ac14ce7560e0ae9cfafec9 (patch)
tree67ed2ae3b2527e8cfa66f835062490decf3052ad /examples
parent1eda86377847155ed3f0e0b2e40a105af35bd387 (diff)
Fixed naming conflicts in heterogeneous-hello-world (#2114)
* Fixed naming conflicts in heterogeneous-hello-world Added 3 new modifiers (`__unmangled`, `__exportDirectly`, `__externLib`) `__unmangled` causes mangleName() to return the normal name of the decl. `__exportDirectly` changes parent decl name concatenation behavior to use "::" instead of "." (for Name Hint) and emits the name hint when it exists, otherwise it emits the mangled name. `__externLib` stops Slang from emitting the corresponding struct. Also made necessary changes to heterogeneous-hello-world so that this new functionality is shown off. * Undo unintentional formatting changes Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/heterogeneous-hello-world/main.cpp107
-rw-r--r--examples/heterogeneous-hello-world/shader.slang65
2 files changed, 58 insertions, 114 deletions
diff --git a/examples/heterogeneous-hello-world/main.cpp b/examples/heterogeneous-hello-world/main.cpp
index 0d8d2ca73..e711d4486 100644
--- a/examples/heterogeneous-hello-world/main.cpp
+++ b/examples/heterogeneous-hello-world/main.cpp
@@ -27,13 +27,7 @@ ComPtr<gfx::ICommandQueue> gQueue;
// Boilerplate types to help the slang-generated file
//
-struct gfx_Device_0;
-struct gfx_BufferResource_0;
-struct gfx_ShaderProgram_0;
-struct gfx_ResourceView_0;
-struct gfx_TransientResourceHeap_0;
-struct gfx_PipelineState_0;
-bool executeComputation_0();
+bool executeComputation();
// Many Slang API functions return detailed diagnostic information
// (error messages, warnings, etc.) as a "blob" of data, or return
@@ -50,7 +44,7 @@ void diagnoseIfNeeded(slang::IBlob *diagnosticsBlob)
}
}
-gfx::IDevice* createDevice()
+gfx::IDevice *createDevice()
{
ComPtr<gfx::IDevice> device;
IDevice::Desc deviceDesc = {};
@@ -62,7 +56,7 @@ gfx::IDevice* createDevice()
// Loads the shader code defined in `shader.slang` for use by the `gfx` layer.
//
-gfx::IShaderProgram* loadShaderProgram(gfx::IDevice *device, String entryPoint, String moduleName)
+gfx::IShaderProgram *loadShaderProgram(gfx::IDevice *device, char* entryPoint, char* moduleName)
{
// We need to obtain a compilation session (`slang::ISession`) that will provide
// a scope to all the compilation and loading of code we do.
@@ -83,17 +77,17 @@ gfx::IShaderProgram* loadShaderProgram(gfx::IDevice *device, String entryPoint,
// already been loaded previously, that would be used directly.
//
ComPtr<slang::IBlob> diagnosticsBlob;
- slang::IModule *module = slangSession->loadModule(moduleName.getBuffer(), diagnosticsBlob.writeRef());
+ slang::IModule *module = slangSession->loadModule(moduleName, diagnosticsBlob.writeRef());
diagnoseIfNeeded(diagnosticsBlob);
if (!module)
return NULL;
// Look up entry point
//
- char const *computeEntryPointName = entryPoint.getBuffer();
+ // char const *computeEntryPointName = entryPoint.getBuffer();
ComPtr<slang::IEntryPoint> computeEntryPoint;
SLANG_RETURN_NULL_ON_FAIL(
- module->findEntryPointByName(computeEntryPointName, computeEntryPoint.writeRef()));
+ module->findEntryPointByName(entryPoint, computeEntryPoint.writeRef()));
// At this point we have a few different Slang API objects that represent
// pieces of our code: `module`, `vertexEntryPoint`, and `fragmentEntryPoint`.
@@ -139,9 +133,7 @@ gfx::IShaderProgram* loadShaderProgram(gfx::IDevice *device, String entryPoint,
return gProgram;
}
-gfx::IBufferResource* createStructuredBuffer(
- gfx::IDevice *device,
- float *initialData)
+gfx::IBufferResource* createStructuredBuffer(gfx::IDevice* device, FixedArray<float, 4> initialData)
{
// Create a structured buffer for storing computation data
//
@@ -160,15 +152,15 @@ gfx::IBufferResource* createStructuredBuffer(
bufferDesc.memoryType = MemoryType::DeviceLocal;
SlangResult result = device->createBufferResource(bufferDesc,
- (void *)initialData,
+ (void *)&initialData,
gBufferResource.writeRef());
SLANG_RETURN_NULL_ON_FAIL(result);
return gBufferResource;
}
-gfx::IResourceView* createBufferView(
- gfx::IDevice* device,
- gfx::IBufferResource* buffer)
+gfx::IResourceView *createBufferView(
+ gfx::IDevice *device,
+ gfx::IBufferResource *buffer)
{
// Create a resource view for the structured buffer
//
@@ -179,7 +171,7 @@ gfx::IResourceView* createBufferView(
return gResourceView;
}
-gfx::ITransientResourceHeap* buildTransientHeap(gfx::IDevice *device)
+gfx::ITransientResourceHeap *buildTransientHeap(gfx::IDevice *device)
{
ITransientResourceHeap::Desc transientHeapDesc = {};
transientHeapDesc.constantBufferSize = 4096;
@@ -188,9 +180,9 @@ gfx::ITransientResourceHeap* buildTransientHeap(gfx::IDevice *device)
return gTransientHeap;
}
-gfx::IPipelineState* buildPipelineState(
+gfx::IPipelineState *buildPipelineState(
gfx::IDevice *device,
- gfx::IShaderProgram* shaderProgram)
+ gfx::IShaderProgram *shaderProgram)
{
gfx::ComputePipelineStateDesc pipelineDesc = {};
pipelineDesc.program = shaderProgram;
@@ -199,7 +191,7 @@ gfx::IPipelineState* buildPipelineState(
return gPipelineState;
}
-void printInitialValues(float *initialArray, int length)
+void printInitialValues(FixedArray<float, 4> initialArray, int length)
{
printf("Before:\n");
for (int i = 0; i < length; i++)
@@ -210,10 +202,10 @@ void printInitialValues(float *initialArray, int length)
}
void dispatchComputation(
- gfx::IDevice* device,
- gfx::ITransientResourceHeap* transientHeap,
- gfx::IPipelineState* pipelineState,
- gfx::IResourceView* bufferView,
+ gfx::IDevice *device,
+ gfx::ITransientResourceHeap *transientHeap,
+ gfx::IPipelineState *pipelineState,
+ gfx::IResourceView *bufferView,
unsigned int gridDimsX,
unsigned int gridDimsY,
unsigned int gridDimsZ)
@@ -257,68 +249,15 @@ bool printOutputValues(
return true;
}
-// Boilerplate functions to help the slang-generated file and types
-
-gfx_Device_0* createDevice_0()
-{
- return (gfx_Device_0*)createDevice();
-}
-
-gfx_BufferResource_0* createStructuredBuffer_0(gfx_Device_0* _0, FixedArray<float, 4> _1)
-{
- return (gfx_BufferResource_0*)createStructuredBuffer((gfx::IDevice*)_0, (float*)&_1);
-}
-
-gfx_ShaderProgram_0* loadShaderProgram_0(gfx_Device_0* _0, char* _1, char* _2)
-{
- return (gfx_ShaderProgram_0*)loadShaderProgram((gfx::IDevice*)_0, _1, _2);
-}
-
-gfx_ResourceView_0* createBufferView_0(gfx_Device_0* _0, gfx_BufferResource_0* _1)
-{
- return (gfx_ResourceView_0*)createBufferView((gfx::IDevice*)_0, (gfx::IBufferResource*)_1);
-}
-
-gfx_TransientResourceHeap_0* buildTransientHeap_0(gfx_Device_0* _0)
-{
- return (gfx_TransientResourceHeap_0*)buildTransientHeap((gfx::IDevice*)_0);
-}
-
-gfx_PipelineState_0* buildPipelineState_0(gfx_Device_0* _0, gfx_ShaderProgram_0* _1)
-{
- return (gfx_PipelineState_0*)buildPipelineState((gfx::IDevice*)_0, (gfx::IShaderProgram*)_1);
-}
-
-void printInitialValues_0(FixedArray<float, 4> _0, int32_t _1)
-{
- printInitialValues((float*)&_0, _1);
-}
-
-void dispatchComputation_0(gfx_Device_0* _0, gfx_TransientResourceHeap_0* _1, gfx_PipelineState_0* _2, gfx_ResourceView_0* _3, unsigned int gridDimsX, unsigned int gridDimsY, unsigned int gridDimsZ)
-{
- dispatchComputation(
- (gfx::IDevice*)_0,
- (gfx::ITransientResourceHeap*)_1,
- (gfx::IPipelineState*)_2,
- (gfx::IResourceView*)_3,
- gridDimsX,
- gridDimsY,
- gridDimsZ);
-}
-
-RWStructuredBuffer<float> convertBuffer_0(gfx_BufferResource_0* _0) {
+RWStructuredBuffer<float> convertBuffer(gfx::IBufferResource* _0) {
RWStructuredBuffer<float> result;
result.data = (float*)_0;
return result;
}
-gfx_BufferResource_0* unconvertBuffer_0(RWStructuredBuffer<float> _0) {
- return (gfx_BufferResource_0*)(_0.data);
-}
-
-bool printOutputValues_0(gfx_Device_0* _0, gfx_BufferResource_0* _1, int32_t _2)
+gfx::IBufferResource *unconvertBuffer(RWStructuredBuffer<float> _0)
{
- return printOutputValues((gfx::IDevice*)_0, (gfx::IBufferResource*)_1, _2);
+ return (gfx::IBufferResource *)(_0.data);
}
int main()
@@ -327,7 +266,7 @@ int main()
// `struct` type, and then walk through the lifecyle
// of the application.
- if (!(executeComputation_0()))
+ if (!(executeComputation()))
{
return -1;
}
diff --git a/examples/heterogeneous-hello-world/shader.slang b/examples/heterogeneous-hello-world/shader.slang
index d67fd5582..d87370be1 100644
--- a/examples/heterogeneous-hello-world/shader.slang
+++ b/examples/heterogeneous-hello-world/shader.slang
@@ -1,7 +1,7 @@
// shader.slang
//TEST_INPUT:ubuffer(random(float, 4096, -1.0, 1.0), stride=4):name=ioBuffer
-RWStructuredBuffer<float> convertBuffer(Ptr<gfx::BufferResource> x);
+__unmangled __exportDirectly RWStructuredBuffer<float> convertBuffer(Ptr<gfx::IBufferResource> x);
[shader("compute")]
[numthreads(4, 1, 1)]
@@ -15,44 +15,49 @@ void computeMain(uniform RWStructuredBuffer<float> ioBuffer, uint3 dispatchThrea
ioBuffer[tid] = o;
}
+
+
// Forward declarations of gfx types
//
namespace gfx {
- struct Device{};
- struct BufferResource{};
- struct ResourceView{};
- struct TransientResourceHeap{};
- struct PipelineState{};
- struct ShaderProgram{};
+ __externLib __exportDirectly struct IDevice{};
+ __externLib __exportDirectly struct IBufferResource{};
+ __exportDirectly struct ITransientResourceHeap{};
+ __exportDirectly struct IPipelineState{};
+ __exportDirectly struct IShaderProgram{};
+ __exportDirectly struct IResourceView{};
}
// 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,
+__unmangled __exportDirectly Ptr<gfx::IDevice> createDevice();
+__unmangled __exportDirectly Ptr<gfx::IShaderProgram> loadShaderProgram(
+ Ptr<gfx::IDevice> device,
+ String entryPoint,
+ String module);
+__unmangled __exportDirectly Ptr<gfx::IBufferResource> createStructuredBuffer(
+ Ptr<gfx::IDevice> 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,
+__unmangled __exportDirectly Ptr<gfx::IResourceView> createBufferView(
+ Ptr<gfx::IDevice> device,
+ Ptr<gfx::IBufferResource> buffer);
+__unmangled __exportDirectly Ptr<gfx::ITransientResourceHeap> buildTransientHeap(
+ Ptr<gfx::IDevice> device);
+__unmangled __exportDirectly Ptr<gfx::IPipelineState> buildPipelineState(
+ Ptr<gfx::IDevice> device,
+ Ptr<gfx::IShaderProgram> shaderProgram);
+__unmangled __exportDirectly void printInitialValues(float[4] initialArray, int length);
+__unmangled __exportDirectly void dispatchComputation(
+ Ptr<gfx::IDevice> device,
+ Ptr<gfx::ITransientResourceHeap> transientHeap,
+ Ptr<gfx::IPipelineState> pipelineState,
+ Ptr<gfx::IResourceView> bufferView);
+__unmangled __exportDirectly bool printOutputValues(
+ Ptr<gfx::IDevice> device,
+ Ptr<gfx::IBufferResource> buffer,
int length);
-public bool executeComputation() {
+public __unmangled __exportDirectly bool executeComputation() {
// We will hard-code the size of our initial array.
//
float initialArray[4] = { 3.0f, -20.0f, -6.0f, 8.0f };
@@ -60,7 +65,7 @@ public bool executeComputation() {
// Declare functions
let device = createDevice();
let structuredBuffer = createStructuredBuffer(device, initialArray);
- let bufferView = createBufferView(device, structuredBuffer);
+ // let bufferView = createBufferView(device, structuredBuffer);
__GPU_FOREACH(device, uint3(4, 1, 1), LAMBDA(uint3 dispatchThreadID)
{ computeMain(convertBuffer(structuredBuffer), dispatchThreadID) ; });
printInitialValues(initialArray, 4);