bool BBox::intersect(const Ray& ray, float *tnear, float *tfar) const { // you may already have those values hanging around somewhere const __m128 plus_inf = loadps(ps_cst_plus_inf), minus_inf = loadps(ps_cst_minus_inf); // use whatever's apropriate to load. const __m128 box_min = loadps(&min), box_max = loadps(&max), pos = loadps(&ray.o), inv_dir = loadps(&ray.inv_d); // use a div if inverted directions aren't available const __m128 l1 = mulps(subps(box_min, pos), inv_dir); const __m128 l2 = mulps(subps(box_max, pos), inv_dir); // the order we use for those min/max is vital to filter out // NaNs that happens when an inv_dir is +/- inf and // (box_min - pos) is 0. inf * 0 = NaN const __m128 filtered_l1a = minps(l1, plus_inf); const __m128 filtered_l2a = minps(l2, plus_inf); const __m128 filtered_l1b = maxps(l1, minus_inf); const __m128 filtered_l2b = maxps(l2, minus_inf); // now that we're back on our feet, test those slabs. __m128 lmax = maxps(filtered_l1a, filtered_l2a); __m128 lmin = minps(filtered_l1b, filtered_l2b); // unfold back. try to hide the latency of the shufps & co. const __m128 lmax0 = rotatelps(lmax); const __m128 lmin0 = rotatelps(lmin); lmax = minss(lmax, lmax0); lmin = maxss(lmin, lmin0); const __m128 lmax1 = muxhps(lmax,lmax); const __m128 lmin1 = muxhps(lmin,lmin); lmax = minss(lmax, lmax1); lmin = maxss(lmin, lmin1); const bool ret = _mm_comige_ss(lmax, _mm_setzero_ps()) & _mm_comige_ss(lmax,lmin); storess(lmin, tnear); storess(lmax, tfar); return ret; }
inline bool ray_box_intersect(const box_t & b, const ray_t & ray, rayseg_t & rs) { /* you may already have those values hanging around somewhere */ const __m128 plus_inf = loadps(ps_cst_plus_inf), minus_inf = loadps(ps_cst_minus_inf); /* use whatever's apropriate to load. */ const __m128 box_min = loadps(&b.min), box_max = loadps(&b.max), pos = loadps(&ray.pos), inv_dir = loadps(&ray.inv_dir); /* use a div if inverted directions aren't available */ const __m128 l1 = mulps(subps(box_min, pos), inv_dir); const __m128 l2 = mulps(subps(box_max, pos), inv_dir); /* the order we use for those min/max is vital to filter out */ /* NaNs that happens when an inv_dir is +/- inf and */ /* (box_min - pos) is 0. inf * 0 = NaN */ const __m128 filtered_l1a = minps(l1, plus_inf); const __m128 filtered_l2a = minps(l2, plus_inf); const __m128 filtered_l1b = maxps(l1, minus_inf); const __m128 filtered_l2b = maxps(l2, minus_inf); /* now that we're back on our feet, test those slabs. */ __m128 lmax = maxps(filtered_l1a, filtered_l2a); __m128 lmin = minps(filtered_l1b, filtered_l2b); /* unfold back. try to hide the latency of the shufps & co. */ const __m128 lmax0 = rotatelps(lmax); const __m128 lmin0 = rotatelps(lmin); lmax = minss(lmax, lmax0); lmin = maxss(lmin, lmin0); const __m128 lmax1 = muxhps(lmax, lmax); const __m128 lmin1 = muxhps(lmin, lmin); lmax = minss(lmax, lmax1); lmin = maxss(lmin, lmin1); const bool ret = _mm_comige_ss(lmax, _mm_setzero_ps()) & _mm_comige_ss(lmax, lmin); storess(lmin, &rs.t_near); storess(lmax, &rs.t_far); return ret; }