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 "framebuffer.h"
34 #include "glformats.h"
41 #include "shaderapi.h"
50 need_signed_unsigned_int_conversion(mesa_format mesaFormat
,
51 GLenum format
, GLenum type
)
53 const GLenum mesaFormatType
= _mesa_get_format_datatype(mesaFormat
);
54 const bool is_format_integer
= _mesa_is_enum_format_integer(format
);
55 return (mesaFormatType
== GL_INT
&&
57 (type
== GL_UNSIGNED_INT
||
58 type
== GL_UNSIGNED_SHORT
||
59 type
== GL_UNSIGNED_BYTE
)) ||
60 (mesaFormatType
== GL_UNSIGNED_INT
&&
67 static struct gl_texture_image
*
68 create_texture_for_pbo(struct gl_context
*ctx
,
69 bool create_pbo
, GLenum pbo_target
,
70 int dims
, int width
, int height
, int depth
,
71 GLenum format
, GLenum type
, const void *pixels
,
72 const struct gl_pixelstore_attrib
*packing
,
73 struct gl_buffer_object
**tmp_pbo
, GLuint
*tmp_tex
)
76 GLenum internal_format
;
78 struct gl_buffer_object
*buffer_obj
;
79 struct gl_texture_object
*tex_obj
;
80 struct gl_texture_image
*tex_image
;
83 if (packing
->SwapBytes
||
88 pbo_format
= _mesa_format_from_format_and_type(format
, type
);
89 if (_mesa_format_is_mesa_array_format(pbo_format
))
90 pbo_format
= _mesa_format_from_array_format(pbo_format
);
92 if (!pbo_format
|| !ctx
->TextureFormatSupported
[pbo_format
])
95 /* Account for SKIP_PIXELS, SKIP_ROWS, ALIGNMENT, and SKIP_IMAGES */
96 uint32_t first_pixel
= _mesa_image_offset(dims
, packing
, width
, height
,
99 uint32_t last_pixel
= _mesa_image_offset(dims
, packing
, width
, height
,
101 depth
-1, height
-1, width
);
102 row_stride
= _mesa_image_row_stride(packing
, width
, format
, type
);
104 if (_mesa_is_bufferobj(packing
->BufferObj
)) {
106 buffer_obj
= packing
->BufferObj
;
107 first_pixel
+= (intptr_t)pixels
;
109 bool is_pixel_pack
= pbo_target
== GL_PIXEL_PACK_BUFFER
;
113 *tmp_pbo
= ctx
->Driver
.NewBufferObject(ctx
, 0xDEADBEEF);
114 if (*tmp_pbo
== NULL
)
117 /* In case of GL_PIXEL_PACK_BUFFER, pass null pointer for the pixel
118 * data to avoid unnecessary data copying in _mesa_buffer_data.
121 _mesa_buffer_data(ctx
, *tmp_pbo
, GL_NONE
,
122 last_pixel
- first_pixel
,
127 _mesa_buffer_data(ctx
, *tmp_pbo
, GL_NONE
,
128 last_pixel
- first_pixel
,
129 (char *)pixels
+ first_pixel
,
133 buffer_obj
= *tmp_pbo
;
137 _mesa_GenTextures(1, tmp_tex
);
138 tex_obj
= _mesa_lookup_texture(ctx
, *tmp_tex
);
139 _mesa_initialize_texture_object(ctx
, tex_obj
, *tmp_tex
, GL_TEXTURE_2D
);
140 /* This must be set after _mesa_initialize_texture_object, not before. */
141 tex_obj
->Immutable
= GL_TRUE
;
142 /* This is required for interactions with ARB_texture_view. */
143 tex_obj
->NumLayers
= 1;
145 internal_format
= _mesa_get_format_base_format(pbo_format
);
147 /* The texture is addressed as a single very-tall image, so we
148 * need to pack the multiple image depths together taking the
149 * inter-image padding into account.
151 int image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
152 int full_height
= image_height
* (depth
- 1) + height
;
154 tex_image
= _mesa_get_tex_image(ctx
, tex_obj
, tex_obj
->Target
, 0);
155 _mesa_init_teximage_fields(ctx
, tex_image
, width
, full_height
, 1,
156 0, internal_format
, pbo_format
);
158 read_only
= pbo_target
== GL_PIXEL_UNPACK_BUFFER
;
159 if (!ctx
->Driver
.SetTextureStorageForBufferObject(ctx
, tex_obj
,
164 _mesa_DeleteTextures(1, tmp_tex
);
165 _mesa_reference_buffer_object(ctx
, tmp_pbo
, NULL
);
173 _mesa_meta_pbo_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
174 struct gl_texture_image
*tex_image
,
175 int xoffset
, int yoffset
, int zoffset
,
176 int width
, int height
, int depth
,
177 GLenum format
, GLenum type
, const void *pixels
,
179 const struct gl_pixelstore_attrib
*packing
)
181 struct gl_buffer_object
*pbo
= NULL
;
183 struct gl_framebuffer
*readFb
= NULL
;
184 struct gl_framebuffer
*drawFb
= NULL
;
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 _mesa_meta_begin(ctx
, ~(MESA_META_PIXEL_TRANSFER
|
218 MESA_META_PIXEL_STORE
));
220 pbo_tex_image
= create_texture_for_pbo(ctx
, create_pbo
,
221 GL_PIXEL_UNPACK_BUFFER
,
222 dims
, width
, height
, depth
,
223 format
, type
, pixels
, packing
,
225 if (!pbo_tex_image
) {
230 readFb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
234 drawFb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
238 _mesa_bind_framebuffers(ctx
, drawFb
, tex_image
? readFb
: ctx
->ReadBuffer
);
240 if (tex_image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
242 assert(zoffset
== 0);
250 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->ReadBuffer
,
251 GL_COLOR_ATTACHMENT0
,
253 /* If this passes on the first layer it should pass on the others */
254 status
= _mesa_check_framebuffer_status(ctx
, ctx
->ReadBuffer
);
255 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
258 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
259 GL_COLOR_ATTACHMENT0
,
261 /* If this passes on the first layer it should pass on the others */
262 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
263 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
266 _mesa_update_state(ctx
);
268 if (_mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
271 xoffset
+ width
, yoffset
+ height
,
272 GL_COLOR_BUFFER_BIT
, GL_NEAREST
))
275 for (z
= 1; z
< depth
; z
++) {
276 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
277 GL_COLOR_ATTACHMENT0
,
278 tex_image
, zoffset
+ z
);
280 _mesa_update_state(ctx
);
282 _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
284 width
, z
* image_height
+ height
,
286 xoffset
+ width
, yoffset
+ height
,
287 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
293 _mesa_reference_framebuffer(&readFb
, NULL
);
294 _mesa_reference_framebuffer(&drawFb
, NULL
);
295 _mesa_DeleteTextures(1, &pbo_tex
);
296 _mesa_reference_buffer_object(ctx
, &pbo
, NULL
);
304 _mesa_meta_pbo_GetTexSubImage(struct gl_context
*ctx
, GLuint dims
,
305 struct gl_texture_image
*tex_image
,
306 int xoffset
, int yoffset
, int zoffset
,
307 int width
, int height
, int depth
,
308 GLenum format
, GLenum type
, const void *pixels
,
309 const struct gl_pixelstore_attrib
*packing
)
311 struct gl_buffer_object
*pbo
= NULL
;
313 struct gl_framebuffer
*readFb
;
314 struct gl_framebuffer
*drawFb
;
316 struct gl_texture_image
*pbo_tex_image
;
317 struct gl_renderbuffer
*rb
= NULL
;
318 GLenum dstBaseFormat
= _mesa_unpack_format_to_base_format(format
);
319 GLenum status
, src_base_format
;
320 bool success
= false, clear_channels_to_zero
= false;
321 float save_clear_color
[4];
324 if (!_mesa_is_bufferobj(packing
->BufferObj
))
327 if (format
== GL_DEPTH_COMPONENT
||
328 format
== GL_DEPTH_STENCIL
||
329 format
== GL_STENCIL_INDEX
||
330 format
== GL_COLOR_INDEX
)
333 /* Don't use meta path for readpixels in below conditions. */
335 rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
337 /* _mesa_get_readpixels_transfer_ops() includes the cases of read
338 * color clamping along with the ctx->_ImageTransferState.
340 if (_mesa_get_readpixels_transfer_ops(ctx
, rb
->Format
, format
,
344 if (_mesa_need_rgb_to_luminance_conversion(rb
->_BaseFormat
,
348 /* This function rely on BlitFramebuffer to fill in the pixel data for
349 * ReadPixels. But, BlitFrameBuffer doesn't support signed to unsigned
350 * or unsigned to signed integer conversions. OpenGL spec expects an
351 * invalid operation in that case.
353 if (need_signed_unsigned_int_conversion(rb
->Format
, format
, type
))
357 /* For arrays, use a tall (height * depth) 2D texture but taking into
358 * account the inter-image padding specified with the image height packing
361 image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
363 _mesa_meta_begin(ctx
, ~(MESA_META_PIXEL_TRANSFER
|
364 MESA_META_PIXEL_STORE
));
366 pbo_tex_image
= create_texture_for_pbo(ctx
, false, GL_PIXEL_PACK_BUFFER
,
367 dims
, width
, height
, depth
,
368 format
, type
, pixels
, packing
,
371 if (!pbo_tex_image
) {
376 /* GL_CLAMP_FRAGMENT_COLOR doesn't affect ReadPixels and GettexImage */
377 if (ctx
->Extensions
.ARB_color_buffer_float
)
378 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
380 readFb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
384 drawFb
= ctx
->Driver
.NewFramebuffer(ctx
, 0xDEADBEEF);
388 if (tex_image
&& tex_image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
390 assert(zoffset
== 0);
398 /* If we were given a texture, bind it to the read framebuffer. If not,
399 * we're doing a ReadPixels and we should just use whatever framebuffer
400 * the client has bound.
402 _mesa_bind_framebuffers(ctx
, drawFb
, tex_image
? readFb
: ctx
->ReadBuffer
);
404 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->ReadBuffer
,
405 GL_COLOR_ATTACHMENT0
,
407 /* If this passes on the first layer it should pass on the others */
408 status
= _mesa_check_framebuffer_status(ctx
, ctx
->ReadBuffer
);
409 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
415 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
416 GL_COLOR_ATTACHMENT0
,
418 /* If this passes on the first layer it should pass on the others */
419 status
= _mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
);
420 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
423 _mesa_update_state(ctx
);
425 if (_mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
427 xoffset
+ width
, yoffset
+ height
,
429 GL_COLOR_BUFFER_BIT
, GL_NEAREST
))
432 src_base_format
= tex_image
?
433 tex_image
->_BaseFormat
:
434 ctx
->ReadBuffer
->_ColorReadBuffer
->_BaseFormat
;
436 /* Depending on the base formats involved we might need to rebase some
437 * values. For example if we download from a Luminance format to RGBA
438 * format, we want G=0 and B=0.
440 clear_channels_to_zero
=
441 _mesa_need_luminance_to_rgb_conversion(src_base_format
,
442 pbo_tex_image
->_BaseFormat
);
444 if (clear_channels_to_zero
) {
445 memcpy(save_clear_color
, ctx
->Color
.ClearColor
.f
, 4 * sizeof(float));
446 /* Clear the Green, Blue channels. */
447 _mesa_ColorMask(GL_FALSE
, GL_TRUE
, GL_TRUE
,
448 src_base_format
!= GL_LUMINANCE_ALPHA
);
449 _mesa_ClearColor(0.0, 0.0, 0.0, 1.0);
450 _mesa_Clear(GL_COLOR_BUFFER_BIT
);
453 for (z
= 1; z
< depth
; z
++) {
454 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->ReadBuffer
,
455 GL_COLOR_ATTACHMENT0
,
456 tex_image
, zoffset
+ z
);
458 _mesa_update_state(ctx
);
460 _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
462 xoffset
+ width
, yoffset
+ height
,
464 width
, z
* image_height
+ height
,
465 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
466 if (clear_channels_to_zero
)
467 _mesa_Clear(GL_COLOR_BUFFER_BIT
);
470 /* Unmask the color channels and restore the saved clear color values. */
471 if (clear_channels_to_zero
) {
472 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
473 _mesa_ClearColor(save_clear_color
[0], save_clear_color
[1],
474 save_clear_color
[2], save_clear_color
[3]);
480 _mesa_reference_framebuffer(&drawFb
, NULL
);
481 _mesa_reference_framebuffer(&readFb
, NULL
);
482 _mesa_DeleteTextures(1, &pbo_tex
);
483 _mesa_reference_buffer_object(ctx
, &pbo
, NULL
);