Merge pull request #7145 from Abextm/premul-stretch
gpu: Fix stretched mode with global premul alpha
This commit is contained in:
@@ -151,10 +151,6 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
|
|||||||
private int glUiVertexShader;
|
private int glUiVertexShader;
|
||||||
private int glUiFragmentShader;
|
private int glUiFragmentShader;
|
||||||
|
|
||||||
private int glUiPremulProgram;
|
|
||||||
private int glUiPremulVertexShader;
|
|
||||||
private int glUiPremulFragmentShader;
|
|
||||||
|
|
||||||
private int vaoUiHandle;
|
private int vaoUiHandle;
|
||||||
private int vboUiHandle;
|
private int vboUiHandle;
|
||||||
|
|
||||||
@@ -162,9 +158,6 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
|
|||||||
private int texSceneHandle;
|
private int texSceneHandle;
|
||||||
private int rboSceneHandle;
|
private int rboSceneHandle;
|
||||||
|
|
||||||
private int fboUiHandle;
|
|
||||||
private int texUiHandle;
|
|
||||||
|
|
||||||
// scene vertex buffer id
|
// scene vertex buffer id
|
||||||
private int bufferId;
|
private int bufferId;
|
||||||
// scene uv buffer id
|
// scene uv buffer id
|
||||||
@@ -225,7 +218,6 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
|
|||||||
private int uniProjectionMatrix;
|
private int uniProjectionMatrix;
|
||||||
private int uniBrightness;
|
private int uniBrightness;
|
||||||
private int uniTex;
|
private int uniTex;
|
||||||
private int uniTexPremul;
|
|
||||||
private int uniTextures;
|
private int uniTextures;
|
||||||
private int uniTextureOffsets;
|
private int uniTextureOffsets;
|
||||||
private int uniBlockSmall;
|
private int uniBlockSmall;
|
||||||
@@ -389,7 +381,6 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
|
|||||||
shutdownInterfaceTexture();
|
shutdownInterfaceTexture();
|
||||||
shutdownProgram();
|
shutdownProgram();
|
||||||
shutdownVao();
|
shutdownVao();
|
||||||
shutdownUiFBO();
|
|
||||||
shutdownSceneFbo();
|
shutdownSceneFbo();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -507,14 +498,6 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
|
|||||||
null,
|
null,
|
||||||
inputStreamToString(getClass().getResourceAsStream("fragui.glsl")));
|
inputStreamToString(getClass().getResourceAsStream("fragui.glsl")));
|
||||||
|
|
||||||
glUiPremulProgram = gl.glCreateProgram();
|
|
||||||
glUiPremulVertexShader = gl.glCreateShader(gl.GL_VERTEX_SHADER);
|
|
||||||
glUiPremulFragmentShader = gl.glCreateShader(gl.GL_FRAGMENT_SHADER);
|
|
||||||
GLUtil.loadShaders(gl, glUiPremulProgram, glUiPremulVertexShader, -1, glUiPremulFragmentShader,
|
|
||||||
inputStreamToString(getClass().getResourceAsStream("vertuipremul.glsl")),
|
|
||||||
null,
|
|
||||||
inputStreamToString(getClass().getResourceAsStream("fraguipremul.glsl")));
|
|
||||||
|
|
||||||
initUniforms();
|
initUniforms();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -525,7 +508,6 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
|
|||||||
uniSmoothBanding = gl.glGetUniformLocation(glProgram, "smoothBanding");
|
uniSmoothBanding = gl.glGetUniformLocation(glProgram, "smoothBanding");
|
||||||
|
|
||||||
uniTex = gl.glGetUniformLocation(glUiProgram, "tex");
|
uniTex = gl.glGetUniformLocation(glUiProgram, "tex");
|
||||||
uniTexPremul = gl.glGetUniformLocation(glUiPremulProgram, "tex");
|
|
||||||
uniTextures = gl.glGetUniformLocation(glProgram, "textures");
|
uniTextures = gl.glGetUniformLocation(glProgram, "textures");
|
||||||
uniTextureOffsets = gl.glGetUniformLocation(glProgram, "textureOffsets");
|
uniTextureOffsets = gl.glGetUniformLocation(glProgram, "textureOffsets");
|
||||||
|
|
||||||
@@ -578,17 +560,6 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
|
|||||||
|
|
||||||
gl.glDeleteProgram(glUiProgram);
|
gl.glDeleteProgram(glUiProgram);
|
||||||
glUiProgram = -1;
|
glUiProgram = -1;
|
||||||
|
|
||||||
///
|
|
||||||
|
|
||||||
gl.glDeleteShader(glUiPremulVertexShader);
|
|
||||||
glUiPremulVertexShader = -1;
|
|
||||||
|
|
||||||
gl.glDeleteShader(glUiPremulFragmentShader);
|
|
||||||
glUiPremulFragmentShader = -1;
|
|
||||||
|
|
||||||
gl.glDeleteProgram(glUiPremulProgram);
|
|
||||||
glUiPremulProgram = -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initVao()
|
private void initVao()
|
||||||
@@ -721,48 +692,6 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initUiFBO(int width, int height)
|
|
||||||
{
|
|
||||||
// Create and bind the FBO
|
|
||||||
fboUiHandle = glGenFrameBuffer(gl);
|
|
||||||
gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, fboUiHandle);
|
|
||||||
|
|
||||||
// Create the texture to render to
|
|
||||||
texUiHandle = glGenTexture(gl);
|
|
||||||
gl.glBindTexture(gl.GL_TEXTURE_2D, texUiHandle);
|
|
||||||
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, width, height, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, null);
|
|
||||||
|
|
||||||
// Since this is an intermediate the same size as the input, just use nearest neighbors
|
|
||||||
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST);
|
|
||||||
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST);
|
|
||||||
|
|
||||||
// Attach the texture to the framebuffer
|
|
||||||
gl.glFramebufferTexture(gl.GL_FRAMEBUFFER, gl.GL_COLOR_ATTACHMENT0, texUiHandle, 0);
|
|
||||||
|
|
||||||
// Specify that we're going to draw onto color attachment 0
|
|
||||||
int drawLocations[] = { gl.GL_COLOR_ATTACHMENT0 };
|
|
||||||
gl.glDrawBuffers(1, drawLocations, 0);
|
|
||||||
|
|
||||||
// Reset
|
|
||||||
gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0);
|
|
||||||
gl.glBindTexture(gl.GL_TEXTURE_2D, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void shutdownUiFBO()
|
|
||||||
{
|
|
||||||
if (fboUiHandle != -1)
|
|
||||||
{
|
|
||||||
glDeleteFrameBuffer(gl, fboUiHandle);
|
|
||||||
fboUiHandle = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (texUiHandle != -1)
|
|
||||||
{
|
|
||||||
glDeleteTexture(gl, texUiHandle);
|
|
||||||
texUiHandle = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createProjectionMatrix(float left, float right, float bottom, float top, float near, float far)
|
private void createProjectionMatrix(float left, float right, float bottom, float top, float near, float far)
|
||||||
{
|
{
|
||||||
// create a standard orthographic projection
|
// create a standard orthographic projection
|
||||||
@@ -1181,16 +1110,7 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
|
|||||||
final int width = bufferProvider.getWidth();
|
final int width = bufferProvider.getWidth();
|
||||||
final int height = bufferProvider.getHeight();
|
final int height = bufferProvider.getHeight();
|
||||||
|
|
||||||
// Don't blend on the login screen because the fires overflow their alphas.
|
gl.glEnable(gl.GL_BLEND);
|
||||||
final GameState gameState = client.getGameState();
|
|
||||||
if (gameState == GameState.LOGGED_IN)
|
|
||||||
{
|
|
||||||
gl.glEnable(gl.GL_BLEND);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
gl.glDisable(gl.GL_BLEND);
|
|
||||||
}
|
|
||||||
|
|
||||||
vertexBuffer.clear(); // reuse vertex buffer for interface
|
vertexBuffer.clear(); // reuse vertex buffer for interface
|
||||||
vertexBuffer.ensureCapacity(pixels.length);
|
vertexBuffer.ensureCapacity(pixels.length);
|
||||||
@@ -1207,49 +1127,14 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
|
|||||||
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, width, height, 0, gl.GL_BGRA, gl.GL_UNSIGNED_INT_8_8_8_8_REV, interfaceBuffer);
|
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, width, height, 0, gl.GL_BGRA, gl.GL_UNSIGNED_INT_8_8_8_8_REV, interfaceBuffer);
|
||||||
lastCanvasWidth = canvasWidth;
|
lastCanvasWidth = canvasWidth;
|
||||||
lastCanvasHeight = canvasHeight;
|
lastCanvasHeight = canvasHeight;
|
||||||
|
|
||||||
shutdownUiFBO();
|
|
||||||
initUiFBO(width, height);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
gl.glTexSubImage2D(gl.GL_TEXTURE_2D, 0, 0, 0, width, height, gl.GL_BGRA, gl.GL_UNSIGNED_INT_8_8_8_8_REV, interfaceBuffer);
|
gl.glTexSubImage2D(gl.GL_TEXTURE_2D, 0, 0, 0, width, height, gl.GL_BGRA, gl.GL_UNSIGNED_INT_8_8_8_8_REV, interfaceBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// First pass: pre-multiply alpha. But only do it if we're blending.
|
gl.glBindTexture(gl.GL_TEXTURE_2D, interfaceTexture);
|
||||||
if (gameState == GameState.LOGGED_IN && client.isStretchedEnabled() && !client.isStretchedFast())
|
|
||||||
{
|
|
||||||
// Setup
|
|
||||||
gl.glDisable(gl.GL_BLEND);
|
|
||||||
gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, fboUiHandle);
|
|
||||||
gl.glViewport(0, 0, width, height);
|
|
||||||
gl.glClear(gl.GL_COLOR_BUFFER_BIT);
|
|
||||||
|
|
||||||
// Set up uniforms
|
|
||||||
gl.glUseProgram(glUiPremulProgram);
|
|
||||||
gl.glUniform1i(uniTexPremul, 0);
|
|
||||||
|
|
||||||
// Do render call
|
|
||||||
gl.glBindVertexArray(vaoUiHandle);
|
|
||||||
gl.glDrawArrays(gl.GL_TRIANGLE_FAN, 0, 4);
|
|
||||||
|
|
||||||
// Cleanup
|
|
||||||
gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0);
|
|
||||||
gl.glEnable(gl.GL_BLEND);
|
|
||||||
|
|
||||||
// Bind the texture we just drew to for use in pass 2
|
|
||||||
gl.glBindTexture(gl.GL_TEXTURE_2D, texUiHandle);
|
|
||||||
|
|
||||||
// Change the blend function to use pre-multiplied alpha
|
|
||||||
gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE_MINUS_SRC_ALPHA);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
gl.glBindTexture(gl.GL_TEXTURE_2D, interfaceTexture);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Second pass: render onto the screen
|
|
||||||
if (client.isStretchedEnabled())
|
if (client.isStretchedEnabled())
|
||||||
{
|
{
|
||||||
Dimension dim = client.getStretchedDimensions();
|
Dimension dim = client.getStretchedDimensions();
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2018, Adam <Adam@sigterm.info>
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
||||||
* list of conditions and the following disclaimer.
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer in the documentation
|
|
||||||
* and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
||||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
||||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
||||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
||||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
||||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
#version 330
|
|
||||||
|
|
||||||
layout (location = 0) out vec4 color;
|
|
||||||
|
|
||||||
uniform sampler2D tex;
|
|
||||||
|
|
||||||
in vec2 TexCoord;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
vec4 c = texture(tex, TexCoord);
|
|
||||||
color = vec4(c.rgb * c.a, c.a);
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2018, Adam <Adam@sigterm.info>
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions are met:
|
|
||||||
*
|
|
||||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
||||||
* list of conditions and the following disclaimer.
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
* this list of conditions and the following disclaimer in the documentation
|
|
||||||
* and/or other materials provided with the distribution.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
||||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
||||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
||||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
||||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
||||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*/
|
|
||||||
#version 330
|
|
||||||
|
|
||||||
layout (location = 0) in vec3 aPos;
|
|
||||||
layout (location = 1) in vec2 aTexCoord;
|
|
||||||
|
|
||||||
out vec2 TexCoord;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
gl_Position = vec4(aPos, 1.0);
|
|
||||||
|
|
||||||
// Flip the UV because it's pre-flipped in the ui texture buffer, but we don't need it to be flipped here.
|
|
||||||
TexCoord = vec2(aTexCoord.x, 1 - aTexCoord.y);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user