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 nir_assign_var_locations(&nir
->uniforms
, &nir
->num_uniforms
,
68 type_size_scalar_bytes
);
69 nir_lower_io(nir
, nir_var_uniform
, type_size_scalar_bytes
, 0);
70 //NIR_PASS_V(nir, brw_nir_lower_uniforms, true);
72 ish
->program_id
= get_new_program_id(screen
);
73 ish
->base
.type
= PIPE_SHADER_IR_NIR
;
74 ish
->base
.ir
.nir
= nir
;
80 iris_delete_shader_state(struct pipe_context
*ctx
, void *hwcso
)
82 struct iris_uncompiled_shader
*ish
= hwcso
;
84 ralloc_free(ish
->base
.ir
.nir
);
89 iris_bind_vs_state(struct pipe_context
*ctx
, void *hwcso
)
91 struct iris_context
*ice
= (struct iris_context
*)ctx
;
93 ice
->shaders
.progs
[MESA_SHADER_VERTEX
] = hwcso
;
94 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_VS
;
98 iris_bind_tcs_state(struct pipe_context
*ctx
, void *hwcso
)
100 struct iris_context
*ice
= (struct iris_context
*)ctx
;
102 ice
->shaders
.progs
[MESA_SHADER_TESS_CTRL
] = hwcso
;
103 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_TCS
;
107 iris_bind_tes_state(struct pipe_context
*ctx
, void *hwcso
)
109 struct iris_context
*ice
= (struct iris_context
*)ctx
;
111 if (!!hwcso
!= !!ice
->shaders
.progs
[MESA_SHADER_TESS_EVAL
])
112 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
114 ice
->shaders
.progs
[MESA_SHADER_TESS_EVAL
] = hwcso
;
115 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_TES
;
119 iris_bind_gs_state(struct pipe_context
*ctx
, void *hwcso
)
121 struct iris_context
*ice
= (struct iris_context
*)ctx
;
123 if (!!hwcso
!= !!ice
->shaders
.progs
[MESA_SHADER_GEOMETRY
])
124 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
126 ice
->shaders
.progs
[MESA_SHADER_GEOMETRY
] = hwcso
;
127 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_GS
;
131 iris_bind_fs_state(struct pipe_context
*ctx
, void *hwcso
)
133 struct iris_context
*ice
= (struct iris_context
*)ctx
;
135 ice
->shaders
.progs
[MESA_SHADER_FRAGMENT
] = hwcso
;
136 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_FS
;
140 * Sets up the starting offsets for the groups of binding table entries
141 * common to all pipeline stages.
143 * Unused groups are initialized to 0xd0d0d0d0 to make it obvious that they're
144 * unused but also make sure that addition of small offsets to them will
145 * trigger some of our asserts that surface indices are < BRW_MAX_SURFACES.
148 assign_common_binding_table_offsets(const struct gen_device_info
*devinfo
,
149 const struct shader_info
*info
,
150 struct brw_stage_prog_data
*prog_data
,
151 uint32_t next_binding_table_offset
)
153 prog_data
->binding_table
.texture_start
= next_binding_table_offset
;
154 prog_data
->binding_table
.gather_texture_start
= next_binding_table_offset
;
155 next_binding_table_offset
+= info
->num_textures
;
157 if (info
->num_ubos
) {
158 //assert(info->num_ubos <= BRW_MAX_UBO);
159 prog_data
->binding_table
.ubo_start
= next_binding_table_offset
;
160 next_binding_table_offset
+= info
->num_ubos
;
162 prog_data
->binding_table
.ubo_start
= 0xd0d0d0d0;
165 if (info
->num_ssbos
|| info
->num_abos
) {
166 //assert(info->num_abos <= BRW_MAX_ABO);
167 //assert(info->num_ssbos <= BRW_MAX_SSBO);
168 prog_data
->binding_table
.ssbo_start
= next_binding_table_offset
;
169 next_binding_table_offset
+= info
->num_abos
+ info
->num_ssbos
;
171 prog_data
->binding_table
.ssbo_start
= 0xd0d0d0d0;
174 prog_data
->binding_table
.shader_time_start
= 0xd0d0d0d0;
176 if (info
->num_images
) {
177 prog_data
->binding_table
.image_start
= next_binding_table_offset
;
178 next_binding_table_offset
+= info
->num_images
;
180 prog_data
->binding_table
.image_start
= 0xd0d0d0d0;
183 /* This may or may not be used depending on how the compile goes. */
184 prog_data
->binding_table
.pull_constants_start
= next_binding_table_offset
;
185 next_binding_table_offset
++;
187 /* Plane 0 is just the regular texture section */
188 prog_data
->binding_table
.plane_start
[0] = prog_data
->binding_table
.texture_start
;
190 prog_data
->binding_table
.plane_start
[1] = next_binding_table_offset
;
191 next_binding_table_offset
+= info
->num_textures
;
193 prog_data
->binding_table
.plane_start
[2] = next_binding_table_offset
;
194 next_binding_table_offset
+= info
->num_textures
;
196 /* prog_data->base.binding_table.size will be set by brw_mark_surface_used. */
198 //assert(next_binding_table_offset <= BRW_MAX_SURFACES);
199 return next_binding_table_offset
;
203 iris_compile_vs(struct iris_context
*ice
,
204 struct iris_uncompiled_shader
*ish
,
205 const struct brw_vs_prog_key
*key
)
207 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
208 const struct brw_compiler
*compiler
= screen
->compiler
;
209 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
210 const unsigned *program
;
211 struct brw_vs_prog_data vs_prog_data
;
212 struct brw_stage_prog_data
*prog_data
= &vs_prog_data
.base
.base
;
213 void *mem_ctx
= ralloc_context(NULL
);
215 assert(ish
->base
.type
== PIPE_SHADER_IR_NIR
);
217 nir_shader
*nir
= ish
->base
.ir
.nir
;
219 memset(&vs_prog_data
, 0, sizeof(vs_prog_data
));
222 assign_common_binding_table_offsets(devinfo
, &nir
->info
, prog_data
, 0);
223 brw_compute_vue_map(devinfo
,
224 &vs_prog_data
.base
.vue_map
, nir
->info
.outputs_written
,
225 nir
->info
.separate_shader
);
227 char *error_str
= NULL
;
228 program
= brw_compile_vs(compiler
, &ice
->dbg
, mem_ctx
, key
, &vs_prog_data
,
229 nir
, -1, &error_str
);
230 if (program
== NULL
) {
231 dbg_printf("Failed to compile vertex shader: %s\n", error_str
);
233 ralloc_free(mem_ctx
);
237 /* The param and pull_param arrays will be freed by the shader cache. */
238 ralloc_steal(NULL
, prog_data
->param
);
239 ralloc_steal(NULL
, prog_data
->pull_param
);
240 iris_upload_cache(ice
, IRIS_CACHE_VS
, key
, sizeof(*key
), program
,
241 prog_data
->program_size
, prog_data
, sizeof(vs_prog_data
),
242 &ice
->shaders
.prog_offset
[MESA_SHADER_VERTEX
],
243 &ice
->shaders
.prog_data
[MESA_SHADER_VERTEX
]);
244 ralloc_free(mem_ctx
);
250 iris_populate_vs_key(struct iris_context
*ice
, struct brw_vs_prog_key
*key
)
252 memset(key
, 0, sizeof(*key
));
256 iris_update_compiled_vs(struct iris_context
*ice
)
258 struct brw_vs_prog_key key
;
259 iris_populate_vs_key(ice
, &key
);
261 if (iris_search_cache(ice
, IRIS_CACHE_VS
, &key
, sizeof(key
), IRIS_DIRTY_VS
,
262 &ice
->shaders
.prog_offset
[MESA_SHADER_VERTEX
],
263 &ice
->shaders
.prog_data
[MESA_SHADER_VERTEX
]))
266 UNUSED
bool success
=
267 iris_compile_vs(ice
, ice
->shaders
.progs
[MESA_SHADER_VERTEX
], &key
);
271 iris_update_compiled_tcs(struct iris_context
*ice
)
277 iris_update_compiled_tes(struct iris_context
*ice
)
283 iris_update_compiled_gs(struct iris_context
*ice
)
289 iris_compile_fs(struct iris_context
*ice
,
290 struct iris_uncompiled_shader
*ish
,
291 const struct brw_wm_prog_key
*key
,
292 struct brw_vue_map
*vue_map
)
294 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
295 const struct brw_compiler
*compiler
= screen
->compiler
;
296 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
297 const unsigned *program
;
298 struct brw_wm_prog_data fs_prog_data
;
299 struct brw_stage_prog_data
*prog_data
= &fs_prog_data
.base
;
300 void *mem_ctx
= ralloc_context(NULL
);
302 assert(ish
->base
.type
== PIPE_SHADER_IR_NIR
);
304 nir_shader
*nir
= ish
->base
.ir
.nir
;
306 memset(&fs_prog_data
, 0, sizeof(fs_prog_data
));
309 assign_common_binding_table_offsets(devinfo
, &nir
->info
, prog_data
,
310 MAX2(key
->nr_color_regions
, 1));
312 char *error_str
= NULL
;
313 program
= brw_compile_fs(compiler
, &ice
->dbg
, mem_ctx
, key
, &fs_prog_data
,
314 nir
, NULL
, -1, -1, -1, true, false, vue_map
,
316 if (program
== NULL
) {
317 dbg_printf("Failed to compile fragment shader: %s\n", error_str
);
319 ralloc_free(mem_ctx
);
323 //brw_alloc_stage_scratch(brw, &brw->wm.base, prog_data.base.total_scratch);
325 /* The param and pull_param arrays will be freed by the shader cache. */
326 ralloc_steal(NULL
, prog_data
->param
);
327 ralloc_steal(NULL
, prog_data
->pull_param
);
329 brw_upload_cache(&brw
->cache
, BRW_CACHE_FS_PROG
,
330 key
, sizeof(struct brw_wm_prog_key
),
331 program
, prog_data
.base
.program_size
,
332 &prog_data
, sizeof(prog_data
),
333 &brw
->wm
.base
.prog_offset
, &brw
->wm
.base
.prog_data
);
336 ralloc_free(mem_ctx
);
342 iris_populate_fs_key(struct iris_context
*ice
, struct brw_wm_prog_key
*key
)
344 memset(key
, 0, sizeof(*key
));
346 /* XXX: dirty flags? */
347 struct pipe_framebuffer_state
*fb
= &ice
->state
.framebuffer
;
348 //struct iris_depth_stencil_alpha_state *zsa = ice->state.framebuffer;
349 // XXX: can't access iris structs outside iris_state.c :(
350 // XXX: maybe just move these to iris_state.c, honestly...they're more
351 // about state than programs...
353 key
->nr_color_regions
= fb
->nr_cbufs
;
355 // key->force_dual_color_blend for unigine
357 //key->replicate_alpha = fb->nr_cbufs > 1 && alpha test or alpha to coverage
358 if (cso_rast
->multisample
) {
359 key
->persample_interp
=
360 ctx
->Multisample
.SampleShading
&&
361 (ctx
->Multisample
.MinSampleShadingValue
*
362 _mesa_geometric_samples(ctx
->DrawBuffer
) > 1);
364 key
->multisample_fbo
= fb
->samples
> 1;
368 key
->coherent_fb_fetch
= true;
372 iris_update_compiled_fs(struct iris_context
*ice
)
374 struct brw_wm_prog_key key
;
375 iris_populate_fs_key(ice
, &key
);
377 if (iris_search_cache(ice
, IRIS_CACHE_FS
, &key
, sizeof(key
), IRIS_DIRTY_FS
,
378 &ice
->shaders
.prog_offset
[MESA_SHADER_FRAGMENT
],
379 &ice
->shaders
.prog_data
[MESA_SHADER_FRAGMENT
]))
382 UNUSED
bool success
=
383 iris_compile_fs(ice
, ice
->shaders
.progs
[MESA_SHADER_FRAGMENT
], &key
,
384 ice
->shaders
.last_vue_map
);
388 update_last_vue_map(struct iris_context
*ice
)
390 struct brw_stage_prog_data
*prog_data
;
392 if (ice
->shaders
.progs
[MESA_SHADER_GEOMETRY
])
393 prog_data
= ice
->shaders
.prog_data
[MESA_SHADER_GEOMETRY
];
394 else if (ice
->shaders
.progs
[MESA_SHADER_TESS_EVAL
])
395 prog_data
= ice
->shaders
.prog_data
[MESA_SHADER_TESS_EVAL
];
397 prog_data
= ice
->shaders
.prog_data
[MESA_SHADER_VERTEX
];
399 struct brw_vue_prog_data
*vue_prog_data
= (void *) prog_data
;
400 ice
->shaders
.last_vue_map
= &vue_prog_data
->vue_map
;
404 iris_update_compiled_shaders(struct iris_context
*ice
)
406 struct brw_vue_prog_data
*old_prog_datas
[4];
407 if (!(ice
->state
.dirty
& IRIS_DIRTY_URB
)) {
408 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++)
409 old_prog_datas
[i
] = (void *) ice
->shaders
.prog_data
[i
];
412 iris_update_compiled_vs(ice
);
413 iris_update_compiled_tcs(ice
);
414 iris_update_compiled_tes(ice
);
415 iris_update_compiled_gs(ice
);
416 update_last_vue_map(ice
);
417 iris_update_compiled_fs(ice
);
420 if (!(ice
->state
.dirty
& IRIS_DIRTY_URB
)) {
421 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
422 struct brw_vue_prog_data
*old
= old_prog_datas
[i
];
423 struct brw_vue_prog_data
*new = (void *) ice
->shaders
.prog_data
[i
];
424 if (!!old
!= !!new ||
425 (new && new->urb_entry_size
!= old
->urb_entry_size
)) {
426 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
432 if (ice
->state
.dirty
& IRIS_DIRTY_URB
) {
433 // ... back to the state module :/
438 iris_init_program_functions(struct pipe_context
*ctx
)
440 ctx
->create_vs_state
= iris_create_shader_state
;
441 ctx
->create_tcs_state
= iris_create_shader_state
;
442 ctx
->create_tes_state
= iris_create_shader_state
;
443 ctx
->create_gs_state
= iris_create_shader_state
;
444 ctx
->create_fs_state
= iris_create_shader_state
;
446 ctx
->delete_vs_state
= iris_delete_shader_state
;
447 ctx
->delete_tcs_state
= iris_delete_shader_state
;
448 ctx
->delete_tes_state
= iris_delete_shader_state
;
449 ctx
->delete_gs_state
= iris_delete_shader_state
;
450 ctx
->delete_fs_state
= iris_delete_shader_state
;
452 ctx
->bind_vs_state
= iris_bind_vs_state
;
453 ctx
->bind_tcs_state
= iris_bind_tcs_state
;
454 ctx
->bind_tes_state
= iris_bind_tes_state
;
455 ctx
->bind_gs_state
= iris_bind_gs_state
;
456 ctx
->bind_fs_state
= iris_bind_fs_state
;