mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
parent
b96b75d51c
commit
4893b13acc
@ -26,6 +26,7 @@ import (
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/shader"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/glsl"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/hlsl"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/msl"
|
||||
)
|
||||
|
||||
@ -45,6 +46,13 @@ func glslFragmentNormalize(str string) string {
|
||||
return strings.TrimSpace(str)
|
||||
}
|
||||
|
||||
func hlslNormalize(str string) string {
|
||||
if strings.HasPrefix(str, hlsl.Prelude) {
|
||||
str = str[len(hlsl.Prelude):]
|
||||
}
|
||||
return strings.TrimSpace(str)
|
||||
}
|
||||
|
||||
func metalNormalize(str string) string {
|
||||
if strings.HasPrefix(str, msl.Prelude) {
|
||||
str = str[len(msl.Prelude):]
|
||||
@ -88,6 +96,7 @@ func TestCompile(t *testing.T) {
|
||||
Src []byte
|
||||
VS []byte
|
||||
FS []byte
|
||||
HLSL []byte
|
||||
Metal []byte
|
||||
}
|
||||
|
||||
@ -138,6 +147,15 @@ func TestCompile(t *testing.T) {
|
||||
t.Fatalf("no expected file for %s", name)
|
||||
}
|
||||
|
||||
hlsln := name + ".expected.hlsl"
|
||||
if _, ok := fnames[hlsln]; ok {
|
||||
hlsl, err := ioutil.ReadFile(filepath.Join("testdata", hlsln))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tc.HLSL = hlsl
|
||||
}
|
||||
|
||||
metaln := name + ".expected.metal"
|
||||
if _, ok := fnames[metaln]; ok {
|
||||
metal, err := ioutil.ReadFile(filepath.Join("testdata", metaln))
|
||||
@ -175,6 +193,13 @@ func TestCompile(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
if tc.HLSL != nil {
|
||||
h, _ := hlsl.Compile(s)
|
||||
if got, want := hlslNormalize(h), hlslNormalize(string(tc.HLSL)); got != want {
|
||||
compare(t, "HLSL", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
if tc.Metal != nil {
|
||||
m := msl.Compile(s, "Vertex", "Fragment")
|
||||
if got, want := metalNormalize(m), metalNormalize(string(tc.Metal)); got != want {
|
||||
|
6
internal/shader/testdata/issue1236.expected.hlsl
vendored
Normal file
6
internal/shader/testdata/issue1236.expected.hlsl
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
void F0(in float4 l0, out float4 l1) {
|
||||
float4 l2 = 0.0;
|
||||
l2 = mul(l0, (float4x4)(1.0));
|
||||
l1 = l2;
|
||||
return;
|
||||
}
|
9
internal/shader/testdata/out.expected.hlsl
vendored
Normal file
9
internal/shader/testdata/out.expected.hlsl
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
void F0(out float l0, out float l1[4], out float4 l2) {
|
||||
l0 = 0.0;
|
||||
l1[0] = 0.0;
|
||||
l1[1] = 0.0;
|
||||
l1[2] = 0.0;
|
||||
l1[3] = 0.0;
|
||||
l2 = 0.0;
|
||||
return;
|
||||
}
|
16
internal/shader/testdata/vertex.expected.hlsl
vendored
Normal file
16
internal/shader/testdata/vertex.expected.hlsl
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
cbuffer Uniforms : register(b0) {
|
||||
float2 U0 : packoffset(c0);
|
||||
}
|
||||
|
||||
Varyings VSMain(float2 A0 : POSITION, float2 A1 : TEXCOORD, float4 A2 : COLOR) {
|
||||
Varyings varyings;
|
||||
float4x4 l0 = 0.0;
|
||||
varyings.Position = 0.0;
|
||||
varyings.M0 = 0.0;
|
||||
varyings.M1 = 0.0;
|
||||
l0 = float4x4((2.0) / ((U0).x), 0.0, 0.0, 0.0, 0.0, (2.0) / ((U0).y), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -1.0, -1.0, 0.0, 1.0);
|
||||
varyings.Position = mul(float4(A0, 0.0, 1.0), l0);
|
||||
varyings.M0 = A1;
|
||||
varyings.M1 = A2;
|
||||
return varyings;
|
||||
}
|
514
internal/shaderir/hlsl/hlsl.go
Normal file
514
internal/shaderir/hlsl/hlsl.go
Normal file
@ -0,0 +1,514 @@
|
||||
// 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 hlsl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/constant"
|
||||
"go/token"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
||||
)
|
||||
|
||||
const (
|
||||
vsOut = "varyings"
|
||||
psOut = "psOut"
|
||||
)
|
||||
|
||||
type compileContext struct {
|
||||
structNames map[string]string
|
||||
structTypes []shaderir.Type
|
||||
}
|
||||
|
||||
func (c *compileContext) structName(p *shaderir.Program, t *shaderir.Type) string {
|
||||
if t.Main != shaderir.Struct {
|
||||
panic("hlsl: the given type at structName must be a struct")
|
||||
}
|
||||
s := t.String()
|
||||
if n, ok := c.structNames[s]; ok {
|
||||
return n
|
||||
}
|
||||
n := fmt.Sprintf("S%d", len(c.structNames))
|
||||
if c.structNames == nil {
|
||||
c.structNames = map[string]string{}
|
||||
}
|
||||
c.structNames[s] = n
|
||||
c.structTypes = append(c.structTypes, *t)
|
||||
return n
|
||||
}
|
||||
|
||||
const Prelude = `struct Varyings {
|
||||
float4 Position : SV_POSITION;
|
||||
float2 M0 : TEXCOORD0;
|
||||
float4 M1 : COLOR;
|
||||
};
|
||||
|
||||
float mod(float x, float y) {
|
||||
return x - y * floor(x/y);
|
||||
}
|
||||
|
||||
float2 mod(float2 x, float2 y) {
|
||||
return x - y * floor(x/y);
|
||||
}
|
||||
|
||||
float3 mod(float3 x, float3 y) {
|
||||
return x - y * floor(x/y);
|
||||
}
|
||||
|
||||
float4 mod(float4 x, float4 y) {
|
||||
return x - y * floor(x/y);
|
||||
}`
|
||||
|
||||
func Compile(p *shaderir.Program) (string, []int) {
|
||||
c := &compileContext{}
|
||||
|
||||
var lines []string
|
||||
lines = append(lines, strings.Split(Prelude, "\n")...)
|
||||
lines = append(lines, "", "{{.Structs}}")
|
||||
|
||||
var offsets []int
|
||||
if len(p.Uniforms) > 0 {
|
||||
offsets = calculateMemoryOffsets(p.Uniforms)
|
||||
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, fmt.Sprintf("\t%s : packoffset(%s);", c.varDecl(p, &t, fmt.Sprintf("U%d", i)), offset))
|
||||
}
|
||||
lines = append(lines, "}")
|
||||
}
|
||||
if p.TextureNum > 0 {
|
||||
lines = append(lines, "")
|
||||
for i := 0; i < p.TextureNum; i++ {
|
||||
lines = append(lines, fmt.Sprintf("Texture2D T%[1]d : register(t%[1]d);", i))
|
||||
}
|
||||
lines = append(lines, "SamplerState samp : register(s0);")
|
||||
}
|
||||
|
||||
if len(p.Funcs) > 0 {
|
||||
lines = append(lines, "")
|
||||
for _, f := range p.Funcs {
|
||||
if len(lines) > 0 && lines[len(lines)-1] != "" {
|
||||
lines = append(lines, "")
|
||||
}
|
||||
lines = append(lines, c.function(p, &f, false)...)
|
||||
}
|
||||
}
|
||||
|
||||
if p.VertexFunc.Block != nil && len(p.VertexFunc.Block.Stmts) > 0 {
|
||||
lines = append(lines, "")
|
||||
lines = append(lines, "Varyings VSMain(float2 A0 : POSITION, float2 A1 : TEXCOORD, float4 A2 : COLOR) {")
|
||||
lines = append(lines, fmt.Sprintf("\tVaryings %s;", vsOut))
|
||||
lines = append(lines, c.block(p, p.VertexFunc.Block, p.VertexFunc.Block, 0)...)
|
||||
if last := fmt.Sprintf("\treturn %s;", vsOut); lines[len(lines)-1] != last {
|
||||
lines = append(lines, last)
|
||||
}
|
||||
lines = append(lines, "}")
|
||||
}
|
||||
|
||||
if p.FragmentFunc.Block != nil && len(p.FragmentFunc.Block.Stmts) > 0 {
|
||||
lines = append(lines, "")
|
||||
lines = append(lines, fmt.Sprintf("float4 PSMain(Varyings %s) : SV_TARGET {", vsOut))
|
||||
lines = append(lines, fmt.Sprintf("\tfloat4 %s = 0.0;", psOut))
|
||||
lines = append(lines, c.block(p, p.FragmentFunc.Block, p.FragmentFunc.Block, 0)...)
|
||||
if last := fmt.Sprintf("\treturn %s;", psOut); lines[len(lines)-1] != last {
|
||||
lines = append(lines, last)
|
||||
}
|
||||
lines = append(lines, "}")
|
||||
}
|
||||
|
||||
ls := strings.Join(lines, "\n")
|
||||
|
||||
// Struct types are determined after converting the program.
|
||||
if len(c.structTypes) > 0 {
|
||||
var stlines []string
|
||||
for i, t := range c.structTypes {
|
||||
stlines = append(stlines, fmt.Sprintf("struct S%d {", i))
|
||||
for j, st := range t.Sub {
|
||||
stlines = append(stlines, fmt.Sprintf("\t%s;", c.varDecl(p, &st, fmt.Sprintf("M%d", j))))
|
||||
}
|
||||
stlines = append(stlines, "};")
|
||||
}
|
||||
st := strings.Join(stlines, "\n")
|
||||
ls = strings.ReplaceAll(ls, "{{.Structs}}", st)
|
||||
} else {
|
||||
ls = strings.ReplaceAll(ls, "{{.Structs}}", "")
|
||||
}
|
||||
|
||||
nls := regexp.MustCompile(`\n\n+`)
|
||||
ls = nls.ReplaceAllString(ls, "\n\n")
|
||||
|
||||
ls = strings.TrimSpace(ls) + "\n"
|
||||
|
||||
return ls, offsets
|
||||
}
|
||||
|
||||
func (c *compileContext) typ(p *shaderir.Program, t *shaderir.Type) (string, string) {
|
||||
switch t.Main {
|
||||
case shaderir.None:
|
||||
return "void", ""
|
||||
case shaderir.Struct:
|
||||
return c.structName(p, t), ""
|
||||
default:
|
||||
return typeString(t)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *compileContext) varDecl(p *shaderir.Program, t *shaderir.Type, varname string) string {
|
||||
switch t.Main {
|
||||
case shaderir.None:
|
||||
return "?(none)"
|
||||
case shaderir.Struct:
|
||||
return fmt.Sprintf("%s %s", c.structName(p, t), varname)
|
||||
default:
|
||||
t0, t1 := typeString(t)
|
||||
return fmt.Sprintf("%s %s%s", t0, varname, t1)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *compileContext) varInit(p *shaderir.Program, t *shaderir.Type) string {
|
||||
switch t.Main {
|
||||
case shaderir.None:
|
||||
return "?(none)"
|
||||
case shaderir.Array:
|
||||
init := c.varInit(p, &t.Sub[0])
|
||||
es := make([]string, 0, t.Length)
|
||||
for i := 0; i < t.Length; i++ {
|
||||
es = append(es, init)
|
||||
}
|
||||
t0, t1 := typeString(t)
|
||||
return fmt.Sprintf("%s%s(%s)", t0, t1, strings.Join(es, ", "))
|
||||
case shaderir.Struct:
|
||||
panic("not implemented")
|
||||
case shaderir.Bool:
|
||||
return "false"
|
||||
case shaderir.Int:
|
||||
return "0"
|
||||
case shaderir.Float, shaderir.Vec2, shaderir.Vec3, shaderir.Vec4, shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
|
||||
return "0.0"
|
||||
default:
|
||||
t0, t1 := c.typ(p, t)
|
||||
panic(fmt.Sprintf("?(unexpected type: %s%s)", t0, t1))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *compileContext) function(p *shaderir.Program, f *shaderir.Func, prototype bool) []string {
|
||||
var args []string
|
||||
var idx int
|
||||
for _, t := range f.InParams {
|
||||
args = append(args, "in "+c.varDecl(p, &t, fmt.Sprintf("l%d", idx)))
|
||||
idx++
|
||||
}
|
||||
for _, t := range f.OutParams {
|
||||
args = append(args, "out "+c.varDecl(p, &t, fmt.Sprintf("l%d", idx)))
|
||||
idx++
|
||||
}
|
||||
argsstr := "void"
|
||||
if len(args) > 0 {
|
||||
argsstr = strings.Join(args, ", ")
|
||||
}
|
||||
|
||||
t0, t1 := c.typ(p, &f.Return)
|
||||
sig := fmt.Sprintf("%s%s F%d(%s)", t0, t1, f.Index, argsstr)
|
||||
|
||||
var lines []string
|
||||
if prototype {
|
||||
lines = append(lines, fmt.Sprintf("%s;", sig))
|
||||
return lines
|
||||
}
|
||||
lines = append(lines, fmt.Sprintf("%s {", sig))
|
||||
lines = append(lines, c.block(p, f.Block, f.Block, 0)...)
|
||||
lines = append(lines, "}")
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
func constantToNumberLiteral(t shaderir.ConstType, v constant.Value) string {
|
||||
switch t {
|
||||
case shaderir.ConstTypeNone:
|
||||
if v.Kind() == constant.Bool {
|
||||
if constant.BoolVal(v) {
|
||||
return "true"
|
||||
}
|
||||
return "false"
|
||||
}
|
||||
fallthrough
|
||||
case shaderir.ConstTypeFloat:
|
||||
if i := constant.ToInt(v); i.Kind() == constant.Int {
|
||||
x, _ := constant.Int64Val(i)
|
||||
return fmt.Sprintf("%d.0", x)
|
||||
}
|
||||
if i := constant.ToFloat(v); i.Kind() == constant.Float {
|
||||
x, _ := constant.Float64Val(i)
|
||||
return fmt.Sprintf("%.10e", x)
|
||||
}
|
||||
case shaderir.ConstTypeInt:
|
||||
if i := constant.ToInt(v); i.Kind() == constant.Int {
|
||||
x, _ := constant.Int64Val(i)
|
||||
return fmt.Sprintf("%d", x)
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("?(unexpected literal: %s)", v)
|
||||
}
|
||||
|
||||
func (c *compileContext) localVariableName(p *shaderir.Program, topBlock *shaderir.Block, idx int) string {
|
||||
switch topBlock {
|
||||
case p.VertexFunc.Block:
|
||||
na := len(p.Attributes)
|
||||
nv := len(p.Varyings)
|
||||
switch {
|
||||
case idx < na:
|
||||
return fmt.Sprintf("A%d", idx)
|
||||
case idx == na:
|
||||
return fmt.Sprintf("%s.Position", vsOut)
|
||||
case idx < na+nv+1:
|
||||
return fmt.Sprintf("%s.M%d", vsOut, idx-na-1)
|
||||
default:
|
||||
return fmt.Sprintf("l%d", idx-(na+nv+1))
|
||||
}
|
||||
case p.FragmentFunc.Block:
|
||||
nv := len(p.Varyings)
|
||||
switch {
|
||||
case idx == 0:
|
||||
return fmt.Sprintf("%s.Position", vsOut)
|
||||
case idx < nv+1:
|
||||
return fmt.Sprintf("%s.M%d", vsOut, idx-1)
|
||||
case idx == nv+1:
|
||||
return psOut
|
||||
default:
|
||||
return fmt.Sprintf("l%d", idx-(nv+2))
|
||||
}
|
||||
default:
|
||||
return fmt.Sprintf("l%d", idx)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *compileContext) initVariable(p *shaderir.Program, topBlock, block *shaderir.Block, index int, decl bool, level int) []string {
|
||||
idt := strings.Repeat("\t", level+1)
|
||||
name := c.localVariableName(p, topBlock, index)
|
||||
t := p.LocalVariableType(topBlock, block, index)
|
||||
|
||||
var lines []string
|
||||
switch t.Main {
|
||||
case shaderir.Array:
|
||||
if decl {
|
||||
lines = append(lines, fmt.Sprintf("%s%s;", idt, c.varDecl(p, &t, name)))
|
||||
}
|
||||
init := c.varInit(p, &t.Sub[0])
|
||||
for i := 0; i < t.Length; i++ {
|
||||
lines = append(lines, fmt.Sprintf("%s%s[%d] = %s;", idt, name, i, init))
|
||||
}
|
||||
case shaderir.None:
|
||||
// The type is None e.g., when the variable is a for-loop counter.
|
||||
default:
|
||||
if decl {
|
||||
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, c.varDecl(p, &t, name), c.varInit(p, &t)))
|
||||
} else {
|
||||
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, name, c.varInit(p, &t)))
|
||||
}
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Block, level int) []string {
|
||||
if block == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var lines []string
|
||||
for i := range block.LocalVars {
|
||||
lines = append(lines, c.initVariable(p, topBlock, block, block.LocalVarIndexOffset+i, true, level)...)
|
||||
}
|
||||
|
||||
var expr func(e *shaderir.Expr) string
|
||||
expr = func(e *shaderir.Expr) string {
|
||||
switch e.Type {
|
||||
case shaderir.NumberExpr:
|
||||
return constantToNumberLiteral(e.ConstType, e.Const)
|
||||
case shaderir.UniformVariable:
|
||||
return fmt.Sprintf("U%d", e.Index)
|
||||
case shaderir.TextureVariable:
|
||||
return fmt.Sprintf("T%d", e.Index)
|
||||
case shaderir.LocalVariable:
|
||||
return c.localVariableName(p, topBlock, e.Index)
|
||||
case shaderir.StructMember:
|
||||
return fmt.Sprintf("M%d", e.Index)
|
||||
case shaderir.BuiltinFuncExpr:
|
||||
return c.builtinFuncString(e.BuiltinFunc)
|
||||
case shaderir.SwizzlingExpr:
|
||||
if !shaderir.IsValidSwizzling(e.Swizzling) {
|
||||
return fmt.Sprintf("?(unexpected swizzling: %s)", e.Swizzling)
|
||||
}
|
||||
return e.Swizzling
|
||||
case shaderir.FunctionExpr:
|
||||
return fmt.Sprintf("F%d", e.Index)
|
||||
case shaderir.Unary:
|
||||
var op string
|
||||
switch e.Op {
|
||||
case shaderir.Add, shaderir.Sub, shaderir.NotOp:
|
||||
op = opString(e.Op)
|
||||
default:
|
||||
op = fmt.Sprintf("?(unexpected op: %d)", e.Op)
|
||||
}
|
||||
return fmt.Sprintf("%s(%s)", op, expr(&e.Exprs[0]))
|
||||
case shaderir.Binary:
|
||||
if e.Op == shaderir.MatrixMul {
|
||||
// If either is a matrix, use the mul function.
|
||||
// Swap the order of the lhs and the rhs since matrices are row-major in HLSL.
|
||||
return fmt.Sprintf("mul(%s, %s)", expr(&e.Exprs[1]), expr(&e.Exprs[0]))
|
||||
}
|
||||
return fmt.Sprintf("(%s) %s (%s)", expr(&e.Exprs[0]), opString(e.Op), expr(&e.Exprs[1]))
|
||||
case shaderir.Selection:
|
||||
return fmt.Sprintf("(%s) ? (%s) : (%s)", expr(&e.Exprs[0]), expr(&e.Exprs[1]), expr(&e.Exprs[2]))
|
||||
case shaderir.Call:
|
||||
callee := e.Exprs[0]
|
||||
var args []string
|
||||
for _, exp := range e.Exprs[1:] {
|
||||
args = append(args, expr(&exp))
|
||||
}
|
||||
if callee.Type == shaderir.BuiltinFuncExpr {
|
||||
switch callee.BuiltinFunc {
|
||||
case shaderir.Vec2F, shaderir.Vec3F, shaderir.Vec4F:
|
||||
if len(args) == 1 {
|
||||
// Use casting. For example, `float4(1)` doesn't work.
|
||||
return fmt.Sprintf("(%s)(%s)", expr(&e.Exprs[0]), args[0])
|
||||
}
|
||||
case shaderir.Mat2F, shaderir.Mat3F, shaderir.Mat4F:
|
||||
if len(args) == 1 {
|
||||
// Use casting. For example, `float4x4(1)` doesn't work.
|
||||
return fmt.Sprintf("(%s)(%s)", expr(&e.Exprs[0]), args[0])
|
||||
}
|
||||
case shaderir.Texture2DF:
|
||||
return fmt.Sprintf("%s.Sample(samp, %s)", args[0], strings.Join(args[1:], ", "))
|
||||
}
|
||||
}
|
||||
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]))
|
||||
case shaderir.Index:
|
||||
return fmt.Sprintf("(%s)[%s]", expr(&e.Exprs[0]), expr(&e.Exprs[1]))
|
||||
default:
|
||||
return fmt.Sprintf("?(unexpected expr: %d)", e.Type)
|
||||
}
|
||||
}
|
||||
|
||||
idt := strings.Repeat("\t", level+1)
|
||||
for _, s := range block.Stmts {
|
||||
switch s.Type {
|
||||
case shaderir.ExprStmt:
|
||||
lines = append(lines, fmt.Sprintf("%s%s;", idt, expr(&s.Exprs[0])))
|
||||
case shaderir.BlockStmt:
|
||||
lines = append(lines, idt+"{")
|
||||
lines = append(lines, c.block(p, topBlock, s.Blocks[0], level+1)...)
|
||||
lines = append(lines, idt+"}")
|
||||
case shaderir.Assign:
|
||||
lhs := s.Exprs[0]
|
||||
rhs := s.Exprs[1]
|
||||
if lhs.Type == shaderir.LocalVariable {
|
||||
if t := p.LocalVariableType(topBlock, block, lhs.Index); t.Main == shaderir.Array {
|
||||
for i := 0; i < t.Length; i++ {
|
||||
lines = append(lines, fmt.Sprintf("%[1]s%[2]s[%[3]d] = %[4]s[%[3]d];", idt, expr(&lhs), i, expr(&rhs)))
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, expr(&lhs), expr(&rhs)))
|
||||
case shaderir.Init:
|
||||
lines = append(lines, c.initVariable(p, topBlock, block, s.InitIndex, false, level)...)
|
||||
case shaderir.If:
|
||||
lines = append(lines, fmt.Sprintf("%sif (%s) {", idt, expr(&s.Exprs[0])))
|
||||
lines = append(lines, c.block(p, topBlock, s.Blocks[0], level+1)...)
|
||||
if len(s.Blocks) > 1 {
|
||||
lines = append(lines, fmt.Sprintf("%s} else {", idt))
|
||||
lines = append(lines, c.block(p, topBlock, s.Blocks[1], level+1)...)
|
||||
}
|
||||
lines = append(lines, fmt.Sprintf("%s}", idt))
|
||||
case shaderir.For:
|
||||
var ct shaderir.ConstType
|
||||
switch s.ForVarType.Main {
|
||||
case shaderir.Int:
|
||||
ct = shaderir.ConstTypeInt
|
||||
case shaderir.Float:
|
||||
ct = shaderir.ConstTypeFloat
|
||||
}
|
||||
|
||||
v := c.localVariableName(p, topBlock, s.ForVarIndex)
|
||||
var delta string
|
||||
switch val, _ := constant.Float64Val(s.ForDelta); val {
|
||||
case 0:
|
||||
delta = fmt.Sprintf("?(unexpected delta: %v)", s.ForDelta)
|
||||
case 1:
|
||||
delta = fmt.Sprintf("%s++", v)
|
||||
case -1:
|
||||
delta = fmt.Sprintf("%s--", v)
|
||||
default:
|
||||
d := s.ForDelta
|
||||
if val > 0 {
|
||||
delta = fmt.Sprintf("%s += %s", v, constantToNumberLiteral(ct, d))
|
||||
} else {
|
||||
d = constant.UnaryOp(token.SUB, d, 0)
|
||||
delta = fmt.Sprintf("%s -= %s", v, constantToNumberLiteral(ct, d))
|
||||
}
|
||||
}
|
||||
var op string
|
||||
switch s.ForOp {
|
||||
case shaderir.LessThanOp, shaderir.LessThanEqualOp, shaderir.GreaterThanOp, shaderir.GreaterThanEqualOp, shaderir.EqualOp, shaderir.NotEqualOp:
|
||||
op = opString(s.ForOp)
|
||||
default:
|
||||
op = fmt.Sprintf("?(unexpected op: %d)", s.ForOp)
|
||||
}
|
||||
|
||||
t := s.ForVarType
|
||||
init := constantToNumberLiteral(ct, s.ForInit)
|
||||
end := constantToNumberLiteral(ct, s.ForEnd)
|
||||
t0, t1 := typeString(&t)
|
||||
lines = append(lines, fmt.Sprintf("%sfor (%s %s%s = %s; %s %s %s; %s) {", idt, t0, v, t1, init, v, op, end, delta))
|
||||
lines = append(lines, c.block(p, topBlock, s.Blocks[0], level+1)...)
|
||||
lines = append(lines, fmt.Sprintf("%s}", idt))
|
||||
case shaderir.Continue:
|
||||
lines = append(lines, idt+"continue;")
|
||||
case shaderir.Break:
|
||||
lines = append(lines, idt+"break;")
|
||||
case shaderir.Return:
|
||||
switch {
|
||||
case topBlock == p.VertexFunc.Block:
|
||||
lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, vsOut))
|
||||
case topBlock == p.FragmentFunc.Block:
|
||||
lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, psOut))
|
||||
case len(s.Exprs) == 0:
|
||||
lines = append(lines, idt+"return;")
|
||||
default:
|
||||
lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, expr(&s.Exprs[0])))
|
||||
}
|
||||
case shaderir.Discard:
|
||||
lines = append(lines, idt+"discard;")
|
||||
default:
|
||||
lines = append(lines, fmt.Sprintf("%s?(unexpected stmt: %d)", idt, s.Type))
|
||||
}
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
98
internal/shaderir/hlsl/packing.go
Normal file
98
internal/shaderir/hlsl/packing.go
Normal file
@ -0,0 +1,98 @@
|
||||
// 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 hlsl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
||||
)
|
||||
|
||||
const boundaryInBytes = 16
|
||||
|
||||
func calculateMemoryOffsets(uniforms []shaderir.Type) []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
|
||||
}
|
||||
|
||||
// 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 {
|
||||
switch u.Main {
|
||||
case shaderir.Float:
|
||||
offsets = append(offsets, head)
|
||||
head += 4
|
||||
case shaderir.Vec2:
|
||||
if head%boundaryInBytes >= 4*3 {
|
||||
head = align(head)
|
||||
}
|
||||
offsets = append(offsets, head)
|
||||
head += 4 * 2
|
||||
case shaderir.Vec3:
|
||||
if head%boundaryInBytes >= 4*2 {
|
||||
head = align(head)
|
||||
}
|
||||
offsets = append(offsets, head)
|
||||
head += 4 * 3
|
||||
case shaderir.Vec4:
|
||||
if head%boundaryInBytes >= 4*1 {
|
||||
head = align(head)
|
||||
}
|
||||
offsets = append(offsets, head)
|
||||
head += 4 * 4
|
||||
case shaderir.Mat2:
|
||||
// For matrices, each column is aligned to the boundary.
|
||||
head = align(head)
|
||||
offsets = append(offsets, head)
|
||||
// TODO: Is this correct?
|
||||
head += 1 * boundaryInBytes
|
||||
head += 4 * 2
|
||||
case shaderir.Mat3:
|
||||
head = align(head)
|
||||
offsets = append(offsets, head)
|
||||
// TODO: Is this correct?
|
||||
head += 2 * boundaryInBytes
|
||||
head += 4 * 3
|
||||
case shaderir.Mat4:
|
||||
head = align(head)
|
||||
offsets = append(offsets, head)
|
||||
head += 4 * boundaryInBytes
|
||||
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)
|
||||
// The last element is not with a padding.
|
||||
head += (u.Length - 1) * align(4*u.Sub[0].FloatNum())
|
||||
head += 4 * u.Sub[0].FloatNum()
|
||||
case shaderir.Struct:
|
||||
// TODO: Implement this
|
||||
panic("hlsl: offset for a struct is not implemented yet")
|
||||
default:
|
||||
panic(fmt.Sprintf("hlsl: unexpected type: %d", u.Main))
|
||||
}
|
||||
}
|
||||
|
||||
return offsets
|
||||
}
|
139
internal/shaderir/hlsl/type.go
Normal file
139
internal/shaderir/hlsl/type.go
Normal file
@ -0,0 +1,139 @@
|
||||
// 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 hlsl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
||||
)
|
||||
|
||||
func opString(op shaderir.Op) string {
|
||||
switch op {
|
||||
case shaderir.Add:
|
||||
return "+"
|
||||
case shaderir.Sub:
|
||||
return "-"
|
||||
case shaderir.NotOp:
|
||||
return "!"
|
||||
case shaderir.ComponentWiseMul:
|
||||
return "*"
|
||||
case shaderir.Div:
|
||||
return "/"
|
||||
case shaderir.ModOp:
|
||||
return "%"
|
||||
case shaderir.LeftShift:
|
||||
return "<<"
|
||||
case shaderir.RightShift:
|
||||
return ">>"
|
||||
case shaderir.LessThanOp:
|
||||
return "<"
|
||||
case shaderir.LessThanEqualOp:
|
||||
return "<="
|
||||
case shaderir.GreaterThanOp:
|
||||
return ">"
|
||||
case shaderir.GreaterThanEqualOp:
|
||||
return ">="
|
||||
case shaderir.EqualOp:
|
||||
return "=="
|
||||
case shaderir.NotEqualOp:
|
||||
return "!="
|
||||
case shaderir.And:
|
||||
return "&"
|
||||
case shaderir.Xor:
|
||||
return "^"
|
||||
case shaderir.Or:
|
||||
return "|"
|
||||
case shaderir.AndAnd:
|
||||
return "&&"
|
||||
case shaderir.OrOr:
|
||||
return "||"
|
||||
}
|
||||
return fmt.Sprintf("!(unexpected operator: %d)", op)
|
||||
}
|
||||
|
||||
func typeString(t *shaderir.Type) (string, string) {
|
||||
switch t.Main {
|
||||
case shaderir.Array:
|
||||
t0, t1 := typeString(&t.Sub[0])
|
||||
return t0 + t1, fmt.Sprintf("[%d]", t.Length)
|
||||
case shaderir.Struct:
|
||||
panic("hlsl: a struct is not implemented")
|
||||
default:
|
||||
return basicTypeString(t.Main), ""
|
||||
}
|
||||
}
|
||||
|
||||
func basicTypeString(t shaderir.BasicType) string {
|
||||
switch t {
|
||||
case shaderir.None:
|
||||
return "?(none)"
|
||||
case shaderir.Bool:
|
||||
return "bool"
|
||||
case shaderir.Int:
|
||||
return "int"
|
||||
case shaderir.Float:
|
||||
return "float"
|
||||
case shaderir.Vec2:
|
||||
return "float2"
|
||||
case shaderir.Vec3:
|
||||
return "float3"
|
||||
case shaderir.Vec4:
|
||||
return "float4"
|
||||
case shaderir.Mat2:
|
||||
return "float2x2"
|
||||
case shaderir.Mat3:
|
||||
return "float3x3"
|
||||
case shaderir.Mat4:
|
||||
return "float4x4"
|
||||
case shaderir.Array:
|
||||
return "?(array)"
|
||||
case shaderir.Struct:
|
||||
return "?(struct)"
|
||||
default:
|
||||
return fmt.Sprintf("?(unknown type: %d)", t)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *compileContext) builtinFuncString(f shaderir.BuiltinFunc) string {
|
||||
switch f {
|
||||
case shaderir.Vec2F:
|
||||
return "float2"
|
||||
case shaderir.Vec3F:
|
||||
return "float3"
|
||||
case shaderir.Vec4F:
|
||||
return "float4"
|
||||
case shaderir.Mat2F:
|
||||
return "float2x2"
|
||||
case shaderir.Mat3F:
|
||||
return "float3x3"
|
||||
case shaderir.Mat4F:
|
||||
return "float4x4"
|
||||
case shaderir.Inversesqrt:
|
||||
return "rsqrt"
|
||||
case shaderir.Fract:
|
||||
return "frac"
|
||||
case shaderir.Mix:
|
||||
return "lerp"
|
||||
case shaderir.Dfdx:
|
||||
return "ddx"
|
||||
case shaderir.Dfdy:
|
||||
return "ddy"
|
||||
case shaderir.Texture2DF:
|
||||
return "?(texture2D)"
|
||||
default:
|
||||
return string(f)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user