1 // Copyright 2020 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 #ifndef SRC_READER_SPIRV_ENUM_CONVERTER_H_
16 #define SRC_READER_SPIRV_ENUM_CONVERTER_H_
17 
18 #include "spirv/unified1/spirv.h"
19 #include "src/ast/builtin.h"
20 #include "src/ast/pipeline_stage.h"
21 #include "src/ast/storage_class.h"
22 #include "src/reader/spirv/fail_stream.h"
23 #include "src/sem/storage_texture_type.h"
24 
25 namespace tint {
26 namespace reader {
27 namespace spirv {
28 
29 /// A converter from SPIR-V enums to Tint AST enums.
30 class EnumConverter {
31  public:
32   /// Creates a new enum converter.
33   /// @param fail_stream the error reporting stream.
34   explicit EnumConverter(const FailStream& fail_stream);
35   /// Destructor
36   ~EnumConverter();
37 
38   /// Converts a SPIR-V execution model to a Tint pipeline stage.
39   /// On failure, logs an error and returns kNone
40   /// @param model the SPIR-V entry point execution model
41   /// @returns a Tint AST pipeline stage
42   ast::PipelineStage ToPipelineStage(SpvExecutionModel model);
43 
44   /// Converts a SPIR-V storage class to a Tint storage class.
45   /// On failure, logs an error and returns kNone
46   /// @param sc the SPIR-V storage class
47   /// @returns a Tint AST storage class
48   ast::StorageClass ToStorageClass(const SpvStorageClass sc);
49 
50   /// Converts a SPIR-V Builtin value a Tint Builtin.
51   /// On failure, logs an error and returns kNone
52   /// @param b the SPIR-V builtin
53   /// @returns a Tint AST builtin
54   ast::Builtin ToBuiltin(SpvBuiltIn b);
55 
56   /// Converts a possibly arrayed SPIR-V Dim to a Tint texture dimension.
57   /// On failure, logs an error and returns kNone
58   /// @param dim the SPIR-V Dim value
59   /// @param arrayed true if the texture is arrayed
60   /// @returns a Tint AST texture dimension
61   ast::TextureDimension ToDim(SpvDim dim, bool arrayed);
62 
63   /// Converts a SPIR-V Image Format to a Tint ImageFormat
64   /// On failure, logs an error and returns kNone
65   /// @param fmt the SPIR-V format
66   /// @returns a Tint AST format
67   ast::ImageFormat ToImageFormat(SpvImageFormat fmt);
68 
69  private:
70   /// Registers a failure and returns a stream for log diagnostics.
71   /// @returns a failure stream
Fail()72   FailStream Fail() { return fail_stream_.Fail(); }
73 
74   FailStream fail_stream_;
75 };
76 
77 }  // namespace spirv
78 }  // namespace reader
79 }  // namespace tint
80 
81 #endif  // SRC_READER_SPIRV_ENUM_CONVERTER_H_
82