none
Using Pixel Shader 3.0 in DirectX

    Question

  • Hi All,

             I am working with an WPF application which uses directX. I need to implement some effect using Pixel Shader 3.0. I can't use Pixel Shader 2.0 because of limitation of number of  instruction set with Pixle Shader 2.0. Can anyone tell me how to use Pixel Shader 3.0 with directX. Any help will be of great value.

    Thanks in advance.

    Thursday, November 10, 2011 4:33 AM

Answers

All replies

  • Just to clarify something ahead.

    Do you use System.Windows.Media.Effects.ShaderEffect objects or have you embed a native DirectX area to your application?

    Thursday, November 10, 2011 5:28 AM
  • Ralf, Thanks for your answer, ya i am using native DirectX effect (.fx) to draw my graphics. I can draw using 2.0 version but not 3.0. If you want i can share my code as well.

     

    Thanks,

    Thursday, November 10, 2011 5:41 AM
  • By using FX files the only thing that should be necessary is changing the shader model there. Have you checked if your hardware/driver support shader model 3.0? You might although activate the DirectX debug runtime to check if there is any error message that could give you a hint.
    Thursday, November 10, 2011 6:50 AM
  • Ralf,

           I am using the following .fx file, Please have a look at it and tell me if you need any further input from  my side. when i make it ps_3_0 it doesn't work while it is working with ps_2_0.

     


    /// <description>A transition effect </description>
    /// <summary>The amount(%) of the transition from first texture to the second texture. </summary>
    /// <minValue>0</minValue>
    /// <maxValue>1</maxValue>
    /// <defaultValue>0</defaultValue>
    float Progress : register(C0);

    /// <summary>The Center point of the effect </summary>
    /// <minValue>0,0</minValue>
    /// <maxValue>1,1</maxValue>
    /// <defaultValue>.5,.5</defaultValue>
    float2 Center: register(C1);

    float2 AspectRatio : register(C2);


    texture Texture;
    sampler Texture2 = sampler_state
    {
        texture = <Texture>;
        magfilter = LINEAR;
        minfilter = LINEAR;
        mipfilter = LINEAR;
        AddressU = mirror;
        AddressV = mirror;
    };

    sampler input : register(s0);

    struct VS_OUTPUT
    {
        float4 Position  : POSITION;
        float4 Color     : COlOR;
        float2 UV        : TEXCOORD;
    };

    float4 Circle(float2 uv,float progress)
    {   
        float i = (floor(uv.y / 0.15) - 1) * 0.15;   
        float2 c = Center;
        c.y +=  i;
        float len1 =   length((uv - c) / AspectRatio);
        c.y += 0.15;
        float len2 =   length((uv - c) / AspectRatio);
        c.y += 0.15;
        float len3 =   length((uv - c) / AspectRatio);   

        if(len1 < progress || len2 < progress || len3 < progress)
        {
            return tex2D(Texture2, uv);
        }
        else
        {
            return tex2D(input, uv);
        }

    }

    //--------------------------------------------------------------------------------------
    // Pixel Shader
    //--------------------------------------------------------------------------------------
    float4 mainPS(VS_OUTPUT input) : COlOR
    {
      return Circle(input.UV, Progress);
    }


    technique TransformTexture {
        pass p0 {       
            PixelShader = compile ps_3_0 mainPS();
        }
    }

    Thursday, November 10, 2011 7:49 AM
  • I don’t spot a vertex shader in your FX file. If I remember correctly the use of Pixel Shader 3.0 requires that you use a 3.0 Vertex Shader, too

    Thursday, November 10, 2011 9:32 AM
  • Ralf, As per my requirement I don't need to implement a Vertex Shader, but as suggested by I implemented a dummy Vertex Shader but that didn't changed anything. My problem is still the same.

    Here is modified code.


    /// <description>A transition effect </description>
    /// <summary>The amount(%) of the transition from first texture to the second texture. </summary>
    /// <minValue>0</minValue>
    /// <maxValue>100</maxValue>
    /// <defaultValue>30</defaultValue>
    float Progress : register(C0);

    /// <summary>The Center point of the effect </summary>
    /// <minValue>0,0</minValue>
    /// <maxValue>1,1</maxValue>
    /// <defaultValue>.5,.5</defaultValue>
    float2 Center: register(C1);

    float2 AspectRatio : register(C2);


    texture Texture;
    sampler Texture2 = sampler_state
    {
        texture = <Texture>;
        magfilter = LINEAR;
        minfilter = LINEAR;
        mipfilter = LINEAR;
        AddressU = mirror;
        AddressV = mirror;
    };

    sampler input : register(s0);

    struct VS_OUTPUT
    {
        float4 Position  : POSITION;
        float4 Color     : COlOR;
        float2 UV        : TEXCOORD;
    };

    float4 Circle(float2 uv,float progress)
    {
        float a=1.0;
        for(int i = 0;i<7;i++)
        {
            float len=    length((uv - (Center + float2(0,i *.15))) / AspectRatio);
            if(len < a)
                a=len;
        }
        if(a<progress)
        {
            return tex2D(Texture2, uv);
        }
        else
        {
        return tex2D(input, uv);
        }
    }

    float4 mainVS(float3 pos : POSITION) : POSITION{
        return float4(0,0,0,1);
    }

    //--------------------------------------------------------------------------------------
    // Pixel Shader
    //--------------------------------------------------------------------------------------
    float4 mainPS(VS_OUTPUT input) : COlOR
    {
      return Circle(input.UV, Progress);
    }


    technique TransformTexture {
        pass p0 {

            CullMode = None;     
            VertexShader = compile vs_3_0 mainVS();
            PixelShader = compile ps_3_0 mainPS();
        }
    }

    Thursday, November 10, 2011 11:14 AM
  • With your vertex shader nothing will rendered at all as all points will be at the same position.

    Thursday, November 10, 2011 3:37 PM
  • Ralf, I dont know the proper application of Vertex Shader, but as per my knowledge i don't need a Vertex Shader for my problem. If it is required to implement Vertex Shader, then please tell me required correction in my code. How I can get this effect working?
    Friday, November 11, 2011 4:51 AM
  • You cannot mix 3.0 profile shaders. If you are using a Pixel Shader profile 3.0 you *must* use a Vertex Shader profile 3.0. If you are using a "NULL" Vertex Shader you are actually using the legacy fixed-function pipeline which cannot work with Pixel Shader 3.0. See http://msdn.microsoft.com/en-us/library/windows/desktop/bb147365.aspx

    You must have a Vertex Shader 3.0 profile even if it is just a pass-through to successfully use a Pixel Shader profile 3.0 shader.

    Have you considered using one of the extended 2.0 pixel shader models like ps_2_b if you're just in need of a longer pixel shader?

    BTW, this strict requirement to match VS3 with PS3 is one of the reasons why 10level9 feature levels for DirectX 11 do not actually use the 3.0 shader profile. See http://msdn.microsoft.com/en-us/library/windows/desktop/ff476876.aspx#Overview  The other reason is that one of the graphics vendors decided not to implement vertex texture fetch which was intended to be a required feature of VS 3.0.



    Tuesday, November 15, 2011 12:04 AM
  • Chuck,

               Thanks for reply.

    Tuesday, November 15, 2011 5:22 AM