diff --git a/rpcs3/Emu/RSX/D3D12/D3D12PipelineState.cpp b/rpcs3/Emu/RSX/D3D12/D3D12PipelineState.cpp index d54dc1af49..54601e7934 100644 --- a/rpcs3/Emu/RSX/D3D12/D3D12PipelineState.cpp +++ b/rpcs3/Emu/RSX/D3D12/D3D12PipelineState.cpp @@ -119,12 +119,12 @@ ID3D12PipelineState *PipelineStateObjectCache::getGraphicPipelineState( { LOG_WARNING(RSX, "VP not found in buffer!"); VertexDecompiler VS(vertexShader->data); - VS.Decompile(); + std::string shaderCode = VS.Decompile(); m_vertex_prog.Compile(SHADER_TYPE::SHADER_TYPE_VERTEX); AddVertexProgram(m_vertex_prog, *vertexShader); // TODO: This shouldn't use current dir - fs::file("./VertexProgram.txt", o_write | o_create | o_trunc).write(VS.m_shader.c_str(), VS.m_shader.size()); + fs::file("./VertexProgram.txt", o_write | o_create | o_trunc).write(shaderCode.c_str(), shaderCode.size()); } if (m_fp_buf_num && m_vp_buf_num) diff --git a/rpcs3/Emu/RSX/D3D12/VertexProgramDecompiler.cpp b/rpcs3/Emu/RSX/D3D12/VertexProgramDecompiler.cpp index dd0056cdb7..789434e473 100644 --- a/rpcs3/Emu/RSX/D3D12/VertexProgramDecompiler.cpp +++ b/rpcs3/Emu/RSX/D3D12/VertexProgramDecompiler.cpp @@ -449,11 +449,6 @@ std::string VertexDecompiler::BuildCode() } std::string p; - - for (auto& param : m_parr.params) { - p += param.Format(); - } - std::string fp; for (int i = m_funcs.size() - 1; i > 0; --i) @@ -499,15 +494,66 @@ std::string VertexDecompiler::BuildCode() f += fmt::Format("\nvoid %s()\n{\n%s}\n", m_funcs[i].name.c_str(), BuildFuncBody(m_funcs[i]).c_str()); } - static const std::string& prot = - "#version 420\n" - "\n" - "uniform mat4 scaleOffsetMat = mat4(1.0);\n" - "%s\n" - "%s\n" - "%s"; + std::stringstream OS; + insertHeader(OS); - return fmt::Format(prot.c_str(), p.c_str(), fp.c_str(), f.c_str()); + insertInputs(OS, m_parr.params[PARAM_IN]); + OS << std::endl; + insertOutputs(OS, m_parr.params[PARAM_OUT]); + OS << std::endl; + insertConstants(OS, m_parr.params[PARAM_UNIFORM]); + OS << std::endl; + + + OS << fp.c_str() << std::endl; + OS << f.c_str() << std::endl; + + return OS.str(); +} + +void VertexDecompiler::insertHeader(std::stringstream &OS) +{ + OS << "cbuffer SCALE_OFFSET : register(b0)" << std::endl; + OS << "{" << std::endl; + OS << " float4x4 scaleOffsetMat;" << std::endl; + OS << "};" << std::endl; +} + +void VertexDecompiler::insertInputs(std::stringstream & OS, const std::vector& inputs) +{ + OS << "struct VertexInput" << std::endl; + OS << "{" << std::endl; + for (const ParamType PT : inputs) + { + for (const ParamItem &PI : PT.items) + OS << " " << PT.type << " " << PI.name << ": TEXCOORD" << PI.location << ";" << std::endl; + } + OS << "};" << std::endl; +} + +void VertexDecompiler::insertConstants(std::stringstream & OS, const std::vector & constants) +{ + OS << "cbuffer CONSTANT_BUFFER" << std::endl; + OS << "{" << std::endl; + for (const ParamType PT : constants) + { + for (const ParamItem &PI : PT.items) + OS << " " << PT.type << " " << PI.name << ";" << std::endl; + } + OS << "};" << std::endl; +} + +void VertexDecompiler::insertOutputs(std::stringstream & OS, const std::vector & outputs) +{ + OS << "struct PixelInput" << std::endl; + OS << "{" << std::endl; + OS << " float4 position : SV_POSITION;" << std::endl; + for (const ParamType PT : outputs) + { + for (const ParamItem &PI : PT.items) + OS << " " << PT.type << " " << PI.name << ": TEXCOORD" << PI.location << ";" << std::endl; + } + OS << "};" << std::endl; } VertexDecompiler::VertexDecompiler(std::vector& data) : @@ -522,9 +568,10 @@ VertexDecompiler::VertexDecompiler(std::vector& data) : //m_cur_func->body = "\tgl_Position = vec4(0.0f, 0.0f, 0.0f, 1.0f);\n"; } -void VertexDecompiler::Decompile() +std::string VertexDecompiler::Decompile() { - m_parr.params.clear(); + for (unsigned i = 0; i < PARAM_COUNT; i++) + m_parr.params[i].clear(); m_instr_count = 0; for (int i = 0; i < m_max_instr_count; ++i) @@ -769,7 +816,7 @@ void VertexDecompiler::Decompile() AddCode("}"); } - m_shader = BuildCode(); + std::string result = BuildCode(); m_jump_lvls.clear(); m_body.clear(); @@ -777,6 +824,7 @@ void VertexDecompiler::Decompile() { m_funcs.erase(m_funcs.begin() + 2, m_funcs.end()); } + return result; } #endif \ No newline at end of file diff --git a/rpcs3/Emu/RSX/D3D12/VertexProgramDecompiler.h b/rpcs3/Emu/RSX/D3D12/VertexProgramDecompiler.h index 2b9c9a02a9..c0e020113a 100644 --- a/rpcs3/Emu/RSX/D3D12/VertexProgramDecompiler.h +++ b/rpcs3/Emu/RSX/D3D12/VertexProgramDecompiler.h @@ -1,8 +1,8 @@ #pragma once #if defined(DX12_SUPPORT) #include "Emu/RSX/RSXVertexProgram.h" -#include #include +#include enum ParamFlag { @@ -11,30 +11,27 @@ enum ParamFlag PARAM_UNIFORM, PARAM_CONST, PARAM_NONE, + PARAM_COUNT, }; -struct GLParamItem +struct ParamItem { std::string name; - std::string location; std::string value; + int location; - GLParamItem(const std::string& _name, int _location, const std::string& _value = "") + ParamItem(const std::string& _name, int _location, const std::string& _value = "") : name(_name) - , value(_value) - { - if (_location > -1) - location = "layout (location = " + std::to_string(_location) + ") "; - else - location = ""; - } + , value(_value), + location(_location) + { } }; struct ParamType { const ParamFlag flag; std::string type; - std::vector items; + std::vector items; ParamType(const ParamFlag _flag, const std::string& _type) : flag(_flag) @@ -51,63 +48,32 @@ struct ParamType return false; } - - std::string Format() - { - std::string ret = ""; - - for (u32 n = 0; n params; + std::vector params[PARAM_COUNT]; - ParamType* SearchParam(const std::string& type) + ParamType* SearchParam(const ParamFlag &flag, const std::string& type) { - for (u32 i = 0; iSearchName(name); } std::string AddParam(const ParamFlag flag, std::string type, const std::string& name, const std::string& value) { - type = GetParamFlag(flag) + type; - ParamType* t = SearchParam(type); + ParamType* t = SearchParam(flag, type); if (t) { @@ -115,9 +81,9 @@ struct ParamArray } else { - const u32 num = params.size(); - params.emplace_back(flag, type); - params[num].items.emplace_back(name, -1, value); + const u32 num = params[flag].size(); + params[flag].emplace_back(flag, type); + params[flag][num].items.emplace_back(name, -1, value); } return name; @@ -125,8 +91,7 @@ struct ParamArray std::string AddParam(const ParamFlag flag, std::string type, const std::string& name, int location = -1) { - type = GetParamFlag(flag) + type; - ParamType* t = SearchParam(type); + ParamType* t = SearchParam(flag, type); if (t) { @@ -134,9 +99,9 @@ struct ParamArray } else { - const u32 num = params.size(); - params.emplace_back(flag, type); - params[num].items.emplace_back(name, location); + const u32 num = params[flag].size(); + params[flag].emplace_back(flag, type); + params[flag][num].items.emplace_back(name, location); } return name; @@ -288,9 +253,13 @@ struct VertexDecompiler std::string BuildFuncBody(const FuncInfo& func); std::string BuildCode(); +protected: + virtual void insertHeader(std::stringstream &OS); + virtual void insertInputs(std::stringstream &OS, const std::vector &inputs); + virtual void insertConstants(std::stringstream &OS, const std::vector &constants); + virtual void insertOutputs(std::stringstream &OS, const std::vector &outputs); public: - std::string m_shader; VertexDecompiler(std::vector& data); - void Decompile(); + std::string Decompile(); }; #endif \ No newline at end of file