updated ebiten version from 2.7.9 to 2.9.9
This commit is contained in:
+97
-41
@@ -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]))
|
||||
|
||||
Generated
Vendored
+42
-38
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user