summaryrefslogtreecommitdiff
path: root/src/mesa/shader/slang/slang_export.c
blob: ffc2ec975eafe49c40a247f347bb830bfaee3566 (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Mesa 3-D graphics library
 * Version:  6.5
 *
 * Copyright (C) 2006  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_export.c
 * interface between assembly code and the application
 * \author Michal Krol
 */

#include "imports.h"
#include "slang_export.h"

/*
 * slang_export_data_quant
 */

GLvoid slang_export_data_quant_ctr (slang_export_data_quant *self)
{
	self->name = SLANG_ATOM_NULL;
	self->size = 0;
	self->array_len = 0;
	self->structure = NULL;
	self->u.basic_type = GL_FLOAT;
}

GLvoid slang_export_data_quant_dtr (slang_export_data_quant *self)
{
	if (self->structure != NULL)
	{
		GLuint i;

		for (i = 0; i < self->u.field_count; i++)
			slang_export_data_quant_dtr (&self->structure[i]);
		slang_alloc_free (self->structure);
	}
}

slang_export_data_quant *slang_export_data_quant_add_field (slang_export_data_quant *self)
{
	const GLuint n = self->u.field_count;

	self->structure = (slang_export_data_quant *) slang_alloc_realloc (self->structure,
		n * sizeof (slang_export_data_quant), (n + 1) * sizeof (slang_export_data_quant));
	if (self->structure == NULL)
		return NULL;
	slang_export_data_quant_ctr (&self->structure[n]);
	self->u.field_count++;
	return &self->structure[n];
}

/*
 * slang_export_data_entry
 */

GLvoid slang_export_data_entry_ctr (slang_export_data_entry *self)
{
	slang_export_data_quant_ctr (&self->quant);
	self->access = slang_exp_uniform;
	self->address = ~0;
}

GLvoid slang_export_data_entry_dtr (slang_export_data_entry *self)
{
	slang_export_data_quant_dtr (&self->quant);
}

/*
 * slang_export_data_table
 */

GLvoid slang_export_data_table_ctr (slang_export_data_table *self)
{
	self->entries = NULL;
	self->count = 0;
	self->atoms = NULL;
}

GLvoid slang_export_data_table_dtr (slang_export_data_table *self)
{
	if (self->entries != NULL)
	{
		GLuint i;

		for (i = 0; i < self->count; i++)
			slang_export_data_entry_dtr (&self->entries[i]);
		slang_alloc_free (self->entries);
	}
}

slang_export_data_entry *slang_export_data_table_add (slang_export_data_table *self)
{
	const GLuint n = self->count;

	self->entries = (slang_export_data_entry *) slang_alloc_realloc (self->entries,
		n * sizeof (slang_export_data_entry), (n + 1) * sizeof (slang_export_data_entry));
	if (self->entries == NULL)
		return NULL;
	slang_export_data_entry_ctr (&self->entries[n]);
	self->count++;
	return &self->entries[n];
}

/*
 * slang_export_code_entry
 */

GLvoid slang_export_code_entry_ctr (slang_export_code_entry *self)
{
	self->name = SLANG_ATOM_NULL;
	self->address = ~0;
}

GLvoid slang_export_code_entry_dtr (slang_export_code_entry *self)
{
}

/*
 * slang_export_code_table
 */

GLvoid slang_export_code_table_ctr (slang_export_code_table *self)
{
	self->entries = NULL;
	self->count = 0;
	self->atoms = NULL;
}

GLvoid slang_export_code_table_dtr (slang_export_code_table *self)
{
	if (self->entries != NULL)
	{
		GLuint i;

		for (i = 0; i < self->count; i++)
			slang_export_code_entry_dtr (&self->entries[i]);
		slang_alloc_free (self->entries);
	}
}

slang_export_code_entry *slang_export_code_table_add (slang_export_code_table *self)
{
	const GLuint n = self->count;

	self->entries = (slang_export_code_entry *) slang_alloc_realloc (self->entries,
		n * sizeof (slang_export_code_entry), (n + 1) * sizeof (slang_export_code_entry));
	if (self->entries == NULL)
		return NULL;
	slang_export_code_entry_ctr (&self->entries[n]);
	self->count++;
	return &self->entries[n];
}

/*
 * _slang_find_exported_data()
 */

#define EXTRACT_ERROR 0
#define EXTRACT_BASIC 1
#define EXTRACT_ARRAY 2
#define EXTRACT_STRUCT 3
#define EXTRACT_STRUCT_ARRAY 4

#define EXTRACT_MAXLEN 255

static GLuint extract_name (const char *name, char *parsed, GLuint *element, const char **end)
{
	GLuint i;

	if ((name[0] >= 'a' && name[0] <= 'z') || (name[0] >= 'A' && name[0] <= 'Z') || name[0] == '_')
	{
		parsed[0] = name[0];

		for (i = 1; i < EXTRACT_MAXLEN; i++)
		{
			if ((name[i] >= 'a' && name[i] <= 'z') || (name[i] >= 'A' && name[i] <= 'Z') ||
				(name[i] >= '0' && name[i] <= '9') || name[0] == '_')
			{
				parsed[i] = name[i];
			}
			else
			{
				if (name[i] == '\0')
				{
					parsed[i] = '\0';
					return EXTRACT_BASIC;
				}
				if (name[i] == '.')
				{
					parsed[i] = '\0';
					*end = &name[i + 1];
					return EXTRACT_STRUCT;
				}
				if (name[i] == '[')
				{
					parsed[i] = '\0';
					i++;
					if (name[i] >= '0' && name[i] <= '9')
					{
						*element = name[i] - '0';
						for (i++; ; i++)
						{
							if (name[i] >= '0' && name[i] <= '9')
								*element = *element * 10 + (name[i] - '0');
							else
							{
								if (name[i] == ']')
								{
									i++;
									if (name[i] == '.')
									{
										*end = &name[i + 1];
										return EXTRACT_STRUCT_ARRAY;
									}
									*end = &name[i];
									return EXTRACT_ARRAY;
								}
								break;
							}
						}
					}
				}
				break;
			}
		}
	}
	return EXTRACT_ERROR;
}

static GLboolean validate_extracted (slang_export_data_quant *q, GLuint element, GLuint extr)
{
	switch (extr)
	{
	case EXTRACT_BASIC:
		return GL_TRUE;
	case EXTRACT_ARRAY:
		return element < q->array_len;
	case EXTRACT_STRUCT:
		return q->structure != NULL;
	case EXTRACT_STRUCT_ARRAY:
		return q->structure != NULL && element < q->array_len;
	}
	return GL_FALSE;
}

static GLuint calculate_offset (slang_export_data_quant *q, GLuint element)
{
	if (q->array_len != 0)
		return element * q->size;
	return 0;
}

static GLboolean find_exported_data (slang_export_data_quant *q, const char *name,
	slang_export_data_quant **quant, GLuint *offset, slang_atom_pool *atoms)
{
	char parsed[EXTRACT_MAXLEN];
	GLuint result, element, i;
	const char *end;
	slang_atom atom;

	result = extract_name (name, parsed, &element, &end);
	if (result == EXTRACT_ERROR)
		return GL_FALSE;

	atom = slang_atom_pool_atom (atoms, parsed);
	if (atom == SLANG_ATOM_NULL)
		return GL_FALSE;

	for (i = 0; i < q->u.field_count; i++)
		if (q->structure[i].name == atom)
		{
			if (!validate_extracted (&q->structure[i], element, result))
				return GL_FALSE;
			*offset += calculate_offset (&q->structure[i], element);
			if (result == EXTRACT_BASIC || result == EXTRACT_ARRAY)
			{
				if (*end != '\0')
					return GL_FALSE;
				*quant = &q->structure[i];
				return GL_TRUE;
			}
			return find_exported_data (&q->structure[i], end, quant, offset, atoms);
		}
	return GL_FALSE;
}

GLboolean _slang_find_exported_data (slang_export_data_table *table, const char *name,
	slang_export_data_entry **entry, slang_export_data_quant **quant, GLuint *offset)
{
	char parsed[EXTRACT_MAXLEN];
	GLuint result, element, i;
	const char *end;
	slang_atom atom;

	result = extract_name (name, parsed, &element, &end);
	if (result == EXTRACT_ERROR)
		return GL_FALSE;

	atom = slang_atom_pool_atom (table->atoms, parsed);
	if (atom == SLANG_ATOM_NULL)
		return GL_FALSE;

	for (i = 0; i < table->count; i++)
		if (table->entries[i].quant.name == atom)
		{
			if (!validate_extracted (&table->entries[i].quant, element, result))
				return GL_FALSE;
			*entry = &table->entries[i];
			*offset = calculate_offset (&table->entries[i].quant, element);
			if (result == EXTRACT_BASIC || result == EXTRACT_ARRAY)
			{
				if (*end != '\0')
					return GL_FALSE;
				*quant = &table->entries[i].quant;
				return GL_TRUE;
			}
			return find_exported_data (&table->entries[i].quant, end, quant, offset, table->atoms);
		}
	return GL_FALSE;
}