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/teximage.h"
25 #include "main/fbobject.h"
26 #include "main/renderbuffer.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"
35 #define FILE_DEBUG_FLAG DEBUG_BLORP
38 * Helper function for handling mirror image blits.
40 * If coord0 > coord1, swap them and invert the "mirror" boolean.
43 fixup_mirroring(bool &mirror
, GLfloat
&coord0
, GLfloat
&coord1
)
45 if (coord0
> coord1
) {
55 * Adjust {src,dst}_x{0,1} to account for clipping and scissoring of
56 * destination coordinates.
58 * Return true if there is still blitting to do, false if all pixels got
59 * rejected by the clip and/or scissor.
61 * For clarity, the nomenclature of this function assumes we are clipping and
62 * scissoring the X coordinate; the exact same logic applies for Y
65 * Note: this function may also be used to account for clipping of source
66 * coordinates, by swapping the roles of src and dst.
69 clip_or_scissor(bool mirror
, GLfloat
&src_x0
, GLfloat
&src_x1
, GLfloat
&dst_x0
,
70 GLfloat
&dst_x1
, GLfloat fb_xmin
, GLfloat fb_xmax
)
72 float scale
= (float) (src_x1
- src_x0
) / (dst_x1
- dst_x0
);
73 /* If we are going to scissor everything away, stop. */
74 if (!(fb_xmin
< fb_xmax
&&
81 /* Clip the destination rectangle, and keep track of how many pixels we
82 * clipped off of the left and right sides of it.
84 GLint pixels_clipped_left
= 0;
85 GLint pixels_clipped_right
= 0;
86 if (dst_x0
< fb_xmin
) {
87 pixels_clipped_left
= fb_xmin
- dst_x0
;
90 if (fb_xmax
< dst_x1
) {
91 pixels_clipped_right
= dst_x1
- fb_xmax
;
95 /* If we are mirrored, then before applying pixels_clipped_{left,right} to
96 * the source coordinates, we need to flip them to account for the
100 GLint tmp
= pixels_clipped_left
;
101 pixels_clipped_left
= pixels_clipped_right
;
102 pixels_clipped_right
= tmp
;
105 /* Adjust the source rectangle to remove the pixels corresponding to those
106 * that were clipped/scissored out of the destination rectangle.
108 src_x0
+= pixels_clipped_left
* scale
;
109 src_x1
-= pixels_clipped_right
* scale
;
115 static struct intel_mipmap_tree
*
116 find_miptree(GLbitfield buffer_bit
, struct intel_renderbuffer
*irb
)
118 struct intel_mipmap_tree
*mt
= irb
->mt
;
119 if (buffer_bit
== GL_STENCIL_BUFFER_BIT
&& mt
->stencil_mt
)
126 * Note: if the src (or dst) is a 2D multisample array texture on Gen7+ using
127 * INTEL_MSAA_LAYOUT_UMS or INTEL_MSAA_LAYOUT_CMS, src_layer (dst_layer) is
128 * the physical layer holding sample 0. So, for example, if
129 * src_mt->num_samples == 4, then logical layer n corresponds to src_layer ==
133 brw_blorp_blit_miptrees(struct brw_context
*brw
,
134 struct intel_mipmap_tree
*src_mt
,
135 unsigned src_level
, unsigned src_layer
,
136 struct intel_mipmap_tree
*dst_mt
,
137 unsigned dst_level
, unsigned dst_layer
,
138 float src_x0
, float src_y0
,
139 float src_x1
, float src_y1
,
140 float dst_x0
, float dst_y0
,
141 float dst_x1
, float dst_y1
,
142 GLenum filter
, bool mirror_x
, bool mirror_y
)
144 /* Get ready to blit. This includes depth resolving the src and dst
145 * buffers if necessary. Note: it's not necessary to do a color resolve on
146 * the destination buffer because we use the standard render path to render
147 * to destination color buffers, and the standard render path is
150 intel_miptree_resolve_color(brw
, src_mt
);
151 intel_miptree_slice_resolve_depth(brw
, src_mt
, src_level
, src_layer
);
152 intel_miptree_slice_resolve_depth(brw
, dst_mt
, dst_level
, dst_layer
);
154 DBG("%s from %s mt %p %d %d (%f,%f) (%f,%f)"
155 "to %s mt %p %d %d (%f,%f) (%f,%f) (flip %d,%d)\n",
157 _mesa_get_format_name(src_mt
->format
), src_mt
,
158 src_level
, src_layer
, src_x0
, src_y0
, src_x1
, src_y1
,
159 _mesa_get_format_name(dst_mt
->format
), dst_mt
,
160 dst_level
, dst_layer
, dst_x0
, dst_y0
, dst_x1
, dst_y1
,
163 brw_blorp_blit_params
params(brw
,
164 src_mt
, src_level
, src_layer
,
165 dst_mt
, dst_level
, dst_layer
,
170 filter
, mirror_x
, mirror_y
);
171 brw_blorp_exec(brw
, ¶ms
);
173 intel_miptree_slice_set_needs_hiz_resolve(dst_mt
, dst_level
, dst_layer
);
177 do_blorp_blit(struct brw_context
*brw
, GLbitfield buffer_bit
,
178 struct intel_renderbuffer
*src_irb
,
179 struct intel_renderbuffer
*dst_irb
,
180 GLfloat srcX0
, GLfloat srcY0
, GLfloat srcX1
, GLfloat srcY1
,
181 GLfloat dstX0
, GLfloat dstY0
, GLfloat dstX1
, GLfloat dstY1
,
182 GLenum filter
, bool mirror_x
, bool mirror_y
)
184 /* Find source/dst miptrees */
185 struct intel_mipmap_tree
*src_mt
= find_miptree(buffer_bit
, src_irb
);
186 struct intel_mipmap_tree
*dst_mt
= find_miptree(buffer_bit
, dst_irb
);
189 brw_blorp_blit_miptrees(brw
,
190 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
191 dst_mt
, dst_irb
->mt_level
, dst_irb
->mt_layer
,
192 srcX0
, srcY0
, srcX1
, srcY1
,
193 dstX0
, dstY0
, dstX1
, dstY1
,
194 filter
, mirror_x
, mirror_y
);
196 intel_renderbuffer_set_needs_downsample(dst_irb
);
200 color_formats_match(gl_format src_format
, gl_format dst_format
)
202 gl_format linear_src_format
= _mesa_get_srgb_format_linear(src_format
);
203 gl_format linear_dst_format
= _mesa_get_srgb_format_linear(dst_format
);
205 /* Normally, we require the formats to be equal. However, we also support
206 * blitting from ARGB to XRGB (discarding alpha), and from XRGB to ARGB
207 * (overriding alpha to 1.0 via blending).
209 return linear_src_format
== linear_dst_format
||
210 (linear_src_format
== MESA_FORMAT_XRGB8888
&&
211 linear_dst_format
== MESA_FORMAT_ARGB8888
) ||
212 (linear_src_format
== MESA_FORMAT_ARGB8888
&&
213 linear_dst_format
== MESA_FORMAT_XRGB8888
);
217 formats_match(GLbitfield buffer_bit
, struct intel_renderbuffer
*src_irb
,
218 struct intel_renderbuffer
*dst_irb
)
220 /* Note: don't just check gl_renderbuffer::Format, because in some cases
221 * multiple gl_formats resolve to the same native type in the miptree (for
222 * example MESA_FORMAT_X8_Z24 and MESA_FORMAT_S8_Z24), and we can blit
223 * between those formats.
225 gl_format src_format
= find_miptree(buffer_bit
, src_irb
)->format
;
226 gl_format dst_format
= find_miptree(buffer_bit
, dst_irb
)->format
;
228 return color_formats_match(src_format
, dst_format
);
232 try_blorp_blit(struct brw_context
*brw
,
233 GLfloat srcX0
, GLfloat srcY0
, GLfloat srcX1
, GLfloat srcY1
,
234 GLfloat dstX0
, GLfloat dstY0
, GLfloat dstX1
, GLfloat dstY1
,
235 GLenum filter
, GLbitfield buffer_bit
)
237 struct gl_context
*ctx
= &brw
->ctx
;
239 /* Sync up the state of window system buffers. We need to do this before
240 * we go looking for the buffers.
242 intel_prepare_render(brw
);
244 const struct gl_framebuffer
*read_fb
= ctx
->ReadBuffer
;
245 const struct gl_framebuffer
*draw_fb
= ctx
->DrawBuffer
;
247 /* Detect if the blit needs to be mirrored */
248 bool mirror_x
= false, mirror_y
= false;
249 fixup_mirroring(mirror_x
, srcX0
, srcX1
);
250 fixup_mirroring(mirror_x
, dstX0
, dstX1
);
251 fixup_mirroring(mirror_y
, srcY0
, srcY1
);
252 fixup_mirroring(mirror_y
, dstY0
, dstY1
);
254 /* If the destination rectangle needs to be clipped or scissored, do so.
256 if (!(clip_or_scissor(mirror_x
, srcX0
, srcX1
, dstX0
, dstX1
,
257 draw_fb
->_Xmin
, draw_fb
->_Xmax
) &&
258 clip_or_scissor(mirror_y
, srcY0
, srcY1
, dstY0
, dstY1
,
259 draw_fb
->_Ymin
, draw_fb
->_Ymax
))) {
260 /* Everything got clipped/scissored away, so the blit was successful. */
264 /* If the source rectangle needs to be clipped or scissored, do so. */
265 if (!(clip_or_scissor(mirror_x
, dstX0
, dstX1
, srcX0
, srcX1
,
266 0, read_fb
->Width
) &&
267 clip_or_scissor(mirror_y
, dstY0
, dstY1
, srcY0
, srcY1
,
268 0, read_fb
->Height
))) {
269 /* Everything got clipped/scissored away, so the blit was successful. */
273 /* Account for the fact that in the system framebuffer, the origin is at
276 if (_mesa_is_winsys_fbo(read_fb
)) {
277 GLint tmp
= read_fb
->Height
- srcY0
;
278 srcY0
= read_fb
->Height
- srcY1
;
280 mirror_y
= !mirror_y
;
282 if (_mesa_is_winsys_fbo(draw_fb
)) {
283 GLint tmp
= draw_fb
->Height
- dstY0
;
284 dstY0
= draw_fb
->Height
- dstY1
;
286 mirror_y
= !mirror_y
;
290 struct intel_renderbuffer
*src_irb
;
291 struct intel_renderbuffer
*dst_irb
;
292 switch (buffer_bit
) {
293 case GL_COLOR_BUFFER_BIT
:
294 src_irb
= intel_renderbuffer(read_fb
->_ColorReadBuffer
);
295 for (unsigned i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; ++i
) {
296 dst_irb
= intel_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[i
]);
297 if (dst_irb
&& !formats_match(buffer_bit
, src_irb
, dst_irb
))
300 for (unsigned i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; ++i
) {
301 dst_irb
= intel_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[i
]);
303 do_blorp_blit(brw
, buffer_bit
, src_irb
, dst_irb
, srcX0
, srcY0
,
304 srcX1
, srcY1
, dstX0
, dstY0
, dstX1
, dstY1
,
305 filter
, mirror_x
, mirror_y
);
308 case GL_DEPTH_BUFFER_BIT
:
310 intel_renderbuffer(read_fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
312 intel_renderbuffer(draw_fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
313 if (!formats_match(buffer_bit
, src_irb
, dst_irb
))
315 do_blorp_blit(brw
, buffer_bit
, src_irb
, dst_irb
, srcX0
, srcY0
,
316 srcX1
, srcY1
, dstX0
, dstY0
, dstX1
, dstY1
,
317 filter
, mirror_x
, mirror_y
);
319 case GL_STENCIL_BUFFER_BIT
:
321 intel_renderbuffer(read_fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
323 intel_renderbuffer(draw_fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
324 if (!formats_match(buffer_bit
, src_irb
, dst_irb
))
326 do_blorp_blit(brw
, buffer_bit
, src_irb
, dst_irb
, srcX0
, srcY0
,
327 srcX1
, srcY1
, dstX0
, dstY0
, dstX1
, dstY1
,
328 filter
, mirror_x
, mirror_y
);
338 brw_blorp_copytexsubimage(struct brw_context
*brw
,
339 struct gl_renderbuffer
*src_rb
,
340 struct gl_texture_image
*dst_image
,
342 int srcX0
, int srcY0
,
343 int dstX0
, int dstY0
,
344 int width
, int height
)
346 struct gl_context
*ctx
= &brw
->ctx
;
347 struct intel_renderbuffer
*src_irb
= intel_renderbuffer(src_rb
);
348 struct intel_texture_image
*intel_image
= intel_texture_image(dst_image
);
350 /* Sync up the state of window system buffers. We need to do this before
351 * we go looking at the src renderbuffer's miptree.
353 intel_prepare_render(brw
);
355 struct intel_mipmap_tree
*src_mt
= src_irb
->mt
;
356 struct intel_mipmap_tree
*dst_mt
= intel_image
->mt
;
358 /* BLORP is not supported before Gen6. */
359 if (brw
->gen
< 6 || brw
->gen
>= 8)
362 if (_mesa_get_format_base_format(src_mt
->format
) !=
363 _mesa_get_format_base_format(dst_mt
->format
)) {
367 /* We can't handle format conversions between Z24 and other formats since
368 * we have to lie about the surface format. See the comments in
369 * brw_blorp_surface_info::set().
371 if ((src_mt
->format
== MESA_FORMAT_X8_Z24
) !=
372 (dst_mt
->format
== MESA_FORMAT_X8_Z24
)) {
376 if (!brw
->format_supported_as_render_target
[dst_mt
->format
])
379 /* Source clipping shouldn't be necessary, since copytexsubimage (in
380 * src/mesa/main/teximage.c) calls _mesa_clip_copytexsubimage() which
383 * Destination clipping shouldn't be necessary since the restrictions on
384 * glCopyTexSubImage prevent the user from specifying a destination rectangle
385 * that falls outside the bounds of the destination texture.
386 * See error_check_subtexture_dimensions().
389 int srcY1
= srcY0
+ height
;
390 int srcX1
= srcX0
+ width
;
391 int dstX1
= dstX0
+ width
;
392 int dstY1
= dstY0
+ height
;
394 /* Account for the fact that in the system framebuffer, the origin is at
397 bool mirror_y
= false;
398 if (_mesa_is_winsys_fbo(ctx
->ReadBuffer
)) {
399 GLint tmp
= src_rb
->Height
- srcY0
;
400 srcY0
= src_rb
->Height
- srcY1
;
405 brw_blorp_blit_miptrees(brw
,
406 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
407 dst_mt
, dst_image
->Level
, dst_image
->Face
+ slice
,
408 srcX0
, srcY0
, srcX1
, srcY1
,
409 dstX0
, dstY0
, dstX1
, dstY1
,
410 GL_NEAREST
, false, mirror_y
);
412 /* If we're copying to a packed depth stencil texture and the source
413 * framebuffer has separate stencil, we need to also copy the stencil data
416 src_rb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
417 if (_mesa_get_format_bits(dst_image
->TexFormat
, GL_STENCIL_BITS
) > 0 &&
419 src_irb
= intel_renderbuffer(src_rb
);
420 src_mt
= src_irb
->mt
;
422 if (src_mt
->stencil_mt
)
423 src_mt
= src_mt
->stencil_mt
;
424 if (dst_mt
->stencil_mt
)
425 dst_mt
= dst_mt
->stencil_mt
;
427 if (src_mt
!= dst_mt
) {
428 brw_blorp_blit_miptrees(brw
,
429 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
430 dst_mt
, dst_image
->Level
,
431 dst_image
->Face
+ slice
,
432 srcX0
, srcY0
, srcX1
, srcY1
,
433 dstX0
, dstY0
, dstX1
, dstY1
,
434 GL_NEAREST
, false, mirror_y
);
443 brw_blorp_framebuffer(struct brw_context
*brw
,
444 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
445 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
446 GLbitfield mask
, GLenum filter
)
448 /* BLORP is not supported before Gen6. */
449 if (brw
->gen
< 6 || brw
->gen
>= 8)
452 static GLbitfield buffer_bits
[] = {
455 GL_STENCIL_BUFFER_BIT
,
458 for (unsigned int i
= 0; i
< ARRAY_SIZE(buffer_bits
); ++i
) {
459 if ((mask
& buffer_bits
[i
]) &&
461 srcX0
, srcY0
, srcX1
, srcY1
,
462 dstX0
, dstY0
, dstX1
, dstY1
,
463 filter
, buffer_bits
[i
])) {
464 mask
&= ~buffer_bits
[i
];
473 * Enum to specify the order of arguments in a sampler message
475 enum sampler_message_arg
477 SAMPLER_MESSAGE_ARG_U_FLOAT
,
478 SAMPLER_MESSAGE_ARG_V_FLOAT
,
479 SAMPLER_MESSAGE_ARG_U_INT
,
480 SAMPLER_MESSAGE_ARG_V_INT
,
481 SAMPLER_MESSAGE_ARG_SI_INT
,
482 SAMPLER_MESSAGE_ARG_MCS_INT
,
483 SAMPLER_MESSAGE_ARG_ZERO_INT
,
487 * Generator for WM programs used in BLORP blits.
489 * The bulk of the work done by the WM program is to wrap and unwrap the
490 * coordinate transformations used by the hardware to store surfaces in
491 * memory. The hardware transforms a pixel location (X, Y, S) (where S is the
492 * sample index for a multisampled surface) to a memory offset by the
493 * following formulas:
495 * offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
496 * (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
498 * For a single-sampled surface, or for a multisampled surface using
499 * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
502 * encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
503 * decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
504 * encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
505 * decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
507 * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
508 * embeds the sample number into bit 1 of the X and Y coordinates:
510 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
511 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
512 * Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
513 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
514 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
515 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
516 * S = (Y & 0b10) | (X & 0b10) >> 1
518 * For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
519 * embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
522 * encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
523 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
524 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
525 * decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
526 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
527 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
528 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
530 * For X tiling, tile() combines together the low-order bits of the X and Y
531 * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
532 * bytes wide and 8 rows high:
534 * tile(x_tiled, X, Y, S) = A
535 * where A = tile_num << 12 | offset
536 * tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
537 * offset = (Y' & 0b111) << 9
538 * | (X & 0b111111111)
540 * Y' = Y + S * qpitch
541 * detile(x_tiled, A) = (X, Y, S)
545 * Y' = (tile_num / tile_pitch) << 3
546 * | (A & 0b111000000000) >> 9
547 * X' = (tile_num % tile_pitch) << 9
548 * | (A & 0b111111111)
550 * (In all tiling formulas, cpp is the number of bytes occupied by a single
551 * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
552 * to fill the width of the surface, and qpitch is the spacing (in rows)
553 * between array slices).
555 * For Y tiling, tile() combines together the low-order bits of the X and Y
556 * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
557 * bytes wide and 32 rows high:
559 * tile(y_tiled, X, Y, S) = A
560 * where A = tile_num << 12 | offset
561 * tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
562 * offset = (X' & 0b1110000) << 5
563 * | (Y' & 0b11111) << 4
566 * Y' = Y + S * qpitch
567 * detile(y_tiled, A) = (X, Y, S)
571 * Y' = (tile_num / tile_pitch) << 5
572 * | (A & 0b111110000) >> 4
573 * X' = (tile_num % tile_pitch) << 7
574 * | (A & 0b111000000000) >> 5
577 * For W tiling, tile() combines together the low-order bits of the X and Y
578 * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
579 * bytes wide and 64 rows high (note that W tiling is only used for stencil
580 * buffers, which always have cpp = 1 and S=0):
582 * tile(w_tiled, X, Y, S) = A
583 * where A = tile_num << 12 | offset
584 * tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
585 * offset = (X' & 0b111000) << 6
586 * | (Y' & 0b111100) << 3
587 * | (X' & 0b100) << 2
593 * Y' = Y + S * qpitch
594 * detile(w_tiled, A) = (X, Y, S)
595 * where X = X' / cpp = X'
596 * Y = Y' % qpitch = Y'
598 * Y' = (tile_num / tile_pitch) << 6
599 * | (A & 0b111100000) >> 3
600 * | (A & 0b1000) >> 2
602 * X' = (tile_num % tile_pitch) << 6
603 * | (A & 0b111000000000) >> 6
604 * | (A & 0b10000) >> 2
608 * Finally, for a non-tiled surface, tile() simply combines together the X and
609 * Y coordinates in the natural way:
611 * tile(untiled, X, Y, S) = A
612 * where A = Y * pitch + X'
614 * Y' = Y + S * qpitch
615 * detile(untiled, A) = (X, Y, S)
622 * (In these formulas, pitch is the number of bytes occupied by a single row
625 class brw_blorp_blit_program
: public brw_blorp_eu_emitter
628 brw_blorp_blit_program(struct brw_context
*brw
,
629 const brw_blorp_blit_prog_key
*key
);
631 const GLuint
*compile(struct brw_context
*brw
, GLuint
*program_size
,
632 FILE *dump_file
= stdout
);
634 brw_blorp_prog_data prog_data
;
638 void alloc_push_const_regs(int base_reg
);
639 void compute_frag_coords();
640 void translate_tiling(bool old_tiled_w
, bool new_tiled_w
);
641 void encode_msaa(unsigned num_samples
, intel_msaa_layout layout
);
642 void decode_msaa(unsigned num_samples
, intel_msaa_layout layout
);
643 void translate_dst_to_src();
644 void clamp_tex_coords(struct brw_reg regX
, struct brw_reg regY
,
645 struct brw_reg clampX0
, struct brw_reg clampY0
,
646 struct brw_reg clampX1
, struct brw_reg clampY1
);
647 void single_to_blend();
648 void manual_blend_average(unsigned num_samples
);
649 void manual_blend_bilinear(unsigned num_samples
);
650 void sample(struct brw_reg dst
);
651 void texel_fetch(struct brw_reg dst
);
653 void texture_lookup(struct brw_reg dst
, enum opcode op
,
654 const sampler_message_arg
*args
, int num_args
);
655 void render_target_write();
657 void emit_lrp(const struct brw_reg
&dst
,
658 const struct brw_reg
&src1
,
659 const struct brw_reg
&src2
,
660 const struct brw_reg
&src3
);
663 * Base-2 logarithm of the maximum number of samples that can be blended.
665 static const unsigned LOG2_MAX_BLEND_SAMPLES
= 3;
667 struct brw_context
*brw
;
668 const brw_blorp_blit_prog_key
*key
;
670 /* Thread dispatch header */
673 /* Pixel X/Y coordinates (always in R1). */
677 struct brw_reg dst_x0
;
678 struct brw_reg dst_x1
;
679 struct brw_reg dst_y0
;
680 struct brw_reg dst_y1
;
681 /* Top right coordinates of the rectangular grid used for scaled blitting */
682 struct brw_reg rect_grid_x1
;
683 struct brw_reg rect_grid_y1
;
685 struct brw_reg multiplier
;
686 struct brw_reg offset
;
687 } x_transform
, y_transform
;
689 /* Data read from texture (4 vec16's per array element) */
690 struct brw_reg texture_data
[LOG2_MAX_BLEND_SAMPLES
+ 1];
692 /* Auxiliary storage for the contents of the MCS surface.
694 * Since the sampler always returns 8 registers worth of data, this is 8
695 * registers wide, even though we only use the first 2 registers of it.
697 struct brw_reg mcs_data
;
699 /* X coordinates. We have two of them so that we can perform coordinate
700 * transformations easily.
702 struct brw_reg x_coords
[2];
704 /* Y coordinates. We have two of them so that we can perform coordinate
705 * transformations easily.
707 struct brw_reg y_coords
[2];
709 /* X, Y coordinates of the pixel from which we need to fetch the specific
710 * sample. These are used for multisample scaled blitting.
712 struct brw_reg x_sample_coords
;
713 struct brw_reg y_sample_coords
;
715 /* Fractional parts of the x and y coordinates, used as bilinear interpolation coefficients */
716 struct brw_reg x_frac
;
717 struct brw_reg y_frac
;
719 /* Which element of x_coords and y_coords is currently in use.
723 /* True if, at the point in the program currently being compiled, the
724 * sample index is known to be zero.
728 /* Register storing the sample index when s_is_zero is false. */
729 struct brw_reg sample_index
;
735 /* MRF used for sampling and render target writes */
739 brw_blorp_blit_program::brw_blorp_blit_program(
740 struct brw_context
*brw
,
741 const brw_blorp_blit_prog_key
*key
)
742 : brw_blorp_eu_emitter(brw
),
749 brw_blorp_blit_program::compile(struct brw_context
*brw
,
750 GLuint
*program_size
,
754 if (key
->dst_tiled_w
&& key
->rt_samples
> 0) {
755 /* If the destination image is W tiled and multisampled, then the thread
756 * must be dispatched once per sample, not once per pixel. This is
757 * necessary because after conversion between W and Y tiling, there's no
758 * guarantee that all samples corresponding to a single pixel will still
761 assert(key
->persample_msaa_dispatch
);
765 /* We are blending, which means we won't have an opportunity to
766 * translate the tiling and sample count for the texture surface. So
767 * the surface state for the texture must be configured with the correct
768 * tiling and sample count.
770 assert(!key
->src_tiled_w
);
771 assert(key
->tex_samples
== key
->src_samples
);
772 assert(key
->tex_layout
== key
->src_layout
);
773 assert(key
->tex_samples
> 0);
776 if (key
->persample_msaa_dispatch
) {
777 /* It only makes sense to do persample dispatch if the render target is
778 * configured as multisampled.
780 assert(key
->rt_samples
> 0);
783 /* Make sure layout is consistent with sample count */
784 assert((key
->tex_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
785 (key
->tex_samples
== 0));
786 assert((key
->rt_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
787 (key
->rt_samples
== 0));
788 assert((key
->src_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
789 (key
->src_samples
== 0));
790 assert((key
->dst_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
791 (key
->dst_samples
== 0));
793 /* Set up prog_data */
794 memset(&prog_data
, 0, sizeof(prog_data
));
795 prog_data
.persample_msaa_dispatch
= key
->persample_msaa_dispatch
;
798 compute_frag_coords();
800 /* Render target and texture hardware don't support W tiling. */
801 const bool rt_tiled_w
= false;
802 const bool tex_tiled_w
= false;
804 /* The address that data will be written to is determined by the
805 * coordinates supplied to the WM thread and the tiling and sample count of
806 * the render target, according to the formula:
808 * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
810 * If the actual tiling and sample count of the destination surface are not
811 * the same as the configuration of the render target, then these
812 * coordinates are wrong and we have to adjust them to compensate for the
815 if (rt_tiled_w
!= key
->dst_tiled_w
||
816 key
->rt_samples
!= key
->dst_samples
||
817 key
->rt_layout
!= key
->dst_layout
) {
818 encode_msaa(key
->rt_samples
, key
->rt_layout
);
819 /* Now (X, Y, S) = detile(rt_tiling, offset) */
820 translate_tiling(rt_tiled_w
, key
->dst_tiled_w
);
821 /* Now (X, Y, S) = detile(dst_tiling, offset) */
822 decode_msaa(key
->dst_samples
, key
->dst_layout
);
825 /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
827 * That is: X, Y and S now contain the true coordinates and sample index of
828 * the data that the WM thread should output.
830 * If we need to kill pixels that are outside the destination rectangle,
831 * now is the time to do it.
835 emit_kill_if_outside_rect(x_coords
[xy_coord_index
],
836 y_coords
[xy_coord_index
],
837 dst_x0
, dst_x1
, dst_y0
, dst_y1
);
839 /* Next, apply a translation to obtain coordinates in the source image. */
840 translate_dst_to_src();
842 /* If the source image is not multisampled, then we want to fetch sample
843 * number 0, because that's the only sample there is.
845 if (key
->src_samples
== 0)
848 /* X, Y, and S are now the coordinates of the pixel in the source image
849 * that we want to texture from. Exception: if we are blending, then S is
850 * irrelevant, because we are going to fetch all samples.
852 if (key
->blend
&& !key
->blit_scaled
) {
854 /* Gen6 hardware an automatically blend using the SAMPLE message */
856 sample(texture_data
[0]);
858 /* Gen7+ hardware doesn't automaticaly blend. */
859 manual_blend_average(key
->src_samples
);
861 } else if(key
->blend
&& key
->blit_scaled
) {
862 manual_blend_bilinear(key
->src_samples
);
864 /* We aren't blending, which means we just want to fetch a single sample
865 * from the source surface. The address that we want to fetch from is
866 * related to the X, Y and S values according to the formula:
868 * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
870 * If the actual tiling and sample count of the source surface are not
871 * the same as the configuration of the texture, then we need to adjust
872 * the coordinates to compensate for the difference.
874 if ((tex_tiled_w
!= key
->src_tiled_w
||
875 key
->tex_samples
!= key
->src_samples
||
876 key
->tex_layout
!= key
->src_layout
) &&
877 !key
->bilinear_filter
) {
878 encode_msaa(key
->src_samples
, key
->src_layout
);
879 /* Now (X, Y, S) = detile(src_tiling, offset) */
880 translate_tiling(key
->src_tiled_w
, tex_tiled_w
);
881 /* Now (X, Y, S) = detile(tex_tiling, offset) */
882 decode_msaa(key
->tex_samples
, key
->tex_layout
);
885 if (key
->bilinear_filter
) {
886 sample(texture_data
[0]);
889 /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
891 * In other words: X, Y, and S now contain values which, when passed to
892 * the texturing unit, will cause data to be read from the correct
893 * memory location. So we can fetch the texel now.
895 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
897 texel_fetch(texture_data
[0]);
901 /* Finally, write the fetched (or blended) value to the render target and
902 * terminate the thread.
904 render_target_write();
906 return get_program(program_size
, dump_file
);
910 brw_blorp_blit_program::alloc_push_const_regs(int base_reg
)
912 #define CONST_LOC(name) offsetof(brw_blorp_wm_push_constants, name)
913 #define ALLOC_REG(name, type) \
915 retype(brw_vec1_reg(BRW_GENERAL_REGISTER_FILE, \
916 base_reg + CONST_LOC(name) / 32, \
917 (CONST_LOC(name) % 32) / 4), type)
919 ALLOC_REG(dst_x0
, BRW_REGISTER_TYPE_UD
);
920 ALLOC_REG(dst_x1
, BRW_REGISTER_TYPE_UD
);
921 ALLOC_REG(dst_y0
, BRW_REGISTER_TYPE_UD
);
922 ALLOC_REG(dst_y1
, BRW_REGISTER_TYPE_UD
);
923 ALLOC_REG(rect_grid_x1
, BRW_REGISTER_TYPE_F
);
924 ALLOC_REG(rect_grid_y1
, BRW_REGISTER_TYPE_F
);
925 ALLOC_REG(x_transform
.multiplier
, BRW_REGISTER_TYPE_F
);
926 ALLOC_REG(x_transform
.offset
, BRW_REGISTER_TYPE_F
);
927 ALLOC_REG(y_transform
.multiplier
, BRW_REGISTER_TYPE_F
);
928 ALLOC_REG(y_transform
.offset
, BRW_REGISTER_TYPE_F
);
934 brw_blorp_blit_program::alloc_regs()
937 this->R0
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
938 this->R1
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
939 prog_data
.first_curbe_grf
= reg
;
940 alloc_push_const_regs(reg
);
941 reg
+= BRW_BLORP_NUM_PUSH_CONST_REGS
;
942 for (unsigned i
= 0; i
< ARRAY_SIZE(texture_data
); ++i
) {
943 this->texture_data
[i
] =
944 retype(vec16(brw_vec8_grf(reg
, 0)), key
->texture_data_type
);
948 retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
); reg
+= 8;
950 for (int i
= 0; i
< 2; ++i
) {
952 = retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
955 = retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
959 if (key
->blit_scaled
&& key
->blend
) {
960 this->x_sample_coords
= brw_vec8_grf(reg
, 0);
962 this->y_sample_coords
= brw_vec8_grf(reg
, 0);
964 this->x_frac
= brw_vec8_grf(reg
, 0);
966 this->y_frac
= brw_vec8_grf(reg
, 0);
970 this->xy_coord_index
= 0;
972 = retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
974 this->t1
= retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
976 this->t2
= retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
979 /* Make sure we didn't run out of registers */
980 assert(reg
<= GEN7_MRF_HACK_START
);
983 this->base_mrf
= mrf
;
986 /* In the code that follows, X and Y can be used to quickly refer to the
987 * active elements of x_coords and y_coords, and Xp and Yp ("X prime" and "Y
988 * prime") to the inactive elements.
990 * S can be used to quickly refer to sample_index.
992 #define X x_coords[xy_coord_index]
993 #define Y y_coords[xy_coord_index]
994 #define Xp x_coords[!xy_coord_index]
995 #define Yp y_coords[!xy_coord_index]
996 #define S sample_index
998 /* Quickly swap the roles of (X, Y) and (Xp, Yp). Saves us from having to do
999 * MOVs to transfor (Xp, Yp) to (X, Y) after a coordinate transformation.
1001 #define SWAP_XY_AND_XPYP() xy_coord_index = !xy_coord_index;
1004 * Emit code to compute the X and Y coordinates of the pixels being rendered
1005 * by this WM invocation.
1007 * Assuming the render target is set up for Y tiling, these (X, Y) values are
1008 * related to the address offset where outputs will be written by the formula:
1010 * (X, Y, S) = decode_msaa(detile(offset)).
1012 * (See brw_blorp_blit_program).
1015 brw_blorp_blit_program::compute_frag_coords()
1017 /* R1.2[15:0] = X coordinate of upper left pixel of subspan 0 (pixel 0)
1018 * R1.3[15:0] = X coordinate of upper left pixel of subspan 1 (pixel 4)
1019 * R1.4[15:0] = X coordinate of upper left pixel of subspan 2 (pixel 8)
1020 * R1.5[15:0] = X coordinate of upper left pixel of subspan 3 (pixel 12)
1022 * Pixels within a subspan are laid out in this arrangement:
1026 * So, to compute the coordinates of each pixel, we need to read every 2nd
1027 * 16-bit value (vstride=2) from R1, starting at the 4th 16-bit value
1028 * (suboffset=4), and duplicate each value 4 times (hstride=0, width=4).
1029 * In other words, the data we want to access is R1.4<2;4,0>UW.
1031 * Then, we need to add the repeating sequence (0, 1, 0, 1, ...) to the
1032 * result, since pixels n+1 and n+3 are in the right half of the subspan.
1034 brw_ADD(&func
, vec16(retype(X
, BRW_REGISTER_TYPE_UW
)),
1035 stride(suboffset(R1
, 4), 2, 4, 0), brw_imm_v(0x10101010));
1037 /* Similarly, Y coordinates for subspans come from R1.2[31:16] through
1038 * R1.5[31:16], so to get pixel Y coordinates we need to start at the 5th
1039 * 16-bit value instead of the 4th (R1.5<2;4,0>UW instead of
1042 * And we need to add the repeating sequence (0, 0, 1, 1, ...), since
1043 * pixels n+2 and n+3 are in the bottom half of the subspan.
1045 brw_ADD(&func
, vec16(retype(Y
, BRW_REGISTER_TYPE_UW
)),
1046 stride(suboffset(R1
, 5), 2, 4, 0), brw_imm_v(0x11001100));
1048 /* Move the coordinates to UD registers. */
1049 emit_mov(vec16(Xp
), retype(X
, BRW_REGISTER_TYPE_UW
));
1050 emit_mov(vec16(Yp
), retype(Y
, BRW_REGISTER_TYPE_UW
));
1053 if (key
->persample_msaa_dispatch
) {
1054 switch (key
->rt_samples
) {
1056 /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 4.
1057 * Therefore, subspan 0 will represent sample 0, subspan 1 will
1058 * represent sample 1, and so on.
1060 * So we need to populate S with the sequence (0, 0, 0, 0, 1, 1, 1,
1061 * 1, 2, 2, 2, 2, 3, 3, 3, 3). The easiest way to do this is to
1062 * populate a temporary variable with the sequence (0, 1, 2, 3), and
1063 * then copy from it using vstride=1, width=4, hstride=0.
1065 struct brw_reg t1_uw1
= retype(t1
, BRW_REGISTER_TYPE_UW
);
1066 emit_mov(vec16(t1_uw1
), brw_imm_v(0x3210));
1067 /* Move to UD sample_index register. */
1068 emit_mov_8(S
, stride(t1_uw1
, 1, 4, 0));
1069 emit_mov_8(offset(S
, 1), suboffset(stride(t1_uw1
, 1, 4, 0), 2));
1073 /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 8.
1074 * Therefore, subspan 0 will represent sample N (where N is 0 or 4),
1075 * subspan 1 will represent sample 1, and so on. We can find the
1076 * value of N by looking at R0.0 bits 7:6 ("Starting Sample Pair
1077 * Index") and multiplying by two (since samples are always delivered
1078 * in pairs). That is, we compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 &
1081 * Then we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1, 2,
1082 * 2, 2, 2, 3, 3, 3, 3), which we compute by populating a temporary
1083 * variable with the sequence (0, 1, 2, 3), and then reading from it
1084 * using vstride=1, width=4, hstride=0.
1086 struct brw_reg t1_ud1
= vec1(retype(t1
, BRW_REGISTER_TYPE_UD
));
1087 struct brw_reg t2_uw1
= retype(t2
, BRW_REGISTER_TYPE_UW
);
1088 struct brw_reg r0_ud1
= vec1(retype(R0
, BRW_REGISTER_TYPE_UD
));
1089 emit_and(t1_ud1
, r0_ud1
, brw_imm_ud(0xc0));
1090 brw_SHR(&func
, t1_ud1
, t1_ud1
, brw_imm_ud(5));
1091 emit_mov(vec16(t2_uw1
), brw_imm_v(0x3210));
1092 brw_ADD(&func
, vec16(S
), retype(t1_ud1
, BRW_REGISTER_TYPE_UW
),
1093 stride(t2_uw1
, 1, 4, 0));
1094 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
1095 brw_ADD(&func
, offset(S
, 1),
1096 retype(t1_ud1
, BRW_REGISTER_TYPE_UW
),
1097 suboffset(stride(t2_uw1
, 1, 4, 0), 2));
1098 brw_set_compression_control(&func
, BRW_COMPRESSION_COMPRESSED
);
1102 assert(!"Unrecognized sample count in "
1103 "brw_blorp_blit_program::compute_frag_coords()");
1108 /* Either the destination surface is single-sampled, or the WM will be
1109 * run in MSDISPMODE_PERPIXEL (which causes a single fragment dispatch
1110 * per pixel). In either case, it's not meaningful to compute a sample
1111 * value. Just set it to 0.
1118 * Emit code to compensate for the difference between Y and W tiling.
1120 * This code modifies the X and Y coordinates according to the formula:
1122 * (X', Y', S') = detile(new_tiling, tile(old_tiling, X, Y, S))
1124 * (See brw_blorp_blit_program).
1126 * It can only translate between W and Y tiling, so new_tiling and old_tiling
1127 * are booleans where true represents W tiling and false represents Y tiling.
1130 brw_blorp_blit_program::translate_tiling(bool old_tiled_w
, bool new_tiled_w
)
1132 if (old_tiled_w
== new_tiled_w
)
1135 /* In the code that follows, we can safely assume that S = 0, because W
1136 * tiling formats always use IMS layout.
1141 /* Given X and Y coordinates that describe an address using Y tiling,
1142 * translate to the X and Y coordinates that describe the same address
1145 * If we break down the low order bits of X and Y, using a
1146 * single letter to represent each low-order bit:
1148 * X = A << 7 | 0bBCDEFGH
1149 * Y = J << 5 | 0bKLMNP (1)
1151 * Then we can apply the Y tiling formula to see the memory offset being
1154 * offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
1156 * If we apply the W detiling formula to this memory location, that the
1157 * corresponding X' and Y' coordinates are:
1159 * X' = A << 6 | 0bBCDPFH (3)
1160 * Y' = J << 6 | 0bKLMNEG
1162 * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
1163 * we need to make the following computation:
1165 * X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
1166 * Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
1168 emit_and(t1
, X
, brw_imm_uw(0xfff4)); /* X & ~0b1011 */
1169 brw_SHR(&func
, t1
, t1
, brw_imm_uw(1)); /* (X & ~0b1011) >> 1 */
1170 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1171 brw_SHL(&func
, t2
, t2
, brw_imm_uw(2)); /* (Y & 0b1) << 2 */
1172 brw_OR(&func
, t1
, t1
, t2
); /* (X & ~0b1011) >> 1 | (Y & 0b1) << 2 */
1173 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1174 brw_OR(&func
, Xp
, t1
, t2
);
1175 emit_and(t1
, Y
, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1176 brw_SHL(&func
, t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1177 emit_and(t2
, X
, brw_imm_uw(8)); /* X & 0b1000 */
1178 brw_SHR(&func
, t2
, t2
, brw_imm_uw(2)); /* (X & 0b1000) >> 2 */
1179 brw_OR(&func
, t1
, t1
, t2
); /* (Y & ~0b1) << 1 | (X & 0b1000) >> 2 */
1180 emit_and(t2
, X
, brw_imm_uw(2)); /* X & 0b10 */
1181 brw_SHR(&func
, t2
, t2
, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1182 brw_OR(&func
, Yp
, t1
, t2
);
1185 /* Applying the same logic as above, but in reverse, we obtain the
1188 * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
1189 * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
1191 emit_and(t1
, X
, brw_imm_uw(0xfffa)); /* X & ~0b101 */
1192 brw_SHL(&func
, t1
, t1
, brw_imm_uw(1)); /* (X & ~0b101) << 1 */
1193 emit_and(t2
, Y
, brw_imm_uw(2)); /* Y & 0b10 */
1194 brw_SHL(&func
, t2
, t2
, brw_imm_uw(2)); /* (Y & 0b10) << 2 */
1195 brw_OR(&func
, t1
, t1
, t2
); /* (X & ~0b101) << 1 | (Y & 0b10) << 2 */
1196 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1197 brw_SHL(&func
, t2
, t2
, brw_imm_uw(1)); /* (Y & 0b1) << 1 */
1198 brw_OR(&func
, t1
, t1
, t2
); /* (X & ~0b101) << 1 | (Y & 0b10) << 2
1200 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1201 brw_OR(&func
, Xp
, t1
, t2
);
1202 emit_and(t1
, Y
, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1203 brw_SHR(&func
, t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1204 emit_and(t2
, X
, brw_imm_uw(4)); /* X & 0b100 */
1205 brw_SHR(&func
, t2
, t2
, brw_imm_uw(2)); /* (X & 0b100) >> 2 */
1206 brw_OR(&func
, Yp
, t1
, t2
);
1212 * Emit code to compensate for the difference between MSAA and non-MSAA
1215 * This code modifies the X and Y coordinates according to the formula:
1217 * (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
1219 * (See brw_blorp_blit_program).
1222 brw_blorp_blit_program::encode_msaa(unsigned num_samples
,
1223 intel_msaa_layout layout
)
1226 case INTEL_MSAA_LAYOUT_NONE
:
1227 /* No translation necessary, and S should already be zero. */
1230 case INTEL_MSAA_LAYOUT_CMS
:
1231 /* We can't compensate for compressed layout since at this point in the
1232 * program we haven't read from the MCS buffer.
1234 assert(!"Bad layout in encode_msaa");
1236 case INTEL_MSAA_LAYOUT_UMS
:
1237 /* No translation necessary. */
1239 case INTEL_MSAA_LAYOUT_IMS
:
1240 switch (num_samples
) {
1242 /* encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
1243 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
1244 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1246 emit_and(t1
, X
, brw_imm_uw(0xfffe)); /* X & ~0b1 */
1248 emit_and(t2
, S
, brw_imm_uw(1)); /* S & 0b1 */
1249 brw_OR(&func
, t1
, t1
, t2
); /* (X & ~0b1) | (S & 0b1) */
1251 brw_SHL(&func
, t1
, t1
, brw_imm_uw(1)); /* (X & ~0b1) << 1
1253 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1254 brw_OR(&func
, Xp
, t1
, t2
);
1255 emit_and(t1
, Y
, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1256 brw_SHL(&func
, t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1258 emit_and(t2
, S
, brw_imm_uw(2)); /* S & 0b10 */
1259 brw_OR(&func
, t1
, t1
, t2
); /* (Y & ~0b1) << 1 | (S & 0b10) */
1261 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1262 brw_OR(&func
, Yp
, t1
, t2
);
1265 /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
1266 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
1268 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1270 emit_and(t1
, X
, brw_imm_uw(0xfffe)); /* X & ~0b1 */
1271 brw_SHL(&func
, t1
, t1
, brw_imm_uw(2)); /* (X & ~0b1) << 2 */
1273 emit_and(t2
, S
, brw_imm_uw(4)); /* S & 0b100 */
1274 brw_OR(&func
, t1
, t1
, t2
); /* (X & ~0b1) << 2 | (S & 0b100) */
1275 emit_and(t2
, S
, brw_imm_uw(1)); /* S & 0b1 */
1276 brw_SHL(&func
, t2
, t2
, brw_imm_uw(1)); /* (S & 0b1) << 1 */
1277 brw_OR(&func
, t1
, t1
, t2
); /* (X & ~0b1) << 2 | (S & 0b100)
1280 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1281 brw_OR(&func
, Xp
, t1
, t2
);
1282 emit_and(t1
, Y
, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1283 brw_SHL(&func
, t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1285 emit_and(t2
, S
, brw_imm_uw(2)); /* S & 0b10 */
1286 brw_OR(&func
, t1
, t1
, t2
); /* (Y & ~0b1) << 1 | (S & 0b10) */
1288 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1289 brw_OR(&func
, Yp
, t1
, t2
);
1299 * Emit code to compensate for the difference between MSAA and non-MSAA
1302 * This code modifies the X and Y coordinates according to the formula:
1304 * (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
1306 * (See brw_blorp_blit_program).
1309 brw_blorp_blit_program::decode_msaa(unsigned num_samples
,
1310 intel_msaa_layout layout
)
1313 case INTEL_MSAA_LAYOUT_NONE
:
1314 /* No translation necessary, and S should already be zero. */
1317 case INTEL_MSAA_LAYOUT_CMS
:
1318 /* We can't compensate for compressed layout since at this point in the
1319 * program we don't have access to the MCS buffer.
1321 assert(!"Bad layout in encode_msaa");
1323 case INTEL_MSAA_LAYOUT_UMS
:
1324 /* No translation necessary. */
1326 case INTEL_MSAA_LAYOUT_IMS
:
1328 switch (num_samples
) {
1330 /* decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
1331 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
1332 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1333 * S = (Y & 0b10) | (X & 0b10) >> 1
1335 emit_and(t1
, X
, brw_imm_uw(0xfffc)); /* X & ~0b11 */
1336 brw_SHR(&func
, t1
, t1
, brw_imm_uw(1)); /* (X & ~0b11) >> 1 */
1337 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1338 brw_OR(&func
, Xp
, t1
, t2
);
1339 emit_and(t1
, Y
, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1340 brw_SHR(&func
, t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1341 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1342 brw_OR(&func
, Yp
, t1
, t2
);
1343 emit_and(t1
, Y
, brw_imm_uw(2)); /* Y & 0b10 */
1344 emit_and(t2
, X
, brw_imm_uw(2)); /* X & 0b10 */
1345 brw_SHR(&func
, t2
, t2
, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1346 brw_OR(&func
, S
, t1
, t2
);
1349 /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
1350 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
1351 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1352 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
1354 emit_and(t1
, X
, brw_imm_uw(0xfff8)); /* X & ~0b111 */
1355 brw_SHR(&func
, t1
, t1
, brw_imm_uw(2)); /* (X & ~0b111) >> 2 */
1356 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1357 brw_OR(&func
, Xp
, t1
, t2
);
1358 emit_and(t1
, Y
, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1359 brw_SHR(&func
, t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1360 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1361 brw_OR(&func
, Yp
, t1
, t2
);
1362 emit_and(t1
, X
, brw_imm_uw(4)); /* X & 0b100 */
1363 emit_and(t2
, Y
, brw_imm_uw(2)); /* Y & 0b10 */
1364 brw_OR(&func
, t1
, t1
, t2
); /* (X & 0b100) | (Y & 0b10) */
1365 emit_and(t2
, X
, brw_imm_uw(2)); /* X & 0b10 */
1366 brw_SHR(&func
, t2
, t2
, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1367 brw_OR(&func
, S
, t1
, t2
);
1377 * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
1381 brw_blorp_blit_program::translate_dst_to_src()
1383 struct brw_reg X_f
= retype(X
, BRW_REGISTER_TYPE_F
);
1384 struct brw_reg Y_f
= retype(Y
, BRW_REGISTER_TYPE_F
);
1385 struct brw_reg Xp_f
= retype(Xp
, BRW_REGISTER_TYPE_F
);
1386 struct brw_reg Yp_f
= retype(Yp
, BRW_REGISTER_TYPE_F
);
1388 /* Move the UD coordinates to float registers. */
1391 /* Scale and offset */
1392 brw_MUL(&func
, X_f
, Xp_f
, x_transform
.multiplier
);
1393 brw_MUL(&func
, Y_f
, Yp_f
, y_transform
.multiplier
);
1394 brw_ADD(&func
, X_f
, X_f
, x_transform
.offset
);
1395 brw_ADD(&func
, Y_f
, Y_f
, y_transform
.offset
);
1396 if (key
->blit_scaled
&& key
->blend
) {
1397 /* Translate coordinates to lay out the samples in a rectangular grid
1398 * roughly corresponding to sample locations.
1400 brw_MUL(&func
, X_f
, X_f
, brw_imm_f(key
->x_scale
));
1401 brw_MUL(&func
, Y_f
, Y_f
, brw_imm_f(key
->y_scale
));
1402 /* Adjust coordinates so that integers represent pixel centers rather
1405 brw_ADD(&func
, X_f
, X_f
, brw_imm_f(-0.5));
1406 brw_ADD(&func
, Y_f
, Y_f
, brw_imm_f(-0.5));
1408 /* Clamp the X, Y texture coordinates to properly handle the sampling of
1409 * texels on texture edges.
1411 clamp_tex_coords(X_f
, Y_f
,
1412 brw_imm_f(0.0), brw_imm_f(0.0),
1413 rect_grid_x1
, rect_grid_y1
);
1415 /* Store the fractional parts to be used as bilinear interpolation
1418 brw_FRC(&func
, x_frac
, X_f
);
1419 brw_FRC(&func
, y_frac
, Y_f
);
1421 /* Round the float coordinates down to nearest integer */
1422 brw_RNDD(&func
, Xp_f
, X_f
);
1423 brw_RNDD(&func
, Yp_f
, Y_f
);
1424 brw_MUL(&func
, X_f
, Xp_f
, brw_imm_f(1 / key
->x_scale
));
1425 brw_MUL(&func
, Y_f
, Yp_f
, brw_imm_f(1 / key
->y_scale
));
1427 } else if (!key
->bilinear_filter
) {
1428 /* Round the float coordinates down to nearest integer by moving to
1438 brw_blorp_blit_program::clamp_tex_coords(struct brw_reg regX
,
1439 struct brw_reg regY
,
1440 struct brw_reg clampX0
,
1441 struct brw_reg clampY0
,
1442 struct brw_reg clampX1
,
1443 struct brw_reg clampY1
)
1445 emit_cond_mov(regX
, clampX0
, BRW_CONDITIONAL_L
, regX
, clampX0
);
1446 emit_cond_mov(regX
, clampX1
, BRW_CONDITIONAL_G
, regX
, clampX1
);
1447 emit_cond_mov(regY
, clampY0
, BRW_CONDITIONAL_L
, regY
, clampY0
);
1448 emit_cond_mov(regY
, clampY1
, BRW_CONDITIONAL_G
, regY
, clampY1
);
1452 * Emit code to transform the X and Y coordinates as needed for blending
1453 * together the different samples in an MSAA texture.
1456 brw_blorp_blit_program::single_to_blend()
1458 /* When looking up samples in an MSAA texture using the SAMPLE message,
1459 * Gen6 requires the texture coordinates to be odd integers (so that they
1460 * correspond to the center of a 2x2 block representing the four samples
1461 * that maxe up a pixel). So we need to multiply our X and Y coordinates
1462 * each by 2 and then add 1.
1464 brw_SHL(&func
, t1
, X
, brw_imm_w(1));
1465 brw_SHL(&func
, t2
, Y
, brw_imm_w(1));
1466 brw_ADD(&func
, Xp
, t1
, brw_imm_w(1));
1467 brw_ADD(&func
, Yp
, t2
, brw_imm_w(1));
1473 * Count the number of trailing 1 bits in the given value. For example:
1475 * count_trailing_one_bits(0) == 0
1476 * count_trailing_one_bits(7) == 3
1477 * count_trailing_one_bits(11) == 2
1479 inline int count_trailing_one_bits(unsigned value
)
1481 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
1482 return __builtin_ctz(~value
);
1484 return _mesa_bitcount(value
& ~(value
+ 1));
1490 brw_blorp_blit_program::manual_blend_average(unsigned num_samples
)
1492 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
1495 /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
1497 * result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
1499 * This ensures that when all samples have the same value, no numerical
1500 * precision is lost, since each addition operation always adds two equal
1501 * values, and summing two equal floating point values does not lose
1504 * We perform this computation by treating the texture_data array as a
1505 * stack and performing the following operations:
1507 * - push sample 0 onto stack
1508 * - push sample 1 onto stack
1509 * - add top two stack entries
1510 * - push sample 2 onto stack
1511 * - push sample 3 onto stack
1512 * - add top two stack entries
1513 * - add top two stack entries
1514 * - divide top stack entry by 4
1516 * Note that after pushing sample i onto the stack, the number of add
1517 * operations we do is equal to the number of trailing 1 bits in i. This
1518 * works provided the total number of samples is a power of two, which it
1519 * always is for i965.
1521 * For integer formats, we replace the add operations with average
1522 * operations and skip the final division.
1524 unsigned stack_depth
= 0;
1525 for (unsigned i
= 0; i
< num_samples
; ++i
) {
1526 assert(stack_depth
== _mesa_bitcount(i
)); /* Loop invariant */
1528 /* Push sample i onto the stack */
1529 assert(stack_depth
< ARRAY_SIZE(texture_data
));
1534 emit_mov(vec16(S
), brw_imm_ud(i
));
1536 texel_fetch(texture_data
[stack_depth
++]);
1538 if (i
== 0 && key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
) {
1539 /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
1540 * suggests an optimization:
1542 * "A simple optimization with probable large return in
1543 * performance is to compare the MCS value to zero (indicating
1544 * all samples are on sample slice 0), and sample only from
1545 * sample slice 0 using ld2dss if MCS is zero."
1547 * Note that in the case where the MCS value is zero, sampling from
1548 * sample slice 0 using ld2dss and sampling from sample 0 using
1549 * ld2dms are equivalent (since all samples are on sample slice 0).
1550 * Since we have already sampled from sample 0, all we need to do is
1551 * skip the remaining fetches and averaging if MCS is zero.
1553 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_NZ
,
1554 mcs_data
, brw_imm_ud(0));
1555 brw_IF(&func
, BRW_EXECUTE_16
);
1558 /* Do count_trailing_one_bits(i) times */
1559 for (int j
= count_trailing_one_bits(i
); j
-- > 0; ) {
1560 assert(stack_depth
>= 2);
1563 /* TODO: should use a smaller loop bound for non_RGBA formats */
1564 for (int k
= 0; k
< 4; ++k
) {
1565 emit_combine(key
->texture_data_type
== BRW_REGISTER_TYPE_F
?
1566 BRW_OPCODE_ADD
: BRW_OPCODE_AVG
,
1567 offset(texture_data
[stack_depth
- 1], 2*k
),
1568 offset(vec8(texture_data
[stack_depth
- 1]), 2*k
),
1569 offset(vec8(texture_data
[stack_depth
]), 2*k
));
1574 /* We should have just 1 sample on the stack now. */
1575 assert(stack_depth
== 1);
1577 if (key
->texture_data_type
== BRW_REGISTER_TYPE_F
) {
1578 /* Scale the result down by a factor of num_samples */
1579 /* TODO: should use a smaller loop bound for non-RGBA formats */
1580 for (int j
= 0; j
< 4; ++j
) {
1581 brw_MUL(&func
, offset(texture_data
[0], 2*j
),
1582 offset(vec8(texture_data
[0]), 2*j
),
1583 brw_imm_f(1.0/num_samples
));
1587 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
1592 brw_blorp_blit_program::emit_lrp(const struct brw_reg
&dst
,
1593 const struct brw_reg
&src1
,
1594 const struct brw_reg
&src2
,
1595 const struct brw_reg
&src3
)
1597 brw_set_access_mode(&func
, BRW_ALIGN_16
);
1598 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
1599 brw_LRP(&func
, dst
, src1
, src2
, src3
);
1600 brw_set_compression_control(&func
, BRW_COMPRESSION_2NDHALF
);
1601 brw_LRP(&func
, sechalf(dst
), sechalf(src1
), sechalf(src2
), sechalf(src3
));
1602 brw_set_compression_control(&func
, BRW_COMPRESSION_COMPRESSED
);
1603 brw_set_access_mode(&func
, BRW_ALIGN_1
);
1607 brw_blorp_blit_program::manual_blend_bilinear(unsigned num_samples
)
1609 /* We do this computation by performing the following operations:
1611 * In case of 4x, 8x MSAA:
1612 * - Compute the pixel coordinates and sample numbers (a, b, c, d)
1613 * which are later used for interpolation
1614 * - linearly interpolate samples a and b in X
1615 * - linearly interpolate samples c and d in X
1616 * - linearly interpolate the results of last two operations in Y
1618 * result = lrp(lrp(a + b) + lrp(c + d))
1620 struct brw_reg Xp_f
= retype(Xp
, BRW_REGISTER_TYPE_F
);
1621 struct brw_reg Yp_f
= retype(Yp
, BRW_REGISTER_TYPE_F
);
1622 struct brw_reg t1_f
= retype(t1
, BRW_REGISTER_TYPE_F
);
1623 struct brw_reg t2_f
= retype(t2
, BRW_REGISTER_TYPE_F
);
1625 for (unsigned i
= 0; i
< 4; ++i
) {
1626 assert(i
< ARRAY_SIZE(texture_data
));
1629 /* Compute pixel coordinates */
1630 brw_ADD(&func
, vec16(x_sample_coords
), Xp_f
,
1631 brw_imm_f((float)(i
& 0x1) * (1.0 / key
->x_scale
)));
1632 brw_ADD(&func
, vec16(y_sample_coords
), Yp_f
,
1633 brw_imm_f((float)((i
>> 1) & 0x1) * (1.0 / key
->y_scale
)));
1634 emit_mov(vec16(X
), x_sample_coords
);
1635 emit_mov(vec16(Y
), y_sample_coords
);
1637 /* The MCS value we fetch has to match up with the pixel that we're
1638 * sampling from. Since we sample from different pixels in each
1639 * iteration of this "for" loop, the call to mcs_fetch() should be
1640 * here inside the loop after computing the pixel coordinates.
1642 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
1645 /* Compute sample index and map the sample index to a sample number.
1646 * Sample index layout shows the numbering of slots in a rectangular
1647 * grid of samples with in a pixel. Sample number layout shows the
1648 * rectangular grid of samples roughly corresponding to the real sample
1649 * locations with in a pixel.
1650 * In case of 4x MSAA, layout of sample indices matches the layout of
1658 * In case of 8x MSAA the two layouts don't match.
1659 * sample index layout : --------- sample number layout : ---------
1660 * | 0 | 1 | | 5 | 2 |
1661 * --------- ---------
1662 * | 2 | 3 | | 4 | 6 |
1663 * --------- ---------
1664 * | 4 | 5 | | 0 | 3 |
1665 * --------- ---------
1666 * | 6 | 7 | | 7 | 1 |
1667 * --------- ---------
1669 brw_FRC(&func
, vec16(t1_f
), x_sample_coords
);
1670 brw_FRC(&func
, vec16(t2_f
), y_sample_coords
);
1671 brw_MUL(&func
, vec16(t1_f
), t1_f
, brw_imm_f(key
->x_scale
));
1672 brw_MUL(&func
, vec16(t2_f
), t2_f
, brw_imm_f(key
->x_scale
* key
->y_scale
));
1673 brw_ADD(&func
, vec16(t1_f
), t1_f
, t2_f
);
1674 emit_mov(vec16(S
), t1_f
);
1676 if (num_samples
== 8) {
1677 /* Map the sample index to a sample number */
1678 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_L
,
1680 brw_IF(&func
, BRW_EXECUTE_16
);
1682 emit_mov(vec16(t2
), brw_imm_d(5));
1683 emit_if_eq_mov(S
, 1, vec16(t2
), 2);
1684 emit_if_eq_mov(S
, 2, vec16(t2
), 4);
1685 emit_if_eq_mov(S
, 3, vec16(t2
), 6);
1689 emit_mov(vec16(t2
), brw_imm_d(0));
1690 emit_if_eq_mov(S
, 5, vec16(t2
), 3);
1691 emit_if_eq_mov(S
, 6, vec16(t2
), 7);
1692 emit_if_eq_mov(S
, 7, vec16(t2
), 1);
1695 emit_mov(vec16(S
), t2
);
1697 texel_fetch(texture_data
[i
]);
1700 #define SAMPLE(x, y) offset(texture_data[x], y)
1701 for (int index
= 3; index
> 0; ) {
1702 /* Since we're doing SIMD16, 4 color channels fits in to 8 registers.
1703 * Counter value of 8 in 'for' loop below is used to interpolate all
1704 * the color components.
1706 for (int k
= 0; k
< 8; k
+= 2)
1707 emit_lrp(vec8(SAMPLE(index
- 1, k
)),
1709 vec8(SAMPLE(index
, k
)),
1710 vec8(SAMPLE(index
- 1, k
)));
1713 for (int k
= 0; k
< 8; k
+= 2)
1714 emit_lrp(vec8(SAMPLE(0, k
)),
1717 vec8(SAMPLE(0, k
)));
1722 * Emit code to look up a value in the texture using the SAMPLE message (which
1723 * does blending of MSAA surfaces).
1726 brw_blorp_blit_program::sample(struct brw_reg dst
)
1728 static const sampler_message_arg args
[2] = {
1729 SAMPLER_MESSAGE_ARG_U_FLOAT
,
1730 SAMPLER_MESSAGE_ARG_V_FLOAT
1733 texture_lookup(dst
, SHADER_OPCODE_TEX
, args
, ARRAY_SIZE(args
));
1737 * Emit code to look up a value in the texture using the SAMPLE_LD message
1738 * (which does a simple texel fetch).
1741 brw_blorp_blit_program::texel_fetch(struct brw_reg dst
)
1743 static const sampler_message_arg gen6_args
[5] = {
1744 SAMPLER_MESSAGE_ARG_U_INT
,
1745 SAMPLER_MESSAGE_ARG_V_INT
,
1746 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* R */
1747 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* LOD */
1748 SAMPLER_MESSAGE_ARG_SI_INT
1750 static const sampler_message_arg gen7_ld_args
[3] = {
1751 SAMPLER_MESSAGE_ARG_U_INT
,
1752 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* LOD */
1753 SAMPLER_MESSAGE_ARG_V_INT
1755 static const sampler_message_arg gen7_ld2dss_args
[3] = {
1756 SAMPLER_MESSAGE_ARG_SI_INT
,
1757 SAMPLER_MESSAGE_ARG_U_INT
,
1758 SAMPLER_MESSAGE_ARG_V_INT
1760 static const sampler_message_arg gen7_ld2dms_args
[4] = {
1761 SAMPLER_MESSAGE_ARG_SI_INT
,
1762 SAMPLER_MESSAGE_ARG_MCS_INT
,
1763 SAMPLER_MESSAGE_ARG_U_INT
,
1764 SAMPLER_MESSAGE_ARG_V_INT
1769 texture_lookup(dst
, SHADER_OPCODE_TXF
, gen6_args
, s_is_zero
? 2 : 5);
1772 switch (key
->tex_layout
) {
1773 case INTEL_MSAA_LAYOUT_IMS
:
1774 /* From the Ivy Bridge PRM, Vol4 Part1 p72 (Multisampled Surface Storage
1777 * If this field is MSFMT_DEPTH_STENCIL
1778 * [a.k.a. INTEL_MSAA_LAYOUT_IMS], the only sampling engine
1779 * messages allowed are "ld2dms", "resinfo", and "sampleinfo".
1781 * So fall through to emit the same message as we use for
1782 * INTEL_MSAA_LAYOUT_CMS.
1784 case INTEL_MSAA_LAYOUT_CMS
:
1785 texture_lookup(dst
, SHADER_OPCODE_TXF_CMS
,
1786 gen7_ld2dms_args
, ARRAY_SIZE(gen7_ld2dms_args
));
1788 case INTEL_MSAA_LAYOUT_UMS
:
1789 texture_lookup(dst
, SHADER_OPCODE_TXF_UMS
,
1790 gen7_ld2dss_args
, ARRAY_SIZE(gen7_ld2dss_args
));
1792 case INTEL_MSAA_LAYOUT_NONE
:
1794 texture_lookup(dst
, SHADER_OPCODE_TXF
, gen7_ld_args
,
1795 ARRAY_SIZE(gen7_ld_args
));
1800 assert(!"Should not get here.");
1806 brw_blorp_blit_program::mcs_fetch()
1808 static const sampler_message_arg gen7_ld_mcs_args
[2] = {
1809 SAMPLER_MESSAGE_ARG_U_INT
,
1810 SAMPLER_MESSAGE_ARG_V_INT
1812 texture_lookup(vec16(mcs_data
), SHADER_OPCODE_TXF_MCS
,
1813 gen7_ld_mcs_args
, ARRAY_SIZE(gen7_ld_mcs_args
));
1817 brw_blorp_blit_program::texture_lookup(struct brw_reg dst
,
1819 const sampler_message_arg
*args
,
1822 struct brw_reg mrf
=
1823 retype(vec16(brw_message_reg(base_mrf
)), BRW_REGISTER_TYPE_UD
);
1824 for (int arg
= 0; arg
< num_args
; ++arg
) {
1825 switch (args
[arg
]) {
1826 case SAMPLER_MESSAGE_ARG_U_FLOAT
:
1827 if (key
->bilinear_filter
)
1828 emit_mov(retype(mrf
, BRW_REGISTER_TYPE_F
),
1829 retype(X
, BRW_REGISTER_TYPE_F
));
1831 emit_mov(retype(mrf
, BRW_REGISTER_TYPE_F
), X
);
1833 case SAMPLER_MESSAGE_ARG_V_FLOAT
:
1834 if (key
->bilinear_filter
)
1835 emit_mov(retype(mrf
, BRW_REGISTER_TYPE_F
),
1836 retype(Y
, BRW_REGISTER_TYPE_F
));
1838 emit_mov(retype(mrf
, BRW_REGISTER_TYPE_F
), Y
);
1840 case SAMPLER_MESSAGE_ARG_U_INT
:
1843 case SAMPLER_MESSAGE_ARG_V_INT
:
1846 case SAMPLER_MESSAGE_ARG_SI_INT
:
1847 /* Note: on Gen7, this code may be reached with s_is_zero==true
1848 * because in Gen7's ld2dss message, the sample index is the first
1849 * argument. When this happens, we need to move a 0 into the
1850 * appropriate message register.
1853 emit_mov(mrf
, brw_imm_ud(0));
1857 case SAMPLER_MESSAGE_ARG_MCS_INT
:
1858 switch (key
->tex_layout
) {
1859 case INTEL_MSAA_LAYOUT_CMS
:
1860 emit_mov(mrf
, mcs_data
);
1862 case INTEL_MSAA_LAYOUT_IMS
:
1863 /* When sampling from an IMS surface, MCS data is not relevant,
1864 * and the hardware ignores it. So don't bother populating it.
1868 /* We shouldn't be trying to send MCS data with any other
1871 assert (!"Unsupported layout for MCS data");
1875 case SAMPLER_MESSAGE_ARG_ZERO_INT
:
1876 emit_mov(mrf
, brw_imm_ud(0));
1882 emit_texture_lookup(retype(dst
, BRW_REGISTER_TYPE_UW
) /* dest */,
1885 mrf
.nr
- base_mrf
/* msg_length */);
1893 #undef SWAP_XY_AND_XPYP
1896 brw_blorp_blit_program::render_target_write()
1898 struct brw_reg mrf_rt_write
=
1899 retype(vec16(brw_message_reg(base_mrf
)), key
->texture_data_type
);
1902 /* If we may have killed pixels, then we need to send R0 and R1 in a header
1903 * so that the render target knows which pixels we killed.
1905 bool use_header
= key
->use_kill
;
1907 /* Copy R0/1 to MRF */
1908 emit_mov(retype(mrf_rt_write
, BRW_REGISTER_TYPE_UD
),
1909 retype(R0
, BRW_REGISTER_TYPE_UD
));
1913 /* Copy texture data to MRFs */
1914 for (int i
= 0; i
< 4; ++i
) {
1915 /* E.g. mov(16) m2.0<1>:f r2.0<8;8,1>:f { Align1, H1 } */
1916 emit_mov(offset(mrf_rt_write
, mrf_offset
),
1917 offset(vec8(texture_data
[0]), 2*i
));
1921 /* Now write to the render target and terminate the thread */
1922 emit_render_target_write(
1925 mrf_offset
/* msg_length. TODO: Should be smaller for non-RGBA formats. */,
1931 brw_blorp_coord_transform_params::setup(GLfloat src0
, GLfloat src1
,
1932 GLfloat dst0
, GLfloat dst1
,
1935 float scale
= (src1
- src0
) / (dst1
- dst0
);
1937 /* When not mirroring a coordinate (say, X), we need:
1938 * src_x - src_x0 = (dst_x - dst_x0 + 0.5) * scale
1940 * src_x = src_x0 + (dst_x - dst_x0 + 0.5) * scale
1942 * blorp program uses "round toward zero" to convert the
1943 * transformed floating point coordinates to integer coordinates,
1944 * whereas the behaviour we actually want is "round to nearest",
1945 * so 0.5 provides the necessary correction.
1948 offset
= src0
+ (-dst0
+ 0.5) * scale
;
1950 /* When mirroring X we need:
1951 * src_x - src_x0 = dst_x1 - dst_x - 0.5
1953 * src_x = src_x0 + (dst_x1 -dst_x - 0.5) * scale
1955 multiplier
= -scale
;
1956 offset
= src0
+ (dst1
- 0.5) * scale
;
1962 * Determine which MSAA layout the GPU pipeline should be configured for,
1963 * based on the chip generation, the number of samples, and the true layout of
1964 * the image in memory.
1966 inline intel_msaa_layout
1967 compute_msaa_layout_for_pipeline(struct brw_context
*brw
, unsigned num_samples
,
1968 intel_msaa_layout true_layout
)
1970 if (num_samples
<= 1) {
1971 /* When configuring the GPU for non-MSAA, we can still accommodate IMS
1972 * format buffers, by transforming coordinates appropriately.
1974 assert(true_layout
== INTEL_MSAA_LAYOUT_NONE
||
1975 true_layout
== INTEL_MSAA_LAYOUT_IMS
);
1976 return INTEL_MSAA_LAYOUT_NONE
;
1978 assert(true_layout
!= INTEL_MSAA_LAYOUT_NONE
);
1981 /* Prior to Gen7, all MSAA surfaces use IMS layout. */
1982 if (brw
->gen
== 6) {
1983 assert(true_layout
== INTEL_MSAA_LAYOUT_IMS
);
1990 brw_blorp_blit_params::brw_blorp_blit_params(struct brw_context
*brw
,
1991 struct intel_mipmap_tree
*src_mt
,
1992 unsigned src_level
, unsigned src_layer
,
1993 struct intel_mipmap_tree
*dst_mt
,
1994 unsigned dst_level
, unsigned dst_layer
,
1995 GLfloat src_x0
, GLfloat src_y0
,
1996 GLfloat src_x1
, GLfloat src_y1
,
1997 GLfloat dst_x0
, GLfloat dst_y0
,
1998 GLfloat dst_x1
, GLfloat dst_y1
,
2000 bool mirror_x
, bool mirror_y
)
2002 struct gl_context
*ctx
= &brw
->ctx
;
2003 const struct gl_framebuffer
*read_fb
= ctx
->ReadBuffer
;
2005 src
.set(brw
, src_mt
, src_level
, src_layer
, false);
2006 dst
.set(brw
, dst_mt
, dst_level
, dst_layer
, true);
2008 /* Even though we do multisample resolves at the time of the blit, OpenGL
2009 * specification defines them as if they happen at the time of rendering,
2010 * which means that the type of averaging we do during the resolve should
2011 * only depend on the source format; the destination format should be
2012 * ignored. But, specification doesn't seem to be strict about it.
2014 * It has been observed that mulitisample resolves produce slightly better
2015 * looking images when averaging is done using destination format. NVIDIA's
2016 * proprietary OpenGL driver also follow this approach. So, we choose to
2017 * follow it in our driver.
2019 * When multisampling, if the source and destination formats are equal
2020 * (aside from the color space), we choose to blit in sRGB space to get
2021 * this higher quality image.
2023 if (src
.num_samples
> 1 &&
2024 _mesa_get_format_color_encoding(dst_mt
->format
) == GL_SRGB
&&
2025 _mesa_get_srgb_format_linear(src_mt
->format
) ==
2026 _mesa_get_srgb_format_linear(dst_mt
->format
)) {
2027 dst
.brw_surfaceformat
= brw_format_for_mesa_format(dst_mt
->format
);
2028 src
.brw_surfaceformat
= dst
.brw_surfaceformat
;
2031 /* When doing a multisample resolve of a GL_LUMINANCE32F or GL_INTENSITY32F
2032 * texture, the above code configures the source format for L32_FLOAT or
2033 * I32_FLOAT, and the destination format for R32_FLOAT. On Sandy Bridge,
2034 * the SAMPLE message appears to handle multisampled L32_FLOAT and
2035 * I32_FLOAT textures incorrectly, resulting in blocky artifacts. So work
2036 * around the problem by using a source format of R32_FLOAT. This
2037 * shouldn't affect rendering correctness, since the destination format is
2038 * R32_FLOAT, so only the contents of the red channel matters.
2040 if (brw
->gen
== 6 && src
.num_samples
> 1 && dst
.num_samples
<= 1 &&
2041 src_mt
->format
== dst_mt
->format
&&
2042 dst
.brw_surfaceformat
== BRW_SURFACEFORMAT_R32_FLOAT
) {
2043 src
.brw_surfaceformat
= dst
.brw_surfaceformat
;
2047 memset(&wm_prog_key
, 0, sizeof(wm_prog_key
));
2049 /* texture_data_type indicates the register type that should be used to
2050 * manipulate texture data.
2052 switch (_mesa_get_format_datatype(src_mt
->format
)) {
2053 case GL_UNSIGNED_NORMALIZED
:
2054 case GL_SIGNED_NORMALIZED
:
2056 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_F
;
2058 case GL_UNSIGNED_INT
:
2059 if (src_mt
->format
== MESA_FORMAT_S8
) {
2060 /* We process stencil as though it's an unsigned normalized color */
2061 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_F
;
2063 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_UD
;
2067 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_D
;
2070 assert(!"Unrecognized blorp format");
2075 /* Gen7's rendering hardware only supports the IMS layout for depth and
2076 * stencil render targets. Blorp always maps its destination surface as
2077 * a color render target (even if it's actually a depth or stencil
2078 * buffer). So if the destination is IMS, we'll have to map it as a
2079 * single-sampled texture and interleave the samples ourselves.
2081 if (dst_mt
->msaa_layout
== INTEL_MSAA_LAYOUT_IMS
)
2082 dst
.num_samples
= 0;
2085 if (dst
.map_stencil_as_y_tiled
&& dst
.num_samples
> 1) {
2086 /* If the destination surface is a W-tiled multisampled stencil buffer
2087 * that we're mapping as Y tiled, then we need to arrange for the WM
2088 * program to run once per sample rather than once per pixel, because
2089 * the memory layout of related samples doesn't match between W and Y
2092 wm_prog_key
.persample_msaa_dispatch
= true;
2095 if (src
.num_samples
> 0 && dst
.num_samples
> 1) {
2096 /* We are blitting from a multisample buffer to a multisample buffer, so
2097 * we must preserve samples within a pixel. This means we have to
2098 * arrange for the WM program to run once per sample rather than once
2101 wm_prog_key
.persample_msaa_dispatch
= true;
2104 /* Scaled blitting or not. */
2105 wm_prog_key
.blit_scaled
=
2106 ((dst_x1
- dst_x0
) == (src_x1
- src_x0
) &&
2107 (dst_y1
- dst_y0
) == (src_y1
- src_y0
)) ? false : true;
2109 /* Scaling factors used for bilinear filtering in multisample scaled
2112 wm_prog_key
.x_scale
= 2.0;
2113 wm_prog_key
.y_scale
= src_mt
->num_samples
/ 2.0;
2115 if (filter
== GL_LINEAR
&& src
.num_samples
<= 1 && dst
.num_samples
<= 1)
2116 wm_prog_key
.bilinear_filter
= true;
2118 GLenum base_format
= _mesa_get_format_base_format(src_mt
->format
);
2119 if (base_format
!= GL_DEPTH_COMPONENT
&& /* TODO: what about depth/stencil? */
2120 base_format
!= GL_STENCIL_INDEX
&&
2121 src_mt
->num_samples
> 1 && dst_mt
->num_samples
<= 1) {
2122 /* We are downsampling a color buffer, so blend. */
2123 wm_prog_key
.blend
= true;
2126 /* src_samples and dst_samples are the true sample counts */
2127 wm_prog_key
.src_samples
= src_mt
->num_samples
;
2128 wm_prog_key
.dst_samples
= dst_mt
->num_samples
;
2130 /* tex_samples and rt_samples are the sample counts that are set up in
2133 wm_prog_key
.tex_samples
= src
.num_samples
;
2134 wm_prog_key
.rt_samples
= dst
.num_samples
;
2136 /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
2137 * use to access the source and destination surfaces.
2139 wm_prog_key
.tex_layout
=
2140 compute_msaa_layout_for_pipeline(brw
, src
.num_samples
, src
.msaa_layout
);
2141 wm_prog_key
.rt_layout
=
2142 compute_msaa_layout_for_pipeline(brw
, dst
.num_samples
, dst
.msaa_layout
);
2144 /* src_layout and dst_layout indicate the true MSAA layout used by src and
2147 wm_prog_key
.src_layout
= src_mt
->msaa_layout
;
2148 wm_prog_key
.dst_layout
= dst_mt
->msaa_layout
;
2150 wm_prog_key
.src_tiled_w
= src
.map_stencil_as_y_tiled
;
2151 wm_prog_key
.dst_tiled_w
= dst
.map_stencil_as_y_tiled
;
2152 x0
= wm_push_consts
.dst_x0
= dst_x0
;
2153 y0
= wm_push_consts
.dst_y0
= dst_y0
;
2154 x1
= wm_push_consts
.dst_x1
= dst_x1
;
2155 y1
= wm_push_consts
.dst_y1
= dst_y1
;
2156 wm_push_consts
.rect_grid_x1
= read_fb
->Width
* wm_prog_key
.x_scale
- 1.0;
2157 wm_push_consts
.rect_grid_y1
= read_fb
->Height
* wm_prog_key
.y_scale
- 1.0;
2159 wm_push_consts
.x_transform
.setup(src_x0
, src_x1
, dst_x0
, dst_x1
, mirror_x
);
2160 wm_push_consts
.y_transform
.setup(src_y0
, src_y1
, dst_y0
, dst_y1
, mirror_y
);
2162 if (dst
.num_samples
<= 1 && dst_mt
->num_samples
> 1) {
2163 /* We must expand the rectangle we send through the rendering pipeline,
2164 * to account for the fact that we are mapping the destination region as
2165 * single-sampled when it is in fact multisampled. We must also align
2166 * it to a multiple of the multisampling pattern, because the
2167 * differences between multisampled and single-sampled surface formats
2168 * will mean that pixels are scrambled within the multisampling pattern.
2169 * TODO: what if this makes the coordinates too large?
2171 * Note: this only works if the destination surface uses the IMS layout.
2172 * If it's UMS, then we have no choice but to set up the rendering
2173 * pipeline as multisampled.
2175 assert(dst_mt
->msaa_layout
== INTEL_MSAA_LAYOUT_IMS
);
2176 switch (dst_mt
->num_samples
) {
2178 x0
= ROUND_DOWN_TO(x0
* 2, 4);
2179 y0
= ROUND_DOWN_TO(y0
* 2, 4);
2180 x1
= ALIGN(x1
* 2, 4);
2181 y1
= ALIGN(y1
* 2, 4);
2184 x0
= ROUND_DOWN_TO(x0
* 4, 8);
2185 y0
= ROUND_DOWN_TO(y0
* 2, 4);
2186 x1
= ALIGN(x1
* 4, 8);
2187 y1
= ALIGN(y1
* 2, 4);
2190 assert(!"Unrecognized sample count in brw_blorp_blit_params ctor");
2193 wm_prog_key
.use_kill
= true;
2196 if (dst
.map_stencil_as_y_tiled
) {
2197 /* We must modify the rectangle we send through the rendering pipeline
2198 * (and the size and x/y offset of the destination surface), to account
2199 * for the fact that we are mapping it as Y-tiled when it is in fact
2202 * Both Y tiling and W tiling can be understood as organizations of
2203 * 32-byte sub-tiles; within each 32-byte sub-tile, the layout of pixels
2204 * is different, but the layout of the 32-byte sub-tiles within the 4k
2205 * tile is the same (8 sub-tiles across by 16 sub-tiles down, in
2206 * column-major order). In Y tiling, the sub-tiles are 16 bytes wide
2207 * and 2 rows high; in W tiling, they are 8 bytes wide and 4 rows high.
2209 * Therefore, to account for the layout differences within the 32-byte
2210 * sub-tiles, we must expand the rectangle so the X coordinates of its
2211 * edges are multiples of 8 (the W sub-tile width), and its Y
2212 * coordinates of its edges are multiples of 4 (the W sub-tile height).
2213 * Then we need to scale the X and Y coordinates of the rectangle to
2214 * account for the differences in aspect ratio between the Y and W
2215 * sub-tiles. We need to modify the layer width and height similarly.
2217 * A correction needs to be applied when MSAA is in use: since
2218 * INTEL_MSAA_LAYOUT_IMS uses an interleaving pattern whose height is 4,
2219 * we need to align the Y coordinates to multiples of 8, so that when
2220 * they are divided by two they are still multiples of 4.
2222 * Note: Since the x/y offset of the surface will be applied using the
2223 * SURFACE_STATE command packet, it will be invisible to the swizzling
2224 * code in the shader; therefore it needs to be in a multiple of the
2225 * 32-byte sub-tile size. Fortunately it is, since the sub-tile is 8
2226 * pixels wide and 4 pixels high (when viewed as a W-tiled stencil
2227 * buffer), and the miplevel alignment used for stencil buffers is 8
2228 * pixels horizontally and either 4 or 8 pixels vertically (see
2229 * intel_horizontal_texture_alignment_unit() and
2230 * intel_vertical_texture_alignment_unit()).
2232 * Note: Also, since the SURFACE_STATE command packet can only apply
2233 * offsets that are multiples of 4 pixels horizontally and 2 pixels
2234 * vertically, it is important that the offsets will be multiples of
2235 * these sizes after they are converted into Y-tiled coordinates.
2236 * Fortunately they will be, since we know from above that the offsets
2237 * are a multiple of the 32-byte sub-tile size, and in Y-tiled
2238 * coordinates the sub-tile is 16 pixels wide and 2 pixels high.
2240 * TODO: what if this makes the coordinates (or the texture size) too
2243 const unsigned x_align
= 8, y_align
= dst
.num_samples
!= 0 ? 8 : 4;
2244 x0
= ROUND_DOWN_TO(x0
, x_align
) * 2;
2245 y0
= ROUND_DOWN_TO(y0
, y_align
) / 2;
2246 x1
= ALIGN(x1
, x_align
) * 2;
2247 y1
= ALIGN(y1
, y_align
) / 2;
2248 dst
.width
= ALIGN(dst
.width
, x_align
) * 2;
2249 dst
.height
= ALIGN(dst
.height
, y_align
) / 2;
2252 wm_prog_key
.use_kill
= true;
2255 if (src
.map_stencil_as_y_tiled
) {
2256 /* We must modify the size and x/y offset of the source surface to
2257 * account for the fact that we are mapping it as Y-tiled when it is in
2260 * See the comments above concerning x/y offset alignment for the
2261 * destination surface.
2263 * TODO: what if this makes the texture size too large?
2265 const unsigned x_align
= 8, y_align
= src
.num_samples
!= 0 ? 8 : 4;
2266 src
.width
= ALIGN(src
.width
, x_align
) * 2;
2267 src
.height
= ALIGN(src
.height
, y_align
) / 2;
2274 brw_blorp_blit_params::get_wm_prog(struct brw_context
*brw
,
2275 brw_blorp_prog_data
**prog_data
) const
2277 uint32_t prog_offset
= 0;
2278 if (!brw_search_cache(&brw
->cache
, BRW_BLORP_BLIT_PROG
,
2279 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
2280 &prog_offset
, prog_data
)) {
2281 brw_blorp_blit_program
prog(brw
, &this->wm_prog_key
);
2282 GLuint program_size
;
2283 const GLuint
*program
= prog
.compile(brw
, &program_size
, stdout
);
2284 brw_upload_cache(&brw
->cache
, BRW_BLORP_BLIT_PROG
,
2285 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
2286 program
, program_size
,
2287 &prog
.prog_data
, sizeof(prog
.prog_data
),
2288 &prog_offset
, prog_data
);
2294 brw_blorp_blit_test_compile(struct brw_context
*brw
,
2295 const brw_blorp_blit_prog_key
*key
,
2298 GLuint program_size
;
2299 brw_blorp_blit_program
prog(brw
, key
);
2300 INTEL_DEBUG
|= DEBUG_BLORP
;
2301 prog
.compile(brw
, &program_size
, out
);