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