st_cb_clear.c revision e7689303a8e4790c38cc69ae7a197712f98e8f5b
1/************************************************************************** 2 * 3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. 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 TUNGSTEN GRAPHICS 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 <keith@tungstengraphics.com> 32 * Brian Paul 33 * Michel Dänzer 34 */ 35 36#include "main/glheader.h" 37#include "main/accum.h" 38#include "main/formats.h" 39#include "main/macros.h" 40#include "program/prog_instruction.h" 41#include "st_context.h" 42#include "st_atom.h" 43#include "st_cb_clear.h" 44#include "st_cb_fbo.h" 45#include "st_format.h" 46#include "st_program.h" 47 48#include "pipe/p_context.h" 49#include "pipe/p_shader_tokens.h" 50#include "pipe/p_state.h" 51#include "pipe/p_defines.h" 52#include "util/u_format.h" 53#include "util/u_inlines.h" 54#include "util/u_simple_shaders.h" 55#include "util/u_draw_quad.h" 56#include "util/u_upload_mgr.h" 57 58#include "cso_cache/cso_context.h" 59 60 61/** 62 * Do per-context initialization for glClear. 63 */ 64void 65st_init_clear(struct st_context *st) 66{ 67 struct pipe_screen *pscreen = st->pipe->screen; 68 69 memset(&st->clear, 0, sizeof(st->clear)); 70 71 st->clear.raster.gl_rasterization_rules = 1; 72 st->clear.raster.depth_clip = 1; 73 st->clear.enable_ds_separate = pscreen->get_param(pscreen, PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE); 74} 75 76 77/** 78 * Free per-context state for glClear. 79 */ 80void 81st_destroy_clear(struct st_context *st) 82{ 83 if (st->clear.fs) { 84 cso_delete_fragment_shader(st->cso_context, st->clear.fs); 85 st->clear.fs = NULL; 86 } 87 if (st->clear.vs) { 88 cso_delete_vertex_shader(st->cso_context, st->clear.vs); 89 st->clear.vs = NULL; 90 } 91} 92 93 94/** 95 * Helper function to set the fragment shaders. 96 */ 97static INLINE void 98set_fragment_shader(struct st_context *st) 99{ 100 if (!st->clear.fs) 101 st->clear.fs = util_make_fragment_passthrough_shader(st->pipe); 102 103 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs); 104} 105 106 107/** 108 * Helper function to set the vertex shader. 109 */ 110static INLINE void 111set_vertex_shader(struct st_context *st) 112{ 113 /* vertex shader - still required to provide the linkage between 114 * fragment shader input semantics and vertex_element/buffers. 115 */ 116 if (!st->clear.vs) 117 { 118 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION, 119 TGSI_SEMANTIC_COLOR }; 120 const uint semantic_indexes[] = { 0, 0 }; 121 st->clear.vs = util_make_vertex_passthrough_shader(st->pipe, 2, 122 semantic_names, 123 semantic_indexes); 124 } 125 126 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs); 127} 128 129 130/** 131 * Draw a screen-aligned quadrilateral. 132 * Coords are clip coords with y=0=bottom. 133 */ 134static void 135draw_quad(struct st_context *st, 136 float x0, float y0, float x1, float y1, GLfloat z, 137 const union pipe_color_union *color) 138{ 139 struct pipe_context *pipe = st->pipe; 140 struct pipe_resource *vbuf = NULL; 141 GLuint i, offset; 142 float (*vertices)[2][4]; /**< vertex pos + color */ 143 144 u_upload_alloc(st->uploader, 0, 4 * sizeof(vertices[0]), &offset, &vbuf, 145 (void**)&vertices); 146 if (!vbuf) { 147 return; 148 } 149 150 /* positions */ 151 vertices[0][0][0] = x0; 152 vertices[0][0][1] = y0; 153 154 vertices[1][0][0] = x1; 155 vertices[1][0][1] = y0; 156 157 vertices[2][0][0] = x1; 158 vertices[2][0][1] = y1; 159 160 vertices[3][0][0] = x0; 161 vertices[3][0][1] = y1; 162 163 /* same for all verts: */ 164 for (i = 0; i < 4; i++) { 165 vertices[i][0][2] = z; 166 vertices[i][0][3] = 1.0; 167 vertices[i][1][0] = color->f[0]; 168 vertices[i][1][1] = color->f[1]; 169 vertices[i][1][2] = color->f[2]; 170 vertices[i][1][3] = color->f[3]; 171 } 172 173 u_upload_unmap(st->uploader); 174 175 /* draw */ 176 util_draw_vertex_buffer(pipe, 177 st->cso_context, 178 vbuf, offset, 179 PIPE_PRIM_TRIANGLE_FAN, 180 4, /* verts */ 181 2); /* attribs/vert */ 182 183 pipe_resource_reference(&vbuf, NULL); 184} 185 186 187 188/** 189 * Do glClear by drawing a quadrilateral. 190 * The vertices of the quad will be computed from the 191 * ctx->DrawBuffer->_X/Ymin/max fields. 192 */ 193static void 194clear_with_quad(struct gl_context *ctx, 195 GLboolean color, GLboolean depth, GLboolean stencil) 196{ 197 struct st_context *st = st_context(ctx); 198 const struct gl_framebuffer *fb = ctx->DrawBuffer; 199 const GLfloat fb_width = (GLfloat) fb->Width; 200 const GLfloat fb_height = (GLfloat) fb->Height; 201 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin / fb_width * 2.0f - 1.0f; 202 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax / fb_width * 2.0f - 1.0f; 203 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin / fb_height * 2.0f - 1.0f; 204 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax / fb_height * 2.0f - 1.0f; 205 union pipe_color_union clearColor; 206 207 /* 208 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__, 209 color ? "color, " : "", 210 depth ? "depth, " : "", 211 stencil ? "stencil" : "", 212 x0, y0, 213 x1, y1); 214 */ 215 216 cso_save_blend(st->cso_context); 217 cso_save_stencil_ref(st->cso_context); 218 cso_save_depth_stencil_alpha(st->cso_context); 219 cso_save_rasterizer(st->cso_context); 220 cso_save_sample_mask(st->cso_context); 221 cso_save_viewport(st->cso_context); 222 cso_save_fragment_shader(st->cso_context); 223 cso_save_stream_outputs(st->cso_context); 224 cso_save_vertex_shader(st->cso_context); 225 cso_save_geometry_shader(st->cso_context); 226 cso_save_vertex_elements(st->cso_context); 227 cso_save_vertex_buffers(st->cso_context); 228 229 /* blend state: RGBA masking */ 230 { 231 struct pipe_blend_state blend; 232 memset(&blend, 0, sizeof(blend)); 233 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE; 234 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE; 235 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO; 236 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO; 237 if (color) { 238 if (ctx->Color.ColorMask[0][0]) 239 blend.rt[0].colormask |= PIPE_MASK_R; 240 if (ctx->Color.ColorMask[0][1]) 241 blend.rt[0].colormask |= PIPE_MASK_G; 242 if (ctx->Color.ColorMask[0][2]) 243 blend.rt[0].colormask |= PIPE_MASK_B; 244 if (ctx->Color.ColorMask[0][3]) 245 blend.rt[0].colormask |= PIPE_MASK_A; 246 if (st->ctx->Color.DitherFlag) 247 blend.dither = 1; 248 } 249 cso_set_blend(st->cso_context, &blend); 250 } 251 252 /* depth_stencil state: always pass/set to ref value */ 253 { 254 struct pipe_depth_stencil_alpha_state depth_stencil; 255 memset(&depth_stencil, 0, sizeof(depth_stencil)); 256 if (depth) { 257 depth_stencil.depth.enabled = 1; 258 depth_stencil.depth.writemask = 1; 259 depth_stencil.depth.func = PIPE_FUNC_ALWAYS; 260 } 261 262 if (stencil) { 263 struct pipe_stencil_ref stencil_ref; 264 memset(&stencil_ref, 0, sizeof(stencil_ref)); 265 depth_stencil.stencil[0].enabled = 1; 266 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS; 267 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE; 268 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE; 269 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE; 270 depth_stencil.stencil[0].valuemask = 0xff; 271 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff; 272 stencil_ref.ref_value[0] = ctx->Stencil.Clear; 273 cso_set_stencil_ref(st->cso_context, &stencil_ref); 274 } 275 276 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil); 277 } 278 279 cso_set_vertex_elements(st->cso_context, 2, st->velems_util_draw); 280 cso_set_stream_outputs(st->cso_context, 0, NULL, 0); 281 cso_set_sample_mask(st->cso_context, ~0); 282 cso_set_rasterizer(st->cso_context, &st->clear.raster); 283 284 /* viewport state: viewport matching window dims */ 285 { 286 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP); 287 struct pipe_viewport_state vp; 288 vp.scale[0] = 0.5f * fb_width; 289 vp.scale[1] = fb_height * (invert ? -0.5f : 0.5f); 290 vp.scale[2] = 1.0f; 291 vp.scale[3] = 1.0f; 292 vp.translate[0] = 0.5f * fb_width; 293 vp.translate[1] = 0.5f * fb_height; 294 vp.translate[2] = 0.0f; 295 vp.translate[3] = 0.0f; 296 cso_set_viewport(st->cso_context, &vp); 297 } 298 299 set_fragment_shader(st); 300 set_vertex_shader(st); 301 cso_set_geometry_shader_handle(st->cso_context, NULL); 302 303 if (ctx->DrawBuffer->_ColorDrawBuffers[0]) { 304 st_translate_color(ctx->Color.ClearColor.f, 305 ctx->DrawBuffer->_ColorDrawBuffers[0]->_BaseFormat, 306 clearColor.f); 307 } 308 309 /* draw quad matching scissor rect */ 310 draw_quad(st, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, &clearColor); 311 312 /* Restore pipe state */ 313 cso_restore_blend(st->cso_context); 314 cso_restore_stencil_ref(st->cso_context); 315 cso_restore_depth_stencil_alpha(st->cso_context); 316 cso_restore_rasterizer(st->cso_context); 317 cso_restore_sample_mask(st->cso_context); 318 cso_restore_viewport(st->cso_context); 319 cso_restore_fragment_shader(st->cso_context); 320 cso_restore_vertex_shader(st->cso_context); 321 cso_restore_geometry_shader(st->cso_context); 322 cso_restore_vertex_elements(st->cso_context); 323 cso_restore_vertex_buffers(st->cso_context); 324 cso_restore_stream_outputs(st->cso_context); 325} 326 327 328/** 329 * Determine if we need to clear the depth buffer by drawing a quad. 330 */ 331static INLINE GLboolean 332check_clear_color_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb) 333{ 334 if (ctx->Scissor.Enabled && 335 (ctx->Scissor.X != 0 || 336 ctx->Scissor.Y != 0 || 337 ctx->Scissor.Width < rb->Width || 338 ctx->Scissor.Height < rb->Height)) 339 return GL_TRUE; 340 341 if (!ctx->Color.ColorMask[0][0] || 342 !ctx->Color.ColorMask[0][1] || 343 !ctx->Color.ColorMask[0][2] || 344 !ctx->Color.ColorMask[0][3]) 345 return GL_TRUE; 346 347 return GL_FALSE; 348} 349 350 351/** 352 * Determine if we need to clear the combiend depth/stencil buffer by 353 * drawing a quad. 354 */ 355static INLINE GLboolean 356check_clear_depth_stencil_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb) 357{ 358 const GLuint stencilMax = 0xff; 359 GLboolean maskStencil 360 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax; 361 362 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0); 363 364 if (ctx->Scissor.Enabled && 365 (ctx->Scissor.X != 0 || 366 ctx->Scissor.Y != 0 || 367 ctx->Scissor.Width < rb->Width || 368 ctx->Scissor.Height < rb->Height)) 369 return GL_TRUE; 370 371 if (maskStencil) 372 return GL_TRUE; 373 374 return GL_FALSE; 375} 376 377 378/** 379 * Determine if we need to clear the depth buffer by drawing a quad. 380 */ 381static INLINE GLboolean 382check_clear_depth_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb, 383 boolean ds_separate) 384{ 385 const struct st_renderbuffer *strb = st_renderbuffer(rb); 386 const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format); 387 388 if (ctx->Scissor.Enabled && 389 (ctx->Scissor.X != 0 || 390 ctx->Scissor.Y != 0 || 391 ctx->Scissor.Width < rb->Width || 392 ctx->Scissor.Height < rb->Height)) 393 return GL_TRUE; 394 395 if (!ds_separate && isDS && ctx->DrawBuffer->Visual.stencilBits > 0) 396 return GL_TRUE; 397 398 return GL_FALSE; 399} 400 401 402/** 403 * Determine if we need to clear the stencil buffer by drawing a quad. 404 */ 405static INLINE GLboolean 406check_clear_stencil_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb, 407 boolean ds_separate) 408{ 409 const struct st_renderbuffer *strb = st_renderbuffer(rb); 410 const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format); 411 const GLuint stencilMax = 0xff; 412 const GLboolean maskStencil 413 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax; 414 415 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0); 416 417 if (maskStencil) 418 return GL_TRUE; 419 420 if (ctx->Scissor.Enabled && 421 (ctx->Scissor.X != 0 || 422 ctx->Scissor.Y != 0 || 423 ctx->Scissor.Width < rb->Width || 424 ctx->Scissor.Height < rb->Height)) 425 return GL_TRUE; 426 427 /* This is correct, but it is necessary to look at the depth clear 428 * value held in the surface when it comes time to issue the clear, 429 * rather than taking depth and stencil clear values from the 430 * current state. 431 */ 432 if (!ds_separate && isDS && ctx->DrawBuffer->Visual.depthBits > 0) 433 return GL_TRUE; 434 435 return GL_FALSE; 436} 437 438 439/** 440 * Called via ctx->Driver.Clear() 441 */ 442static void 443st_Clear(struct gl_context *ctx, GLbitfield mask) 444{ 445 static const GLbitfield BUFFER_BITS_DS 446 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL); 447 struct st_context *st = st_context(ctx); 448 struct gl_renderbuffer *depthRb 449 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer; 450 struct gl_renderbuffer *stencilRb 451 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer; 452 GLbitfield quad_buffers = 0x0; 453 GLbitfield clear_buffers = 0x0; 454 GLuint i; 455 456 /* This makes sure the pipe has the latest scissor, etc values */ 457 st_validate_state( st ); 458 459 if (mask & BUFFER_BITS_COLOR) { 460 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) { 461 GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i]; 462 463 if (mask & (1 << b)) { 464 struct gl_renderbuffer *rb 465 = ctx->DrawBuffer->Attachment[b].Renderbuffer; 466 struct st_renderbuffer *strb = st_renderbuffer(rb); 467 468 if (!strb || !strb->surface) 469 continue; 470 471 if (check_clear_color_with_quad( ctx, rb )) 472 quad_buffers |= PIPE_CLEAR_COLOR; 473 else 474 clear_buffers |= PIPE_CLEAR_COLOR; 475 } 476 } 477 } 478 479 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) { 480 /* clearing combined depth + stencil */ 481 struct st_renderbuffer *strb = st_renderbuffer(depthRb); 482 483 if (strb->surface) { 484 if (check_clear_depth_stencil_with_quad(ctx, depthRb)) 485 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL; 486 else 487 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL; 488 } 489 } 490 else { 491 /* separate depth/stencil clears */ 492 /* I don't think truly separate buffers are actually possible in gallium or hw? */ 493 if (mask & BUFFER_BIT_DEPTH) { 494 struct st_renderbuffer *strb = st_renderbuffer(depthRb); 495 496 if (strb->surface) { 497 if (check_clear_depth_with_quad(ctx, depthRb, 498 st->clear.enable_ds_separate)) 499 quad_buffers |= PIPE_CLEAR_DEPTH; 500 else 501 clear_buffers |= PIPE_CLEAR_DEPTH; 502 } 503 } 504 if (mask & BUFFER_BIT_STENCIL) { 505 struct st_renderbuffer *strb = st_renderbuffer(stencilRb); 506 507 if (strb->surface) { 508 if (check_clear_stencil_with_quad(ctx, stencilRb, 509 st->clear.enable_ds_separate)) 510 quad_buffers |= PIPE_CLEAR_STENCIL; 511 else 512 clear_buffers |= PIPE_CLEAR_STENCIL; 513 } 514 } 515 } 516 517 /* 518 * If we're going to use clear_with_quad() for any reason, use it for 519 * everything possible. 520 */ 521 if (quad_buffers) { 522 quad_buffers |= clear_buffers; 523 clear_with_quad(ctx, 524 quad_buffers & PIPE_CLEAR_COLOR, 525 quad_buffers & PIPE_CLEAR_DEPTH, 526 quad_buffers & PIPE_CLEAR_STENCIL); 527 } else if (clear_buffers) { 528 /* driver cannot know it can clear everything if the buffer 529 * is a combined depth/stencil buffer but this wasn't actually 530 * required from the visual. Hence fix this up to avoid potential 531 * read-modify-write in the driver. 532 */ 533 union pipe_color_union clearColor; 534 535 if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) && 536 ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) && 537 (depthRb == stencilRb) && 538 (ctx->DrawBuffer->Visual.depthBits == 0 || 539 ctx->DrawBuffer->Visual.stencilBits == 0)) 540 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL; 541 542 if (ctx->DrawBuffer->_ColorDrawBuffers[0]) { 543 st_translate_color(ctx->Color.ClearColor.f, 544 ctx->DrawBuffer->_ColorDrawBuffers[0]->_BaseFormat, 545 clearColor.f); 546 } 547 548 st->pipe->clear(st->pipe, clear_buffers, &clearColor, 549 ctx->Depth.Clear, ctx->Stencil.Clear); 550 } 551 if (mask & BUFFER_BIT_ACCUM) 552 _mesa_clear_accum_buffer(ctx); 553} 554 555 556void 557st_init_clear_functions(struct dd_function_table *functions) 558{ 559 functions->Clear = st_Clear; 560} 561