/src/shaderc/libshaderc/src/shaderc_private.h
Line | Count | Source |
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 | 1.41k | 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 | 1.41k | ~shaderc_compilation_result_vector() = default; |
53 | | |
54 | 1.41k | void SetOutputData(std::vector<uint32_t>&& data) { |
55 | 1.41k | output_data_ = std::move(data); |
56 | 1.41k | } |
57 | | |
58 | 677 | const char* GetBytes() const override { |
59 | 677 | return reinterpret_cast<const char*>(output_data_.data()); |
60 | 677 | } |
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 | 0 | ~shaderc_compilation_result_spv_binary() { spvBinaryDestroy(output_data_); } |
77 | | |
78 | 0 | void SetOutputData(spv_binary data) { output_data_ = data; } |
79 | | |
80 | 0 | const char* GetBytes() const override { |
81 | 0 | return reinterpret_cast<const char*>(output_data_->code); |
82 | 0 | } |
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 | | std::unique_ptr<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 | 0 | shaderc_shader_kind kind) { |
101 | 0 | switch (kind) { |
102 | 0 | case shaderc_vertex_shader: |
103 | 0 | return shaderc_util::Compiler::Stage::Vertex; |
104 | 0 | case shaderc_fragment_shader: |
105 | 0 | return shaderc_util::Compiler::Stage::Fragment; |
106 | 0 | case shaderc_tess_control_shader: |
107 | 0 | return shaderc_util::Compiler::Stage::TessControl; |
108 | 0 | case shaderc_tess_evaluation_shader: |
109 | 0 | return shaderc_util::Compiler::Stage::TessEval; |
110 | 0 | case shaderc_geometry_shader: |
111 | 0 | return shaderc_util::Compiler::Stage::Geometry; |
112 | 0 | case shaderc_compute_shader: |
113 | 0 | return shaderc_util::Compiler::Stage::Compute; |
114 | 0 | case shaderc_raygen_shader: |
115 | 0 | return shaderc_util::Compiler::Stage::RayGenNV; |
116 | 0 | case shaderc_intersection_shader: |
117 | 0 | return shaderc_util::Compiler::Stage::IntersectNV; |
118 | 0 | case shaderc_anyhit_shader: |
119 | 0 | return shaderc_util::Compiler::Stage::AnyHitNV; |
120 | 0 | case shaderc_closesthit_shader: |
121 | 0 | return shaderc_util::Compiler::Stage::ClosestHitNV; |
122 | 0 | case shaderc_miss_shader: |
123 | 0 | return shaderc_util::Compiler::Stage::MissNV; |
124 | 0 | case shaderc_callable_shader: |
125 | 0 | return shaderc_util::Compiler::Stage::CallableNV; |
126 | 0 | case shaderc_task_shader: |
127 | 0 | return shaderc_util::Compiler::Stage::TaskNV; |
128 | 0 | case shaderc_mesh_shader: |
129 | 0 | return shaderc_util::Compiler::Stage::MeshNV; |
130 | 0 | default: |
131 | 0 | // We don't care about the other kinds. |
132 | 0 | break; |
133 | 0 | } |
134 | 0 | // This should not occur. |
135 | 0 | assert(false && "Should have specified a specific stage"); |
136 | 0 | return shaderc_util::Compiler::Stage::TessEval; |
137 | 0 | } |
138 | | |
139 | | #endif // LIBSHADERC_SRC_SHADERC_PRIVATE_H_ |