meta: Fix reading luminance texture as rgba in _mesa_meta_pbo_GetTexSubImage()
[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, 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)
54 {
55 uint32_t pbo_format;
56 GLenum internal_format;
57 unsigned row_stride;
58 struct gl_buffer_object *buffer_obj;
59 struct gl_texture_object *tex_obj;
60 struct gl_texture_image *tex_image;
61 bool read_only;
62
63 if (packing->SwapBytes ||
64 packing->LsbFirst ||
65 packing->Invert)
66 return NULL;
67
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);
71
72 if (!pbo_format || !ctx->TextureFormatSupported[pbo_format])
73 return NULL;
74
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);
79
80 if (_mesa_is_bufferobj(packing->BufferObj)) {
81 *tmp_pbo = 0;
82 buffer_obj = packing->BufferObj;
83 } else {
84 bool is_pixel_pack = pbo_target == GL_PIXEL_PACK_BUFFER;
85
86 assert(create_pbo);
87
88 _mesa_GenBuffers(1, tmp_pbo);
89
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.
93 */
94 _mesa_BindBuffer(pbo_target, *tmp_pbo);
95
96 /* In case of GL_PIXEL_PACK_BUFFER, pass null pointer for the pixel
97 * data to avoid unnecessary data copying in _mesa_BufferData().
98 */
99 if (is_pixel_pack)
100 _mesa_BufferData(pbo_target, row_stride * height, NULL,
101 GL_STREAM_READ);
102 else
103 _mesa_BufferData(pbo_target, row_stride * height, pixels,
104 GL_STREAM_DRAW);
105
106 buffer_obj = packing->BufferObj;
107 pixels = NULL;
108
109 _mesa_BindBuffer(pbo_target, 0);
110 }
111
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;
119
120 internal_format = _mesa_get_format_base_format(pbo_format);
121
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);
125
126 read_only = pbo_target == GL_PIXEL_UNPACK_BUFFER;
127 if (!ctx->Driver.SetTextureStorageForBufferObject(ctx, tex_obj,
128 buffer_obj,
129 (intptr_t)pixels,
130 row_stride,
131 read_only)) {
132 _mesa_DeleteTextures(1, tmp_tex);
133 _mesa_DeleteBuffers(1, tmp_pbo);
134 return NULL;
135 }
136
137 return tex_image;
138 }
139
140 bool
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)
148 {
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;
152 GLenum status;
153 bool success = false;
154 int z;
155
156 if (!_mesa_is_bufferobj(packing->BufferObj) &&
157 (!create_pbo || pixels == NULL))
158 return false;
159
160 if (format == GL_DEPTH_COMPONENT ||
161 format == GL_DEPTH_STENCIL ||
162 format == GL_STENCIL_INDEX ||
163 format == GL_COLOR_INDEX)
164 return false;
165
166 if (ctx->_ImageTransferState)
167 return false;
168
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
171 * property.
172 */
173 image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
174 full_height = image_height * (depth - 1) + height;
175
176 pbo_tex_image = create_texture_for_pbo(ctx, create_pbo,
177 GL_PIXEL_UNPACK_BUFFER,
178 width, full_height,
179 format, type, pixels, packing,
180 &pbo, &pbo_tex);
181 if (!pbo_tex_image)
182 return false;
183
184 if (allocate_storage)
185 ctx->Driver.AllocTextureImageBuffer(ctx, tex_image);
186
187 _mesa_meta_begin(ctx, ~(MESA_META_PIXEL_TRANSFER |
188 MESA_META_PIXEL_STORE));
189
190 _mesa_GenFramebuffers(2, fbos);
191 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
192 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
193
194 if (tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
195 assert(depth == 1);
196 assert(zoffset == 0);
197 depth = height;
198 height = 1;
199 image_height = 1;
200 zoffset = yoffset;
201 yoffset = 0;
202 }
203
204 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
205 pbo_tex_image, 0);
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)
209 goto fail;
210
211 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
212 tex_image, zoffset);
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)
216 goto fail;
217
218 _mesa_update_state(ctx);
219
220 if (_mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
221 0, 0, width, height,
222 xoffset, yoffset,
223 xoffset + width, yoffset + height,
224 GL_COLOR_BUFFER_BIT, GL_NEAREST))
225 goto fail;
226
227 for (z = 1; z < depth; z++) {
228 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
229 tex_image, zoffset + z);
230
231 _mesa_update_state(ctx);
232
233 _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
234 0, z * image_height,
235 width, z * image_height + height,
236 xoffset, yoffset,
237 xoffset + width, yoffset + height,
238 GL_COLOR_BUFFER_BIT, GL_NEAREST);
239 }
240
241 success = true;
242
243 fail:
244 _mesa_DeleteFramebuffers(2, fbos);
245 _mesa_DeleteTextures(1, &pbo_tex);
246 _mesa_DeleteBuffers(1, &pbo);
247
248 _mesa_meta_end(ctx);
249
250 return success;
251 }
252
253 static bool
254 need_signed_unsigned_int_conversion(mesa_format rbFormat,
255 GLenum format, GLenum type)
256 {
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 &&
266 (type == GL_INT ||
267 type == GL_SHORT ||
268 type == GL_BYTE));
269 }
270
271 bool
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)
278 {
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];
286 int z;
287
288 if (!_mesa_is_bufferobj(packing->BufferObj))
289 return false;
290
291 if (format == GL_DEPTH_COMPONENT ||
292 format == GL_DEPTH_STENCIL ||
293 format == GL_STENCIL_INDEX ||
294 format == GL_COLOR_INDEX)
295 return false;
296
297 /* Don't use meta path for readpixels in below conditions. */
298 if (!tex_image) {
299 rb = ctx->ReadBuffer->_ColorReadBuffer;
300
301 /* _mesa_get_readpixels_transfer_ops() includes the cases of read
302 * color clamping along with the ctx->_ImageTransferState.
303 */
304 if (_mesa_get_readpixels_transfer_ops(ctx, rb->Format, format,
305 type, GL_FALSE))
306 return false;
307
308 if (_mesa_need_rgb_to_luminance_conversion(rb->Format, format))
309 return false;
310
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.
315 */
316 if (need_signed_unsigned_int_conversion(rb->Format, format, type))
317 return false;
318 }
319
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
322 * property.
323 */
324 image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
325 full_height = image_height * (depth - 1) + height;
326
327 pbo_tex_image = create_texture_for_pbo(ctx, false, GL_PIXEL_PACK_BUFFER,
328 width, full_height * depth,
329 format, type, pixels, packing,
330 &pbo, &pbo_tex);
331 if (!pbo_tex_image)
332 return false;
333
334 _mesa_meta_begin(ctx, ~(MESA_META_PIXEL_TRANSFER |
335 MESA_META_PIXEL_STORE));
336
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);
340
341 _mesa_GenFramebuffers(2, fbos);
342
343 if (tex_image && tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
344 assert(depth == 1);
345 assert(zoffset == 0);
346 depth = height;
347 height = 1;
348 image_height = 1;
349 zoffset = yoffset;
350 yoffset = 0;
351 }
352
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.
356 */
357 if (tex_image) {
358 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
359 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
360 tex_image, zoffset);
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)
364 goto fail;
365 } else {
366 assert(depth == 1);
367 }
368
369 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
370 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
371 pbo_tex_image, 0);
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)
375 goto fail;
376
377 _mesa_update_state(ctx);
378
379 if (_mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
380 xoffset, yoffset,
381 xoffset + width, yoffset + height,
382 0, 0, width, height,
383 GL_COLOR_BUFFER_BIT, GL_NEAREST))
384 goto fail;
385
386 src_base_format = tex_image ?
387 tex_image->_BaseFormat :
388 ctx->ReadBuffer->_ColorReadBuffer->_BaseFormat;
389
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.
393 */
394 clear_channels_to_zero =
395 _mesa_need_luminance_to_rgb_conversion(src_base_format,
396 pbo_tex_image->_BaseFormat);
397
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);
405 }
406
407 for (z = 1; z < depth; z++) {
408 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
409 tex_image, zoffset + z);
410
411 _mesa_update_state(ctx);
412
413 _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
414 xoffset, yoffset,
415 xoffset + width, yoffset + height,
416 0, z * image_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);
421 }
422
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]);
428 }
429
430 success = true;
431
432 fail:
433 _mesa_DeleteFramebuffers(2, fbos);
434 _mesa_DeleteTextures(1, &pbo_tex);
435 _mesa_DeleteBuffers(1, &pbo);
436
437 _mesa_meta_end(ctx);
438
439 return success;
440 }