如何在C#中为D3D9应用顶点着色器?

时间:2019-02-12 00:17:34

标签: c# vertex-shader sharpdx direct3d9

我正在尝试编写一个C#程序以使用SharpDx应用顶点着色器。我不确定要调用的函数是什么。着色器的目的是使图形看起来更好,因为它看起来像是像素化的。

我已经用c ++编写了程序,并证明了我的Vertex着色器可以工作。现在,我想尝试使用SharpDx在c#中编写它,但是看起来好像从未应用过着色器。我必须使用Direct3D9,并且不能升级到11。

以下是着色器代码:

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

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;
   }
}

这是初始化着色器的方式:

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");

    }

实际上是我在这里尝试使用着色器的地方:

// 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();

我希望图形看起来像我的c ++程序一样干净,但是看起来什么都没有发生。我什至在实际的着色器中将inPos.x和inPos.y设置为100.0f来尝试更大的偏移量,只是向我自己证明它不起作用

0 个答案:

没有答案