Skip to content

Commit e2314a7

Browse files
committed
Blur
1 parent a19203f commit e2314a7

File tree

19 files changed

+331
-141
lines changed

19 files changed

+331
-141
lines changed

common/src/main/kotlin/com/lambda/graphics/buffer/frame/FrameBuffer.kt

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ import org.lwjgl.opengl.GL30C.*
2727
import java.nio.IntBuffer
2828

2929
open class FrameBuffer(
30-
protected var width: Int = 1,
31-
protected var height: Int = 1,
30+
var width: Int = 1,
31+
var height: Int = 1,
3232
private val depth: Boolean = false
3333
) {
34-
private val fbo = glGenFramebuffers()
34+
val fbo = glGenFramebuffers()
3535

36-
private val colorAttachment = glGenTextures()
37-
private val depthAttachment by lazy(::glGenTextures)
36+
val colorAttachment = glGenTextures()
37+
val depthAttachment by lazy(::glGenTextures)
3838

3939
private val clearMask = if (!depth) GL_COLOR_BUFFER_BIT
4040
else GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
@@ -57,8 +57,18 @@ open class FrameBuffer(
5757
return this
5858
}
5959

60-
private fun update() {
61-
if (width == lastWidth && height == lastWidth) {
60+
fun bind() {
61+
glBindFramebuffer(GL_FRAMEBUFFER, fbo)
62+
}
63+
64+
fun updateScreenSized() {
65+
width = mc.framebuffer.viewportWidth
66+
height = mc.framebuffer.viewportHeight
67+
update()
68+
}
69+
70+
fun update() {
71+
if (width == lastWidth && height == lastHeight) {
6272
glClear(clearMask)
6373
return
6474
}
@@ -114,5 +124,9 @@ open class FrameBuffer(
114124
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
115125
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
116126
}
127+
128+
fun unbind() {
129+
glBindFramebuffer(GL_FRAMEBUFFER, mc.framebuffer.fbo)
130+
}
117131
}
118132
}

common/src/main/kotlin/com/lambda/graphics/buffer/vertex/attributes/VertexAttrib.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ sealed class VertexAttrib(
7777
Vec3, Vec2, Color
7878
)
7979

80-
object RECT_OUTLINE : Group(
81-
Vec3, Vec2, Float, Color
80+
object BLUR : Group(
81+
Vec2, Vec2
8282
)
8383

8484
// WORLD

common/src/main/kotlin/com/lambda/graphics/gl/GlStateUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ object GlStateUtils {
8888
field.set(flag)
8989
}
9090

91-
private fun blend(flag: Boolean) {
91+
fun blend(flag: Boolean) {
9292
if (flag) {
9393
glEnable(GL_BLEND)
9494
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

common/src/main/kotlin/com/lambda/graphics/pipeline/VertexPipeline.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class VertexPipeline(
3939
) {
4040
private val vao = VertexArray(vertexMode, attributes)
4141
private val vbo = PersistentBuffer(GL_ARRAY_BUFFER, attributes.stride)
42-
private val ibo = PersistentBuffer(GL_ELEMENT_ARRAY_BUFFER, 4)
42+
private val ibo = PersistentBuffer(GL_ELEMENT_ARRAY_BUFFER, UInt.SIZE_BYTES)
4343

4444
/**
4545
* Direct access to the vertex buffer's underlying byte storage

common/src/main/kotlin/com/lambda/graphics/renderer/gui/AbstractGUIRenderer.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ open class AbstractGUIRenderer(
6363
shader["u_ShadeColor1"] = primaryColor
6464
shader["u_ShadeColor2"] = secondaryColor
6565

66-
shader["u_ShadeSize"] = RenderMain.screenSize / Vec2d(GuiSettings.colorWidth, GuiSettings.colorHeight)
66+
var size = RenderMain.screenSize / Vec2d(GuiSettings.colorWidth, GuiSettings.colorHeight)
67+
//if (this is FontRenderer) size *= 5.0
68+
shader["u_ShadeSize"] = size
6769
}
6870

6971
pipeline.apply {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.graphics.renderer.gui
19+
20+
import com.lambda.Lambda.mc
21+
import com.lambda.event.events.TickEvent
22+
import com.lambda.event.listener.SafeListener.Companion.listen
23+
import com.lambda.graphics.buffer.frame.FrameBuffer
24+
import com.lambda.graphics.buffer.vertex.attributes.VertexAttrib
25+
import com.lambda.graphics.buffer.vertex.attributes.VertexMode
26+
import com.lambda.graphics.gl.GlStateUtils
27+
import com.lambda.graphics.pipeline.VertexPipeline
28+
import com.lambda.graphics.shader.Shader.Companion.shader
29+
import com.lambda.graphics.texture.TextureUtils.bindTexture
30+
import com.lambda.util.math.Rect
31+
import com.lambda.util.math.Vec2d
32+
import org.lwjgl.opengl.GL30C.GL_FRAMEBUFFER
33+
import org.lwjgl.opengl.GL30C.glBindFramebuffer
34+
35+
// ToDo: Improve blur
36+
// Round Corners
37+
// Boost brightness
38+
// BlurRect layout impl
39+
object BlurRenderer {
40+
private val fbo1 = FrameBuffer()
41+
private val fbo2 = FrameBuffer()
42+
private val base get() = mc.framebuffer
43+
44+
private val vao = VertexPipeline(VertexMode.TRIANGLES, VertexAttrib.Group.BLUR)
45+
46+
private val hShader = shader("post/gaussian_h")
47+
private val vShader = shader("post/gaussian_v")
48+
49+
fun blur(rect: Rect, iterations: Int) {
50+
if (iterations <= 0) return
51+
52+
vao.upload {
53+
val p1 = rect.leftTop
54+
val p2 = rect.rightBottom
55+
56+
buildQuad(
57+
vertex {
58+
vec2(p1.x, p1.y).vec2(0.0, 0.0)
59+
},
60+
vertex {
61+
vec2(p1.x, p2.y).vec2(0.0, 1.0)
62+
},
63+
vertex {
64+
vec2(p2.x, p2.y).vec2(1.0, 1.0)
65+
},
66+
vertex {
67+
vec2(p2.x, p1.y).vec2(1.0, 0.0)
68+
}
69+
)
70+
}
71+
72+
val i = iterations.toDouble()
73+
74+
GlStateUtils.blend(false)
75+
render(null, fbo1, false, i - 1.0, i)
76+
77+
repeat(iterations - 1) {
78+
render(fbo1, fbo2, true , i - it, i - it)
79+
render(fbo2, fbo1, false, i-it-1, i - it)
80+
}
81+
82+
render(fbo1, null, true)
83+
GlStateUtils.blend(true)
84+
vao.end()
85+
}
86+
87+
init {
88+
listen<TickEvent.Render.Post>(alwaysListen = true) {
89+
vao.sync()
90+
}
91+
}
92+
93+
private fun render(
94+
color: FrameBuffer?,
95+
frameBuffer: FrameBuffer?,
96+
vertical: Boolean,
97+
extendX: Double = 0.0,
98+
extendY: Double = 0.0
99+
) {
100+
color?.bindColorTexture() ?: bindTexture(base.colorAttachment)
101+
102+
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer?.fbo ?: base.fbo)
103+
frameBuffer?.updateScreenSized()
104+
105+
(if (vertical) vShader else hShader).let { shader ->
106+
shader.use()
107+
shader["u_TexelSize"] = Vec2d(1.0 / base.viewportWidth, 1.0 / base.viewportHeight)
108+
shader["u_Extend"] = Vec2d(extendX, extendY)
109+
// if (vertical) shader["u_Final"] = frameBuffer == null
110+
}
111+
112+
vao.render()
113+
}
114+
}

common/src/main/kotlin/com/lambda/graphics/renderer/gui/FontRenderer.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ object FontRenderer : AbstractGUIRenderer(VertexAttrib.Group.FONT, shader("rende
6161
color: Color = Color.WHITE,
6262
scale: Double = ClickGui.fontScale,
6363
shadow: Boolean = true,
64-
parseEmoji: Boolean = LambdaMoji.isEnabled
65-
) = render {
64+
parseEmoji: Boolean = LambdaMoji.isEnabled,
65+
shade: Boolean = false
66+
) = render(shade) {
6667
shader["u_FontTexture"] = 0
6768
shader["u_EmojiTexture"] = 1
6869
shader["u_SDFMin"] = RenderSettings.sdfMin
@@ -90,7 +91,8 @@ object FontRenderer : AbstractGUIRenderer(VertexAttrib.Group.FONT, shader("rende
9091
position: Vec2d,
9192
color: Color = Color.WHITE,
9293
scale: Double = ClickGui.fontScale,
93-
) = render {
94+
shade: Boolean = false
95+
) = render(shade) {
9496
shader["u_FontTexture"] = 0
9597
shader["u_EmojiTexture"] = 1
9698
shader["u_SDFMin"] = RenderSettings.sdfMin

common/src/main/kotlin/com/lambda/graphics/renderer/gui/RectRenderer.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ object RectRenderer {
7171

7272
outline.putRect(
7373
rect,
74-
width * 0.5,
74+
width * 0.25,
7575
shade,
7676
leftTopRadius,
7777
rightTopRadius,
@@ -154,7 +154,7 @@ object RectRenderer {
154154
val pos2 = rect.rightBottom
155155

156156
val expand = expandIn.coerceAtLeast(0.0) + 1
157-
val smoothing = 0.5
157+
val smoothing = 0.3
158158

159159
val p1 = pos1 - expand - smoothing
160160
val p2 = pos2 + expand + smoothing
@@ -192,16 +192,16 @@ object RectRenderer {
192192
upload {
193193
buildQuad(
194194
vertex {
195-
vec3m(p1.x, p1.y, 0.0).vec2(uv1.x, uv1.y).color(leftTop)
195+
vec3m(p1.x, p1.y).vec2(uv1.x, uv1.y).color(leftTop)
196196
},
197197
vertex {
198-
vec3m(p1.x, p2.y, 0.0).vec2(uv1.x, uv2.y).color(leftBottom)
198+
vec3m(p1.x, p2.y).vec2(uv1.x, uv2.y).color(leftBottom)
199199
},
200200
vertex {
201-
vec3m(p2.x, p2.y, 0.0).vec2(uv2.x, uv2.y).color(rightBottom)
201+
vec3m(p2.x, p2.y).vec2(uv2.x, uv2.y).color(rightBottom)
202202
},
203203
vertex {
204-
vec3m(p2.x, p1.y, 0.0).vec2(uv2.x, uv1.y).color(rightTop)
204+
vec3m(p2.x, p1.y).vec2(uv2.x, uv1.y).color(rightTop)
205205
}
206206
)
207207
}

common/src/main/kotlin/com/lambda/graphics/shader/Shader.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.lambda.graphics.shader.ShaderUtils.uniformMatrix
2424
import com.lambda.util.math.Vec2d
2525
import it.unimi.dsi.fastutil.objects.Object2IntMap
2626
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap
27+
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap
2728
import net.minecraft.util.math.Vec3d
2829
import org.joml.Matrix4f
2930
import org.lwjgl.opengl.GL20C.*
@@ -49,13 +50,9 @@ class Shader private constructor(name: String) {
4950
}
5051

5152
private fun loc(name: String) =
52-
if (uniformCache.containsKey(name))
53-
uniformCache.getInt(name)
54-
else
55-
glGetUniformLocation(id, name).let { location ->
56-
uniformCache.put(name, location)
57-
location
58-
}
53+
uniformCache.getOrPut(name) {
54+
glGetUniformLocation(id, name)
55+
}
5956

6057
operator fun set(name: String, v: Boolean) =
6158
glUniform1i(loc(name), if (v) 1 else 0)
@@ -85,7 +82,7 @@ class Shader private constructor(name: String) {
8582
uniformMatrix(loc(name), mat)
8683

8784
companion object {
88-
private val shaderCache = hashMapOf<String, Shader>()
85+
private val shaderCache = Object2ObjectOpenHashMap<String, Shader>()
8986

9087
fun shader(path: String) =
9188
shaderCache.getOrPut(path) {

common/src/main/kotlin/com/lambda/gui/component/DockingRect.kt

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)