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_SPIRV_BINARY_WRITER_H_ 16 #define SRC_WRITER_SPIRV_BINARY_WRITER_H_ 17 18 #include <vector> 19 20 #include "src/writer/spirv/builder.h" 21 22 namespace tint { 23 namespace writer { 24 namespace spirv { 25 26 /// Writer to convert from builder to SPIR-V binary 27 class BinaryWriter { 28 public: 29 /// Constructor 30 BinaryWriter(); 31 ~BinaryWriter(); 32 33 /// Writes the SPIR-V header. 34 /// @param bound the bound to output 35 void WriteHeader(uint32_t bound); 36 37 /// Writes the given builder data into a binary. Note, this does not emit 38 /// the SPIR-V header. You **must** call WriteHeader() before WriteBuilder() 39 /// if you want the SPIR-V to be emitted. 40 /// @param builder the builder to assemble from 41 void WriteBuilder(Builder* builder); 42 43 /// Writes the given instruction into the binary. 44 /// @param inst the instruction to assemble 45 void WriteInstruction(const Instruction& inst); 46 47 /// @returns the assembled SPIR-V result()48 const std::vector<uint32_t>& result() const { return out_; } 49 50 private: 51 void process_instruction(const Instruction& inst); 52 void process_op(const Operand& op); 53 54 std::vector<uint32_t> out_; 55 }; 56 57 } // namespace spirv 58 } // namespace writer 59 } // namespace tint 60 61 #endif // SRC_WRITER_SPIRV_BINARY_WRITER_H_ 62