meta_tex_subimage.c revision a4ff47ade9d95a27c9c55afbf6dd77d3f3b10562
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
46static struct gl_texture_image *
47create_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
138bool
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) && !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   /* For arrays, use a tall (height * depth) 2D texture but taking into
167    * account the inter-image padding specified with the image height packing
168    * property.
169    */
170   image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
171   full_height = image_height * (depth - 1) + height;
172
173   pbo_tex_image = create_texture_for_pbo(ctx, create_pbo,
174                                          GL_PIXEL_UNPACK_BUFFER,
175                                          width, full_height,
176                                          format, type, pixels, packing,
177                                          &pbo, &pbo_tex);
178   if (!pbo_tex_image)
179      return false;
180
181   if (allocate_storage)
182      ctx->Driver.AllocTextureImageBuffer(ctx, tex_image);
183
184   _mesa_meta_begin(ctx, ~(MESA_META_PIXEL_TRANSFER |
185                           MESA_META_PIXEL_STORE));
186
187   _mesa_GenFramebuffers(2, fbos);
188   _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
189   _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
190
191   if (tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
192      assert(depth == 1);
193      assert(zoffset == 0);
194      depth = height;
195      height = 1;
196      image_height = 1;
197      zoffset = yoffset;
198      yoffset = 0;
199   }
200
201   _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
202                             pbo_tex_image, 0);
203   /* If this passes on the first layer it should pass on the others */
204   status = _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER);
205   if (status != GL_FRAMEBUFFER_COMPLETE)
206      goto fail;
207
208   _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
209                             tex_image, zoffset);
210   /* If this passes on the first layer it should pass on the others */
211   status = _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
212   if (status != GL_FRAMEBUFFER_COMPLETE)
213      goto fail;
214
215   _mesa_update_state(ctx);
216
217   if (_mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
218                                  0, 0, width, height,
219                                  xoffset, yoffset,
220                                  xoffset + width, yoffset + height,
221                                  GL_COLOR_BUFFER_BIT, GL_NEAREST))
222      goto fail;
223
224   for (z = 1; z < depth; z++) {
225      _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
226                                tex_image, zoffset + z);
227
228      _mesa_update_state(ctx);
229
230      _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
231                                 0, z * image_height,
232                                 width, z * image_height + height,
233                                 xoffset, yoffset,
234                                 xoffset + width, yoffset + height,
235                                 GL_COLOR_BUFFER_BIT, GL_NEAREST);
236   }
237
238   success = true;
239
240fail:
241   _mesa_DeleteFramebuffers(2, fbos);
242   _mesa_DeleteTextures(1, &pbo_tex);
243   _mesa_DeleteBuffers(1, &pbo);
244
245   _mesa_meta_end(ctx);
246
247   return success;
248}
249
250bool
251_mesa_meta_pbo_GetTexSubImage(struct gl_context *ctx, GLuint dims,
252                              struct gl_texture_image *tex_image,
253                              int xoffset, int yoffset, int zoffset,
254                              int width, int height, int depth,
255                              GLenum format, GLenum type, const void *pixels,
256                              const struct gl_pixelstore_attrib *packing)
257{
258   GLuint pbo = 0, pbo_tex = 0, fbos[2] = { 0, 0 };
259   int full_height, image_height;
260   struct gl_texture_image *pbo_tex_image;
261   struct gl_renderbuffer *rb = NULL;
262   GLenum status;
263   bool success = false;
264   int z;
265
266   if (!_mesa_is_bufferobj(packing->BufferObj))
267      return false;
268
269   if (format == GL_DEPTH_COMPONENT ||
270       format == GL_DEPTH_STENCIL ||
271       format == GL_STENCIL_INDEX ||
272       format == GL_COLOR_INDEX)
273      return false;
274
275   if (ctx->_ImageTransferState)
276      return false;
277
278
279   if (!tex_image) {
280      rb = ctx->ReadBuffer->_ColorReadBuffer;
281      if (_mesa_need_rgb_to_luminance_conversion(rb->Format, format))
282         return false;
283   }
284
285   /* For arrays, use a tall (height * depth) 2D texture but taking into
286    * account the inter-image padding specified with the image height packing
287    * property.
288    */
289   image_height = packing->ImageHeight == 0 ? height : packing->ImageHeight;
290   full_height = image_height * (depth - 1) + height;
291
292   pbo_tex_image = create_texture_for_pbo(ctx, false, GL_PIXEL_PACK_BUFFER,
293                                          width, full_height * depth,
294                                          format, type, pixels, packing,
295                                          &pbo, &pbo_tex);
296   if (!pbo_tex_image)
297      return false;
298
299   _mesa_meta_begin(ctx, ~(MESA_META_PIXEL_TRANSFER |
300                           MESA_META_PIXEL_STORE));
301
302   _mesa_GenFramebuffers(2, fbos);
303
304   if (tex_image && tex_image->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
305      assert(depth == 1);
306      assert(zoffset == 0);
307      depth = height;
308      height = 1;
309      image_height = 1;
310      zoffset = yoffset;
311      yoffset = 0;
312   }
313
314   /* If we were given a texture, bind it to the read framebuffer.  If not,
315    * we're doing a ReadPixels and we should just use whatever framebuffer
316    * the client has bound.
317    */
318   if (tex_image) {
319      _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
320      _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
321                                tex_image, zoffset);
322      /* If this passes on the first layer it should pass on the others */
323      status = _mesa_CheckFramebufferStatus(GL_READ_FRAMEBUFFER);
324      if (status != GL_FRAMEBUFFER_COMPLETE)
325         goto fail;
326   } else {
327      assert(depth == 1);
328   }
329
330   _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
331   _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
332                             pbo_tex_image, 0);
333   /* If this passes on the first layer it should pass on the others */
334   status = _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
335   if (status != GL_FRAMEBUFFER_COMPLETE)
336      goto fail;
337
338   _mesa_update_state(ctx);
339
340   if (_mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
341                                  xoffset, yoffset,
342                                  xoffset + width, yoffset + height,
343                                  0, 0, width, height,
344                                  GL_COLOR_BUFFER_BIT, GL_NEAREST))
345      goto fail;
346
347   for (z = 1; z < depth; z++) {
348      _mesa_meta_bind_fbo_image(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
349                                tex_image, zoffset + z);
350
351      _mesa_update_state(ctx);
352
353      _mesa_meta_BlitFramebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
354                                 xoffset, yoffset,
355                                 xoffset + width, yoffset + height,
356                                 0, z * image_height,
357                                 width, z * image_height + height,
358                                 GL_COLOR_BUFFER_BIT, GL_NEAREST);
359   }
360
361   success = true;
362
363fail:
364   _mesa_DeleteFramebuffers(2, fbos);
365   _mesa_DeleteTextures(1, &pbo_tex);
366   _mesa_DeleteBuffers(1, &pbo);
367
368   _mesa_meta_end(ctx);
369
370   return success;
371}
372