2 * Copyright © 2012 Intel Corporation
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:
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
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
24 #include "main/context.h"
25 #include "main/teximage.h"
26 #include "main/fbobject.h"
28 #include "intel_fbo.h"
30 #include "brw_blorp.h"
31 #include "brw_context.h"
32 #include "brw_blorp_blit_eu.h"
33 #include "brw_state.h"
34 #include "brw_meta_util.h"
36 #define FILE_DEBUG_FLAG DEBUG_BLORP
38 static struct intel_mipmap_tree
*
39 find_miptree(GLbitfield buffer_bit
, struct intel_renderbuffer
*irb
)
41 struct intel_mipmap_tree
*mt
= irb
->mt
;
42 if (buffer_bit
== GL_STENCIL_BUFFER_BIT
&& mt
->stencil_mt
)
49 * Note: if the src (or dst) is a 2D multisample array texture on Gen7+ using
50 * INTEL_MSAA_LAYOUT_UMS or INTEL_MSAA_LAYOUT_CMS, src_layer (dst_layer) is
51 * the physical layer holding sample 0. So, for example, if
52 * src_mt->num_samples == 4, then logical layer n corresponds to src_layer ==
56 brw_blorp_blit_miptrees(struct brw_context
*brw
,
57 struct intel_mipmap_tree
*src_mt
,
58 unsigned src_level
, unsigned src_layer
,
59 mesa_format src_format
, int src_swizzle
,
60 struct intel_mipmap_tree
*dst_mt
,
61 unsigned dst_level
, unsigned dst_layer
,
62 mesa_format dst_format
,
63 float src_x0
, float src_y0
,
64 float src_x1
, float src_y1
,
65 float dst_x0
, float dst_y0
,
66 float dst_x1
, float dst_y1
,
67 GLenum filter
, bool mirror_x
, bool mirror_y
,
68 bool decode_srgb
, bool encode_srgb
)
70 /* Get ready to blit. This includes depth resolving the src and dst
71 * buffers if necessary. Note: it's not necessary to do a color resolve on
72 * the destination buffer because we use the standard render path to render
73 * to destination color buffers, and the standard render path is
75 * Lossless compression is only introduced for gen9 onwards whereas
76 * blorp is not supported even for gen8. Therefore it should be impossible
77 * to end up here with single sampled compressed surfaces.
79 assert(!intel_miptree_is_lossless_compressed(brw
, src_mt
));
80 assert(!intel_miptree_is_lossless_compressed(brw
, dst_mt
));
81 intel_miptree_resolve_color(brw
, src_mt
, 0);
82 intel_miptree_slice_resolve_depth(brw
, src_mt
, src_level
, src_layer
);
83 intel_miptree_slice_resolve_depth(brw
, dst_mt
, dst_level
, dst_layer
);
85 DBG("%s from %dx %s mt %p %d %d (%f,%f) (%f,%f)"
86 "to %dx %s mt %p %d %d (%f,%f) (%f,%f) (flip %d,%d)\n",
88 src_mt
->num_samples
, _mesa_get_format_name(src_mt
->format
), src_mt
,
89 src_level
, src_layer
, src_x0
, src_y0
, src_x1
, src_y1
,
90 dst_mt
->num_samples
, _mesa_get_format_name(dst_mt
->format
), dst_mt
,
91 dst_level
, dst_layer
, dst_x0
, dst_y0
, dst_x1
, dst_y1
,
94 if (!decode_srgb
&& _mesa_get_format_color_encoding(src_format
) == GL_SRGB
)
95 src_format
= _mesa_get_srgb_format_linear(src_format
);
97 if (!encode_srgb
&& _mesa_get_format_color_encoding(dst_format
) == GL_SRGB
)
98 dst_format
= _mesa_get_srgb_format_linear(dst_format
);
100 brw_blorp_blit_params
params(brw
,
101 src_mt
, src_level
, src_layer
, src_format
,
102 dst_mt
, dst_level
, dst_layer
, dst_format
,
107 filter
, mirror_x
, mirror_y
);
108 params
.src
.swizzle
= src_swizzle
;
110 brw_blorp_exec(brw
, ¶ms
);
112 intel_miptree_slice_set_needs_hiz_resolve(dst_mt
, dst_level
, dst_layer
);
116 blorp_get_texture_swizzle(const struct intel_renderbuffer
*irb
)
118 return irb
->Base
.Base
._BaseFormat
== GL_RGB
?
119 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ONE
) :
124 do_blorp_blit(struct brw_context
*brw
, GLbitfield buffer_bit
,
125 struct intel_renderbuffer
*src_irb
, mesa_format src_format
,
126 struct intel_renderbuffer
*dst_irb
, mesa_format dst_format
,
127 GLfloat srcX0
, GLfloat srcY0
, GLfloat srcX1
, GLfloat srcY1
,
128 GLfloat dstX0
, GLfloat dstY0
, GLfloat dstX1
, GLfloat dstY1
,
129 GLenum filter
, bool mirror_x
, bool mirror_y
)
131 /* Find source/dst miptrees */
132 struct intel_mipmap_tree
*src_mt
= find_miptree(buffer_bit
, src_irb
);
133 struct intel_mipmap_tree
*dst_mt
= find_miptree(buffer_bit
, dst_irb
);
135 const bool es3
= _mesa_is_gles3(&brw
->ctx
);
137 brw_blorp_blit_miptrees(brw
,
138 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
139 src_format
, blorp_get_texture_swizzle(src_irb
),
140 dst_mt
, dst_irb
->mt_level
, dst_irb
->mt_layer
,
142 srcX0
, srcY0
, srcX1
, srcY1
,
143 dstX0
, dstY0
, dstX1
, dstY1
,
144 filter
, mirror_x
, mirror_y
,
147 dst_irb
->need_downsample
= true;
151 try_blorp_blit(struct brw_context
*brw
,
152 const struct gl_framebuffer
*read_fb
,
153 const struct gl_framebuffer
*draw_fb
,
154 GLfloat srcX0
, GLfloat srcY0
, GLfloat srcX1
, GLfloat srcY1
,
155 GLfloat dstX0
, GLfloat dstY0
, GLfloat dstX1
, GLfloat dstY1
,
156 GLenum filter
, GLbitfield buffer_bit
)
158 struct gl_context
*ctx
= &brw
->ctx
;
160 /* Sync up the state of window system buffers. We need to do this before
161 * we go looking for the buffers.
163 intel_prepare_render(brw
);
165 bool mirror_x
, mirror_y
;
166 if (brw_meta_mirror_clip_and_scissor(ctx
, read_fb
, draw_fb
,
167 &srcX0
, &srcY0
, &srcX1
, &srcY1
,
168 &dstX0
, &dstY0
, &dstX1
, &dstY1
,
169 &mirror_x
, &mirror_y
))
173 struct intel_renderbuffer
*src_irb
;
174 struct intel_renderbuffer
*dst_irb
;
175 struct intel_mipmap_tree
*src_mt
;
176 struct intel_mipmap_tree
*dst_mt
;
177 switch (buffer_bit
) {
178 case GL_COLOR_BUFFER_BIT
:
179 src_irb
= intel_renderbuffer(read_fb
->_ColorReadBuffer
);
180 for (unsigned i
= 0; i
< draw_fb
->_NumColorDrawBuffers
; ++i
) {
181 dst_irb
= intel_renderbuffer(draw_fb
->_ColorDrawBuffers
[i
]);
183 do_blorp_blit(brw
, buffer_bit
,
184 src_irb
, src_irb
->Base
.Base
.Format
,
185 dst_irb
, dst_irb
->Base
.Base
.Format
,
186 srcX0
, srcY0
, srcX1
, srcY1
,
187 dstX0
, dstY0
, dstX1
, dstY1
,
188 filter
, mirror_x
, mirror_y
);
191 case GL_DEPTH_BUFFER_BIT
:
193 intel_renderbuffer(read_fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
195 intel_renderbuffer(draw_fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
196 src_mt
= find_miptree(buffer_bit
, src_irb
);
197 dst_mt
= find_miptree(buffer_bit
, dst_irb
);
199 /* We can't handle format conversions between Z24 and other formats
200 * since we have to lie about the surface format. See the comments in
201 * brw_blorp_surface_info::set().
203 if ((src_mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
) !=
204 (dst_mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
))
207 do_blorp_blit(brw
, buffer_bit
, src_irb
, MESA_FORMAT_NONE
,
208 dst_irb
, MESA_FORMAT_NONE
, srcX0
, srcY0
,
209 srcX1
, srcY1
, dstX0
, dstY0
, dstX1
, dstY1
,
210 filter
, mirror_x
, mirror_y
);
212 case GL_STENCIL_BUFFER_BIT
:
214 intel_renderbuffer(read_fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
216 intel_renderbuffer(draw_fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
217 do_blorp_blit(brw
, buffer_bit
, src_irb
, MESA_FORMAT_NONE
,
218 dst_irb
, MESA_FORMAT_NONE
, srcX0
, srcY0
,
219 srcX1
, srcY1
, dstX0
, dstY0
, dstX1
, dstY1
,
220 filter
, mirror_x
, mirror_y
);
223 unreachable("not reached");
230 brw_blorp_copytexsubimage(struct brw_context
*brw
,
231 struct gl_renderbuffer
*src_rb
,
232 struct gl_texture_image
*dst_image
,
234 int srcX0
, int srcY0
,
235 int dstX0
, int dstY0
,
236 int width
, int height
)
238 struct gl_context
*ctx
= &brw
->ctx
;
239 struct intel_renderbuffer
*src_irb
= intel_renderbuffer(src_rb
);
240 struct intel_texture_image
*intel_image
= intel_texture_image(dst_image
);
242 /* No pixel transfer operations (zoom, bias, mapping), just a blit */
243 if (brw
->ctx
._ImageTransferState
)
246 /* Sync up the state of window system buffers. We need to do this before
247 * we go looking at the src renderbuffer's miptree.
249 intel_prepare_render(brw
);
251 struct intel_mipmap_tree
*src_mt
= src_irb
->mt
;
252 struct intel_mipmap_tree
*dst_mt
= intel_image
->mt
;
254 /* There is support for only up to eight samples. */
255 if (src_mt
->num_samples
> 8 || dst_mt
->num_samples
> 8)
258 /* BLORP is only supported from Gen6 onwards. */
262 if (_mesa_get_format_base_format(src_rb
->Format
) !=
263 _mesa_get_format_base_format(dst_image
->TexFormat
)) {
267 /* We can't handle format conversions between Z24 and other formats since
268 * we have to lie about the surface format. See the comments in
269 * brw_blorp_surface_info::set().
271 if ((src_mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
) !=
272 (dst_mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
)) {
276 if (!brw
->format_supported_as_render_target
[dst_image
->TexFormat
])
279 /* Source clipping shouldn't be necessary, since copytexsubimage (in
280 * src/mesa/main/teximage.c) calls _mesa_clip_copytexsubimage() which
283 * Destination clipping shouldn't be necessary since the restrictions on
284 * glCopyTexSubImage prevent the user from specifying a destination rectangle
285 * that falls outside the bounds of the destination texture.
286 * See error_check_subtexture_dimensions().
289 int srcY1
= srcY0
+ height
;
290 int srcX1
= srcX0
+ width
;
291 int dstX1
= dstX0
+ width
;
292 int dstY1
= dstY0
+ height
;
294 /* Account for the fact that in the system framebuffer, the origin is at
297 bool mirror_y
= false;
298 if (_mesa_is_winsys_fbo(ctx
->ReadBuffer
)) {
299 GLint tmp
= src_rb
->Height
- srcY0
;
300 srcY0
= src_rb
->Height
- srcY1
;
305 /* Account for face selection and texture view MinLayer */
306 int dst_slice
= slice
+ dst_image
->TexObject
->MinLayer
+ dst_image
->Face
;
307 int dst_level
= dst_image
->Level
+ dst_image
->TexObject
->MinLevel
;
309 brw_blorp_blit_miptrees(brw
,
310 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
311 src_rb
->Format
, blorp_get_texture_swizzle(src_irb
),
312 dst_mt
, dst_level
, dst_slice
,
313 dst_image
->TexFormat
,
314 srcX0
, srcY0
, srcX1
, srcY1
,
315 dstX0
, dstY0
, dstX1
, dstY1
,
316 GL_NEAREST
, false, mirror_y
,
319 /* If we're copying to a packed depth stencil texture and the source
320 * framebuffer has separate stencil, we need to also copy the stencil data
323 src_rb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
324 if (_mesa_get_format_bits(dst_image
->TexFormat
, GL_STENCIL_BITS
) > 0 &&
326 src_irb
= intel_renderbuffer(src_rb
);
327 src_mt
= src_irb
->mt
;
329 if (src_mt
->stencil_mt
)
330 src_mt
= src_mt
->stencil_mt
;
331 if (dst_mt
->stencil_mt
)
332 dst_mt
= dst_mt
->stencil_mt
;
334 if (src_mt
!= dst_mt
) {
335 brw_blorp_blit_miptrees(brw
,
336 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
338 blorp_get_texture_swizzle(src_irb
),
339 dst_mt
, dst_level
, dst_slice
,
341 srcX0
, srcY0
, srcX1
, srcY1
,
342 dstX0
, dstY0
, dstX1
, dstY1
,
343 GL_NEAREST
, false, mirror_y
,
353 brw_blorp_framebuffer(struct brw_context
*brw
,
354 struct gl_framebuffer
*readFb
,
355 struct gl_framebuffer
*drawFb
,
356 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
357 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
358 GLbitfield mask
, GLenum filter
)
360 /* BLORP is not supported before Gen6. */
364 /* There is support for only up to eight samples. */
365 if (readFb
->Visual
.samples
> 8 || drawFb
->Visual
.samples
> 8)
368 static GLbitfield buffer_bits
[] = {
371 GL_STENCIL_BUFFER_BIT
,
374 for (unsigned int i
= 0; i
< ARRAY_SIZE(buffer_bits
); ++i
) {
375 if ((mask
& buffer_bits
[i
]) &&
376 try_blorp_blit(brw
, readFb
, drawFb
,
377 srcX0
, srcY0
, srcX1
, srcY1
,
378 dstX0
, dstY0
, dstX1
, dstY1
,
379 filter
, buffer_bits
[i
])) {
380 mask
&= ~buffer_bits
[i
];
389 * Enum to specify the order of arguments in a sampler message
391 enum sampler_message_arg
393 SAMPLER_MESSAGE_ARG_U_FLOAT
,
394 SAMPLER_MESSAGE_ARG_V_FLOAT
,
395 SAMPLER_MESSAGE_ARG_U_INT
,
396 SAMPLER_MESSAGE_ARG_V_INT
,
397 SAMPLER_MESSAGE_ARG_R_INT
,
398 SAMPLER_MESSAGE_ARG_SI_INT
,
399 SAMPLER_MESSAGE_ARG_MCS_INT
,
400 SAMPLER_MESSAGE_ARG_ZERO_INT
,
404 * Generator for WM programs used in BLORP blits.
406 * The bulk of the work done by the WM program is to wrap and unwrap the
407 * coordinate transformations used by the hardware to store surfaces in
408 * memory. The hardware transforms a pixel location (X, Y, S) (where S is the
409 * sample index for a multisampled surface) to a memory offset by the
410 * following formulas:
412 * offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
413 * (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
415 * For a single-sampled surface, or for a multisampled surface using
416 * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
419 * encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
420 * decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
421 * encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
422 * decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
424 * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
425 * embeds the sample number into bit 1 of the X and Y coordinates:
427 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
428 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
429 * Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
430 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
431 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
432 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
433 * S = (Y & 0b10) | (X & 0b10) >> 1
435 * For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
436 * embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
439 * encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
440 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
441 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
442 * decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
443 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
444 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
445 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
447 * For X tiling, tile() combines together the low-order bits of the X and Y
448 * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
449 * bytes wide and 8 rows high:
451 * tile(x_tiled, X, Y, S) = A
452 * where A = tile_num << 12 | offset
453 * tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
454 * offset = (Y' & 0b111) << 9
455 * | (X & 0b111111111)
457 * Y' = Y + S * qpitch
458 * detile(x_tiled, A) = (X, Y, S)
462 * Y' = (tile_num / tile_pitch) << 3
463 * | (A & 0b111000000000) >> 9
464 * X' = (tile_num % tile_pitch) << 9
465 * | (A & 0b111111111)
467 * (In all tiling formulas, cpp is the number of bytes occupied by a single
468 * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
469 * to fill the width of the surface, and qpitch is the spacing (in rows)
470 * between array slices).
472 * For Y tiling, tile() combines together the low-order bits of the X and Y
473 * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
474 * bytes wide and 32 rows high:
476 * tile(y_tiled, X, Y, S) = A
477 * where A = tile_num << 12 | offset
478 * tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
479 * offset = (X' & 0b1110000) << 5
480 * | (Y' & 0b11111) << 4
483 * Y' = Y + S * qpitch
484 * detile(y_tiled, A) = (X, Y, S)
488 * Y' = (tile_num / tile_pitch) << 5
489 * | (A & 0b111110000) >> 4
490 * X' = (tile_num % tile_pitch) << 7
491 * | (A & 0b111000000000) >> 5
494 * For W tiling, tile() combines together the low-order bits of the X and Y
495 * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
496 * bytes wide and 64 rows high (note that W tiling is only used for stencil
497 * buffers, which always have cpp = 1 and S=0):
499 * tile(w_tiled, X, Y, S) = A
500 * where A = tile_num << 12 | offset
501 * tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
502 * offset = (X' & 0b111000) << 6
503 * | (Y' & 0b111100) << 3
504 * | (X' & 0b100) << 2
510 * Y' = Y + S * qpitch
511 * detile(w_tiled, A) = (X, Y, S)
512 * where X = X' / cpp = X'
513 * Y = Y' % qpitch = Y'
515 * Y' = (tile_num / tile_pitch) << 6
516 * | (A & 0b111100000) >> 3
517 * | (A & 0b1000) >> 2
519 * X' = (tile_num % tile_pitch) << 6
520 * | (A & 0b111000000000) >> 6
521 * | (A & 0b10000) >> 2
525 * Finally, for a non-tiled surface, tile() simply combines together the X and
526 * Y coordinates in the natural way:
528 * tile(untiled, X, Y, S) = A
529 * where A = Y * pitch + X'
531 * Y' = Y + S * qpitch
532 * detile(untiled, A) = (X, Y, S)
539 * (In these formulas, pitch is the number of bytes occupied by a single row
542 class brw_blorp_blit_program
: public brw_blorp_eu_emitter
545 brw_blorp_blit_program(struct brw_context
*brw
,
546 const brw_blorp_blit_prog_key
*key
);
548 const GLuint
*compile(struct brw_context
*brw
, bool debug_flag
,
549 GLuint
*program_size
);
551 brw_blorp_prog_data prog_data
;
555 void alloc_push_const_regs(int base_reg
);
556 void compute_frag_coords();
557 void translate_tiling(bool old_tiled_w
, bool new_tiled_w
);
558 void encode_msaa(unsigned num_samples
, intel_msaa_layout layout
);
559 void decode_msaa(unsigned num_samples
, intel_msaa_layout layout
);
560 void translate_dst_to_src();
561 void clamp_tex_coords(struct brw_reg regX
, struct brw_reg regY
,
562 struct brw_reg clampX0
, struct brw_reg clampY0
,
563 struct brw_reg clampX1
, struct brw_reg clampY1
);
564 void single_to_blend();
565 void manual_blend_average(unsigned num_samples
);
566 void manual_blend_bilinear(unsigned num_samples
);
567 void sample(struct brw_reg dst
);
568 void texel_fetch(struct brw_reg dst
);
570 void texture_lookup(struct brw_reg dst
, enum opcode op
,
571 const sampler_message_arg
*args
, int num_args
);
572 void render_target_write();
575 * Base-2 logarithm of the maximum number of samples that can be blended.
577 static const unsigned LOG2_MAX_BLEND_SAMPLES
= 3;
579 struct brw_context
*brw
;
580 const brw_blorp_blit_prog_key
*key
;
582 /* Thread dispatch header */
585 /* Pixel X/Y coordinates (always in R1). */
589 struct brw_reg dst_x0
;
590 struct brw_reg dst_x1
;
591 struct brw_reg dst_y0
;
592 struct brw_reg dst_y1
;
593 /* Top right coordinates of the rectangular grid used for scaled blitting */
594 struct brw_reg rect_grid_x1
;
595 struct brw_reg rect_grid_y1
;
597 struct brw_reg multiplier
;
598 struct brw_reg offset
;
599 } x_transform
, y_transform
;
600 struct brw_reg src_z
;
602 /* Data read from texture (4 vec16's per array element) */
603 struct brw_reg texture_data
[LOG2_MAX_BLEND_SAMPLES
+ 1];
605 /* Auxiliary storage for the contents of the MCS surface.
607 * Since the sampler always returns 8 registers worth of data, this is 8
608 * registers wide, even though we only use the first 2 registers of it.
610 struct brw_reg mcs_data
;
612 /* X coordinates. We have two of them so that we can perform coordinate
613 * transformations easily.
615 struct brw_reg x_coords
[2];
617 /* Y coordinates. We have two of them so that we can perform coordinate
618 * transformations easily.
620 struct brw_reg y_coords
[2];
622 /* X, Y coordinates of the pixel from which we need to fetch the specific
623 * sample. These are used for multisample scaled blitting.
625 struct brw_reg x_sample_coords
;
626 struct brw_reg y_sample_coords
;
628 /* Fractional parts of the x and y coordinates, used as bilinear interpolation coefficients */
629 struct brw_reg x_frac
;
630 struct brw_reg y_frac
;
632 /* Which element of x_coords and y_coords is currently in use.
636 /* True if, at the point in the program currently being compiled, the
637 * sample index is known to be zero.
641 /* Register storing the sample index when s_is_zero is false. */
642 struct brw_reg sample_index
;
648 /* MRF used for sampling and render target writes */
652 brw_blorp_blit_program::brw_blorp_blit_program(
653 struct brw_context
*brw
, const brw_blorp_blit_prog_key
*key
)
654 : brw_blorp_eu_emitter(), brw(brw
), key(key
)
659 brw_blorp_blit_program::compile(struct brw_context
*brw
, bool debug_flag
,
660 GLuint
*program_size
)
663 if (key
->dst_tiled_w
&& key
->rt_samples
> 0) {
664 /* If the destination image is W tiled and multisampled, then the thread
665 * must be dispatched once per sample, not once per pixel. This is
666 * necessary because after conversion between W and Y tiling, there's no
667 * guarantee that all samples corresponding to a single pixel will still
670 assert(key
->persample_msaa_dispatch
);
674 /* We are blending, which means we won't have an opportunity to
675 * translate the tiling and sample count for the texture surface. So
676 * the surface state for the texture must be configured with the correct
677 * tiling and sample count.
679 assert(!key
->src_tiled_w
);
680 assert(key
->tex_samples
== key
->src_samples
);
681 assert(key
->tex_layout
== key
->src_layout
);
682 assert(key
->tex_samples
> 0);
685 if (key
->persample_msaa_dispatch
) {
686 /* It only makes sense to do persample dispatch if the render target is
687 * configured as multisampled.
689 assert(key
->rt_samples
> 0);
692 /* Make sure layout is consistent with sample count */
693 assert((key
->tex_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
694 (key
->tex_samples
== 0));
695 assert((key
->rt_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
696 (key
->rt_samples
== 0));
697 assert((key
->src_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
698 (key
->src_samples
== 0));
699 assert((key
->dst_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
700 (key
->dst_samples
== 0));
702 /* Set up prog_data */
703 memset(&prog_data
, 0, sizeof(prog_data
));
704 prog_data
.persample_msaa_dispatch
= key
->persample_msaa_dispatch
;
707 compute_frag_coords();
709 /* Render target and texture hardware don't support W tiling until Gen8. */
710 const bool rt_tiled_w
= false;
711 const bool tex_tiled_w
= brw
->gen
>= 8 && key
->src_tiled_w
;
713 /* The address that data will be written to is determined by the
714 * coordinates supplied to the WM thread and the tiling and sample count of
715 * the render target, according to the formula:
717 * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
719 * If the actual tiling and sample count of the destination surface are not
720 * the same as the configuration of the render target, then these
721 * coordinates are wrong and we have to adjust them to compensate for the
724 if (rt_tiled_w
!= key
->dst_tiled_w
||
725 key
->rt_samples
!= key
->dst_samples
||
726 key
->rt_layout
!= key
->dst_layout
) {
727 encode_msaa(key
->rt_samples
, key
->rt_layout
);
728 /* Now (X, Y, S) = detile(rt_tiling, offset) */
729 translate_tiling(rt_tiled_w
, key
->dst_tiled_w
);
730 /* Now (X, Y, S) = detile(dst_tiling, offset) */
731 decode_msaa(key
->dst_samples
, key
->dst_layout
);
734 /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
736 * That is: X, Y and S now contain the true coordinates and sample index of
737 * the data that the WM thread should output.
739 * If we need to kill pixels that are outside the destination rectangle,
740 * now is the time to do it.
744 emit_kill_if_outside_rect(x_coords
[xy_coord_index
],
745 y_coords
[xy_coord_index
],
746 dst_x0
, dst_x1
, dst_y0
, dst_y1
);
748 /* Next, apply a translation to obtain coordinates in the source image. */
749 translate_dst_to_src();
751 /* If the source image is not multisampled, then we want to fetch sample
752 * number 0, because that's the only sample there is.
754 if (key
->src_samples
== 0)
757 /* X, Y, and S are now the coordinates of the pixel in the source image
758 * that we want to texture from. Exception: if we are blending, then S is
759 * irrelevant, because we are going to fetch all samples.
761 if (key
->blend
&& !key
->blit_scaled
) {
763 /* Gen6 hardware an automatically blend using the SAMPLE message */
765 sample(texture_data
[0]);
767 /* Gen7+ hardware doesn't automaticaly blend. */
768 manual_blend_average(key
->src_samples
);
770 } else if(key
->blend
&& key
->blit_scaled
) {
771 manual_blend_bilinear(key
->src_samples
);
773 /* We aren't blending, which means we just want to fetch a single sample
774 * from the source surface. The address that we want to fetch from is
775 * related to the X, Y and S values according to the formula:
777 * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
779 * If the actual tiling and sample count of the source surface are not
780 * the same as the configuration of the texture, then we need to adjust
781 * the coordinates to compensate for the difference.
783 if ((tex_tiled_w
!= key
->src_tiled_w
||
784 key
->tex_samples
!= key
->src_samples
||
785 key
->tex_layout
!= key
->src_layout
) &&
786 !key
->bilinear_filter
) {
787 encode_msaa(key
->src_samples
, key
->src_layout
);
788 /* Now (X, Y, S) = detile(src_tiling, offset) */
789 translate_tiling(key
->src_tiled_w
, tex_tiled_w
);
790 /* Now (X, Y, S) = detile(tex_tiling, offset) */
791 decode_msaa(key
->tex_samples
, key
->tex_layout
);
794 if (key
->bilinear_filter
) {
795 sample(texture_data
[0]);
798 /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
800 * In other words: X, Y, and S now contain values which, when passed to
801 * the texturing unit, will cause data to be read from the correct
802 * memory location. So we can fetch the texel now.
804 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
806 texel_fetch(texture_data
[0]);
810 /* Finally, write the fetched (or blended) value to the render target and
811 * terminate the thread.
813 render_target_write();
815 return get_program(brw
, debug_flag
, program_size
);
819 brw_blorp_blit_program::alloc_push_const_regs(int base_reg
)
821 #define CONST_LOC(name) offsetof(brw_blorp_wm_push_constants, name)
822 #define ALLOC_REG(name, type) \
824 retype(brw_vec1_reg(BRW_GENERAL_REGISTER_FILE, \
825 base_reg + CONST_LOC(name) / 32, \
826 (CONST_LOC(name) % 32) / 4), type)
828 ALLOC_REG(dst_x0
, BRW_REGISTER_TYPE_UD
);
829 ALLOC_REG(dst_x1
, BRW_REGISTER_TYPE_UD
);
830 ALLOC_REG(dst_y0
, BRW_REGISTER_TYPE_UD
);
831 ALLOC_REG(dst_y1
, BRW_REGISTER_TYPE_UD
);
832 ALLOC_REG(rect_grid_x1
, BRW_REGISTER_TYPE_F
);
833 ALLOC_REG(rect_grid_y1
, BRW_REGISTER_TYPE_F
);
834 ALLOC_REG(x_transform
.multiplier
, BRW_REGISTER_TYPE_F
);
835 ALLOC_REG(x_transform
.offset
, BRW_REGISTER_TYPE_F
);
836 ALLOC_REG(y_transform
.multiplier
, BRW_REGISTER_TYPE_F
);
837 ALLOC_REG(y_transform
.offset
, BRW_REGISTER_TYPE_F
);
838 ALLOC_REG(src_z
, BRW_REGISTER_TYPE_UD
);
844 brw_blorp_blit_program::alloc_regs()
847 this->R0
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
848 this->R1
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
849 prog_data
.first_curbe_grf
= reg
;
850 alloc_push_const_regs(reg
);
851 reg
+= BRW_BLORP_NUM_PUSH_CONST_REGS
;
852 for (unsigned i
= 0; i
< ARRAY_SIZE(texture_data
); ++i
) {
853 this->texture_data
[i
] =
854 retype(vec16(brw_vec8_grf(reg
, 0)), key
->texture_data_type
);
858 retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
); reg
+= 8;
860 for (int i
= 0; i
< 2; ++i
) {
862 = retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
865 = retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
869 if (key
->blit_scaled
&& key
->blend
) {
870 this->x_sample_coords
= brw_vec8_grf(reg
, 0);
872 this->y_sample_coords
= brw_vec8_grf(reg
, 0);
874 this->x_frac
= brw_vec8_grf(reg
, 0);
876 this->y_frac
= brw_vec8_grf(reg
, 0);
880 this->xy_coord_index
= 0;
882 = retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
884 this->t1
= retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
886 this->t2
= retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
889 /* Make sure we didn't run out of registers */
890 assert(reg
<= GEN7_MRF_HACK_START
);
893 this->base_mrf
= mrf
;
896 /* In the code that follows, X and Y can be used to quickly refer to the
897 * active elements of x_coords and y_coords, and Xp and Yp ("X prime" and "Y
898 * prime") to the inactive elements.
900 * S can be used to quickly refer to sample_index.
902 #define X x_coords[xy_coord_index]
903 #define Y y_coords[xy_coord_index]
904 #define Xp x_coords[!xy_coord_index]
905 #define Yp y_coords[!xy_coord_index]
906 #define S sample_index
908 /* Quickly swap the roles of (X, Y) and (Xp, Yp). Saves us from having to do
909 * MOVs to transfor (Xp, Yp) to (X, Y) after a coordinate transformation.
911 #define SWAP_XY_AND_XPYP() xy_coord_index = !xy_coord_index;
914 * Emit code to compute the X and Y coordinates of the pixels being rendered
915 * by this WM invocation.
917 * Assuming the render target is set up for Y tiling, these (X, Y) values are
918 * related to the address offset where outputs will be written by the formula:
920 * (X, Y, S) = decode_msaa(detile(offset)).
922 * (See brw_blorp_blit_program).
925 brw_blorp_blit_program::compute_frag_coords()
927 /* R1.2[15:0] = X coordinate of upper left pixel of subspan 0 (pixel 0)
928 * R1.3[15:0] = X coordinate of upper left pixel of subspan 1 (pixel 4)
929 * R1.4[15:0] = X coordinate of upper left pixel of subspan 2 (pixel 8)
930 * R1.5[15:0] = X coordinate of upper left pixel of subspan 3 (pixel 12)
932 * Pixels within a subspan are laid out in this arrangement:
936 * So, to compute the coordinates of each pixel, we need to read every 2nd
937 * 16-bit value (vstride=2) from R1, starting at the 4th 16-bit value
938 * (suboffset=4), and duplicate each value 4 times (hstride=0, width=4).
939 * In other words, the data we want to access is R1.4<2;4,0>UW.
941 * Then, we need to add the repeating sequence (0, 1, 0, 1, ...) to the
942 * result, since pixels n+1 and n+3 are in the right half of the subspan.
944 emit_add(vec16(retype(X
, BRW_REGISTER_TYPE_UW
)),
945 stride(suboffset(R1
, 4), 2, 4, 0), brw_imm_v(0x10101010));
947 /* Similarly, Y coordinates for subspans come from R1.2[31:16] through
948 * R1.5[31:16], so to get pixel Y coordinates we need to start at the 5th
949 * 16-bit value instead of the 4th (R1.5<2;4,0>UW instead of
952 * And we need to add the repeating sequence (0, 0, 1, 1, ...), since
953 * pixels n+2 and n+3 are in the bottom half of the subspan.
955 emit_add(vec16(retype(Y
, BRW_REGISTER_TYPE_UW
)),
956 stride(suboffset(R1
, 5), 2, 4, 0), brw_imm_v(0x11001100));
958 /* Move the coordinates to UD registers. */
959 emit_mov(vec16(Xp
), retype(X
, BRW_REGISTER_TYPE_UW
));
960 emit_mov(vec16(Yp
), retype(Y
, BRW_REGISTER_TYPE_UW
));
963 if (key
->persample_msaa_dispatch
) {
964 switch (key
->rt_samples
) {
967 /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 4.
968 * Therefore, subspan 0 will represent sample 0, subspan 1 will
969 * represent sample 1, and so on.
971 * So we need to populate S with the sequence (0, 0, 0, 0, 1, 1, 1,
972 * 1, 2, 2, 2, 2, 3, 3, 3, 3). The easiest way to do this is to
973 * populate a temporary variable with the sequence (0, 1, 2, 3), and
974 * then copy from it using vstride=1, width=4, hstride=0.
976 struct brw_reg t1_uw1
= retype(t1
, BRW_REGISTER_TYPE_UW
);
977 emit_mov(vec16(t1_uw1
), key
->rt_samples
== 4 ?
978 brw_imm_v(0x3210) : brw_imm_v(0x1010));
979 /* Move to UD sample_index register. */
980 emit_mov_8(S
, stride(t1_uw1
, 1, 4, 0));
981 emit_mov_8(offset(S
, 1), suboffset(stride(t1_uw1
, 1, 4, 0), 2));
985 /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 8.
986 * Therefore, subspan 0 will represent sample N (where N is 0 or 4),
987 * subspan 1 will represent sample 1, and so on. We can find the
988 * value of N by looking at R0.0 bits 7:6 ("Starting Sample Pair
989 * Index") and multiplying by two (since samples are always delivered
990 * in pairs). That is, we compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 &
993 * Then we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1, 2,
994 * 2, 2, 2, 3, 3, 3, 3), which we compute by populating a temporary
995 * variable with the sequence (0, 1, 2, 3), and then reading from it
996 * using vstride=1, width=4, hstride=0.
998 struct brw_reg t1_ud1
= vec1(retype(t1
, BRW_REGISTER_TYPE_UD
));
999 struct brw_reg t2_uw1
= retype(t2
, BRW_REGISTER_TYPE_UW
);
1000 struct brw_reg r0_ud1
= vec1(retype(R0
, BRW_REGISTER_TYPE_UD
));
1001 emit_and(t1_ud1
, r0_ud1
, brw_imm_ud(0xc0));
1002 emit_shr(t1_ud1
, t1_ud1
, brw_imm_ud(5));
1003 emit_mov(vec16(t2_uw1
), brw_imm_v(0x3210));
1004 emit_add(vec16(S
), retype(t1_ud1
, BRW_REGISTER_TYPE_UW
),
1005 stride(t2_uw1
, 1, 4, 0));
1006 emit_add_8(offset(S
, 1),
1007 retype(t1_ud1
, BRW_REGISTER_TYPE_UW
),
1008 suboffset(stride(t2_uw1
, 1, 4, 0), 2));
1012 unreachable("Unrecognized sample count in "
1013 "brw_blorp_blit_program::compute_frag_coords()");
1017 /* Either the destination surface is single-sampled, or the WM will be
1018 * run in MSDISPMODE_PERPIXEL (which causes a single fragment dispatch
1019 * per pixel). In either case, it's not meaningful to compute a sample
1020 * value. Just set it to 0.
1027 * Emit code to compensate for the difference between Y and W tiling.
1029 * This code modifies the X and Y coordinates according to the formula:
1031 * (X', Y', S') = detile(new_tiling, tile(old_tiling, X, Y, S))
1033 * (See brw_blorp_blit_program).
1035 * It can only translate between W and Y tiling, so new_tiling and old_tiling
1036 * are booleans where true represents W tiling and false represents Y tiling.
1039 brw_blorp_blit_program::translate_tiling(bool old_tiled_w
, bool new_tiled_w
)
1041 if (old_tiled_w
== new_tiled_w
)
1044 /* In the code that follows, we can safely assume that S = 0, because W
1045 * tiling formats always use IMS layout.
1050 /* Given X and Y coordinates that describe an address using Y tiling,
1051 * translate to the X and Y coordinates that describe the same address
1054 * If we break down the low order bits of X and Y, using a
1055 * single letter to represent each low-order bit:
1057 * X = A << 7 | 0bBCDEFGH
1058 * Y = J << 5 | 0bKLMNP (1)
1060 * Then we can apply the Y tiling formula to see the memory offset being
1063 * offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
1065 * If we apply the W detiling formula to this memory location, that the
1066 * corresponding X' and Y' coordinates are:
1068 * X' = A << 6 | 0bBCDPFH (3)
1069 * Y' = J << 6 | 0bKLMNEG
1071 * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
1072 * we need to make the following computation:
1074 * X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
1075 * Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
1077 emit_and(t1
, X
, brw_imm_uw(0xfff4)); /* X & ~0b1011 */
1078 emit_shr(t1
, t1
, brw_imm_uw(1)); /* (X & ~0b1011) >> 1 */
1079 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1080 emit_shl(t2
, t2
, brw_imm_uw(2)); /* (Y & 0b1) << 2 */
1081 emit_or(t1
, t1
, t2
); /* (X & ~0b1011) >> 1 | (Y & 0b1) << 2 */
1082 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1083 emit_or(Xp
, t1
, t2
);
1084 emit_and(t1
, Y
, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1085 emit_shl(t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1086 emit_and(t2
, X
, brw_imm_uw(8)); /* X & 0b1000 */
1087 emit_shr(t2
, t2
, brw_imm_uw(2)); /* (X & 0b1000) >> 2 */
1088 emit_or(t1
, t1
, t2
); /* (Y & ~0b1) << 1 | (X & 0b1000) >> 2 */
1089 emit_and(t2
, X
, brw_imm_uw(2)); /* X & 0b10 */
1090 emit_shr(t2
, t2
, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1091 emit_or(Yp
, t1
, t2
);
1094 /* Applying the same logic as above, but in reverse, we obtain the
1097 * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
1098 * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
1100 emit_and(t1
, X
, brw_imm_uw(0xfffa)); /* X & ~0b101 */
1101 emit_shl(t1
, t1
, brw_imm_uw(1)); /* (X & ~0b101) << 1 */
1102 emit_and(t2
, Y
, brw_imm_uw(2)); /* Y & 0b10 */
1103 emit_shl(t2
, t2
, brw_imm_uw(2)); /* (Y & 0b10) << 2 */
1104 emit_or(t1
, t1
, t2
); /* (X & ~0b101) << 1 | (Y & 0b10) << 2 */
1105 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1106 emit_shl(t2
, t2
, brw_imm_uw(1)); /* (Y & 0b1) << 1 */
1107 emit_or(t1
, t1
, t2
); /* (X & ~0b101) << 1 | (Y & 0b10) << 2
1109 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1110 emit_or(Xp
, t1
, t2
);
1111 emit_and(t1
, Y
, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1112 emit_shr(t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1113 emit_and(t2
, X
, brw_imm_uw(4)); /* X & 0b100 */
1114 emit_shr(t2
, t2
, brw_imm_uw(2)); /* (X & 0b100) >> 2 */
1115 emit_or(Yp
, t1
, t2
);
1121 * Emit code to compensate for the difference between MSAA and non-MSAA
1124 * This code modifies the X and Y coordinates according to the formula:
1126 * (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
1128 * (See brw_blorp_blit_program).
1131 brw_blorp_blit_program::encode_msaa(unsigned num_samples
,
1132 intel_msaa_layout layout
)
1135 case INTEL_MSAA_LAYOUT_NONE
:
1136 /* No translation necessary, and S should already be zero. */
1139 case INTEL_MSAA_LAYOUT_CMS
:
1140 /* We can't compensate for compressed layout since at this point in the
1141 * program we haven't read from the MCS buffer.
1143 unreachable("Bad layout in encode_msaa");
1144 case INTEL_MSAA_LAYOUT_UMS
:
1145 /* No translation necessary. */
1147 case INTEL_MSAA_LAYOUT_IMS
:
1148 switch (num_samples
) {
1150 /* encode_msaa(2, IMS, X, Y, S) = (X', Y', 0)
1151 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
1155 /* encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
1156 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
1157 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1159 emit_and(t1
, X
, brw_imm_uw(0xfffe)); /* X & ~0b1 */
1161 emit_and(t2
, S
, brw_imm_uw(1)); /* S & 0b1 */
1162 emit_or(t1
, t1
, t2
); /* (X & ~0b1) | (S & 0b1) */
1164 emit_shl(t1
, t1
, brw_imm_uw(1)); /* (X & ~0b1) << 1
1166 if (num_samples
== 2) {
1171 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1172 emit_or(Xp
, t1
, t2
);
1173 emit_and(t1
, Y
, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1174 emit_shl(t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1176 emit_and(t2
, S
, brw_imm_uw(2)); /* S & 0b10 */
1177 emit_or(t1
, t1
, t2
); /* (Y & ~0b1) << 1 | (S & 0b10) */
1179 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1180 emit_or(Yp
, t1
, t2
);
1183 /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
1184 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
1186 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1188 emit_and(t1
, X
, brw_imm_uw(0xfffe)); /* X & ~0b1 */
1189 emit_shl(t1
, t1
, brw_imm_uw(2)); /* (X & ~0b1) << 2 */
1191 emit_and(t2
, S
, brw_imm_uw(4)); /* S & 0b100 */
1192 emit_or(t1
, t1
, t2
); /* (X & ~0b1) << 2 | (S & 0b100) */
1193 emit_and(t2
, S
, brw_imm_uw(1)); /* S & 0b1 */
1194 emit_shl(t2
, t2
, brw_imm_uw(1)); /* (S & 0b1) << 1 */
1195 emit_or(t1
, t1
, t2
); /* (X & ~0b1) << 2 | (S & 0b100)
1198 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1199 emit_or(Xp
, t1
, t2
);
1200 emit_and(t1
, Y
, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1201 emit_shl(t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1203 emit_and(t2
, S
, brw_imm_uw(2)); /* S & 0b10 */
1204 emit_or(t1
, t1
, t2
); /* (Y & ~0b1) << 1 | (S & 0b10) */
1206 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1207 emit_or(Yp
, t1
, t2
);
1217 * Emit code to compensate for the difference between MSAA and non-MSAA
1220 * This code modifies the X and Y coordinates according to the formula:
1222 * (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
1224 * (See brw_blorp_blit_program).
1227 brw_blorp_blit_program::decode_msaa(unsigned num_samples
,
1228 intel_msaa_layout layout
)
1231 case INTEL_MSAA_LAYOUT_NONE
:
1232 /* No translation necessary, and S should already be zero. */
1235 case INTEL_MSAA_LAYOUT_CMS
:
1236 /* We can't compensate for compressed layout since at this point in the
1237 * program we don't have access to the MCS buffer.
1239 unreachable("Bad layout in encode_msaa");
1240 case INTEL_MSAA_LAYOUT_UMS
:
1241 /* No translation necessary. */
1243 case INTEL_MSAA_LAYOUT_IMS
:
1245 switch (num_samples
) {
1247 /* decode_msaa(2, IMS, X, Y, 0) = (X', Y', S)
1248 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
1249 * S = (X & 0b10) >> 1
1252 /* decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
1253 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
1254 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1255 * S = (Y & 0b10) | (X & 0b10) >> 1
1257 emit_and(t1
, X
, brw_imm_uw(0xfffc)); /* X & ~0b11 */
1258 emit_shr(t1
, t1
, brw_imm_uw(1)); /* (X & ~0b11) >> 1 */
1259 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1260 emit_or(Xp
, t1
, t2
);
1262 if (num_samples
== 2) {
1264 emit_and(t2
, X
, brw_imm_uw(2)); /* X & 0b10 */
1265 emit_shr(S
, t2
, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1267 emit_and(t1
, Y
, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1268 emit_shr(t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1269 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1270 emit_or(Yp
, t1
, t2
);
1271 emit_and(t1
, Y
, brw_imm_uw(2)); /* Y & 0b10 */
1272 emit_and(t2
, X
, brw_imm_uw(2)); /* X & 0b10 */
1273 emit_shr(t2
, t2
, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1278 /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
1279 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
1280 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1281 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
1283 emit_and(t1
, X
, brw_imm_uw(0xfff8)); /* X & ~0b111 */
1284 emit_shr(t1
, t1
, brw_imm_uw(2)); /* (X & ~0b111) >> 2 */
1285 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1286 emit_or(Xp
, t1
, t2
);
1287 emit_and(t1
, Y
, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1288 emit_shr(t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1289 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1290 emit_or(Yp
, t1
, t2
);
1291 emit_and(t1
, X
, brw_imm_uw(4)); /* X & 0b100 */
1292 emit_and(t2
, Y
, brw_imm_uw(2)); /* Y & 0b10 */
1293 emit_or(t1
, t1
, t2
); /* (X & 0b100) | (Y & 0b10) */
1294 emit_and(t2
, X
, brw_imm_uw(2)); /* X & 0b10 */
1295 emit_shr(t2
, t2
, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1306 * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
1310 brw_blorp_blit_program::translate_dst_to_src()
1312 struct brw_reg X_f
= retype(X
, BRW_REGISTER_TYPE_F
);
1313 struct brw_reg Y_f
= retype(Y
, BRW_REGISTER_TYPE_F
);
1314 struct brw_reg Xp_f
= retype(Xp
, BRW_REGISTER_TYPE_F
);
1315 struct brw_reg Yp_f
= retype(Yp
, BRW_REGISTER_TYPE_F
);
1317 /* Move the UD coordinates to float registers. */
1320 /* Scale and offset */
1321 emit_mad(X_f
, x_transform
.offset
, Xp_f
, x_transform
.multiplier
);
1322 emit_mad(Y_f
, y_transform
.offset
, Yp_f
, y_transform
.multiplier
);
1323 if (key
->blit_scaled
&& key
->blend
) {
1324 /* Translate coordinates to lay out the samples in a rectangular grid
1325 * roughly corresponding to sample locations.
1327 emit_mul(X_f
, X_f
, brw_imm_f(key
->x_scale
));
1328 emit_mul(Y_f
, Y_f
, brw_imm_f(key
->y_scale
));
1329 /* Adjust coordinates so that integers represent pixel centers rather
1332 emit_add(X_f
, X_f
, brw_imm_f(-0.5));
1333 emit_add(Y_f
, Y_f
, brw_imm_f(-0.5));
1335 /* Clamp the X, Y texture coordinates to properly handle the sampling of
1336 * texels on texture edges.
1338 clamp_tex_coords(X_f
, Y_f
,
1339 brw_imm_f(0.0), brw_imm_f(0.0),
1340 rect_grid_x1
, rect_grid_y1
);
1342 /* Store the fractional parts to be used as bilinear interpolation
1345 emit_frc(x_frac
, X_f
);
1346 emit_frc(y_frac
, Y_f
);
1348 /* Round the float coordinates down to nearest integer */
1349 emit_rndd(Xp_f
, X_f
);
1350 emit_rndd(Yp_f
, Y_f
);
1351 emit_mul(X_f
, Xp_f
, brw_imm_f(1.0f
/ key
->x_scale
));
1352 emit_mul(Y_f
, Yp_f
, brw_imm_f(1.0f
/ key
->y_scale
));
1354 } else if (!key
->bilinear_filter
) {
1355 /* Round the float coordinates down to nearest integer by moving to
1365 brw_blorp_blit_program::clamp_tex_coords(struct brw_reg regX
,
1366 struct brw_reg regY
,
1367 struct brw_reg clampX0
,
1368 struct brw_reg clampY0
,
1369 struct brw_reg clampX1
,
1370 struct brw_reg clampY1
)
1372 emit_max(regX
, regX
, clampX0
);
1373 emit_max(regY
, regY
, clampY0
);
1374 emit_min(regX
, regX
, clampX1
);
1375 emit_min(regY
, regY
, clampY1
);
1379 * Emit code to transform the X and Y coordinates as needed for blending
1380 * together the different samples in an MSAA texture.
1383 brw_blorp_blit_program::single_to_blend()
1385 /* When looking up samples in an MSAA texture using the SAMPLE message,
1386 * Gen6 requires the texture coordinates to be odd integers (so that they
1387 * correspond to the center of a 2x2 block representing the four samples
1388 * that maxe up a pixel). So we need to multiply our X and Y coordinates
1389 * each by 2 and then add 1.
1391 emit_shl(t1
, X
, brw_imm_w(1));
1392 emit_shl(t2
, Y
, brw_imm_w(1));
1393 emit_add(Xp
, t1
, brw_imm_w(1));
1394 emit_add(Yp
, t2
, brw_imm_w(1));
1400 * Count the number of trailing 1 bits in the given value. For example:
1402 * count_trailing_one_bits(0) == 0
1403 * count_trailing_one_bits(7) == 3
1404 * count_trailing_one_bits(11) == 2
1406 static inline int count_trailing_one_bits(unsigned value
)
1408 #ifdef HAVE___BUILTIN_CTZ
1409 return __builtin_ctz(~value
);
1411 return _mesa_bitcount(value
& ~(value
+ 1));
1417 brw_blorp_blit_program::manual_blend_average(unsigned num_samples
)
1419 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
1422 /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
1424 * result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
1426 * This ensures that when all samples have the same value, no numerical
1427 * precision is lost, since each addition operation always adds two equal
1428 * values, and summing two equal floating point values does not lose
1431 * We perform this computation by treating the texture_data array as a
1432 * stack and performing the following operations:
1434 * - push sample 0 onto stack
1435 * - push sample 1 onto stack
1436 * - add top two stack entries
1437 * - push sample 2 onto stack
1438 * - push sample 3 onto stack
1439 * - add top two stack entries
1440 * - add top two stack entries
1441 * - divide top stack entry by 4
1443 * Note that after pushing sample i onto the stack, the number of add
1444 * operations we do is equal to the number of trailing 1 bits in i. This
1445 * works provided the total number of samples is a power of two, which it
1446 * always is for i965.
1448 * For integer formats, we replace the add operations with average
1449 * operations and skip the final division.
1451 unsigned stack_depth
= 0;
1452 for (unsigned i
= 0; i
< num_samples
; ++i
) {
1453 assert(stack_depth
== _mesa_bitcount(i
)); /* Loop invariant */
1455 /* Push sample i onto the stack */
1456 assert(stack_depth
< ARRAY_SIZE(texture_data
));
1461 emit_mov(vec16(S
), brw_imm_ud(i
));
1463 texel_fetch(texture_data
[stack_depth
++]);
1465 if (i
== 0 && key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
) {
1466 /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
1467 * suggests an optimization:
1469 * "A simple optimization with probable large return in
1470 * performance is to compare the MCS value to zero (indicating
1471 * all samples are on sample slice 0), and sample only from
1472 * sample slice 0 using ld2dss if MCS is zero."
1474 * Note that in the case where the MCS value is zero, sampling from
1475 * sample slice 0 using ld2dss and sampling from sample 0 using
1476 * ld2dms are equivalent (since all samples are on sample slice 0).
1477 * Since we have already sampled from sample 0, all we need to do is
1478 * skip the remaining fetches and averaging if MCS is zero.
1480 emit_cmp_if(BRW_CONDITIONAL_NZ
, mcs_data
, brw_imm_ud(0));
1483 /* Do count_trailing_one_bits(i) times */
1484 for (int j
= count_trailing_one_bits(i
); j
-- > 0; ) {
1485 assert(stack_depth
>= 2);
1488 /* TODO: should use a smaller loop bound for non_RGBA formats */
1489 for (int k
= 0; k
< 4; ++k
) {
1490 emit_combine(key
->texture_data_type
== BRW_REGISTER_TYPE_F
?
1491 BRW_OPCODE_ADD
: BRW_OPCODE_AVG
,
1492 offset(texture_data
[stack_depth
- 1], 2*k
),
1493 offset(vec8(texture_data
[stack_depth
- 1]), 2*k
),
1494 offset(vec8(texture_data
[stack_depth
]), 2*k
));
1499 /* We should have just 1 sample on the stack now. */
1500 assert(stack_depth
== 1);
1502 if (key
->texture_data_type
== BRW_REGISTER_TYPE_F
) {
1503 /* Scale the result down by a factor of num_samples */
1504 /* TODO: should use a smaller loop bound for non-RGBA formats */
1505 for (int j
= 0; j
< 4; ++j
) {
1506 emit_mul(offset(texture_data
[0], 2*j
),
1507 offset(vec8(texture_data
[0]), 2*j
),
1508 brw_imm_f(1.0f
/ num_samples
));
1512 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
1517 brw_blorp_blit_program::manual_blend_bilinear(unsigned num_samples
)
1519 /* We do this computation by performing the following operations:
1521 * In case of 4x, 8x MSAA:
1522 * - Compute the pixel coordinates and sample numbers (a, b, c, d)
1523 * which are later used for interpolation
1524 * - linearly interpolate samples a and b in X
1525 * - linearly interpolate samples c and d in X
1526 * - linearly interpolate the results of last two operations in Y
1528 * result = lrp(lrp(a + b) + lrp(c + d))
1530 struct brw_reg Xp_f
= retype(Xp
, BRW_REGISTER_TYPE_F
);
1531 struct brw_reg Yp_f
= retype(Yp
, BRW_REGISTER_TYPE_F
);
1532 struct brw_reg t1_f
= retype(t1
, BRW_REGISTER_TYPE_F
);
1533 struct brw_reg t2_f
= retype(t2
, BRW_REGISTER_TYPE_F
);
1535 for (unsigned i
= 0; i
< 4; ++i
) {
1536 assert(i
< ARRAY_SIZE(texture_data
));
1539 /* Compute pixel coordinates */
1540 emit_add(vec16(x_sample_coords
), Xp_f
,
1541 brw_imm_f((float)(i
& 0x1) * (1.0f
/ key
->x_scale
)));
1542 emit_add(vec16(y_sample_coords
), Yp_f
,
1543 brw_imm_f((float)((i
>> 1) & 0x1) * (1.0f
/ key
->y_scale
)));
1544 emit_mov(vec16(X
), x_sample_coords
);
1545 emit_mov(vec16(Y
), y_sample_coords
);
1547 /* The MCS value we fetch has to match up with the pixel that we're
1548 * sampling from. Since we sample from different pixels in each
1549 * iteration of this "for" loop, the call to mcs_fetch() should be
1550 * here inside the loop after computing the pixel coordinates.
1552 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
1555 /* Compute sample index and map the sample index to a sample number.
1556 * Sample index layout shows the numbering of slots in a rectangular
1557 * grid of samples with in a pixel. Sample number layout shows the
1558 * rectangular grid of samples roughly corresponding to the real sample
1559 * locations with in a pixel.
1560 * In case of 4x MSAA, layout of sample indices matches the layout of
1568 * In case of 8x MSAA the two layouts don't match.
1569 * sample index layout : --------- sample number layout : ---------
1570 * | 0 | 1 | | 5 | 2 |
1571 * --------- ---------
1572 * | 2 | 3 | | 4 | 6 |
1573 * --------- ---------
1574 * | 4 | 5 | | 0 | 3 |
1575 * --------- ---------
1576 * | 6 | 7 | | 7 | 1 |
1577 * --------- ---------
1579 emit_frc(vec16(t1_f
), x_sample_coords
);
1580 emit_frc(vec16(t2_f
), y_sample_coords
);
1581 emit_mul(vec16(t1_f
), t1_f
, brw_imm_f(key
->x_scale
));
1582 emit_mul(vec16(t2_f
), t2_f
, brw_imm_f(key
->x_scale
* key
->y_scale
));
1583 emit_add(vec16(t1_f
), t1_f
, t2_f
);
1584 emit_mov(vec16(S
), t1_f
);
1586 if (num_samples
== 8) {
1587 /* Map the sample index to a sample number */
1588 emit_cmp_if(BRW_CONDITIONAL_L
, S
, brw_imm_d(4));
1590 emit_mov(vec16(t2
), brw_imm_d(5));
1591 emit_if_eq_mov(S
, 1, vec16(t2
), 2);
1592 emit_if_eq_mov(S
, 2, vec16(t2
), 4);
1593 emit_if_eq_mov(S
, 3, vec16(t2
), 6);
1597 emit_mov(vec16(t2
), brw_imm_d(0));
1598 emit_if_eq_mov(S
, 5, vec16(t2
), 3);
1599 emit_if_eq_mov(S
, 6, vec16(t2
), 7);
1600 emit_if_eq_mov(S
, 7, vec16(t2
), 1);
1603 emit_mov(vec16(S
), t2
);
1605 texel_fetch(texture_data
[i
]);
1608 #define SAMPLE(x, y) offset(texture_data[x], y)
1609 for (int index
= 3; index
> 0; ) {
1610 /* Since we're doing SIMD16, 4 color channels fits in to 8 registers.
1611 * Counter value of 8 in 'for' loop below is used to interpolate all
1612 * the color components.
1614 for (int k
= 0; k
< 8; k
+= 2)
1615 emit_lrp(vec8(SAMPLE(index
- 1, k
)),
1617 vec8(SAMPLE(index
, k
)),
1618 vec8(SAMPLE(index
- 1, k
)));
1621 for (int k
= 0; k
< 8; k
+= 2)
1622 emit_lrp(vec8(SAMPLE(0, k
)),
1625 vec8(SAMPLE(0, k
)));
1630 * Emit code to look up a value in the texture using the SAMPLE message (which
1631 * does blending of MSAA surfaces).
1634 brw_blorp_blit_program::sample(struct brw_reg dst
)
1636 static const sampler_message_arg args
[2] = {
1637 SAMPLER_MESSAGE_ARG_U_FLOAT
,
1638 SAMPLER_MESSAGE_ARG_V_FLOAT
1641 texture_lookup(dst
, SHADER_OPCODE_TEX
, args
, ARRAY_SIZE(args
));
1645 * Emit code to look up a value in the texture using the SAMPLE_LD message
1646 * (which does a simple texel fetch).
1649 brw_blorp_blit_program::texel_fetch(struct brw_reg dst
)
1651 static const sampler_message_arg gen6_args
[5] = {
1652 SAMPLER_MESSAGE_ARG_U_INT
,
1653 SAMPLER_MESSAGE_ARG_V_INT
,
1654 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* R */
1655 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* LOD */
1656 SAMPLER_MESSAGE_ARG_SI_INT
1658 static const sampler_message_arg gen7_ld_args
[] = {
1659 SAMPLER_MESSAGE_ARG_U_INT
,
1660 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* LOD */
1661 SAMPLER_MESSAGE_ARG_V_INT
,
1662 SAMPLER_MESSAGE_ARG_R_INT
1664 static const sampler_message_arg gen7_ld2dss_args
[3] = {
1665 SAMPLER_MESSAGE_ARG_SI_INT
,
1666 SAMPLER_MESSAGE_ARG_U_INT
,
1667 SAMPLER_MESSAGE_ARG_V_INT
1669 static const sampler_message_arg gen7_ld2dms_args
[4] = {
1670 SAMPLER_MESSAGE_ARG_SI_INT
,
1671 SAMPLER_MESSAGE_ARG_MCS_INT
,
1672 SAMPLER_MESSAGE_ARG_U_INT
,
1673 SAMPLER_MESSAGE_ARG_V_INT
1675 static const sampler_message_arg gen9_ld_args
[] = {
1676 SAMPLER_MESSAGE_ARG_U_INT
,
1677 SAMPLER_MESSAGE_ARG_V_INT
,
1678 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* LOD */
1679 SAMPLER_MESSAGE_ARG_R_INT
1684 texture_lookup(dst
, SHADER_OPCODE_TXF
, gen6_args
, s_is_zero
? 2 : 5);
1689 switch (key
->tex_layout
) {
1690 case INTEL_MSAA_LAYOUT_IMS
:
1691 /* From the Ivy Bridge PRM, Vol4 Part1 p72 (Multisampled Surface Storage
1694 * If this field is MSFMT_DEPTH_STENCIL
1695 * [a.k.a. INTEL_MSAA_LAYOUT_IMS], the only sampling engine
1696 * messages allowed are "ld2dms", "resinfo", and "sampleinfo".
1698 * So fall through to emit the same message as we use for
1699 * INTEL_MSAA_LAYOUT_CMS.
1701 case INTEL_MSAA_LAYOUT_CMS
:
1702 texture_lookup(dst
, SHADER_OPCODE_TXF_CMS
,
1703 gen7_ld2dms_args
, ARRAY_SIZE(gen7_ld2dms_args
));
1705 case INTEL_MSAA_LAYOUT_UMS
:
1706 texture_lookup(dst
, SHADER_OPCODE_TXF_UMS
,
1707 gen7_ld2dss_args
, ARRAY_SIZE(gen7_ld2dss_args
));
1709 case INTEL_MSAA_LAYOUT_NONE
:
1712 texture_lookup(dst
, SHADER_OPCODE_TXF
, gen7_ld_args
,
1713 ARRAY_SIZE(gen7_ld_args
));
1715 texture_lookup(dst
, SHADER_OPCODE_TXF
, gen9_ld_args
,
1716 ARRAY_SIZE(gen9_ld_args
));
1722 unreachable("Should not get here.");
1727 brw_blorp_blit_program::mcs_fetch()
1729 static const sampler_message_arg gen7_ld_mcs_args
[2] = {
1730 SAMPLER_MESSAGE_ARG_U_INT
,
1731 SAMPLER_MESSAGE_ARG_V_INT
1733 texture_lookup(vec16(mcs_data
), SHADER_OPCODE_TXF_MCS
,
1734 gen7_ld_mcs_args
, ARRAY_SIZE(gen7_ld_mcs_args
));
1738 brw_blorp_blit_program::texture_lookup(struct brw_reg dst
,
1740 const sampler_message_arg
*args
,
1743 struct brw_reg mrf
=
1744 retype(vec16(brw_message_reg(base_mrf
)), BRW_REGISTER_TYPE_UD
);
1745 for (int arg
= 0; arg
< num_args
; ++arg
) {
1746 switch (args
[arg
]) {
1747 case SAMPLER_MESSAGE_ARG_U_FLOAT
:
1748 if (key
->bilinear_filter
)
1749 emit_mov(retype(mrf
, BRW_REGISTER_TYPE_F
),
1750 retype(X
, BRW_REGISTER_TYPE_F
));
1752 emit_mov(retype(mrf
, BRW_REGISTER_TYPE_F
), X
);
1754 case SAMPLER_MESSAGE_ARG_V_FLOAT
:
1755 if (key
->bilinear_filter
)
1756 emit_mov(retype(mrf
, BRW_REGISTER_TYPE_F
),
1757 retype(Y
, BRW_REGISTER_TYPE_F
));
1759 emit_mov(retype(mrf
, BRW_REGISTER_TYPE_F
), Y
);
1761 case SAMPLER_MESSAGE_ARG_U_INT
:
1764 case SAMPLER_MESSAGE_ARG_V_INT
:
1767 case SAMPLER_MESSAGE_ARG_R_INT
:
1768 emit_mov(mrf
, src_z
);
1770 case SAMPLER_MESSAGE_ARG_SI_INT
:
1771 /* Note: on Gen7, this code may be reached with s_is_zero==true
1772 * because in Gen7's ld2dss message, the sample index is the first
1773 * argument. When this happens, we need to move a 0 into the
1774 * appropriate message register.
1777 emit_mov(mrf
, brw_imm_ud(0));
1781 case SAMPLER_MESSAGE_ARG_MCS_INT
:
1782 switch (key
->tex_layout
) {
1783 case INTEL_MSAA_LAYOUT_CMS
:
1784 emit_mov(mrf
, mcs_data
);
1786 case INTEL_MSAA_LAYOUT_IMS
:
1787 /* When sampling from an IMS surface, MCS data is not relevant,
1788 * and the hardware ignores it. So don't bother populating it.
1792 /* We shouldn't be trying to send MCS data with any other
1795 assert (!"Unsupported layout for MCS data");
1799 case SAMPLER_MESSAGE_ARG_ZERO_INT
:
1800 emit_mov(mrf
, brw_imm_ud(0));
1806 emit_texture_lookup(retype(dst
, BRW_REGISTER_TYPE_UW
) /* dest */,
1809 mrf
.nr
- base_mrf
/* msg_length */);
1817 #undef SWAP_XY_AND_XPYP
1820 brw_blorp_blit_program::render_target_write()
1822 struct brw_reg mrf_rt_write
=
1823 retype(vec16(brw_message_reg(base_mrf
)), key
->texture_data_type
);
1826 /* If we may have killed pixels, then we need to send R0 and R1 in a header
1827 * so that the render target knows which pixels we killed.
1829 bool use_header
= key
->use_kill
;
1831 /* Copy R0/1 to MRF */
1832 emit_mov(retype(mrf_rt_write
, BRW_REGISTER_TYPE_UD
),
1833 retype(R0
, BRW_REGISTER_TYPE_UD
));
1837 /* Copy texture data to MRFs */
1838 for (int i
= 0; i
< 4; ++i
) {
1839 /* E.g. mov(16) m2.0<1>:f r2.0<8;8,1>:f { Align1, H1 } */
1840 emit_mov(offset(mrf_rt_write
, mrf_offset
),
1841 offset(vec8(texture_data
[0]), 2*i
));
1845 /* Now write to the render target and terminate the thread */
1846 emit_render_target_write(
1848 brw
->gen
< 8 ? base_mrf
: -1,
1849 mrf_offset
/* msg_length. TODO: Should be smaller for non-RGBA formats. */,
1855 brw_blorp_coord_transform_params::setup(GLfloat src0
, GLfloat src1
,
1856 GLfloat dst0
, GLfloat dst1
,
1859 float scale
= (src1
- src0
) / (dst1
- dst0
);
1861 /* When not mirroring a coordinate (say, X), we need:
1862 * src_x - src_x0 = (dst_x - dst_x0 + 0.5) * scale
1864 * src_x = src_x0 + (dst_x - dst_x0 + 0.5) * scale
1866 * blorp program uses "round toward zero" to convert the
1867 * transformed floating point coordinates to integer coordinates,
1868 * whereas the behaviour we actually want is "round to nearest",
1869 * so 0.5 provides the necessary correction.
1872 offset
= src0
+ (-dst0
+ 0.5f
) * scale
;
1874 /* When mirroring X we need:
1875 * src_x - src_x0 = dst_x1 - dst_x - 0.5
1877 * src_x = src_x0 + (dst_x1 -dst_x - 0.5) * scale
1879 multiplier
= -scale
;
1880 offset
= src0
+ (dst1
- 0.5f
) * scale
;
1886 * Determine which MSAA layout the GPU pipeline should be configured for,
1887 * based on the chip generation, the number of samples, and the true layout of
1888 * the image in memory.
1890 inline intel_msaa_layout
1891 compute_msaa_layout_for_pipeline(struct brw_context
*brw
, unsigned num_samples
,
1892 intel_msaa_layout true_layout
)
1894 if (num_samples
<= 1) {
1895 /* When configuring the GPU for non-MSAA, we can still accommodate IMS
1896 * format buffers, by transforming coordinates appropriately.
1898 assert(true_layout
== INTEL_MSAA_LAYOUT_NONE
||
1899 true_layout
== INTEL_MSAA_LAYOUT_IMS
);
1900 return INTEL_MSAA_LAYOUT_NONE
;
1902 assert(true_layout
!= INTEL_MSAA_LAYOUT_NONE
);
1905 /* Prior to Gen7, all MSAA surfaces use IMS layout. */
1906 if (brw
->gen
== 6) {
1907 assert(true_layout
== INTEL_MSAA_LAYOUT_IMS
);
1914 brw_blorp_blit_params::brw_blorp_blit_params(struct brw_context
*brw
,
1915 struct intel_mipmap_tree
*src_mt
,
1916 unsigned src_level
, unsigned src_layer
,
1917 mesa_format src_format
,
1918 struct intel_mipmap_tree
*dst_mt
,
1919 unsigned dst_level
, unsigned dst_layer
,
1920 mesa_format dst_format
,
1921 GLfloat src_x0
, GLfloat src_y0
,
1922 GLfloat src_x1
, GLfloat src_y1
,
1923 GLfloat dst_x0
, GLfloat dst_y0
,
1924 GLfloat dst_x1
, GLfloat dst_y1
,
1926 bool mirror_x
, bool mirror_y
)
1928 src
.set(brw
, src_mt
, src_level
, src_layer
, src_format
, false);
1929 dst
.set(brw
, dst_mt
, dst_level
, dst_layer
, dst_format
, true);
1931 /* Even though we do multisample resolves at the time of the blit, OpenGL
1932 * specification defines them as if they happen at the time of rendering,
1933 * which means that the type of averaging we do during the resolve should
1934 * only depend on the source format; the destination format should be
1935 * ignored. But, specification doesn't seem to be strict about it.
1937 * It has been observed that mulitisample resolves produce slightly better
1938 * looking images when averaging is done using destination format. NVIDIA's
1939 * proprietary OpenGL driver also follow this approach. So, we choose to
1940 * follow it in our driver.
1942 * When multisampling, if the source and destination formats are equal
1943 * (aside from the color space), we choose to blit in sRGB space to get
1944 * this higher quality image.
1946 if (src
.num_samples
> 1 &&
1947 _mesa_get_format_color_encoding(dst_mt
->format
) == GL_SRGB
&&
1948 _mesa_get_srgb_format_linear(src_mt
->format
) ==
1949 _mesa_get_srgb_format_linear(dst_mt
->format
)) {
1950 assert(brw
->format_supported_as_render_target
[dst_mt
->format
]);
1951 dst
.brw_surfaceformat
= brw
->render_target_format
[dst_mt
->format
];
1952 src
.brw_surfaceformat
= brw_format_for_mesa_format(dst_mt
->format
);
1955 /* When doing a multisample resolve of a GL_LUMINANCE32F or GL_INTENSITY32F
1956 * texture, the above code configures the source format for L32_FLOAT or
1957 * I32_FLOAT, and the destination format for R32_FLOAT. On Sandy Bridge,
1958 * the SAMPLE message appears to handle multisampled L32_FLOAT and
1959 * I32_FLOAT textures incorrectly, resulting in blocky artifacts. So work
1960 * around the problem by using a source format of R32_FLOAT. This
1961 * shouldn't affect rendering correctness, since the destination format is
1962 * R32_FLOAT, so only the contents of the red channel matters.
1964 if (brw
->gen
== 6 && src
.num_samples
> 1 && dst
.num_samples
<= 1 &&
1965 src_mt
->format
== dst_mt
->format
&&
1966 dst
.brw_surfaceformat
== BRW_SURFACEFORMAT_R32_FLOAT
) {
1967 src
.brw_surfaceformat
= dst
.brw_surfaceformat
;
1971 memset(&wm_prog_key
, 0, sizeof(wm_prog_key
));
1973 /* texture_data_type indicates the register type that should be used to
1974 * manipulate texture data.
1976 switch (_mesa_get_format_datatype(src_mt
->format
)) {
1977 case GL_UNSIGNED_NORMALIZED
:
1978 case GL_SIGNED_NORMALIZED
:
1980 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_F
;
1982 case GL_UNSIGNED_INT
:
1983 if (src_mt
->format
== MESA_FORMAT_S_UINT8
) {
1984 /* We process stencil as though it's an unsigned normalized color */
1985 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_F
;
1987 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_UD
;
1991 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_D
;
1994 unreachable("Unrecognized blorp format");
1998 /* Gen7's rendering hardware only supports the IMS layout for depth and
1999 * stencil render targets. Blorp always maps its destination surface as
2000 * a color render target (even if it's actually a depth or stencil
2001 * buffer). So if the destination is IMS, we'll have to map it as a
2002 * single-sampled texture and interleave the samples ourselves.
2004 if (dst_mt
->msaa_layout
== INTEL_MSAA_LAYOUT_IMS
)
2005 dst
.num_samples
= 0;
2008 if (dst
.map_stencil_as_y_tiled
&& dst
.num_samples
> 1) {
2009 /* If the destination surface is a W-tiled multisampled stencil buffer
2010 * that we're mapping as Y tiled, then we need to arrange for the WM
2011 * program to run once per sample rather than once per pixel, because
2012 * the memory layout of related samples doesn't match between W and Y
2015 wm_prog_key
.persample_msaa_dispatch
= true;
2018 if (src
.num_samples
> 0 && dst
.num_samples
> 1) {
2019 /* We are blitting from a multisample buffer to a multisample buffer, so
2020 * we must preserve samples within a pixel. This means we have to
2021 * arrange for the WM program to run once per sample rather than once
2024 wm_prog_key
.persample_msaa_dispatch
= true;
2027 /* Scaled blitting or not. */
2028 wm_prog_key
.blit_scaled
=
2029 ((dst_x1
- dst_x0
) == (src_x1
- src_x0
) &&
2030 (dst_y1
- dst_y0
) == (src_y1
- src_y0
)) ? false : true;
2032 /* Scaling factors used for bilinear filtering in multisample scaled
2035 wm_prog_key
.x_scale
= 2.0f
;
2036 wm_prog_key
.y_scale
= src_mt
->num_samples
/ 2.0f
;
2038 if (filter
== GL_LINEAR
&& src
.num_samples
<= 1 && dst
.num_samples
<= 1)
2039 wm_prog_key
.bilinear_filter
= true;
2041 GLenum base_format
= _mesa_get_format_base_format(src_mt
->format
);
2042 if (base_format
!= GL_DEPTH_COMPONENT
&& /* TODO: what about depth/stencil? */
2043 base_format
!= GL_STENCIL_INDEX
&&
2044 src_mt
->num_samples
> 1 && dst_mt
->num_samples
<= 1) {
2045 /* We are downsampling a color buffer, so blend. */
2046 wm_prog_key
.blend
= true;
2049 /* src_samples and dst_samples are the true sample counts */
2050 wm_prog_key
.src_samples
= src_mt
->num_samples
;
2051 wm_prog_key
.dst_samples
= dst_mt
->num_samples
;
2053 /* tex_samples and rt_samples are the sample counts that are set up in
2056 wm_prog_key
.tex_samples
= src
.num_samples
;
2057 wm_prog_key
.rt_samples
= dst
.num_samples
;
2059 /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
2060 * use to access the source and destination surfaces.
2062 wm_prog_key
.tex_layout
=
2063 compute_msaa_layout_for_pipeline(brw
, src
.num_samples
, src
.msaa_layout
);
2064 wm_prog_key
.rt_layout
=
2065 compute_msaa_layout_for_pipeline(brw
, dst
.num_samples
, dst
.msaa_layout
);
2067 /* src_layout and dst_layout indicate the true MSAA layout used by src and
2070 wm_prog_key
.src_layout
= src_mt
->msaa_layout
;
2071 wm_prog_key
.dst_layout
= dst_mt
->msaa_layout
;
2073 wm_prog_key
.src_tiled_w
= src
.map_stencil_as_y_tiled
;
2074 wm_prog_key
.dst_tiled_w
= dst
.map_stencil_as_y_tiled
;
2075 /* Round floating point values to nearest integer to avoid "off by one texel"
2076 * kind of errors when blitting.
2078 x0
= wm_push_consts
.dst_x0
= roundf(dst_x0
);
2079 y0
= wm_push_consts
.dst_y0
= roundf(dst_y0
);
2080 x1
= wm_push_consts
.dst_x1
= roundf(dst_x1
);
2081 y1
= wm_push_consts
.dst_y1
= roundf(dst_y1
);
2082 wm_push_consts
.rect_grid_x1
= (minify(src_mt
->logical_width0
, src_level
) *
2083 wm_prog_key
.x_scale
- 1.0f
);
2084 wm_push_consts
.rect_grid_y1
= (minify(src_mt
->logical_height0
, src_level
) *
2085 wm_prog_key
.y_scale
- 1.0f
);
2087 wm_push_consts
.x_transform
.setup(src_x0
, src_x1
, dst_x0
, dst_x1
, mirror_x
);
2088 wm_push_consts
.y_transform
.setup(src_y0
, src_y1
, dst_y0
, dst_y1
, mirror_y
);
2090 wm_push_consts
.src_z
= src
.mt
->target
== GL_TEXTURE_3D
? src
.layer
: 0;
2092 if (dst
.num_samples
<= 1 && dst_mt
->num_samples
> 1) {
2093 /* We must expand the rectangle we send through the rendering pipeline,
2094 * to account for the fact that we are mapping the destination region as
2095 * single-sampled when it is in fact multisampled. We must also align
2096 * it to a multiple of the multisampling pattern, because the
2097 * differences between multisampled and single-sampled surface formats
2098 * will mean that pixels are scrambled within the multisampling pattern.
2099 * TODO: what if this makes the coordinates too large?
2101 * Note: this only works if the destination surface uses the IMS layout.
2102 * If it's UMS, then we have no choice but to set up the rendering
2103 * pipeline as multisampled.
2105 assert(dst_mt
->msaa_layout
== INTEL_MSAA_LAYOUT_IMS
);
2106 switch (dst_mt
->num_samples
) {
2108 x0
= ROUND_DOWN_TO(x0
* 2, 4);
2109 y0
= ROUND_DOWN_TO(y0
, 4);
2110 x1
= ALIGN(x1
* 2, 4);
2114 x0
= ROUND_DOWN_TO(x0
* 2, 4);
2115 y0
= ROUND_DOWN_TO(y0
* 2, 4);
2116 x1
= ALIGN(x1
* 2, 4);
2117 y1
= ALIGN(y1
* 2, 4);
2120 x0
= ROUND_DOWN_TO(x0
* 4, 8);
2121 y0
= ROUND_DOWN_TO(y0
* 2, 4);
2122 x1
= ALIGN(x1
* 4, 8);
2123 y1
= ALIGN(y1
* 2, 4);
2126 unreachable("Unrecognized sample count in brw_blorp_blit_params ctor");
2128 wm_prog_key
.use_kill
= true;
2131 if (dst
.map_stencil_as_y_tiled
) {
2132 /* We must modify the rectangle we send through the rendering pipeline
2133 * (and the size and x/y offset of the destination surface), to account
2134 * for the fact that we are mapping it as Y-tiled when it is in fact
2137 * Both Y tiling and W tiling can be understood as organizations of
2138 * 32-byte sub-tiles; within each 32-byte sub-tile, the layout of pixels
2139 * is different, but the layout of the 32-byte sub-tiles within the 4k
2140 * tile is the same (8 sub-tiles across by 16 sub-tiles down, in
2141 * column-major order). In Y tiling, the sub-tiles are 16 bytes wide
2142 * and 2 rows high; in W tiling, they are 8 bytes wide and 4 rows high.
2144 * Therefore, to account for the layout differences within the 32-byte
2145 * sub-tiles, we must expand the rectangle so the X coordinates of its
2146 * edges are multiples of 8 (the W sub-tile width), and its Y
2147 * coordinates of its edges are multiples of 4 (the W sub-tile height).
2148 * Then we need to scale the X and Y coordinates of the rectangle to
2149 * account for the differences in aspect ratio between the Y and W
2150 * sub-tiles. We need to modify the layer width and height similarly.
2152 * A correction needs to be applied when MSAA is in use: since
2153 * INTEL_MSAA_LAYOUT_IMS uses an interleaving pattern whose height is 4,
2154 * we need to align the Y coordinates to multiples of 8, so that when
2155 * they are divided by two they are still multiples of 4.
2157 * Note: Since the x/y offset of the surface will be applied using the
2158 * SURFACE_STATE command packet, it will be invisible to the swizzling
2159 * code in the shader; therefore it needs to be in a multiple of the
2160 * 32-byte sub-tile size. Fortunately it is, since the sub-tile is 8
2161 * pixels wide and 4 pixels high (when viewed as a W-tiled stencil
2162 * buffer), and the miplevel alignment used for stencil buffers is 8
2163 * pixels horizontally and either 4 or 8 pixels vertically (see
2164 * intel_horizontal_texture_alignment_unit() and
2165 * intel_vertical_texture_alignment_unit()).
2167 * Note: Also, since the SURFACE_STATE command packet can only apply
2168 * offsets that are multiples of 4 pixels horizontally and 2 pixels
2169 * vertically, it is important that the offsets will be multiples of
2170 * these sizes after they are converted into Y-tiled coordinates.
2171 * Fortunately they will be, since we know from above that the offsets
2172 * are a multiple of the 32-byte sub-tile size, and in Y-tiled
2173 * coordinates the sub-tile is 16 pixels wide and 2 pixels high.
2175 * TODO: what if this makes the coordinates (or the texture size) too
2178 const unsigned x_align
= 8, y_align
= dst
.num_samples
!= 0 ? 8 : 4;
2179 x0
= ROUND_DOWN_TO(x0
, x_align
) * 2;
2180 y0
= ROUND_DOWN_TO(y0
, y_align
) / 2;
2181 x1
= ALIGN(x1
, x_align
) * 2;
2182 y1
= ALIGN(y1
, y_align
) / 2;
2183 dst
.width
= ALIGN(dst
.width
, x_align
) * 2;
2184 dst
.height
= ALIGN(dst
.height
, y_align
) / 2;
2187 wm_prog_key
.use_kill
= true;
2190 if (src
.map_stencil_as_y_tiled
) {
2191 /* We must modify the size and x/y offset of the source surface to
2192 * account for the fact that we are mapping it as Y-tiled when it is in
2195 * See the comments above concerning x/y offset alignment for the
2196 * destination surface.
2198 * TODO: what if this makes the texture size too large?
2200 const unsigned x_align
= 8, y_align
= src
.num_samples
!= 0 ? 8 : 4;
2201 src
.width
= ALIGN(src
.width
, x_align
) * 2;
2202 src
.height
= ALIGN(src
.height
, y_align
) / 2;
2209 brw_blorp_blit_params::get_wm_prog(struct brw_context
*brw
,
2210 brw_blorp_prog_data
**prog_data
) const
2212 uint32_t prog_offset
= 0;
2213 if (!brw_search_cache(&brw
->cache
, BRW_CACHE_BLORP_PROG
,
2214 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
2215 &prog_offset
, prog_data
)) {
2216 brw_blorp_blit_program
prog(brw
, &this->wm_prog_key
);
2217 GLuint program_size
;
2218 const GLuint
*program
= prog
.compile(brw
, INTEL_DEBUG
& DEBUG_BLORP
,
2220 brw_upload_cache(&brw
->cache
, BRW_CACHE_BLORP_PROG
,
2221 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
2222 program
, program_size
,
2223 &prog
.prog_data
, sizeof(prog
.prog_data
),
2224 &prog_offset
, prog_data
);