@@ -32,32 +32,84 @@ __anyhit__any_hit_shadow()
32
32
static __device__ void
33
33
globals_from_hit (ShaderGlobals& sg)
34
34
{
35
- const GenericRecord* record = reinterpret_cast <GenericRecord*>(
36
- optixGetSbtDataPointer ());
37
-
38
- ShaderGlobals local_sg;
39
- // hit-kind 0: quad hit
40
- // 1: sphere hit
41
- optixDirectCall<void , unsigned int , float , float3 , float3 , ShaderGlobals*>(
42
- optixGetHitKind (), optixGetPrimitiveIndex (), optixGetRayTmax (),
43
- optixGetWorldRayOrigin (), optixGetWorldRayDirection (), &local_sg);
44
35
// Setup the ShaderGlobals
36
+ const int primID = optixGetPrimitiveIndex ();
45
37
const float3 ray_direction = optixGetWorldRayDirection ();
46
38
const float3 ray_origin = optixGetWorldRayOrigin ();
47
- const float t_hit = optixGetRayTmin ();
48
-
49
- sg.I = ray_direction;
50
- sg.N = normalize (optixTransformNormalFromObjectToWorldSpace (local_sg.N ));
51
- sg.Ng = normalize (optixTransformNormalFromObjectToWorldSpace (local_sg.Ng ));
52
- sg.P = ray_origin + t_hit * ray_direction;
53
- sg.dPdu = local_sg.dPdu ;
54
- sg.dPdv = local_sg.dPdv ;
55
- sg.u = local_sg.u ;
56
- sg.v = local_sg.v ;
39
+ const float t_hit = optixGetRayTmax ();
40
+ const int shader_id = reinterpret_cast <int *>(
41
+ render_params.shader_ids )[primID];
42
+
43
+ const OSL::Vec3* verts = reinterpret_cast <const OSL::Vec3*>(
44
+ render_params.verts );
45
+ const OSL::Vec3* normals = reinterpret_cast <const OSL::Vec3*>(
46
+ render_params.normals );
47
+ const OSL::Vec2* uvs = reinterpret_cast <const OSL::Vec2*>(
48
+ render_params.uvs );
49
+ const int3 * triangles = reinterpret_cast <const int3 *>(
50
+ render_params.triangles );
51
+ const int3 * n_triangles = reinterpret_cast <const int3 *>(
52
+ render_params.normal_indices );
53
+ const int3 * uv_triangles = reinterpret_cast <const int3 *>(
54
+ render_params.uv_indices );
55
+ const int * mesh_ids = reinterpret_cast <const int *>(render_params.mesh_ids );
56
+ const float * surfacearea = reinterpret_cast <const float *>(
57
+ render_params.surfacearea );
58
+
59
+ // Calculate UV and its derivatives
60
+ const float2 barycentrics = optixGetTriangleBarycentrics ();
61
+ const float b1 = barycentrics.x ;
62
+ const float b2 = barycentrics.y ;
63
+ const float b0 = 1 .0f - (b1 + b2);
64
+
65
+ const OSL::Vec2 ta = uvs[uv_triangles[primID].x ];
66
+ const OSL::Vec2 tb = uvs[uv_triangles[primID].y ];
67
+ const OSL::Vec2 tc = uvs[uv_triangles[primID].z ];
68
+ const OSL::Vec2 uv = b0 * ta + b1 * tb + b2 * tc;
69
+ const float u = uv.x ;
70
+ const float v = uv.y ;
71
+
72
+ const OSL::Vec3 va = verts[triangles[primID].x ];
73
+ const OSL::Vec3 vb = verts[triangles[primID].y ];
74
+ const OSL::Vec3 vc = verts[triangles[primID].z ];
75
+
76
+ const OSL::Vec2 dt02 = ta - tc, dt12 = tb - tc;
77
+ const OSL::Vec3 dp02 = va - vc, dp12 = vb - vc;
78
+
79
+ OSL::Vec3 dPdu, dPdv;
80
+ const float det = dt02.x * dt12.y - dt02.y * dt12.x ;
81
+ if (det != 0 .0f ) {
82
+ float invdet = 1 .0f / det;
83
+ dPdu = (dt12.y * dp02 - dt02.y * dp12) * invdet;
84
+ dPdv = (-dt12.x * dp02 + dt02.x * dp12) * invdet;
85
+ }
86
+
87
+ // Calculate the normals
88
+ const OSL::Vec3 Ng = (va - vb).cross (va - vc).normalize ();
89
+ OSL::Vec3 N;
90
+ if (n_triangles[primID].x < 0 .0f ) {
91
+ N = Ng;
92
+ } else {
93
+ const OSL::Vec3 na = normals[n_triangles[primID].x ];
94
+ const OSL::Vec3 nb = normals[n_triangles[primID].y ];
95
+ const OSL::Vec3 nc = normals[n_triangles[primID].z ];
96
+ N = ((1 - u - v) * na + u * nb + v * nc).normalize ();
97
+ }
98
+
99
+ sg.I = ray_direction;
100
+ sg.N = normalize (
101
+ optixTransformNormalFromObjectToWorldSpace (*(float3 *)(&N)));
102
+ sg.Ng = normalize (
103
+ optixTransformNormalFromObjectToWorldSpace (*(float3 *)(&Ng)));
104
+ sg.P = ray_origin + t_hit * ray_direction;
105
+ sg.dPdu = *(float3 *)(&dPdu);
106
+ sg.dPdv = *(float3 *)(&dPdv);
107
+ sg.u = u;
108
+ sg.v = v;
57
109
sg.Ci = NULL ;
58
- sg.surfacearea = local_sg. surfacearea ;
110
+ sg.surfacearea = surfacearea[mesh_ids[primID]] ;
59
111
sg.backfacing = dot (sg.N , sg.I ) > 0 .0f ;
60
- sg.shaderID = local_sg. shaderID ;
112
+ sg.shaderID = shader_id ;
61
113
62
114
if (sg.backfacing ) {
63
115
sg.N = -sg.N ;
@@ -183,7 +235,7 @@ __closesthit__closest_hit_osl()
183
235
// Run the OSL callable
184
236
void * interactive_ptr = reinterpret_cast <void **>(
185
237
render_params.interactive_params )[sg.shaderID ];
186
- const unsigned int shaderIdx = 2u + sg.shaderID + 0u ;
238
+ const unsigned int shaderIdx = sg.shaderID + 0u ;
187
239
optixDirectCall<void , ShaderGlobals*, void *, void *, void *, int , void *>(
188
240
shaderIdx, &sg /* shaderglobals_ptr*/ , nullptr /* groupdata_ptr*/ ,
189
241
nullptr /* userdata_base_ptr*/ , nullptr /* output_base_ptr*/ ,
0 commit comments