• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_WRITER_MSL_GENERATOR_H_
16 #define SRC_WRITER_MSL_GENERATOR_H_
17 
18 #include <memory>
19 #include <string>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include <vector>
23 
24 #include "src/writer/array_length_from_uniform_options.h"
25 #include "src/writer/text.h"
26 
27 namespace tint {
28 
29 // Forward declarations
30 class Program;
31 
32 namespace writer {
33 namespace msl {
34 
35 class GeneratorImpl;
36 
37 /// Configuration options used for generating MSL.
38 struct Options {
39   /// Constructor
40   Options();
41   /// Destructor
42   ~Options();
43   /// Copy constructor
44   Options(const Options&);
45   /// Copy assignment
46   /// @returns this Options
47   Options& operator=(const Options&);
48 
49   /// The index to use when generating a UBO to receive storage buffer sizes.
50   /// Defaults to 30, which is the last valid buffer slot.
51   uint32_t buffer_size_ubo_index = 30;
52 
53   /// The fixed sample mask to combine with fragment shader outputs.
54   /// Defaults to 0xFFFFFFFF.
55   uint32_t fixed_sample_mask = 0xFFFFFFFF;
56 
57   /// Set to `true` to generate a [[point_size]] attribute which is set to 1.0
58   /// for all vertex shaders in the module.
59   bool emit_vertex_point_size = false;
60 
61   /// Set to `true` to disable workgroup memory zero initialization
62   bool disable_workgroup_init = false;
63 
64   /// Options used to specify a mapping of binding points to indices into a UBO
65   /// from which to load buffer sizes.
66   ArrayLengthFromUniformOptions array_length_from_uniform = {};
67 
68   // NOTE: Update fuzzers/data_builder.h when adding or changing any struct
69   // members.
70 };
71 
72 /// The result produced when generating MSL.
73 struct Result {
74   /// Constructor
75   Result();
76 
77   /// Destructor
78   ~Result();
79 
80   /// Copy constructor
81   Result(const Result&);
82 
83   /// True if generation was successful.
84   bool success = false;
85 
86   /// The errors generated during code generation, if any.
87   std::string error;
88 
89   /// The generated MSL.
90   std::string msl = "";
91 
92   /// True if the shader needs a UBO of buffer sizes.
93   bool needs_storage_buffer_sizes = false;
94 
95   /// True if the generated shader uses the invariant attribute.
96   bool has_invariant_attribute = false;
97 
98   /// A map from entry point name to a list of dynamic workgroup allocations.
99   /// Each entry in the vector is the size of the workgroup allocation that
100   /// should be created for that index.
101   std::unordered_map<std::string, std::vector<uint32_t>> workgroup_allocations;
102 
103   /// Indices into the array_length_from_uniform binding that are statically
104   /// used.
105   std::unordered_set<uint32_t> used_array_length_from_uniform_indices;
106 };
107 
108 /// Generate MSL for a program, according to a set of configuration options. The
109 /// result will contain the MSL, as well as success status and diagnostic
110 /// information.
111 /// @param program the program to translate to MSL
112 /// @param options the configuration options to use when generating MSL
113 /// @returns the resulting MSL and supplementary information
114 Result Generate(const Program* program, const Options& options);
115 
116 }  // namespace msl
117 }  // namespace writer
118 }  // namespace tint
119 
120 #endif  // SRC_WRITER_MSL_GENERATOR_H_
121