3f096b513fa4af0dda493b6d125173e6cff9c5d5
[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 if (irb->mt->mcs_state != INTEL_MCS_STATE_NONE && !partial_clear &&
243 wm_prog_key.use_simd16_replicated_data &&
244 is_color_fast_clear_compatible(brw, format, &ctx->Color.ClearColor)) {
245 memset(push_consts, 0xff, 4*sizeof(float));
246 fast_clear_op = GEN7_FAST_CLEAR_OP_FAST_CLEAR;
247
248 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
249 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
250 *
251 * Clear pass must have a clear rectangle that must follow alignment
252 * rules in terms of pixels and lines as shown in the table
253 * below. Further, the clear-rectangle height and width must be
254 * multiple of the following dimensions. If the height and width of
255 * the render target being cleared do not meet these requirements,
256 * an MCS buffer can be created such that it follows the requirement
257 * and covers the RT.
258 *
259 * The alignment size in the table that follows is related to the
260 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but
261 * with X alignment multiplied by 16 and Y alignment multiplied by 32.
262 */
263 unsigned x_align, y_align;
264 intel_get_non_msrt_mcs_alignment(brw, irb->mt, &x_align, &y_align);
265 x_align *= 16;
266 y_align *= 32;
267
268 if (brw->is_haswell) {
269 /* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel
270 * Backend > MCS Buffer for Render Target(s) [DevIVB+] > Table "Color
271 * Clear of Non-MultiSampled Render Target Restrictions":
272 *
273 * [IVB, VLVT, HSW]: Clear rectangle must be aligned to two times
274 * the number of pixels in the table shown below... x_align,
275 * y_align values computed above are the relevant entries in the
276 * referred table.
277 *
278 * We apply the workaround to only Haswell because (a) we suspect that
279 * is the only hardware where it is actually required and (b) we
280 * haven't yet validated the workaround for the other hardware.
281 */
282 x0 = ROUND_DOWN_TO(x0, 2 * x_align);
283 y0 = ROUND_DOWN_TO(y0, 2 * y_align);
284 x1 = ALIGN(x1, 2 * x_align);
285 y1 = ALIGN(y1, 2 * y_align);
286 } else {
287 x0 = ROUND_DOWN_TO(x0, x_align);
288 y0 = ROUND_DOWN_TO(y0, y_align);
289 x1 = ALIGN(x1, x_align);
290 y1 = ALIGN(y1, y_align);
291 }
292
293 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
294 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
295 *
296 * In order to optimize the performance MCS buffer (when bound to 1X
297 * RT) clear similarly to MCS buffer clear for MSRT case, clear rect
298 * is required to be scaled by the following factors in the
299 * horizontal and vertical directions:
300 *
301 * The X and Y scale down factors in the table that follows are each
302 * equal to half the alignment value computed above.
303 */
304 unsigned x_scaledown = x_align / 2;
305 unsigned y_scaledown = y_align / 2;
306 x0 /= x_scaledown;
307 y0 /= y_scaledown;
308 x1 /= x_scaledown;
309 y1 /= y_scaledown;
310 }
311 }
312
313
314 brw_blorp_rt_resolve_params::brw_blorp_rt_resolve_params(
315 struct brw_context *brw,
316 struct intel_mipmap_tree *mt)
317 {
318 dst.set(brw, mt, 0 /* level */, 0 /* layer */, true);
319
320 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
321 *
322 * A rectangle primitive must be scaled down by the following factors
323 * with respect to render target being resolved.
324 *
325 * The scaledown factors in the table that follows are related to the
326 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but with
327 * X and Y alignment each divided by 2.
328 */
329 unsigned x_align, y_align;
330 intel_get_non_msrt_mcs_alignment(brw, mt, &x_align, &y_align);
331 unsigned x_scaledown = x_align / 2;
332 unsigned y_scaledown = y_align / 2;
333 x0 = y0 = 0;
334 x1 = ALIGN(mt->logical_width0, x_scaledown) / x_scaledown;
335 y1 = ALIGN(mt->logical_height0, y_scaledown) / y_scaledown;
336
337 fast_clear_op = GEN7_FAST_CLEAR_OP_RESOLVE;
338
339 /* Note: there is no need to initialize push constants because it doesn't
340 * matter what data gets dispatched to the render target. However, we must
341 * ensure that the fragment shader delivers the data using the "replicated
342 * color" message.
343 */
344 use_wm_prog = true;
345 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
346 wm_prog_key.use_simd16_replicated_data = true;
347 }
348
349
350 uint32_t
351 brw_blorp_const_color_params::get_wm_prog(struct brw_context *brw,
352 brw_blorp_prog_data **prog_data)
353 const
354 {
355 uint32_t prog_offset = 0;
356 if (!brw_search_cache(&brw->cache, BRW_BLORP_CONST_COLOR_PROG,
357 &this->wm_prog_key, sizeof(this->wm_prog_key),
358 &prog_offset, prog_data)) {
359 brw_blorp_const_color_program prog(brw, &this->wm_prog_key);
360 GLuint program_size;
361 const GLuint *program = prog.compile(brw, &program_size);
362 brw_upload_cache(&brw->cache, BRW_BLORP_CONST_COLOR_PROG,
363 &this->wm_prog_key, sizeof(this->wm_prog_key),
364 program, program_size,
365 &prog.prog_data, sizeof(prog.prog_data),
366 &prog_offset, prog_data);
367 }
368 return prog_offset;
369 }
370
371 void
372 brw_blorp_const_color_program::alloc_regs()
373 {
374 int reg = 0;
375 this->R0 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
376 this->R1 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
377
378 prog_data.first_curbe_grf = reg;
379 clear_rgba = retype(brw_vec4_grf(reg++, 0), BRW_REGISTER_TYPE_F);
380 reg += BRW_BLORP_NUM_PUSH_CONST_REGS;
381
382 /* Make sure we didn't run out of registers */
383 assert(reg <= GEN7_MRF_HACK_START);
384
385 this->base_mrf = 2;
386 }
387
388 const GLuint *
389 brw_blorp_const_color_program::compile(struct brw_context *brw,
390 GLuint *program_size)
391 {
392 /* Set up prog_data */
393 memset(&prog_data, 0, sizeof(prog_data));
394 prog_data.persample_msaa_dispatch = false;
395
396 alloc_regs();
397
398 brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
399
400 struct brw_reg mrf_rt_write =
401 retype(vec16(brw_message_reg(base_mrf)), BRW_REGISTER_TYPE_F);
402
403 uint32_t mlen, msg_type;
404 if (key->use_simd16_replicated_data) {
405 /* The message payload is a single register with the low 4 floats/ints
406 * filled with the constant clear color.
407 */
408 brw_set_mask_control(&func, BRW_MASK_DISABLE);
409 brw_MOV(&func, vec4(brw_message_reg(base_mrf)), clear_rgba);
410 brw_set_mask_control(&func, BRW_MASK_ENABLE);
411
412 msg_type = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED;
413 mlen = 1;
414 } else {
415 for (int i = 0; i < 4; i++) {
416 /* The message payload is pairs of registers for 16 pixels each of r,
417 * g, b, and a.
418 */
419 brw_set_compression_control(&func, BRW_COMPRESSION_COMPRESSED);
420 brw_MOV(&func,
421 brw_message_reg(base_mrf + i * 2),
422 brw_vec1_grf(clear_rgba.nr, i));
423 brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
424 }
425
426 msg_type = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE;
427 mlen = 8;
428 }
429
430 /* Now write to the render target and terminate the thread */
431 brw_fb_WRITE(&func,
432 16 /* dispatch_width */,
433 base_mrf /* msg_reg_nr */,
434 mrf_rt_write /* src0 */,
435 msg_type,
436 BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX,
437 mlen,
438 0 /* response_length */,
439 true /* eot */,
440 false /* header present */);
441
442 if (unlikely(INTEL_DEBUG & DEBUG_BLORP)) {
443 printf("Native code for BLORP clear:\n");
444 brw_dump_compile(&func, stdout, 0, func.next_insn_offset);
445 printf("\n");
446 }
447 return brw_get_program(&func, program_size);
448 }
449
450
451 bool
452 do_single_blorp_clear(struct brw_context *brw, struct gl_framebuffer *fb,
453 struct gl_renderbuffer *rb, unsigned buf,
454 bool partial_clear, unsigned layer)
455 {
456 struct gl_context *ctx = &brw->ctx;
457 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
458
459 brw_blorp_clear_params params(brw, fb, rb, ctx->Color.ColorMask[buf],
460 partial_clear, layer);
461
462 bool is_fast_clear =
463 (params.fast_clear_op == GEN7_FAST_CLEAR_OP_FAST_CLEAR);
464 if (is_fast_clear) {
465 /* Record the clear color in the miptree so that it will be
466 * programmed in SURFACE_STATE by later rendering and resolve
467 * operations.
468 */
469 uint32_t new_color_value =
470 compute_fast_clear_color_bits(&ctx->Color.ClearColor);
471 if (irb->mt->fast_clear_color_value != new_color_value) {
472 irb->mt->fast_clear_color_value = new_color_value;
473 brw->state.dirty.brw |= BRW_NEW_SURFACES;
474 }
475
476 /* If the buffer is already in INTEL_MCS_STATE_CLEAR, the clear is
477 * redundant and can be skipped.
478 */
479 if (irb->mt->mcs_state == INTEL_MCS_STATE_CLEAR)
480 return true;
481
482 /* If the MCS buffer hasn't been allocated yet, we need to allocate
483 * it now.
484 */
485 if (!irb->mt->mcs_mt) {
486 if (!intel_miptree_alloc_non_msrt_mcs(brw, irb->mt)) {
487 /* MCS allocation failed--probably this will only happen in
488 * out-of-memory conditions. But in any case, try to recover
489 * by falling back to a non-blorp clear technique.
490 */
491 return false;
492 }
493 brw->state.dirty.brw |= BRW_NEW_SURFACES;
494 }
495 }
496
497 DBG("%s to mt %p level %d layer %d\n", __FUNCTION__,
498 irb->mt, irb->mt_level, irb->mt_layer);
499
500 brw_blorp_exec(brw, &params);
501
502 if (is_fast_clear) {
503 /* Now that the fast clear has occurred, put the buffer in
504 * INTEL_MCS_STATE_CLEAR so that we won't waste time doing redundant
505 * clears.
506 */
507 irb->mt->mcs_state = INTEL_MCS_STATE_CLEAR;
508 }
509
510 return true;
511 }
512
513
514 extern "C" {
515 bool
516 brw_blorp_clear_color(struct brw_context *brw, struct gl_framebuffer *fb,
517 bool partial_clear)
518 {
519 /* The constant color clear code doesn't work for multisampled surfaces, so
520 * we need to support falling back to other clear mechanisms.
521 * Unfortunately, our clear code is based on a bitmask that doesn't
522 * distinguish individual color attachments, so we walk the attachments to
523 * see if any require fallback, and fall back for all if any of them need
524 * to.
525 */
526 for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
527 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
528 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
529
530 if (irb && irb->mt->msaa_layout != INTEL_MSAA_LAYOUT_NONE)
531 return false;
532 }
533
534 for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
535 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
536 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
537
538 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
539 * the framebuffer can be complete with some attachments missing. In
540 * this case the _ColorDrawBuffers pointer will be NULL.
541 */
542 if (rb == NULL)
543 continue;
544
545 if (fb->NumLayers > 0) {
546 assert(fb->NumLayers == irb->mt->level[irb->mt_level].depth);
547 for (unsigned layer = 0; layer < fb->NumLayers; layer++) {
548 if (!do_single_blorp_clear(brw, fb, rb, buf, partial_clear, layer))
549 return false;
550 }
551 } else {
552 unsigned layer = irb->mt_layer;
553 if (!do_single_blorp_clear(brw, fb, rb, buf, partial_clear, layer))
554 return false;
555 }
556 }
557
558 return true;
559 }
560
561 void
562 brw_blorp_resolve_color(struct brw_context *brw, struct intel_mipmap_tree *mt)
563 {
564 DBG("%s to mt %p\n", __FUNCTION__, mt);
565
566 brw_blorp_rt_resolve_params params(brw, mt);
567 brw_blorp_exec(brw, &params);
568 mt->mcs_state = INTEL_MCS_STATE_RESOLVED;
569 }
570
571 } /* extern "C" */