ebiten: allow array types for uniform variables

Closes #2448
This commit is contained in:
Hajime Hoshi 2022-11-18 14:08:27 +09:00
parent 90213d5d80
commit bba196d1ec
3 changed files with 34 additions and 11 deletions

View File

@ -505,10 +505,10 @@ type DrawTrianglesShaderOptions struct {
// Uniforms is a set of uniform variables for the shader.
// The keys are the names of the uniform variables.
// The values must be a numeric type or a slice of a numeric type.
// The values must be a numeric type, or a slice or an array of a numeric type.
// If the uniform variable type is an array, a vector or a matrix,
// you have to specify linearly flattened values as a slice.
// For example, if the uniform variable type is [4]vec4, the number of the slice values will be 16.
// you have to specify linearly flattened values as a slice or an array.
// For example, if the uniform variable type is [4]vec4, the length will be 16.
Uniforms map[string]any
// Images is a set of the source images.
@ -665,10 +665,10 @@ type DrawRectShaderOptions struct {
// Uniforms is a set of uniform variables for the shader.
// The keys are the names of the uniform variables.
// The values must be a numeric type or a slice of a numeric type.
// The values must be a numeric type, or a slice or an array of a numeric type.
// If the uniform variable type is an array, a vector or a matrix,
// you have to specify linearly flattened values as a slice.
// For example, if the uniform variable type is [4]vec4, the number of the slice values will be 16.
// you have to specify linearly flattened values as a slice or an array.
// For example, if the uniform variable type is [4]vec4, the length will be 16.
Uniforms map[string]any
// Images is a set of the source images.

View File

@ -58,8 +58,7 @@ func (s *Shader) ConvertUniforms(uniforms map[string]any) [][]uint32 {
nameToU32s[name] = []uint32{uint32(v.Uint())}
case reflect.Float32, reflect.Float64:
nameToU32s[name] = []uint32{math.Float32bits(float32(v.Float()))}
case reflect.Slice:
// TODO: Allow reflect.Array (#2448)
case reflect.Slice, reflect.Array:
u32s := make([]uint32, v.Len())
switch t.Elem().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:

View File

@ -1335,7 +1335,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
Want: color.RGBA{0x85, 0xa3, 0x08, 0xd3},
},
{
Name: "0xff,array",
Name: "0xff,slice",
Uniforms: map[string]any{
"U": []int{0xff, 0xff, 0xff, 0xff},
},
@ -1343,7 +1343,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
Want: color.RGBA{0xff, 0xff, 0xff, 0xff},
},
{
Name: "int,array",
Name: "int,slice",
Uniforms: map[string]any{
"U": []int16{0x24, 0x3f, 0x6a, 0x88},
},
@ -1351,13 +1351,37 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
Want: color.RGBA{0x24, 0x3f, 0x6a, 0x88},
},
{
Name: "uint,array",
Name: "uint,slice",
Uniforms: map[string]any{
"U": []uint8{0x85, 0xa3, 0x08, 0xd3},
},
Shader: intArray,
Want: color.RGBA{0x85, 0xa3, 0x08, 0xd3},
},
{
Name: "0xff,array",
Uniforms: map[string]any{
"U": [...]int{0xff, 0xff, 0xff, 0xff},
},
Shader: intArray,
Want: color.RGBA{0xff, 0xff, 0xff, 0xff},
},
{
Name: "int,array",
Uniforms: map[string]any{
"U": [...]int16{0x24, 0x3f, 0x6a, 0x88},
},
Shader: intArray,
Want: color.RGBA{0x24, 0x3f, 0x6a, 0x88},
},
{
Name: "uint,array",
Uniforms: map[string]any{
"U": [...]uint8{0x85, 0xa3, 0x08, 0xd3},
},
Shader: intArray,
Want: color.RGBA{0x85, 0xa3, 0x08, 0xd3},
},
}
for _, tc := range testCases {
tc := tc