ecca3928de546d74229dfeb32fbb29d14d70fe83
[mesa.git] / src / intel / vulkan / genX_blorp_exec.c
1 /*
2 * Copyright © 2016 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 <assert.h>
25
26 #include "anv_private.h"
27
28 /* These are defined in anv_private.h and blorp_genX_exec.h */
29 #undef __gen_address_type
30 #undef __gen_user_data
31 #undef __gen_combine_address
32
33 #include "common/gen_l3_config.h"
34 #include "common/gen_sample_positions.h"
35 #include "blorp/blorp_genX_exec.h"
36
37 static void *
38 blorp_emit_dwords(struct blorp_batch *batch, unsigned n)
39 {
40 struct anv_cmd_buffer *cmd_buffer = batch->driver_batch;
41 return anv_batch_emit_dwords(&cmd_buffer->batch, n);
42 }
43
44 static uint64_t
45 blorp_emit_reloc(struct blorp_batch *batch,
46 void *location, struct blorp_address address, uint32_t delta)
47 {
48 struct anv_cmd_buffer *cmd_buffer = batch->driver_batch;
49 assert(cmd_buffer->batch.start <= location &&
50 location < cmd_buffer->batch.end);
51 return anv_batch_emit_reloc(&cmd_buffer->batch, location,
52 address.buffer, address.offset + delta);
53 }
54
55 static void
56 blorp_surface_reloc(struct blorp_batch *batch, uint32_t ss_offset,
57 struct blorp_address address, uint32_t delta)
58 {
59 struct anv_cmd_buffer *cmd_buffer = batch->driver_batch;
60 VkResult result =
61 anv_reloc_list_add(&cmd_buffer->surface_relocs, &cmd_buffer->pool->alloc,
62 ss_offset, address.buffer, address.offset + delta);
63 if (result != VK_SUCCESS)
64 anv_batch_set_error(&cmd_buffer->batch, result);
65
66 void *dest = cmd_buffer->device->surface_state_pool.block_pool.map +
67 ss_offset;
68 uint64_t val = ((struct anv_bo*)address.buffer)->offset + address.offset +
69 delta;
70 write_reloc(cmd_buffer->device, dest, val, false);
71 }
72
73 #if GEN_GEN >= 7 && GEN_GEN < 10
74 static struct blorp_address
75 blorp_get_surface_base_address(struct blorp_batch *batch)
76 {
77 struct anv_cmd_buffer *cmd_buffer = batch->driver_batch;
78 return (struct blorp_address) {
79 .buffer = &cmd_buffer->device->surface_state_pool.block_pool.bo,
80 .offset = 0,
81 };
82 }
83 #endif
84
85 static void *
86 blorp_alloc_dynamic_state(struct blorp_batch *batch,
87 uint32_t size,
88 uint32_t alignment,
89 uint32_t *offset)
90 {
91 struct anv_cmd_buffer *cmd_buffer = batch->driver_batch;
92
93 struct anv_state state =
94 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, size, alignment);
95
96 *offset = state.offset;
97 return state.map;
98 }
99
100 static void
101 blorp_alloc_binding_table(struct blorp_batch *batch, unsigned num_entries,
102 unsigned state_size, unsigned state_alignment,
103 uint32_t *bt_offset,
104 uint32_t *surface_offsets, void **surface_maps)
105 {
106 struct anv_cmd_buffer *cmd_buffer = batch->driver_batch;
107
108 uint32_t state_offset;
109 struct anv_state bt_state;
110
111 VkResult result =
112 anv_cmd_buffer_alloc_blorp_binding_table(cmd_buffer, num_entries,
113 &state_offset, &bt_state);
114 if (result != VK_SUCCESS)
115 return;
116
117 uint32_t *bt_map = bt_state.map;
118 *bt_offset = bt_state.offset;
119
120 for (unsigned i = 0; i < num_entries; i++) {
121 struct anv_state surface_state =
122 anv_cmd_buffer_alloc_surface_state(cmd_buffer);
123 bt_map[i] = surface_state.offset + state_offset;
124 surface_offsets[i] = surface_state.offset;
125 surface_maps[i] = surface_state.map;
126 }
127
128 anv_state_flush(cmd_buffer->device, bt_state);
129 }
130
131 static void *
132 blorp_alloc_vertex_buffer(struct blorp_batch *batch, uint32_t size,
133 struct blorp_address *addr)
134 {
135 struct anv_cmd_buffer *cmd_buffer = batch->driver_batch;
136
137 /* From the Skylake PRM, 3DSTATE_VERTEX_BUFFERS:
138 *
139 * "The VF cache needs to be invalidated before binding and then using
140 * Vertex Buffers that overlap with any previously bound Vertex Buffer
141 * (at a 64B granularity) since the last invalidation. A VF cache
142 * invalidate is performed by setting the "VF Cache Invalidation Enable"
143 * bit in PIPE_CONTROL."
144 *
145 * This restriction first appears in the Skylake PRM but the internal docs
146 * also list it as being an issue on Broadwell. In order to avoid this
147 * problem, we align all vertex buffer allocations to 64 bytes.
148 */
149 struct anv_state vb_state =
150 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, size, 64);
151
152 *addr = (struct blorp_address) {
153 .buffer = &cmd_buffer->device->dynamic_state_pool.block_pool.bo,
154 .offset = vb_state.offset,
155 .mocs = cmd_buffer->device->default_mocs,
156 };
157
158 return vb_state.map;
159 }
160
161 #if GEN_GEN >= 8
162 static struct blorp_address
163 blorp_get_workaround_page(struct blorp_batch *batch)
164 {
165 struct anv_cmd_buffer *cmd_buffer = batch->driver_batch;
166
167 return (struct blorp_address) {
168 .buffer = &cmd_buffer->device->workaround_bo,
169 };
170 }
171 #endif
172
173 static void
174 blorp_flush_range(struct blorp_batch *batch, void *start, size_t size)
175 {
176 struct anv_device *device = batch->blorp->driver_ctx;
177 if (!device->info.has_llc)
178 gen_flush_range(start, size);
179 }
180
181 static void
182 blorp_emit_urb_config(struct blorp_batch *batch,
183 unsigned vs_entry_size, unsigned sf_entry_size)
184 {
185 struct anv_device *device = batch->blorp->driver_ctx;
186 struct anv_cmd_buffer *cmd_buffer = batch->driver_batch;
187
188 assert(sf_entry_size == 0);
189
190 const unsigned entry_size[4] = { vs_entry_size, 1, 1, 1 };
191
192 genX(emit_urb_setup)(device, &cmd_buffer->batch,
193 cmd_buffer->state.current_l3_config,
194 VK_SHADER_STAGE_VERTEX_BIT |
195 VK_SHADER_STAGE_FRAGMENT_BIT,
196 entry_size);
197 }
198
199 void
200 genX(blorp_exec)(struct blorp_batch *batch,
201 const struct blorp_params *params)
202 {
203 struct anv_cmd_buffer *cmd_buffer = batch->driver_batch;
204
205 if (!cmd_buffer->state.current_l3_config) {
206 const struct gen_l3_config *cfg =
207 gen_get_default_l3_config(&cmd_buffer->device->info);
208 genX(cmd_buffer_config_l3)(cmd_buffer, cfg);
209 }
210
211 #if GEN_GEN >= 11
212 /* The PIPE_CONTROL command description says:
213 *
214 * "Whenever a Binding Table Index (BTI) used by a Render Taget Message
215 * points to a different RENDER_SURFACE_STATE, SW must issue a Render
216 * Target Cache Flush by enabling this bit. When render target flush
217 * is set due to new association of BTI, PS Scoreboard Stall bit must
218 * be set in this packet."
219 */
220 cmd_buffer->state.pending_pipe_bits |=
221 ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT |
222 ANV_PIPE_STALL_AT_SCOREBOARD_BIT;
223 #endif
224
225 #if GEN_GEN == 7
226 /* The MI_LOAD/STORE_REGISTER_MEM commands which BLORP uses to implement
227 * indirect fast-clear colors can cause GPU hangs if we don't stall first.
228 * See genX(cmd_buffer_mi_memcpy) for more details.
229 */
230 if (params->src.clear_color_addr.buffer ||
231 params->dst.clear_color_addr.buffer)
232 cmd_buffer->state.pending_pipe_bits |= ANV_PIPE_CS_STALL_BIT;
233 #endif
234
235 genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
236
237 genX(flush_pipeline_select_3d)(cmd_buffer);
238
239 genX(cmd_buffer_emit_gen7_depth_flush)(cmd_buffer);
240
241 /* BLORP doesn't do anything fancy with depth such as discards, so we want
242 * the PMA fix off. Also, off is always the safe option.
243 */
244 genX(cmd_buffer_enable_pma_fix)(cmd_buffer, false);
245
246 /* Disable VF statistics */
247 blorp_emit(batch, GENX(3DSTATE_VF_STATISTICS), vf) {
248 vf.StatisticsEnable = false;
249 }
250
251 blorp_exec(batch, params);
252
253 cmd_buffer->state.gfx.vb_dirty = ~0;
254 cmd_buffer->state.gfx.dirty = ~0;
255 cmd_buffer->state.push_constants_dirty = ~0;
256 }