4

I am trying to write a c# program to apply a vertex shader using SharpDx. I am not sure what are the right functions to call. The purpose of the shader is to make the graphics look a little better because it looks pixelated.

I have written the program in c++ and proved that my Vertex shader works. Now I want to try and write it in c# using SharpDx, but it looks like the shader is never getting applied. I have to use Direct3D9 and am not able to upgrade to 11.

Here is the shader code:

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

float4x4 gWorldViewProjMatrix;

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void spriteVS(in  float4 inPos       : POSITION0,
          in  float2 inTex0      : TEXCOORD0,

          out float4 oPos        : POSITION0,
          out float2 oTex0       : TEXCOORD0
         )
{
  //-- fix half pixel offset problem with sprites.
  inPos.x -= 0.5f;
  inPos.y += 0.5f;

  inPos.w  = 1.0f;

  //-- transform into screenspace
  oPos    = mul(inPos, gWorldViewProjMatrix);

  oTex0   = inTex0;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
technique Shader
{
   pass
   {
      VertexShader = compile vs_3_0 spriteVS();
      PixelShader = NULL;
   }
}

Here is how I am initializing the shader:

public class StandardVertexShader
  {
    public Effect m_vertexShaderEffect { get; set; }

    public BaseEffect m_baseEffect { get; set; }

    public EffectHandle m_EffectParamWorldMatrix { get; set; }

    public VertexShader m_vertexShader { get; set; }

    public EffectHandle shaderFunction { get; set; }

    public void InitEffect( Device d3dDevice )
    {
      // Read the shader binary
      ShaderBytecode VertexShaderByteCode = ShaderBytecode.FromFile("..\\DirectX\\Shaders\\shader.bin");

      // Store the effect read from the file
      m_vertexShaderEffect = Effect.FromStream(d3dDevice, VertexShaderByteCode.Data, ShaderFlags.None);

      // At this point I think the technique is already chosen. No need to validate?

      // pass the effect over to the base effect class
      m_baseEffect = new BaseEffect(m_vertexShaderEffect.NativePointer);

      // Get the technique from the base effect class
      m_vertexShaderEffect.Technique = m_baseEffect.GetTechnique("Shader");

      // validate the technique
     m_vertexShaderEffect.ValidateTechnique(m_vertexShaderEffect.Technique);

      // get the world matrix from the binary
      m_EffectParamWorldMatrix = m_vertexShaderEffect.GetParameter(null, "gWorldViewProjMatrix");

    }

Here is where I am actually trying to use the shader:

// Setup the world matrix (Identity)
Matrix worldMatrix = new Matrix();
worldMatrix.SetIdentity();
RawMatrix rawWorldMatrix = worldMatrix.GetRawMatrix();

// Setup View Matrix
RawMatrix viewMatrix = D3DDevice.GetTransform(TransformState.View);

// Setup Projection Matrix
Matrix projectionMatrix = new Matrix();
projectionMatrix.SetOrthographicProjection(clientWidth, clientHeight, 0.1f, 1000.0f);
RawMatrix rawProjMatrix = projectionMatrix.GetRawMatrix();

// Set all of the transforms
D3DDevice.SetTransform(TransformState.World, rawWorldMatrix);
D3DDevice.SetTransform(TransformState.View, viewMatrix);
D3DDevice.SetTransform(TransformState.Projection, rawProjMatrix); 

// Setup the WorldViewProj Matrix
worldViewProjMatrix = projectionMatrix.GetTranspose();
RawMatrix tempMatrix = worldViewProjMatrix.GetRawMatrix();
m_vertexShader.m_vertexShaderEffect.SetValue(
      m_vertexShader.m_EffectParamWorldMatrix, tempMatrix);

m_vertexShader.SetTechnique("Shader");

m_vertexDeclaration.Set();
m_vertexShader.m_vertexShaderEffect.Begin();
m_vertexShader.m_vertexShaderEffect.BeginPass(0);

Render();

m_vertexShader.m_vertexShaderEffect.EndPass();
m_vertexShader.m_vertexShaderEffect.End();

I expected the graphics to look cleaner like it did for my c++ program, but it looks like nothing is happening. I even tried a bigger offset on inPos.x and inPos.y by setting them to 100.0f in the actual shader just to prove to myself that it wasn't working

Frank
  • 215
  • 3
  • 9
  • These are also the namespaces that I am using: using SharpDX.Direct3D9; using SharpDX.Windows; using SharpDX.Mathematics.Interop; – Frank Feb 12 '19 at 00:23

0 Answers0