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 struct gl_buffer_object
**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 *tmp_pbo
= ctx
->Driver
.NewBufferObject(ctx
, 0xDEADBEEF);
113 if (*tmp_pbo
== NULL
)
116 /* In case of GL_PIXEL_PACK_BUFFER, pass null pointer for the pixel
117 * data to avoid unnecessary data copying in _mesa_buffer_data.
120 _mesa_buffer_data(ctx
, *tmp_pbo
, GL_NONE
,
121 last_pixel
- first_pixel
,
126 _mesa_buffer_data(ctx
, *tmp_pbo
, GL_NONE
,
127 last_pixel
- first_pixel
,
128 (char *)pixels
+ first_pixel
,
132 buffer_obj
= *tmp_pbo
;
136 _mesa_GenTextures(1, tmp_tex
);
137 tex_obj
= _mesa_lookup_texture(ctx
, *tmp_tex
);
138 _mesa_initialize_texture_object(ctx
, tex_obj
, *tmp_tex
, GL_TEXTURE_2D
);
139 /* This must be set after _mesa_initialize_texture_object, not before. */
140 tex_obj
->Immutable
= GL_TRUE
;
141 /* This is required for interactions with ARB_texture_view. */
142 tex_obj
->NumLayers
= 1;
144 internal_format
= _mesa_get_format_base_format(pbo_format
);
146 /* The texture is addressed as a single very-tall image, so we
147 * need to pack the multiple image depths together taking the
148 * inter-image padding into account.
150 int image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
151 int full_height
= image_height
* (depth
- 1) + height
;
153 tex_image
= _mesa_get_tex_image(ctx
, tex_obj
, tex_obj
->Target
, 0);
154 _mesa_init_teximage_fields(ctx
, tex_image
, width
, full_height
, 1,
155 0, internal_format
, pbo_format
);
157 read_only
= pbo_target
== GL_PIXEL_UNPACK_BUFFER
;
158 if (!ctx
->Driver
.SetTextureStorageForBufferObject(ctx
, tex_obj
,
163 _mesa_DeleteTextures(1, tmp_tex
);
164 _mesa_reference_buffer_object(ctx
, tmp_pbo
, NULL
);
172 _mesa_meta_pbo_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
173 struct gl_texture_image
*tex_image
,
174 int xoffset
, int yoffset
, int zoffset
,
175 int width
, int height
, int depth
,
176 GLenum format
, GLenum type
, const void *pixels
,
177 bool allocate_storage
, bool create_pbo
,
178 const struct gl_pixelstore_attrib
*packing
)
180 struct gl_buffer_object
*pbo
= NULL
;
181 GLuint pbo_tex
= 0, fbos
[2] = { 0, 0 };
183 struct gl_texture_image
*pbo_tex_image
;
185 bool success
= false;
188 if (!_mesa_is_bufferobj(packing
->BufferObj
) &&
189 (!create_pbo
|| pixels
== NULL
))
192 if (format
== GL_DEPTH_COMPONENT
||
193 format
== GL_DEPTH_STENCIL
||
194 format
== GL_STENCIL_INDEX
||
195 format
== GL_COLOR_INDEX
)
198 if (ctx
->_ImageTransferState
)
201 /* This function rely on BlitFramebuffer to fill in the pixel data for
202 * glTex[Sub]Image*D. But, BlitFrameBuffer doesn't support signed to
203 * unsigned or unsigned to signed integer conversions.
205 if (need_signed_unsigned_int_conversion(tex_image
->TexFormat
, format
, type
))
208 /* For arrays, use a tall (height * depth) 2D texture but taking into
209 * account the inter-image padding specified with the image height packing
212 image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
214 pbo_tex_image
= create_texture_for_pbo(ctx
, create_pbo
,
215 GL_PIXEL_UNPACK_BUFFER
,
216 dims
, width
, height
, depth
,
217 format
, type
, pixels
, packing
,
222 if (allocate_storage
)
223 ctx
->Driver
.AllocTextureImageBuffer(ctx
, tex_image
);
225 _mesa_meta_begin(ctx
, ~(MESA_META_PIXEL_TRANSFER
|
226 MESA_META_PIXEL_STORE
));
228 _mesa_GenFramebuffers(2, fbos
);
229 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, fbos
[0]);
230 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbos
[1]);
232 if (tex_image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
234 assert(zoffset
== 0);
242 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
244 /* If this passes on the first layer it should pass on the others */
245 status
= _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER
);
246 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
249 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
251 /* If this passes on the first layer it should pass on the others */
252 status
= _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER
);
253 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
256 _mesa_update_state(ctx
);
258 if (_mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
261 xoffset
+ width
, yoffset
+ height
,
262 GL_COLOR_BUFFER_BIT
, GL_NEAREST
))
265 for (z
= 1; z
< depth
; z
++) {
266 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
267 tex_image
, zoffset
+ z
);
269 _mesa_update_state(ctx
);
271 _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
273 width
, z
* image_height
+ height
,
275 xoffset
+ width
, yoffset
+ height
,
276 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
282 _mesa_DeleteFramebuffers(2, fbos
);
283 _mesa_DeleteTextures(1, &pbo_tex
);
284 _mesa_reference_buffer_object(ctx
, &pbo
, NULL
);
292 _mesa_meta_pbo_GetTexSubImage(struct gl_context
*ctx
, GLuint dims
,
293 struct gl_texture_image
*tex_image
,
294 int xoffset
, int yoffset
, int zoffset
,
295 int width
, int height
, int depth
,
296 GLenum format
, GLenum type
, const void *pixels
,
297 const struct gl_pixelstore_attrib
*packing
)
299 struct gl_buffer_object
*pbo
= NULL
;
300 GLuint pbo_tex
= 0, fbos
[2] = { 0, 0 };
302 struct gl_texture_image
*pbo_tex_image
;
303 struct gl_renderbuffer
*rb
= NULL
;
304 GLenum dstBaseFormat
= _mesa_unpack_format_to_base_format(format
);
305 GLenum status
, src_base_format
;
306 bool success
= false, clear_channels_to_zero
= false;
307 float save_clear_color
[4];
310 if (!_mesa_is_bufferobj(packing
->BufferObj
))
313 if (format
== GL_DEPTH_COMPONENT
||
314 format
== GL_DEPTH_STENCIL
||
315 format
== GL_STENCIL_INDEX
||
316 format
== GL_COLOR_INDEX
)
319 /* Don't use meta path for readpixels in below conditions. */
321 rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
323 /* _mesa_get_readpixels_transfer_ops() includes the cases of read
324 * color clamping along with the ctx->_ImageTransferState.
326 if (_mesa_get_readpixels_transfer_ops(ctx
, rb
->Format
, format
,
330 if (_mesa_need_rgb_to_luminance_conversion(rb
->_BaseFormat
,
334 /* This function rely on BlitFramebuffer to fill in the pixel data for
335 * ReadPixels. But, BlitFrameBuffer doesn't support signed to unsigned
336 * or unsigned to signed integer conversions. OpenGL spec expects an
337 * invalid operation in that case.
339 if (need_signed_unsigned_int_conversion(rb
->Format
, format
, type
))
343 /* For arrays, use a tall (height * depth) 2D texture but taking into
344 * account the inter-image padding specified with the image height packing
347 image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
349 pbo_tex_image
= create_texture_for_pbo(ctx
, false, GL_PIXEL_PACK_BUFFER
,
350 dims
, width
, height
, depth
,
351 format
, type
, pixels
, packing
,
356 _mesa_meta_begin(ctx
, ~(MESA_META_PIXEL_TRANSFER
|
357 MESA_META_PIXEL_STORE
));
359 /* GL_CLAMP_FRAGMENT_COLOR doesn't affect ReadPixels and GettexImage */
360 if (ctx
->Extensions
.ARB_color_buffer_float
)
361 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
363 _mesa_GenFramebuffers(2, fbos
);
365 if (tex_image
&& tex_image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
367 assert(zoffset
== 0);
375 /* If we were given a texture, bind it to the read framebuffer. If not,
376 * we're doing a ReadPixels and we should just use whatever framebuffer
377 * the client has bound.
380 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, fbos
[0]);
381 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
383 /* If this passes on the first layer it should pass on the others */
384 status
= _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER
);
385 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
391 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbos
[1]);
392 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
394 /* If this passes on the first layer it should pass on the others */
395 status
= _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER
);
396 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
399 _mesa_update_state(ctx
);
401 if (_mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
403 xoffset
+ width
, yoffset
+ height
,
405 GL_COLOR_BUFFER_BIT
, GL_NEAREST
))
408 src_base_format
= tex_image
?
409 tex_image
->_BaseFormat
:
410 ctx
->ReadBuffer
->_ColorReadBuffer
->_BaseFormat
;
412 /* Depending on the base formats involved we might need to rebase some
413 * values. For example if we download from a Luminance format to RGBA
414 * format, we want G=0 and B=0.
416 clear_channels_to_zero
=
417 _mesa_need_luminance_to_rgb_conversion(src_base_format
,
418 pbo_tex_image
->_BaseFormat
);
420 if (clear_channels_to_zero
) {
421 memcpy(save_clear_color
, ctx
->Color
.ClearColor
.f
, 4 * sizeof(float));
422 /* Clear the Green, Blue channels. */
423 _mesa_ColorMask(GL_FALSE
, GL_TRUE
, GL_TRUE
,
424 src_base_format
!= GL_LUMINANCE_ALPHA
);
425 _mesa_ClearColor(0.0, 0.0, 0.0, 1.0);
426 _mesa_Clear(GL_COLOR_BUFFER_BIT
);
429 for (z
= 1; z
< depth
; z
++) {
430 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
431 tex_image
, zoffset
+ z
);
433 _mesa_update_state(ctx
);
435 _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
437 xoffset
+ width
, yoffset
+ height
,
439 width
, z
* image_height
+ height
,
440 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
441 if (clear_channels_to_zero
)
442 _mesa_Clear(GL_COLOR_BUFFER_BIT
);
445 /* Unmask the color channels and restore the saved clear color values. */
446 if (clear_channels_to_zero
) {
447 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
448 _mesa_ClearColor(save_clear_color
[0], save_clear_color
[1],
449 save_clear_color
[2], save_clear_color
[3]);
455 _mesa_DeleteFramebuffers(2, fbos
);
456 _mesa_DeleteTextures(1, &pbo_tex
);
457 _mesa_reference_buffer_object(ctx
, &pbo
, NULL
);