Code Monkey home page Code Monkey logo

Comments (10)

GeorgesStavracas avatar GeorgesStavracas commented on June 26, 2024

I've looked at it a bit, and found an interesting oddity. In graphene_ray_intersect_box(), at line 492, the inv_dir.value = graphene_simd4f_reciprocal (r->direction.value) call results in a vec3 containing: -nan, 2.0194845200, -1.1510224342. Everything else derails from there.

from graphene.

GeorgesStavracas avatar GeorgesStavracas commented on June 26, 2024

I think the bug here is that inv_dir.x should have resulted in +∞ instead of NaN

from graphene.

jadahl avatar jadahl commented on June 26, 2024

Indeed. I spent some time trying to work around that, and come up with a SSE 4.1 implementation that turned that -nan into FLT_MAX, which made it work on my CPU; but it wasn't very portable, and my aarch64 board isn't booting so I didn't look further into the neon variant.

Also, not sure requiring SSE4.1 is an option.

from graphene.

jadahl avatar jadahl commented on June 26, 2024

Fwiw, here is the SSE4.1 variant:

#  define graphene_simd4f_reciprocal(v) \
  (__extension__ ({ \
    const graphene_simd4f_t __two = graphene_simd4f_init (2.0f, 2.0f, 2.0f, 2.0f); \
    const graphene_simd4f_t __inf = graphene_simd4f_init (FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX); \
    graphene_simd4f_t __mask = _mm_cmpeq_ps(_mm_set1_ps(0.0), (v)); \
    graphene_simd4f_t __s = _mm_andnot_ps (__mask, _mm_rcp_ps ((v))); \
    graphene_simd4f_t __r = _mm_blendv_ps (__s, __inf, __mask); \
    graphene_simd4f_mul (__r, graphene_simd4f_sub (__two, graphene_simd4f_mul ((v), __r))); \
  }))

from graphene.

jadahl avatar jadahl commented on June 26, 2024

Also not sure if graphene_simd4f_t __mask = _mm_cmpeq_ps(_mm_set1_ps(0.0), (v)); is enough; as it does == 0.0 and not withit FLT_EPSILON * 2 which was needed in my tests to make all paths pass (SIMD & non-SIMD).

from graphene.

GeorgesStavracas avatar GeorgesStavracas commented on June 26, 2024

So, based on your code for Mutter, this seems to work here:

@@ -485,27 +486,46 @@ graphene_ray_intersect_box (const graphene_ray_t *r,
                             const graphene_box_t *b,
                             float                *t_out)
 {
+  graphene_vec3_t safe_direction;
   graphene_vec3_t inv_dir;
+  float d[3];
+
+#define V(v) (graphene_approx_val (d[v], 0.f) ? 2 * FLT_EPSILON : d[v])
+  graphene_vec3_to_float (&r->direction, d);
+  graphene_vec3_init (&safe_direction, V (0), V (1), V (2));
+#undef V
 
   /* FIXME: Needs a graphene_vec3_reciprocal() */
-  inv_dir.value = graphene_simd4f_reciprocal (r->direction.value);
+  inv_dir.value = graphene_simd4f_reciprocal (safe_direction.value);

from graphene.

jadahl avatar jadahl commented on June 26, 2024

Here seems like a better place for this code than in mutter.

from graphene.

jadahl avatar jadahl commented on June 26, 2024

Ping @ebassi .

from graphene.

ebassi avatar ebassi commented on June 26, 2024

The solution from @GeorgesStavracas looks good to me. I wonder if he wants to open a PR, or if I should do that.

from graphene.

jadahl avatar jadahl commented on June 26, 2024

I went and created #217 that works slightly the same, except it uses a static inline function and makes sure the direction stays on the correct side of the axis.

from graphene.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.