2020-08-03 17:56:45 +02:00
|
|
|
// Copyright 2020 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.
|
|
|
|
|
2022-03-10 08:24:56 +01:00
|
|
|
package msl
|
2020-08-03 17:56:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/constant"
|
|
|
|
"go/token"
|
2023-07-27 16:00:55 +02:00
|
|
|
"math"
|
2020-08-03 17:56:45 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2020-08-03 17:56:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-08-16 18:01:23 +02:00
|
|
|
vertexOut = "varyings"
|
2020-08-03 17:56:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2022-03-09 16:08:36 +01:00
|
|
|
panic("msl: the given type at structName must be a struct")
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
s := t.String()
|
|
|
|
if n, ok := c.structNames[s]; ok {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
n := fmt.Sprintf("S%d", len(c.structNames))
|
|
|
|
c.structNames[s] = n
|
|
|
|
c.structTypes = append(c.structTypes, *t)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
func Prelude(unit shaderir.Unit) string {
|
|
|
|
str := `#include <metal_stdlib>
|
2020-09-09 18:11:15 +02:00
|
|
|
|
|
|
|
using namespace metal;
|
|
|
|
|
2022-03-25 18:57:34 +01:00
|
|
|
template<typename T, typename U>
|
|
|
|
T mod(T x, U y) {
|
2022-03-04 10:05:42 +01:00
|
|
|
return x - y * floor(x/y);
|
|
|
|
}`
|
2023-04-16 11:56:14 +02:00
|
|
|
if unit == shaderir.Texel {
|
|
|
|
str += `
|
|
|
|
|
|
|
|
constexpr sampler texture_sampler{filter::nearest};`
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
2020-09-09 18:11:15 +02:00
|
|
|
|
2020-08-03 17:56:45 +02:00
|
|
|
func Compile(p *shaderir.Program, vertex, fragment string) (shader string) {
|
|
|
|
c := &compileContext{
|
|
|
|
structNames: map[string]string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
var lines []string
|
2023-04-16 11:56:14 +02:00
|
|
|
lines = append(lines, strings.Split(Prelude(p.Unit), "\n")...)
|
2020-09-09 18:11:15 +02:00
|
|
|
lines = append(lines, "", "{{.Structs}}")
|
2020-08-03 17:56:45 +02:00
|
|
|
|
|
|
|
if len(p.Attributes) > 0 {
|
|
|
|
lines = append(lines, "")
|
|
|
|
lines = append(lines, "struct Attributes {")
|
|
|
|
for i, a := range p.Attributes {
|
2022-05-28 15:47:06 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("\t%s;", c.varDecl(p, &a, fmt.Sprintf("M%d", i), false)))
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
lines = append(lines, "};")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.Varyings) > 0 {
|
|
|
|
lines = append(lines, "")
|
|
|
|
lines = append(lines, "struct Varyings {")
|
|
|
|
lines = append(lines, "\tfloat4 Position [[position]];")
|
|
|
|
for i, v := range p.Varyings {
|
2022-05-28 15:47:06 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("\t%s;", c.varDecl(p, &v, fmt.Sprintf("M%d", i), false)))
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
lines = append(lines, "};")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.Funcs) > 0 {
|
|
|
|
lines = append(lines, "")
|
|
|
|
for _, f := range p.Funcs {
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.function(p, &f, true)...)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
for _, f := range p.Funcs {
|
|
|
|
if len(lines) > 0 && lines[len(lines)-1] != "" {
|
|
|
|
lines = append(lines, "")
|
|
|
|
}
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.function(p, &f, false)...)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 08:55:59 +02:00
|
|
|
if p.VertexFunc.Block != nil && len(p.VertexFunc.Block.Stmts) > 0 {
|
2020-08-03 17:56:45 +02:00
|
|
|
lines = append(lines, "")
|
|
|
|
lines = append(lines,
|
|
|
|
fmt.Sprintf("vertex Varyings %s(", vertex),
|
|
|
|
"\tuint vid [[vertex_id]],",
|
|
|
|
"\tconst device Attributes* attributes [[buffer(0)]]")
|
|
|
|
for i, u := range p.Uniforms {
|
|
|
|
lines[len(lines)-1] += ","
|
2022-05-28 15:47:06 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("\tconstant %s [[buffer(%d)]]", c.varDecl(p, &u, fmt.Sprintf("U%d", i), true), i+1))
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
2022-12-24 07:55:05 +01:00
|
|
|
for i := 0; i < p.TextureCount; i++ {
|
2020-08-03 17:56:45 +02:00
|
|
|
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, fmt.Sprintf("\tVaryings %s = {};", vertexOut))
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.block(p, p.VertexFunc.Block, p.VertexFunc.Block, 0)...)
|
2020-08-03 17:56:45 +02:00
|
|
|
if last := fmt.Sprintf("\treturn %s;", vertexOut); lines[len(lines)-1] != last {
|
|
|
|
lines = append(lines, last)
|
|
|
|
}
|
|
|
|
lines = append(lines, "}")
|
|
|
|
}
|
|
|
|
|
2020-08-09 08:55:59 +02:00
|
|
|
if p.FragmentFunc.Block != nil && len(p.FragmentFunc.Block.Stmts) > 0 {
|
2020-08-03 17:56:45 +02:00
|
|
|
lines = append(lines, "")
|
|
|
|
lines = append(lines,
|
|
|
|
fmt.Sprintf("fragment float4 %s(", fragment),
|
|
|
|
"\tVaryings varyings [[stage_in]]")
|
|
|
|
for i, u := range p.Uniforms {
|
|
|
|
lines[len(lines)-1] += ","
|
2022-05-28 15:47:06 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("\tconstant %s [[buffer(%d)]]", c.varDecl(p, &u, fmt.Sprintf("U%d", i), true), i+1))
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
2022-12-24 07:55:05 +01:00
|
|
|
for i := 0; i < p.TextureCount; i++ {
|
2020-08-03 17:56:45 +02:00
|
|
|
lines[len(lines)-1] += ","
|
|
|
|
lines = append(lines, fmt.Sprintf("\ttexture2d<float> T%[1]d [[texture(%[1]d)]]", i))
|
|
|
|
}
|
|
|
|
lines[len(lines)-1] += ") {"
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.block(p, p.FragmentFunc.Block, p.FragmentFunc.Block, 0)...)
|
2020-08-03 17:56:45 +02:00
|
|
|
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 {
|
2022-05-28 15:47:06 +02:00
|
|
|
stlines = append(stlines, fmt.Sprintf("\t%s;", c.varDecl(p, &st, fmt.Sprintf("M%d", j), false)))
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
stlines = append(stlines, "};")
|
|
|
|
}
|
|
|
|
ls = strings.ReplaceAll(ls, "{{.Structs}}", strings.Join(stlines, "\n"))
|
|
|
|
} else {
|
|
|
|
ls = strings.ReplaceAll(ls, "{{.Structs}}", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
nls := regexp.MustCompile(`\n\n+`)
|
|
|
|
ls = nls.ReplaceAllString(ls, "\n\n")
|
|
|
|
ls = strings.TrimSpace(ls) + "\n"
|
|
|
|
|
|
|
|
return ls
|
|
|
|
}
|
|
|
|
|
2022-05-28 15:47:06 +02:00
|
|
|
func (c *compileContext) typ(p *shaderir.Program, t *shaderir.Type) string {
|
2020-08-03 17:56:45 +02:00
|
|
|
switch t.Main {
|
|
|
|
case shaderir.None:
|
|
|
|
return "void"
|
|
|
|
case shaderir.Struct:
|
|
|
|
return c.structName(p, t)
|
|
|
|
default:
|
2022-05-28 15:47:06 +02:00
|
|
|
return typeString(t, false)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 15:47:06 +02:00
|
|
|
func (c *compileContext) varDecl(p *shaderir.Program, t *shaderir.Type, varname string, ref bool) string {
|
2020-08-03 17:56:45 +02:00
|
|
|
switch t.Main {
|
|
|
|
case shaderir.None:
|
|
|
|
return "?(none)"
|
|
|
|
case shaderir.Struct:
|
|
|
|
s := c.structName(p, t)
|
|
|
|
if ref {
|
|
|
|
s += "&"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s %s", s, varname)
|
|
|
|
default:
|
2022-05-28 15:47:06 +02:00
|
|
|
t := typeString(t, ref)
|
2020-08-03 17:56:45 +02:00
|
|
|
return fmt.Sprintf("%s %s", t, varname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:43:36 +01:00
|
|
|
func (c *compileContext) varInit(p *shaderir.Program, t *shaderir.Type) string {
|
2020-08-03 17:56:45 +02:00
|
|
|
switch t.Main {
|
|
|
|
case shaderir.None:
|
|
|
|
return "?(none)"
|
|
|
|
case shaderir.Array:
|
2020-08-08 21:40:12 +02:00
|
|
|
return "{}"
|
2020-08-03 17:56:45 +02:00
|
|
|
case shaderir.Struct:
|
2020-08-08 21:40:12 +02:00
|
|
|
return "{}"
|
2020-08-03 17:56:45 +02:00
|
|
|
case shaderir.Bool:
|
|
|
|
return "false"
|
|
|
|
case shaderir.Int:
|
|
|
|
return "0"
|
2022-11-20 07:36:07 +01:00
|
|
|
case shaderir.Float, shaderir.Vec2, shaderir.Vec3, shaderir.Vec4,
|
|
|
|
shaderir.IVec2, shaderir.IVec3, shaderir.IVec4,
|
|
|
|
shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
|
2022-05-28 15:47:06 +02:00
|
|
|
return fmt.Sprintf("%s(0)", basicTypeString(t.Main))
|
2020-08-03 17:56:45 +02:00
|
|
|
default:
|
2022-05-28 15:47:06 +02:00
|
|
|
t := c.typ(p, t)
|
2020-08-03 17:56:45 +02:00
|
|
|
panic(fmt.Sprintf("?(unexpected type: %s)", t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:43:36 +01:00
|
|
|
func (c *compileContext) function(p *shaderir.Program, f *shaderir.Func, prototype bool) []string {
|
2020-08-03 17:56:45 +02:00
|
|
|
var args []string
|
|
|
|
|
|
|
|
// Uniform variables and texture variables. In Metal, non-const global variables are not available.
|
|
|
|
for i, u := range p.Uniforms {
|
2022-05-28 15:47:06 +02:00
|
|
|
args = append(args, "constant "+c.varDecl(p, &u, fmt.Sprintf("U%d", i), true))
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
2022-12-24 07:55:05 +01:00
|
|
|
for i := 0; i < p.TextureCount; i++ {
|
2020-08-03 17:56:45 +02:00
|
|
|
args = append(args, fmt.Sprintf("texture2d<float> T%d", i))
|
|
|
|
}
|
|
|
|
|
|
|
|
var idx int
|
|
|
|
for _, t := range f.InParams {
|
2022-05-28 15:47:06 +02:00
|
|
|
args = append(args, c.varDecl(p, &t, fmt.Sprintf("l%d", idx), false))
|
2020-08-03 17:56:45 +02:00
|
|
|
idx++
|
|
|
|
}
|
|
|
|
for _, t := range f.OutParams {
|
2022-05-28 15:47:06 +02:00
|
|
|
args = append(args, "thread "+c.varDecl(p, &t, fmt.Sprintf("l%d", idx), true))
|
2020-08-03 17:56:45 +02:00
|
|
|
idx++
|
|
|
|
}
|
|
|
|
argsstr := "void"
|
|
|
|
if len(args) > 0 {
|
|
|
|
argsstr = strings.Join(args, ", ")
|
|
|
|
}
|
|
|
|
|
2022-05-28 15:47:06 +02:00
|
|
|
t := c.typ(p, &f.Return)
|
2020-08-03 17:56:45 +02:00
|
|
|
sig := fmt.Sprintf("%s F%d(%s)", t, f.Index, argsstr)
|
|
|
|
|
|
|
|
var lines []string
|
|
|
|
if prototype {
|
|
|
|
lines = append(lines, fmt.Sprintf("%s;", sig))
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
lines = append(lines, fmt.Sprintf("%s {", sig))
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.block(p, f.Block, f.Block, 0)...)
|
2020-08-03 17:56:45 +02:00
|
|
|
lines = append(lines, "}")
|
|
|
|
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2023-07-29 07:10:31 +02:00
|
|
|
func constantToNumberLiteral(v constant.Value) string {
|
2023-07-27 16:00:55 +02:00
|
|
|
switch v.Kind() {
|
|
|
|
case constant.Bool:
|
|
|
|
if constant.BoolVal(v) {
|
|
|
|
return "true"
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
2023-07-27 16:00:55 +02:00
|
|
|
return "false"
|
|
|
|
case constant.Int:
|
|
|
|
x, _ := constant.Int64Val(v)
|
|
|
|
return fmt.Sprintf("%d", x)
|
|
|
|
case constant.Float:
|
|
|
|
x, _ := constant.Float64Val(v)
|
|
|
|
if i := math.Floor(x); i == x {
|
|
|
|
return fmt.Sprintf("%d.0", int64(i))
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
2023-07-27 16:00:55 +02:00
|
|
|
return fmt.Sprintf("%.10e", x)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("?(unexpected literal: %s)", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func localVariableName(p *shaderir.Program, topBlock *shaderir.Block, idx int) string {
|
|
|
|
switch topBlock {
|
2020-08-09 08:55:59 +02:00
|
|
|
case p.VertexFunc.Block:
|
2020-08-03 17:56:45 +02:00
|
|
|
na := len(p.Attributes)
|
|
|
|
nv := len(p.Varyings)
|
|
|
|
switch {
|
|
|
|
case idx < na:
|
|
|
|
return fmt.Sprintf("attributes[vid].M%d", idx)
|
|
|
|
case idx == na:
|
|
|
|
return fmt.Sprintf("%s.Position", vertexOut)
|
|
|
|
case idx < na+nv+1:
|
|
|
|
return fmt.Sprintf("%s.M%d", vertexOut, idx-na-1)
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("l%d", idx-(na+nv+1))
|
|
|
|
}
|
2020-08-09 08:55:59 +02:00
|
|
|
case p.FragmentFunc.Block:
|
2020-08-03 17:56:45 +02:00
|
|
|
nv := len(p.Varyings)
|
|
|
|
switch {
|
|
|
|
case idx == 0:
|
2022-03-11 16:18:55 +01:00
|
|
|
return fmt.Sprintf("%s.Position", vertexOut)
|
2020-08-03 17:56:45 +02:00
|
|
|
case idx < nv+1:
|
2022-03-11 16:18:55 +01:00
|
|
|
return fmt.Sprintf("%s.M%d", vertexOut, idx-1)
|
2020-08-03 17:56:45 +02:00
|
|
|
default:
|
2022-08-16 18:01:23 +02:00
|
|
|
return fmt.Sprintf("l%d", idx-(nv+1))
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("l%d", idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:38:54 +02:00
|
|
|
func (c *compileContext) initVariable(p *shaderir.Program, topBlock, block *shaderir.Block, index int, decl bool, level int) []string {
|
|
|
|
idt := strings.Repeat("\t", level+1)
|
2020-09-06 17:13:46 +02:00
|
|
|
name := localVariableName(p, topBlock, index)
|
2020-09-09 18:38:54 +02:00
|
|
|
t := p.LocalVariableType(topBlock, block, index)
|
|
|
|
|
|
|
|
var lines []string
|
|
|
|
if decl {
|
2022-05-28 15:47:06 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, c.varDecl(p, &t, name, false), c.varInit(p, &t)))
|
2020-09-09 18:38:54 +02:00
|
|
|
} else {
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, name, c.varInit(p, &t)))
|
2020-09-09 18:38:54 +02:00
|
|
|
}
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:43:36 +01:00
|
|
|
func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Block, level int) []string {
|
2020-08-09 08:55:59 +02:00
|
|
|
if block == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-03 17:56:45 +02:00
|
|
|
idt := strings.Repeat("\t", level+1)
|
|
|
|
|
|
|
|
var lines []string
|
2020-08-09 16:40:41 +02:00
|
|
|
for i, t := range block.LocalVars {
|
2020-08-03 17:56:45 +02:00
|
|
|
// The type is None e.g., when the variable is a for-loop counter.
|
|
|
|
if t.Main != shaderir.None {
|
2020-09-09 18:38:54 +02:00
|
|
|
lines = append(lines, c.initVariable(p, topBlock, block, block.LocalVarIndexOffset+i, true, level)...)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 07:18:38 +01:00
|
|
|
var expr func(e *shaderir.Expr) string
|
|
|
|
expr = func(e *shaderir.Expr) string {
|
2020-08-03 17:56:45 +02:00
|
|
|
switch e.Type {
|
|
|
|
case shaderir.NumberExpr:
|
2023-07-29 07:10:31 +02:00
|
|
|
return constantToNumberLiteral(e.Const)
|
2020-08-03 17:56:45 +02:00
|
|
|
case shaderir.UniformVariable:
|
|
|
|
return fmt.Sprintf("U%d", e.Index)
|
|
|
|
case shaderir.TextureVariable:
|
|
|
|
return fmt.Sprintf("T%d", e.Index)
|
|
|
|
case shaderir.LocalVariable:
|
|
|
|
return localVariableName(p, topBlock, e.Index)
|
|
|
|
case shaderir.StructMember:
|
|
|
|
return fmt.Sprintf("M%d", e.Index)
|
|
|
|
case shaderir.BuiltinFuncExpr:
|
|
|
|
return 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:
|
2022-03-13 08:20:01 +01:00
|
|
|
op = opString(e.Op)
|
2020-08-03 17:56:45 +02:00
|
|
|
default:
|
2022-03-13 08:20:01 +01:00
|
|
|
op = fmt.Sprintf("?(unexpected op: %d)", e.Op)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
2022-03-11 07:18:38 +01:00
|
|
|
return fmt.Sprintf("%s(%s)", op, expr(&e.Exprs[0]))
|
2020-08-03 17:56:45 +02:00
|
|
|
case shaderir.Binary:
|
2022-07-08 19:26:33 +02:00
|
|
|
switch e.Op {
|
|
|
|
case shaderir.VectorEqualOp:
|
|
|
|
return fmt.Sprintf("all((%s) == (%s))", expr(&e.Exprs[0]), expr(&e.Exprs[1]))
|
|
|
|
case shaderir.VectorNotEqualOp:
|
|
|
|
return fmt.Sprintf("!all((%s) == (%s))", expr(&e.Exprs[0]), expr(&e.Exprs[1]))
|
|
|
|
}
|
2022-03-13 08:20:01 +01:00
|
|
|
return fmt.Sprintf("(%s) %s (%s)", expr(&e.Exprs[0]), opString(e.Op), expr(&e.Exprs[1]))
|
2020-08-03 17:56:45 +02:00
|
|
|
case shaderir.Selection:
|
2022-03-11 07:18:38 +01:00
|
|
|
return fmt.Sprintf("(%s) ? (%s) : (%s)", expr(&e.Exprs[0]), expr(&e.Exprs[1]), expr(&e.Exprs[2]))
|
2020-08-03 17:56:45 +02:00
|
|
|
case shaderir.Call:
|
|
|
|
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))
|
|
|
|
}
|
2022-12-24 07:55:05 +01:00
|
|
|
for i := 0; i < p.TextureCount; i++ {
|
2020-08-03 17:56:45 +02:00
|
|
|
args = append(args, fmt.Sprintf("T%d", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, exp := range e.Exprs[1:] {
|
2022-03-11 07:18:38 +01:00
|
|
|
args = append(args, expr(&exp))
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
2023-04-23 15:57:30 +02:00
|
|
|
if callee.Type == shaderir.BuiltinFuncExpr && callee.BuiltinFunc == shaderir.TexelAt {
|
2023-04-16 11:56:14 +02:00
|
|
|
switch p.Unit {
|
|
|
|
case shaderir.Texel:
|
|
|
|
return fmt.Sprintf("%s.sample(texture_sampler, %s)", args[0], strings.Join(args[1:], ", "))
|
|
|
|
case shaderir.Pixel:
|
|
|
|
return fmt.Sprintf("%s.read(static_cast<uint2>(%s))", args[0], strings.Join(args[1:], ", "))
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("msl: unexpected unit: %d", p.Unit))
|
|
|
|
}
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
2022-03-11 07:18:38 +01:00
|
|
|
return fmt.Sprintf("%s(%s)", expr(&callee), strings.Join(args, ", "))
|
2020-08-03 17:56:45 +02:00
|
|
|
case shaderir.FieldSelector:
|
2022-03-11 07:18:38 +01:00
|
|
|
return fmt.Sprintf("(%s).%s", expr(&e.Exprs[0]), expr(&e.Exprs[1]))
|
2020-08-03 17:56:45 +02:00
|
|
|
case shaderir.Index:
|
2022-03-11 07:18:38 +01:00
|
|
|
return fmt.Sprintf("(%s)[%s]", expr(&e.Exprs[0]), expr(&e.Exprs[1]))
|
2020-08-03 17:56:45 +02:00
|
|
|
default:
|
|
|
|
return fmt.Sprintf("?(unexpected expr: %d)", e.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range block.Stmts {
|
|
|
|
switch s.Type {
|
|
|
|
case shaderir.ExprStmt:
|
2022-03-11 07:18:38 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("%s%s;", idt, expr(&s.Exprs[0])))
|
2020-08-03 17:56:45 +02:00
|
|
|
case shaderir.BlockStmt:
|
|
|
|
lines = append(lines, idt+"{")
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.block(p, topBlock, s.Blocks[0], level+1)...)
|
2020-08-03 17:56:45 +02:00
|
|
|
lines = append(lines, idt+"}")
|
|
|
|
case shaderir.Assign:
|
2022-03-11 07:18:38 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, expr(&s.Exprs[0]), expr(&s.Exprs[1])))
|
2020-09-06 17:13:46 +02:00
|
|
|
case shaderir.Init:
|
|
|
|
init := true
|
|
|
|
if topBlock == p.VertexFunc.Block {
|
|
|
|
// In the vertex function, varying values are the output parameters.
|
|
|
|
// These values are represented as a struct and not needed to be initialized.
|
|
|
|
na := len(p.Attributes)
|
|
|
|
nv := len(p.Varyings)
|
|
|
|
if s.InitIndex < na+nv+1 {
|
|
|
|
init = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if init {
|
|
|
|
lines = append(lines, c.initVariable(p, topBlock, block, s.InitIndex, false, level)...)
|
|
|
|
}
|
2020-08-03 17:56:45 +02:00
|
|
|
case shaderir.If:
|
2022-03-11 07:18:38 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("%sif (%s) {", idt, expr(&s.Exprs[0])))
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.block(p, topBlock, s.Blocks[0], level+1)...)
|
2020-08-03 17:56:45 +02:00
|
|
|
if len(s.Blocks) > 1 {
|
|
|
|
lines = append(lines, fmt.Sprintf("%s} else {", idt))
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.block(p, topBlock, s.Blocks[1], level+1)...)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
lines = append(lines, fmt.Sprintf("%s}", idt))
|
|
|
|
case shaderir.For:
|
|
|
|
v := 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 {
|
2023-07-29 07:10:31 +02:00
|
|
|
delta = fmt.Sprintf("%s += %s", v, constantToNumberLiteral(d))
|
2020-08-03 17:56:45 +02:00
|
|
|
} else {
|
|
|
|
d = constant.UnaryOp(token.SUB, d, 0)
|
2023-07-29 07:10:31 +02:00
|
|
|
delta = fmt.Sprintf("%s -= %s", v, constantToNumberLiteral(d))
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var op string
|
|
|
|
switch s.ForOp {
|
|
|
|
case shaderir.LessThanOp, shaderir.LessThanEqualOp, shaderir.GreaterThanOp, shaderir.GreaterThanEqualOp, shaderir.EqualOp, shaderir.NotEqualOp:
|
2022-03-13 08:20:01 +01:00
|
|
|
op = opString(s.ForOp)
|
2020-08-03 17:56:45 +02:00
|
|
|
default:
|
2022-03-13 08:20:01 +01:00
|
|
|
op = fmt.Sprintf("?(unexpected op: %d)", s.ForOp)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
t := s.ForVarType
|
2023-07-29 07:10:31 +02:00
|
|
|
init := constantToNumberLiteral(s.ForInit)
|
|
|
|
end := constantToNumberLiteral(s.ForEnd)
|
2022-05-28 15:47:06 +02:00
|
|
|
ts := typeString(&t, false)
|
2020-08-03 17:56:45 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("%sfor (%s %s = %s; %s %s %s; %s) {", idt, ts, v, init, v, op, end, delta))
|
2022-03-08 17:43:36 +01:00
|
|
|
lines = append(lines, c.block(p, topBlock, s.Blocks[0], level+1)...)
|
2020-08-03 17:56:45 +02:00
|
|
|
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 {
|
2020-08-09 08:55:59 +02:00
|
|
|
case topBlock == p.VertexFunc.Block:
|
2020-08-03 17:56:45 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, vertexOut))
|
|
|
|
case len(s.Exprs) == 0:
|
|
|
|
lines = append(lines, idt+"return;")
|
|
|
|
default:
|
2022-03-11 07:18:38 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, expr(&s.Exprs[0])))
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
case shaderir.Discard:
|
2022-08-16 18:01:23 +02:00
|
|
|
// 'discard' is invoked only in the fragment shader entry point.
|
|
|
|
lines = append(lines, idt+"discard_fragment();", idt+"return float4(0.0);")
|
2020-08-03 17:56:45 +02:00
|
|
|
default:
|
|
|
|
lines = append(lines, fmt.Sprintf("%s?(unexpected stmt: %d)", idt, s.Type))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines
|
|
|
|
}
|