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 class D3D11on12ResourceCacheEntry; 32 33 DXGI_FORMAT D3D12TextureFormat(wgpu::TextureFormat format); 34 MaybeError ValidateD3D12TextureCanBeWrapped(ID3D12Resource* d3d12Resource, 35 const TextureDescriptor* descriptor); 36 MaybeError ValidateTextureDescriptorCanBeWrapped(const TextureDescriptor* descriptor); 37 MaybeError ValidateD3D12VideoTextureCanBeShared(Device* device, DXGI_FORMAT textureFormat); 38 39 class Texture final : public TextureBase { 40 public: 41 static ResultOrError<Ref<Texture>> Create(Device* device, 42 const TextureDescriptor* descriptor); 43 static ResultOrError<Ref<Texture>> CreateExternalImage( 44 Device* device, 45 const TextureDescriptor* descriptor, 46 ComPtr<ID3D12Resource> d3d12Texture, 47 Ref<D3D11on12ResourceCacheEntry> d3d11on12Resource, 48 ExternalMutexSerial acquireMutexKey, 49 ExternalMutexSerial releaseMutexKey, 50 bool isSwapChainTexture, 51 bool isInitialized); 52 static ResultOrError<Ref<Texture>> Create(Device* device, 53 const TextureDescriptor* descriptor, 54 ComPtr<ID3D12Resource> d3d12Texture); 55 56 DXGI_FORMAT GetD3D12Format() const; 57 ID3D12Resource* GetD3D12Resource() const; 58 DXGI_FORMAT GetD3D12CopyableSubresourceFormat(Aspect aspect) const; 59 60 D3D12_RENDER_TARGET_VIEW_DESC GetRTVDescriptor(uint32_t mipLevel, 61 uint32_t baseSlice, 62 uint32_t sliceCount) const; 63 D3D12_DEPTH_STENCIL_VIEW_DESC GetDSVDescriptor(uint32_t mipLevel, 64 uint32_t baseArrayLayer, 65 uint32_t layerCount, 66 Aspect aspects, 67 bool depthReadOnly, 68 bool stencilReadOnly) const; 69 void EnsureSubresourceContentInitialized(CommandRecordingContext* commandContext, 70 const SubresourceRange& range); 71 72 void TrackUsageAndGetResourceBarrierForPass(CommandRecordingContext* commandContext, 73 std::vector<D3D12_RESOURCE_BARRIER>* barrier, 74 const TextureSubresourceUsage& textureUsages); 75 void TransitionUsageAndGetResourceBarrier(CommandRecordingContext* commandContext, 76 std::vector<D3D12_RESOURCE_BARRIER>* barrier, 77 wgpu::TextureUsage usage, 78 const SubresourceRange& range); 79 void TrackUsageAndTransitionNow(CommandRecordingContext* commandContext, 80 wgpu::TextureUsage usage, 81 const SubresourceRange& range); 82 void TrackUsageAndTransitionNow(CommandRecordingContext* commandContext, 83 D3D12_RESOURCE_STATES newState, 84 const SubresourceRange& range); 85 void TrackAllUsageAndTransitionNow(CommandRecordingContext* commandContext, 86 wgpu::TextureUsage usage); 87 void TrackAllUsageAndTransitionNow(CommandRecordingContext* commandContext, 88 D3D12_RESOURCE_STATES newState); 89 90 private: 91 Texture(Device* device, const TextureDescriptor* descriptor, TextureState state); 92 ~Texture() override; 93 using TextureBase::TextureBase; 94 95 MaybeError InitializeAsInternalTexture(); 96 MaybeError InitializeAsExternalTexture(const TextureDescriptor* descriptor, 97 ComPtr<ID3D12Resource> d3d12Texture, 98 Ref<D3D11on12ResourceCacheEntry> d3d11on12Resource, 99 ExternalMutexSerial acquireMutexKey, 100 ExternalMutexSerial releaseMutexKey, 101 bool isSwapChainTexture); 102 MaybeError InitializeAsSwapChainTexture(ComPtr<ID3D12Resource> d3d12Texture); 103 104 void SetLabelHelper(const char* prefix); 105 106 // Dawn API 107 void SetLabelImpl() override; 108 void DestroyImpl() override; 109 110 MaybeError ClearTexture(CommandRecordingContext* commandContext, 111 const SubresourceRange& range, 112 TextureBase::ClearValue clearValue); 113 114 // Barriers implementation details. 115 struct StateAndDecay { 116 D3D12_RESOURCE_STATES lastState; 117 ExecutionSerial lastDecaySerial; 118 bool isValidToDecay; 119 120 bool operator==(const StateAndDecay& other) const; 121 }; 122 void TransitionUsageAndGetResourceBarrier(CommandRecordingContext* commandContext, 123 std::vector<D3D12_RESOURCE_BARRIER>* barrier, 124 D3D12_RESOURCE_STATES newState, 125 const SubresourceRange& range); 126 void TransitionSubresourceRange(std::vector<D3D12_RESOURCE_BARRIER>* barriers, 127 const SubresourceRange& range, 128 StateAndDecay* state, 129 D3D12_RESOURCE_STATES subresourceNewState, 130 ExecutionSerial pendingCommandSerial) const; 131 void HandleTransitionSpecialCases(CommandRecordingContext* commandContext); 132 133 SubresourceStorage<StateAndDecay> mSubresourceStateAndDecay; 134 135 ResourceHeapAllocation mResourceAllocation; 136 bool mSwapChainTexture = false; 137 D3D12_RESOURCE_FLAGS mD3D12ResourceFlags; 138 139 ExternalMutexSerial mAcquireMutexKey = ExternalMutexSerial(0); 140 ExternalMutexSerial mReleaseMutexKey = ExternalMutexSerial(0); 141 Ref<D3D11on12ResourceCacheEntry> mD3D11on12Resource; 142 }; 143 144 class TextureView final : public TextureViewBase { 145 public: 146 static Ref<TextureView> Create(TextureBase* texture, 147 const TextureViewDescriptor* descriptor); 148 149 DXGI_FORMAT GetD3D12Format() const; 150 151 const D3D12_SHADER_RESOURCE_VIEW_DESC& GetSRVDescriptor() const; 152 D3D12_RENDER_TARGET_VIEW_DESC GetRTVDescriptor() const; 153 D3D12_DEPTH_STENCIL_VIEW_DESC GetDSVDescriptor(bool depthReadOnly, 154 bool stencilReadOnly) const; 155 D3D12_UNORDERED_ACCESS_VIEW_DESC GetUAVDescriptor() const; 156 157 private: 158 TextureView(TextureBase* texture, const TextureViewDescriptor* descriptor); 159 160 D3D12_SHADER_RESOURCE_VIEW_DESC mSrvDesc; 161 }; 162 }} // namespace dawn_native::d3d12 163 164 #endif // DAWNNATIVE_D3D12_TEXTURED3D12_H_ 165