NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
RayBuffer.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 "gpu/Buffer.hpp"
30 #include "Util.hpp"
31 
32 namespace FW
33 {
34 
38 class RayBuffer
39 {
40 public:
46  RayBuffer (S32 n=0, bool closestHit=true) : m_size(0), m_needClosestHit(closestHit) { resize(n); }
47 
52  S32 getSize () const { return m_size; }
53 
58  void resize (S32 n);
59 
65  void setRay (S32 slot, const Ray& ray) { setRay(slot, ray, slot); }
66 
73  void setRay (S32 slot, const Ray& ray, S32 id);
74 
80  void setResult (S32 slot, const RayResult& r) { getMutableResultForSlot(slot) = r; }
81 
82 // const Ray& operator[] (S32 slot) const { return getRayForSlot(slot); }
83 
89  const Ray& getRayForSlot (S32 slot) const { FW_ASSERT(slot >= 0 && slot < m_size); return ((const Ray*)m_rays.getPtr())[slot]; }
90 
96  const Ray& getRayForID (S32 id) const { return getRayForSlot(getSlotForID(id)); }
97 
103  const RayResult& getResultForSlot (S32 slot) const { FW_ASSERT(slot >= 0 && slot < m_size); return ((const RayResult*)m_results.getPtr())[slot]; }
104 
110  RayResult& getMutableResultForSlot (S32 slot) { FW_ASSERT(slot >= 0 && slot < m_size); return ((RayResult*)m_results.getMutablePtr())[slot]; }
111 
117  const RayResult& getResultForID (S32 id) const { return getResultForSlot(getSlotForID(id)); }
118 
125 
131  S32 getSlotForID (S32 id) const { FW_ASSERT(id >= 0 && id < m_size); return ((const S32*)m_IDToSlot.getPtr())[id]; }
132 
138  S32 getIDForSlot (S32 slot) const { FW_ASSERT(slot >= 0 && slot < m_size); return ((const S32*)m_slotToID.getPtr())[slot]; }
139 
144  void setNeedClosestHit (bool c) { m_needClosestHit = c; }
145 
150  bool getNeedClosestHit () const { return m_needClosestHit; }
151 
155  void mortonSort ();
156 
161  void randomSort (U32 randomSeed=0);
162 
167  Buffer& getRayBuffer () { return m_rays; }
168 
173  Buffer& getResultBuffer () { return m_results; }
174 
179  Buffer& getIDToSlotBuffer () { return m_IDToSlot; }
180 
185  Buffer& getSlotToIDBuffer () { return m_slotToID; }
186 
187 private:
188  S32 m_size;
189  mutable Buffer m_rays;
190  mutable Buffer m_results;
191  mutable Buffer m_IDToSlot;
192  mutable Buffer m_slotToID;
193 
194  bool m_needClosestHit;
195 };
196 
197 } //
S32 getSize() const
Gets size of the buffer (number of rays).
Definition: RayBuffer.hpp:52
RayBuffer(S32 n=0, bool closestHit=true)
Constructor.
Definition: RayBuffer.hpp:46
void setResult(S32 slot, const RayResult &r)
Assigns ray result to a slot.
Definition: RayBuffer.hpp:80
S32 getIDForSlot(S32 slot) const
Gets an id for a given ray slot.
Definition: RayBuffer.hpp:138
Buffer & getIDToSlotBuffer()
Gets buffer mapping ids to slots.
Definition: RayBuffer.hpp:179
S32 getSlotForID(S32 id) const
Gets a ray slot for a given id.
Definition: RayBuffer.hpp:131
RayResult & getMutableResultForID(S32 id)
Gets a mutable ray result with a given id.
Definition: RayBuffer.hpp:124
Definition: Util.hpp:62
const Ray & getRayForSlot(S32 slot) const
Gets a ray assigned to a given slot.
Definition: RayBuffer.hpp:89
const U8 * getPtr(S64 ofs=0)
Definition: Buffer.hpp:106
Buffer & getRayBuffer()
Gets ray buffer.
Definition: RayBuffer.hpp:167
bool getNeedClosestHit() const
Returns whether the closest hit is needed.
Definition: RayBuffer.hpp:150
Buffer & getSlotToIDBuffer()
Gets buffer slots to ids.
Definition: RayBuffer.hpp:185
Buffer & getResultBuffer()
Gets ray result buffer.
Definition: RayBuffer.hpp:173
Ray buffer class. Stores rays.
Definition: RayBuffer.hpp:38
U8 * getMutablePtr(S64 ofs=0)
Definition: Buffer.hpp:110
void randomSort(U32 randomSeed=0)
Shuffles rays in the buffer.
Definition: RayBuffer.cpp:66
const Ray & getRayForID(S32 id) const
Gets a ray with a given id.
Definition: RayBuffer.hpp:96
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
const RayResult & getResultForID(S32 id) const
Gets a ray result with a given id.
Definition: RayBuffer.hpp:117
unsigned int U32
Definition: Defs.hpp:85
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 n
Definition: DLLImports.inl:325
const RayResult & getResultForSlot(S32 slot) const
Gets a ray result assigned to a given slot.
Definition: RayBuffer.hpp:103
void setRay(S32 slot, const Ray &ray)
Assigns ray to a slot. Id is same as slot.
Definition: RayBuffer.hpp:65
void resize(S32 n)
Resizes the buffer.
Definition: RayBuffer.cpp:38
void setNeedClosestHit(bool c)
Sets whether the closet hit is needed.
Definition: RayBuffer.hpp:144
RayResult & getMutableResultForSlot(S32 slot)
Gets a mutable ray assigned to a given slot.
Definition: RayBuffer.hpp:110
void mortonSort()
Performs morton sort.
Definition: RayBuffer.cpp:103