i965/blorp: Encode and decode IMS format for 8x MSAA correctly.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_blorp_blit.cpp
1 /*
2 * Copyright © 2012 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 #include "main/teximage.h"
25
26 #include "glsl/ralloc.h"
27
28 #include "intel_fbo.h"
29
30 #include "brw_blorp.h"
31 #include "brw_context.h"
32 #include "brw_eu.h"
33 #include "brw_state.h"
34
35
36 /**
37 * Helper function for handling mirror image blits.
38 *
39 * If coord0 > coord1, swap them and invert the "mirror" boolean.
40 */
41 static inline void
42 fixup_mirroring(bool &mirror, GLint &coord0, GLint &coord1)
43 {
44 if (coord0 > coord1) {
45 mirror = !mirror;
46 GLint tmp = coord0;
47 coord0 = coord1;
48 coord1 = tmp;
49 }
50 }
51
52
53 /**
54 * Adjust {src,dst}_x{0,1} to account for clipping and scissoring of
55 * destination coordinates.
56 *
57 * Return true if there is still blitting to do, false if all pixels got
58 * rejected by the clip and/or scissor.
59 *
60 * For clarity, the nomenclature of this function assumes we are clipping and
61 * scissoring the X coordinate; the exact same logic applies for Y
62 * coordinates.
63 *
64 * Note: this function may also be used to account for clipping of source
65 * coordinates, by swapping the roles of src and dst.
66 */
67 static inline bool
68 clip_or_scissor(bool mirror, GLint &src_x0, GLint &src_x1, GLint &dst_x0,
69 GLint &dst_x1, GLint fb_xmin, GLint fb_xmax)
70 {
71 /* If we are going to scissor everything away, stop. */
72 if (!(fb_xmin < fb_xmax &&
73 dst_x0 < fb_xmax &&
74 fb_xmin < dst_x1 &&
75 dst_x0 < dst_x1)) {
76 return false;
77 }
78
79 /* Clip the destination rectangle, and keep track of how many pixels we
80 * clipped off of the left and right sides of it.
81 */
82 GLint pixels_clipped_left = 0;
83 GLint pixels_clipped_right = 0;
84 if (dst_x0 < fb_xmin) {
85 pixels_clipped_left = fb_xmin - dst_x0;
86 dst_x0 = fb_xmin;
87 }
88 if (fb_xmax < dst_x1) {
89 pixels_clipped_right = dst_x1 - fb_xmax;
90 dst_x1 = fb_xmax;
91 }
92
93 /* If we are mirrored, then before applying pixels_clipped_{left,right} to
94 * the source coordinates, we need to flip them to account for the
95 * mirroring.
96 */
97 if (mirror) {
98 GLint tmp = pixels_clipped_left;
99 pixels_clipped_left = pixels_clipped_right;
100 pixels_clipped_right = tmp;
101 }
102
103 /* Adjust the source rectangle to remove the pixels corresponding to those
104 * that were clipped/scissored out of the destination rectangle.
105 */
106 src_x0 += pixels_clipped_left;
107 src_x1 -= pixels_clipped_right;
108
109 return true;
110 }
111
112
113 static struct intel_mipmap_tree *
114 find_miptree(GLbitfield buffer_bit, struct gl_renderbuffer *rb)
115 {
116 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
117 struct intel_mipmap_tree *mt = irb->mt;
118 if (buffer_bit == GL_STENCIL_BUFFER_BIT && mt->stencil_mt)
119 mt = mt->stencil_mt;
120 return mt;
121 }
122
123
124 static void
125 do_blorp_blit(struct intel_context *intel, GLbitfield buffer_bit,
126 struct gl_renderbuffer *src_rb, struct gl_renderbuffer *dst_rb,
127 GLint srcX0, GLint srcY0,
128 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
129 bool mirror_x, bool mirror_y)
130 {
131 struct gl_context *ctx = &intel->ctx;
132
133 /* Find source/dst miptrees */
134 struct intel_mipmap_tree *src_mt = find_miptree(buffer_bit, src_rb);
135 struct intel_mipmap_tree *dst_mt = find_miptree(buffer_bit, dst_rb);
136
137 /* Get ready to blit. This includes depth resolving the src and dst
138 * buffers if necessary.
139 */
140 intel_renderbuffer_resolve_depth(intel, intel_renderbuffer(src_rb));
141 intel_renderbuffer_resolve_depth(intel, intel_renderbuffer(dst_rb));
142
143 /* Do the blit */
144 brw_blorp_blit_params params(brw_context(ctx), src_mt, dst_mt,
145 srcX0, srcY0, dstX0, dstY0, dstX1, dstY1,
146 mirror_x, mirror_y);
147 brw_blorp_exec(intel, &params);
148
149 /* Mark the dst buffer as needing a HiZ resolve if necessary. */
150 intel_renderbuffer_set_needs_hiz_resolve(intel_renderbuffer(dst_rb));
151 }
152
153
154 static bool
155 formats_match(GLbitfield buffer_bit, struct gl_renderbuffer *src_rb,
156 struct gl_renderbuffer *dst_rb)
157 {
158 /* Note: don't just check gl_renderbuffer::Format, because in some cases
159 * multiple gl_formats resolve to the same native type in the miptree (for
160 * example MESA_FORMAT_X8_Z24 and MESA_FORMAT_S8_Z24), and we can blit
161 * between those formats.
162 */
163 return find_miptree(buffer_bit, src_rb)->format ==
164 find_miptree(buffer_bit, dst_rb)->format;
165 }
166
167
168 static bool
169 try_blorp_blit(struct intel_context *intel,
170 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
171 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
172 GLenum filter, GLbitfield buffer_bit)
173 {
174 struct gl_context *ctx = &intel->ctx;
175
176 /* Sync up the state of window system buffers. We need to do this before
177 * we go looking for the buffers.
178 */
179 intel_prepare_render(intel);
180
181 const struct gl_framebuffer *read_fb = ctx->ReadBuffer;
182 const struct gl_framebuffer *draw_fb = ctx->DrawBuffer;
183
184 /* Detect if the blit needs to be mirrored */
185 bool mirror_x = false, mirror_y = false;
186 fixup_mirroring(mirror_x, srcX0, srcX1);
187 fixup_mirroring(mirror_x, dstX0, dstX1);
188 fixup_mirroring(mirror_y, srcY0, srcY1);
189 fixup_mirroring(mirror_y, dstY0, dstY1);
190
191 /* Make sure width and height match */
192 if (srcX1 - srcX0 != dstX1 - dstX0) return false;
193 if (srcY1 - srcY0 != dstY1 - dstY0) return false;
194
195 /* If the destination rectangle needs to be clipped or scissored, do so.
196 */
197 if (!(clip_or_scissor(mirror_x, srcX0, srcX1, dstX0, dstX1,
198 draw_fb->_Xmin, draw_fb->_Xmax) &&
199 clip_or_scissor(mirror_y, srcY0, srcY1, dstY0, dstY1,
200 draw_fb->_Ymin, draw_fb->_Ymax))) {
201 /* Everything got clipped/scissored away, so the blit was successful. */
202 return true;
203 }
204
205 /* If the source rectangle needs to be clipped or scissored, do so. */
206 if (!(clip_or_scissor(mirror_x, dstX0, dstX1, srcX0, srcX1,
207 0, read_fb->Width) &&
208 clip_or_scissor(mirror_y, dstY0, dstY1, srcY0, srcY1,
209 0, read_fb->Height))) {
210 /* Everything got clipped/scissored away, so the blit was successful. */
211 return true;
212 }
213
214 /* Account for the fact that in the system framebuffer, the origin is at
215 * the lower left.
216 */
217 if (read_fb->Name == 0) {
218 GLint tmp = read_fb->Height - srcY0;
219 srcY0 = read_fb->Height - srcY1;
220 srcY1 = tmp;
221 mirror_y = !mirror_y;
222 }
223 if (draw_fb->Name == 0) {
224 GLint tmp = draw_fb->Height - dstY0;
225 dstY0 = draw_fb->Height - dstY1;
226 dstY1 = tmp;
227 mirror_y = !mirror_y;
228 }
229
230 /* Find buffers */
231 struct gl_renderbuffer *src_rb;
232 struct gl_renderbuffer *dst_rb;
233 switch (buffer_bit) {
234 case GL_COLOR_BUFFER_BIT:
235 src_rb = read_fb->_ColorReadBuffer;
236 for (unsigned i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; ++i) {
237 dst_rb = ctx->DrawBuffer->_ColorDrawBuffers[i];
238 if (dst_rb && !formats_match(buffer_bit, src_rb, dst_rb))
239 return false;
240 }
241 for (unsigned i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; ++i) {
242 dst_rb = ctx->DrawBuffer->_ColorDrawBuffers[i];
243 do_blorp_blit(intel, buffer_bit, src_rb, dst_rb, srcX0, srcY0,
244 dstX0, dstY0, dstX1, dstY1, mirror_x, mirror_y);
245 }
246 break;
247 case GL_DEPTH_BUFFER_BIT:
248 src_rb = read_fb->Attachment[BUFFER_DEPTH].Renderbuffer;
249 dst_rb = draw_fb->Attachment[BUFFER_DEPTH].Renderbuffer;
250 if (!formats_match(buffer_bit, src_rb, dst_rb))
251 return false;
252 do_blorp_blit(intel, buffer_bit, src_rb, dst_rb, srcX0, srcY0,
253 dstX0, dstY0, dstX1, dstY1, mirror_x, mirror_y);
254 break;
255 case GL_STENCIL_BUFFER_BIT:
256 src_rb = read_fb->Attachment[BUFFER_STENCIL].Renderbuffer;
257 dst_rb = draw_fb->Attachment[BUFFER_STENCIL].Renderbuffer;
258 if (!formats_match(buffer_bit, src_rb, dst_rb))
259 return false;
260 do_blorp_blit(intel, buffer_bit, src_rb, dst_rb, srcX0, srcY0,
261 dstX0, dstY0, dstX1, dstY1, mirror_x, mirror_y);
262 break;
263 default:
264 assert(false);
265 }
266
267 return true;
268 }
269
270 GLbitfield
271 brw_blorp_framebuffer(struct intel_context *intel,
272 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
273 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
274 GLbitfield mask, GLenum filter)
275 {
276 /* BLORP is not supported before Gen6. */
277 if (intel->gen < 6)
278 return mask;
279
280 static GLbitfield buffer_bits[] = {
281 GL_COLOR_BUFFER_BIT,
282 GL_DEPTH_BUFFER_BIT,
283 GL_STENCIL_BUFFER_BIT,
284 };
285
286 for (unsigned int i = 0; i < ARRAY_SIZE(buffer_bits); ++i) {
287 if ((mask & buffer_bits[i]) &&
288 try_blorp_blit(intel,
289 srcX0, srcY0, srcX1, srcY1,
290 dstX0, dstY0, dstX1, dstY1,
291 filter, buffer_bits[i])) {
292 mask &= ~buffer_bits[i];
293 }
294 }
295
296 return mask;
297 }
298
299
300 /**
301 * Enum to specify the order of arguments in a sampler message
302 */
303 enum sampler_message_arg
304 {
305 SAMPLER_MESSAGE_ARG_U_FLOAT,
306 SAMPLER_MESSAGE_ARG_V_FLOAT,
307 SAMPLER_MESSAGE_ARG_U_INT,
308 SAMPLER_MESSAGE_ARG_V_INT,
309 SAMPLER_MESSAGE_ARG_SI_INT,
310 SAMPLER_MESSAGE_ARG_MCS_INT,
311 SAMPLER_MESSAGE_ARG_ZERO_INT,
312 };
313
314 /**
315 * Generator for WM programs used in BLORP blits.
316 *
317 * The bulk of the work done by the WM program is to wrap and unwrap the
318 * coordinate transformations used by the hardware to store surfaces in
319 * memory. The hardware transforms a pixel location (X, Y, S) (where S is the
320 * sample index for a multisampled surface) to a memory offset by the
321 * following formulas:
322 *
323 * offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
324 * (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
325 *
326 * For a single-sampled surface, or for a multisampled surface using
327 * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
328 * function:
329 *
330 * encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
331 * decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
332 * encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
333 * decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
334 *
335 * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
336 * embeds the sample number into bit 1 of the X and Y coordinates:
337 *
338 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
339 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
340 * Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
341 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
342 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
343 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
344 * S = (Y & 0b10) | (X & 0b10) >> 1
345 *
346 * For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
347 * embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
348 * the Y coordinate:
349 *
350 * encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
351 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
352 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
353 * decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
354 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
355 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
356 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
357 *
358 * For X tiling, tile() combines together the low-order bits of the X and Y
359 * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
360 * bytes wide and 8 rows high:
361 *
362 * tile(x_tiled, X, Y, S) = A
363 * where A = tile_num << 12 | offset
364 * tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
365 * offset = (Y' & 0b111) << 9
366 * | (X & 0b111111111)
367 * X' = X * cpp
368 * Y' = Y + S * qpitch
369 * detile(x_tiled, A) = (X, Y, S)
370 * where X = X' / cpp
371 * Y = Y' % qpitch
372 * S = Y' / qpitch
373 * Y' = (tile_num / tile_pitch) << 3
374 * | (A & 0b111000000000) >> 9
375 * X' = (tile_num % tile_pitch) << 9
376 * | (A & 0b111111111)
377 *
378 * (In all tiling formulas, cpp is the number of bytes occupied by a single
379 * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
380 * to fill the width of the surface, and qpitch is the spacing (in rows)
381 * between array slices).
382 *
383 * For Y tiling, tile() combines together the low-order bits of the X and Y
384 * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
385 * bytes wide and 32 rows high:
386 *
387 * tile(y_tiled, X, Y, S) = A
388 * where A = tile_num << 12 | offset
389 * tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
390 * offset = (X' & 0b1110000) << 5
391 * | (Y' & 0b11111) << 4
392 * | (X' & 0b1111)
393 * X' = X * cpp
394 * Y' = Y + S * qpitch
395 * detile(y_tiled, A) = (X, Y, S)
396 * where X = X' / cpp
397 * Y = Y' % qpitch
398 * S = Y' / qpitch
399 * Y' = (tile_num / tile_pitch) << 5
400 * | (A & 0b111110000) >> 4
401 * X' = (tile_num % tile_pitch) << 7
402 * | (A & 0b111000000000) >> 5
403 * | (A & 0b1111)
404 *
405 * For W tiling, tile() combines together the low-order bits of the X and Y
406 * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
407 * bytes wide and 64 rows high (note that W tiling is only used for stencil
408 * buffers, which always have cpp = 1 and S=0):
409 *
410 * tile(w_tiled, X, Y, S) = A
411 * where A = tile_num << 12 | offset
412 * tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
413 * offset = (X' & 0b111000) << 6
414 * | (Y' & 0b111100) << 3
415 * | (X' & 0b100) << 2
416 * | (Y' & 0b10) << 2
417 * | (X' & 0b10) << 1
418 * | (Y' & 0b1) << 1
419 * | (X' & 0b1)
420 * X' = X * cpp = X
421 * Y' = Y + S * qpitch
422 * detile(w_tiled, A) = (X, Y, S)
423 * where X = X' / cpp = X'
424 * Y = Y' % qpitch = Y'
425 * S = Y / qpitch = 0
426 * Y' = (tile_num / tile_pitch) << 6
427 * | (A & 0b111100000) >> 3
428 * | (A & 0b1000) >> 2
429 * | (A & 0b10) >> 1
430 * X' = (tile_num % tile_pitch) << 6
431 * | (A & 0b111000000000) >> 6
432 * | (A & 0b10000) >> 2
433 * | (A & 0b100) >> 1
434 * | (A & 0b1)
435 *
436 * Finally, for a non-tiled surface, tile() simply combines together the X and
437 * Y coordinates in the natural way:
438 *
439 * tile(untiled, X, Y, S) = A
440 * where A = Y * pitch + X'
441 * X' = X * cpp
442 * Y' = Y + S * qpitch
443 * detile(untiled, A) = (X, Y, S)
444 * where X = X' / cpp
445 * Y = Y' % qpitch
446 * S = Y' / qpitch
447 * X' = A % pitch
448 * Y' = A / pitch
449 *
450 * (In these formulas, pitch is the number of bytes occupied by a single row
451 * of samples).
452 */
453 class brw_blorp_blit_program
454 {
455 public:
456 brw_blorp_blit_program(struct brw_context *brw,
457 const brw_blorp_blit_prog_key *key);
458 ~brw_blorp_blit_program();
459
460 const GLuint *compile(struct brw_context *brw, GLuint *program_size);
461
462 brw_blorp_prog_data prog_data;
463
464 private:
465 void alloc_regs();
466 void alloc_push_const_regs(int base_reg);
467 void compute_frag_coords();
468 void translate_tiling(bool old_tiled_w, bool new_tiled_w);
469 void encode_msaa(unsigned num_samples, intel_msaa_layout layout);
470 void decode_msaa(unsigned num_samples, intel_msaa_layout layout);
471 void kill_if_outside_dst_rect();
472 void translate_dst_to_src();
473 void single_to_blend();
474 void manual_blend(unsigned num_samples);
475 void sample(struct brw_reg dst);
476 void texel_fetch(struct brw_reg dst);
477 void mcs_fetch();
478 void expand_to_32_bits(struct brw_reg src, struct brw_reg dst);
479 void texture_lookup(struct brw_reg dst, GLuint msg_type,
480 const sampler_message_arg *args, int num_args);
481 void render_target_write();
482
483 /**
484 * Base-2 logarithm of the maximum number of samples that can be blended.
485 */
486 static const unsigned LOG2_MAX_BLEND_SAMPLES = 3;
487
488 void *mem_ctx;
489 struct brw_context *brw;
490 const brw_blorp_blit_prog_key *key;
491 struct brw_compile func;
492
493 /* Thread dispatch header */
494 struct brw_reg R0;
495
496 /* Pixel X/Y coordinates (always in R1). */
497 struct brw_reg R1;
498
499 /* Push constants */
500 struct brw_reg dst_x0;
501 struct brw_reg dst_x1;
502 struct brw_reg dst_y0;
503 struct brw_reg dst_y1;
504 struct {
505 struct brw_reg multiplier;
506 struct brw_reg offset;
507 } x_transform, y_transform;
508
509 /* Data read from texture (4 vec16's per array element) */
510 struct brw_reg texture_data[LOG2_MAX_BLEND_SAMPLES + 1];
511
512 /* Auxiliary storage for the contents of the MCS surface.
513 *
514 * Since the sampler always returns 8 registers worth of data, this is 8
515 * registers wide, even though we only use the first 2 registers of it.
516 */
517 struct brw_reg mcs_data;
518
519 /* X coordinates. We have two of them so that we can perform coordinate
520 * transformations easily.
521 */
522 struct brw_reg x_coords[2];
523
524 /* Y coordinates. We have two of them so that we can perform coordinate
525 * transformations easily.
526 */
527 struct brw_reg y_coords[2];
528
529 /* Which element of x_coords and y_coords is currently in use.
530 */
531 int xy_coord_index;
532
533 /* True if, at the point in the program currently being compiled, the
534 * sample index is known to be zero.
535 */
536 bool s_is_zero;
537
538 /* Register storing the sample index when s_is_zero is false. */
539 struct brw_reg sample_index;
540
541 /* Temporaries */
542 struct brw_reg t1;
543 struct brw_reg t2;
544
545 /* MRF used for sampling and render target writes */
546 GLuint base_mrf;
547 };
548
549 brw_blorp_blit_program::brw_blorp_blit_program(
550 struct brw_context *brw,
551 const brw_blorp_blit_prog_key *key)
552 : mem_ctx(ralloc_context(NULL)),
553 brw(brw),
554 key(key)
555 {
556 brw_init_compile(brw, &func, mem_ctx);
557 }
558
559 brw_blorp_blit_program::~brw_blorp_blit_program()
560 {
561 ralloc_free(mem_ctx);
562 }
563
564 const GLuint *
565 brw_blorp_blit_program::compile(struct brw_context *brw,
566 GLuint *program_size)
567 {
568 /* Sanity checks */
569 if (key->dst_tiled_w && key->rt_samples > 0) {
570 /* If the destination image is W tiled and multisampled, then the thread
571 * must be dispatched once per sample, not once per pixel. This is
572 * necessary because after conversion between W and Y tiling, there's no
573 * guarantee that all samples corresponding to a single pixel will still
574 * be together.
575 */
576 assert(key->persample_msaa_dispatch);
577 }
578
579 if (key->blend) {
580 /* We are blending, which means we won't have an opportunity to
581 * translate the tiling and sample count for the texture surface. So
582 * the surface state for the texture must be configured with the correct
583 * tiling and sample count.
584 */
585 assert(!key->src_tiled_w);
586 assert(key->tex_samples == key->src_samples);
587 assert(key->tex_layout == key->src_layout);
588 assert(key->tex_samples > 0);
589 }
590
591 if (key->persample_msaa_dispatch) {
592 /* It only makes sense to do persample dispatch if the render target is
593 * configured as multisampled.
594 */
595 assert(key->rt_samples > 0);
596 }
597
598 /* Make sure layout is consistent with sample count */
599 assert((key->tex_layout == INTEL_MSAA_LAYOUT_NONE) ==
600 (key->tex_samples == 0));
601 assert((key->rt_layout == INTEL_MSAA_LAYOUT_NONE) ==
602 (key->rt_samples == 0));
603 assert((key->src_layout == INTEL_MSAA_LAYOUT_NONE) ==
604 (key->src_samples == 0));
605 assert((key->dst_layout == INTEL_MSAA_LAYOUT_NONE) ==
606 (key->dst_samples == 0));
607
608 /* Set up prog_data */
609 memset(&prog_data, 0, sizeof(prog_data));
610 prog_data.persample_msaa_dispatch = key->persample_msaa_dispatch;
611
612 brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
613
614 alloc_regs();
615 compute_frag_coords();
616
617 /* Render target and texture hardware don't support W tiling. */
618 const bool rt_tiled_w = false;
619 const bool tex_tiled_w = false;
620
621 /* The address that data will be written to is determined by the
622 * coordinates supplied to the WM thread and the tiling and sample count of
623 * the render target, according to the formula:
624 *
625 * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
626 *
627 * If the actual tiling and sample count of the destination surface are not
628 * the same as the configuration of the render target, then these
629 * coordinates are wrong and we have to adjust them to compensate for the
630 * difference.
631 */
632 if (rt_tiled_w != key->dst_tiled_w ||
633 key->rt_samples != key->dst_samples ||
634 key->rt_layout != key->dst_layout) {
635 encode_msaa(key->rt_samples, key->rt_layout);
636 /* Now (X, Y, S) = detile(rt_tiling, offset) */
637 translate_tiling(rt_tiled_w, key->dst_tiled_w);
638 /* Now (X, Y, S) = detile(dst_tiling, offset) */
639 decode_msaa(key->dst_samples, key->dst_layout);
640 }
641
642 /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
643 *
644 * That is: X, Y and S now contain the true coordinates and sample index of
645 * the data that the WM thread should output.
646 *
647 * If we need to kill pixels that are outside the destination rectangle,
648 * now is the time to do it.
649 */
650
651 if (key->use_kill)
652 kill_if_outside_dst_rect();
653
654 /* Next, apply a translation to obtain coordinates in the source image. */
655 translate_dst_to_src();
656
657 /* If the source image is not multisampled, then we want to fetch sample
658 * number 0, because that's the only sample there is.
659 */
660 if (key->src_samples == 0)
661 s_is_zero = true;
662
663 /* X, Y, and S are now the coordinates of the pixel in the source image
664 * that we want to texture from. Exception: if we are blending, then S is
665 * irrelevant, because we are going to fetch all samples.
666 */
667 if (key->blend) {
668 if (brw->intel.gen == 6) {
669 /* Gen6 hardware an automatically blend using the SAMPLE message */
670 single_to_blend();
671 sample(texture_data[0]);
672 } else {
673 /* Gen7+ hardware doesn't automaticaly blend. */
674 manual_blend(key->src_samples);
675 }
676 } else {
677 /* We aren't blending, which means we just want to fetch a single sample
678 * from the source surface. The address that we want to fetch from is
679 * related to the X, Y and S values according to the formula:
680 *
681 * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
682 *
683 * If the actual tiling and sample count of the source surface are not
684 * the same as the configuration of the texture, then we need to adjust
685 * the coordinates to compensate for the difference.
686 */
687 if (tex_tiled_w != key->src_tiled_w ||
688 key->tex_samples != key->src_samples ||
689 key->tex_layout != key->src_layout) {
690 encode_msaa(key->src_samples, key->src_layout);
691 /* Now (X, Y, S) = detile(src_tiling, offset) */
692 translate_tiling(key->src_tiled_w, tex_tiled_w);
693 /* Now (X, Y, S) = detile(tex_tiling, offset) */
694 decode_msaa(key->tex_samples, key->tex_layout);
695 }
696
697 /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
698 *
699 * In other words: X, Y, and S now contain values which, when passed to
700 * the texturing unit, will cause data to be read from the correct
701 * memory location. So we can fetch the texel now.
702 */
703 if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
704 mcs_fetch();
705 texel_fetch(texture_data[0]);
706 }
707
708 /* Finally, write the fetched (or blended) value to the render target and
709 * terminate the thread.
710 */
711 render_target_write();
712 return brw_get_program(&func, program_size);
713 }
714
715 void
716 brw_blorp_blit_program::alloc_push_const_regs(int base_reg)
717 {
718 #define CONST_LOC(name) offsetof(brw_blorp_wm_push_constants, name)
719 #define ALLOC_REG(name) \
720 this->name = \
721 brw_uw1_reg(BRW_GENERAL_REGISTER_FILE, base_reg, CONST_LOC(name) / 2)
722
723 ALLOC_REG(dst_x0);
724 ALLOC_REG(dst_x1);
725 ALLOC_REG(dst_y0);
726 ALLOC_REG(dst_y1);
727 ALLOC_REG(x_transform.multiplier);
728 ALLOC_REG(x_transform.offset);
729 ALLOC_REG(y_transform.multiplier);
730 ALLOC_REG(y_transform.offset);
731 #undef CONST_LOC
732 #undef ALLOC_REG
733 }
734
735 void
736 brw_blorp_blit_program::alloc_regs()
737 {
738 int reg = 0;
739 this->R0 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
740 this->R1 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
741 prog_data.first_curbe_grf = reg;
742 alloc_push_const_regs(reg);
743 reg += BRW_BLORP_NUM_PUSH_CONST_REGS;
744 for (unsigned i = 0; i < ARRAY_SIZE(texture_data); ++i) {
745 this->texture_data[i] =
746 retype(vec16(brw_vec8_grf(reg, 0)), key->texture_data_type);
747 reg += 8;
748 }
749 this->mcs_data =
750 retype(brw_vec8_grf(reg, 0), BRW_REGISTER_TYPE_UD); reg += 8;
751 for (int i = 0; i < 2; ++i) {
752 this->x_coords[i]
753 = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
754 this->y_coords[i]
755 = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
756 }
757 this->xy_coord_index = 0;
758 this->sample_index
759 = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
760 this->t1 = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
761 this->t2 = vec16(retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW));
762
763 /* Make sure we didn't run out of registers */
764 assert(reg <= GEN7_MRF_HACK_START);
765
766 int mrf = 2;
767 this->base_mrf = mrf;
768 }
769
770 /* In the code that follows, X and Y can be used to quickly refer to the
771 * active elements of x_coords and y_coords, and Xp and Yp ("X prime" and "Y
772 * prime") to the inactive elements.
773 *
774 * S can be used to quickly refer to sample_index.
775 */
776 #define X x_coords[xy_coord_index]
777 #define Y y_coords[xy_coord_index]
778 #define Xp x_coords[!xy_coord_index]
779 #define Yp y_coords[!xy_coord_index]
780 #define S sample_index
781
782 /* Quickly swap the roles of (X, Y) and (Xp, Yp). Saves us from having to do
783 * MOVs to transfor (Xp, Yp) to (X, Y) after a coordinate transformation.
784 */
785 #define SWAP_XY_AND_XPYP() xy_coord_index = !xy_coord_index;
786
787 /**
788 * Emit code to compute the X and Y coordinates of the pixels being rendered
789 * by this WM invocation.
790 *
791 * Assuming the render target is set up for Y tiling, these (X, Y) values are
792 * related to the address offset where outputs will be written by the formula:
793 *
794 * (X, Y, S) = decode_msaa(detile(offset)).
795 *
796 * (See brw_blorp_blit_program).
797 */
798 void
799 brw_blorp_blit_program::compute_frag_coords()
800 {
801 /* R1.2[15:0] = X coordinate of upper left pixel of subspan 0 (pixel 0)
802 * R1.3[15:0] = X coordinate of upper left pixel of subspan 1 (pixel 4)
803 * R1.4[15:0] = X coordinate of upper left pixel of subspan 2 (pixel 8)
804 * R1.5[15:0] = X coordinate of upper left pixel of subspan 3 (pixel 12)
805 *
806 * Pixels within a subspan are laid out in this arrangement:
807 * 0 1
808 * 2 3
809 *
810 * So, to compute the coordinates of each pixel, we need to read every 2nd
811 * 16-bit value (vstride=2) from R1, starting at the 4th 16-bit value
812 * (suboffset=4), and duplicate each value 4 times (hstride=0, width=4).
813 * In other words, the data we want to access is R1.4<2;4,0>UW.
814 *
815 * Then, we need to add the repeating sequence (0, 1, 0, 1, ...) to the
816 * result, since pixels n+1 and n+3 are in the right half of the subspan.
817 */
818 brw_ADD(&func, X, stride(suboffset(R1, 4), 2, 4, 0), brw_imm_v(0x10101010));
819
820 /* Similarly, Y coordinates for subspans come from R1.2[31:16] through
821 * R1.5[31:16], so to get pixel Y coordinates we need to start at the 5th
822 * 16-bit value instead of the 4th (R1.5<2;4,0>UW instead of
823 * R1.4<2;4,0>UW).
824 *
825 * And we need to add the repeating sequence (0, 0, 1, 1, ...), since
826 * pixels n+2 and n+3 are in the bottom half of the subspan.
827 */
828 brw_ADD(&func, Y, stride(suboffset(R1, 5), 2, 4, 0), brw_imm_v(0x11001100));
829
830 if (key->persample_msaa_dispatch) {
831 switch (key->rt_samples) {
832 case 4:
833 /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 4.
834 * Therefore, subspan 0 will represent sample 0, subspan 1 will
835 * represent sample 1, and so on.
836 *
837 * So we need to populate S with the sequence (0, 0, 0, 0, 1, 1, 1,
838 * 1, 2, 2, 2, 2, 3, 3, 3, 3). The easiest way to do this is to
839 * populate a temporary variable with the sequence (0, 1, 2, 3), and
840 * then copy from it using vstride=1, width=4, hstride=0.
841 */
842 brw_MOV(&func, t1, brw_imm_v(0x3210));
843 brw_MOV(&func, S, stride(t1, 1, 4, 0));
844 break;
845 case 8: {
846 /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 8.
847 * Therefore, subspan 0 will represent sample N (where N is 0 or 4),
848 * subspan 1 will represent sample 1, and so on. We can find the
849 * value of N by looking at R0.0 bits 7:6 ("Starting Sample Pair
850 * Index") and multiplying by two (since samples are always delivered
851 * in pairs). That is, we compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 &
852 * 0xc0) >> 5.
853 *
854 * Then we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1, 2,
855 * 2, 2, 2, 3, 3, 3, 3), which we compute by populating a temporary
856 * variable with the sequence (0, 1, 2, 3), and then reading from it
857 * using vstride=1, width=4, hstride=0.
858 */
859 struct brw_reg t1_ud1 = vec1(retype(t1, BRW_REGISTER_TYPE_UD));
860 struct brw_reg r0_ud1 = vec1(retype(R0, BRW_REGISTER_TYPE_UD));
861 brw_AND(&func, t1_ud1, r0_ud1, brw_imm_ud(0xc0));
862 brw_SHR(&func, t1_ud1, t1_ud1, brw_imm_ud(5));
863 brw_MOV(&func, t2, brw_imm_v(0x3210));
864 brw_ADD(&func, S, retype(t1_ud1, BRW_REGISTER_TYPE_UW),
865 stride(t2, 1, 4, 0));
866 break;
867 }
868 default:
869 assert(!"Unrecognized sample count in "
870 "brw_blorp_blit_program::compute_frag_coords()");
871 break;
872 }
873 s_is_zero = false;
874 } else {
875 /* Either the destination surface is single-sampled, or the WM will be
876 * run in MSDISPMODE_PERPIXEL (which causes a single fragment dispatch
877 * per pixel). In either case, it's not meaningful to compute a sample
878 * value. Just set it to 0.
879 */
880 s_is_zero = true;
881 }
882 }
883
884 /**
885 * Emit code to compensate for the difference between Y and W tiling.
886 *
887 * This code modifies the X and Y coordinates according to the formula:
888 *
889 * (X', Y', S') = detile(new_tiling, tile(old_tiling, X, Y, S))
890 *
891 * (See brw_blorp_blit_program).
892 *
893 * It can only translate between W and Y tiling, so new_tiling and old_tiling
894 * are booleans where true represents W tiling and false represents Y tiling.
895 */
896 void
897 brw_blorp_blit_program::translate_tiling(bool old_tiled_w, bool new_tiled_w)
898 {
899 if (old_tiled_w == new_tiled_w)
900 return;
901
902 /* In the code that follows, we can safely assume that S = 0, because W
903 * tiling formats always use IMS layout.
904 */
905 assert(s_is_zero);
906
907 if (new_tiled_w) {
908 /* Given X and Y coordinates that describe an address using Y tiling,
909 * translate to the X and Y coordinates that describe the same address
910 * using W tiling.
911 *
912 * If we break down the low order bits of X and Y, using a
913 * single letter to represent each low-order bit:
914 *
915 * X = A << 7 | 0bBCDEFGH
916 * Y = J << 5 | 0bKLMNP (1)
917 *
918 * Then we can apply the Y tiling formula to see the memory offset being
919 * addressed:
920 *
921 * offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
922 *
923 * If we apply the W detiling formula to this memory location, that the
924 * corresponding X' and Y' coordinates are:
925 *
926 * X' = A << 6 | 0bBCDPFH (3)
927 * Y' = J << 6 | 0bKLMNEG
928 *
929 * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
930 * we need to make the following computation:
931 *
932 * X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
933 * Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
934 */
935 brw_AND(&func, t1, X, brw_imm_uw(0xfff4)); /* X & ~0b1011 */
936 brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b1011) >> 1 */
937 brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
938 brw_SHL(&func, t2, t2, brw_imm_uw(2)); /* (Y & 0b1) << 2 */
939 brw_OR(&func, t1, t1, t2); /* (X & ~0b1011) >> 1 | (Y & 0b1) << 2 */
940 brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
941 brw_OR(&func, Xp, t1, t2);
942 brw_AND(&func, t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
943 brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
944 brw_AND(&func, t2, X, brw_imm_uw(8)); /* X & 0b1000 */
945 brw_SHR(&func, t2, t2, brw_imm_uw(2)); /* (X & 0b1000) >> 2 */
946 brw_OR(&func, t1, t1, t2); /* (Y & ~0b1) << 1 | (X & 0b1000) >> 2 */
947 brw_AND(&func, t2, X, brw_imm_uw(2)); /* X & 0b10 */
948 brw_SHR(&func, t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
949 brw_OR(&func, Yp, t1, t2);
950 SWAP_XY_AND_XPYP();
951 } else {
952 /* Applying the same logic as above, but in reverse, we obtain the
953 * formulas:
954 *
955 * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
956 * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
957 */
958 brw_AND(&func, t1, X, brw_imm_uw(0xfffa)); /* X & ~0b101 */
959 brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b101) << 1 */
960 brw_AND(&func, t2, Y, brw_imm_uw(2)); /* Y & 0b10 */
961 brw_SHL(&func, t2, t2, brw_imm_uw(2)); /* (Y & 0b10) << 2 */
962 brw_OR(&func, t1, t1, t2); /* (X & ~0b101) << 1 | (Y & 0b10) << 2 */
963 brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
964 brw_SHL(&func, t2, t2, brw_imm_uw(1)); /* (Y & 0b1) << 1 */
965 brw_OR(&func, t1, t1, t2); /* (X & ~0b101) << 1 | (Y & 0b10) << 2
966 | (Y & 0b1) << 1 */
967 brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
968 brw_OR(&func, Xp, t1, t2);
969 brw_AND(&func, t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
970 brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
971 brw_AND(&func, t2, X, brw_imm_uw(4)); /* X & 0b100 */
972 brw_SHR(&func, t2, t2, brw_imm_uw(2)); /* (X & 0b100) >> 2 */
973 brw_OR(&func, Yp, t1, t2);
974 SWAP_XY_AND_XPYP();
975 }
976 }
977
978 /**
979 * Emit code to compensate for the difference between MSAA and non-MSAA
980 * surfaces.
981 *
982 * This code modifies the X and Y coordinates according to the formula:
983 *
984 * (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
985 *
986 * (See brw_blorp_blit_program).
987 */
988 void
989 brw_blorp_blit_program::encode_msaa(unsigned num_samples,
990 intel_msaa_layout layout)
991 {
992 switch (layout) {
993 case INTEL_MSAA_LAYOUT_NONE:
994 /* No translation necessary, and S should already be zero. */
995 assert(s_is_zero);
996 break;
997 case INTEL_MSAA_LAYOUT_CMS:
998 /* We can't compensate for compressed layout since at this point in the
999 * program we haven't read from the MCS buffer.
1000 */
1001 assert(!"Bad layout in encode_msaa");
1002 break;
1003 case INTEL_MSAA_LAYOUT_UMS:
1004 /* No translation necessary. */
1005 break;
1006 case INTEL_MSAA_LAYOUT_IMS:
1007 switch (num_samples) {
1008 case 4:
1009 /* encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
1010 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
1011 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1012 */
1013 brw_AND(&func, t1, X, brw_imm_uw(0xfffe)); /* X & ~0b1 */
1014 if (!s_is_zero) {
1015 brw_AND(&func, t2, S, brw_imm_uw(1)); /* S & 0b1 */
1016 brw_OR(&func, t1, t1, t2); /* (X & ~0b1) | (S & 0b1) */
1017 }
1018 brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b1) << 1
1019 | (S & 0b1) << 1 */
1020 brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
1021 brw_OR(&func, Xp, t1, t2);
1022 brw_AND(&func, t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1023 brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1024 if (!s_is_zero) {
1025 brw_AND(&func, t2, S, brw_imm_uw(2)); /* S & 0b10 */
1026 brw_OR(&func, t1, t1, t2); /* (Y & ~0b1) << 1 | (S & 0b10) */
1027 }
1028 brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
1029 brw_OR(&func, Yp, t1, t2);
1030 break;
1031 case 8:
1032 /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
1033 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
1034 * | (X & 0b1)
1035 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1036 */
1037 brw_AND(&func, t1, X, brw_imm_uw(0xfffe)); /* X & ~0b1 */
1038 brw_SHL(&func, t1, t1, brw_imm_uw(2)); /* (X & ~0b1) << 2 */
1039 if (!s_is_zero) {
1040 brw_AND(&func, t2, S, brw_imm_uw(4)); /* S & 0b100 */
1041 brw_OR(&func, t1, t1, t2); /* (X & ~0b1) << 2 | (S & 0b100) */
1042 brw_AND(&func, t2, S, brw_imm_uw(1)); /* S & 0b1 */
1043 brw_SHL(&func, t2, t2, brw_imm_uw(1)); /* (S & 0b1) << 1 */
1044 brw_OR(&func, t1, t1, t2); /* (X & ~0b1) << 2 | (S & 0b100)
1045 | (S & 0b1) << 1 */
1046 }
1047 brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
1048 brw_OR(&func, Xp, t1, t2);
1049 brw_AND(&func, t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1050 brw_SHL(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1051 if (!s_is_zero) {
1052 brw_AND(&func, t2, S, brw_imm_uw(2)); /* S & 0b10 */
1053 brw_OR(&func, t1, t1, t2); /* (Y & ~0b1) << 1 | (S & 0b10) */
1054 }
1055 brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
1056 brw_OR(&func, Yp, t1, t2);
1057 break;
1058 }
1059 SWAP_XY_AND_XPYP();
1060 s_is_zero = true;
1061 break;
1062 }
1063 }
1064
1065 /**
1066 * Emit code to compensate for the difference between MSAA and non-MSAA
1067 * surfaces.
1068 *
1069 * This code modifies the X and Y coordinates according to the formula:
1070 *
1071 * (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
1072 *
1073 * (See brw_blorp_blit_program).
1074 */
1075 void
1076 brw_blorp_blit_program::decode_msaa(unsigned num_samples,
1077 intel_msaa_layout layout)
1078 {
1079 switch (layout) {
1080 case INTEL_MSAA_LAYOUT_NONE:
1081 /* No translation necessary, and S should already be zero. */
1082 assert(s_is_zero);
1083 break;
1084 case INTEL_MSAA_LAYOUT_CMS:
1085 /* We can't compensate for compressed layout since at this point in the
1086 * program we don't have access to the MCS buffer.
1087 */
1088 assert(!"Bad layout in encode_msaa");
1089 break;
1090 case INTEL_MSAA_LAYOUT_UMS:
1091 /* No translation necessary. */
1092 break;
1093 case INTEL_MSAA_LAYOUT_IMS:
1094 assert(s_is_zero);
1095 switch (num_samples) {
1096 case 4:
1097 /* decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
1098 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
1099 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1100 * S = (Y & 0b10) | (X & 0b10) >> 1
1101 */
1102 brw_AND(&func, t1, X, brw_imm_uw(0xfffc)); /* X & ~0b11 */
1103 brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (X & ~0b11) >> 1 */
1104 brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
1105 brw_OR(&func, Xp, t1, t2);
1106 brw_AND(&func, t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1107 brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1108 brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
1109 brw_OR(&func, Yp, t1, t2);
1110 brw_AND(&func, t1, Y, brw_imm_uw(2)); /* Y & 0b10 */
1111 brw_AND(&func, t2, X, brw_imm_uw(2)); /* X & 0b10 */
1112 brw_SHR(&func, t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1113 brw_OR(&func, S, t1, t2);
1114 break;
1115 case 8:
1116 /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
1117 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
1118 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1119 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
1120 */
1121 brw_AND(&func, t1, X, brw_imm_uw(0xfff8)); /* X & ~0b111 */
1122 brw_SHR(&func, t1, t1, brw_imm_uw(2)); /* (X & ~0b111) >> 2 */
1123 brw_AND(&func, t2, X, brw_imm_uw(1)); /* X & 0b1 */
1124 brw_OR(&func, Xp, t1, t2);
1125 brw_AND(&func, t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1126 brw_SHR(&func, t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1127 brw_AND(&func, t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
1128 brw_OR(&func, Yp, t1, t2);
1129 brw_AND(&func, t1, X, brw_imm_uw(4)); /* X & 0b100 */
1130 brw_AND(&func, t2, Y, brw_imm_uw(2)); /* Y & 0b10 */
1131 brw_OR(&func, t1, t1, t2); /* (X & 0b100) | (Y & 0b10) */
1132 brw_AND(&func, t2, X, brw_imm_uw(2)); /* X & 0b10 */
1133 brw_SHR(&func, t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1134 brw_OR(&func, S, t1, t2);
1135 break;
1136 }
1137 s_is_zero = false;
1138 SWAP_XY_AND_XPYP();
1139 break;
1140 }
1141 }
1142
1143 /**
1144 * Emit code that kills pixels whose X and Y coordinates are outside the
1145 * boundary of the rectangle defined by the push constants (dst_x0, dst_y0,
1146 * dst_x1, dst_y1).
1147 */
1148 void
1149 brw_blorp_blit_program::kill_if_outside_dst_rect()
1150 {
1151 struct brw_reg f0 = brw_flag_reg();
1152 struct brw_reg g1 = retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW);
1153 struct brw_reg null16 = vec16(retype(brw_null_reg(), BRW_REGISTER_TYPE_UW));
1154
1155 brw_CMP(&func, null16, BRW_CONDITIONAL_GE, X, dst_x0);
1156 brw_CMP(&func, null16, BRW_CONDITIONAL_GE, Y, dst_y0);
1157 brw_CMP(&func, null16, BRW_CONDITIONAL_L, X, dst_x1);
1158 brw_CMP(&func, null16, BRW_CONDITIONAL_L, Y, dst_y1);
1159
1160 brw_set_predicate_control(&func, BRW_PREDICATE_NONE);
1161 brw_push_insn_state(&func);
1162 brw_set_mask_control(&func, BRW_MASK_DISABLE);
1163 brw_AND(&func, g1, f0, g1);
1164 brw_pop_insn_state(&func);
1165 }
1166
1167 /**
1168 * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
1169 * coordinates.
1170 */
1171 void
1172 brw_blorp_blit_program::translate_dst_to_src()
1173 {
1174 brw_MUL(&func, Xp, X, x_transform.multiplier);
1175 brw_MUL(&func, Yp, Y, y_transform.multiplier);
1176 brw_ADD(&func, Xp, Xp, x_transform.offset);
1177 brw_ADD(&func, Yp, Yp, y_transform.offset);
1178 SWAP_XY_AND_XPYP();
1179 }
1180
1181 /**
1182 * Emit code to transform the X and Y coordinates as needed for blending
1183 * together the different samples in an MSAA texture.
1184 */
1185 void
1186 brw_blorp_blit_program::single_to_blend()
1187 {
1188 /* When looking up samples in an MSAA texture using the SAMPLE message,
1189 * Gen6 requires the texture coordinates to be odd integers (so that they
1190 * correspond to the center of a 2x2 block representing the four samples
1191 * that maxe up a pixel). So we need to multiply our X and Y coordinates
1192 * each by 2 and then add 1.
1193 */
1194 brw_SHL(&func, t1, X, brw_imm_w(1));
1195 brw_SHL(&func, t2, Y, brw_imm_w(1));
1196 brw_ADD(&func, Xp, t1, brw_imm_w(1));
1197 brw_ADD(&func, Yp, t2, brw_imm_w(1));
1198 SWAP_XY_AND_XPYP();
1199 }
1200
1201
1202 /**
1203 * Count the number of trailing 1 bits in the given value. For example:
1204 *
1205 * count_trailing_one_bits(0) == 0
1206 * count_trailing_one_bits(7) == 3
1207 * count_trailing_one_bits(11) == 2
1208 */
1209 inline int count_trailing_one_bits(unsigned value)
1210 {
1211 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
1212 return __builtin_ctz(~value);
1213 #else
1214 return _mesa_bitcount(value & ~(value + 1));
1215 #endif
1216 }
1217
1218
1219 void
1220 brw_blorp_blit_program::manual_blend(unsigned num_samples)
1221 {
1222 if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
1223 mcs_fetch();
1224
1225 /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
1226 *
1227 * result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
1228 *
1229 * This ensures that when all samples have the same value, no numerical
1230 * precision is lost, since each addition operation always adds two equal
1231 * values, and summing two equal floating point values does not lose
1232 * precision.
1233 *
1234 * We perform this computation by treating the texture_data array as a
1235 * stack and performing the following operations:
1236 *
1237 * - push sample 0 onto stack
1238 * - push sample 1 onto stack
1239 * - add top two stack entries
1240 * - push sample 2 onto stack
1241 * - push sample 3 onto stack
1242 * - add top two stack entries
1243 * - add top two stack entries
1244 * - divide top stack entry by 4
1245 *
1246 * Note that after pushing sample i onto the stack, the number of add
1247 * operations we do is equal to the number of trailing 1 bits in i. This
1248 * works provided the total number of samples is a power of two, which it
1249 * always is for i965.
1250 *
1251 * For integer formats, we replace the add operations with average
1252 * operations and skip the final division.
1253 */
1254 typedef struct brw_instruction *(*brw_op2_ptr)(struct brw_compile *,
1255 struct brw_reg,
1256 struct brw_reg,
1257 struct brw_reg);
1258 brw_op2_ptr combine_op =
1259 key->texture_data_type == BRW_REGISTER_TYPE_F ? brw_ADD : brw_AVG;
1260 unsigned stack_depth = 0;
1261 for (unsigned i = 0; i < num_samples; ++i) {
1262 assert(stack_depth == _mesa_bitcount(i)); /* Loop invariant */
1263
1264 /* Push sample i onto the stack */
1265 assert(stack_depth < ARRAY_SIZE(texture_data));
1266 if (i == 0) {
1267 s_is_zero = true;
1268 } else {
1269 s_is_zero = false;
1270 brw_MOV(&func, S, brw_imm_uw(i));
1271 }
1272 texel_fetch(texture_data[stack_depth++]);
1273
1274 if (i == 0 && key->tex_layout == INTEL_MSAA_LAYOUT_CMS) {
1275 /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
1276 * suggests an optimization:
1277 *
1278 * "A simple optimization with probable large return in
1279 * performance is to compare the MCS value to zero (indicating
1280 * all samples are on sample slice 0), and sample only from
1281 * sample slice 0 using ld2dss if MCS is zero."
1282 *
1283 * Note that in the case where the MCS value is zero, sampling from
1284 * sample slice 0 using ld2dss and sampling from sample 0 using
1285 * ld2dms are equivalent (since all samples are on sample slice 0).
1286 * Since we have already sampled from sample 0, all we need to do is
1287 * skip the remaining fetches and averaging if MCS is zero.
1288 */
1289 brw_CMP(&func, vec16(brw_null_reg()), BRW_CONDITIONAL_NZ,
1290 mcs_data, brw_imm_ud(0));
1291 brw_IF(&func, BRW_EXECUTE_16);
1292 }
1293
1294 /* Do count_trailing_one_bits(i) times */
1295 for (int j = count_trailing_one_bits(i); j-- > 0; ) {
1296 assert(stack_depth >= 2);
1297 --stack_depth;
1298
1299 /* TODO: should use a smaller loop bound for non_RGBA formats */
1300 for (int k = 0; k < 4; ++k) {
1301 combine_op(&func, offset(texture_data[stack_depth - 1], 2*k),
1302 offset(vec8(texture_data[stack_depth - 1]), 2*k),
1303 offset(vec8(texture_data[stack_depth]), 2*k));
1304 }
1305 }
1306 }
1307
1308 /* We should have just 1 sample on the stack now. */
1309 assert(stack_depth == 1);
1310
1311 if (key->texture_data_type == BRW_REGISTER_TYPE_F) {
1312 /* Scale the result down by a factor of num_samples */
1313 /* TODO: should use a smaller loop bound for non-RGBA formats */
1314 for (int j = 0; j < 4; ++j) {
1315 brw_MUL(&func, offset(texture_data[0], 2*j),
1316 offset(vec8(texture_data[0]), 2*j),
1317 brw_imm_f(1.0/num_samples));
1318 }
1319 }
1320
1321 if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
1322 brw_ENDIF(&func);
1323 }
1324
1325 /**
1326 * Emit code to look up a value in the texture using the SAMPLE message (which
1327 * does blending of MSAA surfaces).
1328 */
1329 void
1330 brw_blorp_blit_program::sample(struct brw_reg dst)
1331 {
1332 static const sampler_message_arg args[2] = {
1333 SAMPLER_MESSAGE_ARG_U_FLOAT,
1334 SAMPLER_MESSAGE_ARG_V_FLOAT
1335 };
1336
1337 texture_lookup(dst, GEN5_SAMPLER_MESSAGE_SAMPLE, args, ARRAY_SIZE(args));
1338 }
1339
1340 /**
1341 * Emit code to look up a value in the texture using the SAMPLE_LD message
1342 * (which does a simple texel fetch).
1343 */
1344 void
1345 brw_blorp_blit_program::texel_fetch(struct brw_reg dst)
1346 {
1347 static const sampler_message_arg gen6_args[5] = {
1348 SAMPLER_MESSAGE_ARG_U_INT,
1349 SAMPLER_MESSAGE_ARG_V_INT,
1350 SAMPLER_MESSAGE_ARG_ZERO_INT, /* R */
1351 SAMPLER_MESSAGE_ARG_ZERO_INT, /* LOD */
1352 SAMPLER_MESSAGE_ARG_SI_INT
1353 };
1354 static const sampler_message_arg gen7_ld_args[3] = {
1355 SAMPLER_MESSAGE_ARG_U_INT,
1356 SAMPLER_MESSAGE_ARG_ZERO_INT, /* LOD */
1357 SAMPLER_MESSAGE_ARG_V_INT
1358 };
1359 static const sampler_message_arg gen7_ld2dss_args[3] = {
1360 SAMPLER_MESSAGE_ARG_SI_INT,
1361 SAMPLER_MESSAGE_ARG_U_INT,
1362 SAMPLER_MESSAGE_ARG_V_INT
1363 };
1364 static const sampler_message_arg gen7_ld2dms_args[4] = {
1365 SAMPLER_MESSAGE_ARG_SI_INT,
1366 SAMPLER_MESSAGE_ARG_MCS_INT,
1367 SAMPLER_MESSAGE_ARG_U_INT,
1368 SAMPLER_MESSAGE_ARG_V_INT
1369 };
1370
1371 switch (brw->intel.gen) {
1372 case 6:
1373 texture_lookup(dst, GEN5_SAMPLER_MESSAGE_SAMPLE_LD, gen6_args,
1374 s_is_zero ? 2 : 5);
1375 break;
1376 case 7:
1377 switch (key->tex_layout) {
1378 case INTEL_MSAA_LAYOUT_IMS:
1379 /* From the Ivy Bridge PRM, Vol4 Part1 p72 (Multisampled Surface Storage
1380 * Format):
1381 *
1382 * If this field is MSFMT_DEPTH_STENCIL
1383 * [a.k.a. INTEL_MSAA_LAYOUT_IMS], the only sampling engine
1384 * messages allowed are "ld2dms", "resinfo", and "sampleinfo".
1385 *
1386 * So fall through to emit the same message as we use for
1387 * INTEL_MSAA_LAYOUT_CMS.
1388 */
1389 case INTEL_MSAA_LAYOUT_CMS:
1390 texture_lookup(dst, GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS,
1391 gen7_ld2dms_args, ARRAY_SIZE(gen7_ld2dms_args));
1392 break;
1393 case INTEL_MSAA_LAYOUT_UMS:
1394 texture_lookup(dst, GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS,
1395 gen7_ld2dss_args, ARRAY_SIZE(gen7_ld2dss_args));
1396 break;
1397 case INTEL_MSAA_LAYOUT_NONE:
1398 assert(s_is_zero);
1399 texture_lookup(dst, GEN5_SAMPLER_MESSAGE_SAMPLE_LD, gen7_ld_args,
1400 ARRAY_SIZE(gen7_ld_args));
1401 break;
1402 }
1403 break;
1404 default:
1405 assert(!"Should not get here.");
1406 break;
1407 };
1408 }
1409
1410 void
1411 brw_blorp_blit_program::mcs_fetch()
1412 {
1413 static const sampler_message_arg gen7_ld_mcs_args[2] = {
1414 SAMPLER_MESSAGE_ARG_U_INT,
1415 SAMPLER_MESSAGE_ARG_V_INT
1416 };
1417 texture_lookup(vec16(mcs_data), GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS,
1418 gen7_ld_mcs_args, ARRAY_SIZE(gen7_ld_mcs_args));
1419 }
1420
1421 void
1422 brw_blorp_blit_program::expand_to_32_bits(struct brw_reg src,
1423 struct brw_reg dst)
1424 {
1425 brw_MOV(&func, vec8(dst), vec8(src));
1426 brw_set_compression_control(&func, BRW_COMPRESSION_2NDHALF);
1427 brw_MOV(&func, offset(vec8(dst), 1), suboffset(vec8(src), 8));
1428 brw_set_compression_control(&func, BRW_COMPRESSION_NONE);
1429 }
1430
1431 void
1432 brw_blorp_blit_program::texture_lookup(struct brw_reg dst,
1433 GLuint msg_type,
1434 const sampler_message_arg *args,
1435 int num_args)
1436 {
1437 struct brw_reg mrf =
1438 retype(vec16(brw_message_reg(base_mrf)), BRW_REGISTER_TYPE_UD);
1439 for (int arg = 0; arg < num_args; ++arg) {
1440 switch (args[arg]) {
1441 case SAMPLER_MESSAGE_ARG_U_FLOAT:
1442 expand_to_32_bits(X, retype(mrf, BRW_REGISTER_TYPE_F));
1443 break;
1444 case SAMPLER_MESSAGE_ARG_V_FLOAT:
1445 expand_to_32_bits(Y, retype(mrf, BRW_REGISTER_TYPE_F));
1446 break;
1447 case SAMPLER_MESSAGE_ARG_U_INT:
1448 expand_to_32_bits(X, mrf);
1449 break;
1450 case SAMPLER_MESSAGE_ARG_V_INT:
1451 expand_to_32_bits(Y, mrf);
1452 break;
1453 case SAMPLER_MESSAGE_ARG_SI_INT:
1454 /* Note: on Gen7, this code may be reached with s_is_zero==true
1455 * because in Gen7's ld2dss message, the sample index is the first
1456 * argument. When this happens, we need to move a 0 into the
1457 * appropriate message register.
1458 */
1459 if (s_is_zero)
1460 brw_MOV(&func, mrf, brw_imm_ud(0));
1461 else
1462 expand_to_32_bits(S, mrf);
1463 break;
1464 case SAMPLER_MESSAGE_ARG_MCS_INT:
1465 switch (key->tex_layout) {
1466 case INTEL_MSAA_LAYOUT_CMS:
1467 brw_MOV(&func, mrf, mcs_data);
1468 break;
1469 case INTEL_MSAA_LAYOUT_IMS:
1470 /* When sampling from an IMS surface, MCS data is not relevant,
1471 * and the hardware ignores it. So don't bother populating it.
1472 */
1473 break;
1474 default:
1475 /* We shouldn't be trying to send MCS data with any other
1476 * layouts.
1477 */
1478 assert (!"Unsupported layout for MCS data");
1479 break;
1480 }
1481 break;
1482 case SAMPLER_MESSAGE_ARG_ZERO_INT:
1483 brw_MOV(&func, mrf, brw_imm_ud(0));
1484 break;
1485 }
1486 mrf.nr += 2;
1487 }
1488
1489 brw_SAMPLE(&func,
1490 retype(dst, BRW_REGISTER_TYPE_UW) /* dest */,
1491 base_mrf /* msg_reg_nr */,
1492 brw_message_reg(base_mrf) /* src0 */,
1493 BRW_BLORP_TEXTURE_BINDING_TABLE_INDEX,
1494 0 /* sampler */,
1495 WRITEMASK_XYZW,
1496 msg_type,
1497 8 /* response_length. TODO: should be smaller for non-RGBA formats? */,
1498 mrf.nr - base_mrf /* msg_length */,
1499 0 /* header_present */,
1500 BRW_SAMPLER_SIMD_MODE_SIMD16,
1501 BRW_SAMPLER_RETURN_FORMAT_FLOAT32);
1502 }
1503
1504 #undef X
1505 #undef Y
1506 #undef U
1507 #undef V
1508 #undef S
1509 #undef SWAP_XY_AND_XPYP
1510
1511 void
1512 brw_blorp_blit_program::render_target_write()
1513 {
1514 struct brw_reg mrf_rt_write =
1515 retype(vec16(brw_message_reg(base_mrf)), key->texture_data_type);
1516 int mrf_offset = 0;
1517
1518 /* If we may have killed pixels, then we need to send R0 and R1 in a header
1519 * so that the render target knows which pixels we killed.
1520 */
1521 bool use_header = key->use_kill;
1522 if (use_header) {
1523 /* Copy R0/1 to MRF */
1524 brw_MOV(&func, retype(mrf_rt_write, BRW_REGISTER_TYPE_UD),
1525 retype(R0, BRW_REGISTER_TYPE_UD));
1526 mrf_offset += 2;
1527 }
1528
1529 /* Copy texture data to MRFs */
1530 for (int i = 0; i < 4; ++i) {
1531 /* E.g. mov(16) m2.0<1>:f r2.0<8;8,1>:f { Align1, H1 } */
1532 brw_MOV(&func, offset(mrf_rt_write, mrf_offset),
1533 offset(vec8(texture_data[0]), 2*i));
1534 mrf_offset += 2;
1535 }
1536
1537 /* Now write to the render target and terminate the thread */
1538 brw_fb_WRITE(&func,
1539 16 /* dispatch_width */,
1540 base_mrf /* msg_reg_nr */,
1541 mrf_rt_write /* src0 */,
1542 BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE,
1543 BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX,
1544 mrf_offset /* msg_length. TODO: Should be smaller for non-RGBA formats. */,
1545 0 /* response_length */,
1546 true /* eot */,
1547 use_header);
1548 }
1549
1550
1551 void
1552 brw_blorp_coord_transform_params::setup(GLuint src0, GLuint dst0, GLuint dst1,
1553 bool mirror)
1554 {
1555 if (!mirror) {
1556 /* When not mirroring a coordinate (say, X), we need:
1557 * x' - src_x0 = x - dst_x0
1558 * Therefore:
1559 * x' = 1*x + (src_x0 - dst_x0)
1560 */
1561 multiplier = 1;
1562 offset = src0 - dst0;
1563 } else {
1564 /* When mirroring X we need:
1565 * x' - src_x0 = dst_x1 - x - 1
1566 * Therefore:
1567 * x' = -1*x + (src_x0 + dst_x1 - 1)
1568 */
1569 multiplier = -1;
1570 offset = src0 + dst1 - 1;
1571 }
1572 }
1573
1574
1575 /**
1576 * Determine which MSAA layout the GPU pipeline should be configured for,
1577 * based on the chip generation, the number of samples, and the true layout of
1578 * the image in memory.
1579 */
1580 inline intel_msaa_layout
1581 compute_msaa_layout_for_pipeline(struct brw_context *brw, unsigned num_samples,
1582 intel_msaa_layout true_layout)
1583 {
1584 if (num_samples == 0) {
1585 /* When configuring the GPU for non-MSAA, we can still accommodate IMS
1586 * format buffers, by transforming coordinates appropriately.
1587 */
1588 assert(true_layout == INTEL_MSAA_LAYOUT_NONE ||
1589 true_layout == INTEL_MSAA_LAYOUT_IMS);
1590 return INTEL_MSAA_LAYOUT_NONE;
1591 } else {
1592 assert(true_layout != INTEL_MSAA_LAYOUT_NONE);
1593 }
1594
1595 /* Prior to Gen7, all MSAA surfaces use IMS layout. */
1596 if (brw->intel.gen == 6) {
1597 assert(true_layout == INTEL_MSAA_LAYOUT_IMS);
1598 }
1599
1600 return true_layout;
1601 }
1602
1603
1604 brw_blorp_blit_params::brw_blorp_blit_params(struct brw_context *brw,
1605 struct intel_mipmap_tree *src_mt,
1606 struct intel_mipmap_tree *dst_mt,
1607 GLuint src_x0, GLuint src_y0,
1608 GLuint dst_x0, GLuint dst_y0,
1609 GLuint dst_x1, GLuint dst_y1,
1610 bool mirror_x, bool mirror_y)
1611 {
1612 src.set(brw, src_mt, 0, 0);
1613 dst.set(brw, dst_mt, 0, 0);
1614
1615 use_wm_prog = true;
1616 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
1617
1618 /* texture_data_type indicates the register type that should be used to
1619 * manipulate texture data.
1620 */
1621 switch (_mesa_get_format_datatype(src_mt->format)) {
1622 case GL_UNSIGNED_NORMALIZED:
1623 case GL_SIGNED_NORMALIZED:
1624 case GL_FLOAT:
1625 wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_F;
1626 break;
1627 case GL_UNSIGNED_INT:
1628 if (src_mt->format == MESA_FORMAT_S8) {
1629 /* We process stencil as though it's an unsigned normalized color */
1630 wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_F;
1631 } else {
1632 wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_UD;
1633 }
1634 break;
1635 case GL_INT:
1636 wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_D;
1637 break;
1638 default:
1639 assert(!"Unrecognized blorp format");
1640 break;
1641 }
1642
1643 if (brw->intel.gen > 6) {
1644 /* Gen7's rendering hardware only supports the IMS layout for depth and
1645 * stencil render targets. Blorp always maps its destination surface as
1646 * a color render target (even if it's actually a depth or stencil
1647 * buffer). So if the destination is IMS, we'll have to map it as a
1648 * single-sampled texture and interleave the samples ourselves.
1649 */
1650 if (dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS)
1651 dst.num_samples = 0;
1652 }
1653
1654 if (dst.map_stencil_as_y_tiled && dst.num_samples > 0) {
1655 /* If the destination surface is a W-tiled multisampled stencil buffer
1656 * that we're mapping as Y tiled, then we need to arrange for the WM
1657 * program to run once per sample rather than once per pixel, because
1658 * the memory layout of related samples doesn't match between W and Y
1659 * tiling.
1660 */
1661 wm_prog_key.persample_msaa_dispatch = true;
1662 }
1663
1664 if (src.num_samples > 0 && dst.num_samples > 0) {
1665 /* We are blitting from a multisample buffer to a multisample buffer, so
1666 * we must preserve samples within a pixel. This means we have to
1667 * arrange for the WM program to run once per sample rather than once
1668 * per pixel.
1669 */
1670 wm_prog_key.persample_msaa_dispatch = true;
1671 }
1672
1673 /* The render path must be configured to use the same number of samples as
1674 * the destination buffer.
1675 */
1676 num_samples = dst.num_samples;
1677
1678 GLenum base_format = _mesa_get_format_base_format(src_mt->format);
1679 if (base_format != GL_DEPTH_COMPONENT && /* TODO: what about depth/stencil? */
1680 base_format != GL_STENCIL_INDEX &&
1681 src_mt->num_samples > 0 && dst_mt->num_samples == 0) {
1682 /* We are downsampling a color buffer, so blend. */
1683 wm_prog_key.blend = true;
1684 }
1685
1686 /* src_samples and dst_samples are the true sample counts */
1687 wm_prog_key.src_samples = src_mt->num_samples;
1688 wm_prog_key.dst_samples = dst_mt->num_samples;
1689
1690 /* tex_samples and rt_samples are the sample counts that are set up in
1691 * SURFACE_STATE.
1692 */
1693 wm_prog_key.tex_samples = src.num_samples;
1694 wm_prog_key.rt_samples = dst.num_samples;
1695
1696 /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
1697 * use to access the source and destination surfaces.
1698 */
1699 wm_prog_key.tex_layout =
1700 compute_msaa_layout_for_pipeline(brw, src.num_samples, src.msaa_layout);
1701 wm_prog_key.rt_layout =
1702 compute_msaa_layout_for_pipeline(brw, dst.num_samples, dst.msaa_layout);
1703
1704 /* src_layout and dst_layout indicate the true MSAA layout used by src and
1705 * dst.
1706 */
1707 wm_prog_key.src_layout = src_mt->msaa_layout;
1708 wm_prog_key.dst_layout = dst_mt->msaa_layout;
1709
1710 wm_prog_key.src_tiled_w = src.map_stencil_as_y_tiled;
1711 wm_prog_key.dst_tiled_w = dst.map_stencil_as_y_tiled;
1712 x0 = wm_push_consts.dst_x0 = dst_x0;
1713 y0 = wm_push_consts.dst_y0 = dst_y0;
1714 x1 = wm_push_consts.dst_x1 = dst_x1;
1715 y1 = wm_push_consts.dst_y1 = dst_y1;
1716 wm_push_consts.x_transform.setup(src_x0, dst_x0, dst_x1, mirror_x);
1717 wm_push_consts.y_transform.setup(src_y0, dst_y0, dst_y1, mirror_y);
1718
1719 if (dst.num_samples == 0 && dst_mt->num_samples > 0) {
1720 /* We must expand the rectangle we send through the rendering pipeline,
1721 * to account for the fact that we are mapping the destination region as
1722 * single-sampled when it is in fact multisampled. We must also align
1723 * it to a multiple of the multisampling pattern, because the
1724 * differences between multisampled and single-sampled surface formats
1725 * will mean that pixels are scrambled within the multisampling pattern.
1726 * TODO: what if this makes the coordinates too large?
1727 *
1728 * Note: this only works if the destination surface uses the IMS layout.
1729 * If it's UMS, then we have no choice but to set up the rendering
1730 * pipeline as multisampled.
1731 */
1732 assert(dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS);
1733 switch (dst_mt->num_samples) {
1734 case 4:
1735 x0 = (x0 * 2) & ~3;
1736 y0 = (y0 * 2) & ~3;
1737 x1 = ALIGN(x1 * 2, 4);
1738 y1 = ALIGN(y1 * 2, 4);
1739 break;
1740 case 8:
1741 x0 = (x0 * 4) & ~7;
1742 y0 = (y0 * 2) & ~3;
1743 x1 = ALIGN(x1 * 4, 8);
1744 y1 = ALIGN(y1 * 2, 4);
1745 break;
1746 default:
1747 assert(!"Unrecognized sample count in brw_blorp_blit_params ctor");
1748 break;
1749 }
1750 wm_prog_key.use_kill = true;
1751 }
1752
1753 if (dst.map_stencil_as_y_tiled) {
1754 /* We must modify the rectangle we send through the rendering pipeline,
1755 * to account for the fact that we are mapping it as Y-tiled when it is
1756 * in fact W-tiled. Y tiles have dimensions 128x32 whereas W tiles have
1757 * dimensions 64x64. We must also align it to a multiple of the tile
1758 * size, because the differences between W and Y tiling formats will
1759 * mean that pixels are scrambled within the tile.
1760 *
1761 * Note: if the destination surface configured to use IMS layout, then
1762 * the effective tile size we need to align it to is smaller, because
1763 * each pixel covers a 2x2 or a 4x2 block of samples.
1764 *
1765 * TODO: what if this makes the coordinates too large?
1766 */
1767 unsigned x_align = 64, y_align = 64;
1768 if (dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS) {
1769 x_align /= (dst_mt->num_samples == 4 ? 2 : 4);
1770 y_align /= 2;
1771 }
1772 x0 = (x0 & ~(x_align - 1)) * 2;
1773 y0 = (y0 & ~(y_align - 1)) / 2;
1774 x1 = ALIGN(x1, x_align) * 2;
1775 y1 = ALIGN(y1, y_align) / 2;
1776 wm_prog_key.use_kill = true;
1777 }
1778 }
1779
1780 uint32_t
1781 brw_blorp_blit_params::get_wm_prog(struct brw_context *brw,
1782 brw_blorp_prog_data **prog_data) const
1783 {
1784 uint32_t prog_offset;
1785 if (!brw_search_cache(&brw->cache, BRW_BLORP_BLIT_PROG,
1786 &this->wm_prog_key, sizeof(this->wm_prog_key),
1787 &prog_offset, prog_data)) {
1788 brw_blorp_blit_program prog(brw, &this->wm_prog_key);
1789 GLuint program_size;
1790 const GLuint *program = prog.compile(brw, &program_size);
1791 brw_upload_cache(&brw->cache, BRW_BLORP_BLIT_PROG,
1792 &this->wm_prog_key, sizeof(this->wm_prog_key),
1793 program, program_size,
1794 &prog.prog_data, sizeof(prog.prog_data),
1795 &prog_offset, prog_data);
1796 }
1797 return prog_offset;
1798 }