Compute Shaders
FShade also provides an API for compute shaders which is not part of the effect/composition system. We opted for a very low abstraction here, since compute shaders are used vor various tasks and cannot easily be represented as pure functions. Here's a little example of a very basic compute shader adding two arrays/buffers.
1: 2: 3: 4: 5: 6: |
|
compute shaders can (like Effects) be compiled to a Module
which in turn can be assembled to GLSL using the default
pipeline. note that all array-inputs are translated to storage-buffers.
1: 2: 3: 4: 5: |
|
|
Special functions
the compute API includes various special-functions like
getGlobalId()
getLocalId()
getWorkGoupId()
Here's a shader using some of those built-in functions.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: |
|
|
Optimization
Due to the imperative nature of compute shaders we turned off several optimizations in the compiler by default. For example reordering statements may actually change the shader's results here. Our optimizations (designed for regular shaders) currently assume that shaders are functionally pure. We'll fix that in future FShade releases, but for the moment users need to perform some optimizations by hand.
type V3i =
struct
new : v:int -> V3i + 25 overloads
val X : int
val Y : int
val Z : int
member Abs : V3i
member AllDifferent : v:V3i -> bool + 1 overload
member AllEqual : v:V3i -> bool + 1 overload
member AllGreater : v:V3i -> bool + 1 overload
member AllGreaterOrEqual : v:V3i -> bool + 1 overload
member AllSmaller : v:V3i -> bool + 1 overload
...
end
--------------------
V3i ()
(+0 other overloads)
V3i(v: int) : V3i
(+0 other overloads)
V3i(a: int []) : V3i
(+0 other overloads)
V3i(v: int64) : V3i
(+0 other overloads)
V3i(a: int64 []) : V3i
(+0 other overloads)
V3i(v: float32) : V3i
(+0 other overloads)
V3i(a: float32 []) : V3i
(+0 other overloads)
V3i(v: float) : V3i
(+0 other overloads)
V3i(a: float []) : V3i
(+0 other overloads)
V3i(index_fun: System.Func<int,int>) : V3i
(+0 other overloads)
val LocalSize : V3i
--------------------
type LocalSizeAttribute =
inherit Attribute
new : unit -> LocalSizeAttribute
override ToString : unit -> string
member X : int
member Y : int
member Z : int
member X : int with set
member Y : int with set
member Z : int with set
--------------------
new : unit -> LocalSizeAttribute
val float32 : value:'T -> float32 (requires member op_Explicit)
--------------------
type float32 = System.Single
--------------------
type float32<'Measure> = float32
module ComputeShader
from Utilities
--------------------
module ComputeShader
from FShade
--------------------
type ComputeShader =
private new : id:string * method:MethodBase * localSize:V3i * data:Lazy<ComputeShaderData> -> ComputeShader
member csBody : Expr
member csBuffers : Map<string,ComputeBuffer>
member csId : string
member csImages : Map<string,ComputeImage>
member csLocalSize : V3i
member csMethod : MethodBase
member csSamplerStates : Map<(string * int),SamplerState>
member csShared : Map<string,(Type * int)>
member csTextureNames : Map<(string * int),string>
...
module ModuleCompiler
from Utilities
--------------------
module ModuleCompiler
from FShade.Imperative
--------------------
module ModuleCompiler
from FShade.SpirV Extensions
--------------------
module ModuleCompiler
from FShade.Backends
module GLSL
from Utilities
--------------------
namespace FShade.GLSL
type GLSLIntrinsicAttribute =
inherit IntrinsicAttribute
new : format:string -> GLSLIntrinsicAttribute
new : format:string * [<ParamArray>] requiredExtensions:string [] -> GLSLIntrinsicAttribute
private new : format:string * requiredExtensions:Set<string> -> GLSLIntrinsicAttribute
override Intrinsic : CIntrinsic
--------------------
new : format:string -> GLSLIntrinsicAttribute
new : format:string * [<System.ParamArray>] requiredExtensions:string [] -> GLSLIntrinsicAttribute
val ref : value:'T -> 'T ref
--------------------
type 'T ref = Ref<'T>
val int : value:'T -> int (requires member op_Explicit)
--------------------
type int = int32
--------------------
type int<'Measure> = int
type IntImage2d<'f (requires 'f :> ISignedFormat)> =
interface IImage
new : unit -> IntImage2d<'f>
member AtomicAdd : coord:V2i * data:int -> int
member AtomicAnd : coord:V2i * data:int -> int
member AtomicCompareExchange : coord:V2i * cmp:int * data:int -> int
member AtomicExchange : coord:V2i * data:int -> int
member AtomicMax : coord:V2i * data:int -> int
member AtomicMin : coord:V2i * data:int -> int
member AtomicOr : coord:V2i * data:int -> int
member AtomicXor : coord:V2i * data:int -> int
...
--------------------
new : unit -> IntImage2d<'f>
from FShade
type r32i =
interface ISignedFormat
new : unit -> r32i
--------------------
new : unit -> Formats.r32i