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
|
/*
* Mesa 3-D graphics library
* Version: 6.5
*
* Copyright (C) 2005-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_assemble_constructor.c
* slang constructor and vector swizzle assembler
* \author Michal Krol
*/
#include "imports.h"
#include "slang_utility.h"
#include "slang_assemble_constructor.h"
#include "slang_assemble_typeinfo.h"
#include "slang_storage.h"
/* _slang_is_swizzle() */
int _slang_is_swizzle (const char *field, unsigned int rows, slang_swizzle *swz)
{
unsigned int i;
int xyzw = 0, rgba = 0, stpq = 0;
/* the swizzle can be at most 4-component long */
swz->num_components = slang_string_length (field);
if (swz->num_components > 4)
return 0;
for (i = 0; i < swz->num_components; i++)
{
/* mark which swizzle group is used */
switch (field[i])
{
case 'x':
case 'y':
case 'z':
case 'w':
xyzw = 1;
break;
case 'r':
case 'g':
case 'b':
case 'a':
rgba = 1;
break;
case 's':
case 't':
case 'p':
case 'q':
stpq = 1;
break;
default:
return 0;
}
/* collect swizzle component */
switch (field[i])
{
case 'x':
case 'r':
case 's':
swz->swizzle[i] = 0;
break;
case 'y':
case 'g':
case 't':
swz->swizzle[i] = 1;
break;
case 'z':
case 'b':
case 'p':
swz->swizzle[i] = 2;
break;
case 'w':
case 'a':
case 'q':
swz->swizzle[i] = 3;
break;
}
/* check if the component is valid for given vector's row count */
if (rows <= swz->swizzle[i])
return 0;
}
/* only one swizzle group can be used */
if ((xyzw && rgba) || (xyzw && stpq) || (rgba && stpq))
return 0;
return 1;
}
/* _slang_is_swizzle_mask() */
int _slang_is_swizzle_mask (const slang_swizzle *swz, unsigned int rows)
{
unsigned int i, c = 0;
/* the swizzle may not be longer than the vector dim */
if (swz->num_components > rows)
return 0;
/* the swizzle components cannot be duplicated */
for (i = 0; i < swz->num_components; i++)
{
if ((c & (1 << swz->swizzle[i])) != 0)
return 0;
c |= 1 << swz->swizzle[i];
}
return 1;
}
/* _slang_multiply_swizzles() */
void _slang_multiply_swizzles (slang_swizzle *dst, const slang_swizzle *left,
const slang_swizzle *right)
{
unsigned int i;
dst->num_components = right->num_components;
for (i = 0; i < right->num_components; i++)
dst->swizzle[i] = left->swizzle[right->swizzle[i]];
}
/* _slang_assemble_constructor() */
/*
static int constructor_aggregate (slang_assembly_file *file, const slang_storage_aggregate *flat,
unsigned int *index, slang_operation *op, unsigned int size, slang_assembly_flow_control *flow,
slang_assembly_name_space *space, slang_assembly_local_info *info)
{
slang_assembly_typeinfo ti;
int result;
slang_storage_aggregate agg, flat_agg;
slang_assembly_stack_info stk;
unsigned int i;
slang_assembly_typeinfo_construct (&ti);
if (!(result = _slang_typeof_operation (op, space, &ti)))
goto end1;
slang_storage_aggregate_construct (&agg);
if (!(result = _slang_aggregate_variable (&agg, &ti.spec, NULL, space->funcs, space->structs,
space->vars)))
goto end2;
slang_storage_aggregate_construct (&flat_agg);
if (!(result = _slang_flatten_aggregate (&flat_agg, &agg)))
goto end;
if (!(result = _slang_assemble_operation (file, op, 0, flow, space, info, &stk)))
goto end;
for (i = 0; i < flat_agg.count; i++)
{
const slang_storage_array *arr1 = flat_agg.arrays + i;
const slang_storage_array *arr2 = flat->arrays + *index;
if (arr1->type != arr2->type)
{
/* TODO: convert (generic) from arr1 to arr2 */
/* }
(*index)++;
/* TODO: watch the index, if it reaches the size, pop off the stack subsequent values */
/* }
result = 1;
end:
slang_storage_aggregate_destruct (&flat_agg);
end2:
slang_storage_aggregate_destruct (&agg);
end1:
slang_assembly_typeinfo_destruct (&ti);
return result;
}
/* XXX: general swizzle! */
/*int _slang_assemble_constructor (slang_assembly_file *file, slang_operation *op,
slang_assembly_flow_control *flow, slang_assembly_name_space *space,
slang_assembly_local_info *info, struct slang_machine_ *pmach)
{
slang_assembly_typeinfo ti;
int result;
slang_storage_aggregate agg, flat;
unsigned int size, index, i;
if (!slang_assembly_typeinfo_construct (&ti))
return 0;
if (!(result = _slang_typeof_operation (op, space, &ti)))
goto end1;
if (!(result = slang_storage_aggregate_construct (&agg)))
goto end1;
if (!(result = _slang_aggregate_variable (&agg, &ti.spec, NULL, space->funcs, space->structs,
space->vars, pmach, file)))
goto end2;
size = _slang_sizeof_aggregate (&agg);
if (!(result = slang_storage_aggregate_construct (&flat)))
goto end2;
if (!(result = _slang_flatten_aggregate (&flat, &agg)))
goto end;
index = 0;
for (i = 0; i < op->num_children; i++)
{
/* if (!(result = constructor_aggregate (file, &flat, &index, op->children + i, size, flow,
space, info)))
goto end;
/* TODO: watch the index, if it reaches the size, raise an error */
/* }
result = 1;
end:
slang_storage_aggregate_destruct (&flat);
end2:
slang_storage_aggregate_destruct (&agg);
end1:
slang_assembly_typeinfo_destruct (&ti);
return result;
}
/* _slang_assemble_constructor_from_swizzle() */
int _slang_assemble_constructor_from_swizzle (slang_assembly_file *file, const slang_swizzle *swz,
slang_type_specifier *spec, slang_type_specifier *master_spec, slang_assembly_local_info *info)
{
unsigned int master_rows, i;
master_rows = _slang_type_dim (master_spec->type);
for (i = 0; i < master_rows; i++)
{
switch (_slang_type_base (master_spec->type))
{
case slang_spec_bool:
if (!slang_assembly_file_push_label2 (file, slang_asm_bool_copy,
(master_rows - i) * 4, i * 4))
return 0;
break;
case slang_spec_int:
if (!slang_assembly_file_push_label2 (file, slang_asm_int_copy,
(master_rows - i) * 4, i * 4))
return 0;
break;
case slang_spec_float:
if (!slang_assembly_file_push_label2 (file, slang_asm_float_copy,
(master_rows - i) * 4, i * 4))
return 0;
break;
default:
break;
}
}
if (!slang_assembly_file_push_label (file, slang_asm_local_free, 4))
return 0;
for (i = swz->num_components; i > 0; i--)
{
unsigned int n = i - 1;
if (!slang_assembly_file_push_label2 (file, slang_asm_local_addr, info->swizzle_tmp, 16))
return 0;
if (!slang_assembly_file_push_label (file, slang_asm_addr_push, swz->swizzle[n] * 4))
return 0;
if (!slang_assembly_file_push (file, slang_asm_addr_add))
return 0;
switch (_slang_type_base (master_spec->type))
{
case slang_spec_bool:
if (!slang_assembly_file_push (file, slang_asm_bool_deref))
return 0;
break;
case slang_spec_int:
if (!slang_assembly_file_push (file, slang_asm_int_deref))
return 0;
break;
case slang_spec_float:
if (!slang_assembly_file_push (file, slang_asm_float_deref))
return 0;
break;
default:
break;
}
}
return 1;
}
|