yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
43d0c2100
master
1// Duplicated: This this test is identical to slang-rhi\tests\test-uint16-buffer.cpp 2 3#include "core/slang-basic.h" 4#include "gfx-test-util.h" 5#include "slang-rhi.h" 6#include "slang-rhi/shader-cursor.h" 7#include "unit-test/slang-unit-test.h" 8 9using namespace rhi ; 10 11namespace gfx_test 12{ 13void uint16BufferTestImpl (IDevice * device ,UnitTestContext * context ) 14{ 15ComPtr < IShaderProgram > shaderProgram ; 16 slang::ProgramLayout * slangReflection ; 17GFX_CHECK_CALL_ABORT ( 18loadComputeProgram (device ,shaderProgram ,"uint16-buffer" ,"computeMain" ,slangReflection )); 19 20ComputePipelineDesc pipelineDesc = {}; 21pipelineDesc .program = shaderProgram .get (); 22ComPtr < IComputePipeline > pipelineState ; 23GFX_CHECK_CALL_ABORT (device -> createComputePipeline (pipelineDesc ,pipelineState .writeRef ())); 24 25const int numberCount = 4 ; 26uint16_t initialData []= {0 ,1 ,2 ,3 }; 27BufferDesc bufferDesc = {}; 28bufferDesc .size = numberCount * sizeof (uint16_t ); 29bufferDesc .format = rhi::Format ::Undefined ; 30 31bufferDesc .elementSize = 0 ;// Let RHI derive from reflection 32bufferDesc .usage = BufferUsage ::ShaderResource |BufferUsage ::UnorderedAccess | 33BufferUsage ::CopyDestination |BufferUsage ::CopySource ; 34bufferDesc .defaultState = ResourceState ::UnorderedAccess ; 35bufferDesc .memoryType = MemoryType ::DeviceLocal ; 36 37ComPtr < IBuffer > numbersBuffer ; 38GFX_CHECK_CALL_ABORT ( 39device -> createBuffer (bufferDesc , (void * )initialData ,numbersBuffer .writeRef ())); 40 41// We have done all the set up work, now it is time to start recording a command buffer for 42// GPU execution. 43 { 44auto queue = device -> getQueue (QueueType ::Graphics ); 45auto commandEncoder = queue -> createCommandEncoder (); 46auto computePassEncoder = commandEncoder -> beginComputePass (); 47 48auto rootObject = computePassEncoder -> bindPipeline (pipelineState ); 49 50// Bind buffer to the entry point. 51ShaderCursor (rootObject ).getPath ("buffer" ).setBinding (Binding (numbersBuffer )); 52 53computePassEncoder -> dispatchCompute (1 ,1 ,1 ); 54computePassEncoder -> end (); 55queue -> submit (commandEncoder -> finish ()); 56queue -> waitOnHost (); 57 } 58 59compareComputeResult (device ,numbersBuffer , std::array < uint16_t ,4 > {1 ,2 ,3 ,4 }); 60} 61 62SLANG_UNIT_TEST (uint16BufferTestD3D12 ) 63{ 64runTestImpl (uint16BufferTestImpl ,unitTestContext ,DeviceType ::D3D12 ); 65} 66 67SLANG_UNIT_TEST (uint16BufferTestVulkan ) 68{ 69runTestImpl (uint16BufferTestImpl ,unitTestContext ,DeviceType ::Vulkan ); 70} 71 72}// namespace gfx_test