glsl/nir/linker: avoid passing gl_context inside gl_nir linker

Reviewed-by: Timothy Arceri <tarceri@itsqueeze.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14433>
This commit is contained in:
Dave Airlie 2022-01-07 11:20:14 +10:00 committed by Marge Bot
parent 7c8017bbd1
commit e943af6a55
7 changed files with 77 additions and 74 deletions

View file

@ -542,7 +542,6 @@ fill_block(struct gl_uniform_block *block,
*/
static void
link_linked_shader_uniform_blocks(void *mem_ctx,
struct gl_context *ctx,
struct gl_shader_program *prog,
struct gl_linked_shader *shader,
struct gl_uniform_block **blocks,
@ -584,8 +583,7 @@ link_linked_shader_uniform_blocks(void *mem_ctx,
}
bool
gl_nir_link_uniform_blocks(struct gl_context *ctx,
struct gl_shader_program *prog)
gl_nir_link_uniform_blocks(struct gl_shader_program *prog)
{
void *mem_ctx = ralloc_context(NULL);
bool ret = false;
@ -599,11 +597,11 @@ gl_nir_link_uniform_blocks(struct gl_context *ctx,
if (!linked)
continue;
link_linked_shader_uniform_blocks(mem_ctx, ctx, prog, linked,
link_linked_shader_uniform_blocks(mem_ctx, prog, linked,
&ubo_blocks, &num_ubo_blocks,
BLOCK_UBO);
link_linked_shader_uniform_blocks(mem_ctx, ctx, prog, linked,
link_linked_shader_uniform_blocks(mem_ctx, prog, linked,
&ssbo_blocks, &num_ssbo_blocks,
BLOCK_SSBO);

View file

@ -255,7 +255,7 @@ set_uniform_initializer(struct set_uniform_initializer_closure *data,
}
void
gl_nir_set_uniform_initializers(struct gl_context *ctx,
gl_nir_set_uniform_initializers(const struct gl_constants *consts,
struct gl_shader_program *prog)
{
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
@ -273,7 +273,7 @@ gl_nir_set_uniform_initializers(struct gl_context *ctx,
.prog = sh->Program,
.var = var,
.location = var->data.location,
.boolean_true = ctx->Const.UniformBooleanTrue
.boolean_true = consts->UniformBooleanTrue
};
set_uniform_initializer(&data,
var->type,

View file

@ -174,7 +174,7 @@ update_array_sizes(struct gl_shader_program *prog, nir_variable *var,
}
static void
nir_setup_uniform_remap_tables(struct gl_context *ctx,
nir_setup_uniform_remap_tables(const struct gl_constants *consts,
struct gl_shader_program *prog)
{
unsigned total_entries = prog->NumExplicitUniformLocations;
@ -296,10 +296,10 @@ nir_setup_uniform_remap_tables(struct gl_context *ctx,
/* Verify that total amount of entries for explicit and implicit locations
* is less than MAX_UNIFORM_LOCATIONS.
*/
if (total_entries > ctx->Const.MaxUserAssignableUniformLocations) {
if (total_entries > consts->MaxUserAssignableUniformLocations) {
linker_error(prog, "count of uniform locations > MAX_UNIFORM_LOCATIONS"
"(%u > %u)", total_entries,
ctx->Const.MaxUserAssignableUniformLocations);
consts->MaxUserAssignableUniformLocations);
}
/* Reserve all the explicit locations of the active subroutine uniforms. */
@ -635,7 +635,7 @@ struct nir_link_uniforms_state {
static void
add_parameter(struct gl_uniform_storage *uniform,
struct gl_context *ctx,
const struct gl_constants *consts,
struct gl_shader_program *prog,
const struct glsl_type *type,
struct nir_link_uniforms_state *state)
@ -662,7 +662,7 @@ add_parameter(struct gl_uniform_storage *uniform,
int base_index = params->NumParameters;
_mesa_reserve_parameter_storage(params, num_params, num_params);
if (ctx->Const.PackedDriverUniformStorage) {
if (consts->PackedDriverUniformStorage) {
for (unsigned i = 0; i < num_params; i++) {
unsigned dmul = glsl_type_is_64bit(glsl_without_array(type)) ? 2 : 1;
unsigned comps = glsl_get_vector_elements(glsl_without_array(type)) * dmul;
@ -870,7 +870,7 @@ update_uniforms_shader_info(struct gl_shader_program *prog,
}
static bool
find_and_update_named_uniform_storage(struct gl_context *ctx,
find_and_update_named_uniform_storage(const struct gl_constants *consts,
struct gl_shader_program *prog,
struct nir_link_uniforms_state *state,
nir_variable *var, char **name,
@ -918,7 +918,7 @@ find_and_update_named_uniform_storage(struct gl_context *ctx,
ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
}
result = find_and_update_named_uniform_storage(ctx, prog, state,
result = find_and_update_named_uniform_storage(consts, prog, state,
var, name, new_length,
field_type, stage,
first_element);
@ -959,7 +959,7 @@ find_and_update_named_uniform_storage(struct gl_context *ctx,
uniform->active_shader_mask |= 1 << stage;
if (!state->var_is_in_block)
add_parameter(uniform, ctx, prog, type, state);
add_parameter(uniform, consts, prog, type, state);
return true;
}
@ -983,7 +983,7 @@ find_and_update_named_uniform_storage(struct gl_context *ctx,
*
*/
static bool
find_and_update_previous_uniform_storage(struct gl_context *ctx,
find_and_update_previous_uniform_storage(const struct gl_constants *consts,
struct gl_shader_program *prog,
struct nir_link_uniforms_state *state,
nir_variable *var, char *name,
@ -993,7 +993,7 @@ find_and_update_previous_uniform_storage(struct gl_context *ctx,
if (!prog->data->spirv) {
bool first_element = true;
char *name_tmp = ralloc_strdup(NULL, name);
bool r = find_and_update_named_uniform_storage(ctx, prog, state, var,
bool r = find_and_update_named_uniform_storage(consts, prog, state, var,
&name_tmp,
strlen(name_tmp), type,
stage, &first_element);
@ -1055,7 +1055,7 @@ find_and_update_previous_uniform_storage(struct gl_context *ctx,
struct gl_uniform_storage *uniform = &prog->data->UniformStorage[i];
var->data.location = uniform - prog->data->UniformStorage;
add_parameter(uniform, ctx, prog, var->type, state);
add_parameter(uniform, consts, prog, var->type, state);
return true;
}
}
@ -1121,7 +1121,7 @@ hash_free_uniform_name(struct hash_entry *entry)
static void
enter_record(struct nir_link_uniforms_state *state,
struct gl_context *ctx,
const struct gl_constants *consts,
const struct glsl_type *type,
bool row_major)
{
@ -1129,7 +1129,7 @@ enter_record(struct nir_link_uniforms_state *state,
if (!state->var_is_in_block)
return;
bool use_std430 = ctx->Const.UseSTD430AsDefaultPacking;
bool use_std430 = consts->UseSTD430AsDefaultPacking;
const enum glsl_interface_packing packing =
glsl_get_internal_ifc_packing(state->current_var->interface_type,
use_std430);
@ -1144,7 +1144,7 @@ enter_record(struct nir_link_uniforms_state *state,
static void
leave_record(struct nir_link_uniforms_state *state,
struct gl_context *ctx,
const struct gl_constants *consts,
const struct glsl_type *type,
bool row_major)
{
@ -1152,7 +1152,7 @@ leave_record(struct nir_link_uniforms_state *state,
if (!state->var_is_in_block)
return;
bool use_std430 = ctx->Const.UseSTD430AsDefaultPacking;
bool use_std430 = consts->UseSTD430AsDefaultPacking;
const enum glsl_interface_packing packing =
glsl_get_internal_ifc_packing(state->current_var->interface_type,
use_std430);
@ -1170,7 +1170,7 @@ leave_record(struct nir_link_uniforms_state *state,
* the number of locations used or -1 on failure.
*/
static int
nir_link_uniform(struct gl_context *ctx,
nir_link_uniform(const struct gl_constants *consts,
struct gl_shader_program *prog,
struct gl_program *stage_program,
gl_shader_stage stage,
@ -1221,7 +1221,7 @@ nir_link_uniform(struct gl_context *ctx,
length = 1;
if (glsl_type_is_struct(type) && !prog->data->spirv)
enter_record(state, ctx, type, row_major);
enter_record(state, consts, type, row_major);
for (unsigned i = 0; i < length; i++) {
const struct glsl_type *field_type;
@ -1275,7 +1275,7 @@ nir_link_uniform(struct gl_context *ctx,
ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", i);
}
int entries = nir_link_uniform(ctx, prog, stage_program, stage,
int entries = nir_link_uniform(consts, prog, stage_program, stage,
field_type, i, location,
state, name, new_length,
field_row_major);
@ -1292,7 +1292,7 @@ nir_link_uniform(struct gl_context *ctx,
}
if (glsl_type_is_struct(type) && !prog->data->spirv)
leave_record(state, ctx, type, row_major);
leave_record(state, consts, type, row_major);
state->current_type = old_type;
@ -1393,7 +1393,7 @@ nir_link_uniform(struct gl_context *ctx,
}
if (!prog->data->spirv) {
bool use_std430 = ctx->Const.UseSTD430AsDefaultPacking;
bool use_std430 = consts->UseSTD430AsDefaultPacking;
const enum glsl_interface_packing packing =
glsl_get_internal_ifc_packing(state->current_var->interface_type,
use_std430);
@ -1447,7 +1447,7 @@ nir_link_uniform(struct gl_context *ctx,
}
/* Compute the next offset. */
bool use_std430 = ctx->Const.UseSTD430AsDefaultPacking;
bool use_std430 = consts->UseSTD430AsDefaultPacking;
const enum glsl_interface_packing packing =
glsl_get_internal_ifc_packing(state->current_var->interface_type,
use_std430);
@ -1486,7 +1486,7 @@ nir_link_uniform(struct gl_context *ctx,
state->max_uniform_location = uniform->remap_location + entries;
if (!state->var_is_in_block)
add_parameter(uniform, ctx, prog, type, state);
add_parameter(uniform, consts, prog, type, state);
if (name) {
_mesa_hash_table_insert(state->uniform_hash, strdup(*name),
@ -1503,7 +1503,7 @@ nir_link_uniform(struct gl_context *ctx,
}
bool
gl_nir_link_uniforms(struct gl_context *ctx,
gl_nir_link_uniforms(const struct gl_constants *consts,
struct gl_shader_program *prog,
bool fill_parameters)
{
@ -1810,7 +1810,7 @@ gl_nir_link_uniforms(struct gl_context *ctx,
* other stage. If so, validate they are compatible and update
* the active stage mask.
*/
if (find_and_update_previous_uniform_storage(ctx, prog, &state, var,
if (find_and_update_previous_uniform_storage(consts, prog, &state, var,
name, type, shader_type)) {
ralloc_free(name);
free_type_tree(type_tree);
@ -1825,7 +1825,7 @@ gl_nir_link_uniforms(struct gl_context *ctx,
bool row_major =
var->data.matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR;
int res = nir_link_uniform(ctx, prog, sh->Program, shader_type, type,
int res = nir_link_uniform(consts, prog, sh->Program, shader_type, type,
0, location,
&state,
!prog->data->spirv ? &name : NULL,
@ -1845,18 +1845,18 @@ gl_nir_link_uniforms(struct gl_context *ctx,
}
if (state.num_shader_samplers >
ctx->Const.Program[shader_type].MaxTextureImageUnits) {
consts->Program[shader_type].MaxTextureImageUnits) {
linker_error(prog, "Too many %s shader texture samplers\n",
_mesa_shader_stage_to_string(shader_type));
continue;
}
if (state.num_shader_images >
ctx->Const.Program[shader_type].MaxImageUniforms) {
consts->Program[shader_type].MaxImageUniforms) {
linker_error(prog, "Too many %s shader image uniforms (%u > %u)\n",
_mesa_shader_stage_to_string(shader_type),
state.num_shader_images,
ctx->Const.Program[shader_type].MaxImageUniforms);
consts->Program[shader_type].MaxImageUniforms);
continue;
}
@ -1878,8 +1878,8 @@ gl_nir_link_uniforms(struct gl_context *ctx,
if (prog->data->spirv)
prog->NumUniformRemapTable = state.max_uniform_location;
nir_setup_uniform_remap_tables(ctx, prog);
gl_nir_set_uniform_initializers(ctx, prog);
nir_setup_uniform_remap_tables(consts, prog);
gl_nir_set_uniform_initializers(consts, prog);
_mesa_hash_table_destroy(state.uniform_hash, hash_free_uniform_name);

View file

@ -33,7 +33,7 @@
*/
void
gl_nir_link_assign_xfb_resources(struct gl_context *ctx,
gl_nir_link_assign_xfb_resources(const struct gl_constants *consts,
struct gl_shader_program *prog)
{
/*
@ -178,7 +178,7 @@ gl_nir_link_assign_xfb_resources(struct gl_context *ctx,
* tracking the number of buffers doesn't overflow.
*/
unsigned buffers = 0;
assert(ctx->Const.MaxTransformFeedbackBuffers <= sizeof(buffers) * 8);
assert(consts->MaxTransformFeedbackBuffers <= sizeof(buffers) * 8);
for (unsigned buf = 0; buf < MAX_FEEDBACK_BUFFERS; buf++) {
if (xfb_info->buffers[buf].stride > 0) {

View file

@ -173,7 +173,7 @@ create_shader_variable(struct gl_shader_program *shProg,
}
static bool
add_shader_variable(const struct gl_context *ctx,
add_shader_variable(const struct gl_constants *consts,
struct gl_shader_program *shProg,
struct set *resource_set,
unsigned stage_mask,
@ -243,7 +243,7 @@ add_shader_variable(const struct gl_context *ctx,
glsl_get_struct_field_data(type, i);
char *field_name = ralloc_asprintf(shProg, "%s.%s", name, field->name);
if (!add_shader_variable(ctx, shProg, resource_set,
if (!add_shader_variable(consts, shProg, resource_set,
stage_mask, programInterface,
var, field_name, field_type,
use_implicit_location, field_location,
@ -279,7 +279,7 @@ add_shader_variable(const struct gl_context *ctx,
glsl_count_attribute_slots(array_type, false);
for (unsigned i = 0; i < glsl_get_length(type); i++) {
char *elem = ralloc_asprintf(shProg, "%s[%d]", name, i);
if (!add_shader_variable(ctx, shProg, resource_set,
if (!add_shader_variable(consts, shProg, resource_set,
stage_mask, programInterface,
var, elem, array_type,
use_implicit_location, elem_location,
@ -313,7 +313,7 @@ add_shader_variable(const struct gl_context *ctx,
}
static bool
add_vars_with_modes(const struct gl_context *ctx,
add_vars_with_modes(const struct gl_constants *consts,
struct gl_shader_program *prog, struct set *resource_set,
nir_shader *nir, nir_variable_mode modes,
unsigned stage, GLenum programInterface)
@ -379,7 +379,7 @@ add_vars_with_modes(const struct gl_context *ctx,
(stage == MESA_SHADER_FRAGMENT &&
var->data.mode == nir_var_shader_out);
if (!add_shader_variable(ctx, prog, resource_set,
if (!add_shader_variable(consts, prog, resource_set,
1 << stage, programInterface,
var, var->name, var->type,
vs_input_or_fs_output,
@ -394,7 +394,7 @@ add_vars_with_modes(const struct gl_context *ctx,
}
static bool
add_interface_variables(const struct gl_context *ctx,
add_interface_variables(const struct gl_constants *consts,
struct gl_shader_program *prog,
struct set *resource_set,
unsigned stage, GLenum programInterface)
@ -408,12 +408,12 @@ add_interface_variables(const struct gl_context *ctx,
switch (programInterface) {
case GL_PROGRAM_INPUT: {
return add_vars_with_modes(ctx, prog, resource_set,
return add_vars_with_modes(consts, prog, resource_set,
nir, nir_var_shader_in | nir_var_system_value,
stage, programInterface);
}
case GL_PROGRAM_OUTPUT:
return add_vars_with_modes(ctx, prog, resource_set,
return add_vars_with_modes(consts, prog, resource_set,
nir, nir_var_shader_out,
stage, programInterface);
default:
@ -429,7 +429,7 @@ add_interface_variables(const struct gl_context *ctx,
* to check if they could be refactored, and reduce code duplication somehow
*/
void
nir_build_program_resource_list(struct gl_context *ctx,
nir_build_program_resource_list(const struct gl_constants *consts,
struct gl_shader_program *prog,
bool rebuild_resourse_list)
{
@ -461,12 +461,12 @@ nir_build_program_resource_list(struct gl_context *ctx,
struct set *resource_set = _mesa_pointer_set_create(NULL);
/* Add inputs and outputs to the resource list. */
if (!add_interface_variables(ctx, prog, resource_set, input_stage,
if (!add_interface_variables(consts, prog, resource_set, input_stage,
GL_PROGRAM_INPUT)) {
return;
}
if (!add_interface_variables(ctx, prog, resource_set, output_stage,
if (!add_interface_variables(consts, prog, resource_set, output_stage,
GL_PROGRAM_OUTPUT)) {
return;
}
@ -487,7 +487,7 @@ nir_build_program_resource_list(struct gl_context *ctx,
}
/* Add buffers. */
for (unsigned i = 0; i < ctx->Const.MaxTransformFeedbackBuffers; i++) {
for (unsigned i = 0; i < consts->MaxTransformFeedbackBuffers; i++) {
if ((linked_xfb->ActiveBuffers >> i) & 1) {
linked_xfb->Buffers[i].Binding = i;
if (!link_util_add_program_resource(prog, resource_set,
@ -600,7 +600,8 @@ nir_build_program_resource_list(struct gl_context *ctx,
}
bool
gl_nir_link_spirv(struct gl_context *ctx, struct gl_shader_program *prog,
gl_nir_link_spirv(const struct gl_constants *consts,
struct gl_shader_program *prog,
const struct gl_nir_linker_options *options)
{
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
@ -615,14 +616,14 @@ gl_nir_link_spirv(struct gl_context *ctx, struct gl_shader_program *prog,
}
}
if (!gl_nir_link_uniform_blocks(ctx, prog))
if (!gl_nir_link_uniform_blocks(prog))
return false;
if (!gl_nir_link_uniforms(ctx, prog, options->fill_parameters))
if (!gl_nir_link_uniforms(consts, prog, options->fill_parameters))
return false;
gl_nir_link_assign_atomic_counter_resources(&ctx->Const, prog);
gl_nir_link_assign_xfb_resources(ctx, prog);
gl_nir_link_assign_atomic_counter_resources(consts, prog);
gl_nir_link_assign_xfb_resources(consts, prog);
return true;
}
@ -668,7 +669,9 @@ check_image_resources(const struct gl_constants *consts,
}
bool
gl_nir_link_glsl(struct gl_context *ctx, struct gl_shader_program *prog)
gl_nir_link_glsl(const struct gl_constants *consts,
const struct gl_extensions *exts,
struct gl_shader_program *prog)
{
for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
struct gl_linked_shader *shader = prog->_LinkedShaders[i];
@ -682,15 +685,15 @@ gl_nir_link_glsl(struct gl_context *ctx, struct gl_shader_program *prog)
}
}
if (!gl_nir_link_uniforms(ctx, prog, true))
if (!gl_nir_link_uniforms(consts, prog, true))
return false;
link_util_calculate_subroutine_compat(prog);
link_util_check_uniform_resources(&ctx->Const, prog);
link_util_check_uniform_resources(consts, prog);
link_util_check_subroutine_resources(prog);
check_image_resources(&ctx->Const, &ctx->Extensions, prog);
gl_nir_link_assign_atomic_counter_resources(&ctx->Const, prog);
gl_nir_link_check_atomic_counter_resources(&ctx->Const, prog);
check_image_resources(consts, exts, prog);
gl_nir_link_assign_atomic_counter_resources(consts, prog);
gl_nir_link_check_atomic_counter_resources(consts, prog);
if (prog->data->LinkStatus == LINKING_FAILURE)
return false;

View file

@ -28,8 +28,8 @@
extern "C" {
#endif
struct gl_context;
struct gl_constants;
struct gl_extensions;
struct gl_shader_program;
struct gl_nir_linker_options {
@ -42,20 +42,22 @@ struct gl_nir_linker_options {
nir_var_mem_ssbo | \
nir_var_image)
bool gl_nir_link_spirv(struct gl_context *ctx,
bool gl_nir_link_spirv(const struct gl_constants *consts,
struct gl_shader_program *prog,
const struct gl_nir_linker_options *options);
bool gl_nir_link_glsl(struct gl_context *ctx, struct gl_shader_program *prog);
bool gl_nir_link_glsl(const struct gl_constants *consts,
const struct gl_extensions *exts,
struct gl_shader_program *prog);
bool gl_nir_link_uniforms(struct gl_context *ctx,
bool gl_nir_link_uniforms(const struct gl_constants *consts,
struct gl_shader_program *prog,
bool fill_parameters);
void gl_nir_set_uniform_initializers(struct gl_context *ctx,
void gl_nir_set_uniform_initializers(const struct gl_constants *consts,
struct gl_shader_program *prog);
void nir_build_program_resource_list(struct gl_context *ctx,
void nir_build_program_resource_list(const struct gl_constants *consts,
struct gl_shader_program *prog,
bool rebuild_resourse_list);
@ -65,11 +67,10 @@ void gl_nir_link_assign_atomic_counter_resources(const struct gl_constants *cons
void gl_nir_link_check_atomic_counter_resources(const struct gl_constants *consts,
struct gl_shader_program *prog);
void gl_nir_link_assign_xfb_resources(struct gl_context *ctx,
void gl_nir_link_assign_xfb_resources(const struct gl_constants *consts,
struct gl_shader_program *prog);
bool gl_nir_link_uniform_blocks(struct gl_context *ctx,
struct gl_shader_program *prog);
bool gl_nir_link_uniform_blocks(struct gl_shader_program *prog);
#ifdef __cplusplus
} /* extern "C" */

View file

@ -796,10 +796,11 @@ st_link_nir(struct gl_context *ctx,
static const gl_nir_linker_options opts = {
true /*fill_parameters */
};
if (!gl_nir_link_spirv(ctx, shader_program, &opts))
if (!gl_nir_link_spirv(&ctx->Const, shader_program, &opts))
return GL_FALSE;
} else {
if (!gl_nir_link_glsl(ctx, shader_program))
if (!gl_nir_link_glsl(&ctx->Const, &ctx->Extensions,
shader_program))
return GL_FALSE;
}
@ -809,7 +810,7 @@ st_link_nir(struct gl_context *ctx,
_mesa_update_shader_textures_used(shader_program, prog);
}
nir_build_program_resource_list(ctx, shader_program,
nir_build_program_resource_list(&ctx->Const, shader_program,
shader_program->data->spirv);
for (unsigned i = 0; i < num_shaders; i++) {