Changeset View
Changeset View
Standalone View
Standalone View
intern/cycles/kernel/geom/geom_triangle.h
| Show All 19 Lines | |||||
| * ray intersection we use a precomputed triangle storage to accelerate | * ray intersection we use a precomputed triangle storage to accelerate | ||||
| * intersection at the cost of more memory usage */ | * intersection at the cost of more memory usage */ | ||||
| #pragma once | #pragma once | ||||
| CCL_NAMESPACE_BEGIN | CCL_NAMESPACE_BEGIN | ||||
| /* Normal on triangle. */ | /* Normal on triangle. */ | ||||
| ccl_device_inline float3 triangle_normal(ccl_global const KernelGlobals *kg, | ccl_device_inline float3 triangle_normal(KernelGlobals kg, ccl_private ShaderData *sd) | ||||
| ccl_private ShaderData *sd) | |||||
| { | { | ||||
| /* load triangle vertices */ | /* load triangle vertices */ | ||||
| const uint4 tri_vindex = kernel_tex_fetch(__tri_vindex, sd->prim); | const uint4 tri_vindex = kernel_tex_fetch(__tri_vindex, sd->prim); | ||||
| const float3 v0 = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 0)); | const float3 v0 = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 0)); | ||||
| const float3 v1 = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 1)); | const float3 v1 = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 1)); | ||||
| const float3 v2 = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 2)); | const float3 v2 = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 2)); | ||||
| /* return normal */ | /* return normal */ | ||||
| if (sd->object_flag & SD_OBJECT_NEGATIVE_SCALE_APPLIED) { | if (sd->object_flag & SD_OBJECT_NEGATIVE_SCALE_APPLIED) { | ||||
| return normalize(cross(v2 - v0, v1 - v0)); | return normalize(cross(v2 - v0, v1 - v0)); | ||||
| } | } | ||||
| else { | else { | ||||
| return normalize(cross(v1 - v0, v2 - v0)); | return normalize(cross(v1 - v0, v2 - v0)); | ||||
| } | } | ||||
| } | } | ||||
| /* Point and normal on triangle. */ | /* Point and normal on triangle. */ | ||||
| ccl_device_inline void triangle_point_normal(ccl_global const KernelGlobals *kg, | ccl_device_inline void triangle_point_normal(KernelGlobals kg, | ||||
| int object, | int object, | ||||
| int prim, | int prim, | ||||
| float u, | float u, | ||||
| float v, | float v, | ||||
| ccl_private float3 *P, | ccl_private float3 *P, | ||||
| ccl_private float3 *Ng, | ccl_private float3 *Ng, | ||||
| ccl_private int *shader) | ccl_private int *shader) | ||||
| { | { | ||||
| Show All 15 Lines | else { | ||||
| *Ng = normalize(cross(v1 - v0, v2 - v0)); | *Ng = normalize(cross(v1 - v0, v2 - v0)); | ||||
| } | } | ||||
| /* shader`*/ | /* shader`*/ | ||||
| *shader = kernel_tex_fetch(__tri_shader, prim); | *shader = kernel_tex_fetch(__tri_shader, prim); | ||||
| } | } | ||||
| /* Triangle vertex locations */ | /* Triangle vertex locations */ | ||||
| ccl_device_inline void triangle_vertices(ccl_global const KernelGlobals *kg, int prim, float3 P[3]) | ccl_device_inline void triangle_vertices(KernelGlobals kg, int prim, float3 P[3]) | ||||
| { | { | ||||
| const uint4 tri_vindex = kernel_tex_fetch(__tri_vindex, prim); | const uint4 tri_vindex = kernel_tex_fetch(__tri_vindex, prim); | ||||
| P[0] = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 0)); | P[0] = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 0)); | ||||
| P[1] = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 1)); | P[1] = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 1)); | ||||
| P[2] = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 2)); | P[2] = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 2)); | ||||
| } | } | ||||
| /* Triangle vertex locations and vertex normals */ | /* Triangle vertex locations and vertex normals */ | ||||
| ccl_device_inline void triangle_vertices_and_normals(ccl_global const KernelGlobals *kg, | ccl_device_inline void triangle_vertices_and_normals(KernelGlobals kg, | ||||
| int prim, | int prim, | ||||
| float3 P[3], | float3 P[3], | ||||
| float3 N[3]) | float3 N[3]) | ||||
| { | { | ||||
| const uint4 tri_vindex = kernel_tex_fetch(__tri_vindex, prim); | const uint4 tri_vindex = kernel_tex_fetch(__tri_vindex, prim); | ||||
| P[0] = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 0)); | P[0] = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 0)); | ||||
| P[1] = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 1)); | P[1] = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 1)); | ||||
| P[2] = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 2)); | P[2] = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 2)); | ||||
| N[0] = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.x)); | N[0] = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.x)); | ||||
| N[1] = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.y)); | N[1] = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.y)); | ||||
| N[2] = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.z)); | N[2] = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.z)); | ||||
| } | } | ||||
| /* Interpolate smooth vertex normal from vertices */ | /* Interpolate smooth vertex normal from vertices */ | ||||
| ccl_device_inline float3 | ccl_device_inline float3 | ||||
| triangle_smooth_normal(ccl_global const KernelGlobals *kg, float3 Ng, int prim, float u, float v) | triangle_smooth_normal(KernelGlobals kg, float3 Ng, int prim, float u, float v) | ||||
| { | { | ||||
| /* load triangle vertices */ | /* load triangle vertices */ | ||||
| const uint4 tri_vindex = kernel_tex_fetch(__tri_vindex, prim); | const uint4 tri_vindex = kernel_tex_fetch(__tri_vindex, prim); | ||||
| float3 n0 = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.x)); | float3 n0 = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.x)); | ||||
| float3 n1 = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.y)); | float3 n1 = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.y)); | ||||
| float3 n2 = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.z)); | float3 n2 = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.z)); | ||||
| float3 N = safe_normalize((1.0f - u - v) * n2 + u * n0 + v * n1); | float3 N = safe_normalize((1.0f - u - v) * n2 + u * n0 + v * n1); | ||||
| return is_zero(N) ? Ng : N; | return is_zero(N) ? Ng : N; | ||||
| } | } | ||||
| ccl_device_inline float3 triangle_smooth_normal_unnormalized(ccl_global const KernelGlobals *kg, | ccl_device_inline float3 triangle_smooth_normal_unnormalized( | ||||
| ccl_private const ShaderData *sd, | KernelGlobals kg, ccl_private const ShaderData *sd, float3 Ng, int prim, float u, float v) | ||||
| float3 Ng, | |||||
| int prim, | |||||
| float u, | |||||
| float v) | |||||
| { | { | ||||
| /* load triangle vertices */ | /* load triangle vertices */ | ||||
| const uint4 tri_vindex = kernel_tex_fetch(__tri_vindex, prim); | const uint4 tri_vindex = kernel_tex_fetch(__tri_vindex, prim); | ||||
| float3 n0 = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.x)); | float3 n0 = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.x)); | ||||
| float3 n1 = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.y)); | float3 n1 = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.y)); | ||||
| float3 n2 = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.z)); | float3 n2 = float4_to_float3(kernel_tex_fetch(__tri_vnormal, tri_vindex.z)); | ||||
| /* ensure that the normals are in object space */ | /* ensure that the normals are in object space */ | ||||
| if (sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED) { | if (sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED) { | ||||
| object_inverse_normal_transform(kg, sd, &n0); | object_inverse_normal_transform(kg, sd, &n0); | ||||
| object_inverse_normal_transform(kg, sd, &n1); | object_inverse_normal_transform(kg, sd, &n1); | ||||
| object_inverse_normal_transform(kg, sd, &n2); | object_inverse_normal_transform(kg, sd, &n2); | ||||
| } | } | ||||
| float3 N = (1.0f - u - v) * n2 + u * n0 + v * n1; | float3 N = (1.0f - u - v) * n2 + u * n0 + v * n1; | ||||
| return is_zero(N) ? Ng : N; | return is_zero(N) ? Ng : N; | ||||
| } | } | ||||
| /* Ray differentials on triangle */ | /* Ray differentials on triangle */ | ||||
| ccl_device_inline void triangle_dPdudv(ccl_global const KernelGlobals *kg, | ccl_device_inline void triangle_dPdudv(KernelGlobals kg, | ||||
| int prim, | int prim, | ||||
| ccl_private float3 *dPdu, | ccl_private float3 *dPdu, | ||||
| ccl_private float3 *dPdv) | ccl_private float3 *dPdv) | ||||
| { | { | ||||
| /* fetch triangle vertex coordinates */ | /* fetch triangle vertex coordinates */ | ||||
| const uint4 tri_vindex = kernel_tex_fetch(__tri_vindex, prim); | const uint4 tri_vindex = kernel_tex_fetch(__tri_vindex, prim); | ||||
| const float3 p0 = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 0)); | const float3 p0 = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 0)); | ||||
| const float3 p1 = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 1)); | const float3 p1 = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 1)); | ||||
| const float3 p2 = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 2)); | const float3 p2 = float4_to_float3(kernel_tex_fetch(__tri_verts, tri_vindex.w + 2)); | ||||
| /* compute derivatives of P w.r.t. uv */ | /* compute derivatives of P w.r.t. uv */ | ||||
| *dPdu = (p0 - p2); | *dPdu = (p0 - p2); | ||||
| *dPdv = (p1 - p2); | *dPdv = (p1 - p2); | ||||
| } | } | ||||
| /* Reading attributes on various triangle elements */ | /* Reading attributes on various triangle elements */ | ||||
| ccl_device float triangle_attribute_float(ccl_global const KernelGlobals *kg, | ccl_device float triangle_attribute_float(KernelGlobals kg, | ||||
| ccl_private const ShaderData *sd, | ccl_private const ShaderData *sd, | ||||
| const AttributeDescriptor desc, | const AttributeDescriptor desc, | ||||
| ccl_private float *dx, | ccl_private float *dx, | ||||
| ccl_private float *dy) | ccl_private float *dy) | ||||
| { | { | ||||
| if (desc.element & (ATTR_ELEMENT_VERTEX | ATTR_ELEMENT_VERTEX_MOTION | ATTR_ELEMENT_CORNER)) { | if (desc.element & (ATTR_ELEMENT_VERTEX | ATTR_ELEMENT_VERTEX_MOTION | ATTR_ELEMENT_CORNER)) { | ||||
| float f0, f1, f2; | float f0, f1, f2; | ||||
| Show All 33 Lines | if (desc.element & (ATTR_ELEMENT_FACE | ATTR_ELEMENT_OBJECT | ATTR_ELEMENT_MESH)) { | ||||
| return kernel_tex_fetch(__attributes_float, offset); | return kernel_tex_fetch(__attributes_float, offset); | ||||
| } | } | ||||
| else { | else { | ||||
| return 0.0f; | return 0.0f; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| ccl_device float2 triangle_attribute_float2(ccl_global const KernelGlobals *kg, | ccl_device float2 triangle_attribute_float2(KernelGlobals kg, | ||||
| ccl_private const ShaderData *sd, | ccl_private const ShaderData *sd, | ||||
| const AttributeDescriptor desc, | const AttributeDescriptor desc, | ||||
| ccl_private float2 *dx, | ccl_private float2 *dx, | ||||
| ccl_private float2 *dy) | ccl_private float2 *dy) | ||||
| { | { | ||||
| if (desc.element & (ATTR_ELEMENT_VERTEX | ATTR_ELEMENT_VERTEX_MOTION | ATTR_ELEMENT_CORNER)) { | if (desc.element & (ATTR_ELEMENT_VERTEX | ATTR_ELEMENT_VERTEX_MOTION | ATTR_ELEMENT_CORNER)) { | ||||
| float2 f0, f1, f2; | float2 f0, f1, f2; | ||||
| Show All 33 Lines | if (desc.element & (ATTR_ELEMENT_FACE | ATTR_ELEMENT_OBJECT | ATTR_ELEMENT_MESH)) { | ||||
| return kernel_tex_fetch(__attributes_float2, offset); | return kernel_tex_fetch(__attributes_float2, offset); | ||||
| } | } | ||||
| else { | else { | ||||
| return make_float2(0.0f, 0.0f); | return make_float2(0.0f, 0.0f); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| ccl_device float3 triangle_attribute_float3(ccl_global const KernelGlobals *kg, | ccl_device float3 triangle_attribute_float3(KernelGlobals kg, | ||||
| ccl_private const ShaderData *sd, | ccl_private const ShaderData *sd, | ||||
| const AttributeDescriptor desc, | const AttributeDescriptor desc, | ||||
| ccl_private float3 *dx, | ccl_private float3 *dx, | ||||
| ccl_private float3 *dy) | ccl_private float3 *dy) | ||||
| { | { | ||||
| if (desc.element & (ATTR_ELEMENT_VERTEX | ATTR_ELEMENT_VERTEX_MOTION | ATTR_ELEMENT_CORNER)) { | if (desc.element & (ATTR_ELEMENT_VERTEX | ATTR_ELEMENT_VERTEX_MOTION | ATTR_ELEMENT_CORNER)) { | ||||
| float3 f0, f1, f2; | float3 f0, f1, f2; | ||||
| Show All 33 Lines | if (desc.element & (ATTR_ELEMENT_FACE | ATTR_ELEMENT_OBJECT | ATTR_ELEMENT_MESH)) { | ||||
| return float4_to_float3(kernel_tex_fetch(__attributes_float3, offset)); | return float4_to_float3(kernel_tex_fetch(__attributes_float3, offset)); | ||||
| } | } | ||||
| else { | else { | ||||
| return make_float3(0.0f, 0.0f, 0.0f); | return make_float3(0.0f, 0.0f, 0.0f); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| ccl_device float4 triangle_attribute_float4(ccl_global const KernelGlobals *kg, | ccl_device float4 triangle_attribute_float4(KernelGlobals kg, | ||||
| ccl_private const ShaderData *sd, | ccl_private const ShaderData *sd, | ||||
| const AttributeDescriptor desc, | const AttributeDescriptor desc, | ||||
| ccl_private float4 *dx, | ccl_private float4 *dx, | ||||
| ccl_private float4 *dy) | ccl_private float4 *dy) | ||||
| { | { | ||||
| if (desc.element & (ATTR_ELEMENT_VERTEX | ATTR_ELEMENT_VERTEX_MOTION | ATTR_ELEMENT_CORNER | | if (desc.element & (ATTR_ELEMENT_VERTEX | ATTR_ELEMENT_VERTEX_MOTION | ATTR_ELEMENT_CORNER | | ||||
| ATTR_ELEMENT_CORNER_BYTE)) { | ATTR_ELEMENT_CORNER_BYTE)) { | ||||
| float4 f0, f1, f2; | float4 f0, f1, f2; | ||||
| ▲ Show 20 Lines • Show All 53 Lines • Show Last 20 Lines | |||||