2 * Copyright © 2017 Intel Corporation
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:
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
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.
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"
37 get_new_program_id(struct iris_screen
*screen
)
39 return p_atomic_inc_return(&screen
->program_id
);
42 struct iris_uncompiled_shader
{
43 struct pipe_shader_state base
;
47 // XXX: need unify_interfaces() at link time...
50 iris_create_shader_state(struct pipe_context
*ctx
,
51 const struct pipe_shader_state
*state
)
53 //struct iris_context *ice = (struct iris_context *)ctx;
54 struct iris_screen
*screen
= (struct iris_screen
*)ctx
->screen
;
56 assert(state
->type
== PIPE_SHADER_IR_NIR
);
58 nir_shader
*nir
= state
->ir
.nir
;
60 struct iris_uncompiled_shader
*ish
=
61 calloc(1, sizeof(struct iris_uncompiled_shader
));
65 nir
= brw_preprocess_nir(screen
->compiler
, nir
);
67 ish
->program_id
= get_new_program_id(screen
);
68 ish
->base
.type
= PIPE_SHADER_IR_NIR
;
69 ish
->base
.ir
.nir
= nir
;
75 iris_delete_shader_state(struct pipe_context
*ctx
, void *hwcso
)
77 struct iris_uncompiled_shader
*ish
= hwcso
;
79 ralloc_free(ish
->base
.ir
.nir
);
84 iris_bind_vs_state(struct pipe_context
*ctx
, void *hwcso
)
86 struct iris_context
*ice
= (struct iris_context
*)ctx
;
88 ice
->shaders
.uncompiled
[MESA_SHADER_VERTEX
] = hwcso
;
89 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_VS
;
93 iris_bind_tcs_state(struct pipe_context
*ctx
, void *hwcso
)
95 struct iris_context
*ice
= (struct iris_context
*)ctx
;
97 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_CTRL
] = hwcso
;
98 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_TCS
;
102 iris_bind_tes_state(struct pipe_context
*ctx
, void *hwcso
)
104 struct iris_context
*ice
= (struct iris_context
*)ctx
;
106 if (!!hwcso
!= !!ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
])
107 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
109 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
] = hwcso
;
110 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_TES
;
114 iris_bind_gs_state(struct pipe_context
*ctx
, void *hwcso
)
116 struct iris_context
*ice
= (struct iris_context
*)ctx
;
118 if (!!hwcso
!= !!ice
->shaders
.uncompiled
[MESA_SHADER_GEOMETRY
])
119 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
121 ice
->shaders
.uncompiled
[MESA_SHADER_GEOMETRY
] = hwcso
;
122 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_GS
;
126 iris_bind_fs_state(struct pipe_context
*ctx
, void *hwcso
)
128 struct iris_context
*ice
= (struct iris_context
*)ctx
;
130 ice
->shaders
.uncompiled
[MESA_SHADER_FRAGMENT
] = hwcso
;
131 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_FS
;
135 * Sets up the starting offsets for the groups of binding table entries
136 * common to all pipeline stages.
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.
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
)
148 const struct shader_info
*info
= &nir
->info
;
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
;
155 prog_data
->binding_table
.texture_start
= 0xd0d0d0d0;
156 prog_data
->binding_table
.gather_texture_start
= 0xd0d0d0d0;
159 int num_ubos
= info
->num_ubos
+ (nir
->num_uniforms
> 0 ? 1 : 0);
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
;
166 prog_data
->binding_table
.ubo_start
= 0xd0d0d0d0;
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
;
175 prog_data
->binding_table
.ssbo_start
= 0xd0d0d0d0;
178 prog_data
->binding_table
.shader_time_start
= 0xd0d0d0d0;
180 if (info
->num_images
) {
181 prog_data
->binding_table
.image_start
= next_binding_table_offset
;
182 next_binding_table_offset
+= info
->num_images
;
184 prog_data
->binding_table
.image_start
= 0xd0d0d0d0;
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
++;
191 /* Plane 0 is just the regular texture section */
192 prog_data
->binding_table
.plane_start
[0] = prog_data
->binding_table
.texture_start
;
194 prog_data
->binding_table
.plane_start
[1] = next_binding_table_offset
;
195 next_binding_table_offset
+= info
->num_textures
;
197 prog_data
->binding_table
.plane_start
[2] = next_binding_table_offset
;
198 next_binding_table_offset
+= info
->num_textures
;
200 /* prog_data->base.binding_table.size will be set by brw_mark_surface_used. */
202 //assert(next_binding_table_offset <= BRW_MAX_SURFACES);
203 return next_binding_table_offset
;
207 iris_setup_uniforms(const struct brw_compiler
*compiler
,
210 struct brw_stage_prog_data
*prog_data
)
212 prog_data
->nr_params
= nir
->num_uniforms
;
213 prog_data
->param
= rzalloc_array(mem_ctx
, uint32_t, prog_data
->nr_params
);
215 nir_foreach_variable(var
, &nir
->uniforms
) {
216 const unsigned components
= glsl_get_components(var
->type
);
218 for (unsigned i
= 0; i
< components
; i
++) {
219 prog_data
->param
[var
->data
.driver_location
] =
220 var
->data
.driver_location
;
224 // XXX: vs clip planes?
225 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
229 iris_setup_push_uniform_range(const struct brw_compiler
*compiler
,
230 struct brw_stage_prog_data
*prog_data
)
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];
236 prog_data
->ubo_ranges
[0] = (struct brw_ubo_range
) {
239 .length
= DIV_ROUND_UP(prog_data
->nr_params
, 8),
245 iris_compile_vs(struct iris_context
*ice
,
246 struct iris_uncompiled_shader
*ish
,
247 const struct brw_vs_prog_key
*key
)
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
;
258 assert(ish
->base
.type
== PIPE_SHADER_IR_NIR
);
260 nir_shader
*nir
= ish
->base
.ir
.nir
;
263 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
, 0);
265 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
);
267 brw_compute_vue_map(devinfo
,
268 &vue_prog_data
->vue_map
, nir
->info
.outputs_written
,
269 nir
->info
.separate_shader
);
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
);
281 iris_setup_push_uniform_range(compiler
, prog_data
);
283 iris_upload_and_bind_shader(ice
, IRIS_CACHE_VS
, key
, program
, prog_data
);
285 ralloc_free(mem_ctx
);
290 iris_update_compiled_vs(struct iris_context
*ice
)
292 struct brw_vs_prog_key key
;
293 ice
->vtbl
.populate_vs_key(ice
, &key
);
295 if (iris_bind_cached_shader(ice
, IRIS_CACHE_VS
, &key
))
298 UNUSED
bool success
=
299 iris_compile_vs(ice
, ice
->shaders
.uncompiled
[MESA_SHADER_VERTEX
], &key
);
303 iris_update_compiled_tcs(struct iris_context
*ice
)
309 iris_compile_tes(struct iris_context
*ice
,
310 struct iris_uncompiled_shader
*ish
,
311 const struct brw_tes_prog_key
*key
)
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
;
322 assert(ish
->base
.type
== PIPE_SHADER_IR_NIR
);
324 nir_shader
*nir
= ish
->base
.ir
.nir
;
326 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
, 0);
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
);
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
);
342 iris_setup_push_uniform_range(compiler
, prog_data
);
344 iris_upload_and_bind_shader(ice
, IRIS_CACHE_TES
, key
, program
, prog_data
);
346 ralloc_free(mem_ctx
);
352 iris_update_compiled_tes(struct iris_context
*ice
)
354 struct iris_uncompiled_shader
*ish
=
355 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
];
360 struct brw_tes_prog_key key
;
361 ice
->vtbl
.populate_tes_key(ice
, &key
);
363 if (iris_bind_cached_shader(ice
, IRIS_CACHE_TES
, &key
))
366 UNUSED
bool success
= iris_compile_tes(ice
, ish
, &key
);
370 iris_update_compiled_gs(struct iris_context
*ice
)
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
)
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
;
389 assert(ish
->base
.type
== PIPE_SHADER_IR_NIR
);
391 nir_shader
*nir
= ish
->base
.ir
.nir
;
394 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
,
395 MAX2(key
->nr_color_regions
, 1));
397 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
);
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
);
409 //brw_alloc_stage_scratch(brw, &brw->wm.base, prog_data.base.total_scratch);
411 iris_setup_push_uniform_range(compiler
, prog_data
);
413 iris_upload_and_bind_shader(ice
, IRIS_CACHE_FS
, key
, program
, prog_data
);
415 ralloc_free(mem_ctx
);
420 iris_update_compiled_fs(struct iris_context
*ice
)
422 struct brw_wm_prog_key key
;
423 ice
->vtbl
.populate_fs_key(ice
, &key
);
425 if (iris_bind_cached_shader(ice
, IRIS_CACHE_FS
, &key
))
428 UNUSED
bool success
=
429 iris_compile_fs(ice
, ice
->shaders
.uncompiled
[MESA_SHADER_FRAGMENT
], &key
,
430 ice
->shaders
.last_vue_map
);
434 update_last_vue_map(struct iris_context
*ice
)
436 struct brw_stage_prog_data
*prog_data
;
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
;
443 prog_data
= ice
->shaders
.prog
[MESA_SHADER_VERTEX
]->prog_data
;
445 struct brw_vue_prog_data
*vue_prog_data
= (void *) prog_data
;
446 struct brw_vue_map
*vue_map
= &vue_prog_data
->vue_map
;
447 struct brw_vue_map
*old_map
= ice
->shaders
.last_vue_map
;
448 const uint64_t changed_slots
=
449 (old_map
? old_map
->slots_valid
: 0ull) ^ vue_map
->slots_valid
;
451 if (changed_slots
& VARYING_BIT_VIEWPORT
) {
452 // XXX: could use ctx->Const.MaxViewports for old API efficiency
453 ice
->state
.num_viewports
=
454 (vue_map
->slots_valid
& VARYING_BIT_VIEWPORT
) ? IRIS_MAX_VIEWPORTS
: 1;
455 ice
->state
.dirty
|= IRIS_DIRTY_CLIP
|
456 IRIS_DIRTY_SF_CL_VIEWPORT
|
457 IRIS_DIRTY_SCISSOR_RECT
|
458 IRIS_DIRTY_UNCOMPILED_FS
;
462 ice
->shaders
.last_vue_map
= &vue_prog_data
->vue_map
;
465 static struct brw_vue_prog_data
*
466 get_vue_prog_data(struct iris_context
*ice
, gl_shader_stage stage
)
468 if (!ice
->shaders
.prog
[stage
])
471 return (void *) ice
->shaders
.prog
[stage
]->prog_data
;
475 iris_update_compiled_shaders(struct iris_context
*ice
)
477 const uint64_t dirty
= ice
->state
.dirty
;
479 struct brw_vue_prog_data
*old_prog_datas
[4];
480 if (!(dirty
& IRIS_DIRTY_URB
)) {
481 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++)
482 old_prog_datas
[i
] = get_vue_prog_data(ice
, i
);
485 if (dirty
& IRIS_DIRTY_UNCOMPILED_VS
)
486 iris_update_compiled_vs(ice
);
487 if (dirty
& IRIS_DIRTY_UNCOMPILED_TCS
)
488 iris_update_compiled_tcs(ice
);
489 if (dirty
& IRIS_DIRTY_UNCOMPILED_TES
)
490 iris_update_compiled_tes(ice
);
491 if (dirty
& IRIS_DIRTY_UNCOMPILED_GS
)
492 iris_update_compiled_gs(ice
);
494 update_last_vue_map(ice
);
496 if (dirty
& IRIS_DIRTY_UNCOMPILED_FS
)
497 iris_update_compiled_fs(ice
);
500 if (!(dirty
& IRIS_DIRTY_URB
)) {
501 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
502 struct brw_vue_prog_data
*old
= old_prog_datas
[i
];
503 struct brw_vue_prog_data
*new = get_vue_prog_data(ice
, i
);
504 if (!!old
!= !!new ||
505 (new && new->urb_entry_size
!= old
->urb_entry_size
)) {
506 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
514 iris_init_program_functions(struct pipe_context
*ctx
)
516 ctx
->create_vs_state
= iris_create_shader_state
;
517 ctx
->create_tcs_state
= iris_create_shader_state
;
518 ctx
->create_tes_state
= iris_create_shader_state
;
519 ctx
->create_gs_state
= iris_create_shader_state
;
520 ctx
->create_fs_state
= iris_create_shader_state
;
522 ctx
->delete_vs_state
= iris_delete_shader_state
;
523 ctx
->delete_tcs_state
= iris_delete_shader_state
;
524 ctx
->delete_tes_state
= iris_delete_shader_state
;
525 ctx
->delete_gs_state
= iris_delete_shader_state
;
526 ctx
->delete_fs_state
= iris_delete_shader_state
;
528 ctx
->bind_vs_state
= iris_bind_vs_state
;
529 ctx
->bind_tcs_state
= iris_bind_tcs_state
;
530 ctx
->bind_tes_state
= iris_bind_tes_state
;
531 ctx
->bind_gs_state
= iris_bind_gs_state
;
532 ctx
->bind_fs_state
= iris_bind_fs_state
;