• Create Account

## Use 64bit precision (GPU)

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.

10 replies to this topic

### #1montify  Members

590
Like
0Likes
Like

Posted 16 April 2014 - 12:35 PM

Hello,

I've bin working on a large Terrain, so now im on the Point that i reach the precision of float.

Im working with DX11 / ShaderModel 5 and this provided doubles on GPU..

My Question is: How i can use double (Matrix, inputPosition) instead of float?

I read something about Render relative to the Eye or so, but the simple solution should use doubles, but how?

### #2spazzarama  GDNet+

1626
Like
1Likes
Like

Posted 16 April 2014 - 04:51 PM

My Question is: How i can use double (Matrix, inputPosition) instead of float?

From MSDN HLSL Scalar Types: http://msdn.microsoft.com/en-us/library/windows/desktop/bb509646(v=vs.85).aspx

• double - 64-bit floating point value. You cannot use double precision values as inputs and outputs for a stream. To pass double precision values between shaders, declare each double as a pair of uint data types. Then, use the asdouble function to pack each double into the pair of uints and the asuint function to unpack the pair of uints back into the double.

So I think you will have to encode the doubles into a pair of uint values to pass them around.

Justin Stenning | Blog | Book - Direct3D Rendering Cookbook (using C# and SharpDX)

Projects: Direct3D Hook, EasyHook, Shared Memory (IPC), SharpDisasm (x86/64 disassembler in C#), Afterglow, C#raft

@spazzarama

### #3DementedCarrot  Members

1142
Like
1Likes
Like

Posted 16 April 2014 - 09:31 PM

If you want to render stuff relative to the eye in float space using doubles, you:

1. Use doubles for your position vectors.

2. Use the double vector for every object position, and for your camera.

Then you have to translate your positions into a float-capable space for rendering. You translate every object position to get the position relative to the eye with:



DoubleVector3 objectPosition = object.somePosition;
DoubleVector3 cameraPosition = camera.position;
DoubleVector3 doubleRelativePosition = objectPosition - cameraPosition;

// When you translate the object by the camera position, the resulting number is representable by a float.
// Just cast the double-vector components down to floats!

FloatVector3 relativePosition;
relativePosition.x = (float)doubleRelativePosition.x;
relativePosition.y = (float)doubleRelativePosition.y;
relativePosition.z = (float)doubleRelativePosition.z;

and then that's the position you pass into the shader for rendering.

This is really cumbersome for a ton of objects because you have to recompute this translation every time you move your camera. There is an extension of this method to keep you from creating relative coordinates every frame. You have to create a relative anchor point that moves with your camera. To do this you have to:

1. Create a double-vector anchor point that moves with your camera periodically. You move this anchor point when float-precision starts to become insufficient to represent points inside the float-anchor-area.
2. You build relative float-vector positions for everything relative to the anchor point, as we did before with the camera but with the anchor point.

3. When you move far enough away from the anchor, you re-locate it.

4. When the anchor moves you re-translate everything relative to the new anchor point. This means everything has a double-vector world position and a float-vector relative anchor position.

5. You use a regular camera view matrix to move around inside this anchor float space.
6. Draw everything normally as if the anchor-relative position is the position, and the anchor relative camera position is the camera location.

I hope this helps!

Edits: Typo-city

Edited by DementedCarrot, 16 April 2014 - 09:47 PM.

### #4Chris_F  Members

3018
Like
0Likes
Like

Posted 16 April 2014 - 10:09 PM

You should probably consider whether or not lack of precision is really the issue you are facing. Modern GPUs may support DP floating point because of SM5, but it is a feature which is almost never used by anyone (for games anyway) so consumer GPUs tend to be really bad at it. Take my GPU for example, a GTX 760 which is probably pretty typical. Switching from SP floats to DP floats is the difference between 2.25 teraflops and 94 gigaflops. That's a huge performance decrease.

### #5montify  Members

590
Like
0Likes
Like

Posted 17 April 2014 - 01:49 AM

Ok i should use relative to the eye

So i have one vertexbuffer for each childnode and rootnode.
Only the worldmatrix is changing.

My problem is i use cube2sphere mapping which require that the cubecenter is 0,0,0

With relative to the eye the camera position is at 0,0,0?
So whats my line? When i substract the camera position from the patch position the cube is out of the origin in worldspace

### #6DementedCarrot  Members

1142
Like
0Likes
Like

Posted 17 April 2014 - 09:12 AM

My problem is i use cube2sphere mapping which require that the cubecenter is 0,0,0

Can you be more specific about what cube2sphere mapping is, and what it's used for?

### #7montify  Members

590
Like
0Likes
Like

Posted 17 April 2014 - 09:32 AM

Oh Sorry, so :

Im working on a Procedural Planet with ChunkedLOD in earth Size.

To get a Sphere out of 6 Planes, i normalize each Vertex.

But the requirement is, the center of the cube (out of 6 planes) must be 0,0,0 (world-origin) like:

http://britonia.wordpress.com/2010/05/20/planet-geometry/

But here do the Vector3.normalize on the CPU, but i do it on the GPU:

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{

float3 worldPosition = mul(input.Position, World).xyz;

float3 normalized = normalize(worldPosition);
float4 scaled = float4(normalized * SeaLevel, 1);

output.Position = mul(mul(scaled, View), Projection);

output.UV = input.UV;

return output;
}


So, when i subtract the Camera Position from the QuadTree-Patch Position the Center of my Sphere/Cube is not 0,0,0 .

Edited by montify, 17 April 2014 - 10:28 AM.

### #8DementedCarrot  Members

1142
Like
0Likes
Like

Posted 17 April 2014 - 10:45 AM

That's a tough one. I'm not totally sure how to accomplish this when the world meshes are created that way.

I'll think on it.

### #9montify  Members

590
Like
0Likes
Like

Posted 17 April 2014 - 11:32 AM

nono the worldmesh created like:

I have one VertexBuffer 33x33 vertices. ( a flat Plane )

Now i made 6 of this Planet (Rotatet, Translate) to get a Cube.

Now i normalize the Input.Position in the Shader = A perfect Sphere.

---------------------

Is it possible and have it the same effect to translate the Position ( Position - cameraPosition) NOT in the WorldSpace but rather in ViewSpace?

### #10montify  Members

590
Like
0Likes
Like

Posted 19 April 2014 - 10:40 AM

So i have it .

I Set the Camera Posititon to Vector3.Zero and "hack" the worldMatrix

  //new View Matrix
var View = Matrix.CreateLookAt(Vector3.Zero, cam.Forward, cam.Up);

Vector3 cameraSpacePosition = (position - cam.Position);

//Relative to the Camera
world.M41 = cameraSpacePosition.X;
world.M42 = cameraSpacePosition.Y;
world.M43 = cameraSpacePosition.Z;


But i scale/normalize each vertex (the entire Planet) in the Shader like ( with *6000):

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{

float3 worldPosition = mul(input.Position, World).xyz;

float3 normalized = normalize(worldPosition);
float4 scaled = float4(worldPosition * 6000, 1);

output.Position = mul(scaled, ViewProj);

output.Position.z = log(C*output.Position.z + 1) / log(C*Far + 1) * output.Position.w; //Logarithmic Depth Buffer

return output;
}


The Scale and Normalize don't work anymore...

anybody out there who have a Solution for this Problem?

Edited by montify, 19 April 2014 - 10:41 AM.

### #11montify  Members

590
Like
0Likes
Like

Posted 21 April 2014 - 11:14 AM

Update:

Now i do the Relative to the Eye transformation in the VertexShader

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{

float3 worldPosition = mul(input.Position, World).xyz;

float3 normalized = normalize(worldPosition);  // Normalize all Vertex to get a Sphere

float4 scaled = float4(normalized * SeaLevel, 1);  // Scale all Vertex with SeaLevel = 6000

scaled -= float4(camPosition, 1); //All geometry relative to the Eye

output.Position = mul(scaled, ViewProj);
output.Position.z = log(C*output.Position.z + 1) / log(C*Far + 1) * output.Position.w; //Logarithmic Depth Buffer
return output;
}


It works, no Jittering, but i have some odd  "stairs"

So whats going on here?

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.