Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DirectGL on Win32. Hugely reduces OpenGL overhead on windows JVM #1140

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion kmem/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
description = "Memory utilities for Kotlin"

dependencies {
add("jvmMainApi", libs.bundles.jna)
//add("jvmMainApi", libs.bundles.jna)

// Includes this change to support direct OpenGL Win32 mapping:
// https://github.com/korlibs/jna/commit/c5d87cc4b1034b402b5d114e0efd401533636832
add("jvmMainApi", files("lib/jna-patched.jar", "lib/jna-platform-5.12.1.jar"))
}

configurations {
// configuration that holds jars to copy into lib
extraLibs
}
dependencies {
extraLibs files("lib/jna-patched.jar", "lib/jna-platform-5.12.1.jar")
}

jvmJar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
into('lib') {
from {
configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) }
}
}
}
Binary file added kmem/lib/jna-patched.jar
Binary file not shown.
Binary file added kmem/lib/jna-platform-5.12.1.jar
Binary file not shown.
35 changes: 34 additions & 1 deletion 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,10 @@ 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 @@ -205,14 +212,40 @@ object DirectGL : INativeGL {
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
14 changes: 5 additions & 9 deletions korgw/src/jvmMain/kotlin/com/soywiz/korgw/win32/Win32Tools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import com.soywiz.kgl.getIntegerv
import com.soywiz.klogger.Console
import com.soywiz.korgw.GameWindow
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.*
import com.soywiz.korim.bitmap.Bitmap32
import com.soywiz.korio.lang.Environment
import com.sun.jna.*
Expand All @@ -24,7 +22,8 @@ import java.lang.reflect.Method
import java.lang.reflect.Proxy

//open class Win32KmlGl : CheckErrorsKmlGlProxy(NativeKgl(Win32GL)) {
open class Win32KmlGl : NativeKgl(Win32GL) {
//open class Win32KmlGl : NativeKgl(Win32GL) {
open class Win32KmlGl : NativeKgl(DirectGL) {
companion object : Win32KmlGl()

var vertexArrayCachedVersion = -1
Expand All @@ -35,11 +34,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 +53,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