Post · 62 min read· pp. 305–322

Texture Sampling and Antialiasing

10.1 Texture Sampling and Antialiasing

The sampling task from Chapter 8 was a frustrating one since the aliasing problem was known to be unsolvable from the start. The infinite frequency content of geometric edges and hard shadows guarantees aliasing in the final images, no matter how high the image sampling rate. (Our only consolation is that the visual impact of this remaining aliasing can be reduced to unobjectionable levels with a sufficient number of well-placed samples.)

Fortunately, things are not this difficult from the start for textures: either there is often a convenient analytic form of the texture function available, which makes it possible to remove excessively high frequencies before sampling it, or it is possible to be careful when evaluating the function so as not to introduce high frequencies in the first place. When this problem is carefully addressed in texture implementations, as is done through the rest of this chapter, there is usually no need for more than one sample per pixel in order to render an image without texture aliasing. (Of course, sufficiently reducing Monte Carlo noise from lighting calculations may be another matter.)

Two problems must be addressed in order to remove aliasing from texture functions:

  1. The sampling rate in texture space must be computed. The screen-space sampling rate is known from the image resolution and pixel sampling rate, but here we need to determine the resulting sampling rate on a surface in the scene in order to find the rate at which the texture function is being sampled.
  2. Given the texture sampling rate, sampling theory must be applied to guide the computation of a texture value that does not have higher frequency variation than can be represented by the sampling rate (e.g., by removing excess frequencies beyond the Nyquist limit from the texture function).

These two issues will be addressed in turn throughout the rest of this section.

10.1.1 Finding the Texture Sampling Rate

Consider an arbitrary texture function that is a function of position, diagram, defined on a surface in the scene. If we ignore the complications introduced by visibility—the possibility that another object may occlude the surface at nearby image samples or that the surface may have a limited extent on the image plane—this texture function can also be expressed as a function over points diagram on the image plane, diagram, where diagram is the function that maps image points to points on the surface. Thus, diagram gives the value of the texture function as seen at image position diagram.

As a simple example of this idea, consider a 2D texture function diagram applied to a quadrilateral that is perpendicular to the diagram axis and has corners at the world-space points diagram, diagram, diagram, and diagram. If an orthographic camera is placed looking down the diagram axis such that the quadrilateral precisely fills the image plane and if points diagram on the quadrilateral are mapped to 2D diagram texture coordinates by

diagram

then the relationship between diagram and screen diagram pixels is straightforward:

diagram

where the overall image resolution is diagram (Figure 10.2). Thus, given a sample spacing of one pixel in the image plane, the sample spacing in diagram texture parameter space is diagram, and the texture function must remove any detail at a higher frequency than can be represented at that sampling rate.

Figure 10.2: If a quadrilateral is viewed with an orthographic perspective such that the quadrilateral precisely fills the image plane, it is easy to compute the relationship between the sampling rate in diagram pixel coordinates and the texture sampling rate.

This relationship between pixel coordinates and texture coordinates, and thus the relationship between their sampling rates, is the key bit of information that determines the maximum frequency content allowable in the texture function. As a slightly more complex example, given a triangle with diagram texture coordinates at its vertices and viewed with a perspective projection, it is possible to analytically find the differences in diagram and diagram across the sample points on the image plane. This approach was the basis of texture antialiasing in graphics processors before they became programmable.

For more complex scene geometry, camera projections, and mappings to texture coordinates, it is much more difficult to precisely determine the relationship between image positions and texture parameter values. Fortunately, for texture antialiasing, we do not need to be able to evaluate diagram for arbitrary diagram but just need to find the relationship between changes in pixel sample position and the resulting change in texture sample position at a particular point on the image. This relationship is given by the partial derivatives of this function, diagram and diagram. For example, these can be used to find a first-order approximation to the value of diagram,

diagram

If these partial derivatives are changing slowly with respect to the distances diagram and diagram, this is a reasonable approximation. More importantly, the values of these partial derivatives give an approximation to the change in texture sample position for a shift of one pixel in the diagram and diagram directions, respectively, and thus directly yield the texture sampling rate. For example, in the previous quadrilateral example, diagram, diagram, diagram, and diagram.

The key to finding the values of these derivatives in the general case lies in values from the RayDifferential structure, which was defined in Section 3.6.1. This structure is initialized for each camera ray by the Camera::GenerateRayDifferential() method; it contains not only the ray being traced through the scene but also two additional rays, one offset horizontally one pixel sample from the camera ray and the other offset vertically by one pixel sample. All the geometric ray intersection routines use only the main camera ray for their computations; the auxiliary rays are ignored (this is easy to do because RayDifferential is a subclass of Ray).

We can use the offset rays to estimate the partial derivatives of the mapping pdiagram from image position to world-space position and the partial derivatives of the mappings diagram and diagram from diagram to diagram parametric coordinates, giving the partial derivatives of rendering-space positions diagram and diagram and the derivatives of diagram parametric coordinates diagram, diagram, diagram, and diagram. In Section 10.2, we will see how these can be used to compute the screen-space derivatives of arbitrary quantities based on diagram or diagram and consequently the sampling rates of these quantities. The values of these derivatives at the intersection point are stored in the SurfaceInteraction structure.

