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