XNA Creators Club Online
Page 1 of 1 (13 items)
Sort Posts: Previous Next

More shader problems

Last post 11/16/2009 9:37 AM by Stainless. 12 replies.
  • 11/11/2009 1:01 PM

    More shader problems

    I'm trying to enhance my fractal system and  have hit a problem that makes no sense to me

    This shader fragment doesn't work

    //=====================================================================================================// 
    //=  Update the density map                                                                           =// 
    //=====================================================================================================// 
     
    void SpriteVertexShader(inout float4 position : POSITION0,inout float2 texCoord: TEXCOORD0) 
        float4 pos = tex2Dlod(CSampler,float4(texCoord,0,0)); 
        texCoord=pos.xy; 
        position.xy=(pos.xy*0.5f)+0.5f; 
        position.z=1; 
        position.w=1; 
     
    float4 UpdateDensity(float2 texCoord : TEXCOORD0) : COLOR0 
        float old = tex2D(DSampler,texCoord).x; 
        old=(old+1)/2.0f; 
         
        return float4(old,0,0,1); 
     
    technique UpdateDensity 
        pass Pass1 
        { 
            VertexShader = compile vs_3_0 SpriteVertexShader(); 
            PixelShader = compile ps_3_0 UpdateDensity(); 
        } 


    I feed it a point list and a texture full of fractal coordinates (vector2 format)


                        fractal.Parameters["coords"].SetValue(coordstexture); 
                        fractal.CurrentTechnique = fractal.Techniques["UpdateDensity"]; 
                        graf.VertexDeclaration = vertexDecl; 
                        graf.RenderState.AlphaBlendEnable = false;                     
                        fractal.Parameters["density"].SetValue(densitytexture1); 
                        graf.SetRenderTarget(0, density2); 
                        fractal.Begin(); 
                        fractal.CurrentTechnique.Passes[0].Begin(); 
                        graf.DrawUserPrimitives<PointSprite>(PrimitiveType.PointList, verts, 0, verts.Length); 
                        fractal.CurrentTechnique.Passes[0].End(); 
                        fractal.End(); 
                        graf.SetRenderTarget(0, null); 
                        densitytexture2 = density2.GetTexture(); 

    My vertex declaration is 

     public struct PointSprite 
        { 
            public Vector3 Position; 
            public Vector2 TextureCoordinate; 
     
            public static readonly VertexElement[] VertexElements = 
            new VertexElement[] {  
                new VertexElement(0,0,VertexElementFormat.Vector3, 
                    VertexElementMethod.Default,VertexElementUsage.Position,0), 
                new VertexElement(0,sizeof(float)*3,VertexElementFormat.Vector2, 
                    VertexElementMethod.Default,VertexElementUsage.TextureCoordinate,0), 
            }; 
            public PointSprite(Vector3 position, Vector2 tex, Color color) 
            { 
                Position = position; 
                TextureCoordinate = tex; 
            } 
            public static int SizeInBytes = sizeof(float) * (3 + 2); 
        } 

    I cannot see anything wrong with that, so I grudgingly booted up pixwin, and as usual it just confused the matter further

    When I look at the drawUserPrimitives call the inputs are all correct, but the post vertex shader coordinates are all wrong.

    Every vertex is at 0.5,0.5,1,1  with a texture coordinate of 0,0

    When I debug a vertex, the results are correct but they aren't being returned. For some reason I end up with the rubbish pixwin shows.

    WHY?


    The vertex shader disassembles to 

    // 
    // Generated by Microsoft (R) D3DX9 Shader Compiler 9.15.779.0000 
    // 
    // Parameters: 
    // 
    //   sampler2D CSampler; 
    // 
    // 
    // Registers: 
    // 
    //   Name         Reg   Size 
    //   ------------ ----- ---- 
    //   CSampler     s2       1 
    // 
     
        vs_3_0 
        def c0, 1, 0, 0.5, 0 
        dcl_texcoord v0  // texCoord<0,1> 
        dcl_2d s2 
        dcl_position o0 
        dcl_texcoord o1.xy 
        mul r1, c0.xxyy, v0.xyxx 
        texldl r0, r1, s2 
        mov r0.xy, r0  // pos<0,1> 
        mov o1.xy, r0  // texCoord<0,1> 
        mul r2.xy, r0, c0.z 
        add o0.xy, r2, c0.z  // position<0,1> 
        mov o0.zw, c0.x  // position<2,3> 
     
    // approximately 8 instruction slots used (2 texture, 6 arithmetic) 
     

    Now I think this shows a compiler bug.

    Doesn't o0.xy not get updated ?

    shouldn't the line   mov r0.xy,r0  be mov o0.xy,r0   ??????


    Information is not knowledge, knowledge is not wisdom, wisdom is not truth, truth is not beauty, beauty is not love, love is not music, music is the best! Wisdom is the domain of the Wis (which is extinct).
  • 11/11/2009 1:39 PM In reply to

    Re: More shader problems

    I changed the shader to have more formal parameters.

    //=====================================================================================================// 
    //=  Update the density map                                                                           =// 
    //=====================================================================================================// 
     
    struct VertexShaderInput 
        float4 Position : POSITION0; 
        float2 texCoord : TEXCOORD0; 
     }; 
      
    struct VertexShaderOutput 
        float4 Position : POSITION0; 
        float2 texCoord : TEXCOORD0; 
    }; 
    VertexShaderOutput SpriteVertexShader(VertexShaderInput input) 
        VertexShaderOutput output; 
        float4 pos = tex2Dlod(CSampler,float4(input.texCoord,0,0)); 
         
        output.texCoord=pos.xy; 
         
        pos.xy=(pos.xy*0.5f)+0.5f; 
        pos.z=1; 
        pos.w=1; 
        output.Position=pos; 
        return output; 
     
    float4 UpdateDensity(VertexShaderOutput input) : COLOR0 
        float old = tex2D(DSampler,input.texCoord).x; 
        old=(old+1)/2.0f; 
         
        return float4(old,0,0,1); 
     
    technique UpdateDensity 
        pass Pass1 
        { 
            VertexShader = compile vs_3_0 SpriteVertexShader(); 
            PixelShader = compile ps_3_0 UpdateDensity(); 
        } 


    And this produced the exact same results, but in a different way.

    // 
    // Generated by Microsoft (R) D3DX9 Shader Compiler 9.15.779.0000 
    // 
    // Parameters: 
    // 
    //   sampler2D CSampler; 
    // 
    // 
    // Registers: 
    // 
    //   Name         Reg   Size 
    //   ------------ ----- ---- 
    //   CSampler     s2       1 
    // 
     
        vs_3_0 
        def c0, 1, 0, 0.5, 0 
        dcl_texcoord v0  // input<4,5> 
        dcl_2d s2 
        dcl_position o0 
        dcl_texcoord o1.xy 
        mul r6, c0.xxyy, v0.xyxx 
        texldl r5, r6, s2 
        mov r4, r5.xyxx  // pos<0,1,0,0> 
        mov r2.xy, r4  // output<4,5> 
        mul r3, r4, c0.z 
        add r1, r3, c0.z  // pos<0,1,0,0> 
        mov r1, r1  // output<0,1,0,0> 
        mov r1, r1  // ::SpriteVertexShader<0,1,0,0> 
        mov o1.xy, r2  // ::SpriteVertexShader<4,5> 
        mul r0, r1, c0.xxyy 
        add o0, r0, c0.yyxx  // ::SpriteVertexShader<0,1,2,3> 
     
    // approximately 12 instruction slots used (2 texture, 10 arithmetic) 
     

    Those two mov r1,r1   look like another compiler bug.


    Information is not knowledge, knowledge is not wisdom, wisdom is not truth, truth is not beauty, beauty is not love, love is not music, music is the best! Wisdom is the domain of the Wis (which is extinct).
  • 11/11/2009 2:41 PM In reply to

    Re: More shader problems

    Okay it looks like it is pixwin failign again.

    I changed my coords texture to vector4 and the code worked.

    In pixwin I still have garbage.

    I have to say at this point do not use pixwin.

    It may be the only debugger we have, but it is so poor that it ends up sending you down the wrong path.

    I have wasted half a day trying to figure out bugs that don't exist when I should have been chasing the one that did exist.


    Information is not knowledge, knowledge is not wisdom, wisdom is not truth, truth is not beauty, beauty is not love, love is not music, music is the best! Wisdom is the domain of the Wis (which is extinct).
  • 11/11/2009 4:46 PM In reply to

    Re: More shader problems

    It's a shame you have such bad experience with Pix.  It's always worked great for me!

    XNA Framework Developer - blog - homepage
  • 11/11/2009 5:14 PM In reply to

    Re: More shader problems

    The disassembly PIX shows for your vertex shader looks fine.  I'm not sure what the problem is?

    vs_3_0    
        def c0, 1, 0, 0.5, 0    
        dcl_texcoord v0  // texCoord<0,1>    
        dcl_2d s2    
        dcl_position o0    
        dcl_texcoord o1.xy    
        mul r1, c0.xxyy, v0.xyxx  // r1: texCoord.x, texCoord.y, 0, 0  
        texldl r0, r1, s2         
        mov r0.xy, r0  // pos<0,1>        
        mov o1.xy, r0  // texCoord<0,1>   texCoord= (pos.x, pos.y)  
        mul r2.xy, r0, c0.z         r2 = (pos.x, pos.y, ?, ?)  
        add o0.xy, r2, c0.z  // position<0,1>   position = (pos.X + 0.5, pos.y + 0.5, ?, ?)  
        mov o0.zw, c0.x  // position<2,3>    position = (pos.X + 0.5, pos.y + 0.5, 1, 1)  
     
     
     
     

    I don't see any compiler bug.
  • 11/11/2009 6:38 PM In reply to

    Re: More shader problems

    The only bug was the surface format I was using, once I changed that the code worked.


    The problem was pixwin misreporting what was going on.

    Cost me half a day.
    Information is not knowledge, knowledge is not wisdom, wisdom is not truth, truth is not beauty, beauty is not love, love is not music, music is the best! Wisdom is the domain of the Wis (which is extinct).
  • 11/11/2009 6:41 PM In reply to

    Re: More shader problems

    Shawn Hargreaves:
    It's a shame you have such bad experience with Pix.  It's always worked great for me!



    Then either you haven't got enough features in your shaders yet, add more till it breaks.

    Or you have a different version of pixwin that I do, mine is about as useful as a chocolate fire guard.
    Information is not knowledge, knowledge is not wisdom, wisdom is not truth, truth is not beauty, beauty is not love, love is not music, music is the best! Wisdom is the domain of the Wis (which is extinct).
  • 11/11/2009 9:31 PM In reply to

    Re: More shader problems

    My main problem with PIX used to be to that it crashed all the time. But it has gotten more stable with each release of the SDK.  I haven't had it crash in a long time.

    It is true the shader debugger does not always show the correct values.  This is especially true when stepping through the HLSL, which is nearly impossible to follow except for the simplest shaders.  So I often have to revert to stepping through the compiled shader instructions.  I can't recall if I've ever seen garbage values when doing that.

    I know often the result calculated by the shader is sometimes not what is actually "returned".  I think the shader debugging is just a software simulation, correct?  It doesn't actually represent what took place on the GPU.  If I see discrepancies, that usually tells me I'm doing something unexpected/unsupported.


  • 11/12/2009 9:42 AM In reply to

    Re: More shader problems

    I have never seen it crash, however I very very often get a failure when I try to debug a shader.

    It just comes up with something like "pixwin encountered a problem " and you can't debug the shader.

    The shader debugger is rubbish, it returns values that don't match real world values, it cannot handle loops, it's just about useless.

    I would rather it allowed us to single step the assembler.


    Information is not knowledge, knowledge is not wisdom, wisdom is not truth, truth is not beauty, beauty is not love, love is not music, music is the best! Wisdom is the domain of the Wis (which is extinct).
  • 11/12/2009 12:10 PM In reply to

    Re: More shader problems

    You can... can you not??

    Go to the asm button above the code window instead of the src button.
    This shows the asm code and steps through it.
  • 11/14/2009 9:17 AM In reply to

    Re: More shader problems

    My version won't.

    Is there a location I can get a more upto date version?

    I'm on the march 2009 version
    Information is not knowledge, knowledge is not wisdom, wisdom is not truth, truth is not beauty, beauty is not love, love is not music, music is the best! Wisdom is the domain of the Wis (which is extinct).
  • 11/14/2009 6:48 PM In reply to
    • (2340)
    • premium membership MVP
    • Posts 1,225

    Re: More shader problems

    The latest version is August 2009, but March 2009 should work for this just fine. When you debug a shader there should be one tab showing the HLSL source, and another called "Disassembly" that shows the ASM. Picture
    Matt Pettineo | DirectX/XNA MVP


    Ride into The Danger Zone | PIX With XNA Tutorial
  • 11/16/2009 9:37 AM In reply to

    Re: More shader problems

    I can see the disassembly, and often refer to it, but when displaying the disassembly, the single step button is greyed out.

    Information is not knowledge, knowledge is not wisdom, wisdom is not truth, truth is not beauty, beauty is not love, love is not music, music is the best! Wisdom is the domain of the Wis (which is extinct).
Page 1 of 1 (13 items) Previous Next