<>+=

Vector3f dpdx, dpdy; Float dudx = 0, dvdx = 0, dudy = 0, dvdy = 0;

The SurfaceInteraction::ComputeDifferentials() method computes these values. It is called by SurfaceInteraction::GetBSDF() before the Material’s GetBxDF() method is called so that these values will be available for any texture evaluation routines that are called by the material.

Ray differentials are not available for all rays traced by the system—for example, rays starting from light sources traced for photon mapping or bidirectional path tracing. Further, although we will see how to compute ray differentials after rays undergo specular reflection and transmission in Section 10.1.3, how to compute ray differentials after diffuse reflection is less clear. In cases like those as well as the corner case where one of the differentials’ directions is perpendicular to the surface normal, which leads to undefined numerical values in the following, an alternative approach based on approximating the ray differentials of a ray from the camera to the intersection point is used.

<>=

void SurfaceInteraction::ComputeDifferentials(const RayDifferential &ray, Camera camera, int samplesPerPixel) { if (ray.hasDifferentials && Dot(n, ray.rxDirection) != 0 && Dot(n, ray.ryDirection) != 0) { <<Estimate screen-space change in diagram using ray differentials>> 

<<Compute auxiliary intersection points with plane, px and py>> 

Float d = -Dot(n, Vector3f(p())); Float tx = (-Dot(n, Vector3f(ray.rxOrigin)) - d) / Dot(n, ray.rxDirection); Point3f px = ray.rxOrigin + tx * ray.rxDirection; Float ty = (-Dot(n, Vector3f(ray.ryOrigin)) - d) / Dot(n, ray.ryDirection); Point3f py = ray.ryOrigin + ty * ray.ryDirection;

dpdx = px - p(); dpdy = py - p();

} else { <<Approximate screen-space change in diagram based on camera projection>> 

camera.Approximate_dp_dxy(p(), n, time, samplesPerPixel, &dpdx, &dpdy);

} <<Estimate screen-space change in diagram>> 

<<Compute diagram and its determinant>> 

Float ata00 = Dot(dpdu, dpdu), ata01 = Dot(dpdu, dpdv); Float ata11 = Dot(dpdv, dpdv); Float invDet = 1 / DifferenceOfProducts(ata00, ata11, ata01, ata01); invDet = IsFinite(invDet) ? invDet : 0.f;

<<Compute diagram for diagram and diagram>> 

Float atb0x = Dot(dpdu, dpdx), atb1x = Dot(dpdv, dpdx); Float atb0y = Dot(dpdu, dpdy), atb1y = Dot(dpdv, dpdy);

<<Compute diagram and diagram derivatives with respect to diagram and diagram>> 

dudx = DifferenceOfProducts(ata11, atb0x, ata01, atb1x) * invDet; dvdx = DifferenceOfProducts(ata00, atb1x, ata01, atb0x) * invDet; dudy = DifferenceOfProducts(ata11, atb0y, ata01, atb1y) * invDet; dvdy = DifferenceOfProducts(ata00, atb1y, ata01, atb0y) * invDet;

<<Clamp derivatives of diagram and diagram to reasonable values>> 

dudx = IsFinite(dudx) ? Clamp(dudx, -1e8f, 1e8f) : 0.f; dvdx = IsFinite(dvdx) ? Clamp(dvdx, -1e8f, 1e8f) : 0.f; dudy = IsFinite(dudy) ? Clamp(dudy, -1e8f, 1e8f) : 0.f; dvdy = IsFinite(dvdy) ? Clamp(dvdy, -1e8f, 1e8f) : 0.f;

}

The key to estimating the derivatives is the assumption that the surface is locally flat with respect to the sampling rate at the point being shaded. This is a reasonable approximation in practice, and it is hard to do much better. Because ray tracing is a point-sampling technique, we have no additional information about the scene in between the rays we have traced. For highly curved surfaces or at silhouette edges, this approximation can break down, though this is rarely a source of noticeable error.

For this approximation, we need the plane through the point diagram intersected by the main ray that is tangent to the surface. This plane is given by the implicit equation

diagram

where diagram, diagram, diagram, and diagram. We can then compute the intersection points diagram and diagram between the auxiliary rays diagram and diagram and this plane (Figure 10.3). These new points give an approximation to the partial derivatives of position on the surface diagram and diagram, based on forward differences:

diagram

Figure 10.3: By approximating the local surface geometry at the intersection point with the tangent plane through diagram, approximations to the points at which the auxiliary rays diagram and diagram would intersect the surface can be found by finding their intersection points with the tangent plane diagram and diagram.

Because the differential rays are offset one pixel sample in each direction, there is no need to divide these differences by a diagram value, since diagram.

<<Estimate screen-space change in diagram using ray differentials>>=

<<Compute auxiliary intersection points with plane, px and py>> 

