i965: For fast color clears, only check the color of live channels.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_blorp_clear.cpp
1 /*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 extern "C" {
25 #include "main/teximage.h"
26 #include "main/blend.h"
27 #include "main/fbobject.h"
28 #include "main/renderbuffer.h"
29 }
30
31 #include "glsl/ralloc.h"
32
33 #include "intel_fbo.h"
34
35 #include "brw_blorp.h"
36 #include "brw_context.h"
37 #include "brw_eu.h"
38 #include "brw_state.h"
39
40 #define FILE_DEBUG_FLAG DEBUG_BLORP
41
42 struct brw_blorp_const_color_prog_key
43 {
44 bool use_simd16_replicated_data;
45 bool pad[3];
46 };
47
48 /**
49 * Parameters for a blorp operation where the fragment shader outputs a
50 * constant color. This is used for both fast color clears and color
51 * resolves.
52 */
53 class brw_blorp_const_color_params : public brw_blorp_params
54 {
55 public:
56 virtual uint32_t get_wm_prog(struct brw_context *brw,
57 brw_blorp_prog_data **prog_data) const;
58
59 brw_blorp_const_color_prog_key wm_prog_key;
60 };
61
62 class brw_blorp_clear_params : public brw_blorp_const_color_params
63 {
64 public:
65 brw_blorp_clear_params(struct brw_context *brw,
66 struct gl_framebuffer *fb,
67 struct gl_renderbuffer *rb,
68 GLubyte *color_mask,
69 bool partial_clear,
70 unsigned layer);
71 };
72
73
74 /**
75 * Parameters for a blorp operation that performs a "render target resolve".
76 * This is used to resolve pending fast clear pixels before a color buffer is
77 * used for texturing, ReadPixels, or scanout.
78 */
79 class brw_blorp_rt_resolve_params : public brw_blorp_const_color_params
80 {
81 public:
82 brw_blorp_rt_resolve_params(struct brw_context *brw,
83 struct intel_mipmap_tree *mt);
84 };
85
86
87 class brw_blorp_const_color_program
88 {
89 public:
90 brw_blorp_const_color_program(struct brw_context *brw,
91 const brw_blorp_const_color_prog_key *key);
92 ~brw_blorp_const_color_program();
93
94 const GLuint *compile(struct brw_context *brw, GLuint *program_size);
95
96 brw_blorp_prog_data prog_data;
97
98 private:
99 void alloc_regs();
100
101 void *mem_ctx;
102 struct brw_context *brw;
103 const brw_blorp_const_color_prog_key *key;
104 struct brw_compile func;
105
106 /* Thread dispatch header */
107 struct brw_reg R0;
108
109 /* Pixel X/Y coordinates (always in R1). */
110 struct brw_reg R1;
111
112 /* Register with push constants (a single vec4) */
113 struct brw_reg clear_rgba;
114
115 /* MRF used for render target writes */
116 GLuint base_mrf;
117 };
118
119 brw_blorp_const_color_program::brw_blorp_const_color_program(
120 struct brw_context *brw,
121 const brw_blorp_const_color_prog_key *key)
122 : mem_ctx(ralloc_context(NULL)),
123 brw(brw),
124 key(key),
125 R0(),
126 R1(),
127 clear_rgba(),
128 base_mrf(0)
129 {
130 prog_data.first_curbe_grf = 0;
131 prog_data.persample_msaa_dispatch = false;
132 brw_init_compile(brw, &func, mem_ctx);
133 }
134
135 brw_blorp_const_color_program::~brw_blorp_const_color_program()
136 {
137 ralloc_free(mem_ctx);
138 }
139
140
141 /**
142 * Determine if fast color clear supports the given clear color.
143 *
144 * Fast color clear can only clear to color values of 1.0 or 0.0. At the
145 * moment we only support floating point, unorm, and snorm buffers.
146 */
147 static bool
148 is_color_fast_clear_compatible(struct brw_context *brw,
149 mesa_format format,
150 const union gl_color_union *color)
151 {
152 if (_mesa_is_format_integer_color(format))
153 return false;
154
155 for (int i = 0; i < 4; i++) {
156 if (color->f[i] != 0.0 && color->f[i] != 1.0 &&
157 _mesa_format_has_color_component(format, i)) {
158 perf_debug("Clear color unsupported by fast color clear. "
159 "Falling back to slow clear.\n");
160 return false;
161 }
162 }
163 return true;
164 }
165
166
167 /**
168 * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
169 * SURFACE_STATE.
170 */
171 static uint32_t
172 compute_fast_clear_color_bits(const union gl_color_union *color)
173 {
174 uint32_t bits = 0;
175 for (int i = 0; i < 4; i++) {
176 if (color->f[i] != 0.0)
177 bits |= 1 << (GEN7_SURFACE_CLEAR_COLOR_SHIFT + (3 - i));
178 }
179 return bits;
180 }
181
182
183 brw_blorp_clear_params::brw_blorp_clear_params(struct brw_context *brw,
184 struct gl_framebuffer *fb,
185 struct gl_renderbuffer *rb,
186 GLubyte *color_mask,
187 bool partial_clear,
188 unsigned layer)
189 {
190 struct gl_context *ctx = &brw->ctx;
191 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
192
193 dst.set(brw, irb->mt, irb->mt_level, layer, true);
194
195 /* Override the surface format according to the context's sRGB rules. */
196 mesa_format format = _mesa_get_render_format(ctx, irb->mt->format);
197 dst.brw_surfaceformat = brw->render_target_format[format];
198
199 x0 = fb->_Xmin;
200 x1 = fb->_Xmax;
201 if (rb->Name != 0) {
202 y0 = fb->_Ymin;
203 y1 = fb->_Ymax;
204 } else {
205 y0 = rb->Height - fb->_Ymax;
206 y1 = rb->Height - fb->_Ymin;
207 }
208
209 float *push_consts = (float *)&wm_push_consts;
210
211 push_consts[0] = ctx->Color.ClearColor.f[0];
212 push_consts[1] = ctx->Color.ClearColor.f[1];
213 push_consts[2] = ctx->Color.ClearColor.f[2];
214 push_consts[3] = ctx->Color.ClearColor.f[3];
215
216 use_wm_prog = true;
217
218 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
219
220 wm_prog_key.use_simd16_replicated_data = true;
221
222 /* From the SNB PRM (Vol4_Part1):
223 *
224 * "Replicated data (Message Type = 111) is only supported when
225 * accessing tiled memory. Using this Message Type to access linear
226 * (untiled) memory is UNDEFINED."
227 */
228 if (irb->mt->region->tiling == I915_TILING_NONE)
229 wm_prog_key.use_simd16_replicated_data = false;
230
231 /* Constant color writes ignore everyting in blend and color calculator
232 * state. This is not documented.
233 */
234 for (int i = 0; i < 4; i++) {
235 if (_mesa_format_has_color_component(irb->mt->format, i) &&
236 !color_mask[i]) {
237 color_write_disable[i] = true;
238 wm_prog_key.use_simd16_replicated_data = false;
239 }
240 }
241
242 /* If we can do this as a fast color clear, do so.
243 *
244 * Note that the condition "!partial_clear" means we only try to do full
245 * buffer clears using fast color clear logic. This is necessary because
246 * the fast color clear alignment requirements mean that we typically have
247 * to clear a larger rectangle than (x0, y0) to (x1, y1). Restricting fast
248 * color clears to the full-buffer condition guarantees that the extra
249 * memory locations that get written to are outside the image boundary (and
250 * hence irrelevant). Note that the rectangle alignment requirements are
251 * never larger than the size of a tile, so there is no danger of
252 * overflowing beyond the memory belonging to the region.
253 */
254 if (irb->mt->fast_clear_state != INTEL_FAST_CLEAR_STATE_NO_MCS &&
255 !partial_clear && wm_prog_key.use_simd16_replicated_data &&
256 is_color_fast_clear_compatible(brw, format, &ctx->Color.ClearColor)) {
257 memset(push_consts, 0xff, 4*sizeof(float));
258 fast_clear_op = GEN7_FAST_CLEAR_OP_FAST_CLEAR;
259
260 /* Figure out what the clear rectangle needs to be aligned to, and how
261 * much it needs to be scaled down.
262 */
263 unsigned x_align, y_align, x_scaledown, y_scaledown;
264
265 if (irb->mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE) {
266 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
267 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
268 *
269 * Clear pass must have a clear rectangle that must follow
270 * alignment rules in terms of pixels and lines as shown in the
271 * table below. Further, the clear-rectangle height and width
272 * must be multiple of the following dimensions. If the height
273 * and width of the render target being cleared do not meet these
274 * requirements, an MCS buffer can be created such that it
275 * follows the requirement and covers the RT.
276 *
277 * The alignment size in the table that follows is related to the
278 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but
279 * with X alignment multiplied by 16 and Y alignment multiplied by 32.
280 */
281 intel_get_non_msrt_mcs_alignment(brw, irb->mt, &x_align, &y_align);
282 x_align *= 16;
283 y_align *= 32;
284
285 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
286 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
287 *
288 * In order to optimize the performance MCS buffer (when bound to
289 * 1X RT) clear similarly to MCS buffer clear for MSRT case,
290 * clear rect is required to be scaled by the following factors
291 * in the horizontal and vertical directions:
292 *
293 * The X and Y scale down factors in the table that follows are each
294 * equal to half the alignment value computed above.
295 */
296 x_scaledown = x_align / 2;
297 y_scaledown = y_align / 2;
298
299 /* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel
300 * Backend > MCS Buffer for Render Target(s) [DevIVB+] > Table "Color
301 * Clear of Non-MultiSampled Render Target Restrictions":
302 *
303 * Clear rectangle must be aligned to two times the number of
304 * pixels in the table shown below due to 16x16 hashing across the
305 * slice.
306 */
307 x_align *= 2;
308 y_align *= 2;
309 } else {
310 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
311 * Target(s)", beneath the "MSAA Compression" bullet (p326):
312 *
313 * Clear pass for this case requires that scaled down primitive
314 * is sent down with upper left co-ordinate to coincide with
315 * actual rectangle being cleared. For MSAA, clear rectangle’s
316 * height and width need to as show in the following table in
317 * terms of (width,height) of the RT.
318 *
319 * MSAA Width of Clear Rect Height of Clear Rect
320 * 4X Ceil(1/8*width) Ceil(1/2*height)
321 * 8X Ceil(1/2*width) Ceil(1/2*height)
322 *
323 * The text "with upper left co-ordinate to coincide with actual
324 * rectangle being cleared" is a little confusing--it seems to imply
325 * that to clear a rectangle from (x,y) to (x+w,y+h), one needs to
326 * feed the pipeline using the rectangle (x,y) to
327 * (x+Ceil(w/N),y+Ceil(h/2)), where N is either 2 or 8 depending on
328 * the number of samples. Experiments indicate that this is not
329 * quite correct; actually, what the hardware appears to do is to
330 * align whatever rectangle is sent down the pipeline to the nearest
331 * multiple of 2x2 blocks, and then scale it up by a factor of N
332 * horizontally and 2 vertically. So the resulting alignment is 4
333 * vertically and either 4 or 16 horizontally, and the scaledown
334 * factor is 2 vertically and either 2 or 8 horizontally.
335 */
336 switch (irb->mt->num_samples) {
337 case 4:
338 x_scaledown = 8;
339 break;
340 case 8:
341 x_scaledown = 2;
342 break;
343 default:
344 assert(!"Unexpected sample count for fast clear");
345 break;
346 }
347 y_scaledown = 2;
348 x_align = x_scaledown * 2;
349 y_align = y_scaledown * 2;
350 }
351
352 /* Do the alignment and scaledown. */
353 x0 = ROUND_DOWN_TO(x0, x_align) / x_scaledown;
354 y0 = ROUND_DOWN_TO(y0, y_align) / y_scaledown;
355 x1 = ALIGN(x1, x_align) / x_scaledown;
356 y1 = ALIGN(y1, y_align) / y_scaledown;
357 }
358 }
359
360
361 brw_blorp_rt_resolve_params::brw_blorp_rt_resolve_params(
362 struct brw_context *brw,
363 struct intel_mipmap_tree *mt)
364 {
365 dst.set(brw, mt, 0 /* level */, 0 /* layer */, true);
366
367 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
368 *
369 * A rectangle primitive must be scaled down by the following factors
370 * with respect to render target being resolved.
371 *
372 * The scaledown factors in the table that follows are related to the
373 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but with
374 * X and Y alignment each divided by 2.
375 */
376 unsigned x_align, y_align;
377 intel_get_non_msrt_mcs_alignment(brw, mt, &x_align, &y_align);
378 unsigned x_scaledown = x_align / 2;
379 unsigned y_scaledown = y_align / 2;
380 x0 = y0 = 0;
381 x1 = ALIGN(mt->logical_width0, x_scaledown) / x_scaledown;
382 y1 = ALIGN(mt->logical_height0, y_scaledown) / y_scaledown;
383
384 fast_clear_op = GEN7_FAST_CLEAR_OP_RESOLVE;
385
386 /* Note: there is no need to initialize push constants because it doesn't
387 * matter what data gets dispatched to the render target. However, we must
388 * ensure that the fragment shader delivers the data using the "replicated
389 * color" message.
390 */
391 use_wm_prog = true;
392 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
393 wm_prog_key.use_simd16_replicated_data = true;
394 }
395
396
397 uint32_t
398 brw_blorp_const_color_params::get_wm_prog(struct brw_context *brw,
399 brw_blorp_prog_data **prog_data)
400 const
401 {
402 uint32_t prog_offset = 0;
403 if (!brw_search_cache(&brw->cache, BRW_BLORP_CONST_COLOR_PROG,
404 &this->wm_prog_key, sizeof(this->wm_prog_key),
405 &prog_offset, prog_data)) {
406 brw_blorp_const_color_program prog(brw, &this->wm_prog_key);
407 GLuint program_size;
408 const GLuint *program = prog.compile(brw, &program_size);
409 brw_upload_cache(&brw->cache, BRW_BLORP_CONST_COLOR_PROG,
410 &this->wm_prog_key, sizeof(this->wm_prog_key),
411 program, program_size,
412 &prog.prog_data, sizeof(prog.prog_data),
413 &prog_offset, prog_data);
414 }
415 return prog_offset;
416 }
417
418 void
419 brw_blorp_const_color_program::alloc_regs()
420 {
421 int reg = 0;
422 this->R0 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
423 this->R1 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
424
425 prog_data.first_curbe_grf = reg;
426 clear_rgba = retype(brw_vec4_grf(reg++, 0), BRW_REGISTER_TYPE_F);
427 reg += BRW_BLORP_NUM_PUSH_CONST_REGS;
428
429 /* Make sure we didn't run out of registers */
430 assert(reg <= GEN7_MRF_HACK_START);
431
432 this->base_mrf = 2;
433 }
434
435 const GLuint *
436 brw_blorp_const_color_program::compile(struct brw_context *brw,
437 GLuint *program_size)
438 {
439 /* Set up prog_data */
440 memset(&prog_data, 0, sizeof(prog_data));
441 prog_data.persample_msaa_dispatch = false;
442
443 alloc_regs();
444
445 brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
446
447 struct brw_reg mrf_rt_write =
448 retype(vec16(brw_message_reg(base_mrf)), BRW_REGISTER_TYPE_F);
449
450 uint32_t mlen, msg_type;
451 if (key->use_simd16_replicated_data) {
452 /* The message payload is a single register with the low 4 floats/ints
453 * filled with the constant clear color.
454 */
455 brw_set_mask_control(&func, BRW_MASK_DISABLE);
456 brw_MOV(&func, vec4(brw_message_reg(base_mrf)), clear_rgba);
457 brw_set_mask_control(&func, BRW_MASK_ENABLE);
458
459 msg_type = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED;
460 mlen = 1;
461 } else {
462 for (int i = 0; i < 4; i++) {
463 /* The message payload is pairs of registers for 16 pixels each of r,
464 * g, b, and a.
465 */
466 brw_set_compression_control(&func, BRW_COMPRESSION_COMPRESSED);
467 brw_MOV(&func,
468 brw_message_reg(base_mrf + i * 2),
469 brw_vec1_grf(clear_rgba.nr, i));
470 brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
471 }
472
473 msg_type = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE;
474 mlen = 8;
475 }
476
477 /* Now write to the render target and terminate the thread */
478 brw_fb_WRITE(&func,
479 16 /* dispatch_width */,
480 base_mrf /* msg_reg_nr */,
481 mrf_rt_write /* src0 */,
482 msg_type,
483 BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX,
484 mlen,
485 0 /* response_length */,
486 true /* eot */,
487 false /* header present */);
488
489 if (unlikely(INTEL_DEBUG & DEBUG_BLORP)) {
490 fprintf(stderr, "Native code for BLORP clear:\n");
491 brw_dump_compile(&func, stderr, 0, func.next_insn_offset);
492 fprintf(stderr, "\n");
493 }
494 return brw_get_program(&func, program_size);
495 }
496
497
498 bool
499 do_single_blorp_clear(struct brw_context *brw, struct gl_framebuffer *fb,
500 struct gl_renderbuffer *rb, unsigned buf,
501 bool partial_clear, unsigned layer)
502 {
503 struct gl_context *ctx = &brw->ctx;
504 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
505
506 brw_blorp_clear_params params(brw, fb, rb, ctx->Color.ColorMask[buf],
507 partial_clear, layer);
508
509 bool is_fast_clear =
510 (params.fast_clear_op == GEN7_FAST_CLEAR_OP_FAST_CLEAR);
511 if (is_fast_clear) {
512 /* Record the clear color in the miptree so that it will be
513 * programmed in SURFACE_STATE by later rendering and resolve
514 * operations.
515 */
516 uint32_t new_color_value =
517 compute_fast_clear_color_bits(&ctx->Color.ClearColor);
518 if (irb->mt->fast_clear_color_value != new_color_value) {
519 irb->mt->fast_clear_color_value = new_color_value;
520 brw->state.dirty.brw |= BRW_NEW_SURFACES;
521 }
522
523 /* If the buffer is already in INTEL_FAST_CLEAR_STATE_CLEAR, the clear
524 * is redundant and can be skipped.
525 */
526 if (irb->mt->fast_clear_state == INTEL_FAST_CLEAR_STATE_CLEAR)
527 return true;
528
529 /* If the MCS buffer hasn't been allocated yet, we need to allocate
530 * it now.
531 */
532 if (!irb->mt->mcs_mt) {
533 if (!intel_miptree_alloc_non_msrt_mcs(brw, irb->mt)) {
534 /* MCS allocation failed--probably this will only happen in
535 * out-of-memory conditions. But in any case, try to recover
536 * by falling back to a non-blorp clear technique.
537 */
538 return false;
539 }
540 brw->state.dirty.brw |= BRW_NEW_SURFACES;
541 }
542 }
543
544 const char *clear_type;
545 if (is_fast_clear)
546 clear_type = "fast";
547 else if (params.wm_prog_key.use_simd16_replicated_data)
548 clear_type = "replicated";
549 else
550 clear_type = "slow";
551
552 DBG("%s (%s) to mt %p level %d layer %d\n", __FUNCTION__, clear_type,
553 irb->mt, irb->mt_level, irb->mt_layer);
554
555 brw_blorp_exec(brw, &params);
556
557 if (is_fast_clear) {
558 /* Now that the fast clear has occurred, put the buffer in
559 * INTEL_FAST_CLEAR_STATE_CLEAR so that we won't waste time doing
560 * redundant clears.
561 */
562 irb->mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_CLEAR;
563 }
564
565 return true;
566 }
567
568
569 extern "C" {
570 bool
571 brw_blorp_clear_color(struct brw_context *brw, struct gl_framebuffer *fb,
572 bool partial_clear)
573 {
574 for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
575 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
576 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
577
578 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
579 * the framebuffer can be complete with some attachments missing. In
580 * this case the _ColorDrawBuffers pointer will be NULL.
581 */
582 if (rb == NULL)
583 continue;
584
585 if (fb->MaxNumLayers > 0) {
586 unsigned layer_multiplier =
587 (irb->mt->msaa_layout == INTEL_MSAA_LAYOUT_UMS ||
588 irb->mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) ?
589 irb->mt->num_samples : 1;
590 unsigned num_layers =
591 irb->mt->level[irb->mt_level].depth / layer_multiplier;
592 for (unsigned layer = 0; layer < num_layers; layer++) {
593 if (!do_single_blorp_clear(brw, fb, rb, buf, partial_clear,
594 layer * layer_multiplier)) {
595 return false;
596 }
597 }
598 } else {
599 unsigned layer = irb->mt_layer;
600 if (!do_single_blorp_clear(brw, fb, rb, buf, partial_clear, layer))
601 return false;
602 }
603
604 irb->need_downsample = true;
605 }
606
607 return true;
608 }
609
610 void
611 brw_blorp_resolve_color(struct brw_context *brw, struct intel_mipmap_tree *mt)
612 {
613 DBG("%s to mt %p\n", __FUNCTION__, mt);
614
615 brw_blorp_rt_resolve_params params(brw, mt);
616 brw_blorp_exec(brw, &params);
617 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
618 }
619
620 } /* extern "C" */