17338e7326eada83700d59ba56727c7f2e057ffb
[mesa.git] / src / mesa / drivers / dri / i965 / 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 <errno.h>
25 #include "intel_batchbuffer.h"
26 #include "intel_fbo.h"
27
28 #include "blorp_priv.h"
29 #include "brw_compiler.h"
30 #include "brw_nir.h"
31 #include "brw_state.h"
32
33 void
34 blorp_init(struct blorp_context *blorp, void *driver_ctx,
35 struct isl_device *isl_dev)
36 {
37 blorp->driver_ctx = driver_ctx;
38 blorp->isl_dev = isl_dev;
39 }
40
41 void
42 blorp_finish(struct blorp_context *blorp)
43 {
44 blorp->driver_ctx = NULL;
45 }
46
47 void
48 brw_blorp_surface_info_init(struct brw_context *brw,
49 struct brw_blorp_surface_info *info,
50 const struct brw_blorp_surf *surf,
51 unsigned int level, unsigned int layer,
52 enum isl_format format, bool is_render_target)
53 {
54 /* Layer is a physical layer, so if this is a 2D multisample array texture
55 * using INTEL_MSAA_LAYOUT_UMS or INTEL_MSAA_LAYOUT_CMS, then it had better
56 * be a multiple of num_samples.
57 */
58 unsigned layer_multiplier = 1;
59 if (surf->surf->msaa_layout == ISL_MSAA_LAYOUT_ARRAY) {
60 assert(layer % surf->surf->samples == 0);
61 layer_multiplier = surf->surf->samples;
62 }
63
64 if (format == ISL_FORMAT_UNSUPPORTED)
65 format = surf->surf->format;
66
67 if (format == ISL_FORMAT_R24_UNORM_X8_TYPELESS) {
68 /* Unfortunately, ISL_FORMAT_R24_UNORM_X8_TYPELESS it isn't supported as
69 * a render target, which would prevent us from blitting to 24-bit
70 * depth. The miptree consists of 32 bits per pixel, arranged as 24-bit
71 * depth values interleaved with 8 "don't care" bits. Since depth
72 * values don't require any blending, it doesn't matter how we interpret
73 * the bit pattern as long as we copy the right amount of data, so just
74 * map it as 8-bit BGRA.
75 */
76 format = ISL_FORMAT_B8G8R8A8_UNORM;
77 } else if (surf->surf->usage & ISL_SURF_USAGE_STENCIL_BIT) {
78 assert(surf->surf->format == ISL_FORMAT_R8_UINT);
79 /* Prior to Broadwell, we can't render to R8_UINT */
80 if (brw->gen < 8)
81 format = ISL_FORMAT_R8_UNORM;
82 }
83
84 info->surf = *surf->surf;
85 info->bo = surf->bo;
86 info->offset = surf->offset;
87
88 info->aux_usage = surf->aux_usage;
89 if (info->aux_usage != ISL_AUX_USAGE_NONE) {
90 info->aux_surf = *surf->aux_surf;
91 info->aux_bo = surf->aux_bo;
92 info->aux_offset = surf->aux_offset;
93 }
94
95 info->clear_color = surf->clear_color;
96
97 info->view = (struct isl_view) {
98 .usage = is_render_target ? ISL_SURF_USAGE_RENDER_TARGET_BIT :
99 ISL_SURF_USAGE_TEXTURE_BIT,
100 .format = format,
101 .base_level = level,
102 .levels = 1,
103 .channel_select = {
104 ISL_CHANNEL_SELECT_RED,
105 ISL_CHANNEL_SELECT_GREEN,
106 ISL_CHANNEL_SELECT_BLUE,
107 ISL_CHANNEL_SELECT_ALPHA,
108 },
109 };
110
111 if (!is_render_target &&
112 (info->surf.dim == ISL_SURF_DIM_3D ||
113 info->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY)) {
114 /* 3-D textures don't support base_array layer and neither do 2-D
115 * multisampled textures on IVB so we need to pass it through the
116 * sampler in those cases. These are also two cases where we are
117 * guaranteed that we won't be doing any funny surface hacks.
118 */
119 info->view.base_array_layer = 0;
120 info->view.array_len = MAX2(info->surf.logical_level0_px.depth,
121 info->surf.logical_level0_px.array_len);
122 info->z_offset = layer / layer_multiplier;
123 } else {
124 info->view.base_array_layer = layer / layer_multiplier;
125 info->view.array_len = 1;
126 info->z_offset = 0;
127 }
128 }
129
130
131 void
132 brw_blorp_params_init(struct brw_blorp_params *params)
133 {
134 memset(params, 0, sizeof(*params));
135 params->hiz_op = GEN6_HIZ_OP_NONE;
136 params->fast_clear_op = 0;
137 params->num_draw_buffers = 1;
138 params->num_layers = 1;
139 }
140
141 void
142 brw_blorp_init_wm_prog_key(struct brw_wm_prog_key *wm_key)
143 {
144 memset(wm_key, 0, sizeof(*wm_key));
145 wm_key->nr_color_regions = 1;
146 for (int i = 0; i < MAX_SAMPLERS; i++)
147 wm_key->tex.swizzles[i] = SWIZZLE_XYZW;
148 }
149
150 static int
151 nir_uniform_type_size(const struct glsl_type *type)
152 {
153 /* Only very basic types are allowed */
154 assert(glsl_type_is_vector_or_scalar(type));
155 assert(glsl_get_bit_size(type) == 32);
156
157 return glsl_get_vector_elements(type) * 4;
158 }
159
160 const unsigned *
161 brw_blorp_compile_nir_shader(struct brw_context *brw, struct nir_shader *nir,
162 const struct brw_wm_prog_key *wm_key,
163 bool use_repclear,
164 struct brw_blorp_prog_data *prog_data,
165 unsigned *program_size)
166 {
167 const struct brw_compiler *compiler = brw->intelScreen->compiler;
168
169 void *mem_ctx = ralloc_context(NULL);
170
171 /* Calling brw_preprocess_nir and friends is destructive and, if cloning is
172 * enabled, may end up completely replacing the nir_shader. Therefore, we
173 * own it and might as well put it in our context for easy cleanup.
174 */
175 ralloc_steal(mem_ctx, nir);
176 nir->options =
177 compiler->glsl_compiler_options[MESA_SHADER_FRAGMENT].NirOptions;
178
179 struct brw_wm_prog_data wm_prog_data;
180 memset(&wm_prog_data, 0, sizeof(wm_prog_data));
181
182 wm_prog_data.base.nr_params = 0;
183 wm_prog_data.base.param = NULL;
184
185 /* BLORP always just uses the first two binding table entries */
186 wm_prog_data.binding_table.render_target_start = 0;
187 wm_prog_data.base.binding_table.texture_start = 1;
188
189 nir = brw_preprocess_nir(compiler, nir);
190 nir_remove_dead_variables(nir, nir_var_shader_in);
191 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
192
193 /* Uniforms are required to be lowered before going into compile_fs. For
194 * BLORP, we'll assume that whoever builds the shader sets the location
195 * they want so we just need to lower them and figure out how many we have
196 * in total.
197 */
198 nir->num_uniforms = 0;
199 nir_foreach_variable(var, &nir->uniforms) {
200 var->data.driver_location = var->data.location;
201 unsigned end = var->data.location + nir_uniform_type_size(var->type);
202 nir->num_uniforms = MAX2(nir->num_uniforms, end);
203 }
204 nir_lower_io(nir, nir_var_uniform, nir_uniform_type_size);
205
206 const unsigned *program =
207 brw_compile_fs(compiler, brw, mem_ctx, wm_key, &wm_prog_data, nir,
208 NULL, -1, -1, false, use_repclear, program_size, NULL);
209
210 /* Copy the relavent bits of wm_prog_data over into the blorp prog data */
211 prog_data->dispatch_8 = wm_prog_data.dispatch_8;
212 prog_data->dispatch_16 = wm_prog_data.dispatch_16;
213 prog_data->first_curbe_grf_0 = wm_prog_data.base.dispatch_grf_start_reg;
214 prog_data->first_curbe_grf_2 = wm_prog_data.dispatch_grf_start_reg_2;
215 prog_data->ksp_offset_2 = wm_prog_data.prog_offset_2;
216 prog_data->persample_msaa_dispatch = wm_prog_data.persample_dispatch;
217 prog_data->flat_inputs = wm_prog_data.flat_inputs;
218 prog_data->num_varying_inputs = wm_prog_data.num_varying_inputs;
219 prog_data->inputs_read = nir->info.inputs_read;
220
221 assert(wm_prog_data.base.nr_params == 0);
222
223 return program;
224 }
225
226 struct surface_state_info {
227 unsigned num_dwords;
228 unsigned ss_align; /* Required alignment of RENDER_SURFACE_STATE in bytes */
229 unsigned reloc_dw;
230 unsigned aux_reloc_dw;
231 };
232
233 static const struct surface_state_info surface_state_infos[] = {
234 [6] = {6, 32, 1, 0},
235 [7] = {8, 32, 1, 6},
236 [8] = {13, 64, 8, 10},
237 [9] = {16, 64, 8, 10},
238 };
239
240 uint32_t
241 brw_blorp_emit_surface_state(struct brw_context *brw,
242 const struct brw_blorp_surface_info *surface,
243 uint32_t read_domains, uint32_t write_domain,
244 bool is_render_target)
245 {
246 const struct surface_state_info ss_info = surface_state_infos[brw->gen];
247
248 struct isl_surf surf = surface->surf;
249
250 if (surf.dim == ISL_SURF_DIM_1D &&
251 surf.dim_layout == ISL_DIM_LAYOUT_GEN4_2D) {
252 assert(surf.logical_level0_px.height == 1);
253 surf.dim = ISL_SURF_DIM_2D;
254 }
255
256 /* Blorp doesn't support HiZ in any of the blit or slow-clear paths */
257 enum isl_aux_usage aux_usage = surface->aux_usage;
258 if (aux_usage == ISL_AUX_USAGE_HIZ)
259 aux_usage = ISL_AUX_USAGE_NONE;
260
261 uint32_t surf_offset;
262 uint32_t *dw = brw_state_batch(brw, AUB_TRACE_SURFACE_STATE,
263 ss_info.num_dwords * 4, ss_info.ss_align,
264 &surf_offset);
265
266 const uint32_t mocs =
267 is_render_target ? brw->blorp.mocs.rb : brw->blorp.mocs.tex;
268 uint64_t aux_bo_offset = surface->aux_bo ? surface->aux_bo->offset64 : 0;
269
270 isl_surf_fill_state(&brw->isl_dev, dw, .surf = &surf, .view = &surface->view,
271 .address = surface->bo->offset64 + surface->offset,
272 .aux_surf = &surface->aux_surf, .aux_usage = aux_usage,
273 .aux_address = aux_bo_offset + surface->aux_offset,
274 .mocs = mocs, .clear_color = surface->clear_color,
275 .x_offset_sa = surface->tile_x_sa,
276 .y_offset_sa = surface->tile_y_sa);
277
278 /* Emit relocation to surface contents */
279 drm_intel_bo_emit_reloc(brw->batch.bo,
280 surf_offset + ss_info.reloc_dw * 4,
281 surface->bo,
282 dw[ss_info.reloc_dw] - surface->bo->offset64,
283 read_domains, write_domain);
284
285 if (aux_usage != ISL_AUX_USAGE_NONE) {
286 /* On gen7 and prior, the bottom 12 bits of the MCS base address are
287 * used to store other information. This should be ok, however, because
288 * surface buffer addresses are always 4K page alinged.
289 */
290 assert((surface->aux_offset & 0xfff) == 0);
291 drm_intel_bo_emit_reloc(brw->batch.bo,
292 surf_offset + ss_info.aux_reloc_dw * 4,
293 surface->aux_bo,
294 dw[ss_info.aux_reloc_dw] & 0xfff,
295 read_domains, write_domain);
296 }
297
298 return surf_offset;
299 }
300
301 void
302 brw_blorp_exec(struct brw_context *brw, const struct brw_blorp_params *params)
303 {
304 struct gl_context *ctx = &brw->ctx;
305 const uint32_t estimated_max_batch_usage = brw->gen >= 8 ? 1800 : 1500;
306 bool check_aperture_failed_once = false;
307
308 /* Flush the sampler and render caches. We definitely need to flush the
309 * sampler cache so that we get updated contents from the render cache for
310 * the glBlitFramebuffer() source. Also, we are sometimes warned in the
311 * docs to flush the cache between reinterpretations of the same surface
312 * data with different formats, which blorp does for stencil and depth
313 * data.
314 */
315 brw_emit_mi_flush(brw);
316
317 brw_select_pipeline(brw, BRW_RENDER_PIPELINE);
318
319 retry:
320 intel_batchbuffer_require_space(brw, estimated_max_batch_usage, RENDER_RING);
321 intel_batchbuffer_save_state(brw);
322 drm_intel_bo *saved_bo = brw->batch.bo;
323 uint32_t saved_used = USED_BATCH(brw->batch);
324 uint32_t saved_state_batch_offset = brw->batch.state_batch_offset;
325
326 switch (brw->gen) {
327 case 6:
328 gen6_blorp_exec(brw, params);
329 break;
330 case 7:
331 if (brw->is_haswell)
332 gen75_blorp_exec(brw, params);
333 else
334 gen7_blorp_exec(brw, params);
335 break;
336 case 8:
337 gen8_blorp_exec(brw, params);
338 break;
339 case 9:
340 gen9_blorp_exec(brw, params);
341 break;
342 default:
343 /* BLORP is not supported before Gen6. */
344 unreachable("not reached");
345 }
346
347 /* Make sure we didn't wrap the batch unintentionally, and make sure we
348 * reserved enough space that a wrap will never happen.
349 */
350 assert(brw->batch.bo == saved_bo);
351 assert((USED_BATCH(brw->batch) - saved_used) * 4 +
352 (saved_state_batch_offset - brw->batch.state_batch_offset) <
353 estimated_max_batch_usage);
354 /* Shut up compiler warnings on release build */
355 (void)saved_bo;
356 (void)saved_used;
357 (void)saved_state_batch_offset;
358
359 /* Check if the blorp op we just did would make our batch likely to fail to
360 * map all the BOs into the GPU at batch exec time later. If so, flush the
361 * batch and try again with nothing else in the batch.
362 */
363 if (dri_bufmgr_check_aperture_space(&brw->batch.bo, 1)) {
364 if (!check_aperture_failed_once) {
365 check_aperture_failed_once = true;
366 intel_batchbuffer_reset_to_saved(brw);
367 intel_batchbuffer_flush(brw);
368 goto retry;
369 } else {
370 int ret = intel_batchbuffer_flush(brw);
371 WARN_ONCE(ret == -ENOSPC,
372 "i965: blorp emit exceeded available aperture space\n");
373 }
374 }
375
376 if (unlikely(brw->always_flush_batch))
377 intel_batchbuffer_flush(brw);
378
379 /* We've smashed all state compared to what the normal 3D pipeline
380 * rendering tracks for GL.
381 */
382 brw->ctx.NewDriverState |= BRW_NEW_BLORP;
383 brw->no_depth_or_stencil = false;
384 brw->ib.type = -1;
385
386 /* Flush the sampler cache so any texturing from the destination is
387 * coherent.
388 */
389 brw_emit_mi_flush(brw);
390 }
391
392 void
393 blorp_gen6_hiz_op(struct brw_context *brw, struct brw_blorp_surf *surf,
394 unsigned level, unsigned layer, enum gen6_hiz_op op)
395 {
396 struct brw_blorp_params params;
397 brw_blorp_params_init(&params);
398
399 params.hiz_op = op;
400
401 brw_blorp_surface_info_init(brw, &params.depth, surf, level, layer,
402 surf->surf->format, true);
403
404 /* Align the rectangle primitive to 8x4 pixels.
405 *
406 * During fast depth clears, the emitted rectangle primitive must be
407 * aligned to 8x4 pixels. From the Ivybridge PRM, Vol 2 Part 1 Section
408 * 11.5.3.1 Depth Buffer Clear (and the matching section in the Sandybridge
409 * PRM):
410 * If Number of Multisamples is NUMSAMPLES_1, the rectangle must be
411 * aligned to an 8x4 pixel block relative to the upper left corner
412 * of the depth buffer [...]
413 *
414 * For hiz resolves, the rectangle must also be 8x4 aligned. Item
415 * WaHizAmbiguate8x4Aligned from the Haswell workarounds page and the
416 * Ivybridge simulator require the alignment.
417 *
418 * To be safe, let's just align the rect for all hiz operations and all
419 * hardware generations.
420 *
421 * However, for some miptree slices of a Z24 texture, emitting an 8x4
422 * aligned rectangle that covers the slice may clobber adjacent slices if
423 * we strictly adhered to the texture alignments specified in the PRM. The
424 * Ivybridge PRM, Section "Alignment Unit Size", states that
425 * SURFACE_STATE.Surface_Horizontal_Alignment should be 4 for Z24 surfaces,
426 * not 8. But commit 1f112cc increased the alignment from 4 to 8, which
427 * prevents the clobbering.
428 */
429 params.x1 = minify(params.depth.surf.logical_level0_px.width,
430 params.depth.view.base_level);
431 params.y1 = minify(params.depth.surf.logical_level0_px.height,
432 params.depth.view.base_level);
433 params.x1 = ALIGN(params.x1, 8);
434 params.y1 = ALIGN(params.y1, 4);
435
436 if (params.depth.view.base_level == 0) {
437 /* TODO: What about MSAA? */
438 params.depth.surf.logical_level0_px.width = params.x1;
439 params.depth.surf.logical_level0_px.height = params.y1;
440 }
441
442 params.dst.surf.samples = params.depth.surf.samples;
443 params.dst.surf.logical_level0_px = params.depth.surf.logical_level0_px;
444
445 switch (surf->surf->format) {
446 case ISL_FORMAT_R16_UNORM:
447 params.depth_format = BRW_DEPTHFORMAT_D16_UNORM;
448 break;
449 case ISL_FORMAT_R32_FLOAT:
450 params.depth_format = BRW_DEPTHFORMAT_D32_FLOAT;
451 break;
452 case ISL_FORMAT_R24_UNORM_X8_TYPELESS:
453 params.depth_format = BRW_DEPTHFORMAT_D24_UNORM_X8_UINT;
454 break;
455 default:
456 unreachable("not reached");
457 }
458
459 brw_blorp_exec(brw, &params);
460 }