Skip to content

Commit 1bba654

Browse files
committed
test: streaming to pbo
1 parent 7f35a62 commit 1bba654

File tree

20 files changed

+290
-107
lines changed

20 files changed

+290
-107
lines changed

common/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies {
2424
implementation("org.reflections:reflections:0.10.2")
2525
implementation("com.github.Edouard127:KDiscordIPC:$discordIPCVersion")
2626
implementation("com.pngencoder:pngencoder:0.15.0")
27+
implementation("org.jcodec:jcodec:0.2.3")
2728

2829
// Add Kotlin
2930
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinxCoroutinesVersion")

common/src/main/kotlin/com/lambda/graphics/RenderMain.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.lambda.event.events.TickEvent
77
import com.lambda.event.listener.SafeListener.Companion.listener
88
import com.lambda.graphics.animation.Animation.Companion.exp
99
import com.lambda.graphics.animation.AnimationTicker
10-
import com.lambda.graphics.buffer.FrameBuffer
10+
import com.lambda.graphics.buffer.fbo.FrameBuffer
1111
import com.lambda.graphics.gl.GlStateUtils.setupGL
1212
import com.lambda.graphics.gl.Matrices
1313
import com.lambda.graphics.gl.Matrices.resetMatrices

common/src/main/kotlin/com/lambda/graphics/buffer/vao/vertex/BufferUsage.kt renamed to common/src/main/kotlin/com/lambda/graphics/buffer/BufferUsage.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package com.lambda.graphics.buffer.vao.vertex
1+
package com.lambda.graphics.buffer
22

33
import com.lambda.graphics.gl.GLObject
44
import org.lwjgl.opengl.GL30C.GL_DYNAMIC_DRAW
55
import org.lwjgl.opengl.GL30C.GL_STATIC_DRAW
6+
import org.lwjgl.opengl.GL30C.GL_STREAM_DRAW
67

78
enum class BufferUsage(override val gl: Int) : GLObject {
89
STATIC(GL_STATIC_DRAW),
9-
DYNAMIC(GL_DYNAMIC_DRAW)
10-
}
10+
DYNAMIC(GL_DYNAMIC_DRAW),
11+
STREAM(GL_STREAM_DRAW);
12+
}

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

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.lambda.graphics.buffer
1+
package com.lambda.graphics.buffer.fbo
22
import com.lambda.Lambda.mc
33
import com.lambda.graphics.RenderMain
44
import com.lambda.graphics.buffer.vao.VAO
@@ -124,4 +124,4 @@ class FrameBuffer(private val depth: Boolean = false) {
124124
private val vao = VAO(VertexMode.TRIANGLES, VertexAttrib.Group.POS_UV)
125125
private var lastFrameBuffer: Int? = null
126126
}
127-
}
127+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.lambda.graphics.buffer.pbo
2+
3+
import com.lambda.graphics.buffer.BufferUsage
4+
import com.lambda.graphics.texture.TextureUtils.setupTexture
5+
import org.lwjgl.opengl.GL45C.*
6+
import java.nio.ByteBuffer
7+
import kotlin.system.measureNanoTime
8+
9+
class PixelBuffer(
10+
private val width: Int,
11+
private val height: Int,
12+
private val buffers: Int = 2,
13+
private val bufferUsage: BufferUsage = BufferUsage.DYNAMIC,
14+
) {
15+
private val pboIds = IntArray(buffers)
16+
private var writeIdx = 0 // Used to copy pixels from the PBO to the texture
17+
private var uploadIdx = 0 // Used to upload data to the PBO
18+
19+
fun mapTexture(id: Int, buffer: ByteBuffer) {
20+
val time = measureNanoTime {
21+
upload(buffer) { allocate ->
22+
// Bind the texture
23+
glBindTexture(GL_TEXTURE_2D, id)
24+
25+
if (allocate) {
26+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
27+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
28+
29+
// Allocate the texture memory
30+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0)
31+
}
32+
else {
33+
// Update the texture
34+
glTextureSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, 0)
35+
}
36+
}
37+
}
38+
39+
println("PBO => texture $id took $time nanoseconds")
40+
}
41+
42+
fun upload(data: ByteBuffer, process: (Boolean) -> Unit) {
43+
uploadIdx = (writeIdx + 1) % buffers
44+
45+
// Bind the next PBO to update pixel values
46+
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboIds[uploadIdx])
47+
48+
// Allocate the memory for the buffer
49+
process(true)
50+
51+
// Map the buffer into the memory
52+
val bufferData = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY)
53+
if (bufferData != null) {
54+
bufferData.put(data)
55+
56+
// Release the buffer
57+
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER)
58+
} else {
59+
println("Failed to map the PBO")
60+
}
61+
62+
// Bind the current PBO for writing
63+
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboIds[writeIdx])
64+
65+
// Copy the pixel values from the PBO to the texture
66+
process(false)
67+
68+
// Unbind the PBO
69+
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0)
70+
71+
println("Uploaded data to PBO $uploadIdx at $bufferData")
72+
73+
// Swap the indices
74+
writeIdx = uploadIdx
75+
}
76+
77+
// Called when no references to the object exist
78+
fun finalize() {
79+
// Delete the PBOs
80+
glDeleteBuffers(pboIds)
81+
}
82+
83+
init {
84+
if (buffers < 1) throw IllegalArgumentException("Buffers must be greater than or equal to 1")
85+
86+
// Generate the PBOs
87+
glGenBuffers(pboIds)
88+
89+
// Fill the buffers with null data to allocate the memory spaces
90+
repeat(buffers) {
91+
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboIds[it])
92+
glBufferData(GL_PIXEL_UNPACK_BUFFER, width * height * 4L, GL_STREAM_DRAW)
93+
}
94+
95+
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0) // Unbind the buffer
96+
}
97+
}

