2014-07-30 12:04:49 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
|
|
|
|
|
* Copyright (C) 2008 VMware, Inc. All Rights Reserved.
|
|
|
|
|
* Copyright © 2014 Intel Corporation
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-04-30 20:39:43 +10:00
|
|
|
#include "compiler/nir/nir.h"
|
|
|
|
|
#include "compiler/nir/nir_builder.h"
|
|
|
|
|
#include "gl_nir.h"
|
|
|
|
|
#include "ir_uniform.h"
|
2014-07-30 12:04:49 -07:00
|
|
|
|
|
|
|
|
#include "main/compiler.h"
|
|
|
|
|
#include "main/mtypes.h"
|
|
|
|
|
|
2015-08-26 22:18:36 +10:00
|
|
|
/* Calculate the sampler index based on array indicies and also
|
|
|
|
|
* calculate the base uniform location for struct members.
|
|
|
|
|
*/
|
|
|
|
|
static void
|
2018-03-19 10:53:45 -07:00
|
|
|
calc_sampler_offsets(nir_builder *b, nir_ssa_def *ptr,
|
|
|
|
|
const struct gl_shader_program *shader_program,
|
|
|
|
|
unsigned *base_index, nir_ssa_def **index,
|
|
|
|
|
unsigned *array_elements)
|
2014-07-30 12:04:49 -07:00
|
|
|
{
|
2018-03-19 10:53:45 -07:00
|
|
|
*base_index = 0;
|
|
|
|
|
*index = NULL;
|
|
|
|
|
*array_elements = 1;
|
|
|
|
|
unsigned location = 0;
|
|
|
|
|
|
|
|
|
|
nir_deref_instr *deref = nir_instr_as_deref(ptr->parent_instr);
|
|
|
|
|
while (deref->deref_type != nir_deref_type_var) {
|
|
|
|
|
assert(deref->parent.is_ssa);
|
|
|
|
|
nir_deref_instr *parent =
|
|
|
|
|
nir_instr_as_deref(deref->parent.ssa->parent_instr);
|
|
|
|
|
|
|
|
|
|
switch (deref->deref_type) {
|
|
|
|
|
case nir_deref_type_struct:
|
|
|
|
|
location += glsl_get_record_location_offset(parent->type,
|
|
|
|
|
deref->strct.index);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case nir_deref_type_array: {
|
|
|
|
|
nir_const_value *const_deref_index =
|
|
|
|
|
nir_src_as_const_value(deref->arr.index);
|
|
|
|
|
|
|
|
|
|
if (const_deref_index && *index == NULL) {
|
|
|
|
|
/* We're still building a direct index */
|
|
|
|
|
*base_index += const_deref_index->u32[0] * *array_elements;
|
|
|
|
|
} else {
|
|
|
|
|
if (*index == NULL) {
|
|
|
|
|
/* We used to be direct but not anymore */
|
|
|
|
|
*index = nir_imm_int(b, *base_index);
|
|
|
|
|
*base_index = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*index = nir_iadd(b, *index,
|
|
|
|
|
nir_imul(b, nir_imm_int(b, *array_elements),
|
|
|
|
|
nir_ssa_for_src(b, deref->arr.index, 1)));
|
|
|
|
|
}
|
2015-08-26 22:18:36 +10:00
|
|
|
|
2018-03-19 10:53:45 -07:00
|
|
|
*array_elements *= glsl_get_length(parent->type);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-08-26 22:18:36 +10:00
|
|
|
|
2018-03-19 10:53:45 -07:00
|
|
|
default:
|
|
|
|
|
unreachable("Invalid sampler deref type");
|
|
|
|
|
}
|
2015-08-26 22:18:36 +10:00
|
|
|
|
2018-03-19 10:53:45 -07:00
|
|
|
deref = parent;
|
|
|
|
|
}
|
2015-08-26 22:18:36 +10:00
|
|
|
|
2018-03-19 10:53:45 -07:00
|
|
|
if (*index)
|
|
|
|
|
*index = nir_umin(b, *index, nir_imm_int(b, *array_elements - 1));
|
2015-08-26 22:18:36 +10:00
|
|
|
|
2018-03-19 10:53:45 -07:00
|
|
|
/* We hit the deref_var. This is the end of the line */
|
|
|
|
|
assert(deref->deref_type == nir_deref_type_var);
|
2015-08-26 22:18:36 +10:00
|
|
|
|
2018-03-19 10:53:45 -07:00
|
|
|
location += deref->var->data.location;
|
2014-07-30 12:04:49 -07:00
|
|
|
|
2018-03-19 10:53:45 -07:00
|
|
|
gl_shader_stage stage = b->shader->info.stage;
|
|
|
|
|
assert(location < shader_program->data->NumUniformStorage &&
|
|
|
|
|
shader_program->data->UniformStorage[location].opaque[stage].active);
|
2014-07-30 12:04:49 -07:00
|
|
|
|
2018-03-19 10:53:45 -07:00
|
|
|
*base_index +=
|
|
|
|
|
shader_program->data->UniformStorage[location].opaque[stage].index;
|
2014-07-30 12:04:49 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-02 11:28:14 -08:00
|
|
|
static bool
|
2018-03-15 09:32:24 -07:00
|
|
|
lower_sampler(nir_builder *b, nir_tex_instr *instr,
|
|
|
|
|
const struct gl_shader_program *shader_program)
|
2014-07-30 12:04:49 -07:00
|
|
|
{
|
2018-03-19 10:53:45 -07:00
|
|
|
int texture_idx =
|
|
|
|
|
nir_tex_instr_src_index(instr, nir_tex_src_texture_deref);
|
|
|
|
|
int sampler_idx =
|
|
|
|
|
nir_tex_instr_src_index(instr, nir_tex_src_sampler_deref);
|
2014-12-05 16:09:53 -08:00
|
|
|
|
2018-03-19 10:53:45 -07:00
|
|
|
if (texture_idx < 0)
|
|
|
|
|
return false;
|
2015-11-02 17:58:29 -08:00
|
|
|
|
2018-03-19 10:53:45 -07:00
|
|
|
assert(texture_idx >= 0 && sampler_idx >= 0);
|
|
|
|
|
assert(instr->src[texture_idx].src.is_ssa);
|
|
|
|
|
assert(instr->src[sampler_idx].src.is_ssa);
|
|
|
|
|
assert(instr->src[texture_idx].src.ssa == instr->src[sampler_idx].src.ssa);
|
2015-08-26 22:18:36 +10:00
|
|
|
|
2016-05-18 14:56:19 -07:00
|
|
|
b->cursor = nir_before_instr(&instr->instr);
|
2015-08-26 22:18:36 +10:00
|
|
|
|
2018-03-19 10:53:45 -07:00
|
|
|
unsigned base_offset, array_elements;
|
|
|
|
|
nir_ssa_def *indirect;
|
|
|
|
|
calc_sampler_offsets(b, instr->src[texture_idx].src.ssa, shader_program,
|
|
|
|
|
&base_offset, &indirect, &array_elements);
|
2016-05-18 14:56:19 -07:00
|
|
|
|
2018-03-19 10:53:45 -07:00
|
|
|
instr->texture_index = base_offset;
|
|
|
|
|
instr->sampler_index = base_offset;
|
|
|
|
|
if (indirect) {
|
|
|
|
|
nir_instr_rewrite_src(&instr->instr, &instr->src[texture_idx].src,
|
2015-08-26 22:18:36 +10:00
|
|
|
nir_src_for_ssa(indirect));
|
2018-03-19 10:53:45 -07:00
|
|
|
instr->src[texture_idx].src_type = nir_tex_src_texture_offset;
|
|
|
|
|
nir_instr_rewrite_src(&instr->instr, &instr->src[sampler_idx].src,
|
2015-11-02 17:58:29 -08:00
|
|
|
nir_src_for_ssa(indirect));
|
2018-03-19 10:53:45 -07:00
|
|
|
instr->src[sampler_idx].src_type = nir_tex_src_sampler_offset;
|
2015-11-02 17:58:29 -08:00
|
|
|
|
2016-02-06 09:05:10 -08:00
|
|
|
instr->texture_array_size = array_elements;
|
2018-03-19 10:53:45 -07:00
|
|
|
} else {
|
|
|
|
|
nir_tex_instr_remove_src(instr, texture_idx);
|
|
|
|
|
/* The sampler index may have changed */
|
|
|
|
|
sampler_idx = nir_tex_instr_src_index(instr, nir_tex_src_sampler_deref);
|
|
|
|
|
nir_tex_instr_remove_src(instr, sampler_idx);
|
2015-08-26 22:18:36 +10:00
|
|
|
}
|
2014-12-05 16:09:53 -08:00
|
|
|
|
2017-03-02 11:28:14 -08:00
|
|
|
return true;
|
2014-07-30 12:04:49 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-02 11:28:14 -08:00
|
|
|
static bool
|
2018-03-15 09:32:24 -07:00
|
|
|
lower_impl(nir_function_impl *impl,
|
|
|
|
|
const struct gl_shader_program *shader_program)
|
2014-07-30 12:04:49 -07:00
|
|
|
{
|
2016-04-08 17:33:42 -04:00
|
|
|
nir_builder b;
|
|
|
|
|
nir_builder_init(&b, impl);
|
2017-03-02 11:28:14 -08:00
|
|
|
bool progress = false;
|
2014-12-05 16:09:53 -08:00
|
|
|
|
2016-04-08 17:33:42 -04:00
|
|
|
nir_foreach_block(block, impl) {
|
2016-04-26 18:34:19 -07:00
|
|
|
nir_foreach_instr(instr, block) {
|
2016-04-08 17:33:42 -04:00
|
|
|
if (instr->type == nir_instr_type_tex)
|
2018-03-15 09:32:24 -07:00
|
|
|
progress |= lower_sampler(&b, nir_instr_as_tex(instr),
|
|
|
|
|
shader_program);
|
2016-04-08 17:33:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
2017-03-02 11:28:14 -08:00
|
|
|
|
|
|
|
|
return progress;
|
2014-07-30 12:04:49 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-02 11:28:14 -08:00
|
|
|
bool
|
2018-04-30 20:39:43 +10:00
|
|
|
gl_nir_lower_samplers(nir_shader *shader,
|
|
|
|
|
const struct gl_shader_program *shader_program)
|
2014-07-30 12:04:49 -07:00
|
|
|
{
|
2017-03-02 11:28:14 -08:00
|
|
|
bool progress = false;
|
|
|
|
|
|
2016-04-26 20:26:42 -07:00
|
|
|
nir_foreach_function(function, shader) {
|
2015-12-26 10:00:47 -08:00
|
|
|
if (function->impl)
|
2018-03-15 09:32:24 -07:00
|
|
|
progress |= lower_impl(function->impl, shader_program);
|
2014-07-30 12:04:49 -07:00
|
|
|
}
|
2017-03-02 11:28:14 -08:00
|
|
|
|
|
|
|
|
return progress;
|
2014-07-30 12:04:49 -07:00
|
|
|
}
|