• 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 #include "src/writer/spirv/generator.h"
16 
17 #include "src/writer/spirv/binary_writer.h"
18 
19 namespace tint {
20 namespace writer {
21 namespace spirv {
22 
23 Result::Result() = default;
24 Result::~Result() = default;
25 Result::Result(const Result&) = default;
26 
Generate(const Program * program,const Options & options)27 Result Generate(const Program* program, const Options& options) {
28   Result result;
29 
30   // Sanitize the program.
31   auto sanitized_result = Sanitize(program, options.emit_vertex_point_size,
32                                    options.disable_workgroup_init);
33   if (!sanitized_result.program.IsValid()) {
34     result.success = false;
35     result.error = sanitized_result.program.Diagnostics().str();
36     return result;
37   }
38 
39   // Generate the SPIR-V code.
40   auto builder = std::make_unique<Builder>(&sanitized_result.program);
41   auto writer = std::make_unique<BinaryWriter>();
42   if (!builder->Build()) {
43     result.success = false;
44     result.error = builder->error();
45     return result;
46   }
47 
48   writer->WriteHeader(builder->id_bound());
49   writer->WriteBuilder(builder.get());
50 
51   result.success = true;
52   result.spirv = writer->result();
53 
54   return result;
55 }
56 
57 }  // namespace spirv
58 }  // namespace writer
59 }  // namespace tint
60