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