i965/blorp: Add support for averaging resolves to the NIR path
[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 "compiler/nir/nir_builder.h"
29
30 #include "intel_fbo.h"
31
32 #include "brw_blorp.h"
33 #include "brw_context.h"
34 #include "brw_blorp_blit_eu.h"
35 #include "brw_state.h"
36 #include "brw_meta_util.h"
37
38 #define FILE_DEBUG_FLAG DEBUG_BLORP
39
40 static struct intel_mipmap_tree *
41 find_miptree(GLbitfield buffer_bit, struct intel_renderbuffer *irb)
42 {
43 struct intel_mipmap_tree *mt = irb->mt;
44 if (buffer_bit == GL_STENCIL_BUFFER_BIT && mt->stencil_mt)
45 mt = mt->stencil_mt;
46 return mt;
47 }
48
49 static int
50 blorp_get_texture_swizzle(const struct intel_renderbuffer *irb)
51 {
52 return irb->Base.Base._BaseFormat == GL_RGB ?
53 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE) :
54 SWIZZLE_XYZW;
55 }
56
57 static void
58 do_blorp_blit(struct brw_context *brw, GLbitfield buffer_bit,
59 struct intel_renderbuffer *src_irb, mesa_format src_format,
60 struct intel_renderbuffer *dst_irb, mesa_format dst_format,
61 GLfloat srcX0, GLfloat srcY0, GLfloat srcX1, GLfloat srcY1,
62 GLfloat dstX0, GLfloat dstY0, GLfloat dstX1, GLfloat dstY1,
63 GLenum filter, bool mirror_x, bool mirror_y)
64 {
65 /* Find source/dst miptrees */
66 struct intel_mipmap_tree *src_mt = find_miptree(buffer_bit, src_irb);
67 struct intel_mipmap_tree *dst_mt = find_miptree(buffer_bit, dst_irb);
68
69 const bool es3 = _mesa_is_gles3(&brw->ctx);
70 /* Do the blit */
71 brw_blorp_blit_miptrees(brw,
72 src_mt, src_irb->mt_level, src_irb->mt_layer,
73 src_format, blorp_get_texture_swizzle(src_irb),
74 dst_mt, dst_irb->mt_level, dst_irb->mt_layer,
75 dst_format,
76 srcX0, srcY0, srcX1, srcY1,
77 dstX0, dstY0, dstX1, dstY1,
78 filter, mirror_x, mirror_y,
79 es3, es3);
80
81 dst_irb->need_downsample = true;
82 }
83
84 static bool
85 try_blorp_blit(struct brw_context *brw,
86 const struct gl_framebuffer *read_fb,
87 const struct gl_framebuffer *draw_fb,
88 GLfloat srcX0, GLfloat srcY0, GLfloat srcX1, GLfloat srcY1,
89 GLfloat dstX0, GLfloat dstY0, GLfloat dstX1, GLfloat dstY1,
90 GLenum filter, GLbitfield buffer_bit)
91 {
92 struct gl_context *ctx = &brw->ctx;
93
94 /* Sync up the state of window system buffers. We need to do this before
95 * we go looking for the buffers.
96 */
97 intel_prepare_render(brw);
98
99 bool mirror_x, mirror_y;
100 if (brw_meta_mirror_clip_and_scissor(ctx, read_fb, draw_fb,
101 &srcX0, &srcY0, &srcX1, &srcY1,
102 &dstX0, &dstY0, &dstX1, &dstY1,
103 &mirror_x, &mirror_y))
104 return true;
105
106 /* Find buffers */
107 struct intel_renderbuffer *src_irb;
108 struct intel_renderbuffer *dst_irb;
109 struct intel_mipmap_tree *src_mt;
110 struct intel_mipmap_tree *dst_mt;
111 switch (buffer_bit) {
112 case GL_COLOR_BUFFER_BIT:
113 src_irb = intel_renderbuffer(read_fb->_ColorReadBuffer);
114 for (unsigned i = 0; i < draw_fb->_NumColorDrawBuffers; ++i) {
115 dst_irb = intel_renderbuffer(draw_fb->_ColorDrawBuffers[i]);
116 if (dst_irb)
117 do_blorp_blit(brw, buffer_bit,
118 src_irb, src_irb->Base.Base.Format,
119 dst_irb, dst_irb->Base.Base.Format,
120 srcX0, srcY0, srcX1, srcY1,
121 dstX0, dstY0, dstX1, dstY1,
122 filter, mirror_x, mirror_y);
123 }
124 break;
125 case GL_DEPTH_BUFFER_BIT:
126 src_irb =
127 intel_renderbuffer(read_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
128 dst_irb =
129 intel_renderbuffer(draw_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
130 src_mt = find_miptree(buffer_bit, src_irb);
131 dst_mt = find_miptree(buffer_bit, dst_irb);
132
133 /* We can't handle format conversions between Z24 and other formats
134 * since we have to lie about the surface format. See the comments in
135 * brw_blorp_surface_info::set().
136 */
137 if ((src_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT) !=
138 (dst_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT))
139 return false;
140
141 do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
142 dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
143 srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
144 filter, mirror_x, mirror_y);
145 break;
146 case GL_STENCIL_BUFFER_BIT:
147 src_irb =
148 intel_renderbuffer(read_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
149 dst_irb =
150 intel_renderbuffer(draw_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
151 do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
152 dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
153 srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
154 filter, mirror_x, mirror_y);
155 break;
156 default:
157 unreachable("not reached");
158 }
159
160 return true;
161 }
162
163 bool
164 brw_blorp_copytexsubimage(struct brw_context *brw,
165 struct gl_renderbuffer *src_rb,
166 struct gl_texture_image *dst_image,
167 int slice,
168 int srcX0, int srcY0,
169 int dstX0, int dstY0,
170 int width, int height)
171 {
172 struct gl_context *ctx = &brw->ctx;
173 struct intel_renderbuffer *src_irb = intel_renderbuffer(src_rb);
174 struct intel_texture_image *intel_image = intel_texture_image(dst_image);
175
176 /* No pixel transfer operations (zoom, bias, mapping), just a blit */
177 if (brw->ctx._ImageTransferState)
178 return false;
179
180 /* Sync up the state of window system buffers. We need to do this before
181 * we go looking at the src renderbuffer's miptree.
182 */
183 intel_prepare_render(brw);
184
185 struct intel_mipmap_tree *src_mt = src_irb->mt;
186 struct intel_mipmap_tree *dst_mt = intel_image->mt;
187
188 /* There is support for only up to eight samples. */
189 if (src_mt->num_samples > 8 || dst_mt->num_samples > 8)
190 return false;
191
192 /* BLORP is only supported from Gen6 onwards. */
193 if (brw->gen < 6)
194 return false;
195
196 if (_mesa_get_format_base_format(src_rb->Format) !=
197 _mesa_get_format_base_format(dst_image->TexFormat)) {
198 return false;
199 }
200
201 /* We can't handle format conversions between Z24 and other formats since
202 * we have to lie about the surface format. See the comments in
203 * brw_blorp_surface_info::set().
204 */
205 if ((src_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT) !=
206 (dst_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT)) {
207 return false;
208 }
209
210 if (!brw->format_supported_as_render_target[dst_image->TexFormat])
211 return false;
212
213 /* Source clipping shouldn't be necessary, since copytexsubimage (in
214 * src/mesa/main/teximage.c) calls _mesa_clip_copytexsubimage() which
215 * takes care of it.
216 *
217 * Destination clipping shouldn't be necessary since the restrictions on
218 * glCopyTexSubImage prevent the user from specifying a destination rectangle
219 * that falls outside the bounds of the destination texture.
220 * See error_check_subtexture_dimensions().
221 */
222
223 int srcY1 = srcY0 + height;
224 int srcX1 = srcX0 + width;
225 int dstX1 = dstX0 + width;
226 int dstY1 = dstY0 + height;
227
228 /* Account for the fact that in the system framebuffer, the origin is at
229 * the lower left.
230 */
231 bool mirror_y = false;
232 if (_mesa_is_winsys_fbo(ctx->ReadBuffer)) {
233 GLint tmp = src_rb->Height - srcY0;
234 srcY0 = src_rb->Height - srcY1;
235 srcY1 = tmp;
236 mirror_y = true;
237 }
238
239 /* Account for face selection and texture view MinLayer */
240 int dst_slice = slice + dst_image->TexObject->MinLayer + dst_image->Face;
241 int dst_level = dst_image->Level + dst_image->TexObject->MinLevel;
242
243 brw_blorp_blit_miptrees(brw,
244 src_mt, src_irb->mt_level, src_irb->mt_layer,
245 src_rb->Format, blorp_get_texture_swizzle(src_irb),
246 dst_mt, dst_level, dst_slice,
247 dst_image->TexFormat,
248 srcX0, srcY0, srcX1, srcY1,
249 dstX0, dstY0, dstX1, dstY1,
250 GL_NEAREST, false, mirror_y,
251 false, false);
252
253 /* If we're copying to a packed depth stencil texture and the source
254 * framebuffer has separate stencil, we need to also copy the stencil data
255 * over.
256 */
257 src_rb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
258 if (_mesa_get_format_bits(dst_image->TexFormat, GL_STENCIL_BITS) > 0 &&
259 src_rb != NULL) {
260 src_irb = intel_renderbuffer(src_rb);
261 src_mt = src_irb->mt;
262
263 if (src_mt->stencil_mt)
264 src_mt = src_mt->stencil_mt;
265 if (dst_mt->stencil_mt)
266 dst_mt = dst_mt->stencil_mt;
267
268 if (src_mt != dst_mt) {
269 brw_blorp_blit_miptrees(brw,
270 src_mt, src_irb->mt_level, src_irb->mt_layer,
271 src_mt->format,
272 blorp_get_texture_swizzle(src_irb),
273 dst_mt, dst_level, dst_slice,
274 dst_mt->format,
275 srcX0, srcY0, srcX1, srcY1,
276 dstX0, dstY0, dstX1, dstY1,
277 GL_NEAREST, false, mirror_y,
278 false, false);
279 }
280 }
281
282 return true;
283 }
284
285
286 GLbitfield
287 brw_blorp_framebuffer(struct brw_context *brw,
288 struct gl_framebuffer *readFb,
289 struct gl_framebuffer *drawFb,
290 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
291 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
292 GLbitfield mask, GLenum filter)
293 {
294 /* BLORP is not supported before Gen6. */
295 if (brw->gen < 6)
296 return mask;
297
298 /* There is support for only up to eight samples. */
299 if (readFb->Visual.samples > 8 || drawFb->Visual.samples > 8)
300 return mask;
301
302 static GLbitfield buffer_bits[] = {
303 GL_COLOR_BUFFER_BIT,
304 GL_DEPTH_BUFFER_BIT,
305 GL_STENCIL_BUFFER_BIT,
306 };
307
308 for (unsigned int i = 0; i < ARRAY_SIZE(buffer_bits); ++i) {
309 if ((mask & buffer_bits[i]) &&
310 try_blorp_blit(brw, readFb, drawFb,
311 srcX0, srcY0, srcX1, srcY1,
312 dstX0, dstY0, dstX1, dstY1,
313 filter, buffer_bits[i])) {
314 mask &= ~buffer_bits[i];
315 }
316 }
317
318 return mask;
319 }
320
321
322 /**
323 * Enum to specify the order of arguments in a sampler message
324 */
325 enum sampler_message_arg
326 {
327 SAMPLER_MESSAGE_ARG_U_FLOAT,
328 SAMPLER_MESSAGE_ARG_V_FLOAT,
329 SAMPLER_MESSAGE_ARG_U_INT,
330 SAMPLER_MESSAGE_ARG_V_INT,
331 SAMPLER_MESSAGE_ARG_R_INT,
332 SAMPLER_MESSAGE_ARG_SI_INT,
333 SAMPLER_MESSAGE_ARG_MCS_INT,
334 SAMPLER_MESSAGE_ARG_ZERO_INT,
335 };
336
337 struct brw_blorp_blit_vars {
338 /* Uniforms values from brw_blorp_wm_push_constants */
339 nir_variable *u_dst_x0;
340 nir_variable *u_dst_x1;
341 nir_variable *u_dst_y0;
342 nir_variable *u_dst_y1;
343 nir_variable *u_rect_grid_x1;
344 nir_variable *u_rect_grid_y1;
345 struct {
346 nir_variable *multiplier;
347 nir_variable *offset;
348 } u_x_transform, u_y_transform;
349 nir_variable *u_src_z;
350
351 /* gl_FragCoord */
352 nir_variable *frag_coord;
353
354 /* gl_FragColor */
355 nir_variable *color_out;
356 };
357
358 static void
359 brw_blorp_blit_vars_init(nir_builder *b, struct brw_blorp_blit_vars *v,
360 const struct brw_blorp_blit_prog_key *key)
361 {
362 #define LOAD_UNIFORM(name, type)\
363 v->u_##name = nir_variable_create(b->shader, nir_var_uniform, type, #name); \
364 v->u_##name->data.location = \
365 offsetof(struct brw_blorp_wm_push_constants, name);
366
367 LOAD_UNIFORM(dst_x0, glsl_uint_type())
368 LOAD_UNIFORM(dst_x1, glsl_uint_type())
369 LOAD_UNIFORM(dst_y0, glsl_uint_type())
370 LOAD_UNIFORM(dst_y1, glsl_uint_type())
371 LOAD_UNIFORM(rect_grid_x1, glsl_float_type())
372 LOAD_UNIFORM(rect_grid_y1, glsl_float_type())
373 LOAD_UNIFORM(x_transform.multiplier, glsl_float_type())
374 LOAD_UNIFORM(x_transform.offset, glsl_float_type())
375 LOAD_UNIFORM(y_transform.multiplier, glsl_float_type())
376 LOAD_UNIFORM(y_transform.offset, glsl_float_type())
377 LOAD_UNIFORM(src_z, glsl_uint_type())
378
379 #undef DECL_UNIFORM
380
381 v->frag_coord = nir_variable_create(b->shader, nir_var_shader_in,
382 glsl_vec4_type(), "gl_FragCoord");
383 v->frag_coord->data.location = VARYING_SLOT_POS;
384 v->frag_coord->data.origin_upper_left = true;
385
386 v->color_out = nir_variable_create(b->shader, nir_var_shader_out,
387 glsl_vec4_type(), "gl_FragColor");
388 v->color_out->data.location = FRAG_RESULT_COLOR;
389 }
390
391 nir_ssa_def *
392 blorp_blit_get_frag_coords(nir_builder *b,
393 const struct brw_blorp_blit_prog_key *key,
394 struct brw_blorp_blit_vars *v)
395 {
396 nir_ssa_def *coord = nir_f2i(b, nir_load_var(b, v->frag_coord));
397
398 if (key->persample_msaa_dispatch) {
399 return nir_vec3(b, nir_channel(b, coord, 0), nir_channel(b, coord, 1),
400 nir_load_system_value(b, nir_intrinsic_load_sample_id, 0));
401 } else {
402 return nir_vec2(b, nir_channel(b, coord, 0), nir_channel(b, coord, 1));
403 }
404 }
405
406 /**
407 * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
408 * coordinates.
409 */
410 nir_ssa_def *
411 blorp_blit_apply_transform(nir_builder *b, nir_ssa_def *src_pos,
412 struct brw_blorp_blit_vars *v)
413 {
414 nir_ssa_def *offset = nir_vec2(b, nir_load_var(b, v->u_x_transform.offset),
415 nir_load_var(b, v->u_y_transform.offset));
416 nir_ssa_def *mul = nir_vec2(b, nir_load_var(b, v->u_x_transform.multiplier),
417 nir_load_var(b, v->u_y_transform.multiplier));
418
419 nir_ssa_def *pos = nir_ffma(b, src_pos, mul, offset);
420
421 if (src_pos->num_components == 3) {
422 /* Leave the sample id alone */
423 pos = nir_vec3(b, nir_channel(b, pos, 0), nir_channel(b, pos, 1),
424 nir_channel(b, src_pos, 2));
425 }
426
427 return pos;
428 }
429
430 static inline void
431 blorp_nir_discard_if_outside_rect(nir_builder *b, nir_ssa_def *pos,
432 struct brw_blorp_blit_vars *v)
433 {
434 nir_ssa_def *c0, *c1, *c2, *c3;
435 c0 = nir_ult(b, nir_channel(b, pos, 0), nir_load_var(b, v->u_dst_x0));
436 c1 = nir_uge(b, nir_channel(b, pos, 0), nir_load_var(b, v->u_dst_x1));
437 c2 = nir_ult(b, nir_channel(b, pos, 1), nir_load_var(b, v->u_dst_y0));
438 c3 = nir_uge(b, nir_channel(b, pos, 1), nir_load_var(b, v->u_dst_y1));
439 nir_ssa_def *oob = nir_ior(b, nir_ior(b, c0, c1), nir_ior(b, c2, c3));
440
441 nir_intrinsic_instr *discard =
442 nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard_if);
443 discard->src[0] = nir_src_for_ssa(oob);
444 nir_builder_instr_insert(b, &discard->instr);
445 }
446
447 static nir_tex_instr *
448 blorp_create_nir_tex_instr(nir_shader *shader, nir_texop op,
449 nir_ssa_def *pos, unsigned num_srcs,
450 enum brw_reg_type dst_type)
451 {
452 nir_tex_instr *tex = nir_tex_instr_create(shader, num_srcs);
453
454 tex->op = op;
455
456 switch (dst_type) {
457 case BRW_REGISTER_TYPE_F:
458 tex->dest_type = nir_type_float;
459 break;
460 case BRW_REGISTER_TYPE_D:
461 tex->dest_type = nir_type_int;
462 break;
463 case BRW_REGISTER_TYPE_UD:
464 tex->dest_type = nir_type_uint;
465 break;
466 default:
467 unreachable("Invalid texture return type");
468 }
469
470 tex->is_array = false;
471 tex->is_shadow = false;
472
473 /* Blorp only has one texture and it's bound at unit 0 */
474 tex->texture = NULL;
475 tex->sampler = NULL;
476 tex->texture_index = 0;
477 tex->sampler_index = 0;
478
479 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
480
481 return tex;
482 }
483
484 static nir_ssa_def *
485 blorp_nir_tex(nir_builder *b, nir_ssa_def *pos, enum brw_reg_type dst_type)
486 {
487 nir_tex_instr *tex =
488 blorp_create_nir_tex_instr(b->shader, nir_texop_tex, pos, 2, dst_type);
489
490 assert(pos->num_components == 2);
491 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
492 tex->coord_components = 2;
493 tex->src[0].src_type = nir_tex_src_coord;
494 tex->src[0].src = nir_src_for_ssa(pos);
495 tex->src[1].src_type = nir_tex_src_lod;
496 tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
497
498 nir_builder_instr_insert(b, &tex->instr);
499
500 return &tex->dest.ssa;
501 }
502
503 static nir_ssa_def *
504 blorp_nir_txf(nir_builder *b, struct brw_blorp_blit_vars *v,
505 nir_ssa_def *pos, enum brw_reg_type dst_type)
506 {
507 nir_tex_instr *tex =
508 blorp_create_nir_tex_instr(b->shader, nir_texop_txf, pos, 2, dst_type);
509
510 /* In order to properly handle 3-D textures, we pull the Z component from
511 * a uniform. TODO: This is a bit magic; we should probably make this
512 * more explicit in the future.
513 */
514 assert(pos->num_components == 2);
515 pos = nir_vec3(b, nir_channel(b, pos, 0), nir_channel(b, pos, 1),
516 nir_load_var(b, v->u_src_z));
517
518 tex->sampler_dim = GLSL_SAMPLER_DIM_3D;
519 tex->coord_components = 3;
520 tex->src[0].src_type = nir_tex_src_coord;
521 tex->src[0].src = nir_src_for_ssa(pos);
522 tex->src[1].src_type = nir_tex_src_lod;
523 tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
524
525 nir_builder_instr_insert(b, &tex->instr);
526
527 return &tex->dest.ssa;
528 }
529
530 static nir_ssa_def *
531 blorp_nir_txf_ms(nir_builder *b, nir_ssa_def *pos, nir_ssa_def *mcs,
532 enum brw_reg_type dst_type)
533 {
534 nir_tex_instr *tex =
535 blorp_create_nir_tex_instr(b->shader, nir_texop_txf_ms, pos,
536 mcs != NULL ? 3 : 2, dst_type);
537
538 tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
539 tex->coord_components = 2;
540 tex->src[0].src_type = nir_tex_src_coord;
541 tex->src[0].src = nir_src_for_ssa(pos);
542
543 tex->src[1].src_type = nir_tex_src_ms_index;
544 if (pos->num_components == 2) {
545 tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
546 } else {
547 assert(pos->num_components == 3);
548 tex->src[1].src = nir_src_for_ssa(nir_channel(b, pos, 2));
549 }
550
551 if (mcs) {
552 tex->src[2].src_type = nir_tex_src_ms_mcs;
553 tex->src[2].src = nir_src_for_ssa(mcs);
554 }
555
556 nir_builder_instr_insert(b, &tex->instr);
557
558 return &tex->dest.ssa;
559 }
560
561 static nir_ssa_def *
562 blorp_nir_txf_ms_mcs(nir_builder *b, nir_ssa_def *pos)
563 {
564 nir_tex_instr *tex =
565 blorp_create_nir_tex_instr(b->shader, nir_texop_txf_ms_mcs,
566 pos, 1, BRW_REGISTER_TYPE_D);
567
568 tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
569 tex->coord_components = 2;
570 tex->src[0].src_type = nir_tex_src_coord;
571 tex->src[0].src = nir_src_for_ssa(pos);
572
573 nir_builder_instr_insert(b, &tex->instr);
574
575 return &tex->dest.ssa;
576 }
577
578 static nir_ssa_def *
579 nir_mask_shift_or(struct nir_builder *b, nir_ssa_def *dst, nir_ssa_def *src,
580 uint32_t src_mask, int src_left_shift)
581 {
582 nir_ssa_def *masked = nir_iand(b, src, nir_imm_int(b, src_mask));
583
584 nir_ssa_def *shifted;
585 if (src_left_shift > 0) {
586 shifted = nir_ishl(b, masked, nir_imm_int(b, src_left_shift));
587 } else if (src_left_shift < 0) {
588 shifted = nir_ushr(b, masked, nir_imm_int(b, -src_left_shift));
589 } else {
590 assert(src_left_shift == 0);
591 shifted = masked;
592 }
593
594 return nir_ior(b, dst, shifted);
595 }
596
597 /**
598 * Emit code to compensate for the difference between Y and W tiling.
599 *
600 * This code modifies the X and Y coordinates according to the formula:
601 *
602 * (X', Y', S') = detile(W-MAJOR, tile(Y-MAJOR, X, Y, S))
603 *
604 * (See brw_blorp_build_nir_shader).
605 */
606 static inline nir_ssa_def *
607 blorp_nir_retile_y_to_w(nir_builder *b, nir_ssa_def *pos)
608 {
609 assert(pos->num_components == 2);
610 nir_ssa_def *x_Y = nir_channel(b, pos, 0);
611 nir_ssa_def *y_Y = nir_channel(b, pos, 1);
612
613 /* Given X and Y coordinates that describe an address using Y tiling,
614 * translate to the X and Y coordinates that describe the same address
615 * using W tiling.
616 *
617 * If we break down the low order bits of X and Y, using a
618 * single letter to represent each low-order bit:
619 *
620 * X = A << 7 | 0bBCDEFGH
621 * Y = J << 5 | 0bKLMNP (1)
622 *
623 * Then we can apply the Y tiling formula to see the memory offset being
624 * addressed:
625 *
626 * offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
627 *
628 * If we apply the W detiling formula to this memory location, that the
629 * corresponding X' and Y' coordinates are:
630 *
631 * X' = A << 6 | 0bBCDPFH (3)
632 * Y' = J << 6 | 0bKLMNEG
633 *
634 * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
635 * we need to make the following computation:
636 *
637 * X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
638 * Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
639 */
640 nir_ssa_def *x_W = nir_imm_int(b, 0);
641 x_W = nir_mask_shift_or(b, x_W, x_Y, 0xfffffff4, -1);
642 x_W = nir_mask_shift_or(b, x_W, y_Y, 0x1, 2);
643 x_W = nir_mask_shift_or(b, x_W, x_Y, 0x1, 0);
644
645 nir_ssa_def *y_W = nir_imm_int(b, 0);
646 y_W = nir_mask_shift_or(b, y_W, y_Y, 0xfffffffe, 1);
647 y_W = nir_mask_shift_or(b, y_W, x_Y, 0x8, -2);
648 y_W = nir_mask_shift_or(b, y_W, x_Y, 0x2, -1);
649
650 return nir_vec2(b, x_W, y_W);
651 }
652
653 /**
654 * Emit code to compensate for the difference between Y and W tiling.
655 *
656 * This code modifies the X and Y coordinates according to the formula:
657 *
658 * (X', Y', S') = detile(Y-MAJOR, tile(W-MAJOR, X, Y, S))
659 *
660 * (See brw_blorp_build_nir_shader).
661 */
662 static inline nir_ssa_def *
663 blorp_nir_retile_w_to_y(nir_builder *b, nir_ssa_def *pos)
664 {
665 assert(pos->num_components == 2);
666 nir_ssa_def *x_W = nir_channel(b, pos, 0);
667 nir_ssa_def *y_W = nir_channel(b, pos, 1);
668
669 /* Applying the same logic as above, but in reverse, we obtain the
670 * formulas:
671 *
672 * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
673 * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
674 */
675 nir_ssa_def *x_Y = nir_imm_int(b, 0);
676 x_Y = nir_mask_shift_or(b, x_Y, x_W, 0xfffffffa, 1);
677 x_Y = nir_mask_shift_or(b, x_Y, y_W, 0x2, 2);
678 x_Y = nir_mask_shift_or(b, x_Y, y_W, 0x1, 1);
679 x_Y = nir_mask_shift_or(b, x_Y, x_W, 0x1, 0);
680
681 nir_ssa_def *y_Y = nir_imm_int(b, 0);
682 y_Y = nir_mask_shift_or(b, y_Y, y_W, 0xfffffffc, -1);
683 y_Y = nir_mask_shift_or(b, y_Y, x_W, 0x4, -2);
684
685 return nir_vec2(b, x_Y, y_Y);
686 }
687
688 /**
689 * Emit code to compensate for the difference between MSAA and non-MSAA
690 * surfaces.
691 *
692 * This code modifies the X and Y coordinates according to the formula:
693 *
694 * (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
695 *
696 * (See brw_blorp_blit_program).
697 */
698 static inline nir_ssa_def *
699 blorp_nir_encode_msaa(nir_builder *b, nir_ssa_def *pos,
700 unsigned num_samples, enum intel_msaa_layout layout)
701 {
702 assert(pos->num_components == 2 || pos->num_components == 3);
703
704 switch (layout) {
705 case INTEL_MSAA_LAYOUT_NONE:
706 assert(pos->num_components == 2);
707 return pos;
708 case INTEL_MSAA_LAYOUT_CMS:
709 /* We can't compensate for compressed layout since at this point in the
710 * program we haven't read from the MCS buffer.
711 */
712 unreachable("Bad layout in encode_msaa");
713 case INTEL_MSAA_LAYOUT_UMS:
714 /* No translation needed */
715 return pos;
716 case INTEL_MSAA_LAYOUT_IMS: {
717 nir_ssa_def *x_in = nir_channel(b, pos, 0);
718 nir_ssa_def *y_in = nir_channel(b, pos, 1);
719 nir_ssa_def *s_in = pos->num_components == 2 ? nir_imm_int(b, 0) :
720 nir_channel(b, pos, 2);
721
722 nir_ssa_def *x_out = nir_imm_int(b, 0);
723 nir_ssa_def *y_out = nir_imm_int(b, 0);
724 switch (num_samples) {
725 case 2:
726 case 4:
727 /* encode_msaa(2, IMS, X, Y, S) = (X', Y', 0)
728 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
729 * Y' = Y
730 *
731 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
732 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
733 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
734 */
735 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 1);
736 x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
737 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
738 if (num_samples == 2) {
739 y_out = y_in;
740 } else {
741 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 1);
742 y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
743 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
744 }
745 break;
746
747 case 8:
748 /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
749 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
750 * | (X & 0b1)
751 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
752 */
753 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffe, 2);
754 x_out = nir_mask_shift_or(b, x_out, s_in, 0x4, 0);
755 x_out = nir_mask_shift_or(b, x_out, s_in, 0x1, 1);
756 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
757 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffe, 1);
758 y_out = nir_mask_shift_or(b, y_out, s_in, 0x2, 0);
759 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
760 break;
761
762 default:
763 unreachable("Invalid number of samples for IMS layout");
764 }
765
766 return nir_vec2(b, x_out, y_out);
767 }
768
769 default:
770 unreachable("Invalid MSAA layout");
771 }
772 }
773
774 /**
775 * Emit code to compensate for the difference between MSAA and non-MSAA
776 * surfaces.
777 *
778 * This code modifies the X and Y coordinates according to the formula:
779 *
780 * (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
781 *
782 * (See brw_blorp_blit_program).
783 */
784 static inline nir_ssa_def *
785 blorp_nir_decode_msaa(nir_builder *b, nir_ssa_def *pos,
786 unsigned num_samples, enum intel_msaa_layout layout)
787 {
788 assert(pos->num_components == 2 || pos->num_components == 3);
789
790 switch (layout) {
791 case INTEL_MSAA_LAYOUT_NONE:
792 /* No translation necessary, and S should already be zero. */
793 assert(pos->num_components == 2);
794 return pos;
795 case INTEL_MSAA_LAYOUT_CMS:
796 /* We can't compensate for compressed layout since at this point in the
797 * program we don't have access to the MCS buffer.
798 */
799 unreachable("Bad layout in encode_msaa");
800 case INTEL_MSAA_LAYOUT_UMS:
801 /* No translation necessary. */
802 return pos;
803 case INTEL_MSAA_LAYOUT_IMS: {
804 assert(pos->num_components == 2);
805
806 nir_ssa_def *x_in = nir_channel(b, pos, 0);
807 nir_ssa_def *y_in = nir_channel(b, pos, 1);
808
809 nir_ssa_def *x_out = nir_imm_int(b, 0);
810 nir_ssa_def *y_out = nir_imm_int(b, 0);
811 nir_ssa_def *s_out = nir_imm_int(b, 0);
812 switch (num_samples) {
813 case 2:
814 case 4:
815 /* decode_msaa(2, IMS, X, Y, 0) = (X', Y', S)
816 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
817 * S = (X & 0b10) >> 1
818 *
819 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
820 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
821 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
822 * S = (Y & 0b10) | (X & 0b10) >> 1
823 */
824 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffffc, -1);
825 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
826 if (num_samples == 2) {
827 y_out = y_in;
828 s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
829 } else {
830 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffc, -1);
831 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
832 s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
833 s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
834 }
835 break;
836
837 case 8:
838 /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
839 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
840 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
841 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
842 */
843 x_out = nir_mask_shift_or(b, x_out, x_in, 0xfffffff8, -2);
844 x_out = nir_mask_shift_or(b, x_out, x_in, 0x1, 0);
845 y_out = nir_mask_shift_or(b, y_out, y_in, 0xfffffffc, -1);
846 y_out = nir_mask_shift_or(b, y_out, y_in, 0x1, 0);
847 s_out = nir_mask_shift_or(b, s_out, x_in, 0x4, 0);
848 s_out = nir_mask_shift_or(b, s_out, y_in, 0x2, 0);
849 s_out = nir_mask_shift_or(b, s_out, x_in, 0x2, -1);
850 break;
851
852 default:
853 unreachable("Invalid number of samples for IMS layout");
854 }
855
856 return nir_vec3(b, x_out, y_out, s_out);
857 }
858
859 default:
860 unreachable("Invalid MSAA layout");
861 }
862 }
863
864 /**
865 * Count the number of trailing 1 bits in the given value. For example:
866 *
867 * count_trailing_one_bits(0) == 0
868 * count_trailing_one_bits(7) == 3
869 * count_trailing_one_bits(11) == 2
870 */
871 static inline int count_trailing_one_bits(unsigned value)
872 {
873 #ifdef HAVE___BUILTIN_CTZ
874 return __builtin_ctz(~value);
875 #else
876 return _mesa_bitcount(value & ~(value + 1));
877 #endif
878 }
879
880 static nir_ssa_def *
881 blorp_nir_manual_blend_average(nir_builder *b, nir_ssa_def *pos,
882 unsigned tex_samples,
883 enum intel_msaa_layout tex_layout,
884 enum brw_reg_type dst_type)
885 {
886 /* If non-null, this is the outer-most if statement */
887 nir_if *outer_if = NULL;
888
889 nir_variable *color =
890 nir_local_variable_create(b->impl, glsl_vec4_type(), "color");
891
892 nir_ssa_def *mcs = NULL;
893 if (tex_layout == INTEL_MSAA_LAYOUT_CMS)
894 mcs = blorp_nir_txf_ms_mcs(b, pos);
895
896 /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
897 *
898 * result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
899 *
900 * This ensures that when all samples have the same value, no numerical
901 * precision is lost, since each addition operation always adds two equal
902 * values, and summing two equal floating point values does not lose
903 * precision.
904 *
905 * We perform this computation by treating the texture_data array as a
906 * stack and performing the following operations:
907 *
908 * - push sample 0 onto stack
909 * - push sample 1 onto stack
910 * - add top two stack entries
911 * - push sample 2 onto stack
912 * - push sample 3 onto stack
913 * - add top two stack entries
914 * - add top two stack entries
915 * - divide top stack entry by 4
916 *
917 * Note that after pushing sample i onto the stack, the number of add
918 * operations we do is equal to the number of trailing 1 bits in i. This
919 * works provided the total number of samples is a power of two, which it
920 * always is for i965.
921 *
922 * For integer formats, we replace the add operations with average
923 * operations and skip the final division.
924 */
925 nir_ssa_def *texture_data[4];
926 unsigned stack_depth = 0;
927 for (unsigned i = 0; i < tex_samples; ++i) {
928 assert(stack_depth == _mesa_bitcount(i)); /* Loop invariant */
929
930 /* Push sample i onto the stack */
931 assert(stack_depth < ARRAY_SIZE(texture_data));
932
933 nir_ssa_def *ms_pos = nir_vec3(b, nir_channel(b, pos, 0),
934 nir_channel(b, pos, 1),
935 nir_imm_int(b, i));
936 texture_data[stack_depth++] = blorp_nir_txf_ms(b, ms_pos, mcs, dst_type);
937
938 if (i == 0 && tex_layout == INTEL_MSAA_LAYOUT_CMS) {
939 /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
940 * suggests an optimization:
941 *
942 * "A simple optimization with probable large return in
943 * performance is to compare the MCS value to zero (indicating
944 * all samples are on sample slice 0), and sample only from
945 * sample slice 0 using ld2dss if MCS is zero."
946 *
947 * Note that in the case where the MCS value is zero, sampling from
948 * sample slice 0 using ld2dss and sampling from sample 0 using
949 * ld2dms are equivalent (since all samples are on sample slice 0).
950 * Since we have already sampled from sample 0, all we need to do is
951 * skip the remaining fetches and averaging if MCS is zero.
952 */
953 nir_ssa_def *mcs_zero =
954 nir_ieq(b, nir_channel(b, mcs, 0), nir_imm_int(b, 0));
955 nir_if *if_stmt = nir_if_create(b->shader);
956 if_stmt->condition = nir_src_for_ssa(mcs_zero);
957 nir_cf_node_insert(b->cursor, &if_stmt->cf_node);
958
959 b->cursor = nir_after_cf_list(&if_stmt->then_list);
960 nir_store_var(b, color, texture_data[0], 0xf);
961
962 b->cursor = nir_after_cf_list(&if_stmt->else_list);
963 outer_if = if_stmt;
964 }
965
966 for (int j = 0; j < count_trailing_one_bits(i); j++) {
967 assert(stack_depth >= 2);
968 --stack_depth;
969
970 assert(dst_type == BRW_REGISTER_TYPE_F);
971 texture_data[stack_depth - 1] =
972 nir_fadd(b, texture_data[stack_depth - 1],
973 texture_data[stack_depth]);
974 }
975 }
976
977 /* We should have just 1 sample on the stack now. */
978 assert(stack_depth == 1);
979
980 texture_data[0] = nir_fmul(b, texture_data[0],
981 nir_imm_float(b, 1.0 / tex_samples));
982
983 nir_store_var(b, color, texture_data[0], 0xf);
984
985 if (outer_if)
986 b->cursor = nir_after_cf_node(&outer_if->cf_node);
987
988 return nir_load_var(b, color);
989 }
990
991 /**
992 * Generator for WM programs used in BLORP blits.
993 *
994 * The bulk of the work done by the WM program is to wrap and unwrap the
995 * coordinate transformations used by the hardware to store surfaces in
996 * memory. The hardware transforms a pixel location (X, Y, S) (where S is the
997 * sample index for a multisampled surface) to a memory offset by the
998 * following formulas:
999 *
1000 * offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
1001 * (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
1002 *
1003 * For a single-sampled surface, or for a multisampled surface using
1004 * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
1005 * function:
1006 *
1007 * encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
1008 * decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
1009 * encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
1010 * decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
1011 *
1012 * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
1013 * embeds the sample number into bit 1 of the X and Y coordinates:
1014 *
1015 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
1016 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
1017 * Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
1018 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
1019 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
1020 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1021 * S = (Y & 0b10) | (X & 0b10) >> 1
1022 *
1023 * For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
1024 * embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
1025 * the Y coordinate:
1026 *
1027 * encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
1028 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
1029 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1030 * decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
1031 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
1032 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1033 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
1034 *
1035 * For X tiling, tile() combines together the low-order bits of the X and Y
1036 * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
1037 * bytes wide and 8 rows high:
1038 *
1039 * tile(x_tiled, X, Y, S) = A
1040 * where A = tile_num << 12 | offset
1041 * tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
1042 * offset = (Y' & 0b111) << 9
1043 * | (X & 0b111111111)
1044 * X' = X * cpp
1045 * Y' = Y + S * qpitch
1046 * detile(x_tiled, A) = (X, Y, S)
1047 * where X = X' / cpp
1048 * Y = Y' % qpitch
1049 * S = Y' / qpitch
1050 * Y' = (tile_num / tile_pitch) << 3
1051 * | (A & 0b111000000000) >> 9
1052 * X' = (tile_num % tile_pitch) << 9
1053 * | (A & 0b111111111)
1054 *
1055 * (In all tiling formulas, cpp is the number of bytes occupied by a single
1056 * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
1057 * to fill the width of the surface, and qpitch is the spacing (in rows)
1058 * between array slices).
1059 *
1060 * For Y tiling, tile() combines together the low-order bits of the X and Y
1061 * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
1062 * bytes wide and 32 rows high:
1063 *
1064 * tile(y_tiled, X, Y, S) = A
1065 * where A = tile_num << 12 | offset
1066 * tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
1067 * offset = (X' & 0b1110000) << 5
1068 * | (Y' & 0b11111) << 4
1069 * | (X' & 0b1111)
1070 * X' = X * cpp
1071 * Y' = Y + S * qpitch
1072 * detile(y_tiled, A) = (X, Y, S)
1073 * where X = X' / cpp
1074 * Y = Y' % qpitch
1075 * S = Y' / qpitch
1076 * Y' = (tile_num / tile_pitch) << 5
1077 * | (A & 0b111110000) >> 4
1078 * X' = (tile_num % tile_pitch) << 7
1079 * | (A & 0b111000000000) >> 5
1080 * | (A & 0b1111)
1081 *
1082 * For W tiling, tile() combines together the low-order bits of the X and Y
1083 * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
1084 * bytes wide and 64 rows high (note that W tiling is only used for stencil
1085 * buffers, which always have cpp = 1 and S=0):
1086 *
1087 * tile(w_tiled, X, Y, S) = A
1088 * where A = tile_num << 12 | offset
1089 * tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
1090 * offset = (X' & 0b111000) << 6
1091 * | (Y' & 0b111100) << 3
1092 * | (X' & 0b100) << 2
1093 * | (Y' & 0b10) << 2
1094 * | (X' & 0b10) << 1
1095 * | (Y' & 0b1) << 1
1096 * | (X' & 0b1)
1097 * X' = X * cpp = X
1098 * Y' = Y + S * qpitch
1099 * detile(w_tiled, A) = (X, Y, S)
1100 * where X = X' / cpp = X'
1101 * Y = Y' % qpitch = Y'
1102 * S = Y / qpitch = 0
1103 * Y' = (tile_num / tile_pitch) << 6
1104 * | (A & 0b111100000) >> 3
1105 * | (A & 0b1000) >> 2
1106 * | (A & 0b10) >> 1
1107 * X' = (tile_num % tile_pitch) << 6
1108 * | (A & 0b111000000000) >> 6
1109 * | (A & 0b10000) >> 2
1110 * | (A & 0b100) >> 1
1111 * | (A & 0b1)
1112 *
1113 * Finally, for a non-tiled surface, tile() simply combines together the X and
1114 * Y coordinates in the natural way:
1115 *
1116 * tile(untiled, X, Y, S) = A
1117 * where A = Y * pitch + X'
1118 * X' = X * cpp
1119 * Y' = Y + S * qpitch
1120 * detile(untiled, A) = (X, Y, S)
1121 * where X = X' / cpp
1122 * Y = Y' % qpitch
1123 * S = Y' / qpitch
1124 * X' = A % pitch
1125 * Y' = A / pitch
1126 *
1127 * (In these formulas, pitch is the number of bytes occupied by a single row
1128 * of samples).
1129 */
1130 static nir_shader *
1131 brw_blorp_build_nir_shader(struct brw_context *brw,
1132 const brw_blorp_blit_prog_key *key,
1133 struct brw_blorp_prog_data *prog_data)
1134 {
1135 nir_ssa_def *src_pos, *dst_pos, *color;
1136
1137 /* Sanity checks */
1138 if (key->dst_tiled_w && key->rt_samples > 0) {
1139 /* If the destination image is W tiled and multisampled, then the thread
1140 * must be dispatched once per sample, not once per pixel. This is
1141 * necessary because after conversion between W and Y tiling, there's no
1142 * guarantee that all samples corresponding to a single pixel will still
1143 * be together.
1144 */
1145 assert(key->persample_msaa_dispatch);
1146 }
1147
1148 if (key->blend) {
1149 /* We are blending, which means we won't have an opportunity to
1150 * translate the tiling and sample count for the texture surface. So
1151 * the surface state for the texture must be configured with the correct
1152 * tiling and sample count.
1153 */
1154 assert(!key->src_tiled_w);
1155 assert(key->tex_samples == key->src_samples);
1156 assert(key->tex_layout == key->src_layout);
1157 assert(key->tex_samples > 0);
1158 }
1159
1160 if (key->persample_msaa_dispatch) {
1161 /* It only makes sense to do persample dispatch if the render target is
1162 * configured as multisampled.
1163 */
1164 assert(key->rt_samples > 0);
1165 }
1166
1167 /* Make sure layout is consistent with sample count */
1168 assert((key->tex_layout == INTEL_MSAA_LAYOUT_NONE) ==
1169 (key->tex_samples == 0));
1170 assert((key->rt_layout == INTEL_MSAA_LAYOUT_NONE) ==
1171 (key->rt_samples == 0));
1172 assert((key->src_layout == INTEL_MSAA_LAYOUT_NONE) ==
1173 (key->src_samples == 0));
1174 assert((key->dst_layout == INTEL_MSAA_LAYOUT_NONE) ==
1175 (key->dst_samples == 0));
1176
1177 /* Set up prog_data */
1178 brw_blorp_prog_data_init(prog_data);
1179
1180 nir_builder b;
1181 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_FRAGMENT, NULL);
1182
1183 struct brw_blorp_blit_vars v;
1184 brw_blorp_blit_vars_init(&b, &v, key);
1185
1186 dst_pos = blorp_blit_get_frag_coords(&b, key, &v);
1187
1188 /* Render target and texture hardware don't support W tiling until Gen8. */
1189 const bool rt_tiled_w = false;
1190 const bool tex_tiled_w = brw->gen >= 8 && key->src_tiled_w;
1191
1192 /* The address that data will be written to is determined by the
1193 * coordinates supplied to the WM thread and the tiling and sample count of
1194 * the render target, according to the formula:
1195 *
1196 * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
1197 *
1198 * If the actual tiling and sample count of the destination surface are not
1199 * the same as the configuration of the render target, then these
1200 * coordinates are wrong and we have to adjust them to compensate for the
1201 * difference.
1202 */
1203 if (rt_tiled_w != key->dst_tiled_w ||
1204 key->rt_samples != key->dst_samples ||
1205 key->rt_layout != key->dst_layout) {
1206 dst_pos = blorp_nir_encode_msaa(&b, dst_pos, key->rt_samples,
1207 key->rt_layout);
1208 /* Now (X, Y, S) = detile(rt_tiling, offset) */
1209 if (rt_tiled_w != key->dst_tiled_w)
1210 dst_pos = blorp_nir_retile_y_to_w(&b, dst_pos);
1211 /* Now (X, Y, S) = detile(rt_tiling, offset) */
1212 dst_pos = blorp_nir_decode_msaa(&b, dst_pos, key->dst_samples,
1213 key->dst_layout);
1214 }
1215
1216 /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
1217 *
1218 * That is: X, Y and S now contain the true coordinates and sample index of
1219 * the data that the WM thread should output.
1220 *
1221 * If we need to kill pixels that are outside the destination rectangle,
1222 * now is the time to do it.
1223 */
1224 if (key->use_kill)
1225 blorp_nir_discard_if_outside_rect(&b, dst_pos, &v);
1226
1227 src_pos = blorp_blit_apply_transform(&b, nir_i2f(&b, dst_pos), &v);
1228
1229 if (key->blit_scaled && key->blend) {
1230 goto fail;
1231 } else if (!key->bilinear_filter) {
1232 /* We're going to use a texelFetch, so we need integers */
1233 src_pos = nir_f2i(&b, src_pos);
1234 }
1235
1236 /* If the source image is not multisampled, then we want to fetch sample
1237 * number 0, because that's the only sample there is.
1238 */
1239 if (key->src_samples == 0)
1240 src_pos = nir_channels(&b, src_pos, 0x3);
1241
1242 /* X, Y, and S are now the coordinates of the pixel in the source image
1243 * that we want to texture from. Exception: if we are blending, then S is
1244 * irrelevant, because we are going to fetch all samples.
1245 */
1246 if (key->blend && !key->blit_scaled) {
1247 if (brw->gen == 6) {
1248 /* Because gen6 only supports 4x interleved MSAA, we can do all the
1249 * blending we need with a single linear-interpolated texture lookup
1250 * at the center of the sample. The texture coordinates to be odd
1251 * integers so that they correspond to the center of a 2x2 block
1252 * representing the four samples that maxe up a pixel. So we need
1253 * to multiply our X and Y coordinates each by 2 and then add 1.
1254 */
1255 src_pos = nir_ishl(&b, src_pos, nir_imm_int(&b, 1));
1256 src_pos = nir_iadd(&b, src_pos, nir_imm_int(&b, 1));
1257 src_pos = nir_i2f(&b, nir_channels(&b, src_pos, 0x3));
1258 color = blorp_nir_tex(&b, src_pos, key->texture_data_type);
1259 } else {
1260 /* Gen7+ hardware doesn't automaticaly blend. */
1261 color = blorp_nir_manual_blend_average(&b, src_pos, key->src_samples,
1262 key->src_layout,
1263 key->texture_data_type);
1264 }
1265 } else if (key->blend && key->blit_scaled) {
1266 goto fail;
1267 } else {
1268 /* We aren't blending, which means we just want to fetch a single sample
1269 * from the source surface. The address that we want to fetch from is
1270 * related to the X, Y and S values according to the formula:
1271 *
1272 * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
1273 *
1274 * If the actual tiling and sample count of the source surface are not
1275 * the same as the configuration of the texture, then we need to adjust
1276 * the coordinates to compensate for the difference.
1277 */
1278 if ((tex_tiled_w != key->src_tiled_w ||
1279 key->tex_samples != key->src_samples ||
1280 key->tex_layout != key->src_layout) &&
1281 !key->bilinear_filter) {
1282 src_pos = blorp_nir_encode_msaa(&b, src_pos, key->src_samples,
1283 key->src_layout);
1284 /* Now (X, Y, S) = detile(src_tiling, offset) */
1285 if (tex_tiled_w != key->src_tiled_w)
1286 src_pos = blorp_nir_retile_w_to_y(&b, src_pos);
1287 /* Now (X, Y, S) = detile(tex_tiling, offset) */
1288 src_pos = blorp_nir_decode_msaa(&b, src_pos, key->tex_samples,
1289 key->tex_layout);
1290 }
1291
1292 if (key->bilinear_filter) {
1293 color = blorp_nir_tex(&b, src_pos, key->texture_data_type);
1294 } else {
1295 /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
1296 *
1297 * In other words: X, Y, and S now contain values which, when passed to
1298 * the texturing unit, will cause data to be read from the correct
1299 * memory location. So we can fetch the texel now.
1300 */
1301 if (key->src_samples == 0) {
1302 color = blorp_nir_txf(&b, &v, src_pos, key->texture_data_type);
1303 } else {
1304 nir_ssa_def *mcs = NULL;
1305 if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
1306 mcs = blorp_nir_txf_ms_mcs(&b, src_pos);
1307
1308 color = blorp_nir_txf_ms(&b, src_pos, mcs, key->texture_data_type);
1309 }
1310 }
1311 }
1312
1313 nir_store_var(&b, v.color_out, color, 0xf);
1314
1315 return b.shader;
1316
1317 fail:
1318 ralloc_free(b.shader);
1319 return NULL;
1320 }
1321
1322 class brw_blorp_blit_program : public brw_blorp_eu_emitter
1323 {
1324 public:
1325 brw_blorp_blit_program(struct brw_context *brw,
1326 const brw_blorp_blit_prog_key *key);
1327
1328 const GLuint *compile(struct brw_context *brw, bool debug_flag,
1329 GLuint *program_size);
1330
1331 brw_blorp_prog_data prog_data;
1332
1333 private:
1334 void alloc_regs();
1335 void alloc_push_const_regs(int base_reg);
1336 void compute_frag_coords();
1337 void translate_tiling(bool old_tiled_w, bool new_tiled_w);
1338 void encode_msaa(unsigned num_samples, intel_msaa_layout layout);
1339 void decode_msaa(unsigned num_samples, intel_msaa_layout layout);
1340 void translate_dst_to_src();
1341 void clamp_tex_coords(struct brw_reg regX, struct brw_reg regY,
1342 struct brw_reg clampX0, struct brw_reg clampY0,
1343 struct brw_reg clampX1, struct brw_reg clampY1);
1344 void single_to_blend();
1345 void manual_blend_average(unsigned num_samples);
1346 void manual_blend_bilinear(unsigned num_samples);
1347 void sample(struct brw_reg dst);
1348 void texel_fetch(struct brw_reg dst);
1349 void mcs_fetch();
1350 void texture_lookup(struct brw_reg dst, enum opcode op,
1351 const sampler_message_arg *args, int num_args);
1352 void render_target_write();
1353
1354 /**
1355 * Base-2 logarithm of the maximum number of samples that can be blended.
1356 */
1357 static const unsigned LOG2_MAX_BLEND_SAMPLES = 3;
1358
1359 struct brw_context *brw;
1360 const brw_blorp_blit_prog_key *key;
1361
1362 /* Thread dispatch header */
1363 struct brw_reg R0;
1364
1365 /* Pixel X/Y coordinates (always in R1). */
1366 struct brw_reg R1;
1367
1368 /* Push constants */
1369 struct brw_reg dst_x0;
1370 struct brw_reg dst_x1;
1371 struct brw_reg dst_y0;
1372 struct brw_reg dst_y1;
1373 /* Top right coordinates of the rectangular grid used for scaled blitting */
1374 struct brw_reg rect_grid_x1;
1375 struct brw_reg rect_grid_y1;
1376 struct {
1377 struct brw_reg multiplier;
1378 struct brw_reg offset;
1379 } x_transform, y_transform;
1380 struct brw_reg src_z;
1381
1382 /* Data read from texture (4 vec16's per array element) */
1383 struct brw_reg texture_data[LOG2_MAX_BLEND_SAMPLES + 1];
1384
1385 /* Auxiliary storage for the contents of the MCS surface.
1386 *
1387 * Since the sampler always returns 8 registers worth of data, this is 8
1388 * registers wide, even though we only use the first 2 registers of it.
1389 */
1390 struct brw_reg mcs_data;
1391
1392 /* X coordinates. We have two of them so that we can perform coordinate
1393 * transformations easily.
1394 */
1395 struct brw_reg x_coords[2];
1396
1397 /* Y coordinates. We have two of them so that we can perform coordinate
1398 * transformations easily.
1399 */
1400 struct brw_reg y_coords[2];
1401
1402 /* X, Y coordinates of the pixel from which we need to fetch the specific
1403 * sample. These are used for multisample scaled blitting.
1404 */
1405 struct brw_reg x_sample_coords;
1406 struct brw_reg y_sample_coords;
1407
1408 /* Fractional parts of the x and y coordinates, used as bilinear interpolation coefficients */
1409 struct brw_reg x_frac;
1410 struct brw_reg y_frac;
1411
1412 /* Which element of x_coords and y_coords is currently in use.
1413 */
1414 int xy_coord_index;
1415
1416 /* True if, at the point in the program currently being compiled, the
1417 * sample index is known to be zero.
1418 */
1419 bool s_is_zero;
1420
1421 /* Register storing the sample index when s_is_zero is false. */
1422 struct brw_reg sample_index;
1423
1424 /* Temporaries */
1425 struct brw_reg t1;
1426 struct brw_reg t2;
1427
1428 /* MRF used for sampling and render target writes */
1429 GLuint base_mrf;
1430 };
1431
1432 brw_blorp_blit_program::brw_blorp_blit_program(
1433 struct brw_context *brw, const brw_blorp_blit_prog_key *key)
1434 : brw_blorp_eu_emitter(), brw(brw), key(key)
1435 {
1436 }
1437
1438 const GLuint *
1439 brw_blorp_blit_program::compile(struct brw_context *brw, bool debug_flag,
1440 GLuint *program_size)
1441 {
1442 /* Sanity checks */
1443 if (key->dst_tiled_w && key->rt_samples > 0) {
1444 /* If the destination image is W tiled and multisampled, then the thread
1445 * must be dispatched once per sample, not once per pixel. This is
1446 * necessary because after conversion between W and Y tiling, there's no
1447 * guarantee that all samples corresponding to a single pixel will still
1448 * be together.
1449 */
1450 assert(key->persample_msaa_dispatch);
1451 }
1452
1453 if (key->blend) {
1454 /* We are blending, which means we won't have an opportunity to
1455 * translate the tiling and sample count for the texture surface. So
1456 * the surface state for the texture must be configured with the correct
1457 * tiling and sample count.
1458 */
1459 assert(!key->src_tiled_w);
1460 assert(key->tex_samples == key->src_samples);
1461 assert(key->tex_layout == key->src_layout);
1462 assert(key->tex_samples > 0);
1463 }
1464
1465 if (key->persample_msaa_dispatch) {
1466 /* It only makes sense to do persample dispatch if the render target is
1467 * configured as multisampled.
1468 */
1469 assert(key->rt_samples > 0);
1470 }
1471
1472 /* Make sure layout is consistent with sample count */
1473 assert((key->tex_layout == INTEL_MSAA_LAYOUT_NONE) ==
1474 (key->tex_samples == 0));
1475 assert((key->rt_layout == INTEL_MSAA_LAYOUT_NONE) ==
1476 (key->rt_samples == 0));
1477 assert((key->src_layout == INTEL_MSAA_LAYOUT_NONE) ==
1478 (key->src_samples == 0));
1479 assert((key->dst_layout == INTEL_MSAA_LAYOUT_NONE) ==
1480 (key->dst_samples == 0));
1481
1482 /* Set up prog_data */
1483 brw_blorp_prog_data_init(&prog_data);
1484 prog_data.persample_msaa_dispatch = key->persample_msaa_dispatch;
1485
1486 alloc_regs();
1487 compute_frag_coords();
1488
1489 /* Render target and texture hardware don't support W tiling until Gen8. */
1490 const bool rt_tiled_w = false;
1491 const bool tex_tiled_w = brw->gen >= 8 && key->src_tiled_w;
1492
1493 /* The address that data will be written to is determined by the
1494 * coordinates supplied to the WM thread and the tiling and sample count of
1495 * the render target, according to the formula:
1496 *
1497 * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
1498 *
1499 * If the actual tiling and sample count of the destination surface are not
1500 * the same as the configuration of the render target, then these
1501 * coordinates are wrong and we have to adjust them to compensate for the
1502 * difference.
1503 */
1504 if (rt_tiled_w != key->dst_tiled_w ||
1505 key->rt_samples != key->dst_samples ||
1506 key->rt_layout != key->dst_layout) {
1507 encode_msaa(key->rt_samples, key->rt_layout);
1508 /* Now (X, Y, S) = detile(rt_tiling, offset) */
1509 translate_tiling(rt_tiled_w, key->dst_tiled_w);
1510 /* Now (X, Y, S) = detile(dst_tiling, offset) */
1511 decode_msaa(key->dst_samples, key->dst_layout);
1512 }
1513
1514 /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
1515 *
1516 * That is: X, Y and S now contain the true coordinates and sample index of
1517 * the data that the WM thread should output.
1518 *
1519 * If we need to kill pixels that are outside the destination rectangle,
1520 * now is the time to do it.
1521 */
1522
1523 if (key->use_kill)
1524 emit_kill_if_outside_rect(x_coords[xy_coord_index],
1525 y_coords[xy_coord_index],
1526 dst_x0, dst_x1, dst_y0, dst_y1);
1527
1528 /* Next, apply a translation to obtain coordinates in the source image. */
1529 translate_dst_to_src();
1530
1531 /* If the source image is not multisampled, then we want to fetch sample
1532 * number 0, because that's the only sample there is.
1533 */
1534 if (key->src_samples == 0)
1535 s_is_zero = true;
1536
1537 /* X, Y, and S are now the coordinates of the pixel in the source image
1538 * that we want to texture from. Exception: if we are blending, then S is
1539 * irrelevant, because we are going to fetch all samples.
1540 */
1541 if (key->blend && !key->blit_scaled) {
1542 if (brw->gen == 6) {
1543 /* Gen6 hardware an automatically blend using the SAMPLE message */
1544 single_to_blend();
1545 sample(texture_data[0]);
1546 } else {
1547 /* Gen7+ hardware doesn't automaticaly blend. */
1548 manual_blend_average(key->src_samples);
1549 }
1550 } else if(key->blend && key->blit_scaled) {
1551 manual_blend_bilinear(key->src_samples);
1552 } else {
1553 /* We aren't blending, which means we just want to fetch a single sample
1554 * from the source surface. The address that we want to fetch from is
1555 * related to the X, Y and S values according to the formula:
1556 *
1557 * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
1558 *
1559 * If the actual tiling and sample count of the source surface are not
1560 * the same as the configuration of the texture, then we need to adjust
1561 * the coordinates to compensate for the difference.
1562 */
1563 if ((tex_tiled_w != key->src_tiled_w ||
1564 key->tex_samples != key->src_samples ||
1565 key->tex_layout != key->src_layout) &&
1566 !key->bilinear_filter) {
1567 encode_msaa(key->src_samples, key->src_layout);
1568 /* Now (X, Y, S) = detile(src_tiling, offset) */
1569 translate_tiling(key->src_tiled_w, tex_tiled_w);
1570 /* Now (X, Y, S) = detile(tex_tiling, offset) */
1571 decode_msaa(key->tex_samples, key->tex_layout);
1572 }
1573
1574 if (key->bilinear_filter) {
1575 sample(texture_data[0]);
1576 }
1577 else {
1578 /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
1579 *
1580 * In other words: X, Y, and S now contain values which, when passed to
1581 * the texturing unit, will cause data to be read from the correct
1582 * memory location. So we can fetch the texel now.
1583 */
1584 if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
1585 mcs_fetch();
1586 texel_fetch(texture_data[0]);
1587 }
1588 }
1589
1590 /* Finally, write the fetched (or blended) value to the render target and
1591 * terminate the thread.
1592 */
1593 render_target_write();
1594
1595 return get_program(brw, debug_flag, program_size);
1596 }
1597
1598 void
1599 brw_blorp_blit_program::alloc_push_const_regs(int base_reg)
1600 {
1601 #define CONST_LOC(name) offsetof(brw_blorp_wm_push_constants, name)
1602 #define ALLOC_REG(name, type) \
1603 this->name = \
1604 retype(brw_vec1_reg(BRW_GENERAL_REGISTER_FILE, \
1605 base_reg + CONST_LOC(name) / 32, \
1606 (CONST_LOC(name) % 32) / 4), type)
1607
1608 ALLOC_REG(dst_x0, BRW_REGISTER_TYPE_UD);
1609 ALLOC_REG(dst_x1, BRW_REGISTER_TYPE_UD);
1610 ALLOC_REG(dst_y0, BRW_REGISTER_TYPE_UD);
1611 ALLOC_REG(dst_y1, BRW_REGISTER_TYPE_UD);
1612 ALLOC_REG(rect_grid_x1, BRW_REGISTER_TYPE_F);
1613 ALLOC_REG(rect_grid_y1, BRW_REGISTER_TYPE_F);
1614 ALLOC_REG(x_transform.multiplier, BRW_REGISTER_TYPE_F);
1615 ALLOC_REG(x_transform.offset, BRW_REGISTER_TYPE_F);
1616 ALLOC_REG(y_transform.multiplier, BRW_REGISTER_TYPE_F);
1617 ALLOC_REG(y_transform.offset, BRW_REGISTER_TYPE_F);
1618 ALLOC_REG(src_z, BRW_REGISTER_TYPE_UD);
1619 #undef CONST_LOC
1620 #undef ALLOC_REG
1621 }
1622
1623 void
1624 brw_blorp_blit_program::alloc_regs()
1625 {
1626 int reg = 0;
1627 this->R0 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
1628 this->R1 = retype(brw_vec8_grf(reg++, 0), BRW_REGISTER_TYPE_UW);
1629 prog_data.first_curbe_grf_0 = reg;
1630 alloc_push_const_regs(reg);
1631 reg += BRW_BLORP_NUM_PUSH_CONST_REGS;
1632 for (unsigned i = 0; i < ARRAY_SIZE(texture_data); ++i) {
1633 this->texture_data[i] =
1634 retype(vec16(brw_vec8_grf(reg, 0)), key->texture_data_type);
1635 reg += 8;
1636 }
1637 this->mcs_data =
1638 retype(brw_vec8_grf(reg, 0), BRW_REGISTER_TYPE_UD); reg += 8;
1639
1640 for (int i = 0; i < 2; ++i) {
1641 this->x_coords[i]
1642 = retype(brw_vec8_grf(reg, 0), BRW_REGISTER_TYPE_UD);
1643 reg += 2;
1644 this->y_coords[i]
1645 = retype(brw_vec8_grf(reg, 0), BRW_REGISTER_TYPE_UD);
1646 reg += 2;
1647 }
1648
1649 if (key->blit_scaled && key->blend) {
1650 this->x_sample_coords = brw_vec8_grf(reg, 0);
1651 reg += 2;
1652 this->y_sample_coords = brw_vec8_grf(reg, 0);
1653 reg += 2;
1654 this->x_frac = brw_vec8_grf(reg, 0);
1655 reg += 2;
1656 this->y_frac = brw_vec8_grf(reg, 0);
1657 reg += 2;
1658 }
1659
1660 this->xy_coord_index = 0;
1661 this->sample_index
1662 = retype(brw_vec8_grf(reg, 0), BRW_REGISTER_TYPE_UD);
1663 reg += 2;
1664 this->t1 = retype(brw_vec8_grf(reg, 0), BRW_REGISTER_TYPE_UD);
1665 reg += 2;
1666 this->t2 = retype(brw_vec8_grf(reg, 0), BRW_REGISTER_TYPE_UD);
1667 reg += 2;
1668
1669 /* Make sure we didn't run out of registers */
1670 assert(reg <= GEN7_MRF_HACK_START);
1671
1672 int mrf = 2;
1673 this->base_mrf = mrf;
1674 }
1675
1676 /* In the code that follows, X and Y can be used to quickly refer to the
1677 * active elements of x_coords and y_coords, and Xp and Yp ("X prime" and "Y
1678 * prime") to the inactive elements.
1679 *
1680 * S can be used to quickly refer to sample_index.
1681 */
1682 #define X x_coords[xy_coord_index]
1683 #define Y y_coords[xy_coord_index]
1684 #define Xp x_coords[!xy_coord_index]
1685 #define Yp y_coords[!xy_coord_index]
1686 #define S sample_index
1687
1688 /* Quickly swap the roles of (X, Y) and (Xp, Yp). Saves us from having to do
1689 * MOVs to transfor (Xp, Yp) to (X, Y) after a coordinate transformation.
1690 */
1691 #define SWAP_XY_AND_XPYP() xy_coord_index = !xy_coord_index;
1692
1693 /**
1694 * Emit code to compute the X and Y coordinates of the pixels being rendered
1695 * by this WM invocation.
1696 *
1697 * Assuming the render target is set up for Y tiling, these (X, Y) values are
1698 * related to the address offset where outputs will be written by the formula:
1699 *
1700 * (X, Y, S) = decode_msaa(detile(offset)).
1701 *
1702 * (See brw_blorp_blit_program).
1703 */
1704 void
1705 brw_blorp_blit_program::compute_frag_coords()
1706 {
1707 /* R1.2[15:0] = X coordinate of upper left pixel of subspan 0 (pixel 0)
1708 * R1.3[15:0] = X coordinate of upper left pixel of subspan 1 (pixel 4)
1709 * R1.4[15:0] = X coordinate of upper left pixel of subspan 2 (pixel 8)
1710 * R1.5[15:0] = X coordinate of upper left pixel of subspan 3 (pixel 12)
1711 *
1712 * Pixels within a subspan are laid out in this arrangement:
1713 * 0 1
1714 * 2 3
1715 *
1716 * So, to compute the coordinates of each pixel, we need to read every 2nd
1717 * 16-bit value (vstride=2) from R1, starting at the 4th 16-bit value
1718 * (suboffset=4), and duplicate each value 4 times (hstride=0, width=4).
1719 * In other words, the data we want to access is R1.4<2;4,0>UW.
1720 *
1721 * Then, we need to add the repeating sequence (0, 1, 0, 1, ...) to the
1722 * result, since pixels n+1 and n+3 are in the right half of the subspan.
1723 */
1724 emit_add(vec16(retype(X, BRW_REGISTER_TYPE_UW)),
1725 stride(suboffset(R1, 4), 2, 4, 0), brw_imm_v(0x10101010));
1726
1727 /* Similarly, Y coordinates for subspans come from R1.2[31:16] through
1728 * R1.5[31:16], so to get pixel Y coordinates we need to start at the 5th
1729 * 16-bit value instead of the 4th (R1.5<2;4,0>UW instead of
1730 * R1.4<2;4,0>UW).
1731 *
1732 * And we need to add the repeating sequence (0, 0, 1, 1, ...), since
1733 * pixels n+2 and n+3 are in the bottom half of the subspan.
1734 */
1735 emit_add(vec16(retype(Y, BRW_REGISTER_TYPE_UW)),
1736 stride(suboffset(R1, 5), 2, 4, 0), brw_imm_v(0x11001100));
1737
1738 /* Move the coordinates to UD registers. */
1739 emit_mov(vec16(Xp), retype(X, BRW_REGISTER_TYPE_UW));
1740 emit_mov(vec16(Yp), retype(Y, BRW_REGISTER_TYPE_UW));
1741 SWAP_XY_AND_XPYP();
1742
1743 if (key->persample_msaa_dispatch) {
1744 switch (key->rt_samples) {
1745 case 2:
1746 case 4: {
1747 /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 4.
1748 * Therefore, subspan 0 will represent sample 0, subspan 1 will
1749 * represent sample 1, and so on.
1750 *
1751 * So we need to populate S with the sequence (0, 0, 0, 0, 1, 1, 1,
1752 * 1, 2, 2, 2, 2, 3, 3, 3, 3). The easiest way to do this is to
1753 * populate a temporary variable with the sequence (0, 1, 2, 3), and
1754 * then copy from it using vstride=1, width=4, hstride=0.
1755 */
1756 struct brw_reg t1_uw1 = retype(t1, BRW_REGISTER_TYPE_UW);
1757 emit_mov(vec16(t1_uw1), key->rt_samples == 4 ?
1758 brw_imm_v(0x3210) : brw_imm_v(0x1010));
1759 /* Move to UD sample_index register. */
1760 emit_mov_8(S, stride(t1_uw1, 1, 4, 0));
1761 emit_mov_8(offset(S, 1), suboffset(stride(t1_uw1, 1, 4, 0), 2));
1762 break;
1763 }
1764 case 8: {
1765 /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 8.
1766 * Therefore, subspan 0 will represent sample N (where N is 0 or 4),
1767 * subspan 1 will represent sample 1, and so on. We can find the
1768 * value of N by looking at R0.0 bits 7:6 ("Starting Sample Pair
1769 * Index") and multiplying by two (since samples are always delivered
1770 * in pairs). That is, we compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 &
1771 * 0xc0) >> 5.
1772 *
1773 * Then we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1, 2,
1774 * 2, 2, 2, 3, 3, 3, 3), which we compute by populating a temporary
1775 * variable with the sequence (0, 1, 2, 3), and then reading from it
1776 * using vstride=1, width=4, hstride=0.
1777 */
1778 struct brw_reg t1_ud1 = vec1(retype(t1, BRW_REGISTER_TYPE_UD));
1779 struct brw_reg t2_uw1 = retype(t2, BRW_REGISTER_TYPE_UW);
1780 struct brw_reg r0_ud1 = vec1(retype(R0, BRW_REGISTER_TYPE_UD));
1781 emit_and(t1_ud1, r0_ud1, brw_imm_ud(0xc0));
1782 emit_shr(t1_ud1, t1_ud1, brw_imm_ud(5));
1783 emit_mov(vec16(t2_uw1), brw_imm_v(0x3210));
1784 emit_add(vec16(S), retype(t1_ud1, BRW_REGISTER_TYPE_UW),
1785 stride(t2_uw1, 1, 4, 0));
1786 emit_add_8(offset(S, 1),
1787 retype(t1_ud1, BRW_REGISTER_TYPE_UW),
1788 suboffset(stride(t2_uw1, 1, 4, 0), 2));
1789 break;
1790 }
1791 default:
1792 unreachable("Unrecognized sample count in "
1793 "brw_blorp_blit_program::compute_frag_coords()");
1794 }
1795 s_is_zero = false;
1796 } else {
1797 /* Either the destination surface is single-sampled, or the WM will be
1798 * run in MSDISPMODE_PERPIXEL (which causes a single fragment dispatch
1799 * per pixel). In either case, it's not meaningful to compute a sample
1800 * value. Just set it to 0.
1801 */
1802 s_is_zero = true;
1803 }
1804 }
1805
1806 /**
1807 * Emit code to compensate for the difference between Y and W tiling.
1808 *
1809 * This code modifies the X and Y coordinates according to the formula:
1810 *
1811 * (X', Y', S') = detile(new_tiling, tile(old_tiling, X, Y, S))
1812 *
1813 * (See brw_blorp_blit_program).
1814 *
1815 * It can only translate between W and Y tiling, so new_tiling and old_tiling
1816 * are booleans where true represents W tiling and false represents Y tiling.
1817 */
1818 void
1819 brw_blorp_blit_program::translate_tiling(bool old_tiled_w, bool new_tiled_w)
1820 {
1821 if (old_tiled_w == new_tiled_w)
1822 return;
1823
1824 /* In the code that follows, we can safely assume that S = 0, because W
1825 * tiling formats always use IMS layout.
1826 */
1827 assert(s_is_zero);
1828
1829 if (new_tiled_w) {
1830 /* Given X and Y coordinates that describe an address using Y tiling,
1831 * translate to the X and Y coordinates that describe the same address
1832 * using W tiling.
1833 *
1834 * If we break down the low order bits of X and Y, using a
1835 * single letter to represent each low-order bit:
1836 *
1837 * X = A << 7 | 0bBCDEFGH
1838 * Y = J << 5 | 0bKLMNP (1)
1839 *
1840 * Then we can apply the Y tiling formula to see the memory offset being
1841 * addressed:
1842 *
1843 * offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
1844 *
1845 * If we apply the W detiling formula to this memory location, that the
1846 * corresponding X' and Y' coordinates are:
1847 *
1848 * X' = A << 6 | 0bBCDPFH (3)
1849 * Y' = J << 6 | 0bKLMNEG
1850 *
1851 * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
1852 * we need to make the following computation:
1853 *
1854 * X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
1855 * Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
1856 */
1857 emit_and(t1, X, brw_imm_uw(0xfff4)); /* X & ~0b1011 */
1858 emit_shr(t1, t1, brw_imm_uw(1)); /* (X & ~0b1011) >> 1 */
1859 emit_and(t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
1860 emit_shl(t2, t2, brw_imm_uw(2)); /* (Y & 0b1) << 2 */
1861 emit_or(t1, t1, t2); /* (X & ~0b1011) >> 1 | (Y & 0b1) << 2 */
1862 emit_and(t2, X, brw_imm_uw(1)); /* X & 0b1 */
1863 emit_or(Xp, t1, t2);
1864 emit_and(t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1865 emit_shl(t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1866 emit_and(t2, X, brw_imm_uw(8)); /* X & 0b1000 */
1867 emit_shr(t2, t2, brw_imm_uw(2)); /* (X & 0b1000) >> 2 */
1868 emit_or(t1, t1, t2); /* (Y & ~0b1) << 1 | (X & 0b1000) >> 2 */
1869 emit_and(t2, X, brw_imm_uw(2)); /* X & 0b10 */
1870 emit_shr(t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1871 emit_or(Yp, t1, t2);
1872 SWAP_XY_AND_XPYP();
1873 } else {
1874 /* Applying the same logic as above, but in reverse, we obtain the
1875 * formulas:
1876 *
1877 * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
1878 * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
1879 */
1880 emit_and(t1, X, brw_imm_uw(0xfffa)); /* X & ~0b101 */
1881 emit_shl(t1, t1, brw_imm_uw(1)); /* (X & ~0b101) << 1 */
1882 emit_and(t2, Y, brw_imm_uw(2)); /* Y & 0b10 */
1883 emit_shl(t2, t2, brw_imm_uw(2)); /* (Y & 0b10) << 2 */
1884 emit_or(t1, t1, t2); /* (X & ~0b101) << 1 | (Y & 0b10) << 2 */
1885 emit_and(t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
1886 emit_shl(t2, t2, brw_imm_uw(1)); /* (Y & 0b1) << 1 */
1887 emit_or(t1, t1, t2); /* (X & ~0b101) << 1 | (Y & 0b10) << 2
1888 | (Y & 0b1) << 1 */
1889 emit_and(t2, X, brw_imm_uw(1)); /* X & 0b1 */
1890 emit_or(Xp, t1, t2);
1891 emit_and(t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1892 emit_shr(t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1893 emit_and(t2, X, brw_imm_uw(4)); /* X & 0b100 */
1894 emit_shr(t2, t2, brw_imm_uw(2)); /* (X & 0b100) >> 2 */
1895 emit_or(Yp, t1, t2);
1896 SWAP_XY_AND_XPYP();
1897 }
1898 }
1899
1900 /**
1901 * Emit code to compensate for the difference between MSAA and non-MSAA
1902 * surfaces.
1903 *
1904 * This code modifies the X and Y coordinates according to the formula:
1905 *
1906 * (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
1907 *
1908 * (See brw_blorp_blit_program).
1909 */
1910 void
1911 brw_blorp_blit_program::encode_msaa(unsigned num_samples,
1912 intel_msaa_layout layout)
1913 {
1914 switch (layout) {
1915 case INTEL_MSAA_LAYOUT_NONE:
1916 /* No translation necessary, and S should already be zero. */
1917 assert(s_is_zero);
1918 break;
1919 case INTEL_MSAA_LAYOUT_CMS:
1920 /* We can't compensate for compressed layout since at this point in the
1921 * program we haven't read from the MCS buffer.
1922 */
1923 unreachable("Bad layout in encode_msaa");
1924 case INTEL_MSAA_LAYOUT_UMS:
1925 /* No translation necessary. */
1926 break;
1927 case INTEL_MSAA_LAYOUT_IMS:
1928 switch (num_samples) {
1929 case 2:
1930 /* encode_msaa(2, IMS, X, Y, S) = (X', Y', 0)
1931 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
1932 * Y' = Y
1933 */
1934 case 4:
1935 /* encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
1936 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
1937 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1938 */
1939 emit_and(t1, X, brw_imm_uw(0xfffe)); /* X & ~0b1 */
1940 if (!s_is_zero) {
1941 emit_and(t2, S, brw_imm_uw(1)); /* S & 0b1 */
1942 emit_or(t1, t1, t2); /* (X & ~0b1) | (S & 0b1) */
1943 }
1944 emit_shl(t1, t1, brw_imm_uw(1)); /* (X & ~0b1) << 1
1945 | (S & 0b1) << 1 */
1946 if (num_samples == 2) {
1947 emit_mov(Yp, Y);
1948 return;
1949 }
1950
1951 emit_and(t2, X, brw_imm_uw(1)); /* X & 0b1 */
1952 emit_or(Xp, t1, t2);
1953 emit_and(t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1954 emit_shl(t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1955 if (!s_is_zero) {
1956 emit_and(t2, S, brw_imm_uw(2)); /* S & 0b10 */
1957 emit_or(t1, t1, t2); /* (Y & ~0b1) << 1 | (S & 0b10) */
1958 }
1959 emit_and(t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
1960 emit_or(Yp, t1, t2);
1961 break;
1962 case 8:
1963 /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
1964 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
1965 * | (X & 0b1)
1966 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1967 */
1968 emit_and(t1, X, brw_imm_uw(0xfffe)); /* X & ~0b1 */
1969 emit_shl(t1, t1, brw_imm_uw(2)); /* (X & ~0b1) << 2 */
1970 if (!s_is_zero) {
1971 emit_and(t2, S, brw_imm_uw(4)); /* S & 0b100 */
1972 emit_or(t1, t1, t2); /* (X & ~0b1) << 2 | (S & 0b100) */
1973 emit_and(t2, S, brw_imm_uw(1)); /* S & 0b1 */
1974 emit_shl(t2, t2, brw_imm_uw(1)); /* (S & 0b1) << 1 */
1975 emit_or(t1, t1, t2); /* (X & ~0b1) << 2 | (S & 0b100)
1976 | (S & 0b1) << 1 */
1977 }
1978 emit_and(t2, X, brw_imm_uw(1)); /* X & 0b1 */
1979 emit_or(Xp, t1, t2);
1980 emit_and(t1, Y, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1981 emit_shl(t1, t1, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1982 if (!s_is_zero) {
1983 emit_and(t2, S, brw_imm_uw(2)); /* S & 0b10 */
1984 emit_or(t1, t1, t2); /* (Y & ~0b1) << 1 | (S & 0b10) */
1985 }
1986 emit_and(t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
1987 emit_or(Yp, t1, t2);
1988 break;
1989 }
1990 SWAP_XY_AND_XPYP();
1991 s_is_zero = true;
1992 break;
1993 }
1994 }
1995
1996 /**
1997 * Emit code to compensate for the difference between MSAA and non-MSAA
1998 * surfaces.
1999 *
2000 * This code modifies the X and Y coordinates according to the formula:
2001 *
2002 * (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
2003 *
2004 * (See brw_blorp_blit_program).
2005 */
2006 void
2007 brw_blorp_blit_program::decode_msaa(unsigned num_samples,
2008 intel_msaa_layout layout)
2009 {
2010 switch (layout) {
2011 case INTEL_MSAA_LAYOUT_NONE:
2012 /* No translation necessary, and S should already be zero. */
2013 assert(s_is_zero);
2014 break;
2015 case INTEL_MSAA_LAYOUT_CMS:
2016 /* We can't compensate for compressed layout since at this point in the
2017 * program we don't have access to the MCS buffer.
2018 */
2019 unreachable("Bad layout in encode_msaa");
2020 case INTEL_MSAA_LAYOUT_UMS:
2021 /* No translation necessary. */
2022 break;
2023 case INTEL_MSAA_LAYOUT_IMS:
2024 assert(s_is_zero);
2025 switch (num_samples) {
2026 case 2:
2027 /* decode_msaa(2, IMS, X, Y, 0) = (X', Y', S)
2028 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
2029 * S = (X & 0b10) >> 1
2030 */
2031 case 4:
2032 /* decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
2033 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
2034 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
2035 * S = (Y & 0b10) | (X & 0b10) >> 1
2036 */
2037 emit_and(t1, X, brw_imm_uw(0xfffc)); /* X & ~0b11 */
2038 emit_shr(t1, t1, brw_imm_uw(1)); /* (X & ~0b11) >> 1 */
2039 emit_and(t2, X, brw_imm_uw(1)); /* X & 0b1 */
2040 emit_or(Xp, t1, t2);
2041
2042 if (num_samples == 2) {
2043 emit_mov(Yp, Y);
2044 emit_and(t2, X, brw_imm_uw(2)); /* X & 0b10 */
2045 emit_shr(S, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
2046 } else {
2047 emit_and(t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
2048 emit_shr(t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
2049 emit_and(t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
2050 emit_or(Yp, t1, t2);
2051 emit_and(t1, Y, brw_imm_uw(2)); /* Y & 0b10 */
2052 emit_and(t2, X, brw_imm_uw(2)); /* X & 0b10 */
2053 emit_shr(t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
2054 emit_or(S, t1, t2);
2055 }
2056 break;
2057 case 8:
2058 /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
2059 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
2060 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
2061 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
2062 */
2063 emit_and(t1, X, brw_imm_uw(0xfff8)); /* X & ~0b111 */
2064 emit_shr(t1, t1, brw_imm_uw(2)); /* (X & ~0b111) >> 2 */
2065 emit_and(t2, X, brw_imm_uw(1)); /* X & 0b1 */
2066 emit_or(Xp, t1, t2);
2067 emit_and(t1, Y, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
2068 emit_shr(t1, t1, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
2069 emit_and(t2, Y, brw_imm_uw(1)); /* Y & 0b1 */
2070 emit_or(Yp, t1, t2);
2071 emit_and(t1, X, brw_imm_uw(4)); /* X & 0b100 */
2072 emit_and(t2, Y, brw_imm_uw(2)); /* Y & 0b10 */
2073 emit_or(t1, t1, t2); /* (X & 0b100) | (Y & 0b10) */
2074 emit_and(t2, X, brw_imm_uw(2)); /* X & 0b10 */
2075 emit_shr(t2, t2, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
2076 emit_or(S, t1, t2);
2077 break;
2078 }
2079 s_is_zero = false;
2080 SWAP_XY_AND_XPYP();
2081 break;
2082 }
2083 }
2084
2085 /**
2086 * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
2087 * coordinates.
2088 */
2089 void
2090 brw_blorp_blit_program::translate_dst_to_src()
2091 {
2092 struct brw_reg X_f = retype(X, BRW_REGISTER_TYPE_F);
2093 struct brw_reg Y_f = retype(Y, BRW_REGISTER_TYPE_F);
2094 struct brw_reg Xp_f = retype(Xp, BRW_REGISTER_TYPE_F);
2095 struct brw_reg Yp_f = retype(Yp, BRW_REGISTER_TYPE_F);
2096
2097 /* Move the UD coordinates to float registers. */
2098 emit_mov(Xp_f, X);
2099 emit_mov(Yp_f, Y);
2100 /* Scale and offset */
2101 emit_mad(X_f, x_transform.offset, Xp_f, x_transform.multiplier);
2102 emit_mad(Y_f, y_transform.offset, Yp_f, y_transform.multiplier);
2103 if (key->blit_scaled && key->blend) {
2104 /* Translate coordinates to lay out the samples in a rectangular grid
2105 * roughly corresponding to sample locations.
2106 */
2107 emit_mul(X_f, X_f, brw_imm_f(key->x_scale));
2108 emit_mul(Y_f, Y_f, brw_imm_f(key->y_scale));
2109 /* Adjust coordinates so that integers represent pixel centers rather
2110 * than pixel edges.
2111 */
2112 emit_add(X_f, X_f, brw_imm_f(-0.5));
2113 emit_add(Y_f, Y_f, brw_imm_f(-0.5));
2114
2115 /* Clamp the X, Y texture coordinates to properly handle the sampling of
2116 * texels on texture edges.
2117 */
2118 clamp_tex_coords(X_f, Y_f,
2119 brw_imm_f(0.0), brw_imm_f(0.0),
2120 rect_grid_x1, rect_grid_y1);
2121
2122 /* Store the fractional parts to be used as bilinear interpolation
2123 * coefficients.
2124 */
2125 emit_frc(x_frac, X_f);
2126 emit_frc(y_frac, Y_f);
2127
2128 /* Round the float coordinates down to nearest integer */
2129 emit_rndd(Xp_f, X_f);
2130 emit_rndd(Yp_f, Y_f);
2131 emit_mul(X_f, Xp_f, brw_imm_f(1.0f / key->x_scale));
2132 emit_mul(Y_f, Yp_f, brw_imm_f(1.0f / key->y_scale));
2133 SWAP_XY_AND_XPYP();
2134 } else if (!key->bilinear_filter) {
2135 /* Round the float coordinates down to nearest integer by moving to
2136 * UD registers.
2137 */
2138 emit_mov(Xp, X_f);
2139 emit_mov(Yp, Y_f);
2140 SWAP_XY_AND_XPYP();
2141 }
2142 }
2143
2144 void
2145 brw_blorp_blit_program::clamp_tex_coords(struct brw_reg regX,
2146 struct brw_reg regY,
2147 struct brw_reg clampX0,
2148 struct brw_reg clampY0,
2149 struct brw_reg clampX1,
2150 struct brw_reg clampY1)
2151 {
2152 emit_max(regX, regX, clampX0);
2153 emit_max(regY, regY, clampY0);
2154 emit_min(regX, regX, clampX1);
2155 emit_min(regY, regY, clampY1);
2156 }
2157
2158
2159
2160 void
2161 brw_blorp_blit_program::manual_blend_average(unsigned num_samples)
2162 {
2163 if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
2164 mcs_fetch();
2165
2166 assert(key->texture_data_type == BRW_REGISTER_TYPE_F);
2167
2168 /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
2169 *
2170 * result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
2171 *
2172 * This ensures that when all samples have the same value, no numerical
2173 * precision is lost, since each addition operation always adds two equal
2174 * values, and summing two equal floating point values does not lose
2175 * precision.
2176 *
2177 * We perform this computation by treating the texture_data array as a
2178 * stack and performing the following operations:
2179 *
2180 * - push sample 0 onto stack
2181 * - push sample 1 onto stack
2182 * - add top two stack entries
2183 * - push sample 2 onto stack
2184 * - push sample 3 onto stack
2185 * - add top two stack entries
2186 * - add top two stack entries
2187 * - divide top stack entry by 4
2188 *
2189 * Note that after pushing sample i onto the stack, the number of add
2190 * operations we do is equal to the number of trailing 1 bits in i. This
2191 * works provided the total number of samples is a power of two, which it
2192 * always is for i965.
2193 *
2194 * For integer formats, we replace the add operations with average
2195 * operations and skip the final division.
2196 */
2197 unsigned stack_depth = 0;
2198 for (unsigned i = 0; i < num_samples; ++i) {
2199 assert(stack_depth == _mesa_bitcount(i)); /* Loop invariant */
2200
2201 /* Push sample i onto the stack */
2202 assert(stack_depth < ARRAY_SIZE(texture_data));
2203 if (i == 0) {
2204 s_is_zero = true;
2205 } else {
2206 s_is_zero = false;
2207 emit_mov(vec16(S), brw_imm_ud(i));
2208 }
2209 texel_fetch(texture_data[stack_depth++]);
2210
2211 if (i == 0 && key->tex_layout == INTEL_MSAA_LAYOUT_CMS) {
2212 /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
2213 * suggests an optimization:
2214 *
2215 * "A simple optimization with probable large return in
2216 * performance is to compare the MCS value to zero (indicating
2217 * all samples are on sample slice 0), and sample only from
2218 * sample slice 0 using ld2dss if MCS is zero."
2219 *
2220 * Note that in the case where the MCS value is zero, sampling from
2221 * sample slice 0 using ld2dss and sampling from sample 0 using
2222 * ld2dms are equivalent (since all samples are on sample slice 0).
2223 * Since we have already sampled from sample 0, all we need to do is
2224 * skip the remaining fetches and averaging if MCS is zero.
2225 */
2226 emit_cmp_if(BRW_CONDITIONAL_NZ, mcs_data, brw_imm_ud(0));
2227 }
2228
2229 /* Do count_trailing_one_bits(i) times */
2230 for (int j = count_trailing_one_bits(i); j-- > 0; ) {
2231 assert(stack_depth >= 2);
2232 --stack_depth;
2233
2234 /* TODO: should use a smaller loop bound for non_RGBA formats */
2235 for (int k = 0; k < 4; ++k) {
2236 emit_combine(BRW_OPCODE_ADD,
2237 offset(texture_data[stack_depth - 1], 2*k),
2238 offset(vec8(texture_data[stack_depth - 1]), 2*k),
2239 offset(vec8(texture_data[stack_depth]), 2*k));
2240 }
2241 }
2242 }
2243
2244 /* We should have just 1 sample on the stack now. */
2245 assert(stack_depth == 1);
2246
2247 /* Scale the result down by a factor of num_samples */
2248 /* TODO: should use a smaller loop bound for non-RGBA formats */
2249 for (int j = 0; j < 4; ++j) {
2250 emit_mul(offset(texture_data[0], 2*j),
2251 offset(vec8(texture_data[0]), 2*j),
2252 brw_imm_f(1.0f / num_samples));
2253 }
2254
2255 if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
2256 emit_endif();
2257 }
2258
2259 void
2260 brw_blorp_blit_program::manual_blend_bilinear(unsigned num_samples)
2261 {
2262 /* We do this computation by performing the following operations:
2263 *
2264 * In case of 4x, 8x MSAA:
2265 * - Compute the pixel coordinates and sample numbers (a, b, c, d)
2266 * which are later used for interpolation
2267 * - linearly interpolate samples a and b in X
2268 * - linearly interpolate samples c and d in X
2269 * - linearly interpolate the results of last two operations in Y
2270 *
2271 * result = lrp(lrp(a + b) + lrp(c + d))
2272 */
2273 struct brw_reg Xp_f = retype(Xp, BRW_REGISTER_TYPE_F);
2274 struct brw_reg Yp_f = retype(Yp, BRW_REGISTER_TYPE_F);
2275 struct brw_reg t1_f = retype(t1, BRW_REGISTER_TYPE_F);
2276 struct brw_reg t2_f = retype(t2, BRW_REGISTER_TYPE_F);
2277
2278 for (unsigned i = 0; i < 4; ++i) {
2279 assert(i < ARRAY_SIZE(texture_data));
2280 s_is_zero = false;
2281
2282 /* Compute pixel coordinates */
2283 emit_add(vec16(x_sample_coords), Xp_f,
2284 brw_imm_f((float)(i & 0x1) * (1.0f / key->x_scale)));
2285 emit_add(vec16(y_sample_coords), Yp_f,
2286 brw_imm_f((float)((i >> 1) & 0x1) * (1.0f / key->y_scale)));
2287 emit_mov(vec16(X), x_sample_coords);
2288 emit_mov(vec16(Y), y_sample_coords);
2289
2290 /* The MCS value we fetch has to match up with the pixel that we're
2291 * sampling from. Since we sample from different pixels in each
2292 * iteration of this "for" loop, the call to mcs_fetch() should be
2293 * here inside the loop after computing the pixel coordinates.
2294 */
2295 if (key->tex_layout == INTEL_MSAA_LAYOUT_CMS)
2296 mcs_fetch();
2297
2298 /* Compute sample index and map the sample index to a sample number.
2299 * Sample index layout shows the numbering of slots in a rectangular
2300 * grid of samples with in a pixel. Sample number layout shows the
2301 * rectangular grid of samples roughly corresponding to the real sample
2302 * locations with in a pixel.
2303 * In case of 4x MSAA, layout of sample indices matches the layout of
2304 * sample numbers:
2305 * ---------
2306 * | 0 | 1 |
2307 * ---------
2308 * | 2 | 3 |
2309 * ---------
2310 *
2311 * In case of 8x MSAA the two layouts don't match.
2312 * sample index layout : --------- sample number layout : ---------
2313 * | 0 | 1 | | 5 | 2 |
2314 * --------- ---------
2315 * | 2 | 3 | | 4 | 6 |
2316 * --------- ---------
2317 * | 4 | 5 | | 0 | 3 |
2318 * --------- ---------
2319 * | 6 | 7 | | 7 | 1 |
2320 * --------- ---------
2321 *
2322 * Fortunately, this can be done fairly easily as:
2323 * S' = (0x17306425 >> (S * 4)) & 0xf
2324 */
2325 emit_frc(vec16(t1_f), x_sample_coords);
2326 emit_frc(vec16(t2_f), y_sample_coords);
2327 emit_mul(vec16(t1_f), t1_f, brw_imm_f(key->x_scale));
2328 emit_mul(vec16(t2_f), t2_f, brw_imm_f(key->x_scale * key->y_scale));
2329 emit_add(vec16(t1_f), t1_f, t2_f);
2330 emit_mov(vec16(S), t1_f);
2331
2332 if (num_samples == 8) {
2333 emit_mov(vec16(t2), brw_imm_d(0x17306425));
2334 emit_shl(vec16(S), S, brw_imm_d(2));
2335 emit_shr(vec16(S), t2, S);
2336 emit_and(vec16(S), S, brw_imm_d(0xf));
2337 }
2338 texel_fetch(texture_data[i]);
2339 }
2340
2341 #define SAMPLE(x, y) offset(texture_data[x], y)
2342 for (int index = 3; index > 0; ) {
2343 /* Since we're doing SIMD16, 4 color channels fits in to 8 registers.
2344 * Counter value of 8 in 'for' loop below is used to interpolate all
2345 * the color components.
2346 */
2347 for (int k = 0; k < 8; k += 2)
2348 emit_lrp(vec8(SAMPLE(index - 1, k)),
2349 x_frac,
2350 vec8(SAMPLE(index, k)),
2351 vec8(SAMPLE(index - 1, k)));
2352 index -= 2;
2353 }
2354 for (int k = 0; k < 8; k += 2)
2355 emit_lrp(vec8(SAMPLE(0, k)),
2356 y_frac,
2357 vec8(SAMPLE(2, k)),
2358 vec8(SAMPLE(0, k)));
2359 #undef SAMPLE
2360 }
2361
2362 /**
2363 * Emit code to look up a value in the texture using the SAMPLE message (which
2364 * does blending of MSAA surfaces).
2365 */
2366 void
2367 brw_blorp_blit_program::sample(struct brw_reg dst)
2368 {
2369 static const sampler_message_arg args[2] = {
2370 SAMPLER_MESSAGE_ARG_U_FLOAT,
2371 SAMPLER_MESSAGE_ARG_V_FLOAT
2372 };
2373
2374 texture_lookup(dst, SHADER_OPCODE_TEX, args, ARRAY_SIZE(args));
2375 }
2376
2377 /**
2378 * Emit code to look up a value in the texture using the SAMPLE_LD message
2379 * (which does a simple texel fetch).
2380 */
2381 void
2382 brw_blorp_blit_program::texel_fetch(struct brw_reg dst)
2383 {
2384 static const sampler_message_arg gen6_args[5] = {
2385 SAMPLER_MESSAGE_ARG_U_INT,
2386 SAMPLER_MESSAGE_ARG_V_INT,
2387 SAMPLER_MESSAGE_ARG_ZERO_INT, /* R */
2388 SAMPLER_MESSAGE_ARG_ZERO_INT, /* LOD */
2389 SAMPLER_MESSAGE_ARG_SI_INT
2390 };
2391 static const sampler_message_arg gen7_ld_args[] = {
2392 SAMPLER_MESSAGE_ARG_U_INT,
2393 SAMPLER_MESSAGE_ARG_ZERO_INT, /* LOD */
2394 SAMPLER_MESSAGE_ARG_V_INT,
2395 SAMPLER_MESSAGE_ARG_R_INT
2396 };
2397 static const sampler_message_arg gen7_ld2dss_args[3] = {
2398 SAMPLER_MESSAGE_ARG_SI_INT,
2399 SAMPLER_MESSAGE_ARG_U_INT,
2400 SAMPLER_MESSAGE_ARG_V_INT
2401 };
2402 static const sampler_message_arg gen7_ld2dms_args[4] = {
2403 SAMPLER_MESSAGE_ARG_SI_INT,
2404 SAMPLER_MESSAGE_ARG_MCS_INT,
2405 SAMPLER_MESSAGE_ARG_U_INT,
2406 SAMPLER_MESSAGE_ARG_V_INT
2407 };
2408 static const sampler_message_arg gen9_ld_args[] = {
2409 SAMPLER_MESSAGE_ARG_U_INT,
2410 SAMPLER_MESSAGE_ARG_V_INT,
2411 SAMPLER_MESSAGE_ARG_ZERO_INT, /* LOD */
2412 SAMPLER_MESSAGE_ARG_R_INT
2413 };
2414
2415 switch (brw->gen) {
2416 case 6:
2417 texture_lookup(dst, SHADER_OPCODE_TXF, gen6_args, s_is_zero ? 2 : 5);
2418 break;
2419 case 7:
2420 case 8:
2421 case 9:
2422 switch (key->tex_layout) {
2423 case INTEL_MSAA_LAYOUT_IMS:
2424 /* From the Ivy Bridge PRM, Vol4 Part1 p72 (Multisampled Surface Storage
2425 * Format):
2426 *
2427 * If this field is MSFMT_DEPTH_STENCIL
2428 * [a.k.a. INTEL_MSAA_LAYOUT_IMS], the only sampling engine
2429 * messages allowed are "ld2dms", "resinfo", and "sampleinfo".
2430 *
2431 * So fall through to emit the same message as we use for
2432 * INTEL_MSAA_LAYOUT_CMS.
2433 */
2434 case INTEL_MSAA_LAYOUT_CMS:
2435 texture_lookup(dst, SHADER_OPCODE_TXF_CMS,
2436 gen7_ld2dms_args, ARRAY_SIZE(gen7_ld2dms_args));
2437 break;
2438 case INTEL_MSAA_LAYOUT_UMS:
2439 texture_lookup(dst, SHADER_OPCODE_TXF_UMS,
2440 gen7_ld2dss_args, ARRAY_SIZE(gen7_ld2dss_args));
2441 break;
2442 case INTEL_MSAA_LAYOUT_NONE:
2443 assert(s_is_zero);
2444 if (brw->gen < 9) {
2445 texture_lookup(dst, SHADER_OPCODE_TXF, gen7_ld_args,
2446 ARRAY_SIZE(gen7_ld_args));
2447 } else {
2448 texture_lookup(dst, SHADER_OPCODE_TXF, gen9_ld_args,
2449 ARRAY_SIZE(gen9_ld_args));
2450 }
2451 break;
2452 }
2453 break;
2454 default:
2455 unreachable("Should not get here.");
2456 };
2457 }
2458
2459 void
2460 brw_blorp_blit_program::mcs_fetch()
2461 {
2462 static const sampler_message_arg gen7_ld_mcs_args[2] = {
2463 SAMPLER_MESSAGE_ARG_U_INT,
2464 SAMPLER_MESSAGE_ARG_V_INT
2465 };
2466 texture_lookup(vec16(mcs_data), SHADER_OPCODE_TXF_MCS,
2467 gen7_ld_mcs_args, ARRAY_SIZE(gen7_ld_mcs_args));
2468 }
2469
2470 void
2471 brw_blorp_blit_program::texture_lookup(struct brw_reg dst,
2472 enum opcode op,
2473 const sampler_message_arg *args,
2474 int num_args)
2475 {
2476 struct brw_reg mrf =
2477 retype(vec16(brw_message_reg(base_mrf)), BRW_REGISTER_TYPE_UD);
2478 for (int arg = 0; arg < num_args; ++arg) {
2479 switch (args[arg]) {
2480 case SAMPLER_MESSAGE_ARG_U_FLOAT:
2481 if (key->bilinear_filter)
2482 emit_mov(retype(mrf, BRW_REGISTER_TYPE_F),
2483 retype(X, BRW_REGISTER_TYPE_F));
2484 else
2485 emit_mov(retype(mrf, BRW_REGISTER_TYPE_F), X);
2486 break;
2487 case SAMPLER_MESSAGE_ARG_V_FLOAT:
2488 if (key->bilinear_filter)
2489 emit_mov(retype(mrf, BRW_REGISTER_TYPE_F),
2490 retype(Y, BRW_REGISTER_TYPE_F));
2491 else
2492 emit_mov(retype(mrf, BRW_REGISTER_TYPE_F), Y);
2493 break;
2494 case SAMPLER_MESSAGE_ARG_U_INT:
2495 emit_mov(mrf, X);
2496 break;
2497 case SAMPLER_MESSAGE_ARG_V_INT:
2498 emit_mov(mrf, Y);
2499 break;
2500 case SAMPLER_MESSAGE_ARG_R_INT:
2501 emit_mov(mrf, src_z);
2502 break;
2503 case SAMPLER_MESSAGE_ARG_SI_INT:
2504 /* Note: on Gen7, this code may be reached with s_is_zero==true
2505 * because in Gen7's ld2dss message, the sample index is the first
2506 * argument. When this happens, we need to move a 0 into the
2507 * appropriate message register.
2508 */
2509 if (s_is_zero)
2510 emit_mov(mrf, brw_imm_ud(0));
2511 else
2512 emit_mov(mrf, S);
2513 break;
2514 case SAMPLER_MESSAGE_ARG_MCS_INT:
2515 switch (key->tex_layout) {
2516 case INTEL_MSAA_LAYOUT_CMS:
2517 emit_mov(mrf, mcs_data);
2518 break;
2519 case INTEL_MSAA_LAYOUT_IMS:
2520 /* When sampling from an IMS surface, MCS data is not relevant,
2521 * and the hardware ignores it. So don't bother populating it.
2522 */
2523 break;
2524 default:
2525 /* We shouldn't be trying to send MCS data with any other
2526 * layouts.
2527 */
2528 assert (!"Unsupported layout for MCS data");
2529 break;
2530 }
2531 break;
2532 case SAMPLER_MESSAGE_ARG_ZERO_INT:
2533 emit_mov(mrf, brw_imm_ud(0));
2534 break;
2535 }
2536 mrf.nr += 2;
2537 }
2538
2539 emit_texture_lookup(retype(dst, BRW_REGISTER_TYPE_UW) /* dest */,
2540 op,
2541 base_mrf,
2542 mrf.nr - base_mrf /* msg_length */);
2543 }
2544
2545 #undef X
2546 #undef Y
2547 #undef U
2548 #undef V
2549 #undef S
2550 #undef SWAP_XY_AND_XPYP
2551
2552 static void
2553 brw_blorp_get_blit_kernel(struct brw_context *brw,
2554 struct brw_blorp_params *params,
2555 const struct brw_blorp_blit_prog_key *prog_key)
2556 {
2557 if (brw_search_cache(&brw->cache, BRW_CACHE_BLORP_PROG,
2558 prog_key, sizeof(*prog_key),
2559 &params->wm_prog_kernel, &params->wm_prog_data))
2560 return;
2561
2562 const unsigned *program;
2563 unsigned program_size;
2564 struct brw_blorp_prog_data prog_data;
2565
2566 /* Try and compile with NIR first. If that fails, fall back to the old
2567 * method of building shaders manually.
2568 */
2569 nir_shader *nir = brw_blorp_build_nir_shader(brw, prog_key, &prog_data);
2570 if (nir) {
2571 struct brw_wm_prog_key wm_key;
2572 brw_blorp_init_wm_prog_key(&wm_key);
2573 wm_key.tex.compressed_multisample_layout_mask =
2574 prog_key->tex_layout == INTEL_MSAA_LAYOUT_CMS;
2575 wm_key.multisample_fbo = prog_key->rt_samples > 1;
2576
2577 program = brw_blorp_compile_nir_shader(brw, nir, &wm_key, false,
2578 &prog_data, &program_size);
2579 } else {
2580 brw_blorp_blit_program prog(brw, prog_key);
2581 program = prog.compile(brw, INTEL_DEBUG & DEBUG_BLORP, &program_size);
2582 prog_data = prog.prog_data;
2583 }
2584
2585 brw_upload_cache(&brw->cache, BRW_CACHE_BLORP_PROG,
2586 prog_key, sizeof(*prog_key),
2587 program, program_size,
2588 &prog_data, sizeof(prog_data),
2589 &params->wm_prog_kernel, &params->wm_prog_data);
2590 }
2591
2592 void
2593 brw_blorp_blit_program::render_target_write()
2594 {
2595 struct brw_reg mrf_rt_write =
2596 retype(vec16(brw_message_reg(base_mrf)), key->texture_data_type);
2597 int mrf_offset = 0;
2598
2599 /* If we may have killed pixels, then we need to send R0 and R1 in a header
2600 * so that the render target knows which pixels we killed.
2601 */
2602 bool use_header = key->use_kill;
2603 if (use_header) {
2604 /* Copy R0/1 to MRF */
2605 emit_mov(retype(mrf_rt_write, BRW_REGISTER_TYPE_UD),
2606 retype(R0, BRW_REGISTER_TYPE_UD));
2607 mrf_offset += 2;
2608 }
2609
2610 /* Copy texture data to MRFs */
2611 for (int i = 0; i < 4; ++i) {
2612 /* E.g. mov(16) m2.0<1>:f r2.0<8;8,1>:f { Align1, H1 } */
2613 emit_mov(offset(mrf_rt_write, mrf_offset),
2614 offset(vec8(texture_data[0]), 2*i));
2615 mrf_offset += 2;
2616 }
2617
2618 /* Now write to the render target and terminate the thread */
2619 emit_render_target_write(
2620 mrf_rt_write,
2621 brw->gen < 8 ? base_mrf : -1,
2622 mrf_offset /* msg_length. TODO: Should be smaller for non-RGBA formats. */,
2623 use_header);
2624 }
2625
2626
2627 static void
2628 brw_blorp_setup_coord_transform(struct brw_blorp_coord_transform *xform,
2629 GLfloat src0, GLfloat src1,
2630 GLfloat dst0, GLfloat dst1,
2631 bool mirror)
2632 {
2633 float scale = (src1 - src0) / (dst1 - dst0);
2634 if (!mirror) {
2635 /* When not mirroring a coordinate (say, X), we need:
2636 * src_x - src_x0 = (dst_x - dst_x0 + 0.5) * scale
2637 * Therefore:
2638 * src_x = src_x0 + (dst_x - dst_x0 + 0.5) * scale
2639 *
2640 * blorp program uses "round toward zero" to convert the
2641 * transformed floating point coordinates to integer coordinates,
2642 * whereas the behaviour we actually want is "round to nearest",
2643 * so 0.5 provides the necessary correction.
2644 */
2645 xform->multiplier = scale;
2646 xform->offset = src0 + (-dst0 + 0.5f) * scale;
2647 } else {
2648 /* When mirroring X we need:
2649 * src_x - src_x0 = dst_x1 - dst_x - 0.5
2650 * Therefore:
2651 * src_x = src_x0 + (dst_x1 -dst_x - 0.5) * scale
2652 */
2653 xform->multiplier = -scale;
2654 xform->offset = src0 + (dst1 - 0.5f) * scale;
2655 }
2656 }
2657
2658
2659 /**
2660 * Determine which MSAA layout the GPU pipeline should be configured for,
2661 * based on the chip generation, the number of samples, and the true layout of
2662 * the image in memory.
2663 */
2664 inline intel_msaa_layout
2665 compute_msaa_layout_for_pipeline(struct brw_context *brw, unsigned num_samples,
2666 intel_msaa_layout true_layout)
2667 {
2668 if (num_samples <= 1) {
2669 /* Layout is used to determine if ld2dms is needed for sampling. In
2670 * single sampled case normal ld is enough avoiding also the need to
2671 * fetch mcs. Therefore simply set the layout to none.
2672 */
2673 if (brw->gen >= 9 && true_layout == INTEL_MSAA_LAYOUT_CMS) {
2674 return INTEL_MSAA_LAYOUT_NONE;
2675 }
2676
2677 /* When configuring the GPU for non-MSAA, we can still accommodate IMS
2678 * format buffers, by transforming coordinates appropriately.
2679 */
2680 assert(true_layout == INTEL_MSAA_LAYOUT_NONE ||
2681 true_layout == INTEL_MSAA_LAYOUT_IMS);
2682 return INTEL_MSAA_LAYOUT_NONE;
2683 } else {
2684 assert(true_layout != INTEL_MSAA_LAYOUT_NONE);
2685 }
2686
2687 /* Prior to Gen7, all MSAA surfaces use IMS layout. */
2688 if (brw->gen == 6) {
2689 assert(true_layout == INTEL_MSAA_LAYOUT_IMS);
2690 }
2691
2692 return true_layout;
2693 }
2694
2695
2696 /**
2697 * Note: if the src (or dst) is a 2D multisample array texture on Gen7+ using
2698 * INTEL_MSAA_LAYOUT_UMS or INTEL_MSAA_LAYOUT_CMS, src_layer (dst_layer) is
2699 * the physical layer holding sample 0. So, for example, if
2700 * src_mt->num_samples == 4, then logical layer n corresponds to src_layer ==
2701 * 4*n.
2702 */
2703 void
2704 brw_blorp_blit_miptrees(struct brw_context *brw,
2705 struct intel_mipmap_tree *src_mt,
2706 unsigned src_level, unsigned src_layer,
2707 mesa_format src_format, int src_swizzle,
2708 struct intel_mipmap_tree *dst_mt,
2709 unsigned dst_level, unsigned dst_layer,
2710 mesa_format dst_format,
2711 float src_x0, float src_y0,
2712 float src_x1, float src_y1,
2713 float dst_x0, float dst_y0,
2714 float dst_x1, float dst_y1,
2715 GLenum filter, bool mirror_x, bool mirror_y,
2716 bool decode_srgb, bool encode_srgb)
2717 {
2718 /* Get ready to blit. This includes depth resolving the src and dst
2719 * buffers if necessary. Note: it's not necessary to do a color resolve on
2720 * the destination buffer because we use the standard render path to render
2721 * to destination color buffers, and the standard render path is
2722 * fast-color-aware.
2723 */
2724 intel_miptree_resolve_color(brw, src_mt, INTEL_MIPTREE_IGNORE_CCS_E);
2725 intel_miptree_slice_resolve_depth(brw, src_mt, src_level, src_layer);
2726 intel_miptree_slice_resolve_depth(brw, dst_mt, dst_level, dst_layer);
2727
2728 intel_miptree_prepare_mcs(brw, dst_mt);
2729
2730 DBG("%s from %dx %s mt %p %d %d (%f,%f) (%f,%f)"
2731 "to %dx %s mt %p %d %d (%f,%f) (%f,%f) (flip %d,%d)\n",
2732 __func__,
2733 src_mt->num_samples, _mesa_get_format_name(src_mt->format), src_mt,
2734 src_level, src_layer, src_x0, src_y0, src_x1, src_y1,
2735 dst_mt->num_samples, _mesa_get_format_name(dst_mt->format), dst_mt,
2736 dst_level, dst_layer, dst_x0, dst_y0, dst_x1, dst_y1,
2737 mirror_x, mirror_y);
2738
2739 if (!decode_srgb && _mesa_get_format_color_encoding(src_format) == GL_SRGB)
2740 src_format = _mesa_get_srgb_format_linear(src_format);
2741
2742 if (!encode_srgb && _mesa_get_format_color_encoding(dst_format) == GL_SRGB)
2743 dst_format = _mesa_get_srgb_format_linear(dst_format);
2744
2745 struct brw_blorp_params params;
2746 brw_blorp_params_init(&params);
2747
2748 brw_blorp_surface_info_init(brw, &params.src, src_mt, src_level,
2749 src_layer, src_format, false);
2750 brw_blorp_surface_info_init(brw, &params.dst, dst_mt, dst_level,
2751 dst_layer, dst_format, true);
2752
2753 /* Even though we do multisample resolves at the time of the blit, OpenGL
2754 * specification defines them as if they happen at the time of rendering,
2755 * which means that the type of averaging we do during the resolve should
2756 * only depend on the source format; the destination format should be
2757 * ignored. But, specification doesn't seem to be strict about it.
2758 *
2759 * It has been observed that mulitisample resolves produce slightly better
2760 * looking images when averaging is done using destination format. NVIDIA's
2761 * proprietary OpenGL driver also follow this approach. So, we choose to
2762 * follow it in our driver.
2763 *
2764 * When multisampling, if the source and destination formats are equal
2765 * (aside from the color space), we choose to blit in sRGB space to get
2766 * this higher quality image.
2767 */
2768 if (params.src.num_samples > 1 &&
2769 _mesa_get_format_color_encoding(dst_mt->format) == GL_SRGB &&
2770 _mesa_get_srgb_format_linear(src_mt->format) ==
2771 _mesa_get_srgb_format_linear(dst_mt->format)) {
2772 assert(brw->format_supported_as_render_target[dst_mt->format]);
2773 params.dst.brw_surfaceformat = brw->render_target_format[dst_mt->format];
2774 params.src.brw_surfaceformat = brw_format_for_mesa_format(dst_mt->format);
2775 }
2776
2777 /* When doing a multisample resolve of a GL_LUMINANCE32F or GL_INTENSITY32F
2778 * texture, the above code configures the source format for L32_FLOAT or
2779 * I32_FLOAT, and the destination format for R32_FLOAT. On Sandy Bridge,
2780 * the SAMPLE message appears to handle multisampled L32_FLOAT and
2781 * I32_FLOAT textures incorrectly, resulting in blocky artifacts. So work
2782 * around the problem by using a source format of R32_FLOAT. This
2783 * shouldn't affect rendering correctness, since the destination format is
2784 * R32_FLOAT, so only the contents of the red channel matters.
2785 */
2786 if (brw->gen == 6 &&
2787 params.src.num_samples > 1 && params.dst.num_samples <= 1 &&
2788 src_mt->format == dst_mt->format &&
2789 params.dst.brw_surfaceformat == BRW_SURFACEFORMAT_R32_FLOAT) {
2790 params.src.brw_surfaceformat = params.dst.brw_surfaceformat;
2791 }
2792
2793 struct brw_blorp_blit_prog_key wm_prog_key;
2794 memset(&wm_prog_key, 0, sizeof(wm_prog_key));
2795
2796 /* texture_data_type indicates the register type that should be used to
2797 * manipulate texture data.
2798 */
2799 switch (_mesa_get_format_datatype(src_mt->format)) {
2800 case GL_UNSIGNED_NORMALIZED:
2801 case GL_SIGNED_NORMALIZED:
2802 case GL_FLOAT:
2803 wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_F;
2804 break;
2805 case GL_UNSIGNED_INT:
2806 if (src_mt->format == MESA_FORMAT_S_UINT8) {
2807 /* We process stencil as though it's an unsigned normalized color */
2808 wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_F;
2809 } else {
2810 wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_UD;
2811 }
2812 break;
2813 case GL_INT:
2814 wm_prog_key.texture_data_type = BRW_REGISTER_TYPE_D;
2815 break;
2816 default:
2817 unreachable("Unrecognized blorp format");
2818 }
2819
2820 if (brw->gen > 6) {
2821 /* Gen7's rendering hardware only supports the IMS layout for depth and
2822 * stencil render targets. Blorp always maps its destination surface as
2823 * a color render target (even if it's actually a depth or stencil
2824 * buffer). So if the destination is IMS, we'll have to map it as a
2825 * single-sampled texture and interleave the samples ourselves.
2826 */
2827 if (dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS)
2828 params.dst.num_samples = 0;
2829 }
2830
2831 if (params.dst.map_stencil_as_y_tiled && params.dst.num_samples > 1) {
2832 /* If the destination surface is a W-tiled multisampled stencil buffer
2833 * that we're mapping as Y tiled, then we need to arrange for the WM
2834 * program to run once per sample rather than once per pixel, because
2835 * the memory layout of related samples doesn't match between W and Y
2836 * tiling.
2837 */
2838 wm_prog_key.persample_msaa_dispatch = true;
2839 }
2840
2841 if (params.src.num_samples > 0 && params.dst.num_samples > 1) {
2842 /* We are blitting from a multisample buffer to a multisample buffer, so
2843 * we must preserve samples within a pixel. This means we have to
2844 * arrange for the WM program to run once per sample rather than once
2845 * per pixel.
2846 */
2847 wm_prog_key.persample_msaa_dispatch = true;
2848 }
2849
2850 /* Scaled blitting or not. */
2851 wm_prog_key.blit_scaled =
2852 ((dst_x1 - dst_x0) == (src_x1 - src_x0) &&
2853 (dst_y1 - dst_y0) == (src_y1 - src_y0)) ? false : true;
2854
2855 /* Scaling factors used for bilinear filtering in multisample scaled
2856 * blits.
2857 */
2858 wm_prog_key.x_scale = 2.0f;
2859 wm_prog_key.y_scale = src_mt->num_samples / 2.0f;
2860
2861 if (filter == GL_LINEAR &&
2862 params.src.num_samples <= 1 && params.dst.num_samples <= 1)
2863 wm_prog_key.bilinear_filter = true;
2864
2865 GLenum base_format = _mesa_get_format_base_format(src_mt->format);
2866 if (base_format != GL_DEPTH_COMPONENT && /* TODO: what about depth/stencil? */
2867 base_format != GL_STENCIL_INDEX &&
2868 !_mesa_is_format_integer(src_mt->format) &&
2869 src_mt->num_samples > 1 && dst_mt->num_samples <= 1) {
2870 /* We are downsampling a non-integer color buffer, so blend.
2871 *
2872 * Regarding integer color buffers, the OpenGL ES 3.2 spec says:
2873 *
2874 * "If the source formats are integer types or stencil values, a
2875 * single sample's value is selected for each pixel."
2876 *
2877 * This implies we should not blend in that case.
2878 */
2879 wm_prog_key.blend = true;
2880 }
2881
2882 /* src_samples and dst_samples are the true sample counts */
2883 wm_prog_key.src_samples = src_mt->num_samples;
2884 wm_prog_key.dst_samples = dst_mt->num_samples;
2885
2886 /* tex_samples and rt_samples are the sample counts that are set up in
2887 * SURFACE_STATE.
2888 */
2889 wm_prog_key.tex_samples = params.src.num_samples;
2890 wm_prog_key.rt_samples = params.dst.num_samples;
2891
2892 /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
2893 * use to access the source and destination surfaces.
2894 */
2895 wm_prog_key.tex_layout =
2896 compute_msaa_layout_for_pipeline(brw, params.src.num_samples,
2897 params.src.msaa_layout);
2898 wm_prog_key.rt_layout =
2899 compute_msaa_layout_for_pipeline(brw, params.dst.num_samples,
2900 params.dst.msaa_layout);
2901
2902 /* src_layout and dst_layout indicate the true MSAA layout used by src and
2903 * dst.
2904 */
2905 wm_prog_key.src_layout = src_mt->msaa_layout;
2906 wm_prog_key.dst_layout = dst_mt->msaa_layout;
2907
2908 /* On gen9+ compressed single sampled buffers carry the same layout type as
2909 * multisampled. The difference is that they can be sampled using normal
2910 * ld message and as render target behave just like non-compressed surface
2911 * from compiler point of view. Therefore override the type in the program
2912 * key.
2913 */
2914 if (brw->gen >= 9 && params.src.num_samples <= 1 &&
2915 src_mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS)
2916 wm_prog_key.src_layout = INTEL_MSAA_LAYOUT_NONE;
2917 if (brw->gen >= 9 && params.dst.num_samples <= 1 &&
2918 dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS)
2919 wm_prog_key.dst_layout = INTEL_MSAA_LAYOUT_NONE;
2920
2921 wm_prog_key.src_tiled_w = params.src.map_stencil_as_y_tiled;
2922 wm_prog_key.dst_tiled_w = params.dst.map_stencil_as_y_tiled;
2923 /* Round floating point values to nearest integer to avoid "off by one texel"
2924 * kind of errors when blitting.
2925 */
2926 params.x0 = params.wm_push_consts.dst_x0 = roundf(dst_x0);
2927 params.y0 = params.wm_push_consts.dst_y0 = roundf(dst_y0);
2928 params.x1 = params.wm_push_consts.dst_x1 = roundf(dst_x1);
2929 params.y1 = params.wm_push_consts.dst_y1 = roundf(dst_y1);
2930 params.wm_push_consts.rect_grid_x1 =
2931 minify(src_mt->logical_width0, src_level) * wm_prog_key.x_scale - 1.0f;
2932 params.wm_push_consts.rect_grid_y1 =
2933 minify(src_mt->logical_height0, src_level) * wm_prog_key.y_scale - 1.0f;
2934
2935 brw_blorp_setup_coord_transform(&params.wm_push_consts.x_transform,
2936 src_x0, src_x1, dst_x0, dst_x1, mirror_x);
2937 brw_blorp_setup_coord_transform(&params.wm_push_consts.y_transform,
2938 src_y0, src_y1, dst_y0, dst_y1, mirror_y);
2939
2940 params.wm_push_consts.src_z =
2941 params.src.mt->target == GL_TEXTURE_3D ? params.src.layer : 0;
2942
2943 if (params.dst.num_samples <= 1 && dst_mt->num_samples > 1) {
2944 /* We must expand the rectangle we send through the rendering pipeline,
2945 * to account for the fact that we are mapping the destination region as
2946 * single-sampled when it is in fact multisampled. We must also align
2947 * it to a multiple of the multisampling pattern, because the
2948 * differences between multisampled and single-sampled surface formats
2949 * will mean that pixels are scrambled within the multisampling pattern.
2950 * TODO: what if this makes the coordinates too large?
2951 *
2952 * Note: this only works if the destination surface uses the IMS layout.
2953 * If it's UMS, then we have no choice but to set up the rendering
2954 * pipeline as multisampled.
2955 */
2956 assert(dst_mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS);
2957 switch (dst_mt->num_samples) {
2958 case 2:
2959 params.x0 = ROUND_DOWN_TO(params.x0 * 2, 4);
2960 params.y0 = ROUND_DOWN_TO(params.y0, 4);
2961 params.x1 = ALIGN(params.x1 * 2, 4);
2962 params.y1 = ALIGN(params.y1, 4);
2963 break;
2964 case 4:
2965 params.x0 = ROUND_DOWN_TO(params.x0 * 2, 4);
2966 params.y0 = ROUND_DOWN_TO(params.y0 * 2, 4);
2967 params.x1 = ALIGN(params.x1 * 2, 4);
2968 params.y1 = ALIGN(params.y1 * 2, 4);
2969 break;
2970 case 8:
2971 params.x0 = ROUND_DOWN_TO(params.x0 * 4, 8);
2972 params.y0 = ROUND_DOWN_TO(params.y0 * 2, 4);
2973 params.x1 = ALIGN(params.x1 * 4, 8);
2974 params.y1 = ALIGN(params.y1 * 2, 4);
2975 break;
2976 default:
2977 unreachable("Unrecognized sample count in brw_blorp_blit_params ctor");
2978 }
2979 wm_prog_key.use_kill = true;
2980 }
2981
2982 if (params.dst.map_stencil_as_y_tiled) {
2983 /* We must modify the rectangle we send through the rendering pipeline
2984 * (and the size and x/y offset of the destination surface), to account
2985 * for the fact that we are mapping it as Y-tiled when it is in fact
2986 * W-tiled.
2987 *
2988 * Both Y tiling and W tiling can be understood as organizations of
2989 * 32-byte sub-tiles; within each 32-byte sub-tile, the layout of pixels
2990 * is different, but the layout of the 32-byte sub-tiles within the 4k
2991 * tile is the same (8 sub-tiles across by 16 sub-tiles down, in
2992 * column-major order). In Y tiling, the sub-tiles are 16 bytes wide
2993 * and 2 rows high; in W tiling, they are 8 bytes wide and 4 rows high.
2994 *
2995 * Therefore, to account for the layout differences within the 32-byte
2996 * sub-tiles, we must expand the rectangle so the X coordinates of its
2997 * edges are multiples of 8 (the W sub-tile width), and its Y
2998 * coordinates of its edges are multiples of 4 (the W sub-tile height).
2999 * Then we need to scale the X and Y coordinates of the rectangle to
3000 * account for the differences in aspect ratio between the Y and W
3001 * sub-tiles. We need to modify the layer width and height similarly.
3002 *
3003 * A correction needs to be applied when MSAA is in use: since
3004 * INTEL_MSAA_LAYOUT_IMS uses an interleaving pattern whose height is 4,
3005 * we need to align the Y coordinates to multiples of 8, so that when
3006 * they are divided by two they are still multiples of 4.
3007 *
3008 * Note: Since the x/y offset of the surface will be applied using the
3009 * SURFACE_STATE command packet, it will be invisible to the swizzling
3010 * code in the shader; therefore it needs to be in a multiple of the
3011 * 32-byte sub-tile size. Fortunately it is, since the sub-tile is 8
3012 * pixels wide and 4 pixels high (when viewed as a W-tiled stencil
3013 * buffer), and the miplevel alignment used for stencil buffers is 8
3014 * pixels horizontally and either 4 or 8 pixels vertically (see
3015 * intel_horizontal_texture_alignment_unit() and
3016 * intel_vertical_texture_alignment_unit()).
3017 *
3018 * Note: Also, since the SURFACE_STATE command packet can only apply
3019 * offsets that are multiples of 4 pixels horizontally and 2 pixels
3020 * vertically, it is important that the offsets will be multiples of
3021 * these sizes after they are converted into Y-tiled coordinates.
3022 * Fortunately they will be, since we know from above that the offsets
3023 * are a multiple of the 32-byte sub-tile size, and in Y-tiled
3024 * coordinates the sub-tile is 16 pixels wide and 2 pixels high.
3025 *
3026 * TODO: what if this makes the coordinates (or the texture size) too
3027 * large?
3028 */
3029 const unsigned x_align = 8, y_align = params.dst.num_samples != 0 ? 8 : 4;
3030 params.x0 = ROUND_DOWN_TO(params.x0, x_align) * 2;
3031 params.y0 = ROUND_DOWN_TO(params.y0, y_align) / 2;
3032 params.x1 = ALIGN(params.x1, x_align) * 2;
3033 params.y1 = ALIGN(params.y1, y_align) / 2;
3034 params.dst.width = ALIGN(params.dst.width, x_align) * 2;
3035 params.dst.height = ALIGN(params.dst.height, y_align) / 2;
3036 params.dst.x_offset *= 2;
3037 params.dst.y_offset /= 2;
3038 wm_prog_key.use_kill = true;
3039 }
3040
3041 if (params.src.map_stencil_as_y_tiled) {
3042 /* We must modify the size and x/y offset of the source surface to
3043 * account for the fact that we are mapping it as Y-tiled when it is in
3044 * fact W tiled.
3045 *
3046 * See the comments above concerning x/y offset alignment for the
3047 * destination surface.
3048 *
3049 * TODO: what if this makes the texture size too large?
3050 */
3051 const unsigned x_align = 8, y_align = params.src.num_samples != 0 ? 8 : 4;
3052 params.src.width = ALIGN(params.src.width, x_align) * 2;
3053 params.src.height = ALIGN(params.src.height, y_align) / 2;
3054 params.src.x_offset *= 2;
3055 params.src.y_offset /= 2;
3056 }
3057
3058 brw_blorp_get_blit_kernel(brw, &params, &wm_prog_key);
3059
3060 params.src.swizzle = src_swizzle;
3061
3062 brw_blorp_exec(brw, &params);
3063
3064 intel_miptree_slice_set_needs_hiz_resolve(dst_mt, dst_level, dst_layer);
3065
3066 if (intel_miptree_is_lossless_compressed(brw, dst_mt))
3067 dst_mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_UNRESOLVED;
3068 }