Skip to content

Commit abe31c9

Browse files
committed
Create TRI(Turbo Render Interface)
1 parent fcbc8ee commit abe31c9

33 files changed

Lines changed: 939 additions & 41 deletions

build.gradle.kts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
@file:Suppress("PropertyName")
22

3+
import org.slf4j.event.Level
4+
35
plugins {
46
id("java-library")
57
id("maven-publish")
@@ -30,15 +32,17 @@ val parchment_minecraft_version: String by project
3032
version = mod_version
3133
group = mod_group_id
3234

35+
val library: Configuration by configurations.creating {
36+
configurations.implementation.get().extendsFrom(this)
37+
}
38+
3339
repositories {
3440
// Add here additional repositories if required
3541
mavenCentral()
3642
mavenLocal()
3743

38-
maven {
39-
name = "Kotlin for Forge"
40-
url = uri("https://thedarkcolour.github.io/KotlinForForge/")
41-
}
44+
maven("https://thedarkcolour.github.io/KotlinForForge/")
45+
maven("https://maven.luna5ama.dev/")
4246
}
4347

4448
base {
@@ -80,7 +84,7 @@ neoForge {
8084

8185
configureEach {
8286
systemProperty("forge.logging.markers", "REGISTRIES")
83-
logLevel = org.slf4j.event.Level.DEBUG
87+
logLevel = Level.DEBUG
8488
}
8589

8690
}
@@ -104,8 +108,14 @@ configurations {
104108
}
105109

106110
dependencies {
107-
// Add dependencies here
108111
implementation("thedarkcolour:kotlinforforge-neoforge:5.9.0")
112+
113+
library("dev.luna5ama:kmogus-core:1.1-SNAPSHOT") {
114+
exclude("org.jetbrains.kotlin", "kotlin-stdlib")
115+
}
116+
library("dev.luna5ama:kmogus-struct-api:1.1-SNAPSHOT") {
117+
exclude("org.jetbrains.kotlin", "kotlin-stdlib")
118+
}
109119
}
110120

111121
val generateModMetadata by tasks.registering(ProcessResources::class) {
@@ -129,6 +139,7 @@ val generateModMetadata by tasks.registering(ProcessResources::class) {
129139
}
130140

131141
sourceSets["main"].resources.srcDir(generateModMetadata)
142+
sourceSets["main"].compileClasspath += library
132143

133144
neoForge.ideSyncTask(generateModMetadata)
134145

@@ -145,6 +156,14 @@ publishing {
145156
}
146157
}
147158

159+
tasks.jar {
160+
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
161+
162+
from(library.map { if (it.isDirectory) it else zipTree(it) })
163+
164+
exclude("META-INF/*.RSA", "META-INF/*.DSA", "META-INF/*.SF")
165+
}
166+
148167
tasks.withType<JavaCompile>().configureEach {
149168
options.encoding = "UTF-8"
150169
}
@@ -155,3 +174,4 @@ idea {
155174
isDownloadJavadoc = true
156175
}
157176
}
177+
Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package com.github.turboscript
22

33
import com.github.turboscript.config.Configs
4+
import com.github.turboscript.graphics.TRenderSystem
5+
import com.github.turboscript.gui.ConfigGUI
6+
import com.mojang.blaze3d.platform.InputConstants
47
import com.mojang.logging.LogUtils
8+
import net.minecraft.client.KeyMapping
59
import net.neoforged.api.distmarker.Dist
610
import net.neoforged.bus.api.IEventBus
711
import net.neoforged.fml.ModContainer
812
import net.neoforged.fml.common.Mod
913
import net.neoforged.fml.config.ModConfig
1014
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent
15+
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent
16+
import net.neoforged.neoforge.client.settings.KeyConflictContext
17+
import net.neoforged.neoforge.common.NeoForge
18+
import org.lwjgl.glfw.GLFW
1119
import org.slf4j.Logger
1220

1321
@Mod(TurboScript.MOD_ID, dist = [Dist.CLIENT])
@@ -16,23 +24,35 @@ class TurboScript(
1624
modContainer: ModContainer
1725
) {
1826

19-
init {
20-
NAMESPACE = modContainer.namespace
21-
modEventBus.addListener(this::commonSetup)
27+
companion object {
2228

23-
modContainer.registerConfig(ModConfig.Type.COMMON, Configs.spec)
24-
JavaWrapper.registerExtensionPointConfig(modContainer)
25-
}
29+
internal const val MOD_ID = "turboscript"
30+
internal val LOGGER: Logger = LogUtils.getLogger()
31+
32+
internal val uiKey by lazy { KeyMapping(
33+
"turboscript.key.ui",
34+
KeyConflictContext.IN_GAME,
35+
InputConstants.Type.KEYSYM,
36+
GLFW.GLFW_KEY_Y,
37+
"key.categories.misc"
38+
) }
2639

27-
private fun commonSetup(event: FMLCommonSetupEvent) {
28-
LOGGER.info("TurboScript is running!")
2940
}
3041

31-
companion object {
42+
init {
43+
modEventBus.addListener<FMLCommonSetupEvent> {
44+
LOGGER.info("TurboScript is running!")
45+
}
3246

33-
internal const val MOD_ID = "turboscript"
34-
internal val LOGGER: Logger = LogUtils.getLogger()
35-
internal lateinit var NAMESPACE: String; private set
47+
modEventBus.addListener<RegisterKeyMappingsEvent> { event ->
48+
event.register(uiKey)
49+
}
3650

51+
NeoForge.EVENT_BUS.register(TRenderSystem)
52+
NeoForge.EVENT_BUS.register(ConfigGUI)
53+
54+
modContainer.registerConfig(ModConfig.Type.COMMON, Configs.spec)
55+
JavaWrapper.registerExtensionPointConfig(modContainer)
3756
}
57+
3858
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.github.turboscript.graphics
2+
3+
interface IRenderFunctions {
4+
5+
fun beginRender()
6+
7+
fun endRender()
8+
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.turboscript.graphics
2+
3+
class RenderContext {
4+
5+
companion object {
6+
7+
internal var instance: RenderContext? = null
8+
9+
}
10+
11+
}

src/main/kotlin/com/github/turboscript/graphics/TRenderSystem.kt

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,39 @@ package com.github.turboscript.graphics
22

33
import com.github.turboscript.config.Configs
44
import com.github.turboscript.config.RenderApi
5-
import com.github.turboscript.graphics.ogl.TOpenGlRenderSystem
5+
import com.github.turboscript.graphics.ogl.GlRenderFunctions
6+
import com.github.turboscript.graphics.IRenderFunctions
7+
import net.neoforged.bus.api.SubscribeEvent
8+
import net.neoforged.neoforge.client.event.RenderGuiEvent
69

710
object TRenderSystem {
811

912
internal val renderApi: RenderApi
1013
get() = Configs.get().renderApi.get()
1114

12-
internal fun beginRender() {
13-
when (renderApi) {
14-
RenderApi.OPENGL -> TOpenGlRenderSystem.beginRender()
15-
}
15+
private var renderFunctions: MutableMap<RenderApi, IRenderFunctions> = mutableMapOf()
16+
private val currentRenderFunctions get() = renderFunctions[renderApi]
17+
18+
internal fun render(func: RenderContext.() -> Unit) {
19+
beginRender()
20+
RenderContext.instance?.let { func.invoke(it) }
21+
endRender()
1622
}
1723

18-
internal fun endRender() {
19-
when (renderApi) {
20-
RenderApi.OPENGL -> TOpenGlRenderSystem.endRender()
24+
@SubscribeEvent
25+
fun onPreRenderGui(event: RenderGuiEvent.Pre) {
26+
refreshRenderFunctions()
27+
}
28+
29+
private fun refreshRenderFunctions() {
30+
renderFunctions[renderApi]?.let { return }
31+
renderFunctions.clear()
32+
renderFunctions[renderApi] = when (renderApi) {
33+
RenderApi.OPENGL -> GlRenderFunctions
2134
}
2235
}
2336

37+
private fun beginRender() { currentRenderFunctions?.beginRender() }
38+
private fun endRender() { currentRenderFunctions?.endRender() }
39+
2440
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.turboscript.graphics.ogl
2+
3+
import com.github.turboscript.graphics.IRenderFunctions
4+
5+
object GlRenderFunctions: IRenderFunctions {
6+
7+
override fun beginRender() {
8+
OpenGlState.reset()
9+
}
10+
11+
override fun endRender() {
12+
}
13+
14+
}

src/main/kotlin/com/github/turboscript/graphics/ogl/OpenGlState.kt renamed to src/main/kotlin/com/github/turboscript/graphics/ogl/GlState.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ object OpenGlState {
1010
private val blend0 = GlState(false) {
1111
if (it) {
1212
GlStateManager._enableBlend()
13-
GlStateManager._blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
1413
} else {
1514
GlStateManager._disableBlend()
1615
}
1716
}
1817
var blend by blend0
1918

19+
private val blendFunc0 = GlState(GL_ONE_MINUS_SRC_ALPHA) {
20+
GlStateManager._blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
21+
}
22+
var blendFunc by blendFunc0
23+
2024
private val depth0 = GlState(false) {
2125
if (it) GlStateManager._enableDepthTest()
2226
else GlStateManager._disableDepthTest()
@@ -46,6 +50,10 @@ object OpenGlState {
4650
GlStateManager._bindTexture(texture)
4751
}
4852

53+
fun bindBuffer(target: Int, buffer: Int) {
54+
GlStateManager._glBindBuffer(target, buffer)
55+
}
56+
4957
private val vertexArray0 = GlState(0) { glBindVertexArray(it) }
5058
var vertexArray by vertexArray0
5159

src/main/kotlin/com/github/turboscript/graphics/ogl/TOpenGlRenderSystem.kt

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.github.turboscript.graphics.ogl.buffer
2+
3+
import com.github.turboscript.graphics.ogl.OpenGlState
4+
import com.github.turboscript.graphics.turboRender.buffer.TRIBuffer
5+
import dev.luna5ama.kmogus.Arr
6+
import dev.luna5ama.kmogus.MutableArr
7+
import dev.luna5ama.kmogus.asMutable
8+
import org.lwjgl.opengl.GL46.*
9+
import java.nio.ByteBuffer
10+
11+
open class GlBuffer(
12+
size: Long,
13+
extraBits: Int = 0,
14+
mapBits: Int = GL_READ_WRITE,
15+
): TRIBuffer {
16+
17+
val id: Int = glCreateBuffers()
18+
19+
private var lastTarget: Int? = null
20+
protected val arr: MutableArr
21+
22+
override fun getMappedArr(): MutableArr {
23+
return arr
24+
}
25+
26+
init {
27+
glNamedBufferStorage(id, size, GL_MAP_READ_BIT or extraBits)
28+
arr = Arr.wrap(glMapNamedBufferRange(
29+
id, 0L, size, mapBits
30+
) as ByteBuffer).asMutable()
31+
}
32+
33+
fun bind(target: Int) {
34+
lastTarget = target
35+
OpenGlState.bindBuffer(target, id)
36+
}
37+
38+
fun unbind() {
39+
lastTarget?.let {
40+
OpenGlState.bindBuffer(it, 0)
41+
lastTarget = null
42+
}
43+
}
44+
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.github.turboscript.graphics.ogl.buffer
2+
3+
import org.lwjgl.opengl.GL46.*
4+
5+
/**
6+
* Persistent & Coherent Mapped Buffer
7+
* Automatically flushes the buffer when it reaches 3/4 of its size.
8+
*/
9+
class GlPersistentMappedBuffer(
10+
size: Long,
11+
private val stride: Int,
12+
): GlBuffer(size, GL_MAP_PERSISTENT_BIT or GL_MAP_COHERENT_BIT) {
13+
14+
var offset = 0L
15+
16+
fun end() {
17+
offset = (arr.pos / stride)
18+
}
19+
20+
private var sync = 0L
21+
22+
fun waitAndSync() {
23+
// Check if the draw call is complete and reset the Buffer
24+
if (sync == 0L) {
25+
if (arr.pos >= arr.len / 4 * 3) { // 3/4 of the buffer is full
26+
sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0)
27+
}
28+
} else if (
29+
IntArray(1).apply {
30+
glGetSynciv(sync, GL_SYNC_STATUS, IntArray(1), this)
31+
}[0] == GL_SIGNALED
32+
) {
33+
glDeleteSync(sync)
34+
sync = 0L
35+
arr.pos = 0L
36+
offset = 0
37+
}
38+
}
39+
40+
}

0 commit comments

Comments
 (0)