• 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/opengl/SpirvUtils.h"
16 
17 namespace dawn_native {
18 
ShaderStageToExecutionModel(SingleShaderStage stage)19     spv::ExecutionModel ShaderStageToExecutionModel(SingleShaderStage stage) {
20         switch (stage) {
21             case SingleShaderStage::Vertex:
22                 return spv::ExecutionModelVertex;
23             case SingleShaderStage::Fragment:
24                 return spv::ExecutionModelFragment;
25             case SingleShaderStage::Compute:
26                 return spv::ExecutionModelGLCompute;
27         }
28         UNREACHABLE();
29     }
30 
ExecutionModelToShaderStage(spv::ExecutionModel model)31     SingleShaderStage ExecutionModelToShaderStage(spv::ExecutionModel model) {
32         switch (model) {
33             case spv::ExecutionModelVertex:
34                 return SingleShaderStage::Vertex;
35             case spv::ExecutionModelFragment:
36                 return SingleShaderStage::Fragment;
37             case spv::ExecutionModelGLCompute:
38                 return SingleShaderStage::Compute;
39             default:
40                 UNREACHABLE();
41         }
42     }
43 
SpirvDimToTextureViewDimension(spv::Dim dim,bool arrayed)44     wgpu::TextureViewDimension SpirvDimToTextureViewDimension(spv::Dim dim, bool arrayed) {
45         switch (dim) {
46             case spv::Dim::Dim1D:
47                 return wgpu::TextureViewDimension::e1D;
48             case spv::Dim::Dim2D:
49                 if (arrayed) {
50                     return wgpu::TextureViewDimension::e2DArray;
51                 } else {
52                     return wgpu::TextureViewDimension::e2D;
53                 }
54             case spv::Dim::Dim3D:
55                 return wgpu::TextureViewDimension::e3D;
56             case spv::Dim::DimCube:
57                 if (arrayed) {
58                     return wgpu::TextureViewDimension::CubeArray;
59                 } else {
60                     return wgpu::TextureViewDimension::Cube;
61                 }
62             default:
63                 UNREACHABLE();
64         }
65     }
66 
SpirvImageFormatToTextureFormat(spv::ImageFormat format)67     wgpu::TextureFormat SpirvImageFormatToTextureFormat(spv::ImageFormat format) {
68         switch (format) {
69             case spv::ImageFormatR8:
70                 return wgpu::TextureFormat::R8Unorm;
71             case spv::ImageFormatR8Snorm:
72                 return wgpu::TextureFormat::R8Snorm;
73             case spv::ImageFormatR8ui:
74                 return wgpu::TextureFormat::R8Uint;
75             case spv::ImageFormatR8i:
76                 return wgpu::TextureFormat::R8Sint;
77             case spv::ImageFormatR16ui:
78                 return wgpu::TextureFormat::R16Uint;
79             case spv::ImageFormatR16i:
80                 return wgpu::TextureFormat::R16Sint;
81             case spv::ImageFormatR16f:
82                 return wgpu::TextureFormat::R16Float;
83             case spv::ImageFormatRg8:
84                 return wgpu::TextureFormat::RG8Unorm;
85             case spv::ImageFormatRg8Snorm:
86                 return wgpu::TextureFormat::RG8Snorm;
87             case spv::ImageFormatRg8ui:
88                 return wgpu::TextureFormat::RG8Uint;
89             case spv::ImageFormatRg8i:
90                 return wgpu::TextureFormat::RG8Sint;
91             case spv::ImageFormatR32f:
92                 return wgpu::TextureFormat::R32Float;
93             case spv::ImageFormatR32ui:
94                 return wgpu::TextureFormat::R32Uint;
95             case spv::ImageFormatR32i:
96                 return wgpu::TextureFormat::R32Sint;
97             case spv::ImageFormatRg16ui:
98                 return wgpu::TextureFormat::RG16Uint;
99             case spv::ImageFormatRg16i:
100                 return wgpu::TextureFormat::RG16Sint;
101             case spv::ImageFormatRg16f:
102                 return wgpu::TextureFormat::RG16Float;
103             case spv::ImageFormatRgba8:
104                 return wgpu::TextureFormat::RGBA8Unorm;
105             case spv::ImageFormatRgba8Snorm:
106                 return wgpu::TextureFormat::RGBA8Snorm;
107             case spv::ImageFormatRgba8ui:
108                 return wgpu::TextureFormat::RGBA8Uint;
109             case spv::ImageFormatRgba8i:
110                 return wgpu::TextureFormat::RGBA8Sint;
111             case spv::ImageFormatRgb10A2:
112                 return wgpu::TextureFormat::RGB10A2Unorm;
113             case spv::ImageFormatR11fG11fB10f:
114                 return wgpu::TextureFormat::RG11B10Ufloat;
115             case spv::ImageFormatRg32f:
116                 return wgpu::TextureFormat::RG32Float;
117             case spv::ImageFormatRg32ui:
118                 return wgpu::TextureFormat::RG32Uint;
119             case spv::ImageFormatRg32i:
120                 return wgpu::TextureFormat::RG32Sint;
121             case spv::ImageFormatRgba16ui:
122                 return wgpu::TextureFormat::RGBA16Uint;
123             case spv::ImageFormatRgba16i:
124                 return wgpu::TextureFormat::RGBA16Sint;
125             case spv::ImageFormatRgba16f:
126                 return wgpu::TextureFormat::RGBA16Float;
127             case spv::ImageFormatRgba32f:
128                 return wgpu::TextureFormat::RGBA32Float;
129             case spv::ImageFormatRgba32ui:
130                 return wgpu::TextureFormat::RGBA32Uint;
131             case spv::ImageFormatRgba32i:
132                 return wgpu::TextureFormat::RGBA32Sint;
133             default:
134                 return wgpu::TextureFormat::Undefined;
135         }
136     }
137 
SpirvBaseTypeToTextureComponentType(spirv_cross::SPIRType::BaseType spirvBaseType)138     wgpu::TextureComponentType SpirvBaseTypeToTextureComponentType(
139         spirv_cross::SPIRType::BaseType spirvBaseType) {
140         switch (spirvBaseType) {
141             case spirv_cross::SPIRType::Float:
142                 return wgpu::TextureComponentType::Float;
143             case spirv_cross::SPIRType::Int:
144                 return wgpu::TextureComponentType::Sint;
145             case spirv_cross::SPIRType::UInt:
146                 return wgpu::TextureComponentType::Uint;
147             default:
148                 UNREACHABLE();
149         }
150     }
151 
SpirvBaseTypeToSampleTypeBit(spirv_cross::SPIRType::BaseType spirvBaseType)152     SampleTypeBit SpirvBaseTypeToSampleTypeBit(spirv_cross::SPIRType::BaseType spirvBaseType) {
153         switch (spirvBaseType) {
154             case spirv_cross::SPIRType::Float:
155                 return SampleTypeBit::Float | SampleTypeBit::UnfilterableFloat;
156             case spirv_cross::SPIRType::Int:
157                 return SampleTypeBit::Sint;
158             case spirv_cross::SPIRType::UInt:
159                 return SampleTypeBit::Uint;
160             default:
161                 UNREACHABLE();
162         }
163     }
164 
SpirvBaseTypeToVertexFormatBaseType(spirv_cross::SPIRType::BaseType spirvBaseType)165     VertexFormatBaseType SpirvBaseTypeToVertexFormatBaseType(
166         spirv_cross::SPIRType::BaseType spirvBaseType) {
167         switch (spirvBaseType) {
168             case spirv_cross::SPIRType::Float:
169                 return VertexFormatBaseType::Float;
170             case spirv_cross::SPIRType::Int:
171                 return VertexFormatBaseType::Sint;
172             case spirv_cross::SPIRType::UInt:
173                 return VertexFormatBaseType::Uint;
174             default:
175                 UNREACHABLE();
176         }
177     }
178 
179 }  // namespace dawn_native
180