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 "glsl/ralloc.h"
30 #include "intel_fbo.h"
32 #include "brw_blorp.h"
33 #include "brw_context.h"
35 #include "brw_state.h"
37 #define FILE_DEBUG_FLAG DEBUG_BLORP
40 * Helper function for handling mirror image blits.
42 * If coord0 > coord1, swap them and invert the "mirror" boolean.
45 fixup_mirroring(bool &mirror
, GLfloat
&coord0
, GLfloat
&coord1
)
47 if (coord0
> coord1
) {
57 * Adjust {src,dst}_x{0,1} to account for clipping and scissoring of
58 * destination coordinates.
60 * Return true if there is still blitting to do, false if all pixels got
61 * rejected by the clip and/or scissor.
63 * For clarity, the nomenclature of this function assumes we are clipping and
64 * scissoring the X coordinate; the exact same logic applies for Y
67 * Note: this function may also be used to account for clipping of source
68 * coordinates, by swapping the roles of src and dst.
71 clip_or_scissor(bool mirror
, GLfloat
&src_x0
, GLfloat
&src_x1
, GLfloat
&dst_x0
,
72 GLfloat
&dst_x1
, GLfloat fb_xmin
, GLfloat fb_xmax
)
74 float scale
= (float) (src_x1
- src_x0
) / (dst_x1
- dst_x0
);
75 /* If we are going to scissor everything away, stop. */
76 if (!(fb_xmin
< fb_xmax
&&
83 /* Clip the destination rectangle, and keep track of how many pixels we
84 * clipped off of the left and right sides of it.
86 GLint pixels_clipped_left
= 0;
87 GLint pixels_clipped_right
= 0;
88 if (dst_x0
< fb_xmin
) {
89 pixels_clipped_left
= fb_xmin
- dst_x0
;
92 if (fb_xmax
< dst_x1
) {
93 pixels_clipped_right
= dst_x1
- fb_xmax
;
97 /* If we are mirrored, then before applying pixels_clipped_{left,right} to
98 * the source coordinates, we need to flip them to account for the
102 GLint tmp
= pixels_clipped_left
;
103 pixels_clipped_left
= pixels_clipped_right
;
104 pixels_clipped_right
= tmp
;
107 /* Adjust the source rectangle to remove the pixels corresponding to those
108 * that were clipped/scissored out of the destination rectangle.
110 src_x0
+= pixels_clipped_left
* scale
;
111 src_x1
-= pixels_clipped_right
* scale
;
117 static struct intel_mipmap_tree
*
118 find_miptree(GLbitfield buffer_bit
, struct intel_renderbuffer
*irb
)
120 struct intel_mipmap_tree
*mt
= irb
->mt
;
121 if (buffer_bit
== GL_STENCIL_BUFFER_BIT
&& mt
->stencil_mt
)
128 * Note: if the src (or dst) is a 2D multisample array texture on Gen7+ using
129 * INTEL_MSAA_LAYOUT_UMS or INTEL_MSAA_LAYOUT_CMS, src_layer (dst_layer) is
130 * the physical layer holding sample 0. So, for example, if
131 * src_mt->num_samples == 4, then logical layer n corresponds to src_layer ==
135 brw_blorp_blit_miptrees(struct brw_context
*brw
,
136 struct intel_mipmap_tree
*src_mt
,
137 unsigned src_level
, unsigned src_layer
,
138 struct intel_mipmap_tree
*dst_mt
,
139 unsigned dst_level
, unsigned dst_layer
,
140 float src_x0
, float src_y0
,
141 float src_x1
, float src_y1
,
142 float dst_x0
, float dst_y0
,
143 float dst_x1
, float dst_y1
,
144 GLenum filter
, bool mirror_x
, bool mirror_y
)
146 /* Get ready to blit. This includes depth resolving the src and dst
147 * buffers if necessary. Note: it's not necessary to do a color resolve on
148 * the destination buffer because we use the standard render path to render
149 * to destination color buffers, and the standard render path is
152 intel_miptree_resolve_color(brw
, src_mt
);
153 intel_miptree_slice_resolve_depth(brw
, src_mt
, src_level
, src_layer
);
154 intel_miptree_slice_resolve_depth(brw
, dst_mt
, dst_level
, dst_layer
);
156 DBG("%s from %s mt %p %d %d (%f,%f) (%f,%f)"
157 "to %s mt %p %d %d (%f,%f) (%f,%f) (flip %d,%d)\n",
159 _mesa_get_format_name(src_mt
->format
), src_mt
,
160 src_level
, src_layer
, src_x0
, src_y0
, src_x1
, src_y1
,
161 _mesa_get_format_name(dst_mt
->format
), dst_mt
,
162 dst_level
, dst_layer
, dst_x0
, dst_y0
, dst_x1
, dst_y1
,
165 brw_blorp_blit_params
params(brw
,
166 src_mt
, src_level
, src_layer
,
167 dst_mt
, dst_level
, dst_layer
,
172 filter
, mirror_x
, mirror_y
);
173 brw_blorp_exec(brw
, ¶ms
);
175 intel_miptree_slice_set_needs_hiz_resolve(dst_mt
, dst_level
, dst_layer
);
179 do_blorp_blit(struct brw_context
*brw
, GLbitfield buffer_bit
,
180 struct intel_renderbuffer
*src_irb
,
181 struct intel_renderbuffer
*dst_irb
,
182 GLfloat srcX0
, GLfloat srcY0
, GLfloat srcX1
, GLfloat srcY1
,
183 GLfloat dstX0
, GLfloat dstY0
, GLfloat dstX1
, GLfloat dstY1
,
184 GLenum filter
, bool mirror_x
, bool mirror_y
)
186 /* Find source/dst miptrees */
187 struct intel_mipmap_tree
*src_mt
= find_miptree(buffer_bit
, src_irb
);
188 struct intel_mipmap_tree
*dst_mt
= find_miptree(buffer_bit
, dst_irb
);
191 brw_blorp_blit_miptrees(brw
,
192 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
193 dst_mt
, dst_irb
->mt_level
, dst_irb
->mt_layer
,
194 srcX0
, srcY0
, srcX1
, srcY1
,
195 dstX0
, dstY0
, dstX1
, dstY1
,
196 filter
, mirror_x
, mirror_y
);
198 intel_renderbuffer_set_needs_downsample(dst_irb
);
202 color_formats_match(gl_format src_format
, gl_format dst_format
)
204 gl_format linear_src_format
= _mesa_get_srgb_format_linear(src_format
);
205 gl_format linear_dst_format
= _mesa_get_srgb_format_linear(dst_format
);
207 /* Normally, we require the formats to be equal. However, we also support
208 * blitting from ARGB to XRGB (discarding alpha), and from XRGB to ARGB
209 * (overriding alpha to 1.0 via blending).
211 return linear_src_format
== linear_dst_format
||
212 (linear_src_format
== MESA_FORMAT_XRGB8888
&&
213 linear_dst_format
== MESA_FORMAT_ARGB8888
) ||
214 (linear_src_format
== MESA_FORMAT_ARGB8888
&&
215 linear_dst_format
== MESA_FORMAT_XRGB8888
);
219 formats_match(GLbitfield buffer_bit
, struct intel_renderbuffer
*src_irb
,
220 struct intel_renderbuffer
*dst_irb
)
222 /* Note: don't just check gl_renderbuffer::Format, because in some cases
223 * multiple gl_formats resolve to the same native type in the miptree (for
224 * example MESA_FORMAT_X8_Z24 and MESA_FORMAT_S8_Z24), and we can blit
225 * between those formats.
227 gl_format src_format
= find_miptree(buffer_bit
, src_irb
)->format
;
228 gl_format dst_format
= find_miptree(buffer_bit
, dst_irb
)->format
;
230 return color_formats_match(src_format
, dst_format
);
234 try_blorp_blit(struct brw_context
*brw
,
235 GLfloat srcX0
, GLfloat srcY0
, GLfloat srcX1
, GLfloat srcY1
,
236 GLfloat dstX0
, GLfloat dstY0
, GLfloat dstX1
, GLfloat dstY1
,
237 GLenum filter
, GLbitfield buffer_bit
)
239 struct gl_context
*ctx
= &brw
->ctx
;
241 /* Sync up the state of window system buffers. We need to do this before
242 * we go looking for the buffers.
244 intel_prepare_render(brw
);
246 const struct gl_framebuffer
*read_fb
= ctx
->ReadBuffer
;
247 const struct gl_framebuffer
*draw_fb
= ctx
->DrawBuffer
;
249 /* Detect if the blit needs to be mirrored */
250 bool mirror_x
= false, mirror_y
= false;
251 fixup_mirroring(mirror_x
, srcX0
, srcX1
);
252 fixup_mirroring(mirror_x
, dstX0
, dstX1
);
253 fixup_mirroring(mirror_y
, srcY0
, srcY1
);
254 fixup_mirroring(mirror_y
, dstY0
, dstY1
);
256 /* If the destination rectangle needs to be clipped or scissored, do so.
258 if (!(clip_or_scissor(mirror_x
, srcX0
, srcX1
, dstX0
, dstX1
,
259 draw_fb
->_Xmin
, draw_fb
->_Xmax
) &&
260 clip_or_scissor(mirror_y
, srcY0
, srcY1
, dstY0
, dstY1
,
261 draw_fb
->_Ymin
, draw_fb
->_Ymax
))) {
262 /* Everything got clipped/scissored away, so the blit was successful. */
266 /* If the source rectangle needs to be clipped or scissored, do so. */
267 if (!(clip_or_scissor(mirror_x
, dstX0
, dstX1
, srcX0
, srcX1
,
268 0, read_fb
->Width
) &&
269 clip_or_scissor(mirror_y
, dstY0
, dstY1
, srcY0
, srcY1
,
270 0, read_fb
->Height
))) {
271 /* Everything got clipped/scissored away, so the blit was successful. */
275 /* Account for the fact that in the system framebuffer, the origin is at
278 if (_mesa_is_winsys_fbo(read_fb
)) {
279 GLint tmp
= read_fb
->Height
- srcY0
;
280 srcY0
= read_fb
->Height
- srcY1
;
282 mirror_y
= !mirror_y
;
284 if (_mesa_is_winsys_fbo(draw_fb
)) {
285 GLint tmp
= draw_fb
->Height
- dstY0
;
286 dstY0
= draw_fb
->Height
- dstY1
;
288 mirror_y
= !mirror_y
;
292 struct intel_renderbuffer
*src_irb
;
293 struct intel_renderbuffer
*dst_irb
;
294 switch (buffer_bit
) {
295 case GL_COLOR_BUFFER_BIT
:
296 src_irb
= intel_renderbuffer(read_fb
->_ColorReadBuffer
);
297 for (unsigned i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; ++i
) {
298 dst_irb
= intel_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[i
]);
299 if (dst_irb
&& !formats_match(buffer_bit
, src_irb
, dst_irb
))
302 for (unsigned i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; ++i
) {
303 dst_irb
= intel_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[i
]);
305 do_blorp_blit(brw
, buffer_bit
, src_irb
, dst_irb
, srcX0
, srcY0
,
306 srcX1
, srcY1
, dstX0
, dstY0
, dstX1
, dstY1
,
307 filter
, mirror_x
, mirror_y
);
310 case GL_DEPTH_BUFFER_BIT
:
312 intel_renderbuffer(read_fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
314 intel_renderbuffer(draw_fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
315 if (!formats_match(buffer_bit
, src_irb
, dst_irb
))
317 do_blorp_blit(brw
, buffer_bit
, src_irb
, dst_irb
, srcX0
, srcY0
,
318 srcX1
, srcY1
, dstX0
, dstY0
, dstX1
, dstY1
,
319 filter
, mirror_x
, mirror_y
);
321 case GL_STENCIL_BUFFER_BIT
:
323 intel_renderbuffer(read_fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
325 intel_renderbuffer(draw_fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
326 if (!formats_match(buffer_bit
, src_irb
, dst_irb
))
328 do_blorp_blit(brw
, buffer_bit
, src_irb
, dst_irb
, srcX0
, srcY0
,
329 srcX1
, srcY1
, dstX0
, dstY0
, dstX1
, dstY1
,
330 filter
, mirror_x
, mirror_y
);
340 brw_blorp_copytexsubimage(struct brw_context
*brw
,
341 struct gl_renderbuffer
*src_rb
,
342 struct gl_texture_image
*dst_image
,
344 int srcX0
, int srcY0
,
345 int dstX0
, int dstY0
,
346 int width
, int height
)
348 struct gl_context
*ctx
= &brw
->ctx
;
349 struct intel_renderbuffer
*src_irb
= intel_renderbuffer(src_rb
);
350 struct intel_texture_image
*intel_image
= intel_texture_image(dst_image
);
352 /* Sync up the state of window system buffers. We need to do this before
353 * we go looking at the src renderbuffer's miptree.
355 intel_prepare_render(brw
);
357 struct intel_mipmap_tree
*src_mt
= src_irb
->mt
;
358 struct intel_mipmap_tree
*dst_mt
= intel_image
->mt
;
360 /* BLORP is not supported before Gen6. */
361 if (brw
->gen
< 6 || brw
->gen
>= 8)
364 if (_mesa_get_format_base_format(src_mt
->format
) !=
365 _mesa_get_format_base_format(dst_mt
->format
)) {
369 /* We can't handle format conversions between Z24 and other formats since
370 * we have to lie about the surface format. See the comments in
371 * brw_blorp_surface_info::set().
373 if ((src_mt
->format
== MESA_FORMAT_X8_Z24
) !=
374 (dst_mt
->format
== MESA_FORMAT_X8_Z24
)) {
378 if (!brw
->format_supported_as_render_target
[dst_mt
->format
])
381 /* Source clipping shouldn't be necessary, since copytexsubimage (in
382 * src/mesa/main/teximage.c) calls _mesa_clip_copytexsubimage() which
385 * Destination clipping shouldn't be necessary since the restrictions on
386 * glCopyTexSubImage prevent the user from specifying a destination rectangle
387 * that falls outside the bounds of the destination texture.
388 * See error_check_subtexture_dimensions().
391 int srcY1
= srcY0
+ height
;
392 int srcX1
= srcX0
+ width
;
393 int dstX1
= dstX0
+ width
;
394 int dstY1
= dstY0
+ height
;
396 /* Account for the fact that in the system framebuffer, the origin is at
399 bool mirror_y
= false;
400 if (_mesa_is_winsys_fbo(ctx
->ReadBuffer
)) {
401 GLint tmp
= src_rb
->Height
- srcY0
;
402 srcY0
= src_rb
->Height
- srcY1
;
407 brw_blorp_blit_miptrees(brw
,
408 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
409 dst_mt
, dst_image
->Level
, dst_image
->Face
+ slice
,
410 srcX0
, srcY0
, srcX1
, srcY1
,
411 dstX0
, dstY0
, dstX1
, dstY1
,
412 GL_NEAREST
, false, mirror_y
);
414 /* If we're copying to a packed depth stencil texture and the source
415 * framebuffer has separate stencil, we need to also copy the stencil data
418 src_rb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
419 if (_mesa_get_format_bits(dst_image
->TexFormat
, GL_STENCIL_BITS
) > 0 &&
421 src_irb
= intel_renderbuffer(src_rb
);
422 src_mt
= src_irb
->mt
;
424 if (src_mt
->stencil_mt
)
425 src_mt
= src_mt
->stencil_mt
;
426 if (dst_mt
->stencil_mt
)
427 dst_mt
= dst_mt
->stencil_mt
;
429 if (src_mt
!= dst_mt
) {
430 brw_blorp_blit_miptrees(brw
,
431 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
432 dst_mt
, dst_image
->Level
,
433 dst_image
->Face
+ slice
,
434 srcX0
, srcY0
, srcX1
, srcY1
,
435 dstX0
, dstY0
, dstX1
, dstY1
,
436 GL_NEAREST
, false, mirror_y
);
445 brw_blorp_framebuffer(struct brw_context
*brw
,
446 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
447 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
448 GLbitfield mask
, GLenum filter
)
450 /* BLORP is not supported before Gen6. */
451 if (brw
->gen
< 6 || brw
->gen
>= 8)
454 static GLbitfield buffer_bits
[] = {
457 GL_STENCIL_BUFFER_BIT
,
460 for (unsigned int i
= 0; i
< ARRAY_SIZE(buffer_bits
); ++i
) {
461 if ((mask
& buffer_bits
[i
]) &&
463 srcX0
, srcY0
, srcX1
, srcY1
,
464 dstX0
, dstY0
, dstX1
, dstY1
,
465 filter
, buffer_bits
[i
])) {
466 mask
&= ~buffer_bits
[i
];
475 * Enum to specify the order of arguments in a sampler message
477 enum sampler_message_arg
479 SAMPLER_MESSAGE_ARG_U_FLOAT
,
480 SAMPLER_MESSAGE_ARG_V_FLOAT
,
481 SAMPLER_MESSAGE_ARG_U_INT
,
482 SAMPLER_MESSAGE_ARG_V_INT
,
483 SAMPLER_MESSAGE_ARG_SI_INT
,
484 SAMPLER_MESSAGE_ARG_MCS_INT
,
485 SAMPLER_MESSAGE_ARG_ZERO_INT
,
489 * Generator for WM programs used in BLORP blits.
491 * The bulk of the work done by the WM program is to wrap and unwrap the
492 * coordinate transformations used by the hardware to store surfaces in
493 * memory. The hardware transforms a pixel location (X, Y, S) (where S is the
494 * sample index for a multisampled surface) to a memory offset by the
495 * following formulas:
497 * offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
498 * (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
500 * For a single-sampled surface, or for a multisampled surface using
501 * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
504 * encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
505 * decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
506 * encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
507 * decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
509 * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
510 * embeds the sample number into bit 1 of the X and Y coordinates:
512 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
513 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
514 * Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
515 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
516 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
517 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
518 * S = (Y & 0b10) | (X & 0b10) >> 1
520 * For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
521 * embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
524 * encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
525 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
526 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
527 * decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
528 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
529 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
530 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
532 * For X tiling, tile() combines together the low-order bits of the X and Y
533 * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
534 * bytes wide and 8 rows high:
536 * tile(x_tiled, X, Y, S) = A
537 * where A = tile_num << 12 | offset
538 * tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
539 * offset = (Y' & 0b111) << 9
540 * | (X & 0b111111111)
542 * Y' = Y + S * qpitch
543 * detile(x_tiled, A) = (X, Y, S)
547 * Y' = (tile_num / tile_pitch) << 3
548 * | (A & 0b111000000000) >> 9
549 * X' = (tile_num % tile_pitch) << 9
550 * | (A & 0b111111111)
552 * (In all tiling formulas, cpp is the number of bytes occupied by a single
553 * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
554 * to fill the width of the surface, and qpitch is the spacing (in rows)
555 * between array slices).
557 * For Y tiling, tile() combines together the low-order bits of the X and Y
558 * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
559 * bytes wide and 32 rows high:
561 * tile(y_tiled, X, Y, S) = A
562 * where A = tile_num << 12 | offset
563 * tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
564 * offset = (X' & 0b1110000) << 5
565 * | (Y' & 0b11111) << 4
568 * Y' = Y + S * qpitch
569 * detile(y_tiled, A) = (X, Y, S)
573 * Y' = (tile_num / tile_pitch) << 5
574 * | (A & 0b111110000) >> 4
575 * X' = (tile_num % tile_pitch) << 7
576 * | (A & 0b111000000000) >> 5
579 * For W tiling, tile() combines together the low-order bits of the X and Y
580 * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
581 * bytes wide and 64 rows high (note that W tiling is only used for stencil
582 * buffers, which always have cpp = 1 and S=0):
584 * tile(w_tiled, X, Y, S) = A
585 * where A = tile_num << 12 | offset
586 * tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
587 * offset = (X' & 0b111000) << 6
588 * | (Y' & 0b111100) << 3
589 * | (X' & 0b100) << 2
595 * Y' = Y + S * qpitch
596 * detile(w_tiled, A) = (X, Y, S)
597 * where X = X' / cpp = X'
598 * Y = Y' % qpitch = Y'
600 * Y' = (tile_num / tile_pitch) << 6
601 * | (A & 0b111100000) >> 3
602 * | (A & 0b1000) >> 2
604 * X' = (tile_num % tile_pitch) << 6
605 * | (A & 0b111000000000) >> 6
606 * | (A & 0b10000) >> 2
610 * Finally, for a non-tiled surface, tile() simply combines together the X and
611 * Y coordinates in the natural way:
613 * tile(untiled, X, Y, S) = A
614 * where A = Y * pitch + X'
616 * Y' = Y + S * qpitch
617 * detile(untiled, A) = (X, Y, S)
624 * (In these formulas, pitch is the number of bytes occupied by a single row
627 class brw_blorp_blit_program
630 brw_blorp_blit_program(struct brw_context
*brw
,
631 const brw_blorp_blit_prog_key
*key
);
632 ~brw_blorp_blit_program();
634 const GLuint
*compile(struct brw_context
*brw
, GLuint
*program_size
);
636 brw_blorp_prog_data prog_data
;
640 void alloc_push_const_regs(int base_reg
);
641 void compute_frag_coords();
642 void translate_tiling(bool old_tiled_w
, bool new_tiled_w
);
643 void encode_msaa(unsigned num_samples
, intel_msaa_layout layout
);
644 void decode_msaa(unsigned num_samples
, intel_msaa_layout layout
);
645 void kill_if_outside_dst_rect();
646 void translate_dst_to_src();
647 void clamp_tex_coords(struct brw_reg regX
, struct brw_reg regY
,
648 struct brw_reg clampX0
, struct brw_reg clampY0
,
649 struct brw_reg clampX1
, struct brw_reg clampY1
);
650 void single_to_blend();
651 void manual_blend_average(unsigned num_samples
);
652 void manual_blend_bilinear(unsigned num_samples
);
653 void sample(struct brw_reg dst
);
654 void texel_fetch(struct brw_reg dst
);
656 void texture_lookup(struct brw_reg dst
, GLuint msg_type
,
657 const sampler_message_arg
*args
, int num_args
);
658 void render_target_write();
661 * Base-2 logarithm of the maximum number of samples that can be blended.
663 static const unsigned LOG2_MAX_BLEND_SAMPLES
= 3;
666 struct brw_context
*brw
;
667 const brw_blorp_blit_prog_key
*key
;
668 struct brw_compile func
;
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 : mem_ctx(ralloc_context(NULL
)),
746 brw_init_compile(brw
, &func
, mem_ctx
);
749 brw_blorp_blit_program::~brw_blorp_blit_program()
751 ralloc_free(mem_ctx
);
755 brw_blorp_blit_program::compile(struct brw_context
*brw
,
756 GLuint
*program_size
)
759 if (key
->dst_tiled_w
&& key
->rt_samples
> 0) {
760 /* If the destination image is W tiled and multisampled, then the thread
761 * must be dispatched once per sample, not once per pixel. This is
762 * necessary because after conversion between W and Y tiling, there's no
763 * guarantee that all samples corresponding to a single pixel will still
766 assert(key
->persample_msaa_dispatch
);
770 /* We are blending, which means we won't have an opportunity to
771 * translate the tiling and sample count for the texture surface. So
772 * the surface state for the texture must be configured with the correct
773 * tiling and sample count.
775 assert(!key
->src_tiled_w
);
776 assert(key
->tex_samples
== key
->src_samples
);
777 assert(key
->tex_layout
== key
->src_layout
);
778 assert(key
->tex_samples
> 0);
781 if (key
->persample_msaa_dispatch
) {
782 /* It only makes sense to do persample dispatch if the render target is
783 * configured as multisampled.
785 assert(key
->rt_samples
> 0);
788 /* Make sure layout is consistent with sample count */
789 assert((key
->tex_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
790 (key
->tex_samples
== 0));
791 assert((key
->rt_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
792 (key
->rt_samples
== 0));
793 assert((key
->src_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
794 (key
->src_samples
== 0));
795 assert((key
->dst_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
796 (key
->dst_samples
== 0));
798 /* Set up prog_data */
799 memset(&prog_data
, 0, sizeof(prog_data
));
800 prog_data
.persample_msaa_dispatch
= key
->persample_msaa_dispatch
;
802 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
805 compute_frag_coords();
807 /* Render target and texture hardware don't support W tiling. */
808 const bool rt_tiled_w
= false;
809 const bool tex_tiled_w
= false;
811 /* The address that data will be written to is determined by the
812 * coordinates supplied to the WM thread and the tiling and sample count of
813 * the render target, according to the formula:
815 * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
817 * If the actual tiling and sample count of the destination surface are not
818 * the same as the configuration of the render target, then these
819 * coordinates are wrong and we have to adjust them to compensate for the
822 if (rt_tiled_w
!= key
->dst_tiled_w
||
823 key
->rt_samples
!= key
->dst_samples
||
824 key
->rt_layout
!= key
->dst_layout
) {
825 encode_msaa(key
->rt_samples
, key
->rt_layout
);
826 /* Now (X, Y, S) = detile(rt_tiling, offset) */
827 translate_tiling(rt_tiled_w
, key
->dst_tiled_w
);
828 /* Now (X, Y, S) = detile(dst_tiling, offset) */
829 decode_msaa(key
->dst_samples
, key
->dst_layout
);
832 /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
834 * That is: X, Y and S now contain the true coordinates and sample index of
835 * the data that the WM thread should output.
837 * If we need to kill pixels that are outside the destination rectangle,
838 * now is the time to do it.
842 kill_if_outside_dst_rect();
844 /* Next, apply a translation to obtain coordinates in the source image. */
845 translate_dst_to_src();
847 /* If the source image is not multisampled, then we want to fetch sample
848 * number 0, because that's the only sample there is.
850 if (key
->src_samples
== 0)
853 /* X, Y, and S are now the coordinates of the pixel in the source image
854 * that we want to texture from. Exception: if we are blending, then S is
855 * irrelevant, because we are going to fetch all samples.
857 if (key
->blend
&& !key
->blit_scaled
) {
859 /* Gen6 hardware an automatically blend using the SAMPLE message */
861 sample(texture_data
[0]);
863 /* Gen7+ hardware doesn't automaticaly blend. */
864 manual_blend_average(key
->src_samples
);
866 } else if(key
->blend
&& key
->blit_scaled
) {
867 manual_blend_bilinear(key
->src_samples
);
869 /* We aren't blending, which means we just want to fetch a single sample
870 * from the source surface. The address that we want to fetch from is
871 * related to the X, Y and S values according to the formula:
873 * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
875 * If the actual tiling and sample count of the source surface are not
876 * the same as the configuration of the texture, then we need to adjust
877 * the coordinates to compensate for the difference.
879 if ((tex_tiled_w
!= key
->src_tiled_w
||
880 key
->tex_samples
!= key
->src_samples
||
881 key
->tex_layout
!= key
->src_layout
) &&
882 !key
->bilinear_filter
) {
883 encode_msaa(key
->src_samples
, key
->src_layout
);
884 /* Now (X, Y, S) = detile(src_tiling, offset) */
885 translate_tiling(key
->src_tiled_w
, tex_tiled_w
);
886 /* Now (X, Y, S) = detile(tex_tiling, offset) */
887 decode_msaa(key
->tex_samples
, key
->tex_layout
);
890 if (key
->bilinear_filter
) {
891 sample(texture_data
[0]);
894 /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
896 * In other words: X, Y, and S now contain values which, when passed to
897 * the texturing unit, will cause data to be read from the correct
898 * memory location. So we can fetch the texel now.
900 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
902 texel_fetch(texture_data
[0]);
906 /* Finally, write the fetched (or blended) value to the render target and
907 * terminate the thread.
909 render_target_write();
911 if (unlikely(INTEL_DEBUG
& DEBUG_BLORP
)) {
912 printf("Native code for BLORP blit:\n");
913 brw_dump_compile(&func
, stdout
, 0, func
.next_insn_offset
);
916 return brw_get_program(&func
, program_size
);
920 brw_blorp_blit_program::alloc_push_const_regs(int base_reg
)
922 #define CONST_LOC(name) offsetof(brw_blorp_wm_push_constants, name)
923 #define ALLOC_REG(name, type) \
925 retype(brw_vec1_reg(BRW_GENERAL_REGISTER_FILE, \
926 base_reg + CONST_LOC(name) / 32, \
927 (CONST_LOC(name) % 32) / 4), type)
929 ALLOC_REG(dst_x0
, BRW_REGISTER_TYPE_UD
);
930 ALLOC_REG(dst_x1
, BRW_REGISTER_TYPE_UD
);
931 ALLOC_REG(dst_y0
, BRW_REGISTER_TYPE_UD
);
932 ALLOC_REG(dst_y1
, BRW_REGISTER_TYPE_UD
);
933 ALLOC_REG(rect_grid_x1
, BRW_REGISTER_TYPE_F
);
934 ALLOC_REG(rect_grid_y1
, BRW_REGISTER_TYPE_F
);
935 ALLOC_REG(x_transform
.multiplier
, BRW_REGISTER_TYPE_F
);
936 ALLOC_REG(x_transform
.offset
, BRW_REGISTER_TYPE_F
);
937 ALLOC_REG(y_transform
.multiplier
, BRW_REGISTER_TYPE_F
);
938 ALLOC_REG(y_transform
.offset
, BRW_REGISTER_TYPE_F
);
944 brw_blorp_blit_program::alloc_regs()
947 this->R0
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
948 this->R1
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
949 prog_data
.first_curbe_grf
= reg
;
950 alloc_push_const_regs(reg
);
951 reg
+= BRW_BLORP_NUM_PUSH_CONST_REGS
;
952 for (unsigned i
= 0; i
< ARRAY_SIZE(texture_data
); ++i
) {
953 this->texture_data
[i
] =
954 retype(vec16(brw_vec8_grf(reg
, 0)), key
->texture_data_type
);
958 retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
); reg
+= 8;
960 for (int i
= 0; i
< 2; ++i
) {
962 = retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
965 = retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
969 if (key
->blit_scaled
&& key
->blend
) {
970 this->x_sample_coords
= brw_vec8_grf(reg
, 0);
972 this->y_sample_coords
= brw_vec8_grf(reg
, 0);
974 this->x_frac
= brw_vec8_grf(reg
, 0);
976 this->y_frac
= brw_vec8_grf(reg
, 0);
980 this->xy_coord_index
= 0;
982 = retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
984 this->t1
= retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
986 this->t2
= retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
989 /* Make sure we didn't run out of registers */
990 assert(reg
<= GEN7_MRF_HACK_START
);
993 this->base_mrf
= mrf
;
996 /* In the code that follows, X and Y can be used to quickly refer to the
997 * active elements of x_coords and y_coords, and Xp and Yp ("X prime" and "Y
998 * prime") to the inactive elements.
1000 * S can be used to quickly refer to sample_index.
1002 #define X x_coords[xy_coord_index]
1003 #define Y y_coords[xy_coord_index]
1004 #define Xp x_coords[!xy_coord_index]
1005 #define Yp y_coords[!xy_coord_index]
1006 #define S sample_index
1008 /* Quickly swap the roles of (X, Y) and (Xp, Yp). Saves us from having to do
1009 * MOVs to transfor (Xp, Yp) to (X, Y) after a coordinate transformation.
1011 #define SWAP_XY_AND_XPYP() xy_coord_index = !xy_coord_index;
1014 * Emit code to compute the X and Y coordinates of the pixels being rendered
1015 * by this WM invocation.
1017 * Assuming the render target is set up for Y tiling, these (X, Y) values are
1018 * related to the address offset where outputs will be written by the formula:
1020 * (X, Y, S) = decode_msaa(detile(offset)).
1022 * (See brw_blorp_blit_program).
1025 brw_blorp_blit_program::compute_frag_coords()
1027 /* R1.2[15:0] = X coordinate of upper left pixel of subspan 0 (pixel 0)
1028 * R1.3[15:0] = X coordinate of upper left pixel of subspan 1 (pixel 4)
1029 * R1.4[15:0] = X coordinate of upper left pixel of subspan 2 (pixel 8)
1030 * R1.5[15:0] = X coordinate of upper left pixel of subspan 3 (pixel 12)
1032 * Pixels within a subspan are laid out in this arrangement:
1036 * So, to compute the coordinates of each pixel, we need to read every 2nd
1037 * 16-bit value (vstride=2) from R1, starting at the 4th 16-bit value
1038 * (suboffset=4), and duplicate each value 4 times (hstride=0, width=4).
1039 * In other words, the data we want to access is R1.4<2;4,0>UW.
1041 * Then, we need to add the repeating sequence (0, 1, 0, 1, ...) to the
1042 * result, since pixels n+1 and n+3 are in the right half of the subspan.
1044 brw_ADD(&func
, vec16(retype(X
, BRW_REGISTER_TYPE_UW
)),
1045 stride(suboffset(R1
, 4), 2, 4, 0), brw_imm_v(0x10101010));
1047 /* Similarly, Y coordinates for subspans come from R1.2[31:16] through
1048 * R1.5[31:16], so to get pixel Y coordinates we need to start at the 5th
1049 * 16-bit value instead of the 4th (R1.5<2;4,0>UW instead of
1052 * And we need to add the repeating sequence (0, 0, 1, 1, ...), since
1053 * pixels n+2 and n+3 are in the bottom half of the subspan.
1055 brw_ADD(&func
, vec16(retype(Y
, BRW_REGISTER_TYPE_UW
)),
1056 stride(suboffset(R1
, 5), 2, 4, 0), brw_imm_v(0x11001100));
1058 /* Move the coordinates to UD registers. */
1059 brw_MOV(&func
, vec16(Xp
), retype(X
, BRW_REGISTER_TYPE_UW
));
1060 brw_MOV(&func
, vec16(Yp
), retype(Y
, BRW_REGISTER_TYPE_UW
));
1063 if (key
->persample_msaa_dispatch
) {
1064 switch (key
->rt_samples
) {
1066 /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 4.
1067 * Therefore, subspan 0 will represent sample 0, subspan 1 will
1068 * represent sample 1, and so on.
1070 * So we need to populate S with the sequence (0, 0, 0, 0, 1, 1, 1,
1071 * 1, 2, 2, 2, 2, 3, 3, 3, 3). The easiest way to do this is to
1072 * populate a temporary variable with the sequence (0, 1, 2, 3), and
1073 * then copy from it using vstride=1, width=4, hstride=0.
1075 struct brw_reg t1_uw1
= retype(t1
, BRW_REGISTER_TYPE_UW
);
1076 brw_MOV(&func
, vec16(t1_uw1
), brw_imm_v(0x3210));
1077 /* Move to UD sample_index register. */
1078 brw_MOV(&func
, S
, stride(t1_uw1
, 1, 4, 0));
1079 brw_MOV(&func
, offset(S
, 1), suboffset(stride(t1_uw1
, 1, 4, 0), 2));
1083 /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 8.
1084 * Therefore, subspan 0 will represent sample N (where N is 0 or 4),
1085 * subspan 1 will represent sample 1, and so on. We can find the
1086 * value of N by looking at R0.0 bits 7:6 ("Starting Sample Pair
1087 * Index") and multiplying by two (since samples are always delivered
1088 * in pairs). That is, we compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 &
1091 * Then we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1, 2,
1092 * 2, 2, 2, 3, 3, 3, 3), which we compute by populating a temporary
1093 * variable with the sequence (0, 1, 2, 3), and then reading from it
1094 * using vstride=1, width=4, hstride=0.
1096 struct brw_reg t1_ud1
= vec1(retype(t1
, BRW_REGISTER_TYPE_UD
));
1097 struct brw_reg t2_uw1
= retype(t2
, BRW_REGISTER_TYPE_UW
);
1098 struct brw_reg r0_ud1
= vec1(retype(R0
, BRW_REGISTER_TYPE_UD
));
1099 brw_AND(&func
, t1_ud1
, r0_ud1
, brw_imm_ud(0xc0));
1100 brw_SHR(&func
, t1_ud1
, t1_ud1
, brw_imm_ud(5));
1101 brw_MOV(&func
, vec16(t2_uw1
), brw_imm_v(0x3210));
1102 brw_ADD(&func
, vec16(S
), retype(t1_ud1
, BRW_REGISTER_TYPE_UW
),
1103 stride(t2_uw1
, 1, 4, 0));
1104 brw_ADD(&func
, offset(S
, 1),
1105 retype(t1_ud1
, BRW_REGISTER_TYPE_UW
),
1106 suboffset(stride(t2_uw1
, 1, 4, 0), 2));
1110 assert(!"Unrecognized sample count in "
1111 "brw_blorp_blit_program::compute_frag_coords()");
1116 /* Either the destination surface is single-sampled, or the WM will be
1117 * run in MSDISPMODE_PERPIXEL (which causes a single fragment dispatch
1118 * per pixel). In either case, it's not meaningful to compute a sample
1119 * value. Just set it to 0.
1126 * Emit code to compensate for the difference between Y and W tiling.
1128 * This code modifies the X and Y coordinates according to the formula:
1130 * (X', Y', S') = detile(new_tiling, tile(old_tiling, X, Y, S))
1132 * (See brw_blorp_blit_program).
1134 * It can only translate between W and Y tiling, so new_tiling and old_tiling
1135 * are booleans where true represents W tiling and false represents Y tiling.
1138 brw_blorp_blit_program::translate_tiling(bool old_tiled_w
, bool new_tiled_w
)
1140 if (old_tiled_w
== new_tiled_w
)
1143 /* In the code that follows, we can safely assume that S = 0, because W
1144 * tiling formats always use IMS layout.
1148 brw_set_compression_control(&func
, BRW_COMPRESSION_COMPRESSED
);
1150 /* Given X and Y coordinates that describe an address using Y tiling,
1151 * translate to the X and Y coordinates that describe the same address
1154 * If we break down the low order bits of X and Y, using a
1155 * single letter to represent each low-order bit:
1157 * X = A << 7 | 0bBCDEFGH
1158 * Y = J << 5 | 0bKLMNP (1)
1160 * Then we can apply the Y tiling formula to see the memory offset being
1163 * offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
1165 * If we apply the W detiling formula to this memory location, that the
1166 * corresponding X' and Y' coordinates are:
1168 * X' = A << 6 | 0bBCDPFH (3)
1169 * Y' = J << 6 | 0bKLMNEG
1171 * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
1172 * we need to make the following computation:
1174 * X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
1175 * Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
1177 brw_AND(&func
, t1
, X
, brw_imm_uw(0xfff4)); /* X & ~0b1011 */
1178 brw_SHR(&func
, t1
, t1
, brw_imm_uw(1)); /* (X & ~0b1011) >> 1 */
1179 brw_AND(&func
, t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1180 brw_SHL(&func
, t2
, t2
, brw_imm_uw(2)); /* (Y & 0b1) << 2 */
1181 brw_OR(&func
, t1
, t1
, t2
); /* (X & ~0b1011) >> 1 | (Y & 0b1) << 2 */
1182 brw_AND(&func
, t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1183 brw_OR(&func
, Xp
, t1
, t2
);
1184 brw_AND(&func
, t1
, Y
, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1185 brw_SHL(&func
, t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1186 brw_AND(&func
, t2
, X
, brw_imm_uw(8)); /* X & 0b1000 */
1187 brw_SHR(&func
, t2
, t2
, brw_imm_uw(2)); /* (X & 0b1000) >> 2 */
1188 brw_OR(&func
, t1
, t1
, t2
); /* (Y & ~0b1) << 1 | (X & 0b1000) >> 2 */
1189 brw_AND(&func
, t2
, X
, brw_imm_uw(2)); /* X & 0b10 */
1190 brw_SHR(&func
, t2
, t2
, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1191 brw_OR(&func
, Yp
, t1
, t2
);
1194 /* Applying the same logic as above, but in reverse, we obtain the
1197 * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
1198 * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
1200 brw_AND(&func
, t1
, X
, brw_imm_uw(0xfffa)); /* X & ~0b101 */
1201 brw_SHL(&func
, t1
, t1
, brw_imm_uw(1)); /* (X & ~0b101) << 1 */
1202 brw_AND(&func
, t2
, Y
, brw_imm_uw(2)); /* Y & 0b10 */
1203 brw_SHL(&func
, t2
, t2
, brw_imm_uw(2)); /* (Y & 0b10) << 2 */
1204 brw_OR(&func
, t1
, t1
, t2
); /* (X & ~0b101) << 1 | (Y & 0b10) << 2 */
1205 brw_AND(&func
, t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1206 brw_SHL(&func
, t2
, t2
, brw_imm_uw(1)); /* (Y & 0b1) << 1 */
1207 brw_OR(&func
, t1
, t1
, t2
); /* (X & ~0b101) << 1 | (Y & 0b10) << 2
1209 brw_AND(&func
, t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1210 brw_OR(&func
, Xp
, t1
, t2
);
1211 brw_AND(&func
, t1
, Y
, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1212 brw_SHR(&func
, t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1213 brw_AND(&func
, t2
, X
, brw_imm_uw(4)); /* X & 0b100 */
1214 brw_SHR(&func
, t2
, t2
, brw_imm_uw(2)); /* (X & 0b100) >> 2 */
1215 brw_OR(&func
, Yp
, t1
, t2
);
1218 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
1222 * Emit code to compensate for the difference between MSAA and non-MSAA
1225 * This code modifies the X and Y coordinates according to the formula:
1227 * (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
1229 * (See brw_blorp_blit_program).
1232 brw_blorp_blit_program::encode_msaa(unsigned num_samples
,
1233 intel_msaa_layout layout
)
1235 brw_set_compression_control(&func
, BRW_COMPRESSION_COMPRESSED
);
1237 case INTEL_MSAA_LAYOUT_NONE
:
1238 /* No translation necessary, and S should already be zero. */
1241 case INTEL_MSAA_LAYOUT_CMS
:
1242 /* We can't compensate for compressed layout since at this point in the
1243 * program we haven't read from the MCS buffer.
1245 assert(!"Bad layout in encode_msaa");
1247 case INTEL_MSAA_LAYOUT_UMS
:
1248 /* No translation necessary. */
1250 case INTEL_MSAA_LAYOUT_IMS
:
1251 switch (num_samples
) {
1253 /* encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
1254 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
1255 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1257 brw_AND(&func
, t1
, X
, brw_imm_uw(0xfffe)); /* X & ~0b1 */
1259 brw_AND(&func
, t2
, S
, brw_imm_uw(1)); /* S & 0b1 */
1260 brw_OR(&func
, t1
, t1
, t2
); /* (X & ~0b1) | (S & 0b1) */
1262 brw_SHL(&func
, t1
, t1
, brw_imm_uw(1)); /* (X & ~0b1) << 1
1264 brw_AND(&func
, t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1265 brw_OR(&func
, Xp
, t1
, t2
);
1266 brw_AND(&func
, t1
, Y
, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1267 brw_SHL(&func
, t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1269 brw_AND(&func
, t2
, S
, brw_imm_uw(2)); /* S & 0b10 */
1270 brw_OR(&func
, t1
, t1
, t2
); /* (Y & ~0b1) << 1 | (S & 0b10) */
1272 brw_AND(&func
, t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1273 brw_OR(&func
, Yp
, t1
, t2
);
1276 /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
1277 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
1279 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1281 brw_AND(&func
, t1
, X
, brw_imm_uw(0xfffe)); /* X & ~0b1 */
1282 brw_SHL(&func
, t1
, t1
, brw_imm_uw(2)); /* (X & ~0b1) << 2 */
1284 brw_AND(&func
, t2
, S
, brw_imm_uw(4)); /* S & 0b100 */
1285 brw_OR(&func
, t1
, t1
, t2
); /* (X & ~0b1) << 2 | (S & 0b100) */
1286 brw_AND(&func
, t2
, S
, brw_imm_uw(1)); /* S & 0b1 */
1287 brw_SHL(&func
, t2
, t2
, brw_imm_uw(1)); /* (S & 0b1) << 1 */
1288 brw_OR(&func
, t1
, t1
, t2
); /* (X & ~0b1) << 2 | (S & 0b100)
1291 brw_AND(&func
, t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1292 brw_OR(&func
, Xp
, t1
, t2
);
1293 brw_AND(&func
, t1
, Y
, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1294 brw_SHL(&func
, t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1296 brw_AND(&func
, t2
, S
, brw_imm_uw(2)); /* S & 0b10 */
1297 brw_OR(&func
, t1
, t1
, t2
); /* (Y & ~0b1) << 1 | (S & 0b10) */
1299 brw_AND(&func
, t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1300 brw_OR(&func
, Yp
, t1
, t2
);
1307 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
1311 * Emit code to compensate for the difference between MSAA and non-MSAA
1314 * This code modifies the X and Y coordinates according to the formula:
1316 * (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
1318 * (See brw_blorp_blit_program).
1321 brw_blorp_blit_program::decode_msaa(unsigned num_samples
,
1322 intel_msaa_layout layout
)
1324 brw_set_compression_control(&func
, BRW_COMPRESSION_COMPRESSED
);
1326 case INTEL_MSAA_LAYOUT_NONE
:
1327 /* No translation necessary, and S should already be zero. */
1330 case INTEL_MSAA_LAYOUT_CMS
:
1331 /* We can't compensate for compressed layout since at this point in the
1332 * program we don't have access to the MCS buffer.
1334 assert(!"Bad layout in encode_msaa");
1336 case INTEL_MSAA_LAYOUT_UMS
:
1337 /* No translation necessary. */
1339 case INTEL_MSAA_LAYOUT_IMS
:
1341 switch (num_samples
) {
1343 /* decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
1344 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
1345 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1346 * S = (Y & 0b10) | (X & 0b10) >> 1
1348 brw_AND(&func
, t1
, X
, brw_imm_uw(0xfffc)); /* X & ~0b11 */
1349 brw_SHR(&func
, t1
, t1
, brw_imm_uw(1)); /* (X & ~0b11) >> 1 */
1350 brw_AND(&func
, t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1351 brw_OR(&func
, Xp
, t1
, t2
);
1352 brw_AND(&func
, t1
, Y
, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1353 brw_SHR(&func
, t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1354 brw_AND(&func
, t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1355 brw_OR(&func
, Yp
, t1
, t2
);
1356 brw_AND(&func
, t1
, Y
, brw_imm_uw(2)); /* Y & 0b10 */
1357 brw_AND(&func
, t2
, X
, brw_imm_uw(2)); /* X & 0b10 */
1358 brw_SHR(&func
, t2
, t2
, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1359 brw_OR(&func
, S
, t1
, t2
);
1362 /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
1363 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
1364 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1365 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
1367 brw_AND(&func
, t1
, X
, brw_imm_uw(0xfff8)); /* X & ~0b111 */
1368 brw_SHR(&func
, t1
, t1
, brw_imm_uw(2)); /* (X & ~0b111) >> 2 */
1369 brw_AND(&func
, t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1370 brw_OR(&func
, Xp
, t1
, t2
);
1371 brw_AND(&func
, t1
, Y
, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1372 brw_SHR(&func
, t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1373 brw_AND(&func
, t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1374 brw_OR(&func
, Yp
, t1
, t2
);
1375 brw_AND(&func
, t1
, X
, brw_imm_uw(4)); /* X & 0b100 */
1376 brw_AND(&func
, t2
, Y
, brw_imm_uw(2)); /* Y & 0b10 */
1377 brw_OR(&func
, t1
, t1
, t2
); /* (X & 0b100) | (Y & 0b10) */
1378 brw_AND(&func
, t2
, X
, brw_imm_uw(2)); /* X & 0b10 */
1379 brw_SHR(&func
, t2
, t2
, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1380 brw_OR(&func
, S
, t1
, t2
);
1387 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
1391 * Emit code that kills pixels whose X and Y coordinates are outside the
1392 * boundary of the rectangle defined by the push constants (dst_x0, dst_y0,
1396 brw_blorp_blit_program::kill_if_outside_dst_rect()
1398 struct brw_reg f0
= brw_flag_reg(0, 0);
1399 struct brw_reg g1
= retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW
);
1400 struct brw_reg null32
= vec16(retype(brw_null_reg(), BRW_REGISTER_TYPE_UD
));
1402 brw_CMP(&func
, null32
, BRW_CONDITIONAL_GE
, X
, dst_x0
);
1403 brw_CMP(&func
, null32
, BRW_CONDITIONAL_GE
, Y
, dst_y0
);
1404 brw_CMP(&func
, null32
, BRW_CONDITIONAL_L
, X
, dst_x1
);
1405 brw_CMP(&func
, null32
, BRW_CONDITIONAL_L
, Y
, dst_y1
);
1407 brw_set_predicate_control(&func
, BRW_PREDICATE_NONE
);
1408 brw_push_insn_state(&func
);
1409 brw_set_mask_control(&func
, BRW_MASK_DISABLE
);
1410 brw_AND(&func
, g1
, f0
, g1
);
1411 brw_pop_insn_state(&func
);
1415 * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
1419 brw_blorp_blit_program::translate_dst_to_src()
1421 struct brw_reg X_f
= retype(X
, BRW_REGISTER_TYPE_F
);
1422 struct brw_reg Y_f
= retype(Y
, BRW_REGISTER_TYPE_F
);
1423 struct brw_reg Xp_f
= retype(Xp
, BRW_REGISTER_TYPE_F
);
1424 struct brw_reg Yp_f
= retype(Yp
, BRW_REGISTER_TYPE_F
);
1426 brw_set_compression_control(&func
, BRW_COMPRESSION_COMPRESSED
);
1427 /* Move the UD coordinates to float registers. */
1428 brw_MOV(&func
, Xp_f
, X
);
1429 brw_MOV(&func
, Yp_f
, Y
);
1430 /* Scale and offset */
1431 brw_MUL(&func
, X_f
, Xp_f
, x_transform
.multiplier
);
1432 brw_MUL(&func
, Y_f
, Yp_f
, y_transform
.multiplier
);
1433 brw_ADD(&func
, X_f
, X_f
, x_transform
.offset
);
1434 brw_ADD(&func
, Y_f
, Y_f
, y_transform
.offset
);
1435 if (key
->blit_scaled
&& key
->blend
) {
1436 /* Translate coordinates to lay out the samples in a rectangular grid
1437 * roughly corresponding to sample locations.
1439 brw_MUL(&func
, X_f
, X_f
, brw_imm_f(key
->x_scale
));
1440 brw_MUL(&func
, Y_f
, Y_f
, brw_imm_f(key
->y_scale
));
1441 /* Adjust coordinates so that integers represent pixel centers rather
1444 brw_ADD(&func
, X_f
, X_f
, brw_imm_f(-0.5));
1445 brw_ADD(&func
, Y_f
, Y_f
, brw_imm_f(-0.5));
1447 /* Clamp the X, Y texture coordinates to properly handle the sampling of
1448 * texels on texture edges.
1450 clamp_tex_coords(X_f
, Y_f
,
1451 brw_imm_f(0.0), brw_imm_f(0.0),
1452 rect_grid_x1
, rect_grid_y1
);
1454 /* Store the fractional parts to be used as bilinear interpolation
1457 brw_FRC(&func
, x_frac
, X_f
);
1458 brw_FRC(&func
, y_frac
, Y_f
);
1460 /* Round the float coordinates down to nearest integer */
1461 brw_RNDD(&func
, Xp_f
, X_f
);
1462 brw_RNDD(&func
, Yp_f
, Y_f
);
1463 brw_MUL(&func
, X_f
, Xp_f
, brw_imm_f(1 / key
->x_scale
));
1464 brw_MUL(&func
, Y_f
, Yp_f
, brw_imm_f(1 / key
->y_scale
));
1466 } else if (!key
->bilinear_filter
) {
1467 /* Round the float coordinates down to nearest integer by moving to
1470 brw_MOV(&func
, Xp
, X_f
);
1471 brw_MOV(&func
, Yp
, Y_f
);
1474 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
1478 brw_blorp_blit_program::clamp_tex_coords(struct brw_reg regX
,
1479 struct brw_reg regY
,
1480 struct brw_reg clampX0
,
1481 struct brw_reg clampY0
,
1482 struct brw_reg clampX1
,
1483 struct brw_reg clampY1
)
1485 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_L
, regX
, clampX0
);
1486 brw_MOV(&func
, regX
, clampX0
);
1487 brw_set_predicate_control(&func
, BRW_PREDICATE_NONE
);
1489 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_G
, regX
, clampX1
);
1490 brw_MOV(&func
, regX
, clampX1
);
1491 brw_set_predicate_control(&func
, BRW_PREDICATE_NONE
);
1493 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_L
, regY
, clampY0
);
1494 brw_MOV(&func
, regY
, clampY0
);
1495 brw_set_predicate_control(&func
, BRW_PREDICATE_NONE
);
1497 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_G
, regY
, clampY1
);
1498 brw_MOV(&func
, regY
, clampY1
);
1499 brw_set_predicate_control(&func
, BRW_PREDICATE_NONE
);
1503 * Emit code to transform the X and Y coordinates as needed for blending
1504 * together the different samples in an MSAA texture.
1507 brw_blorp_blit_program::single_to_blend()
1509 /* When looking up samples in an MSAA texture using the SAMPLE message,
1510 * Gen6 requires the texture coordinates to be odd integers (so that they
1511 * correspond to the center of a 2x2 block representing the four samples
1512 * that maxe up a pixel). So we need to multiply our X and Y coordinates
1513 * each by 2 and then add 1.
1515 brw_set_compression_control(&func
, BRW_COMPRESSION_COMPRESSED
);
1516 brw_SHL(&func
, t1
, X
, brw_imm_w(1));
1517 brw_SHL(&func
, t2
, Y
, brw_imm_w(1));
1518 brw_ADD(&func
, Xp
, t1
, brw_imm_w(1));
1519 brw_ADD(&func
, Yp
, t2
, brw_imm_w(1));
1520 brw_set_compression_control(&func
, BRW_COMPRESSION_NONE
);
1526 * Count the number of trailing 1 bits in the given value. For example:
1528 * count_trailing_one_bits(0) == 0
1529 * count_trailing_one_bits(7) == 3
1530 * count_trailing_one_bits(11) == 2
1532 inline int count_trailing_one_bits(unsigned value
)
1534 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
1535 return __builtin_ctz(~value
);
1537 return _mesa_bitcount(value
& ~(value
+ 1));
1543 brw_blorp_blit_program::manual_blend_average(unsigned num_samples
)
1545 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
1548 /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
1550 * result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
1552 * This ensures that when all samples have the same value, no numerical
1553 * precision is lost, since each addition operation always adds two equal
1554 * values, and summing two equal floating point values does not lose
1557 * We perform this computation by treating the texture_data array as a
1558 * stack and performing the following operations:
1560 * - push sample 0 onto stack
1561 * - push sample 1 onto stack
1562 * - add top two stack entries
1563 * - push sample 2 onto stack
1564 * - push sample 3 onto stack
1565 * - add top two stack entries
1566 * - add top two stack entries
1567 * - divide top stack entry by 4
1569 * Note that after pushing sample i onto the stack, the number of add
1570 * operations we do is equal to the number of trailing 1 bits in i. This
1571 * works provided the total number of samples is a power of two, which it
1572 * always is for i965.
1574 * For integer formats, we replace the add operations with average
1575 * operations and skip the final division.
1577 typedef struct brw_instruction
*(*brw_op2_ptr
)(struct brw_compile
*,
1581 brw_op2_ptr combine_op
=
1582 key
->texture_data_type
== BRW_REGISTER_TYPE_F
? brw_ADD
: brw_AVG
;
1583 unsigned stack_depth
= 0;
1584 for (unsigned i
= 0; i
< num_samples
; ++i
) {
1585 assert(stack_depth
== _mesa_bitcount(i
)); /* Loop invariant */
1587 /* Push sample i onto the stack */
1588 assert(stack_depth
< ARRAY_SIZE(texture_data
));
1593 brw_MOV(&func
, vec16(S
), brw_imm_ud(i
));
1595 texel_fetch(texture_data
[stack_depth
++]);
1597 if (i
== 0 && key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
) {
1598 /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
1599 * suggests an optimization:
1601 * "A simple optimization with probable large return in
1602 * performance is to compare the MCS value to zero (indicating
1603 * all samples are on sample slice 0), and sample only from
1604 * sample slice 0 using ld2dss if MCS is zero."
1606 * Note that in the case where the MCS value is zero, sampling from
1607 * sample slice 0 using ld2dss and sampling from sample 0 using
1608 * ld2dms are equivalent (since all samples are on sample slice 0).
1609 * Since we have already sampled from sample 0, all we need to do is
1610 * skip the remaining fetches and averaging if MCS is zero.
1612 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_NZ
,
1613 mcs_data
, brw_imm_ud(0));
1614 brw_IF(&func
, BRW_EXECUTE_16
);
1617 /* Do count_trailing_one_bits(i) times */
1618 for (int j
= count_trailing_one_bits(i
); j
-- > 0; ) {
1619 assert(stack_depth
>= 2);
1622 /* TODO: should use a smaller loop bound for non_RGBA formats */
1623 for (int k
= 0; k
< 4; ++k
) {
1624 combine_op(&func
, offset(texture_data
[stack_depth
- 1], 2*k
),
1625 offset(vec8(texture_data
[stack_depth
- 1]), 2*k
),
1626 offset(vec8(texture_data
[stack_depth
]), 2*k
));
1631 /* We should have just 1 sample on the stack now. */
1632 assert(stack_depth
== 1);
1634 if (key
->texture_data_type
== BRW_REGISTER_TYPE_F
) {
1635 /* Scale the result down by a factor of num_samples */
1636 /* TODO: should use a smaller loop bound for non-RGBA formats */
1637 for (int j
= 0; j
< 4; ++j
) {
1638 brw_MUL(&func
, offset(texture_data
[0], 2*j
),
1639 offset(vec8(texture_data
[0]), 2*j
),
1640 brw_imm_f(1.0/num_samples
));
1644 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
1649 brw_blorp_blit_program::manual_blend_bilinear(unsigned num_samples
)
1651 /* We do this computation by performing the following operations:
1653 * In case of 4x, 8x MSAA:
1654 * - Compute the pixel coordinates and sample numbers (a, b, c, d)
1655 * which are later used for interpolation
1656 * - linearly interpolate samples a and b in X
1657 * - linearly interpolate samples c and d in X
1658 * - linearly interpolate the results of last two operations in Y
1660 * result = lrp(lrp(a + b) + lrp(c + d))
1662 struct brw_reg Xp_f
= retype(Xp
, BRW_REGISTER_TYPE_F
);
1663 struct brw_reg Yp_f
= retype(Yp
, BRW_REGISTER_TYPE_F
);
1664 struct brw_reg t1_f
= retype(t1
, BRW_REGISTER_TYPE_F
);
1665 struct brw_reg t2_f
= retype(t2
, BRW_REGISTER_TYPE_F
);
1667 for (unsigned i
= 0; i
< 4; ++i
) {
1668 assert(i
< ARRAY_SIZE(texture_data
));
1671 /* Compute pixel coordinates */
1672 brw_ADD(&func
, vec16(x_sample_coords
), Xp_f
,
1673 brw_imm_f((float)(i
& 0x1) * (1.0 / key
->x_scale
)));
1674 brw_ADD(&func
, vec16(y_sample_coords
), Yp_f
,
1675 brw_imm_f((float)((i
>> 1) & 0x1) * (1.0 / key
->y_scale
)));
1676 brw_MOV(&func
, vec16(X
), x_sample_coords
);
1677 brw_MOV(&func
, vec16(Y
), y_sample_coords
);
1679 /* The MCS value we fetch has to match up with the pixel that we're
1680 * sampling from. Since we sample from different pixels in each
1681 * iteration of this "for" loop, the call to mcs_fetch() should be
1682 * here inside the loop after computing the pixel coordinates.
1684 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
1687 /* Compute sample index and map the sample index to a sample number.
1688 * Sample index layout shows the numbering of slots in a rectangular
1689 * grid of samples with in a pixel. Sample number layout shows the
1690 * rectangular grid of samples roughly corresponding to the real sample
1691 * locations with in a pixel.
1692 * In case of 4x MSAA, layout of sample indices matches the layout of
1700 * In case of 8x MSAA the two layouts don't match.
1701 * sample index layout : --------- sample number layout : ---------
1702 * | 0 | 1 | | 5 | 2 |
1703 * --------- ---------
1704 * | 2 | 3 | | 4 | 6 |
1705 * --------- ---------
1706 * | 4 | 5 | | 0 | 3 |
1707 * --------- ---------
1708 * | 6 | 7 | | 7 | 1 |
1709 * --------- ---------
1711 brw_FRC(&func
, vec16(t1_f
), x_sample_coords
);
1712 brw_FRC(&func
, vec16(t2_f
), y_sample_coords
);
1713 brw_MUL(&func
, vec16(t1_f
), t1_f
, brw_imm_f(key
->x_scale
));
1714 brw_MUL(&func
, vec16(t2_f
), t2_f
, brw_imm_f(key
->x_scale
* key
->y_scale
));
1715 brw_ADD(&func
, vec16(t1_f
), t1_f
, t2_f
);
1716 brw_MOV(&func
, vec16(S
), t1_f
);
1718 if (num_samples
== 8) {
1719 /* Map the sample index to a sample number */
1720 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_L
,
1722 brw_IF(&func
, BRW_EXECUTE_16
);
1724 brw_MOV(&func
, vec16(t2
), brw_imm_d(5));
1725 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_EQ
,
1727 brw_MOV(&func
, vec16(t2
), brw_imm_d(2));
1728 brw_set_predicate_control(&func
, BRW_PREDICATE_NONE
);
1729 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_EQ
,
1731 brw_MOV(&func
, vec16(t2
), brw_imm_d(4));
1732 brw_set_predicate_control(&func
, BRW_PREDICATE_NONE
);
1733 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_EQ
,
1735 brw_MOV(&func
, vec16(t2
), brw_imm_d(6));
1736 brw_set_predicate_control(&func
, BRW_PREDICATE_NONE
);
1740 brw_MOV(&func
, vec16(t2
), brw_imm_d(0));
1741 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_EQ
,
1743 brw_MOV(&func
, vec16(t2
), brw_imm_d(3));
1744 brw_set_predicate_control(&func
, BRW_PREDICATE_NONE
);
1745 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_EQ
,
1747 brw_MOV(&func
, vec16(t2
), brw_imm_d(7));
1748 brw_set_predicate_control(&func
, BRW_PREDICATE_NONE
);
1749 brw_CMP(&func
, vec16(brw_null_reg()), BRW_CONDITIONAL_EQ
,
1751 brw_MOV(&func
, vec16(t2
), brw_imm_d(1));
1752 brw_set_predicate_control(&func
, BRW_PREDICATE_NONE
);
1755 brw_MOV(&func
, vec16(S
), t2
);
1757 texel_fetch(texture_data
[i
]);
1760 #define SAMPLE(x, y) offset(texture_data[x], y)
1761 brw_set_access_mode(&func
, BRW_ALIGN_16
);
1762 for (int index
= 3; index
> 0; ) {
1763 /* Since we're doing SIMD16, 4 color channels fits in to 8 registers.
1764 * Counter value of 8 in 'for' loop below is used to interpolate all
1765 * the color components.
1767 for (int k
= 0; k
< 8; ++k
)
1769 vec8(SAMPLE(index
- 1, k
)),
1770 offset(x_frac
, k
& 1),
1772 SAMPLE(index
- 1, k
));
1775 for (int k
= 0; k
< 8; ++k
)
1778 offset(y_frac
, k
& 1),
1780 vec8(SAMPLE(0, k
)));
1781 brw_set_access_mode(&func
, BRW_ALIGN_1
);
1786 * Emit code to look up a value in the texture using the SAMPLE message (which
1787 * does blending of MSAA surfaces).
1790 brw_blorp_blit_program::sample(struct brw_reg dst
)
1792 static const sampler_message_arg args
[2] = {
1793 SAMPLER_MESSAGE_ARG_U_FLOAT
,
1794 SAMPLER_MESSAGE_ARG_V_FLOAT
1797 texture_lookup(dst
, GEN5_SAMPLER_MESSAGE_SAMPLE
, args
,
1802 * Emit code to look up a value in the texture using the SAMPLE_LD message
1803 * (which does a simple texel fetch).
1806 brw_blorp_blit_program::texel_fetch(struct brw_reg dst
)
1808 static const sampler_message_arg gen6_args
[5] = {
1809 SAMPLER_MESSAGE_ARG_U_INT
,
1810 SAMPLER_MESSAGE_ARG_V_INT
,
1811 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* R */
1812 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* LOD */
1813 SAMPLER_MESSAGE_ARG_SI_INT
1815 static const sampler_message_arg gen7_ld_args
[3] = {
1816 SAMPLER_MESSAGE_ARG_U_INT
,
1817 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* LOD */
1818 SAMPLER_MESSAGE_ARG_V_INT
1820 static const sampler_message_arg gen7_ld2dss_args
[3] = {
1821 SAMPLER_MESSAGE_ARG_SI_INT
,
1822 SAMPLER_MESSAGE_ARG_U_INT
,
1823 SAMPLER_MESSAGE_ARG_V_INT
1825 static const sampler_message_arg gen7_ld2dms_args
[4] = {
1826 SAMPLER_MESSAGE_ARG_SI_INT
,
1827 SAMPLER_MESSAGE_ARG_MCS_INT
,
1828 SAMPLER_MESSAGE_ARG_U_INT
,
1829 SAMPLER_MESSAGE_ARG_V_INT
1834 texture_lookup(dst
, GEN5_SAMPLER_MESSAGE_SAMPLE_LD
, gen6_args
,
1838 switch (key
->tex_layout
) {
1839 case INTEL_MSAA_LAYOUT_IMS
:
1840 /* From the Ivy Bridge PRM, Vol4 Part1 p72 (Multisampled Surface Storage
1843 * If this field is MSFMT_DEPTH_STENCIL
1844 * [a.k.a. INTEL_MSAA_LAYOUT_IMS], the only sampling engine
1845 * messages allowed are "ld2dms", "resinfo", and "sampleinfo".
1847 * So fall through to emit the same message as we use for
1848 * INTEL_MSAA_LAYOUT_CMS.
1850 case INTEL_MSAA_LAYOUT_CMS
:
1851 texture_lookup(dst
, GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS
,
1852 gen7_ld2dms_args
, ARRAY_SIZE(gen7_ld2dms_args
));
1854 case INTEL_MSAA_LAYOUT_UMS
:
1855 texture_lookup(dst
, GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS
,
1856 gen7_ld2dss_args
, ARRAY_SIZE(gen7_ld2dss_args
));
1858 case INTEL_MSAA_LAYOUT_NONE
:
1860 texture_lookup(dst
, GEN5_SAMPLER_MESSAGE_SAMPLE_LD
, gen7_ld_args
,
1861 ARRAY_SIZE(gen7_ld_args
));
1866 assert(!"Should not get here.");
1872 brw_blorp_blit_program::mcs_fetch()
1874 static const sampler_message_arg gen7_ld_mcs_args
[2] = {
1875 SAMPLER_MESSAGE_ARG_U_INT
,
1876 SAMPLER_MESSAGE_ARG_V_INT
1878 texture_lookup(vec16(mcs_data
), GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS
,
1879 gen7_ld_mcs_args
, ARRAY_SIZE(gen7_ld_mcs_args
));
1883 brw_blorp_blit_program::texture_lookup(struct brw_reg dst
,
1885 const sampler_message_arg
*args
,
1888 struct brw_reg mrf
=
1889 retype(vec16(brw_message_reg(base_mrf
)), BRW_REGISTER_TYPE_UD
);
1890 for (int arg
= 0; arg
< num_args
; ++arg
) {
1891 switch (args
[arg
]) {
1892 case SAMPLER_MESSAGE_ARG_U_FLOAT
:
1893 if (key
->bilinear_filter
)
1894 brw_MOV(&func
, retype(mrf
, BRW_REGISTER_TYPE_F
),
1895 retype(X
, BRW_REGISTER_TYPE_F
));
1897 brw_MOV(&func
, retype(mrf
, BRW_REGISTER_TYPE_F
), X
);
1899 case SAMPLER_MESSAGE_ARG_V_FLOAT
:
1900 if (key
->bilinear_filter
)
1901 brw_MOV(&func
, retype(mrf
, BRW_REGISTER_TYPE_F
),
1902 retype(Y
, BRW_REGISTER_TYPE_F
));
1904 brw_MOV(&func
, retype(mrf
, BRW_REGISTER_TYPE_F
), Y
);
1906 case SAMPLER_MESSAGE_ARG_U_INT
:
1907 brw_MOV(&func
, mrf
, X
);
1909 case SAMPLER_MESSAGE_ARG_V_INT
:
1910 brw_MOV(&func
, mrf
, Y
);
1912 case SAMPLER_MESSAGE_ARG_SI_INT
:
1913 /* Note: on Gen7, this code may be reached with s_is_zero==true
1914 * because in Gen7's ld2dss message, the sample index is the first
1915 * argument. When this happens, we need to move a 0 into the
1916 * appropriate message register.
1919 brw_MOV(&func
, mrf
, brw_imm_ud(0));
1921 brw_MOV(&func
, mrf
, S
);
1923 case SAMPLER_MESSAGE_ARG_MCS_INT
:
1924 switch (key
->tex_layout
) {
1925 case INTEL_MSAA_LAYOUT_CMS
:
1926 brw_MOV(&func
, mrf
, mcs_data
);
1928 case INTEL_MSAA_LAYOUT_IMS
:
1929 /* When sampling from an IMS surface, MCS data is not relevant,
1930 * and the hardware ignores it. So don't bother populating it.
1934 /* We shouldn't be trying to send MCS data with any other
1937 assert (!"Unsupported layout for MCS data");
1941 case SAMPLER_MESSAGE_ARG_ZERO_INT
:
1942 brw_MOV(&func
, mrf
, brw_imm_ud(0));
1949 retype(dst
, BRW_REGISTER_TYPE_UW
) /* dest */,
1950 base_mrf
/* msg_reg_nr */,
1951 brw_message_reg(base_mrf
) /* src0 */,
1952 BRW_BLORP_TEXTURE_BINDING_TABLE_INDEX
,
1955 8 /* response_length. TODO: should be smaller for non-RGBA formats? */,
1956 mrf
.nr
- base_mrf
/* msg_length */,
1957 0 /* header_present */,
1958 BRW_SAMPLER_SIMD_MODE_SIMD16
,
1959 BRW_SAMPLER_RETURN_FORMAT_FLOAT32
);
1967 #undef SWAP_XY_AND_XPYP
1970 brw_blorp_blit_program::render_target_write()
1972 struct brw_reg mrf_rt_write
=
1973 retype(vec16(brw_message_reg(base_mrf
)), key
->texture_data_type
);
1976 /* If we may have killed pixels, then we need to send R0 and R1 in a header
1977 * so that the render target knows which pixels we killed.
1979 bool use_header
= key
->use_kill
;
1981 /* Copy R0/1 to MRF */
1982 brw_MOV(&func
, retype(mrf_rt_write
, BRW_REGISTER_TYPE_UD
),
1983 retype(R0
, BRW_REGISTER_TYPE_UD
));
1987 /* Copy texture data to MRFs */
1988 for (int i
= 0; i
< 4; ++i
) {
1989 /* E.g. mov(16) m2.0<1>:f r2.0<8;8,1>:f { Align1, H1 } */
1990 brw_MOV(&func
, offset(mrf_rt_write
, mrf_offset
),
1991 offset(vec8(texture_data
[0]), 2*i
));
1995 /* Now write to the render target and terminate the thread */
1997 16 /* dispatch_width */,
1998 base_mrf
/* msg_reg_nr */,
1999 mrf_rt_write
/* src0 */,
2000 BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE
,
2001 BRW_BLORP_RENDERBUFFER_BINDING_TABLE_INDEX
,
2002 mrf_offset
/* msg_length. TODO: Should be smaller for non-RGBA formats. */,
2003 0 /* response_length */,
2010 brw_blorp_coord_transform_params::setup(GLfloat src0
, GLfloat src1
,
2011 GLfloat dst0
, GLfloat dst1
,
2014 float scale
= (src1
- src0
) / (dst1
- dst0
);
2016 /* When not mirroring a coordinate (say, X), we need:
2017 * src_x - src_x0 = (dst_x - dst_x0 + 0.5) * scale
2019 * src_x = src_x0 + (dst_x - dst_x0 + 0.5) * scale
2021 * blorp program uses "round toward zero" to convert the
2022 * transformed floating point coordinates to integer coordinates,
2023 * whereas the behaviour we actually want is "round to nearest",
2024 * so 0.5 provides the necessary correction.
2027 offset
= src0
+ (-dst0
+ 0.5) * scale
;
2029 /* When mirroring X we need:
2030 * src_x - src_x0 = dst_x1 - dst_x - 0.5
2032 * src_x = src_x0 + (dst_x1 -dst_x - 0.5) * scale
2034 multiplier
= -scale
;
2035 offset
= src0
+ (dst1
- 0.5) * scale
;
2041 * Determine which MSAA layout the GPU pipeline should be configured for,
2042 * based on the chip generation, the number of samples, and the true layout of
2043 * the image in memory.
2045 inline intel_msaa_layout
2046 compute_msaa_layout_for_pipeline(struct brw_context
*brw
, unsigned num_samples
,
2047 intel_msaa_layout true_layout
)
2049 if (num_samples
<= 1) {
2050 /* When configuring the GPU for non-MSAA, we can still accommodate IMS
2051 * format buffers, by transforming coordinates appropriately.
2053 assert(true_layout
== INTEL_MSAA_LAYOUT_NONE
||
2054 true_layout
== INTEL_MSAA_LAYOUT_IMS
);
2055 return INTEL_MSAA_LAYOUT_NONE
;
2057 assert(true_layout
!= INTEL_MSAA_LAYOUT_NONE
);
2060 /* Prior to Gen7, all MSAA surfaces use IMS layout. */
2061 if (brw
->gen
== 6) {
2062 assert(true_layout
== INTEL_MSAA_LAYOUT_IMS
);
2069 brw_blorp_blit_params::brw_blorp_blit_params(struct brw_context
*brw
,
2070 struct intel_mipmap_tree
*src_mt
,
2071 unsigned src_level
, unsigned src_layer
,
2072 struct intel_mipmap_tree
*dst_mt
,
2073 unsigned dst_level
, unsigned dst_layer
,
2074 GLfloat src_x0
, GLfloat src_y0
,
2075 GLfloat src_x1
, GLfloat src_y1
,
2076 GLfloat dst_x0
, GLfloat dst_y0
,
2077 GLfloat dst_x1
, GLfloat dst_y1
,
2079 bool mirror_x
, bool mirror_y
)
2081 struct gl_context
*ctx
= &brw
->ctx
;
2082 const struct gl_framebuffer
*read_fb
= ctx
->ReadBuffer
;
2084 src
.set(brw
, src_mt
, src_level
, src_layer
, false);
2085 dst
.set(brw
, dst_mt
, dst_level
, dst_layer
, true);
2087 /* Even though we do multisample resolves at the time of the blit, OpenGL
2088 * specification defines them as if they happen at the time of rendering,
2089 * which means that the type of averaging we do during the resolve should
2090 * only depend on the source format; the destination format should be
2091 * ignored. But, specification doesn't seem to be strict about it.
2093 * It has been observed that mulitisample resolves produce slightly better
2094 * looking images when averaging is done using destination format. NVIDIA's
2095 * proprietary OpenGL driver also follow this approach. So, we choose to
2096 * follow it in our driver.
2098 * When multisampling, if the source and destination formats are equal
2099 * (aside from the color space), we choose to blit in sRGB space to get
2100 * this higher quality image.
2102 if (src
.num_samples
> 1 &&
2103 _mesa_get_format_color_encoding(dst_mt
->format
) == GL_SRGB
&&
2104 _mesa_get_srgb_format_linear(src_mt
->format
) ==
2105 _mesa_get_srgb_format_linear(dst_mt
->format
)) {
2106 dst
.brw_surfaceformat
= brw_format_for_mesa_format(dst_mt
->format
);
2107 src
.brw_surfaceformat
= dst
.brw_surfaceformat
;
2110 /* When doing a multisample resolve of a GL_LUMINANCE32F or GL_INTENSITY32F
2111 * texture, the above code configures the source format for L32_FLOAT or
2112 * I32_FLOAT, and the destination format for R32_FLOAT. On Sandy Bridge,
2113 * the SAMPLE message appears to handle multisampled L32_FLOAT and
2114 * I32_FLOAT textures incorrectly, resulting in blocky artifacts. So work
2115 * around the problem by using a source format of R32_FLOAT. This
2116 * shouldn't affect rendering correctness, since the destination format is
2117 * R32_FLOAT, so only the contents of the red channel matters.
2119 if (brw
->gen
== 6 && src
.num_samples
> 1 && dst
.num_samples
<= 1 &&
2120 src_mt
->format
== dst_mt
->format
&&
2121 dst
.brw_surfaceformat
== BRW_SURFACEFORMAT_R32_FLOAT
) {
2122 src
.brw_surfaceformat
= dst
.brw_surfaceformat
;
2126 memset(&wm_prog_key
, 0, sizeof(wm_prog_key
));
2128 /* texture_data_type indicates the register type that should be used to
2129 * manipulate texture data.
2131 switch (_mesa_get_format_datatype(src_mt
->format
)) {
2132 case GL_UNSIGNED_NORMALIZED
:
2133 case GL_SIGNED_NORMALIZED
:
2135 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_F
;
2137 case GL_UNSIGNED_INT
:
2138 if (src_mt
->format
== MESA_FORMAT_S8
) {
2139 /* We process stencil as though it's an unsigned normalized color */
2140 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_F
;
2142 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_UD
;
2146 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_D
;
2149 assert(!"Unrecognized blorp format");
2154 /* Gen7's rendering hardware only supports the IMS layout for depth and
2155 * stencil render targets. Blorp always maps its destination surface as
2156 * a color render target (even if it's actually a depth or stencil
2157 * buffer). So if the destination is IMS, we'll have to map it as a
2158 * single-sampled texture and interleave the samples ourselves.
2160 if (dst_mt
->msaa_layout
== INTEL_MSAA_LAYOUT_IMS
)
2161 dst
.num_samples
= 0;
2164 if (dst
.map_stencil_as_y_tiled
&& dst
.num_samples
> 1) {
2165 /* If the destination surface is a W-tiled multisampled stencil buffer
2166 * that we're mapping as Y tiled, then we need to arrange for the WM
2167 * program to run once per sample rather than once per pixel, because
2168 * the memory layout of related samples doesn't match between W and Y
2171 wm_prog_key
.persample_msaa_dispatch
= true;
2174 if (src
.num_samples
> 0 && dst
.num_samples
> 1) {
2175 /* We are blitting from a multisample buffer to a multisample buffer, so
2176 * we must preserve samples within a pixel. This means we have to
2177 * arrange for the WM program to run once per sample rather than once
2180 wm_prog_key
.persample_msaa_dispatch
= true;
2183 /* Scaled blitting or not. */
2184 wm_prog_key
.blit_scaled
=
2185 ((dst_x1
- dst_x0
) == (src_x1
- src_x0
) &&
2186 (dst_y1
- dst_y0
) == (src_y1
- src_y0
)) ? false : true;
2188 /* Scaling factors used for bilinear filtering in multisample scaled
2191 wm_prog_key
.x_scale
= 2.0;
2192 wm_prog_key
.y_scale
= src_mt
->num_samples
/ 2.0;
2194 if (filter
== GL_LINEAR
&& src
.num_samples
<= 1 && dst
.num_samples
<= 1)
2195 wm_prog_key
.bilinear_filter
= true;
2197 /* The render path must be configured to use the same number of samples as
2198 * the destination buffer.
2200 num_samples
= dst
.num_samples
;
2202 GLenum base_format
= _mesa_get_format_base_format(src_mt
->format
);
2203 if (base_format
!= GL_DEPTH_COMPONENT
&& /* TODO: what about depth/stencil? */
2204 base_format
!= GL_STENCIL_INDEX
&&
2205 src_mt
->num_samples
> 1 && dst_mt
->num_samples
<= 1) {
2206 /* We are downsampling a color buffer, so blend. */
2207 wm_prog_key
.blend
= true;
2210 /* src_samples and dst_samples are the true sample counts */
2211 wm_prog_key
.src_samples
= src_mt
->num_samples
;
2212 wm_prog_key
.dst_samples
= dst_mt
->num_samples
;
2214 /* tex_samples and rt_samples are the sample counts that are set up in
2217 wm_prog_key
.tex_samples
= src
.num_samples
;
2218 wm_prog_key
.rt_samples
= dst
.num_samples
;
2220 /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
2221 * use to access the source and destination surfaces.
2223 wm_prog_key
.tex_layout
=
2224 compute_msaa_layout_for_pipeline(brw
, src
.num_samples
, src
.msaa_layout
);
2225 wm_prog_key
.rt_layout
=
2226 compute_msaa_layout_for_pipeline(brw
, dst
.num_samples
, dst
.msaa_layout
);
2228 /* src_layout and dst_layout indicate the true MSAA layout used by src and
2231 wm_prog_key
.src_layout
= src_mt
->msaa_layout
;
2232 wm_prog_key
.dst_layout
= dst_mt
->msaa_layout
;
2234 wm_prog_key
.src_tiled_w
= src
.map_stencil_as_y_tiled
;
2235 wm_prog_key
.dst_tiled_w
= dst
.map_stencil_as_y_tiled
;
2236 x0
= wm_push_consts
.dst_x0
= dst_x0
;
2237 y0
= wm_push_consts
.dst_y0
= dst_y0
;
2238 x1
= wm_push_consts
.dst_x1
= dst_x1
;
2239 y1
= wm_push_consts
.dst_y1
= dst_y1
;
2240 wm_push_consts
.rect_grid_x1
= read_fb
->Width
* wm_prog_key
.x_scale
- 1.0;
2241 wm_push_consts
.rect_grid_y1
= read_fb
->Height
* wm_prog_key
.y_scale
- 1.0;
2243 wm_push_consts
.x_transform
.setup(src_x0
, src_x1
, dst_x0
, dst_x1
, mirror_x
);
2244 wm_push_consts
.y_transform
.setup(src_y0
, src_y1
, dst_y0
, dst_y1
, mirror_y
);
2246 if (dst
.num_samples
<= 1 && dst_mt
->num_samples
> 1) {
2247 /* We must expand the rectangle we send through the rendering pipeline,
2248 * to account for the fact that we are mapping the destination region as
2249 * single-sampled when it is in fact multisampled. We must also align
2250 * it to a multiple of the multisampling pattern, because the
2251 * differences between multisampled and single-sampled surface formats
2252 * will mean that pixels are scrambled within the multisampling pattern.
2253 * TODO: what if this makes the coordinates too large?
2255 * Note: this only works if the destination surface uses the IMS layout.
2256 * If it's UMS, then we have no choice but to set up the rendering
2257 * pipeline as multisampled.
2259 assert(dst_mt
->msaa_layout
== INTEL_MSAA_LAYOUT_IMS
);
2260 switch (dst_mt
->num_samples
) {
2262 x0
= ROUND_DOWN_TO(x0
* 2, 4);
2263 y0
= ROUND_DOWN_TO(y0
* 2, 4);
2264 x1
= ALIGN(x1
* 2, 4);
2265 y1
= ALIGN(y1
* 2, 4);
2268 x0
= ROUND_DOWN_TO(x0
* 4, 8);
2269 y0
= ROUND_DOWN_TO(y0
* 2, 4);
2270 x1
= ALIGN(x1
* 4, 8);
2271 y1
= ALIGN(y1
* 2, 4);
2274 assert(!"Unrecognized sample count in brw_blorp_blit_params ctor");
2277 wm_prog_key
.use_kill
= true;
2280 if (dst
.map_stencil_as_y_tiled
) {
2281 /* We must modify the rectangle we send through the rendering pipeline
2282 * (and the size and x/y offset of the destination surface), to account
2283 * for the fact that we are mapping it as Y-tiled when it is in fact
2286 * Both Y tiling and W tiling can be understood as organizations of
2287 * 32-byte sub-tiles; within each 32-byte sub-tile, the layout of pixels
2288 * is different, but the layout of the 32-byte sub-tiles within the 4k
2289 * tile is the same (8 sub-tiles across by 16 sub-tiles down, in
2290 * column-major order). In Y tiling, the sub-tiles are 16 bytes wide
2291 * and 2 rows high; in W tiling, they are 8 bytes wide and 4 rows high.
2293 * Therefore, to account for the layout differences within the 32-byte
2294 * sub-tiles, we must expand the rectangle so the X coordinates of its
2295 * edges are multiples of 8 (the W sub-tile width), and its Y
2296 * coordinates of its edges are multiples of 4 (the W sub-tile height).
2297 * Then we need to scale the X and Y coordinates of the rectangle to
2298 * account for the differences in aspect ratio between the Y and W
2299 * sub-tiles. We need to modify the layer width and height similarly.
2301 * A correction needs to be applied when MSAA is in use: since
2302 * INTEL_MSAA_LAYOUT_IMS uses an interleaving pattern whose height is 4,
2303 * we need to align the Y coordinates to multiples of 8, so that when
2304 * they are divided by two they are still multiples of 4.
2306 * Note: Since the x/y offset of the surface will be applied using the
2307 * SURFACE_STATE command packet, it will be invisible to the swizzling
2308 * code in the shader; therefore it needs to be in a multiple of the
2309 * 32-byte sub-tile size. Fortunately it is, since the sub-tile is 8
2310 * pixels wide and 4 pixels high (when viewed as a W-tiled stencil
2311 * buffer), and the miplevel alignment used for stencil buffers is 8
2312 * pixels horizontally and either 4 or 8 pixels vertically (see
2313 * intel_horizontal_texture_alignment_unit() and
2314 * intel_vertical_texture_alignment_unit()).
2316 * Note: Also, since the SURFACE_STATE command packet can only apply
2317 * offsets that are multiples of 4 pixels horizontally and 2 pixels
2318 * vertically, it is important that the offsets will be multiples of
2319 * these sizes after they are converted into Y-tiled coordinates.
2320 * Fortunately they will be, since we know from above that the offsets
2321 * are a multiple of the 32-byte sub-tile size, and in Y-tiled
2322 * coordinates the sub-tile is 16 pixels wide and 2 pixels high.
2324 * TODO: what if this makes the coordinates (or the texture size) too
2327 const unsigned x_align
= 8, y_align
= dst
.num_samples
!= 0 ? 8 : 4;
2328 x0
= ROUND_DOWN_TO(x0
, x_align
) * 2;
2329 y0
= ROUND_DOWN_TO(y0
, y_align
) / 2;
2330 x1
= ALIGN(x1
, x_align
) * 2;
2331 y1
= ALIGN(y1
, y_align
) / 2;
2332 dst
.width
= ALIGN(dst
.width
, x_align
) * 2;
2333 dst
.height
= ALIGN(dst
.height
, y_align
) / 2;
2336 wm_prog_key
.use_kill
= true;
2339 if (src
.map_stencil_as_y_tiled
) {
2340 /* We must modify the size and x/y offset of the source surface to
2341 * account for the fact that we are mapping it as Y-tiled when it is in
2344 * See the comments above concerning x/y offset alignment for the
2345 * destination surface.
2347 * TODO: what if this makes the texture size too large?
2349 const unsigned x_align
= 8, y_align
= src
.num_samples
!= 0 ? 8 : 4;
2350 src
.width
= ALIGN(src
.width
, x_align
) * 2;
2351 src
.height
= ALIGN(src
.height
, y_align
) / 2;
2358 brw_blorp_blit_params::get_wm_prog(struct brw_context
*brw
,
2359 brw_blorp_prog_data
**prog_data
) const
2361 uint32_t prog_offset
= 0;
2362 if (!brw_search_cache(&brw
->cache
, BRW_BLORP_BLIT_PROG
,
2363 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
2364 &prog_offset
, prog_data
)) {
2365 brw_blorp_blit_program
prog(brw
, &this->wm_prog_key
);
2366 GLuint program_size
;
2367 const GLuint
*program
= prog
.compile(brw
, &program_size
);
2368 brw_upload_cache(&brw
->cache
, BRW_BLORP_BLIT_PROG
,
2369 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
2370 program
, program_size
,
2371 &prog
.prog_data
, sizeof(prog
.prog_data
),
2372 &prog_offset
, prog_data
);