Float d = -Dot(n, Vector3f(p())); Float tx = (-Dot(n, Vector3f(ray.rxOrigin)) - d) / Dot(n, ray.rxDirection); Point3f px = ray.rxOrigin + tx * ray.rxDirection; Float ty = (-Dot(n, Vector3f(ray.ryOrigin)) - d) / Dot(n, ray.ryDirection); Point3f py = ray.ryOrigin + ty * ray.ryDirection;

dpdx = px - p(); dpdy = py - p();

The ray–plane intersection algorithm described in Section 6.1.2 gives the diagram value where a ray described by origin diagram and direction diagram intersects a plane described by diagram:

diagram

To compute this value for the two auxiliary rays, the plane’s diagram coefficient is computed first. It is not necessary to compute the diagram, diagram, and diagram coefficients, since they are available in n. We can then apply the formula directly.

<<Compute auxiliary intersection points with plane, px and py>>=

Float d = -Dot(n, Vector3f(p())); Float tx = (-Dot(n, Vector3f(ray.rxOrigin)) - d) / Dot(n, ray.rxDirection); Point3f px = ray.rxOrigin + tx * ray.rxDirection; Float ty = (-Dot(n, Vector3f(ray.ryOrigin)) - d) / Dot(n, ray.ryDirection); Point3f py = ray.ryOrigin + ty * ray.ryDirection;

For cases where ray differentials are not available, we will add a method to the Camera interface that returns approximate values for diagram and diagram at a point on a surface in the scene. These should be a reasonable approximation to the differentials of a ray from the camera that found an intersection at the given point. Cameras’ implementations of this method must return reasonable results even for points outside of their viewing volumes for which they cannot actually generate rays.

<>+=

void Approximate_dp_dxy(Point3f p, Normal3f n, Float time, int samplesPerPixel, Vector3f *dpdx, Vector3f *dpdy) const;

CameraBase provides an implementation of an approach to approximating these differentials that is based on the minimum of the camera ray differentials across the entire image. Because all of pbrt’s current camera implementations inherit from CameraBase, the following method takes care of all of them.

<>+=

void Approximate_dp_dxy(Point3f p, Normal3f n, Float time, int samplesPerPixel, Vector3f *dpdx, Vector3f *dpdy) const { <<Compute tangent plane equation for ray differential intersections>> 

Point3f pCamera = CameraFromRender(p, time); Transform DownZFromCamera = RotateFromTo(Normalize(Vector3f(pCamera)), Vector3f(0, 0, 1)); Point3f pDownZ = DownZFromCamera(pCamera); Normal3f nDownZ = DownZFromCamera(CameraFromRender(n, time)); Float d = nDownZ.z * pDownZ.z;

<<Find intersection points for approximated camera differential rays>> 

Ray xRay(Point3f(0,0,0) + minPosDifferentialX, Vector3f(0,0,1) + minDirDifferentialX); Float tx = -(Dot(nDownZ, Vector3f(xRay.o)) - d) / Dot(nDownZ, xRay.d); Ray yRay(Point3f(0,0,0) + minPosDifferentialY, Vector3f(0,0,1) + minDirDifferentialY); Float ty = -(Dot(nDownZ, Vector3f(yRay.o)) - d) / Dot(nDownZ, yRay.d); Point3f px = xRay(tx), py = yRay(ty);

<<Estimate diagram and diagram in tangent plane at intersection point>> 

Float sppScale = GetOptions().disablePixelJitter ? 1 : std::max(.125, 1 / std::sqrt((Float)samplesPerPixel)); *dpdx = sppScale * RenderFromCamera(DownZFromCamera.ApplyInverse(px - pDownZ), time); *dpdy = sppScale * RenderFromCamera(DownZFromCamera.ApplyInverse(py - pDownZ), time);

}

This method starts by orienting the camera so that the camera-space diagram axis is aligned with the vector from the camera position to the intersection point. It then uses lower bounds on the spread of rays over the image that are provided by the camera to find approximate differential rays. It then intersects these rays with the tangent plane at the intersection point. (See Figure 10.4.)

Figure 10.4: CameraBase::Approximate_dp_dxy() effectively reorients the camera to point at the provided intersection point. In camera space, the ray to the intersection then has origin diagram and direction diagram. The extent of ray differentials on the tangent plane defined by the surface normal at the intersection point can then be found.

Figure 10.5: Visualization of the Ratio of Filter Areas Estimated by Regular Ray Differentials to Areas Estimated by CameraBase::Approximate_dp_dxy(). We represent the filter area as the product diagram and visualize the base 2 logarithm of the ratio of areas computed by the two techniques. Log 2 ratios greater than 0 indicate that the camera-based approximation estimated a larger filter area.

There are a number of sources of error in this approximation. Beyond the fact that it does not account for how light was scattered at intermediate surfaces for multiple-bounce ray paths, there is also the fact that it is based on the minimum of the camera’s differentials for all rays. In general, it tries to underestimate those derivatives rather than overestimate them, as we prefer aliasing over blurring here. The former error can at least be addressed with additional pixel samples. In order to give a sense of the impact of some of these approximations, Figure 10.5 has visualization that compares the local area estimated by those derivatives at intersections to the area computed using the actual ray differentials generated by the camera.

