mesa: Remove target parameter from dd_function_table::UnmapBuffer
[mesa.git] / src / mesa / main / pbo.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2011 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file pbo.c
28 * \brief Functions related to Pixel Buffer Objects.
29 */
30
31
32
33 #include "glheader.h"
34 #include "bufferobj.h"
35 #include "image.h"
36 #include "imports.h"
37 #include "mtypes.h"
38 #include "pbo.h"
39
40
41
42 /**
43 * When we're about to read pixel data out of a PBO (via glDrawPixels,
44 * glTexImage, etc) or write data into a PBO (via glReadPixels,
45 * glGetTexImage, etc) we call this function to check that we're not
46 * going to read/write out of bounds.
47 *
48 * XXX This would also be a convenient time to check that the PBO isn't
49 * currently mapped. Whoever calls this function should check for that.
50 * Remember, we can't use a PBO when it's mapped!
51 *
52 * If we're not using a PBO, this is a no-op.
53 *
54 * \param width width of image to read/write
55 * \param height height of image to read/write
56 * \param depth depth of image to read/write
57 * \param format format of image to read/write
58 * \param type datatype of image to read/write
59 * \param clientMemSize the maximum number of bytes to read/write
60 * \param ptr the user-provided pointer/offset
61 * \return GL_TRUE if the buffer access is OK, GL_FALSE if the access would
62 * go out of bounds.
63 */
64 GLboolean
65 _mesa_validate_pbo_access(GLuint dimensions,
66 const struct gl_pixelstore_attrib *pack,
67 GLsizei width, GLsizei height, GLsizei depth,
68 GLenum format, GLenum type, GLsizei clientMemSize,
69 const GLvoid *ptr)
70 {
71 const GLvoid *start, *end, *offset;
72 const GLubyte *sizeAddr; /* buffer size, cast to a pointer */
73
74 /* If no PBO is bound, 'ptr' is a pointer to client memory containing
75 'clientMemSize' bytes.
76 If a PBO is bound, 'ptr' is an offset into the bound PBO.
77 In that case 'clientMemSize' is ignored: we just use the PBO's size.
78 */
79 if (!_mesa_is_bufferobj(pack->BufferObj)) {
80 offset = 0;
81 sizeAddr = ((const GLubyte *) 0) + clientMemSize;
82 } else {
83 offset = ptr;
84 sizeAddr = ((const GLubyte *) 0) + pack->BufferObj->Size;
85 }
86
87 if (sizeAddr == 0)
88 /* no buffer! */
89 return GL_FALSE;
90
91 /* get the offset to the first pixel we'll read/write */
92 start = _mesa_image_address(dimensions, pack, offset, width, height,
93 format, type, 0, 0, 0);
94
95 /* get the offset to just past the last pixel we'll read/write */
96 end = _mesa_image_address(dimensions, pack, offset, width, height,
97 format, type, depth-1, height-1, width);
98
99 if ((const GLubyte *) start > sizeAddr) {
100 /* This will catch negative values / wrap-around */
101 return GL_FALSE;
102 }
103 if ((const GLubyte *) end > sizeAddr) {
104 /* Image read/write goes beyond end of buffer */
105 return GL_FALSE;
106 }
107
108 /* OK! */
109 return GL_TRUE;
110 }
111
112
113 /**
114 * For commands that read from a PBO (glDrawPixels, glTexImage,
115 * glPolygonStipple, etc), if we're reading from a PBO, map it read-only
116 * and return the pointer into the PBO. If we're not reading from a
117 * PBO, return \p src as-is.
118 * If non-null return, must call _mesa_unmap_pbo_source() when done.
119 *
120 * \return NULL if error, else pointer to start of data
121 */
122 const GLvoid *
123 _mesa_map_pbo_source(struct gl_context *ctx,
124 const struct gl_pixelstore_attrib *unpack,
125 const GLvoid *src)
126 {
127 const GLubyte *buf;
128
129 if (_mesa_is_bufferobj(unpack->BufferObj)) {
130 /* unpack from PBO */
131 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
132 GL_READ_ONLY_ARB,
133 unpack->BufferObj);
134 if (!buf)
135 return NULL;
136
137 buf = ADD_POINTERS(buf, src);
138 }
139 else {
140 /* unpack from normal memory */
141 buf = src;
142 }
143
144 return buf;
145 }
146
147
148 /**
149 * Combine PBO-read validation and mapping.
150 * If any GL errors are detected, they'll be recorded and NULL returned.
151 * \sa _mesa_validate_pbo_access
152 * \sa _mesa_map_pbo_source
153 * A call to this function should have a matching call to
154 * _mesa_unmap_pbo_source().
155 */
156 const GLvoid *
157 _mesa_map_validate_pbo_source(struct gl_context *ctx,
158 GLuint dimensions,
159 const struct gl_pixelstore_attrib *unpack,
160 GLsizei width, GLsizei height, GLsizei depth,
161 GLenum format, GLenum type, GLsizei clientMemSize,
162 const GLvoid *ptr, const char *where)
163 {
164 ASSERT(dimensions == 1 || dimensions == 2 || dimensions == 3);
165
166 if (!_mesa_validate_pbo_access(dimensions, unpack, width, height, depth,
167 format, type, clientMemSize, ptr)) {
168 if (_mesa_is_bufferobj(unpack->BufferObj)) {
169 _mesa_error(ctx, GL_INVALID_OPERATION,
170 "%s(out of bounds PBO access)", where);
171 } else {
172 _mesa_error(ctx, GL_INVALID_OPERATION,
173 "%s(out of bounds access: bufSize (%d) is too small)",
174 where, clientMemSize);
175 }
176 return NULL;
177 }
178
179 if (!_mesa_is_bufferobj(unpack->BufferObj)) {
180 /* non-PBO access: no further validation to be done */
181 return ptr;
182 }
183
184 if (_mesa_bufferobj_mapped(unpack->BufferObj)) {
185 /* buffer is already mapped - that's an error */
186 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", where);
187 return NULL;
188 }
189
190 ptr = _mesa_map_pbo_source(ctx, unpack, ptr);
191 return ptr;
192 }
193
194
195 /**
196 * Counterpart to _mesa_map_pbo_source()
197 */
198 void
199 _mesa_unmap_pbo_source(struct gl_context *ctx,
200 const struct gl_pixelstore_attrib *unpack)
201 {
202 ASSERT(unpack != &ctx->Pack); /* catch pack/unpack mismatch */
203 if (_mesa_is_bufferobj(unpack->BufferObj)) {
204 ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj);
205 }
206 }
207
208
209 /**
210 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
211 * if we're writing to a PBO, map it write-only and return the pointer
212 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
213 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
214 *
215 * \return NULL if error, else pointer to start of data
216 */
217 void *
218 _mesa_map_pbo_dest(struct gl_context *ctx,
219 const struct gl_pixelstore_attrib *pack,
220 GLvoid *dest)
221 {
222 void *buf;
223
224 if (_mesa_is_bufferobj(pack->BufferObj)) {
225 /* pack into PBO */
226 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
227 GL_WRITE_ONLY_ARB,
228 pack->BufferObj);
229 if (!buf)
230 return NULL;
231
232 buf = ADD_POINTERS(buf, dest);
233 }
234 else {
235 /* pack to normal memory */
236 buf = dest;
237 }
238
239 return buf;
240 }
241
242
243 /**
244 * Combine PBO-write validation and mapping.
245 * If any GL errors are detected, they'll be recorded and NULL returned.
246 * \sa _mesa_validate_pbo_access
247 * \sa _mesa_map_pbo_dest
248 * A call to this function should have a matching call to
249 * _mesa_unmap_pbo_dest().
250 */
251 GLvoid *
252 _mesa_map_validate_pbo_dest(struct gl_context *ctx,
253 GLuint dimensions,
254 const struct gl_pixelstore_attrib *unpack,
255 GLsizei width, GLsizei height, GLsizei depth,
256 GLenum format, GLenum type, GLsizei clientMemSize,
257 GLvoid *ptr, const char *where)
258 {
259 ASSERT(dimensions == 1 || dimensions == 2 || dimensions == 3);
260
261 if (!_mesa_validate_pbo_access(dimensions, unpack, width, height, depth,
262 format, type, clientMemSize, ptr)) {
263 if (_mesa_is_bufferobj(unpack->BufferObj)) {
264 _mesa_error(ctx, GL_INVALID_OPERATION,
265 "%s(out of bounds PBO access)", where);
266 } else {
267 _mesa_error(ctx, GL_INVALID_OPERATION,
268 "%s(out of bounds access: bufSize (%d) is too small)",
269 where, clientMemSize);
270 }
271 return NULL;
272 }
273
274 if (!_mesa_is_bufferobj(unpack->BufferObj)) {
275 /* non-PBO access: no further validation to be done */
276 return ptr;
277 }
278
279 if (_mesa_bufferobj_mapped(unpack->BufferObj)) {
280 /* buffer is already mapped - that's an error */
281 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", where);
282 return NULL;
283 }
284
285 ptr = _mesa_map_pbo_dest(ctx, unpack, ptr);
286 return ptr;
287 }
288
289
290 /**
291 * Counterpart to _mesa_map_pbo_dest()
292 */
293 void
294 _mesa_unmap_pbo_dest(struct gl_context *ctx,
295 const struct gl_pixelstore_attrib *pack)
296 {
297 ASSERT(pack != &ctx->Unpack); /* catch pack/unpack mismatch */
298 if (_mesa_is_bufferobj(pack->BufferObj)) {
299 ctx->Driver.UnmapBuffer(ctx, pack->BufferObj);
300 }
301 }
302
303
304 /**
305 * Check if an unpack PBO is active prior to fetching a texture image.
306 * If so, do bounds checking and map the buffer into main memory.
307 * Any errors detected will be recorded.
308 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
309 */
310 const GLvoid *
311 _mesa_validate_pbo_teximage(struct gl_context *ctx, GLuint dimensions,
312 GLsizei width, GLsizei height, GLsizei depth,
313 GLenum format, GLenum type, const GLvoid *pixels,
314 const struct gl_pixelstore_attrib *unpack,
315 const char *funcName)
316 {
317 GLubyte *buf;
318
319 if (!_mesa_is_bufferobj(unpack->BufferObj)) {
320 /* no PBO */
321 return pixels;
322 }
323 if (!_mesa_validate_pbo_access(dimensions, unpack, width, height, depth,
324 format, type, INT_MAX, pixels)) {
325 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(invalid PBO access)");
326 return NULL;
327 }
328
329 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
330 GL_READ_ONLY_ARB, unpack->BufferObj);
331 if (!buf) {
332 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(PBO is mapped)");
333 return NULL;
334 }
335
336 return ADD_POINTERS(buf, pixels);
337 }
338
339
340 /**
341 * Check if an unpack PBO is active prior to fetching a compressed texture
342 * image.
343 * If so, do bounds checking and map the buffer into main memory.
344 * Any errors detected will be recorded.
345 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
346 */
347 const GLvoid *
348 _mesa_validate_pbo_compressed_teximage(struct gl_context *ctx,
349 GLsizei imageSize, const GLvoid *pixels,
350 const struct gl_pixelstore_attrib *packing,
351 const char *funcName)
352 {
353 GLubyte *buf;
354
355 if (!_mesa_is_bufferobj(packing->BufferObj)) {
356 /* not using a PBO - return pointer unchanged */
357 return pixels;
358 }
359 if ((const GLubyte *) pixels + imageSize >
360 ((const GLubyte *) 0) + packing->BufferObj->Size) {
361 /* out of bounds read! */
362 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(invalid PBO access)");
363 return NULL;
364 }
365
366 buf = (GLubyte*) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
367 GL_READ_ONLY_ARB, packing->BufferObj);
368 if (!buf) {
369 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(PBO is mapped");
370 return NULL;
371 }
372
373 return ADD_POINTERS(buf, pixels);
374 }
375
376
377 /**
378 * This function must be called after either of the validate_pbo_*_teximage()
379 * functions. It unmaps the PBO buffer if it was mapped earlier.
380 */
381 void
382 _mesa_unmap_teximage_pbo(struct gl_context *ctx,
383 const struct gl_pixelstore_attrib *unpack)
384 {
385 if (_mesa_is_bufferobj(unpack->BufferObj)) {
386 ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj);
387 }
388 }
389
390