<p>Yes, ray differentials are the way to go. The <a href="https://graphics.stanford.edu/papers/trd/trd_jpg.pdf" rel="nofollow noreferrer">Paper by Igehy</a> introduces them for the use case of filtered texture lookups. When generating the primary rays, you initialise the differentials to reflect the pixel footprint. As the ray progresses through the scene, you update the differentials at every bounce. When it comes to a texture lookup, you need to project the differentials to your s,t texture space (aka UV), typically by going to surface parameter space as an intermediate.</p>

<p>The case that you mention, the calculation of derivatives for a ray surface intersection, is implemented in <a href="https://github.com/mmp/pbrt-v3/blob/eafd87d4ea3d6beb42f5e31a400a2158c4bee0f8/src/core/interaction.cpp#L103" rel="nofollow noreferrer">pbrt</a>:
Here the surface is described by a point p, normal n and two tangent vectors dPdu and dPdv. The resulting dudx, dudy, dvdx and dvdy are the differentials of the u, v parameters of the surface with respect to screen space x and y. Conversion from surface parameters to texture space happens <a href="https://github.com/mmp/pbrt-v3/blob/7095acfd8331cdefb03fe0bcae32c2fc9bd95980/src/core/texture.cpp#L93" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 3.0
2017-12-13T09:35:29.633Z
An isolated implementation, outside of a full renderer, was posted here: https://groups.google.com/forum/?hl=en#!topic/comp.graphics.algorithms/e11sjI-nzpM
Stefan Werner
CC BY-SA 3.0
2017-12-13T09:31:40.473Z
2017-12-13T09:31:40.473Z
Stefan Werner
<p>I'm doing some ray tracing on GPU using a fragment shader...</p>

<p>How can I calculate texture coordinates partial derivatives with respect to screen coordinates so I could perform filtered texture lookup with <code>textureGrad</code> GLSL function?</p>

<p>Do I need to use ray differentials?</p>

<p>If yes, then how?</p>

<p>Example explaining derivatives calculation for ray-plane intersection (ray described using origin and direction normal vector, plane using one point laying on a plane and normal vector) would be nice, although pointers for general case would also be appreciated...</p>

CC BY-SA 3.0
2017-12-02T15:59:41.820Z
I'm not actually sure what the usual way to do this is, but if you could somehow know the size of a pixel at this intersection, you could add epsilons to the hit position to get numeric derivatives and use that pixel size to know what mip level to use.
Alan Wolfe
CC BY-SA 3.0
2017-12-02T16:01:10.993Z
This question is related and has some good info in the answers: https://stackoverflow.com/q/1813303/2817105
Alan Wolfe
<raytracing><texture><glsl><fragment-shader><filtering>
Ray tracing - partial derivatives for texture lookup
zduny
# Ray tracing - partial derivatives for texture lookup

(Asked by: username_0 on 2017-11-30)

I'm doing some ray tracing on GPU using a fragment shader...

How can I calculate texture coordinates partial derivatives with respect to screen coordinates so I could perform filtered texture lookup with `textureGrad` GLSL function?

Do I need to use ray differentials?

If yes, then how?

Example explaining derivatives calculation for ray-plane intersection (ray described using origin and direction normal vector, plane using one point laying on a plane and normal vector) would be nice, although pointers for general case would also be appreciated...

**Tags:** raytracing, texture, glsl, fragment-shader, filtering

> **Comments:**
> **username_1**: I'm not actually sure what the usual way to do this is, but if you could somehow know the size of a pixel at this intersection, you could add epsilons to the hit position to get numeric derivatives and use that pixel size to know what mip level to use.  
> **username_1**: This question is related and has some good info in the answers: https://stackoverflow.com/q/1813303/2817105  

---

## 1 answers

### Answer by username_2 on 2017-12-13. Score: 4 | Accepted answer

Yes, ray differentials are the way to go. The [Paper by Igehy](https://graphics.stanford.edu/papers/trd/trd_jpg.pdf) introduces them for the use case of filtered texture lookups. When generating the primary rays, you initialise the differentials to reflect the pixel footprint. As the ray progresses through the scene, you update the differentials at every bounce. When it comes to a texture lookup, you need to project the differentials to your s,t texture space (aka UV), typically by going to surface parameter space as an intermediate.

The case that you mention, the calculation of derivatives for a ray surface intersection, is implemented in [pbrt](https://github.com/mmp/pbrt-v3/blob/eafd87d4ea3d6beb42f5e31a400a2158c4bee0f8/src/core/interaction.cpp#L103):
Here the surface is described by a point p, normal n and two tangent vectors dPdu and dPdv. The resulting dudx, dudy, dvdx and dvdy are the differentials of the u, v parameters of the surface with respect to screen space x and y. Conversion from surface parameters to texture space happens [here](https://github.com/mmp/pbrt-v3/blob/7095acfd8331cdefb03fe0bcae32c2fc9bd95980/src/core/texture.cpp#L93).

> **Comments:**
> **username_2**: An isolated implementation, outside of a full renderer, was posted here: https://groups.google.com/forum/?hl=en#!topic/comp.graphics.algorithms/e11sjI-nzpM  

---

