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 "glsl/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;
60 brw_blorp_const_color_prog_key wm_prog_key
;
63 class brw_blorp_clear_params
: public brw_blorp_const_color_params
66 brw_blorp_clear_params(struct brw_context
*brw
,
67 struct gl_framebuffer
*fb
,
68 struct gl_renderbuffer
*rb
,
76 * Parameters for a blorp operation that performs a "render target resolve".
77 * This is used to resolve pending fast clear pixels before a color buffer is
78 * used for texturing, ReadPixels, or scanout.
80 class brw_blorp_rt_resolve_params
: public brw_blorp_const_color_params
83 brw_blorp_rt_resolve_params(struct brw_context
*brw
,
84 struct intel_mipmap_tree
*mt
);
88 class brw_blorp_const_color_program
91 brw_blorp_const_color_program(struct brw_context
*brw
,
92 const brw_blorp_const_color_prog_key
*key
);
93 ~brw_blorp_const_color_program();
95 const GLuint
*compile(struct brw_context
*brw
, GLuint
*program_size
);
97 brw_blorp_prog_data prog_data
;
103 struct brw_context
*brw
;
104 const brw_blorp_const_color_prog_key
*key
;
105 struct brw_compile func
;
107 /* Thread dispatch header */
110 /* Pixel X/Y coordinates (always in R1). */
113 /* Register with push constants (a single vec4) */
114 struct brw_reg clear_rgba
;
116 /* MRF used for render target writes */
120 brw_blorp_const_color_program::brw_blorp_const_color_program(
121 struct brw_context
*brw
,
122 const brw_blorp_const_color_prog_key
*key
)
123 : mem_ctx(ralloc_context(NULL
)),
131 prog_data
.first_curbe_grf
= 0;
132 prog_data
.persample_msaa_dispatch
= false;
133 brw_init_compile(brw
, &func
, mem_ctx
);
136 brw_blorp_const_color_program::~brw_blorp_const_color_program()
138 ralloc_free(mem_ctx
);
143 * Determine if fast color clear supports the given clear color.
145 * Fast color clear can only clear to color values of 1.0 or 0.0. At the
146 * moment we only support floating point, unorm, and snorm buffers.
149 is_color_fast_clear_compatible(struct brw_context
*brw
,
151 const union gl_color_union
*color
)
153 if (_mesa_is_format_integer_color(format
))
156 for (int i
= 0; i
< 4; i
++) {
157 if (color
->f
[i
] != 0.0 && color
->f
[i
] != 1.0) {
158 perf_debug("Clear color unsupported by fast color clear. "
159 "Falling back to slow clear.\n");
168 * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
172 compute_fast_clear_color_bits(const union gl_color_union
*color
)
175 for (int i
= 0; i
< 4; i
++) {
176 if (color
->f
[i
] != 0.0)
177 bits
|= 1 << (GEN7_SURFACE_CLEAR_COLOR_SHIFT
+ (3 - i
));
183 brw_blorp_clear_params::brw_blorp_clear_params(struct brw_context
*brw
,
184 struct gl_framebuffer
*fb
,
185 struct gl_renderbuffer
*rb
,
190 struct gl_context
*ctx
= &brw
->ctx
;
191 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
193 dst
.set(brw
, irb
->mt
, irb
->mt_level
, layer
, true);
195 /* Override the surface format according to the context's sRGB rules. */
196 mesa_format format
= _mesa_get_render_format(ctx
, irb
->mt
->format
);
197 dst
.brw_surfaceformat
= brw
->render_target_format
[format
];
205 y0
= rb
->Height
- fb
->_Ymax
;
206 y1
= rb
->Height
- fb
->_Ymin
;
209 float *push_consts
= (float *)&wm_push_consts
;
211 push_consts
[0] = ctx
->Color
.ClearColor
.f
[0];
212 push_consts
[1] = ctx
->Color
.ClearColor
.f
[1];
213 push_consts
[2] = ctx
->Color
.ClearColor
.f
[2];
214 push_consts
[3] = ctx
->Color
.ClearColor
.f
[3];
218 memset(&wm_prog_key
, 0, sizeof(wm_prog_key
));
220 wm_prog_key
.use_simd16_replicated_data
= true;
222 /* From the SNB PRM (Vol4_Part1):
224 * "Replicated data (Message Type = 111) is only supported when
225 * accessing tiled memory. Using this Message Type to access linear
226 * (untiled) memory is UNDEFINED."
228 if (irb
->mt
->region
->tiling
== I915_TILING_NONE
)
229 wm_prog_key
.use_simd16_replicated_data
= false;
231 /* Constant color writes ignore everyting in blend and color calculator
232 * state. This is not documented.
234 for (int i
= 0; i
< 4; i
++) {
235 if (!color_mask
[i
]) {
236 color_write_disable
[i
] = true;
237 wm_prog_key
.use_simd16_replicated_data
= false;
241 /* If we can do this as a fast color clear, do so.
243 * Note that the condition "!partial_clear" means we only try to do full
244 * buffer clears using fast color clear logic. This is necessary because
245 * the fast color clear alignment requirements mean that we typically have
246 * to clear a larger rectangle than (x0, y0) to (x1, y1). Restricting fast
247 * color clears to the full-buffer condition guarantees that the extra
248 * memory locations that get written to are outside the image boundary (and
249 * hence irrelevant). Note that the rectangle alignment requirements are
250 * never larger than the size of a tile, so there is no danger of
251 * overflowing beyond the memory belonging to the region.
253 if (irb
->mt
->fast_clear_state
!= INTEL_FAST_CLEAR_STATE_NO_MCS
&&
254 !partial_clear
&& wm_prog_key
.use_simd16_replicated_data
&&
255 is_color_fast_clear_compatible(brw
, format
, &ctx
->Color
.ClearColor
)) {
256 memset(push_consts
, 0xff, 4*sizeof(float));
257 fast_clear_op
= GEN7_FAST_CLEAR_OP_FAST_CLEAR
;
259 /* Figure out what the clear rectangle needs to be aligned to, and how
260 * much it needs to be scaled down.
262 unsigned x_align
, y_align
, x_scaledown
, y_scaledown
;
264 if (irb
->mt
->msaa_layout
== INTEL_MSAA_LAYOUT_NONE
) {
265 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
266 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
268 * Clear pass must have a clear rectangle that must follow
269 * alignment rules in terms of pixels and lines as shown in the
270 * table below. Further, the clear-rectangle height and width
271 * must be multiple of the following dimensions. If the height
272 * and width of the render target being cleared do not meet these
273 * requirements, an MCS buffer can be created such that it
274 * follows the requirement and covers the RT.
276 * The alignment size in the table that follows is related to the
277 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but
278 * with X alignment multiplied by 16 and Y alignment multiplied by 32.
280 intel_get_non_msrt_mcs_alignment(brw
, irb
->mt
, &x_align
, &y_align
);
284 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
285 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
287 * In order to optimize the performance MCS buffer (when bound to
288 * 1X RT) clear similarly to MCS buffer clear for MSRT case,
289 * clear rect is required to be scaled by the following factors
290 * in the horizontal and vertical directions:
292 * The X and Y scale down factors in the table that follows are each
293 * equal to half the alignment value computed above.
295 x_scaledown
= x_align
/ 2;
296 y_scaledown
= y_align
/ 2;
298 /* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel
299 * Backend > MCS Buffer for Render Target(s) [DevIVB+] > Table "Color
300 * Clear of Non-MultiSampled Render Target Restrictions":
302 * Clear rectangle must be aligned to two times the number of
303 * pixels in the table shown below due to 16x16 hashing across the
309 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
310 * Target(s)", beneath the "MSAA Compression" bullet (p326):
312 * Clear pass for this case requires that scaled down primitive
313 * is sent down with upper left co-ordinate to coincide with
314 * actual rectangle being cleared. For MSAA, clear rectangle’s
315 * height and width need to as show in the following table in
316 * terms of (width,height) of the RT.
318 * MSAA Width of Clear Rect Height of Clear Rect
319 * 4X Ceil(1/8*width) Ceil(1/2*height)
320 * 8X Ceil(1/2*width) Ceil(1/2*height)
322 * The text "with upper left co-ordinate to coincide with actual
323 * rectangle being cleared" is a little confusing--it seems to imply
324 * that to clear a rectangle from (x,y) to (x+w,y+h), one needs to
325 * feed the pipeline using the rectangle (x,y) to
326 * (x+Ceil(w/N),y+Ceil(h/2)), where N is either 2 or 8 depending on
327 * the number of samples. Experiments indicate that this is not
328 * quite correct; actually, what the hardware appears to do is to
329 * align whatever rectangle is sent down the pipeline to the nearest
330 * multiple of 2x2 blocks, and then scale it up by a factor of N
331 * horizontally and 2 vertically. So the resulting alignment is 4
332 * vertically and either 4 or 16 horizontally, and the scaledown
333 * factor is 2 vertically and either 2 or 8 horizontally.
335 switch (irb
->mt
->num_samples
) {
343 assert(!"Unexpected sample count for fast clear");
347 x_align
= x_scaledown
* 2;
348 y_align
= y_scaledown
* 2;
351 /* Do the alignment and scaledown. */
352 x0
= ROUND_DOWN_TO(x0
, x_align
) / x_scaledown
;
353 y0
= ROUND_DOWN_TO(y0
, y_align
) / y_scaledown
;
354 x1
= ALIGN(x1
, x_align
) / x_scaledown
;
355 y1
= ALIGN(y1
, y_align
) / y_scaledown
;
360 brw_blorp_rt_resolve_params::brw_blorp_rt_resolve_params(
361 struct brw_context
*brw
,
362 struct intel_mipmap_tree
*mt
)
364 dst
.set(brw
, mt
, 0 /* level */, 0 /* layer */, true);
366 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
368 * A rectangle primitive must be scaled down by the following factors
369 * with respect to render target being resolved.
371 * The scaledown factors in the table that follows are related to the
372 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but with
373 * X and Y alignment each divided by 2.
375 unsigned x_align
, y_align
;
376 intel_get_non_msrt_mcs_alignment(brw
, mt
, &x_align
, &y_align
);
377 unsigned x_scaledown
= x_align
/ 2;
378 unsigned y_scaledown
= y_align
/ 2;
380 x1
= ALIGN(mt
->logical_width0
, x_scaledown
) / x_scaledown
;
381 y1
= ALIGN(mt
->logical_height0
, y_scaledown
) / y_scaledown
;
383 fast_clear_op
= GEN7_FAST_CLEAR_OP_RESOLVE
;
385 /* Note: there is no need to initialize push constants because it doesn't
386 * matter what data gets dispatched to the render target. However, we must
387 * ensure that the fragment shader delivers the data using the "replicated
391 memset(&wm_prog_key
, 0, sizeof(wm_prog_key
));
392 wm_prog_key
.use_simd16_replicated_data
= true;
397 brw_blorp_const_color_params::get_wm_prog(struct brw_context
*brw
,
398 brw_blorp_prog_data
**prog_data
)
401 uint32_t prog_offset
= 0;
402 if (!brw_search_cache(&brw
->cache
, BRW_BLORP_CONST_COLOR_PROG
,
403 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
404 &prog_offset
, prog_data
)) {
405 brw_blorp_const_color_program
prog(brw
, &this->wm_prog_key
);
407 const GLuint
*program
= prog
.compile(brw
, &program_size
);
408 brw_upload_cache(&brw
->cache
, BRW_BLORP_CONST_COLOR_PROG
,
409 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
410 program
, program_size
,
411 &prog
.prog_data
, sizeof(prog
.prog_data
),
412 &prog_offset
, prog_data
);
418 brw_blorp_const_color_program::alloc_regs()
421 this->R0
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
422 this->R1
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
424 prog_data
.first_curbe_grf
= reg
;
425 clear_rgba
= retype(brw_vec4_grf(reg
++, 0), BRW_REGISTER_TYPE_F
);
426 reg
+= BRW_BLORP_NUM_PUSH_CONST_REGS
;
428 /* Make sure we didn't run out of registers */
429 assert(reg
<= GEN7_MRF_HACK_START
);
435 brw_blorp_const_color_program::compile(struct brw_context
*brw
,
436 GLuint
*program_size
)
438 /* Set up prog_data */
439 memset(&prog_data
, 0, sizeof(prog_data
));
440 prog_data
.persample_msaa_dispatch
= false;
444 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
446 struct brw_reg mrf_rt_write
=
447 retype(vec16(brw_message_reg(base_mrf
)), BRW_REGISTER_TYPE_F
);
449 uint32_t mlen
, msg_type
;
450 if (key
->use_simd16_replicated_data
) {
451 /* The message payload is a single register with the low 4 floats/ints
452 * filled with the constant clear color.
454 brw_set_mask_control(&func
, BRW_MASK_DISABLE
);
455 brw_MOV(&func
, vec4(brw_message_reg(base_mrf
)), clear_rgba
);
456 brw_set_mask_control(&func
, BRW_MASK_ENABLE
);
458 msg_type
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED
;
461 for (int i
= 0; i
< 4; i
++) {
462 /* The message payload is pairs of registers for 16 pixels each of r,
465 brw_set_compression_control(&func
, BRW_COMPRESSION_COMPRESSED
);
467 brw_message_reg(base_mrf
+ i
* 2),
468 brw_vec1_grf(clear_rgba
.nr
, i
));
469 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
472 msg_type
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE
;
476 /* Now write to the render target and terminate the thread */
478 16 /* dispatch_width */,
479 base_mrf
/* msg_reg_nr */,
480 mrf_rt_write
/* src0 */,
482 BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX
,
484 0 /* response_length */,
486 false /* header present */);
488 if (unlikely(INTEL_DEBUG
& DEBUG_BLORP
)) {
489 fprintf(stderr
, "Native code for BLORP clear:\n");
490 brw_dump_compile(&func
, stderr
, 0, func
.next_insn_offset
);
491 fprintf(stderr
, "\n");
493 return brw_get_program(&func
, program_size
);
498 do_single_blorp_clear(struct brw_context
*brw
, struct gl_framebuffer
*fb
,
499 struct gl_renderbuffer
*rb
, unsigned buf
,
500 bool partial_clear
, unsigned layer
)
502 struct gl_context
*ctx
= &brw
->ctx
;
503 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
505 brw_blorp_clear_params
params(brw
, fb
, rb
, ctx
->Color
.ColorMask
[buf
],
506 partial_clear
, layer
);
509 (params
.fast_clear_op
== GEN7_FAST_CLEAR_OP_FAST_CLEAR
);
511 /* Record the clear color in the miptree so that it will be
512 * programmed in SURFACE_STATE by later rendering and resolve
515 uint32_t new_color_value
=
516 compute_fast_clear_color_bits(&ctx
->Color
.ClearColor
);
517 if (irb
->mt
->fast_clear_color_value
!= new_color_value
) {
518 irb
->mt
->fast_clear_color_value
= new_color_value
;
519 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
522 /* If the buffer is already in INTEL_FAST_CLEAR_STATE_CLEAR, the clear
523 * is redundant and can be skipped.
525 if (irb
->mt
->fast_clear_state
== INTEL_FAST_CLEAR_STATE_CLEAR
)
528 /* If the MCS buffer hasn't been allocated yet, we need to allocate
531 if (!irb
->mt
->mcs_mt
) {
532 if (!intel_miptree_alloc_non_msrt_mcs(brw
, irb
->mt
)) {
533 /* MCS allocation failed--probably this will only happen in
534 * out-of-memory conditions. But in any case, try to recover
535 * by falling back to a non-blorp clear technique.
539 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
543 DBG("%s to mt %p level %d layer %d\n", __FUNCTION__
,
544 irb
->mt
, irb
->mt_level
, irb
->mt_layer
);
546 brw_blorp_exec(brw
, ¶ms
);
549 /* Now that the fast clear has occurred, put the buffer in
550 * INTEL_FAST_CLEAR_STATE_CLEAR so that we won't waste time doing
553 irb
->mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_CLEAR
;
562 brw_blorp_clear_color(struct brw_context
*brw
, struct gl_framebuffer
*fb
,
565 for (unsigned buf
= 0; buf
< fb
->_NumColorDrawBuffers
; buf
++) {
566 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
567 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
569 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
570 * the framebuffer can be complete with some attachments missing. In
571 * this case the _ColorDrawBuffers pointer will be NULL.
576 if (fb
->MaxNumLayers
> 0) {
577 unsigned layer_multiplier
=
578 (irb
->mt
->msaa_layout
== INTEL_MSAA_LAYOUT_UMS
||
579 irb
->mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
) ?
580 irb
->mt
->num_samples
: 1;
581 unsigned num_layers
=
582 irb
->mt
->level
[irb
->mt_level
].depth
/ layer_multiplier
;
583 for (unsigned layer
= 0; layer
< num_layers
; layer
++) {
584 if (!do_single_blorp_clear(brw
, fb
, rb
, buf
, partial_clear
,
585 layer
* layer_multiplier
)) {
590 unsigned layer
= irb
->mt_layer
;
591 if (!do_single_blorp_clear(brw
, fb
, rb
, buf
, partial_clear
, layer
))
595 irb
->need_downsample
= true;
602 brw_blorp_resolve_color(struct brw_context
*brw
, struct intel_mipmap_tree
*mt
)
604 DBG("%s to mt %p\n", __FUNCTION__
, mt
);
606 brw_blorp_rt_resolve_params
params(brw
, mt
);
607 brw_blorp_exec(brw
, ¶ms
);
608 mt
->fast_clear_state
= INTEL_FAST_CLEAR_STATE_RESOLVED
;