func glSetupShaderProg(shader *shaderProg, unif bool) { var glStatus gl.Int shader.glFragShader = gl.CreateShader(gl.FRAGMENT_SHADER) glSetShaderSource(shader.glFragShader, shader.srcFrag) gl.CompileShader(shader.glFragShader) if gl.GetShaderiv(shader.glFragShader, gl.COMPILE_STATUS, &glStatus); glStatus == 0 { panic("Frag shader compile error: " + glGetInfoLog(shader.glFragShader, true)) } shader.glVertShader = gl.CreateShader(gl.VERTEX_SHADER) glSetShaderSource(shader.glVertShader, shader.srcVert) gl.CompileShader(shader.glVertShader) if gl.GetShaderiv(shader.glVertShader, gl.COMPILE_STATUS, &glStatus); glStatus == 0 { panic("Vert shader compile error: " + glGetInfoLog(shader.glFragShader, true)) } shader.glProg = gl.CreateProgram() gl.AttachShader(shader.glProg, shader.glFragShader) gl.AttachShader(shader.glProg, shader.glVertShader) gl.LinkProgram(shader.glProg) if gl.GetProgramiv(shader.glProg, gl.LINK_STATUS, &glStatus); glStatus == 0 { panic("Shader program link error: " + glGetInfoLog(shader.glProg, false)) } shader.attrPos = glGetLocation(shader.glProg, "aPos", true) if unif { shader.unifTex = gl.Int(glGetLocation(shader.glProg, "uTex", false)) } }
// Loads a shader from a string func LoadShader(typ g.Enum, s string) g.Uint { shader := g.CreateShader(typ) cst := g.GLString(s) defer g.GLStringFree(cst) g.ShaderSource(shader, 1, &cst, nil) g.CompileShader(shader) var status g.Int g.GetShaderiv(shader, g.COMPILE_STATUS, &status) if status == g.FALSE { var length g.Int g.GetShaderiv(shader, g.INFO_LOG_LENGTH, &length) log.Println(int(length)) logStr := g.GLStringAlloc(g.Sizei(length + 1)) // TODO: defer g.GetShaderInfoLog(shader, g.Sizei(length), nil, logStr) log.Println(g.GoString(logStr)) } return shader }
func RecompileShaders(shaderNames ...string) { var vs string var glProg gl.Uint var glStatus gl.Int var glFShader, glVShader gl.Uint var shaderProg *gltypes.ShaderProgram var defines = map[string]interface{}{} defines["CANV_W"] = Screen.W defines["CANV_H"] = Screen.H for fsName, fsSrc := range glsl.FShaders { if (len(shaderNames) == 0) || stringutil.IsInSlice(shaderNames, fsName) { glFShader = gl.CreateShader(gl.FRAGMENT_SHADER) glutil.ShaderSource(fsName, glFShader, fsSrc, defines, false) gl.CompileShader(glFShader) if gl.GetShaderiv(glFShader, gl.COMPILE_STATUS, &glStatus); glStatus == 0 { log.Panicf("fshader %s: %s\n", fsName, glutil.ShaderInfoLog(glFShader, true)) } if vs = glsl.VShaders[fsName]; len(vs) <= 0 { vs = glsl.VShaders["ppquad"] } glVShader = gl.CreateShader(gl.VERTEX_SHADER) glutil.ShaderSource(fsName, glVShader, vs, defines, false) gl.CompileShader(glVShader) if gl.GetShaderiv(glVShader, gl.COMPILE_STATUS, &glStatus); glStatus == 0 { log.Panicf("vshader %s: %s\n", fsName, glutil.ShaderInfoLog(glVShader, true)) } glProg = gl.CreateProgram() gl.AttachShader(glProg, glFShader) if glVShader != 0 { gl.AttachShader(glProg, glVShader) } gl.LinkProgram(glProg) if gl.GetProgramiv(glProg, gl.LINK_STATUS, &glStatus); glStatus == 0 { log.Panicf("sprog %s: %s", fsName, glutil.ShaderInfoLog(glProg, false)) } shaderProg = gltypes.NewShaderProgram(fsName, glProg, glFShader, glVShader) shaderProg.UnifCamLook = glutil.ShaderLocationU(glProg, "uCamLook") shaderProg.UnifCamPos = glutil.ShaderLocationU(glProg, "uCamPos") shaderProg.UnifScreen = glutil.ShaderLocationU(glProg, "uScreen") shaderProg.UnifTex0 = glutil.ShaderLocationU(glProg, "uTex0") shaderProg.UnifTex1 = glutil.ShaderLocationU(glProg, "uTex1") shaderProg.UnifTex2 = glutil.ShaderLocationU(glProg, "uTex2") shaderProg.UnifTime = glutil.ShaderLocationU(glProg, "uTime") if fsName == "canvas" { if Shaders.Canvas != nil { Shaders.Canvas.CleanUp() } Shaders.Canvas = shaderProg } else if fsName == "postfx" { if Shaders.PostFx != nil { Shaders.PostFx.CleanUp() } Shaders.PostFx = shaderProg } else if fsName == "ppblur" { if Shaders.PostBlur != nil { Shaders.PostBlur.CleanUp() } Shaders.PostBlur = shaderProg } else if fsName == "ppbright" { if Shaders.PostBright != nil { Shaders.PostBright.CleanUp() } Shaders.PostBright = shaderProg } else if fsName == "pplum2" { if Shaders.PostLum2 != nil { Shaders.PostLum2.CleanUp() } Shaders.PostLum2 = shaderProg } else if fsName == "pplum3" { if Shaders.PostLum3 != nil { Shaders.PostLum3.CleanUp() } Shaders.PostLum3 = shaderProg } else if fsName == "texture" { if Shaders.Texture != nil { Shaders.Texture.CleanUp() } Shaders.Texture = shaderProg } else if fsName == "cast" { if Shaders.Cast != nil { Shaders.Cast.CleanUp() } Shaders.Cast = shaderProg } } } }