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
);
68 /* Reassign uniform locations using type_size_scalar_bytes instead of
69 * the slot based calculation that st_nir uses.
71 nir_assign_var_locations(&nir
->uniforms
, &nir
->num_uniforms
,
72 type_size_scalar_bytes
);
73 nir_lower_io(nir
, nir_var_uniform
, type_size_scalar_bytes
, 0);
75 nir_foreach_variable(var
, &nir
->uniforms
) {
76 var
->data
.driver_location
*= 4;
78 nir_lower_io(nir
, nir_var_uniform
, type_size_vec4_bytes
, 0);
80 ish
->program_id
= get_new_program_id(screen
);
81 ish
->base
.type
= PIPE_SHADER_IR_NIR
;
82 ish
->base
.ir
.nir
= nir
;
88 iris_delete_shader_state(struct pipe_context
*ctx
, void *hwcso
)
90 struct iris_uncompiled_shader
*ish
= hwcso
;
92 ralloc_free(ish
->base
.ir
.nir
);
97 iris_bind_vs_state(struct pipe_context
*ctx
, void *hwcso
)
99 struct iris_context
*ice
= (struct iris_context
*)ctx
;
101 ice
->shaders
.uncompiled
[MESA_SHADER_VERTEX
] = hwcso
;
102 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_VS
;
106 iris_bind_tcs_state(struct pipe_context
*ctx
, void *hwcso
)
108 struct iris_context
*ice
= (struct iris_context
*)ctx
;
110 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_CTRL
] = hwcso
;
111 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_TCS
;
115 iris_bind_tes_state(struct pipe_context
*ctx
, void *hwcso
)
117 struct iris_context
*ice
= (struct iris_context
*)ctx
;
119 if (!!hwcso
!= !!ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
])
120 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
122 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
] = hwcso
;
123 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_TES
;
127 iris_bind_gs_state(struct pipe_context
*ctx
, void *hwcso
)
129 struct iris_context
*ice
= (struct iris_context
*)ctx
;
131 if (!!hwcso
!= !!ice
->shaders
.uncompiled
[MESA_SHADER_GEOMETRY
])
132 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
134 ice
->shaders
.uncompiled
[MESA_SHADER_GEOMETRY
] = hwcso
;
135 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_GS
;
139 iris_bind_fs_state(struct pipe_context
*ctx
, void *hwcso
)
141 struct iris_context
*ice
= (struct iris_context
*)ctx
;
143 ice
->shaders
.uncompiled
[MESA_SHADER_FRAGMENT
] = hwcso
;
144 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_FS
;
148 * Sets up the starting offsets for the groups of binding table entries
149 * common to all pipeline stages.
151 * Unused groups are initialized to 0xd0d0d0d0 to make it obvious that they're
152 * unused but also make sure that addition of small offsets to them will
153 * trigger some of our asserts that surface indices are < BRW_MAX_SURFACES.
156 assign_common_binding_table_offsets(const struct gen_device_info
*devinfo
,
157 const struct shader_info
*info
,
158 struct brw_stage_prog_data
*prog_data
,
159 uint32_t next_binding_table_offset
)
161 if (info
->num_textures
) {
162 prog_data
->binding_table
.texture_start
= next_binding_table_offset
;
163 prog_data
->binding_table
.gather_texture_start
= next_binding_table_offset
;
164 next_binding_table_offset
+= info
->num_textures
;
166 prog_data
->binding_table
.texture_start
= 0xd0d0d0d0;
167 prog_data
->binding_table
.gather_texture_start
= 0xd0d0d0d0;
170 if (info
->num_ubos
) {
171 //assert(info->num_ubos <= BRW_MAX_UBO);
172 prog_data
->binding_table
.ubo_start
= next_binding_table_offset
;
173 next_binding_table_offset
+= info
->num_ubos
;
175 prog_data
->binding_table
.ubo_start
= 0xd0d0d0d0;
178 if (info
->num_ssbos
|| info
->num_abos
) {
179 //assert(info->num_abos <= BRW_MAX_ABO);
180 //assert(info->num_ssbos <= BRW_MAX_SSBO);
181 prog_data
->binding_table
.ssbo_start
= next_binding_table_offset
;
182 next_binding_table_offset
+= info
->num_abos
+ info
->num_ssbos
;
184 prog_data
->binding_table
.ssbo_start
= 0xd0d0d0d0;
187 prog_data
->binding_table
.shader_time_start
= 0xd0d0d0d0;
189 if (info
->num_images
) {
190 prog_data
->binding_table
.image_start
= next_binding_table_offset
;
191 next_binding_table_offset
+= info
->num_images
;
193 prog_data
->binding_table
.image_start
= 0xd0d0d0d0;
196 /* This may or may not be used depending on how the compile goes. */
197 prog_data
->binding_table
.pull_constants_start
= next_binding_table_offset
;
198 next_binding_table_offset
++;
200 /* Plane 0 is just the regular texture section */
201 prog_data
->binding_table
.plane_start
[0] = prog_data
->binding_table
.texture_start
;
203 prog_data
->binding_table
.plane_start
[1] = next_binding_table_offset
;
204 next_binding_table_offset
+= info
->num_textures
;
206 prog_data
->binding_table
.plane_start
[2] = next_binding_table_offset
;
207 next_binding_table_offset
+= info
->num_textures
;
209 /* prog_data->base.binding_table.size will be set by brw_mark_surface_used. */
211 //assert(next_binding_table_offset <= BRW_MAX_SURFACES);
212 return next_binding_table_offset
;
216 iris_setup_uniforms(void *mem_ctx
,
218 struct brw_stage_prog_data
*prog_data
)
220 prog_data
->nr_params
= nir
->num_uniforms
* 4;
221 prog_data
->param
= rzalloc_array(mem_ctx
, uint32_t, prog_data
->nr_params
);
223 nir
->num_uniforms
*= 16;
225 nir_foreach_variable(var
, &nir
->uniforms
) {
226 /* UBO's, atomics and samplers don't take up space */
227 //if (var->interface_type != NULL || var->type->contains_atomic())
230 const unsigned components
= glsl_get_components(var
->type
);
232 for (unsigned i
= 0; i
< 4; i
++) {
233 prog_data
->param
[var
->data
.driver_location
] =
234 i
< components
? BRW_PARAM_PARAMETER(var
->data
.driver_location
, i
)
235 : BRW_PARAM_BUILTIN_ZERO
;
241 iris_compile_vs(struct iris_context
*ice
,
242 struct iris_uncompiled_shader
*ish
,
243 const struct brw_vs_prog_key
*key
)
245 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
246 const struct brw_compiler
*compiler
= screen
->compiler
;
247 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
248 void *mem_ctx
= ralloc_context(NULL
);
249 struct brw_vs_prog_data
*vs_prog_data
=
250 rzalloc(mem_ctx
, struct brw_vs_prog_data
);
251 struct brw_vue_prog_data
*vue_prog_data
= &vs_prog_data
->base
;
252 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
254 assert(ish
->base
.type
== PIPE_SHADER_IR_NIR
);
256 nir_shader
*nir
= ish
->base
.ir
.nir
;
259 assign_common_binding_table_offsets(devinfo
, &nir
->info
, prog_data
, 0);
261 iris_setup_uniforms(mem_ctx
, nir
, prog_data
);
263 brw_compute_vue_map(devinfo
,
264 &vue_prog_data
->vue_map
, nir
->info
.outputs_written
,
265 nir
->info
.separate_shader
);
267 char *error_str
= NULL
;
268 const unsigned *program
=
269 brw_compile_vs(compiler
, &ice
->dbg
, mem_ctx
, key
, vs_prog_data
,
270 nir
, -1, &error_str
);
271 if (program
== NULL
) {
272 dbg_printf("Failed to compile vertex shader: %s\n", error_str
);
273 ralloc_free(mem_ctx
);
277 iris_upload_and_bind_shader(ice
, IRIS_CACHE_VS
, key
, program
, prog_data
);
279 ralloc_free(mem_ctx
);
284 iris_update_compiled_vs(struct iris_context
*ice
)
286 struct brw_vs_prog_key key
;
287 ice
->vtbl
.populate_vs_key(ice
, &key
);
289 if (iris_bind_cached_shader(ice
, IRIS_CACHE_VS
, &key
))
292 UNUSED
bool success
=
293 iris_compile_vs(ice
, ice
->shaders
.uncompiled
[MESA_SHADER_VERTEX
], &key
);
297 iris_update_compiled_tcs(struct iris_context
*ice
)
303 iris_compile_tes(struct iris_context
*ice
,
304 struct iris_uncompiled_shader
*ish
,
305 const struct brw_tes_prog_key
*key
)
307 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
308 const struct brw_compiler
*compiler
= screen
->compiler
;
309 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
310 void *mem_ctx
= ralloc_context(NULL
);
311 struct brw_tes_prog_data
*tes_prog_data
=
312 rzalloc(mem_ctx
, struct brw_tes_prog_data
);
313 struct brw_vue_prog_data
*vue_prog_data
= &tes_prog_data
->base
;
314 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
316 assert(ish
->base
.type
== PIPE_SHADER_IR_NIR
);
318 nir_shader
*nir
= ish
->base
.ir
.nir
;
320 assign_common_binding_table_offsets(devinfo
, &nir
->info
, prog_data
, 0);
322 struct brw_vue_map input_vue_map
;
323 brw_compute_tess_vue_map(&input_vue_map
, key
->inputs_read
,
324 key
->patch_inputs_read
);
326 char *error_str
= NULL
;
327 const unsigned *program
=
328 brw_compile_tes(compiler
, &ice
->dbg
, mem_ctx
, key
, &input_vue_map
,
329 tes_prog_data
, nir
, NULL
, -1, &error_str
);
330 if (program
== NULL
) {
331 dbg_printf("Failed to compile vertex shader: %s\n", error_str
);
332 ralloc_free(mem_ctx
);
336 iris_upload_and_bind_shader(ice
, IRIS_CACHE_TES
, key
, program
, prog_data
);
338 ralloc_free(mem_ctx
);
344 iris_update_compiled_tes(struct iris_context
*ice
)
346 struct iris_uncompiled_shader
*ish
=
347 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
];
352 struct brw_tes_prog_key key
;
353 ice
->vtbl
.populate_tes_key(ice
, &key
);
355 if (iris_bind_cached_shader(ice
, IRIS_CACHE_TES
, &key
))
358 UNUSED
bool success
= iris_compile_tes(ice
, ish
, &key
);
362 iris_update_compiled_gs(struct iris_context
*ice
)
368 iris_compile_fs(struct iris_context
*ice
,
369 struct iris_uncompiled_shader
*ish
,
370 const struct brw_wm_prog_key
*key
,
371 struct brw_vue_map
*vue_map
)
373 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
374 const struct brw_compiler
*compiler
= screen
->compiler
;
375 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
376 void *mem_ctx
= ralloc_context(NULL
);
377 struct brw_wm_prog_data
*fs_prog_data
=
378 rzalloc(mem_ctx
, struct brw_wm_prog_data
);
379 struct brw_stage_prog_data
*prog_data
= &fs_prog_data
->base
;
381 assert(ish
->base
.type
== PIPE_SHADER_IR_NIR
);
383 nir_shader
*nir
= ish
->base
.ir
.nir
;
386 assign_common_binding_table_offsets(devinfo
, &nir
->info
, prog_data
,
387 MAX2(key
->nr_color_regions
, 1));
389 iris_setup_uniforms(mem_ctx
, nir
, prog_data
);
391 char *error_str
= NULL
;
392 const unsigned *program
=
393 brw_compile_fs(compiler
, &ice
->dbg
, mem_ctx
, key
, fs_prog_data
,
394 nir
, NULL
, -1, -1, -1, true, false, vue_map
, &error_str
);
395 if (program
== NULL
) {
396 dbg_printf("Failed to compile fragment shader: %s\n", error_str
);
397 ralloc_free(mem_ctx
);
401 //brw_alloc_stage_scratch(brw, &brw->wm.base, prog_data.base.total_scratch);
403 iris_upload_and_bind_shader(ice
, IRIS_CACHE_FS
, key
, program
, prog_data
);
405 ralloc_free(mem_ctx
);
410 iris_update_compiled_fs(struct iris_context
*ice
)
412 struct brw_wm_prog_key key
;
413 ice
->vtbl
.populate_fs_key(ice
, &key
);
415 if (iris_bind_cached_shader(ice
, IRIS_CACHE_FS
, &key
))
418 UNUSED
bool success
=
419 iris_compile_fs(ice
, ice
->shaders
.uncompiled
[MESA_SHADER_FRAGMENT
], &key
,
420 ice
->shaders
.last_vue_map
);
424 update_last_vue_map(struct iris_context
*ice
)
426 struct brw_stage_prog_data
*prog_data
;
428 if (ice
->shaders
.prog
[MESA_SHADER_GEOMETRY
])
429 prog_data
= ice
->shaders
.prog
[MESA_SHADER_GEOMETRY
]->prog_data
;
430 else if (ice
->shaders
.prog
[MESA_SHADER_TESS_EVAL
])
431 prog_data
= ice
->shaders
.prog
[MESA_SHADER_TESS_EVAL
]->prog_data
;
433 prog_data
= ice
->shaders
.prog
[MESA_SHADER_VERTEX
]->prog_data
;
435 struct brw_vue_prog_data
*vue_prog_data
= (void *) prog_data
;
436 ice
->shaders
.last_vue_map
= &vue_prog_data
->vue_map
;
439 static struct brw_vue_prog_data
*
440 get_vue_prog_data(struct iris_context
*ice
, gl_shader_stage stage
)
442 if (!ice
->shaders
.prog
[stage
])
445 return (void *) ice
->shaders
.prog
[stage
]->prog_data
;
449 iris_update_compiled_shaders(struct iris_context
*ice
)
451 struct brw_vue_prog_data
*old_prog_datas
[4];
452 if (!(ice
->state
.dirty
& IRIS_DIRTY_URB
)) {
453 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++)
454 old_prog_datas
[i
] = get_vue_prog_data(ice
, i
);
457 iris_update_compiled_vs(ice
);
458 iris_update_compiled_tcs(ice
);
459 iris_update_compiled_tes(ice
);
460 iris_update_compiled_gs(ice
);
461 update_last_vue_map(ice
);
462 iris_update_compiled_fs(ice
);
465 if (!(ice
->state
.dirty
& IRIS_DIRTY_URB
)) {
466 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
467 struct brw_vue_prog_data
*old
= old_prog_datas
[i
];
468 struct brw_vue_prog_data
*new = get_vue_prog_data(ice
, i
);
469 if (!!old
!= !!new ||
470 (new && new->urb_entry_size
!= old
->urb_entry_size
)) {
471 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
479 iris_init_program_functions(struct pipe_context
*ctx
)
481 ctx
->create_vs_state
= iris_create_shader_state
;
482 ctx
->create_tcs_state
= iris_create_shader_state
;
483 ctx
->create_tes_state
= iris_create_shader_state
;
484 ctx
->create_gs_state
= iris_create_shader_state
;
485 ctx
->create_fs_state
= iris_create_shader_state
;
487 ctx
->delete_vs_state
= iris_delete_shader_state
;
488 ctx
->delete_tcs_state
= iris_delete_shader_state
;
489 ctx
->delete_tes_state
= iris_delete_shader_state
;
490 ctx
->delete_gs_state
= iris_delete_shader_state
;
491 ctx
->delete_fs_state
= iris_delete_shader_state
;
493 ctx
->bind_vs_state
= iris_bind_vs_state
;
494 ctx
->bind_tcs_state
= iris_bind_tcs_state
;
495 ctx
->bind_tes_state
= iris_bind_tes_state
;
496 ctx
->bind_gs_state
= iris_bind_gs_state
;
497 ctx
->bind_fs_state
= iris_bind_fs_state
;