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>
28 #include "bufferobj.h"
31 #include "glformats.h"
38 #include "shaderapi.h"
46 static struct gl_texture_image
*
47 create_texture_for_pbo(struct gl_context
*ctx
, bool create_pbo
,
48 GLenum pbo_target
, int width
, int height
,
49 GLenum format
, GLenum type
, const void *pixels
,
50 const struct gl_pixelstore_attrib
*packing
,
51 GLuint
*tmp_pbo
, GLuint
*tmp_tex
)
54 GLenum internal_format
;
56 struct gl_buffer_object
*buffer_obj
;
57 struct gl_texture_object
*tex_obj
;
58 struct gl_texture_image
*tex_image
;
61 if (packing
->SwapBytes
||
66 pbo_format
= _mesa_format_from_format_and_type(format
, type
);
67 if (_mesa_format_is_mesa_array_format(pbo_format
))
68 pbo_format
= _mesa_format_from_array_format(pbo_format
);
70 if (!pbo_format
|| !ctx
->TextureFormatSupported
[pbo_format
])
73 /* Account for SKIP_PIXELS, SKIP_ROWS, ALIGNMENT, and SKIP_IMAGES */
74 pixels
= _mesa_image_address3d(packing
, pixels
,
75 width
, height
, format
, type
, 0, 0, 0);
76 row_stride
= _mesa_image_row_stride(packing
, width
, format
, type
);
78 if (_mesa_is_bufferobj(packing
->BufferObj
)) {
80 buffer_obj
= packing
->BufferObj
;
82 bool is_pixel_pack
= pbo_target
== GL_PIXEL_PACK_BUFFER
;
86 _mesa_GenBuffers(1, tmp_pbo
);
88 /* We are not doing this inside meta_begin/end. However, we know the
89 * client doesn't have the given target bound, so we can go ahead and
90 * squash it. We'll set it back when we're done.
92 _mesa_BindBuffer(pbo_target
, *tmp_pbo
);
94 /* In case of GL_PIXEL_PACK_BUFFER, pass null pointer for the pixel
95 * data to avoid unnecessary data copying in _mesa_BufferData().
98 _mesa_BufferData(pbo_target
, row_stride
* height
, NULL
,
101 _mesa_BufferData(pbo_target
, row_stride
* height
, pixels
,
104 buffer_obj
= packing
->BufferObj
;
107 _mesa_BindBuffer(pbo_target
, 0);
110 _mesa_GenTextures(1, tmp_tex
);
111 tex_obj
= _mesa_lookup_texture(ctx
, *tmp_tex
);
112 _mesa_initialize_texture_object(ctx
, tex_obj
, *tmp_tex
, GL_TEXTURE_2D
);
113 /* This must be set after _mesa_initialize_texture_object, not before. */
114 tex_obj
->Immutable
= GL_TRUE
;
115 /* This is required for interactions with ARB_texture_view. */
116 tex_obj
->NumLayers
= 1;
118 internal_format
= _mesa_get_format_base_format(pbo_format
);
120 tex_image
= _mesa_get_tex_image(ctx
, tex_obj
, tex_obj
->Target
, 0);
121 _mesa_init_teximage_fields(ctx
, tex_image
, width
, height
, 1,
122 0, internal_format
, pbo_format
);
124 read_only
= pbo_target
== GL_PIXEL_UNPACK_BUFFER
;
125 if (!ctx
->Driver
.SetTextureStorageForBufferObject(ctx
, tex_obj
,
130 _mesa_DeleteTextures(1, tmp_tex
);
131 _mesa_DeleteBuffers(1, tmp_pbo
);
139 _mesa_meta_pbo_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
140 struct gl_texture_image
*tex_image
,
141 int xoffset
, int yoffset
, int zoffset
,
142 int width
, int height
, int depth
,
143 GLenum format
, GLenum type
, const void *pixels
,
144 bool allocate_storage
, bool create_pbo
,
145 const struct gl_pixelstore_attrib
*packing
)
147 GLuint pbo
= 0, pbo_tex
= 0, fbos
[2] = { 0, 0 };
148 int full_height
, image_height
;
149 struct gl_texture_image
*pbo_tex_image
;
151 bool success
= false;
154 if (!_mesa_is_bufferobj(packing
->BufferObj
) &&
155 (!create_pbo
|| pixels
== NULL
))
158 if (format
== GL_DEPTH_COMPONENT
||
159 format
== GL_DEPTH_STENCIL
||
160 format
== GL_STENCIL_INDEX
||
161 format
== GL_COLOR_INDEX
)
164 if (ctx
->_ImageTransferState
)
167 /* For arrays, use a tall (height * depth) 2D texture but taking into
168 * account the inter-image padding specified with the image height packing
171 image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
172 full_height
= image_height
* (depth
- 1) + height
;
174 pbo_tex_image
= create_texture_for_pbo(ctx
, create_pbo
,
175 GL_PIXEL_UNPACK_BUFFER
,
177 format
, type
, pixels
, packing
,
182 if (allocate_storage
)
183 ctx
->Driver
.AllocTextureImageBuffer(ctx
, tex_image
);
185 _mesa_meta_begin(ctx
, ~(MESA_META_PIXEL_TRANSFER
|
186 MESA_META_PIXEL_STORE
));
188 _mesa_GenFramebuffers(2, fbos
);
189 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, fbos
[0]);
190 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbos
[1]);
192 if (tex_image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
194 assert(zoffset
== 0);
202 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
204 /* If this passes on the first layer it should pass on the others */
205 status
= _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER
);
206 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
209 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
211 /* If this passes on the first layer it should pass on the others */
212 status
= _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER
);
213 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
216 _mesa_update_state(ctx
);
218 if (_mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
221 xoffset
+ width
, yoffset
+ height
,
222 GL_COLOR_BUFFER_BIT
, GL_NEAREST
))
225 for (z
= 1; z
< depth
; z
++) {
226 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
227 tex_image
, zoffset
+ z
);
229 _mesa_update_state(ctx
);
231 _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
233 width
, z
* image_height
+ height
,
235 xoffset
+ width
, yoffset
+ height
,
236 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
242 _mesa_DeleteFramebuffers(2, fbos
);
243 _mesa_DeleteTextures(1, &pbo_tex
);
244 _mesa_DeleteBuffers(1, &pbo
);
252 _mesa_meta_pbo_GetTexSubImage(struct gl_context
*ctx
, GLuint dims
,
253 struct gl_texture_image
*tex_image
,
254 int xoffset
, int yoffset
, int zoffset
,
255 int width
, int height
, int depth
,
256 GLenum format
, GLenum type
, const void *pixels
,
257 const struct gl_pixelstore_attrib
*packing
)
259 GLuint pbo
= 0, pbo_tex
= 0, fbos
[2] = { 0, 0 };
260 int full_height
, image_height
;
261 struct gl_texture_image
*pbo_tex_image
;
262 struct gl_renderbuffer
*rb
= NULL
;
264 bool success
= false;
267 if (!_mesa_is_bufferobj(packing
->BufferObj
))
270 if (format
== GL_DEPTH_COMPONENT
||
271 format
== GL_DEPTH_STENCIL
||
272 format
== GL_STENCIL_INDEX
||
273 format
== GL_COLOR_INDEX
)
276 if (ctx
->_ImageTransferState
)
281 rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
282 if (_mesa_need_rgb_to_luminance_conversion(rb
->Format
, format
))
286 /* For arrays, use a tall (height * depth) 2D texture but taking into
287 * account the inter-image padding specified with the image height packing
290 image_height
= packing
->ImageHeight
== 0 ? height
: packing
->ImageHeight
;
291 full_height
= image_height
* (depth
- 1) + height
;
293 pbo_tex_image
= create_texture_for_pbo(ctx
, false, GL_PIXEL_PACK_BUFFER
,
294 width
, full_height
* depth
,
295 format
, type
, pixels
, packing
,
300 _mesa_meta_begin(ctx
, ~(MESA_META_PIXEL_TRANSFER
|
301 MESA_META_PIXEL_STORE
));
303 _mesa_GenFramebuffers(2, fbos
);
305 if (tex_image
&& tex_image
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
307 assert(zoffset
== 0);
315 /* If we were given a texture, bind it to the read framebuffer. If not,
316 * we're doing a ReadPixels and we should just use whatever framebuffer
317 * the client has bound.
320 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER
, fbos
[0]);
321 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
323 /* If this passes on the first layer it should pass on the others */
324 status
= _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER
);
325 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
331 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbos
[1]);
332 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
334 /* If this passes on the first layer it should pass on the others */
335 status
= _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER
);
336 if (status
!= GL_FRAMEBUFFER_COMPLETE
)
339 _mesa_update_state(ctx
);
341 if (_mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
343 xoffset
+ width
, yoffset
+ height
,
345 GL_COLOR_BUFFER_BIT
, GL_NEAREST
))
348 for (z
= 1; z
< depth
; z
++) {
349 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
350 tex_image
, zoffset
+ z
);
352 _mesa_update_state(ctx
);
354 _mesa_meta_BlitFramebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
356 xoffset
+ width
, yoffset
+ height
,
358 width
, z
* image_height
+ height
,
359 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
365 _mesa_DeleteFramebuffers(2, fbos
);
366 _mesa_DeleteTextures(1, &pbo_tex
);
367 _mesa_DeleteBuffers(1, &pbo
);