Search code examples
mdxshadermanaged-directx

Basic samples for shader in Managed DirectX


I am new write a pixel shader and use in my managed directx project, where i can get some basic sample to start it.


Solution

  • I'm assuming you know how to create a device.

    First, you need to prepare the shader itself.

    Here's a little sample pixel shader (which uses pixel shader 1.4, as seen by ps_1_4; r0 is a register that is read as the final result; v0 is a register that stores primary colour (of diffuse lighting)):

    ps_1_4
    mov r0, v0

    This shader, which is in shader assembly, has to be assembled. You can do that as follows (note that you need D3DX library referenced, otherwise you won't see the ShaderLoader class):

    Imports Microsoft.DirectX
    
    ' other code
    
    Dim graphicsStream As GraphicsStream = Direct3D.ShaderLoader.FromString(shaderText, Nothing, Direct3D.ShaderFlags.None)
    
    ' other code.
    

    After assembling the shader, you can finally create a PixelShader object as follows:

    ' other code
    
    Dim p As Direct3D.PixelShader = New Direct3D.PixelShader(Device, graphicsStream)
    
    ' other code
    

    To apply the pixel shader, use:

    ' other code
    
    Device.PixelShader = p
    
    ' other code
    

    where Device is the Direct3D Device.

    A similar process applies for compiling shaders in case you use HLSL.

    Edit: Just noticed this was an year old question.