Skip to content

Commit 6c91fb8

Browse files
testrender: Fix uv derivatives for testrender [#1978] (#2037)
Fix dudx, dudy, dvdx, dvdy, as well as dpdx, dpdy in testrender. Now, bump mapping from both UV-based mapping and 3D-based patterns works well. Mip-Map selection also works fine now. Images in Issue: #1978 This PR is intended to validate math and discuss tests. Fixes #1978 This fix could potentially affect many other tests since dpdx, dpdy are changed. I'm going to add tests representing bump and mipmapping in this PR. --------- Signed-off-by: Alexey Smolenchuk <[email protected]> Signed-off-by: Larry Gritz <[email protected]> Co-authored-by: Larry Gritz <[email protected]>
1 parent e0d7a24 commit 6c91fb8

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

src/testrender/raytracer.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,22 @@ struct Scene {
278278
return ((1 - u - v) * na + u * nb + v * nc).normalize();
279279
}
280280

281+
// Project differentials onto surface defined by N
282+
OSL_HOSTDEVICE
283+
void project(Dual2<Vec3>& p, const Vec3& N, const Vec3& I) const
284+
{
285+
Vec3 nI = I.normalized();
286+
float cosI = dot(-nI, N);
287+
288+
if (fabsf(cosI) > 1e-3f) {
289+
float deltaX = dot(p.dx(), N) / cosI;
290+
float deltaY = dot(p.dy(), N) / cosI;
291+
292+
p.dx() += nI * deltaX;
293+
p.dy() += nI * deltaY;
294+
}
295+
}
296+
281297
OSL_HOSTDEVICE
282298
Dual2<Vec2> uv(const Dual2<Vec3>& p, const Vec3& n, Vec3& dPdu, Vec3& dPdv,
283299
int primID, float u, float v) const
@@ -303,7 +319,25 @@ struct Scene {
303319
dPdv = (-dt12.x * dp02 + dt02.x * dp12) * invdet;
304320
// TODO: smooth out dPdu and dPdv by storing per vertex tangents
305321
}
306-
return Dual2<Vec2>((1 - u - v) * ta + u * tb + v * tc);
322+
323+
// L represent planes constructed from opposite points perpendicular
324+
// to N and scaled to return 1.0 for scalar product with itself
325+
Vec3 La = n.cross(vc - vb);
326+
La /= dot(va - vb, La);
327+
328+
Vec3 Lb = n.cross(va - vc);
329+
Lb /= dot(vb - vc, Lb);
330+
331+
Vec3 Lc = n.cross(vb - va);
332+
Lc /= dot(vc - va, Lc);
333+
334+
Vec2 dTdx = dot(La, p.dx()) * ta + dot(Lb, p.dx()) * tb
335+
+ dot(Lc, p.dx()) * tc;
336+
337+
Vec2 dTdy = dot(La, p.dy()) * ta + dot(Lb, p.dy()) * tb
338+
+ dot(Lc, p.dy()) * tc;
339+
340+
return Dual2<Vec2>((1 - u - v) * ta + u * tb + v * tc, dTdx, dTdy);
307341
}
308342

309343
OSL_HOSTDEVICE int shaderid(int primID) const { return shaderids[primID]; }

src/testrender/simpleraytracer.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -896,25 +896,26 @@ SimpleRaytracer::globals_from_hit(ShaderGlobalsType& sg, const Ray& r,
896896
const int meshid = m_meshids[id];
897897
#endif
898898

899-
Dual2<Vec3> P = r.point(t);
900-
// We are missing the projection onto the surface here
901-
sg.P = P.val();
902-
sg.dPdx = P.dx();
903-
sg.dPdy = P.dy();
904-
sg.N = scene.normal(P, sg.Ng, id, u, v);
905-
Dual2<Vec2> uv = scene.uv(P, sg.N, sg.dPdu, sg.dPdv, id, u, v);
906-
sg.u = uv.val().x;
907-
sg.dudx = uv.dx().x;
908-
sg.dudy = uv.dy().x;
909-
sg.v = uv.val().y;
910-
sg.dvdx = uv.dx().y;
911-
sg.dvdy = uv.dy().y;
912-
sg.surfacearea = m_mesh_surfacearea[meshid];
913899
Dual2<Vec3> direction = r.dual_direction();
914900
sg.I = direction.val();
915901
sg.dIdx = direction.dx();
916902
sg.dIdy = direction.dy();
917-
sg.backfacing = sg.Ng.dot(sg.I) > 0;
903+
Dual2<Vec3> P = r.point(t);
904+
sg.P = P.val();
905+
sg.N = scene.normal(P, sg.Ng, id, u, v);
906+
// Projecting onto the surface here
907+
scene.project(P, sg.N, sg.I);
908+
sg.dPdx = P.dx();
909+
sg.dPdy = P.dy();
910+
Dual2<Vec2> uv = scene.uv(P, sg.N, sg.dPdu, sg.dPdv, id, u, v);
911+
sg.u = uv.val().x;
912+
sg.dudx = uv.dx().x;
913+
sg.dudy = uv.dy().x;
914+
sg.v = uv.val().y;
915+
sg.dvdx = uv.dx().y;
916+
sg.dvdy = uv.dy().y;
917+
sg.surfacearea = m_mesh_surfacearea[meshid];
918+
sg.backfacing = sg.Ng.dot(sg.I) > 0;
918919
if (sg.backfacing) {
919920
sg.N = -sg.N;
920921
sg.Ng = -sg.Ng;
2.99 KB
Binary file not shown.

testsuite/render-uv/ref/out.exr

-246 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)