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/* Authors: 29 * Brian Paul 30 */ 31 32#include "util/u_inlines.h" 33#include "util/u_memory.h" 34 35#include "draw/draw_context.h" 36 37#include "lp_context.h" 38#include "lp_screen.h" 39#include "lp_state.h" 40#include "lp_debug.h" 41#include "frontend/sw_winsys.h" 42#include "lp_flush.h" 43 44 45static void * 46llvmpipe_create_sampler_state(struct pipe_context *pipe, 47 const struct pipe_sampler_state *sampler) 48{ 49 struct pipe_sampler_state *state = mem_dup(sampler, sizeof *sampler); 50 51 if (LP_PERF & PERF_NO_MIP_LINEAR) { 52 if (state->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR) 53 state->min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST; 54 } 55 56 if (LP_PERF & PERF_NO_MIPMAPS) 57 state->min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 58 59 if (LP_PERF & PERF_NO_LINEAR) { 60 state->mag_img_filter = PIPE_TEX_FILTER_NEAREST; 61 state->min_img_filter = PIPE_TEX_FILTER_NEAREST; 62 } 63 64 return state; 65} 66 67 68static void 69llvmpipe_bind_sampler_states(struct pipe_context *pipe, 70 enum pipe_shader_type shader, 71 unsigned start, 72 unsigned num, 73 void **samplers) 74{ 75 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe); 76 unsigned i; 77 78 assert(shader < PIPE_SHADER_TYPES); 79 assert(start + num <= ARRAY_SIZE(llvmpipe->samplers[shader])); 80 81 draw_flush(llvmpipe->draw); 82 83 /* set the new samplers */ 84 for (i = 0; i < num; i++) { 85 void *sampler = NULL; 86 87 if (samplers && samplers[i]) 88 sampler = samplers[i]; 89 llvmpipe->samplers[shader][start + i] = sampler; 90 } 91 92 /* find highest non-null samplers[] entry */ 93 { 94 unsigned j = MAX2(llvmpipe->num_samplers[shader], start + num); 95 while (j > 0 && llvmpipe->samplers[shader][j - 1] == NULL) 96 j--; 97 llvmpipe->num_samplers[shader] = j; 98 } 99 100 if (shader == PIPE_SHADER_VERTEX || 101 shader == PIPE_SHADER_GEOMETRY || 102 shader == PIPE_SHADER_TESS_CTRL || 103 shader == PIPE_SHADER_TESS_EVAL) { 104 draw_set_samplers(llvmpipe->draw, 105 shader, 106 llvmpipe->samplers[shader], 107 llvmpipe->num_samplers[shader]); 108 } 109 else if (shader == PIPE_SHADER_COMPUTE) { 110 llvmpipe->cs_dirty |= LP_CSNEW_SAMPLER; 111 } else { 112 llvmpipe->dirty |= LP_NEW_SAMPLER; 113 } 114} 115 116 117static void 118llvmpipe_set_sampler_views(struct pipe_context *pipe, 119 enum pipe_shader_type shader, 120 unsigned start, 121 unsigned num, 122 unsigned unbind_num_trailing_slots, 123 bool take_ownership, 124 struct pipe_sampler_view **views) 125{ 126 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe); 127 uint i; 128 129 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS); 130 131 assert(shader < PIPE_SHADER_TYPES); 132 assert(start + num <= ARRAY_SIZE(llvmpipe->sampler_views[shader])); 133 134 draw_flush(llvmpipe->draw); 135 136 /* set the new sampler views */ 137 for (i = 0; i < num; i++) { 138 struct pipe_sampler_view *view = NULL; 139 140 if (views && views[i]) 141 view = views[i]; 142 /* 143 * Warn if someone tries to set a view created in a different context 144 * (which is why we need the hack above in the first place). 145 * An assert would be better but st/mesa relies on it... 146 */ 147 if (view && view->context != pipe) { 148 debug_printf("Illegal setting of sampler_view %d created in another " 149 "context\n", i); 150 } 151 152 if (view) 153 llvmpipe_flush_resource(pipe, view->texture, 0, true, false, false, "sampler_view"); 154 155 if (take_ownership) { 156 pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][start + i], 157 NULL); 158 llvmpipe->sampler_views[shader][start + i] = view; 159 } else { 160 pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][start + i], 161 view); 162 } 163 } 164 165 for (; i < num + unbind_num_trailing_slots; i++) { 166 pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][start + i], 167 NULL); 168 } 169 170 /* find highest non-null sampler_views[] entry */ 171 { 172 unsigned j = MAX2(llvmpipe->num_sampler_views[shader], start + num); 173 while (j > 0 && llvmpipe->sampler_views[shader][j - 1] == NULL) 174 j--; 175 llvmpipe->num_sampler_views[shader] = j; 176 } 177 178 if (shader == PIPE_SHADER_VERTEX || 179 shader == PIPE_SHADER_GEOMETRY || 180 shader == PIPE_SHADER_TESS_CTRL || 181 shader == PIPE_SHADER_TESS_EVAL) { 182 draw_set_sampler_views(llvmpipe->draw, 183 shader, 184 llvmpipe->sampler_views[shader], 185 llvmpipe->num_sampler_views[shader]); 186 } 187 else if (shader == PIPE_SHADER_COMPUTE) { 188 llvmpipe->cs_dirty |= LP_CSNEW_SAMPLER_VIEW; 189 } 190 else if (shader == PIPE_SHADER_FRAGMENT) { 191 llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW; 192 lp_setup_set_fragment_sampler_views(llvmpipe->setup, 193 llvmpipe->num_sampler_views[PIPE_SHADER_FRAGMENT], 194 llvmpipe->sampler_views[PIPE_SHADER_FRAGMENT]); 195 } 196} 197 198 199static struct pipe_sampler_view * 200llvmpipe_create_sampler_view(struct pipe_context *pipe, 201 struct pipe_resource *texture, 202 const struct pipe_sampler_view *templ) 203{ 204 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view); 205 /* 206 * XXX: bind flags from OpenGL state tracker are notoriously unreliable. 207 * This looks unfixable, so fix the bind flags instead when it happens. 208 */ 209 if (!(texture->bind & PIPE_BIND_SAMPLER_VIEW)) { 210 debug_printf("Illegal sampler view creation without bind flag\n"); 211 texture->bind |= PIPE_BIND_SAMPLER_VIEW; 212 } 213 214 if (view) { 215 *view = *templ; 216 view->reference.count = 1; 217 view->texture = NULL; 218 pipe_resource_reference(&view->texture, texture); 219 view->context = pipe; 220 221#ifdef DEBUG 222 /* 223 * This is possibly too lenient, but the primary reason is just 224 * to catch gallium frontends which forget to initialize this, so 225 * it only catches clearly impossible view targets. 226 */ 227 if (view->target != texture->target) { 228 if (view->target == PIPE_TEXTURE_1D) 229 assert(texture->target == PIPE_TEXTURE_1D_ARRAY); 230 else if (view->target == PIPE_TEXTURE_1D_ARRAY) 231 assert(texture->target == PIPE_TEXTURE_1D); 232 else if (view->target == PIPE_TEXTURE_2D) 233 assert(texture->target == PIPE_TEXTURE_2D_ARRAY || 234 texture->target == PIPE_TEXTURE_CUBE || 235 texture->target == PIPE_TEXTURE_CUBE_ARRAY); 236 else if (view->target == PIPE_TEXTURE_2D_ARRAY) 237 assert(texture->target == PIPE_TEXTURE_2D || 238 texture->target == PIPE_TEXTURE_CUBE || 239 texture->target == PIPE_TEXTURE_CUBE_ARRAY); 240 else if (view->target == PIPE_TEXTURE_CUBE) 241 assert(texture->target == PIPE_TEXTURE_CUBE_ARRAY || 242 texture->target == PIPE_TEXTURE_2D_ARRAY); 243 else if (view->target == PIPE_TEXTURE_CUBE_ARRAY) 244 assert(texture->target == PIPE_TEXTURE_CUBE || 245 texture->target == PIPE_TEXTURE_2D_ARRAY); 246 else 247 assert(0); 248 } 249#endif 250 } 251 252 return view; 253} 254 255 256static void 257llvmpipe_sampler_view_destroy(struct pipe_context *pipe, 258 struct pipe_sampler_view *view) 259{ 260 pipe_resource_reference(&view->texture, NULL); 261 FREE(view); 262} 263 264 265static void 266llvmpipe_delete_sampler_state(struct pipe_context *pipe, 267 void *sampler) 268{ 269 FREE( sampler ); 270} 271 272 273static void 274prepare_shader_sampling( 275 struct llvmpipe_context *lp, 276 unsigned num, 277 struct pipe_sampler_view **views, 278 enum pipe_shader_type shader_type) 279{ 280 281 unsigned i; 282 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS]; 283 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS]; 284 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS]; 285 const void *addr; 286 287 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS); 288 if (!num) 289 return; 290 291 for (i = 0; i < num; i++) { 292 struct pipe_sampler_view *view = i < num ? views[i] : NULL; 293 294 if (view) { 295 struct pipe_resource *tex = view->texture; 296 struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex); 297 unsigned width0 = tex->width0; 298 unsigned num_layers = tex->depth0; 299 unsigned first_level = 0; 300 unsigned last_level = 0; 301 unsigned sample_stride = 0; 302 unsigned num_samples = tex->nr_samples; 303 304 if (!lp_tex->dt) { 305 /* regular texture - setup array of mipmap level offsets */ 306 struct pipe_resource *res = view->texture; 307 int j; 308 309 if (llvmpipe_resource_is_texture(res)) { 310 first_level = view->u.tex.first_level; 311 last_level = view->u.tex.last_level; 312 assert(first_level <= last_level); 313 assert(last_level <= res->last_level); 314 addr = lp_tex->tex_data; 315 316 sample_stride = lp_tex->sample_stride; 317 318 for (j = first_level; j <= last_level; j++) { 319 mip_offsets[j] = lp_tex->mip_offsets[j]; 320 row_stride[j] = lp_tex->row_stride[j]; 321 img_stride[j] = lp_tex->img_stride[j]; 322 } 323 if (tex->target == PIPE_TEXTURE_1D_ARRAY || 324 tex->target == PIPE_TEXTURE_2D_ARRAY || 325 tex->target == PIPE_TEXTURE_CUBE || 326 tex->target == PIPE_TEXTURE_CUBE_ARRAY) { 327 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1; 328 for (j = first_level; j <= last_level; j++) { 329 mip_offsets[j] += view->u.tex.first_layer * 330 lp_tex->img_stride[j]; 331 } 332 if (view->target == PIPE_TEXTURE_CUBE || 333 view->target == PIPE_TEXTURE_CUBE_ARRAY) { 334 assert(num_layers % 6 == 0); 335 } 336 assert(view->u.tex.first_layer <= view->u.tex.last_layer); 337 assert(view->u.tex.last_layer < res->array_size); 338 } 339 } 340 else { 341 unsigned view_blocksize = util_format_get_blocksize(view->format); 342 addr = lp_tex->data; 343 /* probably don't really need to fill that out */ 344 mip_offsets[0] = 0; 345 row_stride[0] = 0; 346 img_stride[0] = 0; 347 348 /* everything specified in number of elements here. */ 349 width0 = view->u.buf.size / view_blocksize; 350 addr = (uint8_t *)addr + view->u.buf.offset; 351 assert(view->u.buf.offset + view->u.buf.size <= res->width0); 352 } 353 } 354 else { 355 /* display target texture/surface */ 356 addr = llvmpipe_resource_map(tex, 0, 0, LP_TEX_USAGE_READ); 357 row_stride[0] = lp_tex->row_stride[0]; 358 img_stride[0] = lp_tex->img_stride[0]; 359 mip_offsets[0] = 0; 360 assert(addr); 361 } 362 draw_set_mapped_texture(lp->draw, 363 shader_type, 364 i, 365 width0, tex->height0, num_layers, 366 first_level, last_level, 367 num_samples, sample_stride, 368 addr, 369 row_stride, img_stride, mip_offsets); 370 } 371 } 372} 373 374 375/** 376 * Called whenever we're about to draw (no dirty flag, FIXME?). 377 */ 378void 379llvmpipe_prepare_vertex_sampling(struct llvmpipe_context *lp, 380 unsigned num, 381 struct pipe_sampler_view **views) 382{ 383 prepare_shader_sampling(lp, num, views, PIPE_SHADER_VERTEX); 384} 385 386 387/** 388 * Called whenever we're about to draw (no dirty flag, FIXME?). 389 */ 390void 391llvmpipe_prepare_geometry_sampling(struct llvmpipe_context *lp, 392 unsigned num, 393 struct pipe_sampler_view **views) 394{ 395 prepare_shader_sampling(lp, num, views, PIPE_SHADER_GEOMETRY); 396} 397 398/** 399 * Called whenever we're about to draw (no dirty flag, FIXME?). 400 */ 401void 402llvmpipe_prepare_tess_ctrl_sampling(struct llvmpipe_context *lp, 403 unsigned num, 404 struct pipe_sampler_view **views) 405{ 406 prepare_shader_sampling(lp, num, views, PIPE_SHADER_TESS_CTRL); 407} 408 409/** 410 * Called whenever we're about to draw (no dirty flag, FIXME?). 411 */ 412void 413llvmpipe_prepare_tess_eval_sampling(struct llvmpipe_context *lp, 414 unsigned num, 415 struct pipe_sampler_view **views) 416{ 417 prepare_shader_sampling(lp, num, views, PIPE_SHADER_TESS_EVAL); 418} 419 420void 421llvmpipe_cleanup_stage_sampling(struct llvmpipe_context *ctx, 422 enum pipe_shader_type stage) 423{ 424 unsigned num, i; 425 struct pipe_sampler_view **views; 426 assert(ctx); 427 assert(stage < ARRAY_SIZE(ctx->num_sampler_views)); 428 assert(stage < ARRAY_SIZE(ctx->sampler_views)); 429 430 num = ctx->num_sampler_views[stage]; 431 views = ctx->sampler_views[stage]; 432 433 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS); 434 435 for (i = 0; i < num; i++) { 436 struct pipe_sampler_view *view = views[i]; 437 if (view) { 438 struct pipe_resource *tex = view->texture; 439 if (tex) 440 llvmpipe_resource_unmap(tex, 0, 0); 441 } 442 } 443} 444 445static void 446prepare_shader_images( 447 struct llvmpipe_context *lp, 448 unsigned num, 449 struct pipe_image_view *views, 450 enum pipe_shader_type shader_type) 451{ 452 453 unsigned i; 454 uint32_t row_stride; 455 uint32_t img_stride; 456 uint32_t sample_stride; 457 const void *addr; 458 459 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS); 460 if (!num) 461 return; 462 463 for (i = 0; i < num; i++) { 464 struct pipe_image_view *view = i < num ? &views[i] : NULL; 465 466 if (view) { 467 struct pipe_resource *img = view->resource; 468 struct llvmpipe_resource *lp_img = llvmpipe_resource(img); 469 if (!img) 470 continue; 471 472 unsigned width = img->width0; 473 unsigned height = img->height0; 474 unsigned num_layers = img->depth0; 475 unsigned num_samples = img->nr_samples; 476 477 const uint32_t bw = util_format_get_blockwidth(view->resource->format); 478 const uint32_t bh = util_format_get_blockheight(view->resource->format); 479 480 width = DIV_ROUND_UP(width, bw); 481 height = DIV_ROUND_UP(height, bh); 482 width = u_minify(width, view->u.tex.level); 483 height = u_minify(height, view->u.tex.level); 484 485 if (!lp_img->dt) { 486 /* regular texture - setup array of mipmap level offsets */ 487 struct pipe_resource *res = view->resource; 488 489 if (llvmpipe_resource_is_texture(res)) { 490 uint32_t mip_offset = lp_img->mip_offsets[view->u.tex.level]; 491 addr = lp_img->tex_data; 492 493 if (img->target == PIPE_TEXTURE_1D_ARRAY || 494 img->target == PIPE_TEXTURE_2D_ARRAY || 495 img->target == PIPE_TEXTURE_3D || 496 img->target == PIPE_TEXTURE_CUBE || 497 img->target == PIPE_TEXTURE_CUBE_ARRAY) { 498 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1; 499 assert(view->u.tex.first_layer <= view->u.tex.last_layer); 500 mip_offset += view->u.tex.first_layer * lp_img->img_stride[view->u.tex.level]; 501 } 502 503 row_stride = lp_img->row_stride[view->u.tex.level]; 504 img_stride = lp_img->img_stride[view->u.tex.level]; 505 sample_stride = lp_img->sample_stride; 506 addr = (uint8_t *)addr + mip_offset; 507 } 508 else { 509 unsigned view_blocksize = util_format_get_blocksize(view->format); 510 addr = lp_img->data; 511 /* probably don't really need to fill that out */ 512 row_stride = 0; 513 img_stride = 0; 514 sample_stride = 0; 515 516 /* everything specified in number of elements here. */ 517 width = view->u.buf.size / view_blocksize; 518 addr = (uint8_t *)addr + view->u.buf.offset; 519 assert(view->u.buf.offset + view->u.buf.size <= res->width0); 520 } 521 } 522 else { 523 /* display target texture/surface */ 524 addr = llvmpipe_resource_map(img, 0, 0, LP_TEX_USAGE_READ); 525 row_stride = lp_img->row_stride[0]; 526 img_stride = lp_img->img_stride[0]; 527 sample_stride = 0; 528 assert(addr); 529 } 530 draw_set_mapped_image(lp->draw, 531 shader_type, 532 i, 533 width, height, num_layers, 534 addr, 535 row_stride, img_stride, 536 num_samples, sample_stride); 537 } 538 } 539} 540 541 542/** 543 * Called whenever we're about to draw (no dirty flag, FIXME?). 544 */ 545void 546llvmpipe_prepare_vertex_images(struct llvmpipe_context *lp, 547 unsigned num, 548 struct pipe_image_view *views) 549{ 550 prepare_shader_images(lp, num, views, PIPE_SHADER_VERTEX); 551} 552 553 554/** 555 * Called whenever we're about to draw (no dirty flag, FIXME?). 556 */ 557void 558llvmpipe_prepare_geometry_images(struct llvmpipe_context *lp, 559 unsigned num, 560 struct pipe_image_view *views) 561{ 562 prepare_shader_images(lp, num, views, PIPE_SHADER_GEOMETRY); 563} 564 565/** 566 * Called whenever we're about to draw (no dirty flag, FIXME?). 567 */ 568void 569llvmpipe_prepare_tess_ctrl_images(struct llvmpipe_context *lp, 570 unsigned num, 571 struct pipe_image_view *views) 572{ 573 prepare_shader_images(lp, num, views, PIPE_SHADER_TESS_CTRL); 574} 575 576/** 577 * Called whenever we're about to draw (no dirty flag, FIXME?). 578 */ 579void 580llvmpipe_prepare_tess_eval_images(struct llvmpipe_context *lp, 581 unsigned num, 582 struct pipe_image_view *views) 583{ 584 prepare_shader_images(lp, num, views, PIPE_SHADER_TESS_EVAL); 585} 586 587void 588llvmpipe_cleanup_stage_images(struct llvmpipe_context *ctx, 589 enum pipe_shader_type stage) 590{ 591 unsigned num, i; 592 struct pipe_image_view *views; 593 assert(ctx); 594 assert(stage < ARRAY_SIZE(ctx->num_images)); 595 assert(stage < ARRAY_SIZE(ctx->images)); 596 597 num = ctx->num_images[stage]; 598 views = ctx->images[stage]; 599 600 assert(num <= LP_MAX_TGSI_SHADER_IMAGES); 601 602 for (i = 0; i < num; i++) { 603 struct pipe_image_view *view = &views[i]; 604 assert(view); 605 struct pipe_resource *img = view->resource; 606 if (img) 607 llvmpipe_resource_unmap(img, 0, 0); 608 } 609} 610 611void 612llvmpipe_init_sampler_funcs(struct llvmpipe_context *llvmpipe) 613{ 614 llvmpipe->pipe.create_sampler_state = llvmpipe_create_sampler_state; 615 616 llvmpipe->pipe.bind_sampler_states = llvmpipe_bind_sampler_states; 617 llvmpipe->pipe.create_sampler_view = llvmpipe_create_sampler_view; 618 llvmpipe->pipe.set_sampler_views = llvmpipe_set_sampler_views; 619 llvmpipe->pipe.sampler_view_destroy = llvmpipe_sampler_view_destroy; 620 llvmpipe->pipe.delete_sampler_state = llvmpipe_delete_sampler_state; 621} 622