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 "common/Serial.h"
19 #include "dawn_native/Texture.h"
20 
21 #include "dawn_native/DawnNative.h"
22 #include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h"
23 #include "dawn_native/d3d12/d3d12_platform.h"
24 
25 namespace dawn_native { namespace d3d12 {
26 
27     class CommandRecordingContext;
28     class Device;
29 
30     DXGI_FORMAT D3D12TextureFormat(wgpu::TextureFormat format);
31     MaybeError ValidateD3D12TextureCanBeWrapped(ID3D12Resource* d3d12Resource,
32                                                 const TextureDescriptor* descriptor);
33     MaybeError ValidateTextureDescriptorCanBeWrapped(const TextureDescriptor* descriptor);
34 
35     class Texture : public TextureBase {
36       public:
37         static ResultOrError<TextureBase*> Create(Device* device,
38                                                   const TextureDescriptor* descriptor);
39         static ResultOrError<TextureBase*> Create(Device* device,
40                                                   const ExternalImageDescriptor* descriptor,
41                                                   HANDLE sharedHandle,
42                                                   uint64_t acquireMutexKey,
43                                                   bool isSwapChainTexture);
44         Texture(Device* device,
45                 const TextureDescriptor* descriptor,
46                 ComPtr<ID3D12Resource> d3d12Texture);
47 
48         ~Texture();
49 
50         DXGI_FORMAT GetD3D12Format() const;
51         ID3D12Resource* GetD3D12Resource() const;
52 
53         D3D12_RENDER_TARGET_VIEW_DESC GetRTVDescriptor(uint32_t mipLevel,
54                                                        uint32_t baseArrayLayer,
55                                                        uint32_t layerCount) const;
56         D3D12_DEPTH_STENCIL_VIEW_DESC GetDSVDescriptor(uint32_t mipLevel,
57                                                        uint32_t baseArrayLayer,
58                                                        uint32_t layerCount) const;
59         void EnsureSubresourceContentInitialized(CommandRecordingContext* commandContext,
60                                                  uint32_t baseMipLevel,
61                                                  uint32_t levelCount,
62                                                  uint32_t baseArrayLayer,
63                                                  uint32_t layerCount);
64 
65         bool TrackUsageAndGetResourceBarrier(CommandRecordingContext* commandContext,
66                                              D3D12_RESOURCE_BARRIER* barrier,
67                                              wgpu::TextureUsage newUsage);
68         void TrackUsageAndTransitionNow(CommandRecordingContext* commandContext,
69                                         wgpu::TextureUsage usage);
70         void TrackUsageAndTransitionNow(CommandRecordingContext* commandContext,
71                                         D3D12_RESOURCE_STATES newState);
72 
73       private:
74         using TextureBase::TextureBase;
75 
76         MaybeError InitializeAsInternalTexture();
77         MaybeError InitializeAsExternalTexture(const TextureDescriptor* descriptor,
78                                                HANDLE sharedHandle,
79                                                uint64_t acquireMutexKey,
80                                                bool isSwapChainTexture);
81 
82         // Dawn API
83         void DestroyImpl() override;
84         MaybeError ClearTexture(CommandRecordingContext* commandContext,
85                                 uint32_t baseMipLevel,
86                                 uint32_t levelCount,
87                                 uint32_t baseArrayLayer,
88                                 uint32_t layerCount,
89                                 TextureBase::ClearValue clearValue);
90 
91         UINT16 GetDepthOrArraySize();
92 
93         bool TrackUsageAndGetResourceBarrier(CommandRecordingContext* commandContext,
94                                              D3D12_RESOURCE_BARRIER* barrier,
95                                              D3D12_RESOURCE_STATES newState);
96         bool TransitionUsageAndGetResourceBarrier(CommandRecordingContext* commandContext,
97                                                   D3D12_RESOURCE_BARRIER* barrier,
98                                                   D3D12_RESOURCE_STATES newState);
99 
100         ResourceHeapAllocation mResourceAllocation;
101         D3D12_RESOURCE_STATES mLastState = D3D12_RESOURCE_STATES::D3D12_RESOURCE_STATE_COMMON;
102 
103         Serial mLastUsedSerial = UINT64_MAX;
104         bool mValidToDecay = false;
105         bool mSwapChainTexture = false;
106 
107         Serial mAcquireMutexKey = 0;
108         ComPtr<IDXGIKeyedMutex> mDxgiKeyedMutex;
109     };
110 
111     class TextureView : public TextureViewBase {
112       public:
113         TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor);
114 
115         DXGI_FORMAT GetD3D12Format() const;
116 
117         const D3D12_SHADER_RESOURCE_VIEW_DESC& GetSRVDescriptor() const;
118         D3D12_RENDER_TARGET_VIEW_DESC GetRTVDescriptor() const;
119         D3D12_DEPTH_STENCIL_VIEW_DESC GetDSVDescriptor() const;
120 
121       private:
122         D3D12_SHADER_RESOURCE_VIEW_DESC mSrvDesc;
123     };
124 }}  // namespace dawn_native::d3d12
125 
126 #endif  // DAWNNATIVE_D3D12_TEXTURED3D12_H_
127