yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1// d3d12-resource.cpp 2#include "d3d12-resource.h" 3 4namespace gfx 5{ 6using namespace Slang ; 7 8/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! D3D12BarrierSubmitter 9* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 10 11void D3D12BarrierSubmitter ::_flush () 12{ 13assert (m_numBarriers > 0 ); 14 15if (m_commandList ) 16 { 17m_commandList -> ResourceBarrier (UINT (m_numBarriers ),m_barriers ); 18 } 19m_numBarriers = 0 ; 20} 21 22D3D12_RESOURCE_BARRIER & D3D12BarrierSubmitter ::_expandOne () 23{ 24_flush (); 25return m_barriers [m_numBarriers ++ ]; 26} 27 28void D3D12BarrierSubmitter ::transition ( 29ID3D12Resource * resource , 30D3D12_RESOURCE_STATES prevState , 31D3D12_RESOURCE_STATES nextState ) 32{ 33if (nextState != prevState ) 34 { 35D3D12_RESOURCE_BARRIER & barrier = expandOne (); 36 37const UINT subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES ; 38const D3D12_RESOURCE_BARRIER_FLAGS flags = D3D12_RESOURCE_BARRIER_FLAG_NONE ; 39 40 ::memset (& barrier ,0 ,sizeof (barrier )); 41barrier .Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION ; 42barrier .Flags = flags ; 43barrier .Transition .pResource = resource ; 44barrier .Transition .StateBefore = prevState ; 45barrier .Transition .StateAfter = nextState ; 46barrier .Transition .Subresource = subresource ; 47 } 48else 49 { 50if (nextState == D3D12_RESOURCE_STATE_UNORDERED_ACCESS ) 51 { 52D3D12_RESOURCE_BARRIER & barrier = expandOne (); 53 54 ::memset (& barrier ,0 ,sizeof (barrier )); 55barrier .Type = D3D12_RESOURCE_BARRIER_TYPE_UAV ; 56barrier .UAV .pResource = resource ; 57 } 58 } 59} 60 61/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! D3D12ResourceBase 62* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 63 64/* static */ DXGI_FORMAT 65D3D12ResourceBase ::calcFormat (D3DUtil ::UsageType usage ,ID3D12Resource * resource ) 66{ 67return resource ?D3DUtil ::calcFormat (usage ,resource -> GetDesc ().Format ) :DXGI_FORMAT_UNKNOWN ; 68} 69 70void D3D12ResourceBase ::transition ( 71D3D12_RESOURCE_STATES oldState , 72D3D12_RESOURCE_STATES nextState , 73D3D12BarrierSubmitter & submitter ) 74{ 75// Transition only if there is a resource 76if (m_resource && oldState != nextState ) 77 { 78submitter .transition (m_resource ,oldState ,nextState ); 79 } 80} 81 82/* !!!!!!!!!!!!!!!!!!!!!!!!! D3D12Resource !!!!!!!!!!!!!!!!!!!!!!!! */ 83 84/* static */ void D3D12Resource ::setDebugName (ID3D12Resource * resource ,const char * name ) 85{ 86if (resource ) 87 { 88resource -> SetName (String (name ).toWString ().begin ()); 89 } 90} 91 92void D3D12Resource ::setDebugName (const char * name ) 93{ 94setDebugName (m_resource ,name ); 95} 96 97void D3D12Resource ::setDebugName (const wchar_t * name ) 98{ 99if (m_resource ) 100 { 101m_resource -> SetName (name ); 102 } 103} 104 105void D3D12Resource ::setResource (ID3D12Resource * resource ) 106{ 107if (resource != m_resource ) 108 { 109if (resource ) 110 { 111resource -> AddRef (); 112 } 113if (m_resource ) 114 { 115m_resource -> Release (); 116 } 117m_resource = resource ; 118 } 119} 120 121void D3D12Resource ::setResourceNull () 122{ 123if (m_resource ) 124 { 125m_resource -> Release (); 126m_resource = nullptr ; 127 } 128} 129 130Result D3D12Resource ::initCommitted ( 131ID3D12Device * device , 132const D3D12_HEAP_PROPERTIES & heapProps , 133D3D12_HEAP_FLAGS heapFlags , 134const D3D12_RESOURCE_DESC & resourceDesc , 135D3D12_RESOURCE_STATES initState , 136const D3D12_CLEAR_VALUE * clearValue ) 137{ 138setResourceNull (); 139ComPtr < ID3D12Resource > resource ; 140SLANG_RETURN_ON_FAIL (device -> CreateCommittedResource ( 141& heapProps , 142heapFlags , 143& resourceDesc , 144initState , 145clearValue , 146IID_PPV_ARGS (resource .writeRef ()))); 147setResource (resource ); 148return SLANG_OK ; 149} 150 151ID3D12Resource * D3D12Resource ::detach () 152{ 153ID3D12Resource * resource = m_resource ; 154m_resource = nullptr ; 155return resource ; 156} 157 158void D3D12Resource ::swap (ComPtr < ID3D12Resource >& resourceInOut ) 159{ 160ID3D12Resource * tmp = m_resource ; 161m_resource = resourceInOut .detach (); 162resourceInOut .attach (tmp ); 163} 164 165}// namespace gfx