• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TEXTURE_H_
16 #define DAWNNATIVE_TEXTURE_H_
17 
18 #include "common/ityp_array.h"
19 #include "common/ityp_bitset.h"
20 #include "dawn_native/Error.h"
21 #include "dawn_native/Forward.h"
22 #include "dawn_native/ObjectBase.h"
23 #include "dawn_native/Subresource.h"
24 
25 #include "dawn_native/dawn_platform.h"
26 
27 #include <vector>
28 
29 namespace dawn_native {
30 
31     MaybeError ValidateTextureDescriptor(const DeviceBase* device,
32                                          const TextureDescriptor* descriptor);
33     MaybeError ValidateTextureViewDescriptor(const DeviceBase* device,
34                                              const TextureBase* texture,
35                                              const TextureViewDescriptor* descriptor);
36     TextureViewDescriptor GetTextureViewDescriptorWithDefaults(
37         const TextureBase* texture,
38         const TextureViewDescriptor* descriptor);
39 
40     bool IsValidSampleCount(uint32_t sampleCount);
41 
42     static constexpr wgpu::TextureUsage kReadOnlyTextureUsages =
43         wgpu::TextureUsage::CopySrc | wgpu::TextureUsage::TextureBinding |
44         kReadOnlyRenderAttachment;
45 
46     class TextureBase : public ApiObjectBase {
47       public:
48         enum class TextureState { OwnedInternal, OwnedExternal, Destroyed };
49         enum class ClearValue { Zero, NonZero };
50         TextureBase(DeviceBase* device, const TextureDescriptor* descriptor, TextureState state);
51 
52         static TextureBase* MakeError(DeviceBase* device);
53 
54         ObjectType GetType() const override;
55 
56         wgpu::TextureDimension GetDimension() const;
57         const Format& GetFormat() const;
58         const Extent3D& GetSize() const;
59         uint32_t GetWidth() const;
60         uint32_t GetHeight() const;
61         uint32_t GetDepth() const;
62         uint32_t GetArrayLayers() const;
63         uint32_t GetNumMipLevels() const;
64         SubresourceRange GetAllSubresources() const;
65         uint32_t GetSampleCount() const;
66         uint32_t GetSubresourceCount() const;
67 
68         // |GetUsage| returns the usage with which the texture was created using the base WebGPU
69         // API. The dawn-internal-usages extension may add additional usages. |GetInternalUsage|
70         // returns the union of base usage and the usages added by the extension.
71         wgpu::TextureUsage GetUsage() const;
72         wgpu::TextureUsage GetInternalUsage() const;
73 
74         TextureState GetTextureState() const;
75         uint32_t GetSubresourceIndex(uint32_t mipLevel, uint32_t arraySlice, Aspect aspect) const;
76         bool IsSubresourceContentInitialized(const SubresourceRange& range) const;
77         void SetIsSubresourceContentInitialized(bool isInitialized, const SubresourceRange& range);
78 
79         MaybeError ValidateCanUseInSubmitNow() const;
80 
81         bool IsMultisampledTexture() const;
82 
83         // For a texture with non-block-compressed texture format, its physical size is always equal
84         // to its virtual size. For a texture with block compressed texture format, the physical
85         // size is the one with paddings if necessary, which is always a multiple of the block size
86         // and used in texture copying. The virtual size is the one without paddings, which is not
87         // required to be a multiple of the block size and used in texture sampling.
88         Extent3D GetMipLevelPhysicalSize(uint32_t level) const;
89         Extent3D GetMipLevelVirtualSize(uint32_t level) const;
90         Extent3D ClampToMipLevelVirtualSize(uint32_t level,
91                                             const Origin3D& origin,
92                                             const Extent3D& extent) const;
93 
94         // Dawn API
95         TextureViewBase* APICreateView(const TextureViewDescriptor* descriptor = nullptr);
96         void APIDestroy();
97 
98       protected:
99         // Constructor used only for mocking and testing.
100         TextureBase(DeviceBase* device, TextureState state);
101         void DestroyImpl() override;
102 
103       private:
104         TextureBase(DeviceBase* device, ObjectBase::ErrorTag tag);
105 
106         MaybeError ValidateDestroy() const;
107         wgpu::TextureDimension mDimension;
108         const Format& mFormat;
109         Extent3D mSize;
110         uint32_t mMipLevelCount;
111         uint32_t mSampleCount;
112         wgpu::TextureUsage mUsage = wgpu::TextureUsage::None;
113         wgpu::TextureUsage mInternalUsage = wgpu::TextureUsage::None;
114         TextureState mState;
115 
116         // TODO(crbug.com/dawn/845): Use a more optimized data structure to save space
117         std::vector<bool> mIsSubresourceContentInitializedAtIndex;
118     };
119 
120     class TextureViewBase : public ApiObjectBase {
121       public:
122         TextureViewBase(TextureBase* texture, const TextureViewDescriptor* descriptor);
123 
124         static TextureViewBase* MakeError(DeviceBase* device);
125 
126         ObjectType GetType() const override;
127 
128         const TextureBase* GetTexture() const;
129         TextureBase* GetTexture();
130 
131         Aspect GetAspects() const;
132         const Format& GetFormat() const;
133         wgpu::TextureViewDimension GetDimension() const;
134         uint32_t GetBaseMipLevel() const;
135         uint32_t GetLevelCount() const;
136         uint32_t GetBaseArrayLayer() const;
137         uint32_t GetLayerCount() const;
138         const SubresourceRange& GetSubresourceRange() const;
139 
140       protected:
141         // Constructor used only for mocking and testing.
142         TextureViewBase(TextureBase* texture);
143         void DestroyImpl() override;
144 
145       private:
146         TextureViewBase(DeviceBase* device, ObjectBase::ErrorTag tag);
147 
148         Ref<TextureBase> mTexture;
149 
150         const Format& mFormat;
151         wgpu::TextureViewDimension mDimension;
152         SubresourceRange mRange;
153     };
154 
155 }  // namespace dawn_native
156 
157 #endif  // DAWNNATIVE_TEXTURE_H_
158