Skip to content

Commit

Permalink
Fix texel addressing in post processing shaders when buffer size isn'…
Browse files Browse the repository at this point in the history
…t 480x272. Fixes #8016.
  • Loading branch information
hrydgard committed Oct 31, 2015
1 parent c84f0e6 commit 27d4be9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
70 changes: 43 additions & 27 deletions GPU/GLES/Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,27 +207,11 @@ void FramebufferManager::CompileDraw2DProgram() {
glsl_bind(postShaderProgram_);
glUniform1i(postShaderProgram_->sampler0, 0);
SetNumExtraFBOs(1);
float u_delta = 1.0f / renderWidth_;
float v_delta = 1.0f / renderHeight_;
float u_pixel_delta = u_delta;
float v_pixel_delta = v_delta;
if (postShaderAtOutputResolution_) {
float x, y, w, h;
CenterRect(&x, &y, &w, &h, 480.0f, 272.0f, (float)pixelWidth_, (float)pixelHeight_, ROTATION_LOCKED_HORIZONTAL);
u_pixel_delta = 1.0f / w;
v_pixel_delta = 1.0f / h;
}

int deltaLoc = glsl_uniform_loc(postShaderProgram_, "u_texelDelta");
if (deltaLoc != -1)
glUniform2f(deltaLoc, u_delta, v_delta);
int pixelDeltaLoc = glsl_uniform_loc(postShaderProgram_, "u_pixelDelta");
if (pixelDeltaLoc != -1)
glUniform2f(pixelDeltaLoc, u_pixel_delta, v_pixel_delta);
deltaLoc_ = glsl_uniform_loc(postShaderProgram_, "u_texelDelta");
pixelDeltaLoc_ = glsl_uniform_loc(postShaderProgram_, "u_pixelDelta");
timeLoc_ = glsl_uniform_loc(postShaderProgram_, "u_time");
if (timeLoc_ != -1)
glUniform4f(timeLoc_, 0.0f, 0.0f, 0.0f, 0.0f);

UpdatePostShaderUniforms(renderWidth_, renderHeight_);
usePostShader_ = true;
}
} else {
Expand All @@ -239,6 +223,30 @@ void FramebufferManager::CompileDraw2DProgram() {
}
}

void FramebufferManager::UpdatePostShaderUniforms(int renderWidth, int renderHeight) {
float u_delta = 1.0f / renderWidth;
float v_delta = 1.0f / renderHeight;
float u_pixel_delta = u_delta;
float v_pixel_delta = v_delta;
if (postShaderAtOutputResolution_) {
float x, y, w, h;
CenterRect(&x, &y, &w, &h, 480.0f, 272.0f, (float)pixelWidth_, (float)pixelHeight_, ROTATION_LOCKED_HORIZONTAL);
u_pixel_delta = 1.0f / w;
v_pixel_delta = 1.0f / h;
}

if (deltaLoc_ != -1)
glUniform2f(deltaLoc_, u_delta, v_delta);
if (pixelDeltaLoc_ != -1)
glUniform2f(pixelDeltaLoc_, u_pixel_delta, v_pixel_delta);
if (timeLoc_ != -1) {
int flipCount = __DisplayGetFlipCount();
int vCount = __DisplayGetVCount();
float time[4] = { time_now(), (vCount % 60) * 1.0f / 60.0f, (float)vCount, (float)(flipCount % 60) };
glUniform4fv(timeLoc_, 1, time);
}
}

