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
,
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 struct brw_context
*brw
;
103 const brw_blorp_const_color_prog_key
*key
;
104 struct brw_compile func
;
106 /* Thread dispatch header */
109 /* Pixel X/Y coordinates (always in R1). */
112 /* Register with push constants (a single vec4) */
113 struct brw_reg clear_rgba
;
115 /* MRF used for render target writes */
119 brw_blorp_const_color_program::brw_blorp_const_color_program(
120 struct brw_context
*brw
,
121 const brw_blorp_const_color_prog_key
*key
)
122 : mem_ctx(ralloc_context(NULL
)),
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 struct intel_context
*intel
= &brw
->intel
;
151 if (_mesa_is_format_integer_color(format
))
154 for (int i
= 0; i
< 4; i
++) {
155 if (color
->f
[i
] != 0.0 && color
->f
[i
] != 1.0) {
156 perf_debug("Clear color unsupported by fast color clear. "
157 "Falling back to slow clear.\n");
166 * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
170 compute_fast_clear_color_bits(const union gl_color_union
*color
)
173 for (int i
= 0; i
< 4; i
++) {
174 if (color
->f
[i
] != 0.0)
175 bits
|= 1 << (GEN7_SURFACE_CLEAR_COLOR_SHIFT
+ (3 - i
));
181 brw_blorp_clear_params::brw_blorp_clear_params(struct brw_context
*brw
,
182 struct gl_framebuffer
*fb
,
183 struct gl_renderbuffer
*rb
,
187 struct intel_context
*intel
= &brw
->intel
;
188 struct gl_context
*ctx
= &intel
->ctx
;
189 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
191 dst
.set(brw
, irb
->mt
, irb
->mt_level
, irb
->mt_layer
);
193 /* Override the surface format according to the context's sRGB rules. */
194 gl_format format
= _mesa_get_render_format(ctx
, irb
->mt
->format
);
195 dst
.brw_surfaceformat
= brw
->render_target_format
[format
];
203 y0
= rb
->Height
- fb
->_Ymax
;
204 y1
= rb
->Height
- fb
->_Ymin
;
207 float *push_consts
= (float *)&wm_push_consts
;
209 push_consts
[0] = ctx
->Color
.ClearColor
.f
[0];
210 push_consts
[1] = ctx
->Color
.ClearColor
.f
[1];
211 push_consts
[2] = ctx
->Color
.ClearColor
.f
[2];
212 push_consts
[3] = ctx
->Color
.ClearColor
.f
[3];
216 memset(&wm_prog_key
, 0, sizeof(wm_prog_key
));
218 wm_prog_key
.use_simd16_replicated_data
= true;
220 /* From the SNB PRM (Vol4_Part1):
222 * "Replicated data (Message Type = 111) is only supported when
223 * accessing tiled memory. Using this Message Type to access linear
224 * (untiled) memory is UNDEFINED."
226 if (irb
->mt
->region
->tiling
== I915_TILING_NONE
)
227 wm_prog_key
.use_simd16_replicated_data
= false;
229 /* Constant color writes ignore everyting in blend and color calculator
230 * state. This is not documented.
232 for (int i
= 0; i
< 4; i
++) {
233 if (!color_mask
[i
]) {
234 color_write_disable
[i
] = true;
235 wm_prog_key
.use_simd16_replicated_data
= false;
239 /* If we can do this as a fast color clear, do so. */
240 if (irb
->mt
->mcs_state
!= INTEL_MCS_STATE_NONE
&& !partial_clear
&&
241 wm_prog_key
.use_simd16_replicated_data
&&
242 is_color_fast_clear_compatible(brw
, format
, &ctx
->Color
.ClearColor
)) {
243 memset(push_consts
, 0xff, 4*sizeof(float));
244 fast_clear_op
= GEN7_FAST_CLEAR_OP_FAST_CLEAR
;
246 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
247 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
249 * Clear pass must have a clear rectangle that must follow alignment
250 * rules in terms of pixels and lines as shown in the table
251 * below. Further, the clear-rectangle height and width must be
252 * multiple of the following dimensions. If the height and width of
253 * the render target being cleared do not meet these requirements,
254 * an MCS buffer can be created such that it follows the requirement
257 * The alignment size in the table that follows is related to the
258 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but
259 * with X alignment multiplied by 16 and Y alignment multiplied by 32.
261 unsigned x_align
, y_align
;
262 intel_get_non_msrt_mcs_alignment(brw
, irb
->mt
, &x_align
, &y_align
);
265 x0
= ROUND_DOWN_TO(x0
, x_align
);
266 y0
= ROUND_DOWN_TO(y0
, y_align
);
267 x1
= ALIGN(x1
, x_align
);
268 y1
= ALIGN(y1
, y_align
);
270 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
271 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
273 * In order to optimize the performance MCS buffer (when bound to 1X
274 * RT) clear similarly to MCS buffer clear for MSRT case, clear rect
275 * is required to be scaled by the following factors in the
276 * horizontal and vertical directions:
278 * The X and Y scale down factors in the table that follows are each
279 * equal to half the alignment value computed above.
281 unsigned x_scaledown
= x_align
/ 2;
282 unsigned y_scaledown
= y_align
/ 2;
291 brw_blorp_rt_resolve_params::brw_blorp_rt_resolve_params(
292 struct brw_context
*brw
,
293 struct intel_mipmap_tree
*mt
)
295 dst
.set(brw
, mt
, 0 /* level */, 0 /* layer */);
297 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
299 * A rectangle primitive must be scaled down by the following factors
300 * with respect to render target being resolved.
302 * The scaledown factors in the table that follows are related to the
303 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but with
304 * X and Y alignment each divided by 2.
306 unsigned x_align
, y_align
;
307 intel_get_non_msrt_mcs_alignment(brw
, mt
, &x_align
, &y_align
);
308 unsigned x_scaledown
= x_align
/ 2;
309 unsigned y_scaledown
= y_align
/ 2;
311 x1
= ALIGN(mt
->logical_width0
, x_scaledown
) / x_scaledown
;
312 y1
= ALIGN(mt
->logical_height0
, y_scaledown
) / y_scaledown
;
314 fast_clear_op
= GEN7_FAST_CLEAR_OP_RESOLVE
;
316 /* Note: there is no need to initialize push constants because it doesn't
317 * matter what data gets dispatched to the render target. However, we must
318 * ensure that the fragment shader delivers the data using the "replicated
322 memset(&wm_prog_key
, 0, sizeof(wm_prog_key
));
323 wm_prog_key
.use_simd16_replicated_data
= true;
328 brw_blorp_const_color_params::get_wm_prog(struct brw_context
*brw
,
329 brw_blorp_prog_data
**prog_data
)
332 uint32_t prog_offset
;
333 if (!brw_search_cache(&brw
->cache
, BRW_BLORP_CONST_COLOR_PROG
,
334 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
335 &prog_offset
, prog_data
)) {
336 brw_blorp_const_color_program
prog(brw
, &this->wm_prog_key
);
338 const GLuint
*program
= prog
.compile(brw
, &program_size
);
339 brw_upload_cache(&brw
->cache
, BRW_BLORP_CONST_COLOR_PROG
,
340 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
341 program
, program_size
,
342 &prog
.prog_data
, sizeof(prog
.prog_data
),
343 &prog_offset
, prog_data
);
349 brw_blorp_const_color_program::alloc_regs()
352 this->R0
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
353 this->R1
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
355 prog_data
.first_curbe_grf
= reg
;
356 clear_rgba
= retype(brw_vec4_grf(reg
++, 0), BRW_REGISTER_TYPE_F
);
357 reg
+= BRW_BLORP_NUM_PUSH_CONST_REGS
;
359 /* Make sure we didn't run out of registers */
360 assert(reg
<= GEN7_MRF_HACK_START
);
366 brw_blorp_const_color_program::compile(struct brw_context
*brw
,
367 GLuint
*program_size
)
369 /* Set up prog_data */
370 memset(&prog_data
, 0, sizeof(prog_data
));
371 prog_data
.persample_msaa_dispatch
= false;
375 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
377 struct brw_reg mrf_rt_write
=
378 retype(vec16(brw_message_reg(base_mrf
)), BRW_REGISTER_TYPE_F
);
380 uint32_t mlen
, msg_type
;
381 if (key
->use_simd16_replicated_data
) {
382 /* The message payload is a single register with the low 4 floats/ints
383 * filled with the constant clear color.
385 brw_set_mask_control(&func
, BRW_MASK_DISABLE
);
386 brw_MOV(&func
, vec4(brw_message_reg(base_mrf
)), clear_rgba
);
387 brw_set_mask_control(&func
, BRW_MASK_ENABLE
);
389 msg_type
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED
;
392 for (int i
= 0; i
< 4; i
++) {
393 /* The message payload is pairs of registers for 16 pixels each of r,
396 brw_set_compression_control(&func
, BRW_COMPRESSION_COMPRESSED
);
398 brw_message_reg(base_mrf
+ i
* 2),
399 brw_vec1_grf(clear_rgba
.nr
, i
));
400 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
403 msg_type
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE
;
407 /* Now write to the render target and terminate the thread */
409 16 /* dispatch_width */,
410 base_mrf
/* msg_reg_nr */,
411 mrf_rt_write
/* src0 */,
413 BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX
,
415 0 /* response_length */,
417 false /* header present */);
419 if (unlikely(INTEL_DEBUG
& DEBUG_BLORP
)) {
420 printf("Native code for BLORP clear:\n");
421 brw_dump_compile(&func
, stdout
, 0, func
.next_insn_offset
);
424 return brw_get_program(&func
, program_size
);
429 brw_blorp_clear_color(struct brw_context
*brw
, struct gl_framebuffer
*fb
,
432 struct gl_context
*ctx
= &brw
->intel
.ctx
;
434 /* The constant color clear code doesn't work for multisampled surfaces, so
435 * we need to support falling back to other clear mechanisms.
436 * Unfortunately, our clear code is based on a bitmask that doesn't
437 * distinguish individual color attachments, so we walk the attachments to
438 * see if any require fallback, and fall back for all if any of them need
441 for (unsigned buf
= 0; buf
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; buf
++) {
442 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[buf
];
443 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
445 if (irb
&& irb
->mt
->msaa_layout
!= INTEL_MSAA_LAYOUT_NONE
)
449 for (unsigned buf
= 0; buf
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; buf
++) {
450 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[buf
];
451 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
453 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
454 * the framebuffer can be complete with some attachments missing. In
455 * this case the _ColorDrawBuffers pointer will be NULL.
460 brw_blorp_clear_params
params(brw
, fb
, rb
, ctx
->Color
.ColorMask
[buf
],
464 (params
.fast_clear_op
== GEN7_FAST_CLEAR_OP_FAST_CLEAR
);
466 /* Record the clear color in the miptree so that it will be
467 * programmed in SURFACE_STATE by later rendering and resolve
470 uint32_t new_color_value
=
471 compute_fast_clear_color_bits(&ctx
->Color
.ClearColor
);
472 if (irb
->mt
->fast_clear_color_value
!= new_color_value
) {
473 irb
->mt
->fast_clear_color_value
= new_color_value
;
474 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
477 /* If the buffer is already in INTEL_MCS_STATE_CLEAR, the clear is
478 * redundant and can be skipped.
480 if (irb
->mt
->mcs_state
== INTEL_MCS_STATE_CLEAR
)
483 /* If the MCS buffer hasn't been allocated yet, we need to allocate
486 if (!irb
->mt
->mcs_mt
) {
487 if (!intel_miptree_alloc_non_msrt_mcs(brw
, irb
->mt
)) {
488 /* MCS allocation failed--probably this will only happen in
489 * out-of-memory conditions. But in any case, try to recover
490 * by falling back to a non-blorp clear technique.
494 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
498 DBG("%s to mt %p level %d layer %d\n", __FUNCTION__
,
499 irb
->mt
, irb
->mt_level
, irb
->mt_layer
);
501 brw_blorp_exec(brw
, ¶ms
);
504 /* Now that the fast clear has occurred, put the buffer in
505 * INTEL_MCS_STATE_CLEAR so that we won't waste time doing redundant
508 irb
->mt
->mcs_state
= INTEL_MCS_STATE_CLEAR
;
516 brw_blorp_resolve_color(struct brw_context
*brw
, struct intel_mipmap_tree
*mt
)
518 DBG("%s to mt %p\n", __FUNCTION__
, mt
);
520 brw_blorp_rt_resolve_params
params(brw
, mt
);
521 brw_blorp_exec(brw
, ¶ms
);
522 mt
->mcs_state
= INTEL_MCS_STATE_RESOLVED
;