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

HLSL Return colour not displaying correctly

Last post 08-22-2008 2:02 PM by Darryl JF. 3 replies.
  • 08-21-2008 11:27 PM

    HLSL Return colour not displaying correctly

    Hey,

    I'm writing a GPU ray tracer which takes the vertices from a simple cube and writes these to a texture i create manually(as an RGBA 32 float). I then pass this texture to the GPU and read the texture,3 pixels at a time to get a triangle. I then pass the ray and three vertices to a function RayTriangleIntersectionTest which returns true if the ray hits or false if misses. The function works good.

    The code below runs through 12 triangles from the texture and returns a hit from two of them. The final block of code says if any triangles were hit then pixelcolour is Red-- however the screen displays black. I've run it through PIX and it clearly shows that pixelcolour is returned red - but it still shows on the screen as black.

    To further baffle me, you'll see a commented out piece of code. If i remove the comment the triangle renders. I could think I'm not looking at the triangles but the commented out triangle is a copy of a triangle in the texture which PIX shows it reads and process fine.

    I've been stuck on this for days on end - I would appreciate any ideas anyone has. Thanks very much.

    float4 PS(PS_INPUT input) : SV_Target  
    {  
        float4 Background   = float4(0.0f, 0.0f, 0.0f, 1.0f);  
        float4 Red          = float4(1.0f, 0.0f, 0.0f, 1.0f);  
        float4 pixelcolour = Background;  
     
     
        // calculate primary ray  
        Ray ray;  
        ray.origin    = focal;  
        ray.direction = normalize(float4(ray.origin.x - input.coords.x,  
                                         ray.origin.y - input.coords.y,  
                                         255,   
                                         1.0f));  
        bool g_hit = false;  
        float4 vert1,vert2,vert3;  

       float texel = 1.0f / (float)12;  // 4 triangles per row

        for (int i=0; i< rows; ++i)  
        {  
            for (int j=0;j < columns; j+=3)           
            {  
                float v = i * texel;  
                float u = j * texel;  
                float4 v1 = vertex.Sample(VertexSampler, float2(u,v));  
                u = (j+1) * texel;  
                float4 v2 = vertex.Sample(VertexSampler, float2(u,v));  
                u = (j+2) * texel;  
                float4 v3 = vertex.Sample(VertexSampler, float2(u,v));  
     
                // This eventually passes in triangle below. It returns "hit"  
                bool hit=RayTriangleIntersectionTest( ray, v1, v2, v3);  
                if (hit)  
                {  
                    g_hit= true;  
                }  
     
    //  This displays the triangle  
    //          hit=RayTriangleIntersectionTest( ray,   float4(250.0, 500.0,-250.0,1),  
    //                                                  float4(-250.0,00.0,-250.0, 1),  
    //                                                  float4(-250.0,500.0,-250.0,1));  
    //          if (hit)  
    //              g_hit= true;  
            }   
        }  
     
        if (g_hit)  
        {  
            pixelcolour=Red;  
        }  
     
        return pixelcolour;  

     

  • 08-22-2008 6:48 AM In reply to

    Re: HLSL Return colour not displaying correctly

    As PIX says red the error is properly not in your shader. Have you checked your pipeline configuration? If I remember right the Direct3D 10 debug runtime doesn’t check for logical errors that can cause that nothing is rendered at all. Common mistakes here are forgotten to set the render target or using a wrong blend state object.

  • 08-22-2008 9:12 AM In reply to

    Re: HLSL Return colour not displaying correctly

    Thanks Ralf, maybe i am looking in the wrong place. I'll check it out.
  • 08-22-2008 2:02 PM In reply to

    Re: HLSL Return colour not displaying correctly

    I've solved it.   This is about creating a manual texure in Direct3D 10, populating with data  and sending it to the GPU for sampling.

    Instead of using texture.sample, a sampler state and a potentially suspect texel value 1.0f/width calculation --- use the Load (DirectX HLSL Texture Object). This samples texels without any filtering or sampling and ensures you get the right value. It's only for shader model 4 though.

    for (int i=0; i< rows; ++i)  
    {  
        for (int j=0;j < columns; j+=3)           
        {  
               vert1 = vertex.Load( float3(j,   i, 0));  
               vert2 = vertex.Load( float3(j+1, i, 0));  
               vert3 = vertex.Load( float3(j+2, i, 0));  
               bool hit=RayTriangleIntersectionTest( ray, vert1, vert2,vert3);  
            }  

    All the indications from PIX was that it was working good. I suppose it can't have. Oh well it works now. Thanks to anyone who spent any time looking at this.

     

Page 1 of 1 (4 items) Previous Next