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