Skip to content

Commit d080489

Browse files
committed
BlockESP, Threading rework
1 parent 469a44c commit d080489

File tree

20 files changed

+358
-437
lines changed

20 files changed

+358
-437
lines changed

common/src/main/kotlin/com/lambda/Lambda.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@ object Lambda {
4242
.registerTypeAdapter(GameProfile::class.java, GameProfileSerializer)
4343
.create()
4444

45-
fun initialize() = Loader.initialize()
45+
fun initialize() {
46+
recordRenderCall {
47+
Loader.initialize()
48+
}
49+
}
4650
}

common/src/main/kotlin/com/lambda/event/events/RenderEvent.kt

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,19 @@ import com.lambda.Lambda.mc
44
import com.lambda.event.Event
55
import com.lambda.event.callback.Cancellable
66
import com.lambda.event.callback.ICancellable
7-
import com.lambda.graphics.renderer.esp.DirectionMask
8-
import com.lambda.graphics.renderer.esp.EntityEspRenderer
7+
import com.lambda.graphics.renderer.esp.global.BlockESPRenderer
8+
import com.lambda.graphics.renderer.esp.global.EntityESPRenderer
99
import com.lambda.util.math.Vec2d
10-
import net.minecraft.entity.Entity
11-
import java.awt.Color
1210

