i915: Remove most of the code under gen >= 4 checks.
[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 {
126 brw_init_compile(brw, &func, mem_ctx);
127 }
128
129 brw_blorp_const_color_program::~brw_blorp_const_color_program()
130 {
131 ralloc_free(mem_ctx);
132 }
133
134
135 /**
136 * Determine if fast color clear supports the given clear color.
137 *
138 * Fast color clear can only clear to color values of 1.0 or 0.0. At the
139 * moment we only support floating point, unorm, and snorm buffers.
140 */
141 static bool
142 is_color_fast_clear_compatible(struct intel_context *intel,
143 gl_format format,
144 const union gl_color_union *color)
145 {
146 if (_mesa_is_format_integer_color(format))
147 return false;
148
149 for (int i = 0; i < 4; i++) {
150 if (color->f[i] != 0.0 && color->f[i] != 1.0) {
151 perf_debug("Clear color unsupported by fast color clear. "
152 "Falling back to slow clear.");
153 return false;
154 }
155 }
156 return true;
157 }
158
159
160 /**
161 * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
162 * SURFACE_STATE.
163 */
164 static uint32_t
165 compute_fast_clear_color_bits(const union gl_color_union *color)
166 {
167 uint32_t bits = 0;
168 for (int i = 0; i < 4; i++) {
169 if (color->f[i] != 0.0)
170 bits |= 1 << (GEN7_SURFACE_CLEAR_COLOR_SHIFT + (3 - i));
171 }
172 return bits;
173 }
174
175
176 brw_blorp_clear_params::brw_blorp_clear_params(struct brw_context *brw,
177 struct gl_framebuffer *fb,
178 struct gl_renderbuffer *rb,
179 GLubyte *color_mask,
180 bool partial_clear)
181 {
182 struct intel_context *intel = &brw->intel;
183 struct gl_context *ctx = &intel->ctx;
184 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
185
186 dst.set(brw, irb->mt, irb->mt_level, irb->mt_layer);
187
188 /* Override the surface format according to the context's sRGB rules. */
189 gl_format format = _mesa_get_render_format(ctx, irb->mt->format);
190 dst.brw_surfaceformat = brw->render_target_format[format];
191
192 x0 = fb->_Xmin;
193 x1 = fb->_Xmax;
194 if (rb->Name != 0) {
195 y0 = fb->_Ymin;
196 y1 = fb->_Ymax;
197 } else {
198 y0 = rb->Height - fb->_Ymax;
199 y1 = rb->Height - fb->_Ymin;
200 }
201
202 float *push_consts = (float *)&wm_push_consts;
203
204 push_consts[0] = ctx->Color.ClearColor.f[0];
205 push_consts[1] = ctx->Color.ClearColor.f[1];
206 push_consts[2] = ctx->Color.ClearColor.f[2];
207 push_consts[3] = ctx->Color.ClearColor.f[3];
208
209 use_wm_prog = true;
210
211 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
212
213 wm_prog_key.use_simd16_replicated_data = true;
214
215 /* From the SNB PRM (Vol4_Part1):
216 *
217 * "Replicated data (Message Type = 111) is only supported when
218 * accessing tiled memory. Using this Message Type to access linear
219 * (untiled) memory is UNDEFINED."
220 */
221 if (irb->mt->region->tiling == I915_TILING_NONE)
222 wm_prog_key.use_simd16_replicated_data = false;
223
224 /* Constant color writes ignore everyting in blend and color calculator
225 * state. This is not documented.
226 */
227 for (int i = 0; i < 4; i++) {
228 if (!color_mask[i]) {
229 color_write_disable[i] = true;
230 wm_prog_key.use_simd16_replicated_data = false;
231 }
232 }
233
234 /* If we can do this as a fast color clear, do so. */
235 if (irb->mt->mcs_state != INTEL_MCS_STATE_NONE && !partial_clear &&
236 wm_prog_key.use_simd16_replicated_data &&
237 is_color_fast_clear_compatible(intel, format, &ctx->Color.ClearColor)) {
238 memset(push_consts, 0xff, 4*sizeof(float));
239 fast_clear_op = GEN7_FAST_CLEAR_OP_FAST_CLEAR;
240
241 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
242 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
243 *
244 * Clear pass must have a clear rectangle that must follow alignment
245 * rules in terms of pixels and lines as shown in the table
246 * below. Further, the clear-rectangle height and width must be
247 * multiple of the following dimensions. If the height and width of
248 * the render target being cleared do not meet these requirements,
249 * an MCS buffer can be created such that it follows the requirement
250 * and covers the RT.
251 *
252 * The alignment size in the table that follows is related to the
253 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but
254 * with X alignment multiplied by 16 and Y alignment multiplied by 32.
255 */
256 unsigned x_align, y_align;
257 intel_get_non_msrt_mcs_alignment(intel, irb->mt, &x_align, &y_align);
258 x_align *= 16;
259 y_align *= 32;
260 x0 = ROUND_DOWN_TO(x0, x_align);
261 y0 = ROUND_DOWN_TO(y0, y_align);
262 x1 = ALIGN(x1, x_align);
263 y1 = ALIGN(y1, y_align);
264
265 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
266 * Target(s)", beneath the "Fast Color Clear" bullet (p327):
267 *
268 * In order to optimize the performance MCS buffer (when bound to 1X
269 * RT) clear similarly to MCS buffer clear for MSRT case, clear rect
270 * is required to be scaled by the following factors in the
271 * horizontal and vertical directions:
272 *
273 * The X and Y scale down factors in the table that follows are each
274 * equal to half the alignment value computed above.
275 */
276 unsigned x_scaledown = x_align / 2;
277 unsigned y_scaledown = y_align / 2;
278 x0 /= x_scaledown;
279 y0 /= y_scaledown;
280 x1 /= x_scaledown;
281 y1 /= y_scaledown;
282 }
283 }
284
285
286 brw_blorp_rt_resolve_params::brw_blorp_rt_resolve_params(
287 struct brw_context *brw,
288 struct intel_mipmap_tree *mt)
289 {
290 dst.set(brw, mt, 0 /* level */, 0 /* layer */);
291
292 /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
293 *
294 * A rectangle primitive must be scaled down by the following factors
295 * with respect to render target being resolved.
296 *
297 * The scaledown factors in the table that follows are related to the
298 * alignment size returned by intel_get_non_msrt_mcs_alignment(), but with
299 * X and Y alignment each divided by 2.
300 */
301 unsigned x_align, y_align;
302 intel_get_non_msrt_mcs_alignment(&brw->intel, mt, &x_align, &y_align);
303 unsigned x_scaledown = x_align / 2;
304 unsigned y_scaledown = y_align / 2;
305 x0 = y0 = 0;
306 x1 = ALIGN(mt->logical_width0, x_scaledown) / x_scaledown;
307 y1 = ALIGN(mt->logical_height0, y_scaledown) / y_scaledown;
308
309 fast_clear_op = GEN7_FAST_CLEAR_OP_RESOLVE;
310
311 /* Note: there is no need to initialize push constants because it doesn't
312 * matter what data gets dispatched to the render target. However, we must
313 * ensure that the fragment shader delivers the data using the "replicated
314 * color" message.
315 */
316 use_wm_prog = true;
317 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
318 wm_prog_key.use_simd16_replicated_data = true;
319 }
320
321
322 uint32_t
323 brw_blorp_const_color_params::get_wm_prog(struct brw_context *brw,
324 brw_blorp_prog_data **prog_data)
325 const
326 {
327 uint32_t prog_offset;
328 if (!brw_search_cache(&brw->cache, BRW_BLORP_CONST_COLOR_PROG,
329 &this->wm_prog_key, sizeof(this->wm_prog_key),
330 &prog_offset, prog_data)) {
331 brw_blorp_const_color_program prog(brw, &this->wm_prog_key);
332 GLuint program_size;
333 const GLuint *program = prog.compile(brw, &program_size);
334 brw_upload_cache(&brw->cache, BRW_BLORP_CONST_COLOR_PROG,
335 &this->wm_prog_key, sizeof(this->wm_prog_key),
336 program, program_size,
337 &prog.prog_data, sizeof(prog.prog_data),
338 &prog_offset, prog_data);
339 }
340 return prog_offset;
341 }
342
343 void
344 brw_blorp_const_color_program::alloc_regs()
345 {
346 int reg = 0;
347 this->R0 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
348 this->R1 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
349
350 prog_data.first_curbe_grf = reg;
351 clear_rgba = retype(brw_vec4_grf(reg++, 0), BRW_REGISTER_TYPE_F);
352 reg += BRW_BLORP_NUM_PUSH_CONST_REGS;
353
354 /* Make sure we didn't run out of registers */
355 assert(reg <= GEN7_MRF_HACK_START);
356
357 this->base_mrf = 2;
358 }
359
360 const GLuint *
361 brw_blorp_const_color_program::compile(struct brw_context *brw,
362 GLuint *program_size)
363 {
364 /* Set up prog_data */
365 memset(&prog_data, 0, sizeof(prog_data));
366 prog_data.persample_msaa_dispatch = false;
367
368 alloc_regs();
369
370 brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
371
372 struct brw_reg mrf_rt_write =
373 retype(vec16(brw_message_reg(base_mrf)), BRW_REGISTER_TYPE_F);
374
375 uint32_t mlen, msg_type;
376 if (key->use_simd16_replicated_data) {
377 /* The message payload is a single register with the low 4 floats/ints
378 * filled with the constant clear color.
379 */
380 brw_set_mask_control(&func, BRW_MASK_DISABLE);
381 brw_MOV(&func, vec4(brw_message_reg(base_mrf)), clear_rgba);
382 brw_set_mask_control(&func, BRW_MASK_ENABLE);
383
384 msg_type = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED;
385 mlen = 1;
386 } else {
387 for (int i = 0; i < 4; i++) {
388 /* The message payload is pairs of registers for 16 pixels each of r,
389 * g, b, and a.
390 */
391 brw_set_compression_control(&func, BRW_COMPRESSION_COMPRESSED);
392 brw_MOV(&func,
393 brw_message_reg(base_mrf + i * 2),
394 brw_vec1_grf(clear_rgba.nr, i));
395 brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
396 }
397
398 msg_type = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE;
399 mlen = 8;
400 }
401
402 /* Now write to the render target and terminate the thread */
403 brw_fb_WRITE(&func,
404 16 /* dispatch_width */,
405 base_mrf /* msg_reg_nr */,
406 mrf_rt_write /* src0 */,
407 msg_type,
408 BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX,
409 mlen,
410 0 /* response_length */,
411 true /* eot */,
412 false /* header present */);
413
414 if (unlikely(INTEL_DEBUG & DEBUG_BLORP)) {
415 printf("Native code for BLORP clear:\n");
416 brw_dump_compile(&func, stdout, 0, func.next_insn_offset);
417 printf("\n");
418 }
419 return brw_get_program(&func, program_size);
420 }
421
422 extern "C" {
423 bool
424 brw_blorp_clear_color(struct intel_context *intel, struct gl_framebuffer *fb,
425 bool partial_clear)
426 {
427 struct gl_context *ctx = &intel->ctx;
428 struct brw_context *brw = brw_context(ctx);
429
430 /* The constant color clear code doesn't work for multisampled surfaces, so
431 * we need to support falling back to other clear mechanisms.
432 * Unfortunately, our clear code is based on a bitmask that doesn't
433 * distinguish individual color attachments, so we walk the attachments to
434 * see if any require fallback, and fall back for all if any of them need
435 * to.
436 */
437 for (unsigned buf = 0; buf < ctx->DrawBuffer->_NumColorDrawBuffers; buf++) {
438 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[buf];
439 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
440
441 if (irb && irb->mt->msaa_layout != INTEL_MSAA_LAYOUT_NONE)
442 return false;
443 }
444
445 for (unsigned buf = 0; buf < ctx->DrawBuffer->_NumColorDrawBuffers; buf++) {
446 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[buf];
447 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
448
449 /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
450 * the framebuffer can be complete with some attachments missing. In
451 * this case the _ColorDrawBuffers pointer will be NULL.
452 */
453 if (rb == NULL)
454 continue;
455
456 brw_blorp_clear_params params(brw, fb, rb, ctx->Color.ColorMask[buf],
457 partial_clear);
458
459 bool is_fast_clear =
460 (params.fast_clear_op == GEN7_FAST_CLEAR_OP_FAST_CLEAR);
461 if (is_fast_clear) {
462 /* Record the clear color in the miptree so that it will be
463 * programmed in SURFACE_STATE by later rendering and resolve
464 * operations.
465 */
466 uint32_t new_color_value =
467 compute_fast_clear_color_bits(&ctx->Color.ClearColor);
468 if (irb->mt->fast_clear_color_value != new_color_value) {
469 irb->mt->fast_clear_color_value = new_color_value;
470 brw->state.dirty.brw |= BRW_NEW_SURFACES;
471 }
472
473 /* If the buffer is already in INTEL_MCS_STATE_CLEAR, the clear is
474 * redundant and can be skipped.
475 */
476 if (irb->mt->mcs_state == INTEL_MCS_STATE_CLEAR)
477 continue;
478
479 /* If the MCS buffer hasn't been allocated yet, we need to allocate
480 * it now.
481 */
482 if (!irb->mt->mcs_mt) {
483 if (!intel_miptree_alloc_non_msrt_mcs(intel, irb->mt)) {
484 /* MCS allocation failed--probably this will only happen in
485 * out-of-memory conditions. But in any case, try to recover
486 * by falling back to a non-blorp clear technique.
487 */
488 return false;
489 }
490 brw->state.dirty.brw |= BRW_NEW_SURFACES;
491 }
492 }
493
494 DBG("%s to mt %p level %d layer %d\n", __FUNCTION__,
495 irb->mt, irb->mt_level, irb->mt_layer);
496
497 brw_blorp_exec(intel, &params);
498
499 if (is_fast_clear) {
500 /* Now that the fast clear has occurred, put the buffer in
501 * INTEL_MCS_STATE_CLEAR so that we won't waste time doing redundant
502 * clears.
503 */
504 irb->mt->mcs_state = INTEL_MCS_STATE_CLEAR;
505 }
506 }
507
508 return true;
509 }
510
511 void
512 brw_blorp_resolve_color(struct intel_context *intel, struct intel_mipmap_tree *mt)
513 {
514 struct brw_context *brw = brw_context(&intel->ctx);
515
516 DBG("%s to mt %p\n", __FUNCTION__, mt);
517
518 brw_blorp_rt_resolve_params params(brw, mt);
519 brw_blorp_exec(intel, &params);
520 mt->mcs_state = INTEL_MCS_STATE_RESOLVED;
521 }
522
523 } /* extern "C" */