Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Fixes falsely reporting an issue with glGenVertexArrays (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz authored Mar 24, 2022
1 parent 8b55686 commit d8e0455
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 33 deletions.
3 changes: 2 additions & 1 deletion korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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})")
}
}
2 changes: 1 addition & 1 deletion korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlProxy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
58 changes: 33 additions & 25 deletions korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]) }
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
13 changes: 8 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 @@ -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()

Expand All @@ -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]
Expand Down Expand Up @@ -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()
}

Expand Down

0 comments on commit d8e0455

Please sign in to comment.