Path: blob/master/drivers/vulkan/rendering_shader_container_vulkan.cpp
9903 views
/**************************************************************************/1/* rendering_shader_container_vulkan.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "rendering_shader_container_vulkan.h"3132#include "thirdparty/misc/smolv.h"3334// RenderingShaderContainerVulkan3536const uint32_t RenderingShaderContainerVulkan::FORMAT_VERSION = 1;3738uint32_t RenderingShaderContainerVulkan::_format() const {39return 0x43565053;40}4142uint32_t RenderingShaderContainerVulkan::_format_version() const {43return FORMAT_VERSION;44}4546bool RenderingShaderContainerVulkan::_set_code_from_spirv(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv) {47PackedByteArray code_bytes;48shaders.resize(p_spirv.size());49for (int64_t i = 0; i < p_spirv.size(); i++) {50RenderingShaderContainer::Shader &shader = shaders.ptrw()[i];5152if (debug_info_enabled) {53// Store SPIR-V as is when debug info is required.54shader.code_compressed_bytes = p_spirv[i].spirv;55shader.code_compression_flags = 0;56shader.code_decompressed_size = 0;57} else {58// Encode into smolv.59smolv::ByteArray smolv_bytes;60bool smolv_encoded = smolv::Encode(p_spirv[i].spirv.ptr(), p_spirv[i].spirv.size(), smolv_bytes, smolv::kEncodeFlagStripDebugInfo);61ERR_FAIL_COND_V_MSG(!smolv_encoded, false, "Failed to compress SPIR-V into smolv.");6263code_bytes.resize(smolv_bytes.size());64memcpy(code_bytes.ptrw(), smolv_bytes.data(), code_bytes.size());6566// Compress.67uint32_t compressed_size = 0;68shader.code_decompressed_size = code_bytes.size();69shader.code_compressed_bytes.resize(code_bytes.size());7071bool compressed = compress_code(code_bytes.ptr(), code_bytes.size(), shader.code_compressed_bytes.ptrw(), &compressed_size, &shader.code_compression_flags);72ERR_FAIL_COND_V_MSG(!compressed, false, vformat("Failed to compress native code to native for SPIR-V #%d.", i));7374shader.code_compressed_bytes.resize(compressed_size);7576// Indicate it uses smolv for compression.77shader.code_compression_flags |= COMPRESSION_FLAG_SMOLV;78}7980shader.shader_stage = p_spirv[i].shader_stage;81}8283return true;84}8586RenderingShaderContainerVulkan::RenderingShaderContainerVulkan(bool p_debug_info_enabled) {87debug_info_enabled = p_debug_info_enabled;88}8990// RenderingShaderContainerFormatVulkan9192Ref<RenderingShaderContainer> RenderingShaderContainerFormatVulkan::create_container() const {93return memnew(RenderingShaderContainerVulkan(debug_info_enabled));94}9596RenderingDeviceCommons::ShaderLanguageVersion RenderingShaderContainerFormatVulkan::get_shader_language_version() const {97return SHADER_LANGUAGE_VULKAN_VERSION_1_1;98}99100RenderingDeviceCommons::ShaderSpirvVersion RenderingShaderContainerFormatVulkan::get_shader_spirv_version() const {101return SHADER_SPIRV_VERSION_1_3;102}103104void RenderingShaderContainerFormatVulkan::set_debug_info_enabled(bool p_debug_info_enabled) {105debug_info_enabled = p_debug_info_enabled;106}107108RenderingShaderContainerFormatVulkan::RenderingShaderContainerFormatVulkan() {}109110RenderingShaderContainerFormatVulkan::~RenderingShaderContainerFormatVulkan() {}111112113