graphicsdriver/opengl: Check the reserved keyword

Fixes #772
This commit is contained in:
Hajime Hoshi 2019-01-08 00:07:47 +09:00
parent 758b957b01
commit 12f7d14d91

View File

@ -16,6 +16,7 @@ package opengl
import ( import (
"fmt" "fmt"
"regexp"
"strings" "strings"
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
@ -28,10 +29,44 @@ const (
shaderFragmentColorMatrix shaderFragmentColorMatrix
) )
// glslReservedKeywords is a set of reserved keywords that cannot be used as an indentifier on some environments.
// See https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.60.pdf.
var glslReservedKeywords = map[string]struct{}{
"common": {}, "partition": {}, "active": {},
"asm": {},
"class": {}, "union": {}, "enum": {}, "typedef": {}, "template": {}, "this": {},
"resource": {},
"goto": {},
"inline": {}, "noinline": {}, "public": {}, "static": {}, "extern": {}, "external": {}, "interface": {},
"long": {}, "short": {}, "half": {}, "fixed": {}, "unsigned": {}, "superp": {},
"input": {}, "output": {},
"hvec2": {}, "hvec3": {}, "hvec4": {}, "fvec2": {}, "fvec3": {}, "fvec4": {},
"filter": {},
"sizeof": {}, "cast": {},
"namespace": {}, "using": {},
"sampler3DRect": {},
}
var glslIdentifier = regexp.MustCompile(`[_a-zA-Z][_a-zA-Z0-9]*`)
func checkGLSL(src string) {
for _, l := range strings.Split(src, "\n") {
if strings.Contains(l, "//") {
l = l[:strings.Index(l, "//")]
}
for _, token := range glslIdentifier.FindAllString(l, -1) {
if _, ok := glslReservedKeywords[token]; ok {
panic(fmt.Sprintf("opengl: %q is a reserved keyword", token))
}
}
}
}
func shaderStr(id shaderID) string { func shaderStr(id shaderID) string {
src := ""
switch id { switch id {
case shaderVertexModelview: case shaderVertexModelview:
return shaderStrVertex src = shaderStrVertex
case shaderFragmentColorMatrix: case shaderFragmentColorMatrix:
replaces := map[string]string{ replaces := map[string]string{
"{{.FilterNearest}}": fmt.Sprintf("%d", graphics.FilterNearest), "{{.FilterNearest}}": fmt.Sprintf("%d", graphics.FilterNearest),
@ -40,14 +75,16 @@ func shaderStr(id shaderID) string {
"{{.AddressClampToZero}}": fmt.Sprintf("%d", graphics.AddressClampToZero), "{{.AddressClampToZero}}": fmt.Sprintf("%d", graphics.AddressClampToZero),
"{{.AddressRepeat}}": fmt.Sprintf("%d", graphics.AddressRepeat), "{{.AddressRepeat}}": fmt.Sprintf("%d", graphics.AddressRepeat),
} }
src := shaderStrFragment src = shaderStrFragment
for k, v := range replaces { for k, v := range replaces {
src = strings.Replace(src, k, v, -1) src = strings.Replace(src, k, v, -1)
} }
return src
default: default:
panic("not reached") panic("not reached")
} }
checkGLSL(src)
return src
} }
const ( const (