2 * Copyright © 2014 Intel Corporation
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:
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
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
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"
41 #include "main/api_validate.h"
42 #include "main/state.h"
44 #include "vbo/vbo_context.h"
46 #include "drivers/common/meta.h"
48 #include "brw_defines.h"
49 #include "brw_context.h"
51 #include "intel_fbo.h"
52 #include "intel_batchbuffer.h"
54 #include "brw_blorp.h"
56 struct brw_fast_clear_state
{
57 struct gl_buffer_object
*buf_obj
;
58 struct gl_vertex_array_object
*array_obj
;
65 brw_fast_clear_init(struct brw_context
*brw
)
67 struct brw_fast_clear_state
*clear
;
68 struct gl_context
*ctx
= &brw
->ctx
;
70 if (brw
->fast_clear_state
) {
71 clear
= brw
->fast_clear_state
;
72 _mesa_BindVertexArray(clear
->vao
);
76 brw
->fast_clear_state
= clear
= malloc(sizeof *clear
);
80 memset(clear
, 0, sizeof *clear
);
81 _mesa_GenVertexArrays(1, &clear
->vao
);
82 _mesa_BindVertexArray(clear
->vao
);
84 clear
->buf_obj
= ctx
->Driver
.NewBufferObject(ctx
, 0xDEADBEEF);
85 if (clear
->buf_obj
== NULL
)
88 clear
->array_obj
= _mesa_lookup_vao(ctx
, clear
->vao
);
89 assert(clear
->array_obj
!= NULL
);
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
,
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));
103 brw_bind_rep_write_shader(struct brw_context
*brw
, float *color
)
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"
112 "#ifdef GL_AMD_vertex_shader_layer\n"
113 " gl_Layer = gl_InstanceID;\n"
115 " gl_Position = position;\n"
117 const char *fs_source
=
118 "uniform vec4 color;\n"
121 " gl_FragColor = color;\n"
125 struct brw_fast_clear_state
*clear
= brw
->fast_clear_state
;
126 struct gl_context
*ctx
= &brw
->ctx
;
128 if (clear
->shader_prog
) {
129 _mesa_UseProgram(clear
->shader_prog
);
130 _mesa_Uniform4fv(clear
->color_location
, 1, color
);
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
);
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
);
146 clear
->color_location
=
147 _mesa_GetUniformLocation(clear
->shader_prog
, "color");
149 _mesa_UseProgram(clear
->shader_prog
);
150 _mesa_Uniform4fv(clear
->color_location
, 1, color
);
154 brw_meta_fast_clear_free(struct brw_context
*brw
)
156 struct brw_fast_clear_state
*clear
= brw
->fast_clear_state
;
157 GET_CURRENT_CONTEXT(old_context
);
162 _mesa_make_current(&brw
->ctx
, NULL
, NULL
);
164 _mesa_DeleteVertexArrays(1, &clear
->vao
);
165 _mesa_reference_buffer_object(&brw
->ctx
, &clear
->buf_obj
, NULL
);
166 _mesa_DeleteProgram(clear
->shader_prog
);
170 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
172 _mesa_make_current(NULL
, NULL
, NULL
);
180 brw_draw_rectlist(struct brw_context
*brw
, struct rect
*rect
, int num_instances
)
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
;
195 /* upload new vertex data */
196 _mesa_buffer_data(ctx
, clear
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
197 GL_DYNAMIC_DRAW
, __func__
);
200 _mesa_update_state(ctx
);
202 vbo_bind_arrays(ctx
);
204 memset(&prim
, 0, sizeof prim
);
207 prim
.mode
= BRW_PRIM_OFFSET
+ _3DPRIM_RECTLIST
;
208 prim
.num_instances
= num_instances
;
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
));
215 brw_draw_prims(ctx
, &prim
, 1, NULL
,
216 GL_TRUE
, start
, start
+ count
- 1,
221 get_fast_clear_rect(struct brw_context
*brw
, struct gl_framebuffer
*fb
,
222 struct intel_renderbuffer
*irb
, struct rect
*rect
)
224 unsigned int x_align
, y_align
;
225 unsigned int x_scaledown
, y_scaledown
;
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):
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.
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.
243 intel_get_non_msrt_mcs_alignment(irb
->mt
, &x_align
, &y_align
);
246 /* SKL+ line alignment requirement for Y-tiled are half those of the prior
254 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
255 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
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:
262 * The X and Y scale down factors in the table that follows are each
263 * equal to half the alignment value computed above.
265 x_scaledown
= x_align
/ 2;
266 y_scaledown
= y_align
/ 2;
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":
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
279 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
280 * Target(s)", beneath the "MSAA Compression" bullet (p326):
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.
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)
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.
307 switch (irb
->mt
->num_samples
) {
319 unreachable("Unexpected sample count for fast clear");
322 x_align
= x_scaledown
* 2;
323 y_align
= y_scaledown
* 2;
326 rect
->x0
= fb
->_Xmin
;
327 rect
->x1
= fb
->_Xmax
;
329 rect
->y0
= fb
->_Ymin
;
330 rect
->y1
= fb
->_Ymax
;
332 rect
->y0
= fb
->Height
- fb
->_Ymax
;
333 rect
->y1
= fb
->Height
- fb
->_Ymin
;
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
;
343 get_buffer_rect(const struct gl_framebuffer
*fb
, struct rect
*rect
)
345 rect
->x0
= fb
->_Xmin
;
346 rect
->x1
= fb
->_Xmax
;
348 rect
->y0
= fb
->_Ymin
;
349 rect
->y1
= fb
->_Ymax
;
351 rect
->y0
= fb
->Height
- fb
->_Ymax
;
352 rect
->y1
= fb
->Height
- fb
->_Ymin
;
357 * Determine if fast color clear supports the given clear color.
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.
363 is_color_fast_clear_compatible(struct brw_context
*brw
,
365 const union gl_color_union
*color
)
367 if (_mesa_is_format_integer_color(format
)) {
369 perf_debug("Integer fast clear not enabled for (%s)",
370 _mesa_get_format_name(format
));
375 for (int i
= 0; i
< 4; i
++) {
376 if (!_mesa_format_has_color_component(format
, i
)) {
381 color
->f
[i
] != 0.0f
&& color
->f
[i
] != 1.0f
) {
389 * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
390 * SURFACE_STATE (DWORD 12-15 on SKL+).
393 set_fast_clear_color(struct brw_context
*brw
,
394 struct intel_mipmap_tree
*mt
,
395 const union gl_color_union
*color
)
397 union gl_color_union override_color
= *color
;
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.
403 switch (_mesa_get_format_base_format(mt
->format
)) {
405 override_color
.ui
[3] = override_color
.ui
[0];
408 case GL_LUMINANCE_ALPHA
:
409 override_color
.ui
[1] = override_color
.ui
[0];
410 override_color
.ui
[2] = override_color
.ui
[0];
413 for (int i
= 0; i
< 3; i
++) {
414 if (!_mesa_format_has_color_component(mt
->format
, i
))
415 override_color
.ui
[i
] = 0;
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;
424 override_color
.f
[3] = 1.0f
;
428 mt
->gen9_fast_clear_color
= override_color
;
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
));
441 static const uint32_t fast_clear_color
[4] = { ~0, ~0, ~0, ~0 };
444 set_fast_clear_op(struct brw_context
*brw
, uint32_t op
)
446 /* Set op and dirty BRW_NEW_FRAGMENT_PROGRAM to make sure we re-emit
449 brw
->wm
.fast_clear_op
= op
;
450 brw
->ctx
.NewDriverState
|= BRW_NEW_FRAGMENT_PROGRAM
;
454 use_rectlist(struct brw_context
*brw
, bool enable
)
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.
461 brw
->sf
.viewport_transform_enable
= !enable
;
462 brw
->use_rep_send
= enable
;
463 brw
->no_simd8
= enable
;
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
474 brw
->NewGLState
|= _NEW_LIGHT
| _NEW_BUFFERS
;
475 brw
->ctx
.NewDriverState
|= BRW_NEW_FRAGMENT_PROGRAM
;
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
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."
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.
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
)
496 assert(brw
->gen
>= 9);
498 brw_bind_rep_write_shader(brw
, (float *) fast_clear_color
);
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.
504 set_fast_clear_op(brw
, GEN7_PS_RENDER_TARGET_FAST_CLEAR_ENABLE
);
506 while (fast_clear_buffers
) {
507 int index
= ffs(fast_clear_buffers
) - 1;
509 fast_clear_buffers
&= ~(1 << index
);
511 _mesa_meta_drawbuffers_from_bitfield(1 << index
);
513 brw_draw_rectlist(brw
, &fast_clear_rect
, MAX2(1, fb
->MaxNumLayers
));
515 /* Now set the mcs we cleared to INTEL_FAST_CLEAR_STATE_CLEAR so we'll
516 * resolve them eventually.
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
;
523 set_fast_clear_op(brw
, 0);
527 brw_meta_fast_clear(struct brw_context
*brw
, struct gl_framebuffer
*fb
,
528 GLbitfield buffers
, bool partial_clear
)
530 struct gl_context
*ctx
= &brw
->ctx
;
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
;
537 fast_clear_buffers
= rep_clear_buffers
= plain_clear_buffers
= 0;
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.
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
];
548 /* Only clear the buffers present in the provided mask */
549 if (((1 << index
) & buffers
) == 0)
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.
559 clear_type
= FAST_CLEAR
;
561 /* We don't have fast clear until gen7. */
563 clear_type
= REP_CLEAR
;
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.
569 if (brw
->gen
>= 9 && irb
->mt
->num_samples
> 1)
570 clear_type
= REP_CLEAR
;
572 if (irb
->mt
->fast_clear_state
== INTEL_FAST_CLEAR_STATE_NO_MCS
)
573 clear_type
= REP_CLEAR
;
575 /* We can't do scissored fast clears because of the restrictions on the
576 * fast clear rectangle size.
579 clear_type
= REP_CLEAR
;
581 /* Fast clear is only supported for colors where all components are
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
;
588 /* From the SNB PRM (Vol4_Part1):
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."
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
;
600 /* Constant color writes ignore everything in blend and color calculator
601 * state. This is not documented.
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
) &&
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
;
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
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
;
621 switch (clear_type
) {
623 set_fast_clear_color(brw
, irb
->mt
, &ctx
->Color
.ClearColor
);
624 irb
->need_downsample
= true;
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.
630 if (irb
->mt
->fast_clear_state
== INTEL_FAST_CLEAR_STATE_CLEAR
)
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.
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
);
643 rep_clear_buffers
|= 1 << index
;
644 get_buffer_rect(fb
, &clear_rect
);
648 plain_clear_buffers
|= 1 << index
;
649 get_buffer_rect(fb
, &clear_rect
);
654 assert((fast_clear_buffers
& rep_clear_buffers
) == 0);
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. */
661 /* Nothing left to do. This happens when we hit the redundant fast
662 * clear case above and nothing else.
668 MESA_META_ALPHA_TEST
|
670 MESA_META_DEPTH_TEST
|
671 MESA_META_RASTERIZATION
|
673 MESA_META_STENCIL_TEST
|
677 MESA_META_CLAMP_FRAGMENT_COLOR
|
678 MESA_META_MULTISAMPLE
|
679 MESA_META_OCCLUSION_QUERY
|
680 MESA_META_DRAW_BUFFERS
;
682 _mesa_meta_begin(ctx
, meta_save
);
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.
688 plain_clear_buffers
= buffers
;
692 /* Clears never have the color clamped. */
693 if (ctx
->Extensions
.ARB_color_buffer_float
)
694 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
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
);
700 use_rectlist(brw
, true);
702 layers
= MAX2(1, fb
->MaxNumLayers
);
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);
713 /* Now set the mcs we cleared to INTEL_FAST_CLEAR_STATE_CLEAR so we'll
714 * resolve them eventually.
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
];
721 if ((1 << index
) & fast_clear_buffers
)
722 irb
->mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_CLEAR
;
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
);
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
737 brw
->NewGLState
|= _NEW_BUFFERS
;
740 /* Set the custom state back to normal and dirty the same bits as above */
741 use_rectlist(brw
, false);
745 /* From BSpec: Render Target Fast Clear:
747 * After Render target fast clear, pipe-control with color cache
748 * write-flush must be issued before sending any DRAW commands on that
751 brw_emit_mi_flush(brw
);
753 /* If we had to fall back to plain clear for any buffers, clear those now
754 * by calling into meta.
757 if (plain_clear_buffers
)
758 _mesa_meta_glsl_Clear(&brw
->ctx
, plain_clear_buffers
);
764 get_resolve_rect(struct brw_context
*brw
,
765 struct intel_mipmap_tree
*mt
, struct rect
*rect
)
767 unsigned x_align
, y_align
;
768 unsigned x_scaledown
, y_scaledown
;
770 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
772 * A rectangle primitive must be scaled down by the following factors
773 * with respect to render target being resolved.
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
782 intel_get_non_msrt_mcs_alignment(mt
, &x_align
, &y_align
);
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;
790 x_scaledown
= x_align
/ 2;
791 y_scaledown
= y_align
/ 2;
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
;
799 brw_meta_resolve_color(struct brw_context
*brw
,
800 struct intel_mipmap_tree
*mt
)
802 struct gl_context
*ctx
= &brw
->ctx
;
806 brw_emit_mi_flush(brw
);
808 _mesa_meta_begin(ctx
, MESA_META_ALL
);
810 _mesa_GenFramebuffers(1, &fbo
);
811 rbo
= brw_get_rb_for_slice(brw
, mt
, 0, 0, false);
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
);
819 brw_fast_clear_init(brw
);
821 use_rectlist(brw
, true);
823 brw_bind_rep_write_shader(brw
, (float *) fast_clear_color
);
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.
829 set_fast_clear_op(brw
, GEN7_PS_RENDER_TARGET_RESOLVE_ENABLE
);
831 mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_RESOLVED
;
832 get_resolve_rect(brw
, mt
, &rect
);
834 brw_draw_rectlist(brw
, &rect
, 1);
836 set_fast_clear_op(brw
, 0);
837 use_rectlist(brw
, false);
839 _mesa_DeleteRenderbuffers(1, &rbo
);
840 _mesa_DeleteFramebuffers(1, &fbo
);
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.
851 _mesa_update_state(ctx
);