1 // Copyright 2015 The Shaderc Authors. All rights reserved. 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 LIBSHADERC_SRC_SHADERC_PRIVATE_H_ 16 #define LIBSHADERC_SRC_SHADERC_PRIVATE_H_ 17 18 #include <cassert> 19 #include <cstdint> 20 #include <string> 21 #include <vector> 22 23 #include "shaderc/shaderc.h" 24 25 #include "libshaderc_util/compiler.h" 26 #include "spirv-tools/libspirv.h" 27 28 // Described in shaderc.h. 29 struct shaderc_compilation_result { 30 virtual ~shaderc_compilation_result() {} 31 32 // Returns the data from this compilation as a sequence of bytes. 33 virtual const char* GetBytes() const = 0; 34 35 // The size of the output data in term of bytes. 36 size_t output_data_size = 0; 37 // Compilation messages. 38 std::string messages; 39 // Number of errors. 40 size_t num_errors = 0; 41 // Number of warnings. 42 size_t num_warnings = 0; 43 // Compilation status. 44 shaderc_compilation_status compilation_status = 45 shaderc_compilation_status_null_result_object; 46 }; 47 48 // Compilation result class using a vector for holding the compilation 49 // output data. 50 class shaderc_compilation_result_vector : public shaderc_compilation_result { 51 public: 52 ~shaderc_compilation_result_vector() = default; 53 54 void SetOutputData(std::vector<uint32_t>&& data) { 55 output_data_ = std::move(data); 56 } 57 58 const char* GetBytes() const override { 59 return reinterpret_cast<const char*>(output_data_.data()); 60 } 61 62 private: 63 // Compilation output data. In normal compilation mode, it contains the 64 // compiled SPIR-V binary code. In disassembly and preprocessing-only mode, it 65 // contains a null-terminated string which is the text output. For text 66 // output, extra bytes with value 0x00 might be appended to complete the last 67 // uint32_t element. 68 std::vector<uint32_t> output_data_; 69 }; 70 71 // Compilation result class using a spv_binary for holding the compilation 72 // output data. 73 class shaderc_compilation_result_spv_binary 74 : public shaderc_compilation_result { 75 public: 76 ~shaderc_compilation_result_spv_binary() { spvBinaryDestroy(output_data_); } 77 78 void SetOutputData(spv_binary data) { output_data_ = data; } 79 80 const char* GetBytes() const override { 81 return reinterpret_cast<const char*>(output_data_->code); 82 } 83 84 private: 85 spv_binary output_data_ = nullptr; 86 }; 87 88 namespace shaderc_util { 89 class GlslangInitializer; 90 } 91 92 struct shaderc_compiler { 93 shaderc_util::GlslangInitializer* initializer; 94 }; 95 96 // Converts a shader stage from shaderc_shader_kind into a shaderc_util::Compiler::Stage. 97 // This is only valid for a specifically named shader stage, e.g. vertex through fragment, 98 // or compute. 99 inline shaderc_util::Compiler::Stage shaderc_convert_specific_stage( 100 shaderc_shader_kind kind) { 101 switch (kind) { 102 case shaderc_vertex_shader: 103 return shaderc_util::Compiler::Stage::Vertex; 104 case shaderc_fragment_shader: 105 return shaderc_util::Compiler::Stage::Fragment; 106 case shaderc_tess_control_shader: 107 return shaderc_util::Compiler::Stage::TessControl; 108 case shaderc_tess_evaluation_shader: 109 return shaderc_util::Compiler::Stage::TessEval; 110 case shaderc_geometry_shader: 111 return shaderc_util::Compiler::Stage::Geometry; 112 case shaderc_compute_shader: 113 return shaderc_util::Compiler::Stage::Compute; 114 default: 115 // We don't care about the other kinds. 116 break; 117 } 118 // This should not occur. 119 assert(false && "Should have specified a specific stage"); 120 return shaderc_util::Compiler::Stage::TessEval; 121 } 122 123 #endif // LIBSHADERC_SRC_SHADERC_PRIVATE_H_ 124