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 existingDeviceHandleTestImpl (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 51ShaderCursor rootCursor (rootObject ); 52// Bind buffer directly to the root. 53rootCursor .getPath ("buffer" ).setBinding (Binding (numbersBuffer )); 54 55encoder -> dispatchCompute (1 ,1 ,1 ); 56encoder -> end (); 57 } 58 59auto commandBuffer = commandEncoder -> finish (); 60queue -> submit (commandBuffer ); 61queue -> waitOnHost (); 62 } 63 64compareComputeResult (device ,numbersBuffer , std::array {1.0f ,2.0f ,3.0f ,4.0f }); 65} 66 67void existingDeviceHandleTestAPI (UnitTestContext * context ,DeviceType deviceType ) 68{ 69if (!deviceTypeInEnabledApis (deviceType ,context -> enabledApis )) 70 { 71SLANG_IGNORE_TEST 72 } 73Slang ::ComPtr < IDevice > device ; 74DeviceDesc deviceDesc = {}; 75deviceDesc .deviceType = deviceType ; 76deviceDesc .slang .slangGlobalSession = context -> slangGlobalSession ; 77const char * searchPaths []= {"" ,"../../tools/gfx-unit-test" ,"tools/gfx-unit-test" }; 78deviceDesc .slang .searchPathCount = (SlangInt )SLANG_COUNT_OF (searchPaths ); 79deviceDesc .slang .searchPaths = searchPaths ; 80auto createDeviceResult = getRHI ()-> createDevice (deviceDesc ,device .writeRef ()); 81if (SLANG_FAILED (createDeviceResult )|| !device ) 82 { 83SLANG_IGNORE_TEST ; 84 } 85 86DeviceNativeHandles handles ; 87GFX_CHECK_CALL_ABORT (device -> getNativeDeviceHandles (& handles )); 88Slang ::ComPtr < IDevice > testDevice ; 89DeviceDesc testDeviceDesc = deviceDesc ; 90testDeviceDesc .existingDeviceHandles .handles [0 ]= handles .handles [0 ]; 91if (deviceType == DeviceType ::Vulkan ) 92 { 93testDeviceDesc .existingDeviceHandles .handles [1 ]= handles .handles [1 ]; 94testDeviceDesc .existingDeviceHandles .handles [2 ]= handles .handles [2 ]; 95 } 96auto createTestDeviceResult = getRHI ()-> createDevice (testDeviceDesc ,testDevice .writeRef ()); 97if (SLANG_FAILED (createTestDeviceResult )|| !testDevice ) 98 { 99SLANG_IGNORE_TEST ; 100 } 101 102existingDeviceHandleTestImpl (testDevice ,context ); 103} 104 105SLANG_UNIT_TEST (existingDeviceHandleD3D12 ) 106{ 107return existingDeviceHandleTestAPI (unitTestContext ,DeviceType ::D3D12 ); 108} 109 110SLANG_UNIT_TEST (existingDeviceHandleVulkan ) 111{ 112return existingDeviceHandleTestAPI (unitTestContext ,DeviceType ::Vulkan ); 113} 114#if SLANG_WIN64 115SLANG_UNIT_TEST (existingDeviceHandleCUDA ) 116{ 117return existingDeviceHandleTestAPI (unitTestContext ,DeviceType ::CUDA ); 118} 119#endif 120}// namespace gfx_test