*/
#include "compiler/nir/nir.h"
-#include "compiler/nir/nir_builder.h"
#include "gl_nir.h"
-#include "ir_uniform.h"
-
-#include "main/compiler.h"
-#include "main/mtypes.h"
-
-/* Calculate the sampler index based on array indicies and also
- * calculate the base uniform location for struct members.
- */
-static void
-calc_sampler_offsets(nir_deref *tail, nir_tex_instr *instr,
- unsigned *array_elements, nir_ssa_def **indirect,
- nir_builder *b, unsigned *location)
-{
- if (tail->child == NULL)
- return;
-
- switch (tail->child->deref_type) {
- case nir_deref_type_array: {
- nir_deref_array *deref_array = nir_deref_as_array(tail->child);
-
- assert(deref_array->deref_array_type != nir_deref_array_type_wildcard);
-
- calc_sampler_offsets(tail->child, instr, array_elements,
- indirect, b, location);
- instr->texture_index += deref_array->base_offset * *array_elements;
-
- if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
- nir_ssa_def *mul =
- nir_imul(b, nir_imm_int(b, *array_elements),
- nir_ssa_for_src(b, deref_array->indirect, 1));
-
- nir_instr_rewrite_src(&instr->instr, &deref_array->indirect,
- NIR_SRC_INIT);
-
- if (*indirect) {
- *indirect = nir_iadd(b, *indirect, mul);
- } else {
- *indirect = mul;
- }
- }
-
- *array_elements *= glsl_get_length(tail->type);
- break;
- }
-
- case nir_deref_type_struct: {
- nir_deref_struct *deref_struct = nir_deref_as_struct(tail->child);
- *location += glsl_get_record_location_offset(tail->type, deref_struct->index);
- calc_sampler_offsets(tail->child, instr, array_elements,
- indirect, b, location);
- break;
- }
-
- default:
- unreachable("Invalid deref type");
- break;
- }
-}
-
-static bool
-lower_sampler(nir_tex_instr *instr, const struct gl_shader_program *shader_program,
- gl_shader_stage stage, nir_builder *b)
-{
- if (instr->texture == NULL)
- return false;
-
- /* In GLSL, we only fill out the texture field. The sampler is inferred */
- assert(instr->sampler == NULL || shader_program->data->spirv);
-
- instr->texture_index = 0;
- unsigned location = instr->texture->var->data.location;
- unsigned array_elements = 1;
- nir_ssa_def *indirect = NULL;
-
- b->cursor = nir_before_instr(&instr->instr);
- calc_sampler_offsets(&instr->texture->deref, instr, &array_elements,
- &indirect, b, &location);
-
- if (indirect) {
- assert(array_elements >= 1);
- indirect = nir_umin(b, indirect, nir_imm_int(b, array_elements - 1));
-
- nir_tex_instr_add_src(instr, nir_tex_src_texture_offset,
- nir_src_for_ssa(indirect));
- nir_tex_instr_add_src(instr, nir_tex_src_sampler_offset,
- nir_src_for_ssa(indirect));
-
- instr->texture_array_size = array_elements;
- }
-
- assert(location < shader_program->data->NumUniformStorage &&
- shader_program->data->UniformStorage[location].opaque[stage].active);
-
- instr->texture_index +=
- shader_program->data->UniformStorage[location].opaque[stage].index;
-
- instr->sampler_index = instr->texture_index;
-
- instr->texture = NULL;
- nir_instr_rewrite_deref(&instr->instr, &instr->sampler, NULL);
-
- return true;
-}
-
-static bool
-lower_impl(nir_function_impl *impl, const struct gl_shader_program *shader_program,
- gl_shader_stage stage)
-{
- nir_builder b;
- nir_builder_init(&b, impl);
- bool progress = false;
-
- nir_foreach_block(block, impl) {
- nir_foreach_instr(instr, block) {
- if (instr->type == nir_instr_type_tex)
- progress |= lower_sampler(nir_instr_as_tex(instr),
- shader_program, stage, &b);
- }
- }
-
- return progress;
-}
bool
gl_nir_lower_samplers(nir_shader *shader,
const struct gl_shader_program *shader_program)
{
- bool progress = false;
-
- nir_assert_lowered_derefs(shader, nir_lower_texture_derefs);
-
- nir_foreach_function(function, shader) {
- if (function->impl)
- progress |= lower_impl(function->impl, shader_program,
- shader->info.stage);
- }
+ /* First, use gl_nir_lower_samplers_as_derefs to set var->data.binding
+ * based on the uniforms, and split structures to simplify derefs.
+ */
+ gl_nir_lower_samplers_as_deref(shader, shader_program);
- return progress;
+ return nir_lower_samplers(shader);
}