iris: fix blorp filters
[mesa.git] / src / gallium / drivers / iris / iris_blit.c
1 /*
2 * Copyright © 2017 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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include <stdio.h>
24 #include "pipe/p_defines.h"
25 #include "pipe/p_state.h"
26 #include "pipe/p_context.h"
27 #include "pipe/p_screen.h"
28 #include "util/u_format.h"
29 #include "util/u_inlines.h"
30 #include "util/ralloc.h"
31 #include "intel/blorp/blorp.h"
32 #include "iris_context.h"
33 #include "iris_resource.h"
34 #include "iris_screen.h"
35
36 void
37 iris_blorp_surf_for_resource(struct blorp_surf *surf,
38 struct pipe_resource *p_res,
39 enum isl_aux_usage aux_usage,
40 bool is_render_target)
41 {
42 struct iris_resource *res = (void *) p_res;
43
44 *surf = (struct blorp_surf) {
45 .surf = &res->surf,
46 .addr = (struct blorp_address) {
47 .buffer = res->bo,
48 .offset = 0, // XXX: ???
49 .reloc_flags = is_render_target ? EXEC_OBJECT_WRITE : 0,
50 .mocs = I915_MOCS_CACHED, // XXX: BDW MOCS, PTE MOCS
51 },
52 .aux_usage = aux_usage,
53 };
54
55 assert(surf->aux_usage == ISL_AUX_USAGE_NONE);
56 }
57
58 static enum isl_format
59 iris_get_blorp_format(enum pipe_format pf)
60 {
61 switch (pf) {
62 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
63 return ISL_FORMAT_R24_UNORM_X8_TYPELESS;
64 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
65 return ISL_FORMAT_R32_FLOAT;
66 default:
67 return iris_isl_format_for_pipe_format(pf);
68 }
69 }
70
71 static void
72 iris_blit(struct pipe_context *ctx, const struct pipe_blit_info *info)
73 {
74 struct iris_context *ice = (void *) ctx;
75 struct blorp_surf src_surf, dst_surf;
76 iris_blorp_surf_for_resource(&src_surf, info->src.resource,
77 ISL_AUX_USAGE_NONE, false);
78 iris_blorp_surf_for_resource(&dst_surf, info->dst.resource,
79 ISL_AUX_USAGE_NONE, true);
80
81 enum isl_format src_isl_format = iris_get_blorp_format(info->src.format);
82 enum isl_format dst_isl_format = iris_get_blorp_format(info->dst.format);
83
84 unsigned dst_layer = info->dst.box.z;
85 unsigned src_layer = info->src.box.z;
86
87 struct isl_swizzle src_isl_swizzle = ISL_SWIZZLE_IDENTITY;
88
89 int src_x0 = info->src.box.x;
90 int src_x1 = info->src.box.x + info->src.box.width;
91 int src_y0 = info->src.box.y;
92 int src_y1 = info->src.box.y + info->src.box.height;
93 int dst_x0 = info->dst.box.x;
94 int dst_x1 = info->dst.box.x + info->dst.box.width;
95 int dst_y0 = info->dst.box.y;
96 int dst_y1 = info->dst.box.y + info->dst.box.height;
97 bool mirror_x = false;
98 bool mirror_y = false;
99 enum blorp_filter filter;
100
101 if (info->dst.box.width == info->src.box.width &&
102 info->dst.box.height == info->src.box.height) {
103 if (src_surf.surf->samples > 1 && dst_surf.surf->samples <= 1) {
104 /* The OpenGL ES 3.2 specification, section 16.2.1, says:
105 *
106 * "If the read framebuffer is multisampled (its effective
107 * value of SAMPLE_BUFFERS is one) and the draw framebuffer
108 * is not (its value of SAMPLE_BUFFERS is zero), the samples
109 * corresponding to each pixel location in the source are
110 * converted to a single sample before being written to the
111 * destination. The filter parameter is ignored. If the
112 * source formats are integer types or stencil values, a
113 * single sample’s value is selected for each pixel. If the
114 * source formats are floating-point or normalized types,
115 * the sample values for each pixel are resolved in an
116 * implementation-dependent manner. If the source formats
117 * are depth values, sample values are resolved in an
118 * implementation-dependent manner where the result will be
119 * between the minimum and maximum depth values in the pixel."
120 *
121 * When selecting a single sample, we always choose sample 0.
122 */
123 if (util_format_is_depth_or_stencil(info->src.format) ||
124 util_format_is_pure_integer(info->src.format)) {
125 filter = BLORP_FILTER_SAMPLE_0;
126 } else {
127 filter = BLORP_FILTER_AVERAGE;
128 }
129 } else {
130 /* The OpenGL 4.6 specification, section 18.3.1, says:
131 *
132 * "If the source and destination dimensions are identical,
133 * no filtering is applied."
134 *
135 * Using BLORP_FILTER_NONE will also handle the upsample case by
136 * replicating the one value in the source to all values in the
137 * destination.
138 */
139 filter = BLORP_FILTER_NONE;
140 }
141 } else if (info->filter == PIPE_TEX_FILTER_LINEAR) {
142 filter = BLORP_FILTER_BILINEAR;
143 } else {
144 filter = BLORP_FILTER_NEAREST;
145 }
146
147 struct iris_batch *batch = &ice->render_batch;
148
149 iris_batch_maybe_flush(batch, 1500);
150
151 struct blorp_batch blorp_batch;
152 blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
153 blorp_blit(&blorp_batch, &src_surf, info->src.level, src_layer,
154 src_isl_format, src_isl_swizzle,
155 &dst_surf, info->dst.level, dst_layer,
156 dst_isl_format, ISL_SWIZZLE_IDENTITY,
157 src_x0, src_y0, src_x1, src_y1,
158 dst_x0, dst_y0, dst_x1, dst_y1,
159 filter, mirror_x, mirror_y);
160
161 blorp_batch_finish(&blorp_batch);
162 }
163
164 static void
165 iris_resource_copy_region(struct pipe_context *ctx,
166 struct pipe_resource *dst,
167 unsigned dst_level,
168 unsigned dstx, unsigned dsty, unsigned dstz,
169 struct pipe_resource *src,
170 unsigned src_level,
171 const struct pipe_box *src_box)
172 {
173 struct iris_context *ice = (void *) ctx;
174 struct blorp_surf src_surf, dst_surf;
175 iris_blorp_surf_for_resource(&src_surf, src, ISL_AUX_USAGE_NONE, false);
176 iris_blorp_surf_for_resource(&dst_surf, dst, ISL_AUX_USAGE_NONE, true);
177
178 // XXX: ???
179 unsigned dst_layer = dstz;
180 unsigned src_layer = src_box->z;
181
182 struct iris_batch *batch = &ice->render_batch;
183
184 iris_batch_maybe_flush(batch, 1500);
185
186 struct blorp_batch blorp_batch;
187 blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
188 blorp_copy(&blorp_batch, &src_surf, src_level, src_layer,
189 &dst_surf, dst_level, dst_layer,
190 src_box->x, src_box->y, dstx, dsty,
191 src_box->width, src_box->height);
192 blorp_batch_finish(&blorp_batch);
193 }
194
195 void
196 iris_init_blit_functions(struct pipe_context *ctx)
197 {
198 ctx->blit = iris_blit;
199 ctx->resource_copy_region = iris_resource_copy_region;
200 }