2 * Mesa 3-D graphics library
4 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Jason Ekstrand <jason.ekstrand@intel.com>
29 #include "bufferobj.h"
33 #include "glformats.h"
40 #include "shaderapi.h"
49 need_signed_unsigned_int_conversion(mesa_format mesaFormat
,
50 GLenum format
, GLenum type
)
52 const GLenum mesaFormatType
= _mesa_get_format_datatype(mesaFormat
);
53 const bool is_format_integer
= _mesa_is_enum_format_integer(format
);
54 return (mesaFormatType
== GL_INT
&&
56 (type
== GL_UNSIGNED_INT
||
57 type
== GL_UNSIGNED_SHORT
||
58 type
== GL_UNSIGNED_BYTE
)) ||
59 (mesaFormatType
== GL_UNSIGNED_INT
&&
66 static struct gl_texture_image
*
67 create_texture_for_pbo(struct gl_context
*ctx
,
68 bool create_pbo
, GLenum pbo_target
,
69 int dims
, int width
, int height
, int depth
,
70 GLenum format
, GLenum type
, const void *pixels
,
71 const struct gl_pixelstore_attrib
*packing
,
72 GLuint
*tmp_pbo
, GLuint
*tmp_tex
)
75 GLenum internal_format
;
77 struct gl_buffer_object
*buffer_obj
;
78 struct gl_texture_object
*tex_obj
;
79 struct gl_texture_image
*tex_image
;
82 if (packing
->SwapBytes
||
87 pbo_format
= _mesa_format_from_format_and_type(format
, type
);
88 if (_mesa_format_is_mesa_array_format(pbo_format
))
89 pbo_format
= _mesa_format_from_array_format(pbo_format
);
91 if (!pbo_format
|| !ctx
->TextureFormatSupported
[pbo_format
])
94 /* Account for SKIP_PIXELS, SKIP_ROWS, ALIGNMENT, and SKIP_IMAGES */
95 uint32_t first_pixel
= _mesa_image_offset(dims
, packing
, width
, height
,
98 uint32_t last_pixel
= _mesa_image_offset(dims
, packing
, width
, height
,
100 depth
-1, height
-1, width
);
101 row_stride
= _mesa_image_row_stride(packing
, width
, format
, type
);
103 if (_mesa_is_bufferobj(packing
->BufferObj
)) {
105 buffer_obj
= packing
->BufferObj
;
106 first_pixel
+= (intptr_t)pixels
;
108 bool is_pixel_pack
= pbo_target
== GL_PIXEL_PACK_BUFFER
;
112 _mesa_GenBuffers(1, tmp_pbo
);
114 /* We are not doing this inside meta_begin/end. However, we know the
115 * client doesn't have the given target bound, so we can go ahead and
116 * squash it. We'll set it back when we're done.
118 _mesa_BindBuffer(pbo_target
, *tmp_pbo
);
120 /* In case of GL_PIXEL_PACK_BUFFER, pass null pointer for the pixel
121 * data to avoid unnecessary data copying in _mesa_BufferData().
124 _mesa_BufferData(pbo_target
,
125 last_pixel
- first_pixel
,
129 _mesa_BufferData(pbo_target
,
130 last_pixel
- first_pixel
,
131 (char *)pixels
+ first_pixel
,
134 buffer_obj
= packing
->BufferObj
;
137 _mesa_BindBuffer(pbo_target
, 0);
140 _mesa_GenTextures(1, tmp_tex
);
141 tex_obj
= _mesa_lookup_texture(ctx
, *tmp_tex
);
142 _mesa_initialize_texture_object(ctx
, tex_obj
, *tmp_tex
, GL_TEXTURE_2D
);
143 /* This must be set after _mesa_initialize_texture_object, not before. */
144 tex_obj
->Immutable
= GL_TRUE
;
145 /* This is required for interactions with ARB_texture_view. */
146 tex_obj
->NumLayers
= 1;
148 internal_format
= _mesa_get_format_base_format(pbo_format
);
150 /* The texture is addressed as a single very-tall image, so we
151 * need to pack the multiple image depths together taking the
152 * inter-image padding into account.
154 int image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
155 int full_height
= image_height
* (depth
- 1) + height
;
157 tex_image
= _mesa_get_tex_image(ctx
, tex_obj
, tex_obj
->Target
, 0);
158 _mesa_init_teximage_fields(ctx
, tex_image
, width
, full_height
, 1,
159 0, internal_format
, pbo_format
);
161 read_only
= pbo_target
== GL_PIXEL_UNPACK_BUFFER
;
162 if (!ctx
->Driver
.SetTextureStorageForBufferObject(ctx
, tex_obj
,
167 _mesa_DeleteTextures(1, tmp_tex
);
168 _mesa_DeleteBuffers(1, tmp_pbo
);
176 _mesa_meta_pbo_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
177 struct gl_texture_image
*tex_image
,
178 int xoffset
, int yoffset
, int zoffset
,
179 int width
, int height
, int depth
,
180 GLenum format
, GLenum type
, const void *pixels
,
181 bool allocate_storage
, bool create_pbo
,
182 const struct gl_pixelstore_attrib
*packing
)
184 GLuint pbo
= 0, pbo_tex
= 0, fbos
[2] = { 0, 0 };
186 struct gl_texture_image
*pbo_tex_image
;
188 bool success
= false;
191 if (!_mesa_is_bufferobj(packing
->BufferObj
) &&
192 (!create_pbo
|| pixels
== NULL
))
195 if (format
== GL_DEPTH_COMPONENT
||
196 format
== GL_DEPTH_STENCIL
||
197 format
== GL_STENCIL_INDEX
||
198 format
== GL_COLOR_INDEX
)
201 if (ctx
->_ImageTransferState
)
204 /* This function rely on BlitFramebuffer to fill in the pixel data for
205 * glTex[Sub]Image*D. But, BlitFrameBuffer doesn't support signed to
206 * unsigned or unsigned to signed integer conversions.
208 if (need_signed_unsigned_int_conversion(tex_image
->TexFormat
, format
, type
))
211 /* For arrays, use a tall (height * depth) 2D texture but taking into
212 * account the inter-image padding specified with the image height packing
215 image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
217 pbo_tex_image
= create_texture_for_pbo(ctx
, create_pbo
,
218 GL_PIXEL_UNPACK_BUFFER
,
219 dims
, width
, height
, depth
,
220 format
, type
, pixels
, packing
,
225 if (allocate_storage
)
226 ctx
->Driver
.AllocTextureImageBuffer(ctx
, tex_image
);
228 _mesa_meta_begin(ctx
, ~(MESA_META_PIXEL_TRANSFER
|
229 MESA_META_PIXEL_STORE
));
231 _mesa_GenFramebuffers(2, fbos
);
232 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, fbos
[0]);
233 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbos
[1]);
235 if (tex_image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
237 assert(zoffset
== 0);
245 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
247 /* If this passes on the first layer it should pass on the others */
248 status
= _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER
);
249 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
252 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
254 /* If this passes on the first layer it should pass on the others */
255 status
= _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER
);
256 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
259 _mesa_update_state(ctx
);
261 if (_mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
264 xoffset
+ width
, yoffset
+ height
,
265 GL_COLOR_BUFFER_BIT
, GL_NEAREST
))
268 for (z
= 1; z
< depth
; z
++) {
269 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
270 tex_image
, zoffset
+ z
);
272 _mesa_update_state(ctx
);
274 _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
276 width
, z
* image_height
+ height
,
278 xoffset
+ width
, yoffset
+ height
,
279 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
285 _mesa_DeleteFramebuffers(2, fbos
);
286 _mesa_DeleteTextures(1, &pbo_tex
);
287 _mesa_DeleteBuffers(1, &pbo
);
295 _mesa_meta_pbo_GetTexSubImage(struct gl_context
*ctx
, GLuint dims
,
296 struct gl_texture_image
*tex_image
,
297 int xoffset
, int yoffset
, int zoffset
,
298 int width
, int height
, int depth
,
299 GLenum format
, GLenum type
, const void *pixels
,
300 const struct gl_pixelstore_attrib
*packing
)
302 GLuint pbo
= 0, pbo_tex
= 0, fbos
[2] = { 0, 0 };
304 struct gl_texture_image
*pbo_tex_image
;
305 struct gl_renderbuffer
*rb
= NULL
;
306 GLenum dstBaseFormat
= _mesa_unpack_format_to_base_format(format
);
307 GLenum status
, src_base_format
;
308 bool success
= false, clear_channels_to_zero
= false;
309 float save_clear_color
[4];
312 if (!_mesa_is_bufferobj(packing
->BufferObj
))
315 if (format
== GL_DEPTH_COMPONENT
||
316 format
== GL_DEPTH_STENCIL
||
317 format
== GL_STENCIL_INDEX
||
318 format
== GL_COLOR_INDEX
)
321 /* Don't use meta path for readpixels in below conditions. */
323 rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
325 /* _mesa_get_readpixels_transfer_ops() includes the cases of read
326 * color clamping along with the ctx->_ImageTransferState.
328 if (_mesa_get_readpixels_transfer_ops(ctx
, rb
->Format
, format
,
332 if (_mesa_need_rgb_to_luminance_conversion(rb
->_BaseFormat
,
336 /* This function rely on BlitFramebuffer to fill in the pixel data for
337 * ReadPixels. But, BlitFrameBuffer doesn't support signed to unsigned
338 * or unsigned to signed integer conversions. OpenGL spec expects an
339 * invalid operation in that case.
341 if (need_signed_unsigned_int_conversion(rb
->Format
, format
, type
))
345 /* For arrays, use a tall (height * depth) 2D texture but taking into
346 * account the inter-image padding specified with the image height packing
349 image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
351 pbo_tex_image
= create_texture_for_pbo(ctx
, false, GL_PIXEL_PACK_BUFFER
,
352 dims
, width
, height
, depth
,
353 format
, type
, pixels
, packing
,
358 _mesa_meta_begin(ctx
, ~(MESA_META_PIXEL_TRANSFER
|
359 MESA_META_PIXEL_STORE
));
361 /* GL_CLAMP_FRAGMENT_COLOR doesn't affect ReadPixels and GettexImage */
362 if (ctx
->Extensions
.ARB_color_buffer_float
)
363 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
365 _mesa_GenFramebuffers(2, fbos
);
367 if (tex_image
&& tex_image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
369 assert(zoffset
== 0);
377 /* If we were given a texture, bind it to the read framebuffer. If not,
378 * we're doing a ReadPixels and we should just use whatever framebuffer
379 * the client has bound.
382 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, fbos
[0]);
383 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
385 /* If this passes on the first layer it should pass on the others */
386 status
= _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER
);
387 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
393 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbos
[1]);
394 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
396 /* If this passes on the first layer it should pass on the others */
397 status
= _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER
);
398 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
401 _mesa_update_state(ctx
);
403 if (_mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
405 xoffset
+ width
, yoffset
+ height
,
407 GL_COLOR_BUFFER_BIT
, GL_NEAREST
))
410 src_base_format
= tex_image
?
411 tex_image
->_BaseFormat
:
412 ctx
->ReadBuffer
->_ColorReadBuffer
->_BaseFormat
;
414 /* Depending on the base formats involved we might need to rebase some
415 * values. For example if we download from a Luminance format to RGBA
416 * format, we want G=0 and B=0.
418 clear_channels_to_zero
=
419 _mesa_need_luminance_to_rgb_conversion(src_base_format
,
420 pbo_tex_image
->_BaseFormat
);
422 if (clear_channels_to_zero
) {
423 memcpy(save_clear_color
, ctx
->Color
.ClearColor
.f
, 4 * sizeof(float));
424 /* Clear the Green, Blue channels. */
425 _mesa_ColorMask(GL_FALSE
, GL_TRUE
, GL_TRUE
,
426 src_base_format
!= GL_LUMINANCE_ALPHA
);
427 _mesa_ClearColor(0.0, 0.0, 0.0, 1.0);
428 _mesa_Clear(GL_COLOR_BUFFER_BIT
);
431 for (z
= 1; z
< depth
; z
++) {
432 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
433 tex_image
, zoffset
+ z
);
435 _mesa_update_state(ctx
);
437 _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
439 xoffset
+ width
, yoffset
+ height
,
441 width
, z
* image_height
+ height
,
442 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
443 if (clear_channels_to_zero
)
444 _mesa_Clear(GL_COLOR_BUFFER_BIT
);
447 /* Unmask the color channels and restore the saved clear color values. */
448 if (clear_channels_to_zero
) {
449 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
450 _mesa_ClearColor(save_clear_color
[0], save_clear_color
[1],
451 save_clear_color
[2], save_clear_color
[3]);
457 _mesa_DeleteFramebuffers(2, fbos
);
458 _mesa_DeleteTextures(1, &pbo_tex
);
459 _mesa_DeleteBuffers(1, &pbo
);