Debian Codegraph @

profiles/mojoshader_profile_d3d.c

raw symbols SCIP
1 /**
2 * MojoShader; generate shader programs from bytecode of compiled
3 * Direct3D shaders.
4 *
5 * Please see the file LICENSE.txt in the source's root directory.
6 *
7 * This file written by Ryan C. Gordon.
8 */
9
10 #define __MOJOSHADER_INTERNAL__ 1
11 #include "mojoshader_profile.h"
12
13 #pragma GCC visibility push(hidden)
14
15 #if SUPPORT_PROFILE_D3D
16
17 const char *make_D3D_srcarg_string_in_buf(Context *ctx,
18 const SourceArgInfo *arg,
19 char *buf, size_t buflen)
20 {
21 const char *premod_str = "";
22 const char *postmod_str = "";
23 switch (arg->src_mod)
24 {
25 case SRCMOD_NEGATE:
26 premod_str = "-";
27 break;
28
29 case SRCMOD_BIASNEGATE:
30 premod_str = "-";
31 // fall through.
32 case SRCMOD_BIAS:
33 postmod_str = "_bias";
34 break;
35
36 case SRCMOD_SIGNNEGATE:
37 premod_str = "-";
38 // fall through.
39 case SRCMOD_SIGN:
40 postmod_str = "_bx2";
41 break;
42
43 case SRCMOD_COMPLEMENT:
44 premod_str = "1-";
45 break;
46
47 case SRCMOD_X2NEGATE:
48 premod_str = "-";
49 // fall through.
50 case SRCMOD_X2:
51 postmod_str = "_x2";
52 break;
53
54 case SRCMOD_DZ:
55 postmod_str = "_dz";
56 break;
57
58 case SRCMOD_DW:
59 postmod_str = "_dw";
60 break;
61
62 case SRCMOD_ABSNEGATE:
63 premod_str = "-";
64 // fall through.
65 case SRCMOD_ABS:
66 postmod_str = "_abs";
67 break;
68
69 case SRCMOD_NOT:
70 premod_str = "!";
71 break;
72
73 case SRCMOD_NONE:
74 case SRCMOD_TOTAL:
75 break; // stop compiler whining.
76 } // switch
77
78
79 char regnum_str[16];
80 const char *regtype_str = get_D3D_register_string(ctx, arg->regtype,
81 arg->regnum, regnum_str,
82 sizeof (regnum_str));
83
84 if (regtype_str == NULL)
85 {
86 fail(ctx, "Unknown source register type.");
87 *buf = '\0';
88 return buf;
89 } // if
90
91 const char *rel_lbracket = "";
92 const char *rel_rbracket = "";
93 char rel_swizzle[4] = { '\0' };
94 char rel_regnum_str[16] = { '\0' };
95 const char *rel_regtype_str = "";
96 if (arg->relative)
97 {
98 if (arg->relative_regtype == REG_TYPE_LOOP)
99 {
100 rel_swizzle[0] = '\0';
101 rel_swizzle[1] = '\0';
102 rel_swizzle[2] = '\0';
103 } // if
104 else
105 {
106 rel_swizzle[0] = '.';
107 rel_swizzle[1] = swizzle_channels[arg->relative_component];
108 rel_swizzle[2] = '\0';
109 } // else
110
111 rel_lbracket = "[";
112 rel_rbracket = "]";
113 rel_regtype_str = get_D3D_register_string(ctx, arg->relative_regtype,
114 arg->relative_regnum,
115 rel_regnum_str,
116 sizeof (rel_regnum_str));
117
118 if (regtype_str == NULL)
119 {
120 fail(ctx, "Unknown relative source register type.");
121 *buf = '\0';
122 return buf;
123 } // if
124 } // if
125
126 char swizzle_str[6];
127 size_t i = 0;
128 const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
129 if (!scalar && !no_swizzle(arg->swizzle))
130 {
131 swizzle_str[i++] = '.';
132 swizzle_str[i++] = swizzle_channels[arg->swizzle_x];
133 swizzle_str[i++] = swizzle_channels[arg->swizzle_y];
134 swizzle_str[i++] = swizzle_channels[arg->swizzle_z];
135 swizzle_str[i++] = swizzle_channels[arg->swizzle_w];
136
137 // .xyzz is the same as .xyz, .z is the same as .zzzz, etc.
138 while (swizzle_str[i-1] == swizzle_str[i-2])
139 i--;
140 } // if
141 swizzle_str[i] = '\0';
142 assert(i < sizeof (swizzle_str));
143
144 // !!! FIXME: c12[a0.x] actually needs to be c[a0.x + 12]
145 snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s",
146 premod_str, regtype_str, regnum_str, postmod_str,
147 rel_lbracket, rel_regtype_str, rel_regnum_str, rel_swizzle,
148 rel_rbracket, swizzle_str);
149 // !!! FIXME: make sure the scratch buffer was large enough.
150 return buf;
151 } // make_D3D_srcarg_string_in_buf
152
153
154 const char *make_D3D_destarg_string(Context *ctx, char *buf,
155 const size_t buflen)
156 {
157 const DestArgInfo *arg = &ctx->dest_arg;
158
159 const char *result_shift_str = "";
160 switch (arg->result_shift)
161 {
162 case 0x1: result_shift_str = "_x2"; break;
163 case 0x2: result_shift_str = "_x4"; break;
164 case 0x3: result_shift_str = "_x8"; break;
165 case 0xD: result_shift_str = "_d8"; break;
166 case 0xE: result_shift_str = "_d4"; break;
167 case 0xF: result_shift_str = "_d2"; break;
168 } // switch
169
170 const char *sat_str = (arg->result_mod & MOD_SATURATE) ? "_sat" : "";
171 const char *pp_str = (arg->result_mod & MOD_PP) ? "_pp" : "";
172 const char *cent_str = (arg->result_mod & MOD_CENTROID) ? "_centroid" : "";
173
174 char regnum_str[16];
175 const char *regtype_str = get_D3D_register_string(ctx, arg->regtype,
176 arg->regnum, regnum_str,
177 sizeof (regnum_str));
178 if (regtype_str == NULL)
179 {
180 fail(ctx, "Unknown destination register type.");
181 *buf = '\0';
182 return buf;
183 } // if
184
185 char writemask_str[6];
186 size_t i = 0;
187 const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
188 if (!scalar && !writemask_xyzw(arg->writemask))
189 {
190 writemask_str[i++] = '.';
191 if (arg->writemask0) writemask_str[i++] = 'x';
192 if (arg->writemask1) writemask_str[i++] = 'y';
193 if (arg->writemask2) writemask_str[i++] = 'z';
194 if (arg->writemask3) writemask_str[i++] = 'w';
195 } // if
196 writemask_str[i] = '\0';
197 assert(i < sizeof (writemask_str));
198
199 const char *pred_left = "";
200 const char *pred_right = "";
201 char pred[32] = { '\0' };
202 if (ctx->predicated)
203 {
204 pred_left = "(";
205 pred_right = ") ";
206 make_D3D_srcarg_string_in_buf(ctx, &ctx->predicate_arg,
207 pred, sizeof (pred));
208 } // if
209
210 // may turn out something like "_x2_sat_pp_centroid (!p0.x) r0.xyzw" ...
211 snprintf(buf, buflen, "%s%s%s%s %s%s%s%s%s%s",
212 result_shift_str, sat_str, pp_str, cent_str,
213 pred_left, pred, pred_right,
214 regtype_str, regnum_str, writemask_str);
215 // !!! FIXME: make sure the scratch buffer was large enough.
216 return buf;
217 } // make_D3D_destarg_string
218
219
220 const char *make_D3D_srcarg_string(Context *ctx, const size_t idx,
221 char *buf, size_t buflen)
222 {
223 if (idx >= STATICARRAYLEN(ctx->source_args))
224 {
225 fail(ctx, "Too many source args");
226 *buf = '\0';
227 return buf;
228 } // if
229
230 const SourceArgInfo *arg = &ctx->source_args[idx];
231 return make_D3D_srcarg_string_in_buf(ctx, arg, buf, buflen);
232 } // make_D3D_srcarg_string
233
234 const char *get_D3D_const_array_varname(Context *ctx, int base, int size)
235 {
236 char buf[64];
237 snprintf(buf, sizeof (buf), "c_array_%d_%d", base, size);
238 return StrDup(ctx, buf);
239 } // get_D3D_const_array_varname
240
241
242 void emit_D3D_start(Context *ctx, const char *profilestr)
243 {
244 const uint major = (uint) ctx->major_ver;
245 const uint minor = (uint) ctx->minor_ver;
246 char minor_str[16];
247
248 ctx->ignores_ctab = 1;
249
250 if (minor == 0xFF)
251 strcpy(minor_str, "sw");
252 else if ((major > 1) && (minor == 1))
253 strcpy(minor_str, "x"); // for >= SM2, apparently this is "x". Weird.
254 else
255 snprintf(minor_str, sizeof (minor_str), "%u", (uint) minor);
256
257 output_line(ctx, "%s_%u_%s", ctx->shader_type_str, major, minor_str);
258 } // emit_D3D_start
259
260
261 void emit_D3D_end(Context *ctx)
262 {
263 output_line(ctx, "end");
264 } // emit_D3D_end
265
266
267 void emit_D3D_phase(Context *ctx)
268 {
269 output_line(ctx, "phase");
270 } // emit_D3D_phase
271
272
273 void emit_D3D_finalize(Context *ctx)
274 {
275 // no-op.
276 } // emit_D3D_finalize
277
278
279 void emit_D3D_global(Context *ctx, RegisterType regtype, int regnum)
280 {
281 // no-op.
282 } // emit_D3D_global
283
284
285 void emit_D3D_array(Context *ctx, VariableList *var)
286 {
287 // no-op.
288 } // emit_D3D_array
289
290
291 void emit_D3D_const_array(Context *ctx, const ConstantsList *clist,
292 int base, int size)
293 {
294 // no-op.
295 } // emit_D3D_const_array
296
297
298 void emit_D3D_uniform(Context *ctx, RegisterType regtype, int regnum,
299 const VariableList *var)
300 {
301 // no-op.
302 } // emit_D3D_uniform
303
304
305 void emit_D3D_sampler(Context *ctx, int s, TextureType ttype, int tb)
306 {
307 // no-op.
308 } // emit_D3D_sampler
309
310
311 void emit_D3D_attribute(Context *ctx, RegisterType regtype, int regnum,
312 MOJOSHADER_usage usage, int index, int wmask,
313 int flags)
314 {
315 // no-op.
316 } // emit_D3D_attribute
317
318
319 void emit_D3D_RESERVED(Context *ctx)
320 {
321 // do nothing; fails in the state machine.
322 } // emit_D3D_RESERVED
323
324
325 // Generic D3D opcode emitters. A list of macros generate all the entry points
326 // that call into these...
327
328 char *lowercase(char *dst, const char *src)
329 {
330 int i = 0;
331 do
332 {
333 const char ch = src[i];
334 dst[i] = (((ch >= 'A') && (ch <= 'Z')) ? (ch - ('A' - 'a')) : ch);
335 } while (src[i++]);
336 return dst;
337 } // lowercase
338
339
340 void emit_D3D_opcode_d(Context *ctx, const char *opcode)
341 {
342 char dst[64]; make_D3D_destarg_string(ctx, dst, sizeof (dst));
343 opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
344 output_line(ctx, "%s%s%s", ctx->coissue ? "+" : "", opcode, dst);
345 } // emit_D3D_opcode_d
346
347
348 void emit_D3D_opcode_s(Context *ctx, const char *opcode)
349 {
350 char src0[64]; make_D3D_srcarg_string(ctx, 0, src0, sizeof (src0));
351 opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
352 output_line(ctx, "%s%s %s", ctx->coissue ? "+" : "", opcode, src0);
353 } // emit_D3D_opcode_s
354
355
356 void emit_D3D_opcode_ss(Context *ctx, const char *opcode)
357 {
358 char src0[64]; make_D3D_srcarg_string(ctx, 0, src0, sizeof (src0));
359 char src1[64]; make_D3D_srcarg_string(ctx, 1, src1, sizeof (src1));
360 opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
361 output_line(ctx, "%s%s %s, %s", ctx->coissue ? "+" : "", opcode, src0, src1);
362 } // emit_D3D_opcode_ss
363
364
365 void emit_D3D_opcode_ds(Context *ctx, const char *opcode)
366 {
367 char dst[64]; make_D3D_destarg_string(ctx, dst, sizeof (dst));
368 char src0[64]; make_D3D_srcarg_string(ctx, 0, src0, sizeof (src0));
369 opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
370 output_line(ctx, "%s%s%s, %s", ctx->coissue ? "+" : "", opcode, dst, src0);
371 } // emit_D3D_opcode_ds
372
373
374 void emit_D3D_opcode_dss(Context *ctx, const char *opcode)
375 {
376 char dst[64]; make_D3D_destarg_string(ctx, dst, sizeof (dst));
377 char src0[64]; make_D3D_srcarg_string(ctx, 0, src0, sizeof (src0));
378 char src1[64]; make_D3D_srcarg_string(ctx, 1, src1, sizeof (src1));
379 opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
380 output_line(ctx, "%s%s%s, %s, %s", ctx->coissue ? "+" : "",
381 opcode, dst, src0, src1);
382 } // emit_D3D_opcode_dss
383
384
385 void emit_D3D_opcode_dsss(Context *ctx, const char *opcode)
386 {
387 char dst[64]; make_D3D_destarg_string(ctx, dst, sizeof (dst));
388 char src0[64]; make_D3D_srcarg_string(ctx, 0, src0, sizeof (src0));
389 char src1[64]; make_D3D_srcarg_string(ctx, 1, src1, sizeof (src1));
390 char src2[64]; make_D3D_srcarg_string(ctx, 2, src2, sizeof (src2));
391 opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
392 output_line(ctx, "%s%s%s, %s, %s, %s", ctx->coissue ? "+" : "",
393 opcode, dst, src0, src1, src2);
394 } // emit_D3D_opcode_dsss
395
396
397 void emit_D3D_opcode_dssss(Context *ctx, const char *opcode)
398 {
399 char dst[64]; make_D3D_destarg_string(ctx, dst, sizeof (dst));
400 char src0[64]; make_D3D_srcarg_string(ctx, 0, src0, sizeof (src0));
401 char src1[64]; make_D3D_srcarg_string(ctx, 1, src1, sizeof (src1));
402 char src2[64]; make_D3D_srcarg_string(ctx, 2, src2, sizeof (src2));
403 char src3[64]; make_D3D_srcarg_string(ctx, 3, src3, sizeof (src3));
404 opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
405 output_line(ctx,"%s%s%s, %s, %s, %s, %s", ctx->coissue ? "+" : "",
406 opcode, dst, src0, src1, src2, src3);
407 } // emit_D3D_opcode_dssss
408
409
410 void emit_D3D_opcode(Context *ctx, const char *opcode)
411 {
412 opcode = lowercase((char *) alloca(strlen(opcode) + 1), opcode);
413 output_line(ctx, "%s%s", ctx->coissue ? "+" : "", opcode);
414 } // emit_D3D_opcode
415
416
417 #define EMIT_D3D_OPCODE_FUNC(op) \
418 void emit_D3D_##op(Context *ctx) { \
419 emit_D3D_opcode(ctx, #op); \
420 }
421 #define EMIT_D3D_OPCODE_D_FUNC(op) \
422 void emit_D3D_##op(Context *ctx) { \
423 emit_D3D_opcode_d(ctx, #op); \
424 }
425 #define EMIT_D3D_OPCODE_S_FUNC(op) \
426 void emit_D3D_##op(Context *ctx) { \
427 emit_D3D_opcode_s(ctx, #op); \
428 }
429 #define EMIT_D3D_OPCODE_SS_FUNC(op) \
430 void emit_D3D_##op(Context *ctx) { \
431 emit_D3D_opcode_ss(ctx, #op); \
432 }
433 #define EMIT_D3D_OPCODE_DS_FUNC(op) \
434 void emit_D3D_##op(Context *ctx) { \
435 emit_D3D_opcode_ds(ctx, #op); \
436 }
437 #define EMIT_D3D_OPCODE_DSS_FUNC(op) \
438 void emit_D3D_##op(Context *ctx) { \
439 emit_D3D_opcode_dss(ctx, #op); \
440 }
441 #define EMIT_D3D_OPCODE_DSSS_FUNC(op) \
442 void emit_D3D_##op(Context *ctx) { \
443 emit_D3D_opcode_dsss(ctx, #op); \
444 }
445 #define EMIT_D3D_OPCODE_DSSSS_FUNC(op) \
446 void emit_D3D_##op(Context *ctx) { \
447 emit_D3D_opcode_dssss(ctx, #op); \
448 }
449
450 EMIT_D3D_OPCODE_FUNC(NOP)
451 EMIT_D3D_OPCODE_DS_FUNC(MOV)
452 EMIT_D3D_OPCODE_DSS_FUNC(ADD)
453 EMIT_D3D_OPCODE_DSS_FUNC(SUB)
454 EMIT_D3D_OPCODE_DSSS_FUNC(MAD)
455 EMIT_D3D_OPCODE_DSS_FUNC(MUL)
456 EMIT_D3D_OPCODE_DS_FUNC(RCP)
457 EMIT_D3D_OPCODE_DS_FUNC(RSQ)
458 EMIT_D3D_OPCODE_DSS_FUNC(DP3)
459 EMIT_D3D_OPCODE_DSS_FUNC(DP4)
460 EMIT_D3D_OPCODE_DSS_FUNC(MIN)
461 EMIT_D3D_OPCODE_DSS_FUNC(MAX)
462 EMIT_D3D_OPCODE_DSS_FUNC(SLT)
463 EMIT_D3D_OPCODE_DSS_FUNC(SGE)
464 EMIT_D3D_OPCODE_DS_FUNC(EXP)
465 EMIT_D3D_OPCODE_DS_FUNC(LOG)
466 EMIT_D3D_OPCODE_DS_FUNC(LIT)
467 EMIT_D3D_OPCODE_DSS_FUNC(DST)
468 EMIT_D3D_OPCODE_DSSS_FUNC(LRP)
469 EMIT_D3D_OPCODE_DS_FUNC(FRC)
470 EMIT_D3D_OPCODE_DSS_FUNC(M4X4)
471 EMIT_D3D_OPCODE_DSS_FUNC(M4X3)
472 EMIT_D3D_OPCODE_DSS_FUNC(M3X4)
473 EMIT_D3D_OPCODE_DSS_FUNC(M3X3)
474 EMIT_D3D_OPCODE_DSS_FUNC(M3X2)
475 EMIT_D3D_OPCODE_S_FUNC(CALL)
476 EMIT_D3D_OPCODE_SS_FUNC(CALLNZ)
477 EMIT_D3D_OPCODE_SS_FUNC(LOOP)
478 EMIT_D3D_OPCODE_FUNC(RET)
479 EMIT_D3D_OPCODE_FUNC(ENDLOOP)
480 EMIT_D3D_OPCODE_S_FUNC(LABEL)
481 EMIT_D3D_OPCODE_DSS_FUNC(POW)
482 EMIT_D3D_OPCODE_DSS_FUNC(CRS)
483 EMIT_D3D_OPCODE_DSSS_FUNC(SGN)
484 EMIT_D3D_OPCODE_DS_FUNC(ABS)
485 EMIT_D3D_OPCODE_DS_FUNC(NRM)
486 EMIT_D3D_OPCODE_S_FUNC(REP)
487 EMIT_D3D_OPCODE_FUNC(ENDREP)
488 EMIT_D3D_OPCODE_S_FUNC(IF)
489 EMIT_D3D_OPCODE_FUNC(ELSE)
490 EMIT_D3D_OPCODE_FUNC(ENDIF)
491 EMIT_D3D_OPCODE_FUNC(BREAK)
492 EMIT_D3D_OPCODE_DS_FUNC(MOVA)
493 EMIT_D3D_OPCODE_D_FUNC(TEXKILL)
494 EMIT_D3D_OPCODE_DS_FUNC(TEXBEM)
495 EMIT_D3D_OPCODE_DS_FUNC(TEXBEML)
496 EMIT_D3D_OPCODE_DS_FUNC(TEXREG2AR)
497 EMIT_D3D_OPCODE_DS_FUNC(TEXREG2GB)
498 EMIT_D3D_OPCODE_DS_FUNC(TEXM3X2PAD)
499 EMIT_D3D_OPCODE_DS_FUNC(TEXM3X2TEX)
500 EMIT_D3D_OPCODE_DS_FUNC(TEXM3X3PAD)
501 EMIT_D3D_OPCODE_DS_FUNC(TEXM3X3TEX)
502 EMIT_D3D_OPCODE_DSS_FUNC(TEXM3X3SPEC)
503 EMIT_D3D_OPCODE_DS_FUNC(TEXM3X3VSPEC)
504 EMIT_D3D_OPCODE_DS_FUNC(EXPP)
505 EMIT_D3D_OPCODE_DS_FUNC(LOGP)
506 EMIT_D3D_OPCODE_DSSS_FUNC(CND)
507 EMIT_D3D_OPCODE_DS_FUNC(TEXREG2RGB)
508 EMIT_D3D_OPCODE_DS_FUNC(TEXDP3TEX)
509 EMIT_D3D_OPCODE_DS_FUNC(TEXM3X2DEPTH)
510 EMIT_D3D_OPCODE_DS_FUNC(TEXDP3)
511 EMIT_D3D_OPCODE_DS_FUNC(TEXM3X3)
512 EMIT_D3D_OPCODE_D_FUNC(TEXDEPTH)
513 EMIT_D3D_OPCODE_DSSS_FUNC(CMP)
514 EMIT_D3D_OPCODE_DSS_FUNC(BEM)
515 EMIT_D3D_OPCODE_DSSS_FUNC(DP2ADD)
516 EMIT_D3D_OPCODE_DS_FUNC(DSX)
517 EMIT_D3D_OPCODE_DS_FUNC(DSY)
518 EMIT_D3D_OPCODE_DSSSS_FUNC(TEXLDD)
519 EMIT_D3D_OPCODE_DSS_FUNC(TEXLDL)
520 EMIT_D3D_OPCODE_S_FUNC(BREAKP)
521
522 // special cases for comparison opcodes...
523 const char *get_D3D_comparison_string(Context *ctx)
524 {
525 const char *comps[] = {
526 "", "_gt", "_eq", "_ge", "_lt", "_ne", "_le"
527 };
528
529 if (ctx->instruction_controls >= STATICARRAYLEN(comps))
530 {
531 fail(ctx, "unknown comparison control");
532 return "";
533 } // if
534
535 return comps[ctx->instruction_controls];
536 } // get_D3D_comparison_string
537
538 void emit_D3D_BREAKC(Context *ctx)
539 {
540 char op[16];
541 snprintf(op, sizeof (op), "break%s", get_D3D_comparison_string(ctx));
542 emit_D3D_opcode_ss(ctx, op);
543 } // emit_D3D_BREAKC
544
545 void emit_D3D_IFC(Context *ctx)
546 {
547 char op[16];
548 snprintf(op, sizeof (op), "if%s", get_D3D_comparison_string(ctx));
549 emit_D3D_opcode_ss(ctx, op);
550 } // emit_D3D_IFC
551
552 void emit_D3D_SETP(Context *ctx)
553 {
554 char op[16];
555 snprintf(op, sizeof (op), "setp%s", get_D3D_comparison_string(ctx));
556 emit_D3D_opcode_dss(ctx, op);
557 } // emit_D3D_SETP
558
559 void emit_D3D_DEF(Context *ctx)
560 {
561 char dst[64];
562 make_D3D_destarg_string(ctx, dst, sizeof (dst));
563 const float *val = (const float *) ctx->dwords; // !!! FIXME: could be int?
564 char val0[32];
565 char val1[32];
566 char val2[32];
567 char val3[32];
568 floatstr(ctx, val0, sizeof (val0), val[0], 0);
569 floatstr(ctx, val1, sizeof (val1), val[1], 0);
570 floatstr(ctx, val2, sizeof (val2), val[2], 0);
571 floatstr(ctx, val3, sizeof (val3), val[3], 0);
572 output_line(ctx, "def%s, %s, %s, %s, %s", dst, val0, val1, val2, val3);
573 } // emit_D3D_DEF
574
575 void emit_D3D_DEFI(Context *ctx)
576 {
577 char dst[64];
578 make_D3D_destarg_string(ctx, dst, sizeof (dst));
579 const int32 *x = (const int32 *) ctx->dwords;
580 output_line(ctx, "defi%s, %d, %d, %d, %d", dst,
581 (int) x[0], (int) x[1], (int) x[2], (int) x[3]);
582 } // emit_D3D_DEFI
583
584 void emit_D3D_DEFB(Context *ctx)
585 {
586 char dst[64];
587 make_D3D_destarg_string(ctx, dst, sizeof (dst));
588 output_line(ctx, "defb%s, %s", dst, ctx->dwords[0] ? "true" : "false");
589 } // emit_D3D_DEFB
590
591
592 static const char *usagestrs[] = {
593 "_position", "_blendweight", "_blendindices", "_normal", "_psize",
594 "_texcoord", "_tangent", "_binormal", "_tessfactor", "_positiont",
595 "_color", "_fog", "_depth", "_sample"
596 };
597
598 void emit_D3D_DCL(Context *ctx)
599 {
600 char dst[64];
601 make_D3D_destarg_string(ctx, dst, sizeof (dst));
602 const DestArgInfo *arg = &ctx->dest_arg;
603 const char *usage_str = "";
604 char index_str[16] = { '\0' };
605
606 if (arg->regtype == REG_TYPE_SAMPLER)
607 {
608 switch ((const TextureType) ctx->dwords[0])
609 {
610 case TEXTURE_TYPE_2D: usage_str = "_2d"; break;
611 case TEXTURE_TYPE_CUBE: usage_str = "_cube"; break;
612 case TEXTURE_TYPE_VOLUME: usage_str = "_volume"; break;
613 default: fail(ctx, "unknown sampler texture type"); return;
614 } // switch
615 } // if
616
617 else if (arg->regtype == REG_TYPE_MISCTYPE)
618 {
619 switch ((const MiscTypeType) arg->regnum)
620 {
621 case MISCTYPE_TYPE_POSITION:
622 case MISCTYPE_TYPE_FACE:
623 usage_str = ""; // just become "dcl vFace" or whatever.
624 break;
625 default: fail(ctx, "unknown misc register type"); return;
626 } // switch
627 } // else if
628
629 else
630 {
631 const uint32 usage = ctx->dwords[0];
632 const uint32 index = ctx->dwords[1];
633 usage_str = usagestrs[usage];
634 if (index != 0)
635 snprintf(index_str, sizeof (index_str), "%u", (uint) index);
636 } // else
637
638 output_line(ctx, "dcl%s%s%s", usage_str, index_str, dst);
639 } // emit_D3D_DCL
640
641
642 void emit_D3D_TEXCRD(Context *ctx)
643 {
644 // this opcode looks and acts differently depending on the shader model.
645 if (shader_version_atleast(ctx, 1, 4))
646 emit_D3D_opcode_ds(ctx, "texcrd");
647 else
648 emit_D3D_opcode_d(ctx, "texcoord");
649 } // emit_D3D_TEXCOORD
650
651 void emit_D3D_TEXLD(Context *ctx)
652 {
653 // this opcode looks and acts differently depending on the shader model.
654 if (shader_version_atleast(ctx, 2, 0))
655 {
656 if (ctx->instruction_controls == CONTROL_TEXLD)
657 emit_D3D_opcode_dss(ctx, "texld");
658 else if (ctx->instruction_controls == CONTROL_TEXLDP)
659 emit_D3D_opcode_dss(ctx, "texldp");
660 else if (ctx->instruction_controls == CONTROL_TEXLDB)
661 emit_D3D_opcode_dss(ctx, "texldb");
662 } // if
663
664 else if (shader_version_atleast(ctx, 1, 4))
665 {
666 emit_D3D_opcode_ds(ctx, "texld");
667 } // else if
668
669 else
670 {
671 emit_D3D_opcode_d(ctx, "tex");
672 } // else
673 } // emit_D3D_TEXLD
674
675 void emit_D3D_SINCOS(Context *ctx)
676 {
677 // this opcode needs extra registers for sm2 and lower.
678 if (!shader_version_atleast(ctx, 3, 0))
679 emit_D3D_opcode_dsss(ctx, "sincos");
680 else
681 emit_D3D_opcode_ds(ctx, "sincos");
682 } // emit_D3D_SINCOS
683
684 #endif // SUPPORT_PROFILE_D3D
685
686 #pragma GCC visibility pop