glsl: move some compiler code out of st

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22846>
This commit is contained in:
Timothy Arceri
2023-06-22 10:24:05 +10:00
committed by Marge Bot
parent ef58936876
commit a513107424
6 changed files with 85 additions and 77 deletions

View File

@@ -22,6 +22,7 @@
*/ */
#include "nir.h" #include "nir.h"
#include "nir_builder.h"
#include "gl_nir.h" #include "gl_nir.h"
#include "gl_nir_linker.h" #include "gl_nir_linker.h"
#include "linker_util.h" #include "linker_util.h"
@@ -125,6 +126,39 @@ gl_nir_opts(nir_shader *nir)
NIR_PASS_V(nir, nir_lower_var_copies); NIR_PASS_V(nir, nir_lower_var_copies);
} }
bool
gl_nir_can_add_pointsize_to_program(const struct gl_constants *consts,
struct gl_program *prog)
{
nir_shader *nir = prog->nir;
if (!nir)
return true; /* fixedfunction */
assert(nir->info.stage == MESA_SHADER_VERTEX ||
nir->info.stage == MESA_SHADER_TESS_EVAL ||
nir->info.stage == MESA_SHADER_GEOMETRY);
if (nir->info.outputs_written & VARYING_BIT_PSIZ)
return false;
unsigned max_components = nir->info.stage == MESA_SHADER_GEOMETRY ?
consts->MaxGeometryTotalOutputComponents :
consts->Program[nir->info.stage].MaxOutputComponents;
unsigned num_components = 0;
unsigned needed_components = nir->info.stage == MESA_SHADER_GEOMETRY ? nir->info.gs.vertices_out : 1;
nir_foreach_shader_out_variable(var, nir) {
num_components += glsl_count_dword_slots(var->type, false);
}
/* Ensure that there is enough attribute space to emit at least one primitive */
if (nir->info.stage == MESA_SHADER_GEOMETRY) {
if (num_components + needed_components > consts->Program[nir->info.stage].MaxOutputComponents)
return false;
num_components *= nir->info.gs.vertices_out;
}
return num_components + needed_components <= max_components;
}
static void static void
gl_nir_link_opts(nir_shader *producer, nir_shader *consumer) gl_nir_link_opts(nir_shader *producer, nir_shader *consumer)
{ {
@@ -752,6 +786,44 @@ nir_build_program_resource_list(const struct gl_constants *consts,
_mesa_set_destroy(resource_set, NULL); _mesa_set_destroy(resource_set, NULL);
} }
/* - create a gl_PointSize variable
* - find every gl_Position write
* - store 1.0 to gl_PointSize after every gl_Position write
*/
void
gl_nir_add_point_size(nir_shader *nir)
{
nir_variable *psiz = nir_create_variable_with_location(nir, nir_var_shader_out,
VARYING_SLOT_PSIZ, glsl_float_type());
psiz->data.how_declared = nir_var_hidden;
nir_function_impl *impl = nir_shader_get_entrypoint(nir);
nir_builder b = nir_builder_create(impl);
bool found = false;
nir_foreach_block_safe(block, impl) {
nir_foreach_instr_safe(instr, block) {
if (instr->type == nir_instr_type_intrinsic) {
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
if (intr->intrinsic == nir_intrinsic_store_deref ||
intr->intrinsic == nir_intrinsic_copy_deref) {
nir_variable *var = nir_intrinsic_get_var(intr, 0);
if (var->data.location == VARYING_SLOT_POS) {
b.cursor = nir_after_instr(instr);
nir_deref_instr *deref = nir_build_deref_var(&b, psiz);
nir_store_deref(&b, deref, nir_imm_float(&b, 1.0), BITFIELD_BIT(0));
found = true;
}
}
}
}
}
if (!found) {
b.cursor = nir_before_cf_list(&impl->body);
nir_deref_instr *deref = nir_build_deref_var(&b, psiz);
nir_store_deref(&b, deref, nir_imm_float(&b, 1.0), BITFIELD_BIT(0));
}
}
bool bool
gl_nir_link_spirv(const struct gl_constants *consts, gl_nir_link_spirv(const struct gl_constants *consts,
struct gl_shader_program *prog, struct gl_shader_program *prog,

View File

@@ -38,6 +38,7 @@ struct gl_constants;
struct gl_extensions; struct gl_extensions;
struct gl_linked_shader; struct gl_linked_shader;
struct gl_shader_program; struct gl_shader_program;
struct gl_program;
struct gl_transform_feedback_info; struct gl_transform_feedback_info;
struct xfb_decl; struct xfb_decl;
struct nir_xfb_info; struct nir_xfb_info;
@@ -113,6 +114,13 @@ void gl_nir_link_assign_xfb_resources(const struct gl_constants *consts,
bool gl_nir_link_uniform_blocks(struct gl_shader_program *prog); bool gl_nir_link_uniform_blocks(struct gl_shader_program *prog);
bool
gl_nir_can_add_pointsize_to_program(const struct gl_constants *consts,
struct gl_program *prog);
void
gl_nir_add_point_size(struct nir_shader *nir);
bool lower_packed_varying_needs_lowering(nir_shader *shader, nir_variable *var, bool lower_packed_varying_needs_lowering(nir_shader *shader, nir_variable *var,
bool xfb_enabled, bool xfb_enabled,
bool disable_xfb_packing, bool disable_xfb_packing,

View File

@@ -228,45 +228,6 @@ st_nir_assign_uniform_locations(struct gl_context *ctx,
} }
} }
/* - create a gl_PointSize variable
* - find every gl_Position write
* - store 1.0 to gl_PointSize after every gl_Position write
*/
void
st_nir_add_point_size(nir_shader *nir)
{
nir_variable *psiz = nir_create_variable_with_location(nir, nir_var_shader_out,
VARYING_SLOT_PSIZ, glsl_float_type());
psiz->data.how_declared = nir_var_hidden;
nir_builder b;
nir_function_impl *impl = nir_shader_get_entrypoint(nir);
b = nir_builder_create(impl);
bool found = false;
nir_foreach_block_safe(block, impl) {
nir_foreach_instr_safe(instr, block) {
if (instr->type == nir_instr_type_intrinsic) {
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
if (intr->intrinsic == nir_intrinsic_store_deref ||
intr->intrinsic == nir_intrinsic_copy_deref) {
nir_variable *var = nir_intrinsic_get_var(intr, 0);
if (var->data.location == VARYING_SLOT_POS) {
b.cursor = nir_after_instr(instr);
nir_deref_instr *deref = nir_build_deref_var(&b, psiz);
nir_store_deref(&b, deref, nir_imm_float(&b, 1.0), BITFIELD_BIT(0));
found = true;
}
}
}
}
}
if (!found) {
b.cursor = nir_before_cf_list(&impl->body);
nir_deref_instr *deref = nir_build_deref_var(&b, psiz);
nir_store_deref(&b, deref, nir_imm_float(&b, 1.0), BITFIELD_BIT(0));
}
}
static void static void
shared_type_info(const struct glsl_type *type, unsigned *size, unsigned *align) shared_type_info(const struct glsl_type *type, unsigned *size, unsigned *align)
{ {
@@ -377,8 +338,8 @@ st_nir_preprocess(struct st_context *st, struct gl_program *prog,
prog->skip_pointsize_xfb = !(nir->info.outputs_written & VARYING_BIT_PSIZ); prog->skip_pointsize_xfb = !(nir->info.outputs_written & VARYING_BIT_PSIZ);
if (st->lower_point_size && prog->skip_pointsize_xfb && if (st->lower_point_size && prog->skip_pointsize_xfb &&
stage < MESA_SHADER_FRAGMENT && stage != MESA_SHADER_TESS_CTRL && stage < MESA_SHADER_FRAGMENT && stage != MESA_SHADER_TESS_CTRL &&
st_can_add_pointsize_to_program(st, prog)) { gl_nir_can_add_pointsize_to_program(&st->ctx->Const, prog)) {
NIR_PASS_V(nir, st_nir_add_point_size); NIR_PASS_V(nir, gl_nir_add_point_size);
} }
if (stage < MESA_SHADER_FRAGMENT && stage != MESA_SHADER_TESS_CTRL && if (stage < MESA_SHADER_FRAGMENT && stage != MESA_SHADER_TESS_CTRL &&

View File

@@ -76,8 +76,6 @@ st_nir_make_passthrough_shader(struct st_context *st,
const gl_varying_slot *output_locations, const gl_varying_slot *output_locations,
unsigned *interpolation_modes, unsigned *interpolation_modes,
unsigned sysval_mask); unsigned sysval_mask);
void
st_nir_add_point_size(struct nir_shader *nir);
struct pipe_shader_state * struct pipe_shader_state *
st_nir_make_clearcolor_shader(struct st_context *st); st_nir_make_clearcolor_shader(struct st_context *st);

View File

@@ -1228,35 +1228,6 @@ st_destroy_program_variants(struct st_context *st)
destroy_shader_program_variants_cb, st); destroy_shader_program_variants_cb, st);
} }
bool
st_can_add_pointsize_to_program(struct st_context *st, struct gl_program *prog)
{
nir_shader *nir = prog->nir;
if (!nir)
return true; //fixedfunction
assert(nir->info.stage == MESA_SHADER_VERTEX ||
nir->info.stage == MESA_SHADER_TESS_EVAL ||
nir->info.stage == MESA_SHADER_GEOMETRY);
if (nir->info.outputs_written & VARYING_BIT_PSIZ)
return false;
unsigned max_components = nir->info.stage == MESA_SHADER_GEOMETRY ?
st->ctx->Const.MaxGeometryTotalOutputComponents :
st->ctx->Const.Program[nir->info.stage].MaxOutputComponents;
unsigned num_components = 0;
unsigned needed_components = nir->info.stage == MESA_SHADER_GEOMETRY ? nir->info.gs.vertices_out : 1;
nir_foreach_shader_out_variable(var, nir) {
num_components += glsl_count_dword_slots(var->type, false);
}
/* Ensure that there is enough attribute space to emit at least one primitive */
if (nir->info.stage == MESA_SHADER_GEOMETRY) {
if (num_components + needed_components > st->ctx->Const.Program[nir->info.stage].MaxOutputComponents)
return false;
num_components *= nir->info.gs.vertices_out;
}
return num_components + needed_components <= max_components;
}
/** /**
* Compile one shader variant. * Compile one shader variant.
*/ */
@@ -1396,9 +1367,10 @@ st_program_string_notify( struct gl_context *ctx,
} else if (target == GL_VERTEX_PROGRAM_ARB) { } else if (target == GL_VERTEX_PROGRAM_ARB) {
if (!st_translate_vertex_program(st, prog)) if (!st_translate_vertex_program(st, prog))
return false; return false;
if (st->lower_point_size && st_can_add_pointsize_to_program(st, prog)) { if (st->lower_point_size &&
gl_nir_can_add_pointsize_to_program(&st->ctx->Const, prog)) {
prog->skip_pointsize_xfb = true; prog->skip_pointsize_xfb = true;
NIR_PASS_V(prog->nir, st_nir_add_point_size); NIR_PASS_V(prog->nir, gl_nir_add_point_size);
} }
} }

View File

@@ -333,9 +333,6 @@ GLboolean st_program_string_notify(struct gl_context *ctx,
GLenum target, GLenum target,
struct gl_program *prog); struct gl_program *prog);
bool
st_can_add_pointsize_to_program(struct st_context *st, struct gl_program *prog);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif