Skip to content

Commit 58095ea

Browse files
committed
Framebuffer optimization & misc
1 parent 2154044 commit 58095ea

File tree

7 files changed

+50
-40
lines changed

7 files changed

+50
-40
lines changed

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ import com.lambda.util.math.Vec2d
1212
import org.lwjgl.opengl.GL30.*
1313
import java.nio.IntBuffer
1414

15-
class FrameBuffer {
15+
class FrameBuffer(private val depth: Boolean = false) {
1616
private val fbo = glGenFramebuffers()
1717

1818
private val colorAttachment = glGenTextures()
19-
private val depthAttachment = glGenTextures()
19+
private val depthAttachment by lazy(::glGenTextures)
20+
21+
private val clearMask = if (!depth) GL_COLOR_BUFFER_BIT
22+
else GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
2023

2124
private var width = 0
2225
private var height = 0
@@ -75,6 +78,10 @@ class FrameBuffer {
7578
}
7679

7780
fun bindDepthTexture(slot: Int = 0): FrameBuffer {
81+
check(depth) {
82+
"Cannot bind depth texture of a non-depth framebuffer"
83+
}
84+
7885
bindTexture(depthAttachment, slot)
7986
return this
8087
}
@@ -91,12 +98,17 @@ class FrameBuffer {
9198
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, null as IntBuffer?)
9299
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorAttachment, 0)
93100

94-
setupBufferTexture(depthAttachment)
95-
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, null as IntBuffer?)
96-
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthAttachment, 0)
101+
if (depth) {
102+
setupBufferTexture(depthAttachment)
103+
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, null as IntBuffer?)
104+
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthAttachment, 0)
105+
}
106+
107+
glClearColor(0f, 0f, 0f, 0f)
108+
glClearDepth(1.0)
97109
}
98110

99-
clear()
111+
glClear(clearMask)
100112
}
101113

