| 1 | // Copyright 2009-2021 Intel Corporation |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include "primitive.h" |
| 7 | #include "curve_intersector_precalculations.h" |
| 8 | |
| 9 | namespace embree |
| 10 | { |
| 11 | template<int M> |
| 12 | struct CurveNi |
| 13 | { |
| 14 | struct Type : public PrimitiveType { |
| 15 | const char* name() const; |
| 16 | size_t sizeActive(const char* This) const; |
| 17 | size_t sizeTotal(const char* This) const; |
| 18 | size_t getBytes(const char* This) const; |
| 19 | }; |
| 20 | static Type type; |
| 21 | |
| 22 | public: |
| 23 | |
| 24 | /* Returns maximum number of stored primitives */ |
| 25 | static __forceinline size_t max_size() { return M; } |
| 26 | |
| 27 | /* Returns required number of primitive blocks for N primitives */ |
| 28 | static __forceinline size_t blocks(size_t N) { return (N+M-1)/M; } |
| 29 | |
| 30 | static __forceinline size_t bytes(size_t N) |
| 31 | { |
| 32 | const size_t f = N/M, r = N%M; |
| 33 | static_assert(sizeof(CurveNi) == 22+25*M, "internal data layout issue" ); |
| 34 | return f*sizeof(CurveNi) + (r!=0)*(22 + 25*r); |
| 35 | } |
| 36 | |
| 37 | public: |
| 38 | |
| 39 | /*! Default constructor. */ |
| 40 | __forceinline CurveNi () {} |
| 41 | |
| 42 | /*! fill curve from curve list */ |
| 43 | __forceinline void fill(const PrimRef* prims, size_t& begin, size_t _end, Scene* scene) |
| 44 | { |
| 45 | size_t end = min(a: begin+M,b: _end); |
| 46 | N = (unsigned char)(end-begin); |
| 47 | const unsigned int geomID0 = prims[begin].geomID(); |
| 48 | this->geomID(N) = geomID0; |
| 49 | ty = (unsigned char) scene->get(i: geomID0)->getType(); |
| 50 | |
| 51 | /* encode all primitives */ |
| 52 | BBox3fa bounds = empty; |
| 53 | for (size_t i=0; i<N; i++) |
| 54 | { |
| 55 | const PrimRef& prim = prims[begin+i]; |
| 56 | const unsigned int geomID = prim.geomID(); assert(geomID == geomID0); |
| 57 | const unsigned int primID = prim.primID(); |
| 58 | bounds.extend(other: scene->get(i: geomID)->vbounds(primID)); |
| 59 | } |
| 60 | |
| 61 | /* calculate offset and scale */ |
| 62 | Vec3fa loffset = bounds.lower; |
| 63 | float lscale = reduce_min(v: 256.0f/(bounds.size()*sqrt(x: 3.0f))); |
| 64 | if (bounds.size() == Vec3fa(zero)) lscale = 0.0f; |
| 65 | *this->offset(N) = loffset; |
| 66 | *this->scale(N) = lscale; |
| 67 | |
| 68 | /* encode all primitives */ |
| 69 | for (size_t i=0; i<M && begin<end; i++, begin++) |
| 70 | { |
| 71 | const PrimRef& prim = prims[begin]; |
| 72 | const unsigned int geomID = prim.geomID(); |
| 73 | const unsigned int primID = prim.primID(); |
| 74 | const LinearSpace3fa space2 = scene->get(i: geomID)->computeAlignedSpace(primID); |
| 75 | |
| 76 | const LinearSpace3fa space3(trunc(a: 126.0f*space2.vx),trunc(a: 126.0f*space2.vy),trunc(a: 126.0f*space2.vz)); |
| 77 | const BBox3fa bounds = scene->get(i: geomID)->vbounds(ofs: loffset,scale: lscale,r_scale0: max(a: length(a: space3.vx),b: length(a: space3.vy),c: length(a: space3.vz)),space: space3.transposed(),i: primID); |
| 78 | |
| 79 | bounds_vx_x(N)[i] = (char) space3.vx.x; |
| 80 | bounds_vx_y(N)[i] = (char) space3.vx.y; |
| 81 | bounds_vx_z(N)[i] = (char) space3.vx.z; |
| 82 | bounds_vx_lower(N)[i] = (short) clamp(x: floor(x: bounds.lower.x),lower: -32767.0f,upper: 32767.0f); |
| 83 | bounds_vx_upper(N)[i] = (short) clamp(x: ceil (x: bounds.upper.x),lower: -32767.0f,upper: 32767.0f); |
| 84 | assert(-32767.0f <= floor(bounds.lower.x) && floor(bounds.lower.x) <= 32767.0f); |
| 85 | assert(-32767.0f <= ceil (bounds.upper.x) && ceil (bounds.upper.x) <= 32767.0f); |
| 86 | |
| 87 | bounds_vy_x(N)[i] = (char) space3.vy.x; |
| 88 | bounds_vy_y(N)[i] = (char) space3.vy.y; |
| 89 | bounds_vy_z(N)[i] = (char) space3.vy.z; |
| 90 | bounds_vy_lower(N)[i] = (short) clamp(x: floor(x: bounds.lower.y),lower: -32767.0f,upper: 32767.0f); |
| 91 | bounds_vy_upper(N)[i] = (short) clamp(x: ceil (x: bounds.upper.y),lower: -32767.0f,upper: 32767.0f); |
| 92 | assert(-32767.0f <= floor(bounds.lower.y) && floor(bounds.lower.y) <= 32767.0f); |
| 93 | assert(-32767.0f <= ceil (bounds.upper.y) && ceil (bounds.upper.y) <= 32767.0f); |
| 94 | |
| 95 | bounds_vz_x(N)[i] = (char) space3.vz.x; |
| 96 | bounds_vz_y(N)[i] = (char) space3.vz.y; |
| 97 | bounds_vz_z(N)[i] = (char) space3.vz.z; |
| 98 | bounds_vz_lower(N)[i] = (short) clamp(x: floor(x: bounds.lower.z),lower: -32767.0f,upper: 32767.0f); |
| 99 | bounds_vz_upper(N)[i] = (short) clamp(x: ceil (x: bounds.upper.z),lower: -32767.0f,upper: 32767.0f); |
| 100 | assert(-32767.0f <= floor(bounds.lower.z) && floor(bounds.lower.z) <= 32767.0f); |
| 101 | assert(-32767.0f <= ceil (bounds.upper.z) && ceil (bounds.upper.z) <= 32767.0f); |
| 102 | |
| 103 | this->primID(N)[i] = primID; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | template<typename BVH, typename Allocator> |
| 108 | __forceinline static typename BVH::NodeRef createLeaf (BVH* bvh, const PrimRef* prims, const range<size_t>& set, const Allocator& alloc) |
| 109 | { |
| 110 | size_t start = set.begin(); |
| 111 | size_t items = CurveNi::blocks(N: set.size()); |
| 112 | size_t numbytes = CurveNi::bytes(N: set.size()); |
| 113 | CurveNi* accel = (CurveNi*) alloc.malloc1(numbytes,BVH::byteAlignment); |
| 114 | for (size_t i=0; i<items; i++) { |
| 115 | accel[i].fill(prims,begin&: start,end: set.end(),scene: bvh->scene); |
| 116 | } |
| 117 | return bvh->encodeLeaf((char*)accel,items); |
| 118 | }; |
| 119 | |
| 120 | public: |
| 121 | |
| 122 | // 27.6 - 46 bytes per primitive |
| 123 | unsigned char ty; |
| 124 | unsigned char N; |
| 125 | unsigned char data[4+25*M+16]; |
| 126 | |
| 127 | /* |
| 128 | struct Layout |
| 129 | { |
| 130 | unsigned int geomID; |
| 131 | unsigned int primID[N]; |
| 132 | |
| 133 | char bounds_vx_x[N]; |
| 134 | char bounds_vx_y[N]; |
| 135 | char bounds_vx_z[N]; |
| 136 | short bounds_vx_lower[N]; |
| 137 | short bounds_vx_upper[N]; |
| 138 | |
| 139 | char bounds_vy_x[N]; |
| 140 | char bounds_vy_y[N]; |
| 141 | char bounds_vy_z[N]; |
| 142 | short bounds_vy_lower[N]; |
| 143 | short bounds_vy_upper[N]; |
| 144 | |
| 145 | char bounds_vz_x[N]; |
| 146 | char bounds_vz_y[N]; |
| 147 | char bounds_vz_z[N]; |
| 148 | short bounds_vz_lower[N]; |
| 149 | short bounds_vz_upper[N]; |
| 150 | |
| 151 | Vec3f offset; |
| 152 | float scale; |
| 153 | }; |
| 154 | */ |
| 155 | |
| 156 | __forceinline unsigned int& geomID(size_t N) { return *(unsigned int*)((char*)this+2); } |
| 157 | __forceinline const unsigned int& geomID(size_t N) const { return *(unsigned int*)((char*)this+2); } |
| 158 | |
| 159 | __forceinline unsigned int* primID(size_t N) { return (unsigned int*)((char*)this+6); } |
| 160 | __forceinline const unsigned int* primID(size_t N) const { return (unsigned int*)((char*)this+6); } |
| 161 | |
| 162 | __forceinline char* bounds_vx_x(size_t N) { return (char*)((char*)this+6+4*N); } |
| 163 | __forceinline const char* bounds_vx_x(size_t N) const { return (char*)((char*)this+6+4*N); } |
| 164 | |
| 165 | __forceinline char* bounds_vx_y(size_t N) { return (char*)((char*)this+6+5*N); } |
| 166 | __forceinline const char* bounds_vx_y(size_t N) const { return (char*)((char*)this+6+5*N); } |
| 167 | |
| 168 | __forceinline char* bounds_vx_z(size_t N) { return (char*)((char*)this+6+6*N); } |
| 169 | __forceinline const char* bounds_vx_z(size_t N) const { return (char*)((char*)this+6+6*N); } |
| 170 | |
| 171 | __forceinline short* bounds_vx_lower(size_t N) { return (short*)((char*)this+6+7*N); } |
| 172 | __forceinline const short* bounds_vx_lower(size_t N) const { return (short*)((char*)this+6+7*N); } |
| 173 | |
| 174 | __forceinline short* bounds_vx_upper(size_t N) { return (short*)((char*)this+6+9*N); } |
| 175 | __forceinline const short* bounds_vx_upper(size_t N) const { return (short*)((char*)this+6+9*N); } |
| 176 | |
| 177 | __forceinline char* bounds_vy_x(size_t N) { return (char*)((char*)this+6+11*N); } |
| 178 | __forceinline const char* bounds_vy_x(size_t N) const { return (char*)((char*)this+6+11*N); } |
| 179 | |
| 180 | __forceinline char* bounds_vy_y(size_t N) { return (char*)((char*)this+6+12*N); } |
| 181 | __forceinline const char* bounds_vy_y(size_t N) const { return (char*)((char*)this+6+12*N); } |
| 182 | |
| 183 | __forceinline char* bounds_vy_z(size_t N) { return (char*)((char*)this+6+13*N); } |
| 184 | __forceinline const char* bounds_vy_z(size_t N) const { return (char*)((char*)this+6+13*N); } |
| 185 | |
| 186 | __forceinline short* bounds_vy_lower(size_t N) { return (short*)((char*)this+6+14*N); } |
| 187 | __forceinline const short* bounds_vy_lower(size_t N) const { return (short*)((char*)this+6+14*N); } |
| 188 | |
| 189 | __forceinline short* bounds_vy_upper(size_t N) { return (short*)((char*)this+6+16*N); } |
| 190 | __forceinline const short* bounds_vy_upper(size_t N) const { return (short*)((char*)this+6+16*N); } |
| 191 | |
| 192 | __forceinline char* bounds_vz_x(size_t N) { return (char*)((char*)this+6+18*N); } |
| 193 | __forceinline const char* bounds_vz_x(size_t N) const { return (char*)((char*)this+6+18*N); } |
| 194 | |
| 195 | __forceinline char* bounds_vz_y(size_t N) { return (char*)((char*)this+6+19*N); } |
| 196 | __forceinline const char* bounds_vz_y(size_t N) const { return (char*)((char*)this+6+19*N); } |
| 197 | |
| 198 | __forceinline char* bounds_vz_z(size_t N) { return (char*)((char*)this+6+20*N); } |
| 199 | __forceinline const char* bounds_vz_z(size_t N) const { return (char*)((char*)this+6+20*N); } |
| 200 | |
| 201 | __forceinline short* bounds_vz_lower(size_t N) { return (short*)((char*)this+6+21*N); } |
| 202 | __forceinline const short* bounds_vz_lower(size_t N) const { return (short*)((char*)this+6+21*N); } |
| 203 | |
| 204 | __forceinline short* bounds_vz_upper(size_t N) { return (short*)((char*)this+6+23*N); } |
| 205 | __forceinline const short* bounds_vz_upper(size_t N) const { return (short*)((char*)this+6+23*N); } |
| 206 | |
| 207 | __forceinline Vec3f* offset(size_t N) { return (Vec3f*)((char*)this+6+25*N); } |
| 208 | __forceinline const Vec3f* offset(size_t N) const { return (Vec3f*)((char*)this+6+25*N); } |
| 209 | |
| 210 | __forceinline float* scale(size_t N) { return (float*)((char*)this+6+25*N+12); } |
| 211 | __forceinline const float* scale(size_t N) const { return (float*)((char*)this+6+25*N+12); } |
| 212 | |
| 213 | __forceinline char* end(size_t N) { return (char*)this+6+25*N+16; } |
| 214 | __forceinline const char* end(size_t N) const { return (char*)this+6+25*N+16; } |
| 215 | }; |
| 216 | |
| 217 | template<int M> |
| 218 | typename CurveNi<M>::Type CurveNi<M>::type; |
| 219 | |
| 220 | typedef CurveNi<4> Curve4i; |
| 221 | typedef CurveNi<8> Curve8i; |
| 222 | } |
| 223 | |