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