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 computeTrivialTestImpl (IDevice * device ,UnitTestContext * context ) 12{ 13ComPtr < IShaderProgram > shaderProgram ; 14 slang::ProgramLayout * slangReflection ; 15GFX_CHECK_CALL_ABORT (loadComputeProgram ( 16device , 17shaderProgram , 18"compute-trivial" , 19"computeMain" , 20slangReflection )); 21 22ComputePipelineDesc pipelineDesc = {}; 23pipelineDesc .program = shaderProgram .get (); 24ComPtr < IComputePipeline > pipelineState ; 25GFX_CHECK_CALL_ABORT (device -> createComputePipeline (pipelineDesc ,pipelineState .writeRef ())); 26 27const int numberCount = 4 ; 28float initialData []= {0.0f ,1.0f ,2.0f ,3.0f }; 29BufferDesc bufferDesc = {}; 30bufferDesc .size = numberCount * sizeof (float ); 31bufferDesc .format = Format ::Undefined ; 32bufferDesc .elementSize = sizeof (float ); 33bufferDesc .usage = BufferUsage ::ShaderResource |BufferUsage ::UnorderedAccess | 34BufferUsage ::CopyDestination |BufferUsage ::CopySource ; 35bufferDesc .defaultState = ResourceState ::UnorderedAccess ; 36bufferDesc .memoryType = MemoryType ::DeviceLocal ; 37 38ComPtr < IBuffer > numbersBuffer ; 39GFX_CHECK_CALL_ABORT ( 40device -> createBuffer (bufferDesc , (void * )initialData ,numbersBuffer .writeRef ())); 41 42// We have done all the set up work, now it is time to start recording a command buffer for 43// GPU execution. 44 { 45auto queue = device -> getQueue (QueueType ::Graphics ); 46auto commandEncoder = queue -> createCommandEncoder (); 47 { 48auto encoder = commandEncoder -> beginComputePass (); 49auto rootObject = encoder -> bindPipeline (pipelineState ); 50 51// Bind buffer directly to the entry point. 52ShaderCursor (rootObject ).getPath ("buffer" ).setBinding (Binding (numbersBuffer )); 53 54encoder -> dispatchCompute (1 ,1 ,1 ); 55encoder -> end (); 56 } 57 58auto commandBuffer = commandEncoder -> finish (); 59queue -> submit (commandBuffer ); 60queue -> waitOnHost (); 61 } 62 63compareComputeResult (device ,numbersBuffer , std::array {1.0f ,2.0f ,3.0f ,4.0f }); 64} 65 66SLANG_UNIT_TEST (computeTrivialD3D12 ) 67{ 68runTestImpl (computeTrivialTestImpl ,unitTestContext ,DeviceType ::D3D12 ); 69} 70 71SLANG_UNIT_TEST (computeTrivialD3D11 ) 72{ 73runTestImpl (computeTrivialTestImpl ,unitTestContext ,DeviceType ::D3D11 ); 74} 75 76SLANG_UNIT_TEST (computeTrivialVulkan ) 77{ 78runTestImpl (computeTrivialTestImpl ,unitTestContext ,DeviceType ::Vulkan ); 79} 80 81}// namespace gfx_test