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