From 1b8580fab777c05750d0aa15af81537f05bf4df1 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 29 Jul 2023 20:24:40 +0900 Subject: [PATCH] ebiten: panic if a non-existent uniform variable name is given Closes #2710 --- image.go | 4 ++++ internal/ui/shader.go | 17 ++++++++++++++++- shader_test.go | 12 ++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/image.go b/image.go index ad099dd5c..7ede04f9d 100644 --- a/image.go +++ b/image.go @@ -581,6 +581,8 @@ var _ [len(DrawTrianglesShaderOptions{}.Images) - graphics.ShaderImageCount]stru // // If a specified uniform variable's length or type doesn't match with an expected one, DrawTrianglesShader panics. // +// If a non-existent uniform variable name is specified, DrawTrianglesShader panics. +// // When the image i is disposed, DrawTrianglesShader does nothing. func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader *Shader, options *DrawTrianglesShaderOptions) { i.copyCheck() @@ -732,6 +734,8 @@ var _ [len(DrawRectShaderOptions{}.Images)]struct{} = [graphics.ShaderImageCount // // If a specified uniform variable's length or type doesn't match with an expected one, DrawRectShader panics. // +// If a non-existent uniform variable name is specified, DrawRectShader panics. +// // When the image i is disposed, DrawRectShader does nothing. func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawRectShaderOptions) { i.copyCheck() diff --git a/internal/ui/shader.go b/internal/ui/shader.go index ddd2909d4..68898a37e 100644 --- a/internal/ui/shader.go +++ b/internal/ui/shader.go @@ -46,6 +46,22 @@ func (s *Shader) MarkDisposed() { } func (s *Shader) AppendUniforms(dst []uint32, uniforms map[string]any) []uint32 { + // Check the given names are valid. + // This is a linear search and not efficient, but the number of uniform variables should not be so big. + for n := range uniforms { + var found bool + for _, nn := range s.uniformNames { + if n == nn { + found = true + break + } + } + if found { + continue + } + panic(fmt.Sprintf("ui: unexpected uniform name: %s", n)) + } + if s.uniformUint32Count == 0 { for _, typ := range s.uniformTypes { s.uniformUint32Count += typ.Uint32Count() @@ -67,7 +83,6 @@ func (s *Shader) AppendUniforms(dst []uint32, uniforms map[string]any) []uint32 typ := s.uniformTypes[i] if uv, ok := uniforms[name]; ok { - // TODO: Panic if uniforms include an invalid name v := reflect.ValueOf(uv) t := v.Type() switch t.Kind() { diff --git a/shader_test.go b/shader_test.go index b553cb3fd..6f00e5332 100644 --- a/shader_test.go +++ b/shader_test.go @@ -1942,6 +1942,18 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { }, err: true, }, + { + uniforms: map[string]any{ + "W": 1, + }, + err: true, + }, + { + uniforms: map[string]any{ + "W": []int32{1, 2, 3}, + }, + err: true, + }, } for _, tc := range tests { tc := tc