1311
abstract class RenderEvent : Event {
1412
class World : RenderEvent()
1513

14+
class BlockESP : RenderEvent() {
15+
val renderer = BlockESPRenderer
16+
}
17+
1618
class EntityESP : RenderEvent() {
17-
fun build(
18-
entity: Entity,
19-
filledColor: Color,
20-
outlineColor: Color,
21-
sides: Int = DirectionMask.ALL,
22-
outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR
23-
) {
24-
buildFilled(entity, filledColor, sides)
25-
buildOutline(entity, outlineColor, sides, outlineMode)
26-
}
27-
28-
fun buildFilled(
29-
entity: Entity,
30-
color: Color,
31-
sides: Int = DirectionMask.ALL
32-
) = EntityEspRenderer.buildFilled(entity, color, sides)
33-
34-
fun buildOutline(
35-
entity: Entity,
36-
color: Color,
37-
sides: Int = DirectionMask.ALL,
38-
outlineMode: DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR
39-
) = EntityEspRenderer.buildOutline(entity, color, sides, outlineMode)
19+
val renderer = EntityESPRenderer
4020
}
4121

4222
abstract class GUI(val scale: Double) : RenderEvent() {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import com.lambda.graphics.gl.GlStateUtils.setupGL
77
import com.lambda.graphics.gl.Matrices
88
import com.lambda.graphics.gl.Matrices.resetMatrix
99
import com.lambda.graphics.gl.Matrices.translate
10-
import com.lambda.graphics.renderer.esp.EntityEspRenderer
10+
import com.lambda.graphics.renderer.esp.global.BlockESPRenderer
11+
import com.lambda.graphics.renderer.esp.global.EntityESPRenderer
1112
import com.lambda.module.modules.client.GuiSettings
1213
import com.lambda.util.math.Vec2d
1314
import com.mojang.blaze3d.systems.RenderSystem.getProjectionMatrix
@@ -41,7 +42,8 @@ object RenderMain {
4142

4243
setupGL {
4344
RenderEvent.World().post()
44-
EntityEspRenderer.render()
45+
BlockESPRenderer.render()
46+
EntityESPRenderer.render()
4547
}
4648
}
4749

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,20 @@ import java.nio.ByteBuffer
2828
class VAO(
2929
private val vertexMode: VertexMode,
3030
attribGroup: VertexAttrib.Group,
31-
private val bufferUsage: BufferUsage = BufferUsage.DYNAMIC,
32-
initializeInstantly: Boolean = false
31+
private val bufferUsage: BufferUsage = BufferUsage.DYNAMIC
3332
) : IRenderContext {
3433
private var vao = 0
3534
private var vbo = 0
3635
private var ibo = 0
3736

3837
private val objectSize: Int
3938

40-
private lateinit var vertices: ByteBuffer
41-
private var verticesPointer = 0L
42-
private var verticesPosition = 0L
39+
private var vertices: ByteBuffer
40+
private var verticesPointer: Long
41+
private var verticesPosition: Long
4342

44-
private lateinit var indices: ByteBuffer
45-
private var indicesPointer = 0L
43+
private var indices: ByteBuffer
44+
private var indicesPointer: Long
4645
private var indicesCount = 0
4746
private var uploadedIndices = 0
4847

@@ -52,20 +51,9 @@ class VAO(
5251
val stride = attribGroup.stride
5352
objectSize = stride * vertexMode.indicesCount
5453

55-
if (initializeInstantly) {
56-
initialize(attribGroup, stride)
57-
} else {
58-
runGameScheduled {
59-
initialize(attribGroup, stride)
60-
}
61-
}
62-
}
63-
64-
private fun initialize(attribGroup: VertexAttrib.Group, stride: Int) {
6554
vertices = byteBuffer(objectSize * 256 * 4)
6655
verticesPointer = address(vertices)
6756
verticesPosition = verticesPointer
68-
6957
indices = byteBuffer(vertexMode.indicesCount * 512 * 4)
7058
indicesPointer = address(indices)
7159

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ 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
89
import com.lambda.module.modules.client.RenderSettings
9-
import com.lambda.threading.runGameBlocking
10+
import com.lambda.threading.awaitMainThread
1011
import net.minecraft.util.math.ChunkPos
1112
import net.minecraft.world.WorldView
1213
import net.minecraft.world.chunk.WorldChunk
@@ -15,17 +16,18 @@ import java.util.concurrent.ConcurrentLinkedDeque
1516

1617
class ChunkedESP private constructor(
1718
owner: Any,
18-
private val update: EspRenderer.(WorldView, Int, Int, Int) -> Unit
19+
private val update: ESPRenderer.(WorldView, Int, Int, Int) -> Unit
1920
) {
2021
private val rendererMap = ConcurrentHashMap<Long, EspChunk>()
2122
private val WorldChunk.renderer get() = rendererMap.getOrPut(pos.toLong()) {
2223
EspChunk(this, this@ChunkedESP)
2324
}
24-
private var ticks = 0
2525

2626
private val uploadQueue = ConcurrentLinkedDeque<() -> Unit>()
2727
private val rebuildQueue = ConcurrentLinkedDeque<EspChunk>()
2828

29+
private var ticks = 0
30+
2931
fun rebuild() {
3032
rebuildQueue.clear()
3133
rebuildQueue.addAll(rendererMap.values)
@@ -74,14 +76,15 @@ class ChunkedESP private constructor(
7476

7577
companion object {
7678
fun Any.newChunkedESP(
77-
update: EspRenderer.(WorldView, Int, Int, Int) -> Unit
79+
update: ESPRenderer.(WorldView, Int, Int, Int) -> Unit
7880
) = ChunkedESP(this, update)
7981
}
8082

8183
private class EspChunk(val chunk: WorldChunk, val owner: ChunkedESP) {
82-
var renderer: EspRenderer? = null
84+
var renderer: ESPRenderer? = null
8385

8486
private val chunkOffsets = listOf(1 to 0, 0 to 1, -1 to 0, 0 to -1)
87+
8588
val neighbors = chunkOffsets.map {
8689
ChunkPos(chunk.pos.x + it.first, chunk.pos.z + it.second)
8790
}.toTypedArray()
@@ -97,8 +100,8 @@ class ChunkedESP private constructor(
97100
}
98101

99102
suspend fun rebuild() {
100-
val newRenderer = runGameBlocking {
101-
EspRenderer()
103+
val newRenderer = awaitMainThread {
104+
ESPRenderer(BufferUsage.STATIC)
102105
}
103106

104107
iterateChunk { x, y, z ->

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.lambda.graphics.renderer.esp
22

3+
import com.lambda.util.BlockUtils.blockState
4+
import net.minecraft.block.BlockState
5+
import net.minecraft.util.math.BlockPos
36
import net.minecraft.util.math.Direction
7+
import net.minecraft.world.BlockView
48

59
object DirectionMask {
610
const val EAST = 1 // X +
@@ -18,6 +22,16 @@ object DirectionMask {
1822
fun Int.exclude(dir: Int) = this xor dir
1923
fun Int.hasDirection(dir: Int) = (this and dir) != 0
2024

25+
fun buildSideMesh(blockPos: BlockPos, filter: (BlockPos) -> Boolean): Int {
26+
var sides = ALL
27+
28+
Direction.entries
29+
.filter { filter(blockPos.offset(it)) }
30+
.forEach { sides = sides.exclude(it.mask) }
31+
32+
return sides
33+
}
34+
2135
val Direction.mask get() = when (this) {
2236
Direction.DOWN -> DOWN
2337
Direction.UP -> UP
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.lambda.graphics.renderer.esp
2+
3+
import com.lambda.Lambda.mc
4+
import com.lambda.graphics.buffer.vao.IRenderContext
5+
import com.lambda.graphics.buffer.vao.VAO
6+
import com.lambda.graphics.buffer.vao.vertex.BufferUsage
7+
import com.lambda.graphics.buffer.vao.vertex.VertexAttrib
8+
import com.lambda.graphics.buffer.vao.vertex.VertexMode
9+
import com.lambda.graphics.gl.GlStateUtils.withFaceCulling
10+
import com.lambda.graphics.gl.GlStateUtils.withLineWidth
11+
import com.lambda.graphics.shader.Shader
12+
import com.lambda.module.modules.client.RenderSettings
13+
import java.awt.Color
14+
import java.util.concurrent.ConcurrentHashMap
15+
16+
open class ESPRenderer(
17+
usage: BufferUsage
18+
) {
19+
val faces = VAO(VertexMode.TRIANGLES, VertexAttrib.Group.STATIC_RENDERER, usage)
20+
val faceVertices = ConcurrentHashMap<Vertex, Int>()
21+
22+
val outlines = VAO(VertexMode.LINES, VertexAttrib.Group.STATIC_RENDERER, usage)
23+
val outlineVertices = ConcurrentHashMap<Vertex, Int>()
24+
25+
var updateFaces = false
26+
var updateOutlines = false
27+
28+
fun upload() {
29+
if (updateFaces) {
30+
updateFaces = false
31+
faces.upload()
32+
}
33+
34+
if (updateOutlines) {
35+
updateOutlines = false
36+
outlines.upload()
37+
}
38+
}
39+
40+
fun render() {
41+
shader.use()
42+
shader["u_CameraPosition"] = mc.gameRenderer.camera.pos
43+
44+
withFaceCulling(faces::render)
45+
withLineWidth(RenderSettings.outlineWidth, outlines::render)
46+
}
47+
48+
fun clear() {
49+
faceVertices.clear()
50+
outlineVertices.clear()
51+
52+
faces.clear()
53+
outlines.clear()
54+
}
55+
56+
fun IRenderContext.vertex(
57+
storage: MutableMap<Vertex, Int>,
58+
x: Double, y: Double, z: Double,
59+
color: Color
60+
) = lazy {
61+
storage.getOrPut(Vertex(x, y, z, color)) {
62+
vec3(x, y, z).color(color).end()
63+
}
64+
}
65+
66+
data class Vertex(val x: Double, val y: Double, val z: Double, val color: Color)
67+
68+
companion object {
69+
private val shader = Shader("renderer/pos_color", "renderer/box_static")
70+
}
71+
}

0 commit comments

Comments
 (0)