• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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 #include "dawn_native/Subresource.h"
16 
17 #include "common/Assert.h"
18 #include "dawn_native/Format.h"
19 
20 namespace dawn_native {
21 
ConvertSingleAspect(const Format & format,wgpu::TextureAspect aspect)22     Aspect ConvertSingleAspect(const Format& format, wgpu::TextureAspect aspect) {
23         Aspect aspectMask = ConvertAspect(format, aspect);
24         ASSERT(HasOneBit(aspectMask));
25         return aspectMask;
26     }
27 
ConvertAspect(const Format & format,wgpu::TextureAspect aspect)28     Aspect ConvertAspect(const Format& format, wgpu::TextureAspect aspect) {
29         Aspect aspectMask = SelectFormatAspects(format, aspect);
30         ASSERT(aspectMask != Aspect::None);
31         return aspectMask;
32     }
33 
ConvertViewAspect(const Format & format,wgpu::TextureAspect aspect)34     Aspect ConvertViewAspect(const Format& format, wgpu::TextureAspect aspect) {
35         // Color view |format| must be treated as the same plane |aspect|.
36         if (format.aspects == Aspect::Color) {
37             switch (aspect) {
38                 case wgpu::TextureAspect::Plane0Only:
39                     return Aspect::Plane0;
40                 case wgpu::TextureAspect::Plane1Only:
41                     return Aspect::Plane1;
42                 default:
43                     break;
44             }
45         }
46         return ConvertAspect(format, aspect);
47     }
48 
SelectFormatAspects(const Format & format,wgpu::TextureAspect aspect)49     Aspect SelectFormatAspects(const Format& format, wgpu::TextureAspect aspect) {
50         switch (aspect) {
51             case wgpu::TextureAspect::All:
52                 return format.aspects;
53             case wgpu::TextureAspect::DepthOnly:
54                 return format.aspects & Aspect::Depth;
55             case wgpu::TextureAspect::StencilOnly:
56                 return format.aspects & Aspect::Stencil;
57             case wgpu::TextureAspect::Plane0Only:
58                 return format.aspects & Aspect::Plane0;
59             case wgpu::TextureAspect::Plane1Only:
60                 return format.aspects & Aspect::Plane1;
61         }
62         UNREACHABLE();
63     }
64 
GetAspectIndex(Aspect aspect)65     uint8_t GetAspectIndex(Aspect aspect) {
66         ASSERT(HasOneBit(aspect));
67         switch (aspect) {
68             case Aspect::Color:
69             case Aspect::Depth:
70             case Aspect::Plane0:
71             case Aspect::CombinedDepthStencil:
72                 return 0;
73             case Aspect::Plane1:
74             case Aspect::Stencil:
75                 return 1;
76             default:
77                 UNREACHABLE();
78         }
79     }
80 
GetAspectCount(Aspect aspects)81     uint8_t GetAspectCount(Aspect aspects) {
82         // TODO(crbug.com/dawn/829): This should use popcount once Dawn has such a function.
83         // Note that we can't do a switch because compilers complain that Depth | Stencil is not
84         // a valid enum value.
85         if (aspects == Aspect::Color || aspects == Aspect::Depth ||
86             aspects == Aspect::CombinedDepthStencil) {
87             return 1;
88         } else if (aspects == (Aspect::Plane0 | Aspect::Plane1)) {
89             return 2;
90         } else {
91             ASSERT(aspects == (Aspect::Depth | Aspect::Stencil));
92             return 2;
93         }
94     }
95 
SubresourceRange(Aspect aspects,FirstAndCountRange<uint32_t> arrayLayerParam,FirstAndCountRange<uint32_t> mipLevelParams)96     SubresourceRange::SubresourceRange(Aspect aspects,
97                                        FirstAndCountRange<uint32_t> arrayLayerParam,
98                                        FirstAndCountRange<uint32_t> mipLevelParams)
99         : aspects(aspects),
100           baseArrayLayer(arrayLayerParam.first),
101           layerCount(arrayLayerParam.count),
102           baseMipLevel(mipLevelParams.first),
103           levelCount(mipLevelParams.count) {
104     }
105 
SubresourceRange()106     SubresourceRange::SubresourceRange()
107         : aspects(Aspect::None), baseArrayLayer(0), layerCount(0), baseMipLevel(0), levelCount(0) {
108     }
109 
110     // static
SingleMipAndLayer(uint32_t baseMipLevel,uint32_t baseArrayLayer,Aspect aspects)111     SubresourceRange SubresourceRange::SingleMipAndLayer(uint32_t baseMipLevel,
112                                                          uint32_t baseArrayLayer,
113                                                          Aspect aspects) {
114         return {aspects, {baseArrayLayer, 1}, {baseMipLevel, 1}};
115     }
116 
117     // static
MakeSingle(Aspect aspect,uint32_t baseArrayLayer,uint32_t baseMipLevel)118     SubresourceRange SubresourceRange::MakeSingle(Aspect aspect,
119                                                   uint32_t baseArrayLayer,
120                                                   uint32_t baseMipLevel) {
121         ASSERT(HasOneBit(aspect));
122         return {aspect, {baseArrayLayer, 1}, {baseMipLevel, 1}};
123     }
124 
125     // static
MakeFull(Aspect aspects,uint32_t layerCount,uint32_t levelCount)126     SubresourceRange SubresourceRange::MakeFull(Aspect aspects,
127                                                 uint32_t layerCount,
128                                                 uint32_t levelCount) {
129         return {aspects, {0, layerCount}, {0, levelCount}};
130     }
131 
132 }  // namespace dawn_native
133