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 struct brw_blorp_const_color_prog_key
42 bool use_simd16_replicated_data
;
47 * Parameters for a blorp operation where the fragment shader outputs a
48 * constant color. This is used for both fast color clears and color
51 class brw_blorp_const_color_params
: public brw_blorp_params
54 virtual uint32_t get_wm_prog(struct brw_context
*brw
,
55 brw_blorp_prog_data
**prog_data
) const;
58 brw_blorp_const_color_prog_key wm_prog_key
;
61 class brw_blorp_clear_params
: public brw_blorp_const_color_params
64 brw_blorp_clear_params(struct brw_context
*brw
,
65 struct gl_framebuffer
*fb
,
66 struct gl_renderbuffer
*rb
,
73 * Parameters for a blorp operation that performs a "render target resolve".
74 * This is used to resolve pending fast clear pixels before a color buffer is
75 * used for texturing, ReadPixels, or scanout.
77 class brw_blorp_rt_resolve_params
: public brw_blorp_const_color_params
80 brw_blorp_rt_resolve_params(struct brw_context
*brw
,
81 struct intel_mipmap_tree
*mt
);
85 class brw_blorp_const_color_program
88 brw_blorp_const_color_program(struct brw_context
*brw
,
89 const brw_blorp_const_color_prog_key
*key
);
90 ~brw_blorp_const_color_program();
92 const GLuint
*compile(struct brw_context
*brw
, GLuint
*program_size
);
94 brw_blorp_prog_data prog_data
;
100 struct brw_context
*brw
;
101 const brw_blorp_const_color_prog_key
*key
;
102 struct brw_compile func
;
104 /* Thread dispatch header */
107 /* Pixel X/Y coordinates (always in R1). */
110 /* Register with push constants (a single vec4) */
111 struct brw_reg clear_rgba
;
113 /* MRF used for render target writes */
117 brw_blorp_const_color_program::brw_blorp_const_color_program(
118 struct brw_context
*brw
,
119 const brw_blorp_const_color_prog_key
*key
)
120 : mem_ctx(ralloc_context(NULL
)),
124 brw_init_compile(brw
, &func
, mem_ctx
);
127 brw_blorp_const_color_program::~brw_blorp_const_color_program()
129 ralloc_free(mem_ctx
);
134 * Determine if fast color clear supports the given clear color.
136 * Fast color clear can only clear to color values of 1.0 or 0.0. At the
137 * moment we only support floating point, unorm, and snorm buffers.
140 is_color_fast_clear_compatible(struct intel_context
*intel
,
142 const union gl_color_union
*color
)
144 if (_mesa_is_format_integer_color(format
))
147 for (int i
= 0; i
< 4; i
++) {
148 if (color
->f
[i
] != 0.0 && color
->f
[i
] != 1.0) {
149 perf_debug("Clear color unsupported by fast color clear. "
150 "Falling back to slow clear.");
159 * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
163 compute_fast_clear_color_bits(const union gl_color_union
*color
)
166 for (int i
= 0; i
< 4; i
++) {
167 if (color
->f
[i
] != 0.0)
168 bits
|= 1 << (GEN7_SURFACE_CLEAR_COLOR_SHIFT
+ (3 - i
));
174 brw_blorp_clear_params::brw_blorp_clear_params(struct brw_context
*brw
,
175 struct gl_framebuffer
*fb
,
176 struct gl_renderbuffer
*rb
,
180 struct intel_context
*intel
= &brw
->intel
;
181 struct gl_context
*ctx
= &intel
->ctx
;
182 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
184 dst
.set(brw
, irb
->mt
, irb
->mt_level
, irb
->mt_layer
);
186 /* Override the surface format according to the context's sRGB rules. */
187 gl_format format
= _mesa_get_render_format(ctx
, irb
->mt
->format
);
188 dst
.brw_surfaceformat
= brw
->render_target_format
[format
];
196 y0
= rb
->Height
- fb
->_Ymax
;
197 y1
= rb
->Height
- fb
->_Ymin
;
200 float *push_consts
= (float *)&wm_push_consts
;
202 push_consts
[0] = ctx
->Color
.ClearColor
.f
[0];
203 push_consts
[1] = ctx
->Color
.ClearColor
.f
[1];
204 push_consts
[2] = ctx
->Color
.ClearColor
.f
[2];
205 push_consts
[3] = ctx
->Color
.ClearColor
.f
[3];
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
->region
->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 (!color_mask
[i
]) {
227 color_write_disable
[i
] = true;
228 wm_prog_key
.use_simd16_replicated_data
= false;
232 /* If we can do this as a fast color clear, do so. */
233 if (irb
->mt
->mcs_state
!= INTEL_MCS_STATE_NONE
&& !partial_clear
&&
234 wm_prog_key
.use_simd16_replicated_data
&&
235 is_color_fast_clear_compatible(intel
, format
, &ctx
->Color
.ClearColor
)) {
236 memset(push_consts
, 0xff, 4*sizeof(float));
237 fast_clear_op
= GEN7_FAST_CLEAR_OP_FAST_CLEAR
;
239 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
240 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
242 * Clear pass must have a clear rectangle that must follow alignment
243 * rules in terms of pixels and lines as shown in the table
244 * below. Further, the clear-rectangle height and width must be
245 * multiple of the following dimensions. If the height and width of
246 * the render target being cleared do not meet these requirements,
247 * an MCS buffer can be created such that it follows the requirement
250 * The alignment size in the table that follows is related to the
251 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but
252 * with X alignment multiplied by 16 and Y alignment multiplied by 32.
254 unsigned x_align
, y_align
;
255 intel_get_non_msrt_mcs_alignment(intel
, irb
->mt
, &x_align
, &y_align
);
258 x0
= ROUND_DOWN_TO(x0
, x_align
);
259 y0
= ROUND_DOWN_TO(y0
, y_align
);
260 x1
= ALIGN(x1
, x_align
);
261 y1
= ALIGN(y1
, y_align
);
263 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
264 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
266 * In order to optimize the performance MCS buffer (when bound to 1X
267 * RT) clear similarly to MCS buffer clear for MSRT case, clear rect
268 * is required to be scaled by the following factors in the
269 * horizontal and vertical directions:
271 * The X and Y scale down factors in the table that follows are each
272 * equal to half the alignment value computed above.
274 unsigned x_scaledown
= x_align
/ 2;
275 unsigned y_scaledown
= y_align
/ 2;
284 brw_blorp_rt_resolve_params::brw_blorp_rt_resolve_params(
285 struct brw_context
*brw
,
286 struct intel_mipmap_tree
*mt
)
288 dst
.set(brw
, mt
, 0 /* level */, 0 /* layer */);
290 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
292 * A rectangle primitive must be scaled down by the following factors
293 * with respect to render target being resolved.
295 * The scaledown factors in the table that follows are related to the
296 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but with
297 * X and Y alignment each divided by 2.
299 unsigned x_align
, y_align
;
300 intel_get_non_msrt_mcs_alignment(&brw
->intel
, mt
, &x_align
, &y_align
);
301 unsigned x_scaledown
= x_align
/ 2;
302 unsigned y_scaledown
= y_align
/ 2;
304 x1
= ALIGN(mt
->logical_width0
, x_scaledown
) / x_scaledown
;
305 y1
= ALIGN(mt
->logical_height0
, y_scaledown
) / y_scaledown
;
307 fast_clear_op
= GEN7_FAST_CLEAR_OP_RESOLVE
;
309 /* Note: there is no need to initialize push constants because it doesn't
310 * matter what data gets dispatched to the render target. However, we must
311 * ensure that the fragment shader delivers the data using the "replicated
315 memset(&wm_prog_key
, 0, sizeof(wm_prog_key
));
316 wm_prog_key
.use_simd16_replicated_data
= true;
321 brw_blorp_const_color_params::get_wm_prog(struct brw_context
*brw
,
322 brw_blorp_prog_data
**prog_data
)
325 uint32_t prog_offset
;
326 if (!brw_search_cache(&brw
->cache
, BRW_BLORP_CONST_COLOR_PROG
,
327 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
328 &prog_offset
, prog_data
)) {
329 brw_blorp_const_color_program
prog(brw
, &this->wm_prog_key
);
331 const GLuint
*program
= prog
.compile(brw
, &program_size
);
332 brw_upload_cache(&brw
->cache
, BRW_BLORP_CONST_COLOR_PROG
,
333 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
334 program
, program_size
,
335 &prog
.prog_data
, sizeof(prog
.prog_data
),
336 &prog_offset
, prog_data
);
342 brw_blorp_const_color_program::alloc_regs()
345 this->R0
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
346 this->R1
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
348 prog_data
.first_curbe_grf
= reg
;
349 clear_rgba
= retype(brw_vec4_grf(reg
++, 0), BRW_REGISTER_TYPE_F
);
350 reg
+= BRW_BLORP_NUM_PUSH_CONST_REGS
;
352 /* Make sure we didn't run out of registers */
353 assert(reg
<= GEN7_MRF_HACK_START
);
359 brw_blorp_const_color_program::compile(struct brw_context
*brw
,
360 GLuint
*program_size
)
362 /* Set up prog_data */
363 memset(&prog_data
, 0, sizeof(prog_data
));
364 prog_data
.persample_msaa_dispatch
= false;
368 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
370 struct brw_reg mrf_rt_write
=
371 retype(vec16(brw_message_reg(base_mrf
)), BRW_REGISTER_TYPE_F
);
373 uint32_t mlen
, msg_type
;
374 if (key
->use_simd16_replicated_data
) {
375 /* The message payload is a single register with the low 4 floats/ints
376 * filled with the constant clear color.
378 brw_set_mask_control(&func
, BRW_MASK_DISABLE
);
379 brw_MOV(&func
, vec4(brw_message_reg(base_mrf
)), clear_rgba
);
380 brw_set_mask_control(&func
, BRW_MASK_ENABLE
);
382 msg_type
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED
;
385 for (int i
= 0; i
< 4; i
++) {
386 /* The message payload is pairs of registers for 16 pixels each of r,
389 brw_set_compression_control(&func
, BRW_COMPRESSION_COMPRESSED
);
391 brw_message_reg(base_mrf
+ i
* 2),
392 brw_vec1_grf(clear_rgba
.nr
, i
));
393 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
396 msg_type
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE
;
400 /* Now write to the render target and terminate the thread */
402 16 /* dispatch_width */,
403 base_mrf
/* msg_reg_nr */,
404 mrf_rt_write
/* src0 */,
406 BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX
,
408 0 /* response_length */,
410 false /* header present */);
412 if (unlikely(INTEL_DEBUG
& DEBUG_BLORP
)) {
413 printf("Native code for BLORP clear:\n");
414 brw_dump_compile(&func
, stdout
, 0, func
.next_insn_offset
);
417 return brw_get_program(&func
, program_size
);
422 brw_blorp_clear_color(struct intel_context
*intel
, struct gl_framebuffer
*fb
,
425 struct gl_context
*ctx
= &intel
->ctx
;
426 struct brw_context
*brw
= brw_context(ctx
);
428 /* The constant color clear code doesn't work for multisampled surfaces, so
429 * we need to support falling back to other clear mechanisms.
430 * Unfortunately, our clear code is based on a bitmask that doesn't
431 * distinguish individual color attachments, so we walk the attachments to
432 * see if any require fallback, and fall back for all if any of them need
435 for (unsigned buf
= 0; buf
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; buf
++) {
436 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[buf
];
437 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
439 if (irb
&& irb
->mt
->msaa_layout
!= INTEL_MSAA_LAYOUT_NONE
)
443 for (unsigned buf
= 0; buf
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; buf
++) {
444 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[buf
];
445 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
447 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
448 * the framebuffer can be complete with some attachments missing. In
449 * this case the _ColorDrawBuffers pointer will be NULL.
454 brw_blorp_clear_params
params(brw
, fb
, rb
, ctx
->Color
.ColorMask
[buf
],
458 (params
.fast_clear_op
== GEN7_FAST_CLEAR_OP_FAST_CLEAR
);
460 /* Record the clear color in the miptree so that it will be
461 * programmed in SURFACE_STATE by later rendering and resolve
464 uint32_t new_color_value
=
465 compute_fast_clear_color_bits(&ctx
->Color
.ClearColor
);
466 if (irb
->mt
->fast_clear_color_value
!= new_color_value
) {
467 irb
->mt
->fast_clear_color_value
= new_color_value
;
468 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
471 /* If the buffer is already in INTEL_MCS_STATE_CLEAR, the clear is
472 * redundant and can be skipped.
474 if (irb
->mt
->mcs_state
== INTEL_MCS_STATE_CLEAR
)
477 /* If the MCS buffer hasn't been allocated yet, we need to allocate
480 if (!irb
->mt
->mcs_mt
) {
481 if (!intel_miptree_alloc_non_msrt_mcs(intel
, irb
->mt
)) {
482 /* MCS allocation failed--probably this will only happen in
483 * out-of-memory conditions. But in any case, try to recover
484 * by falling back to a non-blorp clear technique.
488 brw
->state
.dirty
.brw
|= BRW_NEW_SURFACES
;
492 brw_blorp_exec(intel
, ¶ms
);
495 /* Now that the fast clear has occurred, put the buffer in
496 * INTEL_MCS_STATE_CLEAR so that we won't waste time doing redundant
499 irb
->mt
->mcs_state
= INTEL_MCS_STATE_CLEAR
;
507 brw_blorp_resolve_color(struct intel_context
*intel
, struct intel_mipmap_tree
*mt
)
509 struct brw_context
*brw
= brw_context(&intel
->ctx
);
510 brw_blorp_rt_resolve_params
params(brw
, mt
);
511 brw_blorp_exec(intel
, ¶ms
);
512 mt
->mcs_state
= INTEL_MCS_STATE_RESOLVED
;