1/* 2 * Copyright 2016 VMware, Inc. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sub license, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial portions 15 * of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 26#include "pipe/p_context.h" 27#include "util/u_format.h" 28#include "util/u_inlines.h" 29 30#include "main/context.h" 31#include "main/macros.h" 32#include "main/mtypes.h" 33#include "main/teximage.h" 34#include "main/texobj.h" 35#include "program/prog_instruction.h" 36 37#include "st_context.h" 38#include "st_sampler_view.h" 39#include "st_texture.h" 40#include "st_format.h" 41#include "st_cb_bufferobjects.h" 42#include "st_cb_texture.h" 43 44 45/** 46 * Set the given view as the current context's view for the texture. 47 * 48 * Overwrites any pre-existing view of the context. 49 * 50 * Takes ownership of the view (i.e., stores the view without incrementing the 51 * reference count). 52 * 53 * \return the view, or NULL on error. In case of error, the reference to the 54 * view is released. 55 */ 56static struct pipe_sampler_view * 57st_texture_set_sampler_view(struct st_context *st, 58 struct st_texture_object *stObj, 59 struct pipe_sampler_view *view, 60 bool glsl130_or_later, bool srgb_skip_decode) 61{ 62 struct st_sampler_views *views; 63 struct st_sampler_view *free = NULL; 64 struct st_sampler_view *sv; 65 GLuint i; 66 67 simple_mtx_lock(&stObj->validate_mutex); 68 views = stObj->sampler_views; 69 70 for (i = 0; i < views->count; ++i) { 71 sv = &views->views[i]; 72 73 /* Is the array entry used ? */ 74 if (sv->view) { 75 /* check if the context matches */ 76 if (sv->view->context == st->pipe) { 77 pipe_sampler_view_reference(&sv->view, NULL); 78 goto found; 79 } 80 } else { 81 /* Found a free slot, remember that */ 82 free = sv; 83 } 84 } 85 86 /* Couldn't find a slot for our context, create a new one */ 87 if (free) { 88 sv = free; 89 } else { 90 if (views->count >= views->max) { 91 /* Allocate a larger container. */ 92 unsigned new_max = 2 * views->max; 93 unsigned new_size = sizeof(*views) + new_max * sizeof(views->views[0]); 94 95 if (new_max < views->max || 96 new_max > (UINT_MAX - sizeof(*views)) / sizeof(views->views[0])) { 97 pipe_sampler_view_reference(&view, NULL); 98 goto out; 99 } 100 101 struct st_sampler_views *new_views = malloc(new_size); 102 if (!new_views) { 103 pipe_sampler_view_reference(&view, NULL); 104 goto out; 105 } 106 107 new_views->count = views->count; 108 new_views->max = new_max; 109 memcpy(&new_views->views[0], &views->views[0], 110 views->count * sizeof(views->views[0])); 111 112 /* Initialize the pipe_sampler_view pointers to zero so that we don't 113 * have to worry about racing against readers when incrementing 114 * views->count. 115 */ 116 memset(&new_views->views[views->count], 0, 117 (new_max - views->count) * sizeof(views->views[0])); 118 119 /* Use memory release semantics to ensure that concurrent readers will 120 * get the correct contents of the new container. 121 * 122 * Also, the write should be atomic, but that's guaranteed anyway on 123 * all supported platforms. 124 */ 125 p_atomic_set(&stObj->sampler_views, new_views); 126 127 /* We keep the old container around until the texture object is 128 * deleted, because another thread may still be reading from it. We 129 * double the size of the container each time, so we end up with 130 * at most twice the total memory allocation. 131 */ 132 views->next = stObj->sampler_views_old; 133 stObj->sampler_views_old = views; 134 135 views = new_views; 136 } 137 138 sv = &views->views[views->count]; 139 140 /* Since modification is guarded by the lock, only the write part of the 141 * increment has to be atomic, and that's already guaranteed on all 142 * supported platforms without using an atomic intrinsic. 143 */ 144 views->count++; 145 } 146 147found: 148 assert(sv->view == NULL); 149 150 sv->glsl130_or_later = glsl130_or_later; 151 sv->srgb_skip_decode = srgb_skip_decode; 152 sv->view = view; 153 sv->st = st; 154 155out: 156 simple_mtx_unlock(&stObj->validate_mutex); 157 return view; 158} 159 160 161/** 162 * Return the most-recently validated sampler view for the texture \p stObj 163 * in the given context, if any. 164 * 165 * Performs no additional validation. 166 */ 167const struct st_sampler_view * 168st_texture_get_current_sampler_view(const struct st_context *st, 169 const struct st_texture_object *stObj) 170{ 171 const struct st_sampler_views *views = p_atomic_read(&stObj->sampler_views); 172 173 for (unsigned i = 0; i < views->count; ++i) { 174 const struct st_sampler_view *sv = &views->views[i]; 175 if (sv->view && sv->view->context == st->pipe) 176 return sv; 177 } 178 179 return NULL; 180} 181 182 183/** 184 * For the given texture object, release any sampler views which belong 185 * to the calling context. This is used to free any sampler views 186 * which belong to the context before the context is destroyed. 187 */ 188void 189st_texture_release_context_sampler_view(struct st_context *st, 190 struct st_texture_object *stObj) 191{ 192 GLuint i; 193 194 simple_mtx_lock(&stObj->validate_mutex); 195 struct st_sampler_views *views = stObj->sampler_views; 196 for (i = 0; i < views->count; ++i) { 197 struct pipe_sampler_view **sv = &views->views[i].view; 198 199 if (*sv && (*sv)->context == st->pipe) { 200 pipe_sampler_view_reference(sv, NULL); 201 break; 202 } 203 } 204 simple_mtx_unlock(&stObj->validate_mutex); 205} 206 207 208/** 209 * Release all sampler views attached to the given texture object, regardless 210 * of the context. This is called fairly frequently. For example, whenever 211 * the texture's base level, max level or swizzle change. 212 */ 213void 214st_texture_release_all_sampler_views(struct st_context *st, 215 struct st_texture_object *stObj) 216{ 217 /* TODO: This happens while a texture is deleted, because the Driver API 218 * is asymmetric: the driver allocates the texture object memory, but 219 * mesa/main frees it. 220 */ 221 if (!stObj->sampler_views) 222 return; 223 224 simple_mtx_lock(&stObj->validate_mutex); 225 struct st_sampler_views *views = stObj->sampler_views; 226 for (unsigned i = 0; i < views->count; ++i) { 227 struct st_sampler_view *stsv = &views->views[i]; 228 if (stsv->view) { 229 if (stsv->st != st) { 230 /* Transfer this reference to the zombie list. It will 231 * likely be freed when the zombie list is freed. 232 */ 233 st_save_zombie_sampler_view(stsv->st, stsv->view); 234 stsv->view = NULL; 235 } else { 236 pipe_sampler_view_reference(&stsv->view, NULL); 237 } 238 } 239 } 240 views->count = 0; 241 simple_mtx_unlock(&stObj->validate_mutex); 242} 243 244 245/* 246 * Delete the texture's sampler views and st_sampler_views containers. 247 * This is to be called just before a texture is deleted. 248 */ 249void 250st_delete_texture_sampler_views(struct st_context *st, 251 struct st_texture_object *stObj) 252{ 253 st_texture_release_all_sampler_views(st, stObj); 254 255 /* Free the container of the current per-context sampler views */ 256 assert(stObj->sampler_views->count == 0); 257 free(stObj->sampler_views); 258 stObj->sampler_views = NULL; 259 260 /* Free old sampler view containers */ 261 while (stObj->sampler_views_old) { 262 struct st_sampler_views *views = stObj->sampler_views_old; 263 stObj->sampler_views_old = views->next; 264 free(views); 265 } 266} 267 268 269/** 270 * Return swizzle1(swizzle2) 271 */ 272static unsigned 273swizzle_swizzle(unsigned swizzle1, unsigned swizzle2) 274{ 275 unsigned i, swz[4]; 276 277 if (swizzle1 == SWIZZLE_XYZW) { 278 /* identity swizzle, no change to swizzle2 */ 279 return swizzle2; 280 } 281 282 for (i = 0; i < 4; i++) { 283 unsigned s = GET_SWZ(swizzle1, i); 284 switch (s) { 285 case SWIZZLE_X: 286 case SWIZZLE_Y: 287 case SWIZZLE_Z: 288 case SWIZZLE_W: 289 swz[i] = GET_SWZ(swizzle2, s); 290 break; 291 case SWIZZLE_ZERO: 292 swz[i] = SWIZZLE_ZERO; 293 break; 294 case SWIZZLE_ONE: 295 swz[i] = SWIZZLE_ONE; 296 break; 297 default: 298 assert(!"Bad swizzle term"); 299 swz[i] = SWIZZLE_X; 300 } 301 } 302 303 return MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]); 304} 305 306 307/** 308 * Given a user-specified texture base format, the actual gallium texture 309 * format and the current GL_DEPTH_MODE, return a texture swizzle. 310 * 311 * Consider the case where the user requests a GL_RGB internal texture 312 * format the driver actually uses an RGBA format. The A component should 313 * be ignored and sampling from the texture should always return (r,g,b,1). 314 * But if we rendered to the texture we might have written A values != 1. 315 * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1. 316 * This function computes the texture swizzle needed to get the expected 317 * values. 318 * 319 * In the case of depth textures, the GL_DEPTH_MODE state determines the 320 * texture swizzle. 321 * 322 * This result must be composed with the user-specified swizzle to get 323 * the final swizzle. 324 */ 325static unsigned 326compute_texture_format_swizzle(GLenum baseFormat, GLenum depthMode, 327 bool glsl130_or_later) 328{ 329 switch (baseFormat) { 330 case GL_RGBA: 331 return SWIZZLE_XYZW; 332 case GL_RGB: 333 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE); 334 case GL_RG: 335 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE); 336 case GL_RED: 337 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO, 338 SWIZZLE_ZERO, SWIZZLE_ONE); 339 case GL_ALPHA: 340 return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO, 341 SWIZZLE_ZERO, SWIZZLE_W); 342 case GL_LUMINANCE: 343 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE); 344 case GL_LUMINANCE_ALPHA: 345 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_W); 346 case GL_INTENSITY: 347 return SWIZZLE_XXXX; 348 case GL_STENCIL_INDEX: 349 case GL_DEPTH_STENCIL: 350 case GL_DEPTH_COMPONENT: 351 /* Now examine the depth mode */ 352 switch (depthMode) { 353 case GL_LUMINANCE: 354 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE); 355 case GL_INTENSITY: 356 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X); 357 case GL_ALPHA: 358 /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore 359 * the depth mode and return float, while older shadow* functions 360 * and ARB_fp instructions return vec4 according to the depth mode. 361 * 362 * The problem with the GLSL 1.30 functions is that GL_ALPHA forces 363 * them to return 0, breaking them completely. 364 * 365 * A proper fix would increase code complexity and that's not worth 366 * it for a rarely used feature such as the GL_ALPHA depth mode 367 * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all 368 * shaders that use GLSL 1.30 or later. 369 * 370 * BTW, it's required that sampler views are updated when 371 * shaders change (check_sampler_swizzle takes care of that). 372 */ 373 if (glsl130_or_later) 374 return SWIZZLE_XXXX; 375 else 376 return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO, 377 SWIZZLE_ZERO, SWIZZLE_X); 378 case GL_RED: 379 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO, 380 SWIZZLE_ZERO, SWIZZLE_ONE); 381 default: 382 assert(!"Unexpected depthMode"); 383 return SWIZZLE_XYZW; 384 } 385 default: 386 assert(!"Unexpected baseFormat"); 387 return SWIZZLE_XYZW; 388 } 389} 390 391 392static unsigned 393get_texture_format_swizzle(const struct st_context *st, 394 const struct st_texture_object *stObj, 395 bool glsl130_or_later) 396{ 397 GLenum baseFormat = _mesa_base_tex_image(&stObj->base)->_BaseFormat; 398 unsigned tex_swizzle; 399 GLenum depth_mode = stObj->base.DepthMode; 400 401 /* In ES 3.0, DEPTH_TEXTURE_MODE is expected to be GL_RED for textures 402 * with depth component data specified with a sized internal format. 403 */ 404 if (_mesa_is_gles3(st->ctx) && 405 (baseFormat == GL_DEPTH_COMPONENT || 406 baseFormat == GL_DEPTH_STENCIL || 407 baseFormat == GL_STENCIL_INDEX)) { 408 const struct gl_texture_image *firstImage = 409 _mesa_base_tex_image(&stObj->base); 410 if (firstImage->InternalFormat != GL_DEPTH_COMPONENT && 411 firstImage->InternalFormat != GL_DEPTH_STENCIL && 412 firstImage->InternalFormat != GL_STENCIL_INDEX) 413 depth_mode = GL_RED; 414 } 415 tex_swizzle = compute_texture_format_swizzle(baseFormat, 416 depth_mode, 417 glsl130_or_later); 418 419 /* Combine the texture format swizzle with user's swizzle */ 420 return swizzle_swizzle(stObj->base._Swizzle, tex_swizzle); 421} 422 423 424/** 425 * Return TRUE if the texture's sampler view swizzle is not equal to 426 * the texture's swizzle. 427 * 428 * \param stObj the st texture object, 429 */ 430MAYBE_UNUSED static boolean 431check_sampler_swizzle(const struct st_context *st, 432 const struct st_texture_object *stObj, 433 const struct pipe_sampler_view *sv, 434 bool glsl130_or_later) 435{ 436 unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl130_or_later); 437 438 return ((sv->swizzle_r != GET_SWZ(swizzle, 0)) || 439 (sv->swizzle_g != GET_SWZ(swizzle, 1)) || 440 (sv->swizzle_b != GET_SWZ(swizzle, 2)) || 441 (sv->swizzle_a != GET_SWZ(swizzle, 3))); 442} 443 444 445static unsigned 446last_level(const struct st_texture_object *stObj) 447{ 448 unsigned ret = MIN2(stObj->base.MinLevel + stObj->base._MaxLevel, 449 stObj->pt->last_level); 450 if (stObj->base.Immutable) 451 ret = MIN2(ret, stObj->base.MinLevel + stObj->base.NumLevels - 1); 452 return ret; 453} 454 455 456static unsigned 457last_layer(const struct st_texture_object *stObj) 458{ 459 if (stObj->base.Immutable && stObj->pt->array_size > 1) 460 return MIN2(stObj->base.MinLayer + stObj->base.NumLayers - 1, 461 stObj->pt->array_size - 1); 462 return stObj->pt->array_size - 1; 463} 464 465 466/** 467 * Determine the format for the texture sampler view. 468 */ 469static enum pipe_format 470get_sampler_view_format(struct st_context *st, 471 const struct st_texture_object *stObj, 472 bool srgb_skip_decode) 473{ 474 enum pipe_format format; 475 476 GLenum baseFormat = _mesa_base_tex_image(&stObj->base)->_BaseFormat; 477 format = stObj->surface_based ? stObj->surface_format : stObj->pt->format; 478 479 if (baseFormat == GL_DEPTH_COMPONENT || 480 baseFormat == GL_DEPTH_STENCIL || 481 baseFormat == GL_STENCIL_INDEX) { 482 if (stObj->base.StencilSampling || baseFormat == GL_STENCIL_INDEX) 483 format = util_format_stencil_only(format); 484 485 return format; 486 } 487 488 /* If sRGB decoding is off, use the linear format */ 489 if (srgb_skip_decode) 490 format = util_format_linear(format); 491 492 /* Use R8_UNORM for video formats */ 493 switch (format) { 494 case PIPE_FORMAT_NV12: 495 case PIPE_FORMAT_IYUV: 496 format = PIPE_FORMAT_R8_UNORM; 497 break; 498 default: 499 break; 500 } 501 return format; 502} 503 504 505static struct pipe_sampler_view * 506st_create_texture_sampler_view_from_stobj(struct st_context *st, 507 struct st_texture_object *stObj, 508 enum pipe_format format, 509 bool glsl130_or_later) 510{ 511 /* There is no need to clear this structure (consider CPU overhead). */ 512 struct pipe_sampler_view templ; 513 unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl130_or_later); 514 515 templ.format = format; 516 517 if (stObj->level_override) { 518 templ.u.tex.first_level = templ.u.tex.last_level = stObj->level_override; 519 } else { 520 templ.u.tex.first_level = stObj->base.MinLevel + stObj->base.BaseLevel; 521 templ.u.tex.last_level = last_level(stObj); 522 } 523 if (stObj->layer_override) { 524 templ.u.tex.first_layer = templ.u.tex.last_layer = stObj->layer_override; 525 } else { 526 templ.u.tex.first_layer = stObj->base.MinLayer; 527 templ.u.tex.last_layer = last_layer(stObj); 528 } 529 assert(templ.u.tex.first_layer <= templ.u.tex.last_layer); 530 assert(templ.u.tex.first_level <= templ.u.tex.last_level); 531 templ.target = gl_target_to_pipe(stObj->base.Target); 532 533 templ.swizzle_r = GET_SWZ(swizzle, 0); 534 templ.swizzle_g = GET_SWZ(swizzle, 1); 535 templ.swizzle_b = GET_SWZ(swizzle, 2); 536 templ.swizzle_a = GET_SWZ(swizzle, 3); 537 538 return st->pipe->create_sampler_view(st->pipe, stObj->pt, &templ); 539} 540 541 542struct pipe_sampler_view * 543st_get_texture_sampler_view_from_stobj(struct st_context *st, 544 struct st_texture_object *stObj, 545 const struct gl_sampler_object *samp, 546 bool glsl130_or_later, 547 bool ignore_srgb_decode) 548{ 549 const struct st_sampler_view *sv; 550 bool srgb_skip_decode = false; 551 552 if (!ignore_srgb_decode && samp->sRGBDecode == GL_SKIP_DECODE_EXT) 553 srgb_skip_decode = true; 554 555 sv = st_texture_get_current_sampler_view(st, stObj); 556 557 if (sv && 558 sv->glsl130_or_later == glsl130_or_later && 559 sv->srgb_skip_decode == srgb_skip_decode) { 560 /* Debug check: make sure that the sampler view's parameters are 561 * what they're supposed to be. 562 */ 563 struct pipe_sampler_view *view = sv->view; 564 assert(stObj->pt == view->texture); 565 assert(!check_sampler_swizzle(st, stObj, view, glsl130_or_later)); 566 assert(get_sampler_view_format(st, stObj, srgb_skip_decode) == view->format); 567 assert(gl_target_to_pipe(stObj->base.Target) == view->target); 568 assert(stObj->level_override || 569 stObj->base.MinLevel + stObj->base.BaseLevel == view->u.tex.first_level); 570 assert(stObj->level_override || last_level(stObj) == view->u.tex.last_level); 571 assert(stObj->layer_override || stObj->base.MinLayer == view->u.tex.first_layer); 572 assert(stObj->layer_override || last_layer(stObj) == view->u.tex.last_layer); 573 assert(!stObj->layer_override || 574 (stObj->layer_override == view->u.tex.first_layer && 575 stObj->layer_override == view->u.tex.last_layer)); 576 return view; 577 } 578 579 /* create new sampler view */ 580 enum pipe_format format = get_sampler_view_format(st, stObj, 581 srgb_skip_decode); 582 struct pipe_sampler_view *view = 583 st_create_texture_sampler_view_from_stobj(st, stObj, format, 584 glsl130_or_later); 585 586 view = st_texture_set_sampler_view(st, stObj, view, 587 glsl130_or_later, srgb_skip_decode); 588 589 return view; 590} 591 592 593struct pipe_sampler_view * 594st_get_buffer_sampler_view_from_stobj(struct st_context *st, 595 struct st_texture_object *stObj) 596{ 597 const struct st_sampler_view *sv; 598 struct st_buffer_object *stBuf = 599 st_buffer_object(stObj->base.BufferObject); 600 601 if (!stBuf || !stBuf->buffer) 602 return NULL; 603 604 sv = st_texture_get_current_sampler_view(st, stObj); 605 606 struct pipe_resource *buf = stBuf->buffer; 607 608 if (sv) { 609 struct pipe_sampler_view *view = sv->view; 610 611 if (view->texture == buf) { 612 /* Debug check: make sure that the sampler view's parameters are 613 * what they're supposed to be. 614 */ 615 assert(st_mesa_format_to_pipe_format(st, 616 stObj->base._BufferObjectFormat) 617 == view->format); 618 assert(view->target == PIPE_BUFFER); 619 unsigned base = stObj->base.BufferOffset; 620 MAYBE_UNUSED unsigned size = MIN2(buf->width0 - base, 621 (unsigned) stObj->base.BufferSize); 622 assert(view->u.buf.offset == base); 623 assert(view->u.buf.size == size); 624 return view; 625 } 626 } 627 628 unsigned base = stObj->base.BufferOffset; 629 630 if (base >= buf->width0) 631 return NULL; 632 633 unsigned size = buf->width0 - base; 634 size = MIN2(size, (unsigned)stObj->base.BufferSize); 635 if (!size) 636 return NULL; 637 638 /* Create a new sampler view. There is no need to clear the entire 639 * structure (consider CPU overhead). 640 */ 641 struct pipe_sampler_view templ; 642 643 templ.format = 644 st_mesa_format_to_pipe_format(st, stObj->base._BufferObjectFormat); 645 templ.target = PIPE_BUFFER; 646 templ.swizzle_r = PIPE_SWIZZLE_X; 647 templ.swizzle_g = PIPE_SWIZZLE_Y; 648 templ.swizzle_b = PIPE_SWIZZLE_Z; 649 templ.swizzle_a = PIPE_SWIZZLE_W; 650 templ.u.buf.offset = base; 651 templ.u.buf.size = size; 652 653 struct pipe_sampler_view *view = 654 st->pipe->create_sampler_view(st->pipe, buf, &templ); 655 656 view = st_texture_set_sampler_view(st, stObj, view, false, false); 657 658 return view; 659} 660