framebuffer fixes

This commit is contained in:
Bixilon 2021-12-15 19:26:54 +01:00
parent 13ce836539
commit 52a1a119da
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
15 changed files with 141 additions and 39 deletions

View File

@ -23,4 +23,6 @@ object Colors {
val TRUE_YELLOW = RGBColor(255, 255, 0)
val DARK_RED = RGBColor(255, 50, 30)
val TRANSPARENT = RGBColor(0, 0, 0, 0)
}

View File

@ -246,6 +246,7 @@ class RenderWindow(
),
)) {
val nextMode = it.decide(PolygonModes.LINE, PolygonModes.FILL)
renderSystem.framebuffer = framebufferManager.default.framebuffer
renderSystem.polygonMode = nextMode
sendDebugMessage("Polygon mode: ${nextMode.format()}")
}
@ -319,7 +320,9 @@ class RenderWindow(
}
renderStats.startFrame()
renderSystem.framebuffer = null
renderSystem.clear(IntegratedBufferTypes.COLOR_BUFFER, IntegratedBufferTypes.DEPTH_BUFFER)
framebufferManager.clear()
val currentTickTime = TimeUtil.time
@ -343,11 +346,11 @@ class RenderWindow(
renderer.prepareDraw()
}
for (renderer in rendererList) {
if (renderer is SkipAll && renderer.skipAll) {
continue
}
for (phase in RenderPhases.VALUES) { // ToDo: Move this up after frame buffers
for (phase in RenderPhases.VALUES) { // ToDo: Move this up after frame buffers
for (renderer in rendererList) {
if (renderer is SkipAll && renderer.skipAll) {
continue
}
if (!phase.type.java.isAssignableFrom(renderer::class.java)) {
continue
}
@ -360,6 +363,7 @@ class RenderWindow(
}
}
framebufferManager.draw()
renderSystem.framebuffer = null
renderSystem.reset() // Reset to enable depth mask, etc again
renderStats.endDraw()

View File

@ -3,25 +3,38 @@ package de.bixilon.minosoft.gui.rendering.framebuffer
import de.bixilon.minosoft.gui.rendering.Drawable
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.framebuffer.defaultf.DefaultFramebuffer
import de.bixilon.minosoft.gui.rendering.framebuffer.gui.GUIFramebuffer
import de.bixilon.minosoft.gui.rendering.modding.events.ResizeWindowEvent
import de.bixilon.minosoft.gui.rendering.system.base.IntegratedBufferTypes
import de.bixilon.minosoft.modding.event.invoker.CallbackEventInvoker
class FramebufferManager(
private val renderWindow: RenderWindow,
) : Drawable {
val default = DefaultFramebuffer(renderWindow)
val gui = GUIFramebuffer(renderWindow)
fun init() {
default.init()
gui.init()
renderWindow.connection.registerEvent(CallbackEventInvoker.of<ResizeWindowEvent> {
default.framebuffer.resize(it.size)
gui.framebuffer.resize(it.size)
})
}
fun clear() {
default.clear()
gui.clear()
}
override fun draw() {
default.draw()
renderWindow.renderSystem.clear(IntegratedBufferTypes.DEPTH_BUFFER, IntegratedBufferTypes.STENCIL_BUFFER)
gui.draw()
}
}

View File

@ -1,4 +1,4 @@
package de.bixilon.minosoft.gui.rendering.framebuffer.defaultf
package de.bixilon.minosoft.gui.rendering.framebuffer
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
@ -6,7 +6,7 @@ import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
import glm_.vec2.Vec2
class DefaultFramebufferMesh(renderWindow: RenderWindow) : Mesh(renderWindow, DefaultFramebufferMeshStruct) {
class FramebufferMesh(renderWindow: RenderWindow) : Mesh(renderWindow, DefaultFramebufferMeshStruct) {
init {
val vertices = arrayOf(

View File

@ -0,0 +1,36 @@
package de.bixilon.minosoft.gui.rendering.framebuffer
import de.bixilon.minosoft.gui.rendering.Drawable
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.system.base.IntegratedBufferTypes
import de.bixilon.minosoft.gui.rendering.system.base.buffer.frame.Framebuffer
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
interface IntegratedFramebuffer : Drawable {
val renderWindow: RenderWindow
val shader: Shader
val framebuffer: Framebuffer
val mesh: Mesh
fun init() {
framebuffer.init()
shader.load()
shader.use()
shader.setInt("uTexture", 0)
mesh.load()
}
fun clear() {
renderWindow.renderSystem.framebuffer = framebuffer
renderWindow.renderSystem.clear(IntegratedBufferTypes.COLOR_BUFFER, IntegratedBufferTypes.DEPTH_BUFFER, IntegratedBufferTypes.STENCIL_BUFFER)
}
override fun draw() {
renderWindow.renderSystem.framebuffer = null
framebuffer.bindTexture()
shader.use()
mesh.draw()
}
}

View File

@ -1,28 +1,15 @@
package de.bixilon.minosoft.gui.rendering.framebuffer.defaultf
import de.bixilon.minosoft.gui.rendering.Drawable
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.framebuffer.FramebufferMesh
import de.bixilon.minosoft.gui.rendering.framebuffer.IntegratedFramebuffer
import de.bixilon.minosoft.gui.rendering.system.base.buffer.frame.Framebuffer
import de.bixilon.minosoft.util.KUtil.toResourceLocation
class DefaultFramebuffer(
private val renderWindow: RenderWindow,
) : Drawable {
private var shader = renderWindow.renderSystem.createShader("minosoft:framebuffer/default".toResourceLocation())
val framebuffer: Framebuffer = renderWindow.renderSystem.createFramebuffer()
private val mesh = DefaultFramebufferMesh(renderWindow)
fun init() {
framebuffer.init()
shader.load()
mesh.load()
}
override fun draw() {
renderWindow.renderSystem.framebuffer = null
framebuffer.bindTexture()
shader.use()
shader.setInt("uTexture", 0)
mesh.draw()
}
override val renderWindow: RenderWindow,
) : IntegratedFramebuffer {
override val shader = renderWindow.renderSystem.createShader("minosoft:framebuffer/default".toResourceLocation())
override val framebuffer: Framebuffer = renderWindow.renderSystem.createFramebuffer()
override val mesh = FramebufferMesh(renderWindow)
}

View File

@ -0,0 +1,15 @@
package de.bixilon.minosoft.gui.rendering.framebuffer.gui
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.framebuffer.FramebufferMesh
import de.bixilon.minosoft.gui.rendering.framebuffer.IntegratedFramebuffer
import de.bixilon.minosoft.gui.rendering.system.base.buffer.frame.Framebuffer
import de.bixilon.minosoft.util.KUtil.toResourceLocation
class GUIFramebuffer(
override val renderWindow: RenderWindow,
) : IntegratedFramebuffer {
override val shader = renderWindow.renderSystem.createShader("minosoft:framebuffer/gui".toResourceLocation())
override val framebuffer: Framebuffer = renderWindow.renderSystem.createFramebuffer()
override val mesh = FramebufferMesh(renderWindow)
}

View File

@ -44,6 +44,7 @@ import de.bixilon.minosoft.gui.rendering.gui.hud.elements.title.TitleHUDElement
import de.bixilon.minosoft.gui.rendering.modding.events.ResizeWindowEvent
import de.bixilon.minosoft.gui.rendering.system.base.IntegratedBufferTypes
import de.bixilon.minosoft.gui.rendering.system.base.RenderSystem
import de.bixilon.minosoft.gui.rendering.system.base.buffer.frame.Framebuffer
import de.bixilon.minosoft.gui.rendering.system.base.phases.OtherDrawable
import de.bixilon.minosoft.modding.event.invoker.CallbackEventInvoker
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
@ -66,8 +67,8 @@ class HUDRenderer(
private var enabled = true
var matrixChange = true
private set
// override val framebuffer: Framebuffer
// get() = renderWindow.framebufferManager.gui
override val framebuffer: Framebuffer
get() = renderWindow.framebufferManager.gui.framebuffer
private val hudElements: MutableMap<ResourceLocation, HUDElement> = synchronizedMapOf()

View File

@ -44,7 +44,7 @@ interface RenderSystem {
sourceAlpha: BlendingFunctions = BlendingFunctions.SOURCE_ALPHA,
destinationAlpha: BlendingFunctions = BlendingFunctions.ONE_MINUS_SOURCE_ALPHA,
depth: DepthFunctions = DepthFunctions.LESS,
clearColor: RGBColor = Colors.TRUE_YELLOW,
clearColor: RGBColor = Colors.TRANSPARENT,
) {
setBlendFunc(sourceAlpha, destinationAlpha, BlendingFunctions.ONE, BlendingFunctions.ZERO)
this[RenderingCapabilities.DEPTH_TEST] = depthTest

View File

@ -243,7 +243,7 @@ class OpenGLRenderSystem(
if (value == field) {
return
}
glClearColor(clearColor.floatRed, clearColor.floatGreen, clearColor.floatBlue, clearColor.floatAlpha)
glClearColor(value.floatRed, value.floatGreen, value.floatBlue, value.floatAlpha)
field = value
}

View File

@ -47,11 +47,6 @@ class OpenGLFramebuffer(var size: Vec2i) : Framebuffer {
glBindFramebuffer(GL_FRAMEBUFFER, id)
}
fun unbind() {
check(state == FramebufferState.COMPLETE) { "Framebuffer is incomplete!" }
glBindFramebuffer(GL_FRAMEBUFFER, 0)
}
override fun attach(renderbuffer: Renderbuffer) {
check(renderbuffer is OpenGLRenderbuffer) { "Can not attach non OpenGL renderbuffer!" }
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbuffer.id)
@ -71,6 +66,7 @@ class OpenGLFramebuffer(var size: Vec2i) : Framebuffer {
override fun bindTexture() {
check(state == FramebufferState.COMPLETE) { "Framebuffer is incomplete!" }
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, texture.id)
}

View File

@ -16,8 +16,8 @@ class OpenGLFramebufferTexture(
glBindTexture(GL_TEXTURE_2D, id)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, null as ByteBuffer?)
//glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT24, size.x, size.y, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
}
override fun unload() {

View File

@ -22,5 +22,4 @@ uniform sampler2D uTexture;
void main() {
foutColor = texture(uTexture, finUV);
// foutColor = vec4(1.0f, 0.0f, 1.0f, 1.0f);
}

View File

@ -0,0 +1,25 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
#version 330 core
in vec2 finUV;
out vec4 foutColor;
uniform sampler2D uTexture;
void main() {
foutColor = texture(uTexture, finUV);
}

View File

@ -0,0 +1,24 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
#version 330 core
layout (location = 0) in vec2 vinPosition;
layout (location = 1) in vec2 vinUV;
out vec2 finUV;
void main() {
gl_Position = vec4(vinPosition, 0.0f, 1.0f);
finUV = vinUV;
}