Skip to content

Commit

Permalink
ebiten: panic if a non-existent uniform variable name is given
Browse files Browse the repository at this point in the history
Closes #2710
  • Loading branch information
hajimehoshi committed Jul 29, 2023
1 parent 0b1c740 commit 1b8580f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 16 additions & 1 deletion internal/ui/shader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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() {
Expand Down
12 changes: 12 additions & 0 deletions shader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1b8580f

Please sign in to comment.