Path: blob/main/libshaderc/src/shaderc_private.h
1558 views
// Copyright 2015 The Shaderc Authors. All rights reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#ifndef LIBSHADERC_SRC_SHADERC_PRIVATE_H_15#define LIBSHADERC_SRC_SHADERC_PRIVATE_H_1617#include <cassert>18#include <cstdint>19#include <string>20#include <vector>2122#include "shaderc/shaderc.h"2324#include "libshaderc_util/compiler.h"25#include "spirv-tools/libspirv.h"2627// Described in shaderc.h.28struct shaderc_compilation_result {29virtual ~shaderc_compilation_result() {}3031// Returns the data from this compilation as a sequence of bytes.32virtual const char* GetBytes() const = 0;3334// The size of the output data in term of bytes.35size_t output_data_size = 0;36// Compilation messages.37std::string messages;38// Number of errors.39size_t num_errors = 0;40// Number of warnings.41size_t num_warnings = 0;42// Compilation status.43shaderc_compilation_status compilation_status =44shaderc_compilation_status_null_result_object;45};4647// Compilation result class using a vector for holding the compilation48// output data.49class shaderc_compilation_result_vector : public shaderc_compilation_result {50public:51~shaderc_compilation_result_vector() = default;5253void SetOutputData(std::vector<uint32_t>&& data) {54output_data_ = std::move(data);55}5657const char* GetBytes() const override {58return reinterpret_cast<const char*>(output_data_.data());59}6061private:62// Compilation output data. In normal compilation mode, it contains the63// compiled SPIR-V binary code. In disassembly and preprocessing-only mode, it64// contains a null-terminated string which is the text output. For text65// output, extra bytes with value 0x00 might be appended to complete the last66// uint32_t element.67std::vector<uint32_t> output_data_;68};6970// Compilation result class using a spv_binary for holding the compilation71// output data.72class shaderc_compilation_result_spv_binary73: public shaderc_compilation_result {74public:75~shaderc_compilation_result_spv_binary() { spvBinaryDestroy(output_data_); }7677void SetOutputData(spv_binary data) { output_data_ = data; }7879const char* GetBytes() const override {80return reinterpret_cast<const char*>(output_data_->code);81}8283private:84spv_binary output_data_ = nullptr;85};8687namespace shaderc_util {88class GlslangInitializer;89}9091struct shaderc_compiler {92std::unique_ptr<shaderc_util::GlslangInitializer> initializer;93};9495// Converts a shader stage from shaderc_shader_kind into a shaderc_util::Compiler::Stage.96// This is only valid for a specifically named shader stage, e.g. vertex through fragment,97// or compute.98inline shaderc_util::Compiler::Stage shaderc_convert_specific_stage(99shaderc_shader_kind kind) {100switch (kind) {101case shaderc_vertex_shader:102return shaderc_util::Compiler::Stage::Vertex;103case shaderc_fragment_shader:104return shaderc_util::Compiler::Stage::Fragment;105case shaderc_tess_control_shader:106return shaderc_util::Compiler::Stage::TessControl;107case shaderc_tess_evaluation_shader:108return shaderc_util::Compiler::Stage::TessEval;109case shaderc_geometry_shader:110return shaderc_util::Compiler::Stage::Geometry;111case shaderc_compute_shader:112return shaderc_util::Compiler::Stage::Compute;113case shaderc_raygen_shader:114return shaderc_util::Compiler::Stage::RayGenNV;115case shaderc_intersection_shader:116return shaderc_util::Compiler::Stage::IntersectNV;117case shaderc_anyhit_shader:118return shaderc_util::Compiler::Stage::AnyHitNV;119case shaderc_closesthit_shader:120return shaderc_util::Compiler::Stage::ClosestHitNV;121case shaderc_miss_shader:122return shaderc_util::Compiler::Stage::MissNV;123case shaderc_callable_shader:124return shaderc_util::Compiler::Stage::CallableNV;125case shaderc_task_shader:126return shaderc_util::Compiler::Stage::TaskNV;127case shaderc_mesh_shader:128return shaderc_util::Compiler::Stage::MeshNV;129default:130// We don't care about the other kinds.131break;132}133// This should not occur.134assert(false && "Should have specified a specific stage");135return shaderc_util::Compiler::Stage::TessEval;136}137138#endif // LIBSHADERC_SRC_SHADERC_PRIVATE_H_139140141