yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
43d0c2100
master
1#include "core/slang-basic.h" 2#include "gfx-test-util.h" 3#include "slang-rhi.h" 4#include "slang-rhi/shader-cursor.h" 5#include "unit-test/slang-unit-test.h" 6 7using namespace rhi ; 8 9namespace gfx_test 10{ 11void sharedBufferTestImpl (IDevice * srcDevice ,IDevice * dstDevice ,UnitTestContext * context ) 12{ 13// Create a shareable buffer using srcDevice, get its handle, then create a buffer using the 14// handle using dstDevice. Read back the buffer and check that its contents are correct. 15const int numberCount = 4 ; 16float initialData []= {0.0f ,1.0f ,2.0f ,3.0f }; 17BufferDesc bufferDesc = {}; 18bufferDesc .size = numberCount * sizeof (float ); 19bufferDesc .format = rhi::Format ::Undefined ; 20bufferDesc .elementSize = sizeof (float ); 21bufferDesc .usage = BufferUsage ::ShaderResource |BufferUsage ::UnorderedAccess | 22BufferUsage ::CopySource |BufferUsage ::CopyDestination |BufferUsage ::Shared ; 23bufferDesc .defaultState = ResourceState ::UnorderedAccess ; 24bufferDesc .memoryType = MemoryType ::DeviceLocal ; 25 26ComPtr < IBuffer > srcBuffer ; 27GFX_CHECK_CALL_ABORT ( 28srcDevice -> createBuffer (bufferDesc , (void * )initialData ,srcBuffer .writeRef ())); 29 30NativeHandle sharedHandle ; 31GFX_CHECK_CALL_ABORT (srcBuffer -> getSharedHandle (& sharedHandle )); 32ComPtr < IBuffer > dstBuffer ; 33GFX_CHECK_CALL_ABORT ( 34dstDevice -> createBufferFromSharedHandle (sharedHandle ,bufferDesc ,dstBuffer .writeRef ())); 35// Reading back the buffer from srcDevice to make sure it's been filled in before reading 36// anything back from dstDevice 37// TODO: Implement actual synchronization (and not this hacky solution) 38compareComputeResult (srcDevice ,srcBuffer , std::array {0.0f ,1.0f ,2.0f ,3.0f }); 39 40NativeHandle testHandle ; 41GFX_CHECK_CALL_ABORT (dstBuffer -> getNativeHandle (& testHandle )); 42const BufferDesc & testDesc = dstBuffer -> getDesc (); 43SLANG_CHECK (testDesc .elementSize == sizeof (float )); 44SLANG_CHECK (testDesc .size == numberCount * sizeof (float )); 45compareComputeResult (dstDevice ,dstBuffer , std::array {0.0f ,1.0f ,2.0f ,3.0f }); 46 47// Check that dstBuffer can be successfully used in a compute dispatch using dstDevice. 48ComPtr < IShaderProgram > shaderProgram ; 49 slang::ProgramLayout * slangReflection ; 50GFX_CHECK_CALL_ABORT (loadComputeProgram ( 51dstDevice , 52shaderProgram , 53"compute-trivial" , 54"computeMain" , 55slangReflection )); 56 57ComputePipelineDesc pipelineDesc = {}; 58pipelineDesc .program = shaderProgram .get (); 59ComPtr < IComputePipeline > pipelineState ; 60GFX_CHECK_CALL_ABORT (dstDevice -> createComputePipeline (pipelineDesc ,pipelineState .writeRef ())); 61 62auto queue = dstDevice -> getQueue (QueueType ::Graphics ); 63auto commandEncoder = queue -> createCommandEncoder (); 64auto computePassEncoder = commandEncoder -> beginComputePass (); 65 66auto rootObject = computePassEncoder -> bindPipeline (pipelineState ); 67 68ShaderCursor rootCursor (rootObject ); 69// Bind buffer to the entry point. 70rootCursor .getPath ("buffer" ).setBinding (Binding (dstBuffer )); 71 72computePassEncoder -> dispatchCompute (1 ,1 ,1 ); 73computePassEncoder -> end (); 74auto commandBuffer = commandEncoder -> finish (); 75queue -> submit (commandBuffer ); 76queue -> waitOnHost (); 77 78compareComputeResult (dstDevice ,dstBuffer , std::array {1.0f ,2.0f ,3.0f ,4.0f }); 79} 80 81void sharedBufferTestAPI (UnitTestContext * context ,DeviceType srcApi ,DeviceType dstApi ) 82{ 83auto srcDevice = createTestingDevice (context ,srcApi ); 84auto dstDevice = createTestingDevice (context ,dstApi ); 85if (!srcDevice || !dstDevice ) 86 { 87SLANG_IGNORE_TEST ; 88 } 89 90sharedBufferTestImpl (srcDevice ,dstDevice ,context ); 91} 92#if SLANG_WIN64 93SLANG_UNIT_TEST (sharedBufferD3D12ToCUDA ) 94{ 95sharedBufferTestAPI (unitTestContext ,DeviceType ::D3D12 ,DeviceType ::CUDA ); 96} 97 98SLANG_UNIT_TEST (sharedBufferVulkanToCUDA ) 99{ 100sharedBufferTestAPI (unitTestContext ,DeviceType ::Vulkan ,DeviceType ::CUDA ); 101} 102#endif 103}// namespace gfx_test