iris: Use SATURATE
[mesa.git] / src / gallium / drivers / iris / iris_blorp.c
1 /*
2 * Copyright © 2018 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 shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 /**
24 * @file iris_blorp.c
25 *
26 * ============================= GENXML CODE =============================
27 * [This file is compiled once per generation.]
28 * =======================================================================
29 *
30 * GenX specific code for working with BLORP (blitting, resolves, clears
31 * on the 3D engine). This provides the driver-specific hooks needed to
32 * implement the BLORP API.
33 *
34 * See iris_blit.c, iris_clear.c, and so on.
35 */
36
37 #include <assert.h>
38
39 #include "iris_batch.h"
40 #include "iris_resource.h"
41 #include "iris_context.h"
42
43 #include "util/u_upload_mgr.h"
44 #include "intel/common/gen_l3_config.h"
45
46 #define BLORP_USE_SOFTPIN
47 #include "blorp/blorp_genX_exec.h"
48
49 static uint32_t *
50 stream_state(struct iris_batch *batch,
51 struct u_upload_mgr *uploader,
52 unsigned size,
53 unsigned alignment,
54 uint32_t *out_offset,
55 struct iris_bo **out_bo)
56 {
57 struct pipe_resource *res = NULL;
58 void *ptr = NULL;
59
60 u_upload_alloc(uploader, 0, size, alignment, out_offset, &res, &ptr);
61
62 struct iris_bo *bo = iris_resource_bo(res);
63 iris_use_pinned_bo(batch, bo, false);
64
65 iris_record_state_size(batch->state_sizes,
66 bo->gtt_offset + *out_offset, size);
67
68 /* If the caller has asked for a BO, we leave them the responsibility of
69 * adding bo->gtt_offset (say, by handing an address to genxml). If not,
70 * we assume they want the offset from a base address.
71 */
72 if (out_bo)
73 *out_bo = bo;
74 else
75 *out_offset += iris_bo_offset_from_base_address(bo);
76
77 pipe_resource_reference(&res, NULL);
78
79 return ptr;
80 }
81
82 static void *
83 blorp_emit_dwords(struct blorp_batch *blorp_batch, unsigned n)
84 {
85 struct iris_batch *batch = blorp_batch->driver_batch;
86 return iris_get_command_space(batch, n * sizeof(uint32_t));
87 }
88
89 static uint64_t
90 combine_and_pin_address(struct blorp_batch *blorp_batch,
91 struct blorp_address addr)
92 {
93 struct iris_batch *batch = blorp_batch->driver_batch;
94 struct iris_bo *bo = addr.buffer;
95
96 iris_use_pinned_bo(batch, bo, addr.reloc_flags & RELOC_WRITE);
97
98 /* Assume this is a general address, not relative to a base. */
99 return bo->gtt_offset + addr.offset;
100 }
101
102 static uint64_t
103 blorp_emit_reloc(struct blorp_batch *blorp_batch, UNUSED void *location,
104 struct blorp_address addr, uint32_t delta)
105 {
106 return combine_and_pin_address(blorp_batch, addr) + delta;
107 }
108
109 static void
110 blorp_surface_reloc(struct blorp_batch *blorp_batch, uint32_t ss_offset,
111 struct blorp_address addr, uint32_t delta)
112 {
113 /* Let blorp_get_surface_address do the pinning. */
114 }
115
116 static uint64_t
117 blorp_get_surface_address(struct blorp_batch *blorp_batch,
118 struct blorp_address addr)
119 {
120 return combine_and_pin_address(blorp_batch, addr);
121 }
122
123 UNUSED static struct blorp_address
124 blorp_get_surface_base_address(UNUSED struct blorp_batch *blorp_batch)
125 {
126 return (struct blorp_address) { .offset = IRIS_MEMZONE_BINDER_START };
127 }
128
129 static void *
130 blorp_alloc_dynamic_state(struct blorp_batch *blorp_batch,
131 uint32_t size,
132 uint32_t alignment,
133 uint32_t *offset)
134 {
135 struct iris_context *ice = blorp_batch->blorp->driver_ctx;
136 struct iris_batch *batch = blorp_batch->driver_batch;
137
138 return stream_state(batch, ice->state.dynamic_uploader,
139 size, alignment, offset, NULL);
140 }
141
142 static void
143 blorp_alloc_binding_table(struct blorp_batch *blorp_batch,
144 unsigned num_entries,
145 unsigned state_size,
146 unsigned state_alignment,
147 uint32_t *bt_offset,
148 uint32_t *surface_offsets,
149 void **surface_maps)
150 {
151 struct iris_context *ice = blorp_batch->blorp->driver_ctx;
152 struct iris_binder *binder = &ice->state.binder;
153 struct iris_batch *batch = blorp_batch->driver_batch;
154
155 *bt_offset = iris_binder_reserve(ice, num_entries * sizeof(uint32_t));
156 uint32_t *bt_map = binder->map + *bt_offset;
157
158 for (unsigned i = 0; i < num_entries; i++) {
159 surface_maps[i] = stream_state(batch, ice->state.surface_uploader,
160 state_size, state_alignment,
161 &surface_offsets[i], NULL);
162 bt_map[i] = surface_offsets[i] - (uint32_t) binder->bo->gtt_offset;
163 }
164
165 iris_use_pinned_bo(batch, binder->bo, false);
166
167 batch->screen->vtbl.update_surface_base_address(batch, binder);
168 }
169
170 static void *
171 blorp_alloc_vertex_buffer(struct blorp_batch *blorp_batch,
172 uint32_t size,
173 struct blorp_address *addr)
174 {
175 struct iris_context *ice = blorp_batch->blorp->driver_ctx;
176 struct iris_batch *batch = blorp_batch->driver_batch;
177 struct iris_bo *bo;
178 uint32_t offset;
179
180 void *map = stream_state(batch, ice->ctx.stream_uploader, size, 64,
181 &offset, &bo);
182
183 *addr = (struct blorp_address) {
184 .buffer = bo,
185 .offset = offset,
186 .mocs = iris_mocs(bo, &batch->screen->isl_dev),
187 };
188
189 return map;
190 }
191
192 /**
193 * See iris_upload_render_state's IRIS_DIRTY_VERTEX_BUFFERS handling for
194 * a comment about why these VF invalidations are needed.
195 */
196 static void
197 blorp_vf_invalidate_for_vb_48b_transitions(struct blorp_batch *blorp_batch,
198 const struct blorp_address *addrs,
199 UNUSED uint32_t *sizes,
200 unsigned num_vbs)
201 {
202 #if GEN_GEN < 11
203 struct iris_context *ice = blorp_batch->blorp->driver_ctx;
204 struct iris_batch *batch = blorp_batch->driver_batch;
205 bool need_invalidate = false;
206
207 for (unsigned i = 0; i < num_vbs; i++) {
208 struct iris_bo *bo = addrs[i].buffer;
209 uint16_t high_bits = bo->gtt_offset >> 32u;
210
211 if (high_bits != ice->state.last_vbo_high_bits[i]) {
212 need_invalidate = true;
213 ice->state.last_vbo_high_bits[i] = high_bits;
214 }
215 }
216
217 if (need_invalidate) {
218 iris_emit_pipe_control_flush(batch,
219 "workaround: VF cache 32-bit key [blorp]",
220 PIPE_CONTROL_VF_CACHE_INVALIDATE |
221 PIPE_CONTROL_CS_STALL);
222 }
223 #endif
224 }
225
226 static struct blorp_address
227 blorp_get_workaround_address(struct blorp_batch *blorp_batch)
228 {
229 struct iris_batch *batch = blorp_batch->driver_batch;
230
231 return (struct blorp_address) {
232 .buffer = batch->screen->workaround_address.bo,
233 .offset = batch->screen->workaround_address.offset,
234 };
235 }
236
237 static void
238 blorp_flush_range(UNUSED struct blorp_batch *blorp_batch,
239 UNUSED void *start,
240 UNUSED size_t size)
241 {
242 /* All allocated states come from the batch which we will flush before we
243 * submit it. There's nothing for us to do here.
244 */
245 }
246
247 static const struct gen_l3_config *
248 blorp_get_l3_config(struct blorp_batch *blorp_batch)
249 {
250 struct iris_batch *batch = blorp_batch->driver_batch;
251 return batch->screen->l3_config_3d;
252 }
253
254 static void
255 iris_blorp_exec(struct blorp_batch *blorp_batch,
256 const struct blorp_params *params)
257 {
258 struct iris_context *ice = blorp_batch->blorp->driver_ctx;
259 struct iris_batch *batch = blorp_batch->driver_batch;
260
261 #if GEN_GEN >= 11
262 /* The PIPE_CONTROL command description says:
263 *
264 * "Whenever a Binding Table Index (BTI) used by a Render Target Message
265 * points to a different RENDER_SURFACE_STATE, SW must issue a Render
266 * Target Cache Flush by enabling this bit. When render target flush
267 * is set due to new association of BTI, PS Scoreboard Stall bit must
268 * be set in this packet."
269 */
270 iris_emit_pipe_control_flush(batch,
271 "workaround: RT BTI change [blorp]",
272 PIPE_CONTROL_RENDER_TARGET_FLUSH |
273 PIPE_CONTROL_STALL_AT_SCOREBOARD);
274 #endif
275
276 /* Flush the sampler and render caches. We definitely need to flush the
277 * sampler cache so that we get updated contents from the render cache for
278 * the glBlitFramebuffer() source. Also, we are sometimes warned in the
279 * docs to flush the cache between reinterpretations of the same surface
280 * data with different formats, which blorp does for stencil and depth
281 * data.
282 */
283 if (params->src.enabled)
284 iris_cache_flush_for_read(batch, params->src.addr.buffer);
285 if (params->dst.enabled) {
286 iris_cache_flush_for_render(batch, params->dst.addr.buffer,
287 params->dst.view.format,
288 params->dst.aux_usage);
289 }
290 if (params->depth.enabled)
291 iris_cache_flush_for_depth(batch, params->depth.addr.buffer);
292 if (params->stencil.enabled)
293 iris_cache_flush_for_depth(batch, params->stencil.addr.buffer);
294
295 iris_require_command_space(batch, 1400);
296
297 #if GEN_GEN == 8
298 genX(update_pma_fix)(ice, batch, false);
299 #endif
300
301 const unsigned scale = params->fast_clear_op ? UINT_MAX : 1;
302 if (ice->state.current_hash_scale != scale) {
303 genX(emit_hashing_mode)(ice, batch, params->x1 - params->x0,
304 params->y1 - params->y0, scale);
305 }
306
307 #if GEN_GEN >= 12
308 genX(invalidate_aux_map_state)(batch);
309 #endif
310
311 iris_handle_always_flush_cache(batch);
312
313 blorp_exec(blorp_batch, params);
314
315 iris_handle_always_flush_cache(batch);
316
317 /* We've smashed all state compared to what the normal 3D pipeline
318 * rendering tracks for GL.
319 */
320
321 uint64_t skip_bits = (IRIS_DIRTY_POLYGON_STIPPLE |
322 IRIS_DIRTY_SO_BUFFERS |
323 IRIS_DIRTY_SO_DECL_LIST |
324 IRIS_DIRTY_LINE_STIPPLE |
325 IRIS_ALL_DIRTY_FOR_COMPUTE |
326 IRIS_DIRTY_SCISSOR_RECT |
327 IRIS_DIRTY_UNCOMPILED_VS |
328 IRIS_DIRTY_UNCOMPILED_TCS |
329 IRIS_DIRTY_UNCOMPILED_TES |
330 IRIS_DIRTY_UNCOMPILED_GS |
331 IRIS_DIRTY_UNCOMPILED_FS |
332 IRIS_DIRTY_VF |
333 IRIS_DIRTY_SF_CL_VIEWPORT |
334 IRIS_DIRTY_SAMPLER_STATES_VS |
335 IRIS_DIRTY_SAMPLER_STATES_TCS |
336 IRIS_DIRTY_SAMPLER_STATES_TES |
337 IRIS_DIRTY_SAMPLER_STATES_GS);
338
339 if (!ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL]) {
340 /* BLORP disabled tessellation, that's fine for the next draw */
341 skip_bits |= IRIS_DIRTY_TCS |
342 IRIS_DIRTY_TES |
343 IRIS_DIRTY_CONSTANTS_TCS |
344 IRIS_DIRTY_CONSTANTS_TES |
345 IRIS_DIRTY_BINDINGS_TCS |
346 IRIS_DIRTY_BINDINGS_TES;
347 }
348
349 if (!ice->shaders.uncompiled[MESA_SHADER_GEOMETRY]) {
350 /* BLORP disabled geometry shaders, that's fine for the next draw */
351 skip_bits |= IRIS_DIRTY_GS |
352 IRIS_DIRTY_CONSTANTS_GS |
353 IRIS_DIRTY_BINDINGS_GS;
354 }
355
356 /* we can skip flagging IRIS_DIRTY_DEPTH_BUFFER, if
357 * BLORP_BATCH_NO_EMIT_DEPTH_STENCIL is set.
358 */
359 if (blorp_batch->flags & BLORP_BATCH_NO_EMIT_DEPTH_STENCIL)
360 skip_bits |= IRIS_DIRTY_DEPTH_BUFFER;
361
362 if (!params->wm_prog_data)
363 skip_bits |= IRIS_DIRTY_BLEND_STATE | IRIS_DIRTY_PS_BLEND;
364
365 ice->state.dirty |= ~skip_bits;
366
367 if (params->dst.enabled) {
368 iris_render_cache_add_bo(batch, params->dst.addr.buffer,
369 params->dst.view.format,
370 params->dst.aux_usage);
371 }
372 if (params->depth.enabled)
373 iris_depth_cache_add_bo(batch, params->depth.addr.buffer);
374 if (params->stencil.enabled)
375 iris_depth_cache_add_bo(batch, params->stencil.addr.buffer);
376 }
377
378 void
379 genX(init_blorp)(struct iris_context *ice)
380 {
381 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
382
383 blorp_init(&ice->blorp, ice, &screen->isl_dev);
384 ice->blorp.compiler = screen->compiler;
385 ice->blorp.lookup_shader = iris_blorp_lookup_shader;
386 ice->blorp.upload_shader = iris_blorp_upload_shader;
387 ice->blorp.exec = iris_blorp_exec;
388 }