31 #include <mesh/MeshStorage.h> 32 #include <mesh/MeshCompression.h> 34 #include <cugar/basic/cuda/pointers.h> 35 #include <cugar/linalg/vector.h> 36 #include <cugar/linalg/matrix.h> 39 #define NORMAL_COMPRESSION 1 40 #define TEX_COORD_COMPRESSION 1 41 #define UNIFIED_VERTEX_ATTRIBUTES 1 61 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
62 static int3
load(
const int* indices,
const uint32 tri_idx) {
return reinterpret_cast<const int3*
>(indices)[tri_idx]; }
68 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
69 static int3
load(
const int* indices,
const uint32 tri_idx)
71 #ifdef FERMAT_DEVICE_COMPILATION 72 const int4 i4 = cugar::cuda::load<cugar::cuda::LOAD_DEFAULT>(
reinterpret_cast<const int4*
>(indices) + tri_idx );
75 const int4 i4 =
reinterpret_cast<const int4*
>(indices)[tri_idx];
77 return make_int3(i4.x, i4.y, i4.z);
87 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
88 static float3
load(
const MeshView& mesh,
const uint32 vert_idx) {
return reinterpret_cast<const float3*
>(mesh.vertex_data)[vert_idx]; }
94 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
95 static float4 load4(
const MeshView& mesh,
const uint32 vert_idx)
97 #ifdef FERMAT_DEVICE_COMPILATION 98 return cugar::cuda::load<cugar::cuda::LOAD_LDG>(
reinterpret_cast<const float4*
>(mesh.vertex_data) + vert_idx );
100 return reinterpret_cast<const float4*
>(mesh.vertex_data)[vert_idx];
104 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
105 static float3
load(
const MeshView& mesh,
const uint32 vert_idx)
107 const float4 v4 = load4( mesh, vert_idx );
109 return make_float3(v4.x, v4.y, v4.z);
113 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
116 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
119 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
125 #if UNIFIED_VERTEX_ATTRIBUTES 126 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
135 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
143 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
151 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
159 FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
167 FERMAT_HOST_DEVICE
inline 170 FERMAT_ASSERT(tri_id < uint32(mesh.num_triangles));
184 FERMAT_HOST_DEVICE
inline 187 #if UNIFIED_VERTEX_ATTRIBUTES 188 FERMAT_ASSERT(tri_id < uint32(mesh.num_triangles));
198 geom->position = vp2 * (1.0f - u - v) + vp0 * u + vp1 * v;
203 geom->normal_g = cugar::normalize(cugar::cross(dp_du, dp_dv));
209 const cugar::Vector3f vn0 = cugar::unpack_normal(cugar::binary_cast<uint32>( vp0_w.w ));
210 const cugar::Vector3f vn1 = cugar::unpack_normal(cugar::binary_cast<uint32>( vp1_w.w ));
211 const cugar::Vector3f vn2 = cugar::unpack_normal(cugar::binary_cast<uint32>( vp2_w.w ));
213 const cugar::Vector3f N = cugar::normalize(vn2 * (1.0f - u - v) + vn0 * u + vn1 * v);
216 geom->tangent = cugar::orthogonal(N);
217 geom->binormal = cugar::cross(N, geom->tangent);
220 FERMAT_ASSERT(tri_id < uint32(mesh.num_triangles));
226 geom->position = vp2 * (1.0f - u - v) + vp0 * u + vp1 * v;
231 geom->normal_g = cugar::normalize(cugar::cross(dp_du, dp_dv));
235 if (mesh.normal_indices && mesh.normal_data)
237 #if NORMAL_COMPRESSION 239 const cugar::Vector3f vn0 = tri.x >= 0 ? cugar::unpack_normal(tri.x) : geom->normal_g;
240 const cugar::Vector3f vn1 = tri.y >= 0 ? cugar::unpack_normal(tri.y) : geom->normal_g;
241 const cugar::Vector3f vn2 = tri.z >= 0 ? cugar::unpack_normal(tri.z) : geom->normal_g;
245 (tri.x < 0 || tri.x < mesh.num_normals) &&
246 (tri.y < 0 || tri.y < mesh.num_normals) &&
247 (tri.z < 0 || tri.z < mesh.num_normals));
248 const cugar::Vector3f vn0 = tri.x >= 0 ? cugar::unpack_normal(mesh.normal_data_comp[tri.x]) : geom->normal_g;
249 const cugar::Vector3f vn1 = tri.y >= 0 ? cugar::unpack_normal(mesh.normal_data_comp[tri.y]) : geom->normal_g;
250 const cugar::Vector3f vn2 = tri.z >= 0 ? cugar::unpack_normal(mesh.normal_data_comp[tri.z]) : geom->normal_g;
255 const cugar::Vector3f N = cugar::normalize(vn2 * (1.0f - u - v) + vn0 * u + vn1 * v);
258 geom->tangent = cugar::orthogonal(N);
259 geom->binormal = cugar::cross(N, geom->tangent);
263 geom->normal_s = geom->normal_g;
264 geom->tangent = cugar::orthogonal(geom->normal_g);
265 geom->binormal = cugar::cross(geom->normal_g, geom->tangent);
269 #if TEX_COORD_COMPRESSION 270 if (mesh.texture_indices_comp)
277 if (mesh.texture_indices && mesh.texture_data)
290 #if TEX_COORD_COMPRESSION 291 if (mesh.lightmap_indices_comp)
298 if (mesh.lightmap_indices && mesh.lightmap_data)
314 FERMAT_HOST_DEVICE
inline 322 FERMAT_HOST_DEVICE
inline 336 return vp2 * (1.0f - u - v) + vp0 * u + vp1 * v;
341 FERMAT_HOST_DEVICE
inline 349 FERMAT_HOST_DEVICE
inline 352 #if UNIFIED_VERTEX_ATTRIBUTES 358 const cugar::Vector3f N = cugar::normalize(vn2 * (1.0f - u - v) + vn0 * u + vn1 * v);
371 Ng = cugar::normalize(cugar::cross(dp_du, dp_dv));
374 if (mesh.normal_indices && mesh.normal_data)
376 #if NORMAL_COMPRESSION 378 const cugar::Vector3f vn0 = tri.x >= 0 ? cugar::unpack_normal(tri.x) : Ng;
379 const cugar::Vector3f vn1 = tri.y >= 0 ? cugar::unpack_normal(tri.y) : Ng;
380 const cugar::Vector3f vn2 = tri.z >= 0 ? cugar::unpack_normal(tri.z) : Ng;
383 const cugar::Vector3f vn0 = tri.x >= 0 ?
reinterpret_cast<const float3*
>(mesh.normal_data)[tri.x] : Ng;
384 const cugar::Vector3f vn1 = tri.y >= 0 ? reinterpret_cast<const float3*>(mesh.normal_data)[tri.y] : Ng;
385 const cugar::Vector3f vn2 = tri.z >= 0 ?
reinterpret_cast<const float3*
>(mesh.normal_data)[tri.z] : Ng;
387 const cugar::Vector3f N = cugar::normalize(vn2 * (1.0f - u - v) + vn0 * u + vn1 * v);
398 FERMAT_HOST_DEVICE
inline 411 template <TextureSet TEXTURE_SET>
412 FERMAT_HOST_DEVICE
inline 415 FERMAT_ASSERT(tri_id < uint32(mesh.num_triangles));
416 if (TEXTURE_SET == kTextureCoords0 && mesh.texture_indices)
429 return cugar::transpose(M);
431 else if (TEXTURE_SET == kLightmapCoords && mesh.lightmap_indices)
444 return cugar::transpose(M);
446 return cugar::Matrix2x2f::one();
455 template <TextureSet TEXTURE_SET>
456 FERMAT_HOST_DEVICE
inline 461 cugar::invert(dst_duv, R);
471 FERMAT_HOST_DEVICE
inline 474 FERMAT_ASSERT(tri_id < uint32(mesh.num_triangles));
503 FERMAT_HOST_DEVICE
inline 506 FERMAT_ASSERT(tri_id < uint32(mesh.num_triangles));
539 template <TextureSet TEXTURE_SET>
540 FERMAT_HOST_DEVICE
inline 543 FERMAT_ASSERT(tri_id < uint32(mesh.num_triangles));
544 return prim_dp_duv( mesh, tri_id ) * prim_duv_dst<TEXTURE_SET>( mesh, tri_id );
554 FERMAT_HOST_DEVICE
inline 563 cugar::invert( dp_duvw, R );
574 FERMAT_HOST_DEVICE
inline 577 FERMAT_ASSERT(tri_id < uint32(mesh.num_triangles));
594 M[0][0] = cugar::dot(dp_du, t);
596 M[0][1] = cugar::dot(dp_dv, t);
597 M[1][1] = cugar::dot(dp_dv, b);
608 FERMAT_HOST_DEVICE
inline 616 cugar::invert( dtb_duv, R );
626 FERMAT_HOST_DEVICE
inline 646 template <TextureSet TEXTURE_SET>
647 FERMAT_HOST_DEVICE
inline 655 return dst_duv * duv_dp;
665 template <TextureSet TEXTURE_SET>
666 FERMAT_HOST_DEVICE
inline 674 return dst_duv * duv_dtb;
684 template <TextureSet TEXTURE_SET>
685 FERMAT_HOST_DEVICE
inline 690 return dtb_duv * duv_dst;
696 template <TextureSet TEXTURE_SET>
697 FERMAT_HOST_DEVICE
inline 712 template <TextureSet TEXTURE_SET>
713 FERMAT_HOST_DEVICE
inline FERMAT_HOST_DEVICE cugar::Matrix3x3f prim_dp_duvw(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:504
FERMAT_HOST_DEVICE cugar::Matrix2x2f prim_duv_dtb(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:609
CUGAR_HOST_DEVICE Vector2f singular_values(const Matrix2x2f &m)
Definition: matrix_inline.h:726
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE T load(const T *ptr)
Definition: pointers.h:99
FERMAT_HOST_DEVICE FERMAT_FORCEINLINE int3 load_normal_triangle(const int *triangle_indices, const uint32 tri_idx)
Definition: mesh_utils.h:144
FERMAT_HOST_DEVICE cugar::Matrix2x2f prim_dst_dtb(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:667
FERMAT_HOST_DEVICE float prim_area(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:168
FERMAT_HOST_DEVICE cugar::Matrix2x3f prim_dst_dp(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:648
FERMAT_HOST_DEVICE cugar::Matrix2x3f prim_duv_dp(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:627
Defines various spherical mappings.
FERMAT_HOST_DEVICE cugar::Matrix2x2f prim_dtb_duv(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:575
FERMAT_HOST_DEVICE cugar::Matrix3x3f prim_duvw_dp(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:555
FERMAT_HOST_DEVICE void setup_differential_geometry(const MeshView &mesh, const uint32 tri_id, const float u, const float v, VertexGeometry *geom, float *pdf=0)
Definition: mesh_utils.h:185
Definition: mesh_utils.h:56
CUGAR_HOST_DEVICE void svd(const Matrix2x2f &m, Matrix2x2f &u, Vector2f &s, Matrix2x2f &v)
Definition: matrix_inline.h:752
Definition: mesh_utils.h:82
FERMAT_HOST_DEVICE FERMAT_FORCEINLINE int3 load_texture_triangle(const int *triangle_indices, const uint32 tri_idx)
Definition: mesh_utils.h:152
FERMAT_HOST_DEVICE FERMAT_FORCEINLINE int3 load_vertex_triangle(const int *triangle_indices, const uint32 tri_idx)
Definition: mesh_utils.h:136
FERMAT_HOST_DEVICE cugar::Matrix2x2f prim_duv_dst(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:457
FERMAT_HOST_DEVICE cugar::Vector2f prim_dst_ellipse_size(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:698
FERMAT_HOST_DEVICE cugar::Matrix2x2f prim_dst_duv(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:413
Definition: MeshView.h:96
FERMAT_HOST_DEVICE FERMAT_FORCEINLINE int3 load_lightmap_triangle(const int *triangle_indices, const uint32 tri_idx)
Definition: mesh_utils.h:160
FERMAT_HOST_DEVICE cugar::Matrix3x2f prim_dp_duv(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:472
FERMAT_HOST_DEVICE cugar::Matrix3x2f prim_dp_dst(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:541
FERMAT_HOST_DEVICE cugar::Vector3f interpolate_position(const MeshView &mesh, const uint32 tri_id, const float u, const float v, float *pdf=0)
Definition: mesh_utils.h:323
FERMAT_HOST_DEVICE FERMAT_FORCEINLINE const MeshView::vertex_type & fetch_vertex(const MeshView &mesh, const uint32 vert_idx)
Definition: MeshView.h:150
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE uint32 length(const vector_view< Iterator > &vec)
Definition: vector_view.h:228
FERMAT_HOST_DEVICE cugar::Matrix2x2f prim_dtb_dst(const MeshView &mesh, const uint32 tri_id)
Definition: mesh_utils.h:686
FERMAT_HOST_DEVICE cugar::Vector3f interpolate_normal(const MeshView &mesh, const uint32 tri_id, const float u, const float v)
Definition: mesh_utils.h:350
FERMAT_HOST_DEVICE void prim_dst_ellipse(const MeshView &mesh, const uint32 tri_id, cugar::Matrix2x2f &u, cugar::Vector2f &s)
Definition: mesh_utils.h:714