common/src/main/kotlin/com/lambda/graphics/buffer/vao/VAO.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.lambda.graphics.buffer.vao
22

3-
import com.lambda.graphics.buffer.vao.vertex.BufferUsage
3+
import com.lambda.graphics.buffer.BufferUsage
44
import com.lambda.graphics.buffer.vao.vertex.VertexAttrib
55
import com.lambda.graphics.buffer.vao.vertex.VertexMode
66
import com.lambda.graphics.gl.Matrices

common/src/main/kotlin/com/lambda/graphics/renderer/esp/ChunkedESP.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.lambda.event.events.TickEvent
55
import com.lambda.event.events.WorldEvent
66
import com.lambda.event.listener.SafeListener.Companion.concurrentListener
77
import com.lambda.event.listener.SafeListener.Companion.listener
8-
import com.lambda.graphics.buffer.vao.vertex.BufferUsage
8+
import com.lambda.graphics.buffer.BufferUsage
99
import com.lambda.graphics.renderer.esp.impl.ESPRenderer
1010
import com.lambda.graphics.renderer.esp.impl.StaticESPRenderer
1111
import com.lambda.module.modules.client.RenderSettings
@@ -126,4 +126,4 @@ class ChunkedESP private constructor(
126126
}
127127
}
128128
}
129-
}
129+
}

common/src/main/kotlin/com/lambda/graphics/renderer/esp/global/StaticESP.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.lambda.event.EventFlow.post
44
import com.lambda.event.events.RenderEvent
55
import com.lambda.event.events.TickEvent
66
import com.lambda.event.listener.SafeListener.Companion.listener
7-
import com.lambda.graphics.buffer.vao.vertex.BufferUsage
7+
import com.lambda.graphics.buffer.BufferUsage
88
import com.lambda.graphics.renderer.esp.impl.StaticESPRenderer
99

1010
object StaticESP : StaticESPRenderer(BufferUsage.DYNAMIC, false) {
@@ -15,4 +15,4 @@ object StaticESP : StaticESPRenderer(BufferUsage.DYNAMIC, false) {
1515
upload()
1616
}
1717
}
18-
}
18+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.lambda.graphics.renderer.esp.impl
22

3-
import com.lambda.graphics.buffer.vao.vertex.BufferUsage
3+
import com.lambda.graphics.buffer.BufferUsage
44

5-
open class DynamicESPRenderer : ESPRenderer(BufferUsage.DYNAMIC, true)
5+
open class DynamicESPRenderer : ESPRenderer(BufferUsage.DYNAMIC, true)

0 commit comments

Comments
 (0)