1/************************************************************************** 2 * 3 * Copyright 2007 VMware, Inc. 4 * All Rights Reserved. 5 * Copyright 2009 VMware, Inc. All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 **************************************************************************/ 28 29 /* 30 * Authors: 31 * Keith Whitwell <keithw@vmware.com> 32 * Brian Paul 33 * Michel Dänzer 34 */ 35 36#include "main/errors.h" 37#include "main/glheader.h" 38#include "main/accum.h" 39#include "main/formats.h" 40#include "main/framebuffer.h" 41#include "main/macros.h" 42#include "main/glformats.h" 43#include "program/prog_instruction.h" 44#include "st_context.h" 45#include "st_atom.h" 46#include "st_cb_bitmap.h" 47#include "st_cb_clear.h" 48#include "st_draw.h" 49#include "st_format.h" 50#include "st_nir.h" 51#include "st_program.h" 52#include "st_util.h" 53 54#include "pipe/p_context.h" 55#include "pipe/p_shader_tokens.h" 56#include "pipe/p_state.h" 57#include "pipe/p_defines.h" 58#include "util/format/u_format.h" 59#include "util/u_inlines.h" 60#include "util/u_simple_shaders.h" 61 62#include "cso_cache/cso_context.h" 63 64 65/** 66 * Do per-context initialization for glClear. 67 */ 68void 69st_init_clear(struct st_context *st) 70{ 71 memset(&st->clear, 0, sizeof(st->clear)); 72 73 st->clear.raster.half_pixel_center = 1; 74 st->clear.raster.bottom_edge_rule = 1; 75 st->clear.raster.depth_clip_near = 1; 76 st->clear.raster.depth_clip_far = 1; 77} 78 79 80/** 81 * Free per-context state for glClear. 82 */ 83void 84st_destroy_clear(struct st_context *st) 85{ 86 if (st->clear.fs) { 87 st->pipe->delete_fs_state(st->pipe, st->clear.fs); 88 st->clear.fs = NULL; 89 } 90 if (st->clear.vs) { 91 st->pipe->delete_vs_state(st->pipe, st->clear.vs); 92 st->clear.vs = NULL; 93 } 94 if (st->clear.vs_layered) { 95 st->pipe->delete_vs_state(st->pipe, st->clear.vs_layered); 96 st->clear.vs_layered = NULL; 97 } 98 if (st->clear.gs_layered) { 99 st->pipe->delete_gs_state(st->pipe, st->clear.gs_layered); 100 st->clear.gs_layered = NULL; 101 } 102} 103 104 105/** 106 * Helper function to set the clear color fragment shader. 107 */ 108static void 109set_clearcolor_fs(struct st_context *st, union pipe_color_union *color) 110{ 111 struct pipe_screen *pscreen = st->screen; 112 bool use_nir = PIPE_SHADER_IR_NIR == 113 pscreen->get_shader_param(pscreen, PIPE_SHADER_VERTEX, 114 PIPE_SHADER_CAP_PREFERRED_IR); 115 struct pipe_constant_buffer cb = { 116 .user_buffer = color->f, 117 .buffer_size = 4 * sizeof(float), 118 }; 119 st->pipe->set_constant_buffer(st->pipe, PIPE_SHADER_FRAGMENT, 0, 120 false, &cb); 121 122 if (!st->clear.fs) { 123 if (use_nir) { 124 st->clear.fs = st_nir_make_clearcolor_shader(st); 125 } else { 126 st->clear.fs = util_make_fs_clear_all_cbufs(st->pipe); 127 } 128 } 129 130 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs); 131} 132 133static void * 134make_nir_clear_vertex_shader(struct st_context *st, bool layered) 135{ 136 const char *shader_name = layered ? "layered clear VS" : "clear VS"; 137 unsigned inputs[] = { 138 VERT_ATTRIB_POS, 139 SYSTEM_VALUE_INSTANCE_ID, 140 }; 141 unsigned outputs[] = { 142 VARYING_SLOT_POS, 143 VARYING_SLOT_LAYER 144 }; 145 146 return st_nir_make_passthrough_shader(st, shader_name, MESA_SHADER_VERTEX, 147 layered ? 2 : 1, inputs, outputs, 148 NULL, (1 << 1)); 149} 150 151 152/** 153 * Helper function to set the vertex shader. 154 */ 155static inline void 156set_vertex_shader(struct st_context *st) 157{ 158 struct pipe_screen *pscreen = st->screen; 159 bool use_nir = PIPE_SHADER_IR_NIR == 160 pscreen->get_shader_param(pscreen, PIPE_SHADER_VERTEX, 161 PIPE_SHADER_CAP_PREFERRED_IR); 162 163 /* vertex shader - still required to provide the linkage between 164 * fragment shader input semantics and vertex_element/buffers. 165 */ 166 if (!st->clear.vs) 167 { 168 if (use_nir) { 169 st->clear.vs = make_nir_clear_vertex_shader(st, false); 170 } else { 171 const enum tgsi_semantic semantic_names[] = { 172 TGSI_SEMANTIC_POSITION, 173 }; 174 const uint semantic_indexes[] = { 0 }; 175 st->clear.vs = util_make_vertex_passthrough_shader(st->pipe, 1, 176 semantic_names, 177 semantic_indexes, 178 FALSE); 179 } 180 } 181 182 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs); 183 cso_set_geometry_shader_handle(st->cso_context, NULL); 184} 185 186 187static void 188set_vertex_shader_layered(struct st_context *st) 189{ 190 struct pipe_context *pipe = st->pipe; 191 struct pipe_screen *pscreen = st->screen; 192 bool use_nir = PIPE_SHADER_IR_NIR == 193 pscreen->get_shader_param(pscreen, PIPE_SHADER_VERTEX, 194 PIPE_SHADER_CAP_PREFERRED_IR); 195 196 if (!st->screen->get_param(st->screen, PIPE_CAP_VS_INSTANCEID)) { 197 assert(!"Got layered clear, but VS instancing is unsupported"); 198 set_vertex_shader(st); 199 return; 200 } 201 202 if (!st->clear.vs_layered) { 203 bool vs_layer = 204 st->screen->get_param(st->screen, PIPE_CAP_VS_LAYER_VIEWPORT); 205 if (vs_layer) { 206 st->clear.vs_layered = 207 use_nir ? make_nir_clear_vertex_shader(st, true) 208 : util_make_layered_clear_vertex_shader(pipe); 209 } else { 210 st->clear.vs_layered = util_make_layered_clear_helper_vertex_shader(pipe); 211 st->clear.gs_layered = util_make_layered_clear_geometry_shader(pipe); 212 } 213 } 214 215 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs_layered); 216 cso_set_geometry_shader_handle(st->cso_context, st->clear.gs_layered); 217} 218 219 220/** 221 * Do glClear by drawing a quadrilateral. 222 * The vertices of the quad will be computed from the 223 * ctx->DrawBuffer->_X/Ymin/max fields. 224 */ 225static void 226clear_with_quad(struct gl_context *ctx, unsigned clear_buffers) 227{ 228 struct st_context *st = st_context(ctx); 229 struct cso_context *cso = st->cso_context; 230 const struct gl_framebuffer *fb = ctx->DrawBuffer; 231 const GLfloat fb_width = (GLfloat) fb->Width; 232 const GLfloat fb_height = (GLfloat) fb->Height; 233 234 _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer); 235 236 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin / fb_width * 2.0f - 1.0f; 237 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax / fb_width * 2.0f - 1.0f; 238 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin / fb_height * 2.0f - 1.0f; 239 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax / fb_height * 2.0f - 1.0f; 240 unsigned num_layers = st->state.fb_num_layers; 241 242 /* 243 printf("%s %s%s%s %f,%f %f,%f\n", __func__, 244 color ? "color, " : "", 245 depth ? "depth, " : "", 246 stencil ? "stencil" : "", 247 x0, y0, 248 x1, y1); 249 */ 250 251 cso_save_state(cso, (CSO_BIT_BLEND | 252 CSO_BIT_STENCIL_REF | 253 CSO_BIT_DEPTH_STENCIL_ALPHA | 254 CSO_BIT_RASTERIZER | 255 CSO_BIT_SAMPLE_MASK | 256 CSO_BIT_MIN_SAMPLES | 257 CSO_BIT_VIEWPORT | 258 CSO_BIT_STREAM_OUTPUTS | 259 CSO_BIT_VERTEX_ELEMENTS | 260 (st->active_queries ? CSO_BIT_PAUSE_QUERIES : 0) | 261 CSO_BITS_ALL_SHADERS)); 262 263 /* blend state: RGBA masking */ 264 { 265 struct pipe_blend_state blend; 266 memset(&blend, 0, sizeof(blend)); 267 if (clear_buffers & PIPE_CLEAR_COLOR) { 268 int num_buffers = ctx->Extensions.EXT_draw_buffers2 ? 269 ctx->DrawBuffer->_NumColorDrawBuffers : 1; 270 int i; 271 272 blend.independent_blend_enable = num_buffers > 1; 273 blend.max_rt = num_buffers - 1; 274 275 for (i = 0; i < num_buffers; i++) { 276 if (!(clear_buffers & (PIPE_CLEAR_COLOR0 << i))) 277 continue; 278 279 blend.rt[i].colormask = GET_COLORMASK(ctx->Color.ColorMask, i); 280 } 281 282 if (ctx->Color.DitherFlag) 283 blend.dither = 1; 284 } 285 cso_set_blend(cso, &blend); 286 } 287 288 /* depth_stencil state: always pass/set to ref value */ 289 { 290 struct pipe_depth_stencil_alpha_state depth_stencil; 291 memset(&depth_stencil, 0, sizeof(depth_stencil)); 292 if (clear_buffers & PIPE_CLEAR_DEPTH) { 293 depth_stencil.depth_enabled = 1; 294 depth_stencil.depth_writemask = 1; 295 depth_stencil.depth_func = PIPE_FUNC_ALWAYS; 296 } 297 298 if (clear_buffers & PIPE_CLEAR_STENCIL) { 299 struct pipe_stencil_ref stencil_ref; 300 memset(&stencil_ref, 0, sizeof(stencil_ref)); 301 depth_stencil.stencil[0].enabled = 1; 302 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS; 303 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE; 304 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE; 305 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE; 306 depth_stencil.stencil[0].valuemask = 0xff; 307 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff; 308 stencil_ref.ref_value[0] = ctx->Stencil.Clear; 309 cso_set_stencil_ref(cso, stencil_ref); 310 } 311 312 cso_set_depth_stencil_alpha(cso, &depth_stencil); 313 } 314 315 st->util_velems.count = 1; 316 cso_set_vertex_elements(cso, &st->util_velems); 317 318 cso_set_stream_outputs(cso, 0, NULL, NULL); 319 cso_set_sample_mask(cso, ~0); 320 cso_set_min_samples(cso, 1); 321 st->clear.raster.multisample = st->state.fb_num_samples > 1; 322 cso_set_rasterizer(cso, &st->clear.raster); 323 324 /* viewport state: viewport matching window dims */ 325 cso_set_viewport_dims(st->cso_context, fb_width, fb_height, 326 _mesa_fb_orientation(fb) == Y_0_TOP); 327 328 /* Set constant buffer */ 329 set_clearcolor_fs(st, (union pipe_color_union*)&ctx->Color.ClearColor); 330 cso_set_tessctrl_shader_handle(cso, NULL); 331 cso_set_tesseval_shader_handle(cso, NULL); 332 333 if (num_layers > 1) 334 set_vertex_shader_layered(st); 335 else 336 set_vertex_shader(st); 337 338 /* draw quad matching scissor rect. 339 * 340 * Note: if we're only clearing depth/stencil we still setup vertices 341 * with color, but they'll be ignored. 342 * 343 * We can't translate the clear color to the colorbuffer format, 344 * because different colorbuffers may have different formats. 345 */ 346 if (!st_draw_quad(st, x0, y0, x1, y1, 347 ctx->Depth.Clear * 2.0f - 1.0f, 348 0.0f, 0.0f, 0.0f, 0.0f, 349 (const float *) &ctx->Color.ClearColor.f, 350 num_layers)) { 351 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClear"); 352 } 353 354 /* Restore pipe state */ 355 cso_restore_state(cso, 0); 356 ctx->Array.NewVertexElements = true; 357 st->dirty |= ST_NEW_VERTEX_ARRAYS | 358 ST_NEW_FS_CONSTANTS; 359} 360 361 362/** 363 * Return if the scissor must be enabled during the clear. 364 */ 365static inline GLboolean 366is_scissor_enabled(struct gl_context *ctx, struct gl_renderbuffer *rb) 367{ 368 const struct gl_scissor_rect *scissor = &ctx->Scissor.ScissorArray[0]; 369 370 return (ctx->Scissor.EnableFlags & 1) && 371 (scissor->X > 0 || 372 scissor->Y > 0 || 373 scissor->X + scissor->Width < (int)rb->Width || 374 scissor->Y + scissor->Height < (int)rb->Height); 375} 376 377/** 378 * Return if window rectangles must be enabled during the clear. 379 */ 380static inline bool 381is_window_rectangle_enabled(struct gl_context *ctx) 382{ 383 if (ctx->DrawBuffer == ctx->WinSysDrawBuffer) 384 return false; 385 return ctx->Scissor.NumWindowRects > 0 || 386 ctx->Scissor.WindowRectMode == GL_INCLUSIVE_EXT; 387} 388 389 390/** 391 * Return if all of the stencil bits are masked. 392 */ 393static inline GLboolean 394is_stencil_disabled(struct gl_context *ctx, struct gl_renderbuffer *rb) 395{ 396 const GLuint stencilMax = 0xff; 397 398 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0); 399 return (ctx->Stencil.WriteMask[0] & stencilMax) == 0; 400} 401 402 403/** 404 * Return if any of the stencil bits are masked. 405 */ 406static inline GLboolean 407is_stencil_masked(struct gl_context *ctx, struct gl_renderbuffer *rb) 408{ 409 const GLuint stencilMax = 0xff; 410 411 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0); 412 return (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax; 413} 414 415void 416st_Clear(struct gl_context *ctx, GLbitfield mask) 417{ 418 struct st_context *st = st_context(ctx); 419 struct gl_renderbuffer *depthRb 420 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer; 421 struct gl_renderbuffer *stencilRb 422 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer; 423 GLbitfield quad_buffers = 0x0; 424 GLbitfield clear_buffers = 0x0; 425 bool have_scissor_buffers = false; 426 GLuint i; 427 428 st_flush_bitmap_cache(st); 429 st_invalidate_readpix_cache(st); 430 431 /* This makes sure the pipe has the latest scissor, etc values */ 432 st_validate_state(st, ST_PIPELINE_CLEAR); 433 434 if (mask & BUFFER_BITS_COLOR) { 435 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) { 436 gl_buffer_index b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i]; 437 438 if (b != BUFFER_NONE && mask & (1 << b)) { 439 struct gl_renderbuffer *rb 440 = ctx->DrawBuffer->Attachment[b].Renderbuffer; 441 int colormask_index = ctx->Extensions.EXT_draw_buffers2 ? i : 0; 442 443 if (!rb || !rb->surface) 444 continue; 445 446 unsigned colormask = 447 GET_COLORMASK(ctx->Color.ColorMask, colormask_index); 448 449 if (!colormask) 450 continue; 451 452 unsigned surf_colormask = 453 util_format_colormask(util_format_description(rb->surface->format)); 454 455 bool scissor = is_scissor_enabled(ctx, rb); 456 if ((scissor && !st->can_scissor_clear) || 457 is_window_rectangle_enabled(ctx) || 458 ((colormask & surf_colormask) != surf_colormask)) 459 quad_buffers |= PIPE_CLEAR_COLOR0 << i; 460 else 461 clear_buffers |= PIPE_CLEAR_COLOR0 << i; 462 have_scissor_buffers |= scissor && st->can_scissor_clear; 463 } 464 } 465 } 466 467 if (mask & BUFFER_BIT_DEPTH) { 468 if (depthRb->surface && ctx->Depth.Mask) { 469 bool scissor = is_scissor_enabled(ctx, depthRb); 470 if ((scissor && !st->can_scissor_clear) || 471 is_window_rectangle_enabled(ctx)) 472 quad_buffers |= PIPE_CLEAR_DEPTH; 473 else 474 clear_buffers |= PIPE_CLEAR_DEPTH; 475 have_scissor_buffers |= scissor && st->can_scissor_clear; 476 } 477 } 478 if (mask & BUFFER_BIT_STENCIL) { 479 if (stencilRb->surface && !is_stencil_disabled(ctx, stencilRb)) { 480 bool scissor = is_scissor_enabled(ctx, stencilRb); 481 if ((scissor && !st->can_scissor_clear) || 482 is_window_rectangle_enabled(ctx) || 483 is_stencil_masked(ctx, stencilRb)) 484 quad_buffers |= PIPE_CLEAR_STENCIL; 485 else 486 clear_buffers |= PIPE_CLEAR_STENCIL; 487 have_scissor_buffers |= scissor && st->can_scissor_clear; 488 } 489 } 490 491 /* Always clear depth and stencil together. 492 * This can only happen when the stencil writemask is not a full mask. 493 */ 494 if (quad_buffers & PIPE_CLEAR_DEPTHSTENCIL && 495 clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) { 496 quad_buffers |= clear_buffers & PIPE_CLEAR_DEPTHSTENCIL; 497 clear_buffers &= ~PIPE_CLEAR_DEPTHSTENCIL; 498 } 499 500 /* Only use quad-based clearing for the renderbuffers which cannot 501 * use pipe->clear. We want to always use pipe->clear for the other 502 * renderbuffers, because it's likely to be faster. 503 */ 504 if (clear_buffers) { 505 const struct gl_scissor_rect *scissor = &ctx->Scissor.ScissorArray[0]; 506 struct pipe_scissor_state scissor_state = { 507 .minx = MAX2(scissor->X, 0), 508 .miny = MAX2(scissor->Y, 0), 509 .maxx = MAX2(scissor->X + scissor->Width, 0), 510 .maxy = MAX2(scissor->Y + scissor->Height, 0), 511 512 }; 513 514 /* Now invert Y if needed. 515 * Gallium drivers use the convention Y=0=top for surfaces. 516 */ 517 if (st->state.fb_orientation == Y_0_TOP) { 518 const struct gl_framebuffer *fb = ctx->DrawBuffer; 519 /* use intermediate variables to avoid uint underflow */ 520 GLint miny, maxy; 521 miny = fb->Height - scissor_state.maxy; 522 maxy = fb->Height - scissor_state.miny; 523 scissor_state.miny = MAX2(miny, 0); 524 scissor_state.maxy = MAX2(maxy, 0); 525 } 526 if (have_scissor_buffers) { 527 const struct gl_framebuffer *fb = ctx->DrawBuffer; 528 scissor_state.maxx = MIN2(scissor_state.maxx, fb->Width); 529 scissor_state.maxy = MIN2(scissor_state.maxy, fb->Height); 530 if (scissor_state.minx >= scissor_state.maxx || 531 scissor_state.miny >= scissor_state.maxy) 532 return; 533 } 534 /* We can't translate the clear color to the colorbuffer format, 535 * because different colorbuffers may have different formats. 536 */ 537 st->pipe->clear(st->pipe, clear_buffers, have_scissor_buffers ? &scissor_state : NULL, 538 (union pipe_color_union*)&ctx->Color.ClearColor, 539 ctx->Depth.Clear, ctx->Stencil.Clear); 540 } 541 if (quad_buffers) { 542 clear_with_quad(ctx, quad_buffers); 543 } 544 if (mask & BUFFER_BIT_ACCUM) 545 _mesa_clear_accum_buffer(ctx); 546} 547 548