blob: 4589ea02b0bff14692cb9ac998d6a589a9535a1b [file] [log] [blame]
Ryan Harrison44382a12021-06-17 23:29:03 +00001// 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
25namespace tint {
26namespace inspector {
27
28ResourceBinding::TextureDimension
29TypeTextureDimensionToResourceBindingTextureDimension(
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
50ResourceBinding::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>()) {
Ben Clayton86481202021-10-19 18:38:54 +000056 base_type = at->ElemType();
Ryan Harrison44382a12021-06-17 23:29:03 +000057 } 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
Ben Clayton294ce932022-01-06 17:20:22 +000074ResourceBinding::TexelFormat TypeTexelFormatToResourceBindingTexelFormat(
75 const ast::TexelFormat& image_format) {
Ryan Harrison44382a12021-06-17 23:29:03 +000076 switch (image_format) {
Ben Clayton294ce932022-01-06 17:20:22 +000077 case ast::TexelFormat::kR32Uint:
78 return ResourceBinding::TexelFormat::kR32Uint;
79 case ast::TexelFormat::kR32Sint:
80 return ResourceBinding::TexelFormat::kR32Sint;
81 case ast::TexelFormat::kR32Float:
82 return ResourceBinding::TexelFormat::kR32Float;
Ben Clayton294ce932022-01-06 17:20:22 +000083 case ast::TexelFormat::kRgba8Unorm:
84 return ResourceBinding::TexelFormat::kRgba8Unorm;
Ben Clayton294ce932022-01-06 17:20:22 +000085 case ast::TexelFormat::kRgba8Snorm:
86 return ResourceBinding::TexelFormat::kRgba8Snorm;
87 case ast::TexelFormat::kRgba8Uint:
88 return ResourceBinding::TexelFormat::kRgba8Uint;
89 case ast::TexelFormat::kRgba8Sint:
90 return ResourceBinding::TexelFormat::kRgba8Sint;
Ben Clayton294ce932022-01-06 17:20:22 +000091 case ast::TexelFormat::kRg32Uint:
92 return ResourceBinding::TexelFormat::kRg32Uint;
93 case ast::TexelFormat::kRg32Sint:
94 return ResourceBinding::TexelFormat::kRg32Sint;
95 case ast::TexelFormat::kRg32Float:
96 return ResourceBinding::TexelFormat::kRg32Float;
97 case ast::TexelFormat::kRgba16Uint:
98 return ResourceBinding::TexelFormat::kRgba16Uint;
99 case ast::TexelFormat::kRgba16Sint:
100 return ResourceBinding::TexelFormat::kRgba16Sint;
101 case ast::TexelFormat::kRgba16Float:
102 return ResourceBinding::TexelFormat::kRgba16Float;
103 case ast::TexelFormat::kRgba32Uint:
104 return ResourceBinding::TexelFormat::kRgba32Uint;
105 case ast::TexelFormat::kRgba32Sint:
106 return ResourceBinding::TexelFormat::kRgba32Sint;
107 case ast::TexelFormat::kRgba32Float:
108 return ResourceBinding::TexelFormat::kRgba32Float;
109 case ast::TexelFormat::kNone:
110 return ResourceBinding::TexelFormat::kNone;
Ryan Harrison44382a12021-06-17 23:29:03 +0000111 }
Ben Clayton294ce932022-01-06 17:20:22 +0000112 return ResourceBinding::TexelFormat::kNone;
Ryan Harrison44382a12021-06-17 23:29:03 +0000113}
114
115} // namespace inspector
116} // namespace tint