void FramebufferManager::DestroyDraw2DProgram() {
if (draw2dprogram_) {
glsl_destroy(draw2dprogram_);
Expand All @@ -263,6 +271,8 @@ FramebufferManager::FramebufferManager() :
stencilUploadProgram_(nullptr),
plainColorLoc_(-1),
timeLoc_(-1),
deltaLoc_(-1),
pixelDeltaLoc_(-1),
textureCache_(nullptr),
shaderManager_(nullptr),
usePostShader_(false),
Expand Down Expand Up @@ -405,6 +415,10 @@ void FramebufferManager::DrawFramebuffer(const u8 *srcPixels, GEBufferFormat src
float x, y, w, h;
int uvRotation = (g_Config.iRenderingMode != FB_NON_BUFFERED_MODE) ? g_Config.iInternalScreenRotation : ROTATION_LOCKED_HORIZONTAL;
CenterRect(&x, &y, &w, &h, 480.0f, 272.0f, (float)pixelWidth_, (float)pixelHeight_, uvRotation);
if (applyPostShader) {
glsl_bind(postShaderProgram_);
UpdatePostShaderUniforms(renderWidth_, renderHeight_);
}
if (cardboardSettings.enabled) {
// Left Eye Image
glstate.viewport.set(cardboardSettings.leftEyeXPosition, cardboardSettings.screenYPosition, cardboardSettings.screenWidth, cardboardSettings.screenHeight);
Expand Down Expand Up @@ -537,15 +551,11 @@ void FramebufferManager::DrawActiveTexture(GLuint texture, float x, float y, flo
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, g_Config.iBufFilter == SCALE_NEAREST ? GL_NEAREST : GL_LINEAR);
}

shaderManager_->DirtyLastShader(); // dirty lastShader_

glsl_bind(program);
if (program == postShaderProgram_ && timeLoc_ != -1) {
int flipCount = __DisplayGetFlipCount();
int vCount = __DisplayGetVCount();
float time[4] = {time_now(), (vCount % 60) * 1.0f/60.0f, (float)vCount, (float)(flipCount % 60)};
glUniform4fv(timeLoc_, 1, time);
if (program != postShaderProgram_) {
shaderManager_->DirtyLastShader(); // dirty lastShader_
glsl_bind(program);
}

glstate.arrayBuffer.unbind();
glstate.elementArrayBuffer.unbind();
glEnableVertexAttribArray(program->a_position);
Expand Down Expand Up @@ -1064,6 +1074,9 @@ void FramebufferManager::CopyDisplayToOutput() {
int fbo_w, fbo_h;
fbo_get_dimensions(extraFBOs_[0], &fbo_w, &fbo_h);
glstate.viewport.set(0, 0, fbo_w, fbo_h);
shaderManager_->DirtyLastShader(); // dirty lastShader_
glsl_bind(postShaderProgram_);
UpdatePostShaderUniforms(renderWidth_, renderHeight_);
DrawActiveTexture(colorTexture, 0, 0, fbo_w, fbo_h, fbo_w, fbo_h, true, 0.0f, 0.0f, 1.0f, 1.0f, postShaderProgram_);

fbo_unbind();
Expand Down Expand Up @@ -1096,6 +1109,9 @@ void FramebufferManager::CopyDisplayToOutput() {
glInvalidateFramebuffer(GL_FRAMEBUFFER, 3, attachments);
}
} else {
shaderManager_->DirtyLastShader(); // dirty lastShader_
glsl_bind(postShaderProgram_);
UpdatePostShaderUniforms(vfb->renderWidth, vfb->renderHeight);
if (g_Config.bEnableCardboard) {
// Left Eye Image
glstate.viewport.set(cardboardSettings.leftEyeXPosition, cardboardSettings.screenYPosition, cardboardSettings.screenWidth, cardboardSettings.screenHeight);
Expand Down
3 changes: 3 additions & 0 deletions GPU/GLES/Framebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class FramebufferManager : public FramebufferManagerCommon {
virtual void NotifyRenderFramebufferUpdated(VirtualFramebuffer *vfb, bool vfbFormatChanged) override;

private:
void UpdatePostShaderUniforms(int renderWidth, int renderHeight);
void CompileDraw2DProgram();
void DestroyDraw2DProgram();

Expand All @@ -161,6 +162,8 @@ class FramebufferManager : public FramebufferManagerCommon {
GLSLProgram *stencilUploadProgram_;
int plainColorLoc_;
int timeLoc_;
int pixelDeltaLoc_;
int deltaLoc_;

TextureCache *textureCache_;
ShaderManager *shaderManager_;
Expand Down

0 comments on commit 27d4be9

Please sign in to comment.