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 computeSmokeTestImpl (IDevice * device ,UnitTestContext * context ) 12{ 13ComPtr < IShaderProgram > shaderProgram ; 14 slang::ProgramLayout * slangReflection ; 15GFX_CHECK_CALL_ABORT ( 16loadComputeProgram (device ,shaderProgram ,"compute-smoke" ,"computeMain" ,slangReflection )); 17 18ComputePipelineDesc pipelineDesc = {}; 19pipelineDesc .program = shaderProgram .get (); 20ComPtr < IComputePipeline > pipelineState ; 21GFX_CHECK_CALL_ABORT (device -> createComputePipeline (pipelineDesc ,pipelineState .writeRef ())); 22 23const int numberCount = 4 ; 24float initialData []= {0.0f ,1.0f ,2.0f ,3.0f }; 25BufferDesc bufferDesc = {}; 26bufferDesc .size = numberCount * sizeof (float ); 27bufferDesc .format = rhi::Format ::Undefined ; 28bufferDesc .elementSize = sizeof (float ); 29bufferDesc .usage = BufferUsage ::ShaderResource |BufferUsage ::UnorderedAccess | 30BufferUsage ::CopyDestination |BufferUsage ::CopySource ; 31bufferDesc .defaultState = ResourceState ::UnorderedAccess ; 32bufferDesc .memoryType = MemoryType ::DeviceLocal ; 33 34ComPtr < IBuffer > numbersBuffer ; 35GFX_CHECK_CALL_ABORT ( 36device -> createBuffer (bufferDesc , (void * )initialData ,numbersBuffer .writeRef ())); 37 38// We have done all the set up work, now it is time to start recording a command buffer for 39// GPU execution. 40 { 41auto queue = device -> getQueue (QueueType ::Graphics ); 42auto commandEncoder = queue -> createCommandEncoder (); 43auto encoder = commandEncoder -> beginComputePass (); 44 45auto rootObject = encoder -> bindPipeline (pipelineState ); 46 47 slang::TypeReflection * addTransformerType = 48slangReflection -> findTypeByName ("AddTransformer" ); 49 50// Now we can use this type to create a shader object that can be bound to the root object. 51ComPtr < IShaderObject > transformer ; 52GFX_CHECK_CALL_ABORT (device -> createShaderObject ( 53addTransformerType , 54ShaderObjectContainerType ::None , 55transformer .writeRef ())); 56// Set the `c` field of the `AddTransformer`. 57float c = 1.0f ; 58ShaderCursor (transformer ).getPath ("c" ).setData (& c ,sizeof (float )); 59 60ShaderCursor entryPointCursor ( 61rootObject -> getEntryPoint (0 ));// get a cursor the the first entry-point. 62// Bind buffer to the entry point. 63entryPointCursor .getPath ("buffer" ).setBinding (Binding (numbersBuffer )); 64 65// Bind the previously created transformer object to root object. 66entryPointCursor .getPath ("transformer" ).setObject (transformer ); 67 68encoder -> dispatchCompute (1 ,1 ,1 ); 69encoder -> end (); 70auto commandBuffer = commandEncoder -> finish (); 71queue -> submit (commandBuffer ); 72queue -> waitOnHost (); 73 } 74 75compareComputeResult (device ,numbersBuffer , std::array {11.0f ,12.0f ,13.0f ,14.0f }); 76} 77 78SLANG_UNIT_TEST (computeSmokeD3D12 ) 79{ 80runTestImpl (computeSmokeTestImpl ,unitTestContext ,DeviceType ::D3D12 ); 81} 82 83SLANG_UNIT_TEST (computeSmokeD3D11 ) 84{ 85runTestImpl (computeSmokeTestImpl ,unitTestContext ,DeviceType ::D3D11 ); 86} 87 88SLANG_UNIT_TEST (computeSmokeVulkan ) 89{ 90runTestImpl (computeSmokeTestImpl ,unitTestContext ,DeviceType ::Vulkan ); 91} 92 93}// namespace gfx_test