Blame view

stim/cuda/cuda_texture.cuh 4.38 KB
84eff8b1   Pavel Govyadinov   Merged only the n...
1
2
3
4
5
6
7
8
9
  #ifndef STIM_CUDA_TEXTURE_H
  #define STIM_CUDA_TEXTURE_H
  
  #include <assert.h>
  #include <stim/cuda/cudatools/error.h>
  #include <cuda.h>
  #include <cuda_runtime.h>
  #include <cublas_v2.h>
  #include <stdio.h>
98eecaa9   David Mayerich   VS and win32 updates
10
  //#include <GL/glew.h>
84eff8b1   Pavel Govyadinov   Merged only the n...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include <GL/glut.h>
  #include <sstream>
  #include <stim/visualization/colormap.h>
  #include <stim/cuda/cudatools/devices.h>
  #include <stim/cuda/cudatools/threads.h>
  #include <stim/math/vector.h>
  
  ///A container for the texture based methods used by the spider class.
  namespace stim
  {
  	namespace cuda
  	{
  		class cuda_texture
  		{
  		public:
  			cudaArray* srcArray;
  			cudaGraphicsResource_t resource;
  			struct cudaResourceDesc resDesc;
  			struct cudaTextureDesc texDesc;
  			cudaTextureObject_t tObj;
c37611a6   Pavel Govyadinov   removed the time ...
31
  			float *result;
84eff8b1   Pavel Govyadinov   Merged only the n...
32
33
34
35
36
37
38
39
40
41
42
43
44
  
  
  			///basic constructor that creates the texture with default parameters.
  			cuda_texture()
  			{
  			memset(&texDesc, 0, sizeof(texDesc));
  				texDesc.addressMode[0] 		= cudaAddressModeWrap;
  				texDesc.addressMode[1] 		= cudaAddressModeWrap;
  				texDesc.filterMode		= cudaFilterModePoint;
  				texDesc.readMode		= cudaReadModeElementType;
  				texDesc.normalizedCoords	= 0;
  			}
  
c37611a6   Pavel Govyadinov   removed the time ...
45
46
47
48
49
50
51
52
  			///basic destructor
  			~cuda_texture()
  			{
  				UnmapCudaTexture();
  				if(result != NULL)
  					cudaFree(result);
  			}
  
27194b56   Pavel Govyadinov   major bug fixes, ...
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
  
  			///Enable the nromalized texture coordinates.
  			///@param bool, 1 for on, 0 for off
  			void
  			SetTextureCoordinates(bool val)
  			{
  				if(val)
  					texDesc.normalizedCoords	=	1;
  				else	
  					texDesc.normalizedCoords	= 	0;
  			}
  
  			///sets the dimension dim to used the mode at the borders of the texture.
  			///@param dim : 0-x, 1-y, 2-z
  			///@param mode: cudaAddressModeWrap = 0,
  			///		cudaAddressModeClamp = 1,
  			///		cudaAddressMNodeMirror = 2,
  			///		cudaAddressModeBorder = 3,
  			void
  			SetAddressMode(int dim, int mode)
  			{
  				switch(mode)
  				{
  					case 0:
  						texDesc.addressMode[dim] = cudaAddressModeWrap;
  						break;
  					case 1:
  						texDesc.addressMode[dim] = cudaAddressModeClamp;
  						break;
  					case 2:
  						texDesc.addressMode[dim] = cudaAddressModeMirror;
  						break;
  					case 3:
  						texDesc.addressMode[dim] = cudaAddressModeBorder;
  						break;
  					default:
  						break;
  				}
  			}
  
84eff8b1   Pavel Govyadinov   Merged only the n...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  //-------------------------------------------------------------------------//
  //-------------------------------CUDA_MAPPING------------------------------//
  //-------------------------------------------------------------------------//
  //Methods for creating the cuda texture.
  		///@param GLuint tex	 	 -- GLtexture (must be contained in a frame buffer object)
  		///				    that holds that data that will be handed to cuda.
  		///@param GLenum target		 -- either GL_TEXTURE_1D, GL_TEXTURE_2D or GL_TEXTURE_3D
  		///				    map work with other gl texture types but untested.
  		///Maps the gl texture in cuda memory, binds that data to a cuda array, and binds the cuda
  		///array to a cuda texture.
  		void
  		MapCudaTexture(GLuint tex, GLenum target)
  		{
  			HANDLE_ERROR(
  				cudaGraphicsGLRegisterImage(
  					&resource,
  					tex,
  					target,
efe7b7cc   Pavel Govyadinov   Added a detailed ...
111
112
  					cudaGraphicsMapFlagsReadOnly
  //					cudaGraphicsRegisterFlagsNone	
84eff8b1   Pavel Govyadinov   Merged only the n...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  				)
  			);
  
  			HANDLE_ERROR(
  				cudaGraphicsMapResources(1, &resource)
  			);
  			
  			HANDLE_ERROR(
  				cudaGraphicsSubResourceGetMappedArray(&srcArray, resource, 0, 0)
  			);
  			
  			memset(&resDesc, 0, sizeof(resDesc));
  			resDesc.resType = cudaResourceTypeArray;
  			resDesc.res.array.array = srcArray;
  			HANDLE_ERROR(
  				cudaCreateTextureObject(&tObj, &resDesc, &texDesc, NULL)
  			);
  		}
  
  		///Unmaps the gl texture,  binds that data to a cuda array, and binds the cuda
  		///array to a cuda texture.
  		void
  		UnmapCudaTexture()
  		{
6ada8448   Pavel Govyadinov   Reverted to 40db1...
137
138
139
140
141
142
143
144
145
  		//	HANDLE_ERROR(
  		//		cudaGraphicsUnmapResources(1, &resource)
  		//	);
  		//	HANDLE_ERROR(
  		//		cudaGraphicsUnregisterResource(resource)
  		//	);	
  		//	HANDLE_ERROR(
  		//		cudaDestroyTextureObject(tObj)
  		//	);
c0e09133   Pavel Govyadinov   STABLE: made temp...
146
147
148
  //			HANDLE_ERROR(
  //				cudaFreeArray(srcArray)
  //			);
84eff8b1   Pavel Govyadinov   Merged only the n...
149
150
  		}
  
c37611a6   Pavel Govyadinov   removed the time ...
151
152
153
154
155
156
157
  		///Allocate the auxiliary internal 1D float array
  		void
  		Alloc(int x)
  		{
  			cudaMalloc( (void**) &result, x*sizeof(float));
  		}
  
84eff8b1   Pavel Govyadinov   Merged only the n...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  //-------------------------------------------------------------------------//
  //------------------------------GET/SET METHODS----------------------------//
  //-------------------------------------------------------------------------//
  
  ///Returns the bound texture object.
  		cudaTextureObject_t
  		getTexture()
  		{
  			return tObj;
  		}
  
  		cudaArray*
  		getArray()
  		{
  			return srcArray;
  		}
c37611a6   Pavel Govyadinov   removed the time ...
174
175
176
177
178
179
  
  		float*
  		getAuxArray()
  		{
  			return result;
  		}
84eff8b1   Pavel Govyadinov   Merged only the n...
180
181
182
183
184
185
  	};
  }
  }
  
  
  #endif