SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLOptixCudaBuffer.h
Go to the documentation of this file.
1 /**
2  * \file SLOptixCudaBuffer.h
3  * \authors Nic Dorner
4  * \date October 2019
5  * \authors Nic Dorner
6  * \copyright http://opensource.org/licenses/GPL-3.0
7  * \remarks Please use clangformat to format the code. See more code style on
8  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
9 */
10 
11 #ifdef SL_HAS_OPTIX
12 # ifndef SLCUDABUFFER_H
13 # define SLCUDABUFFER_H
14 # include <cstdio>
15 # include <cassert>
16 # include <vector>
17 # include <cuda.h>
18 # include <driver_types.h>
19 # include <cuda_runtime_api.h>
20 # include <SLOptixHelper.h>
21 
22 //-----------------------------------------------------------------------------
23 template<typename T>
24 class SLOptixCudaBuffer
25 {
26 public:
27  SLOptixCudaBuffer();
28  ~SLOptixCudaBuffer();
29 
30  bool isAllocated();
31 
32  void resize(size_t);
33 
34  //! allocate to given number of bytes
35  void alloc(size_t);
36 
37  //! free allocated memory
38  void free();
39 
40  void alloc_and_upload(vector<T>&);
41  void alloc_and_upload(T*, unsigned int);
42 
43  void upload(vector<T>&);
44  void upload(T*);
45 
46  void download(T*);
47 
48  CUdeviceptr devicePointer() { return _devicePointer; }
49  CUdeviceptr* devicePointerPointer() { return &_devicePointer; }
50  size_t size() { return _size; }
51 
52 private:
53  CUdeviceptr _devicePointer;
54  size_t _size;
55 };
56 //-----------------------------------------------------------------------------
57 template<typename T>
58 SLOptixCudaBuffer<T>::SLOptixCudaBuffer()
59 {
60  _devicePointer = 0;
61  _size = 0;
62 }
63 //-----------------------------------------------------------------------------
64 template<typename T>
65 SLOptixCudaBuffer<T>::~SLOptixCudaBuffer()
66 {
67  free();
68 }
69 //-----------------------------------------------------------------------------
70 template<typename T>
71 bool SLOptixCudaBuffer<T>::isAllocated()
72 {
73  return _devicePointer != 0;
74 }
75 //-----------------------------------------------------------------------------
76 template<typename T>
77 void SLOptixCudaBuffer<T>::resize(size_t size)
78 {
79  free();
80  alloc(size);
81 }
82 //-----------------------------------------------------------------------------
83 template<typename T>
84 void SLOptixCudaBuffer<T>::alloc(size_t size)
85 {
86  _size = size;
87  CUDA_CHECK(cuMemAlloc(&_devicePointer, _size));
88 }
89 //-----------------------------------------------------------------------------
90 template<typename T>
91 void SLOptixCudaBuffer<T>::free()
92 {
93  if (_devicePointer)
94  {
95  CUDA_CHECK(cuMemFree(_devicePointer));
96  _size = 0;
97  _devicePointer = 0;
98  }
99 }
100 //-----------------------------------------------------------------------------
101 template<typename T>
102 void SLOptixCudaBuffer<T>::alloc_and_upload(vector<T>& vt)
103 {
104  alloc(sizeof(T) * vt.size());
105  upload(vt);
106 }
107 //-----------------------------------------------------------------------------
108 template<typename T>
109 void SLOptixCudaBuffer<T>::alloc_and_upload(T* t, unsigned int count)
110 {
111  alloc(sizeof(T) * count);
112  upload(t);
113 }
114 //-----------------------------------------------------------------------------
115 template<typename T>
116 void SLOptixCudaBuffer<T>::upload(vector<T>& vt)
117 {
118  assert(_size == sizeof(T) * vt.size());
119  CUDA_CHECK(cuMemcpyHtoD(_devicePointer,
120  (void*)vt.data(),
121  _size));
122 }
123 //-----------------------------------------------------------------------------
124 template<typename T>
125 void SLOptixCudaBuffer<T>::upload(T* t)
126 {
127  CUDA_CHECK(cuMemcpyHtoD(_devicePointer,
128  (void*)t,
129  _size));
130 }
131 //-----------------------------------------------------------------------------
132 template<typename T>
133 void SLOptixCudaBuffer<T>::download(T* t)
134 {
135  assert(_devicePointer != 0);
136  CUDA_CHECK(cuMemcpyDtoH((void*)t,
137  _devicePointer,
138  _size));
139 }
140 //-----------------------------------------------------------------------------
141 # endif // SLCUDABUFFER_H
142 #endif // SL_HAS_OPTIX