2010-08-06 13:07:25 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2010 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \file ir_set_program_inouts.cpp
|
|
|
|
|
*
|
|
|
|
|
* Sets the InputsRead and OutputsWritten of Mesa programs.
|
|
|
|
|
*
|
2012-06-20 12:49:29 -07:00
|
|
|
* Additionally, for fragment shaders, sets the InterpQualifier array, the
|
|
|
|
|
* IsCentroid bitfield, and the UsesDFdy flag.
|
2011-10-25 18:06:37 -07:00
|
|
|
*
|
2010-08-06 13:07:25 -07:00
|
|
|
* Mesa programs (gl_program, not gl_shader_program) have a set of
|
|
|
|
|
* flags indicating which varyings are read and written. Computing
|
|
|
|
|
* which are actually read from some sort of backend code can be
|
|
|
|
|
* tricky when variable array indexing involved. So this pass
|
|
|
|
|
* provides support for setting InputsRead and OutputsWritten right
|
|
|
|
|
* from the GLSL IR.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-08-23 17:51:42 +08:00
|
|
|
#include "main/core.h" /* for struct gl_program */
|
2010-08-06 13:07:25 -07:00
|
|
|
#include "ir.h"
|
|
|
|
|
#include "ir_visitor.h"
|
|
|
|
|
#include "glsl_types.h"
|
|
|
|
|
|
|
|
|
|
class ir_set_program_inouts_visitor : public ir_hierarchical_visitor {
|
|
|
|
|
public:
|
2013-07-30 20:49:56 -07:00
|
|
|
ir_set_program_inouts_visitor(struct gl_program *prog, GLenum shader_type)
|
2010-08-06 13:07:25 -07:00
|
|
|
{
|
|
|
|
|
this->prog = prog;
|
2013-07-30 20:49:56 -07:00
|
|
|
this->shader_type = shader_type;
|
2010-08-06 13:07:25 -07:00
|
|
|
}
|
2010-08-18 15:59:31 -07:00
|
|
|
~ir_set_program_inouts_visitor()
|
2010-08-06 13:07:25 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ir_visitor_status visit_enter(ir_dereference_array *);
|
|
|
|
|
virtual ir_visitor_status visit_enter(ir_function_signature *);
|
2012-06-20 12:49:29 -07:00
|
|
|
virtual ir_visitor_status visit_enter(ir_expression *);
|
2012-07-18 23:18:14 -07:00
|
|
|
virtual ir_visitor_status visit_enter(ir_discard *);
|
2010-08-06 13:07:25 -07:00
|
|
|
virtual ir_visitor_status visit(ir_dereference_variable *);
|
|
|
|
|
|
2013-07-31 10:15:49 -07:00
|
|
|
private:
|
|
|
|
|
void mark_whole_variable(ir_variable *var);
|
|
|
|
|
bool try_mark_partial_variable(ir_variable *var, ir_rvalue *index);
|
|
|
|
|
|
2010-08-06 13:07:25 -07:00
|
|
|
struct gl_program *prog;
|
2013-07-30 20:49:56 -07:00
|
|
|
GLenum shader_type;
|
2010-08-06 13:07:25 -07:00
|
|
|
};
|
|
|
|
|
|
2013-01-30 22:11:17 -08:00
|
|
|
static inline bool
|
|
|
|
|
is_shader_inout(ir_variable *var)
|
|
|
|
|
{
|
|
|
|
|
return var->mode == ir_var_shader_in ||
|
|
|
|
|
var->mode == ir_var_shader_out ||
|
|
|
|
|
var->mode == ir_var_system_value;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-06 13:07:25 -07:00
|
|
|
static void
|
2011-10-25 18:06:37 -07:00
|
|
|
mark(struct gl_program *prog, ir_variable *var, int offset, int len,
|
|
|
|
|
bool is_fragment_shader)
|
2010-08-06 13:07:25 -07:00
|
|
|
{
|
|
|
|
|
/* As of GLSL 1.20, varyings can only be floats, floating-point
|
|
|
|
|
* vectors or matrices, or arrays of them. For Mesa programs using
|
|
|
|
|
* InputsRead/OutputsWritten, everything but matrices uses one
|
|
|
|
|
* slot, while matrices use a slot per column. Presumably
|
|
|
|
|
* something doing a more clever packing would use something other
|
|
|
|
|
* than InputsRead/OutputsWritten.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-12-09 14:31:43 -08:00
|
|
|
for (int i = 0; i < len; i++) {
|
glsl: add support for ARB_blend_func_extended (v3)
This adds index support to the GLSL compiler.
I'm not 100% sure of my approach here, esp without how output ordering
happens wrt location, index pairs, in the "mark" function.
Since current hw doesn't ever have a location > 0 with an index > 0,
we don't have to work out if the output ordering the hw requires is
location, index, location, index or location, location, index, index.
But we have no hw to know, so punt on it for now.
v2: index requires layout - catch and error
setup explicit index properly.
v3: drop idx_offset stuff, assume index follow location
Signed-off-by: Dave Airlie <airlied@redhat.com>
2012-03-24 13:33:41 +00:00
|
|
|
GLbitfield64 bitfield = BITFIELD64_BIT(var->location + var->index + offset + i);
|
2013-01-11 14:39:32 -08:00
|
|
|
if (var->mode == ir_var_shader_in) {
|
2011-10-25 18:06:37 -07:00
|
|
|
prog->InputsRead |= bitfield;
|
|
|
|
|
if (is_fragment_shader) {
|
|
|
|
|
gl_fragment_program *fprog = (gl_fragment_program *) prog;
|
glsl: add support for ARB_blend_func_extended (v3)
This adds index support to the GLSL compiler.
I'm not 100% sure of my approach here, esp without how output ordering
happens wrt location, index pairs, in the "mark" function.
Since current hw doesn't ever have a location > 0 with an index > 0,
we don't have to work out if the output ordering the hw requires is
location, index, location, index or location, location, index, index.
But we have no hw to know, so punt on it for now.
v2: index requires layout - catch and error
setup explicit index properly.
v3: drop idx_offset stuff, assume index follow location
Signed-off-by: Dave Airlie <airlied@redhat.com>
2012-03-24 13:33:41 +00:00
|
|
|
fprog->InterpQualifier[var->location + var->index + offset + i] =
|
2011-10-25 18:06:37 -07:00
|
|
|
(glsl_interp_qualifier) var->interpolation;
|
2012-06-18 12:10:13 -07:00
|
|
|
if (var->centroid)
|
|
|
|
|
fprog->IsCentroid |= bitfield;
|
2011-10-25 18:06:37 -07:00
|
|
|
}
|
|
|
|
|
} else if (var->mode == ir_var_system_value) {
|
|
|
|
|
prog->SystemValuesRead |= bitfield;
|
|
|
|
|
} else {
|
2013-01-30 22:11:17 -08:00
|
|
|
assert(var->mode == ir_var_shader_out);
|
2011-10-25 18:06:37 -07:00
|
|
|
prog->OutputsWritten |= bitfield;
|
|
|
|
|
}
|
2010-08-06 13:07:25 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-31 10:15:49 -07:00
|
|
|
/**
|
|
|
|
|
* Mark an entire variable as used. Caller must ensure that the variable
|
|
|
|
|
* represents a shader input or output.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
ir_set_program_inouts_visitor::mark_whole_variable(ir_variable *var)
|
|
|
|
|
{
|
|
|
|
|
mark(this->prog, var, 0, var->type->count_attribute_slots(),
|
|
|
|
|
this->shader_type == GL_FRAGMENT_SHADER);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-06 13:07:25 -07:00
|
|
|
/* Default handler: Mark all the locations in the variable as used. */
|
|
|
|
|
ir_visitor_status
|
|
|
|
|
ir_set_program_inouts_visitor::visit(ir_dereference_variable *ir)
|
|
|
|
|
{
|
2013-01-30 22:11:17 -08:00
|
|
|
if (!is_shader_inout(ir->var))
|
2010-08-06 13:07:25 -07:00
|
|
|
return visit_continue;
|
|
|
|
|
|
2013-07-31 10:15:49 -07:00
|
|
|
mark_whole_variable(ir->var);
|
2010-08-06 13:07:25 -07:00
|
|
|
|
|
|
|
|
return visit_continue;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-31 10:15:49 -07:00
|
|
|
/**
|
|
|
|
|
* Try to mark a portion of the given variable as used. Caller must ensure
|
|
|
|
|
* that the variable represents a shader input or output which can be indexed
|
|
|
|
|
* into in array fashion (an array or matrix).
|
|
|
|
|
*
|
|
|
|
|
* If the index can't be interpreted as a constant, or some other problem
|
|
|
|
|
* occurs, then nothing will be marked and false will be returned.
|
|
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
ir_set_program_inouts_visitor::try_mark_partial_variable(ir_variable *var,
|
|
|
|
|
ir_rvalue *index)
|
|
|
|
|
{
|
|
|
|
|
const glsl_type *type = var->type;
|
|
|
|
|
|
2013-07-31 10:57:22 -07:00
|
|
|
/* The code below only handles:
|
|
|
|
|
*
|
|
|
|
|
* - Indexing into matrices
|
|
|
|
|
* - Indexing into arrays of (matrices, vectors, or scalars)
|
|
|
|
|
*
|
|
|
|
|
* All other possibilities are either prohibited by GLSL (vertex inputs and
|
|
|
|
|
* fragment outputs can't be structs) or should have been eliminated by
|
|
|
|
|
* lowering passes (do_vec_index_to_swizzle() gets rid of indexing into
|
|
|
|
|
* vectors, and lower_packed_varyings() gets rid of structs that occur in
|
|
|
|
|
* varyings).
|
|
|
|
|
*/
|
|
|
|
|
if (!(type->is_matrix() ||
|
|
|
|
|
(type->is_array() &&
|
|
|
|
|
(type->fields.array->is_numeric() ||
|
|
|
|
|
type->fields.array->is_boolean())))) {
|
|
|
|
|
assert(!"Unexpected indexing in ir_set_program_inouts");
|
|
|
|
|
|
|
|
|
|
/* For safety in release builds, in case we ever encounter unexpected
|
|
|
|
|
* indexing, give up and let the caller mark the whole variable as used.
|
|
|
|
|
*/
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-31 10:15:49 -07:00
|
|
|
ir_constant *index_as_constant = index->as_constant();
|
|
|
|
|
if (!index_as_constant)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
int width = 1;
|
|
|
|
|
if (type->is_array() && type->fields.array->is_matrix()) {
|
|
|
|
|
width = type->fields.array->matrix_columns;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mark(this->prog, var, index_as_constant->value.u[0] * width, width,
|
|
|
|
|
this->shader_type == GL_FRAGMENT_SHADER);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-06 13:07:25 -07:00
|
|
|
ir_visitor_status
|
|
|
|
|
ir_set_program_inouts_visitor::visit_enter(ir_dereference_array *ir)
|
|
|
|
|
{
|
|
|
|
|
ir_dereference_variable *deref_var;
|
|
|
|
|
deref_var = ir->array->as_dereference_variable();
|
2013-01-30 22:11:17 -08:00
|
|
|
ir_variable *var = deref_var ? deref_var->var : NULL;
|
2010-08-06 13:07:25 -07:00
|
|
|
|
|
|
|
|
/* Check that we're dereferencing a shader in or out */
|
2013-01-30 22:11:17 -08:00
|
|
|
if (!var || !is_shader_inout(var))
|
|
|
|
|
return visit_continue;
|
2010-08-06 13:07:25 -07:00
|
|
|
|
2013-07-31 10:15:49 -07:00
|
|
|
if (try_mark_partial_variable(var, ir->array_index))
|
2010-08-06 13:07:25 -07:00
|
|
|
return visit_continue_with_parent;
|
|
|
|
|
|
|
|
|
|
return visit_continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ir_visitor_status
|
|
|
|
|
ir_set_program_inouts_visitor::visit_enter(ir_function_signature *ir)
|
|
|
|
|
{
|
|
|
|
|
/* We don't want to descend into the function parameters and
|
|
|
|
|
* consider them as shader inputs or outputs.
|
|
|
|
|
*/
|
|
|
|
|
visit_list_elements(this, &ir->body);
|
|
|
|
|
return visit_continue_with_parent;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-20 12:49:29 -07:00
|
|
|
ir_visitor_status
|
|
|
|
|
ir_set_program_inouts_visitor::visit_enter(ir_expression *ir)
|
|
|
|
|
{
|
2013-07-30 20:49:56 -07:00
|
|
|
if (this->shader_type == GL_FRAGMENT_SHADER &&
|
|
|
|
|
ir->operation == ir_unop_dFdy) {
|
2012-06-20 12:49:29 -07:00
|
|
|
gl_fragment_program *fprog = (gl_fragment_program *) prog;
|
|
|
|
|
fprog->UsesDFdy = true;
|
|
|
|
|
}
|
|
|
|
|
return visit_continue;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-18 23:18:14 -07:00
|
|
|
ir_visitor_status
|
|
|
|
|
ir_set_program_inouts_visitor::visit_enter(ir_discard *)
|
|
|
|
|
{
|
|
|
|
|
/* discards are only allowed in fragment shaders. */
|
2013-07-30 20:49:56 -07:00
|
|
|
assert(this->shader_type == GL_FRAGMENT_SHADER);
|
2012-07-18 23:18:14 -07:00
|
|
|
|
|
|
|
|
gl_fragment_program *fprog = (gl_fragment_program *) prog;
|
|
|
|
|
fprog->UsesKill = true;
|
|
|
|
|
|
|
|
|
|
return visit_continue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-06 13:07:25 -07:00
|
|
|
void
|
2011-10-25 18:06:37 -07:00
|
|
|
do_set_program_inouts(exec_list *instructions, struct gl_program *prog,
|
2013-07-30 20:49:56 -07:00
|
|
|
GLenum shader_type)
|
2010-08-06 13:07:25 -07:00
|
|
|
{
|
2013-07-30 20:49:56 -07:00
|
|
|
ir_set_program_inouts_visitor v(prog, shader_type);
|
2010-08-06 13:07:25 -07:00
|
|
|
|
|
|
|
|
prog->InputsRead = 0;
|
|
|
|
|
prog->OutputsWritten = 0;
|
2010-12-08 18:25:38 -07:00
|
|
|
prog->SystemValuesRead = 0;
|
2013-07-30 20:49:56 -07:00
|
|
|
if (shader_type == GL_FRAGMENT_SHADER) {
|
2012-06-20 12:49:29 -07:00
|
|
|
gl_fragment_program *fprog = (gl_fragment_program *) prog;
|
|
|
|
|
memset(fprog->InterpQualifier, 0, sizeof(fprog->InterpQualifier));
|
|
|
|
|
fprog->IsCentroid = 0;
|
|
|
|
|
fprog->UsesDFdy = false;
|
2012-07-18 23:18:14 -07:00
|
|
|
fprog->UsesKill = false;
|
2011-10-25 18:06:37 -07:00
|
|
|
}
|
2010-08-06 13:07:25 -07:00
|
|
|
visit_list_elements(&v, instructions);
|
|
|
|
|
}
|