ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
cuda_texture.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <cuda_runtime.h>
11 
12 #include <memory>
13 
14 #include "mvs/gpu_mat.h"
15 #include "util/cudacc.h"
16 #include "util/logging.h"
17 
18 namespace colmap {
19 namespace mvs {
20 
21 template <typename T>
23 public:
24  static std::unique_ptr<CudaArrayLayeredTexture<T>> FromGpuMat(
25  const cudaTextureDesc& texture_desc, const GpuMat<T>& mat);
26  static std::unique_ptr<CudaArrayLayeredTexture<T>> FromHostArray(
27  const cudaTextureDesc& texture_desc,
28  const size_t width,
29  const size_t height,
30  const size_t depth,
31  const T* data);
32 
33  cudaTextureObject_t GetObj() const;
34 
35  size_t GetWidth() const;
36  size_t GetHeight() const;
37  size_t GetDepth() const;
38 
39  CudaArrayLayeredTexture(const cudaTextureDesc& texture_desc,
40  const size_t width,
41  const size_t height,
42  const size_t depth);
44 
45 private:
46  // Define class as non-copyable and non-movable.
48  void operator=(CudaArrayLayeredTexture const& obj) = delete;
50 
51  const cudaTextureDesc texture_desc_;
52  const size_t width_;
53  const size_t height_;
54  const size_t depth_;
55 
56  cudaArray_t array_;
57  cudaResourceDesc resource_desc_;
58  cudaTextureObject_t texture_;
59 };
60 
62 // Implementation
64 
65 template <typename T>
66 std::unique_ptr<CudaArrayLayeredTexture<T>>
67 CudaArrayLayeredTexture<T>::FromGpuMat(const cudaTextureDesc& texture_desc,
68  const GpuMat<T>& mat) {
69  auto array = std::make_unique<CudaArrayLayeredTexture<T>>(
70  texture_desc, mat.GetWidth(), mat.GetHeight(), mat.GetDepth());
71 
72  cudaMemcpy3DParms params;
73  memset(&params, 0, sizeof(params));
74  params.extent =
75  make_cudaExtent(mat.GetWidth(), mat.GetHeight(), mat.GetDepth());
76  params.kind = cudaMemcpyDeviceToDevice;
77  params.srcPtr = make_cudaPitchedPtr((void*)mat.GetPtr(), mat.GetPitch(),
78  mat.GetWidth(), mat.GetHeight());
79  params.dstArray = array->array_;
80  CUDA_SAFE_CALL(cudaMemcpy3D(&params));
81 
82  return array;
83 }
84 
85 template <typename T>
86 std::unique_ptr<CudaArrayLayeredTexture<T>>
87 CudaArrayLayeredTexture<T>::FromHostArray(const cudaTextureDesc& texture_desc,
88  const size_t width,
89  const size_t height,
90  const size_t depth,
91  const T* data) {
92  auto array = std::make_unique<CudaArrayLayeredTexture<T>>(
93  texture_desc, width, height, depth);
94 
95  cudaMemcpy3DParms params;
96  memset(&params, 0, sizeof(params));
97  params.extent = make_cudaExtent(width, height, depth);
98  params.kind = cudaMemcpyHostToDevice;
99  params.srcPtr =
100  make_cudaPitchedPtr((void*)data, width * sizeof(T), width, height);
101  params.dstArray = array->array_;
102  CUDA_SAFE_CALL(cudaMemcpy3D(&params));
103 
104  return array;
105 }
106 
107 template <typename T>
109  const cudaTextureDesc& texture_desc,
110  const size_t width,
111  const size_t height,
112  const size_t depth)
113  : texture_desc_(texture_desc),
114  width_(width),
115  height_(height),
116  depth_(depth) {
117  CHECK_GT(width_, 0);
118  CHECK_GT(height_, 0);
119  CHECK_GT(depth_, 0);
120 
121  cudaExtent extent = make_cudaExtent(width_, height_, depth_);
122  cudaChannelFormatDesc fmt = cudaCreateChannelDesc<T>();
123  CUDA_SAFE_CALL(cudaMalloc3DArray(&array_, &fmt, extent, cudaArrayLayered));
124 
125  memset(&resource_desc_, 0, sizeof(resource_desc_));
126  resource_desc_.resType = cudaResourceTypeArray;
127  resource_desc_.res.array.array = array_;
128 
129  CUDA_SAFE_CALL(cudaCreateTextureObject(&texture_, &resource_desc_,
130  &texture_desc_, nullptr));
131 }
132 
133 template <typename T>
135  CUDA_SAFE_CALL(cudaFreeArray(array_));
136  CUDA_SAFE_CALL(cudaDestroyTextureObject(texture_));
137 }
138 
139 template <typename T>
140 cudaTextureObject_t CudaArrayLayeredTexture<T>::GetObj() const {
141  return texture_;
142 }
143 
144 template <typename T>
146  return width_;
147 }
148 
149 template <typename T>
151  return height_;
152 }
153 
154 template <typename T>
156  return depth_;
157 }
158 
159 } // namespace mvs
160 } // namespace colmap
int width
int height
CudaArrayLayeredTexture(const cudaTextureDesc &texture_desc, const size_t width, const size_t height, const size_t depth)
Definition: cuda_texture.h:108
static std::unique_ptr< CudaArrayLayeredTexture< T > > FromHostArray(const cudaTextureDesc &texture_desc, const size_t width, const size_t height, const size_t depth, const T *data)
Definition: cuda_texture.h:87
cudaTextureObject_t GetObj() const
Definition: cuda_texture.h:140
static std::unique_ptr< CudaArrayLayeredTexture< T > > FromGpuMat(const cudaTextureDesc &texture_desc, const GpuMat< T > &mat)
Definition: cuda_texture.h:67
__host__ __device__ size_t GetWidth() const
__host__ __device__ size_t GetDepth() const
__host__ __device__ const T * GetPtr() const
__host__ __device__ size_t GetHeight() const
__host__ __device__ size_t GetPitch() const
#define CUDA_SAFE_CALL(error)
Definition: cudacc.h:14
GraphType data
Definition: graph_cut.cc:138