From f1e015f577b15ca1f1a93d38ed70997b133cb776 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Thu, 23 Feb 2017 11:26:16 +0100 Subject: [PATCH] Basic postshader translation to D3D11 works. On-the-fly switching seems broken though. --- GPU/Common/ShaderTranslation.cpp | 14 +++++++++- GPU/D3D11/D3D11Util.cpp | 27 ++++++++++++------ GPU/D3D11/D3D11Util.h | 7 +++-- GPU/D3D11/FramebufferManagerD3D11.cpp | 40 +++++++++++++++++++++++++-- GPU/D3D11/FramebufferManagerD3D11.h | 1 + 5 files changed, 74 insertions(+), 15 deletions(-) diff --git a/GPU/Common/ShaderTranslation.cpp b/GPU/Common/ShaderTranslation.cpp index 92701da27f..0add54d626 100644 --- a/GPU/Common/ShaderTranslation.cpp +++ b/GPU/Common/ShaderTranslation.cpp @@ -58,6 +58,15 @@ void ShaderTranslationShutdown() { glslang::FinalizeProcess(); } +std::string Preprocess(std::string code, ShaderLanguage lang) { + // This takes GL up to the version we need. + return code; +} + +std::string Postprocess(std::string code, ShaderLanguage lang) { + return code; +} + bool TranslateShader(std::string *dest, ShaderLanguage destLang, TranslatedShaderMetadata *destMetadata, std::string src, ShaderLanguage srcLang, Draw::ShaderStage stage, std::string *errorMessage) { if (srcLang != GLSL_300 && srcLang != GLSL_140) return false; @@ -74,6 +83,8 @@ bool TranslateShader(std::string *dest, ShaderLanguage destLang, TranslatedShade EShLanguage shaderStage = GetLanguage(stage); glslang::TShader shader(shaderStage); + std::string preprocessed = Preprocess(src, srcLang); + shaderStrings[0] = src.c_str(); shader.setStrings(shaderStrings, 1); if (!shader.parse(&Resources, 100, EProfile::ECompatibilityProfile, false, false, messages)) { @@ -134,7 +145,8 @@ bool TranslateShader(std::string *dest, ShaderLanguage destLang, TranslatedShade options.fixup_clipspace = true; options.shader_model = 50; hlsl.set_options(options); - *dest = hlsl.compile(); + std::string raw = hlsl.compile(); + *dest = Postprocess(raw, destLang); return true; } #endif diff --git a/GPU/D3D11/D3D11Util.cpp b/GPU/D3D11/D3D11Util.cpp index af83807c70..1799c19fc3 100644 --- a/GPU/D3D11/D3D11Util.cpp +++ b/GPU/D3D11/D3D11Util.cpp @@ -9,10 +9,10 @@ #include "D3D11Util.h" -static std::vector CompileShaderToBytecode(const char *code, size_t codeSize, const char *target) { +static std::vector CompileShaderToBytecode(const char *code, size_t codeSize, const char *target, UINT flags) { ID3DBlob *compiledCode = nullptr; ID3DBlob *errorMsgs = nullptr; - HRESULT result = ptr_D3DCompile(code, codeSize, nullptr, nullptr, nullptr, "main", target, 0, 0, &compiledCode, &errorMsgs); + HRESULT result = ptr_D3DCompile(code, codeSize, nullptr, nullptr, nullptr, "main", target, flags, 0, &compiledCode, &errorMsgs); std::string errors; if (errorMsgs) { errors = std::string((const char *)errorMsgs->GetBufferPointer(), errorMsgs->GetBufferSize()); @@ -30,8 +30,8 @@ static std::vector CompileShaderToBytecode(const char *code, size_t cod return std::vector(); } -ID3D11VertexShader *CreateVertexShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, std::vector *byteCodeOut) { - std::vector byteCode = CompileShaderToBytecode(code, codeSize, "vs_5_0"); +ID3D11VertexShader *CreateVertexShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, std::vector *byteCodeOut, UINT flags) { + std::vector byteCode = CompileShaderToBytecode(code, codeSize, "vs_5_0", flags); if (byteCode.empty()) return nullptr; @@ -42,8 +42,8 @@ ID3D11VertexShader *CreateVertexShaderD3D11(ID3D11Device *device, const char *co return vs; } -ID3D11PixelShader *CreatePixelShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize) { - std::vector byteCode = CompileShaderToBytecode(code, codeSize, "ps_5_0"); +ID3D11PixelShader *CreatePixelShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, UINT flags) { + std::vector byteCode = CompileShaderToBytecode(code, codeSize, "ps_5_0", flags); if (byteCode.empty()) return nullptr; @@ -52,8 +52,8 @@ ID3D11PixelShader *CreatePixelShaderD3D11(ID3D11Device *device, const char *code return ps; } -ID3D11ComputeShader *CreateComputeShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize) { - std::vector byteCode = CompileShaderToBytecode(code, codeSize, "cs_5_0"); +ID3D11ComputeShader *CreateComputeShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, UINT flags) { + std::vector byteCode = CompileShaderToBytecode(code, codeSize, "cs_5_0", flags); if (byteCode.empty()) return nullptr; @@ -62,6 +62,17 @@ ID3D11ComputeShader *CreateComputeShaderD3D11(ID3D11Device *device, const char * return cs; } +ID3D11GeometryShader *CreateGeometryShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, UINT flags) { + std::vector byteCode = CompileShaderToBytecode(code, codeSize, "gs_5_0", flags); + if (byteCode.empty()) + return nullptr; + + ID3D11GeometryShader *gs; + device->CreateGeometryShader(byteCode.data(), byteCode.size(), nullptr, &gs); + return gs; +} + + void StockObjectsD3D11::Create(ID3D11Device *device) { D3D11_BLEND_DESC blend_desc{}; blend_desc.RenderTarget[0].BlendEnable = false; diff --git a/GPU/D3D11/D3D11Util.h b/GPU/D3D11/D3D11Util.h index 1329b13399..2e0abcdeb6 100644 --- a/GPU/D3D11/D3D11Util.h +++ b/GPU/D3D11/D3D11Util.h @@ -64,9 +64,10 @@ private: bool nextMapDiscard_ = false; }; -ID3D11VertexShader *CreateVertexShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, std::vector *byteCodeOut); -ID3D11PixelShader *CreatePixelShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize); -ID3D11ComputeShader *CreateComputeShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize); +ID3D11VertexShader *CreateVertexShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, std::vector *byteCodeOut, UINT flags = 0); +ID3D11PixelShader *CreatePixelShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, UINT flags = 0); +ID3D11ComputeShader *CreateComputeShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, UINT flags = 0); +ID3D11GeometryShader *CreateGeometryShaderD3D11(ID3D11Device *device, const char *code, size_t codeSize, UINT flags = 0); class StockObjectsD3D11 { public: diff --git a/GPU/D3D11/FramebufferManagerD3D11.cpp b/GPU/D3D11/FramebufferManagerD3D11.cpp index 330dc31175..a55d0a658f 100644 --- a/GPU/D3D11/FramebufferManagerD3D11.cpp +++ b/GPU/D3D11/FramebufferManagerD3D11.cpp @@ -15,6 +15,9 @@ // Official git repository and contact information can be found at // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. +#include +#include + #include "math/lin/matrix4x4.h" #include "ext/native/thin3d/thin3d.h" #include "base/basictypes.h" @@ -81,6 +84,13 @@ const D3D11_INPUT_ELEMENT_DESC FramebufferManagerD3D11::g_QuadVertexElements[2] { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, }, }; +// The current simple shader translator outputs everything as semantic texcoords, so let's just play along +// for simplicity. +const D3D11_INPUT_ELEMENT_DESC FramebufferManagerD3D11::g_PostVertexElements[2] = { + { "TEXCOORD", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, }, + { "TEXCOORD", 1, DXGI_FORMAT_R32G32_FLOAT, 0, 12, }, +}; + FramebufferManagerD3D11::FramebufferManagerD3D11(Draw::DrawContext *draw) : FramebufferManagerCommon(draw) { device_ = (ID3D11Device *)draw->GetNativeObject(Draw::NativeObject::DEVICE); @@ -149,6 +159,16 @@ FramebufferManagerD3D11::~FramebufferManagerD3D11() { if (drawPixelsTexView_) drawPixelsTexView_->Release(); + if (postVertexShader_) { + postVertexShader_->Release(); + } + if (postPixelShader_) { + postPixelShader_->Release(); + } + if (postInputLayout_) { + postInputLayout_->Release(); + } + // FBO cleanup for (auto it = tempFBOs_.begin(), end = tempFBOs_.end(); it != end; ++it) { it->second.fbo->Release(); @@ -198,6 +218,19 @@ void FramebufferManagerD3D11::CompilePostShader() { std::string vsSource; std::string psSource; + if (postVertexShader_) { + postVertexShader_->Release(); + postVertexShader_ = nullptr; + } + if (postPixelShader_) { + postPixelShader_->Release(); + postPixelShader_ = nullptr; + } + if (postInputLayout_) { + postInputLayout_->Release(); + postInputLayout_ = nullptr; + } + const ShaderInfo *shaderInfo = 0; if (g_Config.sPostShaderName == "Off") { return; @@ -229,17 +262,18 @@ void FramebufferManagerD3D11::CompilePostShader() { return; } + UINT flags = D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY; std::vector byteCode; - postVertexShader_ = CreateVertexShaderD3D11(device_, vsSource.data(), vsSource.size(), &byteCode); + postVertexShader_ = CreateVertexShaderD3D11(device_, vsSource.data(), vsSource.size(), &byteCode, flags); if (!postVertexShader_) { return; } - postPixelShader_ = CreatePixelShaderD3D11(device_, psSource.data(), psSource.size()); + postPixelShader_ = CreatePixelShaderD3D11(device_, psSource.data(), psSource.size(), flags); if (!postPixelShader_) { postVertexShader_->Release(); return; } - device_->CreateInputLayout(g_QuadVertexElements, 2, byteCode.data(), byteCode.size(), &postInputLayout_); + device_->CreateInputLayout(g_PostVertexElements, 2, byteCode.data(), byteCode.size(), &postInputLayout_); usePostShader_ = true; } diff --git a/GPU/D3D11/FramebufferManagerD3D11.h b/GPU/D3D11/FramebufferManagerD3D11.h index 4caf611c6c..bd953fd399 100644 --- a/GPU/D3D11/FramebufferManagerD3D11.h +++ b/GPU/D3D11/FramebufferManagerD3D11.h @@ -147,6 +147,7 @@ private: ID3D11PixelShader *postPixelShader_ = nullptr; ID3D11InputLayout *postInputLayout_ = nullptr; ID3D11Buffer *postConstants_ = nullptr; + static const D3D11_INPUT_ELEMENT_DESC g_PostVertexElements[2]; // Used by post-processing shader std::vector extraFBOs_;