st_texture.c revision 7ec681f3
1/************************************************************************** 2 * 3 * Copyright 2007 VMware, Inc. 4 * 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 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28#include <stdio.h> 29 30#include "st_context.h" 31#include "st_format.h" 32#include "st_texture.h" 33#include "st_cb_fbo.h" 34#include "main/enums.h" 35 36#include "pipe/p_state.h" 37#include "pipe/p_context.h" 38#include "pipe/p_defines.h" 39#include "util/u_inlines.h" 40#include "util/format/u_format.h" 41#include "util/u_rect.h" 42#include "util/u_math.h" 43#include "util/u_memory.h" 44#include "tgsi/tgsi_from_mesa.h" 45 46 47#define DBG if(0) printf 48 49 50/** 51 * Allocate a new pipe_resource object 52 * width0, height0, depth0 are the dimensions of the level 0 image 53 * (the highest resolution). last_level indicates how many mipmap levels 54 * to allocate storage for. For non-mipmapped textures, this will be zero. 55 */ 56struct pipe_resource * 57st_texture_create(struct st_context *st, 58 enum pipe_texture_target target, 59 enum pipe_format format, 60 GLuint last_level, 61 GLuint width0, 62 GLuint height0, 63 GLuint depth0, 64 GLuint layers, 65 GLuint nr_samples, 66 GLuint bind) 67{ 68 struct pipe_resource pt, *newtex; 69 struct pipe_screen *screen = st->screen; 70 71 assert(target < PIPE_MAX_TEXTURE_TYPES); 72 assert(width0 > 0); 73 assert(height0 > 0); 74 assert(depth0 > 0); 75 if (target == PIPE_TEXTURE_CUBE) 76 assert(layers == 6); 77 78 DBG("%s target %d format %s last_level %d\n", __func__, 79 (int) target, util_format_name(format), last_level); 80 81 assert(format); 82 assert(screen->is_format_supported(screen, format, target, 0, 0, 83 PIPE_BIND_SAMPLER_VIEW)); 84 85 memset(&pt, 0, sizeof(pt)); 86 pt.target = target; 87 pt.format = format; 88 pt.last_level = last_level; 89 pt.width0 = width0; 90 pt.height0 = height0; 91 pt.depth0 = depth0; 92 pt.array_size = layers; 93 pt.usage = PIPE_USAGE_DEFAULT; 94 pt.bind = bind; 95 /* only set this for OpenGL textures, not renderbuffers */ 96 pt.flags = PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY; 97 pt.nr_samples = nr_samples; 98 pt.nr_storage_samples = nr_samples; 99 100 newtex = screen->resource_create(screen, &pt); 101 102 assert(!newtex || pipe_is_referenced(&newtex->reference)); 103 104 return newtex; 105} 106 107 108/** 109 * In OpenGL the number of 1D array texture layers is the "height" and 110 * the number of 2D array texture layers is the "depth". In Gallium the 111 * number of layers in an array texture is a separate 'array_size' field. 112 * This function converts dimensions from the former to the later. 113 */ 114void 115st_gl_texture_dims_to_pipe_dims(GLenum texture, 116 unsigned widthIn, 117 uint16_t heightIn, 118 uint16_t depthIn, 119 unsigned *widthOut, 120 uint16_t *heightOut, 121 uint16_t *depthOut, 122 uint16_t *layersOut) 123{ 124 switch (texture) { 125 case GL_TEXTURE_1D: 126 case GL_PROXY_TEXTURE_1D: 127 assert(heightIn == 1); 128 assert(depthIn == 1); 129 *widthOut = widthIn; 130 *heightOut = 1; 131 *depthOut = 1; 132 *layersOut = 1; 133 break; 134 case GL_TEXTURE_1D_ARRAY: 135 case GL_PROXY_TEXTURE_1D_ARRAY: 136 assert(depthIn == 1); 137 *widthOut = widthIn; 138 *heightOut = 1; 139 *depthOut = 1; 140 *layersOut = heightIn; 141 break; 142 case GL_TEXTURE_2D: 143 case GL_PROXY_TEXTURE_2D: 144 case GL_TEXTURE_RECTANGLE: 145 case GL_PROXY_TEXTURE_RECTANGLE: 146 case GL_TEXTURE_EXTERNAL_OES: 147 case GL_PROXY_TEXTURE_2D_MULTISAMPLE: 148 case GL_TEXTURE_2D_MULTISAMPLE: 149 assert(depthIn == 1); 150 *widthOut = widthIn; 151 *heightOut = heightIn; 152 *depthOut = 1; 153 *layersOut = 1; 154 break; 155 case GL_TEXTURE_CUBE_MAP: 156 case GL_PROXY_TEXTURE_CUBE_MAP: 157 case GL_TEXTURE_CUBE_MAP_POSITIVE_X: 158 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X: 159 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y: 160 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: 161 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: 162 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: 163 assert(depthIn == 1); 164 *widthOut = widthIn; 165 *heightOut = heightIn; 166 *depthOut = 1; 167 *layersOut = 6; 168 break; 169 case GL_TEXTURE_2D_ARRAY: 170 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: 171 case GL_PROXY_TEXTURE_2D_ARRAY: 172 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY: 173 *widthOut = widthIn; 174 *heightOut = heightIn; 175 *depthOut = 1; 176 *layersOut = depthIn; 177 break; 178 case GL_TEXTURE_CUBE_MAP_ARRAY: 179 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY: 180 *widthOut = widthIn; 181 *heightOut = heightIn; 182 *depthOut = 1; 183 *layersOut = util_align_npot(depthIn, 6); 184 break; 185 default: 186 assert(0 && "Unexpected texture in st_gl_texture_dims_to_pipe_dims()"); 187#if defined(NDEBUG) || defined(DEBUG) 188 FALLTHROUGH; 189#endif 190 case GL_TEXTURE_3D: 191 case GL_PROXY_TEXTURE_3D: 192 *widthOut = widthIn; 193 *heightOut = heightIn; 194 *depthOut = depthIn; 195 *layersOut = 1; 196 break; 197 } 198} 199 200 201/** 202 * Check if a texture image can be pulled into a unified mipmap texture. 203 */ 204GLboolean 205st_texture_match_image(struct st_context *st, 206 const struct pipe_resource *pt, 207 const struct gl_texture_image *image) 208{ 209 unsigned ptWidth; 210 uint16_t ptHeight, ptDepth, ptLayers; 211 212 /* Images with borders are never pulled into mipmap textures. 213 */ 214 if (image->Border) 215 return GL_FALSE; 216 217 /* Check if this image's format matches the established texture's format. 218 */ 219 if (st_mesa_format_to_pipe_format(st, image->TexFormat) != pt->format) 220 return GL_FALSE; 221 222 st_gl_texture_dims_to_pipe_dims(image->TexObject->Target, 223 image->Width, image->Height, image->Depth, 224 &ptWidth, &ptHeight, &ptDepth, &ptLayers); 225 226 /* Test if this image's size matches what's expected in the 227 * established texture. 228 */ 229 if (ptWidth != u_minify(pt->width0, image->Level) || 230 ptHeight != u_minify(pt->height0, image->Level) || 231 ptDepth != u_minify(pt->depth0, image->Level) || 232 ptLayers != pt->array_size) 233 return GL_FALSE; 234 235 if (image->Level > pt->last_level) 236 return GL_FALSE; 237 238 return GL_TRUE; 239} 240 241 242/** 243 * Map a texture image and return the address for a particular 2D face/slice/ 244 * layer. The stImage indicates the cube face and mipmap level. The slice 245 * of the 3D texture is passed in 'zoffset'. 246 * \param usage one of the PIPE_MAP_x values 247 * \param x, y, w, h the region of interest of the 2D image. 248 * \return address of mapping or NULL if any error 249 */ 250GLubyte * 251st_texture_image_map(struct st_context *st, struct st_texture_image *stImage, 252 enum pipe_map_flags usage, 253 GLuint x, GLuint y, GLuint z, 254 GLuint w, GLuint h, GLuint d, 255 struct pipe_transfer **transfer) 256{ 257 struct st_texture_object *stObj = 258 st_texture_object(stImage->base.TexObject); 259 GLuint level; 260 void *map; 261 262 DBG("%s \n", __func__); 263 264 if (!stImage->pt) 265 return NULL; 266 267 if (stObj->pt != stImage->pt) 268 level = 0; 269 else 270 level = stImage->base.Level; 271 272 if (stObj->base.Immutable) { 273 level += stObj->base.Attrib.MinLevel; 274 z += stObj->base.Attrib.MinLayer; 275 if (stObj->pt->array_size > 1) 276 d = MIN2(d, stObj->base.Attrib.NumLayers); 277 } 278 279 z += stImage->base.Face; 280 281 map = pipe_texture_map_3d(st->pipe, stImage->pt, level, usage, 282 x, y, z, w, h, d, transfer); 283 if (map) { 284 /* Enlarge the transfer array if it's not large enough. */ 285 if (z >= stImage->num_transfers) { 286 unsigned new_size = z + 1; 287 288 stImage->transfer = realloc(stImage->transfer, 289 new_size * sizeof(struct st_texture_image_transfer)); 290 memset(&stImage->transfer[stImage->num_transfers], 0, 291 (new_size - stImage->num_transfers) * 292 sizeof(struct st_texture_image_transfer)); 293 stImage->num_transfers = new_size; 294 } 295 296 assert(!stImage->transfer[z].transfer); 297 stImage->transfer[z].transfer = *transfer; 298 } 299 return map; 300} 301 302 303void 304st_texture_image_unmap(struct st_context *st, 305 struct st_texture_image *stImage, unsigned slice) 306{ 307 struct pipe_context *pipe = st->pipe; 308 struct st_texture_object *stObj = 309 st_texture_object(stImage->base.TexObject); 310 struct pipe_transfer **transfer; 311 312 if (stObj->base.Immutable) 313 slice += stObj->base.Attrib.MinLayer; 314 transfer = &stImage->transfer[slice + stImage->base.Face].transfer; 315 316 DBG("%s\n", __func__); 317 318 pipe_texture_unmap(pipe, *transfer); 319 *transfer = NULL; 320} 321 322 323/** 324 * For debug only: get/print center pixel in the src resource. 325 */ 326static void 327print_center_pixel(struct pipe_context *pipe, struct pipe_resource *src) 328{ 329 struct pipe_transfer *xfer; 330 struct pipe_box region; 331 ubyte *map; 332 333 region.x = src->width0 / 2; 334 region.y = src->height0 / 2; 335 region.z = 0; 336 region.width = 1; 337 region.height = 1; 338 region.depth = 1; 339 340 map = pipe->texture_map(pipe, src, 0, PIPE_MAP_READ, ®ion, &xfer); 341 342 printf("center pixel: %d %d %d %d\n", map[0], map[1], map[2], map[3]); 343 344 pipe->texture_unmap(pipe, xfer); 345} 346 347 348/** 349 * Copy the image at level=0 in 'src' to the 'dst' resource at 'dstLevel'. 350 * This is used to copy mipmap images from one texture buffer to another. 351 * This typically happens when our initial guess at the total texture size 352 * is incorrect (see the guess_and_alloc_texture() function). 353 */ 354void 355st_texture_image_copy(struct pipe_context *pipe, 356 struct pipe_resource *dst, GLuint dstLevel, 357 struct pipe_resource *src, GLuint srcLevel, 358 GLuint face) 359{ 360 GLuint width = u_minify(dst->width0, dstLevel); 361 GLuint height = u_minify(dst->height0, dstLevel); 362 GLuint depth = u_minify(dst->depth0, dstLevel); 363 struct pipe_box src_box; 364 GLuint i; 365 366 if (u_minify(src->width0, srcLevel) != width || 367 u_minify(src->height0, srcLevel) != height || 368 u_minify(src->depth0, srcLevel) != depth) { 369 /* The source image size doesn't match the destination image size. 370 * This can happen in some degenerate situations such as rendering to a 371 * cube map face which was set up with mismatched texture sizes. 372 */ 373 return; 374 } 375 376 src_box.x = 0; 377 src_box.y = 0; 378 src_box.width = width; 379 src_box.height = height; 380 src_box.depth = 1; 381 382 if (src->target == PIPE_TEXTURE_1D_ARRAY || 383 src->target == PIPE_TEXTURE_2D_ARRAY || 384 src->target == PIPE_TEXTURE_CUBE_ARRAY) { 385 face = 0; 386 depth = src->array_size; 387 } 388 389 /* Loop over 3D image slices */ 390 /* could (and probably should) use "true" 3d box here - 391 but drivers can't quite handle it yet */ 392 for (i = face; i < face + depth; i++) { 393 src_box.z = i; 394 395 if (0) { 396 print_center_pixel(pipe, src); 397 } 398 399 pipe->resource_copy_region(pipe, 400 dst, 401 dstLevel, 402 0, 0, i,/* destX, Y, Z */ 403 src, 404 srcLevel, 405 &src_box); 406 } 407} 408 409 410struct pipe_resource * 411st_create_color_map_texture(struct gl_context *ctx) 412{ 413 struct st_context *st = st_context(ctx); 414 struct pipe_resource *pt; 415 enum pipe_format format; 416 const uint texSize = 256; /* simple, and usually perfect */ 417 418 /* find an RGBA texture format */ 419 format = st_choose_format(st, GL_RGBA, GL_NONE, GL_NONE, 420 PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW, 421 false, false); 422 423 /* create texture for color map/table */ 424 pt = st_texture_create(st, PIPE_TEXTURE_2D, format, 0, 425 texSize, texSize, 1, 1, 0, PIPE_BIND_SAMPLER_VIEW); 426 return pt; 427} 428 429 430/** 431 * Destroy bound texture handles for the given stage. 432 */ 433static void 434st_destroy_bound_texture_handles_per_stage(struct st_context *st, 435 enum pipe_shader_type shader) 436{ 437 struct st_bound_handles *bound_handles = &st->bound_texture_handles[shader]; 438 struct pipe_context *pipe = st->pipe; 439 unsigned i; 440 441 if (likely(!bound_handles->num_handles)) 442 return; 443 444 for (i = 0; i < bound_handles->num_handles; i++) { 445 uint64_t handle = bound_handles->handles[i]; 446 447 pipe->make_texture_handle_resident(pipe, handle, false); 448 pipe->delete_texture_handle(pipe, handle); 449 } 450 free(bound_handles->handles); 451 bound_handles->handles = NULL; 452 bound_handles->num_handles = 0; 453} 454 455 456/** 457 * Destroy all bound texture handles in the context. 458 */ 459void 460st_destroy_bound_texture_handles(struct st_context *st) 461{ 462 unsigned i; 463 464 for (i = 0; i < PIPE_SHADER_TYPES; i++) { 465 st_destroy_bound_texture_handles_per_stage(st, i); 466 } 467} 468 469 470/** 471 * Destroy bound image handles for the given stage. 472 */ 473static void 474st_destroy_bound_image_handles_per_stage(struct st_context *st, 475 enum pipe_shader_type shader) 476{ 477 struct st_bound_handles *bound_handles = &st->bound_image_handles[shader]; 478 struct pipe_context *pipe = st->pipe; 479 unsigned i; 480 481 if (likely(!bound_handles->num_handles)) 482 return; 483 484 for (i = 0; i < bound_handles->num_handles; i++) { 485 uint64_t handle = bound_handles->handles[i]; 486 487 pipe->make_image_handle_resident(pipe, handle, GL_READ_WRITE, false); 488 pipe->delete_image_handle(pipe, handle); 489 } 490 free(bound_handles->handles); 491 bound_handles->handles = NULL; 492 bound_handles->num_handles = 0; 493} 494 495 496/** 497 * Destroy all bound image handles in the context. 498 */ 499void 500st_destroy_bound_image_handles(struct st_context *st) 501{ 502 unsigned i; 503 504 for (i = 0; i < PIPE_SHADER_TYPES; i++) { 505 st_destroy_bound_image_handles_per_stage(st, i); 506 } 507} 508 509 510/** 511 * Create a texture handle from a texture unit. 512 */ 513static GLuint64 514st_create_texture_handle_from_unit(struct st_context *st, 515 struct gl_program *prog, GLuint texUnit) 516{ 517 struct pipe_context *pipe = st->pipe; 518 struct pipe_sampler_view *view; 519 struct pipe_sampler_state sampler = {0}; 520 521 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */ 522 view = st_update_single_texture(st, texUnit, prog->sh.data->Version >= 130, 523 true, false); 524 if (!view) 525 return 0; 526 527 if (view->target != PIPE_BUFFER) 528 st_convert_sampler_from_unit(st, &sampler, texUnit); 529 530 assert(st->ctx->Texture.Unit[texUnit]._Current); 531 532 return pipe->create_texture_handle(pipe, view, &sampler); 533} 534 535 536/** 537 * Create an image handle from an image unit. 538 */ 539static GLuint64 540st_create_image_handle_from_unit(struct st_context *st, 541 struct gl_program *prog, GLuint imgUnit) 542{ 543 struct pipe_context *pipe = st->pipe; 544 struct pipe_image_view img; 545 546 st_convert_image_from_unit(st, &img, imgUnit, GL_READ_WRITE); 547 548 return pipe->create_image_handle(pipe, &img); 549} 550 551 552/** 553 * Make all bindless samplers bound to texture units resident in the context. 554 */ 555void 556st_make_bound_samplers_resident(struct st_context *st, 557 struct gl_program *prog) 558{ 559 enum pipe_shader_type shader = pipe_shader_type_from_mesa(prog->info.stage); 560 struct st_bound_handles *bound_handles = &st->bound_texture_handles[shader]; 561 struct pipe_context *pipe = st->pipe; 562 GLuint64 handle; 563 int i; 564 565 /* Remove previous bound texture handles for this stage. */ 566 st_destroy_bound_texture_handles_per_stage(st, shader); 567 568 if (likely(!prog->sh.HasBoundBindlessSampler)) 569 return; 570 571 for (i = 0; i < prog->sh.NumBindlessSamplers; i++) { 572 struct gl_bindless_sampler *sampler = &prog->sh.BindlessSamplers[i]; 573 574 if (!sampler->bound) 575 continue; 576 577 /* Request a new texture handle from the driver and make it resident. */ 578 handle = st_create_texture_handle_from_unit(st, prog, sampler->unit); 579 if (!handle) 580 continue; 581 582 pipe->make_texture_handle_resident(st->pipe, handle, true); 583 584 /* Overwrite the texture unit value by the resident handle before 585 * uploading the constant buffer. 586 */ 587 *(uint64_t *)sampler->data = handle; 588 589 /* Store the handle in the context. */ 590 bound_handles->handles = (uint64_t *) 591 realloc(bound_handles->handles, 592 (bound_handles->num_handles + 1) * sizeof(uint64_t)); 593 bound_handles->handles[bound_handles->num_handles] = handle; 594 bound_handles->num_handles++; 595 } 596} 597 598 599/** 600 * Make all bindless images bound to image units resident in the context. 601 */ 602void 603st_make_bound_images_resident(struct st_context *st, 604 struct gl_program *prog) 605{ 606 enum pipe_shader_type shader = pipe_shader_type_from_mesa(prog->info.stage); 607 struct st_bound_handles *bound_handles = &st->bound_image_handles[shader]; 608 struct pipe_context *pipe = st->pipe; 609 GLuint64 handle; 610 int i; 611 612 /* Remove previous bound image handles for this stage. */ 613 st_destroy_bound_image_handles_per_stage(st, shader); 614 615 if (likely(!prog->sh.HasBoundBindlessImage)) 616 return; 617 618 for (i = 0; i < prog->sh.NumBindlessImages; i++) { 619 struct gl_bindless_image *image = &prog->sh.BindlessImages[i]; 620 621 if (!image->bound) 622 continue; 623 624 /* Request a new image handle from the driver and make it resident. */ 625 handle = st_create_image_handle_from_unit(st, prog, image->unit); 626 if (!handle) 627 continue; 628 629 pipe->make_image_handle_resident(st->pipe, handle, GL_READ_WRITE, true); 630 631 /* Overwrite the image unit value by the resident handle before uploading 632 * the constant buffer. 633 */ 634 *(uint64_t *)image->data = handle; 635 636 /* Store the handle in the context. */ 637 bound_handles->handles = (uint64_t *) 638 realloc(bound_handles->handles, 639 (bound_handles->num_handles + 1) * sizeof(uint64_t)); 640 bound_handles->handles[bound_handles->num_handles] = handle; 641 bound_handles->num_handles++; 642 } 643} 644