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"
48 static struct gl_texture_image
*
49 create_texture_for_pbo(struct gl_context
*ctx
,
50 bool create_pbo
, GLenum pbo_target
,
51 int dims
, int width
, int height
, int depth
,
52 GLenum format
, GLenum type
, const void *pixels
,
53 const struct gl_pixelstore_attrib
*packing
,
54 GLuint
*tmp_pbo
, GLuint
*tmp_tex
)
57 GLenum internal_format
;
59 struct gl_buffer_object
*buffer_obj
;
60 struct gl_texture_object
*tex_obj
;
61 struct gl_texture_image
*tex_image
;
64 if (packing
->SwapBytes
||
69 pbo_format
= _mesa_format_from_format_and_type(format
, type
);
70 if (_mesa_format_is_mesa_array_format(pbo_format
))
71 pbo_format
= _mesa_format_from_array_format(pbo_format
);
73 if (!pbo_format
|| !ctx
->TextureFormatSupported
[pbo_format
])
76 /* Account for SKIP_PIXELS, SKIP_ROWS, ALIGNMENT, and SKIP_IMAGES */
77 uint32_t first_pixel
= _mesa_image_offset(dims
, packing
, width
, height
,
80 uint32_t last_pixel
= _mesa_image_offset(dims
, packing
, width
, height
,
82 depth
-1, height
-1, width
);
83 row_stride
= _mesa_image_row_stride(packing
, width
, format
, type
);
85 if (_mesa_is_bufferobj(packing
->BufferObj
)) {
87 buffer_obj
= packing
->BufferObj
;
88 first_pixel
+= (intptr_t)pixels
;
90 bool is_pixel_pack
= pbo_target
== GL_PIXEL_PACK_BUFFER
;
94 _mesa_GenBuffers(1, tmp_pbo
);
96 /* We are not doing this inside meta_begin/end. However, we know the
97 * client doesn't have the given target bound, so we can go ahead and
98 * squash it. We'll set it back when we're done.
100 _mesa_BindBuffer(pbo_target
, *tmp_pbo
);
102 /* In case of GL_PIXEL_PACK_BUFFER, pass null pointer for the pixel
103 * data to avoid unnecessary data copying in _mesa_BufferData().
106 _mesa_BufferData(pbo_target
,
107 last_pixel
- first_pixel
,
111 _mesa_BufferData(pbo_target
,
112 last_pixel
- first_pixel
,
113 (char *)pixels
+ first_pixel
,
116 buffer_obj
= packing
->BufferObj
;
119 _mesa_BindBuffer(pbo_target
, 0);
122 _mesa_GenTextures(1, tmp_tex
);
123 tex_obj
= _mesa_lookup_texture(ctx
, *tmp_tex
);
124 _mesa_initialize_texture_object(ctx
, tex_obj
, *tmp_tex
, GL_TEXTURE_2D
);
125 /* This must be set after _mesa_initialize_texture_object, not before. */
126 tex_obj
->Immutable
= GL_TRUE
;
127 /* This is required for interactions with ARB_texture_view. */
128 tex_obj
->NumLayers
= 1;
130 internal_format
= _mesa_get_format_base_format(pbo_format
);
132 /* The texture is addressed as a single very-tall image, so we
133 * need to pack the multiple image depths together taking the
134 * inter-image padding into account.
136 int image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
137 int full_height
= image_height
* (depth
- 1) + height
;
139 tex_image
= _mesa_get_tex_image(ctx
, tex_obj
, tex_obj
->Target
, 0);
140 _mesa_init_teximage_fields(ctx
, tex_image
, width
, full_height
, 1,
141 0, internal_format
, pbo_format
);
143 read_only
= pbo_target
== GL_PIXEL_UNPACK_BUFFER
;
144 if (!ctx
->Driver
.SetTextureStorageForBufferObject(ctx
, tex_obj
,
149 _mesa_DeleteTextures(1, tmp_tex
);
150 _mesa_DeleteBuffers(1, tmp_pbo
);
158 _mesa_meta_pbo_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
159 struct gl_texture_image
*tex_image
,
160 int xoffset
, int yoffset
, int zoffset
,
161 int width
, int height
, int depth
,
162 GLenum format
, GLenum type
, const void *pixels
,
163 bool allocate_storage
, bool create_pbo
,
164 const struct gl_pixelstore_attrib
*packing
)
166 GLuint pbo
= 0, pbo_tex
= 0, fbos
[2] = { 0, 0 };
168 struct gl_texture_image
*pbo_tex_image
;
170 bool success
= false;
173 if (!_mesa_is_bufferobj(packing
->BufferObj
) &&
174 (!create_pbo
|| pixels
== NULL
))
177 if (format
== GL_DEPTH_COMPONENT
||
178 format
== GL_DEPTH_STENCIL
||
179 format
== GL_STENCIL_INDEX
||
180 format
== GL_COLOR_INDEX
)
183 if (ctx
->_ImageTransferState
)
186 /* For arrays, use a tall (height * depth) 2D texture but taking into
187 * account the inter-image padding specified with the image height packing
190 image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
192 pbo_tex_image
= create_texture_for_pbo(ctx
, create_pbo
,
193 GL_PIXEL_UNPACK_BUFFER
,
194 dims
, width
, height
, depth
,
195 format
, type
, pixels
, packing
,
200 if (allocate_storage
)
201 ctx
->Driver
.AllocTextureImageBuffer(ctx
, tex_image
);
203 _mesa_meta_begin(ctx
, ~(MESA_META_PIXEL_TRANSFER
|
204 MESA_META_PIXEL_STORE
));
206 _mesa_GenFramebuffers(2, fbos
);
207 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, fbos
[0]);
208 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbos
[1]);
210 if (tex_image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
212 assert(zoffset
== 0);
220 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
222 /* If this passes on the first layer it should pass on the others */
223 status
= _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER
);
224 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
227 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
229 /* If this passes on the first layer it should pass on the others */
230 status
= _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER
);
231 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
234 _mesa_update_state(ctx
);
236 if (_mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
239 xoffset
+ width
, yoffset
+ height
,
240 GL_COLOR_BUFFER_BIT
, GL_NEAREST
))
243 for (z
= 1; z
< depth
; z
++) {
244 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
245 tex_image
, zoffset
+ z
);
247 _mesa_update_state(ctx
);
249 _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
251 width
, z
* image_height
+ height
,
253 xoffset
+ width
, yoffset
+ height
,
254 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
260 _mesa_DeleteFramebuffers(2, fbos
);
261 _mesa_DeleteTextures(1, &pbo_tex
);
262 _mesa_DeleteBuffers(1, &pbo
);
270 need_signed_unsigned_int_conversion(mesa_format rbFormat
,
271 GLenum format
, GLenum type
)
273 const GLenum srcType
= _mesa_get_format_datatype(rbFormat
);
274 const bool is_dst_format_integer
= _mesa_is_enum_format_integer(format
);
275 return (srcType
== GL_INT
&&
276 is_dst_format_integer
&&
277 (type
== GL_UNSIGNED_INT
||
278 type
== GL_UNSIGNED_SHORT
||
279 type
== GL_UNSIGNED_BYTE
)) ||
280 (srcType
== GL_UNSIGNED_INT
&&
281 is_dst_format_integer
&&
288 _mesa_meta_pbo_GetTexSubImage(struct gl_context
*ctx
, GLuint dims
,
289 struct gl_texture_image
*tex_image
,
290 int xoffset
, int yoffset
, int zoffset
,
291 int width
, int height
, int depth
,
292 GLenum format
, GLenum type
, const void *pixels
,
293 const struct gl_pixelstore_attrib
*packing
)
295 GLuint pbo
= 0, pbo_tex
= 0, fbos
[2] = { 0, 0 };
297 struct gl_texture_image
*pbo_tex_image
;
298 struct gl_renderbuffer
*rb
= NULL
;
299 GLenum dstBaseFormat
= _mesa_unpack_format_to_base_format(format
);
300 GLenum status
, src_base_format
;
301 bool success
= false, clear_channels_to_zero
= false;
302 float save_clear_color
[4];
305 if (!_mesa_is_bufferobj(packing
->BufferObj
))
308 if (format
== GL_DEPTH_COMPONENT
||
309 format
== GL_DEPTH_STENCIL
||
310 format
== GL_STENCIL_INDEX
||
311 format
== GL_COLOR_INDEX
)
314 /* Don't use meta path for readpixels in below conditions. */
316 rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
318 /* _mesa_get_readpixels_transfer_ops() includes the cases of read
319 * color clamping along with the ctx->_ImageTransferState.
321 if (_mesa_get_readpixels_transfer_ops(ctx
, rb
->Format
, format
,
325 if (_mesa_need_rgb_to_luminance_conversion(rb
->_BaseFormat
,
329 /* This function rely on BlitFramebuffer to fill in the pixel data for
330 * ReadPixels. But, BlitFrameBuffer doesn't support signed to unsigned
331 * or unsigned to signed integer conversions. OpenGL spec expects an
332 * invalid operation in that case.
334 if (need_signed_unsigned_int_conversion(rb
->Format
, format
, type
))
338 /* For arrays, use a tall (height * depth) 2D texture but taking into
339 * account the inter-image padding specified with the image height packing
342 image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
344 pbo_tex_image
= create_texture_for_pbo(ctx
, false, GL_PIXEL_PACK_BUFFER
,
345 dims
, width
, height
, depth
,
346 format
, type
, pixels
, packing
,
351 _mesa_meta_begin(ctx
, ~(MESA_META_PIXEL_TRANSFER
|
352 MESA_META_PIXEL_STORE
));
354 /* GL_CLAMP_FRAGMENT_COLOR doesn't affect ReadPixels and GettexImage */
355 if (ctx
->Extensions
.ARB_color_buffer_float
)
356 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
358 _mesa_GenFramebuffers(2, fbos
);
360 if (tex_image
&& tex_image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
362 assert(zoffset
== 0);
370 /* If we were given a texture, bind it to the read framebuffer. If not,
371 * we're doing a ReadPixels and we should just use whatever framebuffer
372 * the client has bound.
375 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, fbos
[0]);
376 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
378 /* If this passes on the first layer it should pass on the others */
379 status
= _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER
);
380 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
386 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbos
[1]);
387 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
389 /* If this passes on the first layer it should pass on the others */
390 status
= _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER
);
391 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
394 _mesa_update_state(ctx
);
396 if (_mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
398 xoffset
+ width
, yoffset
+ height
,
400 GL_COLOR_BUFFER_BIT
, GL_NEAREST
))
403 src_base_format
= tex_image
?
404 tex_image
->_BaseFormat
:
405 ctx
->ReadBuffer
->_ColorReadBuffer
->_BaseFormat
;
407 /* Depending on the base formats involved we might need to rebase some
408 * values. For example if we download from a Luminance format to RGBA
409 * format, we want G=0 and B=0.
411 clear_channels_to_zero
=
412 _mesa_need_luminance_to_rgb_conversion(src_base_format
,
413 pbo_tex_image
->_BaseFormat
);
415 if (clear_channels_to_zero
) {
416 memcpy(save_clear_color
, ctx
->Color
.ClearColor
.f
, 4 * sizeof(float));
417 /* Clear the Green, Blue channels. */
418 _mesa_ColorMask(GL_FALSE
, GL_TRUE
, GL_TRUE
,
419 src_base_format
!= GL_LUMINANCE_ALPHA
);
420 _mesa_ClearColor(0.0, 0.0, 0.0, 1.0);
421 _mesa_Clear(GL_COLOR_BUFFER_BIT
);
424 for (z
= 1; z
< depth
; z
++) {
425 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
426 tex_image
, zoffset
+ z
);
428 _mesa_update_state(ctx
);
430 _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
432 xoffset
+ width
, yoffset
+ height
,
434 width
, z
* image_height
+ height
,
435 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
436 if (clear_channels_to_zero
)
437 _mesa_Clear(GL_COLOR_BUFFER_BIT
);
440 /* Unmask the color channels and restore the saved clear color values. */
441 if (clear_channels_to_zero
) {
442 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
443 _mesa_ClearColor(save_clear_color
[0], save_clear_color
[1],
444 save_clear_color
[2], save_clear_color
[3]);
450 _mesa_DeleteFramebuffers(2, fbos
);
451 _mesa_DeleteTextures(1, &pbo_tex
);
452 _mesa_DeleteBuffers(1, &pbo
);