PostProcessing/FX: Use SPIR-V instead of GLSL for Vulkan

This commit is contained in:
Stenzek 2024-09-08 19:43:18 +10:00
parent c63c4f58ef
commit 2c90680bc2
No known key found for this signature in database
2 changed files with 32 additions and 19 deletions

View File

@ -390,6 +390,25 @@ private:
return {}; return {};
const function &entry_point = *entry_point_it->get(); const function &entry_point = *entry_point_it->get();
const auto write_entry_point = [this](const spirv_instruction& oins, std::basic_string<char>& spirv) {
assert(oins.operands.size() > 2);
spirv_instruction nins(oins.op, oins.type, oins.result);
nins.add(oins.operands[0]);
nins.add(oins.operands[1]);
nins.add_string("main");
size_t param_start_index = 2;
while (param_start_index < oins.operands.size() && (oins.operands[param_start_index] & 0xFF000000) != 0)
param_start_index++;
// skip zero
param_start_index++;
for (size_t i = param_start_index; i < oins.operands.size(); i++)
nins.add(oins.operands[i]);
nins.write(spirv);
};
// Build list of IDs to remove // Build list of IDs to remove
std::vector<spv::Id> variables_to_remove; std::vector<spv::Id> variables_to_remove;
#if 1 #if 1
@ -414,7 +433,7 @@ private:
// Only add the matching entry point // Only add the matching entry point
if (inst.operands[1] == entry_point.id) if (inst.operands[1] == entry_point.id)
{ {
inst.write(spirv); write_entry_point(inst, spirv);
} }
else else
{ {
@ -482,8 +501,9 @@ private:
if (func.definition.instructions.empty()) if (func.definition.instructions.empty())
continue; continue;
assert(func.declaration.instructions[_debug_info ? 1 : 0].op == spv::OpFunction); const bool has_line = (_debug_info && func.declaration.instructions[0].op == spv::OpLine);
const spv::Id definition = func.declaration.instructions[_debug_info ? 1 : 0].result; assert(func.declaration.instructions[has_line ? 1 : 0].op == spv::OpFunction);
const spv::Id definition = func.declaration.instructions[has_line ? 1 : 0].result;
#if 1 #if 1
if (std::find(functions_to_remove.begin(), functions_to_remove.end(), definition) != functions_to_remove.end()) if (std::find(functions_to_remove.begin(), functions_to_remove.end(), definition) != functions_to_remove.end())

View File

@ -82,8 +82,8 @@ static std::unique_ptr<reshadefx::codegen> CreateRFXCodegen()
case RenderAPI::Vulkan: case RenderAPI::Vulkan:
case RenderAPI::Metal: case RenderAPI::Metal:
{ {
return std::unique_ptr<reshadefx::codegen>(reshadefx::create_codegen_glsl( return std::unique_ptr<reshadefx::codegen>(reshadefx::create_codegen_spirv(
460, false, true, debug_info, uniforms_to_spec_constants, false, (rapi == RenderAPI::Vulkan))); true, debug_info, uniforms_to_spec_constants, false, (rapi == RenderAPI::Vulkan)));
} }
case RenderAPI::OpenGL: case RenderAPI::OpenGL:
@ -1303,9 +1303,6 @@ GPUTexture* PostProcessing::ReShadeFXShader::GetTextureByID(TextureID id, GPUTex
bool PostProcessing::ReShadeFXShader::CompilePipeline(GPUTexture::Format format, u32 width, u32 height, bool PostProcessing::ReShadeFXShader::CompilePipeline(GPUTexture::Format format, u32 width, u32 height,
ProgressCallback* progress) ProgressCallback* progress)
{ {
const RenderAPI api = g_gpu_device->GetRenderAPI();
const bool needs_main_defn = (api != RenderAPI::D3D11 && api != RenderAPI::D3D12);
m_valid = false; m_valid = false;
m_textures.clear(); m_textures.clear();
m_passes.clear(); m_passes.clear();
@ -1342,20 +1339,16 @@ bool PostProcessing::ReShadeFXShader::CompilePipeline(GPUTexture::Format format,
return false; return false;
} }
// TODO: If using spv, this will be populated. const RenderAPI api = g_gpu_device->GetRenderAPI();
// const std::string effect_code = cg->finalize_code(); auto get_shader = [api, &cg](const std::string& name, const std::span<Sampler> samplers, GPUShaderStage stage) {
auto get_shader = [api, needs_main_defn, &cg](const std::string& name, const std::span<Sampler> samplers,
GPUShaderStage stage) {
const std::string real_code = cg->finalize_code_for_entry_point(name); const std::string real_code = cg->finalize_code_for_entry_point(name);
const GPUShaderLanguage lang = (api == RenderAPI::Vulkan || api == RenderAPI::Metal) ?
#if 0 GPUShaderLanguage::SPV :
FileSystem::WriteStringToFile(fmt::format("D:\\reshade_{}.txt", Path::SanitizeFileName(name)).c_str(), real_code); ShaderGen::GetShaderLanguageForAPI(api);
#endif const char* entry_point = (lang == GPUShaderLanguage::HLSL) ? name.c_str() : "main";
Error error; Error error;
std::unique_ptr<GPUShader> sshader = g_gpu_device->CreateShader( std::unique_ptr<GPUShader> sshader = g_gpu_device->CreateShader(stage, lang, real_code, &error, entry_point);
stage, ShaderGen::GetShaderLanguageForAPI(api), real_code, &error, needs_main_defn ? "main" : name.c_str());
if (!sshader) if (!sshader)
ERROR_LOG("Failed to compile function '{}': {}", name, error.GetDescription()); ERROR_LOG("Failed to compile function '{}': {}", name, error.GetDescription());