yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
4.3 KiB146 linesraw
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{
30    enum
31    {
32        MAX_BARRIERS = 8
33    };
34
35    /// Expand one space to hold a barrier
36    SLANG_FORCE_INLINE D3D12_RESOURCE_BARRIER& expandOne()
37    {
38        return (m_numBarriers < MAX_BARRIERS) ? m_barriers[m_numBarriers++] : _expandOne();
39    }
40    /// Flush barriers to command list
41    SLANG_FORCE_INLINE void flush()
42    {
43        if (m_numBarriers > 0)
44            _flush();
45    }
46
47    /// Transition resource from prevState to nextState
48    void transition(
49        ID3D12Resource* resource,
50        D3D12_RESOURCE_STATES prevState,
51        D3D12_RESOURCE_STATES nextState);
52
53    /// Ctor
54    SLANG_FORCE_INLINE D3D12BarrierSubmitter(ID3D12GraphicsCommandList* commandList)
55        : m_numBarriers(0), m_commandList(commandList)
56    {
57    }
58    /// Dtor
59    SLANG_FORCE_INLINE ~D3D12BarrierSubmitter() { flush(); }
60
61protected:
62    D3D12_RESOURCE_BARRIER& _expandOne();
63    void _flush();
64
65    ID3D12GraphicsCommandList* m_commandList;
66    int m_numBarriers;
67    D3D12_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
76    void transition(
77        D3D12_RESOURCE_STATES currentState,
78        D3D12_RESOURCE_STATES nextState,
79        D3D12BarrierSubmitter& submitter);
80    /// Get the associated resource
81    SLANG_FORCE_INLINE ID3D12Resource* getResource() const { return m_resource; }
82
83    /// True if a resource is set
84    SLANG_FORCE_INLINE bool isSet() const { return m_resource != nullptr; }
85
86    /// Coercible into ID3D12Resource
87    SLANG_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
91    static DXGI_FORMAT calcFormat(D3DUtil::UsageType usage, ID3D12Resource* resource);
92
93    /// Ctor
94    SLANG_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
103    ID3D12Resource* m_resource; ///< The resource (ref counted)
104};
105
106struct D3D12Resource : public D3D12ResourceBase
107{
108
109    /// Dtor
110    ~D3D12Resource()
111    {
112        if (m_resource)
113        {
114            m_resource->Release();
115        }
116    }
117
118    /// Initialize as committed resource
119    Slang::Result initCommitted(
120        ID3D12Device* device,
121        const D3D12_HEAP_PROPERTIES& heapProps,
122        D3D12_HEAP_FLAGS heapFlags,
123        const D3D12_RESOURCE_DESC& resourceDesc,
124        D3D12_RESOURCE_STATES initState,
125        const D3D12_CLEAR_VALUE* clearValue);
126
127    /// Set a resource.
128    void setResource(ID3D12Resource* resource);
129    /// Make the resource null
130    void setResourceNull();
131    /// Returns the attached resource (with any ref counts) and sets to nullptr on this.
132    ID3D12Resource* detach();
133
134    /// Swaps the resource contents with the contents of the smart pointer
135    void swap(Slang::ComPtr<ID3D12Resource>& resourceInOut);
136
137    /// Set the debug name on a resource
138    static void setDebugName(ID3D12Resource* resource, const char* name);
139
140    /// Set the the debug name on the resource
141    void setDebugName(const wchar_t* name);
142    /// Set the debug name
143    void setDebugName(const char* name);
144};
145
146} // namespace gfx