3 people like it.

Minimal SlimDX DirectX 11 Compute Shader

This is the minimum F# code required to run and view a compute shader with SlimDX in DirectX 11.

 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: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
open SlimDX
open SlimDX.D3DCompiler
open SlimDX.Direct3D11
open SlimDX.Windows
open SlimDX.DXGI

[<EntryPoint>]
let main args =
    let form = new RenderForm("Test Window")
    let width, height = 640, 640
    do  form.SetBounds(0, 0, width, height)

    let swapChainDescription = 
        new SwapChainDescription(
            BufferCount = 1,
            Usage = (Usage.RenderTargetOutput ||| Usage.UnorderedAccess),
            OutputHandle = form.Handle,
            IsWindowed = true,
            ModeDescription = ModeDescription(0, 0, Rational(60, 1), Format.R8G8B8A8_UNorm),
            SampleDescription = SampleDescription(1, 0))

    let result, device, swapChain = 
        Device.CreateWithSwapChain(
            DriverType.Hardware, 
            DeviceCreationFlags.Debug, 
            [|FeatureLevel.Level_11_0|], 
            swapChainDescription)

    let renderTarget = 
        use renderResource = Resource.FromSwapChain<Texture2D>(swapChain, 0)
        new RenderTargetView(device, renderResource)
    
    let computeShader =
        let code = @"
            RWTexture2D<float4> Output;

            [numthreads(32, 32, 1)]
            void main( uint3 threadID : SV_DispatchThreadID )
            {
                Output[threadID.xy] = float4(threadID.xy / 640.0f, 0, 1);
            }
        "
        use bytecode = ShaderBytecode.Compile(code, "main", "cs_5_0", ShaderFlags.None, EffectFlags.None) 
        new ComputeShader(device, bytecode)

    let computeResult = new UnorderedAccessView(device, renderTarget.Resource)      
    let context = device.ImmediateContext

    context.ComputeShader.Set(computeShader);
    context.ComputeShader.SetUnorderedAccessView(computeResult, 0);

    // set viewport
    context.Rasterizer.SetViewports(Viewport(0.0f, 0.0f, float32 width, float32 height, 0.0f, 1.0f))

    // runs per frame
    MessagePump.Run(form, MainLoop(fun () ->
        // clear render buffer to black
        context.ClearRenderTargetView(renderTarget, Color4())
        // run compute shader
        context.Dispatch(32, 32, 1);
        // copy output buffer to screen
        swapChain.Present(0, PresentFlags.None) |> ignore
    ))

    0 // return an integer exit code
Multiple items
type EntryPointAttribute =
  inherit Attribute
  new : unit -> EntryPointAttribute

Full name: Microsoft.FSharp.Core.EntryPointAttribute

--------------------
new : unit -> EntryPointAttribute
val main : args:string [] -> int

Full name: Script.main
val args : string []
val form : obj
val width : int
val height : int
val swapChainDescription : obj
Multiple items
type Format<'Printer,'State,'Residue,'Result> = PrintfFormat<'Printer,'State,'Residue,'Result>

Full name: Microsoft.FSharp.Core.Format<_,_,_,_>

--------------------
type Format<'Printer,'State,'Residue,'Result,'Tuple> = PrintfFormat<'Printer,'State,'Residue,'Result,'Tuple>

Full name: Microsoft.FSharp.Core.Format<_,_,_,_,_>
val result : obj
val device : obj
val swapChain : obj
val renderTarget : obj
val renderResource : System.IDisposable
val computeShader : obj
val code : string
val bytecode : System.IDisposable
union case Option.None: Option<'T>
val computeResult : obj
val context : obj
Multiple items
module Set

from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> =
  interface IComparable
  interface IEnumerable
  interface IEnumerable<'T>
  interface ICollection<'T>
  new : elements:seq<'T> -> Set<'T>
  member Add : value:'T -> Set<'T>
  member Contains : value:'T -> bool
  override Equals : obj -> bool
  member IsProperSubsetOf : otherSet:Set<'T> -> bool
  member IsProperSupersetOf : otherSet:Set<'T> -> bool
  ...

Full name: Microsoft.FSharp.Collections.Set<_>

--------------------
new : elements:seq<'T> -> Set<'T>
Multiple items
val float32 : value:'T -> float32 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float32

--------------------
type float32 = System.Single

Full name: Microsoft.FSharp.Core.float32

--------------------
type float32<'Measure> = float32

Full name: Microsoft.FSharp.Core.float32<_>
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Raw view Test code New version

More information

Link:http://fssnip.net/gx
Posted:11 years ago
Author:gradbot
Tags: slimdx , directx , compute , shader