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