102114
private fun setupBufferTexture(id: Int) {
@@ -108,12 +120,6 @@ class FrameBuffer {
108120
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
109121
}
110122

111-
private fun clear() {
112-
glClearColor(0f, 0f, 0f, 0f)
113-
glClearDepth(1.0)
114-
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
115-
}
116-
117123
companion object {
118124
private val vao = VAO(VertexMode.TRIANGLES, VertexAttrib.Group.POS_UV)
119125
private var lastFrameBuffer: Int? = null

common/src/main/kotlin/com/lambda/gui/impl/AbstractClickGui.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ abstract class AbstractClickGui(name: String, owner: Module? = null) : LambdaGui
4747

4848
when (e) {
4949
is GuiEvent.Render -> {
50-
frameBuffer.write {
51-
windows.onEvent(e)
52-
}.read(shader) {
53-
it["u_Progress"] = childShowAnimation
50+
if (childShowAnimation < 0.99) {
51+
frameBuffer.write {
52+
windows.onEvent(e)
53+
}.read(shader) {
54+
it["u_Progress"] = childShowAnimation
55+
}
56+
57+
return
5458
}
55-
56-
return
5759
}
5860

5961
is GuiEvent.Show -> {

common/src/main/kotlin/com/lambda/gui/impl/hudgui/LambdaHudGui.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ object LambdaHudGui : AbstractClickGui("HudGui") {
4646
if (hoveredWindow == null &&
4747
e.action == Mouse.Action.Click &&
4848
e.button == Mouse.Button.Left
49-
) hudModules.firstOrNull { e.mouse in it.rect }?.let {
49+
) hudModules.filter(Module::isEnabled).firstOrNull { e.mouse in it.rect }?.let {
5050
dragInfo = e.mouse - it.position to it
5151
}
5252
}

common/src/main/kotlin/com/lambda/module/HudModule.kt

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.lambda.event.listener.SafeListener.Companion.listener
55
import com.lambda.gui.api.RenderLayer
66
import com.lambda.module.tag.ModuleTag
77
import com.lambda.util.KeyCode
8+
import com.lambda.util.math.MathUtils.coerceIn
89
import com.lambda.util.math.Rect
910
import com.lambda.util.math.Vec2d
1011

@@ -24,40 +25,38 @@ abstract class HudModule(
2425

2526
private var relativePosX by setting("Position X", 0.0, -10000.0..10000.0, 0.1) { false }
2627
private var relativePosY by setting("Position Y", 0.0, -10000.0..10000.0, 0.1) { false }
27-
2828
private var relativePos get() = Vec2d(relativePosX, relativePosY)
2929
set(value) { relativePosX = value.x; relativePosY = value.y }
3030

31-
var position get() = relativeToAbs(relativePos).let {
32-
val x = it.x.coerceIn(0.0, screenSize.x - width)
33-
val y = it.y.coerceIn(0.0, screenSize.y - height)
34-
Vec2d(x, y)
35-
}; set(value) {
36-
relativePos = absToRelative(value)
37-
autoDocking()
38-
}
31+
var position
32+
get() = relativeToAbs(relativePos).coerceIn(0.0, screenSize.x - width, 0.0, screenSize.y - height)
33+
set(value) { relativePos = absToRelative(value); if (autoDocking) autoDocking() }
3934

40-
private fun relativeToAbs(posIn: Vec2d) = posIn + (screenSize - size) * dockingMultiplier
41-
private fun absToRelative(posIn: Vec2d) = posIn - (screenSize - size) * dockingMultiplier
35+
private val dockingOffset get() = (screenSize - size) * Vec2d(dockingH.multiplier, dockingV.multiplier)
4236

43-
private val autoDocking by setting("Auto Docking", true)
37+
private fun relativeToAbs(posIn: Vec2d) = posIn + dockingOffset
38+
private fun absToRelative(posIn: Vec2d) = posIn - dockingOffset
4439

45-
private var dockingH by setting("Docking H", HAlign.LEFT).apply {
40+
private val autoDocking by setting("Auto Docking", true).apply {
41+
onValueChange { _, _ ->
42+
autoDocking()
43+
}
44+
}
45+
46+
private var dockingH by setting("Docking H", HAlign.LEFT) { !autoDocking }.apply {
4647
onValueChange { from, to ->
4748
val delta = to.multiplier - from.multiplier
4849
relativePosX += delta * (size.x - screenSize.x)
4950
}
5051
}
5152

52-
private var dockingV by setting("Docking V", VAlign.TOP).apply {
53+
private var dockingV by setting("Docking V", VAlign.TOP) { !autoDocking }.apply {
5354
onValueChange { from, to ->
5455
val delta = to.multiplier - from.multiplier
5556
relativePosY += delta * (size.y - screenSize.y)
5657
}
5758
}
5859

59-
private val dockingMultiplier get() = Vec2d(dockingH.multiplier, dockingV.multiplier)
60-
6160
val rect get() = Rect.basedOn(position, width, height)
6261

6362
private var screenSize = Vec2d.ZERO
@@ -79,8 +78,6 @@ abstract class HudModule(
7978
}
8079

8180
private fun autoDocking() {
82-
if (!autoDocking) return
83-
8481
val screenCenterX = (screenSize.x * 0.3333)..(screenSize.x * 0.6666)
8582
val screenCenterY = (screenSize.y * 0.3333)..(screenSize.y * 0.6666)
8683

common/src/main/kotlin/com/lambda/module/hud/Watermark.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.lambda.module.tag.ModuleTag
88

99
object Watermark : HudModule(
1010
name = "Watermark",
11-
defaultTags = setOf(ModuleTag.HUD_CLIENT),
11+
defaultTags = setOf(ModuleTag.CLIENT),
1212
) {
1313
private val shade by setting("Shade", true)
1414

common/src/main/kotlin/com/lambda/module/tag/ModuleTag.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ data class ModuleTag(override val name: String) : Nameable {
2727
val DEBUG = ModuleTag("Debug")
2828
val defaults = setOf(COMBAT, MOVEMENT, RENDER, PLAYER, NETWORK, DEBUG, CLIENT)
2929

30-
val HUD_CLIENT = ModuleTag("Client") // omg
31-
val hudDefaults = setOf(HUD_CLIENT)
30+
val hudDefaults = setOf(CLIENT)
3231

3332
// currently secondary tags
3433
val WORLD = ModuleTag("World")

common/src/main/kotlin/com/lambda/util/math/MathUtils.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,10 @@ object MathUtils {
145145
lerp(c1.b, c2.b, p).toFloat(),
146146
lerp(c1.a, c2.a, p).toFloat()
147147
)
148+
149+
fun Vec2d.coerceIn(minX: Double, maxX: Double, minY: Double, maxY: Double) =
150+
Vec2d(
151+
max(minX, min(x, maxX)),
152+
max(minY, min(y, maxY))
153+
)
148154
}

0 commit comments

Comments
 (0)