i965/blorp: Improve fast color clear comment.
[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 unsigned layer);
72 };
73
74
75 /**
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.
79 */
80 class brw_blorp_rt_resolve_params : public brw_blorp_const_color_params
81 {
82 public:
83 brw_blorp_rt_resolve_params(struct brw_context *brw,
84 struct intel_mipmap_tree *mt);
85 };
86
87
88 class brw_blorp_const_color_program
89 {
90 public:
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();
94
95 const GLuint *compile(struct brw_context *brw, GLuint *program_size);
96
97 brw_blorp_prog_data prog_data;
98
99 private:
100 void alloc_regs();
101
102 void *mem_ctx;
103 struct brw_context *brw;
104 const brw_blorp_const_color_prog_key *key;
105 struct brw_compile func;
106
107 /* Thread dispatch header */
108 struct brw_reg R0;
109
110 /* Pixel X/Y coordinates (always in R1). */
111 struct brw_reg R1;
112
113 /* Register with push constants (a single vec4) */
114 struct brw_reg clear_rgba;
115
116 /* MRF used for render target writes */
117 GLuint base_mrf;
118 };
119
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)),
124 brw(brw),
125 key(key),
126 R0(),
127 R1(),
128 clear_rgba(),
129 base_mrf(0)
130 {
131 prog_data.first_curbe_grf = 0;
132 prog_data.persample_msaa_dispatch = false;
133 brw_init_compile(brw, &func, mem_ctx);
134 }
135
136 brw_blorp_const_color_program::~brw_blorp_const_color_program()
137 {
138 ralloc_free(mem_ctx);
139 }
140
141
142 /**
143 * Determine if fast color clear supports the given clear color.
144 *
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.
147 */
148 static bool
149 is_color_fast_clear_compatible(struct brw_context *brw,
150 gl_format format,
151 const union gl_color_union *color)
152 {
153 if (_mesa_is_format_integer_color(format))
154 return false;
155
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");
160 return false;
161 }
162 }
163 return true;
164 }
165
166
167 /**
168 * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
169 * SURFACE_STATE.
170 */
171 static uint32_t
172 compute_fast_clear_color_bits(const union gl_color_union *color)
173 {
174 uint32_t bits = 0;
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));
178 }
179 return bits;
180 }
181
182
183 brw_blorp_clear_params::brw_blorp_clear_params(struct brw_context *brw,
184 struct gl_framebuffer *fb,
185 struct gl_renderbuffer *rb,
186 GLubyte *color_mask,
187 bool partial_clear,
188 unsigned layer)
189 {
190 struct gl_context *ctx = &brw->ctx;
191 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
192
193 dst.set(brw, irb->mt, irb->mt_level, layer, true);
194
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];
198
199 x0 = fb->_Xmin;
200 x1 = fb->_Xmax;
201 if (rb->Name != 0) {
202 y0 = fb->_Ymin;
203 y1 = fb->_Ymax;
204 } else {
205 y0 = rb->Height - fb->_Ymax;
206 y1 = rb->Height - fb->_Ymin;
207 }
208
209 float *push_consts = (float *)&wm_push_consts;
210
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];
215
216 use_wm_prog = true;
217
218 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
219
220 wm_prog_key.use_simd16_replicated_data = true;
221
222 /* From the SNB PRM (Vol4_Part1):
223 *
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."
227 */
228 if (irb->mt->region->tiling == I915_TILING_NONE)
229 wm_prog_key.use_simd16_replicated_data = false;
230
231 /* Constant color writes ignore everyting in blend and color calculator
232 * state. This is not documented.
233 */
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;
238 }
239 }
240
241 /* If we can do this as a fast color clear, do so.
242 *
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.
252 */
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;
258
259 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
260 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
261 *
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
268 * and covers the RT.
269 *
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.
273 */
274 unsigned x_align, y_align;
275 intel_get_non_msrt_mcs_alignment(brw, irb->mt, &x_align, &y_align);
276 x_align *= 16;
277 y_align *= 32;
278
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":
282 *
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.
285 */
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);
290
291 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
292 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
293 *
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:
298 *
299 * The X and Y scale down factors in the table that follows are each
300 * equal to half the alignment value computed above.
301 */
302 unsigned x_scaledown = x_align / 2;
303 unsigned y_scaledown = y_align / 2;
304 x0 /= x_scaledown;
305 y0 /= y_scaledown;
306 x1 /= x_scaledown;
307 y1 /= y_scaledown;
308 }
309 }
310
311
312 brw_blorp_rt_resolve_params::brw_blorp_rt_resolve_params(
313 struct brw_context *brw,
314 struct intel_mipmap_tree *mt)
315 {
316 dst.set(brw, mt, 0 /* level */, 0 /* layer */, true);
317
318 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
319 *
320 * A rectangle primitive must be scaled down by the following factors
321 * with respect to render target being resolved.
322 *
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.
326 */
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;
331 x0 = y0 = 0;
332 x1 = ALIGN(mt->logical_width0, x_scaledown) / x_scaledown;
333 y1 = ALIGN(mt->logical_height0, y_scaledown) / y_scaledown;
334
335 fast_clear_op = GEN7_FAST_CLEAR_OP_RESOLVE;
336
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
340 * color" message.
341 */
342 use_wm_prog = true;
343 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
344 wm_prog_key.use_simd16_replicated_data = true;
345 }
346
347
348 uint32_t
349 brw_blorp_const_color_params::get_wm_prog(struct brw_context *brw,
350 brw_blorp_prog_data **prog_data)
351 const
352 {
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);
358 GLuint program_size;
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);
365 }
366 return prog_offset;
367 }
368
369 void
370 brw_blorp_const_color_program::alloc_regs()
371 {
372 int reg = 0;
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);
375
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;
379
380 /* Make sure we didn't run out of registers */
381 assert(reg <= GEN7_MRF_HACK_START);
382
383 this->base_mrf = 2;
384 }
385
386 const GLuint *
387 brw_blorp_const_color_program::compile(struct brw_context *brw,
388 GLuint *program_size)
389 {
390 /* Set up prog_data */
391 memset(&prog_data, 0, sizeof(prog_data));
392 prog_data.persample_msaa_dispatch = false;
393
394 alloc_regs();
395
396 brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
397
398 struct brw_reg mrf_rt_write =
399 retype(vec16(brw_message_reg(base_mrf)), BRW_REGISTER_TYPE_F);
400
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.
405 */
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);
409
410 msg_type = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED;
411 mlen = 1;
412 } else {
413 for (int i = 0; i < 4; i++) {
414 /* The message payload is pairs of registers for 16 pixels each of r,
415 * g, b, and a.
416 */
417 brw_set_compression_control(&func, BRW_COMPRESSION_COMPRESSED);
418 brw_MOV(&func,
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);
422 }
423
424 msg_type = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE;
425 mlen = 8;
426 }
427
428 /* Now write to the render target and terminate the thread */
429 brw_fb_WRITE(&func,
430 16 /* dispatch_width */,
431 base_mrf /* msg_reg_nr */,
432 mrf_rt_write /* src0 */,
433 msg_type,
434 BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX,
435 mlen,
436 0 /* response_length */,
437 true /* eot */,
438 false /* header present */);
439
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);
443 printf("\n");
444 }
445 return brw_get_program(&func, program_size);
446 }
447
448
449 bool
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)
453 {
454 struct gl_context *ctx = &brw->ctx;
455 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
456
457 brw_blorp_clear_params params(brw, fb, rb, ctx->Color.ColorMask[buf],
458 partial_clear, layer);
459
460 bool is_fast_clear =
461 (params.fast_clear_op == GEN7_FAST_CLEAR_OP_FAST_CLEAR);
462 if (is_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
465 * operations.
466 */
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;
472 }
473
474 /* If the buffer is already in INTEL_MCS_STATE_CLEAR, the clear is
475 * redundant and can be skipped.
476 */
477 if (irb->mt->mcs_state == INTEL_MCS_STATE_CLEAR)
478 return true;
479
480 /* If the MCS buffer hasn't been allocated yet, we need to allocate
481 * it now.
482 */
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.
488 */
489 return false;
490 }
491 brw->state.dirty.brw |= BRW_NEW_SURFACES;
492 }
493 }
494
495 DBG("%s to mt %p level %d layer %d\n", __FUNCTION__,
496 irb->mt, irb->mt_level, irb->mt_layer);
497
498 brw_blorp_exec(brw, &params);
499
500 if (is_fast_clear) {
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
503 * clears.
504 */
505 irb->mt->mcs_state = INTEL_MCS_STATE_CLEAR;
506 }
507
508 return true;
509 }
510
511
512 extern "C" {
513 bool
514 brw_blorp_clear_color(struct brw_context *brw, struct gl_framebuffer *fb,
515 bool partial_clear)
516 {
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
522 * to.
523 */
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);
527
528 if (irb && irb->mt->msaa_layout != INTEL_MSAA_LAYOUT_NONE)
529 return false;
530 }
531
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);
535
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.
539 */
540 if (rb == NULL)
541 continue;
542
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))
547 return false;
548 }
549 } else {
550 unsigned layer = irb->mt_layer;
551 if (!do_single_blorp_clear(brw, fb, rb, buf, partial_clear, layer))
552 return false;
553 }
554 }
555
556 return true;
557 }
558
559 void
560 brw_blorp_resolve_color(struct brw_context *brw, struct intel_mipmap_tree *mt)
561 {
562 DBG("%s to mt %p\n", __FUNCTION__, mt);
563
564 brw_blorp_rt_resolve_params params(brw, mt);
565 brw_blorp_exec(brw, &params);
566 mt->mcs_state = INTEL_MCS_STATE_RESOLVED;
567 }
568
569 } /* extern "C" */