19#include "owl/common/math/vec.h"
20#include "owl/common/math/box.h"
21#include "owl/common/math/AffineSpace.h"
25#include <vector_types.h>
32# error "this file should only ever get included on the device side"
37 using namespace owl::common;
39 inline __device__ vec2i getLaunchIndex()
41 return (vec2i)optixGetLaunchIndex();
46 inline __device__ vec2i getLaunchDims()
48 return (vec2i)optixGetLaunchDimensions();
55 inline __device__
const void *getProgramDataPointer()
57 return (
const void*)optixGetSbtDataPointer();
65 inline __device__
const T &getProgramData()
67 return *(
const T*)getProgramDataPointer();
74 inline __device__
float linear_to_srgb(
float x) {
75 if (x <= 0.0031308f) {
78 return 1.055f * pow(x, 1.f/2.4f) - 0.055f;
81 inline __device__ uint32_t make_8bit(
const float f)
83 return min(255,max(0,
int(f*256.f)));
86 inline __device__ uint32_t make_rgba(
const vec3f color)
89 (make_8bit(color.x) << 0) +
90 (make_8bit(color.y) << 8) +
91 (make_8bit(color.z) << 16) +
94 inline __device__ uint32_t make_rgba(
const vec4f color)
97 (make_8bit(color.x) << 0) +
98 (make_8bit(color.y) << 8) +
99 (make_8bit(color.z) << 16) +
100 (make_8bit(color.w) << 24);
103 inline __device__
void initializeTransformToIdentity(
float (&t)[12]) {
104 t[0] = 1.f; t[1] = 0.f; t[2] = 0.f; t[3] = 0.f;
105 t[4] = 0.f; t[5] = 1.f; t[6] = 0.f; t[7] = 0.f;
106 t[8] = 0.f; t[9] = 0.f; t[10] = 1.f; t[11] = 0.f;
109 inline __device__
void toRowMajor(
affine3f tfm,
float (&t)[12]) {
110 t[0] = tfm.l.vx.x; t[1] = tfm.l.vx.y; t[2] = tfm.l.vx.z; t[3] = tfm.p.x;
111 t[4] = tfm.l.vy.x; t[5] = tfm.l.vy.y; t[6] = tfm.l.vy.z; t[7] = tfm.p.y;
112 t[8] = tfm.l.vz.x; t[9] = tfm.l.vz.y; t[10] = tfm.l.vz.z; t[11] = tfm.p.z;
115 static __forceinline__ __device__
void* unpackPointer( uint32_t i0, uint32_t i1 )
117 const uint64_t uptr =
static_cast<uint64_t
>( i0 ) << 32 | i1;
118 void* ptr =
reinterpret_cast<void*
>( uptr );
123 static __forceinline__ __device__
void packPointer(
void* ptr, uint32_t& i0, uint32_t& i1 )
125 const uint64_t uptr =
reinterpret_cast<uint64_t
>( ptr );
127 i1 = uptr & 0x00000000ffffffff;
131 static __forceinline__ __device__
void *getPRDPointer()
133 const uint32_t u0 = optixGetPayload_0();
134 const uint32_t u1 = optixGetPayload_1();
135 return unpackPointer(u0, u1);
139 static __forceinline__ __device__ T &getPRD()
140 {
return *(T*)getPRDPointer(); }
142 template<
int _rayType=0,
int _numRayTypes=1,
bool _disablePerGeometrySBTRecords=0>
144 enum { rayType = _rayType };
145 enum { numRayTypes = _numRayTypes };
146 enum { disablePerGeometrySBTRecords = _disablePerGeometrySBTRecords };
147 inline __device__
RayT() {}
148 inline __device__
RayT(
const vec3f &origin,
149 const vec3f &direction,
152 OptixVisibilityMask visibilityMask=(OptixVisibilityMask)(-1))
154 direction(direction),
157 visibilityMask(visibilityMask)
160 vec3f origin, direction;
161 float tmin=0.f,tmax=1e30f,time=0.f;
162 OptixVisibilityMask visibilityMask=(OptixVisibilityMask)-1;
167 template<
typename RayType,
typename PRD>
169 void traceRay(OptixTraversableHandle traversable,
172 uint32_t rayFlags = 0u)
176 owl::packPointer(&prd,p0,p1);
178 optixTrace(traversable,
179 (
const float3&)ray.origin,
180 (
const float3&)ray.direction,
187 ray.numRayTypes * (ray.disablePerGeometrySBTRecords) ? 0 : 1,
193 template<
typename PRD>
195 void trace(OptixTraversableHandle traversable,
203 owl::packPointer(&prd,p0,p1);
205 optixTrace(traversable,
206 (
const float3&)ray.origin,
207 (
const float3&)ray.direction,
213 ray.rayType + numRayTypes*sbtOffset,
220 template<
typename PRD>
222 void trace(OptixTraversableHandle traversable,
225 bool disablePerGeometrySBTRecords,
231 owl::packPointer(&prd,p0,p1);
233 optixTrace(traversable,
234 (
const float3&)ray.origin,
235 (
const float3&)ray.direction,
241 ray.rayType + numRayTypes*sbtOffset,
242 numRayTypes * (ray.disablePerGeometrySBTRecords) ? 0 : 1,
250#define OPTIX_RAYGEN_PROGRAM(programName) \
251 extern "C" __global__ \
252 void __raygen__##programName
254#define OPTIX_CLOSEST_HIT_PROGRAM(programName) \
255 extern "C" __global__ \
256 void __closesthit__##programName
258#define OPTIX_ANY_HIT_PROGRAM(programName) \
259 extern "C" __global__ \
260 void __anyhit__##programName
262#define OPTIX_INTERSECT_PROGRAM(programName) \
263 extern "C" __global__ \
264 void __intersection__##programName
266#define OPTIX_MISS_PROGRAM(programName) \
267 extern "C" __global__ \
268 void __miss__##programName
272#ifndef OPTIX_BOUNDS_PROGRAM
273#define OPTIX_BOUNDS_PROGRAM(progName) \
276 void __boundsFunc__##progName(const void *geomData, \
277 owl::common::box3f &bounds, \
278 const int32_t primID); \
281 extern "C" __global__ \
282 void __boundsFuncKernel__##progName(const void *geomData, \
283 owl::common::box3f *const boundsArray, \
284 const uint32_t numPrims) \
286 uint32_t blockIndex \
288 + blockIdx.y * gridDim.x \
289 + blockIdx.z * gridDim.x * gridDim.y; \
291 = threadIdx.x + blockDim.x*threadIdx.y \
292 + blockDim.x*blockDim.y*blockIndex; \
293 if (primID < numPrims) { \
294 __boundsFunc__##progName(geomData,boundsArray[primID],primID); \
299 inline __device__ void __boundsFunc__##progName \
305#ifndef OPTIX_MOTION_BOUNDS_PROGRAM
306#define OPTIX_MOTION_BOUNDS_PROGRAM(progName) \
309 void __motionBoundsFunc__##progName(const void *geomData, \
310 owl::common::box3f &boundskey1, \
311 owl::common::box3f &boundskey2, \
312 const int32_t primID); \
315 extern "C" __global__ \
316 void __motionBoundsFuncKernel__##progName(const void *geomData, \
317 owl::common::box3f *const boundsArrayKey1, \
318 owl::common::box3f *const boundsArrayKey2, \
319 const uint32_t numPrims) \
321 uint32_t blockIndex \
323 + blockIdx.y * gridDim.x \
324 + blockIdx.z * gridDim.x * gridDim.y; \
326 = threadIdx.x + blockDim.x*threadIdx.y \
327 + blockDim.x*blockDim.y*blockIndex; \
328 if (primID < numPrims) { \
329 __motionBoundsFunc__##progName(geomData, \
330 boundsArrayKey1[primID], \
331 boundsArrayKey2[primID], \
337 inline __device__ void __motionBoundsFunc__##progName \
344#ifndef OPTIX_INSTANCE_PROGRAM
345#define OPTIX_INSTANCE_PROGRAM(progName) \
348 void __instanceFunc__##progName( \
349 const int32_t instanceIndex, OptixInstance &instance); \
352 extern "C" __global__ \
353 void __instanceFuncKernel__##progName( \
354 OptixInstance *insts, uint32_t numInsts, uint32_t numRayTypes) \
356 uint32_t blockIndex \
358 + blockIdx.y * gridDim.x \
359 + blockIdx.z * gridDim.x * gridDim.y; \
360 uint32_t instanceIndex \
361 = threadIdx.x + blockDim.x*threadIdx.y \
362 + blockDim.x*blockDim.y*blockIndex; \
363 if (instanceIndex < numInsts) { \
364 OptixInstance oi = {}; \
366 oi.flags = OPTIX_INSTANCE_FLAG_NONE; \
367 oi.instanceId = instanceIndex; \
368 oi.visibilityMask = 255; \
369 oi.traversableHandle = 0; \
370 initializeTransformToIdentity(oi.transform); \
371 __instanceFunc__##progName(instanceIndex, oi); \
372 insts[instanceIndex] = oi; \
377 inline __device__ void __instanceFunc__##progName \
Definition: owl_device.h:143
Definition: AffineSpace.h:52