2 * Copyright © 2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "main/context.h"
25 #include "main/teximage.h"
26 #include "main/fbobject.h"
28 #include "intel_fbo.h"
30 #include "brw_blorp.h"
31 #include "brw_context.h"
32 #include "brw_blorp_blit_eu.h"
33 #include "brw_state.h"
34 #include "brw_meta_util.h"
36 #define FILE_DEBUG_FLAG DEBUG_BLORP
38 static struct intel_mipmap_tree
*
39 find_miptree(GLbitfield buffer_bit
, struct intel_renderbuffer
*irb
)
41 struct intel_mipmap_tree
*mt
= irb
->mt
;
42 if (buffer_bit
== GL_STENCIL_BUFFER_BIT
&& mt
->stencil_mt
)
49 * Note: if the src (or dst) is a 2D multisample array texture on Gen7+ using
50 * INTEL_MSAA_LAYOUT_UMS or INTEL_MSAA_LAYOUT_CMS, src_layer (dst_layer) is
51 * the physical layer holding sample 0. So, for example, if
52 * src_mt->num_samples == 4, then logical layer n corresponds to src_layer ==
56 brw_blorp_blit_miptrees(struct brw_context
*brw
,
57 struct intel_mipmap_tree
*src_mt
,
58 unsigned src_level
, unsigned src_layer
,
59 mesa_format src_format
, int src_swizzle
,
60 struct intel_mipmap_tree
*dst_mt
,
61 unsigned dst_level
, unsigned dst_layer
,
62 mesa_format dst_format
,
63 float src_x0
, float src_y0
,
64 float src_x1
, float src_y1
,
65 float dst_x0
, float dst_y0
,
66 float dst_x1
, float dst_y1
,
67 GLenum filter
, bool mirror_x
, bool mirror_y
,
68 bool decode_srgb
, bool encode_srgb
)
70 /* Get ready to blit. This includes depth resolving the src and dst
71 * buffers if necessary. Note: it's not necessary to do a color resolve on
72 * the destination buffer because we use the standard render path to render
73 * to destination color buffers, and the standard render path is
75 * Lossless compression is only introduced for gen9 onwards whereas
76 * blorp is not supported even for gen8. Therefore it should be impossible
77 * to end up here with single sampled compressed surfaces.
79 assert(!intel_miptree_is_lossless_compressed(brw
, src_mt
));
80 assert(!intel_miptree_is_lossless_compressed(brw
, dst_mt
));
81 intel_miptree_resolve_color(brw
, src_mt
, 0);
82 intel_miptree_slice_resolve_depth(brw
, src_mt
, src_level
, src_layer
);
83 intel_miptree_slice_resolve_depth(brw
, dst_mt
, dst_level
, dst_layer
);
85 DBG("%s from %dx %s mt %p %d %d (%f,%f) (%f,%f)"
86 "to %dx %s mt %p %d %d (%f,%f) (%f,%f) (flip %d,%d)\n",
88 src_mt
->num_samples
, _mesa_get_format_name(src_mt
->format
), src_mt
,
89 src_level
, src_layer
, src_x0
, src_y0
, src_x1
, src_y1
,
90 dst_mt
->num_samples
, _mesa_get_format_name(dst_mt
->format
), dst_mt
,
91 dst_level
, dst_layer
, dst_x0
, dst_y0
, dst_x1
, dst_y1
,
94 if (!decode_srgb
&& _mesa_get_format_color_encoding(src_format
) == GL_SRGB
)
95 src_format
= _mesa_get_srgb_format_linear(src_format
);
97 if (!encode_srgb
&& _mesa_get_format_color_encoding(dst_format
) == GL_SRGB
)
98 dst_format
= _mesa_get_srgb_format_linear(dst_format
);
100 brw_blorp_blit_params
params(brw
,
101 src_mt
, src_level
, src_layer
, src_format
,
102 dst_mt
, dst_level
, dst_layer
, dst_format
,
107 filter
, mirror_x
, mirror_y
);
108 params
.src
.swizzle
= src_swizzle
;
110 brw_blorp_exec(brw
, ¶ms
);
112 intel_miptree_slice_set_needs_hiz_resolve(dst_mt
, dst_level
, dst_layer
);
116 blorp_get_texture_swizzle(const struct intel_renderbuffer
*irb
)
118 return irb
->Base
.Base
._BaseFormat
== GL_RGB
?
119 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ONE
) :
124 do_blorp_blit(struct brw_context
*brw
, GLbitfield buffer_bit
,
125 struct intel_renderbuffer
*src_irb
, mesa_format src_format
,
126 struct intel_renderbuffer
*dst_irb
, mesa_format dst_format
,
127 GLfloat srcX0
, GLfloat srcY0
, GLfloat srcX1
, GLfloat srcY1
,
128 GLfloat dstX0
, GLfloat dstY0
, GLfloat dstX1
, GLfloat dstY1
,
129 GLenum filter
, bool mirror_x
, bool mirror_y
)
131 /* Find source/dst miptrees */
132 struct intel_mipmap_tree
*src_mt
= find_miptree(buffer_bit
, src_irb
);
133 struct intel_mipmap_tree
*dst_mt
= find_miptree(buffer_bit
, dst_irb
);
135 const bool es3
= _mesa_is_gles3(&brw
->ctx
);
137 brw_blorp_blit_miptrees(brw
,
138 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
139 src_format
, blorp_get_texture_swizzle(src_irb
),
140 dst_mt
, dst_irb
->mt_level
, dst_irb
->mt_layer
,
142 srcX0
, srcY0
, srcX1
, srcY1
,
143 dstX0
, dstY0
, dstX1
, dstY1
,
144 filter
, mirror_x
, mirror_y
,
147 dst_irb
->need_downsample
= true;
151 try_blorp_blit(struct brw_context
*brw
,
152 const struct gl_framebuffer
*read_fb
,
153 const struct gl_framebuffer
*draw_fb
,
154 GLfloat srcX0
, GLfloat srcY0
, GLfloat srcX1
, GLfloat srcY1
,
155 GLfloat dstX0
, GLfloat dstY0
, GLfloat dstX1
, GLfloat dstY1
,
156 GLenum filter
, GLbitfield buffer_bit
)
158 struct gl_context
*ctx
= &brw
->ctx
;
160 /* Sync up the state of window system buffers. We need to do this before
161 * we go looking for the buffers.
163 intel_prepare_render(brw
);
165 bool mirror_x
, mirror_y
;
166 if (brw_meta_mirror_clip_and_scissor(ctx
, read_fb
, draw_fb
,
167 &srcX0
, &srcY0
, &srcX1
, &srcY1
,
168 &dstX0
, &dstY0
, &dstX1
, &dstY1
,
169 &mirror_x
, &mirror_y
))
173 struct intel_renderbuffer
*src_irb
;
174 struct intel_renderbuffer
*dst_irb
;
175 struct intel_mipmap_tree
*src_mt
;
176 struct intel_mipmap_tree
*dst_mt
;
177 switch (buffer_bit
) {
178 case GL_COLOR_BUFFER_BIT
:
179 src_irb
= intel_renderbuffer(read_fb
->_ColorReadBuffer
);
180 for (unsigned i
= 0; i
< draw_fb
->_NumColorDrawBuffers
; ++i
) {
181 dst_irb
= intel_renderbuffer(draw_fb
->_ColorDrawBuffers
[i
]);
183 do_blorp_blit(brw
, buffer_bit
,
184 src_irb
, src_irb
->Base
.Base
.Format
,
185 dst_irb
, dst_irb
->Base
.Base
.Format
,
186 srcX0
, srcY0
, srcX1
, srcY1
,
187 dstX0
, dstY0
, dstX1
, dstY1
,
188 filter
, mirror_x
, mirror_y
);
191 case GL_DEPTH_BUFFER_BIT
:
193 intel_renderbuffer(read_fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
195 intel_renderbuffer(draw_fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
196 src_mt
= find_miptree(buffer_bit
, src_irb
);
197 dst_mt
= find_miptree(buffer_bit
, dst_irb
);
199 /* We can't handle format conversions between Z24 and other formats
200 * since we have to lie about the surface format. See the comments in
201 * brw_blorp_surface_info::set().
203 if ((src_mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
) !=
204 (dst_mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
))
207 do_blorp_blit(brw
, buffer_bit
, src_irb
, MESA_FORMAT_NONE
,
208 dst_irb
, MESA_FORMAT_NONE
, srcX0
, srcY0
,
209 srcX1
, srcY1
, dstX0
, dstY0
, dstX1
, dstY1
,
210 filter
, mirror_x
, mirror_y
);
212 case GL_STENCIL_BUFFER_BIT
:
214 intel_renderbuffer(read_fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
216 intel_renderbuffer(draw_fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
217 do_blorp_blit(brw
, buffer_bit
, src_irb
, MESA_FORMAT_NONE
,
218 dst_irb
, MESA_FORMAT_NONE
, srcX0
, srcY0
,
219 srcX1
, srcY1
, dstX0
, dstY0
, dstX1
, dstY1
,
220 filter
, mirror_x
, mirror_y
);
223 unreachable("not reached");
230 brw_blorp_copytexsubimage(struct brw_context
*brw
,
231 struct gl_renderbuffer
*src_rb
,
232 struct gl_texture_image
*dst_image
,
234 int srcX0
, int srcY0
,
235 int dstX0
, int dstY0
,
236 int width
, int height
)
238 struct gl_context
*ctx
= &brw
->ctx
;
239 struct intel_renderbuffer
*src_irb
= intel_renderbuffer(src_rb
);
240 struct intel_texture_image
*intel_image
= intel_texture_image(dst_image
);
242 /* No pixel transfer operations (zoom, bias, mapping), just a blit */
243 if (brw
->ctx
._ImageTransferState
)
246 /* Sync up the state of window system buffers. We need to do this before
247 * we go looking at the src renderbuffer's miptree.
249 intel_prepare_render(brw
);
251 struct intel_mipmap_tree
*src_mt
= src_irb
->mt
;
252 struct intel_mipmap_tree
*dst_mt
= intel_image
->mt
;
254 /* There is support only for four and eight samples. */
255 if (src_mt
->num_samples
== 2 || dst_mt
->num_samples
== 2 ||
256 src_mt
->num_samples
> 8 || dst_mt
->num_samples
> 8)
259 /* BLORP is only supported for Gen6-7. */
260 if (brw
->gen
< 6 || brw
->gen
> 7)
263 if (_mesa_get_format_base_format(src_rb
->Format
) !=
264 _mesa_get_format_base_format(dst_image
->TexFormat
)) {
268 /* We can't handle format conversions between Z24 and other formats since
269 * we have to lie about the surface format. See the comments in
270 * brw_blorp_surface_info::set().
272 if ((src_mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
) !=
273 (dst_mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
)) {
277 if (!brw
->format_supported_as_render_target
[dst_image
->TexFormat
])
280 /* Source clipping shouldn't be necessary, since copytexsubimage (in
281 * src/mesa/main/teximage.c) calls _mesa_clip_copytexsubimage() which
284 * Destination clipping shouldn't be necessary since the restrictions on
285 * glCopyTexSubImage prevent the user from specifying a destination rectangle
286 * that falls outside the bounds of the destination texture.
287 * See error_check_subtexture_dimensions().
290 int srcY1
= srcY0
+ height
;
291 int srcX1
= srcX0
+ width
;
292 int dstX1
= dstX0
+ width
;
293 int dstY1
= dstY0
+ height
;
295 /* Account for the fact that in the system framebuffer, the origin is at
298 bool mirror_y
= false;
299 if (_mesa_is_winsys_fbo(ctx
->ReadBuffer
)) {
300 GLint tmp
= src_rb
->Height
- srcY0
;
301 srcY0
= src_rb
->Height
- srcY1
;
306 /* Account for face selection and texture view MinLayer */
307 int dst_slice
= slice
+ dst_image
->TexObject
->MinLayer
+ dst_image
->Face
;
308 int dst_level
= dst_image
->Level
+ dst_image
->TexObject
->MinLevel
;
310 brw_blorp_blit_miptrees(brw
,
311 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
312 src_rb
->Format
, blorp_get_texture_swizzle(src_irb
),
313 dst_mt
, dst_level
, dst_slice
,
314 dst_image
->TexFormat
,
315 srcX0
, srcY0
, srcX1
, srcY1
,
316 dstX0
, dstY0
, dstX1
, dstY1
,
317 GL_NEAREST
, false, mirror_y
,
320 /* If we're copying to a packed depth stencil texture and the source
321 * framebuffer has separate stencil, we need to also copy the stencil data
324 src_rb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
325 if (_mesa_get_format_bits(dst_image
->TexFormat
, GL_STENCIL_BITS
) > 0 &&
327 src_irb
= intel_renderbuffer(src_rb
);
328 src_mt
= src_irb
->mt
;
330 if (src_mt
->stencil_mt
)
331 src_mt
= src_mt
->stencil_mt
;
332 if (dst_mt
->stencil_mt
)
333 dst_mt
= dst_mt
->stencil_mt
;
335 if (src_mt
!= dst_mt
) {
336 brw_blorp_blit_miptrees(brw
,
337 src_mt
, src_irb
->mt_level
, src_irb
->mt_layer
,
339 blorp_get_texture_swizzle(src_irb
),
340 dst_mt
, dst_level
, dst_slice
,
342 srcX0
, srcY0
, srcX1
, srcY1
,
343 dstX0
, dstY0
, dstX1
, dstY1
,
344 GL_NEAREST
, false, mirror_y
,
354 brw_blorp_framebuffer(struct brw_context
*brw
,
355 struct gl_framebuffer
*readFb
,
356 struct gl_framebuffer
*drawFb
,
357 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
358 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
359 GLbitfield mask
, GLenum filter
)
361 /* BLORP is not supported before Gen6. */
362 if (brw
->gen
< 6 || brw
->gen
>= 8)
365 /* There is support only for four and eight samples. */
366 if (readFb
->Visual
.samples
== 2 || drawFb
->Visual
.samples
== 2 ||
367 readFb
->Visual
.samples
> 8 || drawFb
->Visual
.samples
> 8)
370 static GLbitfield buffer_bits
[] = {
373 GL_STENCIL_BUFFER_BIT
,
376 for (unsigned int i
= 0; i
< ARRAY_SIZE(buffer_bits
); ++i
) {
377 if ((mask
& buffer_bits
[i
]) &&
378 try_blorp_blit(brw
, readFb
, drawFb
,
379 srcX0
, srcY0
, srcX1
, srcY1
,
380 dstX0
, dstY0
, dstX1
, dstY1
,
381 filter
, buffer_bits
[i
])) {
382 mask
&= ~buffer_bits
[i
];
391 * Enum to specify the order of arguments in a sampler message
393 enum sampler_message_arg
395 SAMPLER_MESSAGE_ARG_U_FLOAT
,
396 SAMPLER_MESSAGE_ARG_V_FLOAT
,
397 SAMPLER_MESSAGE_ARG_U_INT
,
398 SAMPLER_MESSAGE_ARG_V_INT
,
399 SAMPLER_MESSAGE_ARG_R_INT
,
400 SAMPLER_MESSAGE_ARG_SI_INT
,
401 SAMPLER_MESSAGE_ARG_MCS_INT
,
402 SAMPLER_MESSAGE_ARG_ZERO_INT
,
406 * Generator for WM programs used in BLORP blits.
408 * The bulk of the work done by the WM program is to wrap and unwrap the
409 * coordinate transformations used by the hardware to store surfaces in
410 * memory. The hardware transforms a pixel location (X, Y, S) (where S is the
411 * sample index for a multisampled surface) to a memory offset by the
412 * following formulas:
414 * offset = tile(tiling_format, encode_msaa(num_samples, layout, X, Y, S))
415 * (X, Y, S) = decode_msaa(num_samples, layout, detile(tiling_format, offset))
417 * For a single-sampled surface, or for a multisampled surface using
418 * INTEL_MSAA_LAYOUT_UMS, encode_msaa() and decode_msaa are the identity
421 * encode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
422 * decode_msaa(1, NONE, X, Y, 0) = (X, Y, 0)
423 * encode_msaa(n, UMS, X, Y, S) = (X, Y, S)
424 * decode_msaa(n, UMS, X, Y, S) = (X, Y, S)
426 * For a 4x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
427 * embeds the sample number into bit 1 of the X and Y coordinates:
429 * encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
430 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
431 * Y' = (Y & ~0b1 ) << 1 | (S & 0b10) | (Y & 0b1)
432 * decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
433 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
434 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
435 * S = (Y & 0b10) | (X & 0b10) >> 1
437 * For an 8x multisampled surface using INTEL_MSAA_LAYOUT_IMS, encode_msaa()
438 * embeds the sample number into bits 1 and 2 of the X coordinate and bit 1 of
441 * encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
442 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1 | (X & 0b1)
443 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
444 * decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
445 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
446 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
447 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
449 * For X tiling, tile() combines together the low-order bits of the X and Y
450 * coordinates in the pattern 0byyyxxxxxxxxx, creating 4k tiles that are 512
451 * bytes wide and 8 rows high:
453 * tile(x_tiled, X, Y, S) = A
454 * where A = tile_num << 12 | offset
455 * tile_num = (Y' >> 3) * tile_pitch + (X' >> 9)
456 * offset = (Y' & 0b111) << 9
457 * | (X & 0b111111111)
459 * Y' = Y + S * qpitch
460 * detile(x_tiled, A) = (X, Y, S)
464 * Y' = (tile_num / tile_pitch) << 3
465 * | (A & 0b111000000000) >> 9
466 * X' = (tile_num % tile_pitch) << 9
467 * | (A & 0b111111111)
469 * (In all tiling formulas, cpp is the number of bytes occupied by a single
470 * sample ("chars per pixel"), tile_pitch is the number of 4k tiles required
471 * to fill the width of the surface, and qpitch is the spacing (in rows)
472 * between array slices).
474 * For Y tiling, tile() combines together the low-order bits of the X and Y
475 * coordinates in the pattern 0bxxxyyyyyxxxx, creating 4k tiles that are 128
476 * bytes wide and 32 rows high:
478 * tile(y_tiled, X, Y, S) = A
479 * where A = tile_num << 12 | offset
480 * tile_num = (Y' >> 5) * tile_pitch + (X' >> 7)
481 * offset = (X' & 0b1110000) << 5
482 * | (Y' & 0b11111) << 4
485 * Y' = Y + S * qpitch
486 * detile(y_tiled, A) = (X, Y, S)
490 * Y' = (tile_num / tile_pitch) << 5
491 * | (A & 0b111110000) >> 4
492 * X' = (tile_num % tile_pitch) << 7
493 * | (A & 0b111000000000) >> 5
496 * For W tiling, tile() combines together the low-order bits of the X and Y
497 * coordinates in the pattern 0bxxxyyyyxyxyx, creating 4k tiles that are 64
498 * bytes wide and 64 rows high (note that W tiling is only used for stencil
499 * buffers, which always have cpp = 1 and S=0):
501 * tile(w_tiled, X, Y, S) = A
502 * where A = tile_num << 12 | offset
503 * tile_num = (Y' >> 6) * tile_pitch + (X' >> 6)
504 * offset = (X' & 0b111000) << 6
505 * | (Y' & 0b111100) << 3
506 * | (X' & 0b100) << 2
512 * Y' = Y + S * qpitch
513 * detile(w_tiled, A) = (X, Y, S)
514 * where X = X' / cpp = X'
515 * Y = Y' % qpitch = Y'
517 * Y' = (tile_num / tile_pitch) << 6
518 * | (A & 0b111100000) >> 3
519 * | (A & 0b1000) >> 2
521 * X' = (tile_num % tile_pitch) << 6
522 * | (A & 0b111000000000) >> 6
523 * | (A & 0b10000) >> 2
527 * Finally, for a non-tiled surface, tile() simply combines together the X and
528 * Y coordinates in the natural way:
530 * tile(untiled, X, Y, S) = A
531 * where A = Y * pitch + X'
533 * Y' = Y + S * qpitch
534 * detile(untiled, A) = (X, Y, S)
541 * (In these formulas, pitch is the number of bytes occupied by a single row
544 class brw_blorp_blit_program
: public brw_blorp_eu_emitter
547 brw_blorp_blit_program(struct brw_context
*brw
,
548 const brw_blorp_blit_prog_key
*key
, bool debug_flag
);
550 const GLuint
*compile(struct brw_context
*brw
, GLuint
*program_size
);
552 brw_blorp_prog_data prog_data
;
556 void alloc_push_const_regs(int base_reg
);
557 void compute_frag_coords();
558 void translate_tiling(bool old_tiled_w
, bool new_tiled_w
);
559 void encode_msaa(unsigned num_samples
, intel_msaa_layout layout
);
560 void decode_msaa(unsigned num_samples
, intel_msaa_layout layout
);
561 void translate_dst_to_src();
562 void clamp_tex_coords(struct brw_reg regX
, struct brw_reg regY
,
563 struct brw_reg clampX0
, struct brw_reg clampY0
,
564 struct brw_reg clampX1
, struct brw_reg clampY1
);
565 void single_to_blend();
566 void manual_blend_average(unsigned num_samples
);
567 void manual_blend_bilinear(unsigned num_samples
);
568 void sample(struct brw_reg dst
);
569 void texel_fetch(struct brw_reg dst
);
571 void texture_lookup(struct brw_reg dst
, enum opcode op
,
572 const sampler_message_arg
*args
, int num_args
);
573 void render_target_write();
576 * Base-2 logarithm of the maximum number of samples that can be blended.
578 static const unsigned LOG2_MAX_BLEND_SAMPLES
= 3;
580 struct brw_context
*brw
;
581 const brw_blorp_blit_prog_key
*key
;
583 /* Thread dispatch header */
586 /* Pixel X/Y coordinates (always in R1). */
590 struct brw_reg dst_x0
;
591 struct brw_reg dst_x1
;
592 struct brw_reg dst_y0
;
593 struct brw_reg dst_y1
;
594 /* Top right coordinates of the rectangular grid used for scaled blitting */
595 struct brw_reg rect_grid_x1
;
596 struct brw_reg rect_grid_y1
;
598 struct brw_reg multiplier
;
599 struct brw_reg offset
;
600 } x_transform
, y_transform
;
601 struct brw_reg src_z
;
603 /* Data read from texture (4 vec16's per array element) */
604 struct brw_reg texture_data
[LOG2_MAX_BLEND_SAMPLES
+ 1];
606 /* Auxiliary storage for the contents of the MCS surface.
608 * Since the sampler always returns 8 registers worth of data, this is 8
609 * registers wide, even though we only use the first 2 registers of it.
611 struct brw_reg mcs_data
;
613 /* X coordinates. We have two of them so that we can perform coordinate
614 * transformations easily.
616 struct brw_reg x_coords
[2];
618 /* Y coordinates. We have two of them so that we can perform coordinate
619 * transformations easily.
621 struct brw_reg y_coords
[2];
623 /* X, Y coordinates of the pixel from which we need to fetch the specific
624 * sample. These are used for multisample scaled blitting.
626 struct brw_reg x_sample_coords
;
627 struct brw_reg y_sample_coords
;
629 /* Fractional parts of the x and y coordinates, used as bilinear interpolation coefficients */
630 struct brw_reg x_frac
;
631 struct brw_reg y_frac
;
633 /* Which element of x_coords and y_coords is currently in use.
637 /* True if, at the point in the program currently being compiled, the
638 * sample index is known to be zero.
642 /* Register storing the sample index when s_is_zero is false. */
643 struct brw_reg sample_index
;
649 /* MRF used for sampling and render target writes */
653 brw_blorp_blit_program::brw_blorp_blit_program(
654 struct brw_context
*brw
,
655 const brw_blorp_blit_prog_key
*key
,
657 : brw_blorp_eu_emitter(brw
, debug_flag
),
664 brw_blorp_blit_program::compile(struct brw_context
*brw
,
665 GLuint
*program_size
)
668 if (key
->dst_tiled_w
&& key
->rt_samples
> 0) {
669 /* If the destination image is W tiled and multisampled, then the thread
670 * must be dispatched once per sample, not once per pixel. This is
671 * necessary because after conversion between W and Y tiling, there's no
672 * guarantee that all samples corresponding to a single pixel will still
675 assert(key
->persample_msaa_dispatch
);
679 /* We are blending, which means we won't have an opportunity to
680 * translate the tiling and sample count for the texture surface. So
681 * the surface state for the texture must be configured with the correct
682 * tiling and sample count.
684 assert(!key
->src_tiled_w
);
685 assert(key
->tex_samples
== key
->src_samples
);
686 assert(key
->tex_layout
== key
->src_layout
);
687 assert(key
->tex_samples
> 0);
690 if (key
->persample_msaa_dispatch
) {
691 /* It only makes sense to do persample dispatch if the render target is
692 * configured as multisampled.
694 assert(key
->rt_samples
> 0);
697 /* Make sure layout is consistent with sample count */
698 assert((key
->tex_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
699 (key
->tex_samples
== 0));
700 assert((key
->rt_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
701 (key
->rt_samples
== 0));
702 assert((key
->src_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
703 (key
->src_samples
== 0));
704 assert((key
->dst_layout
== INTEL_MSAA_LAYOUT_NONE
) ==
705 (key
->dst_samples
== 0));
707 /* Set up prog_data */
708 memset(&prog_data
, 0, sizeof(prog_data
));
709 prog_data
.persample_msaa_dispatch
= key
->persample_msaa_dispatch
;
712 compute_frag_coords();
714 /* Render target and texture hardware don't support W tiling until Gen8. */
715 const bool rt_tiled_w
= false;
716 const bool tex_tiled_w
= brw
->gen
>= 8 && key
->src_tiled_w
;
718 /* The address that data will be written to is determined by the
719 * coordinates supplied to the WM thread and the tiling and sample count of
720 * the render target, according to the formula:
722 * (X, Y, S) = decode_msaa(rt_samples, detile(rt_tiling, offset))
724 * If the actual tiling and sample count of the destination surface are not
725 * the same as the configuration of the render target, then these
726 * coordinates are wrong and we have to adjust them to compensate for the
729 if (rt_tiled_w
!= key
->dst_tiled_w
||
730 key
->rt_samples
!= key
->dst_samples
||
731 key
->rt_layout
!= key
->dst_layout
) {
732 encode_msaa(key
->rt_samples
, key
->rt_layout
);
733 /* Now (X, Y, S) = detile(rt_tiling, offset) */
734 translate_tiling(rt_tiled_w
, key
->dst_tiled_w
);
735 /* Now (X, Y, S) = detile(dst_tiling, offset) */
736 decode_msaa(key
->dst_samples
, key
->dst_layout
);
739 /* Now (X, Y, S) = decode_msaa(dst_samples, detile(dst_tiling, offset)).
741 * That is: X, Y and S now contain the true coordinates and sample index of
742 * the data that the WM thread should output.
744 * If we need to kill pixels that are outside the destination rectangle,
745 * now is the time to do it.
749 emit_kill_if_outside_rect(x_coords
[xy_coord_index
],
750 y_coords
[xy_coord_index
],
751 dst_x0
, dst_x1
, dst_y0
, dst_y1
);
753 /* Next, apply a translation to obtain coordinates in the source image. */
754 translate_dst_to_src();
756 /* If the source image is not multisampled, then we want to fetch sample
757 * number 0, because that's the only sample there is.
759 if (key
->src_samples
== 0)
762 /* X, Y, and S are now the coordinates of the pixel in the source image
763 * that we want to texture from. Exception: if we are blending, then S is
764 * irrelevant, because we are going to fetch all samples.
766 if (key
->blend
&& !key
->blit_scaled
) {
768 /* Gen6 hardware an automatically blend using the SAMPLE message */
770 sample(texture_data
[0]);
772 /* Gen7+ hardware doesn't automaticaly blend. */
773 manual_blend_average(key
->src_samples
);
775 } else if(key
->blend
&& key
->blit_scaled
) {
776 manual_blend_bilinear(key
->src_samples
);
778 /* We aren't blending, which means we just want to fetch a single sample
779 * from the source surface. The address that we want to fetch from is
780 * related to the X, Y and S values according to the formula:
782 * (X, Y, S) = decode_msaa(src_samples, detile(src_tiling, offset)).
784 * If the actual tiling and sample count of the source surface are not
785 * the same as the configuration of the texture, then we need to adjust
786 * the coordinates to compensate for the difference.
788 if ((tex_tiled_w
!= key
->src_tiled_w
||
789 key
->tex_samples
!= key
->src_samples
||
790 key
->tex_layout
!= key
->src_layout
) &&
791 !key
->bilinear_filter
) {
792 encode_msaa(key
->src_samples
, key
->src_layout
);
793 /* Now (X, Y, S) = detile(src_tiling, offset) */
794 translate_tiling(key
->src_tiled_w
, tex_tiled_w
);
795 /* Now (X, Y, S) = detile(tex_tiling, offset) */
796 decode_msaa(key
->tex_samples
, key
->tex_layout
);
799 if (key
->bilinear_filter
) {
800 sample(texture_data
[0]);
803 /* Now (X, Y, S) = decode_msaa(tex_samples, detile(tex_tiling, offset)).
805 * In other words: X, Y, and S now contain values which, when passed to
806 * the texturing unit, will cause data to be read from the correct
807 * memory location. So we can fetch the texel now.
809 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
811 texel_fetch(texture_data
[0]);
815 /* Finally, write the fetched (or blended) value to the render target and
816 * terminate the thread.
818 render_target_write();
820 return get_program(program_size
);
824 brw_blorp_blit_program::alloc_push_const_regs(int base_reg
)
826 #define CONST_LOC(name) offsetof(brw_blorp_wm_push_constants, name)
827 #define ALLOC_REG(name, type) \
829 retype(brw_vec1_reg(BRW_GENERAL_REGISTER_FILE, \
830 base_reg + CONST_LOC(name) / 32, \
831 (CONST_LOC(name) % 32) / 4), type)
833 ALLOC_REG(dst_x0
, BRW_REGISTER_TYPE_UD
);
834 ALLOC_REG(dst_x1
, BRW_REGISTER_TYPE_UD
);
835 ALLOC_REG(dst_y0
, BRW_REGISTER_TYPE_UD
);
836 ALLOC_REG(dst_y1
, BRW_REGISTER_TYPE_UD
);
837 ALLOC_REG(rect_grid_x1
, BRW_REGISTER_TYPE_F
);
838 ALLOC_REG(rect_grid_y1
, BRW_REGISTER_TYPE_F
);
839 ALLOC_REG(x_transform
.multiplier
, BRW_REGISTER_TYPE_F
);
840 ALLOC_REG(x_transform
.offset
, BRW_REGISTER_TYPE_F
);
841 ALLOC_REG(y_transform
.multiplier
, BRW_REGISTER_TYPE_F
);
842 ALLOC_REG(y_transform
.offset
, BRW_REGISTER_TYPE_F
);
843 ALLOC_REG(src_z
, BRW_REGISTER_TYPE_UD
);
849 brw_blorp_blit_program::alloc_regs()
852 this->R0
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
853 this->R1
= retype(brw_vec8_grf(reg
++, 0), BRW_REGISTER_TYPE_UW
);
854 prog_data
.first_curbe_grf
= reg
;
855 alloc_push_const_regs(reg
);
856 reg
+= BRW_BLORP_NUM_PUSH_CONST_REGS
;
857 for (unsigned i
= 0; i
< ARRAY_SIZE(texture_data
); ++i
) {
858 this->texture_data
[i
] =
859 retype(vec16(brw_vec8_grf(reg
, 0)), key
->texture_data_type
);
863 retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
); reg
+= 8;
865 for (int i
= 0; i
< 2; ++i
) {
867 = retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
870 = retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
874 if (key
->blit_scaled
&& key
->blend
) {
875 this->x_sample_coords
= brw_vec8_grf(reg
, 0);
877 this->y_sample_coords
= brw_vec8_grf(reg
, 0);
879 this->x_frac
= brw_vec8_grf(reg
, 0);
881 this->y_frac
= brw_vec8_grf(reg
, 0);
885 this->xy_coord_index
= 0;
887 = retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
889 this->t1
= retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
891 this->t2
= retype(brw_vec8_grf(reg
, 0), BRW_REGISTER_TYPE_UD
);
894 /* Make sure we didn't run out of registers */
895 assert(reg
<= GEN7_MRF_HACK_START
);
898 this->base_mrf
= mrf
;
901 /* In the code that follows, X and Y can be used to quickly refer to the
902 * active elements of x_coords and y_coords, and Xp and Yp ("X prime" and "Y
903 * prime") to the inactive elements.
905 * S can be used to quickly refer to sample_index.
907 #define X x_coords[xy_coord_index]
908 #define Y y_coords[xy_coord_index]
909 #define Xp x_coords[!xy_coord_index]
910 #define Yp y_coords[!xy_coord_index]
911 #define S sample_index
913 /* Quickly swap the roles of (X, Y) and (Xp, Yp). Saves us from having to do
914 * MOVs to transfor (Xp, Yp) to (X, Y) after a coordinate transformation.
916 #define SWAP_XY_AND_XPYP() xy_coord_index = !xy_coord_index;
919 * Emit code to compute the X and Y coordinates of the pixels being rendered
920 * by this WM invocation.
922 * Assuming the render target is set up for Y tiling, these (X, Y) values are
923 * related to the address offset where outputs will be written by the formula:
925 * (X, Y, S) = decode_msaa(detile(offset)).
927 * (See brw_blorp_blit_program).
930 brw_blorp_blit_program::compute_frag_coords()
932 /* R1.2[15:0] = X coordinate of upper left pixel of subspan 0 (pixel 0)
933 * R1.3[15:0] = X coordinate of upper left pixel of subspan 1 (pixel 4)
934 * R1.4[15:0] = X coordinate of upper left pixel of subspan 2 (pixel 8)
935 * R1.5[15:0] = X coordinate of upper left pixel of subspan 3 (pixel 12)
937 * Pixels within a subspan are laid out in this arrangement:
941 * So, to compute the coordinates of each pixel, we need to read every 2nd
942 * 16-bit value (vstride=2) from R1, starting at the 4th 16-bit value
943 * (suboffset=4), and duplicate each value 4 times (hstride=0, width=4).
944 * In other words, the data we want to access is R1.4<2;4,0>UW.
946 * Then, we need to add the repeating sequence (0, 1, 0, 1, ...) to the
947 * result, since pixels n+1 and n+3 are in the right half of the subspan.
949 emit_add(vec16(retype(X
, BRW_REGISTER_TYPE_UW
)),
950 stride(suboffset(R1
, 4), 2, 4, 0), brw_imm_v(0x10101010));
952 /* Similarly, Y coordinates for subspans come from R1.2[31:16] through
953 * R1.5[31:16], so to get pixel Y coordinates we need to start at the 5th
954 * 16-bit value instead of the 4th (R1.5<2;4,0>UW instead of
957 * And we need to add the repeating sequence (0, 0, 1, 1, ...), since
958 * pixels n+2 and n+3 are in the bottom half of the subspan.
960 emit_add(vec16(retype(Y
, BRW_REGISTER_TYPE_UW
)),
961 stride(suboffset(R1
, 5), 2, 4, 0), brw_imm_v(0x11001100));
963 /* Move the coordinates to UD registers. */
964 emit_mov(vec16(Xp
), retype(X
, BRW_REGISTER_TYPE_UW
));
965 emit_mov(vec16(Yp
), retype(Y
, BRW_REGISTER_TYPE_UW
));
968 if (key
->persample_msaa_dispatch
) {
969 switch (key
->rt_samples
) {
971 /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 4.
972 * Therefore, subspan 0 will represent sample 0, subspan 1 will
973 * represent sample 1, and so on.
975 * So we need to populate S with the sequence (0, 0, 0, 0, 1, 1, 1,
976 * 1, 2, 2, 2, 2, 3, 3, 3, 3). The easiest way to do this is to
977 * populate a temporary variable with the sequence (0, 1, 2, 3), and
978 * then copy from it using vstride=1, width=4, hstride=0.
980 struct brw_reg t1_uw1
= retype(t1
, BRW_REGISTER_TYPE_UW
);
981 emit_mov(vec16(t1_uw1
), brw_imm_v(0x3210));
982 /* Move to UD sample_index register. */
983 emit_mov_8(S
, stride(t1_uw1
, 1, 4, 0));
984 emit_mov_8(offset(S
, 1), suboffset(stride(t1_uw1
, 1, 4, 0), 2));
988 /* The WM will be run in MSDISPMODE_PERSAMPLE with num_samples == 8.
989 * Therefore, subspan 0 will represent sample N (where N is 0 or 4),
990 * subspan 1 will represent sample 1, and so on. We can find the
991 * value of N by looking at R0.0 bits 7:6 ("Starting Sample Pair
992 * Index") and multiplying by two (since samples are always delivered
993 * in pairs). That is, we compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 &
996 * Then we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1, 2,
997 * 2, 2, 2, 3, 3, 3, 3), which we compute by populating a temporary
998 * variable with the sequence (0, 1, 2, 3), and then reading from it
999 * using vstride=1, width=4, hstride=0.
1001 struct brw_reg t1_ud1
= vec1(retype(t1
, BRW_REGISTER_TYPE_UD
));
1002 struct brw_reg t2_uw1
= retype(t2
, BRW_REGISTER_TYPE_UW
);
1003 struct brw_reg r0_ud1
= vec1(retype(R0
, BRW_REGISTER_TYPE_UD
));
1004 emit_and(t1_ud1
, r0_ud1
, brw_imm_ud(0xc0));
1005 emit_shr(t1_ud1
, t1_ud1
, brw_imm_ud(5));
1006 emit_mov(vec16(t2_uw1
), brw_imm_v(0x3210));
1007 emit_add(vec16(S
), retype(t1_ud1
, BRW_REGISTER_TYPE_UW
),
1008 stride(t2_uw1
, 1, 4, 0));
1009 emit_add_8(offset(S
, 1),
1010 retype(t1_ud1
, BRW_REGISTER_TYPE_UW
),
1011 suboffset(stride(t2_uw1
, 1, 4, 0), 2));
1015 unreachable("Unrecognized sample count in "
1016 "brw_blorp_blit_program::compute_frag_coords()");
1020 /* Either the destination surface is single-sampled, or the WM will be
1021 * run in MSDISPMODE_PERPIXEL (which causes a single fragment dispatch
1022 * per pixel). In either case, it's not meaningful to compute a sample
1023 * value. Just set it to 0.
1030 * Emit code to compensate for the difference between Y and W tiling.
1032 * This code modifies the X and Y coordinates according to the formula:
1034 * (X', Y', S') = detile(new_tiling, tile(old_tiling, X, Y, S))
1036 * (See brw_blorp_blit_program).
1038 * It can only translate between W and Y tiling, so new_tiling and old_tiling
1039 * are booleans where true represents W tiling and false represents Y tiling.
1042 brw_blorp_blit_program::translate_tiling(bool old_tiled_w
, bool new_tiled_w
)
1044 if (old_tiled_w
== new_tiled_w
)
1047 /* In the code that follows, we can safely assume that S = 0, because W
1048 * tiling formats always use IMS layout.
1053 /* Given X and Y coordinates that describe an address using Y tiling,
1054 * translate to the X and Y coordinates that describe the same address
1057 * If we break down the low order bits of X and Y, using a
1058 * single letter to represent each low-order bit:
1060 * X = A << 7 | 0bBCDEFGH
1061 * Y = J << 5 | 0bKLMNP (1)
1063 * Then we can apply the Y tiling formula to see the memory offset being
1066 * offset = (J * tile_pitch + A) << 12 | 0bBCDKLMNPEFGH (2)
1068 * If we apply the W detiling formula to this memory location, that the
1069 * corresponding X' and Y' coordinates are:
1071 * X' = A << 6 | 0bBCDPFH (3)
1072 * Y' = J << 6 | 0bKLMNEG
1074 * Combining (1) and (3), we see that to transform (X, Y) to (X', Y'),
1075 * we need to make the following computation:
1077 * X' = (X & ~0b1011) >> 1 | (Y & 0b1) << 2 | X & 0b1 (4)
1078 * Y' = (Y & ~0b1) << 1 | (X & 0b1000) >> 2 | (X & 0b10) >> 1
1080 emit_and(t1
, X
, brw_imm_uw(0xfff4)); /* X & ~0b1011 */
1081 emit_shr(t1
, t1
, brw_imm_uw(1)); /* (X & ~0b1011) >> 1 */
1082 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1083 emit_shl(t2
, t2
, brw_imm_uw(2)); /* (Y & 0b1) << 2 */
1084 emit_or(t1
, t1
, t2
); /* (X & ~0b1011) >> 1 | (Y & 0b1) << 2 */
1085 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1086 emit_or(Xp
, t1
, t2
);
1087 emit_and(t1
, Y
, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1088 emit_shl(t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1089 emit_and(t2
, X
, brw_imm_uw(8)); /* X & 0b1000 */
1090 emit_shr(t2
, t2
, brw_imm_uw(2)); /* (X & 0b1000) >> 2 */
1091 emit_or(t1
, t1
, t2
); /* (Y & ~0b1) << 1 | (X & 0b1000) >> 2 */
1092 emit_and(t2
, X
, brw_imm_uw(2)); /* X & 0b10 */
1093 emit_shr(t2
, t2
, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1094 emit_or(Yp
, t1
, t2
);
1097 /* Applying the same logic as above, but in reverse, we obtain the
1100 * X' = (X & ~0b101) << 1 | (Y & 0b10) << 2 | (Y & 0b1) << 1 | X & 0b1
1101 * Y' = (Y & ~0b11) >> 1 | (X & 0b100) >> 2
1103 emit_and(t1
, X
, brw_imm_uw(0xfffa)); /* X & ~0b101 */
1104 emit_shl(t1
, t1
, brw_imm_uw(1)); /* (X & ~0b101) << 1 */
1105 emit_and(t2
, Y
, brw_imm_uw(2)); /* Y & 0b10 */
1106 emit_shl(t2
, t2
, brw_imm_uw(2)); /* (Y & 0b10) << 2 */
1107 emit_or(t1
, t1
, t2
); /* (X & ~0b101) << 1 | (Y & 0b10) << 2 */
1108 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1109 emit_shl(t2
, t2
, brw_imm_uw(1)); /* (Y & 0b1) << 1 */
1110 emit_or(t1
, t1
, t2
); /* (X & ~0b101) << 1 | (Y & 0b10) << 2
1112 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1113 emit_or(Xp
, t1
, t2
);
1114 emit_and(t1
, Y
, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1115 emit_shr(t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1116 emit_and(t2
, X
, brw_imm_uw(4)); /* X & 0b100 */
1117 emit_shr(t2
, t2
, brw_imm_uw(2)); /* (X & 0b100) >> 2 */
1118 emit_or(Yp
, t1
, t2
);
1124 * Emit code to compensate for the difference between MSAA and non-MSAA
1127 * This code modifies the X and Y coordinates according to the formula:
1129 * (X', Y', S') = encode_msaa(num_samples, IMS, X, Y, S)
1131 * (See brw_blorp_blit_program).
1134 brw_blorp_blit_program::encode_msaa(unsigned num_samples
,
1135 intel_msaa_layout layout
)
1138 case INTEL_MSAA_LAYOUT_NONE
:
1139 /* No translation necessary, and S should already be zero. */
1142 case INTEL_MSAA_LAYOUT_CMS
:
1143 /* We can't compensate for compressed layout since at this point in the
1144 * program we haven't read from the MCS buffer.
1146 unreachable("Bad layout in encode_msaa");
1147 case INTEL_MSAA_LAYOUT_UMS
:
1148 /* No translation necessary. */
1150 case INTEL_MSAA_LAYOUT_IMS
:
1151 switch (num_samples
) {
1153 /* encode_msaa(4, IMS, X, Y, S) = (X', Y', 0)
1154 * where X' = (X & ~0b1) << 1 | (S & 0b1) << 1 | (X & 0b1)
1155 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1157 emit_and(t1
, X
, brw_imm_uw(0xfffe)); /* X & ~0b1 */
1159 emit_and(t2
, S
, brw_imm_uw(1)); /* S & 0b1 */
1160 emit_or(t1
, t1
, t2
); /* (X & ~0b1) | (S & 0b1) */
1162 emit_shl(t1
, t1
, brw_imm_uw(1)); /* (X & ~0b1) << 1
1164 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1165 emit_or(Xp
, t1
, t2
);
1166 emit_and(t1
, Y
, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1167 emit_shl(t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1169 emit_and(t2
, S
, brw_imm_uw(2)); /* S & 0b10 */
1170 emit_or(t1
, t1
, t2
); /* (Y & ~0b1) << 1 | (S & 0b10) */
1172 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1173 emit_or(Yp
, t1
, t2
);
1176 /* encode_msaa(8, IMS, X, Y, S) = (X', Y', 0)
1177 * where X' = (X & ~0b1) << 2 | (S & 0b100) | (S & 0b1) << 1
1179 * Y' = (Y & ~0b1) << 1 | (S & 0b10) | (Y & 0b1)
1181 emit_and(t1
, X
, brw_imm_uw(0xfffe)); /* X & ~0b1 */
1182 emit_shl(t1
, t1
, brw_imm_uw(2)); /* (X & ~0b1) << 2 */
1184 emit_and(t2
, S
, brw_imm_uw(4)); /* S & 0b100 */
1185 emit_or(t1
, t1
, t2
); /* (X & ~0b1) << 2 | (S & 0b100) */
1186 emit_and(t2
, S
, brw_imm_uw(1)); /* S & 0b1 */
1187 emit_shl(t2
, t2
, brw_imm_uw(1)); /* (S & 0b1) << 1 */
1188 emit_or(t1
, t1
, t2
); /* (X & ~0b1) << 2 | (S & 0b100)
1191 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1192 emit_or(Xp
, t1
, t2
);
1193 emit_and(t1
, Y
, brw_imm_uw(0xfffe)); /* Y & ~0b1 */
1194 emit_shl(t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b1) << 1 */
1196 emit_and(t2
, S
, brw_imm_uw(2)); /* S & 0b10 */
1197 emit_or(t1
, t1
, t2
); /* (Y & ~0b1) << 1 | (S & 0b10) */
1199 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1200 emit_or(Yp
, t1
, t2
);
1210 * Emit code to compensate for the difference between MSAA and non-MSAA
1213 * This code modifies the X and Y coordinates according to the formula:
1215 * (X', Y', S) = decode_msaa(num_samples, IMS, X, Y, S)
1217 * (See brw_blorp_blit_program).
1220 brw_blorp_blit_program::decode_msaa(unsigned num_samples
,
1221 intel_msaa_layout layout
)
1224 case INTEL_MSAA_LAYOUT_NONE
:
1225 /* No translation necessary, and S should already be zero. */
1228 case INTEL_MSAA_LAYOUT_CMS
:
1229 /* We can't compensate for compressed layout since at this point in the
1230 * program we don't have access to the MCS buffer.
1232 unreachable("Bad layout in encode_msaa");
1233 case INTEL_MSAA_LAYOUT_UMS
:
1234 /* No translation necessary. */
1236 case INTEL_MSAA_LAYOUT_IMS
:
1238 switch (num_samples
) {
1240 /* decode_msaa(4, IMS, X, Y, 0) = (X', Y', S)
1241 * where X' = (X & ~0b11) >> 1 | (X & 0b1)
1242 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1243 * S = (Y & 0b10) | (X & 0b10) >> 1
1245 emit_and(t1
, X
, brw_imm_uw(0xfffc)); /* X & ~0b11 */
1246 emit_shr(t1
, t1
, brw_imm_uw(1)); /* (X & ~0b11) >> 1 */
1247 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1248 emit_or(Xp
, t1
, t2
);
1249 emit_and(t1
, Y
, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1250 emit_shr(t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1251 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1252 emit_or(Yp
, t1
, t2
);
1253 emit_and(t1
, Y
, brw_imm_uw(2)); /* Y & 0b10 */
1254 emit_and(t2
, X
, brw_imm_uw(2)); /* X & 0b10 */
1255 emit_shr(t2
, t2
, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1259 /* decode_msaa(8, IMS, X, Y, 0) = (X', Y', S)
1260 * where X' = (X & ~0b111) >> 2 | (X & 0b1)
1261 * Y' = (Y & ~0b11) >> 1 | (Y & 0b1)
1262 * S = (X & 0b100) | (Y & 0b10) | (X & 0b10) >> 1
1264 emit_and(t1
, X
, brw_imm_uw(0xfff8)); /* X & ~0b111 */
1265 emit_shr(t1
, t1
, brw_imm_uw(2)); /* (X & ~0b111) >> 2 */
1266 emit_and(t2
, X
, brw_imm_uw(1)); /* X & 0b1 */
1267 emit_or(Xp
, t1
, t2
);
1268 emit_and(t1
, Y
, brw_imm_uw(0xfffc)); /* Y & ~0b11 */
1269 emit_shr(t1
, t1
, brw_imm_uw(1)); /* (Y & ~0b11) >> 1 */
1270 emit_and(t2
, Y
, brw_imm_uw(1)); /* Y & 0b1 */
1271 emit_or(Yp
, t1
, t2
);
1272 emit_and(t1
, X
, brw_imm_uw(4)); /* X & 0b100 */
1273 emit_and(t2
, Y
, brw_imm_uw(2)); /* Y & 0b10 */
1274 emit_or(t1
, t1
, t2
); /* (X & 0b100) | (Y & 0b10) */
1275 emit_and(t2
, X
, brw_imm_uw(2)); /* X & 0b10 */
1276 emit_shr(t2
, t2
, brw_imm_uw(1)); /* (X & 0b10) >> 1 */
1287 * Emit code to translate from destination (X, Y) coordinates to source (X, Y)
1291 brw_blorp_blit_program::translate_dst_to_src()
1293 struct brw_reg X_f
= retype(X
, BRW_REGISTER_TYPE_F
);
1294 struct brw_reg Y_f
= retype(Y
, BRW_REGISTER_TYPE_F
);
1295 struct brw_reg Xp_f
= retype(Xp
, BRW_REGISTER_TYPE_F
);
1296 struct brw_reg Yp_f
= retype(Yp
, BRW_REGISTER_TYPE_F
);
1298 /* Move the UD coordinates to float registers. */
1301 /* Scale and offset */
1302 emit_mad(X_f
, x_transform
.offset
, Xp_f
, x_transform
.multiplier
);
1303 emit_mad(Y_f
, y_transform
.offset
, Yp_f
, y_transform
.multiplier
);
1304 if (key
->blit_scaled
&& key
->blend
) {
1305 /* Translate coordinates to lay out the samples in a rectangular grid
1306 * roughly corresponding to sample locations.
1308 emit_mul(X_f
, X_f
, brw_imm_f(key
->x_scale
));
1309 emit_mul(Y_f
, Y_f
, brw_imm_f(key
->y_scale
));
1310 /* Adjust coordinates so that integers represent pixel centers rather
1313 emit_add(X_f
, X_f
, brw_imm_f(-0.5));
1314 emit_add(Y_f
, Y_f
, brw_imm_f(-0.5));
1316 /* Clamp the X, Y texture coordinates to properly handle the sampling of
1317 * texels on texture edges.
1319 clamp_tex_coords(X_f
, Y_f
,
1320 brw_imm_f(0.0), brw_imm_f(0.0),
1321 rect_grid_x1
, rect_grid_y1
);
1323 /* Store the fractional parts to be used as bilinear interpolation
1326 emit_frc(x_frac
, X_f
);
1327 emit_frc(y_frac
, Y_f
);
1329 /* Round the float coordinates down to nearest integer */
1330 emit_rndd(Xp_f
, X_f
);
1331 emit_rndd(Yp_f
, Y_f
);
1332 emit_mul(X_f
, Xp_f
, brw_imm_f(1.0f
/ key
->x_scale
));
1333 emit_mul(Y_f
, Yp_f
, brw_imm_f(1.0f
/ key
->y_scale
));
1335 } else if (!key
->bilinear_filter
) {
1336 /* Round the float coordinates down to nearest integer by moving to
1346 brw_blorp_blit_program::clamp_tex_coords(struct brw_reg regX
,
1347 struct brw_reg regY
,
1348 struct brw_reg clampX0
,
1349 struct brw_reg clampY0
,
1350 struct brw_reg clampX1
,
1351 struct brw_reg clampY1
)
1353 emit_max(regX
, regX
, clampX0
);
1354 emit_max(regY
, regY
, clampY0
);
1355 emit_min(regX
, regX
, clampX1
);
1356 emit_min(regY
, regY
, clampY1
);
1360 * Emit code to transform the X and Y coordinates as needed for blending
1361 * together the different samples in an MSAA texture.
1364 brw_blorp_blit_program::single_to_blend()
1366 /* When looking up samples in an MSAA texture using the SAMPLE message,
1367 * Gen6 requires the texture coordinates to be odd integers (so that they
1368 * correspond to the center of a 2x2 block representing the four samples
1369 * that maxe up a pixel). So we need to multiply our X and Y coordinates
1370 * each by 2 and then add 1.
1372 emit_shl(t1
, X
, brw_imm_w(1));
1373 emit_shl(t2
, Y
, brw_imm_w(1));
1374 emit_add(Xp
, t1
, brw_imm_w(1));
1375 emit_add(Yp
, t2
, brw_imm_w(1));
1381 * Count the number of trailing 1 bits in the given value. For example:
1383 * count_trailing_one_bits(0) == 0
1384 * count_trailing_one_bits(7) == 3
1385 * count_trailing_one_bits(11) == 2
1387 static inline int count_trailing_one_bits(unsigned value
)
1389 #ifdef HAVE___BUILTIN_CTZ
1390 return __builtin_ctz(~value
);
1392 return _mesa_bitcount(value
& ~(value
+ 1));
1398 brw_blorp_blit_program::manual_blend_average(unsigned num_samples
)
1400 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
1403 /* We add together samples using a binary tree structure, e.g. for 4x MSAA:
1405 * result = ((sample[0] + sample[1]) + (sample[2] + sample[3])) / 4
1407 * This ensures that when all samples have the same value, no numerical
1408 * precision is lost, since each addition operation always adds two equal
1409 * values, and summing two equal floating point values does not lose
1412 * We perform this computation by treating the texture_data array as a
1413 * stack and performing the following operations:
1415 * - push sample 0 onto stack
1416 * - push sample 1 onto stack
1417 * - add top two stack entries
1418 * - push sample 2 onto stack
1419 * - push sample 3 onto stack
1420 * - add top two stack entries
1421 * - add top two stack entries
1422 * - divide top stack entry by 4
1424 * Note that after pushing sample i onto the stack, the number of add
1425 * operations we do is equal to the number of trailing 1 bits in i. This
1426 * works provided the total number of samples is a power of two, which it
1427 * always is for i965.
1429 * For integer formats, we replace the add operations with average
1430 * operations and skip the final division.
1432 unsigned stack_depth
= 0;
1433 for (unsigned i
= 0; i
< num_samples
; ++i
) {
1434 assert(stack_depth
== _mesa_bitcount(i
)); /* Loop invariant */
1436 /* Push sample i onto the stack */
1437 assert(stack_depth
< ARRAY_SIZE(texture_data
));
1442 emit_mov(vec16(S
), brw_imm_ud(i
));
1444 texel_fetch(texture_data
[stack_depth
++]);
1446 if (i
== 0 && key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
) {
1447 /* The Ivy Bridge PRM, Vol4 Part1 p27 (Multisample Control Surface)
1448 * suggests an optimization:
1450 * "A simple optimization with probable large return in
1451 * performance is to compare the MCS value to zero (indicating
1452 * all samples are on sample slice 0), and sample only from
1453 * sample slice 0 using ld2dss if MCS is zero."
1455 * Note that in the case where the MCS value is zero, sampling from
1456 * sample slice 0 using ld2dss and sampling from sample 0 using
1457 * ld2dms are equivalent (since all samples are on sample slice 0).
1458 * Since we have already sampled from sample 0, all we need to do is
1459 * skip the remaining fetches and averaging if MCS is zero.
1461 emit_cmp_if(BRW_CONDITIONAL_NZ
, mcs_data
, brw_imm_ud(0));
1464 /* Do count_trailing_one_bits(i) times */
1465 for (int j
= count_trailing_one_bits(i
); j
-- > 0; ) {
1466 assert(stack_depth
>= 2);
1469 /* TODO: should use a smaller loop bound for non_RGBA formats */
1470 for (int k
= 0; k
< 4; ++k
) {
1471 emit_combine(key
->texture_data_type
== BRW_REGISTER_TYPE_F
?
1472 BRW_OPCODE_ADD
: BRW_OPCODE_AVG
,
1473 offset(texture_data
[stack_depth
- 1], 2*k
),
1474 offset(vec8(texture_data
[stack_depth
- 1]), 2*k
),
1475 offset(vec8(texture_data
[stack_depth
]), 2*k
));
1480 /* We should have just 1 sample on the stack now. */
1481 assert(stack_depth
== 1);
1483 if (key
->texture_data_type
== BRW_REGISTER_TYPE_F
) {
1484 /* Scale the result down by a factor of num_samples */
1485 /* TODO: should use a smaller loop bound for non-RGBA formats */
1486 for (int j
= 0; j
< 4; ++j
) {
1487 emit_mul(offset(texture_data
[0], 2*j
),
1488 offset(vec8(texture_data
[0]), 2*j
),
1489 brw_imm_f(1.0f
/ num_samples
));
1493 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
1498 brw_blorp_blit_program::manual_blend_bilinear(unsigned num_samples
)
1500 /* We do this computation by performing the following operations:
1502 * In case of 4x, 8x MSAA:
1503 * - Compute the pixel coordinates and sample numbers (a, b, c, d)
1504 * which are later used for interpolation
1505 * - linearly interpolate samples a and b in X
1506 * - linearly interpolate samples c and d in X
1507 * - linearly interpolate the results of last two operations in Y
1509 * result = lrp(lrp(a + b) + lrp(c + d))
1511 struct brw_reg Xp_f
= retype(Xp
, BRW_REGISTER_TYPE_F
);
1512 struct brw_reg Yp_f
= retype(Yp
, BRW_REGISTER_TYPE_F
);
1513 struct brw_reg t1_f
= retype(t1
, BRW_REGISTER_TYPE_F
);
1514 struct brw_reg t2_f
= retype(t2
, BRW_REGISTER_TYPE_F
);
1516 for (unsigned i
= 0; i
< 4; ++i
) {
1517 assert(i
< ARRAY_SIZE(texture_data
));
1520 /* Compute pixel coordinates */
1521 emit_add(vec16(x_sample_coords
), Xp_f
,
1522 brw_imm_f((float)(i
& 0x1) * (1.0f
/ key
->x_scale
)));
1523 emit_add(vec16(y_sample_coords
), Yp_f
,
1524 brw_imm_f((float)((i
>> 1) & 0x1) * (1.0f
/ key
->y_scale
)));
1525 emit_mov(vec16(X
), x_sample_coords
);
1526 emit_mov(vec16(Y
), y_sample_coords
);
1528 /* The MCS value we fetch has to match up with the pixel that we're
1529 * sampling from. Since we sample from different pixels in each
1530 * iteration of this "for" loop, the call to mcs_fetch() should be
1531 * here inside the loop after computing the pixel coordinates.
1533 if (key
->tex_layout
== INTEL_MSAA_LAYOUT_CMS
)
1536 /* Compute sample index and map the sample index to a sample number.
1537 * Sample index layout shows the numbering of slots in a rectangular
1538 * grid of samples with in a pixel. Sample number layout shows the
1539 * rectangular grid of samples roughly corresponding to the real sample
1540 * locations with in a pixel.
1541 * In case of 4x MSAA, layout of sample indices matches the layout of
1549 * In case of 8x MSAA the two layouts don't match.
1550 * sample index layout : --------- sample number layout : ---------
1551 * | 0 | 1 | | 5 | 2 |
1552 * --------- ---------
1553 * | 2 | 3 | | 4 | 6 |
1554 * --------- ---------
1555 * | 4 | 5 | | 0 | 3 |
1556 * --------- ---------
1557 * | 6 | 7 | | 7 | 1 |
1558 * --------- ---------
1560 emit_frc(vec16(t1_f
), x_sample_coords
);
1561 emit_frc(vec16(t2_f
), y_sample_coords
);
1562 emit_mul(vec16(t1_f
), t1_f
, brw_imm_f(key
->x_scale
));
1563 emit_mul(vec16(t2_f
), t2_f
, brw_imm_f(key
->x_scale
* key
->y_scale
));
1564 emit_add(vec16(t1_f
), t1_f
, t2_f
);
1565 emit_mov(vec16(S
), t1_f
);
1567 if (num_samples
== 8) {
1568 /* Map the sample index to a sample number */
1569 emit_cmp_if(BRW_CONDITIONAL_L
, S
, brw_imm_d(4));
1571 emit_mov(vec16(t2
), brw_imm_d(5));
1572 emit_if_eq_mov(S
, 1, vec16(t2
), 2);
1573 emit_if_eq_mov(S
, 2, vec16(t2
), 4);
1574 emit_if_eq_mov(S
, 3, vec16(t2
), 6);
1578 emit_mov(vec16(t2
), brw_imm_d(0));
1579 emit_if_eq_mov(S
, 5, vec16(t2
), 3);
1580 emit_if_eq_mov(S
, 6, vec16(t2
), 7);
1581 emit_if_eq_mov(S
, 7, vec16(t2
), 1);
1584 emit_mov(vec16(S
), t2
);
1586 texel_fetch(texture_data
[i
]);
1589 #define SAMPLE(x, y) offset(texture_data[x], y)
1590 for (int index
= 3; index
> 0; ) {
1591 /* Since we're doing SIMD16, 4 color channels fits in to 8 registers.
1592 * Counter value of 8 in 'for' loop below is used to interpolate all
1593 * the color components.
1595 for (int k
= 0; k
< 8; k
+= 2)
1596 emit_lrp(vec8(SAMPLE(index
- 1, k
)),
1598 vec8(SAMPLE(index
, k
)),
1599 vec8(SAMPLE(index
- 1, k
)));
1602 for (int k
= 0; k
< 8; k
+= 2)
1603 emit_lrp(vec8(SAMPLE(0, k
)),
1606 vec8(SAMPLE(0, k
)));
1611 * Emit code to look up a value in the texture using the SAMPLE message (which
1612 * does blending of MSAA surfaces).
1615 brw_blorp_blit_program::sample(struct brw_reg dst
)
1617 static const sampler_message_arg args
[2] = {
1618 SAMPLER_MESSAGE_ARG_U_FLOAT
,
1619 SAMPLER_MESSAGE_ARG_V_FLOAT
1622 texture_lookup(dst
, SHADER_OPCODE_TEX
, args
, ARRAY_SIZE(args
));
1626 * Emit code to look up a value in the texture using the SAMPLE_LD message
1627 * (which does a simple texel fetch).
1630 brw_blorp_blit_program::texel_fetch(struct brw_reg dst
)
1632 static const sampler_message_arg gen6_args
[5] = {
1633 SAMPLER_MESSAGE_ARG_U_INT
,
1634 SAMPLER_MESSAGE_ARG_V_INT
,
1635 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* R */
1636 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* LOD */
1637 SAMPLER_MESSAGE_ARG_SI_INT
1639 static const sampler_message_arg gen7_ld_args
[] = {
1640 SAMPLER_MESSAGE_ARG_U_INT
,
1641 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* LOD */
1642 SAMPLER_MESSAGE_ARG_V_INT
,
1643 SAMPLER_MESSAGE_ARG_R_INT
1645 static const sampler_message_arg gen7_ld2dss_args
[3] = {
1646 SAMPLER_MESSAGE_ARG_SI_INT
,
1647 SAMPLER_MESSAGE_ARG_U_INT
,
1648 SAMPLER_MESSAGE_ARG_V_INT
1650 static const sampler_message_arg gen7_ld2dms_args
[4] = {
1651 SAMPLER_MESSAGE_ARG_SI_INT
,
1652 SAMPLER_MESSAGE_ARG_MCS_INT
,
1653 SAMPLER_MESSAGE_ARG_U_INT
,
1654 SAMPLER_MESSAGE_ARG_V_INT
1656 static const sampler_message_arg gen9_ld_args
[] = {
1657 SAMPLER_MESSAGE_ARG_U_INT
,
1658 SAMPLER_MESSAGE_ARG_V_INT
,
1659 SAMPLER_MESSAGE_ARG_ZERO_INT
, /* LOD */
1660 SAMPLER_MESSAGE_ARG_R_INT
1665 texture_lookup(dst
, SHADER_OPCODE_TXF
, gen6_args
, s_is_zero
? 2 : 5);
1670 switch (key
->tex_layout
) {
1671 case INTEL_MSAA_LAYOUT_IMS
:
1672 /* From the Ivy Bridge PRM, Vol4 Part1 p72 (Multisampled Surface Storage
1675 * If this field is MSFMT_DEPTH_STENCIL
1676 * [a.k.a. INTEL_MSAA_LAYOUT_IMS], the only sampling engine
1677 * messages allowed are "ld2dms", "resinfo", and "sampleinfo".
1679 * So fall through to emit the same message as we use for
1680 * INTEL_MSAA_LAYOUT_CMS.
1682 case INTEL_MSAA_LAYOUT_CMS
:
1683 texture_lookup(dst
, SHADER_OPCODE_TXF_CMS
,
1684 gen7_ld2dms_args
, ARRAY_SIZE(gen7_ld2dms_args
));
1686 case INTEL_MSAA_LAYOUT_UMS
:
1687 texture_lookup(dst
, SHADER_OPCODE_TXF_UMS
,
1688 gen7_ld2dss_args
, ARRAY_SIZE(gen7_ld2dss_args
));
1690 case INTEL_MSAA_LAYOUT_NONE
:
1693 texture_lookup(dst
, SHADER_OPCODE_TXF
, gen7_ld_args
,
1694 ARRAY_SIZE(gen7_ld_args
));
1696 texture_lookup(dst
, SHADER_OPCODE_TXF
, gen9_ld_args
,
1697 ARRAY_SIZE(gen9_ld_args
));
1703 unreachable("Should not get here.");
1708 brw_blorp_blit_program::mcs_fetch()
1710 static const sampler_message_arg gen7_ld_mcs_args
[2] = {
1711 SAMPLER_MESSAGE_ARG_U_INT
,
1712 SAMPLER_MESSAGE_ARG_V_INT
1714 texture_lookup(vec16(mcs_data
), SHADER_OPCODE_TXF_MCS
,
1715 gen7_ld_mcs_args
, ARRAY_SIZE(gen7_ld_mcs_args
));
1719 brw_blorp_blit_program::texture_lookup(struct brw_reg dst
,
1721 const sampler_message_arg
*args
,
1724 struct brw_reg mrf
=
1725 retype(vec16(brw_message_reg(base_mrf
)), BRW_REGISTER_TYPE_UD
);
1726 for (int arg
= 0; arg
< num_args
; ++arg
) {
1727 switch (args
[arg
]) {
1728 case SAMPLER_MESSAGE_ARG_U_FLOAT
:
1729 if (key
->bilinear_filter
)
1730 emit_mov(retype(mrf
, BRW_REGISTER_TYPE_F
),
1731 retype(X
, BRW_REGISTER_TYPE_F
));
1733 emit_mov(retype(mrf
, BRW_REGISTER_TYPE_F
), X
);
1735 case SAMPLER_MESSAGE_ARG_V_FLOAT
:
1736 if (key
->bilinear_filter
)
1737 emit_mov(retype(mrf
, BRW_REGISTER_TYPE_F
),
1738 retype(Y
, BRW_REGISTER_TYPE_F
));
1740 emit_mov(retype(mrf
, BRW_REGISTER_TYPE_F
), Y
);
1742 case SAMPLER_MESSAGE_ARG_U_INT
:
1745 case SAMPLER_MESSAGE_ARG_V_INT
:
1748 case SAMPLER_MESSAGE_ARG_R_INT
:
1749 emit_mov(mrf
, src_z
);
1751 case SAMPLER_MESSAGE_ARG_SI_INT
:
1752 /* Note: on Gen7, this code may be reached with s_is_zero==true
1753 * because in Gen7's ld2dss message, the sample index is the first
1754 * argument. When this happens, we need to move a 0 into the
1755 * appropriate message register.
1758 emit_mov(mrf
, brw_imm_ud(0));
1762 case SAMPLER_MESSAGE_ARG_MCS_INT
:
1763 switch (key
->tex_layout
) {
1764 case INTEL_MSAA_LAYOUT_CMS
:
1765 emit_mov(mrf
, mcs_data
);
1767 case INTEL_MSAA_LAYOUT_IMS
:
1768 /* When sampling from an IMS surface, MCS data is not relevant,
1769 * and the hardware ignores it. So don't bother populating it.
1773 /* We shouldn't be trying to send MCS data with any other
1776 assert (!"Unsupported layout for MCS data");
1780 case SAMPLER_MESSAGE_ARG_ZERO_INT
:
1781 emit_mov(mrf
, brw_imm_ud(0));
1787 emit_texture_lookup(retype(dst
, BRW_REGISTER_TYPE_UW
) /* dest */,
1790 mrf
.nr
- base_mrf
/* msg_length */);
1798 #undef SWAP_XY_AND_XPYP
1801 brw_blorp_blit_program::render_target_write()
1803 struct brw_reg mrf_rt_write
=
1804 retype(vec16(brw_message_reg(base_mrf
)), key
->texture_data_type
);
1807 /* If we may have killed pixels, then we need to send R0 and R1 in a header
1808 * so that the render target knows which pixels we killed.
1810 bool use_header
= key
->use_kill
;
1812 /* Copy R0/1 to MRF */
1813 emit_mov(retype(mrf_rt_write
, BRW_REGISTER_TYPE_UD
),
1814 retype(R0
, BRW_REGISTER_TYPE_UD
));
1818 /* Copy texture data to MRFs */
1819 for (int i
= 0; i
< 4; ++i
) {
1820 /* E.g. mov(16) m2.0<1>:f r2.0<8;8,1>:f { Align1, H1 } */
1821 emit_mov(offset(mrf_rt_write
, mrf_offset
),
1822 offset(vec8(texture_data
[0]), 2*i
));
1826 /* Now write to the render target and terminate the thread */
1827 emit_render_target_write(
1829 brw
->gen
< 8 ? base_mrf
: -1,
1830 mrf_offset
/* msg_length. TODO: Should be smaller for non-RGBA formats. */,
1836 brw_blorp_coord_transform_params::setup(GLfloat src0
, GLfloat src1
,
1837 GLfloat dst0
, GLfloat dst1
,
1840 float scale
= (src1
- src0
) / (dst1
- dst0
);
1842 /* When not mirroring a coordinate (say, X), we need:
1843 * src_x - src_x0 = (dst_x - dst_x0 + 0.5) * scale
1845 * src_x = src_x0 + (dst_x - dst_x0 + 0.5) * scale
1847 * blorp program uses "round toward zero" to convert the
1848 * transformed floating point coordinates to integer coordinates,
1849 * whereas the behaviour we actually want is "round to nearest",
1850 * so 0.5 provides the necessary correction.
1853 offset
= src0
+ (-dst0
+ 0.5f
) * scale
;
1855 /* When mirroring X we need:
1856 * src_x - src_x0 = dst_x1 - dst_x - 0.5
1858 * src_x = src_x0 + (dst_x1 -dst_x - 0.5) * scale
1860 multiplier
= -scale
;
1861 offset
= src0
+ (dst1
- 0.5f
) * scale
;
1867 * Determine which MSAA layout the GPU pipeline should be configured for,
1868 * based on the chip generation, the number of samples, and the true layout of
1869 * the image in memory.
1871 inline intel_msaa_layout
1872 compute_msaa_layout_for_pipeline(struct brw_context
*brw
, unsigned num_samples
,
1873 intel_msaa_layout true_layout
)
1875 if (num_samples
<= 1) {
1876 /* When configuring the GPU for non-MSAA, we can still accommodate IMS
1877 * format buffers, by transforming coordinates appropriately.
1879 assert(true_layout
== INTEL_MSAA_LAYOUT_NONE
||
1880 true_layout
== INTEL_MSAA_LAYOUT_IMS
);
1881 return INTEL_MSAA_LAYOUT_NONE
;
1883 assert(true_layout
!= INTEL_MSAA_LAYOUT_NONE
);
1886 /* Prior to Gen7, all MSAA surfaces use IMS layout. */
1887 if (brw
->gen
== 6) {
1888 assert(true_layout
== INTEL_MSAA_LAYOUT_IMS
);
1895 brw_blorp_blit_params::brw_blorp_blit_params(struct brw_context
*brw
,
1896 struct intel_mipmap_tree
*src_mt
,
1897 unsigned src_level
, unsigned src_layer
,
1898 mesa_format src_format
,
1899 struct intel_mipmap_tree
*dst_mt
,
1900 unsigned dst_level
, unsigned dst_layer
,
1901 mesa_format dst_format
,
1902 GLfloat src_x0
, GLfloat src_y0
,
1903 GLfloat src_x1
, GLfloat src_y1
,
1904 GLfloat dst_x0
, GLfloat dst_y0
,
1905 GLfloat dst_x1
, GLfloat dst_y1
,
1907 bool mirror_x
, bool mirror_y
)
1909 src
.set(brw
, src_mt
, src_level
, src_layer
, src_format
, false);
1910 dst
.set(brw
, dst_mt
, dst_level
, dst_layer
, dst_format
, true);
1912 /* Even though we do multisample resolves at the time of the blit, OpenGL
1913 * specification defines them as if they happen at the time of rendering,
1914 * which means that the type of averaging we do during the resolve should
1915 * only depend on the source format; the destination format should be
1916 * ignored. But, specification doesn't seem to be strict about it.
1918 * It has been observed that mulitisample resolves produce slightly better
1919 * looking images when averaging is done using destination format. NVIDIA's
1920 * proprietary OpenGL driver also follow this approach. So, we choose to
1921 * follow it in our driver.
1923 * When multisampling, if the source and destination formats are equal
1924 * (aside from the color space), we choose to blit in sRGB space to get
1925 * this higher quality image.
1927 if (src
.num_samples
> 1 &&
1928 _mesa_get_format_color_encoding(dst_mt
->format
) == GL_SRGB
&&
1929 _mesa_get_srgb_format_linear(src_mt
->format
) ==
1930 _mesa_get_srgb_format_linear(dst_mt
->format
)) {
1931 assert(brw
->format_supported_as_render_target
[dst_mt
->format
]);
1932 dst
.brw_surfaceformat
= brw
->render_target_format
[dst_mt
->format
];
1933 src
.brw_surfaceformat
= brw_format_for_mesa_format(dst_mt
->format
);
1936 /* When doing a multisample resolve of a GL_LUMINANCE32F or GL_INTENSITY32F
1937 * texture, the above code configures the source format for L32_FLOAT or
1938 * I32_FLOAT, and the destination format for R32_FLOAT. On Sandy Bridge,
1939 * the SAMPLE message appears to handle multisampled L32_FLOAT and
1940 * I32_FLOAT textures incorrectly, resulting in blocky artifacts. So work
1941 * around the problem by using a source format of R32_FLOAT. This
1942 * shouldn't affect rendering correctness, since the destination format is
1943 * R32_FLOAT, so only the contents of the red channel matters.
1945 if (brw
->gen
== 6 && src
.num_samples
> 1 && dst
.num_samples
<= 1 &&
1946 src_mt
->format
== dst_mt
->format
&&
1947 dst
.brw_surfaceformat
== BRW_SURFACEFORMAT_R32_FLOAT
) {
1948 src
.brw_surfaceformat
= dst
.brw_surfaceformat
;
1952 memset(&wm_prog_key
, 0, sizeof(wm_prog_key
));
1954 /* texture_data_type indicates the register type that should be used to
1955 * manipulate texture data.
1957 switch (_mesa_get_format_datatype(src_mt
->format
)) {
1958 case GL_UNSIGNED_NORMALIZED
:
1959 case GL_SIGNED_NORMALIZED
:
1961 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_F
;
1963 case GL_UNSIGNED_INT
:
1964 if (src_mt
->format
== MESA_FORMAT_S_UINT8
) {
1965 /* We process stencil as though it's an unsigned normalized color */
1966 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_F
;
1968 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_UD
;
1972 wm_prog_key
.texture_data_type
= BRW_REGISTER_TYPE_D
;
1975 unreachable("Unrecognized blorp format");
1979 /* Gen7's rendering hardware only supports the IMS layout for depth and
1980 * stencil render targets. Blorp always maps its destination surface as
1981 * a color render target (even if it's actually a depth or stencil
1982 * buffer). So if the destination is IMS, we'll have to map it as a
1983 * single-sampled texture and interleave the samples ourselves.
1985 if (dst_mt
->msaa_layout
== INTEL_MSAA_LAYOUT_IMS
)
1986 dst
.num_samples
= 0;
1989 if (dst
.map_stencil_as_y_tiled
&& dst
.num_samples
> 1) {
1990 /* If the destination surface is a W-tiled multisampled stencil buffer
1991 * that we're mapping as Y tiled, then we need to arrange for the WM
1992 * program to run once per sample rather than once per pixel, because
1993 * the memory layout of related samples doesn't match between W and Y
1996 wm_prog_key
.persample_msaa_dispatch
= true;
1999 if (src
.num_samples
> 0 && dst
.num_samples
> 1) {
2000 /* We are blitting from a multisample buffer to a multisample buffer, so
2001 * we must preserve samples within a pixel. This means we have to
2002 * arrange for the WM program to run once per sample rather than once
2005 wm_prog_key
.persample_msaa_dispatch
= true;
2008 /* Scaled blitting or not. */
2009 wm_prog_key
.blit_scaled
=
2010 ((dst_x1
- dst_x0
) == (src_x1
- src_x0
) &&
2011 (dst_y1
- dst_y0
) == (src_y1
- src_y0
)) ? false : true;
2013 /* Scaling factors used for bilinear filtering in multisample scaled
2016 wm_prog_key
.x_scale
= 2.0f
;
2017 wm_prog_key
.y_scale
= src_mt
->num_samples
/ 2.0f
;
2019 if (filter
== GL_LINEAR
&& src
.num_samples
<= 1 && dst
.num_samples
<= 1)
2020 wm_prog_key
.bilinear_filter
= true;
2022 GLenum base_format
= _mesa_get_format_base_format(src_mt
->format
);
2023 if (base_format
!= GL_DEPTH_COMPONENT
&& /* TODO: what about depth/stencil? */
2024 base_format
!= GL_STENCIL_INDEX
&&
2025 src_mt
->num_samples
> 1 && dst_mt
->num_samples
<= 1) {
2026 /* We are downsampling a color buffer, so blend. */
2027 wm_prog_key
.blend
= true;
2030 /* src_samples and dst_samples are the true sample counts */
2031 wm_prog_key
.src_samples
= src_mt
->num_samples
;
2032 wm_prog_key
.dst_samples
= dst_mt
->num_samples
;
2034 /* tex_samples and rt_samples are the sample counts that are set up in
2037 wm_prog_key
.tex_samples
= src
.num_samples
;
2038 wm_prog_key
.rt_samples
= dst
.num_samples
;
2040 /* tex_layout and rt_layout indicate the MSAA layout the GPU pipeline will
2041 * use to access the source and destination surfaces.
2043 wm_prog_key
.tex_layout
=
2044 compute_msaa_layout_for_pipeline(brw
, src
.num_samples
, src
.msaa_layout
);
2045 wm_prog_key
.rt_layout
=
2046 compute_msaa_layout_for_pipeline(brw
, dst
.num_samples
, dst
.msaa_layout
);
2048 /* src_layout and dst_layout indicate the true MSAA layout used by src and
2051 wm_prog_key
.src_layout
= src_mt
->msaa_layout
;
2052 wm_prog_key
.dst_layout
= dst_mt
->msaa_layout
;
2054 wm_prog_key
.src_tiled_w
= src
.map_stencil_as_y_tiled
;
2055 wm_prog_key
.dst_tiled_w
= dst
.map_stencil_as_y_tiled
;
2056 /* Round floating point values to nearest integer to avoid "off by one texel"
2057 * kind of errors when blitting.
2059 x0
= wm_push_consts
.dst_x0
= roundf(dst_x0
);
2060 y0
= wm_push_consts
.dst_y0
= roundf(dst_y0
);
2061 x1
= wm_push_consts
.dst_x1
= roundf(dst_x1
);
2062 y1
= wm_push_consts
.dst_y1
= roundf(dst_y1
);
2063 wm_push_consts
.rect_grid_x1
= (minify(src_mt
->logical_width0
, src_level
) *
2064 wm_prog_key
.x_scale
- 1.0f
);
2065 wm_push_consts
.rect_grid_y1
= (minify(src_mt
->logical_height0
, src_level
) *
2066 wm_prog_key
.y_scale
- 1.0f
);
2068 wm_push_consts
.x_transform
.setup(src_x0
, src_x1
, dst_x0
, dst_x1
, mirror_x
);
2069 wm_push_consts
.y_transform
.setup(src_y0
, src_y1
, dst_y0
, dst_y1
, mirror_y
);
2071 wm_push_consts
.src_z
= src
.mt
->target
== GL_TEXTURE_3D
? src
.layer
: 0;
2073 if (dst
.num_samples
<= 1 && dst_mt
->num_samples
> 1) {
2074 /* We must expand the rectangle we send through the rendering pipeline,
2075 * to account for the fact that we are mapping the destination region as
2076 * single-sampled when it is in fact multisampled. We must also align
2077 * it to a multiple of the multisampling pattern, because the
2078 * differences between multisampled and single-sampled surface formats
2079 * will mean that pixels are scrambled within the multisampling pattern.
2080 * TODO: what if this makes the coordinates too large?
2082 * Note: this only works if the destination surface uses the IMS layout.
2083 * If it's UMS, then we have no choice but to set up the rendering
2084 * pipeline as multisampled.
2086 assert(dst_mt
->msaa_layout
== INTEL_MSAA_LAYOUT_IMS
);
2087 switch (dst_mt
->num_samples
) {
2089 x0
= ROUND_DOWN_TO(x0
* 2, 4);
2090 y0
= ROUND_DOWN_TO(y0
* 2, 4);
2091 x1
= ALIGN(x1
* 2, 4);
2092 y1
= ALIGN(y1
* 2, 4);
2095 x0
= ROUND_DOWN_TO(x0
* 4, 8);
2096 y0
= ROUND_DOWN_TO(y0
* 2, 4);
2097 x1
= ALIGN(x1
* 4, 8);
2098 y1
= ALIGN(y1
* 2, 4);
2101 unreachable("Unrecognized sample count in brw_blorp_blit_params ctor");
2103 wm_prog_key
.use_kill
= true;
2106 if (dst
.map_stencil_as_y_tiled
) {
2107 /* We must modify the rectangle we send through the rendering pipeline
2108 * (and the size and x/y offset of the destination surface), to account
2109 * for the fact that we are mapping it as Y-tiled when it is in fact
2112 * Both Y tiling and W tiling can be understood as organizations of
2113 * 32-byte sub-tiles; within each 32-byte sub-tile, the layout of pixels
2114 * is different, but the layout of the 32-byte sub-tiles within the 4k
2115 * tile is the same (8 sub-tiles across by 16 sub-tiles down, in
2116 * column-major order). In Y tiling, the sub-tiles are 16 bytes wide
2117 * and 2 rows high; in W tiling, they are 8 bytes wide and 4 rows high.
2119 * Therefore, to account for the layout differences within the 32-byte
2120 * sub-tiles, we must expand the rectangle so the X coordinates of its
2121 * edges are multiples of 8 (the W sub-tile width), and its Y
2122 * coordinates of its edges are multiples of 4 (the W sub-tile height).
2123 * Then we need to scale the X and Y coordinates of the rectangle to
2124 * account for the differences in aspect ratio between the Y and W
2125 * sub-tiles. We need to modify the layer width and height similarly.
2127 * A correction needs to be applied when MSAA is in use: since
2128 * INTEL_MSAA_LAYOUT_IMS uses an interleaving pattern whose height is 4,
2129 * we need to align the Y coordinates to multiples of 8, so that when
2130 * they are divided by two they are still multiples of 4.
2132 * Note: Since the x/y offset of the surface will be applied using the
2133 * SURFACE_STATE command packet, it will be invisible to the swizzling
2134 * code in the shader; therefore it needs to be in a multiple of the
2135 * 32-byte sub-tile size. Fortunately it is, since the sub-tile is 8
2136 * pixels wide and 4 pixels high (when viewed as a W-tiled stencil
2137 * buffer), and the miplevel alignment used for stencil buffers is 8
2138 * pixels horizontally and either 4 or 8 pixels vertically (see
2139 * intel_horizontal_texture_alignment_unit() and
2140 * intel_vertical_texture_alignment_unit()).
2142 * Note: Also, since the SURFACE_STATE command packet can only apply
2143 * offsets that are multiples of 4 pixels horizontally and 2 pixels
2144 * vertically, it is important that the offsets will be multiples of
2145 * these sizes after they are converted into Y-tiled coordinates.
2146 * Fortunately they will be, since we know from above that the offsets
2147 * are a multiple of the 32-byte sub-tile size, and in Y-tiled
2148 * coordinates the sub-tile is 16 pixels wide and 2 pixels high.
2150 * TODO: what if this makes the coordinates (or the texture size) too
2153 const unsigned x_align
= 8, y_align
= dst
.num_samples
!= 0 ? 8 : 4;
2154 x0
= ROUND_DOWN_TO(x0
, x_align
) * 2;
2155 y0
= ROUND_DOWN_TO(y0
, y_align
) / 2;
2156 x1
= ALIGN(x1
, x_align
) * 2;
2157 y1
= ALIGN(y1
, y_align
) / 2;
2158 dst
.width
= ALIGN(dst
.width
, x_align
) * 2;
2159 dst
.height
= ALIGN(dst
.height
, y_align
) / 2;
2162 wm_prog_key
.use_kill
= true;
2165 if (src
.map_stencil_as_y_tiled
) {
2166 /* We must modify the size and x/y offset of the source surface to
2167 * account for the fact that we are mapping it as Y-tiled when it is in
2170 * See the comments above concerning x/y offset alignment for the
2171 * destination surface.
2173 * TODO: what if this makes the texture size too large?
2175 const unsigned x_align
= 8, y_align
= src
.num_samples
!= 0 ? 8 : 4;
2176 src
.width
= ALIGN(src
.width
, x_align
) * 2;
2177 src
.height
= ALIGN(src
.height
, y_align
) / 2;
2184 brw_blorp_blit_params::get_wm_prog(struct brw_context
*brw
,
2185 brw_blorp_prog_data
**prog_data
) const
2187 uint32_t prog_offset
= 0;
2188 if (!brw_search_cache(&brw
->cache
, BRW_CACHE_BLORP_BLIT_PROG
,
2189 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
2190 &prog_offset
, prog_data
)) {
2191 brw_blorp_blit_program
prog(brw
, &this->wm_prog_key
,
2192 INTEL_DEBUG
& DEBUG_BLORP
);
2193 GLuint program_size
;
2194 const GLuint
*program
= prog
.compile(brw
, &program_size
);
2195 brw_upload_cache(&brw
->cache
, BRW_CACHE_BLORP_BLIT_PROG
,
2196 &this->wm_prog_key
, sizeof(this->wm_prog_key
),
2197 program
, program_size
,
2198 &prog
.prog_data
, sizeof(prog
.prog_data
),
2199 &prog_offset
, prog_data
);