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 * @file iris_program.c
27 * This file contains the driver interface for compiling shaders.
29 * See iris_program_cache.c for the in-memory program cache where the
30 * compiled shaders are stored.
35 #include "pipe/p_defines.h"
36 #include "pipe/p_state.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_screen.h"
39 #include "util/u_atomic.h"
40 #include "compiler/nir/nir.h"
41 #include "compiler/nir/nir_builder.h"
42 #include "intel/compiler/brw_compiler.h"
43 #include "intel/compiler/brw_nir.h"
44 #include "iris_context.h"
47 get_new_program_id(struct iris_screen
*screen
)
49 return p_atomic_inc_return(&screen
->program_id
);
53 * An uncompiled, API-facing shader. This is the Gallium CSO for shaders.
54 * It primarily contains the NIR for the shader.
56 * Each API-facing shader can be compiled into multiple shader variants,
57 * based on non-orthogonal state dependencies, recorded in the shader key.
59 * See iris_compiled_shader, which represents a compiled shader variant.
61 struct iris_uncompiled_shader
{
64 struct pipe_stream_output_info stream_output
;
68 /** Bitfield of (1 << IRIS_NOS_*) flags. */
72 // XXX: need unify_interfaces() at link time...
75 * The pipe->create_[stage]_state() driver hooks.
77 * Performs basic NIR preprocessing, records any state dependencies, and
78 * returns an iris_uncompiled_shader as the Gallium CSO.
80 * Actual shader compilation to assembly happens later, at first use.
83 iris_create_shader_state(struct pipe_context
*ctx
,
84 const struct pipe_shader_state
*state
)
86 //struct iris_context *ice = (struct iris_context *)ctx;
87 struct iris_screen
*screen
= (struct iris_screen
*)ctx
->screen
;
89 assert(state
->type
== PIPE_SHADER_IR_NIR
);
91 nir_shader
*nir
= state
->ir
.nir
;
93 struct iris_uncompiled_shader
*ish
=
94 calloc(1, sizeof(struct iris_uncompiled_shader
));
98 nir
= brw_preprocess_nir(screen
->compiler
, nir
);
100 ish
->program_id
= get_new_program_id(screen
);
102 memcpy(&ish
->stream_output
, &state
->stream_output
,
103 sizeof(struct pipe_stream_output_info
));
105 switch (nir
->info
.stage
) {
106 case MESA_SHADER_VERTEX
:
109 case MESA_SHADER_TESS_CTRL
:
112 case MESA_SHADER_TESS_EVAL
:
115 case MESA_SHADER_GEOMETRY
:
118 case MESA_SHADER_FRAGMENT
:
119 ish
->nos
|= IRIS_NOS_FRAMEBUFFER
|
120 IRIS_NOS_DEPTH_STENCIL_ALPHA
|
121 IRIS_NOS_RASTERIZER
|
124 /* The program key needs the VUE map if there are > 16 inputs */
125 if (util_bitcount64(ish
->nir
->info
.inputs_read
&
126 BRW_FS_VARYING_INPUT_MASK
) > 16) {
127 ish
->nos
|= IRIS_NOS_LAST_VUE_MAP
;
130 case MESA_SHADER_COMPUTE
:
143 * The pipe->delete_[stage]_state() driver hooks.
145 * Frees the iris_uncompiled_shader.
148 iris_delete_shader_state(struct pipe_context
*ctx
, void *state
)
150 struct iris_uncompiled_shader
*ish
= state
;
152 ralloc_free(ish
->nir
);
157 * The pipe->bind_[stage]_state() driver hook.
159 * Binds an uncompiled shader as the current one for a particular stage.
160 * Updates dirty tracking to account for the shader's NOS.
163 bind_state(struct iris_context
*ice
,
164 struct iris_uncompiled_shader
*ish
,
165 gl_shader_stage stage
)
167 uint64_t dirty_bit
= IRIS_DIRTY_UNCOMPILED_VS
<< stage
;
168 const uint64_t nos
= ish
? ish
->nos
: 0;
170 ice
->shaders
.uncompiled
[stage
] = ish
;
171 ice
->state
.dirty
|= dirty_bit
;
173 /* Record that CSOs need to mark IRIS_DIRTY_UNCOMPILED_XS when they change
174 * (or that they no longer need to do so).
176 for (int i
= 0; i
< IRIS_NOS_COUNT
; i
++) {
178 ice
->state
.dirty_for_nos
[i
] |= dirty_bit
;
180 ice
->state
.dirty_for_nos
[i
] &= ~dirty_bit
;
185 iris_bind_vs_state(struct pipe_context
*ctx
, void *state
)
187 bind_state((void *) ctx
, state
, MESA_SHADER_VERTEX
);
191 iris_bind_tcs_state(struct pipe_context
*ctx
, void *state
)
193 bind_state((void *) ctx
, state
, MESA_SHADER_TESS_CTRL
);
197 iris_bind_tes_state(struct pipe_context
*ctx
, void *state
)
199 struct iris_context
*ice
= (struct iris_context
*)ctx
;
201 /* Enabling/disabling optional stages requires a URB reconfiguration. */
202 if (!!state
!= !!ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
])
203 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
205 bind_state((void *) ctx
, state
, MESA_SHADER_TESS_EVAL
);
209 iris_bind_gs_state(struct pipe_context
*ctx
, void *state
)
211 struct iris_context
*ice
= (struct iris_context
*)ctx
;
213 /* Enabling/disabling optional stages requires a URB reconfiguration. */
214 if (!!state
!= !!ice
->shaders
.uncompiled
[MESA_SHADER_GEOMETRY
])
215 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
217 bind_state((void *) ctx
, state
, MESA_SHADER_GEOMETRY
);
221 iris_bind_fs_state(struct pipe_context
*ctx
, void *state
)
223 bind_state((void *) ctx
, state
, MESA_SHADER_FRAGMENT
);
227 * Sets up the starting offsets for the groups of binding table entries
228 * common to all pipeline stages.
230 * Unused groups are initialized to 0xd0d0d0d0 to make it obvious that they're
231 * unused but also make sure that addition of small offsets to them will
232 * trigger some of our asserts that surface indices are < BRW_MAX_SURFACES.
235 assign_common_binding_table_offsets(const struct gen_device_info
*devinfo
,
236 const struct nir_shader
*nir
,
237 struct brw_stage_prog_data
*prog_data
,
238 uint32_t next_binding_table_offset
)
240 const struct shader_info
*info
= &nir
->info
;
242 if (info
->num_textures
) {
243 prog_data
->binding_table
.texture_start
= next_binding_table_offset
;
244 prog_data
->binding_table
.gather_texture_start
= next_binding_table_offset
;
245 next_binding_table_offset
+= info
->num_textures
;
247 prog_data
->binding_table
.texture_start
= 0xd0d0d0d0;
248 prog_data
->binding_table
.gather_texture_start
= 0xd0d0d0d0;
251 int num_ubos
= info
->num_ubos
+ (nir
->num_uniforms
> 0 ? 1 : 0);
254 //assert(info->num_ubos <= BRW_MAX_UBO);
255 prog_data
->binding_table
.ubo_start
= next_binding_table_offset
;
256 next_binding_table_offset
+= num_ubos
;
258 prog_data
->binding_table
.ubo_start
= 0xd0d0d0d0;
261 if (info
->num_ssbos
|| info
->num_abos
) {
262 //assert(info->num_abos <= BRW_MAX_ABO);
263 //assert(info->num_ssbos <= BRW_MAX_SSBO);
264 prog_data
->binding_table
.ssbo_start
= next_binding_table_offset
;
265 next_binding_table_offset
+= info
->num_abos
+ info
->num_ssbos
;
267 prog_data
->binding_table
.ssbo_start
= 0xd0d0d0d0;
270 prog_data
->binding_table
.shader_time_start
= 0xd0d0d0d0;
272 if (info
->num_images
) {
273 prog_data
->binding_table
.image_start
= next_binding_table_offset
;
274 next_binding_table_offset
+= info
->num_images
;
276 prog_data
->binding_table
.image_start
= 0xd0d0d0d0;
279 /* This may or may not be used depending on how the compile goes. */
280 prog_data
->binding_table
.pull_constants_start
= next_binding_table_offset
;
281 next_binding_table_offset
++;
283 /* Plane 0 is just the regular texture section */
284 prog_data
->binding_table
.plane_start
[0] = prog_data
->binding_table
.texture_start
;
286 prog_data
->binding_table
.plane_start
[1] = next_binding_table_offset
;
287 next_binding_table_offset
+= info
->num_textures
;
289 prog_data
->binding_table
.plane_start
[2] = next_binding_table_offset
;
290 next_binding_table_offset
+= info
->num_textures
;
292 /* prog_data->base.binding_table.size will be set by brw_mark_surface_used. */
294 //assert(next_binding_table_offset <= BRW_MAX_SURFACES);
295 return next_binding_table_offset
;
299 * Associate NIR uniform variables with the prog_data->param[] mechanism
300 * used by the backend. Also, decide which UBOs we'd like to push in an
301 * ideal situation (though the backend can reduce this).
304 iris_setup_uniforms(const struct brw_compiler
*compiler
,
307 struct brw_stage_prog_data
*prog_data
)
309 prog_data
->nr_params
= nir
->num_uniforms
;
310 prog_data
->param
= rzalloc_array(mem_ctx
, uint32_t, prog_data
->nr_params
);
312 nir_foreach_variable(var
, &nir
->uniforms
) {
313 const unsigned components
= glsl_get_components(var
->type
);
315 for (unsigned i
= 0; i
< components
; i
++) {
316 prog_data
->param
[var
->data
.driver_location
] =
317 var
->data
.driver_location
;
321 // XXX: vs clip planes?
322 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
326 * If we still have regular uniforms as push constants after the backend
327 * compilation, set up a UBO range for them. This will be used to fill
328 * out the 3DSTATE_CONSTANT_* packets which cause the data to be pushed.
331 iris_setup_push_uniform_range(const struct brw_compiler
*compiler
,
332 struct brw_stage_prog_data
*prog_data
)
334 if (prog_data
->nr_params
) {
335 for (int i
= 3; i
> 0; i
--)
336 prog_data
->ubo_ranges
[i
] = prog_data
->ubo_ranges
[i
- 1];
338 prog_data
->ubo_ranges
[0] = (struct brw_ubo_range
) {
341 .length
= DIV_ROUND_UP(prog_data
->nr_params
, 8),
347 * Compile a vertex shader, and upload the assembly.
350 iris_compile_vs(struct iris_context
*ice
,
351 struct iris_uncompiled_shader
*ish
,
352 const struct brw_vs_prog_key
*key
)
354 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
355 const struct brw_compiler
*compiler
= screen
->compiler
;
356 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
357 void *mem_ctx
= ralloc_context(NULL
);
358 struct brw_vs_prog_data
*vs_prog_data
=
359 rzalloc(mem_ctx
, struct brw_vs_prog_data
);
360 struct brw_vue_prog_data
*vue_prog_data
= &vs_prog_data
->base
;
361 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
363 nir_shader
*nir
= ish
->nir
;
366 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
, 0);
368 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
);
370 brw_compute_vue_map(devinfo
,
371 &vue_prog_data
->vue_map
, nir
->info
.outputs_written
,
372 nir
->info
.separate_shader
);
374 char *error_str
= NULL
;
375 const unsigned *program
=
376 brw_compile_vs(compiler
, &ice
->dbg
, mem_ctx
, key
, vs_prog_data
,
377 nir
, -1, &error_str
);
378 if (program
== NULL
) {
379 dbg_printf("Failed to compile vertex shader: %s\n", error_str
);
380 ralloc_free(mem_ctx
);
384 iris_setup_push_uniform_range(compiler
, prog_data
);
387 ice
->vtbl
.create_so_decl_list(&ish
->stream_output
,
388 &vue_prog_data
->vue_map
);
390 iris_upload_and_bind_shader(ice
, IRIS_CACHE_VS
, key
, program
, prog_data
,
393 ralloc_free(mem_ctx
);
398 * Update the current vertex shader variant.
400 * Fill out the key, look in the cache, compile and bind if needed.
403 iris_update_compiled_vs(struct iris_context
*ice
)
405 struct iris_uncompiled_shader
*ish
=
406 ice
->shaders
.uncompiled
[MESA_SHADER_VERTEX
];
408 struct brw_vs_prog_key key
= { .program_string_id
= ish
->program_id
};
409 ice
->vtbl
.populate_vs_key(ice
, &key
);
411 if (iris_bind_cached_shader(ice
, IRIS_CACHE_VS
, &key
))
414 UNUSED
bool success
= iris_compile_vs(ice
, ish
, &key
);
418 * Get the shader_info for a given stage, or NULL if the stage is disabled.
420 const struct shader_info
*
421 iris_get_shader_info(const struct iris_context
*ice
, gl_shader_stage stage
)
423 const struct iris_uncompiled_shader
*ish
= ice
->shaders
.uncompiled
[stage
];
428 const nir_shader
*nir
= ish
->nir
;
433 * Get the union of TCS output and TES input slots.
435 * TCS and TES need to agree on a common URB entry layout. In particular,
436 * the data for all patch vertices is stored in a single URB entry (unlike
437 * GS which has one entry per input vertex). This means that per-vertex
438 * array indexing needs a stride.
440 * SSO requires locations to match, but doesn't require the number of
441 * outputs/inputs to match (in fact, the TCS often has extra outputs).
442 * So, we need to take the extra step of unifying these on the fly.
445 get_unified_tess_slots(const struct iris_context
*ice
,
446 uint64_t *per_vertex_slots
,
447 uint32_t *per_patch_slots
)
449 const struct shader_info
*tcs
=
450 iris_get_shader_info(ice
, MESA_SHADER_TESS_CTRL
);
451 const struct shader_info
*tes
=
452 iris_get_shader_info(ice
, MESA_SHADER_TESS_EVAL
);
454 *per_vertex_slots
= tes
->inputs_read
;
455 *per_patch_slots
= tes
->patch_inputs_read
;
458 *per_vertex_slots
|= tcs
->inputs_read
;
459 *per_patch_slots
|= tcs
->patch_inputs_read
;
464 * Compile a tessellation control shader, and upload the assembly.
467 iris_compile_tcs(struct iris_context
*ice
,
468 struct iris_uncompiled_shader
*ish
,
469 const struct brw_tcs_prog_key
*key
)
471 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
472 const struct brw_compiler
*compiler
= screen
->compiler
;
473 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
474 void *mem_ctx
= ralloc_context(NULL
);
475 struct brw_tcs_prog_data
*tcs_prog_data
=
476 rzalloc(mem_ctx
, struct brw_tcs_prog_data
);
477 struct brw_vue_prog_data
*vue_prog_data
= &tcs_prog_data
->base
;
478 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
480 nir_shader
*nir
= ish
->nir
;
482 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
, 0);
484 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
);
486 char *error_str
= NULL
;
487 const unsigned *program
=
488 brw_compile_tcs(compiler
, &ice
->dbg
, mem_ctx
, key
, tcs_prog_data
, nir
,
490 if (program
== NULL
) {
491 dbg_printf("Failed to compile evaluation shader: %s\n", error_str
);
492 ralloc_free(mem_ctx
);
496 iris_setup_push_uniform_range(compiler
, prog_data
);
498 iris_upload_and_bind_shader(ice
, IRIS_CACHE_TCS
, key
, program
, prog_data
,
501 ralloc_free(mem_ctx
);
506 * Update the current tessellation control shader variant.
508 * Fill out the key, look in the cache, compile and bind if needed.
511 iris_update_compiled_tcs(struct iris_context
*ice
)
513 struct iris_uncompiled_shader
*tcs
=
514 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_CTRL
];
515 struct iris_uncompiled_shader
*tes
=
516 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
];
518 assert(!(tes
&& !tcs
));
521 iris_unbind_shader(ice
, IRIS_CACHE_TCS
);
525 const struct shader_info
*tes_info
=
526 iris_get_shader_info(ice
, MESA_SHADER_TESS_EVAL
);
527 struct brw_tcs_prog_key key
= {
528 .program_string_id
= tcs
->program_id
,
529 .tes_primitive_mode
= tes_info
->tess
.primitive_mode
,
531 get_unified_tess_slots(ice
, &key
.outputs_written
,
532 &key
.patch_outputs_written
);
533 ice
->vtbl
.populate_tcs_key(ice
, &key
);
535 if (iris_bind_cached_shader(ice
, IRIS_CACHE_TCS
, &key
))
538 UNUSED
bool success
= iris_compile_tcs(ice
, tcs
, &key
);
542 * Compile a tessellation evaluation shader, and upload the assembly.
545 iris_compile_tes(struct iris_context
*ice
,
546 struct iris_uncompiled_shader
*ish
,
547 const struct brw_tes_prog_key
*key
)
549 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
550 const struct brw_compiler
*compiler
= screen
->compiler
;
551 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
552 void *mem_ctx
= ralloc_context(NULL
);
553 struct brw_tes_prog_data
*tes_prog_data
=
554 rzalloc(mem_ctx
, struct brw_tes_prog_data
);
555 struct brw_vue_prog_data
*vue_prog_data
= &tes_prog_data
->base
;
556 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
558 nir_shader
*nir
= ish
->nir
;
560 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
, 0);
562 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
);
564 struct brw_vue_map input_vue_map
;
565 brw_compute_tess_vue_map(&input_vue_map
, key
->inputs_read
,
566 key
->patch_inputs_read
);
568 char *error_str
= NULL
;
569 const unsigned *program
=
570 brw_compile_tes(compiler
, &ice
->dbg
, mem_ctx
, key
, &input_vue_map
,
571 tes_prog_data
, nir
, NULL
, -1, &error_str
);
572 if (program
== NULL
) {
573 dbg_printf("Failed to compile evaluation shader: %s\n", error_str
);
574 ralloc_free(mem_ctx
);
578 iris_setup_push_uniform_range(compiler
, prog_data
);
581 ice
->vtbl
.create_so_decl_list(&ish
->stream_output
,
582 &vue_prog_data
->vue_map
);
584 iris_upload_and_bind_shader(ice
, IRIS_CACHE_TES
, key
, program
, prog_data
,
587 ralloc_free(mem_ctx
);
592 * Update the current tessellation evaluation shader variant.
594 * Fill out the key, look in the cache, compile and bind if needed.
597 iris_update_compiled_tes(struct iris_context
*ice
)
599 struct iris_uncompiled_shader
*ish
=
600 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
];
603 iris_unbind_shader(ice
, IRIS_CACHE_TES
);
607 struct brw_tes_prog_key key
= { .program_string_id
= ish
->program_id
};
608 get_unified_tess_slots(ice
, &key
.inputs_read
, &key
.patch_inputs_read
);
609 ice
->vtbl
.populate_tes_key(ice
, &key
);
611 if (iris_bind_cached_shader(ice
, IRIS_CACHE_TES
, &key
))
614 UNUSED
bool success
= iris_compile_tes(ice
, ish
, &key
);
618 * Compile a geometry shader, and upload the assembly.
621 iris_compile_gs(struct iris_context
*ice
,
622 struct iris_uncompiled_shader
*ish
,
623 const struct brw_gs_prog_key
*key
)
625 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
626 const struct brw_compiler
*compiler
= screen
->compiler
;
627 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
628 void *mem_ctx
= ralloc_context(NULL
);
629 struct brw_gs_prog_data
*gs_prog_data
=
630 rzalloc(mem_ctx
, struct brw_gs_prog_data
);
631 struct brw_vue_prog_data
*vue_prog_data
= &gs_prog_data
->base
;
632 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
634 nir_shader
*nir
= ish
->nir
;
636 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
, 0);
638 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
);
640 brw_compute_vue_map(devinfo
,
641 &vue_prog_data
->vue_map
, nir
->info
.outputs_written
,
642 nir
->info
.separate_shader
);
644 char *error_str
= NULL
;
645 const unsigned *program
=
646 brw_compile_gs(compiler
, &ice
->dbg
, mem_ctx
, key
, gs_prog_data
, nir
,
647 NULL
, -1, &error_str
);
648 if (program
== NULL
) {
649 dbg_printf("Failed to compile geometry shader: %s\n", error_str
);
650 ralloc_free(mem_ctx
);
654 iris_setup_push_uniform_range(compiler
, prog_data
);
657 ice
->vtbl
.create_so_decl_list(&ish
->stream_output
,
658 &vue_prog_data
->vue_map
);
660 iris_upload_and_bind_shader(ice
, IRIS_CACHE_GS
, key
, program
, prog_data
,
663 ralloc_free(mem_ctx
);
668 * Update the current geometry shader variant.
670 * Fill out the key, look in the cache, compile and bind if needed.
673 iris_update_compiled_gs(struct iris_context
*ice
)
675 struct iris_uncompiled_shader
*ish
=
676 ice
->shaders
.uncompiled
[MESA_SHADER_GEOMETRY
];
679 iris_unbind_shader(ice
, IRIS_CACHE_GS
);
683 struct brw_gs_prog_key key
= { .program_string_id
= ish
->program_id
};
684 ice
->vtbl
.populate_gs_key(ice
, &key
);
686 if (iris_bind_cached_shader(ice
, IRIS_CACHE_GS
, &key
))
689 UNUSED
bool success
= iris_compile_gs(ice
, ish
, &key
);
693 * Compile a fragment (pixel) shader, and upload the assembly.
696 iris_compile_fs(struct iris_context
*ice
,
697 struct iris_uncompiled_shader
*ish
,
698 const struct brw_wm_prog_key
*key
,
699 struct brw_vue_map
*vue_map
)
701 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
702 const struct brw_compiler
*compiler
= screen
->compiler
;
703 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
704 void *mem_ctx
= ralloc_context(NULL
);
705 struct brw_wm_prog_data
*fs_prog_data
=
706 rzalloc(mem_ctx
, struct brw_wm_prog_data
);
707 struct brw_stage_prog_data
*prog_data
= &fs_prog_data
->base
;
709 nir_shader
*nir
= ish
->nir
;
712 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
,
713 MAX2(key
->nr_color_regions
, 1));
715 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
);
717 char *error_str
= NULL
;
718 const unsigned *program
=
719 brw_compile_fs(compiler
, &ice
->dbg
, mem_ctx
, key
, fs_prog_data
,
720 nir
, NULL
, -1, -1, -1, true, false, vue_map
, &error_str
);
721 if (program
== NULL
) {
722 dbg_printf("Failed to compile fragment shader: %s\n", error_str
);
723 ralloc_free(mem_ctx
);
727 //brw_alloc_stage_scratch(brw, &brw->wm.base, prog_data.base.total_scratch);
729 iris_setup_push_uniform_range(compiler
, prog_data
);
731 iris_upload_and_bind_shader(ice
, IRIS_CACHE_FS
, key
, program
, prog_data
,
734 ralloc_free(mem_ctx
);
739 * Update the current fragment shader variant.
741 * Fill out the key, look in the cache, compile and bind if needed.
744 iris_update_compiled_fs(struct iris_context
*ice
)
746 struct iris_uncompiled_shader
*ish
=
747 ice
->shaders
.uncompiled
[MESA_SHADER_FRAGMENT
];
748 struct brw_wm_prog_key key
= { .program_string_id
= ish
->program_id
};
749 ice
->vtbl
.populate_fs_key(ice
, &key
);
751 if (ish
->nos
& IRIS_NOS_LAST_VUE_MAP
)
752 key
.input_slots_valid
= ice
->shaders
.last_vue_map
->slots_valid
;
754 if (iris_bind_cached_shader(ice
, IRIS_CACHE_FS
, &key
))
757 UNUSED
bool success
=
758 iris_compile_fs(ice
, ish
, &key
, ice
->shaders
.last_vue_map
);
762 * Get the compiled shader for the last enabled geometry stage.
764 * This stage is the one which will feed stream output and the rasterizer.
766 static struct iris_compiled_shader
*
767 last_vue_shader(struct iris_context
*ice
)
769 if (ice
->shaders
.prog
[MESA_SHADER_GEOMETRY
])
770 return ice
->shaders
.prog
[MESA_SHADER_GEOMETRY
];
772 if (ice
->shaders
.prog
[MESA_SHADER_TESS_EVAL
])
773 return ice
->shaders
.prog
[MESA_SHADER_TESS_EVAL
];
775 return ice
->shaders
.prog
[MESA_SHADER_VERTEX
];
779 * Update the last enabled stage's VUE map.
781 * When the shader feeding the rasterizer's output interface changes, we
782 * need to re-emit various packets.
785 update_last_vue_map(struct iris_context
*ice
,
786 struct brw_stage_prog_data
*prog_data
)
788 struct brw_vue_prog_data
*vue_prog_data
= (void *) prog_data
;
789 struct brw_vue_map
*vue_map
= &vue_prog_data
->vue_map
;
790 struct brw_vue_map
*old_map
= ice
->shaders
.last_vue_map
;
791 const uint64_t changed_slots
=
792 (old_map
? old_map
->slots_valid
: 0ull) ^ vue_map
->slots_valid
;
794 if (changed_slots
& VARYING_BIT_VIEWPORT
) {
795 // XXX: could use ctx->Const.MaxViewports for old API efficiency
796 ice
->state
.num_viewports
=
797 (vue_map
->slots_valid
& VARYING_BIT_VIEWPORT
) ? IRIS_MAX_VIEWPORTS
: 1;
798 ice
->state
.dirty
|= IRIS_DIRTY_CLIP
|
799 IRIS_DIRTY_SF_CL_VIEWPORT
|
800 IRIS_DIRTY_SCISSOR_RECT
|
801 IRIS_DIRTY_UNCOMPILED_FS
|
802 ice
->state
.dirty_for_nos
[IRIS_NOS_LAST_VUE_MAP
];
806 if (changed_slots
|| (old_map
&& old_map
->separate
!= vue_map
->separate
)) {
807 ice
->state
.dirty
|= IRIS_DIRTY_SBE
;
810 ice
->shaders
.last_vue_map
= &vue_prog_data
->vue_map
;
814 * Get the prog_data for a given stage, or NULL if the stage is disabled.
816 static struct brw_vue_prog_data
*
817 get_vue_prog_data(struct iris_context
*ice
, gl_shader_stage stage
)
819 if (!ice
->shaders
.prog
[stage
])
822 return (void *) ice
->shaders
.prog
[stage
]->prog_data
;
826 * Update the current shader variants for the given state.
828 * This should be called on every draw call to ensure that the correct
829 * shaders are bound. It will also flag any dirty state triggered by
830 * swapping out those shaders.
833 iris_update_compiled_shaders(struct iris_context
*ice
)
835 const uint64_t dirty
= ice
->state
.dirty
;
837 struct brw_vue_prog_data
*old_prog_datas
[4];
838 if (!(dirty
& IRIS_DIRTY_URB
)) {
839 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++)
840 old_prog_datas
[i
] = get_vue_prog_data(ice
, i
);
843 if (dirty
& IRIS_DIRTY_UNCOMPILED_VS
)
844 iris_update_compiled_vs(ice
);
845 if (dirty
& IRIS_DIRTY_UNCOMPILED_TCS
)
846 iris_update_compiled_tcs(ice
);
847 if (dirty
& IRIS_DIRTY_UNCOMPILED_TES
)
848 iris_update_compiled_tes(ice
);
849 if (dirty
& IRIS_DIRTY_UNCOMPILED_GS
)
850 iris_update_compiled_gs(ice
);
852 struct iris_compiled_shader
*shader
= last_vue_shader(ice
);
853 update_last_vue_map(ice
, shader
->prog_data
);
854 if (ice
->state
.streamout
!= shader
->streamout
) {
855 ice
->state
.streamout
= shader
->streamout
;
856 ice
->state
.dirty
|= IRIS_DIRTY_SO_DECL_LIST
| IRIS_DIRTY_STREAMOUT
;
859 if (dirty
& IRIS_DIRTY_UNCOMPILED_FS
)
860 iris_update_compiled_fs(ice
);
863 /* Changing shader interfaces may require a URB configuration. */
864 if (!(dirty
& IRIS_DIRTY_URB
)) {
865 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
866 struct brw_vue_prog_data
*old
= old_prog_datas
[i
];
867 struct brw_vue_prog_data
*new = get_vue_prog_data(ice
, i
);
868 if (!!old
!= !!new ||
869 (new && new->urb_entry_size
!= old
->urb_entry_size
)) {
870 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
878 iris_init_program_functions(struct pipe_context
*ctx
)
880 ctx
->create_vs_state
= iris_create_shader_state
;
881 ctx
->create_tcs_state
= iris_create_shader_state
;
882 ctx
->create_tes_state
= iris_create_shader_state
;
883 ctx
->create_gs_state
= iris_create_shader_state
;
884 ctx
->create_fs_state
= iris_create_shader_state
;
886 ctx
->delete_vs_state
= iris_delete_shader_state
;
887 ctx
->delete_tcs_state
= iris_delete_shader_state
;
888 ctx
->delete_tes_state
= iris_delete_shader_state
;
889 ctx
->delete_gs_state
= iris_delete_shader_state
;
890 ctx
->delete_fs_state
= iris_delete_shader_state
;
892 ctx
->bind_vs_state
= iris_bind_vs_state
;
893 ctx
->bind_tcs_state
= iris_bind_tcs_state
;
894 ctx
->bind_tes_state
= iris_bind_tes_state
;
895 ctx
->bind_gs_state
= iris_bind_gs_state
;
896 ctx
->bind_fs_state
= iris_bind_fs_state
;