iris: comment about reemitting and flushing
[mesa.git] / src / gallium / drivers / iris / iris_program.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 <errno.h>
25 #include "pipe/p_defines.h"
26 #include "pipe/p_state.h"
27 #include "pipe/p_context.h"
28 #include "pipe/p_screen.h"
29 #include "util/u_atomic.h"
30 #include "compiler/nir/nir.h"
31 #include "compiler/nir/nir_builder.h"
32 #include "intel/compiler/brw_compiler.h"
33 #include "intel/compiler/brw_nir.h"
34 #include "iris_context.h"
35
36 static unsigned
37 get_new_program_id(struct iris_screen *screen)
38 {
39 return p_atomic_inc_return(&screen->program_id);
40 }
41
42 struct iris_uncompiled_shader {
43 struct pipe_shader_state base;
44 unsigned program_id;
45 };
46
47 // XXX: need unify_interfaces() at link time...
48
49 static void *
50 iris_create_shader_state(struct pipe_context *ctx,
51 const struct pipe_shader_state *state)
52 {
53 //struct iris_context *ice = (struct iris_context *)ctx;
54 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
55
56 assert(state->type == PIPE_SHADER_IR_NIR);
57
58 nir_shader *nir = state->ir.nir;
59
60 struct iris_uncompiled_shader *ish =
61 calloc(1, sizeof(struct iris_uncompiled_shader));
62 if (!ish)
63 return NULL;
64
65 nir = brw_preprocess_nir(screen->compiler, nir);
66
67 ish->program_id = get_new_program_id(screen);
68 ish->base.type = PIPE_SHADER_IR_NIR;
69 ish->base.ir.nir = nir;
70
71 return ish;
72 }
73
74 static void
75 iris_delete_shader_state(struct pipe_context *ctx, void *hwcso)
76 {
77 struct iris_uncompiled_shader *ish = hwcso;
78
79 ralloc_free(ish->base.ir.nir);
80 free(ish);
81 }
82
83 static void
84 iris_bind_vs_state(struct pipe_context *ctx, void *hwcso)
85 {
86 struct iris_context *ice = (struct iris_context *)ctx;
87
88 ice->shaders.uncompiled[MESA_SHADER_VERTEX] = hwcso;
89 ice->state.dirty |= IRIS_DIRTY_UNCOMPILED_VS;
90 }
91
92 static void
93 iris_bind_tcs_state(struct pipe_context *ctx, void *hwcso)
94 {
95 struct iris_context *ice = (struct iris_context *)ctx;
96
97 ice->shaders.uncompiled[MESA_SHADER_TESS_CTRL] = hwcso;
98 ice->state.dirty |= IRIS_DIRTY_UNCOMPILED_TCS;
99 }
100
101 static void
102 iris_bind_tes_state(struct pipe_context *ctx, void *hwcso)
103 {
104 struct iris_context *ice = (struct iris_context *)ctx;
105
106 if (!!hwcso != !!ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL])
107 ice->state.dirty |= IRIS_DIRTY_URB;
108
109 ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL] = hwcso;
110 ice->state.dirty |= IRIS_DIRTY_UNCOMPILED_TES;
111 }
112
113 static void
114 iris_bind_gs_state(struct pipe_context *ctx, void *hwcso)
115 {
116 struct iris_context *ice = (struct iris_context *)ctx;
117
118 if (!!hwcso != !!ice->shaders.uncompiled[MESA_SHADER_GEOMETRY])
119 ice->state.dirty |= IRIS_DIRTY_URB;
120
121 ice->shaders.uncompiled[MESA_SHADER_GEOMETRY] = hwcso;
122 ice->state.dirty |= IRIS_DIRTY_UNCOMPILED_GS;
123 }
124
125 static void
126 iris_bind_fs_state(struct pipe_context *ctx, void *hwcso)
127 {
128 struct iris_context *ice = (struct iris_context *)ctx;
129
130 ice->shaders.uncompiled[MESA_SHADER_FRAGMENT] = hwcso;
131 ice->state.dirty |= IRIS_DIRTY_UNCOMPILED_FS;
132 }
133
134 /**
135 * Sets up the starting offsets for the groups of binding table entries
136 * common to all pipeline stages.
137 *
138 * Unused groups are initialized to 0xd0d0d0d0 to make it obvious that they're
139 * unused but also make sure that addition of small offsets to them will
140 * trigger some of our asserts that surface indices are < BRW_MAX_SURFACES.
141 */
142 static uint32_t
143 assign_common_binding_table_offsets(const struct gen_device_info *devinfo,
144 const struct nir_shader *nir,
145 struct brw_stage_prog_data *prog_data,
146 uint32_t next_binding_table_offset)
147 {
148 const struct shader_info *info = &nir->info;
149
150 if (info->num_textures) {
151 prog_data->binding_table.texture_start = next_binding_table_offset;
152 prog_data->binding_table.gather_texture_start = next_binding_table_offset;
153 next_binding_table_offset += info->num_textures;
154 } else {
155 prog_data->binding_table.texture_start = 0xd0d0d0d0;
156 prog_data->binding_table.gather_texture_start = 0xd0d0d0d0;
157 }
158
159 int num_ubos = info->num_ubos + (nir->num_uniforms > 0 ? 1 : 0);
160
161 if (num_ubos) {
162 //assert(info->num_ubos <= BRW_MAX_UBO);
163 prog_data->binding_table.ubo_start = next_binding_table_offset;
164 next_binding_table_offset += num_ubos;
165 } else {
166 prog_data->binding_table.ubo_start = 0xd0d0d0d0;
167 }
168
169 if (info->num_ssbos || info->num_abos) {
170 //assert(info->num_abos <= BRW_MAX_ABO);
171 //assert(info->num_ssbos <= BRW_MAX_SSBO);
172 prog_data->binding_table.ssbo_start = next_binding_table_offset;
173 next_binding_table_offset += info->num_abos + info->num_ssbos;
174 } else {
175 prog_data->binding_table.ssbo_start = 0xd0d0d0d0;
176 }
177
178 prog_data->binding_table.shader_time_start = 0xd0d0d0d0;
179
180 if (info->num_images) {
181 prog_data->binding_table.image_start = next_binding_table_offset;
182 next_binding_table_offset += info->num_images;
183 } else {
184 prog_data->binding_table.image_start = 0xd0d0d0d0;
185 }
186
187 /* This may or may not be used depending on how the compile goes. */
188 prog_data->binding_table.pull_constants_start = next_binding_table_offset;
189 next_binding_table_offset++;
190
191 /* Plane 0 is just the regular texture section */
192 prog_data->binding_table.plane_start[0] = prog_data->binding_table.texture_start;
193
194 prog_data->binding_table.plane_start[1] = next_binding_table_offset;
195 next_binding_table_offset += info->num_textures;
196
197 prog_data->binding_table.plane_start[2] = next_binding_table_offset;
198 next_binding_table_offset += info->num_textures;
199
200 /* prog_data->base.binding_table.size will be set by brw_mark_surface_used. */
201
202 //assert(next_binding_table_offset <= BRW_MAX_SURFACES);
203 return next_binding_table_offset;
204 }
205
206 static void
207 iris_setup_uniforms(const struct brw_compiler *compiler,
208 void *mem_ctx,
209 nir_shader *nir,
210 struct brw_stage_prog_data *prog_data)
211 {
212 prog_data->nr_params = nir->num_uniforms;
213 prog_data->param = rzalloc_array(mem_ctx, uint32_t, prog_data->nr_params);
214
215 nir_foreach_variable(var, &nir->uniforms) {
216 const unsigned components = glsl_get_components(var->type);
217
218 for (unsigned i = 0; i < components; i++) {
219 prog_data->param[var->data.driver_location] =
220 var->data.driver_location;
221 }
222 }
223
224 // XXX: vs clip planes?
225 brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);
226 }
227
228 static void
229 iris_setup_push_uniform_range(const struct brw_compiler *compiler,
230 struct brw_stage_prog_data *prog_data)
231 {
232 if (prog_data->nr_params) {
233 for (int i = 3; i > 0; i--)
234 prog_data->ubo_ranges[i] = prog_data->ubo_ranges[i - 1];
235
236 prog_data->ubo_ranges[0] = (struct brw_ubo_range) {
237 .block = 0,
238 .start = 0,
239 .length = DIV_ROUND_UP(prog_data->nr_params, 8),
240 };
241 }
242 }
243
244 static bool
245 iris_compile_vs(struct iris_context *ice,
246 struct iris_uncompiled_shader *ish,
247 const struct brw_vs_prog_key *key)
248 {
249 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
250 const struct brw_compiler *compiler = screen->compiler;
251 const struct gen_device_info *devinfo = &screen->devinfo;
252 void *mem_ctx = ralloc_context(NULL);
253 struct brw_vs_prog_data *vs_prog_data =
254 rzalloc(mem_ctx, struct brw_vs_prog_data);
255 struct brw_vue_prog_data *vue_prog_data = &vs_prog_data->base;
256 struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
257
258 assert(ish->base.type == PIPE_SHADER_IR_NIR);
259
260 nir_shader *nir = ish->base.ir.nir;
261
262 // XXX: alt mode
263 assign_common_binding_table_offsets(devinfo, nir, prog_data, 0);
264
265 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data);
266
267 brw_compute_vue_map(devinfo,
268 &vue_prog_data->vue_map, nir->info.outputs_written,
269 nir->info.separate_shader);
270
271 char *error_str = NULL;
272 const unsigned *program =
273 brw_compile_vs(compiler, &ice->dbg, mem_ctx, key, vs_prog_data,
274 nir, -1, &error_str);
275 if (program == NULL) {
276 dbg_printf("Failed to compile vertex shader: %s\n", error_str);
277 ralloc_free(mem_ctx);
278 return false;
279 }
280
281 iris_setup_push_uniform_range(compiler, prog_data);
282
283 iris_upload_and_bind_shader(ice, IRIS_CACHE_VS, key, program, prog_data);
284
285 ralloc_free(mem_ctx);
286 return true;
287 }
288
289 static void
290 iris_update_compiled_vs(struct iris_context *ice)
291 {
292 struct brw_vs_prog_key key;
293 ice->vtbl.populate_vs_key(ice, &key);
294
295 if (iris_bind_cached_shader(ice, IRIS_CACHE_VS, &key))
296 return;
297
298 UNUSED bool success =
299 iris_compile_vs(ice, ice->shaders.uncompiled[MESA_SHADER_VERTEX], &key);
300 }
301
302 static void
303 iris_update_compiled_tcs(struct iris_context *ice)
304 {
305 // XXX: TCS
306 }
307
308 static bool
309 iris_compile_tes(struct iris_context *ice,
310 struct iris_uncompiled_shader *ish,
311 const struct brw_tes_prog_key *key)
312 {
313 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
314 const struct brw_compiler *compiler = screen->compiler;
315 const struct gen_device_info *devinfo = &screen->devinfo;
316 void *mem_ctx = ralloc_context(NULL);
317 struct brw_tes_prog_data *tes_prog_data =
318 rzalloc(mem_ctx, struct brw_tes_prog_data);
319 struct brw_vue_prog_data *vue_prog_data = &tes_prog_data->base;
320 struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
321
322 assert(ish->base.type == PIPE_SHADER_IR_NIR);
323
324 nir_shader *nir = ish->base.ir.nir;
325
326 assign_common_binding_table_offsets(devinfo, nir, prog_data, 0);
327
328 struct brw_vue_map input_vue_map;
329 brw_compute_tess_vue_map(&input_vue_map, key->inputs_read,
330 key->patch_inputs_read);
331
332 char *error_str = NULL;
333 const unsigned *program =
334 brw_compile_tes(compiler, &ice->dbg, mem_ctx, key, &input_vue_map,
335 tes_prog_data, nir, NULL, -1, &error_str);
336 if (program == NULL) {
337 dbg_printf("Failed to compile vertex shader: %s\n", error_str);
338 ralloc_free(mem_ctx);
339 return false;
340 }
341
342 iris_setup_push_uniform_range(compiler, prog_data);
343
344 iris_upload_and_bind_shader(ice, IRIS_CACHE_TES, key, program, prog_data);
345
346 ralloc_free(mem_ctx);
347 return true;
348 }
349
350
351 static void
352 iris_update_compiled_tes(struct iris_context *ice)
353 {
354 struct iris_uncompiled_shader *ish =
355 ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL];
356
357 if (!ish)
358 return;
359
360 struct brw_tes_prog_key key;
361 ice->vtbl.populate_tes_key(ice, &key);
362
363 if (iris_bind_cached_shader(ice, IRIS_CACHE_TES, &key))
364 return;
365
366 UNUSED bool success = iris_compile_tes(ice, ish, &key);
367 }
368
369 static void
370 iris_update_compiled_gs(struct iris_context *ice)
371 {
372 // XXX: GS
373 }
374
375 static bool
376 iris_compile_fs(struct iris_context *ice,
377 struct iris_uncompiled_shader *ish,
378 const struct brw_wm_prog_key *key,
379 struct brw_vue_map *vue_map)
380 {
381 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
382 const struct brw_compiler *compiler = screen->compiler;
383 const struct gen_device_info *devinfo = &screen->devinfo;
384 void *mem_ctx = ralloc_context(NULL);
385 struct brw_wm_prog_data *fs_prog_data =
386 rzalloc(mem_ctx, struct brw_wm_prog_data);
387 struct brw_stage_prog_data *prog_data = &fs_prog_data->base;
388
389 assert(ish->base.type == PIPE_SHADER_IR_NIR);
390
391 nir_shader *nir = ish->base.ir.nir;
392
393 // XXX: alt mode
394 assign_common_binding_table_offsets(devinfo, nir, prog_data,
395 MAX2(key->nr_color_regions, 1));
396
397 iris_setup_uniforms(compiler, mem_ctx, nir, prog_data);
398
399 char *error_str = NULL;
400 const unsigned *program =
401 brw_compile_fs(compiler, &ice->dbg, mem_ctx, key, fs_prog_data,
402 nir, NULL, -1, -1, -1, true, false, vue_map, &error_str);
403 if (program == NULL) {
404 dbg_printf("Failed to compile fragment shader: %s\n", error_str);
405 ralloc_free(mem_ctx);
406 return false;
407 }
408
409 //brw_alloc_stage_scratch(brw, &brw->wm.base, prog_data.base.total_scratch);
410
411 iris_setup_push_uniform_range(compiler, prog_data);
412
413 iris_upload_and_bind_shader(ice, IRIS_CACHE_FS, key, program, prog_data);
414
415 ralloc_free(mem_ctx);
416 return true;
417 }
418
419 static void
420 iris_update_compiled_fs(struct iris_context *ice)
421 {
422 struct brw_wm_prog_key key;
423 ice->vtbl.populate_fs_key(ice, &key);
424
425 if (iris_bind_cached_shader(ice, IRIS_CACHE_FS, &key))
426 return;
427
428 UNUSED bool success =
429 iris_compile_fs(ice, ice->shaders.uncompiled[MESA_SHADER_FRAGMENT], &key,
430 ice->shaders.last_vue_map);
431 }
432
433 static void
434 update_last_vue_map(struct iris_context *ice)
435 {
436 struct brw_stage_prog_data *prog_data;
437
438 if (ice->shaders.prog[MESA_SHADER_GEOMETRY])
439 prog_data = ice->shaders.prog[MESA_SHADER_GEOMETRY]->prog_data;
440 else if (ice->shaders.prog[MESA_SHADER_TESS_EVAL])
441 prog_data = ice->shaders.prog[MESA_SHADER_TESS_EVAL]->prog_data;
442 else
443 prog_data = ice->shaders.prog[MESA_SHADER_VERTEX]->prog_data;
444
445 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
446 ice->shaders.last_vue_map = &vue_prog_data->vue_map;
447 }
448
449 static struct brw_vue_prog_data *
450 get_vue_prog_data(struct iris_context *ice, gl_shader_stage stage)
451 {
452 if (!ice->shaders.prog[stage])
453 return NULL;
454
455 return (void *) ice->shaders.prog[stage]->prog_data;
456 }
457
458 void
459 iris_update_compiled_shaders(struct iris_context *ice)
460 {
461 struct brw_vue_prog_data *old_prog_datas[4];
462 if (!(ice->state.dirty & IRIS_DIRTY_URB)) {
463 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++)
464 old_prog_datas[i] = get_vue_prog_data(ice, i);
465 }
466
467 // XXX: dirty bits...
468 iris_update_compiled_vs(ice);
469 iris_update_compiled_tcs(ice);
470 iris_update_compiled_tes(ice);
471 iris_update_compiled_gs(ice);
472 update_last_vue_map(ice);
473 iris_update_compiled_fs(ice);
474 // ...
475
476 if (!(ice->state.dirty & IRIS_DIRTY_URB)) {
477 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
478 struct brw_vue_prog_data *old = old_prog_datas[i];
479 struct brw_vue_prog_data *new = get_vue_prog_data(ice, i);
480 if (!!old != !!new ||
481 (new && new->urb_entry_size != old->urb_entry_size)) {
482 ice->state.dirty |= IRIS_DIRTY_URB;
483 break;
484 }
485 }
486 }
487 }
488
489 void
490 iris_init_program_functions(struct pipe_context *ctx)
491 {
492 ctx->create_vs_state = iris_create_shader_state;
493 ctx->create_tcs_state = iris_create_shader_state;
494 ctx->create_tes_state = iris_create_shader_state;
495 ctx->create_gs_state = iris_create_shader_state;
496 ctx->create_fs_state = iris_create_shader_state;
497
498 ctx->delete_vs_state = iris_delete_shader_state;
499 ctx->delete_tcs_state = iris_delete_shader_state;
500 ctx->delete_tes_state = iris_delete_shader_state;
501 ctx->delete_gs_state = iris_delete_shader_state;
502 ctx->delete_fs_state = iris_delete_shader_state;
503
504 ctx->bind_vs_state = iris_bind_vs_state;
505 ctx->bind_tcs_state = iris_bind_tcs_state;
506 ctx->bind_tes_state = iris_bind_tes_state;
507 ctx->bind_gs_state = iris_bind_gs_state;
508 ctx->bind_fs_state = iris_bind_fs_state;
509 }