<commit_before>// Copyright 2014 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package opengl

import (
	"fmt"
	"strings"

	"github.com/hajimehoshi/ebiten/internal/graphics"
)

type shaderID int

const (
	shaderVertexModelview shaderID = iota
	shaderFragmentColorMatrix
)

func shaderStr(id shaderID) string {
	switch id {
	case shaderVertexModelview:
		return shaderStrVertex
	case shaderFragmentColorMatrix:
		replaces := map[string]string{
			"{{.FilterNearest}}":      fmt.Sprintf("%d", graphics.FilterNearest),
			"{{.FilterLinear}}":       fmt.Sprintf("%d", graphics.FilterLinear),
			"{{.FilterScreen}}":       fmt.Sprintf("%d", graphics.FilterScreen),
			"{{.AddressClampToZero}}": fmt.Sprintf("%d", graphics.AddressClampToZero),
			"{{.AddressRepeat}}":      fmt.Sprintf("%d", graphics.AddressRepeat),
		}
		src := shaderStrFragment
		for k, v := range replaces {
			src = strings.Replace(src, k, v, -1)
		}
		return src
	default:
		panic("not reached")
	}
}

const (
	shaderStrVertex = `
uniform vec2 viewport_size;
attribute vec2 vertex;
attribute vec2 tex;
attribute vec4 tex_region;<commit_msg>graphicsdriver/opengl: Check the reserved keyword<commit_after>// Copyright 2014 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package opengl

import (
	"fmt"
	"regexp"
	"strings"

	"github.com/hajimehoshi/ebiten/internal/graphics"
)

type shaderID int

const (
	shaderVertexModelview shaderID = iota
	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 {
	src := ""
	switch id {
	case shaderVertexModelview:
		src = shaderStrVertex
	case shaderFragmentColorMatrix:
		replaces := map[string]string{
			"{{.FilterNearest}}":      fmt.Sprintf("%d", graphics.FilterNearest),
			"{{.FilterLinear}}":       fmt.Sprintf("%d", graphics.FilterLinear),
			"{{.FilterScreen}}":       fmt.Sprintf("%d", graphics.FilterScreen),
			"{{.AddressClampToZero}}": fmt.Sprintf("%d", graphics.AddressClampToZero),
			"{{.AddressRepeat}}":      fmt.Sprintf("%d", graphics.AddressRepeat),
		}
		src = shaderStrFragment
		for k, v := range replaces {
			src = strings.Replace(src, k, v, -1)
		}
	default:
		panic("not reached")
	}

	checkGLSL(src)
	return src
}

const (
	shaderStrVertex = `
uniform vec2 viewport_size;
attribute vec2 vertex;
attribute vec2 tex;
attribute vec4 tex_region;
{'fraction_non_alphanumeric': 0.16431718061674008, 'fraction_numerical': 0.008590308370044052, 'mean_word_length': 4.045555555555556, 'pattern_counts': {'":': 49, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2352978', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1436, 'n_words': 411}