ebiten: panic if a non-existent uniform variable name is given

Closes #2710
This commit is contained in:
Hajime Hoshi 2023-07-29 20:24:40 +09:00
parent 0b1c7404d5
commit 1b8580fab7
3 changed files with 32 additions and 1 deletions

View File

@ -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()

View File

@ -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() {

View File

@ -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