For the first step of the algorithm, we have an intersection point in rendering space p that we would like to transform into a coordinate system where it is along the diagram axis with the camera at the origin. Transforming to camera space gets us started and an additional rotation that transforms the vector from the origin to the intersection point to be aligned with diagram finishes the job. The diagram coefficient of the plane equation can then be found by taking the dot product of the transformed point and surface normal. Because the diagram and diagram components of the transformed point are equal to 0, the dot product can be optimized to be a single multiply.

<>=

Point3f pCamera = CameraFromRender(p, time); Transform DownZFromCamera = RotateFromTo(Normalize(Vector3f(pCamera)), Vector3f(0, 0, 1)); Point3f pDownZ = DownZFromCamera(pCamera); Normal3f nDownZ = DownZFromCamera(CameraFromRender(n, time)); Float d = nDownZ.z * pDownZ.z;

Camera implementations that inherit from CameraBase and use this method must initialize the following member variables with values that are lower bounds on each of the respective position and direction differentials over all the pixels in the image.

<>+=

Vector3f minPosDifferentialX, minPosDifferentialY; Vector3f minDirDifferentialX, minDirDifferentialY;

The main ray in this coordinate system has origin diagram and direction diagram. Adding the position and direction differential vectors to those gives the origin and direction of each differential ray. Given those, the same calculation as earlier gives us the diagram values for the ray–plane intersections for the differential rays and thence the intersection points.

<>=

Ray xRay(Point3f(0,0,0) + minPosDifferentialX, Vector3f(0,0,1) + minDirDifferentialX); Float tx = -(Dot(nDownZ, Vector3f(xRay.o)) - d) / Dot(nDownZ, xRay.d); Ray yRay(Point3f(0,0,0) + minPosDifferentialY, Vector3f(0,0,1) + minDirDifferentialY); Float ty = -(Dot(nDownZ, Vector3f(yRay.o)) - d) / Dot(nDownZ, yRay.d); Point3f px = xRay(tx), py = yRay(ty);

For an orthographic camera, these differentials can be computed directly. There is no change in the direction vector, and the position differentials are the same at every pixel. Their values are already computed in the OrthographicCamera constructor, so can be used directly to initialize the base class’s member variables.

<>=

minDirDifferentialX = minDirDifferentialY = Vector3f(0, 0, 0); minPosDifferentialX = dxCamera; minPosDifferentialY = dyCamera;

All the other cameras call FindMinimumDifferentials(), which estimates these values by sampling at many points across the diagonal of the image and storing the minimum of all the differentials encountered. That function is not very interesting, so it is not included here.

<>=

FindMinimumDifferentials(this);

Given the intersection points px and py, diagram and diagram can now be estimated by taking their differences with the main intersection point. To get final estimates of the partial derivatives, these vectors must be transformed back out into rendering space and scaled to account for the actual pixel sampling rate. As with the initial ray differentials that were generated in the <<Scale camera ray differentials based on image sampling rate>> fragment, these are scaled to account for the pixel sampling rate.

<<Estimate diagram and diagram in tangent plane at intersection point>>=

Float sppScale = GetOptions().disablePixelJitter ? 1 : std::max(.125, 1 / std::sqrt((Float)samplesPerPixel)); *dpdx = sppScale * RenderFromCamera(DownZFromCamera.ApplyInverse(px - pDownZ), time); *dpdy = sppScale * RenderFromCamera(DownZFromCamera.ApplyInverse(py - pDownZ), time);

A call to this method takes care of computing the diagram and diagram differentials in the ComputeDifferentials() method.

<<Approximate screen-space change in diagram based on camera projection>>=

camera.Approximate_dp_dxy(p(), n, time, samplesPerPixel, &dpdx, &dpdy);

We now have both the partial derivatives diagram and diagram as well as, one way or another, diagram and diagram. From them, we would now like to compute diagram, diagram, diagram, and diagram. Using the chain rule, we can find that

diagram

(10.1)

(diagram has a similar expression with diagram replaced by diagram and diagram replaced by diagram.)

Equation (10.1) can be written as a matrix equation where the two following matrices that include diagram have three rows, one for each of diagram’s diagram, diagram, and diagram components:

diagram

This is an overdetermined linear system since there are three equations but only two unknowns, diagram and diagram. An effective solution approach in this case is to apply linear least squares, which says that for a linear system of the form diagram with diagram and diagram known, the least-squares solution for diagram is given by

diagram

(10.2)

In this case, diagram, diagram, and diagram.

<<Estimate screen-space change in diagram>>=

<<Compute diagram and its determinant>> 

Float ata00 = Dot(dpdu, dpdu), ata01 = Dot(dpdu, dpdv); Float ata11 = Dot(dpdv, dpdv); Float invDet = 1 / DifferenceOfProducts(ata00, ata11, ata01, ata01); invDet = IsFinite(invDet) ? invDet : 0.f;

<<Compute diagram for diagram and diagram>> 

Float atb0x = Dot(dpdu, dpdx), atb1x = Dot(dpdv, dpdx); Float atb0y = Dot(dpdu, dpdy), atb1y = Dot(dpdv, dpdy);

