yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1// d3d12-resource.h 2#pragma once 3 4#pragma push_macro("WIN32_LEAN_AND_MEAN") 5#pragma push_macro("NOMINMAX") 6#undef WIN32_LEAN_AND_MEAN 7#define WIN32_LEAN_AND_MEAN 8#undef NOMINMAX 9#define NOMINMAX 10#include <windows.h> 11#pragma pop_macro("NOMINMAX") 12#pragma pop_macro("WIN32_LEAN_AND_MEAN") 13 14#include "../d3d/d3d-util.h" 15#include "slang-com-ptr.h" 16 17#include <d3d12.h> 18#include <dxgi1_4.h> 19 20namespace gfx 21{ 22 23// Enables more conservative barriers - restoring the state of resources after they are used. 24// Should not need to be enabled in normal builds, as the barriers should correctly sync resources 25// If enabling fixes an issue it implies regular barriers are not correctly used. 26#define SLANG_ENABLE_CONSERVATIVE_RESOURCE_BARRIERS 0 27 28struct D3D12BarrierSubmitter 29{ 30enum 31 { 32MAX_BARRIERS = 8 33 }; 34 35/// Expand one space to hold a barrier 36SLANG_FORCE_INLINE D3D12_RESOURCE_BARRIER & expandOne () 37 { 38return (m_numBarriers < MAX_BARRIERS ) ?m_barriers [m_numBarriers ++ ] :_expandOne (); 39 } 40/// Flush barriers to command list 41SLANG_FORCE_INLINE void flush () 42 { 43if (m_numBarriers > 0 ) 44_flush (); 45 } 46 47/// Transition resource from prevState to nextState 48void transition ( 49ID3D12Resource * resource , 50D3D12_RESOURCE_STATES prevState , 51D3D12_RESOURCE_STATES nextState ); 52 53/// Ctor 54SLANG_FORCE_INLINE D3D12BarrierSubmitter (ID3D12GraphicsCommandList * commandList ) 55 :m_numBarriers (0 ),m_commandList (commandList ) 56 { 57 } 58/// Dtor 59SLANG_FORCE_INLINE ~D3D12BarrierSubmitter () {flush (); } 60 61protected : 62D3D12_RESOURCE_BARRIER & _expandOne (); 63void _flush (); 64 65ID3D12GraphicsCommandList * m_commandList ; 66int m_numBarriers ; 67D3D12_RESOURCE_BARRIER m_barriers [MAX_BARRIERS ]; 68}; 69 70/** The base class for resource types allows for tracking of state. It does not allow for setting of 71the resource though, such that an interface can return a D3D12ResourceBase, and a client cant 72manipulate it's state, but it cannot replace/change the actual resource */ 73struct D3D12ResourceBase 74{ 75/// Add a transition if necessary to the list 76void transition ( 77D3D12_RESOURCE_STATES currentState , 78D3D12_RESOURCE_STATES nextState , 79D3D12BarrierSubmitter & submitter ); 80/// Get the associated resource 81SLANG_FORCE_INLINE ID3D12Resource * getResource ()const {return m_resource ; } 82 83/// True if a resource is set 84SLANG_FORCE_INLINE bool isSet ()const {return m_resource != nullptr ; } 85 86/// Coercible into ID3D12Resource 87SLANG_FORCE_INLINE operator ID3D12Resource * ()const {return m_resource ; } 88 89/// Given the usage, flags, and format will return the most suitable format. Will return 90/// DXGI_UNKNOWN if combination is not possible 91static DXGI_FORMAT calcFormat (D3DUtil ::UsageType usage ,ID3D12Resource * resource ); 92 93/// Ctor 94SLANG_FORCE_INLINE D3D12ResourceBase () 95 :m_resource (nullptr ) 96 { 97 } 98 99protected : 100/// This is protected so as clients cannot slice the class, and so state tracking is lost 101 ~D3D12ResourceBase () {} 102 103ID3D12Resource * m_resource ;///< The resource (ref counted) 104}; 105 106struct D3D12Resource :public D3D12ResourceBase 107{ 108 109/// Dtor 110 ~D3D12Resource () 111 { 112if (m_resource ) 113 { 114m_resource -> Release (); 115 } 116 } 117 118/// Initialize as committed resource 119Slang ::Result initCommitted ( 120ID3D12Device * device , 121const D3D12_HEAP_PROPERTIES & heapProps , 122D3D12_HEAP_FLAGS heapFlags , 123const D3D12_RESOURCE_DESC & resourceDesc , 124D3D12_RESOURCE_STATES initState , 125const D3D12_CLEAR_VALUE * clearValue ); 126 127/// Set a resource. 128void setResource (ID3D12Resource * resource ); 129/// Make the resource null 130void setResourceNull (); 131/// Returns the attached resource (with any ref counts) and sets to nullptr on this. 132ID3D12Resource * detach (); 133 134/// Swaps the resource contents with the contents of the smart pointer 135void swap (Slang ::ComPtr < ID3D12Resource >& resourceInOut ); 136 137/// Set the debug name on a resource 138static void setDebugName (ID3D12Resource * resource ,const char * name ); 139 140/// Set the the debug name on the resource 141void setDebugName (const wchar_t * name ); 142/// Set the debug name 143void setDebugName (const char * name ); 144}; 145 146}// namespace gfx