i965: refactor blorp clear code in preparation for layered clears.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_blorp_clear.cpp
1 /*
2 * Copyright © 2013 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 extern "C" {
25 #include "main/teximage.h"
26 #include "main/blend.h"
27 #include "main/fbobject.h"
28 #include "main/renderbuffer.h"
29 }
30
31 #include "glsl/ralloc.h"
32
33 #include "intel_fbo.h"
34
35 #include "brw_blorp.h"
36 #include "brw_context.h"
37 #include "brw_eu.h"
38 #include "brw_state.h"
39
40 #define FILE_DEBUG_FLAG DEBUG_BLORP
41
42 struct brw_blorp_const_color_prog_key
43 {
44 bool use_simd16_replicated_data;
45 bool pad[3];
46 };
47
48 /**
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
51 * resolves.
52 */
53 class brw_blorp_const_color_params : public brw_blorp_params
54 {
55 public:
56 virtual uint32_t get_wm_prog(struct brw_context *brw,
57 brw_blorp_prog_data **prog_data) const;
58
59 protected:
60 brw_blorp_const_color_prog_key wm_prog_key;
61 };
62
63 class brw_blorp_clear_params : public brw_blorp_const_color_params
64 {
65 public:
66 brw_blorp_clear_params(struct brw_context *brw,
67 struct gl_framebuffer *fb,
68 struct gl_renderbuffer *rb,
69 GLubyte *color_mask,
70 bool partial_clear);
71 };
72
73
74 /**
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.
78 */
79 class brw_blorp_rt_resolve_params : public brw_blorp_const_color_params
80 {
81 public:
82 brw_blorp_rt_resolve_params(struct brw_context *brw,
83 struct intel_mipmap_tree *mt);
84 };
85
86
87 class brw_blorp_const_color_program
88 {
89 public:
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();
93
94 const GLuint *compile(struct brw_context *brw, GLuint *program_size);
95
96 brw_blorp_prog_data prog_data;
97
98 private:
99 void alloc_regs();
100
101 void *mem_ctx;
102 struct brw_context *brw;
103 const brw_blorp_const_color_prog_key *key;
104 struct brw_compile func;
105
106 /* Thread dispatch header */
107 struct brw_reg R0;
108
109 /* Pixel X/Y coordinates (always in R1). */
110 struct brw_reg R1;
111
112 /* Register with push constants (a single vec4) */
113 struct brw_reg clear_rgba;
114
115 /* MRF used for render target writes */
116 GLuint base_mrf;
117 };
118
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)),
123 brw(brw),
124 key(key),
125 R0(),
126 R1(),
127 clear_rgba(),
128 base_mrf(0)
129 {
130 prog_data.first_curbe_grf = 0;
131 prog_data.persample_msaa_dispatch = false;
132 brw_init_compile(brw, &func, mem_ctx);
133 }
134
135 brw_blorp_const_color_program::~brw_blorp_const_color_program()
136 {
137 ralloc_free(mem_ctx);
138 }
139
140
141 /**
142 * Determine if fast color clear supports the given clear color.
143 *
144 * Fast color clear can only clear to color values of 1.0 or 0.0. At the
145 * moment we only support floating point, unorm, and snorm buffers.
146 */
147 static bool
148 is_color_fast_clear_compatible(struct brw_context *brw,
149 gl_format format,
150 const union gl_color_union *color)
151 {
152 if (_mesa_is_format_integer_color(format))
153 return false;
154
155 for (int i = 0; i < 4; i++) {
156 if (color->f[i] != 0.0 && color->f[i] != 1.0) {
157 perf_debug("Clear color unsupported by fast color clear. "
158 "Falling back to slow clear.\n");
159 return false;
160 }
161 }
162 return true;
163 }
164
165
166 /**
167 * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
168 * SURFACE_STATE.
169 */
170 static uint32_t
171 compute_fast_clear_color_bits(const union gl_color_union *color)
172 {
173 uint32_t bits = 0;
174 for (int i = 0; i < 4; i++) {
175 if (color->f[i] != 0.0)
176 bits |= 1 << (GEN7_SURFACE_CLEAR_COLOR_SHIFT + (3 - i));
177 }
178 return bits;
179 }
180
181
182 brw_blorp_clear_params::brw_blorp_clear_params(struct brw_context *brw,
183 struct gl_framebuffer *fb,
184 struct gl_renderbuffer *rb,
185 GLubyte *color_mask,
186 bool partial_clear)
187 {
188 struct gl_context *ctx = &brw->ctx;
189 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
190
191 dst.set(brw, irb->mt, irb->mt_level, irb->mt_layer, true);
192
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];
196
197 x0 = fb->_Xmin;
198 x1 = fb->_Xmax;
199 if (rb->Name != 0) {
200 y0 = fb->_Ymin;
201 y1 = fb->_Ymax;
202 } else {
203 y0 = rb->Height - fb->_Ymax;
204 y1 = rb->Height - fb->_Ymin;
205 }
206
207 float *push_consts = (float *)&wm_push_consts;
208
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];
213
214 use_wm_prog = true;
215
216 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
217
218 wm_prog_key.use_simd16_replicated_data = true;
219
220 /* From the SNB PRM (Vol4_Part1):
221 *
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."
225 */
226 if (irb->mt->region->tiling == I915_TILING_NONE)
227 wm_prog_key.use_simd16_replicated_data = false;
228
229 /* Constant color writes ignore everyting in blend and color calculator
230 * state. This is not documented.
231 */
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;
236 }
237 }
238
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;
245
246 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
247 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
248 *
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
255 * and covers the RT.
256 *
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.
260 */
261 unsigned x_align, y_align;
262 intel_get_non_msrt_mcs_alignment(brw, irb->mt, &x_align, &y_align);
263 x_align *= 16;
264 y_align *= 32;
265
266 if (brw->is_haswell && brw->gt == 3) {
267 /* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel
268 * Backend > MCS Buffer for Render Target(s) [DevIVB+]:
269 * [DevHSW:GT3]: Clear rectangle must be aligned to two times the
270 * number of pixels in the table shown below...
271 * x_align, y_align values computed above are the relevant entries
272 * in the referred table.
273 */
274 x0 = ROUND_DOWN_TO(x0, 2 * x_align);
275 y0 = ROUND_DOWN_TO(y0, 2 * y_align);
276 x1 = ALIGN(x1, 2 * x_align);
277 y1 = ALIGN(y1, 2 * y_align);
278 } else {
279 x0 = ROUND_DOWN_TO(x0, x_align);
280 y0 = ROUND_DOWN_TO(y0, y_align);
281 x1 = ALIGN(x1, x_align);
282 y1 = ALIGN(y1, y_align);
283 }
284
285 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
286 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
287 *
288 * In order to optimize the performance MCS buffer (when bound to 1X
289 * RT) clear similarly to MCS buffer clear for MSRT case, clear rect
290 * is required to be scaled by the following factors in the
291 * horizontal and vertical directions:
292 *
293 * The X and Y scale down factors in the table that follows are each
294 * equal to half the alignment value computed above.
295 */
296 unsigned x_scaledown = x_align / 2;
297 unsigned y_scaledown = y_align / 2;
298 x0 /= x_scaledown;
299 y0 /= y_scaledown;
300 x1 /= x_scaledown;
301 y1 /= y_scaledown;
302 }
303 }
304
305
306 brw_blorp_rt_resolve_params::brw_blorp_rt_resolve_params(
307 struct brw_context *brw,
308 struct intel_mipmap_tree *mt)
309 {
310 dst.set(brw, mt, 0 /* level */, 0 /* layer */, true);
311
312 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
313 *
314 * A rectangle primitive must be scaled down by the following factors
315 * with respect to render target being resolved.
316 *
317 * The scaledown factors in the table that follows are related to the
318 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but with
319 * X and Y alignment each divided by 2.
320 */
321 unsigned x_align, y_align;
322 intel_get_non_msrt_mcs_alignment(brw, mt, &x_align, &y_align);
323 unsigned x_scaledown = x_align / 2;
324 unsigned y_scaledown = y_align / 2;
325 x0 = y0 = 0;
326 x1 = ALIGN(mt->logical_width0, x_scaledown) / x_scaledown;
327 y1 = ALIGN(mt->logical_height0, y_scaledown) / y_scaledown;
328
329 fast_clear_op = GEN7_FAST_CLEAR_OP_RESOLVE;
330
331 /* Note: there is no need to initialize push constants because it doesn't
332 * matter what data gets dispatched to the render target. However, we must
333 * ensure that the fragment shader delivers the data using the "replicated
334 * color" message.
335 */
336 use_wm_prog = true;
337 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
338 wm_prog_key.use_simd16_replicated_data = true;
339 }
340
341
342 uint32_t
343 brw_blorp_const_color_params::get_wm_prog(struct brw_context *brw,
344 brw_blorp_prog_data **prog_data)
345 const
346 {
347 uint32_t prog_offset = 0;
348 if (!brw_search_cache(&brw->cache, BRW_BLORP_CONST_COLOR_PROG,
349 &this->wm_prog_key, sizeof(this->wm_prog_key),
350 &prog_offset, prog_data)) {
351 brw_blorp_const_color_program prog(brw, &this->wm_prog_key);
352 GLuint program_size;
353 const GLuint *program = prog.compile(brw, &program_size);
354 brw_upload_cache(&brw->cache, BRW_BLORP_CONST_COLOR_PROG,
355 &this->wm_prog_key, sizeof(this->wm_prog_key),
356 program, program_size,
357 &prog.prog_data, sizeof(prog.prog_data),
358 &prog_offset, prog_data);
359 }
360 return prog_offset;
361 }
362
363 void
364 brw_blorp_const_color_program::alloc_regs()
365 {
366 int reg = 0;
367 this->R0 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
368 this->R1 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
369
370 prog_data.first_curbe_grf = reg;
371 clear_rgba = retype(brw_vec4_grf(reg++, 0), BRW_REGISTER_TYPE_F);
372 reg += BRW_BLORP_NUM_PUSH_CONST_REGS;
373
374 /* Make sure we didn't run out of registers */
375 assert(reg <= GEN7_MRF_HACK_START);
376
377 this->base_mrf = 2;
378 }
379
380 const GLuint *
381 brw_blorp_const_color_program::compile(struct brw_context *brw,
382 GLuint *program_size)
383 {
384 /* Set up prog_data */
385 memset(&prog_data, 0, sizeof(prog_data));
386 prog_data.persample_msaa_dispatch = false;
387
388 alloc_regs();
389
390 brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
391
392 struct brw_reg mrf_rt_write =
393 retype(vec16(brw_message_reg(base_mrf)), BRW_REGISTER_TYPE_F);
394
395 uint32_t mlen, msg_type;
396 if (key->use_simd16_replicated_data) {
397 /* The message payload is a single register with the low 4 floats/ints
398 * filled with the constant clear color.
399 */
400 brw_set_mask_control(&func, BRW_MASK_DISABLE);
401 brw_MOV(&func, vec4(brw_message_reg(base_mrf)), clear_rgba);
402 brw_set_mask_control(&func, BRW_MASK_ENABLE);
403
404 msg_type = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED;
405 mlen = 1;
406 } else {
407 for (int i = 0; i < 4; i++) {
408 /* The message payload is pairs of registers for 16 pixels each of r,
409 * g, b, and a.
410 */
411 brw_set_compression_control(&func, BRW_COMPRESSION_COMPRESSED);
412 brw_MOV(&func,
413 brw_message_reg(base_mrf + i * 2),
414 brw_vec1_grf(clear_rgba.nr, i));
415 brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
416 }
417
418 msg_type = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE;
419 mlen = 8;
420 }
421
422 /* Now write to the render target and terminate the thread */
423 brw_fb_WRITE(&func,
424 16 /* dispatch_width */,
425 base_mrf /* msg_reg_nr */,
426 mrf_rt_write /* src0 */,
427 msg_type,
428 BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX,
429 mlen,
430 0 /* response_length */,
431 true /* eot */,
432 false /* header present */);
433
434 if (unlikely(INTEL_DEBUG & DEBUG_BLORP)) {
435 printf("Native code for BLORP clear:\n");
436 brw_dump_compile(&func, stdout, 0, func.next_insn_offset);
437 printf("\n");
438 }
439 return brw_get_program(&func, program_size);
440 }
441
442
443 bool
444 do_single_blorp_clear(struct brw_context *brw, struct gl_framebuffer *fb,
445 struct gl_renderbuffer *rb, unsigned buf,
446 bool partial_clear)
447 {
448 struct gl_context *ctx = &brw->ctx;
449 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
450
451 brw_blorp_clear_params params(brw, fb, rb, ctx->Color.ColorMask[buf],
452 partial_clear);
453
454 bool is_fast_clear =
455 (params.fast_clear_op == GEN7_FAST_CLEAR_OP_FAST_CLEAR);
456 if (is_fast_clear) {
457 /* Record the clear color in the miptree so that it will be
458 * programmed in SURFACE_STATE by later rendering and resolve
459 * operations.
460 */
461 uint32_t new_color_value =
462 compute_fast_clear_color_bits(&ctx->Color.ClearColor);
463 if (irb->mt->fast_clear_color_value != new_color_value) {
464 irb->mt->fast_clear_color_value = new_color_value;
465 brw->state.dirty.brw |= BRW_NEW_SURFACES;
466 }
467
468 /* If the buffer is already in INTEL_MCS_STATE_CLEAR, the clear is
469 * redundant and can be skipped.
470 */
471 if (irb->mt->mcs_state == INTEL_MCS_STATE_CLEAR)
472 return true;
473
474 /* If the MCS buffer hasn't been allocated yet, we need to allocate
475 * it now.
476 */
477 if (!irb->mt->mcs_mt) {
478 if (!intel_miptree_alloc_non_msrt_mcs(brw, irb->mt)) {
479 /* MCS allocation failed--probably this will only happen in
480 * out-of-memory conditions. But in any case, try to recover
481 * by falling back to a non-blorp clear technique.
482 */
483 return false;
484 }
485 brw->state.dirty.brw |= BRW_NEW_SURFACES;
486 }
487 }
488
489 DBG("%s to mt %p level %d layer %d\n", __FUNCTION__,
490 irb->mt, irb->mt_level, irb->mt_layer);
491
492 brw_blorp_exec(brw, &params);
493
494 if (is_fast_clear) {
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
497 * clears.
498 */
499 irb->mt->mcs_state = INTEL_MCS_STATE_CLEAR;
500 }
501
502 return true;
503 }
504
505
506 extern "C" {
507 bool
508 brw_blorp_clear_color(struct brw_context *brw, struct gl_framebuffer *fb,
509 bool partial_clear)
510 {
511 /* The constant color clear code doesn't work for multisampled surfaces, so
512 * we need to support falling back to other clear mechanisms.
513 * Unfortunately, our clear code is based on a bitmask that doesn't
514 * distinguish individual color attachments, so we walk the attachments to
515 * see if any require fallback, and fall back for all if any of them need
516 * to.
517 */
518 for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
519 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
520 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
521
522 if (irb && irb->mt->msaa_layout != INTEL_MSAA_LAYOUT_NONE)
523 return false;
524 }
525
526 for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
527 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
528
529 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
530 * the framebuffer can be complete with some attachments missing. In
531 * this case the _ColorDrawBuffers pointer will be NULL.
532 */
533 if (rb == NULL)
534 continue;
535
536 if (!do_single_blorp_clear(brw, fb, rb, buf, partial_clear))
537 return false;
538 }
539
540 return true;
541 }
542
543 void
544 brw_blorp_resolve_color(struct brw_context *brw, struct intel_mipmap_tree *mt)
545 {
546 DBG("%s to mt %p\n", __FUNCTION__, mt);
547
548 brw_blorp_rt_resolve_params params(brw, mt);
549 brw_blorp_exec(brw, &params);
550 mt->mcs_state = INTEL_MCS_STATE_RESOLVED;
551 }
552
553 } /* extern "C" */