meta: Abort texture upload if pixels == null and no pixel unpack buffer set
[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 "bufferobj.h"
29 #include "buffers.h"
30 #include "fbobject.h"
31 #include "glformats.h"
32 #include "glheader.h"
33 #include "image.h"
34 #include "macros.h"
35 #include "meta.h"
36 #include "pbo.h"
37 #include "readpix.h"
38 #include "shaderapi.h"
39 #include "state.h"
40 #include "teximage.h"
41 #include "texobj.h"
42 #include "texstate.h"
43 #include "uniforms.h"
44 #include "varray.h"
45
46 static struct gl_texture_image *
47 create_texture_for_pbo(struct gl_context *ctx, bool create_pbo,
48 GLenum pbo_target, int width, int height,
49 GLenum format, GLenum type, const void *pixels,
50 const struct gl_pixelstore_attrib *packing,
51 GLuint *tmp_pbo, GLuint *tmp_tex)
52 {
53 uint32_t pbo_format;
54 GLenum internal_format;
55 unsigned row_stride;
56 struct gl_buffer_object *buffer_obj;
57 struct gl_texture_object *tex_obj;
58 struct gl_texture_image *tex_image;
59 bool read_only;
60
61 if (packing->SwapBytes ||
62 packing->LsbFirst ||
63 packing->Invert)
64 return NULL;
65
66 pbo_format = _mesa_format_from_format_and_type(format, type);
67 if (_mesa_format_is_mesa_array_format(pbo_format))
68 pbo_format = _mesa_format_from_array_format(pbo_format);
69
70 if (!pbo_format || !ctx->TextureFormatSupported[pbo_format])
71 return NULL;
72
73 /* Account for SKIP_PIXELS, SKIP_ROWS, ALIGNMENT, and SKIP_IMAGES */
74 pixels = _mesa_image_address3d(packing, pixels,
75 width, height, format, type, 0, 0, 0);
76 row_stride = _mesa_image_row_stride(packing, width, format, type);
77
78 if (_mesa_is_bufferobj(packing->BufferObj)) {
79 *tmp_pbo = 0;
80 buffer_obj = packing->BufferObj;
81 } else {
82 bool is_pixel_pack = pbo_target == GL_PIXEL_PACK_BUFFER;
83
84 assert(create_pbo);
85
86 _mesa_GenBuffers(1, tmp_pbo);
87
88 /* We are not doing this inside meta_begin/end. However, we know the
89 * client doesn't have the given target bound, so we can go ahead and
90 * squash it. We'll set it back when we're done.
91 */
92 _mesa_BindBuffer(pbo_target, *tmp_pbo);
93
94 /* In case of GL_PIXEL_PACK_BUFFER, pass null pointer for the pixel
95 * data to avoid unnecessary data copying in _mesa_BufferData().
96 */
97 if (is_pixel_pack)
98 _mesa_BufferData(pbo_target, row_stride * height, NULL,
99 GL_STREAM_READ);
100 else
101 _mesa_BufferData(pbo_target, row_stride * height, pixels,
102 GL_STREAM_DRAW);
103
104 buffer_obj = packing->BufferObj;
105 pixels = NULL;
106
107 _mesa_BindBuffer(pbo_target, 0);
108 }
109
110 _mesa_GenTextures(1, tmp_tex);
111 tex_obj = _mesa_lookup_texture(ctx, *tmp_tex);
112 _mesa_initialize_texture_object(ctx, tex_obj, *tmp_tex, GL_TEXTURE_2D);
113 /* This must be set after _mesa_initialize_texture_object, not before. */
114 tex_obj->Immutable = GL_TRUE;
115 /* This is required for interactions with ARB_texture_view. */
116 tex_obj->NumLayers = 1;
117
118 internal_format = _mesa_get_format_base_format(pbo_format);
119
120 tex_image = _mesa_get_tex_image(ctx, tex_obj, tex_obj->Target, 0);
121 _mesa_init_teximage_fields(ctx, tex_image, width, height, 1,
122 0, internal_format, pbo_format);
123
124 read_only = pbo_target == GL_PIXEL_UNPACK_BUFFER;
125 if (!ctx->Driver.SetTextureStorageForBufferObject(ctx, tex_obj,
126 buffer_obj,
127 (intptr_t)pixels,
128 row_stride,
129 read_only)) {
130 _mesa_DeleteTextures(1, tmp_tex);
131 _mesa_DeleteBuffers(1, tmp_pbo);
132 return NULL;
133 }
134
135 return tex_image;
136 }
137
138 bool
139 _mesa_meta_pbo_TexSubImage(struct gl_context *ctx, GLuint dims,
140 struct gl_texture_image *tex_image,
141 int xoffset, int yoffset, int zoffset,
142 int width, int height, int depth,
143 GLenum format, GLenum type, const void *pixels,
144 bool allocate_storage, bool create_pbo,
145 const struct gl_pixelstore_attrib *packing)
146 {
147 GLuint pbo = 0, pbo_tex = 0, fbos[2] = { 0, 0 };
148 int full_height, image_height;
149 struct gl_texture_image *pbo_tex_image;
150 GLenum status;
151 bool success = false;
152 int z;
153
154 if (!_mesa_is_bufferobj(packing->BufferObj) &&
155 (!create_pbo || pixels == NULL))
156 return false;
157
158 if (format == GL_DEPTH_COMPONENT ||
159 format == GL_DEPTH_STENCIL ||
160 format == GL_STENCIL_INDEX ||
161 format == GL_COLOR_INDEX)
162 return false;
163
164 if (ctx->_ImageTransferState)
165 return false;
166
167 /* For arrays, use a tall (height * depth) 2D texture but taking into
168 * account the inter-image padding specified with the image height packing
169 * property.
170 */
171 image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
172 full_height = image_height * (depth - 1) + height;
173
174 pbo_tex_image = create_texture_for_pbo(ctx, create_pbo,
175 GL_PIXEL_UNPACK_BUFFER,
176 width, full_height,
177 format, type, pixels, packing,
178 &pbo, &pbo_tex);
179 if (!pbo_tex_image)
180 return false;
181
182 if (allocate_storage)
183 ctx->Driver.AllocTextureImageBuffer(ctx, tex_image);
184
185 _mesa_meta_begin(ctx, ~(MESA_META_PIXEL_TRANSFER |
186 MESA_META_PIXEL_STORE));
187
188 _mesa_GenFramebuffers(2, fbos);
189 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
190 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
191
192 if (tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
193 assert(depth == 1);
194 assert(zoffset == 0);
195 depth = height;
196 height = 1;
197 image_height = 1;
198 zoffset = yoffset;
199 yoffset = 0;
200 }
201
202 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
203 pbo_tex_image, 0);
204 /* If this passes on the first layer it should pass on the others */
205 status = _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER);
206 if (status != GL_FRAMEBUFFER_COMPLETE)
207 goto fail;
208
209 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
210 tex_image, zoffset);
211 /* If this passes on the first layer it should pass on the others */
212 status = _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
213 if (status != GL_FRAMEBUFFER_COMPLETE)
214 goto fail;
215
216 _mesa_update_state(ctx);
217
218 if (_mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
219 0, 0, width, height,
220 xoffset, yoffset,
221 xoffset + width, yoffset + height,
222 GL_COLOR_BUFFER_BIT, GL_NEAREST))
223 goto fail;
224
225 for (z = 1; z < depth; z++) {
226 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
227 tex_image, zoffset + z);
228
229 _mesa_update_state(ctx);
230
231 _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
232 0, z * image_height,
233 width, z * image_height + height,
234 xoffset, yoffset,
235 xoffset + width, yoffset + height,
236 GL_COLOR_BUFFER_BIT, GL_NEAREST);
237 }
238
239 success = true;
240
241 fail:
242 _mesa_DeleteFramebuffers(2, fbos);
243 _mesa_DeleteTextures(1, &pbo_tex);
244 _mesa_DeleteBuffers(1, &pbo);
245
246 _mesa_meta_end(ctx);
247
248 return success;
249 }
250
251 bool
252 _mesa_meta_pbo_GetTexSubImage(struct gl_context *ctx, GLuint dims,
253 struct gl_texture_image *tex_image,
254 int xoffset, int yoffset, int zoffset,
255 int width, int height, int depth,
256 GLenum format, GLenum type, const void *pixels,
257 const struct gl_pixelstore_attrib *packing)
258 {
259 GLuint pbo = 0, pbo_tex = 0, fbos[2] = { 0, 0 };
260 int full_height, image_height;
261 struct gl_texture_image *pbo_tex_image;
262 struct gl_renderbuffer *rb = NULL;
263 GLenum status;
264 bool success = false;
265 int z;
266
267 if (!_mesa_is_bufferobj(packing->BufferObj))
268 return false;
269
270 if (format == GL_DEPTH_COMPONENT ||
271 format == GL_DEPTH_STENCIL ||
272 format == GL_STENCIL_INDEX ||
273 format == GL_COLOR_INDEX)
274 return false;
275
276 if (ctx->_ImageTransferState)
277 return false;
278
279
280 if (!tex_image) {
281 rb = ctx->ReadBuffer->_ColorReadBuffer;
282 if (_mesa_need_rgb_to_luminance_conversion(rb->Format, format))
283 return false;
284 }
285
286 /* For arrays, use a tall (height * depth) 2D texture but taking into
287 * account the inter-image padding specified with the image height packing
288 * property.
289 */
290 image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
291 full_height = image_height * (depth - 1) + height;
292
293 pbo_tex_image = create_texture_for_pbo(ctx, false, GL_PIXEL_PACK_BUFFER,
294 width, full_height * depth,
295 format, type, pixels, packing,
296 &pbo, &pbo_tex);
297 if (!pbo_tex_image)
298 return false;
299
300 _mesa_meta_begin(ctx, ~(MESA_META_PIXEL_TRANSFER |
301 MESA_META_PIXEL_STORE));
302
303 _mesa_GenFramebuffers(2, fbos);
304
305 if (tex_image && tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
306 assert(depth == 1);
307 assert(zoffset == 0);
308 depth = height;
309 height = 1;
310 image_height = 1;
311 zoffset = yoffset;
312 yoffset = 0;
313 }
314
315 /* If we were given a texture, bind it to the read framebuffer. If not,
316 * we're doing a ReadPixels and we should just use whatever framebuffer
317 * the client has bound.
318 */
319 if (tex_image) {
320 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
321 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
322 tex_image, zoffset);
323 /* If this passes on the first layer it should pass on the others */
324 status = _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER);
325 if (status != GL_FRAMEBUFFER_COMPLETE)
326 goto fail;
327 } else {
328 assert(depth == 1);
329 }
330
331 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
332 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
333 pbo_tex_image, 0);
334 /* If this passes on the first layer it should pass on the others */
335 status = _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
336 if (status != GL_FRAMEBUFFER_COMPLETE)
337 goto fail;
338
339 _mesa_update_state(ctx);
340
341 if (_mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
342 xoffset, yoffset,
343 xoffset + width, yoffset + height,
344 0, 0, width, height,
345 GL_COLOR_BUFFER_BIT, GL_NEAREST))
346 goto fail;
347
348 for (z = 1; z < depth; z++) {
349 _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
350 tex_image, zoffset + z);
351
352 _mesa_update_state(ctx);
353
354 _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
355 xoffset, yoffset,
356 xoffset + width, yoffset + height,
357 0, z * image_height,
358 width, z * image_height + height,
359 GL_COLOR_BUFFER_BIT, GL_NEAREST);
360 }
361
362 success = true;
363
364 fail:
365 _mesa_DeleteFramebuffers(2, fbos);
366 _mesa_DeleteTextures(1, &pbo_tex);
367 _mesa_DeleteBuffers(1, &pbo);
368
369 _mesa_meta_end(ctx);
370
371 return success;
372 }