Skip to content

Commit

Permalink
DirectGL on Win32. Hugely reduces OpenGL overhead on windows JVM: Use…
Browse files Browse the repository at this point in the history
… JNA 5.13 Library.OPTION_SYMBOL_PROVIDER to implement fast direct OpenGL mapping on Windows (#1195)

Use JNA 5.13 Library.OPTION_SYMBOL_PROVIDER to implement fast direct OpenGL mapping on Windows
  • Loading branch information
soywiz authored Jan 14, 2023
1 parent 4f5e84e commit 67c38ab
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.soywiz.korge.gradle

object BuildVersions {
const val GIT = "main"
const val KOTLIN = "1.8.0"
const val KOTLIN = "1.7.21"
const val NODE_JS = "16.9.1"
const val JNA = "5.12.1"
const val JNA = "5.13.0"
const val COROUTINES = "1.6.4"
const val ANDROID_BUILD = "7.0.4"
const val KOTLIN_SERIALIZATION = "1.4.0"
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
jna = "5.12.1"
jna = "5.13.0"
jcodec = "0.2.5"
proguard-gradle = "7.2.2"
kotlin = "1.7.21"
Expand Down
34 changes: 32 additions & 2 deletions korgw/src/jvmMain/kotlin/com/soywiz/korgw/platform/INativeGL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

package com.soywiz.korgw.platform

import com.soywiz.kmem.*
import com.soywiz.kmem.Platform
import com.soywiz.kmem.dyn.*
import com.soywiz.korio.lang.*
import com.soywiz.korio.time.*
import com.soywiz.korio.util.*
Expand Down Expand Up @@ -38,6 +41,9 @@ typealias IntPtr = IntBuffer
typealias FloatPtr = FloatBuffer

object DirectGL : INativeGL {
external fun glGenVertexArrays(n: Int, out: IntArray)
external fun glBindVertexArray(varray: Int)

external override fun glActiveTexture(texture: GLenum)
external override fun glAttachShader(program: GLuint, shader: GLuint)
external override fun glBindAttribLocation(program: GLuint, index: GLuint, name: String)
Expand Down Expand Up @@ -204,15 +210,39 @@ object DirectGL : INativeGL {
try {
if (nativeOpenGLLibraryPath == null) error("Can't get OpenGL library")
traceTime("OpenGL Native.register") {
// @TODO: Can we provide a custom loader? like a dysym, to use the glGetProcAddress? If so, we will be able to use this on Windows too
Native.register(DirectGL::class.java, NativeLibrary.getInstance(nativeOpenGLLibraryPath, mutableMapOf<String, Any?>()))
Native.register(
DirectGL::class.java,
NativeLibrary.getInstance(
nativeOpenGLLibraryPath,
mutableMapOf<String, Any?>().apply {
if (Platform.isWindows) {
this[Library.OPTION_SYMBOL_PROVIDER] = SymbolProvider { handle, name, parent ->
val ptr = Win32.wglGetProcAddress(name)
//println("LOADING $name: ${ptr.address}, $parent")
//error(name)
if (ptr.address == 0L) {
parent.getSymbolAddress(handle, name, null)
} else {
ptr.address
}
}
}
}
)
)
}
loaded = true
} catch (e: Throwable) {
com.soywiz.klogger.Console.error("Failed to initialize OpenAL: arch=$arch, OS.rawName=${OS.rawName}, nativeOpenGLLibraryPath=$nativeOpenGLLibraryPath, message=${e.message}")
//e.printStackTrace()
}
}

interface Win32 : Library {
fun wglGetProcAddress(name: String): Pointer

companion object : Win32 by Native.load("opengl32", Win32::class.java)
}
}

private val arch by lazy { System.getProperty("os.arch").toLowerCase() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.soywiz.korim.awt.AwtNativeImage
import com.soywiz.korim.bitmap.NativeImage
import com.sun.jna.NativeLong

open class NativeKgl(private val gl: INativeGL) : KmlGlWithExtensions() {
open class NativeKgl constructor(private val gl: INativeGL) : KmlGlWithExtensions() {
override fun activeTexture(texture: Int): Unit = gl.glActiveTexture(texture)
override fun attachShader(program: Int, shader: Int): Unit = gl.glAttachShader(program, shader)
override fun bindAttribLocation(program: Int, index: Int, name: String): Unit = gl.glBindAttribLocation(program, index, name)
Expand Down
8 changes: 3 additions & 5 deletions korgw/src/jvmMain/kotlin/com/soywiz/korgw/win32/Win32Tools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.soywiz.korgw.GameWindowConfig
import com.soywiz.korgw.platform.BaseOpenglContext
import com.soywiz.korgw.platform.INativeGL
import com.soywiz.korgw.platform.NativeKgl
import com.soywiz.korgw.platform.DirectGL
import com.soywiz.korim.bitmap.Bitmap32
import com.soywiz.korio.lang.Environment
import com.sun.jna.*
Expand Down Expand Up @@ -35,11 +36,11 @@ open class Win32KmlGl : NativeKgl(Win32GL) {
vertexArrayCachedVersion = contextVersion
val out = intArrayOf(-1)
//checkError("before glGenVertexArrays")
Win32GL.glGenVertexArrays(1, out)
DirectGL.glGenVertexArrays(1, out)
checkError("glGenVertexArrays")
vertexArray = out[0]
}
Win32GL.glBindVertexArray(vertexArray)
DirectGL.glBindVertexArray(vertexArray)
//checkError("glBindVertexArray")
}
}
Expand All @@ -54,9 +55,6 @@ interface Win32GL : INativeGL, Library {
//fun wglCreateContextAttribsARB(hDC: HDC, hshareContext: WinGDI.PIXELFORMATDESCRIPTOR.ByReference?, attribList: Pointer?): Int
fun wglCreateContextAttribsARB(hDC: HDC, hshareContext: WinGDI.PIXELFORMATDESCRIPTOR.ByReference?, attribList: IntArray?): HGLRC?

fun glGenVertexArrays(n: Int, out: IntArray)
fun glBindVertexArray(varray: Int)

companion object : Win32GLBase(Win32GLLoader())

class Win32GLLoader {
Expand Down

0 comments on commit 67c38ab

Please sign in to comment.