#include "glsl_symbol_table.h"
#include "ir.h"
+#include "ir_builder.h"
#include "ir_optimization.h"
+#include "program/prog_instruction.h"
+
+using namespace ir_builder;
+
+namespace {
/**
* Visitor that performs varying packing. For each varying declared in the
class lower_packed_varyings_visitor
{
public:
- lower_packed_varyings_visitor(void *mem_ctx, unsigned location_base,
- unsigned locations_used,
+ lower_packed_varyings_visitor(void *mem_ctx, unsigned locations_used,
ir_variable_mode mode,
unsigned gs_input_vertices,
- exec_list *out_instructions);
+ exec_list *out_instructions,
+ exec_list *out_variables);
- void run(exec_list *instructions);
+ void run(struct gl_shader *shader);
private:
- ir_assignment *bitwise_assign_pack(ir_rvalue *lhs, ir_rvalue *rhs);
- ir_assignment *bitwise_assign_unpack(ir_rvalue *lhs, ir_rvalue *rhs);
+ void bitwise_assign_pack(ir_rvalue *lhs, ir_rvalue *rhs);
+ void bitwise_assign_unpack(ir_rvalue *lhs, ir_rvalue *rhs);
unsigned lower_rvalue(ir_rvalue *rvalue, unsigned fine_location,
ir_variable *unpacked_var, const char *name,
bool gs_input_toplevel, unsigned vertex_index);
*/
void * const mem_ctx;
- /**
- * Location representing the first generic varying slot for this shader
- * stage (e.g. VARYING_SLOT_VAR0 if we are packing vertex shader outputs).
- * Varyings whose location is less than this value are assumed to
- * correspond to special fixed function hardware, so they are not lowered.
- */
- const unsigned location_base;
-
/**
* Number of generic varying slots which are used by this shader. This is
- * used to allocate temporary intermediate data structures. If any any
- * varying used by this shader has a location greater than or equal to
- * location_base + locations_used, an assertion will fire.
+ * used to allocate temporary intermediate data structures. If any varying
+ * used by this shader has a location greater than or equal to
+ * VARYING_SLOT_VAR0 + locations_used, an assertion will fire.
*/
const unsigned locations_used;
* appropriate place in the shader once the visitor has finished running.
*/
exec_list *out_instructions;
+
+ /**
+ * Exec list into which the visitor should insert any new variables.
+ */
+ exec_list *out_variables;
};
+} /* anonymous namespace */
+
lower_packed_varyings_visitor::lower_packed_varyings_visitor(
- void *mem_ctx, unsigned location_base, unsigned locations_used,
- ir_variable_mode mode, unsigned gs_input_vertices,
- exec_list *out_instructions)
+ void *mem_ctx, unsigned locations_used, ir_variable_mode mode,
+ unsigned gs_input_vertices, exec_list *out_instructions,
+ exec_list *out_variables)
: mem_ctx(mem_ctx),
- location_base(location_base),
locations_used(locations_used),
packed_varyings((ir_variable **)
rzalloc_array_size(mem_ctx, sizeof(*packed_varyings),
locations_used)),
mode(mode),
gs_input_vertices(gs_input_vertices),
- out_instructions(out_instructions)
+ out_instructions(out_instructions),
+ out_variables(out_variables)
{
}
void
-lower_packed_varyings_visitor::run(exec_list *instructions)
+lower_packed_varyings_visitor::run(struct gl_shader *shader)
{
- foreach_list (node, instructions) {
- ir_variable *var = ((ir_instruction *) node)->as_variable();
+ foreach_in_list(ir_instruction, node, shader->ir) {
+ ir_variable *var = node->as_variable();
if (var == NULL)
continue;
- if (var->mode != this->mode ||
- var->location < (int) this->location_base ||
+ if (var->data.mode != this->mode ||
+ var->data.location < VARYING_SLOT_VAR0 ||
!this->needs_lowering(var))
continue;
* safe, caller should ensure that integral varyings always use flat
* interpolation, even when this is not required by GLSL.
*/
- assert(var->interpolation == INTERP_QUALIFIER_FLAT ||
+ assert(var->data.interpolation == INTERP_QUALIFIER_FLAT ||
!var->type->contains_integer());
+ /* Clone the variable for program resource list before
+ * it gets modified and lost.
+ */
+ if (!shader->packed_varyings)
+ shader->packed_varyings = new (shader) exec_list;
+
+ shader->packed_varyings->push_tail(var->clone(shader, NULL));
+
/* Change the old varying into an ordinary global. */
- var->mode = ir_var_auto;
+ assert(var->data.mode != ir_var_temporary);
+ var->data.mode = ir_var_auto;
/* Create a reference to the old varying. */
ir_dereference_variable *deref
= new(this->mem_ctx) ir_dereference_variable(var);
/* Recursively pack or unpack it. */
- this->lower_rvalue(deref, var->location * 4 + var->location_frac, var,
+ this->lower_rvalue(deref, var->data.location * 4 + var->data.location_frac, var,
var->name, this->gs_input_vertices != 0, 0);
}
}
+#define SWIZZLE_ZWZW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_Z, SWIZZLE_W)
/**
* Make an ir_assignment from \c rhs to \c lhs, performing appropriate
*
* This function is called when packing varyings.
*/
-ir_assignment *
+void
lower_packed_varyings_visitor::bitwise_assign_pack(ir_rvalue *lhs,
ir_rvalue *rhs)
{
rhs = new(this->mem_ctx)
ir_expression(ir_unop_bitcast_f2i, lhs->type, rhs);
break;
+ case GLSL_TYPE_DOUBLE:
+ assert(rhs->type->vector_elements <= 2);
+ if (rhs->type->vector_elements == 2) {
+ ir_variable *t = new(mem_ctx) ir_variable(lhs->type, "pack", ir_var_temporary);
+
+ assert(lhs->type->vector_elements == 4);
+ this->out_variables->push_tail(t);
+ this->out_instructions->push_tail(
+ assign(t, u2i(expr(ir_unop_unpack_double_2x32, swizzle_x(rhs->clone(mem_ctx, NULL)))), 0x3));
+ this->out_instructions->push_tail(
+ assign(t, u2i(expr(ir_unop_unpack_double_2x32, swizzle_y(rhs))), 0xc));
+ rhs = deref(t).val;
+ } else {
+ rhs = u2i(expr(ir_unop_unpack_double_2x32, rhs));
+ }
+ break;
default:
assert(!"Unexpected type conversion while lowering varyings");
break;
}
}
- return new(this->mem_ctx) ir_assignment(lhs, rhs);
+ this->out_instructions->push_tail(new (this->mem_ctx) ir_assignment(lhs, rhs));
}
*
* This function is called when unpacking varyings.
*/
-ir_assignment *
+void
lower_packed_varyings_visitor::bitwise_assign_unpack(ir_rvalue *lhs,
ir_rvalue *rhs)
{
rhs = new(this->mem_ctx)
ir_expression(ir_unop_bitcast_i2f, lhs->type, rhs);
break;
+ case GLSL_TYPE_DOUBLE:
+ assert(lhs->type->vector_elements <= 2);
+ if (lhs->type->vector_elements == 2) {
+ ir_variable *t = new(mem_ctx) ir_variable(lhs->type, "unpack", ir_var_temporary);
+ assert(rhs->type->vector_elements == 4);
+ this->out_variables->push_tail(t);
+ this->out_instructions->push_tail(
+ assign(t, expr(ir_unop_pack_double_2x32, i2u(swizzle_xy(rhs->clone(mem_ctx, NULL)))), 0x1));
+ this->out_instructions->push_tail(
+ assign(t, expr(ir_unop_pack_double_2x32, i2u(swizzle(rhs->clone(mem_ctx, NULL), SWIZZLE_ZWZW, 2))), 0x2));
+ rhs = deref(t).val;
+ } else {
+ rhs = expr(ir_unop_pack_double_2x32, i2u(rhs));
+ }
+ break;
default:
assert(!"Unexpected type conversion while lowering varyings");
break;
}
}
- return new(this->mem_ctx) ir_assignment(lhs, rhs);
+ this->out_instructions->push_tail(new(this->mem_ctx) ir_assignment(lhs, rhs));
}
bool gs_input_toplevel,
unsigned vertex_index)
{
+ unsigned dmul = rvalue->type->is_double() ? 2 : 1;
/* When gs_input_toplevel is set, we should be looking at a geometry shader
* input array.
*/
return this->lower_arraylike(rvalue, rvalue->type->matrix_columns,
fine_location, unpacked_var, name,
false, vertex_index);
- } else if (rvalue->type->vector_elements + fine_location % 4 > 4) {
+ } else if (rvalue->type->vector_elements * dmul +
+ fine_location % 4 > 4) {
/* This vector is going to be "double parked" across two varying slots,
- * so handle it as two separate assignments.
+ * so handle it as two separate assignments. For doubles, a dvec3/dvec4
+ * can end up being spread over 3 slots. However the second splitting
+ * will happen later, here we just always want to split into 2.
*/
- unsigned left_components = 4 - fine_location % 4;
- unsigned right_components
- = rvalue->type->vector_elements - left_components;
+ unsigned left_components, right_components;
unsigned left_swizzle_values[4] = { 0, 0, 0, 0 };
unsigned right_swizzle_values[4] = { 0, 0, 0, 0 };
char left_swizzle_name[4] = { 0, 0, 0, 0 };
char right_swizzle_name[4] = { 0, 0, 0, 0 };
+
+ left_components = 4 - fine_location % 4;
+ if (rvalue->type->is_double()) {
+ /* We might actually end up with 0 left components! */
+ left_components /= 2;
+ }
+ right_components = rvalue->type->vector_elements - left_components;
+
for (unsigned i = 0; i < left_components; i++) {
left_swizzle_values[i] = i;
left_swizzle_name[i] = "xyzw"[i];
= ralloc_asprintf(this->mem_ctx, "%s.%s", name, left_swizzle_name);
char *right_name
= ralloc_asprintf(this->mem_ctx, "%s.%s", name, right_swizzle_name);
- fine_location = this->lower_rvalue(left_swizzle, fine_location,
- unpacked_var, left_name, false,
- vertex_index);
+ if (left_components)
+ fine_location = this->lower_rvalue(left_swizzle, fine_location,
+ unpacked_var, left_name, false,
+ vertex_index);
+ else
+ /* Top up the fine location to the next slot */
+ fine_location++;
return this->lower_rvalue(right_swizzle, fine_location, unpacked_var,
right_name, false, vertex_index);
} else {
* varying.
*/
unsigned swizzle_values[4] = { 0, 0, 0, 0 };
- unsigned components = rvalue->type->vector_elements;
+ unsigned components = rvalue->type->vector_elements * dmul;
unsigned location = fine_location / 4;
unsigned location_frac = fine_location % 4;
for (unsigned i = 0; i < components; ++i)
ir_swizzle *swizzle = new(this->mem_ctx)
ir_swizzle(packed_deref, swizzle_values, components);
if (this->mode == ir_var_shader_out) {
- ir_assignment *assignment
- = this->bitwise_assign_pack(swizzle, rvalue);
- this->out_instructions->push_tail(assignment);
+ this->bitwise_assign_pack(swizzle, rvalue);
} else {
- ir_assignment *assignment
- = this->bitwise_assign_unpack(rvalue, swizzle);
- this->out_instructions->push_tail(assignment);
+ this->bitwise_assign_unpack(rvalue, swizzle);
}
return fine_location + components;
}
ir_constant *constant = new(this->mem_ctx) ir_constant(i);
ir_dereference_array *dereference_array = new(this->mem_ctx)
ir_dereference_array(rvalue, constant);
- char *subscripted_name
- = ralloc_asprintf(this->mem_ctx, "%s[%d]", name, i);
if (gs_input_toplevel) {
/* Geometry shader inputs are a special case. Instead of storing
* each element of the array at a different location, all elements
* are at the same location, but with a different vertex index.
*/
(void) this->lower_rvalue(dereference_array, fine_location,
- unpacked_var, subscripted_name,
- false, i);
+ unpacked_var, name, false, i);
} else {
+ char *subscripted_name
+ = ralloc_asprintf(this->mem_ctx, "%s[%d]", name, i);
fine_location =
this->lower_rvalue(dereference_array, fine_location,
unpacked_var, subscripted_name,
unsigned location, ir_variable *unpacked_var, const char *name,
unsigned vertex_index)
{
- unsigned slot = location - this->location_base;
+ unsigned slot = location - VARYING_SLOT_VAR0;
assert(slot < locations_used);
if (this->packed_varyings[slot] == NULL) {
char *packed_name = ralloc_asprintf(this->mem_ctx, "packed:%s", name);
const glsl_type *packed_type;
- if (unpacked_var->interpolation == INTERP_QUALIFIER_FLAT)
+ if (unpacked_var->data.interpolation == INTERP_QUALIFIER_FLAT)
packed_type = glsl_type::ivec4_type;
else
packed_type = glsl_type::vec4_type;
/* Prevent update_array_sizes() from messing with the size of the
* array.
*/
- packed_var->max_array_access = this->gs_input_vertices - 1;
+ packed_var->data.max_array_access = this->gs_input_vertices - 1;
}
- packed_var->centroid = unpacked_var->centroid;
- packed_var->interpolation = unpacked_var->interpolation;
- packed_var->location = location;
+ packed_var->data.centroid = unpacked_var->data.centroid;
+ packed_var->data.sample = unpacked_var->data.sample;
+ packed_var->data.patch = unpacked_var->data.patch;
+ packed_var->data.interpolation = unpacked_var->data.interpolation;
+ packed_var->data.location = location;
+ packed_var->data.precision = unpacked_var->data.precision;
+ packed_var->data.always_active_io = unpacked_var->data.always_active_io;
unpacked_var->insert_before(packed_var);
this->packed_varyings[slot] = packed_var;
} else {
bool
lower_packed_varyings_visitor::needs_lowering(ir_variable *var)
{
- /* Things composed of vec4's don't need lowering. Everything else does. */
- const glsl_type *type = var->type;
- if (this->gs_input_vertices != 0) {
- assert(type->is_array());
- type = type->element_type();
- }
- if (type->is_array())
- type = type->fields.array;
- if (type->vector_elements == 4)
+ /* Things composed of vec4's and varyings with explicitly assigned
+ * locations don't need lowering. Everything else does.
+ */
+ if (var->data.explicit_location)
+ return false;
+
+ const glsl_type *type = var->type->without_array();
+ if (type->vector_elements == 4 && !type->is_double())
return false;
return true;
}
+
+/**
+ * Visitor that splices varying packing code before every use of EmitVertex()
+ * in a geometry shader.
+ */
+class lower_packed_varyings_gs_splicer : public ir_hierarchical_visitor
+{
+public:
+ explicit lower_packed_varyings_gs_splicer(void *mem_ctx,
+ const exec_list *instructions);
+
+ virtual ir_visitor_status visit_leave(ir_emit_vertex *ev);
+
+private:
+ /**
+ * Memory context used to allocate new instructions for the shader.
+ */
+ void * const mem_ctx;
+
+ /**
+ * Instructions that should be spliced into place before each EmitVertex()
+ * call.
+ */
+ const exec_list *instructions;
+};
+
+
+lower_packed_varyings_gs_splicer::lower_packed_varyings_gs_splicer(
+ void *mem_ctx, const exec_list *instructions)
+ : mem_ctx(mem_ctx), instructions(instructions)
+{
+}
+
+
+ir_visitor_status
+lower_packed_varyings_gs_splicer::visit_leave(ir_emit_vertex *ev)
+{
+ foreach_in_list(ir_instruction, ir, this->instructions) {
+ ev->insert_before(ir->clone(this->mem_ctx, NULL));
+ }
+ return visit_continue;
+}
+
+
void
-lower_packed_varyings(void *mem_ctx, unsigned location_base,
- unsigned locations_used, ir_variable_mode mode,
- unsigned gs_input_vertices, gl_shader *shader)
+lower_packed_varyings(void *mem_ctx, unsigned locations_used,
+ ir_variable_mode mode, unsigned gs_input_vertices,
+ gl_shader *shader)
{
exec_list *instructions = shader->ir;
ir_function *main_func = shader->symbols->get_function("main");
exec_list void_parameters;
ir_function_signature *main_func_sig
- = main_func->matching_signature(&void_parameters);
- exec_list new_instructions;
- lower_packed_varyings_visitor visitor(mem_ctx, location_base,
- locations_used, mode,
- gs_input_vertices, &new_instructions);
- visitor.run(instructions);
+ = main_func->matching_signature(NULL, &void_parameters, false);
+ exec_list new_instructions, new_variables;
+ lower_packed_varyings_visitor visitor(mem_ctx, locations_used, mode,
+ gs_input_vertices,
+ &new_instructions,
+ &new_variables);
+ visitor.run(shader);
if (mode == ir_var_shader_out) {
- /* Shader outputs need to be lowered at the end of main() */
- main_func_sig->body.append_list(&new_instructions);
+ if (shader->Stage == MESA_SHADER_GEOMETRY) {
+ /* For geometry shaders, outputs need to be lowered before each call
+ * to EmitVertex()
+ */
+ lower_packed_varyings_gs_splicer splicer(mem_ctx, &new_instructions);
+
+ /* Add all the variables in first. */
+ main_func_sig->body.head->insert_before(&new_variables);
+
+ /* Now update all the EmitVertex instances */
+ splicer.run(instructions);
+ } else {
+ /* For other shader types, outputs need to be lowered at the end of
+ * main()
+ */
+ main_func_sig->body.append_list(&new_variables);
+ main_func_sig->body.append_list(&new_instructions);
+ }
} else {
/* Shader inputs need to be lowered at the beginning of main() */
main_func_sig->body.head->insert_before(&new_instructions);
+ main_func_sig->body.head->insert_before(&new_variables);
}
}