2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2011 VMware, Inc. All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
28 * \brief Functions related to Pixel Buffer Objects.
34 #include "bufferobj.h"
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.
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!
52 * If we're not using a PBO, this is a no-op.
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
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
,
71 const GLvoid
*start
, *end
, *offset
;
72 const GLubyte
*sizeAddr
; /* buffer size, cast to a pointer */
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.
79 if (!_mesa_is_bufferobj(pack
->BufferObj
)) {
81 sizeAddr
= ((const GLubyte
*) 0) + clientMemSize
;
84 sizeAddr
= ((const GLubyte
*) 0) + pack
->BufferObj
->Size
;
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);
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
);
99 if ((const GLubyte
*) start
> sizeAddr
) {
100 /* This will catch negative values / wrap-around */
103 if ((const GLubyte
*) end
> sizeAddr
) {
104 /* Image read/write goes beyond end of buffer */
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.
120 * \return NULL if error, else pointer to start of data
123 _mesa_map_pbo_source(struct gl_context
*ctx
,
124 const struct gl_pixelstore_attrib
*unpack
,
129 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
130 /* unpack from PBO */
131 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
137 buf
= ADD_POINTERS(buf
, src
);
140 /* unpack from normal memory */
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().
157 _mesa_map_validate_pbo_source(struct gl_context
*ctx
,
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
)
164 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
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
);
172 _mesa_error(ctx
, GL_INVALID_OPERATION
,
173 "%s(out of bounds access: bufSize (%d) is too small)",
174 where
, clientMemSize
);
179 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
180 /* non-PBO access: no further validation to be done */
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
);
190 ptr
= _mesa_map_pbo_source(ctx
, unpack
, ptr
);
196 * Counterpart to _mesa_map_pbo_source()
199 _mesa_unmap_pbo_source(struct gl_context
*ctx
,
200 const struct gl_pixelstore_attrib
*unpack
)
202 ASSERT(unpack
!= &ctx
->Pack
); /* catch pack/unpack mismatch */
203 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
204 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
211 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
212 * if we're writing to a PBO, map it write-only and return the pointer
213 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
214 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
216 * \return NULL if error, else pointer to start of data
219 _mesa_map_pbo_dest(struct gl_context
*ctx
,
220 const struct gl_pixelstore_attrib
*pack
,
225 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
227 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
233 buf
= ADD_POINTERS(buf
, dest
);
236 /* pack to normal memory */
245 * Combine PBO-write validation and mapping.
246 * If any GL errors are detected, they'll be recorded and NULL returned.
247 * \sa _mesa_validate_pbo_access
248 * \sa _mesa_map_pbo_dest
249 * A call to this function should have a matching call to
250 * _mesa_unmap_pbo_dest().
253 _mesa_map_validate_pbo_dest(struct gl_context
*ctx
,
255 const struct gl_pixelstore_attrib
*unpack
,
256 GLsizei width
, GLsizei height
, GLsizei depth
,
257 GLenum format
, GLenum type
, GLsizei clientMemSize
,
258 GLvoid
*ptr
, const char *where
)
260 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
262 if (!_mesa_validate_pbo_access(dimensions
, unpack
, width
, height
, depth
,
263 format
, type
, clientMemSize
, ptr
)) {
264 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
265 _mesa_error(ctx
, GL_INVALID_OPERATION
,
266 "%s(out of bounds PBO access)", where
);
268 _mesa_error(ctx
, GL_INVALID_OPERATION
,
269 "%s(out of bounds access: bufSize (%d) is too small)",
270 where
, clientMemSize
);
275 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
276 /* non-PBO access: no further validation to be done */
280 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
281 /* buffer is already mapped - that's an error */
282 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
286 ptr
= _mesa_map_pbo_dest(ctx
, unpack
, ptr
);
292 * Counterpart to _mesa_map_pbo_dest()
295 _mesa_unmap_pbo_dest(struct gl_context
*ctx
,
296 const struct gl_pixelstore_attrib
*pack
)
298 ASSERT(pack
!= &ctx
->Unpack
); /* catch pack/unpack mismatch */
299 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
300 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
, pack
->BufferObj
);
306 * Check if an unpack PBO is active prior to fetching a texture image.
307 * If so, do bounds checking and map the buffer into main memory.
308 * Any errors detected will be recorded.
309 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
312 _mesa_validate_pbo_teximage(struct gl_context
*ctx
, GLuint dimensions
,
313 GLsizei width
, GLsizei height
, GLsizei depth
,
314 GLenum format
, GLenum type
, const GLvoid
*pixels
,
315 const struct gl_pixelstore_attrib
*unpack
,
316 const char *funcName
)
320 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
324 if (!_mesa_validate_pbo_access(dimensions
, unpack
, width
, height
, depth
,
325 format
, type
, INT_MAX
, pixels
)) {
326 _mesa_error(ctx
, GL_INVALID_OPERATION
, funcName
, "(invalid PBO access)");
330 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
331 GL_READ_ONLY_ARB
, unpack
->BufferObj
);
333 _mesa_error(ctx
, GL_INVALID_OPERATION
, funcName
, "(PBO is mapped)");
337 return ADD_POINTERS(buf
, pixels
);
342 * Check if an unpack PBO is active prior to fetching a compressed texture
344 * If so, do bounds checking and map the buffer into main memory.
345 * Any errors detected will be recorded.
346 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
349 _mesa_validate_pbo_compressed_teximage(struct gl_context
*ctx
,
350 GLsizei imageSize
, const GLvoid
*pixels
,
351 const struct gl_pixelstore_attrib
*packing
,
352 const char *funcName
)
356 if (!_mesa_is_bufferobj(packing
->BufferObj
)) {
357 /* not using a PBO - return pointer unchanged */
360 if ((const GLubyte
*) pixels
+ imageSize
>
361 ((const GLubyte
*) 0) + packing
->BufferObj
->Size
) {
362 /* out of bounds read! */
363 _mesa_error(ctx
, GL_INVALID_OPERATION
, funcName
, "(invalid PBO access)");
367 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
368 GL_READ_ONLY_ARB
, packing
->BufferObj
);
370 _mesa_error(ctx
, GL_INVALID_OPERATION
, funcName
, "(PBO is mapped");
374 return ADD_POINTERS(buf
, pixels
);
379 * This function must be called after either of the validate_pbo_*_teximage()
380 * functions. It unmaps the PBO buffer if it was mapped earlier.
383 _mesa_unmap_teximage_pbo(struct gl_context
*ctx
,
384 const struct gl_pixelstore_attrib
*unpack
)
386 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
387 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,