<<Compute diagram and diagram derivatives with respect to diagram and diagram>> 

dudx = DifferenceOfProducts(ata11, atb0x, ata01, atb1x) * invDet; dvdx = DifferenceOfProducts(ata00, atb1x, ata01, atb0x) * invDet; dudy = DifferenceOfProducts(ata11, atb0y, ata01, atb1y) * invDet; dvdy = DifferenceOfProducts(ata00, atb1y, ata01, atb0y) * invDet;

<<Clamp derivatives of diagram and diagram to reasonable values>> 

dudx = IsFinite(dudx) ? Clamp(dudx, -1e8f, 1e8f) : 0.f; dvdx = IsFinite(dvdx) ? Clamp(dvdx, -1e8f, 1e8f) : 0.f; dudy = IsFinite(dudy) ? Clamp(dudy, -1e8f, 1e8f) : 0.f; dvdy = IsFinite(dvdy) ? Clamp(dvdy, -1e8f, 1e8f) : 0.f;

diagram is a diagram matrix with elements given by dot products of partial derivatives of position:

diagram

Its inverse is

diagram

(10.3)

Note that in both matrices the two off-diagonal entries are equal. Thus, the fragment that computes the entries of diagram only needs to compute three values. The inverse of the matrix determinant is computed here as well. If its value is infinite, the linear system cannot be solved; setting invDet to 0 causes the subsequently computed derivatives to be 0, which leads to point-sampled textures, the best remaining option in that case.

<<Compute diagram and its determinant>>=

Float ata00 = Dot(dpdu, dpdu), ata01 = Dot(dpdu, dpdv); Float ata11 = Dot(dpdv, dpdv); Float invDet = 1 / DifferenceOfProducts(ata00, ata11, ata01, ata01); invDet = IsFinite(invDet) ? invDet : 0.f;

The diagram portion of the solution is easily computed. For the derivatives with respect to screen-space diagram, we have the two-element matrix

diagram

(10.4)

The solution for screen-space diagram is analogous.

<<Compute diagram for diagram and diagram>>=

Float atb0x = Dot(dpdu, dpdx), atb1x = Dot(dpdv, dpdx); Float atb0y = Dot(dpdu, dpdy), atb1y = Dot(dpdv, dpdy);

The solution to Equation (10.2) for each partial derivative can be found by taking the product of Equations (10.3) and (10.4). We will gloss past the algebra; its result can be directly expressed in terms of the values computed so far.

<<Compute diagram and diagram derivatives with respect to diagram and diagram>>=

dudx = DifferenceOfProducts(ata11, atb0x, ata01, atb1x) * invDet; dvdx = DifferenceOfProducts(ata00, atb1x, ata01, atb0x) * invDet; dudy = DifferenceOfProducts(ata11, atb0y, ata01, atb1y) * invDet; dvdy = DifferenceOfProducts(ata00, atb1y, ata01, atb0y) * invDet;

In certain tricky cases (e.g., with highly distorted diagram parameterizations or at object silhouette edges), the estimated partial derivatives may be infinite or have very large magnitudes. It is worth clamping them to reasonable values in that case to prevent overflow and not-a-number values in subsequent computations that are based on them.

<<Clamp derivatives of diagram and diagram to reasonable values>>=

dudx = IsFinite(dudx) ? Clamp(dudx, -1e8f, 1e8f) : 0.f; dvdx = IsFinite(dvdx) ? Clamp(dvdx, -1e8f, 1e8f) : 0.f; dudy = IsFinite(dudy) ? Clamp(dudy, -1e8f, 1e8f) : 0.f; dvdy = IsFinite(dvdy) ? Clamp(dvdy, -1e8f, 1e8f) : 0.f;

10.1.2 Ray Differentials at Medium Transitions

Now is a good time to take care of another detail related to ray differentials: recall from Section 9.1.5 that materials may return an unset BSDF to indicate an interface between two scattering media that does not itself scatter light. In this case, it is necessary to spawn a new ray in the same direction, but past the intersection on the surface. In this case we would like the effect of the ray differentials to be the same as if no scattering had occurred. This can be achieved by setting the differential origins to the points given by evaluating the ray equation at the intersection diagram (see Figure 10.6).

Figure 10.6: When a ray intersects a surface that delineates the boundary between two media, a new ray is spawned on the other side of the boundary. If the origins of this ray’s differentials are set by evaluating the ray equation for the original differentials at the intersection diagram, then the new ray will represent the same footprint as the original one when it subsequently intersects a surface.

<>+=

void SurfaceInteraction::SkipIntersection(RayDifferential *ray, Float t) const { *((Ray *)ray) = SpawnRay(ray->d); if (ray->hasDifferentials) { ray->rxOrigin = ray->rxOrigin + t * ray->rxDirection; ray->ryOrigin = ray->ryOrigin + t * ray->ryDirection; } }

10.1.3 Ray Differentials for Specular Reflection and Transmission

