zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs | README

type.go (2456B)


      1 // Copyright 2020 The Ebiten Authors
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package metal
     16 
     17 import (
     18 	"fmt"
     19 
     20 	"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
     21 )
     22 
     23 func typeString(t *shaderir.Type, packed bool, ref bool) string {
     24 	switch t.Main {
     25 	case shaderir.Array:
     26 		st := typeString(&t.Sub[0], packed, false)
     27 		t := fmt.Sprintf("array<%s, %d>", st, t.Length)
     28 		if ref {
     29 			t += "&"
     30 		}
     31 		return t
     32 	case shaderir.Struct:
     33 		panic("metal: a struct is not implemented")
     34 	default:
     35 		t := basicTypeString(t.Main, packed)
     36 		if ref {
     37 			t += "&"
     38 		}
     39 		return t
     40 	}
     41 }
     42 
     43 func basicTypeString(t shaderir.BasicType, packed bool) string {
     44 	switch t {
     45 	case shaderir.None:
     46 		return "?(none)"
     47 	case shaderir.Bool:
     48 		return "bool"
     49 	case shaderir.Int:
     50 		return "int"
     51 	case shaderir.Float:
     52 		return "float"
     53 	case shaderir.Vec2:
     54 		if packed {
     55 			return "packed_float2"
     56 		}
     57 		return "float2"
     58 	case shaderir.Vec3:
     59 		if packed {
     60 			return "packed_float3"
     61 		}
     62 		return "float3"
     63 	case shaderir.Vec4:
     64 		if packed {
     65 			return "packed_float4"
     66 		}
     67 		return "float4"
     68 	case shaderir.Mat2:
     69 		return "float2x2"
     70 	case shaderir.Mat3:
     71 		return "float3x3"
     72 	case shaderir.Mat4:
     73 		return "float4x4"
     74 	case shaderir.Array:
     75 		return "?(array)"
     76 	case shaderir.Struct:
     77 		return "?(struct)"
     78 	default:
     79 		return fmt.Sprintf("?(unknown type: %d)", t)
     80 	}
     81 }
     82 
     83 func builtinFuncString(f shaderir.BuiltinFunc) string {
     84 	switch f {
     85 	case shaderir.BoolF:
     86 		return "static_cast<bool>"
     87 	case shaderir.IntF:
     88 		return "static_cast<int>"
     89 	case shaderir.FloatF:
     90 		return "static_cast<float>"
     91 	case shaderir.Vec2F:
     92 		return "float2"
     93 	case shaderir.Vec3F:
     94 		return "float3"
     95 	case shaderir.Vec4F:
     96 		return "float4"
     97 	case shaderir.Mat2F:
     98 		return "float2x2"
     99 	case shaderir.Mat3F:
    100 		return "float3x3"
    101 	case shaderir.Mat4F:
    102 		return "float4x4"
    103 	case shaderir.Inversesqrt:
    104 		return "rsqrt"
    105 	case shaderir.Mod:
    106 		return "fmod"
    107 	case shaderir.Texture2DF:
    108 		return "?(texture2D)"
    109 	}
    110 	return string(f)
    111 }