• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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_FORMAT_H_
16 #define DAWNNATIVE_FORMAT_H_
17 
18 #include "dawn_native/dawn_platform.h"
19 
20 #include "common/ityp_bitset.h"
21 #include "dawn_native/EnumClassBitmasks.h"
22 #include "dawn_native/Error.h"
23 #include "dawn_native/Subresource.h"
24 
25 #include <array>
26 
27 // About multi-planar formats.
28 //
29 // Dawn supports additional multi-planar formats when the multiplanar-formats extension is enabled.
30 // When enabled, Dawn treats planar data as sub-resources (ie. 1 sub-resource == 1 view == 1 plane).
31 // A multi-planar format name encodes the channel mapping and order of planes. For example,
32 // R8BG8Biplanar420Unorm is YUV 4:2:0 where Plane 0 = R8, and Plane 1 = BG8.
33 //
34 // Requirements:
35 // * Plane aspects cannot be combined with color, depth, or stencil aspects.
36 // * Only compatible multi-planar formats of planes can be used with multi-planar texture
37 // formats.
38 // * Can't access multiple planes without creating per plane views (no color conversion).
39 // * Multi-planar format cannot be written or read without a per plane view.
40 //
41 // TODO(dawn:551): Consider moving this comment.
42 
43 namespace dawn_native {
44 
45     enum class Aspect : uint8_t;
46     class DeviceBase;
47 
48     // This mirrors wgpu::TextureSampleType as a bitmask instead.
49     enum class SampleTypeBit : uint8_t {
50         None = 0x0,
51         Float = 0x1,
52         UnfilterableFloat = 0x2,
53         Depth = 0x4,
54         Sint = 0x8,
55         Uint = 0x10,
56     };
57 
58     // Converts an wgpu::TextureComponentType to its bitmask representation.
59     SampleTypeBit ToSampleTypeBit(wgpu::TextureComponentType type);
60     // Converts an wgpu::TextureSampleType to its bitmask representation.
61     SampleTypeBit SampleTypeToSampleTypeBit(wgpu::TextureSampleType sampleType);
62 
63     struct TexelBlockInfo {
64         uint32_t byteSize;
65         uint32_t width;
66         uint32_t height;
67     };
68 
69     struct AspectInfo {
70         TexelBlockInfo block;
71         // TODO(crbug.com/dawn/367): Replace TextureComponentType with TextureSampleType, or make it
72         // an internal Dawn enum.
73         wgpu::TextureComponentType baseType;
74         SampleTypeBit supportedSampleTypes;
75         wgpu::TextureFormat format;
76     };
77 
78     // The number of formats Dawn knows about. Asserts in BuildFormatTable ensure that this is the
79     // exact number of known format.
80     static constexpr size_t kKnownFormatCount = 96;
81 
82     struct Format;
83     using FormatTable = std::array<Format, kKnownFormatCount>;
84 
85     // A wgpu::TextureFormat along with all the information about it necessary for validation.
86     struct Format {
87         wgpu::TextureFormat format;
88         bool isRenderable;
89         bool isCompressed;
90         // A format can be known but not supported because it is part of a disabled extension.
91         bool isSupported;
92         bool supportsStorageUsage;
93         Aspect aspects;
94         // Only used for renderable color formats, number of color channels.
95         uint8_t componentCount;
96 
97         bool IsColor() const;
98         bool HasDepth() const;
99         bool HasStencil() const;
100         bool HasDepthOrStencil() const;
101 
102         // IsMultiPlanar() returns true if the format allows selecting a plane index. This is only
103         // allowed by multi-planar formats (ex. NV12).
104         bool IsMultiPlanar() const;
105 
106         const AspectInfo& GetAspectInfo(wgpu::TextureAspect aspect) const;
107         const AspectInfo& GetAspectInfo(Aspect aspect) const;
108 
109         // The index of the format in the list of all known formats: a unique number for each format
110         // in [0, kKnownFormatCount)
111         size_t GetIndex() const;
112 
113       private:
114         // Used to store the aspectInfo for one or more planes. For single plane "color" formats,
115         // only the first aspect info or aspectInfo[0] is valid. For depth-stencil, the first aspect
116         // info is depth and the second aspect info is stencil. For multi-planar formats,
117         // aspectInfo[i] is the ith plane.
118         std::array<AspectInfo, kMaxPlanesPerFormat> aspectInfo;
119 
120         friend FormatTable BuildFormatTable(const DeviceBase* device);
121     };
122 
123     // Implementation details of the format table in the device.
124 
125     // Returns the index of a format in the FormatTable.
126     size_t ComputeFormatIndex(wgpu::TextureFormat format);
127     // Builds the format table with the extensions enabled on the device.
128     FormatTable BuildFormatTable(const DeviceBase* device);
129 
130 }  // namespace dawn_native
131 
132 namespace dawn {
133 
134     template <>
135     struct IsDawnBitmask<dawn_native::SampleTypeBit> {
136         static constexpr bool enable = true;
137     };
138 
139 }  // namespace dawn
140 
141 #endif  // DAWNNATIVE_FORMAT_H_
142