• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Tint 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 "src/inspector/resource_binding.h"
16 
17 #include "src/sem/array.h"
18 #include "src/sem/f32_type.h"
19 #include "src/sem/i32_type.h"
20 #include "src/sem/matrix_type.h"
21 #include "src/sem/type.h"
22 #include "src/sem/u32_type.h"
23 #include "src/sem/vector_type.h"
24 
25 namespace tint {
26 namespace inspector {
27 
28 ResourceBinding::TextureDimension
TypeTextureDimensionToResourceBindingTextureDimension(const ast::TextureDimension & type_dim)29 TypeTextureDimensionToResourceBindingTextureDimension(
30     const ast::TextureDimension& type_dim) {
31   switch (type_dim) {
32     case ast::TextureDimension::k1d:
33       return ResourceBinding::TextureDimension::k1d;
34     case ast::TextureDimension::k2d:
35       return ResourceBinding::TextureDimension::k2d;
36     case ast::TextureDimension::k2dArray:
37       return ResourceBinding::TextureDimension::k2dArray;
38     case ast::TextureDimension::k3d:
39       return ResourceBinding::TextureDimension::k3d;
40     case ast::TextureDimension::kCube:
41       return ResourceBinding::TextureDimension::kCube;
42     case ast::TextureDimension::kCubeArray:
43       return ResourceBinding::TextureDimension::kCubeArray;
44     case ast::TextureDimension::kNone:
45       return ResourceBinding::TextureDimension::kNone;
46   }
47   return ResourceBinding::TextureDimension::kNone;
48 }
49 
BaseTypeToSampledKind(const sem::Type * base_type)50 ResourceBinding::SampledKind BaseTypeToSampledKind(const sem::Type* base_type) {
51   if (!base_type) {
52     return ResourceBinding::SampledKind::kUnknown;
53   }
54 
55   if (auto* at = base_type->As<sem::Array>()) {
56     base_type = at->ElemType();
57   } else if (auto* mt = base_type->As<sem::Matrix>()) {
58     base_type = mt->type();
59   } else if (auto* vt = base_type->As<sem::Vector>()) {
60     base_type = vt->type();
61   }
62 
63   if (base_type->Is<sem::F32>()) {
64     return ResourceBinding::SampledKind::kFloat;
65   } else if (base_type->Is<sem::U32>()) {
66     return ResourceBinding::SampledKind::kUInt;
67   } else if (base_type->Is<sem::I32>()) {
68     return ResourceBinding::SampledKind::kSInt;
69   } else {
70     return ResourceBinding::SampledKind::kUnknown;
71   }
72 }
73 
TypeImageFormatToResourceBindingImageFormat(const ast::ImageFormat & image_format)74 ResourceBinding::ImageFormat TypeImageFormatToResourceBindingImageFormat(
75     const ast::ImageFormat& image_format) {
76   switch (image_format) {
77     case ast::ImageFormat::kR8Unorm:
78       return ResourceBinding::ImageFormat::kR8Unorm;
79     case ast::ImageFormat::kR8Snorm:
80       return ResourceBinding::ImageFormat::kR8Snorm;
81     case ast::ImageFormat::kR8Uint:
82       return ResourceBinding::ImageFormat::kR8Uint;
83     case ast::ImageFormat::kR8Sint:
84       return ResourceBinding::ImageFormat::kR8Sint;
85     case ast::ImageFormat::kR16Uint:
86       return ResourceBinding::ImageFormat::kR16Uint;
87     case ast::ImageFormat::kR16Sint:
88       return ResourceBinding::ImageFormat::kR16Sint;
89     case ast::ImageFormat::kR16Float:
90       return ResourceBinding::ImageFormat::kR16Float;
91     case ast::ImageFormat::kRg8Unorm:
92       return ResourceBinding::ImageFormat::kRg8Unorm;
93     case ast::ImageFormat::kRg8Snorm:
94       return ResourceBinding::ImageFormat::kRg8Snorm;
95     case ast::ImageFormat::kRg8Uint:
96       return ResourceBinding::ImageFormat::kRg8Uint;
97     case ast::ImageFormat::kRg8Sint:
98       return ResourceBinding::ImageFormat::kRg8Sint;
99     case ast::ImageFormat::kR32Uint:
100       return ResourceBinding::ImageFormat::kR32Uint;
101     case ast::ImageFormat::kR32Sint:
102       return ResourceBinding::ImageFormat::kR32Sint;
103     case ast::ImageFormat::kR32Float:
104       return ResourceBinding::ImageFormat::kR32Float;
105     case ast::ImageFormat::kRg16Uint:
106       return ResourceBinding::ImageFormat::kRg16Uint;
107     case ast::ImageFormat::kRg16Sint:
108       return ResourceBinding::ImageFormat::kRg16Sint;
109     case ast::ImageFormat::kRg16Float:
110       return ResourceBinding::ImageFormat::kRg16Float;
111     case ast::ImageFormat::kRgba8Unorm:
112       return ResourceBinding::ImageFormat::kRgba8Unorm;
113     case ast::ImageFormat::kRgba8UnormSrgb:
114       return ResourceBinding::ImageFormat::kRgba8UnormSrgb;
115     case ast::ImageFormat::kRgba8Snorm:
116       return ResourceBinding::ImageFormat::kRgba8Snorm;
117     case ast::ImageFormat::kRgba8Uint:
118       return ResourceBinding::ImageFormat::kRgba8Uint;
119     case ast::ImageFormat::kRgba8Sint:
120       return ResourceBinding::ImageFormat::kRgba8Sint;
121     case ast::ImageFormat::kBgra8Unorm:
122       return ResourceBinding::ImageFormat::kBgra8Unorm;
123     case ast::ImageFormat::kBgra8UnormSrgb:
124       return ResourceBinding::ImageFormat::kBgra8UnormSrgb;
125     case ast::ImageFormat::kRgb10A2Unorm:
126       return ResourceBinding::ImageFormat::kRgb10A2Unorm;
127     case ast::ImageFormat::kRg11B10Float:
128       return ResourceBinding::ImageFormat::kRg11B10Float;
129     case ast::ImageFormat::kRg32Uint:
130       return ResourceBinding::ImageFormat::kRg32Uint;
131     case ast::ImageFormat::kRg32Sint:
132       return ResourceBinding::ImageFormat::kRg32Sint;
133     case ast::ImageFormat::kRg32Float:
134       return ResourceBinding::ImageFormat::kRg32Float;
135     case ast::ImageFormat::kRgba16Uint:
136       return ResourceBinding::ImageFormat::kRgba16Uint;
137     case ast::ImageFormat::kRgba16Sint:
138       return ResourceBinding::ImageFormat::kRgba16Sint;
139     case ast::ImageFormat::kRgba16Float:
140       return ResourceBinding::ImageFormat::kRgba16Float;
141     case ast::ImageFormat::kRgba32Uint:
142       return ResourceBinding::ImageFormat::kRgba32Uint;
143     case ast::ImageFormat::kRgba32Sint:
144       return ResourceBinding::ImageFormat::kRgba32Sint;
145     case ast::ImageFormat::kRgba32Float:
146       return ResourceBinding::ImageFormat::kRgba32Float;
147     case ast::ImageFormat::kNone:
148       return ResourceBinding::ImageFormat::kNone;
149   }
150   return ResourceBinding::ImageFormat::kNone;
151 }
152 
153 }  // namespace inspector
154 }  // namespace tint
155