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 gl_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
->mcs_state
!= INTEL_MCS_STATE_NONE
&& !partial_clear
&&
254 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 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
260 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
262 * Clear pass must have a clear rectangle that must follow alignment
263 * rules in terms of pixels and lines as shown in the table
264 * below. Further, the clear-rectangle height and width must be
265 * multiple of the following dimensions. If the height and width of
266 * the render target being cleared do not meet these requirements,
267 * an MCS buffer can be created such that it follows the requirement
270 * The alignment size in the table that follows is related to the
271 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but
272 * with X alignment multiplied by 16 and Y alignment multiplied by 32.
274 unsigned x_align
, y_align
;
275 intel_get_non_msrt_mcs_alignment(brw
, irb
->mt
, &x_align
, &y_align
);
279 /* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel
280 * Backend > MCS Buffer for Render Target(s) [DevIVB+] > Table "Color
281 * Clear of Non-MultiSampled Render Target Restrictions":
283 * Clear rectangle must be aligned to two times the number of pixels in
284 * the table shown below due to 16x16 hashing across the slice.
286 x0
= ROUND_DOWN_TO(x0
, 2 * x_align
);
287 y0
= ROUND_DOWN_TO(y0
, 2 * y_align
);
288 x1
= ALIGN(x1
, 2 * x_align
);
289 y1
= ALIGN(y1
, 2 * y_align
);
291 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
292 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
294 * In order to optimize the performance MCS buffer (when bound to 1X
295 * RT) clear similarly to MCS buffer clear for MSRT case, clear rect
296 * is required to be scaled by the following factors in the
297 * horizontal and vertical directions:
299 * The X and Y scale down factors in the table that follows are each
300 * equal to half the alignment value computed above.
302 unsigned x_scaledown
= x_align
/ 2;
303 unsigned y_scaledown
= y_align
/ 2;
312 brw_blorp_rt_resolve_params::brw_blorp_rt_resolve_params(
313 struct brw_context
*brw
,
314 struct intel_mipmap_tree
*mt
)
316 dst
.set(brw
, mt
, 0 /* level */, 0 /* layer */, true);
318 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
320 * A rectangle primitive must be scaled down by the following factors
321 * with respect to render target being resolved.
323 * The scaledown factors in the table that follows are related to the
324 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but with
325 * X and Y alignment each divided by 2.
327 unsigned x_align
, y_align
;
328 intel_get_non_msrt_mcs_alignment(brw
, mt
, &x_align
, &y_align
);
329 unsigned x_scaledown
= x_align
/ 2;
330 unsigned y_scaledown
= y_align
/ 2;
332 x1
= ALIGN(mt
->logical_width0
, x_scaledown
) / x_scaledown
;
333 y1
= ALIGN(mt
->logical_height0
, y_scaledown
) / y_scaledown
;
335 fast_clear_op
= GEN7_FAST_CLEAR_OP_RESOLVE
;
337 /* Note: there is no need to initialize push constants because it doesn't
338 * matter what data gets dispatched to the render target. However, we must
339 * ensure that the fragment shader delivers the data using the "replicated
343 memset(&wm_prog_key
, 0, sizeof(wm_prog_key
));
344 wm_prog_key
.use_simd16_replicated_data
= true;
349 brw_blorp_const_color_params::get_wm_prog(struct brw_context
*brw
,
350 brw_blorp_prog_data
**prog_data
)
353 uint32_t prog_offset
= 0;
354 if (!brw_search_cache(&brw
->cache
, BRW_BLORP_CONST_COLOR_PROG
,
355 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
356 &prog_offset
, prog_data
)) {
357 brw_blorp_const_color_program
prog(brw
, &this->wm_prog_key
);
359 const GLuint
*program
= prog
.compile(brw
, &program_size
);
360 brw_upload_cache(&brw
->cache
, BRW_BLORP_CONST_COLOR_PROG
,
361 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
362 program
, program_size
,
363 &prog
.prog_data
, sizeof(prog
.prog_data
),
364 &prog_offset
, prog_data
);
370 brw_blorp_const_color_program::alloc_regs()
373 this->R0
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
374 this->R1
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
376 prog_data
.first_curbe_grf
= reg
;
377 clear_rgba
= retype(brw_vec4_grf(reg
++, 0), BRW_REGISTER_TYPE_F
);
378 reg
+= BRW_BLORP_NUM_PUSH_CONST_REGS
;
380 /* Make sure we didn't run out of registers */
381 assert(reg
<= GEN7_MRF_HACK_START
);
387 brw_blorp_const_color_program::compile(struct brw_context
*brw
,
388 GLuint
*program_size
)
390 /* Set up prog_data */
391 memset(&prog_data
, 0, sizeof(prog_data
));
392 prog_data
.persample_msaa_dispatch
= false;
396 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
398 struct brw_reg mrf_rt_write
=
399 retype(vec16(brw_message_reg(base_mrf
)), BRW_REGISTER_TYPE_F
);
401 uint32_t mlen
, msg_type
;
402 if (key
->use_simd16_replicated_data
) {
403 /* The message payload is a single register with the low 4 floats/ints
404 * filled with the constant clear color.
406 brw_set_mask_control(&func
, BRW_MASK_DISABLE
);
407 brw_MOV(&func
, vec4(brw_message_reg(base_mrf
)), clear_rgba
);
408 brw_set_mask_control(&func
, BRW_MASK_ENABLE
);
410 msg_type
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED
;
413 for (int i
= 0; i
< 4; i
++) {
414 /* The message payload is pairs of registers for 16 pixels each of r,
417 brw_set_compression_control(&func
, BRW_COMPRESSION_COMPRESSED
);
419 brw_message_reg(base_mrf
+ i
* 2),
420 brw_vec1_grf(clear_rgba
.nr
, i
));
421 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
424 msg_type
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE
;
428 /* Now write to the render target and terminate the thread */
430 16 /* dispatch_width */,
431 base_mrf
/* msg_reg_nr */,
432 mrf_rt_write
/* src0 */,
434 BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX
,
436 0 /* response_length */,
438 false /* header present */);
440 if (unlikely(INTEL_DEBUG
& DEBUG_BLORP
)) {
441 printf("Native code for BLORP clear:\n");
442 brw_dump_compile(&func
, stdout
, 0, func
.next_insn_offset
);
445 return brw_get_program(&func
, program_size
);
450 do_single_blorp_clear(struct brw_context
*brw
, struct gl_framebuffer
*fb
,
451 struct gl_renderbuffer
*rb
, unsigned buf
,
452 bool partial_clear
, unsigned layer
)
454 struct gl_context
*ctx
= &brw
->ctx
;
455 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
457 brw_blorp_clear_params
params(brw
, fb
, rb
, ctx
->Color
.ColorMask
[buf
],
458 partial_clear
, layer
);
461 (params
.fast_clear_op
== GEN7_FAST_CLEAR_OP_FAST_CLEAR
);
463 /* Record the clear color in the miptree so that it will be
464 * programmed in SURFACE_STATE by later rendering and resolve
467 uint32_t new_color_value
=
468 compute_fast_clear_color_bits(&ctx
->Color
.ClearColor
);
469 if (irb
->mt
->fast_clear_color_value
!= new_color_value
) {
470 irb
->mt
->fast_clear_color_value
= new_color_value
;
471 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
474 /* If the buffer is already in INTEL_MCS_STATE_CLEAR, the clear is
475 * redundant and can be skipped.
477 if (irb
->mt
->mcs_state
== INTEL_MCS_STATE_CLEAR
)
480 /* If the MCS buffer hasn't been allocated yet, we need to allocate
483 if (!irb
->mt
->mcs_mt
) {
484 if (!intel_miptree_alloc_non_msrt_mcs(brw
, irb
->mt
)) {
485 /* MCS allocation failed--probably this will only happen in
486 * out-of-memory conditions. But in any case, try to recover
487 * by falling back to a non-blorp clear technique.
491 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
495 DBG("%s to mt %p level %d layer %d\n", __FUNCTION__
,
496 irb
->mt
, irb
->mt_level
, irb
->mt_layer
);
498 brw_blorp_exec(brw
, ¶ms
);
501 /* Now that the fast clear has occurred, put the buffer in
502 * INTEL_MCS_STATE_CLEAR so that we won't waste time doing redundant
505 irb
->mt
->mcs_state
= INTEL_MCS_STATE_CLEAR
;
514 brw_blorp_clear_color(struct brw_context
*brw
, struct gl_framebuffer
*fb
,
517 /* The constant color clear code doesn't work for multisampled surfaces, so
518 * we need to support falling back to other clear mechanisms.
519 * Unfortunately, our clear code is based on a bitmask that doesn't
520 * distinguish individual color attachments, so we walk the attachments to
521 * see if any require fallback, and fall back for all if any of them need
524 for (unsigned buf
= 0; buf
< fb
->_NumColorDrawBuffers
; buf
++) {
525 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
526 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
528 if (irb
&& irb
->mt
->msaa_layout
!= INTEL_MSAA_LAYOUT_NONE
)
532 for (unsigned buf
= 0; buf
< fb
->_NumColorDrawBuffers
; buf
++) {
533 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
534 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
536 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
537 * the framebuffer can be complete with some attachments missing. In
538 * this case the _ColorDrawBuffers pointer will be NULL.
543 if (fb
->NumLayers
> 0) {
544 assert(fb
->NumLayers
== irb
->mt
->level
[irb
->mt_level
].depth
);
545 for (unsigned layer
= 0; layer
< fb
->NumLayers
; layer
++) {
546 if (!do_single_blorp_clear(brw
, fb
, rb
, buf
, partial_clear
, layer
))
550 unsigned layer
= irb
->mt_layer
;
551 if (!do_single_blorp_clear(brw
, fb
, rb
, buf
, partial_clear
, layer
))
560 brw_blorp_resolve_color(struct brw_context
*brw
, struct intel_mipmap_tree
*mt
)
562 DBG("%s to mt %p\n", __FUNCTION__
, mt
);
564 brw_blorp_rt_resolve_params
params(brw
, mt
);
565 brw_blorp_exec(brw
, ¶ms
);
566 mt
->mcs_state
= INTEL_MCS_STATE_RESOLVED
;