2 * Copyright © 2013 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
25 #include "main/teximage.h"
26 #include "main/blend.h"
27 #include "main/fbobject.h"
28 #include "main/renderbuffer.h"
31 #include "util/ralloc.h"
33 #include "intel_fbo.h"
35 #include "brw_blorp.h"
36 #include "brw_context.h"
38 #include "brw_state.h"
40 #define FILE_DEBUG_FLAG DEBUG_BLORP
42 struct brw_blorp_const_color_prog_key
44 bool use_simd16_replicated_data
;
49 * Parameters for a blorp operation where the fragment shader outputs a
50 * constant color. This is used for both fast color clears and color
53 class brw_blorp_const_color_params
: public brw_blorp_params
56 virtual uint32_t get_wm_prog(struct brw_context
*brw
,
57 brw_blorp_prog_data
**prog_data
) const;
59 brw_blorp_const_color_prog_key wm_prog_key
;
62 class brw_blorp_clear_params
: public brw_blorp_const_color_params
65 brw_blorp_clear_params(struct brw_context
*brw
,
66 struct gl_framebuffer
*fb
,
67 struct gl_renderbuffer
*rb
,
75 * Parameters for a blorp operation that performs a "render target resolve".
76 * This is used to resolve pending fast clear pixels before a color buffer is
77 * used for texturing, ReadPixels, or scanout.
79 class brw_blorp_rt_resolve_params
: public brw_blorp_const_color_params
82 brw_blorp_rt_resolve_params(struct brw_context
*brw
,
83 struct intel_mipmap_tree
*mt
);
87 class brw_blorp_const_color_program
90 brw_blorp_const_color_program(struct brw_context
*brw
,
91 const brw_blorp_const_color_prog_key
*key
);
92 ~brw_blorp_const_color_program();
94 const GLuint
*compile(struct brw_context
*brw
, GLuint
*program_size
);
96 brw_blorp_prog_data prog_data
;
102 const brw_blorp_const_color_prog_key
*key
;
103 struct brw_compile func
;
105 /* Thread dispatch header */
108 /* Pixel X/Y coordinates (always in R1). */
111 /* Register with push constants (a single vec4) */
112 struct brw_reg clear_rgba
;
114 /* MRF used for render target writes */
118 brw_blorp_const_color_program::brw_blorp_const_color_program(
119 struct brw_context
*brw
,
120 const brw_blorp_const_color_prog_key
*key
)
121 : mem_ctx(ralloc_context(NULL
)),
128 prog_data
.first_curbe_grf
= 0;
129 prog_data
.persample_msaa_dispatch
= false;
130 brw_init_compile(brw
, &func
, mem_ctx
);
133 brw_blorp_const_color_program::~brw_blorp_const_color_program()
135 ralloc_free(mem_ctx
);
140 * Determine if fast color clear supports the given clear color.
142 * Fast color clear can only clear to color values of 1.0 or 0.0. At the
143 * moment we only support floating point, unorm, and snorm buffers.
146 is_color_fast_clear_compatible(struct brw_context
*brw
,
148 const union gl_color_union
*color
)
150 if (_mesa_is_format_integer_color(format
))
153 for (int i
= 0; i
< 4; i
++) {
154 if (color
->f
[i
] != 0.0 && color
->f
[i
] != 1.0 &&
155 _mesa_format_has_color_component(format
, i
)) {
164 * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
168 compute_fast_clear_color_bits(const union gl_color_union
*color
)
171 for (int i
= 0; i
< 4; i
++) {
172 if (color
->f
[i
] != 0.0)
173 bits
|= 1 << (GEN7_SURFACE_CLEAR_COLOR_SHIFT
+ (3 - i
));
179 brw_blorp_clear_params::brw_blorp_clear_params(struct brw_context
*brw
,
180 struct gl_framebuffer
*fb
,
181 struct gl_renderbuffer
*rb
,
186 struct gl_context
*ctx
= &brw
->ctx
;
187 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
189 dst
.set(brw
, irb
->mt
, irb
->mt_level
, layer
, true);
191 /* Override the surface format according to the context's sRGB rules. */
192 mesa_format format
= _mesa_get_render_format(ctx
, irb
->mt
->format
);
193 dst
.brw_surfaceformat
= brw
->render_target_format
[format
];
201 y0
= rb
->Height
- fb
->_Ymax
;
202 y1
= rb
->Height
- fb
->_Ymin
;
205 memcpy(&wm_push_consts
.dst_x0
, ctx
->Color
.ClearColor
.f
, sizeof(float) * 4);
209 memset(&wm_prog_key
, 0, sizeof(wm_prog_key
));
211 wm_prog_key
.use_simd16_replicated_data
= true;
213 /* From the SNB PRM (Vol4_Part1):
215 * "Replicated data (Message Type = 111) is only supported when
216 * accessing tiled memory. Using this Message Type to access linear
217 * (untiled) memory is UNDEFINED."
219 if (irb
->mt
->tiling
== I915_TILING_NONE
)
220 wm_prog_key
.use_simd16_replicated_data
= false;
222 /* Constant color writes ignore everyting in blend and color calculator
223 * state. This is not documented.
225 for (int i
= 0; i
< 4; i
++) {
226 if (_mesa_format_has_color_component(irb
->mt
->format
, i
) &&
228 color_write_disable
[i
] = true;
229 wm_prog_key
.use_simd16_replicated_data
= false;
233 /* If we can do this as a fast color clear, do so.
235 * Note that the condition "!partial_clear" means we only try to do full
236 * buffer clears using fast color clear logic. This is necessary because
237 * the fast color clear alignment requirements mean that we typically have
238 * to clear a larger rectangle than (x0, y0) to (x1, y1). Restricting fast
239 * color clears to the full-buffer condition guarantees that the extra
240 * memory locations that get written to are outside the image boundary (and
241 * hence irrelevant). Note that the rectangle alignment requirements are
242 * never larger than the size of a tile, so there is no danger of
243 * overflowing beyond the memory belonging to the region.
245 if (irb
->mt
->fast_clear_state
!= INTEL_FAST_CLEAR_STATE_NO_MCS
&&
246 !partial_clear
&& wm_prog_key
.use_simd16_replicated_data
&&
247 is_color_fast_clear_compatible(brw
, format
, &ctx
->Color
.ClearColor
)) {
248 memset(&wm_push_consts
, 0xff, 4*sizeof(float));
249 fast_clear_op
= GEN7_FAST_CLEAR_OP_FAST_CLEAR
;
251 /* Figure out what the clear rectangle needs to be aligned to, and how
252 * much it needs to be scaled down.
254 unsigned x_align
, y_align
, x_scaledown
, y_scaledown
;
256 if (irb
->mt
->msaa_layout
== INTEL_MSAA_LAYOUT_NONE
) {
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 * Clear pass must have a clear rectangle that must follow
261 * alignment rules in terms of pixels and lines as shown in the
262 * table below. Further, the clear-rectangle height and width
263 * must be multiple of the following dimensions. If the height
264 * and width of the render target being cleared do not meet these
265 * requirements, an MCS buffer can be created such that it
266 * follows the requirement and covers the RT.
268 * The alignment size in the table that follows is related to the
269 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but
270 * with X alignment multiplied by 16 and Y alignment multiplied by 32.
272 intel_get_non_msrt_mcs_alignment(brw
, irb
->mt
, &x_align
, &y_align
);
276 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
277 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
279 * In order to optimize the performance MCS buffer (when bound to
280 * 1X RT) clear similarly to MCS buffer clear for MSRT case,
281 * clear rect is required to be scaled by the following factors
282 * in the horizontal and vertical directions:
284 * The X and Y scale down factors in the table that follows are each
285 * equal to half the alignment value computed above.
287 x_scaledown
= x_align
/ 2;
288 y_scaledown
= y_align
/ 2;
290 /* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel
291 * Backend > MCS Buffer for Render Target(s) [DevIVB+] > Table "Color
292 * Clear of Non-MultiSampled Render Target Restrictions":
294 * Clear rectangle must be aligned to two times the number of
295 * pixels in the table shown below due to 16x16 hashing across the
301 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
302 * Target(s)", beneath the "MSAA Compression" bullet (p326):
304 * Clear pass for this case requires that scaled down primitive
305 * is sent down with upper left co-ordinate to coincide with
306 * actual rectangle being cleared. For MSAA, clear rectangle’s
307 * height and width need to as show in the following table in
308 * terms of (width,height) of the RT.
310 * MSAA Width of Clear Rect Height of Clear Rect
311 * 4X Ceil(1/8*width) Ceil(1/2*height)
312 * 8X Ceil(1/2*width) Ceil(1/2*height)
314 * The text "with upper left co-ordinate to coincide with actual
315 * rectangle being cleared" is a little confusing--it seems to imply
316 * that to clear a rectangle from (x,y) to (x+w,y+h), one needs to
317 * feed the pipeline using the rectangle (x,y) to
318 * (x+Ceil(w/N),y+Ceil(h/2)), where N is either 2 or 8 depending on
319 * the number of samples. Experiments indicate that this is not
320 * quite correct; actually, what the hardware appears to do is to
321 * align whatever rectangle is sent down the pipeline to the nearest
322 * multiple of 2x2 blocks, and then scale it up by a factor of N
323 * horizontally and 2 vertically. So the resulting alignment is 4
324 * vertically and either 4 or 16 horizontally, and the scaledown
325 * factor is 2 vertically and either 2 or 8 horizontally.
327 switch (irb
->mt
->num_samples
) {
335 unreachable("Unexpected sample count for fast clear");
338 x_align
= x_scaledown
* 2;
339 y_align
= y_scaledown
* 2;
342 /* Do the alignment and scaledown. */
343 x0
= ROUND_DOWN_TO(x0
, x_align
) / x_scaledown
;
344 y0
= ROUND_DOWN_TO(y0
, y_align
) / y_scaledown
;
345 x1
= ALIGN(x1
, x_align
) / x_scaledown
;
346 y1
= ALIGN(y1
, y_align
) / y_scaledown
;
351 brw_blorp_rt_resolve_params::brw_blorp_rt_resolve_params(
352 struct brw_context
*brw
,
353 struct intel_mipmap_tree
*mt
)
355 dst
.set(brw
, mt
, 0 /* level */, 0 /* layer */, true);
357 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
359 * A rectangle primitive must be scaled down by the following factors
360 * with respect to render target being resolved.
362 * The scaledown factors in the table that follows are related to the
363 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but with
364 * X and Y alignment each divided by 2.
366 unsigned x_align
, y_align
;
367 intel_get_non_msrt_mcs_alignment(brw
, mt
, &x_align
, &y_align
);
368 unsigned x_scaledown
= x_align
/ 2;
369 unsigned y_scaledown
= y_align
/ 2;
371 x1
= ALIGN(mt
->logical_width0
, x_scaledown
) / x_scaledown
;
372 y1
= ALIGN(mt
->logical_height0
, y_scaledown
) / y_scaledown
;
374 fast_clear_op
= GEN7_FAST_CLEAR_OP_RESOLVE
;
376 /* Note: there is no need to initialize push constants because it doesn't
377 * matter what data gets dispatched to the render target. However, we must
378 * ensure that the fragment shader delivers the data using the "replicated
382 memset(&wm_prog_key
, 0, sizeof(wm_prog_key
));
383 wm_prog_key
.use_simd16_replicated_data
= true;
388 brw_blorp_const_color_params::get_wm_prog(struct brw_context
*brw
,
389 brw_blorp_prog_data
**prog_data
)
392 uint32_t prog_offset
= 0;
393 if (!brw_search_cache(&brw
->cache
, BRW_BLORP_CONST_COLOR_PROG
,
394 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
395 &prog_offset
, prog_data
)) {
396 brw_blorp_const_color_program
prog(brw
, &this->wm_prog_key
);
398 const GLuint
*program
= prog
.compile(brw
, &program_size
);
399 brw_upload_cache(&brw
->cache
, BRW_BLORP_CONST_COLOR_PROG
,
400 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
401 program
, program_size
,
402 &prog
.prog_data
, sizeof(prog
.prog_data
),
403 &prog_offset
, prog_data
);
409 brw_blorp_const_color_program::alloc_regs()
412 this->R0
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
413 this->R1
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
415 prog_data
.first_curbe_grf
= reg
;
416 clear_rgba
= retype(brw_vec4_grf(reg
++, 0), BRW_REGISTER_TYPE_F
);
417 reg
+= BRW_BLORP_NUM_PUSH_CONST_REGS
;
419 /* Make sure we didn't run out of registers */
420 assert(reg
<= GEN7_MRF_HACK_START
);
426 brw_blorp_const_color_program::compile(struct brw_context
*brw
,
427 GLuint
*program_size
)
429 /* Set up prog_data */
430 memset(&prog_data
, 0, sizeof(prog_data
));
431 prog_data
.persample_msaa_dispatch
= false;
435 brw_set_default_compression_control(&func
, BRW_COMPRESSION_NONE
);
437 struct brw_reg mrf_rt_write
=
438 retype(vec16(brw_message_reg(base_mrf
)), BRW_REGISTER_TYPE_F
);
440 uint32_t mlen
, msg_type
;
441 if (key
->use_simd16_replicated_data
) {
442 /* The message payload is a single register with the low 4 floats/ints
443 * filled with the constant clear color.
445 brw_set_default_mask_control(&func
, BRW_MASK_DISABLE
);
446 brw_MOV(&func
, vec4(brw_message_reg(base_mrf
)), clear_rgba
);
447 brw_set_default_mask_control(&func
, BRW_MASK_ENABLE
);
449 msg_type
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED
;
452 for (int i
= 0; i
< 4; i
++) {
453 /* The message payload is pairs of registers for 16 pixels each of r,
456 brw_set_default_compression_control(&func
, BRW_COMPRESSION_COMPRESSED
);
458 brw_message_reg(base_mrf
+ i
* 2),
459 brw_vec1_grf(clear_rgba
.nr
, i
));
460 brw_set_default_compression_control(&func
, BRW_COMPRESSION_NONE
);
463 msg_type
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE
;
467 /* Now write to the render target and terminate the thread */
469 16 /* dispatch_width */,
470 base_mrf
/* msg_reg_nr */,
471 mrf_rt_write
/* src0 */,
473 BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX
,
475 0 /* response_length */,
477 false /* header present */);
479 if (unlikely(INTEL_DEBUG
& DEBUG_BLORP
)) {
480 fprintf(stderr
, "Native code for BLORP clear:\n");
481 brw_disassemble(brw
, func
.store
, 0, func
.next_insn_offset
, stderr
);
482 fprintf(stderr
, "\n");
485 brw_compact_instructions(&func
, 0, 0, NULL
);
486 return brw_get_program(&func
, program_size
);
491 do_single_blorp_clear(struct brw_context
*brw
, struct gl_framebuffer
*fb
,
492 struct gl_renderbuffer
*rb
, unsigned buf
,
493 bool partial_clear
, unsigned layer
)
495 struct gl_context
*ctx
= &brw
->ctx
;
496 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
498 brw_blorp_clear_params
params(brw
, fb
, rb
, ctx
->Color
.ColorMask
[buf
],
499 partial_clear
, layer
);
502 (params
.fast_clear_op
== GEN7_FAST_CLEAR_OP_FAST_CLEAR
);
504 /* Record the clear color in the miptree so that it will be
505 * programmed in SURFACE_STATE by later rendering and resolve
508 uint32_t new_color_value
=
509 compute_fast_clear_color_bits(&ctx
->Color
.ClearColor
);
510 if (irb
->mt
->fast_clear_color_value
!= new_color_value
) {
511 irb
->mt
->fast_clear_color_value
= new_color_value
;
512 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
515 /* If the buffer is already in INTEL_FAST_CLEAR_STATE_CLEAR, the clear
516 * is redundant and can be skipped.
518 if (irb
->mt
->fast_clear_state
== INTEL_FAST_CLEAR_STATE_CLEAR
)
521 /* If the MCS buffer hasn't been allocated yet, we need to allocate
524 if (!irb
->mt
->mcs_mt
) {
525 if (!intel_miptree_alloc_non_msrt_mcs(brw
, irb
->mt
)) {
526 /* MCS allocation failed--probably this will only happen in
527 * out-of-memory conditions. But in any case, try to recover
528 * by falling back to a non-blorp clear technique.
532 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
536 const char *clear_type
;
539 else if (params
.wm_prog_key
.use_simd16_replicated_data
)
540 clear_type
= "replicated";
544 DBG("%s (%s) to mt %p level %d layer %d\n", __FUNCTION__
, clear_type
,
545 irb
->mt
, irb
->mt_level
, irb
->mt_layer
);
547 brw_blorp_exec(brw
, ¶ms
);
550 /* Now that the fast clear has occurred, put the buffer in
551 * INTEL_FAST_CLEAR_STATE_CLEAR so that we won't waste time doing
554 irb
->mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_CLEAR
;
563 brw_blorp_clear_color(struct brw_context
*brw
, struct gl_framebuffer
*fb
,
564 GLbitfield mask
, bool partial_clear
)
566 for (unsigned buf
= 0; buf
< fb
->_NumColorDrawBuffers
; buf
++) {
567 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
568 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
570 /* Only clear the buffers present in the provided mask */
571 if (((1 << fb
->_ColorDrawBufferIndexes
[buf
]) & mask
) == 0)
574 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
575 * the framebuffer can be complete with some attachments missing. In
576 * this case the _ColorDrawBuffers pointer will be NULL.
581 if (fb
->MaxNumLayers
> 0) {
582 unsigned layer_multiplier
=
583 (irb
->mt
->msaa_layout
== INTEL_MSAA_LAYOUT_UMS
||
584 irb
->mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
) ?
585 irb
->mt
->num_samples
: 1;
586 unsigned num_layers
= irb
->layer_count
;
587 for (unsigned layer
= 0; layer
< num_layers
; layer
++) {
588 if (!do_single_blorp_clear(brw
, fb
, rb
, buf
, partial_clear
,
589 irb
->mt_layer
+ layer
* layer_multiplier
)) {
594 unsigned layer
= irb
->mt_layer
;
595 if (!do_single_blorp_clear(brw
, fb
, rb
, buf
, partial_clear
, layer
))
599 irb
->need_downsample
= true;
606 brw_blorp_resolve_color(struct brw_context
*brw
, struct intel_mipmap_tree
*mt
)
608 DBG("%s to mt %p\n", __FUNCTION__
, mt
);
610 brw_blorp_rt_resolve_params
params(brw
, mt
);
611 brw_blorp_exec(brw
, ¶ms
);
612 mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_RESOLVED
;