shaderir/metal: Implement the shader IR compiler for Metal

Fixes #1165
This commit is contained in:
Hajime Hoshi 2020-08-04 00:56:45 +09:00
parent 18b3859e20
commit a45e241da1
10 changed files with 823 additions and 61 deletions

View File

@ -40,14 +40,6 @@ func NeedsRestoring() bool {
return theGraphicsDriver.NeedsRestoring()
}
// IsShaderAvailable reports whether shaders are available. This function is only for testing.
func IsShaderAvailable() bool {
if !theGraphicsDriver.IsGL() {
return false
}
return true
}
// command represents a drawing command.
//
// A command for drawing that is created when Image functions are called like DrawTriangles,

View File

@ -80,10 +80,6 @@ func TestReplacePixelsPartAfterDrawTriangles(t *testing.T) {
}
func TestShader(t *testing.T) {
if !IsShaderAvailable() {
t.Skip("shader is not implemented on this environment")
}
const w, h = 16, 16
clr := NewImage(w, h)
dst := NewImage(w, h)

View File

@ -318,6 +318,9 @@ type Graphics struct {
images map[driver.ImageID]*Image
nextImageID driver.ImageID
shaders map[driver.ShaderID]*Shader
nextShaderID driver.ShaderID
src *Image
dst *Image
@ -433,6 +436,12 @@ func (g *Graphics) InvalidImageID() driver.ImageID {
return -1
}
func (g *Graphics) genNextShaderID() driver.ShaderID {
id := g.nextShaderID
g.nextShaderID++
return id
}
func (g *Graphics) NewImage(width, height int) (driver.Image, error) {
g.checkSize(width, height)
td := mtl.TextureDescriptor{
@ -493,6 +502,27 @@ func (g *Graphics) SetTransparent(transparent bool) {
g.transparent = transparent
}
func operationToBlendFactor(c driver.Operation) mtl.BlendFactor {
switch c {
case driver.Zero:
return mtl.BlendFactorZero
case driver.One:
return mtl.BlendFactorOne
case driver.SrcAlpha:
return mtl.BlendFactorSourceAlpha
case driver.DstAlpha:
return mtl.BlendFactorDestinationAlpha
case driver.OneMinusSrcAlpha:
return mtl.BlendFactorOneMinusSourceAlpha
case driver.OneMinusDstAlpha:
return mtl.BlendFactorOneMinusDestinationAlpha
case driver.DstColor:
return mtl.BlendFactorDestinationColor
default:
panic(fmt.Sprintf("metal: invalid operation: %d", c))
}
}
func (g *Graphics) Reset() error {
if err := g.t.Call(func() error {
if g.cq != (mtl.CommandQueue{}) {
@ -554,27 +584,6 @@ func (g *Graphics) Reset() error {
}
g.screenRPS = rps
conv := func(c driver.Operation) mtl.BlendFactor {
switch c {
case driver.Zero:
return mtl.BlendFactorZero
case driver.One:
return mtl.BlendFactorOne
case driver.SrcAlpha:
return mtl.BlendFactorSourceAlpha
case driver.DstAlpha:
return mtl.BlendFactorDestinationAlpha
case driver.OneMinusSrcAlpha:
return mtl.BlendFactorOneMinusSourceAlpha
case driver.OneMinusDstAlpha:
return mtl.BlendFactorOneMinusDestinationAlpha
case driver.DstColor:
return mtl.BlendFactorDestinationColor
default:
panic(fmt.Sprintf("metal: invalid operation: %d", c))
}
}
for _, screen := range []bool{false, true} {
for _, cm := range []bool{false, true} {
for _, a := range []driver.Address{
@ -608,10 +617,10 @@ func (g *Graphics) Reset() error {
rpld.ColorAttachments[0].BlendingEnabled = true
src, dst := c.Operations()
rpld.ColorAttachments[0].DestinationAlphaBlendFactor = conv(dst)
rpld.ColorAttachments[0].DestinationRGBBlendFactor = conv(dst)
rpld.ColorAttachments[0].SourceAlphaBlendFactor = conv(src)
rpld.ColorAttachments[0].SourceRGBBlendFactor = conv(src)
rpld.ColorAttachments[0].DestinationAlphaBlendFactor = operationToBlendFactor(dst)
rpld.ColorAttachments[0].DestinationRGBBlendFactor = operationToBlendFactor(dst)
rpld.ColorAttachments[0].SourceAlphaBlendFactor = operationToBlendFactor(src)
rpld.ColorAttachments[0].SourceRGBBlendFactor = operationToBlendFactor(src)
rps, err := g.view.getMTLDevice().MakeRenderPipelineState(rpld)
if err != nil {
return err
@ -818,11 +827,33 @@ func (g *Graphics) MaxImageSize() int {
}
func (g *Graphics) NewShader(program *shaderir.Program) (driver.Shader, error) {
panic("metal: NewShader is not implemented")
var s *Shader
if err := g.t.Call(func() error {
var err error
s, err = newShader(g.view.getMTLDevice(), g.genNextShaderID(), program)
if err != nil {
return err
}
return nil
}); err != nil {
return nil, err
}
g.addShader(s)
return s, nil
}
func (g *Graphics) DrawShader(dst driver.ImageID, srcs [graphics.ShaderImageNum]driver.ImageID, offsets [graphics.ShaderImageNum - 1][2]float32, shader driver.ShaderID, indexLen int, indexOffset int, mode driver.CompositeMode, uniforms []interface{}) error {
panic("metal: DrawShader is not implemented")
func (g *Graphics) addShader(shader *Shader) {
if g.shaders == nil {
g.shaders = map[driver.ShaderID]*Shader{}
}
if _, ok := g.shaders[shader.id]; ok {
panic(fmt.Sprintf("metal: shader ID %d was already registered", shader.id))
}
g.shaders[shader.id] = shader
}
func (g *Graphics) removeShader(shader *Shader) {
delete(g.shaders, shader.id)
}
type Image struct {
@ -959,3 +990,57 @@ func (i *Image) ReplacePixels(args []*driver.ReplacePixelsArgs) {
return nil
})
}
func (g *Graphics) DrawShader(dstID driver.ImageID, srcIDs [graphics.ShaderImageNum]driver.ImageID, offsets [graphics.ShaderImageNum - 1][2]float32, shader driver.ShaderID, indexLen int, indexOffset int, mode driver.CompositeMode, uniforms []interface{}) error {
dst := g.images[dstID]
var srcs [graphics.ShaderImageNum]*Image
for i, srcID := range srcIDs {
srcs[i] = g.images[srcID]
}
if err := g.t.Call(func() error {
rps, err := g.shaders[shader].RenderPipelineState(g.view.getMTLDevice(), mode)
if err != nil {
return err
}
us := make([]interface{}, graphics.PreservedUniformVariablesNum+len(uniforms))
// Set the destination texture size.
dw, dh := dst.internalSize()
us[0] = []float32{float32(dw), float32(dh)}
// Set the source texture sizes.
usizes := make([]float32, 2*len(srcs))
for i, src := range srcs {
if src != nil {
w, h := src.internalSize()
usizes[2*i] = float32(w)
usizes[2*i+1] = float32(h)
}
}
us[1] = usizes
// Set the source offsets.
uoffsets := make([]float32, 2*len(offsets))
for i, offset := range offsets {
uoffsets[2*i] = offset[0]
uoffsets[2*i+1] = offset[1]
}
us[2] = uoffsets
// Set the additional uniform variables.
for i, v := range uniforms {
const offset = graphics.PreservedUniformVariablesNum
us[offset+i] = v
}
if err := g.draw(rps, dst, srcs, indexLen, indexOffset, us); err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
}

View File

@ -803,6 +803,10 @@ type Function struct {
function unsafe.Pointer
}
func (f Function) Release() {
C.Function_Release(f.function)
}
// RenderPipelineState contains the graphics functions
// and configuration state used in a render pass.
//
@ -811,6 +815,10 @@ type RenderPipelineState struct {
renderPipelineState unsafe.Pointer
}
func (r RenderPipelineState) Release() {
C.RenderPipelineState_Release(r.renderPipelineState)
}
// Region is a rectangular block of pixels in an image or texture,
// defined by its upper-left corner and its size.
//

View File

@ -182,3 +182,5 @@ int Texture_Height(void *texture);
void Buffer_CopyToContents(void *buffer, void *data, size_t lengthInBytes);
void Buffer_Retain(void *buffer);
void Buffer_Release(void *buffer);
void Function_Release(void *function);
void RenderPipelineState_Release(void *renderPipelineState);

View File

@ -343,3 +343,9 @@ void Buffer_CopyToContents(void *buffer, void *data, size_t lengthInBytes) {
void Buffer_Retain(void *buffer) { [(id<MTLBuffer>)buffer retain]; }
void Buffer_Release(void *buffer) { [(id<MTLBuffer>)buffer release]; }
void Function_Release(void *function) { [(id<MTLFunction>)function release]; }
void RenderPipelineState_Release(void *renderPipelineState) {
[(id<MTLRenderPipelineState>)renderPipelineState release];
}

View File

@ -0,0 +1,110 @@
// 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.
// +build darwin
package metal
import (
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/metal/mtl"
"github.com/hajimehoshi/ebiten/internal/shaderir"
"github.com/hajimehoshi/ebiten/internal/shaderir/metal"
)
type Shader struct {
id driver.ShaderID
ir *shaderir.Program
fs mtl.Function
vs mtl.Function
rpss map[driver.CompositeMode]mtl.RenderPipelineState
}
func newShader(device mtl.Device, id driver.ShaderID, program *shaderir.Program) (*Shader, error) {
s := &Shader{
id: id,
ir: program,
rpss: map[driver.CompositeMode]mtl.RenderPipelineState{},
}
if err := s.init(device); err != nil {
return nil, err
}
return s, nil
}
func (s *Shader) ID() driver.ShaderID {
return s.id
}
func (s *Shader) Dispose() {
for _, rps := range s.rpss {
rps.Release()
}
s.vs.Release()
s.fs.Release()
}
func (s *Shader) init(device mtl.Device) error {
const (
v = "Vertex"
f = "Fragment"
)
src := metal.Compile(s.ir, v, f)
lib, err := device.MakeLibrary(src, mtl.CompileOptions{})
if err != nil {
return err
}
vs, err := lib.MakeFunction(v)
if err != nil {
return err
}
fs, err := lib.MakeFunction(f)
if err != nil {
return err
}
s.fs = fs
s.vs = vs
return nil
}
func (s *Shader) RenderPipelineState(device mtl.Device, c driver.CompositeMode) (mtl.RenderPipelineState, error) {
if rps, ok := s.rpss[c]; ok {
return rps, nil
}
rpld := mtl.RenderPipelineDescriptor{
VertexFunction: s.vs,
FragmentFunction: s.fs,
}
// TODO: For the precise pixel format, whether the render target is the screen or not must be considered.
rpld.ColorAttachments[0].PixelFormat = mtl.PixelFormatRGBA8UNorm
rpld.ColorAttachments[0].BlendingEnabled = true
src, dst := c.Operations()
rpld.ColorAttachments[0].DestinationAlphaBlendFactor = operationToBlendFactor(dst)
rpld.ColorAttachments[0].DestinationRGBBlendFactor = operationToBlendFactor(dst)
rpld.ColorAttachments[0].SourceAlphaBlendFactor = operationToBlendFactor(src)
rpld.ColorAttachments[0].SourceRGBBlendFactor = operationToBlendFactor(src)
rps, err := device.MakeRenderPipelineState(rpld)
if err != nil {
return mtl.RenderPipelineState{}, err
}
s.rpss[c] = rps
return rps, nil
}

View File

@ -20,16 +20,11 @@ import (
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphicscommand"
. "github.com/hajimehoshi/ebiten/internal/restorable"
etesting "github.com/hajimehoshi/ebiten/internal/testing"
)
func TestShader(t *testing.T) {
if !graphicscommand.IsShaderAvailable() {
t.Skip("shader is not available on this environment")
}
img := NewImage(1, 1, false)
defer img.Dispose()
@ -52,10 +47,6 @@ func TestShader(t *testing.T) {
}
func TestShaderChain(t *testing.T) {
if !graphicscommand.IsShaderAvailable() {
t.Skip("shader is not available on this environment")
}
const num = 10
imgs := []*Image{}
for i := 0; i < num; i++ {
@ -89,10 +80,6 @@ func TestShaderChain(t *testing.T) {
}
func TestShaderMultipleSources(t *testing.T) {
if !graphicscommand.IsShaderAvailable() {
t.Skip("shader is not available on this environment")
}
var srcs [graphics.ShaderImageNum]*Image
for i := range srcs {
srcs[i] = NewImage(1, 1, false)
@ -126,10 +113,6 @@ func TestShaderMultipleSources(t *testing.T) {
}
func TestShaderMultipleSourcesOnOneTexture(t *testing.T) {
if !graphicscommand.IsShaderAvailable() {
t.Skip("shader is not available on this environment")
}
src := NewImage(3, 1, false)
src.ReplacePixels([]byte{
0x40, 0, 0, 0xff,
@ -166,10 +149,6 @@ func TestShaderMultipleSourcesOnOneTexture(t *testing.T) {
}
func TestShaderDispose(t *testing.T) {
if !graphicscommand.IsShaderAvailable() {
t.Skip("shader is not available on this environment")
}
img := NewImage(1, 1, false)
defer img.Dispose()

View File

@ -0,0 +1,479 @@
// 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.
package metal
import (
"fmt"
"go/constant"
"go/token"
"regexp"
"strings"
"github.com/hajimehoshi/ebiten/internal/shaderir"
)
const (
vertexOut = "varyings"
fragmentOut = "out"
)
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("metal: 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))
c.structNames[s] = n
c.structTypes = append(c.structTypes, *t)
return n
}
func Compile(p *shaderir.Program, vertex, fragment string) (shader string) {
c := &compileContext{
structNames: map[string]string{},
}
var lines []string
lines = append(lines,
"#include <metal_stdlib>",
"",
"using namespace metal;",
"",
"{{.Structs}}",
"",
"constexpr sampler texture_sampler{filter::nearest};")
if len(p.Attributes) > 0 {
lines = append(lines, "")
lines = append(lines, "struct Attributes {")
for i, a := range p.Attributes {
lines = append(lines, fmt.Sprintf("\t%s;", c.metalVarDecl(p, &a, fmt.Sprintf("M%d", i), true, false)))
}
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 {
lines = append(lines, fmt.Sprintf("\t%s;", c.metalVarDecl(p, &v, fmt.Sprintf("M%d", i), false, false)))
}
lines = append(lines, "};")
}
if len(p.Funcs) > 0 {
lines = append(lines, "")
for _, f := range p.Funcs {
lines = append(lines, c.metalFunc(p, &f, true)...)
}
for _, f := range p.Funcs {
if len(lines) > 0 && lines[len(lines)-1] != "" {
lines = append(lines, "")
}
lines = append(lines, c.metalFunc(p, &f, false)...)
}
}
if len(p.VertexFunc.Block.Stmts) > 0 {
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] += ","
lines = append(lines, fmt.Sprintf("\tconstant %s [[buffer(%d)]]", c.metalVarDecl(p, &u, fmt.Sprintf("U%d", i), false, true), i+1))
}
for i := 0; i < p.TextureNum; 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, fmt.Sprintf("\tVaryings %s = {};", vertexOut))
lines = append(lines, c.metalBlock(p, &p.VertexFunc.Block, &p.VertexFunc.Block, 0, 0)...)
if last := fmt.Sprintf("\treturn %s;", vertexOut); lines[len(lines)-1] != last {
lines = append(lines, last)
}
lines = append(lines, "}")
}
if len(p.FragmentFunc.Block.Stmts) > 0 {
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] += ","
lines = append(lines, fmt.Sprintf("\tconstant %s [[buffer(%d)]]", c.metalVarDecl(p, &u, fmt.Sprintf("U%d", i), false, true), i+1))
}
for i := 0; i < p.TextureNum; 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, fmt.Sprintf("\tfloat4 %s = float4(0);", fragmentOut))
lines = append(lines, c.metalBlock(p, &p.FragmentFunc.Block, &p.FragmentFunc.Block, 0, 0)...)
if last := fmt.Sprintf("\treturn %s;", fragmentOut); 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.metalVarDecl(p, &st, fmt.Sprintf("M%d", j), false, false)))
}
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
}
func (c *compileContext) metalType(p *shaderir.Program, t *shaderir.Type, packed bool, ref bool) string {
switch t.Main {
case shaderir.None:
return "void"
case shaderir.Struct:
return c.structName(p, t)
default:
return typeString(t, packed, ref)
}
}
func (c *compileContext) metalVarDecl(p *shaderir.Program, t *shaderir.Type, varname string, packed bool, ref bool) string {
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:
t := typeString(t, packed, ref)
return fmt.Sprintf("%s %s", t, varname)
}
}
func (c *compileContext) metalVarInit(p *shaderir.Program, t *shaderir.Type) string {
switch t.Main {
case shaderir.None:
return "?(none)"
case shaderir.Array:
init := c.metalVarInit(p, &t.Sub[0])
es := make([]string, 0, t.Length)
for i := 0; i < t.Length; i++ {
es = append(es, init)
}
t := typeString(t, false, false)
return fmt.Sprintf("%s(%s)", t, 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 fmt.Sprintf("%s(0)", basicTypeString(t.Main, false))
default:
t := c.metalType(p, t, false, false)
panic(fmt.Sprintf("?(unexpected type: %s)", t))
}
}
func (c *compileContext) metalFunc(p *shaderir.Program, f *shaderir.Func, prototype bool) []string {
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.metalVarDecl(p, &u, fmt.Sprintf("U%d", i), false, true))
}
for i := 0; i < p.TextureNum; i++ {
args = append(args, fmt.Sprintf("texture2d<float> T%d", i))
}
var idx int
for _, t := range f.InParams {
args = append(args, c.metalVarDecl(p, &t, fmt.Sprintf("l%d", idx), false, false))
idx++
}
for _, t := range f.OutParams {
args = append(args, "thread "+c.metalVarDecl(p, &t, fmt.Sprintf("l%d", idx), false, true))
idx++
}
argsstr := "void"
if len(args) > 0 {
argsstr = strings.Join(args, ", ")
}
t := c.metalType(p, &f.Return, false, false)
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))
lines = append(lines, c.metalBlock(p, &f.Block, &f.Block, 0, idx)...)
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("%.9e", 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 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("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))
}
case &p.FragmentFunc.Block:
nv := len(p.Varyings)
switch {
case idx == 0:
return fmt.Sprintf("varyings.Position")
case idx < nv+1:
return fmt.Sprintf("varyings.M%d", idx-1)
case idx == nv+1:
return fragmentOut
default:
return fmt.Sprintf("l%d", idx-(nv+2))
}
default:
return fmt.Sprintf("l%d", idx)
}
}
func (c *compileContext) metalBlock(p *shaderir.Program, topBlock, block *shaderir.Block, level int, localVarIndex int) []string {
idt := strings.Repeat("\t", level+1)
var lines []string
for _, t := range block.LocalVars {
// The type is None e.g., when the variable is a for-loop counter.
if t.Main != shaderir.None {
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, c.metalVarDecl(p, &t, fmt.Sprintf("l%d", localVarIndex), false, false), c.metalVarInit(p, &t)))
}
localVarIndex++
}
var metalExpr func(e *shaderir.Expr) string
metalExpr = 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 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:
op = string(e.Op)
default:
op = fmt.Sprintf("?(unexpected op: %s)", string(e.Op))
}
return fmt.Sprintf("%s(%s)", op, metalExpr(&e.Exprs[0]))
case shaderir.Binary:
return fmt.Sprintf("(%s) %s (%s)", metalExpr(&e.Exprs[0]), e.Op, metalExpr(&e.Exprs[1]))
case shaderir.Selection:
return fmt.Sprintf("(%s) ? (%s) : (%s)", metalExpr(&e.Exprs[0]), metalExpr(&e.Exprs[1]), metalExpr(&e.Exprs[2]))
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))
}
for i := 0; i < p.TextureNum; i++ {
args = append(args, fmt.Sprintf("T%d", i))
}
}
for _, exp := range e.Exprs[1:] {
args = append(args, metalExpr(&exp))
}
if callee.Type == shaderir.BuiltinFuncExpr && callee.BuiltinFunc == shaderir.Texture2DF {
return fmt.Sprintf("%s.sample(texture_sampler, %s)", args[0], strings.Join(args[1:], ", "))
}
return fmt.Sprintf("%s(%s)", metalExpr(&callee), strings.Join(args, ", "))
case shaderir.FieldSelector:
return fmt.Sprintf("(%s).%s", metalExpr(&e.Exprs[0]), metalExpr(&e.Exprs[1]))
case shaderir.Index:
return fmt.Sprintf("(%s)[%s]", metalExpr(&e.Exprs[0]), metalExpr(&e.Exprs[1]))
default:
return fmt.Sprintf("?(unexpected expr: %d)", e.Type)
}
}
for _, s := range block.Stmts {
switch s.Type {
case shaderir.ExprStmt:
lines = append(lines, fmt.Sprintf("%s%s;", idt, metalExpr(&s.Exprs[0])))
case shaderir.BlockStmt:
lines = append(lines, idt+"{")
lines = append(lines, c.metalBlock(p, topBlock, &s.Blocks[0], level+1, localVarIndex)...)
lines = append(lines, idt+"}")
case shaderir.Assign:
// TODO: Give an appropriate context
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, metalExpr(&s.Exprs[0]), metalExpr(&s.Exprs[1])))
case shaderir.If:
lines = append(lines, fmt.Sprintf("%sif (%s) {", idt, metalExpr(&s.Exprs[0])))
lines = append(lines, c.metalBlock(p, topBlock, &s.Blocks[0], level+1, localVarIndex)...)
if len(s.Blocks) > 1 {
lines = append(lines, fmt.Sprintf("%s} else {", idt))
lines = append(lines, c.metalBlock(p, topBlock, &s.Blocks[1], level+1, localVarIndex)...)
}
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 := 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 = string(s.ForOp)
default:
op = fmt.Sprintf("?(unexpected op: %s)", string(s.ForOp))
}
t := s.ForVarType
init := constantToNumberLiteral(ct, s.ForInit)
end := constantToNumberLiteral(ct, s.ForEnd)
ts := typeString(&t, false, false)
lines = append(lines, fmt.Sprintf("%sfor (%s %s = %s; %s %s %s; %s) {", idt, ts, v, init, v, op, end, delta))
lines = append(lines, c.metalBlock(p, topBlock, &s.Blocks[0], level+1, localVarIndex)...)
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, vertexOut))
case topBlock == &p.FragmentFunc.Block:
lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, fragmentOut))
case len(s.Exprs) == 0:
lines = append(lines, idt+"return;")
default:
// TODO: Give an appropriate context.
lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, metalExpr(&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
}

View File

@ -0,0 +1,105 @@
// 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.
package metal
import (
"fmt"
"github.com/hajimehoshi/ebiten/internal/shaderir"
)
func typeString(t *shaderir.Type, packed bool, ref bool) string {
switch t.Main {
case shaderir.Array:
st := typeString(&t.Sub[0], packed, false)
t := fmt.Sprintf("array<%s, %d>", st, t.Length)
if ref {
t += "&"
}
return t
case shaderir.Struct:
panic("metal: a struct is not implemented")
default:
t := basicTypeString(t.Main, packed)
if ref {
t += "&"
}
return t
}
}
func basicTypeString(t shaderir.BasicType, packed bool) 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:
if packed {
return "packed_float2"
}
return "float2"
case shaderir.Vec3:
if packed {
return "packed_float3"
}
return "float3"
case shaderir.Vec4:
if packed {
return "packed_float4"
}
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 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.Mod:
return "fmod"
case shaderir.Texture2DF:
return "?(texture2D)"
}
return string(f)
}