Skip to content

Commit

Permalink
Merge pull request #12921 from unknownbrackets/postshader
Browse files Browse the repository at this point in the history
Use less FBOs for chained post-processing shaders
  • Loading branch information
hrydgard authored Jul 13, 2020
2 parents 59af3d5 + 804b673 commit bb6219e
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 83 deletions.
5 changes: 2 additions & 3 deletions GPU/Common/FramebufferCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ FramebufferManagerCommon::FramebufferManagerCommon(Draw::DrawContext *draw)
: draw_(draw),
displayFormat_(GE_FORMAT_565) {
presentation_ = new PresentationCommon(draw);
UpdateSize();
}

FramebufferManagerCommon::~FramebufferManagerCommon() {
Expand All @@ -70,8 +69,8 @@ FramebufferManagerCommon::~FramebufferManagerCommon() {
}

void FramebufferManagerCommon::Init() {
BeginFrame();
presentation_->UpdatePostShader();
// We may need to override the render size if the shader is upscaling or SSAA.
Resized();
}

bool FramebufferManagerCommon::UpdateSize() {
Expand Down
45 changes: 36 additions & 9 deletions GPU/Common/PresentationCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,42 @@ bool PresentationCommon::BuildPostShader(const ShaderInfo *shaderInfo, const Sha
nextHeight = (int)rc.h;
}

// No depth/stencil for post processing
Draw::Framebuffer *fbo = draw_->CreateFramebuffer({ nextWidth, nextHeight, 1, 1, false, Draw::FBO_8888 });
if (!fbo) {
if (!AllocateFramebuffer(nextWidth, nextHeight)) {
pipeline->Release();
return false;
}
postShaderFramebuffers_.push_back(fbo);
}

postShaderPipelines_.push_back(pipeline);
postShaderInfo_.push_back(*shaderInfo);
return true;
}

bool PresentationCommon::AllocateFramebuffer(int w, int h) {
using namespace Draw;

// First, let's try to find a framebuffer of the right size that is NOT the most recent.
Framebuffer *last = postShaderFramebuffers_.empty() ? nullptr : postShaderFramebuffers_.back();
for (const auto &prev : postShaderFBOUsage_) {
if (prev.w == w && prev.h == h && prev.fbo != last) {
// Great, this one's perfect. Ref it for when we release.
prev.fbo->AddRef();
postShaderFramebuffers_.push_back(prev.fbo);
return true;
}
}

// No depth/stencil for post processing
Draw::Framebuffer *fbo = draw_->CreateFramebuffer({ w, h, 1, 1, false, Draw::FBO_8888 });
if (!fbo) {
return false;
}

postShaderFBOUsage_.push_back({ fbo, w, h });
postShaderFramebuffers_.push_back(fbo);
return true;
}

void PresentationCommon::ShowPostShaderError(const std::string &errorString) {
// let's show the first line of the error string as an OSM.
std::set<std::string> blacklistedLines;
Expand Down Expand Up @@ -440,6 +462,7 @@ void PresentationCommon::DestroyPostShader() {
DoReleaseVector(postShaderPipelines_);
DoReleaseVector(postShaderFramebuffers_);
postShaderInfo_.clear();
postShaderFBOUsage_.clear();
}

Draw::ShaderModule *PresentationCommon::CompileShaderModule(Draw::ShaderStage stage, ShaderLanguage lang, const std::string &src, std::string *errorString) {
Expand Down Expand Up @@ -502,11 +525,11 @@ void PresentationCommon::SourceFramebuffer(Draw::Framebuffer *fb, int bufferWidt
srcHeight_ = bufferHeight;
}

void PresentationCommon::BindSource() {
void PresentationCommon::BindSource(int binding) {
if (srcTexture_) {
draw_->BindTexture(0, srcTexture_);
draw_->BindTexture(binding, srcTexture_);
} else if (srcFramebuffer_) {
draw_->BindFramebufferAsTexture(srcFramebuffer_, 0, Draw::FB_COLOR_BIT, 0);
draw_->BindFramebufferAsTexture(srcFramebuffer_, binding, Draw::FB_COLOR_BIT, 0);
} else {
assert(false);
}
Expand Down Expand Up @@ -630,8 +653,9 @@ void PresentationCommon::CopyToOutput(OutputFlags flags, int uvRotation, float u
if (usePostShaderOutput) {
draw_->BindFramebufferAsTexture(postShaderFramebuffers_[i - 1], 0, Draw::FB_COLOR_BIT, 0);
} else {
BindSource();
BindSource(0);
}
BindSource(1);

int nextWidth, nextHeight;
draw_->GetFramebufferDimensions(postShaderFramebuffer, &nextWidth, &nextHeight);
Expand All @@ -647,6 +671,7 @@ void PresentationCommon::CopyToOutput(OutputFlags flags, int uvRotation, float u

Draw::SamplerState *sampler = useNearest || shaderInfo->isUpscalingFilter ? samplerNearest_ : samplerLinear_;
draw_->BindSamplerStates(0, 1, &sampler);
draw_->BindSamplerStates(1, 1, &sampler);

draw_->BindVertexBuffers(0, 1, &vdata_, &postVertsOffset);
draw_->BindIndexBuffer(idata_, 0);
Expand Down Expand Up @@ -677,8 +702,9 @@ void PresentationCommon::CopyToOutput(OutputFlags flags, int uvRotation, float u
if (usePostShaderOutput) {
draw_->BindFramebufferAsTexture(postShaderFramebuffers_.back(), 0, Draw::FB_COLOR_BIT, 0);
} else {
BindSource();
BindSource(0);
}
BindSource(1);

if (isFinalAtOutputResolution) {
PostShaderUniforms uniforms;
Expand All @@ -695,6 +721,7 @@ void PresentationCommon::CopyToOutput(OutputFlags flags, int uvRotation, float u

Draw::SamplerState *sampler = useNearest ? samplerNearest_ : samplerLinear_;
draw_->BindSamplerStates(0, 1, &sampler);
draw_->BindSamplerStates(1, 1, &sampler);

auto setViewport = [&](float x, float y, float w, float h) {
Draw::Viewport viewport{ x, y, w, h, 0.0f, 1.0f };
Expand Down
10 changes: 9 additions & 1 deletion GPU/Common/PresentationCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ class PresentationCommon {
Draw::ShaderModule *CompileShaderModule(Draw::ShaderStage stage, ShaderLanguage lang, const std::string &src, std::string *errorString);
Draw::Pipeline *CreatePipeline(std::vector<Draw::ShaderModule *> shaders, bool postShader, const Draw::UniformBufferDesc *uniformDesc);
bool BuildPostShader(const ShaderInfo *shaderInfo, const ShaderInfo *next);
bool AllocateFramebuffer(int w, int h);

void BindSource();
void BindSource(int binding);

void GetCardboardSettings(CardboardSettings *cardboardSettings);
void CalculatePostShaderUniforms(int bufferWidth, int bufferHeight, int targetWidth, int targetHeight, const ShaderInfo *shaderInfo, PostShaderUniforms *uniforms);
Expand Down Expand Up @@ -146,4 +147,11 @@ class PresentationCommon {
bool usePostShader_ = false;
bool restorePostShader_ = false;
ShaderLanguage lang_;

struct PrevFBO {
Draw::Framebuffer *fbo;
int w;
int h;
};
std::vector<PrevFBO> postShaderFBOUsage_;
};
9 changes: 8 additions & 1 deletion GPU/Common/ShaderTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ std::string Postprocess(std::string code, ShaderLanguage lang, Draw::ShaderStage
out << "sampler2D sampler0 : register(s0);\n";
continue;
}
if (line == "uniform sampler2D sampler1;" && lang == HLSL_DX9) {
out << "sampler2D sampler1 : register(s1);\n";
continue;
}
if (line.find("uniform float") != std::string::npos) {
continue;
}
Expand Down Expand Up @@ -181,7 +185,10 @@ bool ConvertToVulkanGLSL(std::string *dest, TranslatedShaderMetadata *destMetada
if (line.find("uniform bool") != std::string::npos) {
continue;
} else if (line.find("uniform sampler2D") == 0) {
line = "layout(set = 0, binding = 1) " + line;
if (line.find("sampler0") != line.npos)
line = "layout(set = 0, binding = 1) " + line;
else
line = "layout(set = 0, binding = 2) " + line;
} else if (line.find("uniform ") != std::string::npos) {
continue;
} else if (2 == sscanf(line.c_str(), "varying vec%d v_texcoord%d;", &vecSize, &num)) {
Expand Down
2 changes: 1 addition & 1 deletion GPU/D3D11/GPU_D3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ GPU_D3D11::GPU_D3D11(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
drawEngine_.SetShaderManager(shaderManagerD3D11_);
drawEngine_.SetTextureCache(textureCacheD3D11_);
drawEngine_.SetFramebufferManager(framebufferManagerD3D11_);
framebufferManagerD3D11_->Init();
framebufferManagerD3D11_->SetTextureCache(textureCacheD3D11_);
framebufferManagerD3D11_->SetShaderManager(shaderManagerD3D11_);
framebufferManagerD3D11_->SetDrawEngine(&drawEngine_);
framebufferManagerD3D11_->Init();
textureCacheD3D11_->SetFramebufferManager(framebufferManagerD3D11_);
textureCacheD3D11_->SetDepalShaderCache(depalShaderCache_);
textureCacheD3D11_->SetShaderManager(shaderManagerD3D11_);
Expand Down
2 changes: 1 addition & 1 deletion GPU/Directx9/GPU_DX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ GPU_DX9::GPU_DX9(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
drawEngine_.SetShaderManager(shaderManagerDX9_);
drawEngine_.SetTextureCache(textureCacheDX9_);
drawEngine_.SetFramebufferManager(framebufferManagerDX9_);
framebufferManagerDX9_->Init();
framebufferManagerDX9_->SetTextureCache(textureCacheDX9_);
framebufferManagerDX9_->SetShaderManager(shaderManagerDX9_);
framebufferManagerDX9_->SetDrawEngine(&drawEngine_);
framebufferManagerDX9_->Init();
textureCacheDX9_->SetFramebufferManager(framebufferManagerDX9_);
textureCacheDX9_->SetDepalShaderCache(&depalShaderCache_);
textureCacheDX9_->SetShaderManager(shaderManagerDX9_);
Expand Down
7 changes: 4 additions & 3 deletions GPU/GLES/DepalettizeShaderGLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#include "Common/Log.h"
#include "Common/StringUtils.h"
#include "Core/Reporting.h"
#include "DepalettizeShaderGLES.h"
#include "GPU/GLES/DepalettizeShaderGLES.h"
#include "GPU/GLES/DrawEngineGLES.h"
#include "GPU/GLES/TextureCacheGLES.h"
#include "GPU/Common/DepalettizeShaderCommon.h"

Expand Down Expand Up @@ -178,8 +179,8 @@ DepalShader *DepalShaderCacheGLES::GetDepalettizeShader(uint32_t clutMode, GEBuf
queries.push_back({ &depal->u_pal, "pal" });

std::vector<GLRProgram::Initializer> initializer;
initializer.push_back({ &depal->u_tex, 0, 0 });
initializer.push_back({ &depal->u_pal, 0, 3 });
initializer.push_back({ &depal->u_tex, 0, TEX_SLOT_PSP_TEXTURE });
initializer.push_back({ &depal->u_pal, 0, TEX_SLOT_CLUT });

std::vector<GLRShader *> shaders{ vertexShader_, fragShader };

Expand Down
2 changes: 0 additions & 2 deletions GPU/GLES/FramebufferManagerGLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ FramebufferManagerGLES::FramebufferManagerGLES(Draw::DrawContext *draw, GLRender

void FramebufferManagerGLES::Init() {
FramebufferManagerCommon::Init();
// Workaround for upscaling shaders where we force x1 resolution without saving it
Resized();
CompileDraw2DProgram();
}

Expand Down
2 changes: 1 addition & 1 deletion GPU/GLES/GPU_GLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ GPU_GLES::GPU_GLES(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
drawEngine_.SetTextureCache(textureCacheGL_);
drawEngine_.SetFramebufferManager(framebufferManagerGL_);
drawEngine_.SetFragmentTestCache(&fragmentTestCache_);
framebufferManagerGL_->Init();
framebufferManagerGL_->SetTextureCache(textureCacheGL_);
framebufferManagerGL_->SetShaderManager(shaderManagerGL_);
framebufferManagerGL_->SetDrawEngine(&drawEngine_);
framebufferManagerGL_->Init();
depalShaderCache_.Init();
textureCacheGL_->SetFramebufferManager(framebufferManagerGL_);
textureCacheGL_->SetDepalShaderCache(&depalShaderCache_);
Expand Down
6 changes: 0 additions & 6 deletions GPU/Vulkan/FramebufferVulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,6 @@ void FramebufferManagerVulkan::NotifyClear(bool clearColor, bool clearAlpha, boo
}
}

void FramebufferManagerVulkan::Init() {
FramebufferManagerCommon::Init();
// Workaround for upscaling shaders where we force x1 resolution without saving it
Resized();
}

void FramebufferManagerVulkan::DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) {
float texCoords[8] = {
u0,v0,
Expand Down
2 changes: 0 additions & 2 deletions GPU/Vulkan/FramebufferVulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class FramebufferManagerVulkan : public FramebufferManagerCommon {
// x,y,w,h are relative to destW, destH which fill out the target completely.
void DrawActiveTexture(float x, float y, float w, float h, float destW, float destH, float u0, float v0, float u1, float v1, int uvRotation, int flags) override;

virtual void Init() override;

void BeginFrameVulkan(); // there's a BeginFrame in the base class, which this calls
void EndFrame();

Expand Down
2 changes: 1 addition & 1 deletion GPU/Vulkan/GPU_Vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ GPU_Vulkan::GPU_Vulkan(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
drawEngine_.SetShaderManager(shaderManagerVulkan_);
drawEngine_.SetPipelineManager(pipelineManager_);
framebufferManagerVulkan_->SetVulkan2D(&vulkan2D_);
framebufferManagerVulkan_->Init();
framebufferManagerVulkan_->SetTextureCache(textureCacheVulkan_);
framebufferManagerVulkan_->SetDrawEngine(&drawEngine_);
framebufferManagerVulkan_->SetShaderManager(shaderManagerVulkan_);
framebufferManagerVulkan_->Init();
textureCacheVulkan_->SetDepalShaderCache(&depalShaderCache_);
textureCacheVulkan_->SetFramebufferManager(framebufferManagerVulkan_);
textureCacheVulkan_->SetShaderManager(shaderManagerVulkan_);
Expand Down
4 changes: 2 additions & 2 deletions ext/native/thin3d/GLQueueRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ struct GLRRenderData {
} drawIndexed;
struct {
const char *name; // if null, use loc
GLint *loc; // NOTE: This is a pointer so we can immediately use things that are "queried" during program creation.
const GLint *loc; // NOTE: This is a pointer so we can immediately use things that are "queried" during program creation.
GLint count;
float v[4];
} uniform4;
struct {
const char *name; // if null, use loc
GLint *loc;
const GLint *loc;
float m[16];
} uniformMatrix4;
struct {
Expand Down
10 changes: 5 additions & 5 deletions ext/native/thin3d/GLRenderManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ class GLRenderManager {
curRenderStep_->commands.push_back(data);
}

void SetUniformI(GLint *loc, int count, const int *udata) {
void SetUniformI(const GLint *loc, int count, const int *udata) {
_dbg_assert_(G3D, curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER);
#ifdef _DEBUG
assert(curProgram_);
Expand All @@ -659,7 +659,7 @@ class GLRenderManager {
curRenderStep_->commands.push_back(data);
}

void SetUniformI1(GLint *loc, int udata) {
void SetUniformI1(const GLint *loc, int udata) {
_dbg_assert_(G3D, curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER);
#ifdef _DEBUG
assert(curProgram_);
Expand All @@ -671,7 +671,7 @@ class GLRenderManager {
curRenderStep_->commands.push_back(data);
}

void SetUniformF(GLint *loc, int count, const float *udata) {
void SetUniformF(const GLint *loc, int count, const float *udata) {
_dbg_assert_(G3D, curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER);
#ifdef _DEBUG
assert(curProgram_);
Expand All @@ -683,7 +683,7 @@ class GLRenderManager {
curRenderStep_->commands.push_back(data);
}

void SetUniformF1(GLint *loc, const float udata) {
void SetUniformF1(const GLint *loc, const float udata) {
_dbg_assert_(G3D, curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER);
#ifdef _DEBUG
assert(curProgram_);
Expand All @@ -707,7 +707,7 @@ class GLRenderManager {
curRenderStep_->commands.push_back(data);
}

void SetUniformM4x4(GLint *loc, const float *udata) {
void SetUniformM4x4(const GLint *loc, const float *udata) {
_dbg_assert_(G3D, curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER);
#ifdef _DEBUG
assert(curProgram_);
Expand Down
2 changes: 1 addition & 1 deletion ext/native/thin3d/thin3d_d3d9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ class D3D9Context : public DrawContext {
void BindTextures(int start, int count, Texture **textures) override;
void BindSamplerStates(int start, int count, SamplerState **states) override {
for (int i = 0; i < count; ++i) {
D3D9SamplerState *s = static_cast<D3D9SamplerState *>(states[start + i]);
D3D9SamplerState *s = static_cast<D3D9SamplerState *>(states[i]);
s->Apply(device_, start + i);
}
}
Expand Down
Loading

0 comments on commit bb6219e

Please sign in to comment.