triro 1.3.0
A Python Ray-Mesh Intersector in OptiX
Loading...
Searching...
No Matches
CUDABuffer.h
Go to the documentation of this file.
1// ======================================================================== //
2// Copyright 2018-2019 Ingo Wald //
3// //
4// Licensed under the Apache License, Version 2.0 (the "License"); //
5// you may not use this file except in compliance with the License. //
6// You may obtain a copy of the License at //
7// //
8// http://www.apache.org/licenses/LICENSE-2.0 //
9// //
10// Unless required by applicable law or agreed to in writing, software //
11// distributed under the License is distributed on an "AS IS" BASIS, //
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
13// See the License for the specific language governing permissions and //
14// limitations under the License. //
15// ======================================================================== //
16
17#pragma once
18
19#include "optix8.h"
20// common std stuff
21#include <assert.h>
22#include <vector>
23
24namespace hmesh {
25
28struct CUDABuffer {
29 inline CUdeviceptr d_pointer() const { return (CUdeviceptr)d_ptr; }
30
32 void resize(size_t size) {
33 if (d_ptr)
34 free();
35 alloc(size);
36 }
37
39 void alloc(size_t size) {
40 assert(d_ptr == nullptr);
41 this->sizeInBytes = size;
42 CUDA_CHECK(Malloc((void **)&d_ptr, sizeInBytes));
43 }
44
46 void free() {
47 CUDA_CHECK(Free(d_ptr));
48 d_ptr = nullptr;
49 sizeInBytes = 0;
50 }
51
52 template <typename T> void alloc_and_upload(const std::vector<T> &vt) {
53 alloc(vt.size() * sizeof(T));
54 upload((const T *)vt.data(), vt.size());
55 }
56
57 template <typename T> void alloc_and_upload(const T *t, size_t count) {
58 alloc(count * sizeof(T));
59 upload(t, count);
60 }
61
62 template <typename T> void upload(const T *t, size_t count) {
63 assert(d_ptr != nullptr);
64 assert(sizeInBytes == count * sizeof(T));
65 CUDA_CHECK(Memcpy(d_ptr, (void *)t, count * sizeof(T),
66 cudaMemcpyHostToDevice));
67 }
68
69 template <typename T> void download(T *t, size_t count) {
70 assert(d_ptr != nullptr);
71 assert(sizeInBytes == count * sizeof(T));
72 CUDA_CHECK(Memcpy((void *)t, d_ptr, count * sizeof(T),
73 cudaMemcpyDeviceToHost));
74 }
75
76 size_t sizeInBytes{0};
77 void *d_ptr{nullptr};
78};
79
80} // namespace hmesh
Definition base.cpp:13
#define CUDA_CHECK(call)
Definition optix8.h:26
@ count
Definition sbtdef.h:15
CUdeviceptr d_pointer() const
Definition CUDABuffer.h:29
void free()
free allocated memory
Definition CUDABuffer.h:46
void resize(size_t size)
re-size buffer to given number of bytes
Definition CUDABuffer.h:32
void download(T *t, size_t count)
Definition CUDABuffer.h:69
void alloc(size_t size)
allocate to given number of bytes
Definition CUDABuffer.h:39
void alloc_and_upload(const T *t, size_t count)
Definition CUDABuffer.h:57
void upload(const T *t, size_t count)
Definition CUDABuffer.h:62
void alloc_and_upload(const std::vector< T > &vt)
Definition CUDABuffer.h:52