iris: comment everything
[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 /**
72 * The pipe->blit() driver hook.
73 *
74 * This performs a blit between two surfaces, which copies data but may
75 * also perform format conversion, scaling, flipping, and so on.
76 */
77 static void
78 iris_blit(struct pipe_context *ctx, const struct pipe_blit_info *info)
79 {
80 struct iris_context *ice = (void *) ctx;
81 struct blorp_surf src_surf, dst_surf;
82 iris_blorp_surf_for_resource(&src_surf, info->src.resource,
83 ISL_AUX_USAGE_NONE, false);
84 iris_blorp_surf_for_resource(&dst_surf, info->dst.resource,
85 ISL_AUX_USAGE_NONE, true);
86
87 enum isl_format src_isl_format = iris_get_blorp_format(info->src.format);
88 enum isl_format dst_isl_format = iris_get_blorp_format(info->dst.format);
89
90 unsigned dst_layer = info->dst.box.z;
91 unsigned src_layer = info->src.box.z;
92
93 struct isl_swizzle src_isl_swizzle = ISL_SWIZZLE_IDENTITY;
94
95 int src_x0 = info->src.box.x;
96 int src_x1 = info->src.box.x + info->src.box.width;
97 int src_y0 = info->src.box.y;
98 int src_y1 = info->src.box.y + info->src.box.height;
99 int dst_x0 = info->dst.box.x;
100 int dst_x1 = info->dst.box.x + info->dst.box.width;
101 int dst_y0 = info->dst.box.y;
102 int dst_y1 = info->dst.box.y + info->dst.box.height;
103 bool mirror_x = false;
104 bool mirror_y = false;
105 enum blorp_filter filter;
106
107 if (abs(info->dst.box.width) == abs(info->src.box.width) &&
108 abs(info->dst.box.height) == abs(info->src.box.height)) {
109 if (src_surf.surf->samples > 1 && dst_surf.surf->samples <= 1) {
110 /* The OpenGL ES 3.2 specification, section 16.2.1, says:
111 *
112 * "If the read framebuffer is multisampled (its effective
113 * value of SAMPLE_BUFFERS is one) and the draw framebuffer
114 * is not (its value of SAMPLE_BUFFERS is zero), the samples
115 * corresponding to each pixel location in the source are
116 * converted to a single sample before being written to the
117 * destination. The filter parameter is ignored. If the
118 * source formats are integer types or stencil values, a
119 * single sample’s value is selected for each pixel. If the
120 * source formats are floating-point or normalized types,
121 * the sample values for each pixel are resolved in an
122 * implementation-dependent manner. If the source formats
123 * are depth values, sample values are resolved in an
124 * implementation-dependent manner where the result will be
125 * between the minimum and maximum depth values in the pixel."
126 *
127 * When selecting a single sample, we always choose sample 0.
128 */
129 if (util_format_is_depth_or_stencil(info->src.format) ||
130 util_format_is_pure_integer(info->src.format)) {
131 filter = BLORP_FILTER_SAMPLE_0;
132 } else {
133 filter = BLORP_FILTER_AVERAGE;
134 }
135 } else {
136 /* The OpenGL 4.6 specification, section 18.3.1, says:
137 *
138 * "If the source and destination dimensions are identical,
139 * no filtering is applied."
140 *
141 * Using BLORP_FILTER_NONE will also handle the upsample case by
142 * replicating the one value in the source to all values in the
143 * destination.
144 */
145 filter = BLORP_FILTER_NONE;
146 }
147 } else if (info->filter == PIPE_TEX_FILTER_LINEAR) {
148 filter = BLORP_FILTER_BILINEAR;
149 } else {
150 filter = BLORP_FILTER_NEAREST;
151 }
152
153 struct iris_batch *batch = &ice->render_batch;
154
155 iris_batch_maybe_flush(batch, 1500);
156
157 struct blorp_batch blorp_batch;
158 blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
159 blorp_blit(&blorp_batch, &src_surf, info->src.level, src_layer,
160 src_isl_format, src_isl_swizzle,
161 &dst_surf, info->dst.level, dst_layer,
162 dst_isl_format, ISL_SWIZZLE_IDENTITY,
163 src_x0, src_y0, src_x1, src_y1,
164 dst_x0, dst_y0, dst_x1, dst_y1,
165 filter, mirror_x, mirror_y);
166
167 blorp_batch_finish(&blorp_batch);
168 }
169
170 /**
171 * The pipe->resource_copy_region() driver hook.
172 *
173 * This implements ARB_copy_image semantics - a raw memory copy between
174 * compatible view classes.
175 */
176 static void
177 iris_resource_copy_region(struct pipe_context *ctx,
178 struct pipe_resource *dst,
179 unsigned dst_level,
180 unsigned dstx, unsigned dsty, unsigned dstz,
181 struct pipe_resource *src,
182 unsigned src_level,
183 const struct pipe_box *src_box)
184 {
185 struct iris_context *ice = (void *) ctx;
186 struct blorp_surf src_surf, dst_surf;
187 iris_blorp_surf_for_resource(&src_surf, src, ISL_AUX_USAGE_NONE, false);
188 iris_blorp_surf_for_resource(&dst_surf, dst, ISL_AUX_USAGE_NONE, true);
189
190 // XXX: ???
191 unsigned dst_layer = dstz;
192 unsigned src_layer = src_box->z;
193
194 struct iris_batch *batch = &ice->render_batch;
195
196 iris_batch_maybe_flush(batch, 1500);
197
198 struct blorp_batch blorp_batch;
199 blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
200 blorp_copy(&blorp_batch, &src_surf, src_level, src_layer,
201 &dst_surf, dst_level, dst_layer,
202 src_box->x, src_box->y, dstx, dsty,
203 src_box->width, src_box->height);
204 blorp_batch_finish(&blorp_batch);
205 }
206
207 void
208 iris_init_blit_functions(struct pipe_context *ctx)
209 {
210 ctx->blit = iris_blit;
211 ctx->resource_copy_region = iris_resource_copy_region;
212 }