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