Skip to content

Commit e85bd38

Browse files
author
Antti Tapaninen
committed
explicitly handle square root of negative value scenario. Make sure the plane pair is identified as invalid.
1 parent 0bb5d01 commit e85bd38

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

Assets/ScriptableRenderPipeline/HDRenderPipeline/Lighting/TilePass/Resources/scrbound.compute

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,15 @@ float4 TransformPlaneToPostSpace(float4x4 InvProjection, float4 plane)
407407
return mul(plane, InvProjection);
408408
}
409409

410-
float4 EvalPlanePair(float2 posXY_in, float r)
410+
float4 EvalPlanePair(out bool validPlanes, float2 posXY_in, float r)
411411
{
412412
// rotate by 90 degrees to avoid potential division by zero
413413
bool bMustFlip = abs(posXY_in.y)<abs(posXY_in.x);
414414
float2 posXY = bMustFlip ? float2(-posXY_in.y, posXY_in.x) : posXY_in;
415415

416416
float fLenSQ = dot(posXY, posXY);
417-
float D = posXY.y * sqrt(fLenSQ - r*r);
417+
float diffSq = fLenSQ - r*r;
418+
float D = posXY.y * sqrt(max(0.0, diffSq));
418419

419420
float4 res;
420421
res.x = (-r*posXY.x - D) / fLenSQ;
@@ -425,22 +426,25 @@ float4 EvalPlanePair(float2 posXY_in, float r)
425426
// rotate back by 90 degrees
426427
res = bMustFlip ? float4(res.y, -res.x, res.w, -res.z) : res;
427428

429+
validPlanes = diffSq>0.0;
430+
428431
return res;
429432
}
430433

431434
void CalcBound(out bool2 bIsMinValid, out bool2 bIsMaxValid, out float2 vMin, out float2 vMax, float4x4 InvProjection, float3 pos_view_space, float r)
432435
{
433-
float4 planeX = EvalPlanePair(float2(pos_view_space.x, pos_view_space.z), r);
434-
float4 planeY = EvalPlanePair(float2(pos_view_space.y, pos_view_space.z), r);
436+
bool validX, validY;
437+
float4 planeX = EvalPlanePair(validX, float2(pos_view_space.x, pos_view_space.z), r);
438+
float4 planeY = EvalPlanePair(validY, float2(pos_view_space.y, pos_view_space.z), r);
435439

436440

437441
#if USE_LEFTHAND_CAMERASPACE
438442
planeX = planeX.zwxy; // need to swap left/right and top/bottom planes when using left hand system
439443
planeY = planeY.zwxy;
440444
#endif
441445

442-
bIsMinValid = bool2(planeX.z<0, planeY.z<0);
443-
bIsMaxValid = bool2((-planeX.x)<0, (-planeY.x)<0);
446+
bIsMinValid = bool2(planeX.z<0, planeY.z<0) && bool2(validX,validY);
447+
bIsMaxValid = bool2((-planeX.x)<0, (-planeY.x)<0) && bool2(validX,validY);
444448

445449
// hopefully the compiler takes zeros into account
446450
// should be the case since the transformation in TransformPlaneToPostSpace()

Assets/ScriptableRenderPipeline/fptl/scrbound.compute

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,15 @@ float4 TransformPlaneToPostSpace(float4x4 InvProjection, float4 plane)
408408
return mul(plane, InvProjection);
409409
}
410410

411-
float4 EvalPlanePair(float2 posXY_in, float r)
411+
float4 EvalPlanePair(out bool validPlanes, float2 posXY_in, float r)
412412
{
413413
// rotate by 90 degrees to avoid potential division by zero
414414
bool bMustFlip = abs(posXY_in.y)<abs(posXY_in.x);
415415
float2 posXY = bMustFlip ? float2(-posXY_in.y, posXY_in.x) : posXY_in;
416416

417417
float fLenSQ = dot(posXY, posXY);
418-
float D = posXY.y * sqrt(fLenSQ - r*r);
418+
float diffSq = fLenSQ - r*r;
419+
float D = posXY.y * sqrt(max(0.0, diffSq));
419420

420421
float4 res;
421422
res.x = (-r*posXY.x - D) / fLenSQ;
@@ -426,22 +427,25 @@ float4 EvalPlanePair(float2 posXY_in, float r)
426427
// rotate back by 90 degrees
427428
res = bMustFlip ? float4(res.y, -res.x, res.w, -res.z) : res;
428429

430+
validPlanes = diffSq>0.0;
431+
429432
return res;
430433
}
431434

432435
void CalcBound(out bool2 bIsMinValid, out bool2 bIsMaxValid, out float2 vMin, out float2 vMax, float4x4 InvProjection, float3 pos_view_space, float r)
433436
{
434-
float4 planeX = EvalPlanePair(float2(pos_view_space.x, pos_view_space.z), r);
435-
float4 planeY = EvalPlanePair(float2(pos_view_space.y, pos_view_space.z), r);
437+
bool validX, validY;
438+
float4 planeX = EvalPlanePair(validX, float2(pos_view_space.x, pos_view_space.z), r);
439+
float4 planeY = EvalPlanePair(validY, float2(pos_view_space.y, pos_view_space.z), r);
436440

437441

438442
#if USE_LEFTHAND_CAMERASPACE
439443
planeX = planeX.zwxy; // need to swap left/right and top/bottom planes when using left hand system
440444
planeY = planeY.zwxy;
441445
#endif
442446

443-
bIsMinValid = bool2(planeX.z<0, planeY.z<0);
444-
bIsMaxValid = bool2((-planeX.x)<0, (-planeY.x)<0);
447+
bIsMinValid = bool2(planeX.z<0, planeY.z<0) && bool2(validX,validY);
448+
bIsMaxValid = bool2((-planeX.x)<0, (-planeY.x)<0) && bool2(validX,validY);
445449

446450
// hopefully the compiler takes zeros into account
447451
// should be the case since the transformation in TransformPlaneToPostSpace()

0 commit comments

Comments
 (0)