Given the effectiveness of ray differentials for finding filter regions for texture antialiasing for camera rays, it is useful to extend the method to make it possible to determine texture-space sampling rates for objects that are seen indirectly via specular reflection or refraction; objects seen in mirrors, for example, should not have texture aliasing, identical to the case for directly visible objects. Igehy (1999) developed an elegant solution to the problem of how to find the appropriate differential rays for specular reflection and refraction, which is the approach used in pbrt.

Figure 10.7 illustrates the difference that proper texture filtering for specular reflection and transmission can make: it shows a glass ball and a mirrored ball on a plane with a texture map containing high-frequency components. Ray differentials ensure that the images of the texture seen via reflection and refraction from the balls are free of aliasing artifacts. Here, ray differentials eliminate aliasing without excessively blurring the texture.

Figure 10.7: (a) Tracking ray differentials for reflected and refracted rays ensures that the image map texture seen in the balls is filtered to avoid aliasing. The left ball is glass, exhibiting reflection and refraction, and the right ball is a mirror, just showing reflection. Note that the texture is well filtered over both of the balls. (b) shows the aliasing artifacts that are present if ray differentials are not used.

Figure 10.8: The specular reflection formula gives the direction of the reflected ray at a point on a surface. An offset ray for a ray differential diagram (dashed line) will generally intersect the surface at a different point and be reflected in a different direction. The new direction is affected by the different surface normal at the point as well as by the offset ray’s different incident direction. The computation to find the reflected direction for the offset ray in pbrt estimates the change in reflected direction as a function of image-space position and approximates the ray differential’s direction with the main ray’s direction added to the estimated change in direction.

To compute the reflected or transmitted ray differentials at a surface intersection point, we need an approximation to the rays that would have been traced at the intersection points for the two offset rays in the ray differential that hit the surface (Figure 10.8). The new ray for the main ray is found by sampling the BSDF, so here we only need to compute the outgoing rays for the diagram and diagram differentials. This task is handled by another SurfaceInteraction::SpawnRay() variant that takes an incident ray differential as well as information about the BSDF and the type of scattering that occurred.

<>+=

RayDifferential SurfaceInteraction::SpawnRay( const RayDifferential &rayi, const BSDF &bsdf, Vector3f wi, int flags, Float eta) const { RayDifferential rd(SpawnRay(wi)); if (rayi.hasDifferentials) { <<Compute ray differentials for specular reflection or transmission>> 

<<Compute common factors for specular ray differentials>> 

Normal3f n = shading.n; Normal3f dndx = shading.dndu * dudx + shading.dndv * dvdx; Normal3f dndy = shading.dndu * dudy + shading.dndv * dvdy; Vector3f dwodx = -rayi.rxDirection - wo, dwody = -rayi.ryDirection - wo;

if (flags == BxDFFlags::SpecularReflection) { <<Initialize origins of specular differential rays>> 

rd.hasDifferentials = true; rd.rxOrigin = p() + dpdx; rd.ryOrigin = p() + dpdy;

<<Compute differential reflected directions>> 

Float dwoDotn_dx = Dot(dwodx, n) + Dot(wo, dndx); Float dwoDotn_dy = Dot(dwody, n) + Dot(wo, dndy); rd.rxDirection = wi - dwodx + 2 * Vector3f(Dot(wo, n) * dndx + dwoDotn_dx * n); rd.ryDirection = wi - dwody + 2 * Vector3f(Dot(wo, n) * dndy + dwoDotn_dy * n);

} else if (flags == BxDFFlags::SpecularTransmission) { <<Initialize origins of specular differential rays>> 

rd.hasDifferentials = true; rd.rxOrigin = p() + dpdx; rd.ryOrigin = p() + dpdy;

<<Compute differential transmitted directions>> 

<<Find oriented surface normal for transmission>> 

if (Dot(wo, n) < 0) { n = -n; dndx = -dndx; dndy = -dndy; }

<<Compute partial derivatives of diagram>> 

Float dwoDotn_dx = Dot(dwodx, n) + Dot(wo, dndx); Float dwoDotn_dy = Dot(dwody, n) + Dot(wo, dndy); Float mu = Dot(wo, n) / eta - AbsDot(wi, n); Float dmudx = dwoDotn_dx * (1/eta + 1/Sqr(eta) * Dot(wo, n) / Dot(wi, n)); Float dmudy = dwoDotn_dy * (1/eta + 1/Sqr(eta) * Dot(wo, n) / Dot(wi, n));

rd.rxDirection = wi - eta * dwodx + Vector3f(mu * dndx + dmudx * n); rd.ryDirection = wi - eta * dwody + Vector3f(mu * dndy + dmudy * n);

}

} <> 

if (LengthSquared(rd.rxDirection) > 1e16f || LengthSquared(rd.ryDirection) > 1e16f || LengthSquared(Vector3f(rd.rxOrigin)) > 1e16f || LengthSquared(Vector3f(rd.ryOrigin)) > 1e16f) rd.hasDifferentials = false;

return rd; }

It is not well defined what the ray differentials should be in the case of non-specular scattering. Therefore, this method handles the two types of specular scattering only; for all other types of rays, approximate differentials will be computed at their subsequent intersection points with Camera::Approximate_dp_dxy().

<>=

<<Compute common factors for specular ray differentials>> 

