<p>Problem was that perspective matrix had very short Z_NEAR and Z_FAR values (0.01f and 200.0f). By changing it to much higher range (10.0f, 20000.0f), problem with jaggies and artifacts disappears.</p>

CC BY-SA 4.0
2020-09-18T06:46:45.557Z
You could generate rays without using a near/far plane so you avoid introducing precision loss
PaulHK
CC BY-SA 4.0
2020-09-18T07:20:04.153Z
@PaulHK I'm using inverse viewProj matrix to transform ray from ndc to world space. What is other way of generating ray direction?
DirectX_Programmer
CC BY-SA 4.0
2020-09-18T07:37:23.923Z
You could interpolate between the 4 far corner positions of your view frustum in screen space (keep the distance near 1, we only care about direction and not length) and generate per-pixel ray directions from that. The ray starts at the eye position.
PaulHK
CC BY-SA 4.0
2020-09-18T07:41:02.643Z
Your current solution of modifying near/far to be close in magnitude is also valid, ray generation shouldn't need to concern itself with far/near planes
PaulHK
CC BY-SA 4.0
2020-09-18T11:57:15.010Z
Yeah, it worked for me and for now I don't need to change it and I've seen that invViewProj matrix is used in many code samples. I don't know how it looks in industry standard AAA engines, but I'm grateful for an insight of other method of casting rays.
DirectX_Programmer
CC BY-SA 4.0
2020-09-16T07:42:54.283Z
2020-09-16T07:42:54.283Z
DirectX_Programmer
<p>I am using RT shader (DXR) to generate g-buffers with code below. There was no problems with smaller scene (pink room from cwyman tutorial - <a href="http://cwyman.org/code/dxrTutors/tutors/Tutor5/tutorial05.md.html" rel="nofollow noreferrer">http://cwyman.org/code/dxrTutors/tutors/Tutor5/tutorial05.md.html</a> ). However, when I moved to bigger scene (Sun Temple by Epic Games - <a href="https://developer.nvidia.com/ue4-sun-temple" rel="nofollow noreferrer">https://developer.nvidia.com/ue4-sun-temple</a> ), I've started noticing problems with texture sampling. Please compare textures at the end of this post. The greater the distance to world origin (0, 0, 0), the more jaggy artifacts appear and quality becomes worse.</p>
<p>At the beginning, I was suspecting acceleration structures. But skybox quality is becoming worse, so it's probably unrelated to AS.</p>
<pre><code>inline void GenerateCameraRay(uint2 index, uint2 dimensions, float4x4 projectionToWorld, inout  float3 origin, out float3 direction)
{
    float2 xy = index + float2(0.5f, 0.5f); // center in the middle of the pixel.
    float2 screenPos = (xy / (float2) dimensions) * 2.0 - 1.0;

    // Invert Y for DirectX-style coordinates.
    screenPos.y = -screenPos.y;

    // Unproject the pixel coordinate into a ray.
    float4 world = mul(float4(screenPos, 0, 1), projectionToWorld);

    world.xyz /= world.w;
    direction = normalize(world.xyz - origin);
}

[shader(&quot;raygeneration&quot;)]
void RayGen()
{
    float3 rayDir;
    float3 origin = g_sceneCB.cameraPosition.xyz;
    
    // Generate a ray for a camera pixel corresponding to an index from the dispatched 2D grid.
    GenerateCameraRay(DispatchRaysIndex().xy, DispatchRaysDimensions().xy, g_sceneCB.projectionToWorld, origin, rayDir);

    RayDesc ray = { origin, 1e-4f, rayDir, 1e+38f };
    RayPayload payload = { 0 };
    TraceRay(SceneBVH, RAY_FLAG_NONE, 0xFF, 0, 1, 0, ray, payload);
}
    
[shader(&quot;miss&quot;)]
void Miss(inout RayPayload payload)
{
    float3 rayDir = WorldRayDirection();
    rayDir.z = -rayDir.z;

    RTOutputAlbedo[DispatchRaysIndex().xy] = float4(skyboxTexture.SampleLevel(g_sampler, rayDir, 0).rgb, 0);
    RTOutputNormal[DispatchRaysIndex().xy] = float4(0, 0, 0, 0);
    RTOutputSpecRoughness[DispatchRaysIndex().xy] = float4(0, 0, 0, 0);
}
</code></pre>
<p><a href="https://i.sstatic.net/fIwbx.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/fIwbx.jpg" alt="enter image description here" /></a>
<a href="https://i.sstatic.net/7l9wO.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/7l9wO.png" alt="enter image description here" /></a></p>

<raytracing><texture><hlsl><directx12>
Worsening sampling quality of raytracing with increasing distance from world origin
DirectX_Programmer
# Worsening sampling quality of raytracing with increasing distance from world origin

(Asked by: username_0 on 2020-09-13)

I am using RT shader (DXR) to generate g-buffers with code below. There was no problems with smaller scene (pink room from cwyman tutorial - <http://cwyman.org/code/dxrTutors/tutors/Tutor5/tutorial05.md.html> ). However, when I moved to bigger scene (Sun Temple by Epic Games - <https://developer.nvidia.com/ue4-sun-temple> ), I've started noticing problems with texture sampling. Please compare textures at the end of this post. The greater the distance to world origin (0, 0, 0), the more jaggy artifacts appear and quality becomes worse.

At the beginning, I was suspecting acceleration structures. But skybox quality is becoming worse, so it's probably unrelated to AS.

```
inline void GenerateCameraRay(uint2 index, uint2 dimensions, float4x4 projectionToWorld, inout  float3 origin, out float3 direction)
{
    float2 xy = index + float2(0.5f, 0.5f); // center in the middle of the pixel.
    float2 screenPos = (xy / (float2) dimensions) * 2.0 - 1.0;

    // Invert Y for DirectX-style coordinates.
    screenPos.y = -screenPos.y;

    // Unproject the pixel coordinate into a ray.
    float4 world = mul(float4(screenPos, 0, 1), projectionToWorld);

    world.xyz /= world.w;
    direction = normalize(world.xyz - origin);
}

[shader("raygeneration")]
void RayGen()
{
    float3 rayDir;
    float3 origin = g_sceneCB.cameraPosition.xyz;
    
    // Generate a ray for a camera pixel corresponding to an index from the dispatched 2D grid.
    GenerateCameraRay(DispatchRaysIndex().xy, DispatchRaysDimensions().xy, g_sceneCB.projectionToWorld, origin, rayDir);

    RayDesc ray = { origin, 1e-4f, rayDir, 1e+38f };
    RayPayload payload = { 0 };
    TraceRay(SceneBVH, RAY_FLAG_NONE, 0xFF, 0, 1, 0, ray, payload);
}
    
[shader("miss")]
void Miss(inout RayPayload payload)
{
    float3 rayDir = WorldRayDirection();
    rayDir.z = -rayDir.z;

    RTOutputAlbedo[DispatchRaysIndex().xy] = float4(skyboxTexture.SampleLevel(g_sampler, rayDir, 0).rgb, 0);
    RTOutputNormal[DispatchRaysIndex().xy] = float4(0, 0, 0, 0);
    RTOutputSpecRoughness[DispatchRaysIndex().xy] = float4(0, 0, 0, 0);
}

```

[![enter image description here](https://i.sstatic.net/fIwbx.jpg)](https://i.sstatic.net/fIwbx.jpg)
[![enter image description here](https://i.sstatic.net/7l9wO.png)](https://i.sstatic.net/7l9wO.png)

**Tags:** raytracing, texture, hlsl, directx12


---

## 1 answers

### Answer by username_0 on 2020-09-16. Score: 1 | Accepted answer

Problem was that perspective matrix had very short Z\_NEAR and Z\_FAR values (0.01f and 200.0f). By changing it to much higher range (10.0f, 20000.0f), problem with jaggies and artifacts disappears.

> **Comments:**
> **username_1**: You could generate rays without using a near/far plane so you avoid introducing precision loss  
> **username_0**: @username_1 I'm using inverse viewProj matrix to transform ray from ndc to world space. What is other way of generating ray direction?  
> **username_1**: You could interpolate between the 4 far corner positions of your view frustum in screen space (keep the distance near 1, we only care about direction and not length) and generate per-pixel ray directions from that. The ray starts at the eye position.  
> **username_1**: Your current solution of modifying near/far to be close in magnitude is also valid, ray generation shouldn't need to concern itself with far/near planes  
> **username_0**: Yeah, it worked for me and for now I don't need to change it and I've seen that invViewProj matrix is used in many code samples. I don't know how it looks in industry standard AAA engines, but I'm grateful for an insight of other method of casting rays.  

---

