/* * WiVRn VR streaming * Copyright (C) 2022 Guillaume Meunier * Copyright (C) 2022 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "shader.h" #include "wivrn_shaders.h" #include #include #include #include template std::vector enumerate(F && f, SpvReflectShaderModule & reflected_shader_module) { uint32_t count; [[maybe_unused]] SpvReflectResult result = f(&reflected_shader_module, &count, nullptr); assert(result == SPV_REFLECT_RESULT_SUCCESS); std::vector data(count); result = f(&reflected_shader_module, &count, data.data()); assert(result == SPV_REFLECT_RESULT_SUCCESS); return data; } shader_loader::shader_loader(vk::raii::Device & device) : device(device) {} std::shared_ptr shader_loader::operator()(std::span spirv) { vk::ShaderModuleCreateInfo create_info{ .codeSize = spirv.size_bytes(), .pCode = spirv.data(), }; std::vector inputs; std::vector specialization_constants; SpvReflectShaderModule reflect; if (auto result = spvReflectCreateShaderModule2(SPV_REFLECT_MODULE_FLAG_NONE, spirv.size_bytes(), spirv.data(), &reflect); result == SPV_REFLECT_RESULT_SUCCESS) { for (SpvReflectInterfaceVariable * variable: enumerate(spvReflectEnumerateInputVariables, reflect)) { int array_size = 1; for (int i = 0; i < variable->array.dims_count; i++) array_size *= variable->array.dims[i]; if (variable->built_in == -1) inputs.emplace_back( variable->location, variable->name, (vk::Format)variable->format, array_size); } for (SpvReflectSpecializationConstant * variable: enumerate(spvReflectEnumerateSpecializationConstants, reflect)) specialization_constants.emplace_back(variable->constant_id, variable->name); spvReflectDestroyShaderModule(&reflect); } else spdlog::error("Cannot reflect shader: {}", magic_enum::enum_name(result)); return std::make_shared(vk::raii::ShaderModule{device, create_info}, inputs, specialization_constants); } std::shared_ptr shader_loader::operator()(const std::string & name) { std::span spirv; try { spirv = shaders.at(name); } catch (std::exception & e) { spdlog::error("Cannot load shader {}: {}", name, e.what()); throw; } return (*this)(shaders.at(name)); }