#include "program/prog_statevars.h"
#include "program/prog_parameter.h"
#include "program/ir_to_mesa.h"
+#include "main/context.h"
#include "main/mtypes.h"
#include "main/errors.h"
#include "main/glspirv.h"
#include "main/shaderobj.h"
#include "st_context.h"
-#include "st_glsl_types.h"
#include "st_program.h"
#include "st_shader_cache.h"
st->ctx->SoftFP64 = glsl_float64_funcs_to_nir(st->ctx, options);
}
- nir_variable_mode mask =
- (nir_variable_mode) (nir_var_shader_in | nir_var_shader_out);
- nir_remove_dead_variables(nir, mask);
+ /* ES has strict SSO validation rules for shader IO matching so we can't
+ * remove dead IO until the resource list has been built. Here we skip
+ * removing them until later. This will potentially make the IO lowering
+ * calls below do a little extra work but should otherwise have no impact.
+ */
+ if (!_mesa_is_gles(st->ctx) || !nir->info.separate_shader) {
+ nir_variable_mode mask =
+ (nir_variable_mode) (nir_var_shader_in | nir_var_shader_out);
+ nir_remove_dead_variables(nir, mask);
+ }
if (options->lower_all_io_to_temps ||
nir->info.stage == MESA_SHADER_VERTEX ||
}
/* before buffers and vars_to_ssa */
- NIR_PASS_V(nir, gl_nir_lower_bindless_images);
+ NIR_PASS_V(nir, gl_nir_lower_images, true);
/* TODO: Change GLSL to not lower shared memory. */
if (prog->nir->info.stage == MESA_SHADER_COMPUTE &&
nir_var_mem_shared, nir_address_format_32bit_offset);
}
- NIR_PASS_V(nir, gl_nir_lower_buffers, shader_program);
/* Do a round of constant folding to clean up address calculations */
NIR_PASS_V(nir, nir_opt_constant_folding);
}
(nir_var_shader_in | nir_var_shader_out | nir_var_function_temp );
nir_remove_dead_variables(nir, mask);
- NIR_PASS_V(nir, nir_lower_atomics_to_ssbo,
- st->ctx->Const.Program[nir->info.stage].MaxAtomicBuffers);
+ if (!st->has_hw_atomics)
+ NIR_PASS_V(nir, nir_lower_atomics_to_ssbo);
st_finalize_nir_before_variants(nir);
}
}
-static void
-set_st_program(struct gl_program *prog,
- struct gl_shader_program *shader_program,
- nir_shader *nir)
-{
- struct st_program *stp = (struct st_program *)prog;
-
- stp->shader_program = shader_program;
- stp->state.type = PIPE_SHADER_IR_NIR;
- stp->state.ir.nir = nir;
-}
-
static void
st_nir_vectorize_io(nir_shader *producer, nir_shader *consumer)
{
}
static void
-st_nir_link_shaders(nir_shader **producer, nir_shader **consumer)
+st_nir_link_shaders(nir_shader *producer, nir_shader *consumer)
{
- if ((*producer)->options->lower_to_scalar) {
- NIR_PASS_V(*producer, nir_lower_io_to_scalar_early, nir_var_shader_out);
- NIR_PASS_V(*consumer, nir_lower_io_to_scalar_early, nir_var_shader_in);
+ if (producer->options->lower_to_scalar) {
+ NIR_PASS_V(producer, nir_lower_io_to_scalar_early, nir_var_shader_out);
+ NIR_PASS_V(consumer, nir_lower_io_to_scalar_early, nir_var_shader_in);
}
- nir_lower_io_arrays_to_elements(*producer, *consumer);
+ nir_lower_io_arrays_to_elements(producer, consumer);
- st_nir_opts(*producer);
- st_nir_opts(*consumer);
+ st_nir_opts(producer);
+ st_nir_opts(consumer);
- if (nir_link_opt_varyings(*producer, *consumer))
- st_nir_opts(*consumer);
+ if (nir_link_opt_varyings(producer, consumer))
+ st_nir_opts(consumer);
- NIR_PASS_V(*producer, nir_remove_dead_variables, nir_var_shader_out);
- NIR_PASS_V(*consumer, nir_remove_dead_variables, nir_var_shader_in);
+ NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out);
+ NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in);
- if (nir_remove_unused_varyings(*producer, *consumer)) {
- NIR_PASS_V(*producer, nir_lower_global_vars_to_local);
- NIR_PASS_V(*consumer, nir_lower_global_vars_to_local);
+ if (nir_remove_unused_varyings(producer, consumer)) {
+ NIR_PASS_V(producer, nir_lower_global_vars_to_local);
+ NIR_PASS_V(consumer, nir_lower_global_vars_to_local);
- st_nir_opts(*producer);
- st_nir_opts(*consumer);
+ st_nir_opts(producer);
+ st_nir_opts(consumer);
/* Optimizations can cause varyings to become unused.
* nir_compact_varyings() depends on all dead varyings being removed so
* we need to call nir_remove_dead_variables() again here.
*/
- NIR_PASS_V(*producer, nir_remove_dead_variables, nir_var_shader_out);
- NIR_PASS_V(*consumer, nir_remove_dead_variables, nir_var_shader_in);
+ NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out);
+ NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in);
}
}
struct gl_shader_program *shader_program)
{
struct st_context *st = st_context(ctx);
- unsigned num_linked_shaders = 0;
+ struct gl_linked_shader *linked_shader[MESA_SHADER_STAGES];
+ unsigned num_shaders = 0;
- unsigned last_stage = 0;
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
- struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
- if (shader == NULL)
- continue;
-
- num_linked_shaders++;
+ if (shader_program->_LinkedShaders[i])
+ linked_shader[num_shaders++] = shader_program->_LinkedShaders[i];
+ }
+ for (unsigned i = 0; i < num_shaders; i++) {
+ struct gl_linked_shader *shader = linked_shader[i];
const nir_shader_compiler_options *options =
st->ctx->Const.ShaderCompilerOptions[shader->Stage].NirOptions;
struct gl_program *prog = shader->Program;
+ struct st_program *stp = (struct st_program *)prog;
+
_mesa_copy_linked_program_data(shader_program, shader);
assert(!prog->nir);
+ stp->shader_program = shader_program;
+ stp->state.type = PIPE_SHADER_IR_NIR;
- if (shader_program->data->spirv) {
- prog->Parameters = _mesa_new_parameter_list();
- /* Parameters will be filled during NIR linking. */
+ /* Parameters will be filled during NIR linking. */
+ prog->Parameters = _mesa_new_parameter_list();
+ if (shader_program->data->spirv) {
prog->nir = _mesa_spirv_to_nir(ctx, shader_program, shader->Stage, options);
- set_st_program(prog, shader_program, prog->nir);
} else {
validate_ir_tree(shader->ir);
- prog->Parameters = _mesa_new_parameter_list();
- _mesa_generate_parameters_list_for_uniforms(ctx, shader_program, shader,
- prog->Parameters);
-
if (ctx->_Shader->Flags & GLSL_DUMP) {
_mesa_log("\n");
_mesa_log("GLSL IR for linked %s program %d:\n",
_mesa_log("\n\n");
}
- prog->ExternalSamplersUsed = gl_external_samplers(prog);
- _mesa_update_shader_textures_used(shader_program, prog);
-
prog->nir = glsl_to_nir(st->ctx, shader_program, shader->Stage, options);
- set_st_program(prog, shader_program, prog->nir);
st_nir_preprocess(st, prog, shader_program, shader->Stage);
}
- last_stage = i;
-
if (options->lower_to_scalar) {
NIR_PASS_V(shader->Program->nir, nir_lower_load_const_to_scalar);
}
}
+ st_lower_patch_vertices_in(shader_program);
+
/* For SPIR-V, we have to perform the NIR linking before applying
* st_nir_preprocess.
*/
static const gl_nir_linker_options opts = {
true /*fill_parameters */
};
- if (!gl_nir_link(ctx, shader_program, &opts))
+ if (!gl_nir_link_spirv(ctx, shader_program, &opts))
return GL_FALSE;
- nir_build_program_resource_list(ctx, shader_program);
-
- for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
- struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
- if (shader == NULL)
- continue;
+ nir_build_program_resource_list(ctx, shader_program, true);
+ for (unsigned i = 0; i < num_shaders; i++) {
+ struct gl_linked_shader *shader = linked_shader[i];
struct gl_program *prog = shader->Program;
+
prog->ExternalSamplersUsed = gl_external_samplers(prog);
_mesa_update_shader_textures_used(shader_program, prog);
-
st_nir_preprocess(st, prog, shader_program, shader->Stage);
}
}
* are eliminated if they are (transitively) not used in a later
* stage.
*/
- int next = last_stage;
- for (int i = next - 1; i >= 0; i--) {
- struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
- if (shader == NULL)
- continue;
-
- st_nir_link_shaders(&shader->Program->nir,
- &shader_program->_LinkedShaders[next]->Program->nir);
- next = i;
+ for (int i = num_shaders - 2; i >= 0; i--) {
+ st_nir_link_shaders(linked_shader[i]->Program->nir,
+ linked_shader[i + 1]->Program->nir);
}
+ /* Linking shaders also optimizes them. Separate shaders, compute shaders
+ * and shaders with a fixed-func VS or FS that don't need linking are
+ * optimized here.
+ */
+ if (num_shaders == 1)
+ st_nir_opts(linked_shader[0]->Program->nir);
- int prev = -1;
- for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
- struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
- if (shader == NULL)
- continue;
+ if (!shader_program->data->spirv) {
+ if (!gl_nir_link_glsl(ctx, shader_program))
+ return GL_FALSE;
+
+ for (unsigned i = 0; i < num_shaders; i++) {
+ struct gl_program *prog = linked_shader[i]->Program;
+ prog->ExternalSamplersUsed = gl_external_samplers(prog);
+ _mesa_update_shader_textures_used(shader_program, prog);
+ }
+
+ nir_build_program_resource_list(ctx, shader_program, false);
+ }
+ for (unsigned i = 0; i < num_shaders; i++) {
+ struct gl_linked_shader *shader = linked_shader[i];
nir_shader *nir = shader->Program->nir;
- /* Linked shaders are optimized in st_nir_link_shaders. Separate shaders
- * and shaders with a fixed-func VS or FS are optimized here.
+ /* This needs to run after the initial pass of nir_lower_vars_to_ssa, so
+ * that the buffer indices are constants in nir where they where
+ * constants in GLSL. */
+ NIR_PASS_V(nir, gl_nir_lower_buffers, shader_program);
+
+ /* Remap the locations to slots so those requiring two slots will occupy
+ * two locations. For instance, if we have in the IR code a dvec3 attr0 in
+ * location 0 and vec4 attr1 in location 1, in NIR attr0 will use
+ * locations/slots 0 and 1, and attr1 will use location/slot 2
*/
- if (num_linked_shaders == 1)
- st_nir_opts(nir);
+ if (nir->info.stage == MESA_SHADER_VERTEX && !shader_program->data->spirv)
+ nir_remap_dual_slot_attributes(nir, &shader->Program->DualSlotInputs);
NIR_PASS_V(nir, st_nir_lower_wpos_ytransform, shader->Program,
st->pipe->screen);
nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
shader->Program->info = nir->info;
- if (i == MESA_SHADER_VERTEX) {
+ if (shader->Stage == MESA_SHADER_VERTEX) {
/* NIR expands dual-slot inputs out to two locations. We need to
* compact things back down GL-style single-slot inputs to avoid
* confusing the state tracker.
shader->Program->DualSlotInputs);
}
- if (prev != -1) {
- struct gl_program *prev_shader =
- shader_program->_LinkedShaders[prev]->Program;
+ if (i >= 1) {
+ struct gl_program *prev_shader = linked_shader[i - 1]->Program;
/* We can't use nir_compact_varyings with transform feedback, since
* the pipe_stream_output->output_register field is based on the
*/
if (!(prev_shader->sh.LinkedTransformFeedback &&
prev_shader->sh.LinkedTransformFeedback->NumVarying > 0))
- nir_compact_varyings(shader_program->_LinkedShaders[prev]->Program->nir,
- nir, ctx->API != API_OPENGL_COMPAT);
+ nir_compact_varyings(prev_shader->nir,
+ nir, ctx->API != API_OPENGL_COMPAT);
- if (ctx->Const.ShaderCompilerOptions[i].NirOptions->vectorize_io)
+ if (ctx->Const.ShaderCompilerOptions[shader->Stage].NirOptions->vectorize_io)
st_nir_vectorize_io(prev_shader->nir, nir);
}
- prev = i;
}
- st_lower_patch_vertices_in(shader_program);
+ struct shader_info *prev_info = NULL;
- for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
- struct gl_linked_shader *shader = shader_program->_LinkedShaders[i];
- if (shader == NULL)
- continue;
+ for (unsigned i = 0; i < num_shaders; i++) {
+ struct gl_linked_shader *shader = linked_shader[i];
+ struct shader_info *info = &shader->Program->nir->info;
+
+ if (prev_info &&
+ ctx->Const.ShaderCompilerOptions[shader->Stage].NirOptions->unify_interfaces) {
+ prev_info->outputs_written |= info->inputs_read &
+ ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
+ info->inputs_read |= prev_info->outputs_written &
+ ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
+
+ prev_info->patch_outputs_written |= info->patch_inputs_read;
+ info->patch_inputs_read |= prev_info->patch_outputs_written;
+ }
+ prev_info = info;
+ }
+ for (unsigned i = 0; i < num_shaders; i++) {
+ struct gl_linked_shader *shader = linked_shader[i];
struct gl_program *prog = shader->Program;
+ struct st_program *stp = st_program(prog);
st_glsl_to_nir_post_opts(st, prog, shader_program);
/* Initialize st_vertex_program members. */
- if (i == MESA_SHADER_VERTEX)
- st_prepare_vertex_program(st_program(prog));
+ if (shader->Stage == MESA_SHADER_VERTEX)
+ st_prepare_vertex_program(stp);
/* Get pipe_stream_output_info. */
- if (i == MESA_SHADER_VERTEX ||
- i == MESA_SHADER_TESS_EVAL ||
- i == MESA_SHADER_GEOMETRY)
+ if (shader->Stage == MESA_SHADER_VERTEX ||
+ shader->Stage == MESA_SHADER_TESS_EVAL ||
+ shader->Stage == MESA_SHADER_GEOMETRY)
st_translate_stream_output_info(prog);
st_store_ir_in_disk_cache(st, prog, true);
- if (!ctx->Driver.ProgramStringNotify(ctx,
- _mesa_shader_stage_to_program(i),
- prog)) {
- _mesa_reference_program(ctx, &shader->Program, NULL);
- return false;
- }
-
- nir_sweep(prog->nir);
+ st_release_variants(st, stp);
+ st_finalize_program(st, prog);
/* The GLSL IR won't be needed anymore. */
ralloc_free(shader->ir);
if (prog) {
prog->info.textures_used = nir->info.textures_used;
prog->info.textures_used_by_txf = nir->info.textures_used_by_txf;
+ prog->info.images_used = nir->info.images_used;
+ }
+}
+
+static int
+st_packed_uniforms_type_size(const struct glsl_type *type, bool bindless)
+{
+ return glsl_count_dword_slots(type, bindless);
+}
+
+static int
+st_unpacked_uniforms_type_size(const struct glsl_type *type, bool bindless)
+{
+ return glsl_count_vec4_slots(type, false, bindless);
+}
+
+void
+st_nir_lower_uniforms(struct st_context *st, nir_shader *nir)
+{
+ if (st->ctx->Const.PackedDriverUniformStorage) {
+ NIR_PASS_V(nir, nir_lower_io, nir_var_uniform,
+ st_packed_uniforms_type_size,
+ (nir_lower_io_options)0);
+ NIR_PASS_V(nir, nir_lower_uniforms_to_ubo, 4);
+ } else {
+ NIR_PASS_V(nir, nir_lower_io, nir_var_uniform,
+ st_unpacked_uniforms_type_size,
+ (nir_lower_io_options)0);
}
}
/* Set num_uniforms in number of attribute slots (vec4s) */
nir->num_uniforms = DIV_ROUND_UP(prog->Parameters->NumParameterValues, 4);
- if (st->ctx->Const.PackedDriverUniformStorage) {
- NIR_PASS_V(nir, nir_lower_io, nir_var_uniform, st_glsl_type_dword_size,
- (nir_lower_io_options)0);
- NIR_PASS_V(nir, nir_lower_uniforms_to_ubo, 4);
- } else {
- NIR_PASS_V(nir, nir_lower_io, nir_var_uniform, st_glsl_uniforms_type_size,
- (nir_lower_io_options)0);
- }
-
+ st_nir_lower_uniforms(st, nir);
st_nir_lower_samplers(screen, nir, shader_program, prog);
+ if (!screen->get_param(screen, PIPE_CAP_NIR_IMAGES_AS_DEREF))
+ NIR_PASS_V(nir, gl_nir_lower_images, false);
if (finalize_by_driver && screen->finalize_nir)
screen->finalize_nir(screen, nir, false);