summaryrefslogtreecommitdiff
path: root/src/mesa/shader/slang/slang_storage.c
blob: 3b2fda415b298fd9235c4dbad25ca67f98170615 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Mesa 3-D graphics library
 * Version:  6.3
 *
 * Copyright (C) 2005  Brian Paul   All Rights Reserved.
 *
 * 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 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
 * BRIAN PAUL 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 slang_storage.c
 * slang variable storage
 * \author Michal Krol
 */

#include "imports.h"
#include "slang_utility.h"
#include "slang_storage.h"
#include "slang_assemble.h"

/* slang_storage_array */

void slang_storage_array_construct (slang_storage_array *arr)
{
	arr->type = slang_stor_aggregate;
	arr->aggregate = NULL;
	arr->length = 0;
}

void slang_storage_array_destruct (slang_storage_array *arr)
{
	if (arr->aggregate != NULL)
	{
		slang_storage_aggregate_destruct (arr->aggregate);
		slang_alloc_free (arr->aggregate);
	}
}

/* slang_storage_aggregate */

void slang_storage_aggregate_construct (slang_storage_aggregate *agg)
{
	agg->arrays = NULL;
	agg->count = 0;
}

void slang_storage_aggregate_destruct (slang_storage_aggregate *agg)
{
	unsigned int i;
	for (i = 0; i < agg->count; i++)
		slang_storage_array_destruct (agg->arrays + i);
	slang_alloc_free (agg->arrays);
}

static slang_storage_array *slang_storage_aggregate_push_new (slang_storage_aggregate *agg)
{
	slang_storage_array *arr = NULL;
	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;
		slang_storage_array_construct (arr);
		agg->count++;
	}
	return arr;
}

/* _slang_aggregate_variable() */

static int aggregate_vector (slang_storage_aggregate *agg, slang_storage_type basic_type,
	unsigned int row_count)
{
	slang_storage_array *arr = slang_storage_aggregate_push_new (agg);
	if (arr == NULL)
		return 0;
	arr->type = basic_type;
	arr->length = row_count;
	return 1;
}

static int aggregate_matrix (slang_storage_aggregate *agg, slang_storage_type basic_type,
	unsigned int dimension)
{
	slang_storage_array *arr = slang_storage_aggregate_push_new (agg);
	if (arr == NULL)
		return 0;
	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 0;
	slang_storage_aggregate_construct (arr->aggregate);
	if (!aggregate_vector (arr->aggregate, basic_type, dimension))
		return 0;
	return 1;
}

static int aggregate_variables (slang_storage_aggregate *agg, const slang_variable_scope *vars,
	slang_function_scope *funcs, slang_struct_scope *structs)
{
	unsigned int 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))
			return 0;
	return 1;
}

int _slang_aggregate_variable (slang_storage_aggregate *agg, slang_type_specifier *spec,
	slang_operation *array_size, slang_function_scope *funcs, slang_struct_scope *structs)
{
	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);
	case slang_spec_array:
		{
			slang_storage_array *arr;
			slang_assembly_file file;
			slang_assembly_flow_control flow;
			slang_assembly_name_space space;
			slang_assembly_local_info info;
			slang_assembly_stack_info stk;

			arr = slang_storage_aggregate_push_new (agg);
			if (arr == NULL)
				return 0;
			arr->type = slang_stor_aggregate;
			arr->aggregate = (slang_storage_aggregate *) slang_alloc_malloc (sizeof (
				slang_storage_aggregate));
			if (arr->aggregate == NULL)
				return 0;
			slang_storage_aggregate_construct (arr->aggregate);
			if (!_slang_aggregate_variable (arr->aggregate, spec->_array, NULL, funcs, structs))
				return 0;
			slang_assembly_file_construct (&file);
			space.funcs = funcs;
			space.structs = structs;
			/* XXX: vars! */
			space.vars = NULL;
			if (!_slang_assemble_operation (&file, array_size, 0, &flow, &space, &info, &stk))
			{
				slang_assembly_file_destruct (&file);
				return 0;
			}
			/* TODO: evaluate array size */
			slang_assembly_file_destruct (&file);
			arr->length = 256;
		}
		return 1;
	default:
		return 0;
	}
}

/* _slang_sizeof_aggregate() */

unsigned int _slang_sizeof_aggregate (const slang_storage_aggregate *agg)
{
	unsigned int i, size = 0;
	for (i = 0; i < agg->count; i++)
	{
		unsigned int element_size;
		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;
}

/* _slang_flatten_aggregate () */

int _slang_flatten_aggregate (slang_storage_aggregate *flat, const slang_storage_aggregate *agg)
{
	unsigned int i;
	for (i = 0; i < agg->count; i++)
	{
		unsigned int j;
		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 0;
			}
			else
			{
				slang_storage_array *arr;
				arr = slang_storage_aggregate_push_new (flat);
				if (arr == NULL)
					return 0;
				arr->type = agg->arrays[i].type;
				arr->length = 1;
			}
		}
	}
	return 1;
}