OSDN Git Service

intel/blorp: make upload_shader() return a bool indicating success or failure
[android-x86/external-mesa.git] / src / mesa / drivers / dri / i965 / brw_blorp.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23
24 #include "main/context.h"
25 #include "main/teximage.h"
26 #include "main/blend.h"
27 #include "main/fbobject.h"
28 #include "main/renderbuffer.h"
29 #include "main/glformats.h"
30
31 #include "brw_blorp.h"
32 #include "brw_context.h"
33 #include "brw_defines.h"
34 #include "brw_meta_util.h"
35 #include "brw_state.h"
36 #include "intel_fbo.h"
37 #include "common/gen_debug.h"
38
39 #define FILE_DEBUG_FLAG DEBUG_BLORP
40
41 static bool
42 brw_blorp_lookup_shader(struct blorp_context *blorp,
43                         const void *key, uint32_t key_size,
44                         uint32_t *kernel_out, void *prog_data_out)
45 {
46    struct brw_context *brw = blorp->driver_ctx;
47    return brw_search_cache(&brw->cache, BRW_CACHE_BLORP_PROG,
48                            key, key_size, kernel_out, prog_data_out);
49 }
50
51 static bool
52 brw_blorp_upload_shader(struct blorp_context *blorp,
53                         const void *key, uint32_t key_size,
54                         const void *kernel, uint32_t kernel_size,
55                         const struct brw_stage_prog_data *prog_data,
56                         uint32_t prog_data_size,
57                         uint32_t *kernel_out, void *prog_data_out)
58 {
59    struct brw_context *brw = blorp->driver_ctx;
60    brw_upload_cache(&brw->cache, BRW_CACHE_BLORP_PROG, key, key_size,
61                     kernel, kernel_size, prog_data, prog_data_size,
62                     kernel_out, prog_data_out);
63    return true;
64 }
65
66 void
67 brw_blorp_init(struct brw_context *brw)
68 {
69    blorp_init(&brw->blorp, brw, &brw->isl_dev);
70
71    brw->blorp.compiler = brw->screen->compiler;
72
73    switch (brw->gen) {
74    case 6:
75       brw->blorp.mocs.tex = 0;
76       brw->blorp.mocs.rb = 0;
77       brw->blorp.mocs.vb = 0;
78       brw->blorp.exec = gen6_blorp_exec;
79       break;
80    case 7:
81       brw->blorp.mocs.tex = GEN7_MOCS_L3;
82       brw->blorp.mocs.rb = GEN7_MOCS_L3;
83       brw->blorp.mocs.vb = GEN7_MOCS_L3;
84       if (brw->is_haswell) {
85          brw->blorp.exec = gen75_blorp_exec;
86       } else {
87          brw->blorp.exec = gen7_blorp_exec;
88       }
89       break;
90    case 8:
91       brw->blorp.mocs.tex = BDW_MOCS_WB;
92       brw->blorp.mocs.rb = BDW_MOCS_PTE;
93       brw->blorp.mocs.vb = BDW_MOCS_WB;
94       brw->blorp.exec = gen8_blorp_exec;
95       break;
96    case 9:
97       brw->blorp.mocs.tex = SKL_MOCS_WB;
98       brw->blorp.mocs.rb = SKL_MOCS_PTE;
99       brw->blorp.mocs.vb = SKL_MOCS_WB;
100       brw->blorp.exec = gen9_blorp_exec;
101       break;
102    default:
103       unreachable("Invalid gen");
104    }
105
106    brw->blorp.lookup_shader = brw_blorp_lookup_shader;
107    brw->blorp.upload_shader = brw_blorp_upload_shader;
108 }
109
110 static void
111 apply_gen6_stencil_hiz_offset(struct isl_surf *surf,
112                               struct intel_mipmap_tree *mt,
113                               uint32_t lod,
114                               uint32_t *offset)
115 {
116    assert(mt->array_layout == ALL_SLICES_AT_EACH_LOD);
117
118    if (mt->format == MESA_FORMAT_S_UINT8) {
119       /* Note: we can't compute the stencil offset using
120        * intel_miptree_get_aligned_offset(), because the miptree
121        * claims that the region is untiled even though it's W tiled.
122        */
123       *offset = mt->level[lod].level_y * mt->pitch +
124                 mt->level[lod].level_x * 64;
125    } else {
126       *offset = intel_miptree_get_aligned_offset(mt,
127                                                  mt->level[lod].level_x,
128                                                  mt->level[lod].level_y);
129    }
130
131    surf->logical_level0_px.width = minify(surf->logical_level0_px.width, lod);
132    surf->logical_level0_px.height = minify(surf->logical_level0_px.height, lod);
133    surf->phys_level0_sa.width = minify(surf->phys_level0_sa.width, lod);
134    surf->phys_level0_sa.height = minify(surf->phys_level0_sa.height, lod);
135    surf->levels = 1;
136    surf->array_pitch_el_rows =
137       ALIGN(surf->phys_level0_sa.height, surf->image_alignment_el.height);
138 }
139
140 static void
141 blorp_surf_for_miptree(struct brw_context *brw,
142                        struct blorp_surf *surf,
143                        struct intel_mipmap_tree *mt,
144                        bool is_render_target,
145                        uint32_t safe_aux_usage,
146                        unsigned *level,
147                        unsigned start_layer, unsigned num_layers,
148                        struct isl_surf tmp_surfs[2])
149 {
150    if (mt->msaa_layout == INTEL_MSAA_LAYOUT_UMS ||
151        mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) {
152       const unsigned num_samples = MAX2(1, mt->num_samples);
153       for (unsigned i = 0; i < num_layers; i++) {
154          for (unsigned s = 0; s < num_samples; s++) {
155             const unsigned phys_layer = (start_layer + i) * num_samples + s;
156             intel_miptree_check_level_layer(mt, *level, phys_layer);
157          }
158       }
159    } else {
160       for (unsigned i = 0; i < num_layers; i++)
161          intel_miptree_check_level_layer(mt, *level, start_layer + i);
162    }
163
164    intel_miptree_get_isl_surf(brw, mt, &tmp_surfs[0]);
165    surf->surf = &tmp_surfs[0];
166    surf->addr = (struct blorp_address) {
167       .buffer = mt->bo,
168       .offset = mt->offset,
169       .read_domains = is_render_target ? I915_GEM_DOMAIN_RENDER :
170                                          I915_GEM_DOMAIN_SAMPLER,
171       .write_domain = is_render_target ? I915_GEM_DOMAIN_RENDER : 0,
172    };
173
174    if (brw->gen == 6 && mt->format == MESA_FORMAT_S_UINT8 &&
175        mt->array_layout == ALL_SLICES_AT_EACH_LOD) {
176       /* Sandy bridge stencil and HiZ use this ALL_SLICES_AT_EACH_LOD hack in
177        * order to allow for layered rendering.  The hack makes each LOD of the
178        * stencil or HiZ buffer a single tightly packed array surface at some
179        * offset into the surface.  Since ISL doesn't know how to deal with the
180        * crazy ALL_SLICES_AT_EACH_LOD layout and since we have to do a manual
181        * offset of it anyway, we might as well do the offset here and keep the
182        * hacks inside the i965 driver.
183        *
184        * See also gen6_depth_stencil_state.c
185        */
186       uint32_t offset;
187       apply_gen6_stencil_hiz_offset(&tmp_surfs[0], mt, *level, &offset);
188       surf->addr.offset += offset;
189       *level = 0;
190    }
191
192    struct isl_surf *aux_surf = &tmp_surfs[1];
193    intel_miptree_get_aux_isl_surf(brw, mt, aux_surf, &surf->aux_usage);
194
195    if (surf->aux_usage != ISL_AUX_USAGE_NONE) {
196       if (surf->aux_usage == ISL_AUX_USAGE_HIZ) {
197          /* If we're not going to use it as a depth buffer, resolve HiZ */
198          if (!(safe_aux_usage & (1 << ISL_AUX_USAGE_HIZ))) {
199             for (unsigned i = 0; i < num_layers; i++) {
200                intel_miptree_slice_resolve_depth(brw, mt, *level,
201                                                  start_layer + i);
202
203                /* If we're rendering to it then we'll need a HiZ resolve once
204                 * we're done before we can use it with HiZ again.
205                 */
206                if (is_render_target)
207                   intel_miptree_slice_set_needs_hiz_resolve(mt, *level,
208                                                             start_layer + i);
209             }
210             surf->aux_usage = ISL_AUX_USAGE_NONE;
211          }
212       } else if (!(safe_aux_usage & (1 << surf->aux_usage))) {
213          uint32_t flags = 0;
214          if (safe_aux_usage & (1 << ISL_AUX_USAGE_CCS_E))
215             flags |= INTEL_MIPTREE_IGNORE_CCS_E;
216
217          intel_miptree_resolve_color(brw, mt,
218                                      *level, start_layer, num_layers, flags);
219
220          assert(!intel_miptree_has_color_unresolved(mt, *level, 1,
221                                                     start_layer, num_layers));
222          surf->aux_usage = ISL_AUX_USAGE_NONE;
223       }
224    }
225
226    if (is_render_target)
227       intel_miptree_used_for_rendering(brw, mt, *level,
228                                        start_layer, num_layers);
229
230    if (surf->aux_usage != ISL_AUX_USAGE_NONE) {
231       /* We only really need a clear color if we also have an auxiliary
232        * surface.  Without one, it does nothing.
233        */
234       surf->clear_color = intel_miptree_get_isl_clear_color(brw, mt);
235
236       surf->aux_surf = aux_surf;
237       surf->aux_addr = (struct blorp_address) {
238          .read_domains = is_render_target ? I915_GEM_DOMAIN_RENDER :
239                                             I915_GEM_DOMAIN_SAMPLER,
240          .write_domain = is_render_target ? I915_GEM_DOMAIN_RENDER : 0,
241       };
242
243       if (mt->mcs_buf) {
244          surf->aux_addr.buffer = mt->mcs_buf->bo;
245          surf->aux_addr.offset = mt->mcs_buf->offset;
246       } else {
247          assert(surf->aux_usage == ISL_AUX_USAGE_HIZ);
248
249          surf->aux_addr.buffer = mt->hiz_buf->aux_base.bo;
250          surf->aux_addr.offset = mt->hiz_buf->aux_base.offset;
251
252          struct intel_mipmap_tree *hiz_mt = mt->hiz_buf->mt;
253          if (hiz_mt) {
254             assert(brw->gen == 6 &&
255                    hiz_mt->array_layout == ALL_SLICES_AT_EACH_LOD);
256
257             /* gen6 requires the HiZ buffer to be manually offset to the
258              * right location.  We could fixup the surf but it doesn't
259              * matter since most of those fields don't matter.
260              */
261             apply_gen6_stencil_hiz_offset(aux_surf, hiz_mt, *level,
262                                           &surf->aux_addr.offset);
263             assert(hiz_mt->pitch == aux_surf->row_pitch);
264          }
265       }
266    } else {
267       surf->aux_addr = (struct blorp_address) {
268          .buffer = NULL,
269       };
270       memset(&surf->clear_color, 0, sizeof(surf->clear_color));
271    }
272    assert((surf->aux_usage == ISL_AUX_USAGE_NONE) ==
273           (surf->aux_addr.buffer == NULL));
274 }
275
276 static enum isl_format
277 brw_blorp_to_isl_format(struct brw_context *brw, mesa_format format,
278                         bool is_render_target)
279 {
280    switch (format) {
281    case MESA_FORMAT_NONE:
282       return ISL_FORMAT_UNSUPPORTED;
283    case MESA_FORMAT_S_UINT8:
284       return ISL_FORMAT_R8_UINT;
285    case MESA_FORMAT_Z24_UNORM_X8_UINT:
286    case MESA_FORMAT_Z24_UNORM_S8_UINT:
287       return ISL_FORMAT_R24_UNORM_X8_TYPELESS;
288    case MESA_FORMAT_Z_FLOAT32:
289    case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
290       return ISL_FORMAT_R32_FLOAT;
291    case MESA_FORMAT_Z_UNORM16:
292       return ISL_FORMAT_R16_UNORM;
293    default: {
294       if (is_render_target) {
295          assert(brw->format_supported_as_render_target[format]);
296          return brw->render_target_format[format];
297       } else {
298          return brw_isl_format_for_mesa_format(format);
299       }
300       break;
301    }
302    }
303 }
304
305 /**
306  * Convert an swizzle enumeration (i.e. SWIZZLE_X) to one of the Gen7.5+
307  * "Shader Channel Select" enumerations (i.e. HSW_SCS_RED).  The mappings are
308  *
309  * SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_ZERO, SWIZZLE_ONE
310  *         0          1          2          3             4            5
311  *         4          5          6          7             0            1
312  *   SCS_RED, SCS_GREEN,  SCS_BLUE, SCS_ALPHA,     SCS_ZERO,     SCS_ONE
313  *
314  * which is simply adding 4 then modding by 8 (or anding with 7).
315  *
316  * We then may need to apply workarounds for textureGather hardware bugs.
317  */
318 static enum isl_channel_select
319 swizzle_to_scs(GLenum swizzle)
320 {
321    return (enum isl_channel_select)((swizzle + 4) & 7);
322 }
323
324 static unsigned
325 physical_to_logical_layer(struct intel_mipmap_tree *mt,
326                           unsigned physical_layer)
327 {
328    if (mt->num_samples > 1 &&
329        (mt->msaa_layout == INTEL_MSAA_LAYOUT_UMS ||
330         mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS)) {
331       assert(physical_layer % mt->num_samples == 0);
332       return physical_layer / mt->num_samples;
333    } else {
334       return physical_layer;
335    }
336 }
337
338 /**
339  * Note: if the src (or dst) is a 2D multisample array texture on Gen7+ using
340  * INTEL_MSAA_LAYOUT_UMS or INTEL_MSAA_LAYOUT_CMS, src_layer (dst_layer) is
341  * the physical layer holding sample 0.  So, for example, if
342  * src_mt->num_samples == 4, then logical layer n corresponds to src_layer ==
343  * 4*n.
344  */
345 void
346 brw_blorp_blit_miptrees(struct brw_context *brw,
347                         struct intel_mipmap_tree *src_mt,
348                         unsigned src_level, unsigned src_layer,
349                         mesa_format src_format, int src_swizzle,
350                         struct intel_mipmap_tree *dst_mt,
351                         unsigned dst_level, unsigned dst_layer,
352                         mesa_format dst_format,
353                         float src_x0, float src_y0,
354                         float src_x1, float src_y1,
355                         float dst_x0, float dst_y0,
356                         float dst_x1, float dst_y1,
357                         GLenum filter, bool mirror_x, bool mirror_y,
358                         bool decode_srgb, bool encode_srgb)
359 {
360    /* Blorp operates in logical layers */
361    src_layer = physical_to_logical_layer(src_mt, src_layer);
362    dst_layer = physical_to_logical_layer(dst_mt, dst_layer);
363
364    DBG("%s from %dx %s mt %p %d %d (%f,%f) (%f,%f)"
365        "to %dx %s mt %p %d %d (%f,%f) (%f,%f) (flip %d,%d)\n",
366        __func__,
367        src_mt->num_samples, _mesa_get_format_name(src_mt->format), src_mt,
368        src_level, src_layer, src_x0, src_y0, src_x1, src_y1,
369        dst_mt->num_samples, _mesa_get_format_name(dst_mt->format), dst_mt,
370        dst_level, dst_layer, dst_x0, dst_y0, dst_x1, dst_y1,
371        mirror_x, mirror_y);
372
373    if (!decode_srgb && _mesa_get_format_color_encoding(src_format) == GL_SRGB)
374       src_format = _mesa_get_srgb_format_linear(src_format);
375
376    if (!encode_srgb && _mesa_get_format_color_encoding(dst_format) == GL_SRGB)
377       dst_format = _mesa_get_srgb_format_linear(dst_format);
378
379    /* When doing a multisample resolve of a GL_LUMINANCE32F or GL_INTENSITY32F
380     * texture, the above code configures the source format for L32_FLOAT or
381     * I32_FLOAT, and the destination format for R32_FLOAT.  On Sandy Bridge,
382     * the SAMPLE message appears to handle multisampled L32_FLOAT and
383     * I32_FLOAT textures incorrectly, resulting in blocky artifacts.  So work
384     * around the problem by using a source format of R32_FLOAT.  This
385     * shouldn't affect rendering correctness, since the destination format is
386     * R32_FLOAT, so only the contents of the red channel matters.
387     */
388    if (brw->gen == 6 &&
389        src_mt->num_samples > 1 && dst_mt->num_samples <= 1 &&
390        src_mt->format == dst_mt->format &&
391        (dst_format == MESA_FORMAT_L_FLOAT32 ||
392         dst_format == MESA_FORMAT_I_FLOAT32)) {
393       src_format = dst_format = MESA_FORMAT_R_FLOAT32;
394    }
395
396    uint32_t src_usage_flags = (1 << ISL_AUX_USAGE_MCS);
397    if (src_format == src_mt->format)
398       src_usage_flags |= (1 << ISL_AUX_USAGE_CCS_E);
399
400    uint32_t dst_usage_flags = (1 << ISL_AUX_USAGE_MCS);
401    if (dst_format == dst_mt->format) {
402       dst_usage_flags |= (1 << ISL_AUX_USAGE_CCS_E) |
403                          (1 << ISL_AUX_USAGE_CCS_D);
404    }
405
406    struct isl_surf tmp_surfs[4];
407    struct blorp_surf src_surf, dst_surf;
408    blorp_surf_for_miptree(brw, &src_surf, src_mt, false, src_usage_flags,
409                           &src_level, src_layer, 1, &tmp_surfs[0]);
410    blorp_surf_for_miptree(brw, &dst_surf, dst_mt, true, dst_usage_flags,
411                           &dst_level, dst_layer, 1, &tmp_surfs[2]);
412
413    struct isl_swizzle src_isl_swizzle = {
414       .r = swizzle_to_scs(GET_SWZ(src_swizzle, 0)),
415       .g = swizzle_to_scs(GET_SWZ(src_swizzle, 1)),
416       .b = swizzle_to_scs(GET_SWZ(src_swizzle, 2)),
417       .a = swizzle_to_scs(GET_SWZ(src_swizzle, 3)),
418    };
419
420    struct blorp_batch batch;
421    blorp_batch_init(&brw->blorp, &batch, brw, 0);
422    blorp_blit(&batch, &src_surf, src_level, src_layer,
423               brw_blorp_to_isl_format(brw, src_format, false), src_isl_swizzle,
424               &dst_surf, dst_level, dst_layer,
425               brw_blorp_to_isl_format(brw, dst_format, true),
426               ISL_SWIZZLE_IDENTITY,
427               src_x0, src_y0, src_x1, src_y1,
428               dst_x0, dst_y0, dst_x1, dst_y1,
429               filter, mirror_x, mirror_y);
430    blorp_batch_finish(&batch);
431 }
432
433 void
434 brw_blorp_copy_miptrees(struct brw_context *brw,
435                         struct intel_mipmap_tree *src_mt,
436                         unsigned src_level, unsigned src_layer,
437                         struct intel_mipmap_tree *dst_mt,
438                         unsigned dst_level, unsigned dst_layer,
439                         unsigned src_x, unsigned src_y,
440                         unsigned dst_x, unsigned dst_y,
441                         unsigned src_width, unsigned src_height)
442 {
443    DBG("%s from %dx %s mt %p %d %d (%d,%d) %dx%d"
444        "to %dx %s mt %p %d %d (%d,%d)\n",
445        __func__,
446        src_mt->num_samples, _mesa_get_format_name(src_mt->format), src_mt,
447        src_level, src_layer, src_x, src_y, src_width, src_height,
448        dst_mt->num_samples, _mesa_get_format_name(dst_mt->format), dst_mt,
449        dst_level, dst_layer, dst_x, dst_y);
450
451    struct isl_surf tmp_surfs[4];
452    struct blorp_surf src_surf, dst_surf;
453    blorp_surf_for_miptree(brw, &src_surf, src_mt, false,
454                           (1 << ISL_AUX_USAGE_MCS) |
455                           (1 << ISL_AUX_USAGE_CCS_E),
456                           &src_level, src_layer, 1, &tmp_surfs[0]);
457    blorp_surf_for_miptree(brw, &dst_surf, dst_mt, true,
458                           (1 << ISL_AUX_USAGE_MCS) |
459                           (1 << ISL_AUX_USAGE_CCS_E),
460                           &dst_level, dst_layer, 1, &tmp_surfs[2]);
461
462    struct blorp_batch batch;
463    blorp_batch_init(&brw->blorp, &batch, brw, 0);
464    blorp_copy(&batch, &src_surf, src_level, src_layer,
465               &dst_surf, dst_level, dst_layer,
466               src_x, src_y, dst_x, dst_y, src_width, src_height);
467    blorp_batch_finish(&batch);
468 }
469
470 static struct intel_mipmap_tree *
471 find_miptree(GLbitfield buffer_bit, struct intel_renderbuffer *irb)
472 {
473    struct intel_mipmap_tree *mt = irb->mt;
474    if (buffer_bit == GL_STENCIL_BUFFER_BIT && mt->stencil_mt)
475       mt = mt->stencil_mt;
476    return mt;
477 }
478
479 static int
480 blorp_get_texture_swizzle(const struct intel_renderbuffer *irb)
481 {
482    return irb->Base.Base._BaseFormat == GL_RGB ?
483       MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE) :
484       SWIZZLE_XYZW;
485 }
486
487 static void
488 do_blorp_blit(struct brw_context *brw, GLbitfield buffer_bit,
489               struct intel_renderbuffer *src_irb, mesa_format src_format,
490               struct intel_renderbuffer *dst_irb, mesa_format dst_format,
491               GLfloat srcX0, GLfloat srcY0, GLfloat srcX1, GLfloat srcY1,
492               GLfloat dstX0, GLfloat dstY0, GLfloat dstX1, GLfloat dstY1,
493               GLenum filter, bool mirror_x, bool mirror_y)
494 {
495    const struct gl_context *ctx = &brw->ctx;
496
497    /* Find source/dst miptrees */
498    struct intel_mipmap_tree *src_mt = find_miptree(buffer_bit, src_irb);
499    struct intel_mipmap_tree *dst_mt = find_miptree(buffer_bit, dst_irb);
500
501    const bool do_srgb = ctx->Color.sRGBEnabled;
502
503    /* Do the blit */
504    brw_blorp_blit_miptrees(brw,
505                            src_mt, src_irb->mt_level, src_irb->mt_layer,
506                            src_format, blorp_get_texture_swizzle(src_irb),
507                            dst_mt, dst_irb->mt_level, dst_irb->mt_layer,
508                            dst_format,
509                            srcX0, srcY0, srcX1, srcY1,
510                            dstX0, dstY0, dstX1, dstY1,
511                            filter, mirror_x, mirror_y,
512                            do_srgb, do_srgb);
513
514    dst_irb->need_downsample = true;
515 }
516
517 static bool
518 try_blorp_blit(struct brw_context *brw,
519                const struct gl_framebuffer *read_fb,
520                const struct gl_framebuffer *draw_fb,
521                GLfloat srcX0, GLfloat srcY0, GLfloat srcX1, GLfloat srcY1,
522                GLfloat dstX0, GLfloat dstY0, GLfloat dstX1, GLfloat dstY1,
523                GLenum filter, GLbitfield buffer_bit)
524 {
525    struct gl_context *ctx = &brw->ctx;
526
527    /* Sync up the state of window system buffers.  We need to do this before
528     * we go looking for the buffers.
529     */
530    intel_prepare_render(brw);
531
532    bool mirror_x, mirror_y;
533    if (brw_meta_mirror_clip_and_scissor(ctx, read_fb, draw_fb,
534                                         &srcX0, &srcY0, &srcX1, &srcY1,
535                                         &dstX0, &dstY0, &dstX1, &dstY1,
536                                         &mirror_x, &mirror_y))
537       return true;
538
539    /* Find buffers */
540    struct intel_renderbuffer *src_irb;
541    struct intel_renderbuffer *dst_irb;
542    struct intel_mipmap_tree *src_mt;
543    struct intel_mipmap_tree *dst_mt;
544    switch (buffer_bit) {
545    case GL_COLOR_BUFFER_BIT:
546       src_irb = intel_renderbuffer(read_fb->_ColorReadBuffer);
547       for (unsigned i = 0; i < draw_fb->_NumColorDrawBuffers; ++i) {
548          dst_irb = intel_renderbuffer(draw_fb->_ColorDrawBuffers[i]);
549          if (dst_irb)
550             do_blorp_blit(brw, buffer_bit,
551                           src_irb, src_irb->Base.Base.Format,
552                           dst_irb, dst_irb->Base.Base.Format,
553                           srcX0, srcY0, srcX1, srcY1,
554                           dstX0, dstY0, dstX1, dstY1,
555                           filter, mirror_x, mirror_y);
556       }
557       break;
558    case GL_DEPTH_BUFFER_BIT:
559       src_irb =
560          intel_renderbuffer(read_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
561       dst_irb =
562          intel_renderbuffer(draw_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
563       src_mt = find_miptree(buffer_bit, src_irb);
564       dst_mt = find_miptree(buffer_bit, dst_irb);
565
566       /* We can't handle format conversions between Z24 and other formats
567        * since we have to lie about the surface format. See the comments in
568        * brw_blorp_surface_info::set().
569        */
570       if ((src_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT) !=
571           (dst_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT))
572          return false;
573
574       do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
575                     dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
576                     srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
577                     filter, mirror_x, mirror_y);
578       break;
579    case GL_STENCIL_BUFFER_BIT:
580       src_irb =
581          intel_renderbuffer(read_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
582       dst_irb =
583          intel_renderbuffer(draw_fb->Attachment[BUFFER_STENCIL].Renderbuffer);
584       do_blorp_blit(brw, buffer_bit, src_irb, MESA_FORMAT_NONE,
585                     dst_irb, MESA_FORMAT_NONE, srcX0, srcY0,
586                     srcX1, srcY1, dstX0, dstY0, dstX1, dstY1,
587                     filter, mirror_x, mirror_y);
588       break;
589    default:
590       unreachable("not reached");
591    }
592
593    return true;
594 }
595
596 bool
597 brw_blorp_copytexsubimage(struct brw_context *brw,
598                           struct gl_renderbuffer *src_rb,
599                           struct gl_texture_image *dst_image,
600                           int slice,
601                           int srcX0, int srcY0,
602                           int dstX0, int dstY0,
603                           int width, int height)
604 {
605    struct gl_context *ctx = &brw->ctx;
606    struct intel_renderbuffer *src_irb = intel_renderbuffer(src_rb);
607    struct intel_texture_image *intel_image = intel_texture_image(dst_image);
608
609    /* No pixel transfer operations (zoom, bias, mapping), just a blit */
610    if (brw->ctx._ImageTransferState)
611       return false;
612
613    /* Sync up the state of window system buffers.  We need to do this before
614     * we go looking at the src renderbuffer's miptree.
615     */
616    intel_prepare_render(brw);
617
618    struct intel_mipmap_tree *src_mt = src_irb->mt;
619    struct intel_mipmap_tree *dst_mt = intel_image->mt;
620
621    /* There is support for only up to eight samples. */
622    if (src_mt->num_samples > 8 || dst_mt->num_samples > 8)
623       return false;
624
625    /* BLORP is only supported from Gen6 onwards. */
626    if (brw->gen < 6)
627       return false;
628
629    if (_mesa_get_format_base_format(src_rb->Format) !=
630        _mesa_get_format_base_format(dst_image->TexFormat)) {
631       return false;
632    }
633
634    /* We can't handle format conversions between Z24 and other formats since
635     * we have to lie about the surface format.  See the comments in
636     * brw_blorp_surface_info::set().
637     */
638    if ((src_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT) !=
639        (dst_mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT)) {
640       return false;
641    }
642
643    if (!brw->format_supported_as_render_target[dst_image->TexFormat])
644       return false;
645
646    /* Source clipping shouldn't be necessary, since copytexsubimage (in
647     * src/mesa/main/teximage.c) calls _mesa_clip_copytexsubimage() which
648     * takes care of it.
649     *
650     * Destination clipping shouldn't be necessary since the restrictions on
651     * glCopyTexSubImage prevent the user from specifying a destination rectangle
652     * that falls outside the bounds of the destination texture.
653     * See error_check_subtexture_dimensions().
654     */
655
656    int srcY1 = srcY0 + height;
657    int srcX1 = srcX0 + width;
658    int dstX1 = dstX0 + width;
659    int dstY1 = dstY0 + height;
660
661    /* Account for the fact that in the system framebuffer, the origin is at
662     * the lower left.
663     */
664    bool mirror_y = false;
665    if (_mesa_is_winsys_fbo(ctx->ReadBuffer)) {
666       GLint tmp = src_rb->Height - srcY0;
667       srcY0 = src_rb->Height - srcY1;
668       srcY1 = tmp;
669       mirror_y = true;
670    }
671
672    /* Account for face selection and texture view MinLayer */
673    int dst_slice = slice + dst_image->TexObject->MinLayer + dst_image->Face;
674    int dst_level = dst_image->Level + dst_image->TexObject->MinLevel;
675
676    brw_blorp_blit_miptrees(brw,
677                            src_mt, src_irb->mt_level, src_irb->mt_layer,
678                            src_rb->Format, blorp_get_texture_swizzle(src_irb),
679                            dst_mt, dst_level, dst_slice,
680                            dst_image->TexFormat,
681                            srcX0, srcY0, srcX1, srcY1,
682                            dstX0, dstY0, dstX1, dstY1,
683                            GL_NEAREST, false, mirror_y,
684                            false, false);
685
686    /* If we're copying to a packed depth stencil texture and the source
687     * framebuffer has separate stencil, we need to also copy the stencil data
688     * over.
689     */
690    src_rb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
691    if (_mesa_get_format_bits(dst_image->TexFormat, GL_STENCIL_BITS) > 0 &&
692        src_rb != NULL) {
693       src_irb = intel_renderbuffer(src_rb);
694       src_mt = src_irb->mt;
695
696       if (src_mt->stencil_mt)
697          src_mt = src_mt->stencil_mt;
698       if (dst_mt->stencil_mt)
699          dst_mt = dst_mt->stencil_mt;
700
701       if (src_mt != dst_mt) {
702          brw_blorp_blit_miptrees(brw,
703                                  src_mt, src_irb->mt_level, src_irb->mt_layer,
704                                  src_mt->format,
705                                  blorp_get_texture_swizzle(src_irb),
706                                  dst_mt, dst_level, dst_slice,
707                                  dst_mt->format,
708                                  srcX0, srcY0, srcX1, srcY1,
709                                  dstX0, dstY0, dstX1, dstY1,
710                                  GL_NEAREST, false, mirror_y,
711                                  false, false);
712       }
713    }
714
715    return true;
716 }
717
718
719 GLbitfield
720 brw_blorp_framebuffer(struct brw_context *brw,
721                       struct gl_framebuffer *readFb,
722                       struct gl_framebuffer *drawFb,
723                       GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
724                       GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
725                       GLbitfield mask, GLenum filter)
726 {
727    /* BLORP is not supported before Gen6. */
728    if (brw->gen < 6)
729       return mask;
730
731    static GLbitfield buffer_bits[] = {
732       GL_COLOR_BUFFER_BIT,
733       GL_DEPTH_BUFFER_BIT,
734       GL_STENCIL_BUFFER_BIT,
735    };
736
737    for (unsigned int i = 0; i < ARRAY_SIZE(buffer_bits); ++i) {
738       if ((mask & buffer_bits[i]) &&
739        try_blorp_blit(brw, readFb, drawFb,
740                       srcX0, srcY0, srcX1, srcY1,
741                       dstX0, dstY0, dstX1, dstY1,
742                       filter, buffer_bits[i])) {
743          mask &= ~buffer_bits[i];
744       }
745    }
746
747    return mask;
748 }
749
750 static bool
751 set_write_disables(const struct intel_renderbuffer *irb,
752                    const GLubyte *color_mask, bool *color_write_disable)
753 {
754    /* Format information in the renderbuffer represents the requirements
755     * given by the client. There are cases where the backing miptree uses,
756     * for example, RGBA to represent RGBX. Since the client is only expecting
757     * RGB we can treat alpha as not used and write whatever we like into it.
758     */
759    const GLenum base_format = irb->Base.Base._BaseFormat;
760    const int components = _mesa_base_format_component_count(base_format);
761    bool disables = false;
762
763    assert(components > 0);
764
765    for (int i = 0; i < components; i++) {
766       color_write_disable[i] = !color_mask[i];
767       disables = disables || !color_mask[i];
768    }
769
770    return disables;
771 }
772
773 static unsigned
774 irb_logical_mt_layer(struct intel_renderbuffer *irb)
775 {
776    return physical_to_logical_layer(irb->mt, irb->mt_layer);
777 }
778
779 static bool
780 do_single_blorp_clear(struct brw_context *brw, struct gl_framebuffer *fb,
781                       struct gl_renderbuffer *rb, unsigned buf,
782                       bool partial_clear, bool encode_srgb)
783 {
784    struct gl_context *ctx = &brw->ctx;
785    struct intel_renderbuffer *irb = intel_renderbuffer(rb);
786    mesa_format format = irb->mt->format;
787    uint32_t x0, x1, y0, y1;
788
789    if (!encode_srgb && _mesa_get_format_color_encoding(format) == GL_SRGB)
790       format = _mesa_get_srgb_format_linear(format);
791
792    x0 = fb->_Xmin;
793    x1 = fb->_Xmax;
794    if (rb->Name != 0) {
795       y0 = fb->_Ymin;
796       y1 = fb->_Ymax;
797    } else {
798       y0 = rb->Height - fb->_Ymax;
799       y1 = rb->Height - fb->_Ymin;
800    }
801
802    /* If the clear region is empty, just return. */
803    if (x0 == x1 || y0 == y1)
804       return true;
805
806    bool can_fast_clear = !partial_clear;
807
808    bool color_write_disable[4] = { false, false, false, false };
809    if (set_write_disables(irb, ctx->Color.ColorMask[buf], color_write_disable))
810       can_fast_clear = false;
811
812    if (irb->mt->aux_disable & INTEL_AUX_DISABLE_CCS ||
813        !brw_is_color_fast_clear_compatible(brw, irb->mt, &ctx->Color.ClearColor))
814       can_fast_clear = false;
815
816    const unsigned logical_layer = irb_logical_mt_layer(irb);
817    const enum intel_fast_clear_state fast_clear_state =
818       intel_miptree_get_fast_clear_state(irb->mt, irb->mt_level,
819                                          logical_layer);
820
821    /* Surface state can only record one fast clear color value. Therefore
822     * unless different levels/layers agree on the color it can be used to
823     * represent only single level/layer. Here it will be reserved for the
824     * first slice (level 0, layer 0).
825     */
826    if (irb->layer_count > 1 || irb->mt_level || irb->mt_layer)
827       can_fast_clear = false;
828
829    if (can_fast_clear) {
830       union gl_color_union override_color =
831          brw_meta_convert_fast_clear_color(brw, irb->mt,
832                                            &ctx->Color.ClearColor);
833
834       /* Record the clear color in the miptree so that it will be
835        * programmed in SURFACE_STATE by later rendering and resolve
836        * operations.
837        */
838       const bool color_updated = brw_meta_set_fast_clear_color(
839                                     brw, &irb->mt->gen9_fast_clear_color,
840                                     &override_color);
841
842       /* If the buffer is already in INTEL_FAST_CLEAR_STATE_CLEAR, the clear
843        * is redundant and can be skipped.
844        */
845       if (!color_updated && fast_clear_state == INTEL_FAST_CLEAR_STATE_CLEAR)
846          return true;
847
848       /* If the MCS buffer hasn't been allocated yet, we need to allocate
849        * it now.
850        */
851       if (!irb->mt->mcs_buf) {
852          assert(!intel_miptree_is_lossless_compressed(brw, irb->mt));
853          if (!intel_miptree_alloc_non_msrt_mcs(brw, irb->mt, false)) {
854             /* MCS allocation failed--probably this will only happen in
855              * out-of-memory conditions.  But in any case, try to recover
856              * by falling back to a non-blorp clear technique.
857              */
858             return false;
859          }
860       }
861    }
862
863    const unsigned num_layers = fb->MaxNumLayers ? irb->layer_count : 1;
864
865    /* We can't setup the blorp_surf until we've allocated the MCS above */
866    struct isl_surf isl_tmp[2];
867    struct blorp_surf surf;
868    unsigned level = irb->mt_level;
869    blorp_surf_for_miptree(brw, &surf, irb->mt, true,
870                           (1 << ISL_AUX_USAGE_MCS) |
871                           (1 << ISL_AUX_USAGE_CCS_E) |
872                           (1 << ISL_AUX_USAGE_CCS_D),
873                           &level, logical_layer, num_layers, isl_tmp);
874
875    if (can_fast_clear) {
876       DBG("%s (fast) to mt %p level %d layers %d+%d\n", __FUNCTION__,
877           irb->mt, irb->mt_level, irb->mt_layer, num_layers);
878
879       struct blorp_batch batch;
880       blorp_batch_init(&brw->blorp, &batch, brw, 0);
881       blorp_fast_clear(&batch, &surf,
882                        (enum isl_format)brw->render_target_format[format],
883                        level, logical_layer, num_layers,
884                        x0, y0, x1, y1);
885       blorp_batch_finish(&batch);
886
887       /* Now that the fast clear has occurred, put the buffer in
888        * INTEL_FAST_CLEAR_STATE_CLEAR so that we won't waste time doing
889        * redundant clears.
890        */
891       intel_miptree_set_fast_clear_state(brw, irb->mt, irb->mt_level,
892                                          logical_layer, num_layers,
893                                          INTEL_FAST_CLEAR_STATE_CLEAR);
894    } else {
895       DBG("%s (slow) to mt %p level %d layer %d+%d\n", __FUNCTION__,
896           irb->mt, irb->mt_level, irb->mt_layer, num_layers);
897
898       union isl_color_value clear_color;
899       memcpy(clear_color.f32, ctx->Color.ClearColor.f, sizeof(float) * 4);
900
901       struct blorp_batch batch;
902       blorp_batch_init(&brw->blorp, &batch, brw, 0);
903       blorp_clear(&batch, &surf,
904                   (enum isl_format)brw->render_target_format[format],
905                   ISL_SWIZZLE_IDENTITY,
906                   level, irb_logical_mt_layer(irb), num_layers,
907                   x0, y0, x1, y1,
908                   clear_color, color_write_disable);
909       blorp_batch_finish(&batch);
910    }
911
912    /*
913     * Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
914     *
915     *  Any transition from any value in {Clear, Render, Resolve} to a
916     *  different value in {Clear, Render, Resolve} requires end of pipe
917     *  synchronization.
918     */
919    brw_emit_pipe_control_flush(brw,
920                                PIPE_CONTROL_RENDER_TARGET_FLUSH |
921                                PIPE_CONTROL_CS_STALL);
922
923    return true;
924 }
925
926 bool
927 brw_blorp_clear_color(struct brw_context *brw, struct gl_framebuffer *fb,
928                       GLbitfield mask, bool partial_clear, bool encode_srgb)
929 {
930    for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
931       struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
932       struct intel_renderbuffer *irb = intel_renderbuffer(rb);
933
934       /* Only clear the buffers present in the provided mask */
935       if (((1 << fb->_ColorDrawBufferIndexes[buf]) & mask) == 0)
936          continue;
937
938       /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
939        * the framebuffer can be complete with some attachments missing.  In
940        * this case the _ColorDrawBuffers pointer will be NULL.
941        */
942       if (rb == NULL)
943          continue;
944
945       if (!do_single_blorp_clear(brw, fb, rb, buf, partial_clear,
946                                  encode_srgb)) {
947          return false;
948       }
949
950       irb->need_downsample = true;
951    }
952
953    return true;
954 }
955
956 void
957 brw_blorp_resolve_color(struct brw_context *brw, struct intel_mipmap_tree *mt,
958                         unsigned level, unsigned layer)
959 {
960    DBG("%s to mt %p level %u layer %u\n", __FUNCTION__, mt, level, layer);
961
962    const mesa_format format = _mesa_get_srgb_format_linear(mt->format);
963
964    struct isl_surf isl_tmp[2];
965    struct blorp_surf surf;
966    blorp_surf_for_miptree(brw, &surf, mt, true,
967                           (1 << ISL_AUX_USAGE_CCS_E) |
968                           (1 << ISL_AUX_USAGE_CCS_D),
969                           &level, layer, 1 /* num_layers */,
970                           isl_tmp);
971
972    enum blorp_fast_clear_op resolve_op;
973    if (brw->gen >= 9) {
974       if (surf.aux_usage == ISL_AUX_USAGE_CCS_E)
975          resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
976       else
977          resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL;
978    } else {
979       assert(surf.aux_usage == ISL_AUX_USAGE_CCS_D);
980       /* Broadwell and earlier do not have a partial resolve */
981       resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
982    }
983
984    struct blorp_batch batch;
985    blorp_batch_init(&brw->blorp, &batch, brw, 0);
986    blorp_ccs_resolve(&batch, &surf, level, layer,
987                      brw_blorp_to_isl_format(brw, format, true),
988                      resolve_op);
989    blorp_batch_finish(&batch);
990
991    /*
992     * Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
993     *
994     *  Any transition from any value in {Clear, Render, Resolve} to a
995     *  different value in {Clear, Render, Resolve} requires end of pipe
996     *  synchronization.
997     */
998    brw_emit_pipe_control_flush(brw,
999                                PIPE_CONTROL_RENDER_TARGET_FLUSH |
1000                                PIPE_CONTROL_CS_STALL);
1001 }
1002
1003 static void
1004 gen6_blorp_hiz_exec(struct brw_context *brw, struct intel_mipmap_tree *mt,
1005                     unsigned int level, unsigned int layer, enum blorp_hiz_op op)
1006 {
1007    assert(intel_miptree_level_has_hiz(mt, level));
1008
1009    struct isl_surf isl_tmp[2];
1010    struct blorp_surf surf;
1011    blorp_surf_for_miptree(brw, &surf, mt, true, (1 << ISL_AUX_USAGE_HIZ),
1012                           &level, layer, 1, isl_tmp);
1013
1014    struct blorp_batch batch;
1015    blorp_batch_init(&brw->blorp, &batch, brw, 0);
1016    blorp_gen6_hiz_op(&batch, &surf, level, layer, op);
1017    blorp_batch_finish(&batch);
1018 }
1019
1020 /**
1021  * Perform a HiZ or depth resolve operation.
1022  *
1023  * For an overview of HiZ ops, see the following sections of the Sandy Bridge
1024  * PRM, Volume 1, Part 2:
1025  *   - 7.5.3.1 Depth Buffer Clear
1026  *   - 7.5.3.2 Depth Buffer Resolve
1027  *   - 7.5.3.3 Hierarchical Depth Buffer Resolve
1028  */
1029 void
1030 intel_hiz_exec(struct brw_context *brw, struct intel_mipmap_tree *mt,
1031                unsigned int level, unsigned int layer, enum blorp_hiz_op op)
1032 {
1033    const char *opname = NULL;
1034
1035    switch (op) {
1036    case BLORP_HIZ_OP_DEPTH_RESOLVE:
1037       opname = "depth resolve";
1038       break;
1039    case BLORP_HIZ_OP_HIZ_RESOLVE:
1040       opname = "hiz ambiguate";
1041       break;
1042    case BLORP_HIZ_OP_DEPTH_CLEAR:
1043       opname = "depth clear";
1044       break;
1045    case BLORP_HIZ_OP_NONE:
1046       opname = "noop?";
1047       break;
1048    }
1049
1050    DBG("%s %s to mt %p level %d layer %d\n",
1051        __func__, opname, mt, level, layer);
1052
1053    if (brw->gen >= 8) {
1054       gen8_hiz_exec(brw, mt, level, layer, op);
1055    } else {
1056       gen6_blorp_hiz_exec(brw, mt, level, layer, op);
1057    }
1058 }