Normal3f n = shading.n; Normal3f dndx = shading.dndu * dudx + shading.dndv * dvdx; Normal3f dndy = shading.dndu * dudy + shading.dndv * dvdy; Vector3f dwodx = -rayi.rxDirection - wo, dwody = -rayi.ryDirection - wo;

if (flags == BxDFFlags::SpecularReflection) { <<Initialize origins of specular differential rays>> 

rd.hasDifferentials = true; rd.rxOrigin = p() + dpdx; rd.ryOrigin = p() + dpdy;

<<Compute differential reflected directions>> 

Float dwoDotn_dx = Dot(dwodx, n) + Dot(wo, dndx); Float dwoDotn_dy = Dot(dwody, n) + Dot(wo, dndy); rd.rxDirection = wi - dwodx + 2 * Vector3f(Dot(wo, n) * dndx + dwoDotn_dx * n); rd.ryDirection = wi - dwody + 2 * Vector3f(Dot(wo, n) * dndy + dwoDotn_dy * n);

} else if (flags == BxDFFlags::SpecularTransmission) { <<Initialize origins of specular differential rays>> 

rd.hasDifferentials = true; rd.rxOrigin = p() + dpdx; rd.ryOrigin = p() + dpdy;

<<Compute differential transmitted directions>> 

<<Find oriented surface normal for transmission>> 

if (Dot(wo, n) < 0) { n = -n; dndx = -dndx; dndy = -dndy; }

<<Compute partial derivatives of diagram>> 

Float dwoDotn_dx = Dot(dwodx, n) + Dot(wo, dndx); Float dwoDotn_dy = Dot(dwody, n) + Dot(wo, dndy); Float mu = Dot(wo, n) / eta - AbsDot(wi, n); Float dmudx = dwoDotn_dx * (1/eta + 1/Sqr(eta) * Dot(wo, n) / Dot(wi, n)); Float dmudy = dwoDotn_dy * (1/eta + 1/Sqr(eta) * Dot(wo, n) / Dot(wi, n));

rd.rxDirection = wi - eta * dwodx + Vector3f(mu * dndx + dmudx * n); rd.ryDirection = wi - eta * dwody + Vector3f(mu * dndy + dmudy * n);

}

A few variables will be used for both types of scattering, including the partial derivatives of the surface normal with respect to diagram and diagram on the image and diagram and diagram, which are computed using the chain rule.

<>=

Normal3f n = shading.n; Normal3f dndx = shading.dndu * dudx + shading.dndv * dvdx; Normal3f dndy = shading.dndu * dudy + shading.dndv * dvdy; Vector3f dwodx = -rayi.rxDirection - wo, dwody = -rayi.ryDirection - wo;

For both reflection and transmission, the origin of each differential ray can be found using the already-computed approximations of how much the surface position changes with respect to diagram position on the image plane diagram and diagram.

<>=

rd.hasDifferentials = true; rd.rxOrigin = p() + dpdx; rd.ryOrigin = p() + dpdy;

Finding the directions of these rays is slightly trickier. If we know how much the reflected direction diagram changes with respect to a shift of a pixel sample in the diagram and diagram directions on the image plane, we can use this information to approximate the direction of the offset rays. For example, the direction for the ray offset in diagram is

diagram

Recall from Equation (9.1) that for a normal diagram and outgoing direction diagram the direction for perfect specular reflection is

diagram

The partial derivatives of this expression are easily computed:

diagram

Using the properties of the dot product, it can further be shown that

diagram

The value of diagram has already been computed from the difference between the direction of the ray differential’s main ray and the direction of the diagram offset ray, and all the other necessary quantities are readily available from the SurfaceInteraction.

<>=

Float dwoDotn_dx = Dot(dwodx, n) + Dot(wo, dndx); Float dwoDotn_dy = Dot(dwody, n) + Dot(wo, dndy); rd.rxDirection = wi - dwodx + 2 * Vector3f(Dot(wo, n) * dndx + dwoDotn_dx * n); rd.ryDirection = wi - dwody + 2 * Vector3f(Dot(wo, n) * dndy + dwoDotn_dy * n);

A similar process of differentiating the equation for the direction of a specularly transmitted ray, Equation (9.4), gives the equation to find the differential change in the transmitted direction. pbrt computes refracted rays as

diagram

where diagram is flipped if necessary to lie in the same hemisphere as diagram, and where diagram is the relative index of refraction from diagram’s medium to diagram’s medium.

If we denote the term in brackets by diagram, then we have diagram. Taking the partial derivative in diagram, we have

diagram

(10.5)

Using some of the values found from computing specularly reflected ray differentials, we can find that we already know how to compute all of these values except for diagram.

<>=

<<Find oriented surface normal for transmission>> 

if (Dot(wo, n) < 0) { n = -n; dndx = -dndx; dndy = -dndy; }

<<Compute partial derivatives of diagram>> 

