Skip to content

Latest commit

 

History

History
7645 lines (6455 loc) · 246 KB

mojoshader.c

File metadata and controls

7645 lines (6455 loc) · 246 KB
 
Feb 10, 2008
Feb 10, 2008
1
/**
Mar 22, 2008
Mar 22, 2008
2
3
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
Feb 10, 2008
Feb 10, 2008
4
5
6
7
8
9
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
May 25, 2008
May 25, 2008
10
// !!! FIXME: this file really needs to be split up.
Mar 14, 2008
Mar 14, 2008
11
12
// !!! FIXME: I keep changing coding styles for symbols and typedefs.
Dec 2, 2008
Dec 2, 2008
13
14
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
Mar 27, 2008
Mar 27, 2008
15
Apr 19, 2008
Apr 19, 2008
16
Mar 11, 2008
Mar 11, 2008
17
// predeclare.
Mar 14, 2008
Mar 14, 2008
18
typedef struct Context Context;
Jun 29, 2008
Jun 29, 2008
19
struct ConstantsList;
Mar 11, 2008
Mar 11, 2008
20
21
// one emit function for each opcode in each profile.
Mar 14, 2008
Mar 14, 2008
22
typedef void (*emit_function)(Context *ctx);
Mar 11, 2008
Mar 11, 2008
23
24
// one emit function for starting output in each profile.
Jun 18, 2008
Jun 18, 2008
25
typedef void (*emit_start)(Context *ctx, const char *profilestr);
Mar 11, 2008
Mar 11, 2008
26
27
// one emit function for ending output in each profile.
Mar 14, 2008
Mar 14, 2008
28
typedef void (*emit_end)(Context *ctx);
Mar 11, 2008
Mar 11, 2008
29
Jun 26, 2008
Jun 26, 2008
30
31
32
// one emit function for phase opcode output in each profile.
typedef void (*emit_phase)(Context *ctx);
Apr 4, 2008
Apr 4, 2008
33
34
35
36
37
38
// one emit function for finalizing output in each profile.
typedef void (*emit_finalize)(Context *ctx);
// one emit function for global definitions in each profile.
typedef void (*emit_global)(Context *ctx, RegisterType regtype, int regnum);
May 5, 2008
May 5, 2008
39
// one emit function for relative uniform arrays in each profile.
Jun 27, 2008
Jun 27, 2008
40
typedef void (*emit_array)(Context *ctx, int base, int size);
May 5, 2008
May 5, 2008
41
Jun 29, 2008
Jun 29, 2008
42
43
44
45
46
// one emit function for relative constants arrays in each profile.
typedef void (*emit_const_array)(Context *ctx,
const struct ConstantsList *constslist,
int base, int size);
Apr 4, 2008
Apr 4, 2008
47
// one emit function for uniforms in each profile.
Jun 27, 2008
Jun 27, 2008
48
49
typedef void (*emit_uniform)(Context *ctx, RegisterType regtype, int regnum,
int arraybase, int arraysize);
Apr 4, 2008
Apr 4, 2008
50
Apr 19, 2008
Apr 19, 2008
51
52
53
// one emit function for samplers in each profile.
typedef void (*emit_sampler)(Context *ctx, int stage, TextureType ttype);
Apr 5, 2008
Apr 5, 2008
54
55
// one emit function for attributes in each profile.
typedef void (*emit_attribute)(Context *ctx, RegisterType regtype, int regnum,
Jul 7, 2008
Jul 7, 2008
56
57
MOJOSHADER_usage usage, int index, int wmask,
int flags);
Apr 5, 2008
Apr 5, 2008
58
Mar 16, 2008
Mar 16, 2008
59
60
61
// one args function for each possible sequence of opcode arguments.
typedef int (*args_function)(Context *ctx);
Mar 11, 2008
Mar 11, 2008
62
// one state function for each opcode where we have state machine updates.
Mar 27, 2008
Mar 27, 2008
63
typedef void (*state_function)(Context *ctx);
Mar 11, 2008
Mar 11, 2008
64
Jun 2, 2008
Jun 2, 2008
65
66
67
68
// one function for varnames in each profile.
typedef const char *(*varname_function)(Context *c, RegisterType t, int num);
// one function for const var array in each profile.
Jun 27, 2008
Jun 27, 2008
69
typedef const char *(*const_array_varname_function)(Context *c, int base, int size);
Jun 2, 2008
Jun 2, 2008
70
Mar 11, 2008
Mar 11, 2008
71
72
73
74
75
typedef struct
{
const char *name;
emit_start start_emitter;
emit_end end_emitter;
Jun 26, 2008
Jun 26, 2008
76
emit_phase phase_emitter;
Apr 4, 2008
Apr 4, 2008
77
emit_global global_emitter;
Jun 27, 2008
Jun 27, 2008
78
emit_array array_emitter;
Jun 29, 2008
Jun 29, 2008
79
emit_const_array const_array_emitter;
Apr 4, 2008
Apr 4, 2008
80
emit_uniform uniform_emitter;
Apr 19, 2008
Apr 19, 2008
81
emit_sampler sampler_emitter;
Apr 5, 2008
Apr 5, 2008
82
emit_attribute attribute_emitter;
Apr 4, 2008
Apr 4, 2008
83
emit_finalize finalize_emitter;
Jun 2, 2008
Jun 2, 2008
84
85
varname_function get_varname;
const_array_varname_function get_const_array_varname;
Mar 22, 2008
Mar 22, 2008
86
} Profile;
Mar 11, 2008
Mar 11, 2008
87
Mar 12, 2008
Mar 12, 2008
88
Mar 14, 2008
Mar 14, 2008
89
90
91
// A simple linked list of strings, so we can build the final output without
// realloc()ing for each new line, and easily insert lines into the middle
// of the output without much trouble.
Mar 28, 2008
Mar 28, 2008
92
typedef struct OutputListNode
Mar 14, 2008
Mar 14, 2008
93
94
{
char *str;
Mar 28, 2008
Mar 28, 2008
95
96
97
98
99
100
101
struct OutputListNode *next;
} OutputListNode;
typedef struct OutputList
{
OutputListNode head;
OutputListNode *tail;
Mar 14, 2008
Mar 14, 2008
102
103
} OutputList;
Jun 29, 2008
Jun 29, 2008
104
105
106
107
108
109
typedef struct ConstantsList
{
MOJOSHADER_constant constant;
struct ConstantsList *next;
} ConstantsList;
Jun 25, 2008
Jun 25, 2008
110
111
112
113
114
typedef struct VariableList
{
MOJOSHADER_uniformType type;
int index;
int count;
Jun 29, 2008
Jun 29, 2008
115
ConstantsList *constant;
Jun 27, 2008
Jun 27, 2008
116
int used;
Jun 25, 2008
Jun 25, 2008
117
118
struct VariableList *next;
} VariableList;
Mar 16, 2008
Mar 16, 2008
119
Apr 4, 2008
Apr 4, 2008
120
121
122
123
typedef struct RegisterList
{
RegisterType regtype;
int regnum;
Apr 5, 2008
Apr 5, 2008
124
125
126
MOJOSHADER_usage usage;
int index;
int writemask;
Apr 29, 2008
Apr 29, 2008
127
int misc;
Jun 27, 2008
Jun 27, 2008
128
const VariableList *array;
Apr 4, 2008
Apr 4, 2008
129
130
131
struct RegisterList *next;
} RegisterList;
Dec 7, 2008
Dec 7, 2008
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
typedef struct
{
const uint32 *token; // this is the unmolested token in the stream.
int regnum;
int swizzle; // xyzw (all four, not split out).
int swizzle_x;
int swizzle_y;
int swizzle_z;
int swizzle_w;
SourceMod src_mod;
RegisterType regtype;
int relative;
RegisterType relative_regtype;
int relative_regnum;
int relative_component;
const VariableList *relative_array;
} SourceArgInfo;
Apr 2, 2008
Apr 2, 2008
149
Apr 18, 2008
Apr 18, 2008
150
#define SCRATCH_BUFFER_SIZE 128
May 30, 2008
May 30, 2008
151
#define SCRATCH_BUFFERS 32
Mar 9, 2008
Mar 9, 2008
152
Apr 4, 2008
Apr 4, 2008
153
// !!! FIXME: the scratch buffers make Context pretty big.
Apr 2, 2008
Apr 2, 2008
154
155
// !!! FIXME: might be worth having one set of static scratch buffers that
// !!! FIXME: are mutex protected?
Apr 4, 2008
Apr 4, 2008
156
Mar 9, 2008
Mar 9, 2008
157
// Context...this is state that changes as we parse through a shader...
Mar 14, 2008
Mar 14, 2008
158
struct Context
Mar 9, 2008
Mar 9, 2008
159
{
Feb 3, 2009
Feb 3, 2009
160
161
int isfail;
int out_of_memory;
Mar 22, 2008
Mar 22, 2008
162
163
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
Apr 4, 2008
Apr 4, 2008
164
void *malloc_data;
Dec 10, 2008
Dec 10, 2008
165
const uint32 *orig_tokens;
Mar 9, 2008
Mar 9, 2008
166
167
const uint32 *tokens;
uint32 tokencount;
Dec 19, 2008
Dec 19, 2008
168
MOJOSHADER_parsePhase parse_phase;
Aug 26, 2008
Aug 26, 2008
169
170
const MOJOSHADER_swizzle *swizzles;
unsigned int swizzles_count;
Mar 28, 2008
Mar 28, 2008
171
172
OutputList *output;
OutputList globals;
Apr 2, 2008
Apr 2, 2008
173
OutputList helpers;
Mar 28, 2008
Mar 28, 2008
174
OutputList subroutines;
Apr 24, 2008
Apr 24, 2008
175
OutputList mainline_intro;
Mar 28, 2008
Mar 28, 2008
176
OutputList mainline;
Apr 2, 2008
Apr 2, 2008
177
178
OutputList ignore;
OutputList *output_stack[2];
Apr 6, 2008
Apr 6, 2008
179
uint8 *output_bytes; // can be used instead of the OutputLists.
Apr 2, 2008
Apr 2, 2008
180
181
int indent_stack[2];
int output_stack_len;
Mar 28, 2008
Mar 28, 2008
182
int output_len; // total strlen; prevents walking the lists just to malloc.
Mar 24, 2008
Mar 24, 2008
183
int indent;
May 28, 2008
May 28, 2008
184
const char *shader_type_str;
Mar 14, 2008
Mar 14, 2008
185
186
const char *endline;
int endline_len;
Mar 22, 2008
Mar 22, 2008
187
char scratch[SCRATCH_BUFFERS][SCRATCH_BUFFER_SIZE];
Mar 14, 2008
Mar 14, 2008
188
189
int scratchidx; // current scratch buffer.
int profileid;
Mar 22, 2008
Mar 22, 2008
190
const Profile *profile;
Apr 4, 2008
Apr 4, 2008
191
MOJOSHADER_shaderType shader_type;
Mar 28, 2008
Mar 28, 2008
192
193
uint8 major_ver;
uint8 minor_ver;
Apr 21, 2008
Apr 21, 2008
194
DestArgInfo dest_arg;
Apr 2, 2008
Apr 2, 2008
195
SourceArgInfo source_args[5];
Apr 18, 2008
Apr 18, 2008
196
SourceArgInfo predicate_arg; // for predicated instructions.
Mar 21, 2008
Mar 21, 2008
197
uint32 dwords[4];
Jun 25, 2008
Jun 25, 2008
198
uint32 version_token;
Mar 28, 2008
Mar 28, 2008
199
int instruction_count;
Mar 17, 2008
Mar 17, 2008
200
uint32 instruction_controls;
Apr 2, 2008
Apr 2, 2008
201
202
uint32 previous_opcode;
int loops;
Apr 6, 2008
Apr 6, 2008
203
int reps;
Jun 20, 2008
Jun 20, 2008
204
int max_reps;
Apr 6, 2008
Apr 6, 2008
205
int cmps;
May 26, 2008
May 26, 2008
206
207
int scratch_registers;
int max_scratch_registers;
Jun 20, 2008
Jun 20, 2008
208
209
210
int branch_labels_stack_index;
int branch_labels_stack[32];
int assigned_branch_labels;
May 27, 2008
May 27, 2008
211
int assigned_vertex_attributes;
May 29, 2008
May 29, 2008
212
int last_address_reg_component;
Apr 4, 2008
Apr 4, 2008
213
214
RegisterList used_registers;
RegisterList defined_registers;
Feb 3, 2009
Feb 3, 2009
215
216
int error_count;
ErrorList *errors;
May 3, 2008
May 3, 2008
217
218
int constant_count;
ConstantsList *constants;
Apr 4, 2008
Apr 4, 2008
219
220
int uniform_count;
RegisterList uniforms;
Apr 5, 2008
Apr 5, 2008
221
222
int attribute_count;
RegisterList attributes;
Apr 19, 2008
Apr 19, 2008
223
224
int sampler_count;
RegisterList samplers;
Jun 25, 2008
Jun 25, 2008
225
VariableList *variables; // variables to register mapping.
Dec 2, 2008
Dec 2, 2008
226
227
int centroid_allowed;
int have_ctab;
Feb 1, 2009
Feb 1, 2009
228
int have_relative_input_registers;
Dec 2, 2008
Dec 2, 2008
229
230
231
232
233
234
235
int determined_constants_arrays;
int predicated;
int support_nv2;
int support_nv3;
int support_nv4;
int support_glsl120;
int glsl_generated_lit_opcode;
Mar 11, 2008
Mar 11, 2008
236
237
238
};
Apr 4, 2008
Apr 4, 2008
239
// Convenience functions for allocators...
Feb 13, 2009
Feb 13, 2009
240
#if !MOJOSHADER_FORCE_ALLOCATOR
Feb 7, 2009
Feb 7, 2009
241
242
void *MOJOSHADER_internal_malloc(int bytes, void *d) { return malloc(bytes); }
void MOJOSHADER_internal_free(void *ptr, void *d) { free(ptr); }
Feb 13, 2009
Feb 13, 2009
243
#endif
Apr 4, 2008
Apr 4, 2008
244
Feb 3, 2009
Feb 3, 2009
245
246
247
248
MOJOSHADER_error MOJOSHADER_out_of_mem_error = { "Out of memory", NULL, -1 };
MOJOSHADER_parseData MOJOSHADER_out_of_mem_data = {
1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0,
MOJOSHADER_TYPE_UNKNOWN, 0, 0, 0, 0
Apr 25, 2008
Apr 25, 2008
249
250
};
Feb 3, 2009
Feb 3, 2009
251
static inline void out_of_memory(Context *ctx)
Apr 4, 2008
Apr 4, 2008
252
{
Feb 3, 2009
Feb 3, 2009
253
ctx->isfail = ctx->out_of_memory = 1;
Apr 25, 2008
Apr 25, 2008
254
255
} // out_of_memory
May 10, 2008
May 10, 2008
256
static inline void *Malloc(Context *ctx, const size_t len)
Apr 25, 2008
Apr 25, 2008
257
{
May 10, 2008
May 10, 2008
258
void *retval = ctx->malloc((int) len, ctx->malloc_data);
Apr 25, 2008
Apr 25, 2008
259
260
261
if (retval == NULL)
out_of_memory(ctx);
return retval;
Apr 4, 2008
Apr 4, 2008
262
263
} // Malloc
Feb 3, 2009
Feb 3, 2009
264
265
266
static inline char *StrDup(Context *ctx, const char *str)
{
char *retval = (char *) Malloc(ctx, strlen(str) + 1);
Feb 7, 2009
Feb 7, 2009
267
if (retval != NULL)
Feb 3, 2009
Feb 3, 2009
268
269
270
strcpy(retval, str);
return retval;
} // StrDup
Apr 4, 2008
Apr 4, 2008
271
Apr 25, 2008
Apr 25, 2008
272
static inline void Free(Context *ctx, void *ptr)
Apr 4, 2008
Apr 4, 2008
273
{
Apr 12, 2008
Apr 12, 2008
274
if (ptr != NULL) // check for NULL in case of dumb free() impl.
Apr 4, 2008
Apr 4, 2008
275
ctx->free(ptr, ctx->malloc_data);
Apr 4, 2008
Apr 4, 2008
276
277
278
} // Free
Apr 2, 2008
Apr 2, 2008
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// jump between output sections in the context...
static inline void push_output(Context *ctx, OutputList *section)
{
assert(ctx->output_stack_len < STATICARRAYLEN(ctx->output_stack));
ctx->output_stack[ctx->output_stack_len] = ctx->output;
ctx->indent_stack[ctx->output_stack_len] = ctx->indent;
ctx->output_stack_len++;
ctx->output = section;
ctx->indent = 0;
} // push_output
static inline void pop_output(Context *ctx)
{
assert(ctx->output_stack_len > 0);
ctx->output_stack_len--;
ctx->output = ctx->output_stack[ctx->output_stack_len];
ctx->indent = ctx->indent_stack[ctx->output_stack_len];
} // pop_output
// Shader model version magic...
Mar 27, 2008
Mar 27, 2008
303
304
static inline uint32 ver_ui32(const uint8 major, const uint8 minor)
{
Dec 10, 2008
Dec 10, 2008
305
return ( (((uint32) major) << 16) | (((minor) == 0xFF) ? 1 : (minor)) );
Mar 27, 2008
Mar 27, 2008
306
307
} // version_ui32
Apr 19, 2008
Apr 19, 2008
308
static inline int shader_version_supported(const uint8 maj, const uint8 min)
Mar 27, 2008
Mar 27, 2008
309
310
311
312
{
return (ver_ui32(maj,min) <= ver_ui32(MAX_SHADER_MAJOR, MAX_SHADER_MINOR));
} // shader_version_supported
Apr 19, 2008
Apr 19, 2008
313
314
static inline int shader_version_atleast(const Context *ctx, const uint8 maj,
const uint8 min)
Mar 27, 2008
Mar 27, 2008
315
316
317
318
{
return (ver_ui32(ctx->major_ver, ctx->minor_ver) >= ver_ui32(maj, min));
} // shader_version_atleast
Jun 26, 2008
Jun 26, 2008
319
320
321
322
323
324
static inline int shader_version_exactly(const Context *ctx, const uint8 maj,
const uint8 min)
{
return ((ctx->major_ver == maj) && (ctx->minor_ver == min));
} // shader_version_exactly
Apr 19, 2008
Apr 19, 2008
325
326
327
328
329
330
331
332
333
334
static inline int shader_is_pixel(const Context *ctx)
{
return (ctx->shader_type == MOJOSHADER_TYPE_PIXEL);
} // shader_is_pixel
static inline int shader_is_vertex(const Context *ctx)
{
return (ctx->shader_type == MOJOSHADER_TYPE_VERTEX);
} // shader_is_vertex
Mar 27, 2008
Mar 27, 2008
335
May 30, 2008
May 30, 2008
336
337
static inline int isfail(const Context *ctx)
{
Feb 3, 2009
Feb 3, 2009
338
return ctx->isfail;
May 30, 2008
May 30, 2008
339
340
} // isfail
Apr 2, 2008
Apr 2, 2008
341
Mar 14, 2008
Mar 14, 2008
342
static inline char *get_scratch_buffer(Context *ctx)
Mar 9, 2008
Mar 9, 2008
343
{
Feb 3, 2009
Feb 3, 2009
344
assert(ctx->scratchidx < SCRATCH_BUFFERS);
Mar 22, 2008
Mar 22, 2008
345
ctx->scratchidx = (ctx->scratchidx + 1) % SCRATCH_BUFFERS;
Mar 14, 2008
Mar 14, 2008
346
return ctx->scratch[ctx->scratchidx];
Mar 9, 2008
Mar 9, 2008
347
348
} // get_scratch_buffer
Feb 10, 2008
Feb 10, 2008
349
Feb 3, 2009
Feb 3, 2009
350
351
static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static void failf(Context *ctx, const char *fmt, ...)
Feb 10, 2008
Feb 10, 2008
352
{
Feb 3, 2009
Feb 3, 2009
353
354
ctx->isfail = 1;
if (ctx->out_of_memory)
Feb 3, 2009
Feb 3, 2009
355
return;
Feb 3, 2009
Feb 3, 2009
356
357
358
int error_position = 0;
switch (ctx->parse_phase)
Mar 9, 2008
Mar 9, 2008
359
{
Feb 3, 2009
Feb 3, 2009
360
361
362
363
364
365
366
367
368
369
370
case MOJOSHADER_PARSEPHASE_NOTSTARTED:
error_position = -2;
break;
case MOJOSHADER_PARSEPHASE_WORKING:
error_position = (ctx->tokens - ctx->orig_tokens) * sizeof (uint32);
break;
case MOJOSHADER_PARSEPHASE_DONE:
error_position = -1;
break;
default:
assert(0 && "Unexpected value");
Feb 3, 2009
Feb 3, 2009
371
return;
Feb 3, 2009
Feb 3, 2009
372
373
374
375
} // switch
ErrorList *error = (ErrorList *) Malloc(ctx, sizeof (ErrorList));
if (error == NULL)
Feb 3, 2009
Feb 3, 2009
376
return;
Feb 3, 2009
Feb 3, 2009
377
378
379
380
381
382
char *scratch = get_scratch_buffer(ctx);
va_list ap;
va_start(ap, fmt);
const int len = vsnprintf(scratch, SCRATCH_BUFFER_SIZE, fmt, ap);
va_end(ap);
Mar 9, 2008
Mar 9, 2008
383
Feb 3, 2009
Feb 3, 2009
384
385
386
387
388
389
390
391
392
char *failstr = (char *) Malloc(ctx, len + 1);
if (failstr == NULL)
Free(ctx, error);
else
{
// see comments about scratch buffer overflow in output_line().
if (len < SCRATCH_BUFFER_SIZE)
strcpy(failstr, scratch); // copy it over.
else
Mar 14, 2008
Mar 14, 2008
393
{
Feb 3, 2009
Feb 3, 2009
394
395
396
397
398
399
400
401
va_start(ap, fmt);
vsnprintf(failstr, len + 1, fmt, ap); // rebuild it.
va_end(ap);
} // else
error->error.error = failstr;
error->error.filename = NULL; // no filename at this level.
error->error.error_position = error_position;
Feb 3, 2009
Feb 3, 2009
402
error->next = NULL;
Feb 3, 2009
Feb 3, 2009
403
404
ErrorList *prev = NULL;
Feb 3, 2009
Feb 3, 2009
405
406
ErrorList *item = ctx->errors;
while (item != NULL)
Feb 3, 2009
Feb 3, 2009
407
{
Feb 3, 2009
Feb 3, 2009
408
prev = item;
Feb 10, 2009
Feb 10, 2009
409
item = item->next;
Feb 3, 2009
Feb 3, 2009
410
411
} // while
Feb 3, 2009
Feb 3, 2009
412
if (prev == NULL)
Feb 3, 2009
Feb 3, 2009
413
ctx->errors = error;
Feb 3, 2009
Feb 3, 2009
414
415
else
prev->next = error;
Feb 3, 2009
Feb 3, 2009
416
417
418
ctx->error_count++;
} // else
Mar 9, 2008
Mar 9, 2008
419
420
421
} // failf
Feb 3, 2009
Feb 3, 2009
422
static inline void fail(Context *ctx, const char *reason)
Mar 9, 2008
Mar 9, 2008
423
{
Feb 3, 2009
Feb 3, 2009
424
failf(ctx, "%s", reason);
Mar 9, 2008
Mar 9, 2008
425
426
427
} // fail
Feb 3, 2009
Feb 3, 2009
428
429
static void output_line(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static void output_line(Context *ctx, const char *fmt, ...)
Mar 9, 2008
Mar 9, 2008
430
{
Mar 28, 2008
Mar 28, 2008
431
432
OutputListNode *item = NULL;
Feb 3, 2009
Feb 3, 2009
433
434
if (isfail(ctx) || ctx->out_of_memory)
return; // we failed previously, don't go on...
Mar 9, 2008
Mar 9, 2008
435
Mar 14, 2008
Mar 14, 2008
436
char *scratch = get_scratch_buffer(ctx);
Mar 24, 2008
Mar 24, 2008
437
438
const int indent = ctx->indent;
Mar 27, 2008
Mar 27, 2008
439
440
if (indent > 0)
memset(scratch, '\t', indent);
Apr 2, 2008
Apr 2, 2008
441
Mar 9, 2008
Mar 9, 2008
442
443
va_list ap;
va_start(ap, fmt);
Mar 24, 2008
Mar 24, 2008
444
const int len = vsnprintf(scratch+indent, SCRATCH_BUFFER_SIZE-indent, fmt, ap) + indent;
Mar 9, 2008
Mar 9, 2008
445
446
va_end(ap);
Apr 4, 2008
Apr 4, 2008
447
item = (OutputListNode *) Malloc(ctx, sizeof (OutputListNode));
Apr 2, 2008
Apr 2, 2008
448
if (item == NULL)
Feb 3, 2009
Feb 3, 2009
449
return;
Apr 2, 2008
Apr 2, 2008
450
Apr 4, 2008
Apr 4, 2008
451
item->str = (char *) Malloc(ctx, len + 1);
Mar 14, 2008
Mar 14, 2008
452
if (item->str == NULL)
Mar 9, 2008
Mar 9, 2008
453
{
Apr 4, 2008
Apr 4, 2008
454
Free(ctx, item);
Feb 3, 2009
Feb 3, 2009
455
return;
Mar 9, 2008
Mar 9, 2008
456
457
} // if
Mar 14, 2008
Mar 14, 2008
458
459
460
// If we overflowed our scratch buffer, that's okay. We were going to
// allocate anyhow...the scratch buffer just lets us avoid a second
// run of vsnprintf().
Mar 22, 2008
Mar 22, 2008
461
if (len < SCRATCH_BUFFER_SIZE)
Mar 14, 2008
Mar 14, 2008
462
463
464
strcpy(item->str, scratch); // copy it over.
else
{
Mar 27, 2008
Mar 27, 2008
465
466
if (indent > 0)
memset(item->str, '\t', indent);
Mar 14, 2008
Mar 14, 2008
467
va_start(ap, fmt);
Mar 24, 2008
Mar 24, 2008
468
vsnprintf(item->str+indent, len + 1, fmt, ap); // rebuild it.
Mar 14, 2008
Mar 14, 2008
469
470
va_end(ap);
} // else
Mar 9, 2008
Mar 9, 2008
471
Mar 14, 2008
Mar 14, 2008
472
item->next = NULL;
Mar 28, 2008
Mar 28, 2008
473
474
475
ctx->output->tail->next = item;
ctx->output->tail = item;
Mar 14, 2008
Mar 14, 2008
476
ctx->output_len += len + ctx->endline_len;
Mar 9, 2008
Mar 9, 2008
477
478
479
} // output_line
Apr 2, 2008
Apr 2, 2008
480
// this is just to stop gcc whining.
Feb 3, 2009
Feb 3, 2009
481
static inline void output_blank_line(Context *ctx)
Apr 2, 2008
Apr 2, 2008
482
{
Feb 3, 2009
Feb 3, 2009
483
output_line(ctx, "%s", "");
Apr 2, 2008
Apr 2, 2008
484
485
486
} // output_blank_line
Mar 27, 2008
Mar 27, 2008
487
// !!! FIXME: this is sort of nasty.
Apr 3, 2008
Apr 3, 2008
488
489
static void floatstr(Context *ctx, char *buf, size_t bufsize, float f,
int leavedecimal)
Mar 27, 2008
Mar 27, 2008
490
491
{
const size_t len = snprintf(buf, bufsize, "%f", f);
Apr 3, 2008
Apr 3, 2008
492
if ((len+2) >= bufsize)
Mar 27, 2008
Mar 27, 2008
493
494
495
496
497
498
fail(ctx, "BUG: internal buffer is too small");
else
{
char *end = buf + len;
char *ptr = strchr(buf, '.');
if (ptr == NULL)
Apr 3, 2008
Apr 3, 2008
499
500
501
{
if (leavedecimal)
strcat(buf, ".0");
Mar 27, 2008
Mar 27, 2008
502
return; // done.
Apr 3, 2008
Apr 3, 2008
503
} // if
Mar 27, 2008
Mar 27, 2008
504
505
506
507
508
509
510
511
512
while (--end != ptr)
{
if (*end != '0')
{
end++;
break;
} // if
} // while
Apr 3, 2008
Apr 3, 2008
513
514
if ((leavedecimal) && (end == ptr))
end += 2;
Mar 27, 2008
Mar 27, 2008
515
516
517
518
*end = '\0'; // chop extra '0' or all decimal places off.
} // else
} // floatstr
Mar 12, 2008
Mar 12, 2008
519
Apr 4, 2008
Apr 4, 2008
520
521
// Deal with register lists... !!! FIXME: I sort of hate this.
Apr 4, 2008
Apr 4, 2008
522
static void free_reglist(MOJOSHADER_free f, void *d, RegisterList *item)
Apr 4, 2008
Apr 4, 2008
523
524
525
526
{
while (item != NULL)
{
RegisterList *next = item->next;
Apr 4, 2008
Apr 4, 2008
527
f(item, d);
Apr 4, 2008
Apr 4, 2008
528
529
530
531
532
533
534
535
536
item = next;
} // while
} // free_reglist
static inline uint32 reg_to_ui32(const RegisterType regtype, const int regnum)
{
return ( ((uint32) regtype) | (((uint32) regnum) << 16) );
} // reg_to_uint32
Apr 5, 2008
Apr 5, 2008
537
538
539
static RegisterList *reglist_insert(Context *ctx, RegisterList *prev,
const RegisterType regtype,
const int regnum)
Apr 4, 2008
Apr 4, 2008
540
541
542
543
544
545
546
{
const uint32 newval = reg_to_ui32(regtype, regnum);
RegisterList *item = prev->next;
while (item != NULL)
{
const uint32 val = reg_to_ui32(item->regtype, item->regnum);
if (newval == val)
Apr 5, 2008
Apr 5, 2008
547
return item; // already set, so we're done.
Apr 4, 2008
Apr 4, 2008
548
549
550
551
552
553
554
555
556
557
558
else if (newval < val) // insert it here.
break;
else // if (newval > val)
{
// keep going, we're not to the insertion point yet.
prev = item;
item = item->next;
} // else
} // while
// we need to insert an entry after (prev).
Apr 4, 2008
Apr 4, 2008
559
item = (RegisterList *) Malloc(ctx, sizeof (RegisterList));
Apr 25, 2008
Apr 25, 2008
560
if (item != NULL)
Apr 4, 2008
Apr 4, 2008
561
562
563
{
item->regtype = regtype;
item->regnum = regnum;
Apr 29, 2008
Apr 29, 2008
564
item->usage = MOJOSHADER_USAGE_UNKNOWN;
Apr 5, 2008
Apr 5, 2008
565
566
item->index = 0;
item->writemask = 0;
Apr 29, 2008
Apr 29, 2008
567
item->misc = 0;
Jun 27, 2008
Jun 27, 2008
568
item->array = NULL;
Apr 4, 2008
Apr 4, 2008
569
570
item->next = prev->next;
prev->next = item;
Apr 25, 2008
Apr 25, 2008
571
} // if
Apr 5, 2008
Apr 5, 2008
572
573
return item;
Apr 4, 2008
Apr 4, 2008
574
575
} // reglist_insert
Aug 26, 2008
Aug 26, 2008
576
577
static RegisterList *reglist_find(const RegisterList *prev,
const RegisterType rtype, const int regnum)
Apr 4, 2008
Apr 4, 2008
578
{
Apr 6, 2008
Apr 6, 2008
579
580
const uint32 newval = reg_to_ui32(rtype, regnum);
RegisterList *item = prev->next;
Apr 4, 2008
Apr 4, 2008
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
while (item != NULL)
{
const uint32 val = reg_to_ui32(item->regtype, item->regnum);
if (newval == val)
return item; // here it is.
else if (newval < val) // should have been here if it existed.
return NULL;
else // if (newval > val)
{
// keep going, we're not to the insertion point yet.
prev = item;
item = item->next;
} // else
} // while
return NULL; // wasn't in the list.
Apr 6, 2008
Apr 6, 2008
597
598
599
600
601
602
603
} // reglist_find
static inline const RegisterList *reglist_exists(RegisterList *prev,
const RegisterType regtype,
const int regnum)
{
return (reglist_find(prev, regtype, regnum));
Apr 4, 2008
Apr 4, 2008
604
605
606
607
608
609
610
611
612
613
614
615
} // reglist_exists
static inline void set_used_register(Context *ctx, const RegisterType regtype,
const int regnum)
{
reglist_insert(ctx, &ctx->used_registers, regtype, regnum);
} // set_used_register
static inline int get_used_register(Context *ctx, const RegisterType regtype,
const int regnum)
{
return (reglist_exists(&ctx->used_registers, regtype, regnum) != NULL);
Apr 4, 2008
Apr 4, 2008
616
} // get_used_register
Apr 4, 2008
Apr 4, 2008
617
618
619
620
621
622
623
624
625
626
627
628
629
static inline void set_defined_register(Context *ctx, const RegisterType rtype,
const int regnum)
{
reglist_insert(ctx, &ctx->defined_registers, rtype, regnum);
} // set_defined_register
static inline int get_defined_register(Context *ctx, const RegisterType rtype,
const int regnum)
{
return (reglist_exists(&ctx->defined_registers, rtype, regnum) != NULL);
} // get_defined_register
Apr 5, 2008
Apr 5, 2008
630
631
static void add_attribute_register(Context *ctx, const RegisterType rtype,
const int regnum, const MOJOSHADER_usage usage,
Jul 7, 2008
Jul 7, 2008
632
const int index, const int writemask, int flags)
Apr 5, 2008
Apr 5, 2008
633
634
635
636
637
{
RegisterList *item = reglist_insert(ctx, &ctx->attributes, rtype, regnum);
item->usage = usage;
item->index = index;
item->writemask = writemask;
Jul 7, 2008
Jul 7, 2008
638
item->misc = flags;
Apr 5, 2008
Apr 5, 2008
639
640
} // add_attribute_register
Apr 19, 2008
Apr 19, 2008
641
642
643
static inline void add_sampler(Context *ctx, const RegisterType rtype,
const int regnum, const TextureType ttype)
{
May 8, 2008
May 8, 2008
644
// !!! FIXME: make sure it doesn't exist?
Apr 19, 2008
Apr 19, 2008
645
646
647
648
RegisterList *item = reglist_insert(ctx, &ctx->samplers, rtype, regnum);
item->index = (int) ttype;
} // add_sampler
Apr 4, 2008
Apr 4, 2008
649
May 8, 2008
May 8, 2008
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
static inline int writemask_xyzw(const int writemask)
{
return (writemask == 0xF); // 0xF == 1111. No explicit mask (full!).
} // writemask_xyzw
static inline int writemask_xyz(const int writemask)
{
return (writemask == 0x7); // 0x7 == 0111. (that is: xyz)
} // writemask_xyz
static inline int writemask_xy(const int writemask)
{
return (writemask == 0x3); // 0x3 == 0011. (that is: xy)
} // writemask_xy
static inline int writemask_x(const int writemask)
{
return (writemask == 0x1); // 0x1 == 0001. (that is: x)
} // writemask_x
static inline int writemask_y(const int writemask)
{
return (writemask == 0x2); // 0x1 == 0010. (that is: y)
} // writemask_y
Apr 6, 2008
Apr 6, 2008
680
681
682
683
684
685
686
687
static inline int replicate_swizzle(const int swizzle)
{
return ( (((swizzle >> 0) & 0x3) == ((swizzle >> 2) & 0x3)) &&
(((swizzle >> 2) & 0x3) == ((swizzle >> 4) & 0x3)) &&
(((swizzle >> 4) & 0x3) == ((swizzle >> 6) & 0x3)) );
} // replicate_swizzle
May 8, 2008
May 8, 2008
688
689
static inline int no_swizzle(const int swizzle)
{
May 8, 2008
May 8, 2008
690
return (swizzle == 0xE4); // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
May 8, 2008
May 8, 2008
691
692
693
} // no_swizzle
Apr 21, 2008
Apr 21, 2008
694
695
696
697
698
static inline int vecsize_from_writemask(const int m)
{
return (m & 1) + ((m >> 1) & 1) + ((m >> 2) & 1) + ((m >> 3) & 1);
} // vecsize_from_writemask
May 26, 2008
May 26, 2008
699
700
701
702
703
704
705
706
static int allocate_scratch_register(Context *ctx)
{
const int retval = ctx->scratch_registers++;
if (retval >= ctx->max_scratch_registers)
ctx->max_scratch_registers = retval + 1;
return retval;
} // allocate_scratch_register
Jun 20, 2008
Jun 20, 2008
707
static int allocate_branch_label(Context *ctx)
Jun 19, 2008
Jun 19, 2008
708
{
Jun 20, 2008
Jun 20, 2008
709
710
return ctx->assigned_branch_labels++;
} // allocate_branch_label
Jun 19, 2008
Jun 19, 2008
711
Apr 6, 2008
Apr 6, 2008
712
Apr 5, 2008
Apr 5, 2008
713
714
// D3D stuff that's used in more than just the d3d profile...
Apr 18, 2008
Apr 18, 2008
715
716
717
static const char swizzle_channels[] = { 'x', 'y', 'z', 'w' };
Apr 5, 2008
Apr 5, 2008
718
719
720
721
722
static const char *usagestrs[] = {
"_position", "_blendweight", "_blendindices", "_normal", "_psize",
"_texcoord", "_tangent", "_binormal", "_tessfactor", "_positiont",
"_color", "_fog", "_depth", "_sample"
};
Apr 4, 2008
Apr 4, 2008
723
Mar 17, 2008
Mar 17, 2008
724
static const char *get_D3D_register_string(Context *ctx,
Mar 22, 2008
Mar 22, 2008
725
RegisterType regtype,
Mar 17, 2008
Mar 17, 2008
726
727
int regnum, char *regnum_str,
size_t regnum_size)
Mar 12, 2008
Mar 12, 2008
728
{
Mar 17, 2008
Mar 17, 2008
729
const char *retval = NULL;
Mar 21, 2008
Mar 21, 2008
730
int has_number = 1;
Mar 16, 2008
Mar 16, 2008
731
Mar 17, 2008
Mar 17, 2008
732
switch (regtype)
Mar 16, 2008
Mar 16, 2008
733
{
Apr 3, 2008
Apr 3, 2008
734
case REG_TYPE_TEMP:
Mar 17, 2008
Mar 17, 2008
735
retval = "r";
Mar 16, 2008
Mar 16, 2008
736
737
break;
Apr 3, 2008
Apr 3, 2008
738
case REG_TYPE_INPUT:
Mar 17, 2008
Mar 17, 2008
739
retval = "v";
Mar 16, 2008
Mar 16, 2008
740
741
break;
Apr 3, 2008
Apr 3, 2008
742
case REG_TYPE_CONST:
Mar 21, 2008
Mar 21, 2008
743
744
745
retval = "c";
break;
Apr 3, 2008
Apr 3, 2008
746
case REG_TYPE_ADDRESS: // (or REG_TYPE_TEXTURE, same value.)
Apr 19, 2008
Apr 19, 2008
747
retval = shader_is_vertex(ctx) ? "a" : "t";
Mar 16, 2008
Mar 16, 2008
748
749
break;
Apr 3, 2008
Apr 3, 2008
750
case REG_TYPE_RASTOUT:
Mar 22, 2008
Mar 22, 2008
751
switch ((RastOutType) regnum)
Mar 16, 2008
Mar 16, 2008
752
{
Mar 17, 2008
Mar 17, 2008
753
754
755
case RASTOUT_TYPE_POSITION: retval = "oPos"; break;
case RASTOUT_TYPE_FOG: retval = "oFog"; break;
case RASTOUT_TYPE_POINT_SIZE: retval = "oPts"; break;
Mar 16, 2008
Mar 16, 2008
756
} // switch
Mar 21, 2008
Mar 21, 2008
757
has_number = 0;
Mar 16, 2008
Mar 16, 2008
758
759
break;
Apr 3, 2008
Apr 3, 2008
760
case REG_TYPE_ATTROUT:
Mar 17, 2008
Mar 17, 2008
761
retval = "oD";
Mar 16, 2008
Mar 16, 2008
762
763
break;
Apr 3, 2008
Apr 3, 2008
764
case REG_TYPE_OUTPUT: // (or REG_TYPE_TEXCRDOUT, same value.)
Apr 19, 2008
Apr 19, 2008
765
if (shader_is_vertex(ctx) && shader_version_atleast(ctx, 3, 0))
Mar 17, 2008
Mar 17, 2008
766
retval = "o";
Mar 16, 2008
Mar 16, 2008
767
else
Mar 17, 2008
Mar 17, 2008
768
retval = "oT";
Mar 16, 2008
Mar 16, 2008
769
770
break;
Apr 3, 2008
Apr 3, 2008
771
case REG_TYPE_CONSTINT:
Mar 17, 2008
Mar 17, 2008
772
retval = "i";
Mar 16, 2008
Mar 16, 2008
773
774
break;
Apr 3, 2008
Apr 3, 2008
775
case REG_TYPE_COLOROUT:
Mar 17, 2008
Mar 17, 2008
776
retval = "oC";
Mar 16, 2008
Mar 16, 2008
777
778
break;
Apr 3, 2008
Apr 3, 2008
779
case REG_TYPE_DEPTHOUT:
Mar 17, 2008
Mar 17, 2008
780
retval = "oDepth";
Mar 21, 2008
Mar 21, 2008
781
has_number = 0;
Mar 16, 2008
Mar 16, 2008
782
783
break;
Apr 3, 2008
Apr 3, 2008
784
case REG_TYPE_SAMPLER:
Mar 17, 2008
Mar 17, 2008
785
retval = "s";
Mar 16, 2008
Mar 16, 2008
786
787
break;
Apr 3, 2008
Apr 3, 2008
788
case REG_TYPE_CONSTBOOL:
Mar 17, 2008
Mar 17, 2008
789
retval = "b";
Mar 16, 2008
Mar 16, 2008
790
791
break;
Apr 3, 2008
Apr 3, 2008
792
case REG_TYPE_LOOP:
Mar 17, 2008
Mar 17, 2008
793
retval = "aL";
Mar 21, 2008
Mar 21, 2008
794
has_number = 0;
Mar 16, 2008
Mar 16, 2008
795
796
break;
Apr 3, 2008
Apr 3, 2008
797
case REG_TYPE_MISCTYPE:
May 12, 2008
May 12, 2008
798
switch ((const MiscTypeType) regnum)
Mar 16, 2008
Mar 16, 2008
799
{
Mar 17, 2008
Mar 17, 2008
800
801
case MISCTYPE_TYPE_POSITION: retval = "vPos"; break;
case MISCTYPE_TYPE_FACE: retval = "vFace"; break;
Mar 16, 2008
Mar 16, 2008
802
} // switch
Mar 21, 2008
Mar 21, 2008
803
has_number = 0;
Mar 16, 2008
Mar 16, 2008
804
805
break;
Apr 3, 2008
Apr 3, 2008
806
case REG_TYPE_LABEL:
Mar 17, 2008
Mar 17, 2008
807
retval = "l";
Mar 16, 2008
Mar 16, 2008
808
809
break;
Apr 3, 2008
Apr 3, 2008
810
case REG_TYPE_PREDICATE:
Mar 17, 2008
Mar 17, 2008
811
retval = "p";
Mar 16, 2008
Mar 16, 2008
812
break;
Apr 28, 2008
Apr 28, 2008
813
814
815
816
817
818
819
//case REG_TYPE_TEMPFLOAT16: // !!! FIXME: don't know this asm string
default:
fail(ctx, "unknown register type");
retval = "???";
has_number = 0;
break;
Mar 16, 2008
Mar 16, 2008
820
821
} // switch
Mar 21, 2008
Mar 21, 2008
822
823
824
825
826
if (has_number)
snprintf(regnum_str, regnum_size, "%u", (uint) regnum);
else
regnum_str[0] = '\0';
Mar 17, 2008
Mar 17, 2008
827
828
829
830
return retval;
} // get_D3D_register_string
Apr 3, 2008
Apr 3, 2008
831
832
833
834
835
836
837
838
839
#define AT_LEAST_ONE_PROFILE 0
#if !SUPPORT_PROFILE_D3D
#define PROFILE_EMITTER_D3D(op)
#else
#undef AT_LEAST_ONE_PROFILE
#define AT_LEAST_ONE_PROFILE 1
#define PROFILE_EMITTER_D3D(op) emit_D3D_##op,
Apr 21, 2008
Apr 21, 2008
840
static const char *make_D3D_srcarg_string_in_buf(Context *ctx,
Apr 18, 2008
Apr 18, 2008
841
842
const SourceArgInfo *arg,
char *buf, size_t buflen)
Mar 17, 2008
Mar 17, 2008
843
844
845
{
const char *premod_str = "";
const char *postmod_str = "";
Apr 18, 2008
Apr 18, 2008
846
switch (arg->src_mod)
Mar 17, 2008
Mar 17, 2008
847
848
849
850
851
852
853
854
855
856
857
{
case SRCMOD_NEGATE:
premod_str = "-";
break;
case SRCMOD_BIASNEGATE:
premod_str = "-";
// fall through.
case SRCMOD_BIAS:
postmod_str = "_bias";
break;
Mar 12, 2008
Mar 12, 2008
858
Mar 17, 2008
Mar 17, 2008
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
case SRCMOD_SIGNNEGATE:
premod_str = "-";
// fall through.
case SRCMOD_SIGN:
postmod_str = "_bx2";
break;
case SRCMOD_COMPLEMENT:
premod_str = "1-";
break;
case SRCMOD_X2NEGATE:
premod_str = "-";
// fall through.
case SRCMOD_X2:
postmod_str = "_x2";
break;
case SRCMOD_DZ:
postmod_str = "_dz";
break;
case SRCMOD_DW:
postmod_str = "_dw";
break;
case SRCMOD_ABSNEGATE:
premod_str = "-";
// fall through.
case SRCMOD_ABS:
postmod_str = "_abs";
break;
case SRCMOD_NOT:
premod_str = "!";
break;
Mar 28, 2008
Mar 28, 2008
895
896
897
898
case SRCMOD_NONE:
case SRCMOD_TOTAL:
break; // stop compiler whining.
Mar 17, 2008
Mar 17, 2008
899
900
901
902
} // switch
char regnum_str[16];
Apr 2, 2008
Apr 2, 2008
903
904
905
const char *regtype_str = get_D3D_register_string(ctx, arg->regtype,
arg->regnum, regnum_str,
sizeof (regnum_str));
Mar 17, 2008
Mar 17, 2008
906
907
908
909
if (regtype_str == NULL)
{
fail(ctx, "Unknown source register type.");
Apr 3, 2008
Apr 3, 2008
910
return "";
Mar 17, 2008
Mar 17, 2008
911
912
} // if
Apr 19, 2008
Apr 19, 2008
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
const char *rel_lbracket = "";
const char *rel_rbracket = "";
char rel_swizzle[4] = { '\0' };
char rel_regnum_str[16] = { '\0' };
const char *rel_regtype_str = "";
if (arg->relative)
{
rel_swizzle[0] = '.';
rel_swizzle[1] = swizzle_channels[arg->relative_component];
rel_swizzle[2] = '\0';
rel_lbracket = "[";
rel_rbracket = "]";
rel_regtype_str = get_D3D_register_string(ctx, arg->relative_regtype,
arg->relative_regnum,
rel_regnum_str,
sizeof (rel_regnum_str));
if (regtype_str == NULL)
{
fail(ctx, "Unknown relative source register type.");
return "";
} // if
} // if
Mar 17, 2008
Mar 17, 2008
937
938
char swizzle_str[6];
int i = 0;
Dec 10, 2008
Dec 10, 2008
939
const int scalar = scalar_register(ctx->shader_type, arg->regtype, arg->regnum);
May 12, 2008
May 12, 2008
940
if (!scalar && !no_swizzle(arg->swizzle))
Mar 17, 2008
Mar 17, 2008
941
942
{
swizzle_str[i++] = '.';
Apr 18, 2008
Apr 18, 2008
943
944
945
946
swizzle_str[i++] = swizzle_channels[arg->swizzle_x];
swizzle_str[i++] = swizzle_channels[arg->swizzle_y];
swizzle_str[i++] = swizzle_channels[arg->swizzle_z];
swizzle_str[i++] = swizzle_channels[arg->swizzle_w];
Mar 17, 2008
Mar 17, 2008
947
948
949
950
// .xyzz is the same as .xyz, .z is the same as .zzzz, etc.
while (swizzle_str[i-1] == swizzle_str[i-2])
i--;
Mar 17, 2008
Mar 17, 2008
951
952
953
954
} // if
swizzle_str[i] = '\0';
assert(i < sizeof (swizzle_str));
Dec 10, 2008
Dec 10, 2008
955
// !!! FIXME: c12[a0.x] actually needs to be c[a0.x + 12]
Apr 19, 2008
Apr 19, 2008
956
957
958
959
snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s",
premod_str, regtype_str, regnum_str, postmod_str,
rel_lbracket, rel_regtype_str, rel_regnum_str, rel_swizzle,
rel_rbracket, swizzle_str);
Apr 3, 2008
Apr 3, 2008
960
// !!! FIXME: make sure the scratch buffer was large enough.
Apr 18, 2008
Apr 18, 2008
961
return buf;
Apr 21, 2008
Apr 21, 2008
962
} // make_D3D_srcarg_string_in_buf
Apr 18, 2008
Apr 18, 2008
963
964
Apr 21, 2008
Apr 21, 2008
965
static const char *make_D3D_destarg_string(Context *ctx)
Apr 18, 2008
Apr 18, 2008
966
{
Apr 21, 2008
Apr 21, 2008
967
const DestArgInfo *arg = &ctx->dest_arg;
Apr 18, 2008
Apr 18, 2008
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
const char *result_shift_str = "";
switch (arg->result_shift)
{
case 0x1: result_shift_str = "_x2"; break;
case 0x2: result_shift_str = "_x4"; break;
case 0x3: result_shift_str = "_x8"; break;
case 0xD: result_shift_str = "_d8"; break;
case 0xE: result_shift_str = "_d4"; break;
case 0xF: result_shift_str = "_d2"; break;
} // switch
const char *sat_str = (arg->result_mod & MOD_SATURATE) ? "_sat" : "";
const char *pp_str = (arg->result_mod & MOD_PP) ? "_pp" : "";
const char *cent_str = (arg->result_mod & MOD_CENTROID) ? "_centroid" : "";
char regnum_str[16];
const char *regtype_str = get_D3D_register_string(ctx, arg->regtype,
arg->regnum, regnum_str,
sizeof (regnum_str));
if (regtype_str == NULL)
{
fail(ctx, "Unknown destination register type.");
return "";
} // if
char writemask_str[6];
int i = 0;
Dec 10, 2008
Dec 10, 2008
996
const int scalar = scalar_register(ctx->shader_type, arg->regtype, arg->regnum);
May 12, 2008
May 12, 2008
997
if (!scalar && !writemask_xyzw(arg->writemask))
Apr 18, 2008
Apr 18, 2008
998
999
1000
{
writemask_str[i++] = '.';
if (arg->writemask0) writemask_str[i++] = 'x';