natario1 / Egloo

A lightweight Kotlin multiplatform framework for OpenGL ES and EGL management based on object-oriented components, inspired by Google's Grafika.

Home Page:https://natario1.github.io/Egloo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Validate shaders at runtime

MaxPleaner opened this issue · comments

This is possibly a kind of niche request, but I am building an application that lets users write their own shaders. I am looking for a way to catch compile errors before they are eventually caught in draw (resulting in a Fatal exception). I have tried with the following, but this fails to compile:

            val dummyShader = """
                precision mediump float;
                void main() {
                    gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
                }
            """.trimIndent()
            GlShader(GL_FRAGMENT_SHADER.toInt(), dummyShader)

I am not sure what else I need to do to get any shader to compile in this context (I am calling this outside of any Egloo class, in my own code).

Make sure you have a valid egl context, that is, create EglCore and make it current.

Thanks so much for the hint, @natario1. I got it figured out. If anyone stumbles upon this, my function is as follows:

    // Returns null if shader is valid. Otherwise, returns error message
    fun validateShader(fragmentShader: String): String? {
        val core = EglCore()
        val texture = GlTexture()
        val surfaceTexture = SurfaceTexture(texture.id)
        val window = EglWindowSurface(core, surfaceTexture)
        window.makeCurrent()

        try {
            GlShader(GL_FRAGMENT_SHADER, fragmentShader)
        } catch (e: java.lang.RuntimeException) {
            return e.message
        } finally {
            core.release()
        }
        return null
    }

I'd release the window surface and texture too just in case.