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 /* unsigned, to detect overflow/wrap-around */
72 uintptr_t start
, end
, offset
, size
;
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
)) {
83 offset
= (uintptr_t)ptr
;
84 size
= pack
->BufferObj
->Size
;
85 /* The ARB_pixel_buffer_object spec says:
86 * "INVALID_OPERATION is generated by ColorTable, ColorSubTable,
87 * ConvolutionFilter2D, ConvolutionFilter1D, SeparableFilter2D,
88 * TexImage1D, TexImage2D, TexImage3D, TexSubImage1D,
89 * TexSubImage2D, TexSubImage3D, and DrawPixels if the current
90 * PIXEL_UNPACK_BUFFER_BINDING_ARB value is non-zero and the data
91 * parameter is not evenly divisible into the number of basic machine
92 * units needed to store in memory a datum indicated by the type
95 if (type
!= GL_BITMAP
&&
96 (offset
% _mesa_sizeof_packed_type(type
)))
104 /* get the offset to the first pixel we'll read/write */
105 start
= _mesa_image_offset(dimensions
, pack
, width
, height
,
106 format
, type
, 0, 0, 0);
108 /* get the offset to just past the last pixel we'll read/write */
109 end
= _mesa_image_offset(dimensions
, pack
, width
, height
,
110 format
, type
, depth
-1, height
-1, width
);
116 /* This will catch negative values / wrap-around */
120 /* Image read/write goes beyond end of buffer */
130 * For commands that read from a PBO (glDrawPixels, glTexImage,
131 * glPolygonStipple, etc), if we're reading from a PBO, map it read-only
132 * and return the pointer into the PBO. If we're not reading from a
133 * PBO, return \p src as-is.
134 * If non-null return, must call _mesa_unmap_pbo_source() when done.
136 * \return NULL if error, else pointer to start of data
139 _mesa_map_pbo_source(struct gl_context
*ctx
,
140 const struct gl_pixelstore_attrib
*unpack
,
145 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
146 /* unpack from PBO */
147 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0,
148 unpack
->BufferObj
->Size
,
154 buf
= ADD_POINTERS(buf
, src
);
157 /* unpack from normal memory */
166 * Combine PBO-read validation and mapping.
167 * If any GL errors are detected, they'll be recorded and NULL returned.
168 * \sa _mesa_validate_pbo_access
169 * \sa _mesa_map_pbo_source
170 * A call to this function should have a matching call to
171 * _mesa_unmap_pbo_source().
174 _mesa_map_validate_pbo_source(struct gl_context
*ctx
,
176 const struct gl_pixelstore_attrib
*unpack
,
177 GLsizei width
, GLsizei height
, GLsizei depth
,
178 GLenum format
, GLenum type
,
179 GLsizei clientMemSize
,
180 const GLvoid
*ptr
, const char *where
)
182 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
184 if (!_mesa_validate_pbo_access(dimensions
, unpack
, width
, height
, depth
,
185 format
, type
, clientMemSize
, ptr
)) {
186 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
187 _mesa_error(ctx
, GL_INVALID_OPERATION
,
188 "%s(out of bounds PBO access)", where
);
190 _mesa_error(ctx
, GL_INVALID_OPERATION
,
191 "%s(out of bounds access: bufSize (%d) is too small)",
192 where
, clientMemSize
);
197 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
198 /* non-PBO access: no further validation to be done */
202 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
203 /* buffer is already mapped - that's an error */
204 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
208 ptr
= _mesa_map_pbo_source(ctx
, unpack
, ptr
);
214 * Counterpart to _mesa_map_pbo_source()
217 _mesa_unmap_pbo_source(struct gl_context
*ctx
,
218 const struct gl_pixelstore_attrib
*unpack
)
220 ASSERT(unpack
!= &ctx
->Pack
); /* catch pack/unpack mismatch */
221 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
222 ctx
->Driver
.UnmapBuffer(ctx
, unpack
->BufferObj
);
228 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
229 * if we're writing to a PBO, map it write-only and return the pointer
230 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
231 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
233 * \return NULL if error, else pointer to start of data
236 _mesa_map_pbo_dest(struct gl_context
*ctx
,
237 const struct gl_pixelstore_attrib
*pack
,
242 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
244 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0,
245 pack
->BufferObj
->Size
,
251 buf
= ADD_POINTERS(buf
, dest
);
254 /* pack to normal memory */
263 * Combine PBO-write validation and mapping.
264 * If any GL errors are detected, they'll be recorded and NULL returned.
265 * \sa _mesa_validate_pbo_access
266 * \sa _mesa_map_pbo_dest
267 * A call to this function should have a matching call to
268 * _mesa_unmap_pbo_dest().
271 _mesa_map_validate_pbo_dest(struct gl_context
*ctx
,
273 const struct gl_pixelstore_attrib
*unpack
,
274 GLsizei width
, GLsizei height
, GLsizei depth
,
275 GLenum format
, GLenum type
, GLsizei clientMemSize
,
276 GLvoid
*ptr
, const char *where
)
278 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
280 if (!_mesa_validate_pbo_access(dimensions
, unpack
, width
, height
, depth
,
281 format
, type
, clientMemSize
, ptr
)) {
282 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
283 _mesa_error(ctx
, GL_INVALID_OPERATION
,
284 "%s(out of bounds PBO access)", where
);
286 _mesa_error(ctx
, GL_INVALID_OPERATION
,
287 "%s(out of bounds access: bufSize (%d) is too small)",
288 where
, clientMemSize
);
293 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
294 /* non-PBO access: no further validation to be done */
298 if (_mesa_bufferobj_mapped(unpack
->BufferObj
)) {
299 /* buffer is already mapped - that's an error */
300 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
304 ptr
= _mesa_map_pbo_dest(ctx
, unpack
, ptr
);
310 * Counterpart to _mesa_map_pbo_dest()
313 _mesa_unmap_pbo_dest(struct gl_context
*ctx
,
314 const struct gl_pixelstore_attrib
*pack
)
316 ASSERT(pack
!= &ctx
->Unpack
); /* catch pack/unpack mismatch */
317 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
318 ctx
->Driver
.UnmapBuffer(ctx
, pack
->BufferObj
);
324 * Check if an unpack PBO is active prior to fetching a texture image.
325 * If so, do bounds checking and map the buffer into main memory.
326 * Any errors detected will be recorded.
327 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
330 _mesa_validate_pbo_teximage(struct gl_context
*ctx
, GLuint dimensions
,
331 GLsizei width
, GLsizei height
, GLsizei depth
,
332 GLenum format
, GLenum type
, const GLvoid
*pixels
,
333 const struct gl_pixelstore_attrib
*unpack
,
334 const char *funcName
)
338 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
342 if (!_mesa_validate_pbo_access(dimensions
, unpack
, width
, height
, depth
,
343 format
, type
, INT_MAX
, pixels
)) {
344 _mesa_error(ctx
, GL_INVALID_OPERATION
, funcName
, "(invalid PBO access)");
348 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0,
349 unpack
->BufferObj
->Size
,
353 _mesa_error(ctx
, GL_INVALID_OPERATION
, funcName
, "(PBO is mapped)");
357 return ADD_POINTERS(buf
, pixels
);
362 * Check if an unpack PBO is active prior to fetching a compressed texture
364 * If so, do bounds checking and map the buffer into main memory.
365 * Any errors detected will be recorded.
366 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
369 _mesa_validate_pbo_compressed_teximage(struct gl_context
*ctx
,
370 GLsizei imageSize
, const GLvoid
*pixels
,
371 const struct gl_pixelstore_attrib
*packing
,
372 const char *funcName
)
376 if (!_mesa_is_bufferobj(packing
->BufferObj
)) {
377 /* not using a PBO - return pointer unchanged */
380 if ((const GLubyte
*) pixels
+ imageSize
>
381 ((const GLubyte
*) 0) + packing
->BufferObj
->Size
) {
382 /* out of bounds read! */
383 _mesa_error(ctx
, GL_INVALID_OPERATION
, funcName
, "(invalid PBO access)");
387 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0,
388 packing
->BufferObj
->Size
,
392 _mesa_error(ctx
, GL_INVALID_OPERATION
, funcName
, "(PBO is mapped");
396 return ADD_POINTERS(buf
, pixels
);
401 * This function must be called after either of the validate_pbo_*_teximage()
402 * functions. It unmaps the PBO buffer if it was mapped earlier.
405 _mesa_unmap_teximage_pbo(struct gl_context
*ctx
,
406 const struct gl_pixelstore_attrib
*unpack
)
408 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
409 ctx
->Driver
.UnmapBuffer(ctx
, unpack
->BufferObj
);