mesa: remove Driver.ResizeBuffers
[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 bool
49 need_signed_unsigned_int_conversion(mesa_format mesaFormat,
50 GLenum format, GLenum type)
51 {
52 const GLenum mesaFormatType = _mesa_get_format_datatype(mesaFormat);
53 const bool is_format_integer = _mesa_is_enum_format_integer(format);
54 return (mesaFormatType == GL_INT &&
55 is_format_integer &&
56 (type == GL_UNSIGNED_INT ||
57 type == GL_UNSIGNED_SHORT ||
58 type == GL_UNSIGNED_BYTE)) ||
59 (mesaFormatType == GL_UNSIGNED_INT &&
60 is_format_integer &&
61 (type == GL_INT ||
62 type == GL_SHORT ||
63 type == GL_BYTE));
64 }
65
66 static struct gl_texture_image *
67 create_texture_for_pbo(struct gl_context *ctx,
68 bool create_pbo, GLenum pbo_target,
69 int dims, int width, int height, int depth,
70 GLenum format, GLenum type, const void *pixels,
71 const struct gl_pixelstore_attrib *packing,
72 GLuint *tmp_pbo, GLuint *tmp_tex)
73 {
74 uint32_t pbo_format;
75 GLenum internal_format;
76 unsigned row_stride;
77 struct gl_buffer_object *buffer_obj;
78 struct gl_texture_object *tex_obj;
79 struct gl_texture_image *tex_image;
80 bool read_only;
81
82 if (packing->SwapBytes ||
83 packing->LsbFirst ||
84 packing->Invert)
85 return NULL;
86
87 pbo_format = _mesa_format_from_format_and_type(format, type);
88 if (_mesa_format_is_mesa_array_format(pbo_format))
89 pbo_format = _mesa_format_from_array_format(pbo_format);
90
91 if (!pbo_format || !ctx->TextureFormatSupported[pbo_format])
92 return NULL;
93
94 /* Account for SKIP_PIXELS, SKIP_ROWS, ALIGNMENT, and SKIP_IMAGES */
95 uint32_t first_pixel = _mesa_image_offset(dims, packing, width, height,
96 format, type,
97 0, 0, 0);
98 uint32_t last_pixel = _mesa_image_offset(dims, packing, width, height,
99 format, type,
100 depth-1, height-1, width);
101 row_stride = _mesa_image_row_stride(packing, width, format, type);
102
103 if (_mesa_is_bufferobj(packing->BufferObj)) {
104 *tmp_pbo = 0;
105 buffer_obj = packing->BufferObj;
106 first_pixel += (intptr_t)pixels;
107 } else {
108 bool is_pixel_pack = pbo_target == GL_PIXEL_PACK_BUFFER;
109
110 assert(create_pbo);
111
112 _mesa_GenBuffers(1, tmp_pbo);
113
114 /* We are not doing this inside meta_begin/end. However, we know the
115 * client doesn't have the given target bound, so we can go ahead and
116 * squash it. We'll set it back when we're done.
117 */
118 _mesa_BindBuffer(pbo_target, *tmp_pbo);
119
120 /* In case of GL_PIXEL_PACK_BUFFER, pass null pointer for the pixel
121 * data to avoid unnecessary data copying in _mesa_BufferData().
122 */
123 if (is_pixel_pack)
124 _mesa_BufferData(pbo_target,
125 last_pixel - first_pixel,
126 NULL,
127 GL_STREAM_READ);
128 else
129 _mesa_BufferData(pbo_target,
130 last_pixel - first_pixel,
131 (char *)pixels + first_pixel,
132 GL_STREAM_DRAW);
133
134 buffer_obj = packing->BufferObj;
135 first_pixel = 0;
136
137 _mesa_BindBuffer(pbo_target, 0);
138 }
139
140 _mesa_GenTextures(1, tmp_tex);
141 tex_obj = _mesa_lookup_texture(ctx, *tmp_tex);
142 _mesa_initialize_texture_object(ctx, tex_obj, *tmp_tex, GL_TEXTURE_2D);
143 /* This must be set after _mesa_initialize_texture_object, not before. */
144 tex_obj->Immutable = GL_TRUE;
145 /* This is required for interactions with ARB_texture_view. */
146 tex_obj->NumLayers = 1;
147
148 internal_format = _mesa_get_format_base_format(pbo_format);
149
150 /* The texture is addressed as a single very-tall image, so we
151 * need to pack the multiple image depths together taking the
152 * inter-image padding into account.
153 */
154 int image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
155 int full_height = image_height * (depth - 1) + height;
156
157 tex_image = _mesa_get_tex_image(ctx, tex_obj, tex_obj->Target, 0);
158 _mesa_init_teximage_fields(ctx, tex_image, width, full_height, 1,
159 0, internal_format, pbo_format);
160
161 read_only = pbo_target == GL_PIXEL_UNPACK_BUFFER;
162 if (!ctx->Driver.SetTextureStorageForBufferObject(ctx, tex_obj,
163 buffer_obj,
164 first_pixel,
165 row_stride,
166 read_only)) {
167 _mesa_DeleteTextures(1, tmp_tex);
168 _mesa_DeleteBuffers(1, tmp_pbo);
169 return NULL;
170 }
171
172 return tex_image;
173 }
174
175 bool
176 _mesa_meta_pbo_TexSubImage(struct gl_context *ctx, GLuint dims,
177 struct gl_texture_image *tex_image,
178 int xoffset, int yoffset, int zoffset,
179 int width, int height, int depth,
180 GLenum format, GLenum type, const void *pixels,
181 bool allocate_storage, bool create_pbo,
182 const struct gl_pixelstore_attrib *packing)
183 {
184 GLuint pbo = 0, pbo_tex = 0, fbos[2] = { 0, 0 };
185 int image_height;
186 struct gl_texture_image *pbo_tex_image;
187 GLenum status;
188 bool success = false;
189 int z;
190
191 if (!_mesa_is_bufferobj(packing->BufferObj) &&
192 (!create_pbo || pixels == NULL))
193 return false;
194
195 if (format == GL_DEPTH_COMPONENT ||
196 format == GL_DEPTH_STENCIL ||
197 format == GL_STENCIL_INDEX ||
198 format == GL_COLOR_INDEX)
199 return false;
200
201 if (ctx->_ImageTransferState)
202 return false;
203
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.
207 */
208 if (need_signed_unsigned_int_conversion(tex_image->TexFormat, format, type))
209 return false;
210
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
213 * property.
214 */
215 image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
216
217 pbo_tex_image = create_texture_for_pbo(ctx, create_pbo,
218 GL_PIXEL_UNPACK_BUFFER,
219 dims, width, height, depth,
220 format, type, pixels, packing,
221 &pbo, &pbo_tex);
222 if (!pbo_tex_image)
223 return false;
224
225 if (allocate_storage)
226 ctx->Driver.AllocTextureImageBuffer(ctx, tex_image);
227
228 _mesa_meta_begin(ctx, ~(MESA_META_PIXEL_TRANSFER |
229 MESA_META_PIXEL_STORE));
230
231 _mesa_GenFramebuffers(2, fbos);
232 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
233 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
234
235 if (tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
236 assert(depth == 1);
237 assert(zoffset == 0);
238 depth = height;
239 height = 1;
240 image_height = 1;
241 zoffset = yoffset;
242 yoffset = 0;
243 }
244
245 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
246 pbo_tex_image, 0);
247 /* If this passes on the first layer it should pass on the others */
248 status = _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER);
249 if (status != GL_FRAMEBUFFER_COMPLETE)
250 goto fail;
251
252 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
253 tex_image, zoffset);
254 /* If this passes on the first layer it should pass on the others */
255 status = _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
256 if (status != GL_FRAMEBUFFER_COMPLETE)
257 goto fail;
258
259 _mesa_update_state(ctx);
260
261 if (_mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
262 0, 0, width, height,
263 xoffset, yoffset,
264 xoffset + width, yoffset + height,
265 GL_COLOR_BUFFER_BIT, GL_NEAREST))
266 goto fail;
267
268 for (z = 1; z < depth; z++) {
269 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
270 tex_image, zoffset + z);
271
272 _mesa_update_state(ctx);
273
274 _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
275 0, z * image_height,
276 width, z * image_height + height,
277 xoffset, yoffset,
278 xoffset + width, yoffset + height,
279 GL_COLOR_BUFFER_BIT, GL_NEAREST);
280 }
281
282 success = true;
283
284 fail:
285 _mesa_DeleteFramebuffers(2, fbos);
286 _mesa_DeleteTextures(1, &pbo_tex);
287 _mesa_DeleteBuffers(1, &pbo);
288
289 _mesa_meta_end(ctx);
290
291 return success;
292 }
293
294 bool
295 _mesa_meta_pbo_GetTexSubImage(struct gl_context *ctx, GLuint dims,
296 struct gl_texture_image *tex_image,
297 int xoffset, int yoffset, int zoffset,
298 int width, int height, int depth,
299 GLenum format, GLenum type, const void *pixels,
300 const struct gl_pixelstore_attrib *packing)
301 {
302 GLuint pbo = 0, pbo_tex = 0, fbos[2] = { 0, 0 };
303 int image_height;
304 struct gl_texture_image *pbo_tex_image;
305 struct gl_renderbuffer *rb = NULL;
306 GLenum dstBaseFormat = _mesa_unpack_format_to_base_format(format);
307 GLenum status, src_base_format;
308 bool success = false, clear_channels_to_zero = false;
309 float save_clear_color[4];
310 int z;
311
312 if (!_mesa_is_bufferobj(packing->BufferObj))
313 return false;
314
315 if (format == GL_DEPTH_COMPONENT ||
316 format == GL_DEPTH_STENCIL ||
317 format == GL_STENCIL_INDEX ||
318 format == GL_COLOR_INDEX)
319 return false;
320
321 /* Don't use meta path for readpixels in below conditions. */
322 if (!tex_image) {
323 rb = ctx->ReadBuffer->_ColorReadBuffer;
324
325 /* _mesa_get_readpixels_transfer_ops() includes the cases of read
326 * color clamping along with the ctx->_ImageTransferState.
327 */
328 if (_mesa_get_readpixels_transfer_ops(ctx, rb->Format, format,
329 type, GL_FALSE))
330 return false;
331
332 if (_mesa_need_rgb_to_luminance_conversion(rb->_BaseFormat,
333 dstBaseFormat))
334 return false;
335
336 /* This function rely on BlitFramebuffer to fill in the pixel data for
337 * ReadPixels. But, BlitFrameBuffer doesn't support signed to unsigned
338 * or unsigned to signed integer conversions. OpenGL spec expects an
339 * invalid operation in that case.
340 */
341 if (need_signed_unsigned_int_conversion(rb->Format, format, type))
342 return false;
343 }
344
345 /* For arrays, use a tall (height * depth) 2D texture but taking into
346 * account the inter-image padding specified with the image height packing
347 * property.
348 */
349 image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
350
351 pbo_tex_image = create_texture_for_pbo(ctx, false, GL_PIXEL_PACK_BUFFER,
352 dims, width, height, depth,
353 format, type, pixels, packing,
354 &pbo, &pbo_tex);
355 if (!pbo_tex_image)
356 return false;
357
358 _mesa_meta_begin(ctx, ~(MESA_META_PIXEL_TRANSFER |
359 MESA_META_PIXEL_STORE));
360
361 /* GL_CLAMP_FRAGMENT_COLOR doesn't affect ReadPixels and GettexImage */
362 if (ctx->Extensions.ARB_color_buffer_float)
363 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
364
365 _mesa_GenFramebuffers(2, fbos);
366
367 if (tex_image && tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
368 assert(depth == 1);
369 assert(zoffset == 0);
370 depth = height;
371 height = 1;
372 image_height = 1;
373 zoffset = yoffset;
374 yoffset = 0;
375 }
376
377 /* If we were given a texture, bind it to the read framebuffer. If not,
378 * we're doing a ReadPixels and we should just use whatever framebuffer
379 * the client has bound.
380 */
381 if (tex_image) {
382 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
383 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
384 tex_image, zoffset);
385 /* If this passes on the first layer it should pass on the others */
386 status = _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER);
387 if (status != GL_FRAMEBUFFER_COMPLETE)
388 goto fail;
389 } else {
390 assert(depth == 1);
391 }
392
393 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
394 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
395 pbo_tex_image, 0);
396 /* If this passes on the first layer it should pass on the others */
397 status = _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
398 if (status != GL_FRAMEBUFFER_COMPLETE)
399 goto fail;
400
401 _mesa_update_state(ctx);
402
403 if (_mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
404 xoffset, yoffset,
405 xoffset + width, yoffset + height,
406 0, 0, width, height,
407 GL_COLOR_BUFFER_BIT, GL_NEAREST))
408 goto fail;
409
410 src_base_format = tex_image ?
411 tex_image->_BaseFormat :
412 ctx->ReadBuffer->_ColorReadBuffer->_BaseFormat;
413
414 /* Depending on the base formats involved we might need to rebase some
415 * values. For example if we download from a Luminance format to RGBA
416 * format, we want G=0 and B=0.
417 */
418 clear_channels_to_zero =
419 _mesa_need_luminance_to_rgb_conversion(src_base_format,
420 pbo_tex_image->_BaseFormat);
421
422 if (clear_channels_to_zero) {
423 memcpy(save_clear_color, ctx->Color.ClearColor.f, 4 * sizeof(float));
424 /* Clear the Green, Blue channels. */
425 _mesa_ColorMask(GL_FALSE, GL_TRUE, GL_TRUE,
426 src_base_format != GL_LUMINANCE_ALPHA);
427 _mesa_ClearColor(0.0, 0.0, 0.0, 1.0);
428 _mesa_Clear(GL_COLOR_BUFFER_BIT);
429 }
430
431 for (z = 1; z < depth; z++) {
432 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
433 tex_image, zoffset + z);
434
435 _mesa_update_state(ctx);
436
437 _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
438 xoffset, yoffset,
439 xoffset + width, yoffset + height,
440 0, z * image_height,
441 width, z * image_height + height,
442 GL_COLOR_BUFFER_BIT, GL_NEAREST);
443 if (clear_channels_to_zero)
444 _mesa_Clear(GL_COLOR_BUFFER_BIT);
445 }
446
447 /* Unmask the color channels and restore the saved clear color values. */
448 if (clear_channels_to_zero) {
449 _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
450 _mesa_ClearColor(save_clear_color[0], save_clear_color[1],
451 save_clear_color[2], save_clear_color[3]);
452 }
453
454 success = true;
455
456 fail:
457 _mesa_DeleteFramebuffers(2, fbos);
458 _mesa_DeleteTextures(1, &pbo_tex);
459 _mesa_DeleteBuffers(1, &pbo);
460
461 _mesa_meta_end(ctx);
462
463 return success;
464 }