2022-03-19 18:00:44 +01:00
|
|
|
// Copyright 2022 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// 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 ui
|
|
|
|
|
|
|
|
import (
|
2022-08-26 10:03:17 +02:00
|
|
|
"fmt"
|
2022-11-12 11:02:38 +01:00
|
|
|
"math"
|
2022-11-12 10:38:15 +01:00
|
|
|
"reflect"
|
2022-08-26 10:03:17 +02:00
|
|
|
|
2022-12-03 10:15:02 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2022-03-19 18:00:44 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/mipmap"
|
2022-04-03 19:15:33 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2022-03-19 18:00:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Shader struct {
|
2022-03-21 09:48:47 +01:00
|
|
|
shader *mipmap.Shader
|
2022-08-26 10:03:17 +02:00
|
|
|
|
2022-12-03 10:15:02 +01:00
|
|
|
uniformNames []string
|
|
|
|
uniformTypes []shaderir.Type
|
2022-03-19 18:00:44 +01:00
|
|
|
}
|
|
|
|
|
2022-04-03 19:15:33 +02:00
|
|
|
func NewShader(ir *shaderir.Program) *Shader {
|
2022-03-19 18:00:44 +01:00
|
|
|
return &Shader{
|
2022-08-26 10:03:17 +02:00
|
|
|
shader: mipmap.NewShader(ir),
|
2022-12-03 13:25:28 +01:00
|
|
|
uniformNames: ir.UniformNames[graphics.PreservedUniformVariablesCount:],
|
|
|
|
uniformTypes: ir.Uniforms[graphics.PreservedUniformVariablesCount:],
|
2022-03-19 18:00:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) MarkDisposed() {
|
|
|
|
s.shader.MarkDisposed()
|
|
|
|
s.shader = nil
|
|
|
|
}
|
2022-08-26 10:03:17 +02:00
|
|
|
|
2022-12-03 13:47:10 +01:00
|
|
|
func (s *Shader) UniformUint32Count() int {
|
2022-12-03 08:11:26 +01:00
|
|
|
var n int
|
2022-12-03 13:25:28 +01:00
|
|
|
for _, typ := range s.uniformTypes {
|
2022-12-03 08:11:26 +01:00
|
|
|
n += typ.Uint32Count()
|
|
|
|
}
|
2022-12-03 13:47:10 +01:00
|
|
|
return n
|
|
|
|
}
|
2022-12-03 10:15:02 +01:00
|
|
|
|
2022-12-03 13:47:10 +01:00
|
|
|
func (s *Shader) ConvertUniforms(dst []uint32, uniforms map[string]any) {
|
2022-12-03 08:11:26 +01:00
|
|
|
var idx int
|
2022-12-03 13:25:28 +01:00
|
|
|
for i, name := range s.uniformNames {
|
|
|
|
typ := s.uniformTypes[i]
|
2022-12-03 08:11:26 +01:00
|
|
|
|
|
|
|
if uv, ok := uniforms[name]; ok {
|
|
|
|
// TODO: Panic if uniforms include an invalid name
|
|
|
|
v := reflect.ValueOf(uv)
|
|
|
|
t := v.Type()
|
|
|
|
switch t.Kind() {
|
2022-11-12 10:38:15 +01:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2022-12-03 13:47:10 +01:00
|
|
|
dst[idx] = uint32(v.Int())
|
2022-11-12 10:38:15 +01:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
2022-12-03 13:47:10 +01:00
|
|
|
dst[idx] = uint32(v.Uint())
|
2022-11-12 10:38:15 +01:00
|
|
|
case reflect.Float32, reflect.Float64:
|
2022-12-03 13:47:10 +01:00
|
|
|
dst[idx] = math.Float32bits(float32(v.Float()))
|
2022-12-03 08:11:26 +01:00
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
l := v.Len()
|
|
|
|
switch t.Elem().Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
for i := 0; i < l; i++ {
|
2022-12-03 13:47:10 +01:00
|
|
|
dst[idx+i] = uint32(v.Index(i).Int())
|
2022-12-03 08:11:26 +01:00
|
|
|
}
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
|
|
for i := 0; i < l; i++ {
|
2022-12-03 13:47:10 +01:00
|
|
|
dst[idx+i] = uint32(v.Index(i).Uint())
|
2022-12-03 08:11:26 +01:00
|
|
|
}
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
for i := 0; i < l; i++ {
|
2022-12-03 13:47:10 +01:00
|
|
|
dst[idx+i] = math.Float32bits(float32(v.Index(i).Float()))
|
2022-12-03 08:11:26 +01:00
|
|
|
}
|
|
|
|
default:
|
2022-12-03 13:47:10 +01:00
|
|
|
panic(fmt.Sprintf("ui: unexpected uniform value type: %s (%s)", name, v.Kind().String()))
|
2022-11-12 10:38:15 +01:00
|
|
|
}
|
|
|
|
default:
|
2022-12-03 13:47:10 +01:00
|
|
|
panic(fmt.Sprintf("ui: unexpected uniform value type: %s (%s)", name, v.Kind().String()))
|
2022-11-12 11:02:38 +01:00
|
|
|
}
|
2022-08-26 10:03:17 +02:00
|
|
|
}
|
|
|
|
|
2022-12-03 08:11:26 +01:00
|
|
|
idx += typ.Uint32Count()
|
2022-08-26 10:03:17 +02:00
|
|
|
}
|
|
|
|
}
|