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