i965/skl: Add fast color clear infrastructure
[mesa.git] / src / mesa / drivers / dri / i965 / brw_meta_fast_clear.c
1 /*
2 * Copyright © 2014 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 #include "main/mtypes.h"
25 #include "main/macros.h"
26 #include "main/context.h"
27 #include "main/objectlabel.h"
28 #include "main/shaderapi.h"
29 #include "main/arrayobj.h"
30 #include "main/bufferobj.h"
31 #include "main/buffers.h"
32 #include "main/blend.h"
33 #include "main/enable.h"
34 #include "main/depth.h"
35 #include "main/stencil.h"
36 #include "main/varray.h"
37 #include "main/uniforms.h"
38 #include "main/fbobject.h"
39 #include "main/texobj.h"
40
41 #include "main/api_validate.h"
42 #include "main/state.h"
43
44 #include "vbo/vbo_context.h"
45
46 #include "drivers/common/meta.h"
47
48 #include "brw_defines.h"
49 #include "brw_context.h"
50 #include "brw_draw.h"
51 #include "intel_fbo.h"
52 #include "intel_batchbuffer.h"
53
54 #include "brw_blorp.h"
55
56 struct brw_fast_clear_state {
57 GLuint vao;
58 GLuint vbo;
59 GLuint shader_prog;
60 GLint color_location;
61 };
62
63 static bool
64 brw_fast_clear_init(struct brw_context *brw)
65 {
66 struct brw_fast_clear_state *clear;
67
68 if (brw->fast_clear_state) {
69 clear = brw->fast_clear_state;
70 _mesa_BindVertexArray(clear->vao);
71 _mesa_BindBuffer(GL_ARRAY_BUFFER, clear->vbo);
72 return true;
73 }
74
75 brw->fast_clear_state = clear = malloc(sizeof *clear);
76 if (clear == NULL)
77 return false;
78
79 memset(clear, 0, sizeof *clear);
80 _mesa_GenVertexArrays(1, &clear->vao);
81 _mesa_BindVertexArray(clear->vao);
82 _mesa_GenBuffers(1, &clear->vbo);
83 _mesa_BindBuffer(GL_ARRAY_BUFFER, clear->vbo);
84 _mesa_VertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
85 _mesa_EnableVertexAttribArray(0);
86
87 return true;
88 }
89
90 static void
91 brw_bind_rep_write_shader(struct brw_context *brw, float *color)
92 {
93 const char *vs_source =
94 "#extension GL_AMD_vertex_shader_layer : enable\n"
95 "#extension GL_ARB_draw_instanced : enable\n"
96 "attribute vec4 position;\n"
97 "uniform int layer;\n"
98 "void main()\n"
99 "{\n"
100 "#ifdef GL_AMD_vertex_shader_layer\n"
101 " gl_Layer = gl_InstanceID;\n"
102 "#endif\n"
103 " gl_Position = position;\n"
104 "}\n";
105 const char *fs_source =
106 "uniform vec4 color;\n"
107 "void main()\n"
108 "{\n"
109 " gl_FragColor = color;\n"
110 "}\n";
111
112 GLuint vs, fs;
113 struct brw_fast_clear_state *clear = brw->fast_clear_state;
114 struct gl_context *ctx = &brw->ctx;
115
116 if (clear->shader_prog) {
117 _mesa_UseProgram(clear->shader_prog);
118 _mesa_Uniform4fv(clear->color_location, 1, color);
119 return;
120 }
121
122 vs = _mesa_meta_compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
123 fs = _mesa_meta_compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
124
125 clear->shader_prog = _mesa_CreateProgram();
126 _mesa_AttachShader(clear->shader_prog, fs);
127 _mesa_DeleteShader(fs);
128 _mesa_AttachShader(clear->shader_prog, vs);
129 _mesa_DeleteShader(vs);
130 _mesa_BindAttribLocation(clear->shader_prog, 0, "position");
131 _mesa_ObjectLabel(GL_PROGRAM, clear->shader_prog, -1, "meta repclear");
132 _mesa_LinkProgram(clear->shader_prog);
133
134 clear->color_location =
135 _mesa_GetUniformLocation(clear->shader_prog, "color");
136
137 _mesa_UseProgram(clear->shader_prog);
138 _mesa_Uniform4fv(clear->color_location, 1, color);
139 }
140
141 void
142 brw_meta_fast_clear_free(struct brw_context *brw)
143 {
144 struct brw_fast_clear_state *clear = brw->fast_clear_state;
145 GET_CURRENT_CONTEXT(old_context);
146
147 if (clear == NULL)
148 return;
149
150 _mesa_make_current(&brw->ctx, NULL, NULL);
151
152 _mesa_DeleteVertexArrays(1, &clear->vao);
153 _mesa_DeleteBuffers(1, &clear->vbo);
154 _mesa_DeleteProgram(clear->shader_prog);
155 free(clear);
156
157 if (old_context)
158 _mesa_make_current(old_context, old_context->WinSysDrawBuffer, old_context->WinSysReadBuffer);
159 else
160 _mesa_make_current(NULL, NULL, NULL);
161 }
162
163 struct rect {
164 int x0, y0, x1, y1;
165 };
166
167 static void
168 brw_draw_rectlist(struct gl_context *ctx, struct rect *rect, int num_instances)
169 {
170 int start = 0, count = 3;
171 struct _mesa_prim prim;
172 float verts[6];
173
174 verts[0] = rect->x1;
175 verts[1] = rect->y1;
176 verts[2] = rect->x0;
177 verts[3] = rect->y1;
178 verts[4] = rect->x0;
179 verts[5] = rect->y0;
180
181 /* upload new vertex data */
182 _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
183 GL_DYNAMIC_DRAW_ARB);
184
185 if (ctx->NewState)
186 _mesa_update_state(ctx);
187
188 vbo_bind_arrays(ctx);
189
190 memset(&prim, 0, sizeof prim);
191 prim.begin = 1;
192 prim.end = 1;
193 prim.mode = BRW_PRIM_OFFSET + _3DPRIM_RECTLIST;
194 prim.num_instances = num_instances;
195 prim.start = start;
196 prim.count = count;
197
198 /* Make sure our internal prim value doesn't clash with a valid GL value. */
199 assert(!_mesa_is_valid_prim_mode(ctx, prim.mode));
200
201 brw_draw_prims(ctx, &prim, 1, NULL,
202 GL_TRUE, start, start + count - 1,
203 NULL, 0, NULL);
204 }
205
206 static void
207 get_fast_clear_rect(struct brw_context *brw, struct gl_framebuffer *fb,
208 struct intel_renderbuffer *irb, struct rect *rect)
209 {
210 unsigned int x_align, y_align;
211 unsigned int x_scaledown, y_scaledown;
212
213 if (irb->mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE) {
214 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
215 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
216 *
217 * Clear pass must have a clear rectangle that must follow
218 * alignment rules in terms of pixels and lines as shown in the
219 * table below. Further, the clear-rectangle height and width
220 * must be multiple of the following dimensions. If the height
221 * and width of the render target being cleared do not meet these
222 * requirements, an MCS buffer can be created such that it
223 * follows the requirement and covers the RT.
224 *
225 * The alignment size in the table that follows is related to the
226 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but
227 * with X alignment multiplied by 16 and Y alignment multiplied by 32.
228 */
229 intel_get_non_msrt_mcs_alignment(irb->mt, &x_align, &y_align);
230 x_align *= 16;
231
232 /* SKL+ line alignment requirement for Y-tiled are half those of the prior
233 * generations.
234 */
235 if (brw->gen >= 9)
236 y_align *= 16;
237 else
238 y_align *= 32;
239
240 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
241 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
242 *
243 * In order to optimize the performance MCS buffer (when bound to
244 * 1X RT) clear similarly to MCS buffer clear for MSRT case,
245 * clear rect is required to be scaled by the following factors
246 * in the horizontal and vertical directions:
247 *
248 * The X and Y scale down factors in the table that follows are each
249 * equal to half the alignment value computed above.
250 */
251 x_scaledown = x_align / 2;
252 y_scaledown = y_align / 2;
253
254 /* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel
255 * Backend > MCS Buffer for Render Target(s) [DevIVB+] > Table "Color
256 * Clear of Non-MultiSampled Render Target Restrictions":
257 *
258 * Clear rectangle must be aligned to two times the number of
259 * pixels in the table shown below due to 16x16 hashing across the
260 * slice.
261 */
262 x_align *= 2;
263 y_align *= 2;
264 } else {
265 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
266 * Target(s)", beneath the "MSAA Compression" bullet (p326):
267 *
268 * Clear pass for this case requires that scaled down primitive
269 * is sent down with upper left co-ordinate to coincide with
270 * actual rectangle being cleared. For MSAA, clear rectangle’s
271 * height and width need to as show in the following table in
272 * terms of (width,height) of the RT.
273 *
274 * MSAA Width of Clear Rect Height of Clear Rect
275 * 2X Ceil(1/8*width) Ceil(1/2*height)
276 * 4X Ceil(1/8*width) Ceil(1/2*height)
277 * 8X Ceil(1/2*width) Ceil(1/2*height)
278 * 16X width Ceil(1/2*height)
279 *
280 * The text "with upper left co-ordinate to coincide with actual
281 * rectangle being cleared" is a little confusing--it seems to imply
282 * that to clear a rectangle from (x,y) to (x+w,y+h), one needs to
283 * feed the pipeline using the rectangle (x,y) to
284 * (x+Ceil(w/N),y+Ceil(h/2)), where N is either 2 or 8 depending on
285 * the number of samples. Experiments indicate that this is not
286 * quite correct; actually, what the hardware appears to do is to
287 * align whatever rectangle is sent down the pipeline to the nearest
288 * multiple of 2x2 blocks, and then scale it up by a factor of N
289 * horizontally and 2 vertically. So the resulting alignment is 4
290 * vertically and either 4 or 16 horizontally, and the scaledown
291 * factor is 2 vertically and either 2 or 8 horizontally.
292 */
293 switch (irb->mt->num_samples) {
294 case 2:
295 case 4:
296 x_scaledown = 8;
297 break;
298 case 8:
299 x_scaledown = 2;
300 break;
301 case 16:
302 x_scaledown = 1;
303 break;
304 default:
305 unreachable("Unexpected sample count for fast clear");
306 }
307 y_scaledown = 2;
308 x_align = x_scaledown * 2;
309 y_align = y_scaledown * 2;
310 }
311
312 rect->x0 = fb->_Xmin;
313 rect->x1 = fb->_Xmax;
314 if (fb->Name != 0) {
315 rect->y0 = fb->_Ymin;
316 rect->y1 = fb->_Ymax;
317 } else {
318 rect->y0 = fb->Height - fb->_Ymax;
319 rect->y1 = fb->Height - fb->_Ymin;
320 }
321
322 rect->x0 = ROUND_DOWN_TO(rect->x0, x_align) / x_scaledown;
323 rect->y0 = ROUND_DOWN_TO(rect->y0, y_align) / y_scaledown;
324 rect->x1 = ALIGN(rect->x1, x_align) / x_scaledown;
325 rect->y1 = ALIGN(rect->y1, y_align) / y_scaledown;
326 }
327
328 static void
329 get_buffer_rect(const struct gl_framebuffer *fb, struct rect *rect)
330 {
331 rect->x0 = fb->_Xmin;
332 rect->x1 = fb->_Xmax;
333 if (fb->Name != 0) {
334 rect->y0 = fb->_Ymin;
335 rect->y1 = fb->_Ymax;
336 } else {
337 rect->y0 = fb->Height - fb->_Ymax;
338 rect->y1 = fb->Height - fb->_Ymin;
339 }
340 }
341
342 /**
343 * Determine if fast color clear supports the given clear color.
344 *
345 * Fast color clear can only clear to color values of 1.0 or 0.0. At the
346 * moment we only support floating point, unorm, and snorm buffers.
347 */
348 static bool
349 is_color_fast_clear_compatible(struct brw_context *brw,
350 mesa_format format,
351 const union gl_color_union *color)
352 {
353 if (_mesa_is_format_integer_color(format)) {
354 if (brw->gen >= 8) {
355 perf_debug("Integer fast clear not enabled for (%s)",
356 _mesa_get_format_name(format));
357 }
358 return false;
359 }
360
361 for (int i = 0; i < 4; i++) {
362 if (color->f[i] != 0.0f && color->f[i] != 1.0f &&
363 _mesa_format_has_color_component(format, i)) {
364 return false;
365 }
366 }
367 return true;
368 }
369
370 /**
371 * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
372 * SURFACE_STATE (DWORD 12-15 on SKL+).
373 */
374 static void
375 set_fast_clear_color(struct brw_context *brw,
376 struct intel_mipmap_tree *mt,
377 const union gl_color_union *color)
378 {
379 if (brw->gen >= 9) {
380 mt->gen9_fast_clear_color = *color;
381 } else {
382 mt->fast_clear_color_value = 0;
383 for (int i = 0; i < 4; i++) {
384 /* Testing for non-0 works for integer and float colors */
385 if (color->f[i] != 0.0f) {
386 mt->fast_clear_color_value |=
387 1 << (GEN7_SURFACE_CLEAR_COLOR_SHIFT + (3 - i));
388 }
389 }
390 }
391 }
392
393 static const uint32_t fast_clear_color[4] = { ~0, ~0, ~0, ~0 };
394
395 static void
396 set_fast_clear_op(struct brw_context *brw, uint32_t op)
397 {
398 /* Set op and dirty BRW_NEW_FRAGMENT_PROGRAM to make sure we re-emit
399 * 3DSTATE_PS.
400 */
401 brw->wm.fast_clear_op = op;
402 brw->ctx.NewDriverState |= BRW_NEW_FRAGMENT_PROGRAM;
403 }
404
405 static void
406 use_rectlist(struct brw_context *brw, bool enable)
407 {
408 /* Set custom state to let us use _3DPRIM_RECTLIST and the replicated
409 * rendertarget write. When we enable reclist mode, we disable the
410 * viewport transform, disable clipping, enable the rep16 write
411 * optimization and disable simd8 dispatch in the PS.
412 */
413 brw->sf.viewport_transform_enable = !enable;
414 brw->use_rep_send = enable;
415 brw->no_simd8 = enable;
416
417 /* Dirty state to make sure we reemit the state packages affected by the
418 * custom state. We dirty BRW_NEW_FRAGMENT_PROGRAM to emit 3DSTATE_PS for
419 * disabling simd8 dispatch, _NEW_LIGHT to emit 3DSTATE_SF for disabling
420 * the viewport transform and 3DSTATE_CLIP to disable clipping for the
421 * reclist primitive. This is a little messy - it would be nicer to
422 * BRW_NEW_FAST_CLEAR flag or so, but we're out of brw state bits. Dirty
423 * _NEW_BUFFERS to make sure we emit new SURFACE_STATE with the new fast
424 * clear color value.
425 */
426 brw->NewGLState |= _NEW_LIGHT | _NEW_BUFFERS;
427 brw->ctx.NewDriverState |= BRW_NEW_FRAGMENT_PROGRAM;
428 }
429
430 bool
431 brw_meta_fast_clear(struct brw_context *brw, struct gl_framebuffer *fb,
432 GLbitfield buffers, bool partial_clear)
433 {
434 struct gl_context *ctx = &brw->ctx;
435 mesa_format format;
436 enum { FAST_CLEAR, REP_CLEAR, PLAIN_CLEAR } clear_type;
437 GLbitfield plain_clear_buffers, meta_save, rep_clear_buffers, fast_clear_buffers;
438 struct rect fast_clear_rect, clear_rect;
439 int layers;
440
441 fast_clear_buffers = rep_clear_buffers = plain_clear_buffers = 0;
442
443 /* First we loop through the color draw buffers and determine which ones
444 * can be fast cleared, which ones can use the replicated write and which
445 * ones have to fall back to regular color clear.
446 */
447 for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
448 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
449 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
450 int index = fb->_ColorDrawBufferIndexes[buf];
451
452 /* Only clear the buffers present in the provided mask */
453 if (((1 << index) & buffers) == 0)
454 continue;
455
456 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
457 * the framebuffer can be complete with some attachments missing. In
458 * this case the _ColorDrawBuffers pointer will be NULL.
459 */
460 if (rb == NULL)
461 continue;
462
463 clear_type = FAST_CLEAR;
464
465 /* We don't have fast clear until gen7. */
466 if (brw->gen < 7)
467 clear_type = REP_CLEAR;
468
469 /* Certain formats have unresolved issues with sampling from the MCS
470 * buffer on Gen9. This disables fast clears altogether for MSRTs until
471 * we can figure out what's going on.
472 */
473 if (brw->gen >= 9 && irb->mt->num_samples > 1)
474 clear_type = REP_CLEAR;
475
476 if (irb->mt->fast_clear_state == INTEL_FAST_CLEAR_STATE_NO_MCS)
477 clear_type = REP_CLEAR;
478
479 if (brw->gen >= 9 && clear_type == FAST_CLEAR) {
480 perf_debug("fast MCS clears are disabled on gen9");
481 clear_type = REP_CLEAR;
482 }
483
484 /* We can't do scissored fast clears because of the restrictions on the
485 * fast clear rectangle size.
486 */
487 if (partial_clear)
488 clear_type = REP_CLEAR;
489
490 /* Fast clear is only supported for colors where all components are
491 * either 0 or 1.
492 */
493 format = _mesa_get_render_format(ctx, irb->mt->format);
494 if (!is_color_fast_clear_compatible(brw, format, &ctx->Color.ClearColor))
495 clear_type = REP_CLEAR;
496
497 /* From the SNB PRM (Vol4_Part1):
498 *
499 * "Replicated data (Message Type = 111) is only supported when
500 * accessing tiled memory. Using this Message Type to access
501 * linear (untiled) memory is UNDEFINED."
502 */
503 if (irb->mt->tiling == I915_TILING_NONE) {
504 perf_debug("Falling back to plain clear because %dx%d buffer is untiled\n",
505 irb->mt->logical_width0, irb->mt->logical_height0);
506 clear_type = PLAIN_CLEAR;
507 }
508
509 /* Constant color writes ignore everything in blend and color calculator
510 * state. This is not documented.
511 */
512 GLubyte *color_mask = ctx->Color.ColorMask[buf];
513 for (int i = 0; i < 4; i++) {
514 if (_mesa_format_has_color_component(irb->mt->format, i) &&
515 !color_mask[i]) {
516 perf_debug("Falling back to plain clear on %dx%d buffer because of color mask\n",
517 irb->mt->logical_width0, irb->mt->logical_height0);
518 clear_type = PLAIN_CLEAR;
519 }
520 }
521
522 /* Allocate the MCS for non MSRT surfaces now if we're doing a fast
523 * clear and we don't have the MCS yet. On failure, fall back to
524 * replicated clear.
525 */
526 if (clear_type == FAST_CLEAR && irb->mt->mcs_mt == NULL)
527 if (!intel_miptree_alloc_non_msrt_mcs(brw, irb->mt))
528 clear_type = REP_CLEAR;
529
530 switch (clear_type) {
531 case FAST_CLEAR:
532 set_fast_clear_color(brw, irb->mt, &ctx->Color.ClearColor);
533 irb->need_downsample = true;
534
535 /* If the buffer is already in INTEL_FAST_CLEAR_STATE_CLEAR, the
536 * clear is redundant and can be skipped. Only skip after we've
537 * updated the fast clear color above though.
538 */
539 if (irb->mt->fast_clear_state == INTEL_FAST_CLEAR_STATE_CLEAR)
540 continue;
541
542 /* Set fast_clear_state to RESOLVED so we don't try resolve them when
543 * we draw, in case the mt is also bound as a texture.
544 */
545 irb->mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
546 irb->need_downsample = true;
547 fast_clear_buffers |= 1 << index;
548 get_fast_clear_rect(brw, fb, irb, &fast_clear_rect);
549 break;
550
551 case REP_CLEAR:
552 rep_clear_buffers |= 1 << index;
553 get_buffer_rect(fb, &clear_rect);
554 break;
555
556 case PLAIN_CLEAR:
557 plain_clear_buffers |= 1 << index;
558 get_buffer_rect(fb, &clear_rect);
559 continue;
560 }
561 }
562
563 assert((fast_clear_buffers & rep_clear_buffers) == 0);
564
565 if (!(fast_clear_buffers | rep_clear_buffers)) {
566 if (plain_clear_buffers)
567 /* If we only have plain clears, skip the meta save/restore. */
568 goto out;
569 else
570 /* Nothing left to do. This happens when we hit the redundant fast
571 * clear case above and nothing else.
572 */
573 return true;
574 }
575
576 meta_save =
577 MESA_META_ALPHA_TEST |
578 MESA_META_BLEND |
579 MESA_META_DEPTH_TEST |
580 MESA_META_RASTERIZATION |
581 MESA_META_SHADER |
582 MESA_META_STENCIL_TEST |
583 MESA_META_VERTEX |
584 MESA_META_VIEWPORT |
585 MESA_META_CLIP |
586 MESA_META_CLAMP_FRAGMENT_COLOR |
587 MESA_META_MULTISAMPLE |
588 MESA_META_OCCLUSION_QUERY |
589 MESA_META_DRAW_BUFFERS;
590
591 _mesa_meta_begin(ctx, meta_save);
592
593 if (!brw_fast_clear_init(brw)) {
594 /* This is going to be hard to recover from, most likely out of memory.
595 * Bail and let meta try and (probably) fail for us.
596 */
597 plain_clear_buffers = buffers;
598 goto bail_to_meta;
599 }
600
601 /* Clears never have the color clamped. */
602 if (ctx->Extensions.ARB_color_buffer_float)
603 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
604
605 _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_FALSE);
606 _mesa_DepthMask(GL_FALSE);
607 _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_FALSE);
608
609 use_rectlist(brw, true);
610
611 layers = MAX2(1, fb->MaxNumLayers);
612 if (fast_clear_buffers) {
613 _mesa_meta_drawbuffers_from_bitfield(fast_clear_buffers);
614 brw_bind_rep_write_shader(brw, (float *) fast_clear_color);
615 set_fast_clear_op(brw, GEN7_PS_RENDER_TARGET_FAST_CLEAR_ENABLE);
616 brw_draw_rectlist(ctx, &fast_clear_rect, layers);
617 set_fast_clear_op(brw, 0);
618 }
619
620 if (rep_clear_buffers) {
621 _mesa_meta_drawbuffers_from_bitfield(rep_clear_buffers);
622 brw_bind_rep_write_shader(brw, ctx->Color.ClearColor.f);
623 brw_draw_rectlist(ctx, &clear_rect, layers);
624 }
625
626 /* Now set the mts we cleared to INTEL_FAST_CLEAR_STATE_CLEAR so we'll
627 * resolve them eventually.
628 */
629 for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
630 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
631 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
632 int index = fb->_ColorDrawBufferIndexes[buf];
633
634 if ((1 << index) & fast_clear_buffers)
635 irb->mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_CLEAR;
636 }
637
638 bail_to_meta:
639 /* Dirty _NEW_BUFFERS so we reemit SURFACE_STATE which sets the fast clear
640 * color before resolve and sets irb->mt->fast_clear_state to UNRESOLVED if
641 * we render to it.
642 */
643 brw->NewGLState |= _NEW_BUFFERS;
644
645
646 /* Set the custom state back to normal and dirty the same bits as above */
647 use_rectlist(brw, false);
648
649 _mesa_meta_end(ctx);
650
651 /* From BSpec: Render Target Fast Clear:
652 *
653 * After Render target fast clear, pipe-control with color cache
654 * write-flush must be issued before sending any DRAW commands on that
655 * render target.
656 */
657 brw_emit_mi_flush(brw);
658
659 /* If we had to fall back to plain clear for any buffers, clear those now
660 * by calling into meta.
661 */
662 out:
663 if (plain_clear_buffers)
664 _mesa_meta_glsl_Clear(&brw->ctx, plain_clear_buffers);
665
666 return true;
667 }
668
669 static void
670 get_resolve_rect(struct brw_context *brw,
671 struct intel_mipmap_tree *mt, struct rect *rect)
672 {
673 unsigned x_align, y_align;
674 unsigned x_scaledown, y_scaledown;
675
676 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
677 *
678 * A rectangle primitive must be scaled down by the following factors
679 * with respect to render target being resolved.
680 *
681 * The scaledown factors in the table that follows are related to the
682 * alignment size returned by intel_get_non_msrt_mcs_alignment() by a
683 * multiplier. For IVB and HSW, we divide by two, for BDW we multiply
684 * by 8 and 16. Similar to the fast clear, SKL eases the BDW vertical scaling
685 * by a factor of 2.
686 */
687
688 intel_get_non_msrt_mcs_alignment(mt, &x_align, &y_align);
689 if (brw->gen >= 9) {
690 x_scaledown = x_align * 8;
691 y_scaledown = y_align * 8;
692 } else if (brw->gen >= 8) {
693 x_scaledown = x_align * 8;
694 y_scaledown = y_align * 16;
695 } else {
696 x_scaledown = x_align / 2;
697 y_scaledown = y_align / 2;
698 }
699 rect->x0 = rect->y0 = 0;
700 rect->x1 = ALIGN(mt->logical_width0, x_scaledown) / x_scaledown;
701 rect->y1 = ALIGN(mt->logical_height0, y_scaledown) / y_scaledown;
702 }
703
704 void
705 brw_meta_resolve_color(struct brw_context *brw,
706 struct intel_mipmap_tree *mt)
707 {
708 struct gl_context *ctx = &brw->ctx;
709 GLuint fbo, rbo;
710 struct rect rect;
711
712 brw_emit_mi_flush(brw);
713
714 _mesa_meta_begin(ctx, MESA_META_ALL);
715
716 _mesa_GenFramebuffers(1, &fbo);
717 rbo = brw_get_rb_for_slice(brw, mt, 0, 0, false);
718
719 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
720 _mesa_FramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER,
721 GL_COLOR_ATTACHMENT0,
722 GL_RENDERBUFFER, rbo);
723 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0);
724
725 brw_fast_clear_init(brw);
726
727 use_rectlist(brw, true);
728
729 brw_bind_rep_write_shader(brw, (float *) fast_clear_color);
730
731 /* SKL+ also has a resolve mode for compressed render targets and thus more
732 * bits to let us select the type of resolve. For fast clear resolves, it
733 * turns out we can use the same value as pre-SKL though.
734 */
735 set_fast_clear_op(brw, GEN7_PS_RENDER_TARGET_RESOLVE_ENABLE);
736
737 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
738 get_resolve_rect(brw, mt, &rect);
739
740 brw_draw_rectlist(ctx, &rect, 1);
741
742 set_fast_clear_op(brw, 0);
743 use_rectlist(brw, false);
744
745 _mesa_DeleteRenderbuffers(1, &rbo);
746 _mesa_DeleteFramebuffers(1, &fbo);
747
748 _mesa_meta_end(ctx);
749
750 /* We're typically called from intel_update_state() and we're supposed to
751 * return with the state all updated to what it was before
752 * brw_meta_resolve_color() was called. The meta rendering will have
753 * messed up the state and we need to call _mesa_update_state() again to
754 * get back to where we were supposed to be when resolve was called.
755 */
756 if (ctx->NewState)
757 _mesa_update_state(ctx);
758 }