gpu: use pbo & driver owned buffer for ui tex upload

This commit is contained in:
Max Weber
2021-11-02 04:26:32 -06:00
parent 59308db50b
commit 5268f2766d

View File

@@ -194,6 +194,7 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
private int vaoHandle;
private int interfaceTexture;
private int interfacePbo;
private int vaoUiHandle;
private int vboUiHandle;
@@ -726,6 +727,8 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
private void initInterfaceTexture()
{
interfacePbo = glGenBuffers(gl);
interfaceTexture = glGenTexture(gl);
gl.glBindTexture(gl.GL_TEXTURE_2D, interfaceTexture);
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE);
@@ -737,6 +740,7 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
private void shutdownInterfaceTexture()
{
glDeleteBuffer(gl, interfacePbo);
glDeleteTexture(gl, interfaceTexture);
interfaceTexture = -1;
}
@@ -1037,15 +1041,19 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
invokeOnMainThread(() -> drawFrame(overlayColor));
}
private void resize(int canvasWidth, int canvasHeight, int viewportWidth, int viewportHeight)
private void prepareInterfaceTexture(int canvasWidth, int canvasHeight)
{
if (canvasWidth != lastCanvasWidth || canvasHeight != lastCanvasHeight)
{
lastCanvasWidth = canvasWidth;
lastCanvasHeight = canvasHeight;
gl.glBindBuffer(gl.GL_PIXEL_UNPACK_BUFFER, interfacePbo);
gl.glBufferData(gl.GL_PIXEL_UNPACK_BUFFER, canvasWidth * canvasHeight * 4L, null, gl.GL_STREAM_DRAW);
gl.glBindBuffer(gl.GL_PIXEL_UNPACK_BUFFER, 0);
gl.glBindTexture(gl.GL_TEXTURE_2D, interfaceTexture);
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, canvasWidth, canvasHeight, 0, gl.GL_BGRA, gl.GL_UNSIGNED_INT_8_8_8_8_REV, null);
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, canvasWidth, canvasHeight, 0, gl.GL_BGRA, gl.GL_UNSIGNED_BYTE, null);
gl.glBindTexture(gl.GL_TEXTURE_2D, 0);
if (OSType.getOSType() == OSType.MacOS && glDrawable instanceof GLFBODrawable)
@@ -1058,6 +1066,21 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
glfboDrawable.resetSize(gl);
}
}
final BufferProvider bufferProvider = client.getBufferProvider();
final int[] pixels = bufferProvider.getPixels();
final int width = bufferProvider.getWidth();
final int height = bufferProvider.getHeight();
gl.glBindBuffer(gl.GL_PIXEL_UNPACK_BUFFER, interfacePbo);
gl.glMapBuffer(gl.GL_PIXEL_UNPACK_BUFFER, gl.GL_WRITE_ONLY)
.asIntBuffer()
.put(pixels, 0, width * height);
gl.glUnmapBuffer(gl.GL_PIXEL_UNPACK_BUFFER);
gl.glBindTexture(gl.GL_TEXTURE_2D, interfaceTexture);
gl.glTexSubImage2D(gl.GL_TEXTURE_2D, 0, 0, 0, width, height, gl.GL_BGRA, gl.GL_UNSIGNED_INT_8_8_8_8_REV, 0);
gl.glBindBuffer(gl.GL_PIXEL_UNPACK_BUFFER, 0);
gl.glBindTexture(gl.GL_TEXTURE_2D, 0);
}
private void drawFrame(int overlayColor)
@@ -1072,7 +1095,7 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
final int viewportHeight = client.getViewportHeight();
final int viewportWidth = client.getViewportWidth();
resize(canvasWidth, canvasHeight, viewportWidth, viewportHeight);
prepareInterfaceTexture(canvasWidth, canvasHeight);
// Setup anti-aliasing
final AntiAliasingMode antiAliasingMode = config.antiAliasingMode();
@@ -1312,25 +1335,10 @@ public class GpuPlugin extends Plugin implements DrawCallbacks
private void drawUi(final int overlayColor, final int canvasHeight, final int canvasWidth)
{
final BufferProvider bufferProvider = client.getBufferProvider();
final int[] pixels = bufferProvider.getPixels();
final int width = bufferProvider.getWidth();
final int height = bufferProvider.getHeight();
gl.glEnable(gl.GL_BLEND);
vertexBuffer.clear(); // reuse vertex buffer for interface
vertexBuffer.ensureCapacity(pixels.length);
IntBuffer interfaceBuffer = vertexBuffer.getBuffer();
interfaceBuffer.put(pixels);
vertexBuffer.flip();
gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE_MINUS_SRC_ALPHA);
gl.glBindTexture(gl.GL_TEXTURE_2D, interfaceTexture);
gl.glTexSubImage2D(gl.GL_TEXTURE_2D, 0, 0, 0, width, height, gl.GL_BGRA, gl.GL_UNSIGNED_INT_8_8_8_8_REV, interfaceBuffer);
// Use the texture bound in the first pass
final UIScalingMode uiScalingMode = config.uiScalingMode();
gl.glUseProgram(glUiProgram);