df34c7240730f668fa06ef2fda091809d86ffbca
[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 const brw_blorp_const_color_prog_key *key;
103 struct brw_compile func;
104
105 /* Thread dispatch header */
106 struct brw_reg R0;
107
108 /* Pixel X/Y coordinates (always in R1). */
109 struct brw_reg R1;
110
111 /* Register with push constants (a single vec4) */
112 struct brw_reg clear_rgba;
113
114 /* MRF used for render target writes */
115 GLuint base_mrf;
116 };
117
118 brw_blorp_const_color_program::brw_blorp_const_color_program(
119 struct brw_context *brw,
120 const brw_blorp_const_color_prog_key *key)
121 : mem_ctx(ralloc_context(NULL)),
122 key(key),
123 R0(),
124 R1(),
125 clear_rgba(),
126 base_mrf(0)
127 {
128 prog_data.first_curbe_grf = 0;
129 prog_data.persample_msaa_dispatch = false;
130 brw_init_compile(brw, &func, mem_ctx);
131 }
132
133 brw_blorp_const_color_program::~brw_blorp_const_color_program()
134 {
135 ralloc_free(mem_ctx);
136 }
137
138
139 /**
140 * Determine if fast color clear supports the given clear color.
141 *
142 * Fast color clear can only clear to color values of 1.0 or 0.0. At the
143 * moment we only support floating point, unorm, and snorm buffers.
144 */
145 static bool
146 is_color_fast_clear_compatible(struct brw_context *brw,
147 mesa_format format,
148 const union gl_color_union *color)
149 {
150 if (_mesa_is_format_integer_color(format))
151 return false;
152
153 for (int i = 0; i < 4; i++) {
154 if (color->f[i] != 0.0 && color->f[i] != 1.0 &&
155 _mesa_format_has_color_component(format, i)) {
156 return false;
157 }
158 }
159 return true;
160 }
161
162
163 /**
164 * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
165 * SURFACE_STATE.
166 */
167 static uint32_t
168 compute_fast_clear_color_bits(const union gl_color_union *color)
169 {
170 uint32_t bits = 0;
171 for (int i = 0; i < 4; i++) {
172 if (color->f[i] != 0.0)
173 bits |= 1 << (GEN7_SURFACE_CLEAR_COLOR_SHIFT + (3 - i));
174 }
175 return bits;
176 }
177
178
179 brw_blorp_clear_params::brw_blorp_clear_params(struct brw_context *brw,
180 struct gl_framebuffer *fb,
181 struct gl_renderbuffer *rb,
182 GLubyte *color_mask,
183 bool partial_clear,
184 unsigned layer)
185 {
186 struct gl_context *ctx = &brw->ctx;
187 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
188
189 dst.set(brw, irb->mt, irb->mt_level, layer, true);
190
191 /* Override the surface format according to the context's sRGB rules. */
192 mesa_format format = _mesa_get_render_format(ctx, irb->mt->format);
193 dst.brw_surfaceformat = brw->render_target_format[format];
194
195 x0 = fb->_Xmin;
196 x1 = fb->_Xmax;
197 if (rb->Name != 0) {
198 y0 = fb->_Ymin;
199 y1 = fb->_Ymax;
200 } else {
201 y0 = rb->Height - fb->_Ymax;
202 y1 = rb->Height - fb->_Ymin;
203 }
204
205 float *push_consts = (float *)&wm_push_consts;
206
207 push_consts[0] = ctx->Color.ClearColor.f[0];
208 push_consts[1] = ctx->Color.ClearColor.f[1];
209 push_consts[2] = ctx->Color.ClearColor.f[2];
210 push_consts[3] = ctx->Color.ClearColor.f[3];
211
212 use_wm_prog = true;
213
214 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
215
216 wm_prog_key.use_simd16_replicated_data = true;
217
218 /* From the SNB PRM (Vol4_Part1):
219 *
220 * "Replicated data (Message Type = 111) is only supported when
221 * accessing tiled memory. Using this Message Type to access linear
222 * (untiled) memory is UNDEFINED."
223 */
224 if (irb->mt->tiling == I915_TILING_NONE)
225 wm_prog_key.use_simd16_replicated_data = false;
226
227 /* Constant color writes ignore everyting in blend and color calculator
228 * state. This is not documented.
229 */
230 for (int i = 0; i < 4; i++) {
231 if (_mesa_format_has_color_component(irb->mt->format, i) &&
232 !color_mask[i]) {
233 color_write_disable[i] = true;
234 wm_prog_key.use_simd16_replicated_data = false;
235 }
236 }
237
238 /* If we can do this as a fast color clear, do so.
239 *
240 * Note that the condition "!partial_clear" means we only try to do full
241 * buffer clears using fast color clear logic. This is necessary because
242 * the fast color clear alignment requirements mean that we typically have
243 * to clear a larger rectangle than (x0, y0) to (x1, y1). Restricting fast
244 * color clears to the full-buffer condition guarantees that the extra
245 * memory locations that get written to are outside the image boundary (and
246 * hence irrelevant). Note that the rectangle alignment requirements are
247 * never larger than the size of a tile, so there is no danger of
248 * overflowing beyond the memory belonging to the region.
249 */
250 if (irb->mt->fast_clear_state != INTEL_FAST_CLEAR_STATE_NO_MCS &&
251 !partial_clear && wm_prog_key.use_simd16_replicated_data &&
252 is_color_fast_clear_compatible(brw, format, &ctx->Color.ClearColor)) {
253 memset(push_consts, 0xff, 4*sizeof(float));
254 fast_clear_op = GEN7_FAST_CLEAR_OP_FAST_CLEAR;
255
256 /* Figure out what the clear rectangle needs to be aligned to, and how
257 * much it needs to be scaled down.
258 */
259 unsigned x_align, y_align, x_scaledown, y_scaledown;
260
261 if (irb->mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE) {
262 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
263 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
264 *
265 * Clear pass must have a clear rectangle that must follow
266 * alignment rules in terms of pixels and lines as shown in the
267 * table below. Further, the clear-rectangle height and width
268 * must be multiple of the following dimensions. If the height
269 * and width of the render target being cleared do not meet these
270 * requirements, an MCS buffer can be created such that it
271 * follows the requirement and covers the RT.
272 *
273 * The alignment size in the table that follows is related to the
274 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but
275 * with X alignment multiplied by 16 and Y alignment multiplied by 32.
276 */
277 intel_get_non_msrt_mcs_alignment(brw, irb->mt, &x_align, &y_align);
278 x_align *= 16;
279 y_align *= 32;
280
281 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
282 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
283 *
284 * In order to optimize the performance MCS buffer (when bound to
285 * 1X RT) clear similarly to MCS buffer clear for MSRT case,
286 * clear rect is required to be scaled by the following factors
287 * in the horizontal and vertical directions:
288 *
289 * The X and Y scale down factors in the table that follows are each
290 * equal to half the alignment value computed above.
291 */
292 x_scaledown = x_align / 2;
293 y_scaledown = y_align / 2;
294
295 /* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel
296 * Backend > MCS Buffer for Render Target(s) [DevIVB+] > Table "Color
297 * Clear of Non-MultiSampled Render Target Restrictions":
298 *
299 * Clear rectangle must be aligned to two times the number of
300 * pixels in the table shown below due to 16x16 hashing across the
301 * slice.
302 */
303 x_align *= 2;
304 y_align *= 2;
305 } else {
306 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
307 * Target(s)", beneath the "MSAA Compression" bullet (p326):
308 *
309 * Clear pass for this case requires that scaled down primitive
310 * is sent down with upper left co-ordinate to coincide with
311 * actual rectangle being cleared. For MSAA, clear rectangle’s
312 * height and width need to as show in the following table in
313 * terms of (width,height) of the RT.
314 *
315 * MSAA Width of Clear Rect Height of Clear Rect
316 * 4X Ceil(1/8*width) Ceil(1/2*height)
317 * 8X Ceil(1/2*width) Ceil(1/2*height)
318 *
319 * The text "with upper left co-ordinate to coincide with actual
320 * rectangle being cleared" is a little confusing--it seems to imply
321 * that to clear a rectangle from (x,y) to (x+w,y+h), one needs to
322 * feed the pipeline using the rectangle (x,y) to
323 * (x+Ceil(w/N),y+Ceil(h/2)), where N is either 2 or 8 depending on
324 * the number of samples. Experiments indicate that this is not
325 * quite correct; actually, what the hardware appears to do is to
326 * align whatever rectangle is sent down the pipeline to the nearest
327 * multiple of 2x2 blocks, and then scale it up by a factor of N
328 * horizontally and 2 vertically. So the resulting alignment is 4
329 * vertically and either 4 or 16 horizontally, and the scaledown
330 * factor is 2 vertically and either 2 or 8 horizontally.
331 */
332 switch (irb->mt->num_samples) {
333 case 4:
334 x_scaledown = 8;
335 break;
336 case 8:
337 x_scaledown = 2;
338 break;
339 default:
340 unreachable("Unexpected sample count for fast clear");
341 }
342 y_scaledown = 2;
343 x_align = x_scaledown * 2;
344 y_align = y_scaledown * 2;
345 }
346
347 /* Do the alignment and scaledown. */
348 x0 = ROUND_DOWN_TO(x0, x_align) / x_scaledown;
349 y0 = ROUND_DOWN_TO(y0, y_align) / y_scaledown;
350 x1 = ALIGN(x1, x_align) / x_scaledown;
351 y1 = ALIGN(y1, y_align) / y_scaledown;
352 }
353 }
354
355
356 brw_blorp_rt_resolve_params::brw_blorp_rt_resolve_params(
357 struct brw_context *brw,
358 struct intel_mipmap_tree *mt)
359 {
360 dst.set(brw, mt, 0 /* level */, 0 /* layer */, true);
361
362 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
363 *
364 * A rectangle primitive must be scaled down by the following factors
365 * with respect to render target being resolved.
366 *
367 * The scaledown factors in the table that follows are related to the
368 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but with
369 * X and Y alignment each divided by 2.
370 */
371 unsigned x_align, y_align;
372 intel_get_non_msrt_mcs_alignment(brw, mt, &x_align, &y_align);
373 unsigned x_scaledown = x_align / 2;
374 unsigned y_scaledown = y_align / 2;
375 x0 = y0 = 0;
376 x1 = ALIGN(mt->logical_width0, x_scaledown) / x_scaledown;
377 y1 = ALIGN(mt->logical_height0, y_scaledown) / y_scaledown;
378
379 fast_clear_op = GEN7_FAST_CLEAR_OP_RESOLVE;
380
381 /* Note: there is no need to initialize push constants because it doesn't
382 * matter what data gets dispatched to the render target. However, we must
383 * ensure that the fragment shader delivers the data using the "replicated
384 * color" message.
385 */
386 use_wm_prog = true;
387 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
388 wm_prog_key.use_simd16_replicated_data = true;
389 }
390
391
392 uint32_t
393 brw_blorp_const_color_params::get_wm_prog(struct brw_context *brw,
394 brw_blorp_prog_data **prog_data)
395 const
396 {
397 uint32_t prog_offset = 0;
398 if (!brw_search_cache(&brw->cache, BRW_BLORP_CONST_COLOR_PROG,
399 &this->wm_prog_key, sizeof(this->wm_prog_key),
400 &prog_offset, prog_data)) {
401 brw_blorp_const_color_program prog(brw, &this->wm_prog_key);
402 GLuint program_size;
403 const GLuint *program = prog.compile(brw, &program_size);
404 brw_upload_cache(&brw->cache, BRW_BLORP_CONST_COLOR_PROG,
405 &this->wm_prog_key, sizeof(this->wm_prog_key),
406 program, program_size,
407 &prog.prog_data, sizeof(prog.prog_data),
408 &prog_offset, prog_data);
409 }
410 return prog_offset;
411 }
412
413 void
414 brw_blorp_const_color_program::alloc_regs()
415 {
416 int reg = 0;
417 this->R0 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
418 this->R1 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
419
420 prog_data.first_curbe_grf = reg;
421 clear_rgba = retype(brw_vec4_grf(reg++, 0), BRW_REGISTER_TYPE_F);
422 reg += BRW_BLORP_NUM_PUSH_CONST_REGS;
423
424 /* Make sure we didn't run out of registers */
425 assert(reg <= GEN7_MRF_HACK_START);
426
427 this->base_mrf = 2;
428 }
429
430 const GLuint *
431 brw_blorp_const_color_program::compile(struct brw_context *brw,
432 GLuint *program_size)
433 {
434 /* Set up prog_data */
435 memset(&prog_data, 0, sizeof(prog_data));
436 prog_data.persample_msaa_dispatch = false;
437
438 alloc_regs();
439
440 brw_set_default_compression_control(&func, BRW_COMPRESSION_NONE);
441
442 struct brw_reg mrf_rt_write =
443 retype(vec16(brw_message_reg(base_mrf)), BRW_REGISTER_TYPE_F);
444
445 uint32_t mlen, msg_type;
446 if (key->use_simd16_replicated_data) {
447 /* The message payload is a single register with the low 4 floats/ints
448 * filled with the constant clear color.
449 */
450 brw_set_default_mask_control(&func, BRW_MASK_DISABLE);
451 brw_MOV(&func, vec4(brw_message_reg(base_mrf)), clear_rgba);
452 brw_set_default_mask_control(&func, BRW_MASK_ENABLE);
453
454 msg_type = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED;
455 mlen = 1;
456 } else {
457 for (int i = 0; i < 4; i++) {
458 /* The message payload is pairs of registers for 16 pixels each of r,
459 * g, b, and a.
460 */
461 brw_set_default_compression_control(&func, BRW_COMPRESSION_COMPRESSED);
462 brw_MOV(&func,
463 brw_message_reg(base_mrf + i * 2),
464 brw_vec1_grf(clear_rgba.nr, i));
465 brw_set_default_compression_control(&func, BRW_COMPRESSION_NONE);
466 }
467
468 msg_type = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE;
469 mlen = 8;
470 }
471
472 /* Now write to the render target and terminate the thread */
473 brw_fb_WRITE(&func,
474 16 /* dispatch_width */,
475 base_mrf /* msg_reg_nr */,
476 mrf_rt_write /* src0 */,
477 msg_type,
478 BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX,
479 mlen,
480 0 /* response_length */,
481 true /* eot */,
482 false /* header present */);
483
484 if (unlikely(INTEL_DEBUG & DEBUG_BLORP)) {
485 fprintf(stderr, "Native code for BLORP clear:\n");
486 brw_disassemble(brw, func.store, 0, func.next_insn_offset, stderr);
487 fprintf(stderr, "\n");
488 }
489
490 brw_compact_instructions(&func, 0, 0, NULL);
491 return brw_get_program(&func, program_size);
492 }
493
494
495 bool
496 do_single_blorp_clear(struct brw_context *brw, struct gl_framebuffer *fb,
497 struct gl_renderbuffer *rb, unsigned buf,
498 bool partial_clear, unsigned layer)
499 {
500 struct gl_context *ctx = &brw->ctx;
501 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
502
503 brw_blorp_clear_params params(brw, fb, rb, ctx->Color.ColorMask[buf],
504 partial_clear, layer);
505
506 bool is_fast_clear =
507 (params.fast_clear_op == GEN7_FAST_CLEAR_OP_FAST_CLEAR);
508 if (is_fast_clear) {
509 /* Record the clear color in the miptree so that it will be
510 * programmed in SURFACE_STATE by later rendering and resolve
511 * operations.
512 */
513 uint32_t new_color_value =
514 compute_fast_clear_color_bits(&ctx->Color.ClearColor);
515 if (irb->mt->fast_clear_color_value != new_color_value) {
516 irb->mt->fast_clear_color_value = new_color_value;
517 brw->state.dirty.brw |= BRW_NEW_SURFACES;
518 }
519
520 /* If the buffer is already in INTEL_FAST_CLEAR_STATE_CLEAR, the clear
521 * is redundant and can be skipped.
522 */
523 if (irb->mt->fast_clear_state == INTEL_FAST_CLEAR_STATE_CLEAR)
524 return true;
525
526 /* If the MCS buffer hasn't been allocated yet, we need to allocate
527 * it now.
528 */
529 if (!irb->mt->mcs_mt) {
530 if (!intel_miptree_alloc_non_msrt_mcs(brw, irb->mt)) {
531 /* MCS allocation failed--probably this will only happen in
532 * out-of-memory conditions. But in any case, try to recover
533 * by falling back to a non-blorp clear technique.
534 */
535 return false;
536 }
537 brw->state.dirty.brw |= BRW_NEW_SURFACES;
538 }
539 }
540
541 const char *clear_type;
542 if (is_fast_clear)
543 clear_type = "fast";
544 else if (params.wm_prog_key.use_simd16_replicated_data)
545 clear_type = "replicated";
546 else
547 clear_type = "slow";
548
549 DBG("%s (%s) to mt %p level %d layer %d\n", __FUNCTION__, clear_type,
550 irb->mt, irb->mt_level, irb->mt_layer);
551
552 brw_blorp_exec(brw, &params);
553
554 if (is_fast_clear) {
555 /* Now that the fast clear has occurred, put the buffer in
556 * INTEL_FAST_CLEAR_STATE_CLEAR so that we won't waste time doing
557 * redundant clears.
558 */
559 irb->mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_CLEAR;
560 }
561
562 return true;
563 }
564
565
566 extern "C" {
567 bool
568 brw_blorp_clear_color(struct brw_context *brw, struct gl_framebuffer *fb,
569 GLbitfield mask, bool partial_clear)
570 {
571 for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
572 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
573 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
574
575 /* Only clear the buffers present in the provided mask */
576 if (((1 << fb->_ColorDrawBufferIndexes[buf]) & mask) == 0)
577 continue;
578
579 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
580 * the framebuffer can be complete with some attachments missing. In
581 * this case the _ColorDrawBuffers pointer will be NULL.
582 */
583 if (rb == NULL)
584 continue;
585
586 if (fb->MaxNumLayers > 0) {
587 unsigned layer_multiplier =
588 (irb->mt->msaa_layout == INTEL_MSAA_LAYOUT_UMS ||
589 irb->mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) ?
590 irb->mt->num_samples : 1;
591 unsigned num_layers = irb->layer_count;
592 for (unsigned layer = 0; layer < num_layers; layer++) {
593 if (!do_single_blorp_clear(brw, fb, rb, buf, partial_clear,
594 irb->mt_layer + 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" */