From 5eb835f0332868fd56ac14ce7560e0ae9cfafec9 Mon Sep 17 00:00:00 2001 From: David Siher <32305650+dsiher@users.noreply.github.com> Date: Thu, 3 Feb 2022 19:16:54 -0800 Subject: 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 --- examples/heterogeneous-hello-world/main.cpp | 107 +++++------------------- examples/heterogeneous-hello-world/shader.slang | 65 +++++++------- 2 files changed, 58 insertions(+), 114 deletions(-) (limited to 'examples') 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 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 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 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 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 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 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 _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 _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 convertBuffer_0(gfx_BufferResource_0* _0) { +RWStructuredBuffer convertBuffer(gfx::IBufferResource* _0) { RWStructuredBuffer result; result.data = (float*)_0; return result; } -gfx_BufferResource_0* unconvertBuffer_0(RWStructuredBuffer _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 _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 convertBuffer(Ptr x); +__unmangled __exportDirectly RWStructuredBuffer convertBuffer(Ptr x); [shader("compute")] [numthreads(4, 1, 1)] @@ -15,44 +15,49 @@ void computeMain(uniform RWStructuredBuffer 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 createDevice(); -Ptr loadShaderProgram(Ptr device, String entryPoint, String module); -Ptr createStructuredBuffer( - Ptr device, +__unmangled __exportDirectly Ptr createDevice(); +__unmangled __exportDirectly Ptr loadShaderProgram( + Ptr device, + String entryPoint, + String module); +__unmangled __exportDirectly Ptr createStructuredBuffer( + Ptr device, float[4] initialData); -Ptr createBufferView( - Ptr device, - Ptr buffer); -Ptr buildTransientHeap( - Ptr device); -Ptr buildPipelineState( - Ptr device, - Ptr shaderProgram); -void printInitialValues(float[4] initialArray, int length); -void dispatchComputation( - Ptr device, - Ptr transientHeap, - Ptr pipelineState, - Ptr bufferView); -bool printOutputValues( - Ptr device, - Ptr buffer, +__unmangled __exportDirectly Ptr createBufferView( + Ptr device, + Ptr buffer); +__unmangled __exportDirectly Ptr buildTransientHeap( + Ptr device); +__unmangled __exportDirectly Ptr buildPipelineState( + Ptr device, + Ptr shaderProgram); +__unmangled __exportDirectly void printInitialValues(float[4] initialArray, int length); +__unmangled __exportDirectly void dispatchComputation( + Ptr device, + Ptr transientHeap, + Ptr pipelineState, + Ptr bufferView); +__unmangled __exportDirectly bool printOutputValues( + Ptr device, + Ptr 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); -- cgit v1.2.3