NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Buffer.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 "io/Stream.hpp"
30 
31 namespace FW
32 {
33 //------------------------------------------------------------------------
34 
35 class Buffer : public Serializable
36 {
37 public:
38  enum Module
39  {
40  CPU = 1 << 0,
41  GL = 1 << 1,
42  Cuda = 1 << 2,
43 
45  Module_All = (1 << 3) - 1
46  };
47 
48  enum Hint
49  {
50  Hint_PageLock = 1 << 0,
51  Hint_CudaGL = 1 << 1,
52 
53  Hint_None = 0,
54  Hint_All = (1 << 2) - 1
55  };
56 
57 public:
58  explicit Buffer (U32 hints = Hint_None) { init(0, hints, 1); }
59  explicit Buffer (const void* ptr, S64 size, U32 hints = Hint_None, int align = 1) { init(size, hints, align); if (ptr) set(ptr); }
60  template <class T> explicit Buffer (const Array<T>& data, U32 hints = Hint_None, int align = 1) { init(data.getNumBytes(), hints, align); set(data.getPtr()); }
61  template <class T> explicit Buffer (const Array<T>& data, int start, int end, U32 hints = Hint_None, int align = 1) { init((end - start) * sizeof(T), hints, align); set(data.getPtr(start)); }
62  Buffer (Buffer& other) { init(other.getSize(), other.getHints(), other.getAlign()); setRange(0, other, 0, other.getSize()); }
63  virtual ~Buffer (void) { deinit(); }
64 
65  void wrapCPU (void* cpuPtr, S64 size);
66  void wrapGL (GLuint glBuffer);
67  void wrapCuda (CUdeviceptr cudaPtr, S64 size);
68 
69  S64 getSize (void) const { return m_size; }
70  U32 getHints (void) const { return m_hints; }
71  int getAlign (void) const { return m_align; }
72  void setHintsAndAlign (U32 hints, int align) { realloc(m_size, validateHints(hints, align, m_original), align); }
73  void setHints (U32 hints) { setHintsAndAlign(hints, m_align); }
74  void setAlign (int align) { setHintsAndAlign(m_hints, align); }
75 
76  void reset (U32 hints, int align) { deinit(); init(0, hints, align); }
77  void reset (U32 hints) { reset(hints, m_align); }
78  void reset (void) { reset(m_hints, m_align); }
79  void reset (const void* ptr, S64 size, U32 hints, int align) { deinit(); init(size, hints, align); if (ptr) setRange(0, ptr, size); }
80  void reset (const void* ptr, S64 size, U32 hints) { reset(ptr, size, hints, m_align); }
81  void reset (const void* ptr, S64 size) { reset(ptr, size, m_hints, m_align); }
82  void resize (S64 size) { realloc(size, m_hints, m_align); }
83  void resizeDiscard (S64 size) { if (m_size != size) reset(NULL, size, m_hints, m_align); }
84  void free (Module module);
85 
86  void getRange (void* dst, S64 srcOfs, S64 size, bool async = false, CUstream cudaStream = NULL) const;
87  void get (void* ptr) { getRange(ptr, 0, getSize()); }
88  template <class T> void get (Array<T>& data) { FW_ASSERT(data.getNumBytes() == getSize()); get(data.getPtr()); }
89 
90  void setRange (S64 dstOfs, const void* src, S64 size, bool async = false, CUstream cudaStream = NULL);
91  void setRange (S64 dstOfs, Buffer& src, S64 srcOfs, S64 size, bool async = false, CUstream cudaStream = NULL);
92  void set (const void* ptr) { setRange(0, ptr, getSize()); }
93 
94  void set (const void* ptr, S64 size) { resizeDiscard(size); setRange(0, ptr, size); }
95  void set (Buffer& other) { if (&other != this) { resizeDiscard(other.getSize()); setRange(0, other, 0, other.getSize()); } }
96  template <class T> void set (const Array<T>& data) { set(data.getPtr(), data.getNumBytes()); }
97  template <class T> void set (const Array<T>& data, int start, int end) { set(data.getPtr(start), (end - start) * sizeof(T)); }
98 
99  void clearRange (S64 dstOfs, int value, S64 size, bool async = false, CUstream cudaStream = NULL);
100  void clear (int value = 0) { clearRange(0, value, m_size); }
101 
102  void setOwner (Module module, bool modify, bool async = false, CUstream cudaStream = NULL, S64 validSize = -1);
103  Module getOwner (void) const { return m_owner; }
104  void discard (void) { m_dirty = 0; }
105 
106  const U8* getPtr (S64 ofs = 0) { FW_ASSERT(ofs >= 0 && ofs <= m_size); setOwner(CPU, false); return m_cpuPtr + ofs; }
107  GLuint getGLBuffer (void) { setOwner(GL, false); return m_glBuffer; }
108  CUdeviceptr getCudaPtr (S64 ofs = 0) { FW_ASSERT(ofs >= 0 && ofs <= m_size); setOwner(Cuda, false); return m_cudaPtr + (U32)ofs; }
109 
110  U8* getMutablePtr (S64 ofs = 0) { FW_ASSERT(ofs >= 0 && ofs <= m_size); setOwner(CPU, true); return m_cpuPtr + ofs; }
111  GLuint getMutableGLBuffer (void) { setOwner(GL, true); return m_glBuffer; }
112  CUdeviceptr getMutableCudaPtr (S64 ofs = 0) { FW_ASSERT(ofs >= 0 && ofs <= m_size); setOwner(Cuda, true); return m_cudaPtr + (U32)ofs; }
113 
114  U8* getMutablePtrDiscard(S64 ofs = 0) { discard(); return getMutablePtr(ofs); }
116  CUdeviceptr getMutableCudaPtrDiscard(S64 ofs = 0) { discard(); return getMutableCudaPtr(ofs); }
117 
118  Buffer& operator= (Buffer& other) { set(other); return *this; }
119  U8 operator[] (S64 idx) { FW_ASSERT(idx < m_size); return *getPtr(idx); }
120 
121  virtual void readFromStream (InputStream& s);
122  virtual void writeToStream (OutputStream& s) const;
123 
124  static void memcpyHtoD (CUdeviceptr dst, const void* src, S64 size, bool async = false, CUstream cudaStream = NULL) { memcpyXtoX(NULL, dst, src, NULL, size, async, cudaStream); }
125  static void memcpyDtoH (void* dst, CUdeviceptr src, S64 size, bool async = false, CUstream cudaStream = NULL) { memcpyXtoX(dst, NULL, NULL, src, size, async, cudaStream); }
126  static void memcpyDtoD (CUdeviceptr dst, CUdeviceptr src, S64 size, bool async = false, CUstream cudaStream = NULL) { memcpyXtoX(NULL, dst, NULL, src, size, async, cudaStream); }
127 
128 private:
129  static U32 validateHints (U32 hints, int align, Module original);
130 
131  void init (S64 size, U32 hints, int align);
132  void deinit (void);
133  void wrap (Module module, S64 size);
134  void realloc (S64 size, U32 hints, int align);
135  void validateCPU (bool async, CUstream cudaStream, S64 validSize);
136 
137  static void cpuAlloc (U8*& cpuPtr, U8*& cpuBase, S64 size, U32 hints, int align);
138  static void cpuFree (U8*& cpuPtr, U8*& cpuBase, U32 hints);
139  static void glAlloc (GLuint& glBuffer, S64 size, const void* data);
140  static void glFree (GLuint& glBuffer, bool& cudaGLReg);
141  static void cudaAlloc (CUdeviceptr& cudaPtr, CUdeviceptr& cudaBase, bool& cudaGLReg, S64 size, GLuint glBuffer, U32 hints, int align);
142  static void cudaFree (CUdeviceptr& cudaPtr, CUdeviceptr& cudaBase, GLuint glBuffer, U32 hints);
143 
144  static void checkSize (S64 size, int bits, const String& funcName);
145 
146  static void memcpyXtoX (void* dstHost, CUdeviceptr dstDevice, const void* srcHost, CUdeviceptr srcDevice, S64 size, bool async, CUstream cudaStream);
147 
148 private:
149  U32 m_hints;
150  S32 m_align;
151  S64 m_size;
152  Module m_original;
153  Module m_owner;
154  U32 m_exists;
155  U32 m_dirty;
156 
157  U8* m_cpuPtr;
158  U8* m_cpuBase;
159  GLuint m_glBuffer;
160  CUdeviceptr m_cudaPtr;
161  CUdeviceptr m_cudaBase;
162  bool m_cudaGLReg;
163 };
164 
165 //------------------------------------------------------------------------
166 }
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 * srcHost
Definition: DLLImports.inl:128
#define NULL
Definition: Defs.hpp:39
void set(const void *ptr)
Definition: Buffer.hpp:92
void setRange(S64 dstOfs, const void *src, S64 size, bool async=false, CUstream cudaStream=NULL)
Definition: Buffer.cpp:149
GLuint getMutableGLBuffer(void)
Definition: Buffer.hpp:111
Buffer(const Array< T > &data, U32 hints=Hint_None, int align=1)
Definition: Buffer.hpp:60
Buffer(const Array< T > &data, int start, int end, U32 hints=Hint_None, int align=1)
Definition: Buffer.hpp:61
void reset(const void *ptr, S64 size)
Definition: Buffer.hpp:81
virtual void writeToStream(OutputStream &s) const
Definition: Buffer.cpp:350
void clear(int value=0)
Definition: Buffer.hpp:100
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 dstDevice
Definition: DLLImports.inl:128
void reset(const void *ptr, S64 size, U32 hints)
Definition: Buffer.hpp:80
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule * module
Definition: DLLImports.inl:60
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 * dstHost
Definition: DLLImports.inl:116
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 * data
Definition: DLLImports.inl:319
int getAlign(void) const
Definition: Buffer.hpp:71
void set(Buffer &other)
Definition: Buffer.hpp:95
void ** ptr
Definition: DLLImports.cpp:74
U8 * getMutablePtrDiscard(S64 ofs=0)
Definition: Buffer.hpp:114
void wrapCuda(CUdeviceptr cudaPtr, S64 size)
Definition: Buffer.cpp:70
virtual void readFromStream(InputStream &s)
Definition: Buffer.cpp:331
CUdeviceptr getCudaPtr(S64 ofs=0)
Definition: Buffer.hpp:108
void reset(U32 hints)
Definition: Buffer.hpp:77
void set(const Array< T > &data)
Definition: Buffer.hpp:96
S64 getSize(void) const
Definition: Buffer.hpp:69
void discard(void)
Definition: Buffer.hpp:104
const U8 * getPtr(S64 ofs=0)
Definition: Buffer.hpp:106
void setOwner(Module module, bool modify, bool async=false, CUstream cudaStream=NULL, S64 validSize=-1)
Definition: Buffer.cpp:220
void set(const Array< T > &data, int start, int end)
Definition: Buffer.hpp:97
S32 getNumBytes(void) const
void reset(const void *ptr, S64 size, U32 hints, int align)
Definition: Buffer.hpp:79
void wrapCPU(void *cpuPtr, S64 size)
Definition: Buffer.cpp:39
Buffer(U32 hints=Hint_None)
Definition: Buffer.hpp:58
U32 getHints(void) const
Definition: Buffer.hpp:70
static void memcpyHtoD(CUdeviceptr dst, const void *src, S64 size, bool async=false, CUstream cudaStream=NULL)
Definition: Buffer.hpp:124
CUdeviceptr getMutableCudaPtr(S64 ofs=0)
Definition: Buffer.hpp:112
void setHints(U32 hints)
Definition: Buffer.hpp:73
virtual ~Buffer(void)
Definition: Buffer.hpp:63
U8 * getMutablePtr(S64 ofs=0)
Definition: Buffer.hpp:110
void set(const void *ptr, S64 size)
Definition: Buffer.hpp:94
void free(Module module)
Definition: Buffer.cpp:80
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
void setAlign(int align)
Definition: Buffer.hpp:74
void getRange(void *dst, S64 srcOfs, S64 size, bool async=false, CUstream cudaStream=NULL) const
Definition: Buffer.cpp:114
signed __int64 S64
Definition: Defs.hpp:98
unsigned int U32
Definition: Defs.hpp:85
void wrapGL(GLuint glBuffer)
Definition: Buffer.cpp:50
static void memcpyDtoD(CUdeviceptr dst, CUdeviceptr src, S64 size, bool async=false, CUstream cudaStream=NULL)
Definition: Buffer.hpp:126
void clearRange(S64 dstOfs, int value, S64 size, bool async=false, CUstream cudaStream=NULL)
Definition: Buffer.cpp:202
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
Definition: DLLImports.inl:84
Module getOwner(void) const
Definition: Buffer.hpp:103
GLuint getMutableGLBufferDiscard(void)
Definition: Buffer.hpp:115
static void memcpyDtoH(void *dst, CUdeviceptr src, S64 size, bool async=false, CUstream cudaStream=NULL)
Definition: Buffer.hpp:125
void reset(void)
Definition: Buffer.hpp:78
unsigned char U8
Definition: Defs.hpp:83
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 srcDevice
Definition: DLLImports.inl:116
Buffer(Buffer &other)
Definition: Buffer.hpp:62
Buffer & operator=(Buffer &other)
Definition: Buffer.hpp:118
void setHintsAndAlign(U32 hints, int align)
Definition: Buffer.hpp:72
GLuint getGLBuffer(void)
Definition: Buffer.hpp:107
const T * getPtr(S32idx=0) const
U8 operator[](S64 idx)
Definition: Buffer.hpp:119
void resizeDiscard(S64 size)
Definition: Buffer.hpp:83
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 size
Definition: DLLImports.inl:319
void resize(S64 size)
Definition: Buffer.hpp:82
void reset(U32 hints, int align)
Definition: Buffer.hpp:76
CUdeviceptr getMutableCudaPtrDiscard(S64 ofs=0)
Definition: Buffer.hpp:116
Buffer(const void *ptr, S64 size, U32 hints=Hint_None, int align=1)
Definition: Buffer.hpp:59