Jump to content

  • Log In with Google      Sign In   
  • Create Account


Vertex Shader Clamps 0-1... Need float!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 dssannyasi   Members   -  Reputation: 102

Like
0Likes
Like

Posted 15 January 2013 - 06:34 PM

I have written a point cloud viewer using slimdx and directx 9.

We use it to visualize and render images from some content creation software we're building here.

Currently my renderer can display points and polys. The main purpose is to render EXR files with color and zDepth (floating point values > 1) information in the alpha.

 

My render target is 32bit float, and I dump the framebuffer to an exr file.

When rendering polys it works perfectly, I just calculate the color of the pixel shader like this (hlsl).    

 

color = Input.color;
color.w = distance(Input.Position, cameraPosition),

 

    and I get values in real world units away from the camera. So if a object is 1000 units away the alpha of the exr shows 1000 for that pixel's raw value;

 

 

 

Now I'm trying to render just the vertices of point clouds using a vertex shader with the same calculation as above. But my depth values clamp themselves to a value of 1.

 

If I change the above code to    

 

color = Input.color;
color.w = (distance(Input.Position, cameraPosition) - near) / far;

(near and far are my camera clipping distances)    This scales the depth values into the 0-1 range and they output this way, but I need greater precision and real world units!

 

Is there a limitation on vertex shaders that won't allow these kinds of floating point operations?

 

Thanks



Sponsor:

#2 Hodgman   Moderators   -  Reputation: 27055

Like
0Likes
Like

Posted 15 January 2013 - 06:55 PM

Is there a limitation on vertex shaders that won't allow these kinds of floating point operations?

No.

What kind of variable is color in your vertex shader?



#3 Dancin_Fool   Members   -  Reputation: 581

Like
0Likes
Like

Posted 15 January 2013 - 08:14 PM

If you're using the color semantic, try switching it to TexCoord. On certain cards the color gets clamped from 0-1.



#4 dssannyasi   Members   -  Reputation: 102

Like
0Likes
Like

Posted 15 January 2013 - 08:39 PM

Thanks for the responses... I was writing this up and found the issue and have resolved it... still not certain why its an issue.. maybe the clamping issue you spoke of "Dancin Fool"... I've included the shaders I was using incase anyone is able to give me any more insight.


While writing this post I saw the differences in my shaders in the use of the depth : TEXCOORD0 parameter in the struct and being used in the poly shader.
For the point clouds I figured since I only need points I shouldn't use a pixel shader thats why the 2nd shader only uses a vertexShader.

I've switch the struct used to the one in the poly shader and added the same pixelShader and now I have proper depth.

As a test I also tried switching the poly shader to write to color.w in the vertex shader and left out the color.w = depth from the pixelShader, and sure enough even the polys were then clamped at 1.

Any one know why this extra step is necessary?, Why can't a vertex Shader write floating values to the screen? And does the VertexFormat used while passing the draw codes matter once the data is pushed to the gfxCard? (I would think not since my VertexFormat Struct in Slimdx doesn't even have texture coordinates in the first place)

Thanks



Here is the Pixel shader (This works on polys).

float4x4 WVP;
float4 camPos;
float near;
float far;

/////// STRUCTS ////////////////////////////////////////////

struct VS_INPUT
{
float4 Color : COLOR0;
float4 Position : POSITION0;
};


struct VS_OUTPUT
{
float4 Color : COLOR0;
float4 Position : POSITION0;
float Depth : TEXCOORD0;
};


//// SHADER  ///////////////////////////////////////////////

VS_OUTPUT VS( VS_INPUT Input )
{
    VS_OUTPUT Output;

    Output.Position = mul( Input.Position, WVP );
    Output.Color = Input.Color;
    Output.Depth = distance(Input.Position, camPos);
    return( Output );
}

float4 PS(VS_OUTPUT input) : COLOR0
{
    float4 Color = input.Color;
    Color.w = input.Depth;
    return (Color);
}


////// Techniques   /////////////////////////////////////////

technique depthRaw
{
    pass Pass0
    {   
        VertexShader = compile vs_2_0 VS();
        PixelShader  = compile ps_2_0 PS();
    }
}

 


Here's the Vertex Shader... (clamps 0-1)

 

 

float4x4 WVP;
float4 camPos;
float near;
float far;

/////// STRUCTS ////////////////////////////////////////////

struct VS_INPUT
{
    float4 Color : COLOR0;
    float4 Position : POSITION0;
};


struct VS_OUTPUT
{
    float4 Color : COLOR0;
    float4 Position : POSITION0;
};


//// SHADERS  //////////////////////////////////////////////




VS_OUTPUT VS( VS_INPUT Input)
{
    VS_OUTPUT Output;


    Output.Position = mul( Input.Position, WVP );

    Output.Color = Input.Color;
    //Output.Color.w = distance(Input.Position, camPos);               // NEED THIS TO WORK
    Output.Color.w = (distance(Input.Position, camPos) - near) / far;  // THIS IS WORKAROUND
    return(Output);
}

////// Techniques   /////////////////////////////////////////


technique pointDepth
{
    pass Pass0
    {   
        VertexShader = compile vs_2_0 VS();
    }
}

 



#5 Hodgman   Moderators   -  Reputation: 27055

Like
0Likes
Like

Posted 15 January 2013 - 09:40 PM

From shader-model 3.0 an onwards, you can't just use a vertex-shader by itself, it always needs to be paired with a pixel shader.

In your case, where you're not supplying your own pixel shader (and using shader-model 2.0), you're asking the driver to automatically write one for you and I wouldn't trust it to do the right thing, especially when it comes to advanced features like floating-point render targets.

 

Anyway, as Dancin_Fool said, the issue is that you're trying to pass your floating-point value through a colour register. As this page says: "Color registers ... have lower precision than most registers, guaranteed to have 8 bits of unsigned data in the range (0, +1)."

 

You should be using a tex-coord register to pass your floating point value to the pixel shader.


Edited by Hodgman, 15 January 2013 - 09:42 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS