-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCuTexImage.cpp
More file actions
executable file
·149 lines (120 loc) · 3.62 KB
/
CuTexImage.cpp
File metadata and controls
executable file
·149 lines (120 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <cuda.h>
#include <cuda_runtime_api.h>
#include "GlobalUtil.h"
#include "CuTexImage.h"
#include "ProgramCU.h"
#if CUDA_VERSION <= 2010 && defined(SIFTGPU_ENABLE_LINEAR_TEX2D)
#error "Require CUDA 2.2 or higher"
#endif
CuTexImage::CuTexImage()
{
_cuData = NULL;
_cuData2D = NULL;
_numChannel = _numBytes = 0;
_imgWidth = _imgHeight = _texWidth = _texHeight = 0;
m_external = false;
}
CuTexImage::~CuTexImage()
{
if (!m_external) {
if (_cuData) cudaFree(_cuData);
}
if (_cuData2D) cudaFreeArray(_cuData2D);
}
void CuTexImage::SetImageSize(int width, int height)
{
_imgWidth = width;
_imgHeight = height;
}
void CuTexImage::InitTexture(int width, int height, int nchannel)
{
int size;
_imgWidth = width;
_imgHeight = height;
_numChannel = std::min(std::max(nchannel, 1), 4); //1<x<4
size = width * height * _numChannel * sizeof(float);
if(size <= _numBytes) return;
if(_cuData) cudaFree(_cuData);
//allocate the array data
cudaMalloc(&_cuData, _numBytes = size);
#ifdef _DEBUG
ProgramCU::CheckErrorCUDA("CuTexImage::InitTexture");
#endif
}
void CuTexImage::CopyFromHost(const void * buf)
{
if(_cuData == NULL) return;
cudaMemcpy( _cuData, buf, _imgWidth * _imgHeight * _numChannel * sizeof(float), cudaMemcpyHostToDevice);
}
void CuTexImage::CopyToDevice(CuTexImage* other) const
{
other->InitTexture(_imgWidth, _imgHeight, _numChannel);
cudaMemcpy(other->_cuData, _cuData, _imgWidth * _imgHeight * _numChannel * sizeof(float), cudaMemcpyDeviceToDevice);
}
void CuTexImage::CopyToHost(void * buf)
{
if(_cuData == NULL) return;
cudaMemcpy(buf, _cuData, _imgWidth * _imgHeight * _numChannel * sizeof(float), cudaMemcpyDeviceToHost);
}
void CuTexImage::CopyToHost(void * buf, int stream)
{
if(_cuData == NULL) return;
cudaMemcpyAsync(buf, _cuData, _imgWidth * _imgHeight * _numChannel * sizeof(float), cudaMemcpyDeviceToHost, (cudaStream_t)stream);
}
void CuTexImage::InitTexture2D()
{
#if !defined(SIFTGPU_ENABLE_LINEAR_TEX2D)
if(_cuData2D && (_texWidth < _imgWidth || _texHeight < _imgHeight))
{
cudaFreeArray(_cuData2D);
_cuData2D = NULL;
}
if(_cuData2D == NULL)
{
_texWidth = std::max(_texWidth, _imgWidth);
_texHeight = std::max(_texHeight, _imgHeight);
cudaChannelFormatDesc desc;
desc.f = cudaChannelFormatKindFloat;
desc.x = sizeof(float) * 8;
desc.y = _numChannel >=2 ? sizeof(float) * 8 : 0;
desc.z = _numChannel >=3 ? sizeof(float) * 8 : 0;
desc.w = _numChannel >=4 ? sizeof(float) * 8 : 0;
cudaMallocArray(&_cuData2D, &desc, _texWidth, _texHeight);
ProgramCU::CheckErrorCUDA("cudaMallocArray");
}
#endif
}
void CuTexImage::CopyToTexture2D()
{
#if !defined(SIFTGPU_ENABLE_LINEAR_TEX2D)
InitTexture2D();
if(_cuData2D)
{
cudaMemcpy2DToArray(_cuData2D, 0, 0, _cuData, _imgWidth* _numChannel* sizeof(float) ,
_imgWidth * _numChannel*sizeof(float), _imgHeight, cudaMemcpyDeviceToDevice);
ProgramCU::CheckErrorCUDA("cudaMemcpy2DToArray");
}
#endif
}
int CuTexImage::DebugCopyToTexture2D()
{
/* CuTexImage tex;
float data1[2][3] = {{1, 2, 5}, {3, 4, 5}}, data2[2][5];
tex.InitTexture(3, 2, 1);
cudaMemcpy(tex._cuData, data1[0], 6 * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(data1, tex._cuData, 4 * sizeof(float) , cudaMemcpyDeviceToHost);
tex._texWidth =5; tex._texHeight = 2;
tex.CopyToTexture2D();
cudaMemcpyFromArray(data2[0], tex._cuData2D, 0, 0, 10 * sizeof(float), cudaMemcpyDeviceToHost);*/
return 1;
}
void CuTexImage::Memset( int value /*= 0*/)
{
if (_cuData == NULL) return;
cudaMemset(_cuData, value, _imgWidth * _imgHeight * _numChannel * sizeof(float));
}