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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
29 * \brief Functions related to Pixel Buffer Objects.
35 #include "bufferobj.h"
36 #include "glformats.h"
45 * When we're about to read pixel data out of a PBO (via glDrawPixels,
46 * glTexImage, etc) or write data into a PBO (via glReadPixels,
47 * glGetTexImage, etc) we call this function to check that we're not
48 * going to read/write out of bounds.
50 * XXX This would also be a convenient time to check that the PBO isn't
51 * currently mapped. Whoever calls this function should check for that.
52 * Remember, we can't use a PBO when it's mapped!
54 * If we're not using a PBO, this is a no-op.
56 * \param width width of image to read/write
57 * \param height height of image to read/write
58 * \param depth depth of image to read/write
59 * \param format format of image to read/write
60 * \param type datatype of image to read/write
61 * \param clientMemSize the maximum number of bytes to read/write
62 * \param ptr the user-provided pointer/offset
63 * \return GL_TRUE if the buffer access is OK, GL_FALSE if the access would
67 _mesa_validate_pbo_access(GLuint dimensions
,
68 const struct gl_pixelstore_attrib
*pack
,
69 GLsizei width
, GLsizei height
, GLsizei depth
,
70 GLenum format
, GLenum type
, GLsizei clientMemSize
,
73 /* unsigned, to detect overflow/wrap-around */
74 uintptr_t start
, end
, offset
, size
;
76 /* If no PBO is bound, 'ptr' is a pointer to client memory containing
77 'clientMemSize' bytes.
78 If a PBO is bound, 'ptr' is an offset into the bound PBO.
79 In that case 'clientMemSize' is ignored: we just use the PBO's size.
81 if (!_mesa_is_bufferobj(pack
->BufferObj
)) {
85 offset
= (uintptr_t)ptr
;
86 size
= pack
->BufferObj
->Size
;
87 /* The ARB_pixel_buffer_object spec says:
88 * "INVALID_OPERATION is generated by ColorTable, ColorSubTable,
89 * ConvolutionFilter2D, ConvolutionFilter1D, SeparableFilter2D,
90 * TexImage1D, TexImage2D, TexImage3D, TexSubImage1D,
91 * TexSubImage2D, TexSubImage3D, and DrawPixels if the current
92 * PIXEL_UNPACK_BUFFER_BINDING_ARB value is non-zero and the data
93 * parameter is not evenly divisible into the number of basic machine
94 * units needed to store in memory a datum indicated by the type
97 if (type
!= GL_BITMAP
&&
98 (offset
% _mesa_sizeof_packed_type(type
)))
106 /* get the offset to the first pixel we'll read/write */
107 start
= _mesa_image_offset(dimensions
, pack
, width
, height
,
108 format
, type
, 0, 0, 0);
110 /* get the offset to just past the last pixel we'll read/write */
111 end
= _mesa_image_offset(dimensions
, pack
, width
, height
,
112 format
, type
, depth
-1, height
-1, width
);
118 /* This will catch negative values / wrap-around */
122 /* Image read/write goes beyond end of buffer */
132 * For commands that read from a PBO (glDrawPixels, glTexImage,
133 * glPolygonStipple, etc), if we're reading from a PBO, map it read-only
134 * and return the pointer into the PBO. If we're not reading from a
135 * PBO, return \p src as-is.
136 * If non-null return, must call _mesa_unmap_pbo_source() when done.
138 * \return NULL if error, else pointer to start of data
141 _mesa_map_pbo_source(struct gl_context
*ctx
,
142 const struct gl_pixelstore_attrib
*unpack
,
147 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
148 /* unpack from PBO */
149 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0,
150 unpack
->BufferObj
->Size
,
157 buf
= ADD_POINTERS(buf
, src
);
160 /* unpack from normal memory */
169 * Combine PBO-read validation and mapping.
170 * If any GL errors are detected, they'll be recorded and NULL returned.
171 * \sa _mesa_validate_pbo_access
172 * \sa _mesa_map_pbo_source
173 * A call to this function should have a matching call to
174 * _mesa_unmap_pbo_source().
177 _mesa_map_validate_pbo_source(struct gl_context
*ctx
,
179 const struct gl_pixelstore_attrib
*unpack
,
180 GLsizei width
, GLsizei height
, GLsizei depth
,
181 GLenum format
, GLenum type
,
182 GLsizei clientMemSize
,
183 const GLvoid
*ptr
, const char *where
)
185 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
187 if (!_mesa_validate_pbo_access(dimensions
, unpack
, width
, height
, depth
,
188 format
, type
, clientMemSize
, ptr
)) {
189 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
190 _mesa_error(ctx
, GL_INVALID_OPERATION
,
191 "%s(out of bounds PBO access)", where
);
193 _mesa_error(ctx
, GL_INVALID_OPERATION
,
194 "%s(out of bounds access: bufSize (%d) is too small)",
195 where
, clientMemSize
);
200 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
201 /* non-PBO access: no further validation to be done */
205 if (_mesa_check_disallowed_mapping(unpack
->BufferObj
)) {
206 /* buffer is already mapped - that's an error */
207 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
211 ptr
= _mesa_map_pbo_source(ctx
, unpack
, ptr
);
217 * Counterpart to _mesa_map_pbo_source()
220 _mesa_unmap_pbo_source(struct gl_context
*ctx
,
221 const struct gl_pixelstore_attrib
*unpack
)
223 ASSERT(unpack
!= &ctx
->Pack
); /* catch pack/unpack mismatch */
224 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
225 ctx
->Driver
.UnmapBuffer(ctx
, unpack
->BufferObj
, MAP_INTERNAL
);
231 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
232 * if we're writing to a PBO, map it write-only and return the pointer
233 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
234 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
236 * \return NULL if error, else pointer to start of data
239 _mesa_map_pbo_dest(struct gl_context
*ctx
,
240 const struct gl_pixelstore_attrib
*pack
,
245 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
247 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0,
248 pack
->BufferObj
->Size
,
255 buf
= ADD_POINTERS(buf
, dest
);
258 /* pack to normal memory */
267 * Combine PBO-write validation and mapping.
268 * If any GL errors are detected, they'll be recorded and NULL returned.
269 * \sa _mesa_validate_pbo_access
270 * \sa _mesa_map_pbo_dest
271 * A call to this function should have a matching call to
272 * _mesa_unmap_pbo_dest().
275 _mesa_map_validate_pbo_dest(struct gl_context
*ctx
,
277 const struct gl_pixelstore_attrib
*unpack
,
278 GLsizei width
, GLsizei height
, GLsizei depth
,
279 GLenum format
, GLenum type
, GLsizei clientMemSize
,
280 GLvoid
*ptr
, const char *where
)
282 ASSERT(dimensions
== 1 || dimensions
== 2 || dimensions
== 3);
284 if (!_mesa_validate_pbo_access(dimensions
, unpack
, width
, height
, depth
,
285 format
, type
, clientMemSize
, ptr
)) {
286 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
287 _mesa_error(ctx
, GL_INVALID_OPERATION
,
288 "%s(out of bounds PBO access)", where
);
290 _mesa_error(ctx
, GL_INVALID_OPERATION
,
291 "%s(out of bounds access: bufSize (%d) is too small)",
292 where
, clientMemSize
);
297 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
298 /* non-PBO access: no further validation to be done */
302 if (_mesa_check_disallowed_mapping(unpack
->BufferObj
)) {
303 /* buffer is already mapped - that's an error */
304 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", where
);
308 ptr
= _mesa_map_pbo_dest(ctx
, unpack
, ptr
);
314 * Counterpart to _mesa_map_pbo_dest()
317 _mesa_unmap_pbo_dest(struct gl_context
*ctx
,
318 const struct gl_pixelstore_attrib
*pack
)
320 ASSERT(pack
!= &ctx
->Unpack
); /* catch pack/unpack mismatch */
321 if (_mesa_is_bufferobj(pack
->BufferObj
)) {
322 ctx
->Driver
.UnmapBuffer(ctx
, pack
->BufferObj
, MAP_INTERNAL
);
328 * Check if an unpack PBO is active prior to fetching a texture image.
329 * If so, do bounds checking and map the buffer into main memory.
330 * Any errors detected will be recorded.
331 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
334 _mesa_validate_pbo_teximage(struct gl_context
*ctx
, GLuint dimensions
,
335 GLsizei width
, GLsizei height
, GLsizei depth
,
336 GLenum format
, GLenum type
, const GLvoid
*pixels
,
337 const struct gl_pixelstore_attrib
*unpack
,
338 const char *funcName
)
342 if (!_mesa_is_bufferobj(unpack
->BufferObj
)) {
346 if (!_mesa_validate_pbo_access(dimensions
, unpack
, width
, height
, depth
,
347 format
, type
, INT_MAX
, pixels
)) {
348 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s%uD(invalid PBO access)",
349 funcName
, dimensions
);
353 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0,
354 unpack
->BufferObj
->Size
,
359 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s%uD(PBO is mapped)", funcName
,
364 return ADD_POINTERS(buf
, pixels
);
369 * Check if an unpack PBO is active prior to fetching a compressed texture
371 * If so, do bounds checking and map the buffer into main memory.
372 * Any errors detected will be recorded.
373 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
376 _mesa_validate_pbo_compressed_teximage(struct gl_context
*ctx
,
377 GLuint dimensions
, GLsizei imageSize
,
378 const GLvoid
*pixels
,
379 const struct gl_pixelstore_attrib
*packing
,
380 const char *funcName
)
384 if (!_mesa_is_bufferobj(packing
->BufferObj
)) {
385 /* not using a PBO - return pointer unchanged */
388 if ((const GLubyte
*) pixels
+ imageSize
>
389 ((const GLubyte
*) 0) + packing
->BufferObj
->Size
) {
390 /* out of bounds read! */
391 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s%uD(invalid PBO access)",
392 funcName
, dimensions
);
396 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0,
397 packing
->BufferObj
->Size
,
402 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s%uD(PBO is mapped)", funcName
,
407 return ADD_POINTERS(buf
, pixels
);
412 * This function must be called after either of the validate_pbo_*_teximage()
413 * functions. It unmaps the PBO buffer if it was mapped earlier.
416 _mesa_unmap_teximage_pbo(struct gl_context
*ctx
,
417 const struct gl_pixelstore_attrib
*unpack
)
419 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
420 ctx
->Driver
.UnmapBuffer(ctx
, unpack
->BufferObj
, MAP_INTERNAL
);