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