-
Notifications
You must be signed in to change notification settings - Fork 289
/
glTexture.h
226 lines (192 loc) · 6.37 KB
/
glTexture.h
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef __GL_TEXTURE_H__
#define __GL_TEXTURE_H__
#include "glBuffer.h"
/**
* Alias for GL_RGB32F
* @ingroup OpenGL
*/
#ifndef GL_RGB32F
#define GL_RGB32F GL_RGB32F_ARB
#endif
/**
* Alias for GL_RGBA32F
* @ingroup OpenGL
*/
#ifndef GL_RGBA32F
#define GL_RGBA32F GL_RGBA32F_ARB
#endif
/**
* OpenGL texture with CUDA interoperability.
* @ingroup OpenGL
*/
class glTexture
{
public:
/**
* Allocate an OpenGL texture
* @param width the width of the texture in pixels
* @param height the height of the texture in pixels
* @param format GL_RGBA8, GL_RGBA32F, ect.
* @param data initialize the texture's memory with this CPU pointer, size is width*height*bpp
*/
static glTexture* Create( uint32_t width, uint32_t height, uint32_t format, void* data=NULL );
/**
* Free the texture
*/
~glTexture();
/**
* Activate using the texture
*/
bool Bind();
/**
* Deactivate using the texture
*/
void Unbind();
/**
* Render the texture at the specified window coordinates.
*/
void Render( float x, float y );
/**
* Render the texture with the specified position and size.
*/
void Render( float x, float y, float width, float height );
/**
* Render the texture to the specific screen rectangle.
*/
void Render( const float4& rect );
/**
* Retrieve the OpenGL resource handle of the texture.
*/
inline uint32_t GetID() const { return mID; }
/**
* Retrieve the width in pixels of the texture.
*/
inline uint32_t GetWidth() const { return mWidth; }
/**
* Retrieve the height in pixels of the texture.
*/
inline uint32_t GetHeight() const { return mHeight; }
/**
* Retrieve the texture's format (e.g. GL_RGBA8, GL_RGBA32F, ect.)
*/
inline uint32_t GetFormat() const { return mFormat; }
/**
* Retrieve the size in bytes of the texture
*/
inline uint32_t GetSize() const { return mSize; }
/**
* Map the texture for accessing from the CPU or CUDA.
*
* @param device either GL_MAP_CPU or GL_MAP_CUDA
*
* @param flags should be one of the following:
* - GL_READ_WRITE
* - GL_READ_ONLY
* - GL_WRITE_ONLY
* - GL_WRITE_DISCARD
*
* @returns CPU pointer to buffer if GL_MAP_CPU was specified,
* CUDA device pointer to buffer if GL_MAP_CUDA was specified,
* or NULL if an error occurred mapping the buffer.
*/
void* Map( uint32_t device, uint32_t flags, cudaStream_t stream=0 );
/**
* Unmap the texture from CPU/CUDA access.
* @note the texture will be unbound after calling Unmap()
*/
void Unmap( cudaStream_t stream=0 );
/**
* Copy entire contents of the texture to/from CPU or CUDA memory.
*
* @param ptr the memory pointer to copy to/from, either in
* CPU or CUDA address space depending on flags.
* It's assumed that the size of the memory from
* this pointer is equal to GetSize(), and the
* entire contents of the buffer will be copied.
*
* @param flags should be one of the following:
* - GL_FROM_CPU (copy from CPU->OpenGL)
* - GL_FROM_CUDA (copy from CUDA->OpenGL)
* - GL_TO_CPU (copy from OpenGL->CPU)
* - GL_TO_CUDA (copy from OpenGL->CUDA)
*
* @returns true on success, false on failure
*/
bool Copy( void* ptr, uint32_t flags, cudaStream_t stream=0 );
/**
* Copy contents of the texture to/from CPU or CUDA memory.
*
* @param ptr the memory pointer to copy to/from, either in
* CPU or CUDA address space depending on flags.
*
* @param size the number of bytes to copy
*
* @param flags should be one of the following:
* - GL_FROM_CPU (copy from CPU->OpenGL)
* - GL_FROM_CUDA (copy from CUDA->OpenGL)
* - GL_TO_CPU (copy from OpenGL->CPU)
* - GL_TO_CUDA (copy from OpenGL->CUDA)
*
* @returns true on success, false on failure
*/
bool Copy( void* ptr, uint32_t size, uint32_t flags, cudaStream_t stream=0 );
/**
* Copy contents of the texture to/from CPU or CUDA memory.
*
* @param ptr the memory buffer to copy to/from, either in
* CPU or CUDA address space depending on flags
*
* @param offset the offset into the OpenGL buffer to copy.
* It is assumed any offset to the CPU/CUDA
* pointer argument has already been applied.
*
* @param size the number of bytes to copy
*
* @param flags should be one of the following:
* - GL_FROM_CPU (copy from CPU->OpenGL)
* - GL_FROM_CUDA (copy from CUDA->OpenGL)
* - GL_TO_CPU (copy from OpenGL->CPU)
* - GL_TO_CUDA (copy from OpenGL->CUDA)
*
* @returns true on success, false on failure
*/
bool Copy( void* ptr, uint32_t offset, uint32_t size, uint32_t flags, cudaStream_t stream=0 );
private:
glTexture();
bool init( uint32_t width, uint32_t height, uint32_t format, void* data );
uint32_t allocDMA( uint32_t type );
cudaGraphicsResource* allocInterop( uint32_t type, uint32_t flags );
uint32_t mID;
uint32_t mPackDMA;
uint32_t mUnpackDMA;
uint32_t mWidth;
uint32_t mHeight;
uint32_t mFormat;
uint32_t mSize;
uint32_t mMapDevice;
uint32_t mMapFlags;
cudaGraphicsResource* mInteropPack;
cudaGraphicsResource* mInteropUnpack;
};
#endif