yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
43d0c2100
master
1#if 0 2 3// Duplicated: This test is similar to slang-rhi\tests\test-surface.cpp 4 5#include "gfx-test-util.h" 6#include "platform/window.h" 7#include "unit-test/slang-unit-test.h" 8 9#include <slang-rhi.h> 10#include <slang-rhi/shader-cursor.h> 11 12using namespace rhi ; 13 14namespace gfx_test 15{ 16struct Vertex 17{ 18float position [3 ]; 19}; 20 21static const int kVertexCount = 3 ; 22static const Vertex kVertexData [kVertexCount ]= { 23 {0 ,0 ,1 }, 24 {4 ,0 ,1 }, 25 {0 ,4 ,1 }, 26}; 27 28struct SwapchainResizeTest 29{ 30IDevice * device ; 31UnitTestContext * context ; 32 33ComPtr < platform::Window > window ; 34ComPtr < ICommandQueue > queue ; 35ComPtr < ISurface > surface ; 36 37ComPtr < ITexture > swapchainImages [2 ]; 38uint32_t swapchainImageCount = 2 ; 39Format desiredFormat = Format ::RGBA8Unorm ; 40 41ComPtr < IBuffer > vertexBuffer ; 42ComPtr < IInputLayout > inputLayout ; 43ComPtr < IRenderPipeline > pipeline ; 44ComPtr < IShaderProgram > shaderProgram ; 45ComPtr < IShaderObject > rootShaderObject ; 46 47uint32_t width = 500 ; 48uint32_t height = 500 ; 49 50void init (IDevice * device ,UnitTestContext * context ) 51 { 52this -> device = device ; 53this -> context = context ; 54 } 55 56void createSwapchainAndResources () 57 { 58// Create window 59 platform::Application ::init (); 60 platform::WindowDesc windowDesc ; 61windowDesc .title = "" ; 62windowDesc .width = width ; 63windowDesc .height = height ; 64windowDesc .style = platform::WindowStyle ::Default ; 65window = platform::Application ::createWindow (windowDesc ); 66 67// Create surface 68WindowHandle windowHandle = WindowHandle ::fromHwnd ((void * )window -> getNativeHandle ().handleValues [0 ]); 69surface = device -> createSurface (windowHandle ); 70 71// Configure surface (swapchain) 72SurfaceConfig config = {}; 73config .format = desiredFormat ; 74config .width = width ; 75config .height = height ; 76config .desiredImageCount = swapchainImageCount ; 77config .vsync = true; 78surface -> configure (config ); 79 80// Create vertex buffer 81BufferDesc vertexBufferDesc = {}; 82vertexBufferDesc .size = sizeof (Vertex )* kVertexCount ; 83vertexBufferDesc .memoryType = MemoryType ::DeviceLocal ; 84vertexBufferDesc .usage = BufferUsage ::VertexBuffer ; 85vertexBufferDesc .defaultState = ResourceState ::VertexBuffer ; 86vertexBuffer = device -> createBuffer (vertexBufferDesc ,kVertexData ); 87 88// Input layout 89InputElementDesc inputElements []= { 90 {"POSITIONA" ,0 ,Format ::RGB32Float , offsetof(Vertex ,position ),0 }, 91 }; 92VertexStreamDesc vertexStreams []= { 93 {sizeof (Vertex ),InputSlotClass ::PerVertex ,0 }, 94 }; 95InputLayoutDesc inputLayoutDesc = {}; 96inputLayoutDesc .inputElementCount = sizeof (inputElements ) /sizeof (InputElementDesc ); 97inputLayoutDesc .inputElements = inputElements ; 98inputLayoutDesc .vertexStreamCount = sizeof (vertexStreams ) /sizeof (VertexStreamDesc );; 99inputLayoutDesc .vertexStreams = vertexStreams ; 100 101GFX_CHECK_CALL_ABORT (device -> createInputLayout (inputLayoutDesc ,inputLayout .writeRef ())); 102 103ComPtr < IShaderProgram > shaderProgram ; 104 slang::ProgramLayout * slangReflection = nullptr ; 105GFX_CHECK_CALL_ABORT (loadGraphicsProgram ( 106device , 107shaderProgram , 108"swapchain-shader" , 109"vertexMain" , 110"fragmentMain" , 111slangReflection 112 )); 113 114 115// Pipeline 116ColorTargetDesc colorTarget = {}; 117colorTarget .format = desiredFormat ; 118RenderPipelineDesc pipelineDesc = {}; 119pipelineDesc .program = shaderProgram .get (); 120pipelineDesc .inputLayout = inputLayout .get (); 121pipelineDesc .primitiveTopology = PrimitiveTopology ::TriangleList ; 122pipelineDesc .targets = & colorTarget ; 123pipelineDesc .targetCount = 1 ; 124pipeline = device -> createRenderPipeline (pipelineDesc ); 125 } 126 127void renderFrame (uint32_t imageIndex ) 128 { 129// Acquire next image 130ComPtr < ITexture > backBuffer ; 131if (SLANG_FAILED (surface -> acquireNextImage (backBuffer .writeRef ()))) 132 { 133return ; 134 } 135 136// Create command encoder 137ComPtr < ICommandQueue > queue = device -> getQueue (QueueType ::Graphics ); 138ComPtr < ICommandEncoder > encoder = queue -> createCommandEncoder (); 139 140// Render pass setup 141RenderPassColorAttachment colorAttachment = {}; 142colorAttachment .view = backBuffer -> getDefaultView (); 143colorAttachment .loadOp = LoadOp ::Clear ; 144colorAttachment .storeOp = StoreOp ::Store ; 145float clearColor [4 ]= {0.2f ,0.2f ,0.2f ,1.0f }; 146memcpy (colorAttachment .clearValue ,clearColor ,sizeof (clearColor )); 147RenderPassDesc passDesc = {}; 148passDesc .colorAttachments = & colorAttachment ; 149passDesc .colorAttachmentCount = 1 ; 150 151// Begin render pass 152auto pass = encoder -> beginRenderPass (passDesc ); 153 154// Bind pipeline and root object 155pass -> bindPipeline (pipeline ,rootShaderObject ); 156 157// Set render state 158RenderState state = {}; 159state .vertexBuffers [0 ]= BufferOffsetPair (vertexBuffer ,0 ); 160state .vertexBufferCount = 1 ; 161// Set viewport 162Viewport viewport = Viewport ::fromSize ((float )width , (float )height ); 163state .viewportCount = 1 ; 164state .viewports [0 ]= viewport ; 165 166pass -> setRenderState (state ); 167 168// Draw 169DrawArguments args = {}; 170args .vertexCount = kVertexCount ; 171pass -> draw (args ); 172 173pass -> end (); 174ComPtr < ICommandBuffer > cmdBuffer ; 175encoder -> finish (cmdBuffer .writeRef ()); 176queue -> submit (cmdBuffer ); 177 178// Present 179surface -> present (); 180 } 181 182void run () 183 { 184createSwapchainAndResources (); 185for (uint32_t i = 0 ;i < 5 ;++ i ) 186 { 187renderFrame (i %swapchainImageCount ); 188 } 189queue -> waitOnHost (); 190 191// Resize swapchain 192width = 700 ; 193height = 700 ; 194SurfaceConfig config = surface -> getConfig (); 195config .width = width ; 196config .height = height ; 197surface -> configure (config ); 198 199for (uint32_t i = 0 ;i < 5 ;++ i ) 200 { 201renderFrame (i %swapchainImageCount ); 202 } 203queue -> waitOnHost (); 204 } 205}; 206 207void swapchainResizeTestImpl (IDevice * device ,UnitTestContext * context ) 208{ 209SwapchainResizeTest t ; 210t .init (device ,context ); 211t .run (); 212} 213 214SLANG_UNIT_TEST (swapchainResizeD3D12 ) 215{ 216runTestImpl (swapchainResizeTestImpl ,unitTestContext ,DeviceType ::D3D12 ); 217} 218 219SLANG_UNIT_TEST (swapchainResizeVulkan ) 220{ 221runTestImpl (swapchainResizeTestImpl ,unitTestContext ,DeviceType ::Vulkan ); 222} 223 224}// namespace gfx_test 225 226#endif