Skip to content

Commit 687dec0

Browse files
committed
Optimizatio
1 parent 1cf79c0 commit 687dec0

File tree

27 files changed

+534
-494
lines changed

27 files changed

+534
-494
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import com.lambda.graphics.gl.Matrices.resetMatrices
2828
import com.lambda.graphics.renderer.esp.global.DynamicESP
2929
import com.lambda.graphics.renderer.esp.global.StaticESP
3030
import com.lambda.module.modules.client.GuiSettings
31-
import com.lambda.util.Communication.info
3231
import com.lambda.util.math.Vec2d
3332
import com.mojang.blaze3d.systems.RenderSystem.getProjectionMatrix
3433
import org.joml.Matrix4f

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ abstract class Buffer(
3636
* Edge case to handle vertex arrays
3737
*/
3838
val isVertexArray: Boolean = false,
39+
val validate: Boolean = true
3940
) {
4041
/**
4142
* Specifies how the buffers are used
@@ -105,7 +106,19 @@ abstract class Buffer(
105106
/**
106107
* Index of the current buffer.
107108
*/
108-
var index: Int = 0; private set
109+
private var index: Int = 0; private set(value) {
110+
if (field == value) return
111+
field = value
112+
id = bufferIds[value]
113+
}
114+
115+
/**
116+
* ID of the current buffer.
117+
*/
118+
var id: Int = 0; get() {
119+
if (field == 0) field = bufferIds[0]
120+
return field
121+
} private set
109122

110123
/**
111124
* List of all the buffers.
@@ -129,7 +142,7 @@ abstract class Buffer(
129142
/**
130143
* Binds current the buffer [index] to the [target].
131144
*/
132-
fun bind() = bind(bufferIds[index])
145+
fun bind() = bind(id)
133146

134147
/**
135148
* Swaps the buffer [index] if [buffers] is greater than 1.
@@ -321,6 +334,8 @@ abstract class Buffer(
321334
abstract fun upload(data: ByteBuffer, offset: Long)
322335

323336
private fun validate() {
337+
if (!validate) return
338+
324339
check(usage in GL_STREAM_DRAW..GL_DYNAMIC_COPY)
325340
{ "Usage is invalid, refer to the documentation table." }
326341

@@ -363,7 +378,7 @@ abstract class Buffer(
363378
var lastIbo = 0
364379
var prevIbo = 0
365380

366-
fun createPipelineBuffer(bufferTarget: Int) = object : Buffer(buffers = 1) {
381+
fun createPipelineBuffer(bufferTarget: Int) = object : Buffer(buffers = 1, validate = false) {
367382
override val target: Int = bufferTarget
368383

369384
override val usage: Int = GL_STATIC_DRAW

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,33 @@ package com.lambda.graphics.buffer.vertex
2020
import com.lambda.graphics.buffer.Buffer
2121
import com.lambda.graphics.buffer.vertex.attributes.VertexAttrib
2222
import com.lambda.graphics.buffer.vertex.attributes.VertexMode
23+
import com.lambda.graphics.pipeline.PersistentBuffer
2324
import net.minecraft.client.render.BufferRenderer
2425
import org.lwjgl.opengl.GL30C.*
2526
import org.lwjgl.opengl.GL32C.glDrawElementsBaseVertex
2627
import java.nio.ByteBuffer
2728

2829
class VertexArray(
29-
private val vertexMode: VertexMode,
30-
private val attributes: VertexAttrib.Group
30+
val vertexMode: VertexMode,
31+
val attributes: VertexAttrib.Group
3132
) : Buffer(isVertexArray = true) {
3233
override val usage: Int = -1
3334
override val target: Int = -1
3435
override val access: Int = -1
3536

36-
fun render(
37+
private var linkedVBO: PersistentBuffer? = null
38+
39+
fun renderIndices(
40+
ibo: PersistentBuffer
41+
) = linkedVBO?.let { vbo ->
42+
renderInternal(
43+
indicesSize = ibo.byteBuffer.bytesPut - ibo.uploadOffset,
44+
indicesPointer = ibo.byteBuffer.pointer + ibo.uploadOffset,
45+
verticesOffset = vbo.uploadOffset
46+
)
47+
} ?: throw IllegalStateException("Unable to use vertex array without having a VBO linked to it.")
48+
49+
private fun renderInternal(
3750
indicesSize: Long,
3851
indicesPointer: Long,
3952
verticesOffset: Int
@@ -47,6 +60,17 @@ class VertexArray(
4760
)
4861
}
4962

63+
fun linkVbo(vbo: PersistentBuffer, block: VertexArray.() -> Unit = { }) {
64+
linkedVBO = vbo
65+
66+
bind {
67+
vbo.use {
68+
attributes.link()
69+
block(this@VertexArray)
70+
}
71+
}
72+
}
73+
5074
override fun map(size: Long, offset: Long, block: (ByteBuffer) -> Unit) = throw UnsupportedOperationException()
5175
override fun upload(data: ByteBuffer, offset: Long) = throw UnsupportedOperationException()
5276

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

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,34 @@ import org.lwjgl.opengl.GL11C.GL_FLOAT
2121
import org.lwjgl.opengl.GL11C.GL_UNSIGNED_BYTE
2222
import org.lwjgl.opengl.GL20C.glEnableVertexAttribArray
2323
import org.lwjgl.opengl.GL20C.glVertexAttribPointer
24-
import org.lwjgl.opengl.GL33.glVertexAttribDivisor
2524

2625
sealed class VertexAttrib(
2726
private val componentCount: Int,
2827
componentSize: Int,
2928
private val normalized: Boolean,
30-
private val single: Boolean,
3129
private val type: Int
3230
) {
3331
open class Float(
34-
normalized: Boolean = false, single: Boolean = false
35-
) : VertexAttrib(1, 4, normalized, single, GL_FLOAT) {
32+
normalized: Boolean = false
33+
) : VertexAttrib(1, 4, normalized, GL_FLOAT) {
3634
companion object : Float()
3735
}
3836

3937
open class Vec2(
40-
normalized: Boolean = false, single: Boolean = false
41-
) : VertexAttrib(2, 4, normalized, single, GL_FLOAT) {
38+
normalized: Boolean = false
39+
) : VertexAttrib(2, 4, normalized, GL_FLOAT) {
4240
companion object : Vec2()
4341
}
4442

4543
open class Vec3(
46-
normalized: Boolean = false, single: Boolean = false
47-
) : VertexAttrib(3, 4, normalized, single, GL_FLOAT) {
44+
normalized: Boolean = false
45+
) : VertexAttrib(3, 4, normalized, GL_FLOAT) {
4846
companion object : Vec3()
4947
}
5048

5149
open class Color(
52-
normalized: Boolean = true, single: Boolean = false
53-
) : VertexAttrib(4, 1, normalized, single, GL_UNSIGNED_BYTE) {
50+
normalized: Boolean = true
51+
) : VertexAttrib(4, 1, normalized, GL_UNSIGNED_BYTE) {
5452
companion object : Color()
5553
}
5654

@@ -59,7 +57,6 @@ sealed class VertexAttrib(
5957
fun link(index: Int, pointer: Long, stride: Int) {
6058
glEnableVertexAttribArray(index)
6159
glVertexAttribPointer(index, componentCount, type, normalized, stride, pointer)
62-
if (single) glVertexAttribDivisor(index, 1)
6360
}
6461

6562
@Suppress("ClassName")
@@ -70,15 +67,7 @@ sealed class VertexAttrib(
7067

7168
// GUI
7269
object FONT : Group(
73-
Vec3, Vec2, Color
74-
)
75-
76-
object RECT : Group(
77-
Vec3, Vec2, Color
78-
)
79-
80-
object BLUR : Group(
81-
Vec2, Vec2
70+
Vec2, Vec2, Color
8271
)
8372

8473
// WORLD

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,33 @@
1818
package com.lambda.graphics.pipeline
1919

2020
import com.lambda.graphics.buffer.Buffer.Companion.createPipelineBuffer
21+
import com.lambda.graphics.buffer.DynamicByteBuffer
2122
import com.lambda.graphics.buffer.DynamicByteBuffer.Companion.dynamicByteBuffer
2223
import com.lambda.graphics.gl.kibibyte
2324
import org.lwjgl.system.MemoryUtil.memCopy
24-
import java.nio.ByteBuffer
25+
import sun.misc.Unsafe
2526

2627
/**
2728
* Represents a persistent dynamic coherent buffer for fast opengl rendering purposes
2829
*/
2930
class PersistentBuffer(
30-
target: Int, stride: Int
31+
target: Int, stride: Int, initialSize: Int = 1.kibibyte
3132
) {
3233
/**
3334
* Resizable byte buffer that stores all data used last frame
3435
*/
35-
val byteBuffer = dynamicByteBuffer(stride * 1.kibibyte)
36+
val byteBuffer = dynamicByteBuffer(stride * initialSize)
3637

3738
/**
3839
* Data that has passed through the buffer within previous frame
3940
*/
4041
private val snapshot = dynamicByteBuffer(1)
41-
private var cacheSize = 0
42+
private var snapshotData = 0L
4243

4344
/**
4445
* Represents a gpu-side buffer
4546
*/
46-
private val glBuffer = createPipelineBuffer(target)
47+
val glBuffer = createPipelineBuffer(target)
4748
private var glSize = 0
4849

4950
var uploadOffset = 0
@@ -58,19 +59,12 @@ class PersistentBuffer(
5859

5960
glBuffer.allocate(byteBuffer.data)
6061
snapshot.realloc(byteBuffer.capacity)
61-
cacheSize = 0
62+
snapshotData = 0
6263
return
64+
}
6365

64-
/* TODO:
65-
Cache data in range min(snapshot.capacity, byteBuffer.bytesPut)
66-
and force upload after byteBuffer.bytesPut
67-
*/
68-
} else if (cacheSize > 0 && snapshot.capacity >= byteBuffer.bytesPut) {
69-
// TODO: precise compare-mapping to minimize uploaded data
70-
// Split data by chunks of modified regions and upload them only
71-
// Might be useful in cases when position updates but uv/color/etc doesn't
72-
// Might be not...
73-
if (memcmp(snapshot.data, byteBuffer.data, uploadOffset, dataCount.toInt())) return
66+
if (snapshotData > 0 && snapshot.capacity >= byteBuffer.bytesPut) {
67+
if (memcmp(snapshot, byteBuffer, uploadOffset, dataCount.toInt())) return
7468
}
7569

7670
glBuffer.update(uploadOffset.toLong(), dataCount, dataStart)
@@ -82,7 +76,7 @@ class PersistentBuffer(
8276

8377
fun sync() {
8478
memCopy(byteBuffer.pointer, snapshot.pointer, byteBuffer.bytesPut)
85-
cacheSize = byteBuffer.bytesPut.toInt()
79+
snapshotData = byteBuffer.bytesPut
8680

8781
byteBuffer.resetPosition()
8882
uploadOffset = 0
@@ -92,17 +86,26 @@ class PersistentBuffer(
9286
snapshot.resetPosition()
9387
byteBuffer.resetPosition()
9488
uploadOffset = 0
95-
cacheSize = 0
89+
snapshotData = 0
9690
}
9791

9892
fun use(block: () -> Unit) = glBuffer.bind { block() }
9993

100-
private fun memcmp(a: ByteBuffer, b: ByteBuffer, pointer: Int, size: Int): Boolean {
101-
for (i in pointer..<(pointer + size)) {
102-
if (a[i] != b[i]) {
94+
private fun memcmp(a: DynamicByteBuffer, b: DynamicByteBuffer, position: Int, size: Int): Boolean {
95+
for (i in position..<(position + size)) {
96+
if (UNSAFE.getByte(null, a.pointer + i) !=
97+
UNSAFE.getByte(null, b.pointer + i)) {
10398
return false
10499
}
105100
}
106101
return true
107102
}
103+
104+
companion object {
105+
private val UNSAFE = run {
106+
val unsafeField = Unsafe::class.java.getDeclaredField("theUnsafe")
107+
unsafeField.setAccessible(true)
108+
unsafeField.get(null) as Unsafe
109+
}
110+
}
108111
}

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import org.joml.Vector4d
2929
class VertexBuilder(
3030
private val direct: VertexPipeline? = null
3131
) {
32-
private val vertices by lazy { mutableListOf<Attribute>() }
33-
private val indices by lazy { mutableListOf<Int>() }
32+
val vertices by lazy { mutableListOf<Attribute>() }
33+
val indices by lazy { mutableListOf<Int>() }
3434

3535
private var verticesCounter = 0
3636

@@ -97,9 +97,8 @@ class VertexBuilder(
9797
fun collect(vararg indices: Int) =
9898
indices
9999

100-
fun use(block: VertexBuilder.() -> Unit) {
100+
fun use(block: VertexBuilder.() -> Unit) =
101101
apply(block)
102-
}
103102

104103
/**
105104
* Creates a new vertex with specified attributes
@@ -120,14 +119,19 @@ class VertexBuilder(
120119
"Builder is already associated with a rendering pipeline. Cannot upload data again."
121120
}
122121

123-
/* Upload vertices */
122+
uploadVertices(pipeline.vertices)
123+
uploadIndices(pipeline.indices)
124+
}
125+
126+
fun uploadVertices(buffer: DynamicByteBuffer) {
124127
vertices.forEach { attribute ->
125-
attribute.upload(pipeline.vertices)
128+
attribute.upload(buffer)
126129
}
130+
}
127131

128-
/* Upload indices */
132+
fun uploadIndices(buffer: DynamicByteBuffer) {
129133
indices.forEach {
130-
pipeline.indices.putInt(it)
134+
buffer.putInt(it)
131135
}
132136
}
133137

@@ -264,4 +268,4 @@ class VertexBuilder(
264268
var color: java.awt.Color
265269
) : Attribute({ putColor(color) })
266270
}
267-
}
271+
}

0 commit comments

Comments
 (0)