Skip to content

Commit

Permalink
PostProcessing/FX: Use SPIR-V instead of GLSL for Vulkan
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Sep 8, 2024
1 parent 86bff86 commit 432fd80
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
26 changes: 23 additions & 3 deletions dep/reshadefx/src/effect_codegen_spirv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,25 @@ class codegen_spirv final : public codegen
return {};
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
std::vector<spv::Id> variables_to_remove;
#if 1
Expand All @@ -414,7 +433,7 @@ class codegen_spirv final : public codegen
// Only add the matching entry point
if (inst.operands[1] == entry_point.id)
{
inst.write(spirv);
write_entry_point(inst, spirv);
}
else
{
Expand Down Expand Up @@ -482,8 +501,9 @@ class codegen_spirv final : public codegen
if (func.definition.instructions.empty())
continue;

assert(func.declaration.instructions[_debug_info ? 1 : 0].op == spv::OpFunction);
const spv::Id definition = func.declaration.instructions[_debug_info ? 1 : 0].result;
const bool has_line = (_debug_info && func.declaration.instructions[0].op == spv::OpLine);
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 (std::find(functions_to_remove.begin(), functions_to_remove.end(), definition) != functions_to_remove.end())
Expand Down
25 changes: 9 additions & 16 deletions src/util/postprocessing_shader_fx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ static std::unique_ptr<reshadefx::codegen> CreateRFXCodegen()
case RenderAPI::Vulkan:
case RenderAPI::Metal:
{
return std::unique_ptr<reshadefx::codegen>(reshadefx::create_codegen_glsl(
460, false, true, debug_info, uniforms_to_spec_constants, false, (rapi == RenderAPI::Vulkan)));
return std::unique_ptr<reshadefx::codegen>(reshadefx::create_codegen_spirv(
true, debug_info, uniforms_to_spec_constants, false, (rapi == RenderAPI::Vulkan)));
}

case RenderAPI::OpenGL:
Expand Down Expand Up @@ -1303,9 +1303,6 @@ GPUTexture* PostProcessing::ReShadeFXShader::GetTextureByID(TextureID id, GPUTex
bool PostProcessing::ReShadeFXShader::CompilePipeline(GPUTexture::Format format, u32 width, u32 height,
ProgressCallback* progress)
{
const RenderAPI api = g_gpu_device->GetRenderAPI();
const bool needs_main_defn = (api != RenderAPI::D3D11 && api != RenderAPI::D3D12);

m_valid = false;
m_textures.clear();
m_passes.clear();
Expand Down Expand Up @@ -1342,20 +1339,16 @@ bool PostProcessing::ReShadeFXShader::CompilePipeline(GPUTexture::Format format,
return false;
}

// TODO: If using spv, this will be populated.
// const std::string effect_code = cg->finalize_code();

auto get_shader = [api, needs_main_defn, &cg](const std::string& name, const std::span<Sampler> samplers,
GPUShaderStage stage) {
const RenderAPI api = g_gpu_device->GetRenderAPI();
auto get_shader = [api, &cg](const std::string& name, const std::span<Sampler> samplers, GPUShaderStage stage) {
const std::string real_code = cg->finalize_code_for_entry_point(name);

#if 0
FileSystem::WriteStringToFile(fmt::format("D:\\reshade_{}.txt", Path::SanitizeFileName(name)).c_str(), real_code);
#endif
const GPUShaderLanguage lang = (api == RenderAPI::Vulkan || api == RenderAPI::Metal) ?
GPUShaderLanguage::SPV :
ShaderGen::GetShaderLanguageForAPI(api);
const char* entry_point = (lang == GPUShaderLanguage::HLSL) ? name.c_str() : "main";

Error error;
std::unique_ptr<GPUShader> sshader = g_gpu_device->CreateShader(
stage, ShaderGen::GetShaderLanguageForAPI(api), real_code, &error, needs_main_defn ? "main" : name.c_str());
std::unique_ptr<GPUShader> sshader = g_gpu_device->CreateShader(stage, lang, real_code, &error, entry_point);
if (!sshader)
ERROR_LOG("Failed to compile function '{}': {}", name, error.GetDescription());

Expand Down

0 comments on commit 432fd80

Please sign in to comment.