Types
FShade translates .NET types to GLSL types using a simple scheme which is outlined here.
Due to limitations in GLSL various .NET constructs cannot be translated and FShade will simply fail (or produce invalid code) when trying to do so. However when used with care custom types can be quite helpful in FShade.
Records
F#'s record types get translated to simple structs and may therefore not be recursive in any way.
1: 2: 3: 4: 5: 6: 7: 8: 9: |
|
|
As you see here the resulting code defines the type itself as a GLSL struct and a constructor-function creating an instance of the type. The use of records is therefore relatively straight-forward as long as no rendering-engine interop is required (custom uniform-types, etc.)
Discriminated Unions
Union types are not that easy to translate, since GLSL does not provide mechanisms for encoding them directly. Therefore FShade translates union types to structs carrying all fields for each case and a special field holding a tag representing the constructor.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: |
|
|
Arrays
Since F# does not include a type for fixed-size arrays we decided to define our own type using type providers for the size. That ways array types can easily be written like:
1:
|
|
Here's a little shader calculating the sum of an array
1: 2: 3: 4: 5: 6: 7: 8: 9: |
|
|
Samplers
Shaders often need samplers for texture-lookups and similar uses. FShade defines its own types for samplers, which are rather similar to the ones defined by GLSL.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: |
|
|
{pos: V4d;
color: V4d;}
type PositionAttribute =
inherit SemanticAttribute
new : unit -> PositionAttribute
--------------------
new : unit -> PositionAttribute
type V4d =
struct
new : v:int -> V4d + 25 overloads
val X : float
val Y : float
val Z : float
val W : float
member Abs : V4d
member AllDifferent : v:V4d -> bool + 1 overload
member AllEqual : v:V4d -> bool + 1 overload
member AllGreater : v:V4d -> bool + 1 overload
member AllGreaterOrEqual : v:V4d -> bool + 1 overload
...
end
--------------------
V4d ()
(+0 other overloads)
V4d(v: int) : V4d
(+0 other overloads)
V4d(a: int []) : V4d
(+0 other overloads)
V4d(v: int64) : V4d
(+0 other overloads)
V4d(a: int64 []) : V4d
(+0 other overloads)
V4d(v: float32) : V4d
(+0 other overloads)
V4d(a: float32 []) : V4d
(+0 other overloads)
V4d(v: float) : V4d
(+0 other overloads)
V4d(a: float []) : V4d
(+0 other overloads)
V4d(index_fun: System.Func<int,float>) : V4d
(+0 other overloads)
type SemanticAttribute =
inherit Attribute
new : s:string -> SemanticAttribute
member Semantic : string
--------------------
new : s:string -> SemanticAttribute
{a: V4d;
b: float;}
val float : value:'T -> float (requires member op_Explicit)
--------------------
type float = System.Double
--------------------
type float<'Measure> = float
| Case1 of col: V4d
| Case2 of factor: int
| Case3
val int : value:'T -> int (requires member op_Explicit)
--------------------
type int = int32
--------------------
type int<'Measure> = int
module Arr
from Aardvark.Base.Arrays
--------------------
module Arr
from Aardvark.Base
--------------------
type Arr<'d,'a (requires 'd :> INatural)> =
interface IEnumerable<'a>
new : unit -> Arr<'d,'a>
new : elements:seq<'a> -> Arr<'d,'a>
member AsString : string
member Data : 'a []
member Item : i:int -> 'a with get
member Length : int
member Item : i:int -> 'a with set
--------------------
new : unit -> Arr<'d,'a>
new : elements:seq<'a> -> Arr<'d,'a>
val arr : Arr<N<...>,V4d>
--------------------
type arr<'a> =
private {root: FingerTreeNode<'a,int>;}
interface IEnumerable<'a>
interface IEnumerable
member private AsString : string
member Item : i:int -> 'a with get
member Length : int
Calls SamplerBaseBuilder.Texture
Calls SamplerBaseBuilder.AddressU
| Wrap = 0
| Mirror = 1
| Clamp = 2
| Border = 3
| MirrorOnce = 4
Calls SamplerBaseBuilder.AddressV
Calls SamplerBaseBuilder.Filter
| Anisotropic = 0
| MinLinearMagMipPoint = 1
| MinLinearMagPointMipLinear = 2
| MinMagLinearMipPoint = 3
| MinMagMipLinear = 4
| MinMagMipPoint = 5
| MinMagPointMipLinear = 6
| MinPointMagLinearMipPoint = 7
| MinPointMagMipLinear = 8
| MinMagPoint = 9
| MinMagLinear = 10
| MinPointMagLinear = 11
| MinLinearMagPoint = 12
Calls SamplerBaseBuilder.MinLod
Calls SamplerBaseBuilder.MaxLod
Calls SamplerBaseBuilder.MipLodBias