NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Util.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 #include "base/Math.hpp"
30 
31 namespace FW
32 {
33 //------------------------------------------------------------------------
34 
35 class AABB
36 {
37 public:
39  FW_CUDA_FUNC AABB (const Vec3f& mn, const Vec3f& mx) : m_mn(mn), m_mx(mx) {}
40 
41  FW_CUDA_FUNC void grow (const Vec3f& pt) { m_mn = m_mn.min(pt); m_mx = m_mx.max(pt); }
42  FW_CUDA_FUNC void grow (const AABB& aabb) { grow(aabb.m_mn); grow(aabb.m_mx); }
43  FW_CUDA_FUNC void intersect (const AABB& aabb) { m_mn = m_mn.max(aabb.m_mn); m_mx = m_mx.min(aabb.m_mx); }
44  FW_CUDA_FUNC float volume (void) const { if(!valid()) return 0.0f; return (m_mx.x-m_mn.x) * (m_mx.y-m_mn.y) * (m_mx.z-m_mn.z); }
45  FW_CUDA_FUNC float area (void) const { if(!valid()) return 0.0f; Vec3f d = m_mx - m_mn; return (d.x*d.y + d.y*d.z + d.z*d.x)*2.0f; }
46  FW_CUDA_FUNC bool valid (void) const { return m_mn.x<=m_mx.x && m_mn.y<=m_mx.y && m_mn.z<=m_mx.z; }
47  FW_CUDA_FUNC Vec3f midPoint (void) const { return (m_mn+m_mx)*0.5f; }
48  FW_CUDA_FUNC const Vec3f& min (void) const { return m_mn; }
49  FW_CUDA_FUNC const Vec3f& max (void) const { return m_mx; }
50  FW_CUDA_FUNC Vec3f& min (void) { return m_mn; }
51  FW_CUDA_FUNC Vec3f& max (void) { return m_mx; }
52 
53  FW_CUDA_FUNC AABB operator+ (const AABB& aabb) const { AABB u(*this); u.grow(aabb); return u; }
54 
55 private:
56  Vec3f m_mn;
57  Vec3f m_mx;
58 };
59 
60 //------------------------------------------------------------------------
61 
62 struct Ray
63 {
64  FW_CUDA_FUNC Ray (void) : origin(0.0f), tmin(0.0f), direction(0.0f), tmax(0.0f) {}
65  FW_CUDA_FUNC void degenerate (void) { tmax = tmin - 1.0f; }
66 
68  float tmin;
70  float tmax;
71 };
72 
73 //------------------------------------------------------------------------
74 
75 #define RAY_NO_HIT (-1)
76 
77 struct RayResult
78 {
79  FW_CUDA_FUNC RayResult (S32 ii = RAY_NO_HIT, float ti = 0.f) : id(ii), t(ti) {}
80  FW_CUDA_FUNC bool hit (void) const { return (id != RAY_NO_HIT); }
81  FW_CUDA_FUNC void clear (void) { id = RAY_NO_HIT; }
82 
84  float t;
87 };
88 
89 //------------------------------------------------------------------------
90 
91 namespace Intersect
92 {
93  Vec2f RayBox(const AABB& box, const Ray& ray);
94  Vec3f RayTriangle(const Vec3f& v0, const Vec3f& v1, const Vec3f& v2, const Ray& ray);
95  Vec3f RayTriangleWoop(const Vec4f& zpleq, const Vec4f& upleq, const Vec4f& vpleq, const Ray& ray);
96 }
97 
98 //------------------------------------------------------------------------
99 }
FW_CUDA_FUNC AABB(void)
Definition: Util.hpp:38
#define FW_F32_MAX
Definition: Defs.hpp:118
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1
Definition: DLLImports.inl:353
FW_CUDA_FUNC const Vec3f & max(void) const
Definition: Util.hpp:49
FW_CUDA_FUNC T max(void) const
Definition: Math.hpp:147
FW_CUDA_FUNC AABB operator+(const AABB &aabb) const
Definition: Util.hpp:53
float t
Definition: Util.hpp:84
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2
Definition: DLLImports.inl:355
FW_CUDA_FUNC bool hit(void) const
Definition: Util.hpp:80
Vec2f RayBox(const AABB &box, const Ray &ray)
Definition: Util.cpp:34
FW_CUDA_FUNC Ray(void)
Definition: Util.hpp:64
#define FW_CUDA_FUNC
Definition: Defs.hpp:74
Definition: Util.hpp:62
FW_CUDA_FUNC Vec3f & max(void)
Definition: Util.hpp:51
Vec3f origin
Definition: Util.hpp:67
FW_CUDA_FUNC AABB(const Vec3f &mn, const Vec3f &mx)
Definition: Util.hpp:39
Vec3f RayTriangle(const Vec3f &v0, const Vec3f &v1, const Vec3f &v2, const Ray &ray)
Definition: Util.cpp:50
FW_CUDA_FUNC Vec3f midPoint(void) const
Definition: Util.hpp:47
#define RAY_NO_HIT
Definition: Util.hpp:75
FW_CUDA_FUNC void degenerate(void)
Definition: Util.hpp:65
FW_CUDA_FUNC float area(void) const
Definition: Util.hpp:45
FW_CUDA_FUNC bool valid(void) const
Definition: Util.hpp:46
FW_CUDA_FUNC void grow(const AABB &aabb)
Definition: Util.hpp:42
Vec3f RayTriangleWoop(const Vec4f &zpleq, const Vec4f &upleq, const Vec4f &vpleq, const Ray &ray)
Definition: Util.cpp:99
Vec3f direction
Definition: Util.hpp:69
float tmax
Definition: Util.hpp:70
signed int S32
Definition: Defs.hpp:88
S32 padA
Definition: Util.hpp:85
FW_CUDA_FUNC void clear(void)
Definition: Util.hpp:81
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat v0
Definition: DLLImports.inl:353
FW_CUDA_FUNC const Vec3f & min(void) const
Definition: Util.hpp:48
float tmin
Definition: Util.hpp:68
FW_CUDA_FUNC RayResult(S32 ii=RAY_NO_HIT, float ti=0.f)
Definition: Util.hpp:79
FW_CUDA_FUNC float volume(void) const
Definition: Util.hpp:44
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction f
Definition: DLLImports.inl:88
FW_CUDA_FUNC void grow(const Vec3f &pt)
Definition: Util.hpp:41
S32 padB
Definition: Util.hpp:86
FW_CUDA_FUNC Vec3f & min(void)
Definition: Util.hpp:50
FW_CUDA_FUNC void intersect(const AABB &aabb)
Definition: Util.hpp:43
FW_CUDA_FUNC T min(void) const
Definition: Math.hpp:146