• 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 "dawn_native/Error.h"
19 #include "dawn_native/Forward.h"
20 #include "dawn_native/ObjectBase.h"
21 
22 #include "dawn_native/dawn_platform.h"
23 
24 #include <vector>
25 
26 namespace dawn_native {
27     MaybeError ValidateTextureDescriptor(const DeviceBase* device,
28                                          const TextureDescriptor* descriptor);
29     MaybeError ValidateTextureViewDescriptor(const DeviceBase* device,
30                                              const TextureBase* texture,
31                                              const TextureViewDescriptor* descriptor);
32 
33     bool IsValidSampleCount(uint32_t sampleCount);
34 
35     static constexpr dawn::TextureUsageBit kReadOnlyTextureUsages = dawn::TextureUsageBit::CopySrc |
36                                                                     dawn::TextureUsageBit::Sampled |
37                                                                     dawn::TextureUsageBit::Present;
38 
39     static constexpr dawn::TextureUsageBit kWritableTextureUsages =
40         dawn::TextureUsageBit::CopyDst | dawn::TextureUsageBit::Storage |
41         dawn::TextureUsageBit::OutputAttachment;
42 
43     class TextureBase : public ObjectBase {
44       public:
45         enum class TextureState { OwnedInternal, OwnedExternal, Destroyed };
46 
47         TextureBase(DeviceBase* device, const TextureDescriptor* descriptor, TextureState state);
48 
49         static TextureBase* MakeError(DeviceBase* device);
50 
51         dawn::TextureDimension GetDimension() const;
52         const Format& GetFormat() const;
53         const Extent3D& GetSize() const;
54         uint32_t GetArrayLayers() const;
55         uint32_t GetNumMipLevels() const;
56         uint32_t GetSampleCount() const;
57         dawn::TextureUsageBit GetUsage() const;
58         TextureState GetTextureState() const;
59         uint32_t GetSubresourceIndex(uint32_t mipLevel, uint32_t arraySlice) const;
60         bool IsSubresourceContentInitialized(uint32_t baseMipLevel,
61                                              uint32_t levelCount,
62                                              uint32_t baseArrayLayer,
63                                              uint32_t layerCount) const;
64         void SetIsSubresourceContentInitialized(uint32_t baseMipLevel,
65                                                 uint32_t levelCount,
66                                                 uint32_t baseArrayLayer,
67                                                 uint32_t layerCount);
68 
69         MaybeError ValidateCanUseInSubmitNow() const;
70 
71         bool IsMultisampledTexture() const;
72 
73         // For a texture with non-block-compressed texture format, its physical size is always equal
74         // to its virtual size. For a texture with block compressed texture format, the physical
75         // size is the one with paddings if necessary, which is always a multiple of the block size
76         // and used in texture copying. The virtual size is the one without paddings, which is not
77         // required to be a multiple of the block size and used in texture sampling.
78         Extent3D GetMipLevelPhysicalSize(uint64_t level) const;
79         Extent3D GetMipLevelVirtualSize(uint64_t level) const;
80 
81         // Dawn API
82         TextureViewBase* CreateDefaultView();
83         TextureViewBase* CreateView(const TextureViewDescriptor* descriptor);
84         void Destroy();
85 
86       protected:
87         void DestroyInternal();
88 
89       private:
90         TextureBase(DeviceBase* device, ObjectBase::ErrorTag tag);
91         virtual void DestroyImpl();
92 
93         MaybeError ValidateDestroy() const;
94         dawn::TextureDimension mDimension;
95         // TODO(cwallez@chromium.org): This should be deduplicated in the Device
96         const Format& mFormat;
97         Extent3D mSize;
98         uint32_t mArrayLayerCount;
99         uint32_t mMipLevelCount;
100         uint32_t mSampleCount;
101         dawn::TextureUsageBit mUsage = dawn::TextureUsageBit::None;
102         TextureState mState;
103 
104         // TODO(natlee@microsoft.com): Use a more optimized data structure to save space
105         std::vector<bool> mIsSubresourceContentInitializedAtIndex;
106     };
107 
108     class TextureViewBase : public ObjectBase {
109       public:
110         TextureViewBase(TextureBase* texture, const TextureViewDescriptor* descriptor);
111 
112         static TextureViewBase* MakeError(DeviceBase* device);
113 
114         const TextureBase* GetTexture() const;
115         TextureBase* GetTexture();
116 
117         const Format& GetFormat() const;
118         uint32_t GetBaseMipLevel() const;
119         uint32_t GetLevelCount() const;
120         uint32_t GetBaseArrayLayer() const;
121         uint32_t GetLayerCount() const;
122 
123       private:
124         TextureViewBase(DeviceBase* device, ObjectBase::ErrorTag tag);
125 
126         Ref<TextureBase> mTexture;
127 
128         // TODO(cwallez@chromium.org): This should be deduplicated in the Device
129         const Format& mFormat;
130         uint32_t mBaseMipLevel;
131         uint32_t mMipLevelCount;
132         uint32_t mBaseArrayLayer;
133         uint32_t mArrayLayerCount;
134     };
135 
136 }  // namespace dawn_native
137 
138 #endif  // DAWNNATIVE_TEXTURE_H_
139