Skip to content

Commit 185a3a8

Browse files
authored
Fix testrender OptiX build (#1869)
* Adapt OptixRaytracer to use triangle meshes. * Refactor the accel build and pipeline creation. * Remove the old quad & sphere code. * Change globals_from_hit to use the mesh data. Signed-off-by: Tim Grant <tgrant@nvidia.com> * Update the OptiX reference images. Also, fix the outdated output for test_str_ops, which has been broken since the change to string hashes. Signed-off-by: Tim Grant <tgrant@nvidia.com> * clang-format Signed-off-by: Tim Grant <tgrant@nvidia.com> * Fix build issue when using OptiX 7.0. Signed-off-by: Tim Grant <tgrant@nvidia.com> --------- Signed-off-by: Tim Grant <tgrant@nvidia.com>
1 parent 35a4ef8 commit 185a3a8

File tree

17 files changed

+592
-668
lines changed

17 files changed

+592
-668
lines changed

src/testrender/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ find_package(Threads REQUIRED)
1515
if (OSL_USE_OPTIX)
1616
list (APPEND testrender_srcs optixraytracer.cpp)
1717
set (testrender_cuda_srcs
18-
cuda/quad.cu
1918
cuda/optix_raytracer.cu
20-
cuda/sphere.cu
2119
cuda/wrapper.cu
2220
)
2321

@@ -28,11 +26,15 @@ if (OSL_USE_OPTIX)
2826
# We need to make sure that the PTX files are regenerated whenever these
2927
# headers change.
3028
set (testrender_cuda_headers
31-
cuda/rend_lib.h)
29+
cuda/rend_lib.h
30+
render_params.h)
31+
32+
set ( extra_cuda_headers
33+
render_params.h )
3234

3335
# Generate PTX for all of the CUDA files
3436
foreach (cudasrc ${testrender_cuda_srcs})
35-
NVCC_COMPILE ( ${cudasrc} "" ptx_generated "" )
37+
NVCC_COMPILE ( ${cudasrc} ${extra_cuda_headers} ptx_generated "" )
3638
list (APPEND ptx_list ${ptx_generated})
3739
endforeach ()
3840

src/testrender/cuda/optix_raytracer.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
#include <OSL/hashes.h>
1313

14+
#include "../render_params.h"
1415
#include "rend_lib.h"
15-
#include "render_params.h"
1616

1717

1818
OSL_NAMESPACE_ENTER

src/testrender/cuda/quad.cu

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/testrender/cuda/sphere.cu

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/testrender/cuda/wrapper.cu

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,84 @@ __anyhit__any_hit_shadow()
3232
static __device__ void
3333
globals_from_hit(ShaderGlobals& sg)
3434
{
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);
4435
// Setup the ShaderGlobals
36+
const int primID = optixGetPrimitiveIndex();
4537
const float3 ray_direction = optixGetWorldRayDirection();
4638
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;
57109
sg.Ci = NULL;
58-
sg.surfacearea = local_sg.surfacearea;
110+
sg.surfacearea = surfacearea[mesh_ids[primID]];
59111
sg.backfacing = dot(sg.N, sg.I) > 0.0f;
60-
sg.shaderID = local_sg.shaderID;
112+
sg.shaderID = shader_id;
61113

62114
if (sg.backfacing) {
63115
sg.N = -sg.N;
@@ -183,7 +235,7 @@ __closesthit__closest_hit_osl()
183235
// Run the OSL callable
184236
void* interactive_ptr = reinterpret_cast<void**>(
185237
render_params.interactive_params)[sg.shaderID];
186-
const unsigned int shaderIdx = 2u + sg.shaderID + 0u;
238+
const unsigned int shaderIdx = sg.shaderID + 0u;
187239
optixDirectCall<void, ShaderGlobals*, void*, void*, void*, int, void*>(
188240
shaderIdx, &sg /*shaderglobals_ptr*/, nullptr /*groupdata_ptr*/,
189241
nullptr /*userdata_base_ptr*/, nullptr /*output_base_ptr*/,

0 commit comments

Comments
 (0)