pex-renderer icon indicating copy to clipboard operation
pex-renderer copied to clipboard

Shadow mapping breaks after 2 minutes

Open vorg opened this issue 6 years ago • 2 comments

Screenshot 2019-05-18 at 22 02 46

What i discovered so far:

The code responsible is in directional light shadowmap update

  const sceneBboxInLightSpace = aabb.fromPoints(bboxPointsInLightSpace)

  const lightNear = -sceneBboxInLightSpace[1][2]
  const lightFar = -sceneBboxInLightSpace[0][2]

  light.set({
    _near: lightNear,
    _far: lightFar
  })

When the bug appears the near/far values are invalid (identical)

Screenshot 2019-05-18 at 23 20 35

instead of usual

Screenshot 2019-05-18 at 23 20 31

Points are definitely not identical and yet aabb.fromtPoints returns 2x last point

Screenshot 2019-05-18 at 23 56 11

abb.fromPoints function from pex-geom seems to be ok

function fromPoints (points) {
  var aabb = create()
  var min = aabb[0]
  var max = aabb[1]
  for (var i = 0, len = points.length; i < len; i++) {
    var p = points[i]
    min[0] = Math.min(min[0], p[0])
    min[1] = Math.min(min[1], p[1])
    min[2] = Math.min(min[2], p[2])
    max[0] = Math.max(max[0], p[0])
    max[1] = Math.max(max[1], p[1])
    max[2] = Math.max(max[2], p[2])
  }
  return aabb
}

Running code 3x returns different results 0_o Screenshot 2019-05-19 at 01 19 18 Screenshot 2019-05-19 at 01 20 02

Putting debugger at the end prints 3x same invalid result !!! Screenshot 2019-05-19 at 01 25 35 Screenshot 2019-05-19 at 01 23 19

Where it happens?

  • Only Safari 12.1, MacOS 10.14.4
  • Chrome, can't reproduce
  • Firefox, can't reproduce

Is this Safari JIT bug?

vorg avatar May 19 '19 14:05 vorg

Added test cases

Expected

Screenshot 2019-05-19 at 15 57 56

Broken

Screenshot 2019-05-19 at 15 57 43

Visit in Safari with dev tools opened and leave for few minutes. Whenever it encouters the bug (near == far) it will stop at debugger statement. No interaction is required, just leave the tab in the foreground. Wait for the hdr envmap to load.

Possible fix is to replace https://github.com/pex-gl/pex-geom/blob/master/aabb.js#L27

1. len variable inline, crashing after few min

http://marcinignac.com/experiments/noise-strokes/for-inline/

for (var i = 0, len = points.length; i < len; i++) {

2. using array length, crashing

http://marcinignac.com/experiments/noise-strokes/for-length/

for (var i = 0; i <  points.length; i++) {

3. declaring var len, crashing

http://marcinignac.com/experiments/noise-strokes/for-declare/

var len = points.length
for (var i = 0; i <  len; i++) {

4. added try catch around fromPoints code to deoptimize the function, crashing

http://marcinignac.com/experiments/noise-strokes/for-deopt/

Note: apparently that has to be around function call not the body

5. while looop, crashing

http://marcinignac.com/experiments/noise-strokes/for-while/

  var len = points.length
  let i = 0
  while (i < len) {
    var p = points[i]

6. const everywhere

http://marcinignac.com/experiments/noise-strokes/for-const/

const min = [Infinity, Infinity, Infinity]
const max = [-Infinity, -Infinity, -Infinity]

for (let i = 0; i < points.length; i++) {
  const p = points[i]

7. no create(), crashing

http://marcinignac.com/experiments/noise-strokes/for-no-infinity/

if (!points.length) {
  return create()
}

const min = [points[0][0], points[0][1], points[0][2]]
const max = [points[0][0], points[0][1], points[0][2]]

for (let i = 1; i < points.length; i++) {
const p = points[i]

vorg avatar May 19 '19 15:05 vorg

@vorg please test if we can still recreate it

vorg avatar Jul 16 '19 11:07 vorg