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 "util/format_srgb.h"
46 #include "vbo/vbo_context.h"
48 #include "drivers/common/meta.h"
50 #include "brw_defines.h"
51 #include "brw_context.h"
53 #include "brw_state.h"
54 #include "intel_fbo.h"
55 #include "intel_batchbuffer.h"
57 #include "brw_blorp.h"
59 struct brw_fast_clear_state
{
60 struct gl_buffer_object
*buf_obj
;
61 struct gl_vertex_array_object
*array_obj
;
68 brw_fast_clear_init(struct brw_context
*brw
)
70 struct brw_fast_clear_state
*clear
;
71 struct gl_context
*ctx
= &brw
->ctx
;
73 if (brw
->fast_clear_state
) {
74 clear
= brw
->fast_clear_state
;
75 _mesa_BindVertexArray(clear
->vao
);
79 brw
->fast_clear_state
= clear
= malloc(sizeof *clear
);
83 memset(clear
, 0, sizeof *clear
);
84 _mesa_GenVertexArrays(1, &clear
->vao
);
85 _mesa_BindVertexArray(clear
->vao
);
87 clear
->buf_obj
= ctx
->Driver
.NewBufferObject(ctx
, 0xDEADBEEF);
88 if (clear
->buf_obj
== NULL
)
91 clear
->array_obj
= _mesa_lookup_vao(ctx
, clear
->vao
);
92 assert(clear
->array_obj
!= NULL
);
94 _mesa_update_array_format(ctx
, clear
->array_obj
, VERT_ATTRIB_GENERIC(0),
95 2, GL_FLOAT
, GL_RGBA
, GL_FALSE
, GL_FALSE
, GL_FALSE
,
97 _mesa_bind_vertex_buffer(ctx
, clear
->array_obj
, VERT_ATTRIB_GENERIC(0),
98 clear
->buf_obj
, 0, sizeof(float) * 2);
99 _mesa_enable_vertex_array_attrib(ctx
, clear
->array_obj
,
100 VERT_ATTRIB_GENERIC(0));
106 brw_bind_rep_write_shader(struct brw_context
*brw
, float *color
)
108 const char *vs_source
=
109 "#extension GL_AMD_vertex_shader_layer : enable\n"
110 "#extension GL_ARB_draw_instanced : enable\n"
111 "attribute vec4 position;\n"
112 "uniform int layer;\n"
115 "#ifdef GL_AMD_vertex_shader_layer\n"
116 " gl_Layer = gl_InstanceID;\n"
118 " gl_Position = position;\n"
120 const char *fs_source
=
121 "uniform vec4 color;\n"
124 " gl_FragColor = color;\n"
128 struct brw_fast_clear_state
*clear
= brw
->fast_clear_state
;
129 struct gl_context
*ctx
= &brw
->ctx
;
131 if (clear
->shader_prog
) {
132 _mesa_UseProgram(clear
->shader_prog
);
133 _mesa_Uniform4fv(clear
->color_location
, 1, color
);
137 vs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
138 fs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
140 clear
->shader_prog
= _mesa_CreateProgram();
141 _mesa_AttachShader(clear
->shader_prog
, fs
);
142 _mesa_DeleteShader(fs
);
143 _mesa_AttachShader(clear
->shader_prog
, vs
);
144 _mesa_DeleteShader(vs
);
145 _mesa_BindAttribLocation(clear
->shader_prog
, 0, "position");
146 _mesa_ObjectLabel(GL_PROGRAM
, clear
->shader_prog
, -1, "meta repclear");
147 _mesa_LinkProgram(clear
->shader_prog
);
149 clear
->color_location
=
150 _mesa_GetUniformLocation(clear
->shader_prog
, "color");
152 _mesa_UseProgram(clear
->shader_prog
);
153 _mesa_Uniform4fv(clear
->color_location
, 1, color
);
157 brw_meta_fast_clear_free(struct brw_context
*brw
)
159 struct brw_fast_clear_state
*clear
= brw
->fast_clear_state
;
160 GET_CURRENT_CONTEXT(old_context
);
165 _mesa_make_current(&brw
->ctx
, NULL
, NULL
);
167 _mesa_DeleteVertexArrays(1, &clear
->vao
);
168 _mesa_reference_buffer_object(&brw
->ctx
, &clear
->buf_obj
, NULL
);
169 _mesa_DeleteProgram(clear
->shader_prog
);
173 _mesa_make_current(old_context
, old_context
->WinSysDrawBuffer
, old_context
->WinSysReadBuffer
);
175 _mesa_make_current(NULL
, NULL
, NULL
);
183 brw_draw_rectlist(struct brw_context
*brw
, struct rect
*rect
, int num_instances
)
185 struct gl_context
*ctx
= &brw
->ctx
;
186 struct brw_fast_clear_state
*clear
= brw
->fast_clear_state
;
187 int start
= 0, count
= 3;
188 struct _mesa_prim prim
;
198 /* upload new vertex data */
199 _mesa_buffer_data(ctx
, clear
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
200 GL_DYNAMIC_DRAW
, __func__
);
203 _mesa_update_state(ctx
);
205 vbo_bind_arrays(ctx
);
207 memset(&prim
, 0, sizeof prim
);
210 prim
.mode
= BRW_PRIM_OFFSET
+ _3DPRIM_RECTLIST
;
211 prim
.num_instances
= num_instances
;
215 /* Make sure our internal prim value doesn't clash with a valid GL value. */
216 assert(!_mesa_is_valid_prim_mode(ctx
, prim
.mode
));
218 brw_draw_prims(ctx
, &prim
, 1, NULL
,
219 GL_TRUE
, start
, start
+ count
- 1,
224 get_fast_clear_rect(struct brw_context
*brw
, struct gl_framebuffer
*fb
,
225 struct intel_renderbuffer
*irb
, struct rect
*rect
)
227 unsigned int x_align
, y_align
;
228 unsigned int x_scaledown
, y_scaledown
;
230 if (irb
->mt
->msaa_layout
== INTEL_MSAA_LAYOUT_NONE
) {
231 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
232 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
234 * Clear pass must have a clear rectangle that must follow
235 * alignment rules in terms of pixels and lines as shown in the
236 * table below. Further, the clear-rectangle height and width
237 * must be multiple of the following dimensions. If the height
238 * and width of the render target being cleared do not meet these
239 * requirements, an MCS buffer can be created such that it
240 * follows the requirement and covers the RT.
242 * The alignment size in the table that follows is related to the
243 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but
244 * with X alignment multiplied by 16 and Y alignment multiplied by 32.
246 intel_get_non_msrt_mcs_alignment(irb
->mt
, &x_align
, &y_align
);
249 /* SKL+ line alignment requirement for Y-tiled are half those of the prior
257 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
258 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
260 * In order to optimize the performance MCS buffer (when bound to
261 * 1X RT) clear similarly to MCS buffer clear for MSRT case,
262 * clear rect is required to be scaled by the following factors
263 * in the horizontal and vertical directions:
265 * The X and Y scale down factors in the table that follows are each
266 * equal to half the alignment value computed above.
268 x_scaledown
= x_align
/ 2;
269 y_scaledown
= y_align
/ 2;
271 /* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel
272 * Backend > MCS Buffer for Render Target(s) [DevIVB+] > Table "Color
273 * Clear of Non-MultiSampled Render Target Restrictions":
275 * Clear rectangle must be aligned to two times the number of
276 * pixels in the table shown below due to 16x16 hashing across the
282 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
283 * Target(s)", beneath the "MSAA Compression" bullet (p326):
285 * Clear pass for this case requires that scaled down primitive
286 * is sent down with upper left co-ordinate to coincide with
287 * actual rectangle being cleared. For MSAA, clear rectangle’s
288 * height and width need to as show in the following table in
289 * terms of (width,height) of the RT.
291 * MSAA Width of Clear Rect Height of Clear Rect
292 * 2X Ceil(1/8*width) Ceil(1/2*height)
293 * 4X Ceil(1/8*width) Ceil(1/2*height)
294 * 8X Ceil(1/2*width) Ceil(1/2*height)
295 * 16X width Ceil(1/2*height)
297 * The text "with upper left co-ordinate to coincide with actual
298 * rectangle being cleared" is a little confusing--it seems to imply
299 * that to clear a rectangle from (x,y) to (x+w,y+h), one needs to
300 * feed the pipeline using the rectangle (x,y) to
301 * (x+Ceil(w/N),y+Ceil(h/2)), where N is either 2 or 8 depending on
302 * the number of samples. Experiments indicate that this is not
303 * quite correct; actually, what the hardware appears to do is to
304 * align whatever rectangle is sent down the pipeline to the nearest
305 * multiple of 2x2 blocks, and then scale it up by a factor of N
306 * horizontally and 2 vertically. So the resulting alignment is 4
307 * vertically and either 4 or 16 horizontally, and the scaledown
308 * factor is 2 vertically and either 2 or 8 horizontally.
310 switch (irb
->mt
->num_samples
) {
322 unreachable("Unexpected sample count for fast clear");
325 x_align
= x_scaledown
* 2;
326 y_align
= y_scaledown
* 2;
329 rect
->x0
= fb
->_Xmin
;
330 rect
->x1
= fb
->_Xmax
;
332 rect
->y0
= fb
->_Ymin
;
333 rect
->y1
= fb
->_Ymax
;
335 rect
->y0
= fb
->Height
- fb
->_Ymax
;
336 rect
->y1
= fb
->Height
- fb
->_Ymin
;
339 rect
->x0
= ROUND_DOWN_TO(rect
->x0
, x_align
) / x_scaledown
;
340 rect
->y0
= ROUND_DOWN_TO(rect
->y0
, y_align
) / y_scaledown
;
341 rect
->x1
= ALIGN(rect
->x1
, x_align
) / x_scaledown
;
342 rect
->y1
= ALIGN(rect
->y1
, y_align
) / y_scaledown
;
346 get_buffer_rect(const struct gl_framebuffer
*fb
, struct rect
*rect
)
348 rect
->x0
= fb
->_Xmin
;
349 rect
->x1
= fb
->_Xmax
;
351 rect
->y0
= fb
->_Ymin
;
352 rect
->y1
= fb
->_Ymax
;
354 rect
->y0
= fb
->Height
- fb
->_Ymax
;
355 rect
->y1
= fb
->Height
- fb
->_Ymin
;
360 * Determine if fast color clear supports the given clear color.
362 * Fast color clear can only clear to color values of 1.0 or 0.0. At the
363 * moment we only support floating point, unorm, and snorm buffers.
366 is_color_fast_clear_compatible(struct brw_context
*brw
,
368 const union gl_color_union
*color
)
370 if (_mesa_is_format_integer_color(format
)) {
372 perf_debug("Integer fast clear not enabled for (%s)",
373 _mesa_get_format_name(format
));
378 for (int i
= 0; i
< 4; i
++) {
379 if (!_mesa_format_has_color_component(format
, i
)) {
384 color
->f
[i
] != 0.0f
&& color
->f
[i
] != 1.0f
) {
392 * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
393 * SURFACE_STATE (DWORD 12-15 on SKL+).
396 set_fast_clear_color(struct brw_context
*brw
,
397 struct intel_mipmap_tree
*mt
,
398 const union gl_color_union
*color
)
400 union gl_color_union override_color
= *color
;
402 /* The sampler doesn't look at the format of the surface when the fast
403 * clear color is used so we need to implement luminance, intensity and
404 * missing components manually.
406 switch (_mesa_get_format_base_format(mt
->format
)) {
408 override_color
.ui
[3] = override_color
.ui
[0];
411 case GL_LUMINANCE_ALPHA
:
412 override_color
.ui
[1] = override_color
.ui
[0];
413 override_color
.ui
[2] = override_color
.ui
[0];
416 for (int i
= 0; i
< 3; i
++) {
417 if (!_mesa_format_has_color_component(mt
->format
, i
))
418 override_color
.ui
[i
] = 0;
423 if (!_mesa_format_has_color_component(mt
->format
, 3)) {
424 if (_mesa_is_format_integer_color(mt
->format
))
425 override_color
.ui
[3] = 1;
427 override_color
.f
[3] = 1.0f
;
430 /* Handle linear→SRGB conversion */
431 if (brw
->ctx
.Color
.sRGBEnabled
&&
432 _mesa_get_srgb_format_linear(mt
->format
) != mt
->format
) {
433 for (int i
= 0; i
< 3; i
++) {
434 override_color
.f
[i
] =
435 util_format_linear_to_srgb_float(override_color
.f
[i
]);
440 mt
->gen9_fast_clear_color
= override_color
;
442 mt
->fast_clear_color_value
= 0;
443 for (int i
= 0; i
< 4; i
++) {
444 /* Testing for non-0 works for integer and float colors */
445 if (override_color
.f
[i
] != 0.0f
) {
446 mt
->fast_clear_color_value
|=
447 1 << (GEN7_SURFACE_CLEAR_COLOR_SHIFT
+ (3 - i
));
453 static const uint32_t fast_clear_color
[4] = { ~0, ~0, ~0, ~0 };
456 set_fast_clear_op(struct brw_context
*brw
, uint32_t op
)
458 /* Set op and dirty BRW_NEW_FRAGMENT_PROGRAM to make sure we re-emit
461 brw
->wm
.fast_clear_op
= op
;
462 brw
->ctx
.NewDriverState
|= BRW_NEW_FRAGMENT_PROGRAM
;
466 use_rectlist(struct brw_context
*brw
, bool enable
)
468 /* Set custom state to let us use _3DPRIM_RECTLIST and the replicated
469 * rendertarget write. When we enable reclist mode, we disable the
470 * viewport transform, disable clipping, enable the rep16 write
471 * optimization and disable simd8 dispatch in the PS.
473 brw
->sf
.viewport_transform_enable
= !enable
;
474 brw
->use_rep_send
= enable
;
475 brw
->no_simd8
= enable
;
477 /* Dirty state to make sure we reemit the state packages affected by the
478 * custom state. We dirty BRW_NEW_FRAGMENT_PROGRAM to emit 3DSTATE_PS for
479 * disabling simd8 dispatch, _NEW_LIGHT to emit 3DSTATE_SF for disabling
480 * the viewport transform and 3DSTATE_CLIP to disable clipping for the
481 * reclist primitive. This is a little messy - it would be nicer to
482 * BRW_NEW_FAST_CLEAR flag or so, but we're out of brw state bits. Dirty
483 * _NEW_BUFFERS to make sure we emit new SURFACE_STATE with the new fast
486 brw
->NewGLState
|= _NEW_LIGHT
| _NEW_BUFFERS
;
487 brw
->ctx
.NewDriverState
|= BRW_NEW_FRAGMENT_PROGRAM
;
491 * Individually fast clear each color buffer attachment. On previous gens this
492 * isn't required. The motivation for this comes from one line (which seems to
493 * be specific to SKL+). The list item is in section titled _MCS Buffer for
496 * "Since only one RT is bound with a clear pass, only one RT can be cleared
497 * at a time. To clear multiple RTs, multiple clear passes are required."
499 * The code follows the same idea as the resolve code which creates a fake FBO
500 * to avoid interfering with too much of the GL state.
503 fast_clear_attachments(struct brw_context
*brw
,
504 struct gl_framebuffer
*fb
,
505 uint32_t fast_clear_buffers
,
506 struct rect fast_clear_rect
)
508 struct gl_context
*ctx
= &brw
->ctx
;
509 const bool srgb_enabled
= ctx
->Color
.sRGBEnabled
;
511 assert(brw
->gen
>= 9);
513 /* Make sure the GL_FRAMEBUFFER_SRGB is disabled during fast clear so that
514 * the surface state will always be uploaded with a linear buffer. SRGB
515 * buffers are not supported on Gen9 because they are not marked as
516 * losslessly compressible. This shouldn't matter for the fast clear
517 * because the color is not written to the framebuffer yet so the hardware
518 * doesn't need to do any SRGB conversion.
521 _mesa_set_framebuffer_srgb(ctx
, GL_FALSE
);
523 brw_bind_rep_write_shader(brw
, (float *) fast_clear_color
);
525 /* SKL+ also has a resolve mode for compressed render targets and thus more
526 * bits to let us select the type of resolve. For fast clear resolves, it
527 * turns out we can use the same value as pre-SKL though.
529 set_fast_clear_op(brw
, GEN7_PS_RENDER_TARGET_FAST_CLEAR_ENABLE
);
531 while (fast_clear_buffers
) {
532 int index
= ffs(fast_clear_buffers
) - 1;
534 fast_clear_buffers
&= ~(1 << index
);
536 _mesa_meta_drawbuffers_from_bitfield(1 << index
);
538 brw_draw_rectlist(brw
, &fast_clear_rect
, MAX2(1, fb
->MaxNumLayers
));
540 /* Now set the mcs we cleared to INTEL_FAST_CLEAR_STATE_CLEAR so we'll
541 * resolve them eventually.
543 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[0];
544 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
545 irb
->mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_CLEAR
;
548 set_fast_clear_op(brw
, 0);
551 _mesa_set_framebuffer_srgb(ctx
, GL_TRUE
);
555 brw_meta_fast_clear(struct brw_context
*brw
, struct gl_framebuffer
*fb
,
556 GLbitfield buffers
, bool partial_clear
)
558 struct gl_context
*ctx
= &brw
->ctx
;
560 enum { FAST_CLEAR
, REP_CLEAR
, PLAIN_CLEAR
} clear_type
;
561 GLbitfield plain_clear_buffers
, meta_save
, rep_clear_buffers
, fast_clear_buffers
;
562 struct rect fast_clear_rect
, clear_rect
;
565 fast_clear_buffers
= rep_clear_buffers
= plain_clear_buffers
= 0;
567 /* First we loop through the color draw buffers and determine which ones
568 * can be fast cleared, which ones can use the replicated write and which
569 * ones have to fall back to regular color clear.
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 int index
= fb
->_ColorDrawBufferIndexes
[buf
];
576 /* Only clear the buffers present in the provided mask */
577 if (((1 << index
) & buffers
) == 0)
580 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
581 * the framebuffer can be complete with some attachments missing. In
582 * this case the _ColorDrawBuffers pointer will be NULL.
587 clear_type
= FAST_CLEAR
;
589 /* We don't have fast clear until gen7. */
591 clear_type
= REP_CLEAR
;
593 /* If we're mapping the render format to a different format than the
594 * format we use for texturing then it is a bit questionable whether it
595 * should be possible to use a fast clear. Although we only actually
596 * render using a renderable format, without the override workaround it
597 * wouldn't be possible to have a non-renderable surface in a fast clear
598 * state so the hardware probably legitimately doesn't need to support
599 * this case. At least on Gen9 this really does seem to cause problems.
602 brw_format_for_mesa_format(irb
->mt
->format
) !=
603 brw
->render_target_format
[irb
->mt
->format
])
604 clear_type
= REP_CLEAR
;
606 /* Gen9 doesn't support fast clear on single-sampled SRGB buffers. When
607 * GL_FRAMEBUFFER_SRGB is enabled any color renderbuffers will be
608 * resolved in intel_update_state. In that case it's pointless to do a
609 * fast clear because it's very likely to be immediately resolved.
612 irb
->mt
->num_samples
<= 1 &&
613 brw
->ctx
.Color
.sRGBEnabled
&&
614 _mesa_get_srgb_format_linear(irb
->mt
->format
) != irb
->mt
->format
)
615 clear_type
= REP_CLEAR
;
617 if (irb
->mt
->fast_clear_state
== INTEL_FAST_CLEAR_STATE_NO_MCS
)
618 clear_type
= REP_CLEAR
;
620 /* We can't do scissored fast clears because of the restrictions on the
621 * fast clear rectangle size.
624 clear_type
= REP_CLEAR
;
626 /* Fast clear is only supported for colors where all components are
629 format
= _mesa_get_render_format(ctx
, irb
->mt
->format
);
630 if (!is_color_fast_clear_compatible(brw
, format
, &ctx
->Color
.ClearColor
))
631 clear_type
= REP_CLEAR
;
633 /* From the SNB PRM (Vol4_Part1):
635 * "Replicated data (Message Type = 111) is only supported when
636 * accessing tiled memory. Using this Message Type to access
637 * linear (untiled) memory is UNDEFINED."
639 if (irb
->mt
->tiling
== I915_TILING_NONE
) {
640 perf_debug("Falling back to plain clear because %dx%d buffer is untiled\n",
641 irb
->mt
->logical_width0
, irb
->mt
->logical_height0
);
642 clear_type
= PLAIN_CLEAR
;
645 /* Constant color writes ignore everything in blend and color calculator
646 * state. This is not documented.
648 GLubyte
*color_mask
= ctx
->Color
.ColorMask
[buf
];
649 for (int i
= 0; i
< 4; i
++) {
650 if (_mesa_format_has_color_component(irb
->mt
->format
, i
) &&
652 perf_debug("Falling back to plain clear on %dx%d buffer because of color mask\n",
653 irb
->mt
->logical_width0
, irb
->mt
->logical_height0
);
654 clear_type
= PLAIN_CLEAR
;
658 /* Allocate the MCS for non MSRT surfaces now if we're doing a fast
659 * clear and we don't have the MCS yet. On failure, fall back to
662 if (clear_type
== FAST_CLEAR
&& irb
->mt
->mcs_mt
== NULL
)
663 if (!intel_miptree_alloc_non_msrt_mcs(brw
, irb
->mt
))
664 clear_type
= REP_CLEAR
;
666 switch (clear_type
) {
668 set_fast_clear_color(brw
, irb
->mt
, &ctx
->Color
.ClearColor
);
669 irb
->need_downsample
= true;
671 /* If the buffer is already in INTEL_FAST_CLEAR_STATE_CLEAR, the
672 * clear is redundant and can be skipped. Only skip after we've
673 * updated the fast clear color above though.
675 if (irb
->mt
->fast_clear_state
== INTEL_FAST_CLEAR_STATE_CLEAR
)
678 /* Set fast_clear_state to RESOLVED so we don't try resolve them when
679 * we draw, in case the mt is also bound as a texture.
681 irb
->mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_RESOLVED
;
682 irb
->need_downsample
= true;
683 fast_clear_buffers
|= 1 << index
;
684 get_fast_clear_rect(brw
, fb
, irb
, &fast_clear_rect
);
688 rep_clear_buffers
|= 1 << index
;
689 get_buffer_rect(fb
, &clear_rect
);
693 plain_clear_buffers
|= 1 << index
;
694 get_buffer_rect(fb
, &clear_rect
);
699 assert((fast_clear_buffers
& rep_clear_buffers
) == 0);
701 if (!(fast_clear_buffers
| rep_clear_buffers
)) {
702 if (plain_clear_buffers
)
703 /* If we only have plain clears, skip the meta save/restore. */
706 /* Nothing left to do. This happens when we hit the redundant fast
707 * clear case above and nothing else.
713 MESA_META_ALPHA_TEST
|
715 MESA_META_DEPTH_TEST
|
716 MESA_META_RASTERIZATION
|
718 MESA_META_STENCIL_TEST
|
722 MESA_META_CLAMP_FRAGMENT_COLOR
|
723 MESA_META_MULTISAMPLE
|
724 MESA_META_OCCLUSION_QUERY
|
725 MESA_META_DRAW_BUFFERS
;
727 _mesa_meta_begin(ctx
, meta_save
);
729 if (!brw_fast_clear_init(brw
)) {
730 /* This is going to be hard to recover from, most likely out of memory.
731 * Bail and let meta try and (probably) fail for us.
733 plain_clear_buffers
= buffers
;
737 /* Clears never have the color clamped. */
738 if (ctx
->Extensions
.ARB_color_buffer_float
)
739 _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR
, GL_FALSE
);
741 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
742 _mesa_DepthMask(GL_FALSE
);
743 _mesa_set_enable(ctx
, GL_STENCIL_TEST
, GL_FALSE
);
745 use_rectlist(brw
, true);
747 layers
= MAX2(1, fb
->MaxNumLayers
);
749 if (brw
->gen
>= 9 && fast_clear_buffers
) {
750 fast_clear_attachments(brw
, fb
, fast_clear_buffers
, fast_clear_rect
);
751 } else if (fast_clear_buffers
) {
752 _mesa_meta_drawbuffers_from_bitfield(fast_clear_buffers
);
753 brw_bind_rep_write_shader(brw
, (float *) fast_clear_color
);
754 set_fast_clear_op(brw
, GEN7_PS_RENDER_TARGET_FAST_CLEAR_ENABLE
);
755 brw_draw_rectlist(brw
, &fast_clear_rect
, layers
);
756 set_fast_clear_op(brw
, 0);
758 /* Now set the mcs we cleared to INTEL_FAST_CLEAR_STATE_CLEAR so we'll
759 * resolve them eventually.
761 for (unsigned buf
= 0; buf
< fb
->_NumColorDrawBuffers
; buf
++) {
762 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
763 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
764 int index
= fb
->_ColorDrawBufferIndexes
[buf
];
766 if ((1 << index
) & fast_clear_buffers
)
767 irb
->mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_CLEAR
;
771 if (rep_clear_buffers
) {
772 _mesa_meta_drawbuffers_from_bitfield(rep_clear_buffers
);
773 brw_bind_rep_write_shader(brw
, ctx
->Color
.ClearColor
.f
);
774 brw_draw_rectlist(brw
, &clear_rect
, layers
);
778 /* Dirty _NEW_BUFFERS so we reemit SURFACE_STATE which sets the fast clear
779 * color before resolve and sets irb->mt->fast_clear_state to UNRESOLVED if
782 brw
->NewGLState
|= _NEW_BUFFERS
;
785 /* Set the custom state back to normal and dirty the same bits as above */
786 use_rectlist(brw
, false);
790 /* From BSpec: Render Target Fast Clear:
792 * After Render target fast clear, pipe-control with color cache
793 * write-flush must be issued before sending any DRAW commands on that
796 brw_emit_mi_flush(brw
);
798 /* If we had to fall back to plain clear for any buffers, clear those now
799 * by calling into meta.
802 if (plain_clear_buffers
)
803 _mesa_meta_glsl_Clear(&brw
->ctx
, plain_clear_buffers
);
809 get_resolve_rect(struct brw_context
*brw
,
810 struct intel_mipmap_tree
*mt
, struct rect
*rect
)
812 unsigned x_align
, y_align
;
813 unsigned x_scaledown
, y_scaledown
;
815 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
817 * A rectangle primitive must be scaled down by the following factors
818 * with respect to render target being resolved.
820 * The scaledown factors in the table that follows are related to the
821 * alignment size returned by intel_get_non_msrt_mcs_alignment() by a
822 * multiplier. For IVB and HSW, we divide by two, for BDW we multiply
823 * by 8 and 16. Similar to the fast clear, SKL eases the BDW vertical scaling
827 intel_get_non_msrt_mcs_alignment(mt
, &x_align
, &y_align
);
829 x_scaledown
= x_align
* 8;
830 y_scaledown
= y_align
* 8;
831 } else if (brw
->gen
>= 8) {
832 x_scaledown
= x_align
* 8;
833 y_scaledown
= y_align
* 16;
835 x_scaledown
= x_align
/ 2;
836 y_scaledown
= y_align
/ 2;
838 rect
->x0
= rect
->y0
= 0;
839 rect
->x1
= ALIGN(mt
->logical_width0
, x_scaledown
) / x_scaledown
;
840 rect
->y1
= ALIGN(mt
->logical_height0
, y_scaledown
) / y_scaledown
;
844 brw_meta_resolve_color(struct brw_context
*brw
,
845 struct intel_mipmap_tree
*mt
)
847 struct gl_context
*ctx
= &brw
->ctx
;
851 brw_emit_mi_flush(brw
);
853 _mesa_meta_begin(ctx
, MESA_META_ALL
);
855 _mesa_GenFramebuffers(1, &fbo
);
856 rbo
= brw_get_rb_for_slice(brw
, mt
, 0, 0, false);
858 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbo
);
859 _mesa_FramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER
,
860 GL_COLOR_ATTACHMENT0
,
861 GL_RENDERBUFFER
, rbo
);
862 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0
);
864 brw_fast_clear_init(brw
);
866 use_rectlist(brw
, true);
868 brw_bind_rep_write_shader(brw
, (float *) fast_clear_color
);
870 /* SKL+ also has a resolve mode for compressed render targets and thus more
871 * bits to let us select the type of resolve. For fast clear resolves, it
872 * turns out we can use the same value as pre-SKL though.
874 set_fast_clear_op(brw
, GEN7_PS_RENDER_TARGET_RESOLVE_ENABLE
);
876 mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_RESOLVED
;
877 get_resolve_rect(brw
, mt
, &rect
);
879 brw_draw_rectlist(brw
, &rect
, 1);
881 set_fast_clear_op(brw
, 0);
882 use_rectlist(brw
, false);
884 _mesa_DeleteRenderbuffers(1, &rbo
);
885 _mesa_DeleteFramebuffers(1, &fbo
);
889 /* We're typically called from intel_update_state() and we're supposed to
890 * return with the state all updated to what it was before
891 * brw_meta_resolve_color() was called. The meta rendering will have
892 * messed up the state and we need to call _mesa_update_state() again to
893 * get back to where we were supposed to be when resolve was called.
896 _mesa_update_state(ctx
);