From d8e045530bc6531c2cff79e536d803dad60e35a1 Mon Sep 17 00:00:00 2001 From: Carlos Ballesteros Velasco Date: Fri, 25 Mar 2022 00:34:41 +0100 Subject: [PATCH] Fixes falsely reporting an issue with glGenVertexArrays (#533) Fixes https://github.com/korlibs/korge-hello-world/issues/18 --- .../kotlin/com/soywiz/kgl/KmlGlExt.kt | 3 +- .../kotlin/com/soywiz/kgl/KmlGlProxy.kt | 2 +- .../kotlin/com/soywiz/kgl/KmlGlState.kt | 58 +++++++++++-------- .../com/soywiz/korgw/awt/BaseAwtGameWindow.kt | 13 ++++- .../com/soywiz/korgw/win32/Win32Tools.kt | 13 +++-- 5 files changed, 56 insertions(+), 33 deletions(-) diff --git a/korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlExt.kt b/korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlExt.kt index 3893f13db..628c4e96c 100644 --- a/korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlExt.kt +++ b/korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlExt.kt @@ -2,6 +2,7 @@ package com.soywiz.kgl import com.soywiz.klogger.* import com.soywiz.kmem.* +import com.soywiz.krypto.encoding.* class KmlGlException(message: String) : RuntimeException(message) @@ -62,6 +63,6 @@ fun KmlGl.getErrorString(error: Int = getError()): String { fun KmlGl.checkError(message: String) { val error = getError() if (error != NO_ERROR) { - Console.error("glGetError after $message") + Console.error("glGetError after $message with error $error (${error.hex})") } } diff --git a/korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlProxy.kt b/korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlProxy.kt index bfde695a5..1ab27944e 100644 --- a/korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlProxy.kt +++ b/korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlProxy.kt @@ -1524,7 +1524,7 @@ class LogKmlGlProxy(parent: KmlGl = KmlGlDummy, var logBefore: Boolean = false, if (logAfter) println("after: $name ($params) = $result") } } -class CheckErrorsKmlGlProxy(parent: KmlGl, val throwException: Boolean = false, val printStackTrace: Boolean = false) : KmlGlProxy(parent) { +open class CheckErrorsKmlGlProxy(parent: KmlGl, val throwException: Boolean = false, val printStackTrace: Boolean = false) : KmlGlProxy(parent) { init { //println("CheckErrorsKmlGlProxy") } diff --git a/korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlState.kt b/korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlState.kt index 65c4ce8e1..a0337faa6 100644 --- a/korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlState.kt +++ b/korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlState.kt @@ -55,21 +55,25 @@ class KmlGlState(val gl: KmlGl) { } gl.getIntegerv(gl.CURRENT_PROGRAM, currentProgram) - //println("maxAttribs: $maxAttribs") - for (n in 0 until MAX_ATTRIB) { - //println(gl.getVertexAttribiv(n, gl.VERTEX_ATTRIB_ARRAY_ENABLED)) - vertexAttribEnabled[n] = gl.getVertexAttribiv(n, gl.VERTEX_ATTRIB_ARRAY_ENABLED) != 0 - if (vertexAttribEnabled[n]) { - //println("$n: ${vertexAttribEnabled[n]}") - gl.getVertexAttribiv(n, gl.VERTEX_ATTRIB_ARRAY_SIZE, vertexAttribSize[n]) - gl.getVertexAttribiv(n, gl.VERTEX_ATTRIB_ARRAY_TYPE, vertexAttribType[n]) - gl.getVertexAttribiv(n, gl.VERTEX_ATTRIB_ARRAY_NORMALIZED, vertexAttribNormal[n]) - gl.getVertexAttribiv(n, gl.VERTEX_ATTRIB_ARRAY_STRIDE, vertexAttribStride[n]) - gl.getVertexAttribPointerv(n, gl.VERTEX_ATTRIB_ARRAY_POINTER, vertexAttribPointer[n]) + //if (currentProgram.getAlignedInt32(0) > 0) { + run { + //println("maxAttribs: $maxAttribs") + for (n in 0 until MAX_ATTRIB) { + //println(gl.getVertexAttribiv(n, gl.VERTEX_ATTRIB_ARRAY_ENABLED)) + vertexAttribEnabled[n] = gl.getVertexAttribiv(n, gl.VERTEX_ATTRIB_ARRAY_ENABLED) != 0 + if (vertexAttribEnabled[n]) { + //println("$n: ${vertexAttribEnabled[n]}") + gl.getVertexAttribiv(n, gl.VERTEX_ATTRIB_ARRAY_SIZE, vertexAttribSize[n]) + gl.getVertexAttribiv(n, gl.VERTEX_ATTRIB_ARRAY_TYPE, vertexAttribType[n]) + gl.getVertexAttribiv(n, gl.VERTEX_ATTRIB_ARRAY_NORMALIZED, vertexAttribNormal[n]) + gl.getVertexAttribiv(n, gl.VERTEX_ATTRIB_ARRAY_STRIDE, vertexAttribStride[n]) + gl.getVertexAttribPointerv(n, gl.VERTEX_ATTRIB_ARRAY_POINTER, vertexAttribPointer[n]) + } } + arrayBufferBinding = gl.getIntegerv(gl.ARRAY_BUFFER_BINDING) + elementArrayBufferBinding = gl.getIntegerv(gl.ELEMENT_ARRAY_BUFFER_BINDING) } - arrayBufferBinding = gl.getIntegerv(gl.ARRAY_BUFFER_BINDING) - elementArrayBufferBinding = gl.getIntegerv(gl.ELEMENT_ARRAY_BUFFER_BINDING) + gl.getError() // Clears the error } fun saveEnable() { for (n in enabledList.indices) enabledArray[n] = gl.isEnabled(enabledList[n]) } @@ -96,22 +100,26 @@ class KmlGlState(val gl: KmlGl) { gl.useProgram(currentProgram.i32[0]) //gl.bindAttribLocation() - for (n in 0 until MAX_ATTRIB) { - gl.enableDisableVertexAttribArray(n, vertexAttribEnabled[n]) - if (vertexAttribEnabled[n]) { - val ptr = vertexAttribPointer[n].getAlignedInt64(0) - gl.vertexAttribPointer( - n, - vertexAttribSize[n].i32[0], - vertexAttribType[n].i32[0], - vertexAttribNormal[n].i32[0] != 0, - vertexAttribStride[n].i32[0], - ptr - ) + //if (currentProgram.getAlignedInt32(0) > 0) { + run { + for (n in 0 until MAX_ATTRIB) { + gl.enableDisableVertexAttribArray(n, vertexAttribEnabled[n]) + if (vertexAttribEnabled[n]) { + val ptr = vertexAttribPointer[n].getAlignedInt64(0) + gl.vertexAttribPointer( + n, + vertexAttribSize[n].i32[0], + vertexAttribType[n].i32[0], + vertexAttribNormal[n].i32[0] != 0, + vertexAttribStride[n].i32[0], + ptr + ) + } } } gl.bindBuffer(gl.ARRAY_BUFFER, arrayBufferBinding) gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementArrayBufferBinding) + gl.getError() // Clears the error /* gl.blendEquationSeparate(blending.eqRGB.toGl(), blending.eqA.toGl()) diff --git a/korgw/src/jvmMain/kotlin/com/soywiz/korgw/awt/BaseAwtGameWindow.kt b/korgw/src/jvmMain/kotlin/com/soywiz/korgw/awt/BaseAwtGameWindow.kt index 316806da3..1dfd2e23b 100644 --- a/korgw/src/jvmMain/kotlin/com/soywiz/korgw/awt/BaseAwtGameWindow.kt +++ b/korgw/src/jvmMain/kotlin/com/soywiz/korgw/awt/BaseAwtGameWindow.kt @@ -148,7 +148,18 @@ abstract class BaseAwtGameWindow : GameWindow() { val state: KmlGlState by lazy { ag.createGlState() } fun paintInContext(g: Graphics, info: BaseOpenglContext.ContextInfo) { - state.keep { + // We only have to keep the state on mac, in linux and windows, the context/state is not shared + if (OS.isMac) { + state.keep { + paintInContextInternal(g, info) + } + } else { + paintInContextInternal(g, info) + } + } + + fun paintInContextInternal(g: Graphics, info: BaseOpenglContext.ContextInfo) { + run { //run { ctx?.swapInterval(1) diff --git a/korgw/src/jvmMain/kotlin/com/soywiz/korgw/win32/Win32Tools.kt b/korgw/src/jvmMain/kotlin/com/soywiz/korgw/win32/Win32Tools.kt index f6cd60408..7d8d36864 100644 --- a/korgw/src/jvmMain/kotlin/com/soywiz/korgw/win32/Win32Tools.kt +++ b/korgw/src/jvmMain/kotlin/com/soywiz/korgw/win32/Win32Tools.kt @@ -16,6 +16,7 @@ import java.io.* import java.lang.reflect.* import javax.swing.* +//open class Win32KmlGl : CheckErrorsKmlGlProxy(NativeKgl(Win32GL)) { open class Win32KmlGl : NativeKgl(Win32GL) { companion object : Win32KmlGl() @@ -26,6 +27,7 @@ open class Win32KmlGl : NativeKgl(Win32GL) { if (vertexArrayCachedVersion != contextVersion) { vertexArrayCachedVersion = contextVersion val out = intArrayOf(-1) + //checkError("before glGenVertexArrays") Win32GL.glGenVertexArrays(1, out) checkError("glGenVertexArrays") vertexArray = out[0] @@ -288,11 +290,12 @@ class Win32OpenglContext(val hWnd: WinDef.HWND, val hDC: WinDef.HDC, val doubleB Win32KmlGl.apply { Console.trace("GL_VERSION: ${getString(VERSION)}, GL_VENDOR: ${getString(VENDOR)}") - Console.trace( - "GL_RED_BITS: ${getIntegerv(RED_BITS)}, GL_GREEN_BITS: ${getIntegerv(GREEN_BITS)}, " + - "GL_BLUE_BITS: ${getIntegerv(BLUE_BITS)}, GL_ALPHA_BITS: ${getIntegerv(ALPHA_BITS)}, " + - "GL_DEPTH_BITS: ${getIntegerv(DEPTH_BITS)}, GL_STENCIL_BITS: ${getIntegerv(STENCIL_BITS)}" - ) + // Only available on GL_ES? + //Console.trace( + // "GL_RED_BITS: ${getIntegerv(RED_BITS)}, GL_GREEN_BITS: ${getIntegerv(GREEN_BITS)}, " + + // "GL_BLUE_BITS: ${getIntegerv(BLUE_BITS)}, GL_ALPHA_BITS: ${getIntegerv(ALPHA_BITS)}, " + + // "GL_DEPTH_BITS: ${getIntegerv(DEPTH_BITS)}, GL_STENCIL_BITS: ${getIntegerv(STENCIL_BITS)}" + //) //println() }