* \author Michal Krol
*/
-#include "imports.h"
-#include "slang_utility.h"
+#include "main/imports.h"
#include "slang_storage.h"
-#include "slang_assemble.h"\r
-#include "slang_execute.h"
+#include "slang_mem.h"
/* slang_storage_array */
-GLboolean slang_storage_array_construct (slang_storage_array *arr)
+GLboolean
+slang_storage_array_construct(slang_storage_array * arr)
{
- arr->type = slang_stor_aggregate;
- arr->aggregate = NULL;
- arr->length = 0;\r
- return GL_TRUE;
+ arr->type = SLANG_STORE_AGGREGATE;
+ arr->aggregate = NULL;
+ arr->length = 0;
+ return GL_TRUE;
}
-GLvoid slang_storage_array_destruct (slang_storage_array *arr)
+GLvoid
+slang_storage_array_destruct(slang_storage_array * arr)
{
- if (arr->aggregate != NULL)
- {
- slang_storage_aggregate_destruct (arr->aggregate);
- slang_alloc_free (arr->aggregate);
- }
+ if (arr->aggregate != NULL) {
+ slang_storage_aggregate_destruct(arr->aggregate);
+ _slang_free(arr->aggregate);
+ }
}
/* slang_storage_aggregate */
-GLboolean slang_storage_aggregate_construct (slang_storage_aggregate *agg)
+GLboolean
+slang_storage_aggregate_construct(slang_storage_aggregate * agg)
{
- agg->arrays = NULL;
- agg->count = 0;\r
- return GL_TRUE;
+ agg->arrays = NULL;
+ agg->count = 0;
+ return GL_TRUE;
}
-GLvoid slang_storage_aggregate_destruct (slang_storage_aggregate *agg)
+GLvoid
+slang_storage_aggregate_destruct(slang_storage_aggregate * agg)
{
- GLuint i;\r
+ GLuint i;
- for (i = 0; i < agg->count; i++)
- slang_storage_array_destruct (agg->arrays + i);
- slang_alloc_free (agg->arrays);
+ for (i = 0; i < agg->count; i++)
+ slang_storage_array_destruct(agg->arrays + i);
+ _slang_free(agg->arrays);
}
-static slang_storage_array *slang_storage_aggregate_push_new (slang_storage_aggregate *agg)
+static slang_storage_array *
+slang_storage_aggregate_push_new(slang_storage_aggregate * agg)
{
- slang_storage_array *arr = NULL;\r
-
- agg->arrays = (slang_storage_array *) slang_alloc_realloc (agg->arrays, agg->count * sizeof (
- slang_storage_array), (agg->count + 1) * sizeof (slang_storage_array));
- if (agg->arrays != NULL)
- {
- arr = agg->arrays + agg->count;
- if (!slang_storage_array_construct (arr))\r
- return NULL;
- agg->count++;
- }
- return arr;
+ slang_storage_array *arr = NULL;
+
+ agg->arrays = (slang_storage_array *)
+ _slang_realloc(agg->arrays,
+ agg->count * sizeof(slang_storage_array),
+ (agg->count + 1) * sizeof(slang_storage_array));
+ if (agg->arrays != NULL) {
+ arr = agg->arrays + agg->count;
+ if (!slang_storage_array_construct(arr))
+ return NULL;
+ agg->count++;
+ }
+ return arr;
}
/* _slang_aggregate_variable() */
-static GLboolean aggregate_vector (slang_storage_aggregate *agg, slang_storage_type basic_type,
- GLuint row_count)
+static GLboolean
+aggregate_vector(slang_storage_aggregate * agg, slang_storage_type basic_type,
+ GLuint row_count)
{
- slang_storage_array *arr = slang_storage_aggregate_push_new (agg);
- if (arr == NULL)
- return GL_FALSE;
- arr->type = basic_type;
- arr->length = row_count;
- return GL_TRUE;
+ slang_storage_array *arr = slang_storage_aggregate_push_new(agg);
+ if (arr == NULL)
+ return GL_FALSE;
+ arr->type = basic_type;
+ arr->length = row_count;
+ return GL_TRUE;
}
-static GLboolean aggregate_matrix (slang_storage_aggregate *agg, slang_storage_type basic_type,
- GLuint dimension)
+static GLboolean
+aggregate_matrix(slang_storage_aggregate * agg, slang_storage_type basic_type,
+ GLuint columns, GLuint rows)
{
- slang_storage_array *arr = slang_storage_aggregate_push_new (agg);\r
- if (arr == NULL)
- return GL_FALSE;
- arr->type = slang_stor_aggregate;
- arr->length = dimension;
- arr->aggregate = (slang_storage_aggregate *) slang_alloc_malloc (sizeof (slang_storage_aggregate));
- if (arr->aggregate == NULL)
- return GL_FALSE;
- if (!slang_storage_aggregate_construct (arr->aggregate))\r
- {\r
- slang_alloc_free (arr->aggregate);\r
- arr->aggregate = NULL;\r
- return GL_FALSE;\r
- }
- if (!aggregate_vector (arr->aggregate, basic_type, dimension))
- return GL_FALSE;
- return GL_TRUE;
+ slang_storage_array *arr = slang_storage_aggregate_push_new(agg);
+ if (arr == NULL)
+ return GL_FALSE;
+ arr->type = SLANG_STORE_AGGREGATE;
+ arr->length = columns;
+ arr->aggregate = (slang_storage_aggregate *)
+ _slang_alloc(sizeof(slang_storage_aggregate));
+ if (arr->aggregate == NULL)
+ return GL_FALSE;
+ if (!slang_storage_aggregate_construct(arr->aggregate)) {
+ _slang_free(arr->aggregate);
+ arr->aggregate = NULL;
+ return GL_FALSE;
+ }
+ if (!aggregate_vector(arr->aggregate, basic_type, rows))
+ return GL_FALSE;
+ return GL_TRUE;
}
-static GLboolean aggregate_variables (slang_storage_aggregate *agg, slang_variable_scope *vars,
- slang_function_scope *funcs, slang_struct_scope *structs, slang_variable_scope *globals,\r
- slang_machine *mach, slang_assembly_file *file, slang_atom_pool *atoms)
+
+static GLboolean
+aggregate_variables(slang_storage_aggregate * agg,
+ slang_variable_scope * vars, slang_function_scope * funcs,
+ slang_struct_scope * structs,
+ slang_variable_scope * globals,
+ slang_atom_pool * atoms)
{
- GLuint i;\r
+ GLuint i;
- for (i = 0; i < vars->num_variables; i++)
- if (!_slang_aggregate_variable (agg, &vars->variables[i].type.specifier,
- vars->variables[i].array_size, funcs, structs, globals, mach, file, atoms))
- return GL_FALSE;
- return GL_TRUE;
+ for (i = 0; i < vars->num_variables; i++)
+ if (!_slang_aggregate_variable(agg, &vars->variables[i]->type.specifier,
+ vars->variables[i]->array_len, funcs,
+ structs, globals, atoms))
+ return GL_FALSE;
+ return GL_TRUE;
}
-\r
-static GLboolean eval_array_size (slang_assembly_file *file, slang_machine *pmach,\r
- slang_assembly_name_space *space, slang_operation *array_size, GLuint *plength,\r
- slang_atom_pool *atoms)\r
-{\r
- slang_assembly_file_restore_point point;\r
- slang_machine mach;\r
- slang_assemble_ctx A;\r
-\r
- A.file = file;\r
- A.mach = pmach;\r
- A.atoms = atoms;\r
- A.space = *space;\r
- A.local.ret_size = 0;\r
- A.local.addr_tmp = 0;\r
- A.local.swizzle_tmp = 4;\r
-\r
- /* save the current assembly */\r
- if (!slang_assembly_file_restore_point_save (file, &point))\r
- return GL_FALSE;\r
-\r
- /* setup the machine */\r
- mach = *pmach;\r
- mach.ip = file->count;\r
-\r
- /* allocate local storage for expression */\r
- if (!slang_assembly_file_push_label (file, slang_asm_local_alloc, 20))\r
- return GL_FALSE;\r
- if (!slang_assembly_file_push_label (file, slang_asm_enter, 20))\r
- return GL_FALSE;\r
-\r
- /* insert the actual expression */\r
- if (!_slang_assemble_operation_ (&A, array_size, slang_ref_forbid))\r
- return GL_FALSE;\r
- if (!slang_assembly_file_push (file, slang_asm_exit))\r
- return GL_FALSE;\r
-\r
- /* execute the expression */\r
- if (!_slang_execute2 (file, &mach))\r
- return GL_FALSE;\r
-\r
- /* the evaluated expression is on top of the stack */\r
- *plength = (GLuint) mach.mem[mach.sp + SLANG_MACHINE_GLOBAL_SIZE]._float;\r
- /* TODO: check if 0 < arr->length <= 65535 */\r
-\r
- /* restore the old assembly */\r
- if (!slang_assembly_file_restore_point_load (file, &point))\r
- return GL_FALSE;\r
-\r
- return GL_TRUE;\r
-}\r
-
-GLboolean _slang_aggregate_variable (slang_storage_aggregate *agg, slang_type_specifier *spec,
- slang_operation *array_size, slang_function_scope *funcs, slang_struct_scope *structs,
- slang_variable_scope *vars, slang_machine *mach, slang_assembly_file *file,\r
- slang_atom_pool *atoms)
+
+
+GLboolean
+_slang_aggregate_variable(slang_storage_aggregate * agg,
+ slang_type_specifier * spec, GLuint array_len,
+ slang_function_scope * funcs,
+ slang_struct_scope * structs,
+ slang_variable_scope * vars,
+ slang_atom_pool * atoms)
{
- switch (spec->type)
- {
- case slang_spec_bool:
- return aggregate_vector (agg, slang_stor_bool, 1);
- case slang_spec_bvec2:
- return aggregate_vector (agg, slang_stor_bool, 2);
- case slang_spec_bvec3:
- return aggregate_vector (agg, slang_stor_bool, 3);
- case slang_spec_bvec4:
- return aggregate_vector (agg, slang_stor_bool, 4);
- case slang_spec_int:
- return aggregate_vector (agg, slang_stor_int, 1);
- case slang_spec_ivec2:
- return aggregate_vector (agg, slang_stor_int, 2);
- case slang_spec_ivec3:
- return aggregate_vector (agg, slang_stor_int, 3);
- case slang_spec_ivec4:
- return aggregate_vector (agg, slang_stor_int, 4);
- case slang_spec_float:
- return aggregate_vector (agg, slang_stor_float, 1);
- case slang_spec_vec2:
- return aggregate_vector (agg, slang_stor_float, 2);
- case slang_spec_vec3:
- return aggregate_vector (agg, slang_stor_float, 3);
- case slang_spec_vec4:
- return aggregate_vector (agg, slang_stor_float, 4);
- case slang_spec_mat2:
- return aggregate_matrix (agg, slang_stor_float, 2);
- case slang_spec_mat3:
- return aggregate_matrix (agg, slang_stor_float, 3);
- case slang_spec_mat4:
- return aggregate_matrix (agg, slang_stor_float, 4);
- case slang_spec_sampler1D:
- case slang_spec_sampler2D:
- case slang_spec_sampler3D:
- case slang_spec_samplerCube:
- case slang_spec_sampler1DShadow:
- case slang_spec_sampler2DShadow:
- return aggregate_vector (agg, slang_stor_int, 1);
- case slang_spec_struct:
- return aggregate_variables (agg, spec->_struct->fields, funcs, structs, vars, mach,\r
- file, atoms);
- case slang_spec_array:
- {
- slang_storage_array *arr;\r
- slang_assembly_name_space space;
-
- arr = slang_storage_aggregate_push_new (agg);
- if (arr == NULL)
- return GL_FALSE;
- arr->type = slang_stor_aggregate;
- arr->aggregate = (slang_storage_aggregate *) slang_alloc_malloc (sizeof (slang_storage_aggregate));
- if (arr->aggregate == NULL)
- return GL_FALSE;
- if (!slang_storage_aggregate_construct (arr->aggregate))\r
- {\r
- slang_alloc_free (arr->aggregate);\r
- arr->aggregate = NULL;\r
- return GL_FALSE;\r
- }
- if (!_slang_aggregate_variable (arr->aggregate, spec->_array, NULL, funcs, structs,\r
- vars, mach, file, atoms))
- return GL_FALSE;\r
- space.funcs = funcs;\r
- space.structs = structs;\r
- space.vars = vars;\r
- if (!eval_array_size (file, mach, &space, array_size, &arr->length, atoms))\r
- return GL_FALSE;
- }
- return GL_TRUE;
- default:
- return GL_FALSE;
- }
+ switch (spec->type) {
+ case SLANG_SPEC_BOOL:
+ return aggregate_vector(agg, SLANG_STORE_BOOL, 1);
+ case SLANG_SPEC_BVEC2:
+ return aggregate_vector(agg, SLANG_STORE_BOOL, 2);
+ case SLANG_SPEC_BVEC3:
+ return aggregate_vector(agg, SLANG_STORE_BOOL, 3);
+ case SLANG_SPEC_BVEC4:
+ return aggregate_vector(agg, SLANG_STORE_BOOL, 4);
+ case SLANG_SPEC_INT:
+ return aggregate_vector(agg, SLANG_STORE_INT, 1);
+ case SLANG_SPEC_IVEC2:
+ return aggregate_vector(agg, SLANG_STORE_INT, 2);
+ case SLANG_SPEC_IVEC3:
+ return aggregate_vector(agg, SLANG_STORE_INT, 3);
+ case SLANG_SPEC_IVEC4:
+ return aggregate_vector(agg, SLANG_STORE_INT, 4);
+ case SLANG_SPEC_FLOAT:
+ return aggregate_vector(agg, SLANG_STORE_FLOAT, 1);
+ case SLANG_SPEC_VEC2:
+ return aggregate_vector(agg, SLANG_STORE_FLOAT, 2);
+ case SLANG_SPEC_VEC3:
+ return aggregate_vector(agg, SLANG_STORE_FLOAT, 3);
+ case SLANG_SPEC_VEC4:
+ return aggregate_vector(agg, SLANG_STORE_FLOAT, 4);
+ case SLANG_SPEC_MAT2:
+ return aggregate_matrix(agg, SLANG_STORE_FLOAT, 2, 2);
+ case SLANG_SPEC_MAT3:
+ return aggregate_matrix(agg, SLANG_STORE_FLOAT, 3, 3);
+ case SLANG_SPEC_MAT4:
+ return aggregate_matrix(agg, SLANG_STORE_FLOAT, 4, 4);
+
+ case SLANG_SPEC_MAT23:
+ return aggregate_matrix(agg, SLANG_STORE_FLOAT, 2, 3);
+ case SLANG_SPEC_MAT32:
+ return aggregate_matrix(agg, SLANG_STORE_FLOAT, 3, 2);
+ case SLANG_SPEC_MAT24:
+ return aggregate_matrix(agg, SLANG_STORE_FLOAT, 2, 4);
+ case SLANG_SPEC_MAT42:
+ return aggregate_matrix(agg, SLANG_STORE_FLOAT, 4, 2);
+ case SLANG_SPEC_MAT34:
+ return aggregate_matrix(agg, SLANG_STORE_FLOAT, 3, 4);
+ case SLANG_SPEC_MAT43:
+ return aggregate_matrix(agg, SLANG_STORE_FLOAT, 4, 3);
+
+ case SLANG_SPEC_SAMPLER1D:
+ case SLANG_SPEC_SAMPLER2D:
+ case SLANG_SPEC_SAMPLER3D:
+ case SLANG_SPEC_SAMPLERCUBE:
+ case SLANG_SPEC_SAMPLER1DSHADOW:
+ case SLANG_SPEC_SAMPLER2DSHADOW:
+ case SLANG_SPEC_SAMPLER2DRECT:
+ case SLANG_SPEC_SAMPLER2DRECTSHADOW:
+ return aggregate_vector(agg, SLANG_STORE_INT, 1);
+ case SLANG_SPEC_STRUCT:
+ return aggregate_variables(agg, spec->_struct->fields, funcs, structs,
+ vars, atoms);
+ case SLANG_SPEC_ARRAY:
+ {
+ slang_storage_array *arr;
+
+ arr = slang_storage_aggregate_push_new(agg);
+ if (arr == NULL)
+ return GL_FALSE;
+ arr->type = SLANG_STORE_AGGREGATE;
+ arr->aggregate = (slang_storage_aggregate *)
+ _slang_alloc(sizeof(slang_storage_aggregate));
+ if (arr->aggregate == NULL)
+ return GL_FALSE;
+ if (!slang_storage_aggregate_construct(arr->aggregate)) {
+ _slang_free(arr->aggregate);
+ arr->aggregate = NULL;
+ return GL_FALSE;
+ }
+ if (!_slang_aggregate_variable(arr->aggregate, spec->_array, 0,
+ funcs, structs, vars, atoms))
+ return GL_FALSE;
+ arr->length = array_len;
+ /* TODO: check if 0 < arr->length <= 65535 */
+ }
+ return GL_TRUE;
+ default:
+ return GL_FALSE;
+ }
}
-/* _slang_sizeof_aggregate() */
-GLuint _slang_sizeof_aggregate (const slang_storage_aggregate *agg)
+GLuint
+_slang_sizeof_type(slang_storage_type type)
{
- GLuint i, size = 0;\r
-
- for (i = 0; i < agg->count; i++)
- {
- GLuint element_size;\r
-
- if (agg->arrays[i].type == slang_stor_aggregate)
- element_size = _slang_sizeof_aggregate (agg->arrays[i].aggregate);
- else
- element_size = sizeof (GLfloat);
- size += element_size * agg->arrays[i].length;
- }
- return size;
+ if (type == SLANG_STORE_AGGREGATE)
+ return 0;
+ if (type == SLANG_STORE_VEC4)
+ return 4 * sizeof(GLfloat);
+ return sizeof(GLfloat);
}
-/* _slang_flatten_aggregate () */
-GLboolean _slang_flatten_aggregate (slang_storage_aggregate *flat, const slang_storage_aggregate *agg)
+GLuint
+_slang_sizeof_aggregate(const slang_storage_aggregate * agg)
{
- GLuint i;\r
-
- for (i = 0; i < agg->count; i++)
- {
- GLuint j;\r
-
- for (j = 0; j < agg->arrays[i].length; j++)
- {
- if (agg->arrays[i].type == slang_stor_aggregate)
- {
- if (!_slang_flatten_aggregate (flat, agg->arrays[i].aggregate))
- return GL_FALSE;
- }
- else
- {
- slang_storage_array *arr;\r
-
- arr = slang_storage_aggregate_push_new (flat);
- if (arr == NULL)
- return GL_FALSE;
- arr->type = agg->arrays[i].type;
- arr->length = 1;
- }
- }
- }
- return GL_TRUE;
+ GLuint i, size = 0;
+
+ for (i = 0; i < agg->count; i++) {
+ slang_storage_array *arr = &agg->arrays[i];
+ GLuint element_size;
+
+ if (arr->type == SLANG_STORE_AGGREGATE)
+ element_size = _slang_sizeof_aggregate(arr->aggregate);
+ else
+ element_size = _slang_sizeof_type(arr->type);
+ size += element_size * arr->length;
+ }
+ return size;
}
+
+#if 0
+GLboolean
+_slang_flatten_aggregate(slang_storage_aggregate * flat,
+ const slang_storage_aggregate * agg)
+{
+ GLuint i;
+
+ for (i = 0; i < agg->count; i++) {
+ GLuint j;
+
+ for (j = 0; j < agg->arrays[i].length; j++) {
+ if (agg->arrays[i].type == SLANG_STORE_AGGREGATE) {
+ if (!_slang_flatten_aggregate(flat, agg->arrays[i].aggregate))
+ return GL_FALSE;
+ }
+ else {
+ GLuint k, count;
+ slang_storage_type type;
+
+ if (agg->arrays[i].type == SLANG_STORE_VEC4) {
+ count = 4;
+ type = SLANG_STORE_FLOAT;
+ }
+ else {
+ count = 1;
+ type = agg->arrays[i].type;
+ }
+
+ for (k = 0; k < count; k++) {
+ slang_storage_array *arr;
+
+ arr = slang_storage_aggregate_push_new(flat);
+ if (arr == NULL)
+ return GL_FALSE;
+ arr->type = type;
+ arr->length = 1;
+ }
+ }
+ }
+ }
+ return GL_TRUE;
+}
+#endif