1 // Copyright 2017 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DAWNNATIVE_D3D12_TEXTURED3D12_H_
16 #define DAWNNATIVE_D3D12_TEXTURED3D12_H_
17 
18 #include "dawn_native/Texture.h"
19 
20 #include "dawn_native/DawnNative.h"
21 #include "dawn_native/IntegerTypes.h"
22 #include "dawn_native/PassResourceUsage.h"
23 #include "dawn_native/d3d12/IntegerTypes.h"
24 #include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h"
25 #include "dawn_native/d3d12/d3d12_platform.h"
26 
27 namespace dawn_native { namespace d3d12 {
28 
29     class CommandRecordingContext;
30     class Device;
31 
32     DXGI_FORMAT D3D12TextureFormat(wgpu::TextureFormat format);
33     MaybeError ValidateD3D12TextureCanBeWrapped(ID3D12Resource* d3d12Resource,
34                                                 const TextureDescriptor* descriptor);
35     MaybeError ValidateTextureDescriptorCanBeWrapped(const TextureDescriptor* descriptor);
36 
37     class Texture final : public TextureBase {
38       public:
39         static ResultOrError<Ref<TextureBase>> Create(Device* device,
40                                                       const TextureDescriptor* descriptor);
41         static ResultOrError<Ref<TextureBase>> Create(Device* device,
42                                                       const ExternalImageDescriptor* descriptor,
43                                                       HANDLE sharedHandle,
44                                                       ExternalMutexSerial acquireMutexKey,
45                                                       bool isSwapChainTexture);
46         Texture(Device* device,
47                 const TextureDescriptor* descriptor,
48                 ComPtr<ID3D12Resource> d3d12Texture);
49 
50         DXGI_FORMAT GetD3D12Format() const;
51         ID3D12Resource* GetD3D12Resource() const;
52         DXGI_FORMAT GetD3D12CopyableSubresourceFormat(Aspect aspect) const;
53 
54         D3D12_RENDER_TARGET_VIEW_DESC GetRTVDescriptor(uint32_t mipLevel,
55                                                        uint32_t baseArrayLayer,
56                                                        uint32_t layerCount) const;
57         D3D12_DEPTH_STENCIL_VIEW_DESC GetDSVDescriptor(uint32_t mipLevel,
58                                                        uint32_t baseArrayLayer,
59                                                        uint32_t layerCount) const;
60         void EnsureSubresourceContentInitialized(CommandRecordingContext* commandContext,
61                                                  const SubresourceRange& range);
62 
63         void TrackUsageAndGetResourceBarrierForPass(CommandRecordingContext* commandContext,
64                                                     std::vector<D3D12_RESOURCE_BARRIER>* barrier,
65                                                     const PassTextureUsage& textureUsages);
66         void TransitionUsageAndGetResourceBarrier(CommandRecordingContext* commandContext,
67                                                   std::vector<D3D12_RESOURCE_BARRIER>* barrier,
68                                                   wgpu::TextureUsage usage,
69                                                   const SubresourceRange& range);
70         void TrackUsageAndTransitionNow(CommandRecordingContext* commandContext,
71                                         wgpu::TextureUsage usage,
72                                         const SubresourceRange& range);
73         void TrackUsageAndTransitionNow(CommandRecordingContext* commandContext,
74                                         D3D12_RESOURCE_STATES newState,
75                                         const SubresourceRange& range);
76         void TrackAllUsageAndTransitionNow(CommandRecordingContext* commandContext,
77                                            wgpu::TextureUsage usage);
78         void TrackAllUsageAndTransitionNow(CommandRecordingContext* commandContext,
79                                            D3D12_RESOURCE_STATES newState);
80 
81       private:
82         Texture(Device* device, const TextureDescriptor* descriptor, TextureState state);
83         ~Texture() override;
84         using TextureBase::TextureBase;
85 
86         MaybeError InitializeAsInternalTexture();
87         MaybeError InitializeAsExternalTexture(const TextureDescriptor* descriptor,
88                                                HANDLE sharedHandle,
89                                                ExternalMutexSerial acquireMutexKey,
90                                                bool isSwapChainTexture);
91 
92         // Dawn API
93         void DestroyImpl() override;
94         MaybeError ClearTexture(CommandRecordingContext* commandContext,
95                                 const SubresourceRange& range,
96                                 TextureBase::ClearValue clearValue);
97 
98         void TransitionUsageAndGetResourceBarrier(CommandRecordingContext* commandContext,
99                                                   std::vector<D3D12_RESOURCE_BARRIER>* barrier,
100                                                   D3D12_RESOURCE_STATES newState,
101                                                   const SubresourceRange& range);
102 
103         void TransitionSingleOrAllSubresources(std::vector<D3D12_RESOURCE_BARRIER>* barriers,
104                                                uint32_t index,
105                                                D3D12_RESOURCE_STATES subresourceNewState,
106                                                ExecutionSerial pendingCommandSerial,
107                                                bool allSubresources);
108         void HandleTransitionSpecialCases(CommandRecordingContext* commandContext);
109 
110         bool mSameLastUsagesAcrossSubresources = true;
111 
112         struct StateAndDecay {
113             D3D12_RESOURCE_STATES lastState;
114             ExecutionSerial lastDecaySerial;
115             bool isValidToDecay;
116         };
117         std::vector<StateAndDecay> mSubresourceStateAndDecay;
118 
119         ResourceHeapAllocation mResourceAllocation;
120         bool mSwapChainTexture = false;
121 
122         ExternalMutexSerial mAcquireMutexKey = ExternalMutexSerial(0);
123         ComPtr<IDXGIKeyedMutex> mDxgiKeyedMutex;
124     };
125 
126     class TextureView final : public TextureViewBase {
127       public:
128         TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor);
129 
130         DXGI_FORMAT GetD3D12Format() const;
131 
132         const D3D12_SHADER_RESOURCE_VIEW_DESC& GetSRVDescriptor() const;
133         D3D12_RENDER_TARGET_VIEW_DESC GetRTVDescriptor() const;
134         D3D12_DEPTH_STENCIL_VIEW_DESC GetDSVDescriptor() const;
135         D3D12_UNORDERED_ACCESS_VIEW_DESC GetUAVDescriptor() const;
136 
137       private:
138         D3D12_SHADER_RESOURCE_VIEW_DESC mSrvDesc;
139     };
140 }}  // namespace dawn_native::d3d12
141 
142 #endif  // DAWNNATIVE_D3D12_TEXTURED3D12_H_
143