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