yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1// d3d12-swap-chain.cpp 2#include "d3d12-swap-chain.h" 3 4#include "d3d12-command-queue.h" 5#include "d3d12-device.h" 6#include "d3d12-texture.h" 7 8namespace gfx 9{ 10namespace d3d12 11{ 12 13using namespace Slang ; 14 15Result SwapchainImpl ::init ( 16DeviceImpl * renderer , 17const ISwapchain ::Desc & swapchainDesc , 18WindowHandle window ) 19{ 20m_queue = static_cast < CommandQueueImpl *> (swapchainDesc .queue )-> m_d3dQueue ; 21m_dxgiFactory = renderer -> m_deviceInfo .m_dxgiFactory ; 22SLANG_RETURN_ON_FAIL ( 23D3DSwapchainBase ::init (swapchainDesc ,window ,DXGI_SWAP_EFFECT_FLIP_DISCARD )); 24SLANG_RETURN_ON_FAIL (renderer -> m_device -> CreateFence ( 250 , 26D3D12_FENCE_FLAG_NONE , 27IID_PPV_ARGS (m_fence .writeRef ()))); 28 29SLANG_RETURN_ON_FAIL (m_swapChain -> QueryInterface (m_swapChain3 .writeRef ())); 30for (GfxIndex i = 0 ;i < swapchainDesc .imageCount ;i ++ ) 31 { 32m_frameEvents .add (CreateEventEx ( 33nullptr , 34 FALSE, 35CREATE_EVENT_INITIAL_SET |CREATE_EVENT_MANUAL_RESET , 36EVENT_ALL_ACCESS )); 37 } 38return SLANG_OK ; 39} 40 41Result SwapchainImpl ::resize (GfxCount width ,GfxCount height ) 42{ 43for (auto evt :m_frameEvents ) 44SetEvent (evt ); 45SLANG_RETURN_ON_FAIL (D3DSwapchainBase ::resize (width ,height )); 46return SLANG_OK ; 47} 48 49void SwapchainImpl ::createSwapchainBufferImages () 50{ 51m_images .clear (); 52 53for (GfxIndex i = 0 ;i < m_desc .imageCount ;i ++ ) 54 { 55ComPtr < ID3D12Resource > d3dResource ; 56m_swapChain -> GetBuffer (i ,IID_PPV_ARGS (d3dResource .writeRef ())); 57ITextureResource ::Desc imageDesc = {}; 58imageDesc .allowedStates = ResourceStateSet ( 59ResourceState ::Present , 60ResourceState ::RenderTarget , 61ResourceState ::CopyDestination ); 62imageDesc .type = IResource ::Type ::Texture2D ; 63imageDesc .arraySize = 0 ; 64imageDesc .format = m_desc .format ; 65imageDesc .size .width = m_desc .width ; 66imageDesc .size .height = m_desc .height ; 67imageDesc .size .depth = 1 ; 68imageDesc .numMipLevels = 1 ; 69imageDesc .defaultState = ResourceState ::Present ; 70RefPtr < TextureResourceImpl > image = new TextureResourceImpl (imageDesc ); 71image -> m_resource .setResource (d3dResource .get ()); 72image -> m_defaultState = D3D12_RESOURCE_STATE_PRESENT ; 73m_images .add (image ); 74 } 75for (auto evt :m_frameEvents ) 76SetEvent (evt ); 77} 78 79int SwapchainImpl ::acquireNextImage () 80{ 81auto result = (int )m_swapChain3 -> GetCurrentBackBufferIndex (); 82WaitForSingleObject (m_frameEvents [result ],INFINITE ); 83ResetEvent (m_frameEvents [result ]); 84return result ; 85} 86 87Result SwapchainImpl ::present () 88{ 89m_fence -> SetEventOnCompletion ( 90fenceValue , 91m_frameEvents [m_swapChain3 -> GetCurrentBackBufferIndex ()]); 92SLANG_RETURN_ON_FAIL (D3DSwapchainBase ::present ()); 93fenceValue ++ ; 94m_queue -> Signal (m_fence ,fenceValue ); 95return SLANG_OK ; 96} 97 98bool SwapchainImpl ::isOccluded () 99{ 100return (m_swapChain3 -> Present (0 ,DXGI_PRESENT_TEST )== DXGI_STATUS_OCCLUDED ); 101} 102 103Result SwapchainImpl ::setFullScreenMode (bool mode ) 104{ 105return m_swapChain3 -> SetFullscreenState (mode ,nullptr ); 106} 107 108}// namespace d3d12 109}// namespace gfx