Skip to content

Latest commit

 

History

History
4006 lines (3403 loc) · 134 KB

mojoshader.c

File metadata and controls

4006 lines (3403 loc) · 134 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.
Apr 15, 2012
Apr 15, 2012
13
14
15
16
17
// !!! FIXME: rules from MSDN about temp registers we probably don't check.
// - There are limited temporaries: vs_1_1 has 12 (ps_1_1 has _2_!).
// - SM2 apparently was variable, between 12 and 32. Shader Model 3 has 32.
// - A maximum of three temp registers can be used in a single instruction.
Dec 2, 2008
Dec 2, 2008
18
#define __MOJOSHADER_INTERNAL__ 1
Apr 23, 2019
Apr 23, 2019
19
#include "profiles/mojoshader_profile.h"
Mar 12, 2008
Mar 12, 2008
20
Apr 4, 2008
Apr 4, 2008
21
22
// Deal with register lists... !!! FIXME: I sort of hate this.
Apr 4, 2008
Apr 4, 2008
23
static void free_reglist(MOJOSHADER_free f, void *d, RegisterList *item)
Apr 4, 2008
Apr 4, 2008
24
25
26
27
{
while (item != NULL)
{
RegisterList *next = item->next;
Apr 4, 2008
Apr 4, 2008
28
f(item, d);
Apr 4, 2008
Apr 4, 2008
29
30
31
32
item = next;
} // while
} // free_reglist
Apr 6, 2008
Apr 6, 2008
33
34
35
36
37
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
38
39
} // reglist_exists
Apr 15, 2012
Apr 15, 2012
40
41
42
43
44
45
46
static inline int register_was_written(Context *ctx, const RegisterType rtype,
const int regnum)
{
RegisterList *reg = reglist_find(&ctx->used_registers, rtype, regnum);
return (reg && reg->written);
} // register_was_written
Apr 4, 2008
Apr 4, 2008
47
48
49
50
51
52
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
53
54
static void add_attribute_register(Context *ctx, const RegisterType rtype,
const int regnum, const MOJOSHADER_usage usage,
Jul 7, 2008
Jul 7, 2008
55
const int index, const int writemask, int flags)
Apr 5, 2008
Apr 5, 2008
56
57
58
59
60
{
RegisterList *item = reglist_insert(ctx, &ctx->attributes, rtype, regnum);
item->usage = usage;
item->index = index;
item->writemask = writemask;
Jul 7, 2008
Jul 7, 2008
61
item->misc = flags;
Jun 20, 2011
Jun 20, 2011
62
63
64
if ((rtype == REG_TYPE_OUTPUT) && (usage == MOJOSHADER_USAGE_POINTSIZE))
ctx->uses_pointsize = 1; // note that we have to check this later.
Apr 13, 2012
Apr 13, 2012
65
66
else if ((rtype == REG_TYPE_OUTPUT) && (usage == MOJOSHADER_USAGE_FOG))
ctx->uses_fog = 1; // note that we have to check this later.
Apr 5, 2008
Apr 5, 2008
67
68
} // add_attribute_register
Apr 23, 2019
Apr 23, 2019
69
70
71
72
73
74
75
76
77
78
static inline TextureType cvtMojoToD3DSamplerType(const MOJOSHADER_samplerType type)
{
return (TextureType) (((int) type) + 2);
} // cvtMojoToD3DSamplerType
static inline MOJOSHADER_samplerType cvtD3DToMojoSamplerType(const TextureType type)
{
return (MOJOSHADER_samplerType) (((int) type) - 2);
} // cvtD3DToMojoSamplerType
May 29, 2012
May 29, 2012
79
80
static inline void add_sampler(Context *ctx, const int regnum,
TextureType ttype, const int texbem)
Apr 19, 2008
Apr 19, 2008
81
{
May 29, 2012
May 29, 2012
82
83
const RegisterType rtype = REG_TYPE_SAMPLER;
May 8, 2008
May 8, 2008
84
// !!! FIXME: make sure it doesn't exist?
Apr 15, 2012
Apr 15, 2012
85
// !!! FIXME: (ps_1_1 assume we can add it multiple times...)
Apr 19, 2008
Apr 19, 2008
86
RegisterList *item = reglist_insert(ctx, &ctx->samplers, rtype, regnum);
May 29, 2012
May 29, 2012
87
88
89
90
91
92
93
94
95
96
97
98
99
100
if (ctx->samplermap != NULL)
{
unsigned int i;
for (i = 0; i < ctx->samplermap_count; i++)
{
if (ctx->samplermap[i].index == regnum)
{
ttype = cvtMojoToD3DSamplerType(ctx->samplermap[i].type);
break;
} // if
} // for
} // if
Apr 19, 2008
Apr 19, 2008
101
item->index = (int) ttype;
Apr 17, 2012
Apr 17, 2012
102
item->misc |= texbem;
Apr 19, 2008
Apr 19, 2008
103
104
} // add_sampler
Nov 11, 2010
Nov 11, 2010
105
106
107
108
109
110
111
static inline void adjust_token_position(Context *ctx, const int incr)
{
ctx->tokens += incr;
ctx->tokencount -= incr;
ctx->current_position += incr * sizeof (uint32);
} // adjust_token_position
Apr 23, 2019
Apr 23, 2019
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
// Generate emitter declarations for each profile with this macro...
#define PREDECLARE_PROFILE(prof) \
void emit_##prof##_start(Context *ctx, const char *profilestr); \
void emit_##prof##_end(Context *ctx); \
void emit_##prof##_phase(Context *ctx); \
void emit_##prof##_finalize(Context *ctx); \
void emit_##prof##_global(Context *ctx, RegisterType regtype, int regnum);\
void emit_##prof##_array(Context *ctx, VariableList *var); \
void emit_##prof##_const_array(Context *ctx, const ConstantsList *clist, \
int base, int size); \
void emit_##prof##_uniform(Context *ctx, RegisterType regtype, int regnum,\
const VariableList *var); \
void emit_##prof##_sampler(Context *ctx, int stage, TextureType ttype, \
int tb); \
void emit_##prof##_attribute(Context *ctx, RegisterType regtype, \
int regnum, MOJOSHADER_usage usage, \
int index, int wmask, int flags); \
void emit_##prof##_NOP(Context *ctx); \
void emit_##prof##_MOV(Context *ctx); \
void emit_##prof##_ADD(Context *ctx); \
void emit_##prof##_SUB(Context *ctx); \
void emit_##prof##_MAD(Context *ctx); \
void emit_##prof##_MUL(Context *ctx); \
void emit_##prof##_RCP(Context *ctx); \
void emit_##prof##_RSQ(Context *ctx); \
void emit_##prof##_DP3(Context *ctx); \
void emit_##prof##_DP4(Context *ctx); \
void emit_##prof##_MIN(Context *ctx); \
void emit_##prof##_MAX(Context *ctx); \
void emit_##prof##_SLT(Context *ctx); \
void emit_##prof##_SGE(Context *ctx); \
void emit_##prof##_EXP(Context *ctx); \
void emit_##prof##_LOG(Context *ctx); \
void emit_##prof##_LIT(Context *ctx); \
void emit_##prof##_DST(Context *ctx); \
void emit_##prof##_LRP(Context *ctx); \
void emit_##prof##_FRC(Context *ctx); \
void emit_##prof##_M4X4(Context *ctx); \
void emit_##prof##_M4X3(Context *ctx); \
void emit_##prof##_M3X4(Context *ctx); \
void emit_##prof##_M3X3(Context *ctx); \
void emit_##prof##_M3X2(Context *ctx); \
void emit_##prof##_CALL(Context *ctx); \
void emit_##prof##_CALLNZ(Context *ctx); \
void emit_##prof##_LOOP(Context *ctx); \
void emit_##prof##_ENDLOOP(Context *ctx); \
void emit_##prof##_LABEL(Context *ctx); \
void emit_##prof##_DCL(Context *ctx); \
void emit_##prof##_POW(Context *ctx); \
void emit_##prof##_CRS(Context *ctx); \
void emit_##prof##_SGN(Context *ctx); \
void emit_##prof##_ABS(Context *ctx); \
void emit_##prof##_NRM(Context *ctx); \
void emit_##prof##_SINCOS(Context *ctx); \
void emit_##prof##_REP(Context *ctx); \
void emit_##prof##_ENDREP(Context *ctx); \
void emit_##prof##_IF(Context *ctx); \
void emit_##prof##_IFC(Context *ctx); \
void emit_##prof##_ELSE(Context *ctx); \
void emit_##prof##_ENDIF(Context *ctx); \
void emit_##prof##_BREAK(Context *ctx); \
void emit_##prof##_BREAKC(Context *ctx); \
void emit_##prof##_MOVA(Context *ctx); \
void emit_##prof##_DEFB(Context *ctx); \
void emit_##prof##_DEFI(Context *ctx); \
void emit_##prof##_TEXCRD(Context *ctx); \
void emit_##prof##_TEXKILL(Context *ctx); \
void emit_##prof##_TEXLD(Context *ctx); \
void emit_##prof##_TEXBEM(Context *ctx); \
void emit_##prof##_TEXBEML(Context *ctx); \
void emit_##prof##_TEXREG2AR(Context *ctx); \
void emit_##prof##_TEXREG2GB(Context *ctx); \
void emit_##prof##_TEXM3X2PAD(Context *ctx); \
void emit_##prof##_TEXM3X2TEX(Context *ctx); \
void emit_##prof##_TEXM3X3PAD(Context *ctx); \
void emit_##prof##_TEXM3X3TEX(Context *ctx); \
void emit_##prof##_TEXM3X3SPEC(Context *ctx); \
void emit_##prof##_TEXM3X3VSPEC(Context *ctx); \
void emit_##prof##_EXPP(Context *ctx); \
void emit_##prof##_LOGP(Context *ctx); \
void emit_##prof##_CND(Context *ctx); \
void emit_##prof##_DEF(Context *ctx); \
void emit_##prof##_TEXREG2RGB(Context *ctx); \
void emit_##prof##_TEXDP3TEX(Context *ctx); \
void emit_##prof##_TEXM3X2DEPTH(Context *ctx); \
void emit_##prof##_TEXDP3(Context *ctx); \
void emit_##prof##_TEXM3X3(Context *ctx); \
void emit_##prof##_TEXDEPTH(Context *ctx); \
void emit_##prof##_CMP(Context *ctx); \
void emit_##prof##_BEM(Context *ctx); \
void emit_##prof##_DP2ADD(Context *ctx); \
void emit_##prof##_DSX(Context *ctx); \
void emit_##prof##_DSY(Context *ctx); \
void emit_##prof##_TEXLDD(Context *ctx); \
void emit_##prof##_SETP(Context *ctx); \
void emit_##prof##_TEXLDL(Context *ctx); \
void emit_##prof##_BREAKP(Context *ctx); \
void emit_##prof##_RESERVED(Context *ctx); \
void emit_##prof##_RET(Context *ctx); \
const char *get_##prof##_varname(Context *ctx, RegisterType rt, \
int regnum); \
const char *get_##prof##_const_array_varname(Context *ctx, \
int base, int size);
// Check for profile support...
Nov 4, 2010
Nov 4, 2010
218
Apr 3, 2008
Apr 3, 2008
219
220
#define AT_LEAST_ONE_PROFILE 0
Apr 23, 2019
Apr 23, 2019
221
222
223
224
225
226
227
228
229
#if !SUPPORT_PROFILE_BYTECODE
#define PROFILE_EMITTER_BYTECODE(op)
#else
#undef AT_LEAST_ONE_PROFILE
#define AT_LEAST_ONE_PROFILE 1
#define PROFILE_EMITTER_BYTECODE(op) emit_BYTECODE_##op,
PREDECLARE_PROFILE(BYTECODE)
#endif
Apr 3, 2008
Apr 3, 2008
230
231
232
233
234
235
#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 23, 2019
Apr 23, 2019
236
237
PREDECLARE_PROFILE(D3D)
#endif
Apr 3, 2008
Apr 3, 2008
238
May 21, 2020
May 21, 2020
239
240
241
242
243
244
245
246
247
#if !SUPPORT_PROFILE_HLSL
#define PROFILE_EMITTER_HLSL(op)
#else
#undef AT_LEAST_ONE_PROFILE
#define AT_LEAST_ONE_PROFILE 1
#define PROFILE_EMITTER_HLSL(op) emit_HLSL_##op,
PREDECLARE_PROFILE(HLSL)
#endif
Apr 23, 2019
Apr 23, 2019
248
249
250
251
252
253
254
255
#if !SUPPORT_PROFILE_GLSL
#define PROFILE_EMITTER_GLSL(op)
#else
#undef AT_LEAST_ONE_PROFILE
#define AT_LEAST_ONE_PROFILE 1
#define PROFILE_EMITTER_GLSL(op) emit_GLSL_##op,
PREDECLARE_PROFILE(GLSL)
#endif
Mar 12, 2008
Mar 12, 2008
256
Apr 23, 2019
Apr 23, 2019
257
258
259
260
261
262
263
264
#if !SUPPORT_PROFILE_METAL
#define PROFILE_EMITTER_METAL(op)
#else
#undef AT_LEAST_ONE_PROFILE
#define AT_LEAST_ONE_PROFILE 1
#define PROFILE_EMITTER_METAL(op) emit_METAL_##op,
PREDECLARE_PROFILE(METAL)
#endif
May 25, 2008
May 25, 2008
265
Apr 23, 2019
Apr 23, 2019
266
267
268
269
270
271
272
273
#if !SUPPORT_PROFILE_ARB1
#define PROFILE_EMITTER_ARB1(op)
#else
#undef AT_LEAST_ONE_PROFILE
#define AT_LEAST_ONE_PROFILE 1
#define PROFILE_EMITTER_ARB1(op) emit_ARB1_##op,
PREDECLARE_PROFILE(ARB1)
#endif
May 25, 2008
May 25, 2008
274
Dec 31, 2019
Dec 31, 2019
275
276
277
278
279
280
281
282
283
#if !SUPPORT_PROFILE_SPIRV
#define PROFILE_EMITTER_SPIRV(op)
#else
#undef AT_LEAST_ONE_PROFILE
#define AT_LEAST_ONE_PROFILE 1
#define PROFILE_EMITTER_SPIRV(op) emit_SPIRV_##op,
PREDECLARE_PROFILE(SPIRV)
#endif
Mar 11, 2008
Mar 11, 2008
284
285
286
287
#if !AT_LEAST_ONE_PROFILE
#error No profiles are supported. Fix your build.
#endif
Apr 4, 2008
Apr 4, 2008
288
289
290
291
#define DEFINE_PROFILE(prof) { \
MOJOSHADER_PROFILE_##prof, \
emit_##prof##_start, \
emit_##prof##_end, \
Jun 26, 2008
Jun 26, 2008
292
emit_##prof##_phase, \
Apr 4, 2008
Apr 4, 2008
293
emit_##prof##_global, \
Jun 27, 2008
Jun 27, 2008
294
emit_##prof##_array, \
Jun 29, 2008
Jun 29, 2008
295
emit_##prof##_const_array, \
Apr 4, 2008
Apr 4, 2008
296
emit_##prof##_uniform, \
Apr 19, 2008
Apr 19, 2008
297
emit_##prof##_sampler, \
Apr 5, 2008
Apr 5, 2008
298
emit_##prof##_attribute, \
Apr 4, 2008
Apr 4, 2008
299
emit_##prof##_finalize, \
Jun 2, 2008
Jun 2, 2008
300
301
get_##prof##_varname, \
get_##prof##_const_array_varname, \
Apr 4, 2008
Apr 4, 2008
302
303
},
Mar 22, 2008
Mar 22, 2008
304
static const Profile profiles[] =
Mar 11, 2008
Mar 11, 2008
305
306
{
#if SUPPORT_PROFILE_D3D
Apr 4, 2008
Apr 4, 2008
307
DEFINE_PROFILE(D3D)
Mar 11, 2008
Mar 11, 2008
308
#endif
Dec 7, 2008
Dec 7, 2008
309
310
#if SUPPORT_PROFILE_BYTECODE
DEFINE_PROFILE(BYTECODE)
Apr 6, 2008
Apr 6, 2008
311
#endif
May 21, 2020
May 21, 2020
312
313
314
#if SUPPORT_PROFILE_HLSL
DEFINE_PROFILE(HLSL)
#endif
Mar 11, 2008
Mar 11, 2008
315
#if SUPPORT_PROFILE_GLSL
Apr 4, 2008
Apr 4, 2008
316
DEFINE_PROFILE(GLSL)
Mar 11, 2008
Mar 11, 2008
317
#endif
May 25, 2008
May 25, 2008
318
319
320
#if SUPPORT_PROFILE_ARB1
DEFINE_PROFILE(ARB1)
#endif
Apr 25, 2016
Apr 25, 2016
321
322
323
#if SUPPORT_PROFILE_METAL
DEFINE_PROFILE(METAL)
#endif
Dec 31, 2019
Dec 31, 2019
324
325
326
#if SUPPORT_PROFILE_SPIRV
DEFINE_PROFILE(SPIRV)
#endif
Mar 11, 2008
Mar 11, 2008
327
328
};
Apr 4, 2008
Apr 4, 2008
329
330
#undef DEFINE_PROFILE
Jun 18, 2008
Jun 18, 2008
331
332
333
// This is for profiles that extend other profiles...
static const struct { const char *from; const char *to; } profileMap[] =
{
Dec 31, 2019
Dec 31, 2019
334
{ MOJOSHADER_PROFILE_GLSPIRV, MOJOSHADER_PROFILE_SPIRV },
Jan 1, 2016
Jan 1, 2016
335
{ MOJOSHADER_PROFILE_GLSLES, MOJOSHADER_PROFILE_GLSL },
Nov 11, 2023
Nov 11, 2023
336
{ MOJOSHADER_PROFILE_GLSLES3, MOJOSHADER_PROFILE_GLSL },
Jun 29, 2008
Jun 29, 2008
337
{ MOJOSHADER_PROFILE_GLSL120, MOJOSHADER_PROFILE_GLSL },
Jun 18, 2008
Jun 18, 2008
338
{ MOJOSHADER_PROFILE_NV2, MOJOSHADER_PROFILE_ARB1 },
Jul 7, 2008
Jul 7, 2008
339
{ MOJOSHADER_PROFILE_NV3, MOJOSHADER_PROFILE_ARB1 },
Jul 7, 2008
Jul 7, 2008
340
{ MOJOSHADER_PROFILE_NV4, MOJOSHADER_PROFILE_ARB1 },
Jun 18, 2008
Jun 18, 2008
341
342
};
Mar 11, 2008
Mar 11, 2008
343
344
345
// The PROFILE_EMITTER_* items MUST be in the same order as profiles[]!
#define PROFILE_EMITTERS(op) { \
PROFILE_EMITTER_D3D(op) \
Dec 7, 2008
Dec 7, 2008
346
PROFILE_EMITTER_BYTECODE(op) \
May 21, 2020
May 21, 2020
347
PROFILE_EMITTER_HLSL(op) \
Mar 11, 2008
Mar 11, 2008
348
PROFILE_EMITTER_GLSL(op) \
May 25, 2008
May 25, 2008
349
PROFILE_EMITTER_ARB1(op) \
Apr 25, 2016
Apr 25, 2016
350
PROFILE_EMITTER_METAL(op) \
Dec 31, 2019
Dec 31, 2019
351
PROFILE_EMITTER_SPIRV(op) \
Mar 11, 2008
Mar 11, 2008
352
}
Mar 11, 2008
Mar 11, 2008
353
Mar 16, 2008
Mar 16, 2008
354
355
static int parse_destination_token(Context *ctx, DestArgInfo *info)
{
Mar 21, 2008
Mar 21, 2008
356
// !!! FIXME: recheck against the spec for ranges (like RASTOUT values, etc).
Mar 16, 2008
Mar 16, 2008
357
if (ctx->tokencount == 0)
Feb 3, 2009
Feb 3, 2009
358
359
360
361
{
fail(ctx, "Out of tokens in destination parameter");
return 0;
} // if
Mar 16, 2008
Mar 16, 2008
362
363
364
365
366
const uint32 token = SWAP32(*(ctx->tokens));
const int reserved1 = (int) ((token >> 14) & 0x3); // bits 14 through 15
const int reserved2 = (int) ((token >> 31) & 0x1); // bit 31
Mar 16, 2008
Mar 16, 2008
367
info->token = ctx->tokens;
Mar 16, 2008
Mar 16, 2008
368
369
info->regnum = (int) (token & 0x7ff); // bits 0 through 10
info->relative = (int) ((token >> 13) & 0x1); // bit 13
May 12, 2008
May 12, 2008
370
info->orig_writemask = (int) ((token >> 16) & 0xF); // bits 16 through 19
Mar 16, 2008
Mar 16, 2008
371
info->result_mod = (int) ((token >> 20) & 0xF); // bits 20 through 23
Apr 2, 2008
Apr 2, 2008
372
373
info->result_shift = (int) ((token >> 24) & 0xF); // bits 24 through 27 abc
info->regtype = (RegisterType) (((token >> 28) & 0x7) | ((token >> 8) & 0x18)); // bits 28-30, 11-12
Mar 16, 2008
Mar 16, 2008
374
May 12, 2008
May 12, 2008
375
int writemask;
Jun 20, 2011
Jun 20, 2011
376
if (isscalar(ctx, ctx->shader_type, info->regtype, info->regnum))
May 12, 2008
May 12, 2008
377
378
379
380
writemask = 0x1; // just x.
else
writemask = info->orig_writemask;
Apr 13, 2012
Apr 13, 2012
381
set_dstarg_writemask(info, writemask); // bits 16 through 19.
May 12, 2008
May 12, 2008
382
Apr 28, 2008
Apr 28, 2008
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// all the REG_TYPE_CONSTx types are the same register type, it's just
// split up so its regnum can be > 2047 in the bytecode. Clean it up.
if (info->regtype == REG_TYPE_CONST2)
{
info->regtype = REG_TYPE_CONST;
info->regnum += 2048;
} // else if
else if (info->regtype == REG_TYPE_CONST3)
{
info->regtype = REG_TYPE_CONST;
info->regnum += 4096;
} // else if
else if (info->regtype == REG_TYPE_CONST4)
{
info->regtype = REG_TYPE_CONST;
info->regnum += 6144;
} // else if
Nov 11, 2010
Nov 11, 2010
401
402
// swallow token for now, for multiple calls in a row.
adjust_token_position(ctx, 1);
Mar 16, 2008
Mar 16, 2008
403
Mar 16, 2008
Mar 16, 2008
404
if (reserved1 != 0x0)
Feb 3, 2009
Feb 3, 2009
405
fail(ctx, "Reserved bit #1 in destination token must be zero");
Mar 16, 2008
Mar 16, 2008
406
407
if (reserved2 != 0x1)
Feb 3, 2009
Feb 3, 2009
408
fail(ctx, "Reserved bit #2 in destination token must be one");
Mar 16, 2008
Mar 16, 2008
409
410
411
if (info->relative)
{
Apr 19, 2008
Apr 19, 2008
412
if (!shader_is_vertex(ctx))
Feb 3, 2009
Feb 3, 2009
413
414
415
fail(ctx, "Relative addressing in non-vertex shader");
if (!shader_version_atleast(ctx, 3, 0))
fail(ctx, "Relative addressing in vertex shader version < 3.0");
Aug 1, 2011
Aug 1, 2011
416
417
418
if ((!ctx->ctab.have_ctab) && (!ctx->ignores_ctab))
{
// it's hard to do this efficiently without!
Feb 3, 2009
Feb 3, 2009
419
fail(ctx, "relative addressing unsupported without a CTAB");
Aug 1, 2011
Aug 1, 2011
420
} // if
Feb 3, 2009
Feb 3, 2009
421
Apr 19, 2008
Apr 19, 2008
422
// !!! FIXME: I don't have a shader that has a relative dest currently.
Feb 3, 2009
Feb 3, 2009
423
424
fail(ctx, "Relative addressing of dest tokens is unsupported");
return 2;
Mar 16, 2008
Mar 16, 2008
425
426
} // if
Mar 16, 2008
Mar 16, 2008
427
428
const int s = info->result_shift;
if (s != 0)
Mar 16, 2008
Mar 16, 2008
429
{
Apr 19, 2008
Apr 19, 2008
430
if (!shader_is_pixel(ctx))
Feb 3, 2009
Feb 3, 2009
431
432
433
434
435
fail(ctx, "Result shift scale in non-pixel shader");
if (shader_version_atleast(ctx, 2, 0))
fail(ctx, "Result shift scale in pixel shader version >= 2.0");
if ( ! (((s >= 1) && (s <= 3)) || ((s >= 0xD) && (s <= 0xF))) )
fail(ctx, "Result shift scale isn't 1 to 3, or 13 to 15.");
Mar 16, 2008
Mar 16, 2008
436
437
} // if
Mar 16, 2008
Mar 16, 2008
438
if (info->result_mod & MOD_PP) // Partial precision (pixel shaders only)
Mar 16, 2008
Mar 16, 2008
439
{
Apr 19, 2008
Apr 19, 2008
440
if (!shader_is_pixel(ctx))
Feb 3, 2009
Feb 3, 2009
441
fail(ctx, "Partial precision result mod in non-pixel shader");
Mar 16, 2008
Mar 16, 2008
442
443
} // if
Mar 16, 2008
Mar 16, 2008
444
if (info->result_mod & MOD_CENTROID) // Centroid (pixel shaders only)
Mar 16, 2008
Mar 16, 2008
445
{
Apr 19, 2008
Apr 19, 2008
446
if (!shader_is_pixel(ctx))
Feb 3, 2009
Feb 3, 2009
447
fail(ctx, "Centroid result mod in non-pixel shader");
Jul 7, 2008
Jul 7, 2008
448
else if (!ctx->centroid_allowed) // only on DCL opcodes!
Feb 3, 2009
Feb 3, 2009
449
fail(ctx, "Centroid modifier not allowed here");
Mar 16, 2008
Mar 16, 2008
450
451
} // if
Apr 25, 2016
Apr 25, 2016
452
if (/*(info->regtype < 0) ||*/ (info->regtype > REG_TYPE_MAX))
Feb 3, 2009
Feb 3, 2009
453
fail(ctx, "Register type is out of range");
Mar 16, 2008
Mar 16, 2008
454
Feb 3, 2009
Feb 3, 2009
455
if (!isfail(ctx))
Apr 15, 2012
Apr 15, 2012
456
set_used_register(ctx, info->regtype, info->regnum, 1);
Apr 22, 2008
Apr 22, 2008
457
Mar 16, 2008
Mar 16, 2008
458
459
460
461
return 1;
} // parse_destination_token
Jun 29, 2008
Jun 29, 2008
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
static void determine_constants_arrays(Context *ctx)
{
// Only process this stuff once. This is called after all DEF* opcodes
// could have been parsed.
if (ctx->determined_constants_arrays)
return;
ctx->determined_constants_arrays = 1;
if (ctx->constant_count <= 1)
return; // nothing to sort or group.
// Sort the linked list into an array for easier tapdancing...
ConstantsList **array = (ConstantsList **) alloca(sizeof (ConstantsList *) * (ctx->constant_count + 1));
ConstantsList *item = ctx->constants;
int i;
for (i = 0; i < ctx->constant_count; i++)
{
if (item == NULL)
{
fail(ctx, "BUG: mismatched constant list and count");
return;
} // if
array[i] = item;
item = item->next;
} // for
array[ctx->constant_count] = NULL;
// bubble sort ftw.
int sorted;
do
{
sorted = 1;
for (i = 0; i < ctx->constant_count-1; i++)
{
if (array[i]->constant.index > array[i+1]->constant.index)
{
ConstantsList *tmp = array[i];
array[i] = array[i+1];
array[i+1] = tmp;
sorted = 0;
} // if
} // for
} while (!sorted);
// okay, sorted. While we're here, let's redo the linked list in order...
for (i = 0; i < ctx->constant_count; i++)
array[i]->next = array[i+1];
ctx->constants = array[0];
// now figure out the groupings of constants and add to ctx->variables...
int start = -1;
int prev = -1;
int count = 0;
const int hi = ctx->constant_count;
for (i = 0; i <= hi; i++)
{
if (array[i] && (array[i]->constant.type != MOJOSHADER_UNIFORM_FLOAT))
continue; // we only care about REG_TYPE_CONST for array groups.
if (start == -1)
{
prev = start = i; // first REG_TYPE_CONST we've seen. Mark it!
continue;
} // if
// not a match (or last item in the array)...see if we had a
// contiguous set before this point...
if ( (array[i]) && (array[i]->constant.index == (array[prev]->constant.index + 1)) )
count++;
else
{
if (count > 0) // multiple constants in the set?
{
VariableList *var;
var = (VariableList *) Malloc(ctx, sizeof (VariableList));
if (var == NULL)
break;
var->type = MOJOSHADER_UNIFORM_FLOAT;
var->index = array[start]->constant.index;
var->count = (array[prev]->constant.index - var->index) + 1;
var->constant = array[start];
var->used = 0;
Aug 6, 2009
Aug 6, 2009
549
var->emit_position = -1;
Jun 29, 2008
Jun 29, 2008
550
551
var->next = ctx->variables;
ctx->variables = var;
Dec 20, 2019
Dec 20, 2019
552
} // if
Jun 29, 2008
Jun 29, 2008
553
554
555
556
557
558
559
560
start = i; // set this as new start of sequence.
} // if
prev = i;
} // for
} // determine_constants_arrays
Oct 24, 2020
Oct 24, 2020
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
static void shader_model_1_input_usage(const int regnum, MOJOSHADER_usage *usage, int *index)
{
*index = 0;
switch (regnum) // these are hardcoded for Shader Model 1: v0 is POSITION, v1 is BLENDWEIGHT, etc.
{
case 0: *usage = MOJOSHADER_USAGE_POSITION; break;
case 1: *usage = MOJOSHADER_USAGE_BLENDWEIGHT; break;
case 2: *usage = MOJOSHADER_USAGE_BLENDINDICES; break;
case 3: *usage = MOJOSHADER_USAGE_NORMAL; break;
case 4: *usage = MOJOSHADER_USAGE_POINTSIZE; break;
case 5: *usage = MOJOSHADER_USAGE_COLOR; break; // diffuse
case 6: *usage = MOJOSHADER_USAGE_COLOR; *index = 1; break; // specular
case 7: *usage = MOJOSHADER_USAGE_TEXCOORD; break;
case 8: *usage = MOJOSHADER_USAGE_TEXCOORD; *index = 1; break;
case 9: *usage = MOJOSHADER_USAGE_TEXCOORD; *index = 2; break;
case 10: *usage = MOJOSHADER_USAGE_TEXCOORD; *index = 3; break;
case 11: *usage = MOJOSHADER_USAGE_TEXCOORD; *index = 4; break;
case 12: *usage = MOJOSHADER_USAGE_TEXCOORD; *index = 5; break;
case 13: *usage = MOJOSHADER_USAGE_TEXCOORD; *index = 6; break;
case 14: *usage = MOJOSHADER_USAGE_TEXCOORD; *index = 7; break;
case 15: *usage = MOJOSHADER_USAGE_POSITION; *index = 1; break;
case 16: *usage = MOJOSHADER_USAGE_NORMAL; *index = 1; break;
default: *usage = MOJOSHADER_USAGE_UNKNOWN; break;
} // switch
} // shader_model_1_input_usage
Jun 29, 2008
Jun 29, 2008
586
Aug 26, 2008
Aug 26, 2008
587
588
589
590
591
592
593
594
static int adjust_swizzle(const Context *ctx, const RegisterType regtype,
const int regnum, const int swizzle)
{
if (regtype != REG_TYPE_INPUT) // !!! FIXME: maybe lift this later?
return swizzle;
else if (ctx->swizzles_count == 0)
return swizzle;
Oct 24, 2020
Oct 24, 2020
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
MOJOSHADER_usage usage = MOJOSHADER_USAGE_UNKNOWN;
int index = 0;
// Shader Model 1 didn't have to predeclare attribute variables, so
// reglist_find won't be able to look them up at this point, but
// their usages don't change.
if (!shader_version_atleast(ctx, 2, 0))
shader_model_1_input_usage(regnum, &usage, &index);
else
{
const RegisterList *reg = reglist_find(&ctx->attributes, regtype, regnum);
if (reg == NULL)
return swizzle;
usage = reg->usage;
index = reg->index;
} // else
if (usage == MOJOSHADER_USAGE_UNKNOWN)
Aug 26, 2008
Aug 26, 2008
613
614
return swizzle;
Nov 15, 2009
Nov 15, 2009
615
616
size_t i;
for (i = 0; i < ctx->swizzles_count; i++)
Aug 26, 2008
Aug 26, 2008
617
{
Nov 15, 2009
Nov 15, 2009
618
const MOJOSHADER_swizzle *swiz = &ctx->swizzles[i];
Oct 24, 2020
Oct 24, 2020
619
if ((swiz->usage == usage) && (swiz->index == index))
Aug 26, 2008
Aug 26, 2008
620
621
622
623
624
625
626
627
628
629
630
631
{
return ( (((int)(swiz->swizzles[((swizzle >> 0) & 0x3)])) << 0) |
(((int)(swiz->swizzles[((swizzle >> 2) & 0x3)])) << 2) |
(((int)(swiz->swizzles[((swizzle >> 4) & 0x3)])) << 4) |
(((int)(swiz->swizzles[((swizzle >> 6) & 0x3)])) << 6) );
} // if
} // for
return swizzle;
} // adjust_swizzle
Mar 16, 2008
Mar 16, 2008
632
633
static int parse_source_token(Context *ctx, SourceArgInfo *info)
{
Apr 19, 2008
Apr 19, 2008
634
635
int retval = 1;
Mar 16, 2008
Mar 16, 2008
636
if (ctx->tokencount == 0)
Feb 3, 2009
Feb 3, 2009
637
638
639
640
{
fail(ctx, "Out of tokens in source parameter");
return 0;
} // if
Mar 16, 2008
Mar 16, 2008
641
642
643
644
645
const uint32 token = SWAP32(*(ctx->tokens));
const int reserved1 = (int) ((token >> 14) & 0x3); // bits 14 through 15
const int reserved2 = (int) ((token >> 31) & 0x1); // bit 31
Mar 16, 2008
Mar 16, 2008
646
info->token = ctx->tokens;
Mar 16, 2008
Mar 16, 2008
647
648
info->regnum = (int) (token & 0x7ff); // bits 0 through 10
info->relative = (int) ((token >> 13) & 0x1); // bit 13
Aug 26, 2008
Aug 26, 2008
649
const int swizzle = (int) ((token >> 16) & 0xFF); // bits 16 through 23
Apr 18, 2008
Apr 18, 2008
650
info->src_mod = (SourceMod) ((token >> 24) & 0xF); // bits 24 through 27
Apr 2, 2008
Apr 2, 2008
651
info->regtype = (RegisterType) (((token >> 28) & 0x7) | ((token >> 8) & 0x18)); // bits 28-30, 11-12
Mar 16, 2008
Mar 16, 2008
652
Apr 28, 2008
Apr 28, 2008
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// all the REG_TYPE_CONSTx types are the same register type, it's just
// split up so its regnum can be > 2047 in the bytecode. Clean it up.
if (info->regtype == REG_TYPE_CONST2)
{
info->regtype = REG_TYPE_CONST;
info->regnum += 2048;
} // else if
else if (info->regtype == REG_TYPE_CONST3)
{
info->regtype = REG_TYPE_CONST;
info->regnum += 4096;
} // else if
else if (info->regtype == REG_TYPE_CONST4)
{
info->regtype = REG_TYPE_CONST;
info->regnum += 6144;
} // else if
Aug 26, 2008
Aug 26, 2008
671
672
673
674
675
676
info->swizzle = adjust_swizzle(ctx, info->regtype, info->regnum, swizzle);
info->swizzle_x = ((info->swizzle >> 0) & 0x3);
info->swizzle_y = ((info->swizzle >> 2) & 0x3);
info->swizzle_z = ((info->swizzle >> 4) & 0x3);
info->swizzle_w = ((info->swizzle >> 6) & 0x3);
Nov 11, 2010
Nov 11, 2010
677
678
// swallow token for now, for multiple calls in a row.
adjust_token_position(ctx, 1);
Mar 16, 2008
Mar 16, 2008
679
Mar 16, 2008
Mar 16, 2008
680
if (reserved1 != 0x0)
Feb 3, 2009
Feb 3, 2009
681
fail(ctx, "Reserved bits #1 in source token must be zero");
Mar 16, 2008
Mar 16, 2008
682
683
if (reserved2 != 0x1)
Feb 3, 2009
Feb 3, 2009
684
685
686
687
688
689
690
fail(ctx, "Reserved bit #2 in source token must be one");
if ((info->relative) && (ctx->tokencount == 0))
{
fail(ctx, "Out of tokens in relative source parameter");
info->relative = 0; // don't try to process it.
} // if
Mar 16, 2008
Mar 16, 2008
691
692
693
if (info->relative)
{
Apr 19, 2008
Apr 19, 2008
694
if ( (shader_is_pixel(ctx)) && (!shader_version_atleast(ctx, 3, 0)) )
Feb 3, 2009
Feb 3, 2009
695
fail(ctx, "Relative addressing in pixel shader version < 3.0");
Apr 19, 2008
Apr 19, 2008
696
Apr 28, 2014
Apr 28, 2014
697
698
699
700
701
702
703
704
// Shader Model 1 doesn't have an extra token to specify the
// relative register: it's always a0.x.
if (!shader_version_atleast(ctx, 2, 0))
{
info->relative_regnum = 0;
info->relative_regtype = REG_TYPE_ADDRESS;
info->relative_component = 0;
} // if
Apr 19, 2008
Apr 19, 2008
705
Apr 28, 2014
Apr 28, 2014
706
707
708
709
710
else // Shader Model 2 and later...
{
const uint32 reltoken = SWAP32(*(ctx->tokens));
// swallow token for now, for multiple calls in a row.
adjust_token_position(ctx, 1);
Apr 19, 2008
Apr 19, 2008
711
Apr 28, 2014
Apr 28, 2014
712
713
714
715
716
const int relswiz = (int) ((reltoken >> 16) & 0xFF);
info->relative_regnum = (int) (reltoken & 0x7ff);
info->relative_regtype = (RegisterType)
(((reltoken >> 28) & 0x7) |
((reltoken >> 8) & 0x18));
Apr 19, 2008
Apr 19, 2008
717
Apr 28, 2014
Apr 28, 2014
718
719
if (((reltoken >> 31) & 0x1) == 0)
fail(ctx, "bit #31 in relative address must be set");
Apr 19, 2008
Apr 19, 2008
720
Apr 28, 2014
Apr 28, 2014
721
722
723
724
725
726
727
728
729
730
731
732
733
734
if ((reltoken & 0xF00E000) != 0) // usused bits.
fail(ctx, "relative address reserved bit must be zero");
switch (info->relative_regtype)
{
case REG_TYPE_LOOP:
case REG_TYPE_ADDRESS:
break;
default:
fail(ctx, "invalid register for relative address");
break;
} // switch
if (info->relative_regnum != 0) // true for now.
Feb 3, 2009
Feb 3, 2009
735
fail(ctx, "invalid register for relative address");
Apr 19, 2008
Apr 19, 2008
736
Feb 19, 2019
Feb 19, 2019
737
if ( (info->relative_regtype != REG_TYPE_LOOP) && !replicate_swizzle(relswiz) )
Apr 28, 2014
Apr 28, 2014
738
fail(ctx, "relative address needs replicate swizzle");
Apr 19, 2008
Apr 19, 2008
739
Apr 28, 2014
Apr 28, 2014
740
info->relative_component = (relswiz & 0x3);
Apr 19, 2008
Apr 19, 2008
741
Apr 28, 2014
Apr 28, 2014
742
743
retval++;
} // else
Oct 24, 2011
Oct 24, 2011
744
Feb 1, 2009
Feb 1, 2009
745
746
747
if (info->regtype == REG_TYPE_INPUT)
{
if ( (shader_is_pixel(ctx)) || (!shader_version_atleast(ctx, 3, 0)) )
Feb 3, 2009
Feb 3, 2009
748
fail(ctx, "relative addressing of input registers not supported in this shader model");
Feb 1, 2009
Feb 1, 2009
749
750
751
752
753
ctx->have_relative_input_registers = 1;
} // if
else if (info->regtype == REG_TYPE_CONST)
{
// figure out what array we're in...
Aug 1, 2011
Aug 1, 2011
754
if (!ctx->ignores_ctab)
Feb 1, 2009
Feb 1, 2009
755
{
Aug 1, 2011
Aug 1, 2011
756
757
758
if (!ctx->ctab.have_ctab) // hard to do efficiently without!
fail(ctx, "relative addressing unsupported without a CTAB");
else
Feb 3, 2009
Feb 3, 2009
759
{
Aug 1, 2011
Aug 1, 2011
760
761
762
763
764
765
766
767
768
769
770
771
772
determine_constants_arrays(ctx);
VariableList *var;
const int reltarget = info->regnum;
for (var = ctx->variables; var != NULL; var = var->next)
{
const int lo = var->index;
if ( (reltarget >= lo) && (reltarget < (lo + var->count)) )
break; // match!
} // for
if (var == NULL)
fail(ctx, "relative addressing of indeterminate array");
Oct 20, 2011
Oct 20, 2011
773
774
775
776
else
{
var->used = 1;
info->relative_array = var;
Apr 15, 2012
Apr 15, 2012
777
set_used_register(ctx, info->relative_regtype, info->relative_regnum, 0);
Oct 20, 2011
Oct 20, 2011
778
} // else
Aug 1, 2011
Aug 1, 2011
779
780
} // else
} // if
Feb 1, 2009
Feb 1, 2009
781
782
} // else if
else
Jun 27, 2008
Jun 27, 2008
783
{
Feb 3, 2009
Feb 3, 2009
784
fail(ctx, "relative addressing of invalid register");
Feb 1, 2009
Feb 1, 2009
785
} // else
Mar 16, 2008
Mar 16, 2008
786
787
} // if
Jul 7, 2008
Jul 7, 2008
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
switch (info->src_mod)
{
case SRCMOD_NONE:
case SRCMOD_ABSNEGATE:
case SRCMOD_ABS:
case SRCMOD_NEGATE:
break; // okay in any shader model.
// apparently these are only legal in Shader Model 1.x ...
case SRCMOD_BIASNEGATE:
case SRCMOD_BIAS:
case SRCMOD_SIGNNEGATE:
case SRCMOD_SIGN:
case SRCMOD_COMPLEMENT:
case SRCMOD_X2NEGATE:
case SRCMOD_X2:
case SRCMOD_DZ:
case SRCMOD_DW:
if (shader_version_atleast(ctx, 2, 0))
Feb 3, 2009
Feb 3, 2009
807
fail(ctx, "illegal source mod for this Shader Model.");
Jul 7, 2008
Jul 7, 2008
808
809
break;
Dec 10, 2008
Dec 10, 2008
810
811
812
case SRCMOD_NOT: // !!! FIXME: I _think_ this is right...
if (shader_version_atleast(ctx, 2, 0))
{
Feb 3, 2017
Feb 3, 2017
813
814
815
if (info->regtype != REG_TYPE_PREDICATE
&& info->regtype != REG_TYPE_CONSTBOOL)
fail(ctx, "NOT only allowed on bool registers.");
Dec 10, 2008
Dec 10, 2008
816
817
818
} // if
break;
Jul 7, 2008
Jul 7, 2008
819
default:
Feb 3, 2009
Feb 3, 2009
820
fail(ctx, "Unknown source modifier");
Jul 7, 2008
Jul 7, 2008
821
822
823
824
825
826
827
828
829
830
} // switch
// !!! FIXME: docs say this for sm3 ... check these!
// "The negate modifier cannot be used on second source register of these
// instructions: m3x2 - ps, m3x3 - ps, m3x4 - ps, m4x3 - ps, and
// m4x4 - ps."
// "If any version 3 shader reads from one or more constant float
// registers (c#), one of the following must be true.
// All of the constant floating-point registers must use the abs modifier.
// None of the constant floating-point registers can use the abs modifier.
Mar 16, 2008
Mar 16, 2008
831
Feb 3, 2009
Feb 3, 2009
832
if (!isfail(ctx))
Apr 15, 2012
Apr 15, 2012
833
834
835
836
837
838
839
840
841
842
843
{
RegisterList *reg;
reg = set_used_register(ctx, info->regtype, info->regnum, 0);
// !!! FIXME: this test passes if you write to the register
// !!! FIXME: in this same instruction, because we parse the
// !!! FIXME: destination token first.
// !!! FIXME: Microsoft's shader validation explicitly checks temp
// !!! FIXME: registers for this...do they check other writable ones?
if ((info->regtype == REG_TYPE_TEMP) && (reg) && (!reg->written))
failf(ctx, "Temp register r%d used uninitialized", info->regnum);
} // if
Feb 3, 2009
Feb 3, 2009
844
Apr 19, 2008
Apr 19, 2008
845
return retval;
Mar 16, 2008
Mar 16, 2008
846
847
848
} // parse_source_token
Apr 18, 2008
Apr 18, 2008
849
850
851
static int parse_predicated_token(Context *ctx)
{
SourceArgInfo *arg = &ctx->predicate_arg;
Feb 3, 2009
Feb 3, 2009
852
853
854
855
856
857
858
859
860
parse_source_token(ctx, arg);
if (arg->regtype != REG_TYPE_PREDICATE)
fail(ctx, "Predicated instruction but not predicate register!");
if ((arg->src_mod != SRCMOD_NONE) && (arg->src_mod != SRCMOD_NOT))
fail(ctx, "Predicated instruction register is not NONE or NOT");
if ( !no_swizzle(arg->swizzle) && !replicate_swizzle(arg->swizzle) )
fail(ctx, "Predicated instruction register has wrong swizzle");
if (arg->relative) // I'm pretty sure this is illegal...?
fail(ctx, "relative addressing in predicated token");
Apr 19, 2008
Apr 19, 2008
861
862
return 1;
Apr 18, 2008
Apr 18, 2008
863
864
865
} // parse_predicated_token
Mar 16, 2008
Mar 16, 2008
866
867
static int parse_args_NULL(Context *ctx)
{
Feb 3, 2009
Feb 3, 2009
868
return 1;
Mar 16, 2008
Mar 16, 2008
869
870
871
} // parse_args_NULL
Mar 21, 2008
Mar 21, 2008
872
873
static int parse_args_DEF(Context *ctx)
{
Feb 3, 2009
Feb 3, 2009
874
875
876
877
878
parse_destination_token(ctx, &ctx->dest_arg);
if (ctx->dest_arg.regtype != REG_TYPE_CONST)
fail(ctx, "DEF using non-CONST register");
if (ctx->dest_arg.relative) // I'm pretty sure this is illegal...?
fail(ctx, "relative addressing in DEF");
Apr 19, 2008
Apr 19, 2008
879
Apr 2, 2008
Apr 2, 2008
880
881
882
883
884
ctx->dwords[0] = SWAP32(ctx->tokens[0]);
ctx->dwords[1] = SWAP32(ctx->tokens[1]);
ctx->dwords[2] = SWAP32(ctx->tokens[2]);
ctx->dwords[3] = SWAP32(ctx->tokens[3]);
Mar 28, 2008
Mar 28, 2008
885
return 6;
Mar 21, 2008
Mar 21, 2008
886
887
888
} // parse_args_DEF
Dec 7, 2008
Dec 7, 2008
889
static int parse_args_DEFI(Context *ctx)
Mar 21, 2008
Mar 21, 2008
890
{
Feb 3, 2009
Feb 3, 2009
891
892
893
894
895
parse_destination_token(ctx, &ctx->dest_arg);
if (ctx->dest_arg.regtype != REG_TYPE_CONSTINT)
fail(ctx, "DEFI using non-CONSTING register");
if (ctx->dest_arg.relative) // I'm pretty sure this is illegal...?
fail(ctx, "relative addressing in DEFI");
Dec 7, 2008
Dec 7, 2008
896
897
898
899
900
ctx->dwords[0] = SWAP32(ctx->tokens[0]);
ctx->dwords[1] = SWAP32(ctx->tokens[1]);
ctx->dwords[2] = SWAP32(ctx->tokens[2]);
ctx->dwords[3] = SWAP32(ctx->tokens[3]);
Mar 21, 2008
Mar 21, 2008
901
Dec 7, 2008
Dec 7, 2008
902
903
904
905
906
907
return 6;
} // parse_args_DEFI
static int parse_args_DEFB(Context *ctx)
{
Feb 3, 2009
Feb 3, 2009
908
909
910
911
912
parse_destination_token(ctx, &ctx->dest_arg);
if (ctx->dest_arg.regtype != REG_TYPE_CONSTBOOL)
fail(ctx, "DEFB using non-CONSTBOOL register");
if (ctx->dest_arg.relative) // I'm pretty sure this is illegal...?
fail(ctx, "relative addressing in DEFB");
Apr 19, 2008
Apr 19, 2008
913
Mar 21, 2008
Mar 21, 2008
914
915
ctx->dwords[0] = *(ctx->tokens) ? 1 : 0;
Mar 28, 2008
Mar 28, 2008
916
return 3;
Mar 27, 2008
Mar 27, 2008
917
} // parse_args_DEFB
Mar 21, 2008
Mar 21, 2008
918
919
May 10, 2008
May 10, 2008
920
921
922
923
924
925
926
927
928
929
930
931
932
933
static int valid_texture_type(const uint32 ttype)
{
switch ((const TextureType) ttype)
{
case TEXTURE_TYPE_2D:
case TEXTURE_TYPE_CUBE:
case TEXTURE_TYPE_VOLUME:
return 1; // it's okay.
} // switch
return 0;
} // valid_texture_type
Apr 20, 2008
Apr 20, 2008
934
// !!! FIXME: this function is kind of a mess.
Mar 21, 2008
Mar 21, 2008
935
936
937
938
939
940
941
942
static int parse_args_DCL(Context *ctx)
{
int unsupported = 0;
const uint32 token = SWAP32(*(ctx->tokens));
const int reserved1 = (int) ((token >> 31) & 0x1); // bit 31
uint32 reserved_mask = 0x00000000;
if (reserved1 != 0x1)
Feb 3, 2009
Feb 3, 2009
943
fail(ctx, "Bit #31 in DCL token must be one");
Mar 21, 2008
Mar 21, 2008
944
Jul 7, 2008
Jul 7, 2008
945
ctx->centroid_allowed = 1;
Nov 11, 2010
Nov 11, 2010
946
adjust_token_position(ctx, 1);
Feb 3, 2009
Feb 3, 2009
947
parse_destination_token(ctx, &ctx->dest_arg);
Jul 7, 2008
Jul 7, 2008
948
ctx->centroid_allowed = 0;
Mar 21, 2008
Mar 21, 2008
949
Jul 7, 2008
Jul 7, 2008
950
if (ctx->dest_arg.result_shift != 0) // I'm pretty sure this is illegal...?
Feb 3, 2009
Feb 3, 2009
951
952
953
fail(ctx, "shift scale in DCL");
if (ctx->dest_arg.relative) // I'm pretty sure this is illegal...?
fail(ctx, "relative addressing in DCL");
Apr 19, 2008
Apr 19, 2008
954
Apr 21, 2008
Apr 21, 2008
955
956
const RegisterType regtype = ctx->dest_arg.regtype;
const int regnum = ctx->dest_arg.regnum;
Apr 19, 2008
Apr 19, 2008
957
if ( (shader_is_pixel(ctx)) && (shader_version_atleast(ctx, 3, 0)) )
Mar 21, 2008
Mar 21, 2008
958
{
Apr 3, 2008
Apr 3, 2008
959
if (regtype == REG_TYPE_INPUT)
Apr 20, 2008
Apr 20, 2008
960
961
962
963
964
965
966
{
const uint32 usage = (token & 0xF);
const uint32 index = ((token >> 16) & 0xF);
reserved_mask = 0x7FF0FFE0;
ctx->dwords[0] = usage;
ctx->dwords[1] = index;
} // if
Mar 21, 2008
Mar 21, 2008
967
Apr 3, 2008
Apr 3, 2008
968
else if (regtype == REG_TYPE_MISCTYPE)
Mar 21, 2008
Mar 21, 2008
969
{
Apr 4, 2008
Apr 4, 2008
970
const MiscTypeType mt = (MiscTypeType) regnum;
Mar 21, 2008
Mar 21, 2008
971
972
973
974
975
if (mt == MISCTYPE_TYPE_POSITION)
reserved_mask = 0x7FFFFFFF;
else if (mt == MISCTYPE_TYPE_FACE)
{
reserved_mask = 0x7FFFFFFF;
May 12, 2008
May 12, 2008
976
if (!writemask_xyzw(ctx->dest_arg.orig_writemask))
Feb 3, 2009
Feb 3, 2009
977
978
979
980
981
fail(ctx, "DCL face writemask must be full");
if (ctx->dest_arg.result_mod != 0)
fail(ctx, "DCL face result modifier must be zero");
if (ctx->dest_arg.result_shift != 0)
fail(ctx, "DCL face shift scale must be zero");
Mar 21, 2008
Mar 21, 2008
982
983
984
985
986
} // else if
else
{
unsupported = 1;
} // else
May 12, 2008
May 12, 2008
987
988
989
ctx->dwords[0] = (uint32) MOJOSHADER_USAGE_UNKNOWN;
ctx->dwords[1] = 0;
Mar 21, 2008
Mar 21, 2008
990
991
} // else if
Apr 3, 2008
Apr 3, 2008
992
else if (regtype == REG_TYPE_TEXTURE)
Mar 21, 2008
Mar 21, 2008
993
994
995
{
const uint32 usage = (token & 0xF);
const uint32 index = ((token >> 16) & 0xF);
Apr 5, 2008
Apr 5, 2008
996
if (usage == MOJOSHADER_USAGE_TEXCOORD)
Mar 21, 2008
Mar 21, 2008
997
998
{
if (index > 7)
Feb 3, 2009
Feb 3, 2009
999
fail(ctx, "DCL texcoord usage must have 0-7 index");
Mar 21, 2008
Mar 21, 2008
1000
} // if