Validate shaders at runtime
MaxPleaner opened this issue · 3 comments
MaxPleaner commented
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).
natario1 commented
Make sure you have a valid egl context, that is, create EglCore and make it current.
MaxPleaner commented
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
}
natario1 commented
I'd release the window surface and texture too just in case.