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
, bool create_pbo
,
50 GLenum pbo_target
, int width
, int height
,
51 GLenum format
, GLenum type
, const void *pixels
,
52 const struct gl_pixelstore_attrib
*packing
,
53 GLuint
*tmp_pbo
, GLuint
*tmp_tex
)
56 GLenum internal_format
;
58 struct gl_buffer_object
*buffer_obj
;
59 struct gl_texture_object
*tex_obj
;
60 struct gl_texture_image
*tex_image
;
63 if (packing
->SwapBytes
||
68 pbo_format
= _mesa_format_from_format_and_type(format
, type
);
69 if (_mesa_format_is_mesa_array_format(pbo_format
))
70 pbo_format
= _mesa_format_from_array_format(pbo_format
);
72 if (!pbo_format
|| !ctx
->TextureFormatSupported
[pbo_format
])
75 /* Account for SKIP_PIXELS, SKIP_ROWS, ALIGNMENT, and SKIP_IMAGES */
76 pixels
= _mesa_image_address3d(packing
, pixels
,
77 width
, height
, format
, type
, 0, 0, 0);
78 row_stride
= _mesa_image_row_stride(packing
, width
, format
, type
);
80 if (_mesa_is_bufferobj(packing
->BufferObj
)) {
82 buffer_obj
= packing
->BufferObj
;
84 bool is_pixel_pack
= pbo_target
== GL_PIXEL_PACK_BUFFER
;
88 _mesa_GenBuffers(1, tmp_pbo
);
90 /* We are not doing this inside meta_begin/end. However, we know the
91 * client doesn't have the given target bound, so we can go ahead and
92 * squash it. We'll set it back when we're done.
94 _mesa_BindBuffer(pbo_target
, *tmp_pbo
);
96 /* In case of GL_PIXEL_PACK_BUFFER, pass null pointer for the pixel
97 * data to avoid unnecessary data copying in _mesa_BufferData().
100 _mesa_BufferData(pbo_target
, row_stride
* height
, NULL
,
103 _mesa_BufferData(pbo_target
, row_stride
* height
, pixels
,
106 buffer_obj
= packing
->BufferObj
;
109 _mesa_BindBuffer(pbo_target
, 0);
112 _mesa_GenTextures(1, tmp_tex
);
113 tex_obj
= _mesa_lookup_texture(ctx
, *tmp_tex
);
114 _mesa_initialize_texture_object(ctx
, tex_obj
, *tmp_tex
, GL_TEXTURE_2D
);
115 /* This must be set after _mesa_initialize_texture_object, not before. */
116 tex_obj
->Immutable
= GL_TRUE
;
117 /* This is required for interactions with ARB_texture_view. */
118 tex_obj
->NumLayers
= 1;
120 internal_format
= _mesa_get_format_base_format(pbo_format
);
122 tex_image
= _mesa_get_tex_image(ctx
, tex_obj
, tex_obj
->Target
, 0);
123 _mesa_init_teximage_fields(ctx
, tex_image
, width
, height
, 1,
124 0, internal_format
, pbo_format
);
126 read_only
= pbo_target
== GL_PIXEL_UNPACK_BUFFER
;
127 if (!ctx
->Driver
.SetTextureStorageForBufferObject(ctx
, tex_obj
,
132 _mesa_DeleteTextures(1, tmp_tex
);
133 _mesa_DeleteBuffers(1, tmp_pbo
);
141 _mesa_meta_pbo_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
142 struct gl_texture_image
*tex_image
,
143 int xoffset
, int yoffset
, int zoffset
,
144 int width
, int height
, int depth
,
145 GLenum format
, GLenum type
, const void *pixels
,
146 bool allocate_storage
, bool create_pbo
,
147 const struct gl_pixelstore_attrib
*packing
)
149 GLuint pbo
= 0, pbo_tex
= 0, fbos
[2] = { 0, 0 };
150 int full_height
, image_height
;
151 struct gl_texture_image
*pbo_tex_image
;
153 bool success
= false;
156 if (!_mesa_is_bufferobj(packing
->BufferObj
) &&
157 (!create_pbo
|| pixels
== NULL
))
160 if (format
== GL_DEPTH_COMPONENT
||
161 format
== GL_DEPTH_STENCIL
||
162 format
== GL_STENCIL_INDEX
||
163 format
== GL_COLOR_INDEX
)
166 if (ctx
->_ImageTransferState
)
169 /* For arrays, use a tall (height * depth) 2D texture but taking into
170 * account the inter-image padding specified with the image height packing
173 image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
174 full_height
= image_height
* (depth
- 1) + height
;
176 pbo_tex_image
= create_texture_for_pbo(ctx
, create_pbo
,
177 GL_PIXEL_UNPACK_BUFFER
,
179 format
, type
, pixels
, packing
,
184 if (allocate_storage
)
185 ctx
->Driver
.AllocTextureImageBuffer(ctx
, tex_image
);
187 _mesa_meta_begin(ctx
, ~(MESA_META_PIXEL_TRANSFER
|
188 MESA_META_PIXEL_STORE
));
190 _mesa_GenFramebuffers(2, fbos
);
191 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, fbos
[0]);
192 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbos
[1]);
194 if (tex_image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
196 assert(zoffset
== 0);
204 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
206 /* If this passes on the first layer it should pass on the others */
207 status
= _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER
);
208 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
211 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
213 /* If this passes on the first layer it should pass on the others */
214 status
= _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER
);
215 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
218 _mesa_update_state(ctx
);
220 if (_mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
223 xoffset
+ width
, yoffset
+ height
,
224 GL_COLOR_BUFFER_BIT
, GL_NEAREST
))
227 for (z
= 1; z
< depth
; z
++) {
228 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
229 tex_image
, zoffset
+ z
);
231 _mesa_update_state(ctx
);
233 _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
235 width
, z
* image_height
+ height
,
237 xoffset
+ width
, yoffset
+ height
,
238 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
244 _mesa_DeleteFramebuffers(2, fbos
);
245 _mesa_DeleteTextures(1, &pbo_tex
);
246 _mesa_DeleteBuffers(1, &pbo
);
254 need_signed_unsigned_int_conversion(mesa_format rbFormat
,
255 GLenum format
, GLenum type
)
257 const GLenum srcType
= _mesa_get_format_datatype(rbFormat
);
258 const bool is_dst_format_integer
= _mesa_is_enum_format_integer(format
);
259 return (srcType
== GL_INT
&&
260 is_dst_format_integer
&&
261 (type
== GL_UNSIGNED_INT
||
262 type
== GL_UNSIGNED_SHORT
||
263 type
== GL_UNSIGNED_BYTE
)) ||
264 (srcType
== GL_UNSIGNED_INT
&&
265 is_dst_format_integer
&&
272 _mesa_meta_pbo_GetTexSubImage(struct gl_context
*ctx
, GLuint dims
,
273 struct gl_texture_image
*tex_image
,
274 int xoffset
, int yoffset
, int zoffset
,
275 int width
, int height
, int depth
,
276 GLenum format
, GLenum type
, const void *pixels
,
277 const struct gl_pixelstore_attrib
*packing
)
279 GLuint pbo
= 0, pbo_tex
= 0, fbos
[2] = { 0, 0 };
280 int full_height
, image_height
;
281 struct gl_texture_image
*pbo_tex_image
;
282 struct gl_renderbuffer
*rb
= NULL
;
283 GLenum status
, src_base_format
;
284 bool success
= false, clear_channels_to_zero
= false;
285 float save_clear_color
[4];
288 if (!_mesa_is_bufferobj(packing
->BufferObj
))
291 if (format
== GL_DEPTH_COMPONENT
||
292 format
== GL_DEPTH_STENCIL
||
293 format
== GL_STENCIL_INDEX
||
294 format
== GL_COLOR_INDEX
)
297 /* Don't use meta path for readpixels in below conditions. */
299 rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
301 /* _mesa_get_readpixels_transfer_ops() includes the cases of read
302 * color clamping along with the ctx->_ImageTransferState.
304 if (_mesa_get_readpixels_transfer_ops(ctx
, rb
->Format
, format
,
308 if (_mesa_need_rgb_to_luminance_conversion(rb
->Format
, format
))
311 /* This function rely on BlitFramebuffer to fill in the pixel data for
312 * ReadPixels. But, BlitFrameBuffer doesn't support signed to unsigned
313 * or unsigned to signed integer conversions. OpenGL spec expects an
314 * invalid operation in that case.
316 if (need_signed_unsigned_int_conversion(rb
->Format
, format
, type
))
320 /* For arrays, use a tall (height * depth) 2D texture but taking into
321 * account the inter-image padding specified with the image height packing
324 image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
325 full_height
= image_height
* (depth
- 1) + height
;
327 pbo_tex_image
= create_texture_for_pbo(ctx
, false, GL_PIXEL_PACK_BUFFER
,
328 width
, full_height
* depth
,
329 format
, type
, pixels
, packing
,
334 _mesa_meta_begin(ctx
, ~(MESA_META_PIXEL_TRANSFER
|
335 MESA_META_PIXEL_STORE
));
337 /* GL_CLAMP_FRAGMENT_COLOR doesn't affect ReadPixels and GettexImage */
338 if (ctx
->Extensions
.ARB_color_buffer_float
)
339 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
341 _mesa_GenFramebuffers(2, fbos
);
343 if (tex_image
&& tex_image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
345 assert(zoffset
== 0);
353 /* If we were given a texture, bind it to the read framebuffer. If not,
354 * we're doing a ReadPixels and we should just use whatever framebuffer
355 * the client has bound.
358 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, fbos
[0]);
359 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
361 /* If this passes on the first layer it should pass on the others */
362 status
= _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER
);
363 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
369 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbos
[1]);
370 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
372 /* If this passes on the first layer it should pass on the others */
373 status
= _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER
);
374 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
377 _mesa_update_state(ctx
);
379 if (_mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
381 xoffset
+ width
, yoffset
+ height
,
383 GL_COLOR_BUFFER_BIT
, GL_NEAREST
))
386 src_base_format
= tex_image
?
387 tex_image
->_BaseFormat
:
388 ctx
->ReadBuffer
->_ColorReadBuffer
->_BaseFormat
;
390 /* Depending on the base formats involved we might need to rebase some
391 * values. For example if we download from a Luminance format to RGBA
392 * format, we want G=0 and B=0.
394 clear_channels_to_zero
=
395 _mesa_need_luminance_to_rgb_conversion(src_base_format
,
396 pbo_tex_image
->_BaseFormat
);
398 if (clear_channels_to_zero
) {
399 memcpy(save_clear_color
, ctx
->Color
.ClearColor
.f
, 4 * sizeof(float));
400 /* Clear the Green, Blue channels. */
401 _mesa_ColorMask(GL_FALSE
, GL_TRUE
, GL_TRUE
,
402 src_base_format
!= GL_LUMINANCE_ALPHA
);
403 _mesa_ClearColor(0.0, 0.0, 0.0, 1.0);
404 _mesa_Clear(GL_COLOR_BUFFER_BIT
);
407 for (z
= 1; z
< depth
; z
++) {
408 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
409 tex_image
, zoffset
+ z
);
411 _mesa_update_state(ctx
);
413 _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
415 xoffset
+ width
, yoffset
+ height
,
417 width
, z
* image_height
+ height
,
418 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
419 if (clear_channels_to_zero
)
420 _mesa_Clear(GL_COLOR_BUFFER_BIT
);
423 /* Unmask the color channels and restore the saved clear color values. */
424 if (clear_channels_to_zero
) {
425 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
426 _mesa_ClearColor(save_clear_color
[0], save_clear_color
[1],
427 save_clear_color
[2], save_clear_color
[3]);
433 _mesa_DeleteFramebuffers(2, fbos
);
434 _mesa_DeleteTextures(1, &pbo_tex
);
435 _mesa_DeleteBuffers(1, &pbo
);