meta: Compute correct buffer size with SkipRows/SkipPixels
[mesa.git] / src / mesa / drivers / common / meta_tex_subimage.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 *
24 * Authors:
25 * Jason Ekstrand <jason.ekstrand@intel.com>
26 */
27
28 #include "blend.h"
29 #include "bufferobj.h"
30 #include "buffers.h"
31 #include "clear.h"
32 #include "fbobject.h"
33 #include "glformats.h"
34 #include "glheader.h"
35 #include "image.h"
36 #include "macros.h"
37 #include "meta.h"
38 #include "pbo.h"
39 #include "readpix.h"
40 #include "shaderapi.h"
41 #include "state.h"
42 #include "teximage.h"
43 #include "texobj.h"
44 #include "texstate.h"
45 #include "uniforms.h"
46 #include "varray.h"
47
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)
55 {
56 uint32_t pbo_format;
57 GLenum internal_format;
58 unsigned row_stride;
59 struct gl_buffer_object *buffer_obj;
60 struct gl_texture_object *tex_obj;
61 struct gl_texture_image *tex_image;
62 bool read_only;
63
64 if (packing->SwapBytes ||
65 packing->LsbFirst ||
66 packing->Invert)
67 return NULL;
68
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);
72
73 if (!pbo_format || !ctx->TextureFormatSupported[pbo_format])
74 return NULL;
75
76 /* Account for SKIP_PIXELS, SKIP_ROWS, ALIGNMENT, and SKIP_IMAGES */
77 uint32_t first_pixel = _mesa_image_offset(dims, packing, width, height,
78 format, type,
79 0, 0, 0);
80 uint32_t last_pixel = _mesa_image_offset(dims, packing, width, height,
81 format, type,
82 depth-1, height-1, width);
83 row_stride = _mesa_image_row_stride(packing, width, format, type);
84
85 if (_mesa_is_bufferobj(packing->BufferObj)) {
86 *tmp_pbo = 0;
87 buffer_obj = packing->BufferObj;
88 first_pixel += (intptr_t)pixels;
89 } else {
90 bool is_pixel_pack = pbo_target == GL_PIXEL_PACK_BUFFER;
91
92 assert(create_pbo);
93
94 _mesa_GenBuffers(1, tmp_pbo);
95
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.
99 */
100 _mesa_BindBuffer(pbo_target, *tmp_pbo);
101
102 /* In case of GL_PIXEL_PACK_BUFFER, pass null pointer for the pixel
103 * data to avoid unnecessary data copying in _mesa_BufferData().
104 */
105 if (is_pixel_pack)
106 _mesa_BufferData(pbo_target,
107 last_pixel - first_pixel,
108 NULL,
109 GL_STREAM_READ);
110 else
111 _mesa_BufferData(pbo_target,
112 last_pixel - first_pixel,
113 (char *)pixels + first_pixel,
114 GL_STREAM_DRAW);
115
116 buffer_obj = packing->BufferObj;
117 first_pixel = 0;
118
119 _mesa_BindBuffer(pbo_target, 0);
120 }
121
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;
129
130 internal_format = _mesa_get_format_base_format(pbo_format);
131
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.
135 */
136 int image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
137 int full_height = image_height * (depth - 1) + height;
138
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);
142
143 read_only = pbo_target == GL_PIXEL_UNPACK_BUFFER;
144 if (!ctx->Driver.SetTextureStorageForBufferObject(ctx, tex_obj,
145 buffer_obj,
146 first_pixel,
147 row_stride,
148 read_only)) {
149 _mesa_DeleteTextures(1, tmp_tex);
150 _mesa_DeleteBuffers(1, tmp_pbo);
151 return NULL;
152 }
153
154 return tex_image;
155 }
156
157 bool
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)
165 {
166 GLuint pbo = 0, pbo_tex = 0, fbos[2] = { 0, 0 };
167 int image_height;
168 struct gl_texture_image *pbo_tex_image;
169 GLenum status;
170 bool success = false;
171 int z;
172
173 if (!_mesa_is_bufferobj(packing->BufferObj) &&
174 (!create_pbo || pixels == NULL))
175 return false;
176
177 if (format == GL_DEPTH_COMPONENT ||
178 format == GL_DEPTH_STENCIL ||
179 format == GL_STENCIL_INDEX ||
180 format == GL_COLOR_INDEX)
181 return false;
182
183 if (ctx->_ImageTransferState)
184 return false;
185
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
188 * property.
189 */
190 image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
191
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,
196 &pbo, &pbo_tex);
197 if (!pbo_tex_image)
198 return false;
199
200 if (allocate_storage)
201 ctx->Driver.AllocTextureImageBuffer(ctx, tex_image);
202
203 _mesa_meta_begin(ctx, ~(MESA_META_PIXEL_TRANSFER |
204 MESA_META_PIXEL_STORE));
205
206 _mesa_GenFramebuffers(2, fbos);
207 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
208 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
209
210 if (tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
211 assert(depth == 1);
212 assert(zoffset == 0);
213 depth = height;
214 height = 1;
215 image_height = 1;
216 zoffset = yoffset;
217 yoffset = 0;
218 }
219
220 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
221 pbo_tex_image, 0);
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)
225 goto fail;
226
227 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
228 tex_image, zoffset);
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)
232 goto fail;
233
234 _mesa_update_state(ctx);
235
236 if (_mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
237 0, 0, width, height,
238 xoffset, yoffset,
239 xoffset + width, yoffset + height,
240 GL_COLOR_BUFFER_BIT, GL_NEAREST))
241 goto fail;
242
243 for (z = 1; z < depth; z++) {
244 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
245 tex_image, zoffset + z);
246
247 _mesa_update_state(ctx);
248
249 _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
250 0, z * image_height,
251 width, z * image_height + height,
252 xoffset, yoffset,
253 xoffset + width, yoffset + height,
254 GL_COLOR_BUFFER_BIT, GL_NEAREST);
255 }
256
257 success = true;
258
259 fail:
260 _mesa_DeleteFramebuffers(2, fbos);
261 _mesa_DeleteTextures(1, &pbo_tex);
262 _mesa_DeleteBuffers(1, &pbo);
263
264 _mesa_meta_end(ctx);
265
266 return success;
267 }
268
269 static bool
270 need_signed_unsigned_int_conversion(mesa_format rbFormat,
271 GLenum format, GLenum type)
272 {
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 &&
282 (type == GL_INT ||
283 type == GL_SHORT ||
284 type == GL_BYTE));
285 }
286
287 bool
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)
294 {
295 GLuint pbo = 0, pbo_tex = 0, fbos[2] = { 0, 0 };
296 int image_height;
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];
303 int z;
304
305 if (!_mesa_is_bufferobj(packing->BufferObj))
306 return false;
307
308 if (format == GL_DEPTH_COMPONENT ||
309 format == GL_DEPTH_STENCIL ||
310 format == GL_STENCIL_INDEX ||
311 format == GL_COLOR_INDEX)
312 return false;
313
314 /* Don't use meta path for readpixels in below conditions. */
315 if (!tex_image) {
316 rb = ctx->ReadBuffer->_ColorReadBuffer;
317
318 /* _mesa_get_readpixels_transfer_ops() includes the cases of read
319 * color clamping along with the ctx->_ImageTransferState.
320 */
321 if (_mesa_get_readpixels_transfer_ops(ctx, rb->Format, format,
322 type, GL_FALSE))
323 return false;
324
325 if (_mesa_need_rgb_to_luminance_conversion(rb->_BaseFormat,
326 dstBaseFormat))
327 return false;
328
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.
333 */
334 if (need_signed_unsigned_int_conversion(rb->Format, format, type))
335 return false;
336 }
337
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
340 * property.
341 */
342 image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
343
344 pbo_tex_image = create_texture_for_pbo(ctx, false, GL_PIXEL_PACK_BUFFER,
345 dims, width, height, depth,
346 format, type, pixels, packing,
347 &pbo, &pbo_tex);
348 if (!pbo_tex_image)
349 return false;
350
351 _mesa_meta_begin(ctx, ~(MESA_META_PIXEL_TRANSFER |
352 MESA_META_PIXEL_STORE));
353
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);
357
358 _mesa_GenFramebuffers(2, fbos);
359
360 if (tex_image && tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
361 assert(depth == 1);
362 assert(zoffset == 0);
363 depth = height;
364 height = 1;
365 image_height = 1;
366 zoffset = yoffset;
367 yoffset = 0;
368 }
369
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.
373 */
374 if (tex_image) {
375 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
376 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
377 tex_image, zoffset);
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)
381 goto fail;
382 } else {
383 assert(depth == 1);
384 }
385
386 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
387 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
388 pbo_tex_image, 0);
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)
392 goto fail;
393
394 _mesa_update_state(ctx);
395
396 if (_mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
397 xoffset, yoffset,
398 xoffset + width, yoffset + height,
399 0, 0, width, height,
400 GL_COLOR_BUFFER_BIT, GL_NEAREST))
401 goto fail;
402
403 src_base_format = tex_image ?
404 tex_image->_BaseFormat :
405 ctx->ReadBuffer->_ColorReadBuffer->_BaseFormat;
406
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.
410 */
411 clear_channels_to_zero =
412 _mesa_need_luminance_to_rgb_conversion(src_base_format,
413 pbo_tex_image->_BaseFormat);
414
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);
422 }
423
424 for (z = 1; z < depth; z++) {
425 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
426 tex_image, zoffset + z);
427
428 _mesa_update_state(ctx);
429
430 _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
431 xoffset, yoffset,
432 xoffset + width, yoffset + height,
433 0, z * image_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);
438 }
439
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]);
445 }
446
447 success = true;
448
449 fail:
450 _mesa_DeleteFramebuffers(2, fbos);
451 _mesa_DeleteTextures(1, &pbo_tex);
452 _mesa_DeleteBuffers(1, &pbo);
453
454 _mesa_meta_end(ctx);
455
456 return success;
457 }