* \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
*/
static GLboolean
-swizzle_to_writemask(GLuint swizzle,
+swizzle_to_writemask(slang_assemble_ctx *A, GLuint swizzle,
GLuint *writemaskOut, GLuint *swizzleOut)
{
GLuint mask = 0x0, newSwizzle[4];
break;
}
assert(swz >= 0 && swz <= 3);
+
+ if (swizzle != SWIZZLE_XXXX &&
+ swizzle != SWIZZLE_YYYY &&
+ swizzle != SWIZZLE_ZZZZ &&
+ swizzle != SWIZZLE_WWWW &&
+ (mask & (1 << swz))) {
+ /* a channel can't be specified twice (ex: ".xyyz") */
+ slang_info_log_error(A->log, "Invalid writemask '%s'",
+ _mesa_swizzle_string(swizzle, 0, 0));
+ return GL_FALSE;
+ }
+
mask |= (1 << swz);
}
assert(mask <= 0xf);
* As above, but produce a writemask.
*/
static GLuint
-resolve_writemask(const slang_operation *oper)
+resolve_writemask(slang_assemble_ctx *A, const slang_operation *oper)
{
GLuint swizzle = resolve_swizzle(oper);
GLuint writemask, swizzleOut;
- swizzle_to_writemask(swizzle, &writemask, &swizzleOut);
+ swizzle_to_writemask(A, swizzle, &writemask, &swizzleOut);
return writemask;
}
dest_oper = &oper->children[0];
- writemask = resolve_writemask(dest_oper);
+ writemask = resolve_writemask(A, dest_oper);
n0 = _slang_gen_operation(A, dest_oper);
if (!n0)
fun = _slang_locate_struct_constructor(A, name);
}
+ /*
+ * At this point, some heuristics are used to try to find a function
+ * that matches the calling signature by means of casting or "unrolling"
+ * of constructors.
+ */
+
if (!fun && _slang_is_vec_mat_type(name)) {
/* Next, if this call looks like a vec() or mat() constructor call,
* try "unwinding" the args to satisfy a constructor.
}
}
- if (!fun) {
+ if (!fun && _slang_is_vec_mat_type(name)) {
/* Next, try casting args to the types of the formal parameters */
int numArgs = oper->num_children;
fun = _slang_find_function_by_argc(A->space.funcs, name, numArgs);
assert(fun);
}
+ if (!fun) {
+ slang_info_log_error(A->log,
+ "Function '%s' not found (check argument types)",
+ name);
+ return NULL;
+ }
+
n = _slang_gen_function_call(A, fun, oper, dest);
if (n && !n->Store && !dest
/* type-check expression */
if (!_slang_is_boolean(A, &oper->children[0])) {
- slang_info_log_error(A->log, "boolean expression expected for 'while'");
+ slang_info_log_error(A->log, "boolean expression expected for 'if'");
return NULL;
}
/*assert(!var->declared);*/
var->declared = GL_TRUE;
- assert(!is_sampler_type(&var->type));
-
n = new_node0(IR_VAR_DECL);
if (n) {
_slang_attach_storage(n, var);
assert(n->Store);
assert(n->Store->Index < 0);
- n->Store->File = PROGRAM_TEMPORARY;
+ if (is_sampler_type(&var->type)) {
+ n->Store->File = PROGRAM_SAMPLER;
+ }
+ else {
+ n->Store->File = PROGRAM_TEMPORARY;
+ }
+
n->Store->Size = _slang_sizeof_type_specifier(&n->Var->type.specifier);
if (n->Store->Size <= 0) {
*/
n->Store->Swizzle = SWIZZLE_NOOP;
}
-
- A->program->NumTemporaries++; /* an approximation */
}
return n;
}
}
+/**
+ * Check if an assignment of type t1 to t0 is legal.
+ * XXX more cases needed.
+ */
+static GLboolean
+_slang_assignment_compatible(slang_assemble_ctx *A,
+ slang_operation *op0,
+ slang_operation *op1)
+{
+ slang_typeinfo t0, t1;
+ GLuint sz0, sz1;
+
+ if (op0->type == SLANG_OPER_POSTINCREMENT ||
+ op0->type == SLANG_OPER_POSTDECREMENT) {
+ return GL_FALSE;
+ }
+
+ slang_typeinfo_construct(&t0);
+ _slang_typeof_operation(A, op0, &t0);
+
+ slang_typeinfo_construct(&t1);
+ _slang_typeof_operation(A, op1, &t1);
+
+ sz0 = _slang_sizeof_type_specifier(&t0.spec);
+ sz1 = _slang_sizeof_type_specifier(&t1.spec);
+
+#if 1
+ if (sz0 != sz1) {
+ /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
+ return GL_FALSE;
+ }
+#endif
+
+ if (t0.spec.type == SLANG_SPEC_STRUCT &&
+ t1.spec.type == SLANG_SPEC_STRUCT &&
+ t0.spec._struct->a_name != t1.spec._struct->a_name)
+ return GL_FALSE;
+
+ if (t0.spec.type == SLANG_SPEC_FLOAT &&
+ t1.spec.type == SLANG_SPEC_BOOL)
+ return GL_FALSE;
+
+#if 0 /* not used just yet - causes problems elsewhere */
+ if (t0.spec.type == SLANG_SPEC_INT &&
+ t1.spec.type == SLANG_SPEC_FLOAT)
+ return GL_FALSE;
+#endif
+
+ if (t0.spec.type == SLANG_SPEC_BOOL &&
+ t1.spec.type == SLANG_SPEC_FLOAT)
+ return GL_FALSE;
+
+ if (t0.spec.type == SLANG_SPEC_BOOL &&
+ t1.spec.type == SLANG_SPEC_INT)
+ return GL_FALSE;
+
+ return GL_TRUE;
+}
+
+
/**
* Generate IR tree for a variable declaration.
slang_ir_node *varDecl;
slang_variable *v;
const char *varName = (char *) oper->a_id;
+ slang_operation *initializer;
- assert(oper->num_children == 0 || oper->num_children == 1);
+ assert(oper->type == SLANG_OPER_VARIABLE_DECL);
+ assert(oper->num_children <= 1);
v = _slang_locate_variable(oper->locals, oper->a_id, GL_TRUE);
- /*printf("Declare %s at %p\n", varName, (void *) v);*/
- assert(v);
+ if (!v)
+ return NULL; /* "shouldn't happen" */
+
+ if (v->type.qualifier == SLANG_QUAL_ATTRIBUTE ||
+ v->type.qualifier == SLANG_QUAL_VARYING ||
+ v->type.qualifier == SLANG_QUAL_UNIFORM) {
+ /* can't declare attribute/uniform vars inside functions */
+ slang_info_log_error(A->log,
+ "local variable '%s' cannot be an attribute/uniform/varying",
+ varName);
+ return NULL;
+ }
#if 0
if (v->declared) {
#endif
varDecl = _slang_gen_var_decl(A, v);
+ if (!varDecl)
+ return NULL;
+ /* check if the var has an initializer */
if (oper->num_children > 0) {
- /* child is initializer */
- slang_ir_node *var, *init, *rhs;
assert(oper->num_children == 1);
- var = new_var(A, oper, oper->a_id);
- if (!var) {
- slang_info_log_error(A->log, "undefined variable '%s'", varName);
- return NULL;
- }
- /* XXX make copy of this initializer? */
- rhs = _slang_gen_operation(A, &oper->children[0]);
- if (!rhs)
- return NULL; /* must have found an error */
- init = new_node2(IR_COPY, var, rhs);
-
- /*assert(rhs->Opcode != IR_SEQ);*/
- n = new_seq(varDecl, init);
+ initializer = &oper->children[0];
}
else if (v->initializer) {
- slang_ir_node *var, *init, *rhs;
+ initializer = v->initializer;
+ }
+ else {
+ initializer = NULL;
+ }
+
+ if (v->type.qualifier == SLANG_QUAL_CONST && !initializer) {
+ slang_info_log_error(A->log,
+ "const-qualified variable '%s' requires initializer",
+ varName);
+ return NULL;
+ }
+
+
+ if (initializer) {
+ slang_ir_node *var, *init;
+
+ /* type check/compare var and initializer */
+ if (!_slang_assignment_compatible(A, oper, initializer)) {
+ slang_info_log_error(A->log, "incompatible types in assignment");
+ return NULL;
+ }
+
var = new_var(A, oper, oper->a_id);
if (!var) {
slang_info_log_error(A->log, "undefined variable '%s'", varName);
/* if the variable is const, the initializer must be a const
* expression as well.
*/
- if (!_slang_is_constant_expr(v->initializer)) {
+#if 0
+ if (!_slang_is_constant_expr(initializer)) {
slang_info_log_error(A->log,
"initializer for %s not constant", varName);
return NULL;
}
+#endif
}
-#if 0
- /* XXX make copy of this initializer? */
- {
- slang_operation dup;
- slang_operation_construct(&dup);
- slang_operation_copy(&dup, v->initializer);
- _slang_simplify(&dup, &A->space, A->atoms);
- rhs = _slang_gen_operation(A, &dup);
- }
-#else
- _slang_simplify(v->initializer, &A->space, A->atoms);
- rhs = _slang_gen_operation(A, v->initializer);
-#endif
- if (!rhs)
+ _slang_simplify(initializer, &A->space, A->atoms);
+
+ init = _slang_gen_operation(A, initializer);
+ if (!init)
return NULL;
- /*assert(rhs->Store);*/
+ /*assert(init->Store);*/
- if (rhs->Store && var->Store->Size != rhs->Store->Size) {
+ /* XXX remove this when type checking is added above */
+ if (init->Store && var->Store->Size != init->Store->Size) {
slang_info_log_error(A->log, "invalid assignment (wrong types)");
return NULL;
}
- init = new_node2(IR_COPY, var, rhs);
- n = new_seq(varDecl, init);
+ n = new_node2(IR_COPY, var, init);
+ n = new_seq(varDecl, n);
}
else {
n = varDecl;
}
+
return n;
}
}
-/**
- * Check if an assignment of type t1 to t0 is legal.
- * XXX more cases needed.
- */
static GLboolean
-_slang_assignment_compatible(slang_assemble_ctx *A,
- slang_operation *op0,
- slang_operation *op1)
+is_store_writable(const slang_assemble_ctx *A, const slang_ir_storage *store)
{
- slang_typeinfo t0, t1;
- GLuint sz0, sz1;
+ while (store->Parent)
+ store = store->Parent;
-
- slang_typeinfo_construct(&t0);
- _slang_typeof_operation(A, op0, &t0);
-
- slang_typeinfo_construct(&t1);
- _slang_typeof_operation(A, op1, &t1);
-
- sz0 = _slang_sizeof_type_specifier(&t0.spec);
- sz1 = _slang_sizeof_type_specifier(&t1.spec);
-
-#if 1
- if (sz0 != sz1) {
- printf("size mismatch %u vs %u\n", sz0, sz1);
+ if (!(store->File == PROGRAM_OUTPUT ||
+ store->File == PROGRAM_TEMPORARY ||
+ (store->File == PROGRAM_VARYING &&
+ A->program->Target == GL_VERTEX_PROGRAM_ARB))) {
return GL_FALSE;
}
-#endif
-
- if (t0.spec.type == SLANG_SPEC_STRUCT &&
- t1.spec.type == SLANG_SPEC_STRUCT &&
- t0.spec._struct->a_name != t1.spec._struct->a_name)
- return GL_FALSE;
-
-#if 0 /* not used just yet - causes problems elsewhere */
- if (t0.spec.type == SLANG_SPEC_INT &&
- t1.spec.type == SLANG_SPEC_FLOAT)
- return GL_FALSE;
-
- if (t0.spec.type == SLANG_SPEC_BOOL &&
- t1.spec.type == SLANG_SPEC_FLOAT)
- return GL_FALSE;
-
- if (t0.spec.type == SLANG_SPEC_BOOL &&
- t1.spec.type == SLANG_SPEC_INT)
- return GL_FALSE;
-#endif
- return GL_TRUE;
+ else {
+ return GL_TRUE;
+ }
}
if (!_slang_assignment_compatible(A,
&oper->children[0],
&oper->children[1])) {
- slang_info_log_error(A->log, "illegal types in assignment");
+ slang_info_log_error(A->log, "incompatible types in assignment");
return NULL;
}
lhs = _slang_gen_operation(A, &oper->children[0]);
- if (lhs) {
- if (!lhs->Store) {
- slang_info_log_error(A->log,
- "invalid left hand side for assignment");
- return NULL;
- }
- if (!(lhs->Store->File == PROGRAM_OUTPUT ||
- lhs->Store->File == PROGRAM_TEMPORARY ||
- (lhs->Store->File == PROGRAM_VARYING &&
- A->program->Target == GL_VERTEX_PROGRAM_ARB) ||
- lhs->Store->File == PROGRAM_UNDEFINED)) {
- slang_info_log_error(A->log,
- "illegal assignment to read-only l-value");
- return NULL;
- }
+ if (!lhs) {
+ return NULL;
+ }
+
+ if (!lhs->Store) {
+ slang_info_log_error(A->log,
+ "invalid left hand side for assignment");
+ return NULL;
+ }
+
+ /* check that lhs is writable */
+ if (!is_store_writable(A, lhs->Store)) {
+ slang_info_log_error(A->log,
+ "illegal assignment to read-only l-value");
+ return NULL;
}
rhs = _slang_gen_operation(A, &oper->children[1]);
if (lhs && rhs) {
/* convert lhs swizzle into writemask */
GLuint writemask, newSwizzle;
- if (!swizzle_to_writemask(lhs->Store->Swizzle,
+ if (!swizzle_to_writemask(A, lhs->Store->Swizzle,
&writemask, &newSwizzle)) {
/* Non-simple writemask, need to swizzle right hand side in
* order to put components into the right place.
index = _slang_gen_operation(A, &oper->children[1]);
if (array && index) {
/* bounds check */
- GLint constIndex = 0;
+ GLint constIndex = -1;
if (index->Opcode == IR_FLOAT) {
constIndex = (int) index->Value[0];
if (constIndex < 0 || constIndex >= arrayLen) {
return NULL;
}
+ if (oper->type != SLANG_OPER_EQUAL &&
+ oper->type != SLANG_OPER_NOTEQUAL) {
+ /* <, <=, >, >= can only be used with scalars */
+ if ((t0.spec.type != SLANG_SPEC_INT &&
+ t0.spec.type != SLANG_SPEC_FLOAT) ||
+ (t1.spec.type != SLANG_SPEC_INT &&
+ t1.spec.type != SLANG_SPEC_FLOAT)) {
+ slang_info_log_error(A->log, "Incompatible type(s) for inequality operator");
+ return NULL;
+ }
+ }
+
n = new_node2(opcode,
_slang_gen_operation(A, &oper->children[0]),
_slang_gen_operation(A, &oper->children[1]));
* store->Index = sampler number (0..7, typically)
* store->Size = texture type index (1D, 2D, 3D, cube, etc)
*/
- GLint sampNum = _mesa_add_sampler(prog->Parameters, varName, datatype);
- store = _slang_new_ir_storage(PROGRAM_SAMPLER, sampNum, texIndex);
+ if (var->initializer) {
+ slang_info_log_error(A->log, "illegal assignment to '%s'", varName);
+ return GL_FALSE;
+ }
+#if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
+ /* disallow rect samplers */
+ if (var->type.specifier.type == SLANG_SPEC_SAMPLER2DRECT ||
+ var->type.specifier.type == SLANG_SPEC_SAMPLER2DRECTSHADOW) {
+ slang_info_log_error(A->log, "invalid sampler type for '%s'", varName);
+ return GL_FALSE;
+ }
+#endif
+ {
+ GLint sampNum = _mesa_add_sampler(prog->Parameters, varName, datatype);
+ store = _slang_new_ir_storage(PROGRAM_SAMPLER, sampNum, texIndex);
+ }
if (dbg) printf("SAMPLER ");
}
else if (var->type.qualifier == SLANG_QUAL_UNIFORM) {
/* Uniform variable */
const GLint totalSize = array_size(size, var->array_len);
const GLuint swizzle = _slang_var_swizzle(totalSize, 0);
+
if (prog) {
/* user-defined uniform */
if (datatype == GL_NONE) {
if (var->type.specifier.type == SLANG_SPEC_STRUCT) {
- _mesa_problem(NULL, "user-declared uniform structs not supported yet");
+ /* temporary work-around */
+ GLenum datatype = GL_FLOAT;
+ GLint uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
+ totalSize, datatype, NULL);
+ store = _slang_new_ir_storage_swz(PROGRAM_UNIFORM, uniformLoc,
+ totalSize, swizzle);
+
/* XXX what we need to do is unroll the struct into its
* basic types, creating a uniform variable for each.
* For example:
* "f.a" (GL_FLOAT_VEC3)
* "f.b" (GL_FLOAT_VEC4)
*/
+
+ if (var->initializer) {
+ slang_info_log_error(A->log,
+ "unsupported initializer for uniform '%s'", varName);
+ return GL_FALSE;
+ }
}
else {
slang_info_log_error(A->log,
"invalid datatype for uniform variable %s",
varName);
+ return GL_FALSE;
}
- return GL_FALSE;
}
else {
- GLint uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
- totalSize, datatype);
+ GLint uniformLoc;
+ const GLfloat *initialValues = NULL;
+ if (var->initializer) {
+ _slang_simplify(var->initializer, &A->space, A->atoms);
+ if (var->initializer->type == SLANG_OPER_LITERAL_FLOAT ||
+ var->initializer->type == SLANG_OPER_LITERAL_INT) {
+ /* simple float/vector initializer */
+ initialValues = var->initializer->literal;
+ }
+ else {
+ /* complex initializer */
+ slang_info_log_error(A->log,
+ "unsupported initializer for uniform '%s'", varName);
+ return GL_FALSE;
+ }
+ }
+
+ uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
+ totalSize, datatype, initialValues);
store = _slang_new_ir_storage_swz(PROGRAM_UNIFORM, uniformLoc,
totalSize, swizzle);
}
if (dbg) printf("UNIFORM (sz %d) ", totalSize);
}
else if (var->type.qualifier == SLANG_QUAL_VARYING) {
- if (var->type.specifier.type == SLANG_SPEC_STRUCT) {
+ const GLint totalSize = array_size(size, var->array_len);
+
+ /* varyings must be float, vec or mat */
+ if (!_slang_type_is_float_vec_mat(var->type.specifier.type) &&
+ var->type.specifier.type != SLANG_SPEC_ARRAY) {
slang_info_log_error(A->log,
- "varying '%s' cannot be a structure type",
+ "varying '%s' must be float/vector/matrix",
+ varName);
+ return GL_FALSE;
+ }
+
+ if (var->initializer) {
+ slang_info_log_error(A->log, "illegal initializer for varying '%s'",
varName);
return GL_FALSE;
}
if (prog) {
/* user-defined varying */
- GLint varyingLoc = _mesa_add_varying(prog->Varying, varName, size);
+ GLint varyingLoc = _mesa_add_varying(prog->Varying, varName, totalSize);
GLuint swizzle = _slang_var_swizzle(size, 0);
store = _slang_new_ir_storage_swz(PROGRAM_VARYING, varyingLoc,
- size, swizzle);
+ totalSize, swizzle);
}
else {
/* pre-defined varying, like gl_Color or gl_TexCoord */
if (dbg) printf("VARYING ");
}
else if (var->type.qualifier == SLANG_QUAL_ATTRIBUTE) {
+ GLuint swizzle;
+ GLint index;
+ /* attributes must be float, vec or mat */
+ if (!_slang_type_is_float_vec_mat(var->type.specifier.type)) {
+ slang_info_log_error(A->log,
+ "attribute '%s' must be float/vector/matrix",
+ varName);
+ return GL_FALSE;
+ }
+
if (prog) {
/* user-defined vertex attribute */
const GLint attr = -1; /* unknown */
- GLint index = _mesa_add_attribute(prog->Attributes, varName,
- size, datatype, attr);
+ swizzle = _slang_var_swizzle(size, 0);
+ index = _mesa_add_attribute(prog->Attributes, varName,
+ size, datatype, attr);
assert(index >= 0);
- store = _slang_new_ir_storage(PROGRAM_INPUT,
- VERT_ATTRIB_GENERIC0 + index, size);
+ index = VERT_ATTRIB_GENERIC0 + index;
}
else {
/* pre-defined vertex attrib */
- GLuint swizzle;
- GLint index = _slang_input_index(varName, GL_VERTEX_PROGRAM_ARB,
- &swizzle);
+ index = _slang_input_index(varName, GL_VERTEX_PROGRAM_ARB, &swizzle);
assert(index >= 0);
- store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index, size, swizzle);
}
+ store = _slang_new_ir_storage_swz(PROGRAM_INPUT, index, size, swizzle);
if (dbg) printf("ATTRIB ");
}
else if (var->type.qualifier == SLANG_QUAL_FIXEDINPUT) {