Skip to content

Commit

Permalink
Add more diagnostics to OpenGL drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvanyo committed Oct 10, 2024
1 parent 6d78eb3 commit 4aa578b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,10 @@ class GameOfLifeShape(

fun draw(mvpMatrix: FloatArray) {
GLES20.glUseProgram(program)
checkOpenGLError()

GLES20.glEnableVertexAttribArray(positionHandle)
checkOpenGLError()

GLES20.glVertexAttribPointer(
positionHandle,
Expand All @@ -272,15 +274,19 @@ class GameOfLifeShape(
vertexStride,
vertexBuffer,
)
checkOpenGLError()

GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0)
checkOpenGLError()
checkOpenGLFramebufferStatus()

GLES20.glDrawElements(
GLES20.GL_TRIANGLES,
drawOrder.size,
GLES20.GL_UNSIGNED_SHORT,
drawListBuffer,
)
checkOpenGLError()

GLES20.glDisableVertexAttribArray(positionHandle)
checkOpenGLError()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ fun checkOpenGLError() {
}
}

/**
* Throws if the framebuffer is not complete.
*/
fun checkOpenGLFramebufferStatus() {
val status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER)
if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) {
error("OpenGL framebuffer error: $status")
}
}

/**
* Creates and compiles the given [shaderCode] of type [type].
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import com.alexvanyo.composelife.ui.mobile.ComposeLifeTheme
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onEach
import java.nio.IntBuffer
import kotlin.concurrent.Volatile

@Composable
fun openGLSupported(): Boolean {
Expand Down Expand Up @@ -137,15 +138,14 @@ fun OpenGLNonInteractableCells(
surface: Surface,
width: Int,
height: Int,
): EGLSurface? {
return super.onSurfaceCreated(spec, config, surface, width, height).also {
): EGLSurface? =
super.onSurfaceCreated(spec, config, surface, width, height).also {
GLES20.glClearColor(0f, 0f, 0f, 0f)
GLES20.glViewport(0, 0, width, height)
gameOfLifeShape = GameOfLifeShape().apply {
setSize(width, height)
}
}
}

override fun onDrawFrame(eglManager: EGLManager) {
gameOfLifeShape.setScreenShapeParameters(parameters)
Expand Down

0 comments on commit 4aa578b

Please sign in to comment.