updated ebiten version from 2.7.9 to 2.9.9

This commit is contained in:
2026-06-15 19:06:55 +02:00
parent 21edbc41c4
commit db1b625069
405 changed files with 31913 additions and 12595 deletions
+16 -4
View File
@@ -508,12 +508,24 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl
for _, exp := range e.Exprs[1:] {
args = append(args, expr(&exp))
}
f := expr(&e.Exprs[0])
if f == "texelFetch" {
return fmt.Sprintf("%s(%s, ivec2(%s), 0)", f, args[0], args[1])
callee := e.Exprs[0]
if callee.Type == shaderir.BuiltinFuncExpr {
if c.unit == shaderir.Pixels && callee.BuiltinFunc == shaderir.TexelAt {
return fmt.Sprintf("%s(%s, ivec2(%s), 0)", expr(&callee), args[0], args[1])
}
if callee.BuiltinFunc == shaderir.Min || callee.BuiltinFunc == shaderir.Max {
result := args[0]
for i := 1; i < len(args); i++ {
result = fmt.Sprintf("%s(%s, %s)", expr(&callee), result, args[i])
}
return result
}
if callee.BuiltinFunc == shaderir.FrontFacing {
return "gl_FrontFacing"
}
}
// Using parentheses at the callee is illegal.
return fmt.Sprintf("%s(%s)", f, strings.Join(args, ", "))
return fmt.Sprintf("%s(%s)", expr(&callee), strings.Join(args, ", "))
case shaderir.FieldSelector:
return fmt.Sprintf("(%s).%s", expr(&e.Exprs[0]), expr(&e.Exprs[1]))
case shaderir.Index:
+97 -41
View File
@@ -52,13 +52,7 @@ func (c *compileContext) structName(p *shaderir.Program, t *shaderir.Type) strin
return n
}
const Prelude = `struct Varyings {
float4 Position : SV_POSITION;
float2 M0 : TEXCOORD0;
float4 M1 : COLOR;
};
float mod(float x, float y) {
const utilFuncs = `float mod(float x, float y) {
return x - y * floor(x/y);
}
@@ -86,50 +80,84 @@ float4x4 float4x4FromScalar(float x) {
return float4x4(x, 0, 0, 0, 0, x, 0, 0, 0, 0, x, 0, 0, 0, 0, x);
}`
func Compile(p *shaderir.Program) (vertexShader, pixelShader string, offsets []int) {
offsets = calculateMemoryOffsets(p.Uniforms)
func Compile(p *shaderir.Program) (vertexShader, pixelShader, vertexPrelude, pixelPrelude string) {
offsets := UniformVariableOffsetsInDwords(p)
c := &compileContext{
unit: p.Unit,
}
var lines []string
lines = append(lines, strings.Split(Prelude, "\n")...)
lines = append(lines, "", "{{.Structs}}")
appendPrelude := func(lines []string, vertex bool) []string {
lines = append(lines, strings.Split(utilFuncs, "\n")...)
if len(p.Uniforms) > 0 {
lines = append(lines, "")
lines = append(lines, "cbuffer Uniforms : register(b0) {")
for i, t := range p.Uniforms {
// packingoffset is not mandatory, but this is useful to ensure the correct offset is used.
offset := fmt.Sprintf("c%d", offsets[i]/boundaryInBytes)
switch offsets[i] % boundaryInBytes {
case 4:
offset += ".y"
case 8:
offset += ".z"
case 12:
offset += ".w"
lines = append(lines, "", "struct Varyings {")
lines = append(lines, "\tfloat4 Position : SV_POSITION;")
if len(p.Varyings) > 0 {
for i, v := range p.Varyings {
switch i {
case 0:
lines = append(lines, fmt.Sprintf("\tfloat2 M%d : TEXCOORD;", i))
case 1:
lines = append(lines, fmt.Sprintf("\tfloat4 M%d : COLOR0;", i))
default:
// Use COLOR[n] as a general purpose varying.
if v.Main != shaderir.Vec4 {
lines = append(lines, fmt.Sprintf("\t?(unexpected type: %s) M%d : COLOR%d;", v, i, i-1))
} else {
lines = append(lines, fmt.Sprintf("\tfloat4 M%d : COLOR%d;", i, i-1))
}
}
}
lines = append(lines, fmt.Sprintf("\t%s : packoffset(%s);", c.varDecl(p, &t, fmt.Sprintf("U%d", i)), offset))
}
lines = append(lines, "}")
if !vertex {
lines = append(lines, "\tbool FrontFacing : SV_IsFrontFace;")
}
lines = append(lines, "};")
return lines
}
if p.TextureCount > 0 {
lines = append(lines, "")
for i := 0; i < p.TextureCount; i++ {
lines = append(lines, fmt.Sprintf("Texture2D T%[1]d : register(t%[1]d);", i))
}
if c.unit == shaderir.Texels {
lines = append(lines, "SamplerState samp : register(s0);")
}
}
var vslines, pslines []string
vslines = appendPrelude(vslines, true)
pslines = appendPrelude(pslines, false)
vslines := make([]string, len(lines))
copy(vslines, lines)
pslines := make([]string, len(lines))
copy(pslines, lines)
vertexPrelude = strings.Join(vslines, "\n")
pixelPrelude = strings.Join(pslines, "\n")
appendGlobalVariables := func(lines []string) []string {
lines = append(lines, "", "{{.Structs}}")
if len(p.Uniforms) > 0 {
lines = append(lines, "")
lines = append(lines, "cbuffer Uniforms : register(b0) {")
for i, t := range p.Uniforms {
// packingoffset is not mandatory, but this is useful to ensure the correct offset is used.
offset := fmt.Sprintf("c%d", offsets[i]/UniformVariableBoundaryInDwords)
switch offsets[i] % UniformVariableBoundaryInDwords {
case 1:
offset += ".y"
case 2:
offset += ".z"
case 3:
offset += ".w"
}
lines = append(lines, fmt.Sprintf("\t%s : packoffset(%s);", c.varDecl(p, &t, fmt.Sprintf("U%d", i)), offset))
}
lines = append(lines, "}")
}
if p.TextureCount > 0 {
lines = append(lines, "")
for i := 0; i < p.TextureCount; i++ {
lines = append(lines, fmt.Sprintf("Texture2D T%[1]d : register(t%[1]d);", i))
}
if c.unit == shaderir.Texels {
lines = append(lines, "SamplerState samp : register(s0);")
}
}
return lines
}
vslines = appendGlobalVariables(vslines)
pslines = appendGlobalVariables(pslines)
var vsfuncs []*shaderir.Func
if p.VertexFunc.Block != nil {
@@ -156,7 +184,25 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader string, offsets []i
}
if p.VertexFunc.Block != nil && len(p.VertexFunc.Block.Stmts) > 0 {
vslines = append(vslines, "")
vslines = append(vslines, "Varyings VSMain(float2 A0 : POSITION, float2 A1 : TEXCOORD, float4 A2 : COLOR) {")
var args []string
for i, a := range p.Attributes {
switch i {
case 0:
args = append(args, fmt.Sprintf("float2 A%d : POSITION", i))
case 1:
args = append(args, fmt.Sprintf("float2 A%d : TEXCOORD", i))
case 2:
args = append(args, fmt.Sprintf("float4 A%d : COLOR0", i))
default:
// Use COLOR[n] as a general purpose varying.
if a.Main != shaderir.Vec4 {
args = append(args, fmt.Sprintf("?(unexpected type: %s) A%d : COLOR%d", a, i, i-2))
} else {
args = append(args, fmt.Sprintf("float4 A%d : COLOR%d", i, i-2))
}
}
}
vslines = append(vslines, "Varyings VSMain("+strings.Join(args, ", ")+") {")
vslines = append(vslines, fmt.Sprintf("\tVaryings %s;", vsOut))
vslines = append(vslines, c.block(p, p.VertexFunc.Block, p.VertexFunc.Block, 0)...)
if last := fmt.Sprintf("\treturn %s;", vsOut); vslines[len(vslines)-1] != last {
@@ -480,6 +526,16 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl
}
}
}
if callee.Type == shaderir.BuiltinFuncExpr && (callee.BuiltinFunc == shaderir.Min || callee.BuiltinFunc == shaderir.Max) {
result := args[0]
for i := 1; i < len(args); i++ {
result = fmt.Sprintf("%s(%s, %s)", expr(&callee), result, args[i])
}
return result
}
if callee.Type == shaderir.BuiltinFuncExpr && callee.BuiltinFunc == shaderir.FrontFacing {
return fmt.Sprintf("%s.FrontFacing", vsOut)
}
return fmt.Sprintf("%s(%s)", expr(&e.Exprs[0]), strings.Join(args, ", "))
case shaderir.FieldSelector:
return fmt.Sprintf("(%s).%s", expr(&e.Exprs[0]), expr(&e.Exprs[1]))
@@ -20,69 +20,73 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
)
const boundaryInBytes = 16
const UniformVariableBoundaryInDwords = 4
func calculateMemoryOffsets(uniforms []shaderir.Type) []int {
// UniformVariableOffsetsInDwords returns the offsets of the uniform variables in DWROD units in the HLSL layout.
func UniformVariableOffsetsInDwords(program *shaderir.Program) []int {
// https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-packing-rules
// https://github.com/microsoft/DirectXShaderCompiler/wiki/Buffer-Packing
var offsets []int
var head int
align := func(x int) int {
if x == 0 {
return 0
}
return ((x-1)/boundaryInBytes + 1) * boundaryInBytes
return ((x-1)/UniformVariableBoundaryInDwords + 1) * UniformVariableBoundaryInDwords
}
var offsetsInDwords []int
var headInDwords int
// TODO: Reorder the variables with packoffset.
// See https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-variable-packoffset
for _, u := range uniforms {
for _, u := range program.Uniforms {
switch u.Main {
case shaderir.Bool:
offsetsInDwords = append(offsetsInDwords, headInDwords)
headInDwords += 1
case shaderir.Float:
offsets = append(offsets, head)
head += 4
offsetsInDwords = append(offsetsInDwords, headInDwords)
headInDwords += 1
case shaderir.Int:
offsets = append(offsets, head)
head += 4
offsetsInDwords = append(offsetsInDwords, headInDwords)
headInDwords += 1
case shaderir.Vec2, shaderir.IVec2:
if head%boundaryInBytes >= 4*3 {
head = align(head)
if headInDwords%UniformVariableBoundaryInDwords >= 3 {
headInDwords = align(headInDwords)
}
offsets = append(offsets, head)
head += 4 * 2
offsetsInDwords = append(offsetsInDwords, headInDwords)
headInDwords += 2
case shaderir.Vec3, shaderir.IVec3:
if head%boundaryInBytes >= 4*2 {
head = align(head)
if headInDwords%UniformVariableBoundaryInDwords >= 2 {
headInDwords = align(headInDwords)
}
offsets = append(offsets, head)
head += 4 * 3
offsetsInDwords = append(offsetsInDwords, headInDwords)
headInDwords += 3
case shaderir.Vec4, shaderir.IVec4:
if head%boundaryInBytes >= 4*1 {
head = align(head)
if headInDwords%UniformVariableBoundaryInDwords >= 1 {
headInDwords = align(headInDwords)
}
offsets = append(offsets, head)
head += 4 * 4
offsetsInDwords = append(offsetsInDwords, headInDwords)
headInDwords += 4
case shaderir.Mat2:
// For matrices, each column is aligned to the boundary.
head = align(head)
offsets = append(offsets, head)
head += 4 * 6
headInDwords = align(headInDwords)
offsetsInDwords = append(offsetsInDwords, headInDwords)
headInDwords += 6
case shaderir.Mat3:
head = align(head)
offsets = append(offsets, head)
head += 4 * 11
headInDwords = align(headInDwords)
offsetsInDwords = append(offsetsInDwords, headInDwords)
headInDwords += 11
case shaderir.Mat4:
head = align(head)
offsets = append(offsets, head)
head += 4 * 16
headInDwords = align(headInDwords)
offsetsInDwords = append(offsetsInDwords, headInDwords)
headInDwords += 16
case shaderir.Array:
// Each array is 16-byte aligned.
// TODO: What if the array has 2 or more dimensions?
head = align(head)
offsets = append(offsets, head)
n := u.Sub[0].Uint32Count()
headInDwords = align(headInDwords)
offsetsInDwords = append(offsetsInDwords, headInDwords)
n := u.Sub[0].DwordCount()
switch u.Sub[0].Main {
case shaderir.Mat2:
n = 6
@@ -91,9 +95,9 @@ func calculateMemoryOffsets(uniforms []shaderir.Type) []int {
case shaderir.Mat4:
n = 16
}
head += (u.Length - 1) * align(4*n)
headInDwords += (u.Length - 1) * align(n)
// The last element is not with a padding.
head += 4 * n
headInDwords += n
case shaderir.Struct:
// TODO: Implement this
panic("hlsl: offset for a struct is not implemented yet")
@@ -102,5 +106,5 @@ func calculateMemoryOffsets(uniforms []shaderir.Type) []int {
}
}
return offsets
return offsetsInDwords
}
+32 -9
View File
@@ -79,6 +79,15 @@ func Compile(p *shaderir.Program) (shader string) {
lines = append(lines, strings.Split(Prelude(p.Unit), "\n")...)
lines = append(lines, "", "{{.Structs}}")
if len(p.Uniforms) > 0 {
lines = append(lines, "")
lines = append(lines, "struct Uniforms {")
for i, u := range p.Uniforms {
lines = append(lines, fmt.Sprintf("\t%s;", c.varDecl(p, &u, fmt.Sprintf("U%d", i), false)))
}
lines = append(lines, "};")
}
if len(p.Attributes) > 0 {
lines = append(lines, "")
lines = append(lines, "struct Attributes {")
@@ -117,9 +126,9 @@ func Compile(p *shaderir.Program) (shader string) {
fmt.Sprintf("vertex Varyings %s(", VertexName),
"\tuint vid [[vertex_id]],",
"\tconst device Attributes* attributes [[buffer(0)]]")
for i, u := range p.Uniforms {
if len(p.Uniforms) > 0 {
lines[len(lines)-1] += ","
lines = append(lines, fmt.Sprintf("\tconstant %s [[buffer(%d)]]", c.varDecl(p, &u, fmt.Sprintf("U%d", i), true), i+1))
lines = append(lines, "\tconstant Uniforms& uniforms [[buffer(1)]]")
}
for i := 0; i < p.TextureCount; i++ {
lines[len(lines)-1] += ","
@@ -139,14 +148,16 @@ func Compile(p *shaderir.Program) (shader string) {
lines = append(lines,
fmt.Sprintf("fragment float4 %s(", FragmentName),
"\tVaryings varyings [[stage_in]]")
for i, u := range p.Uniforms {
if len(p.Uniforms) > 0 {
lines[len(lines)-1] += ","
lines = append(lines, fmt.Sprintf("\tconstant %s [[buffer(%d)]]", c.varDecl(p, &u, fmt.Sprintf("U%d", i), true), i+1))
lines = append(lines, "\tconstant Uniforms& uniforms [[buffer(0)]]")
}
for i := 0; i < p.TextureCount; i++ {
lines[len(lines)-1] += ","
lines = append(lines, fmt.Sprintf("\ttexture2d<float> T%[1]d [[texture(%[1]d)]]", i))
}
lines[len(lines)-1] += ","
lines = append(lines, "\tbool front_facing [[front_facing]]")
lines[len(lines)-1] += ") {"
lines = append(lines, c.block(p, p.FragmentFunc.Block, p.FragmentFunc.Block, 0)...)
lines = append(lines, "}")
@@ -229,12 +240,13 @@ func (c *compileContext) function(p *shaderir.Program, f *shaderir.Func, prototy
var args []string
// Uniform variables and texture variables. In Metal, non-const global variables are not available.
for i, u := range p.Uniforms {
args = append(args, "constant "+c.varDecl(p, &u, fmt.Sprintf("U%d", i), true))
if len(p.Uniforms) > 0 {
args = append(args, "constant Uniforms& uniforms")
}
for i := 0; i < p.TextureCount; i++ {
args = append(args, fmt.Sprintf("texture2d<float> T%d", i))
}
args = append(args, "bool front_facing")
var idx int
for _, t := range f.InParams {
@@ -350,7 +362,7 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl
case shaderir.NumberExpr:
return constantToNumberLiteral(e.Const)
case shaderir.UniformVariable:
return fmt.Sprintf("U%d", e.Index)
return fmt.Sprintf("uniforms.U%d", e.Index)
case shaderir.TextureVariable:
return fmt.Sprintf("T%d", e.Index)
case shaderir.LocalVariable:
@@ -389,12 +401,13 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl
callee := e.Exprs[0]
var args []string
if callee.Type != shaderir.BuiltinFuncExpr {
for i := range p.Uniforms {
args = append(args, fmt.Sprintf("U%d", i))
if len(p.Uniforms) > 0 {
args = append(args, "uniforms")
}
for i := 0; i < p.TextureCount; i++ {
args = append(args, fmt.Sprintf("T%d", i))
}
args = append(args, "front_facing")
}
for _, exp := range e.Exprs[1:] {
args = append(args, expr(&exp))
@@ -409,6 +422,16 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl
panic(fmt.Sprintf("msl: unexpected unit: %d", p.Unit))
}
}
if callee.Type == shaderir.BuiltinFuncExpr && (callee.BuiltinFunc == shaderir.Min || callee.BuiltinFunc == shaderir.Max) {
result := args[0]
for i := 1; i < len(args); i++ {
result = fmt.Sprintf("%s(%s, %s)", expr(&callee), result, args[i])
}
return result
}
if callee.Type == shaderir.BuiltinFuncExpr && callee.BuiltinFunc == shaderir.FrontFacing {
return "(front_facing)"
}
return fmt.Sprintf("%s(%s)", expr(&callee), strings.Join(args, ", "))
case shaderir.FieldSelector:
return fmt.Sprintf("(%s).%s", expr(&e.Exprs[0]), expr(&e.Exprs[1]))
+27 -5
View File
@@ -16,8 +16,11 @@
package shaderir
import (
"bytes"
"encoding/hex"
"go/constant"
"go/token"
"hash/fnv"
"sort"
"strings"
)
@@ -29,6 +32,21 @@ const (
Pixels
)
type SourceHash [16]byte
func CalcSourceHash(source []byte) SourceHash {
h := fnv.New128a()
_, _ = h.Write(bytes.TrimSpace(source))
var hash SourceHash
h.Sum(hash[:0])
return hash
}
func (s SourceHash) String() string {
return hex.EncodeToString(s[:])
}
type Program struct {
UniformNames []string
Uniforms []Type
@@ -40,6 +58,8 @@ type Program struct {
FragmentFunc FragmentFunc
Unit Unit
SourceHash SourceHash
uniformFactors []uint32
}
@@ -274,6 +294,7 @@ const (
Fwidth BuiltinFunc = "fwidth"
DiscardF BuiltinFunc = "discard"
TexelAt BuiltinFunc = "__texelAt"
FrontFacing BuiltinFunc = "frontfacing"
)
func ParseBuiltinFunc(str string) (BuiltinFunc, bool) {
@@ -331,7 +352,8 @@ func ParseBuiltinFunc(str string) (BuiltinFunc, bool) {
Dfdy,
Fwidth,
DiscardF,
TexelAt:
TexelAt,
FrontFacing:
return BuiltinFunc(str), true
}
return "", false
@@ -351,21 +373,21 @@ func IsValidSwizzling(s string) bool {
switch {
case strings.IndexByte(xyzw, s[0]) >= 0:
for _, c := range s {
if strings.IndexRune(xyzw, c) == -1 {
if !strings.ContainsRune(xyzw, c) {
return false
}
}
return true
case strings.IndexByte(rgba, s[0]) >= 0:
for _, c := range s {
if strings.IndexRune(rgba, c) == -1 {
if !strings.ContainsRune(rgba, c) {
return false
}
}
return true
case strings.IndexByte(strq, s[0]) >= 0:
for _, c := range s {
if strings.IndexRune(strq, c) == -1 {
if !strings.ContainsRune(strq, c) {
return false
}
}
@@ -474,7 +496,7 @@ func (p *Program) FilterUniformVariables(uniforms []uint32) {
p.uniformFactors = make([]uint32, len(uniforms))
var idx int
for i, typ := range p.Uniforms {
c := typ.Uint32Count()
c := typ.DwordCount()
if reachableUniforms[i] {
for i := idx; i < idx+c; i++ {
p.uniformFactors[i] = 1
+24 -8
View File
@@ -25,7 +25,7 @@ type Type struct {
Length int
}
func (t *Type) Equal(rhs *Type) bool {
func (t Type) Equal(rhs *Type) bool {
if t.Main != rhs.Main {
return false
}
@@ -43,7 +43,7 @@ func (t *Type) Equal(rhs *Type) bool {
return true
}
func (t *Type) String() string {
func (t Type) String() string {
switch t.Main {
case None:
return "none"
@@ -87,8 +87,11 @@ func (t *Type) String() string {
}
}
func (t *Type) Uint32Count() int {
func (t Type) DwordCount() int {
switch t.Main {
case Bool:
// The size of a bool varies by the shader language, but use 1 for simplicity.
return 1
case Int:
return 1
case Float:
@@ -112,13 +115,13 @@ func (t *Type) Uint32Count() int {
case Mat4:
return 16
case Array:
return t.Length * t.Sub[0].Uint32Count()
return t.Length * t.Sub[0].DwordCount()
default: // TODO: Parse a struct correctly
return -1
}
}
func (t *Type) IsFloatVector() bool {
func (t Type) IsFloatVector() bool {
switch t.Main {
case Vec2, Vec3, Vec4:
return true
@@ -126,7 +129,7 @@ func (t *Type) IsFloatVector() bool {
return false
}
func (t *Type) IsIntVector() bool {
func (t Type) IsIntVector() bool {
switch t.Main {
case IVec2, IVec3, IVec4:
return true
@@ -134,7 +137,7 @@ func (t *Type) IsIntVector() bool {
return false
}
func (t *Type) VectorElementCount() int {
func (t Type) VectorElementCount() int {
switch t.Main {
case Vec2:
return 2
@@ -153,7 +156,7 @@ func (t *Type) VectorElementCount() int {
}
}
func (t *Type) IsMatrix() bool {
func (t Type) IsMatrix() bool {
switch t.Main {
case Mat2, Mat3, Mat4:
return true
@@ -161,6 +164,19 @@ func (t *Type) IsMatrix() bool {
return false
}
func (t Type) MatrixSize() int {
switch t.Main {
case Mat2:
return 2
case Mat3:
return 3
case Mat4:
return 4
default:
return -1
}
}
type BasicType int
const (