Float dwoDotn_dx = Dot(dwodx, n) + Dot(wo, dndx); Float dwoDotn_dy = Dot(dwody, n) + Dot(wo, dndy); Float mu = Dot(wo, n) / eta - AbsDot(wi, n); Float dmudx = dwoDotn_dx * (1/eta + 1/Sqr(eta) * Dot(wo, n) / Dot(wi, n)); Float dmudy = dwoDotn_dy * (1/eta + 1/Sqr(eta) * Dot(wo, n) / Dot(wi, n));

rd.rxDirection = wi - eta * dwodx + Vector3f(mu * dndx + dmudx * n); rd.ryDirection = wi - eta * dwody + Vector3f(mu * dndy + dmudy * n);

Before we get to the computation of diagram’s partial derivatives, we will start by reorienting the surface normal if necessary so that it lies on the same side of the surface as diagram. This matches pbrt’s computation of refracted ray directions.

<>=

if (Dot(wo, n) < 0) { n = -n; dndx = -dndx; dndy = -dndy; }

Returning to diagram and considering diagram, we have

diagram

Its first term can be evaluated with already known values. For the second term, we will start with Snell’s law, which gives

diagram

If we square both sides of the equation and take the partial derivative diagram, we find

diagram

We now can solve for diagram:

diagram

Putting it all together and simplifying, we have

diagram

The partial derivative in diagram is analogous and the implementation follows.

<<Compute partial derivatives of diagram>>=

Float dwoDotn_dx = Dot(dwodx, n) + Dot(wo, dndx); Float dwoDotn_dy = Dot(dwody, n) + Dot(wo, dndy); Float mu = Dot(wo, n) / eta - AbsDot(wi, n); Float dmudx = dwoDotn_dx * (1/eta + 1/Sqr(eta) * Dot(wo, n) / Dot(wi, n)); Float dmudy = dwoDotn_dy * (1/eta + 1/Sqr(eta) * Dot(wo, n) / Dot(wi, n));

If a ray undergoes many specular bounces, ray differentials sometimes drift off to have very large magnitudes, which can leave a trail of infinite and not-a-number values in their wake when they are used for texture filtering calculations. Therefore, the final fragment in this SpawnRay() method computes the squared length of all the differentials. If any is greater than diagram, the ray differentials are discarded and the RayDifferential hasDifferentials value is set to false. The fragment that handles this, <>, is simple and thus not included here.

10.1.4 Filtering Texture Functions

To eliminate texture aliasing, it is necessary to remove frequencies in texture functions that are past the Nyquist limit for the texture sampling rate. The goal is to compute, with as few approximations as possible, the result of the ideal texture resampling process, which says that in order to evaluate a texture function diagram at a point diagram on the image without aliasing, we must first band-limit it, removing frequencies beyond the Nyquist limit by convolving it with the sinc filter:

diagram

where, as in Section 10.1.1, diagram maps pixel locations to points in the texture function’s domain. The band-limited function diagram in turn should then be convolved with the pixel filter diagram centered at the diagram point on the screen at which we want to evaluate the texture function:

diagram

This gives the theoretically perfect value for the texture as projected onto the screen.

In practice, there are many simplifications that can be made to this process. For example, a box filter may be used for the band-limiting step, and the second step is usually ignored completely, effectively acting as if the pixel filter were a box filter, which makes it possible to do the antialiasing work completely in texture space. (The EWA filtering algorithm in Section 10.4.4 is a notable exception in that it assumes a Gaussian pixel filter.)

Assuming box filters then if, for example, the texture function is defined over parametric diagram coordinates, the filtering task is to average it over a region in diagram:

diagram

The extent of the filter region can be determined using the derivatives from the previous sections—for example, setting

diagram

and similarly for diagram and diagram to conservatively specify the box’s extent.

The box filter is easy to use, since it can be applied analytically by computing the average of the texture function over the appropriate region. Intuitively, this is a reasonable approach to the texture filtering problem, and it can be computed directly for many texture functions. Indeed, through the rest of this chapter, we will often use a box filter to average texture function values between samples and informally use the term filter region to describe the area being averaged over. This is the most common approach when filtering texture functions.

Even the box filter, with all of its shortcomings, gives acceptable results for texture filtering in many cases. One factor that helps is the fact that a number of samples are usually taken in each pixel. Thus, even if the filtered texture values used in each one are suboptimal, once they are filtered by the pixel reconstruction filter, the end result generally does not suffer too much.

An alternative to using the box filter to filter texture functions is to use the observation that the effect of the ideal sinc filter is to let frequency components below the Nyquist limit pass through unchanged but to remove frequencies past it. Therefore, if we know the frequency content of the texture function (e.g., if it is a sum of terms, each one with known frequency content), then if we replace the high-frequency terms with their average values, we are effectively doing the work of the sinc prefilter.

Finally, for texture functions where none of these techniques is easily applied, a final option is supersampling—the function is evaluated and filtered at multiple locations near the main evaluation point, thus increasing the sampling rate in texture space. If a box filter is used to filter these sample values, this is equivalent to averaging the value of the function. This approach can be expensive if the texture function is complex to evaluate, and as with image sampling, a very large number of samples may be needed to remove aliasing. Although this is a brute-force solution, it is still more efficient than increasing the image sampling rate, since it does not incur the cost of tracing more rays through the scene.