Debian Codegraph @

profiles/mojoshader_profile_arb1.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_ARB1
16
17 static inline const char *get_ARB1_register_string(Context *ctx,
18 const RegisterType regtype, const int regnum,
19 char *regnum_str, const size_t regnum_size)
20 {
21 // turns out these are identical at the moment.
22 return get_D3D_register_string(ctx,regtype,regnum,regnum_str,regnum_size);
23 } // get_ARB1_register_string
24
25 int allocate_scratch_register(Context *ctx)
26 {
27 const int retval = ctx->scratch_registers++;
28 if (retval >= ctx->max_scratch_registers)
29 ctx->max_scratch_registers = retval + 1;
30 return retval;
31 } // allocate_scratch_register
32
33 int allocate_branch_label(Context *ctx)
34 {
35 return ctx->assigned_branch_labels++;
36 } // allocate_branch_label
37
38 const char *allocate_ARB1_scratch_reg_name(Context *ctx, char *buf,
39 const size_t buflen)
40 {
41 const int scratch = allocate_scratch_register(ctx);
42 snprintf(buf, buflen, "scratch%d", scratch);
43 return buf;
44 } // allocate_ARB1_scratch_reg_name
45
46 static inline const char *get_ARB1_branch_label_name(Context *ctx, const int id,
47 char *buf, const size_t buflen)
48 {
49 snprintf(buf, buflen, "branch_label%d", id);
50 return buf;
51 } // get_ARB1_branch_label_name
52
53 const char *get_ARB1_varname_in_buf(Context *ctx, const RegisterType rt,
54 const int regnum, char *buf,
55 const size_t buflen)
56 {
57 // turns out these are identical at the moment.
58 return get_D3D_varname_in_buf(ctx, rt, regnum, buf, buflen);
59 } // get_ARB1_varname_in_buf
60
61 const char *get_ARB1_varname(Context *ctx, const RegisterType rt,
62 const int regnum)
63 {
64 // turns out these are identical at the moment.
65 return get_D3D_varname(ctx, rt, regnum);
66 } // get_ARB1_varname
67
68
69 static inline const char *get_ARB1_const_array_varname_in_buf(Context *ctx,
70 const int base, const int size,
71 char *buf, const size_t buflen)
72 {
73 snprintf(buf, buflen, "c_array_%d_%d", base, size);
74 return buf;
75 } // get_ARB1_const_array_varname_in_buf
76
77
78 const char *get_ARB1_const_array_varname(Context *ctx, int base, int size)
79 {
80 char buf[64];
81 get_ARB1_const_array_varname_in_buf(ctx, base, size, buf, sizeof (buf));
82 return StrDup(ctx, buf);
83 } // get_ARB1_const_array_varname
84
85
86 const char *make_ARB1_srcarg_string_in_buf(Context *ctx,
87 const SourceArgInfo *arg,
88 char *buf, size_t buflen)
89 {
90 // !!! FIXME: this can hit pathological cases where we look like this...
91 //
92 // dp3 r1.xyz, t0_bx2, t0_bx2
93 // mad r1.xyz, t0_bias, 1-r1, t0_bx2
94 //
95 // ...which do a lot of duplicate work in arb1...
96 //
97 // SUB scratch0, t0, { 0.5, 0.5, 0.5, 0.5 };
98 // MUL scratch0, scratch0, { 2.0, 2.0, 2.0, 2.0 };
99 // SUB scratch1, t0, { 0.5, 0.5, 0.5, 0.5 };
100 // MUL scratch1, scratch1, { 2.0, 2.0, 2.0, 2.0 };
101 // DP3 r1.xyz, scratch0, scratch1;
102 // SUB scratch0, t0, { 0.5, 0.5, 0.5, 0.5 };
103 // SUB scratch1, { 1.0, 1.0, 1.0, 1.0 }, r1;
104 // SUB scratch2, t0, { 0.5, 0.5, 0.5, 0.5 };
105 // MUL scratch2, scratch2, { 2.0, 2.0, 2.0, 2.0 };
106 // MAD r1.xyz, scratch0, scratch1, scratch2;
107 //
108 // ...notice that the dp3 calculates the same value into two scratch
109 // registers. This case is easier to handle; just see if multiple
110 // source args are identical, build it up once, and use the same
111 // scratch register for multiple arguments in that opcode.
112 // Even better still, only calculate things once across instructions,
113 // and be smart about letting it linger in a scratch register until we
114 // definitely don't need the calculation anymore. That's harder to
115 // write, though.
116
117 char regnum_str[16] = { '\0' };
118
119 // !!! FIXME: use get_ARB1_varname_in_buf() instead?
120 const char *regtype_str = NULL;
121 if (!arg->relative)
122 {
123 regtype_str = get_ARB1_register_string(ctx, arg->regtype,
124 arg->regnum, regnum_str,
125 sizeof (regnum_str));
126 } // if
127
128 const char *rel_lbracket = "";
129 char rel_offset[32] = { '\0' };
130 const char *rel_rbracket = "";
131 char rel_swizzle[4] = { '\0' };
132 const char *rel_regtype_str = "";
133 if (arg->relative)
134 {
135 rel_regtype_str = get_ARB1_varname_in_buf(ctx, arg->relative_regtype,
136 arg->relative_regnum,
137 (char *) alloca(64), 64);
138
139 rel_swizzle[0] = '.';
140 rel_swizzle[1] = swizzle_channels[arg->relative_component];
141 rel_swizzle[2] = '\0';
142
143 if (!support_nv2(ctx))
144 {
145 // The address register in ARB1 only allows the '.x' component, so
146 // we need to load the component we need from a temp vector
147 // register into .x as needed.
148 assert(arg->relative_regtype == REG_TYPE_ADDRESS);
149 assert(arg->relative_regnum == 0);
150 if (ctx->last_address_reg_component != arg->relative_component)
151 {
152 output_line(ctx, "ARL %s.x, addr%d.%c;", rel_regtype_str,
153 arg->relative_regnum,
154 swizzle_channels[arg->relative_component]);
155 ctx->last_address_reg_component = arg->relative_component;
156 } // if
157
158 rel_swizzle[1] = 'x';
159 } // if
160
161 if (arg->regtype == REG_TYPE_INPUT)
162 regtype_str = "vertex.attrib";
163 else
164 {
165 assert(arg->regtype == REG_TYPE_CONST);
166 const int arrayidx = arg->relative_array->index;
167 const int arraysize = arg->relative_array->count;
168 const int offset = arg->regnum - arrayidx;
169 assert(offset >= 0);
170 regtype_str = get_ARB1_const_array_varname_in_buf(ctx, arrayidx,
171 arraysize, (char *) alloca(64), 64);
172 if (offset != 0)
173 snprintf(rel_offset, sizeof (rel_offset), " + %d", offset);
174 } // else
175
176 rel_lbracket = "[";
177 rel_rbracket = "]";
178 } // if
179
180 // This is the source register with everything but swizzle and source mods.
181 snprintf(buf, buflen, "%s%s%s%s%s%s%s", regtype_str, regnum_str,
182 rel_lbracket, rel_regtype_str, rel_swizzle, rel_offset,
183 rel_rbracket);
184
185 // Some of the source mods need to generate instructions to a temp
186 // register, in which case we'll replace the register name.
187 const SourceMod mod = arg->src_mod;
188 const int inplace = ( (mod == SRCMOD_NONE) || (mod == SRCMOD_NEGATE) ||
189 ((mod == SRCMOD_ABS) && support_nv2(ctx)) );
190
191 if (!inplace)
192 {
193 const size_t len = 64;
194 char *stackbuf = (char *) alloca(len);
195 regtype_str = allocate_ARB1_scratch_reg_name(ctx, stackbuf, len);
196 regnum_str[0] = '\0'; // move value to scratch register.
197 rel_lbracket = ""; // scratch register won't use array.
198 rel_rbracket = "";
199 rel_offset[0] = '\0';
200 rel_swizzle[0] = '\0';
201 rel_regtype_str = "";
202 } // if
203
204 const char *premod_str = "";
205 const char *postmod_str = "";
206 switch (mod)
207 {
208 case SRCMOD_NEGATE:
209 premod_str = "-";
210 break;
211
212 case SRCMOD_BIASNEGATE:
213 premod_str = "-";
214 // fall through.
215 case SRCMOD_BIAS:
216 output_line(ctx, "SUB %s, %s, { 0.5, 0.5, 0.5, 0.5 };",
217 regtype_str, buf);
218 break;
219
220 case SRCMOD_SIGNNEGATE:
221 premod_str = "-";
222 // fall through.
223 case SRCMOD_SIGN:
224 output_line(ctx,
225 "MAD %s, %s, { 2.0, 2.0, 2.0, 2.0 }, { -1.0, -1.0, -1.0, -1.0 };",
226 regtype_str, buf);
227 break;
228
229 case SRCMOD_COMPLEMENT:
230 output_line(ctx, "SUB %s, { 1.0, 1.0, 1.0, 1.0 }, %s;",
231 regtype_str, buf);
232 break;
233
234 case SRCMOD_X2NEGATE:
235 premod_str = "-";
236 // fall through.
237 case SRCMOD_X2:
238 output_line(ctx, "MUL %s, %s, { 2.0, 2.0, 2.0, 2.0 };",
239 regtype_str, buf);
240 break;
241
242 case SRCMOD_DZ:
243 fail(ctx, "SRCMOD_DZ currently unsupported in arb1");
244 postmod_str = "_dz";
245 break;
246
247 case SRCMOD_DW:
248 fail(ctx, "SRCMOD_DW currently unsupported in arb1");
249 postmod_str = "_dw";
250 break;
251
252 case SRCMOD_ABSNEGATE:
253 premod_str = "-";
254 // fall through.
255 case SRCMOD_ABS:
256 if (!support_nv2(ctx)) // GL_NV_vertex_program2_option adds this.
257 output_line(ctx, "ABS %s, %s;", regtype_str, buf);
258 else
259 {
260 premod_str = (mod == SRCMOD_ABSNEGATE) ? "-|" : "|";
261 postmod_str = "|";
262 } // else
263 break;
264
265 case SRCMOD_NOT:
266 fail(ctx, "SRCMOD_NOT currently unsupported in arb1");
267 premod_str = "!";
268 break;
269
270 case SRCMOD_NONE:
271 case SRCMOD_TOTAL:
272 break; // stop compiler whining.
273 } // switch
274
275 char swizzle_str[6];
276 size_t i = 0;
277
278 if (support_nv4(ctx)) // vFace must be output as "vFace.x" in nv4.
279 {
280 if (arg->regtype == REG_TYPE_MISCTYPE)
281 {
282 if ( ((const MiscTypeType) arg->regnum) == MISCTYPE_TYPE_FACE )
283 {
284 swizzle_str[i++] = '.';
285 swizzle_str[i++] = 'x';
286 } // if
287 } // if
288 } // if
289
290 const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
291 if (!scalar && !no_swizzle(arg->swizzle))
292 {
293 swizzle_str[i++] = '.';
294
295 // .xxxx is the same as .x, but .xx is illegal...scalar or full!
296 if (replicate_swizzle(arg->swizzle))
297 swizzle_str[i++] = swizzle_channels[arg->swizzle_x];
298 else
299 {
300 swizzle_str[i++] = swizzle_channels[arg->swizzle_x];
301 swizzle_str[i++] = swizzle_channels[arg->swizzle_y];
302 swizzle_str[i++] = swizzle_channels[arg->swizzle_z];
303 swizzle_str[i++] = swizzle_channels[arg->swizzle_w];
304 } // else
305 } // if
306 swizzle_str[i] = '\0';
307 assert(i < sizeof (swizzle_str));
308
309 snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s", premod_str,
310 regtype_str, regnum_str, rel_lbracket,
311 rel_regtype_str, rel_swizzle, rel_offset, rel_rbracket,
312 swizzle_str, postmod_str);
313 // !!! FIXME: make sure the scratch buffer was large enough.
314 return buf;
315 } // make_ARB1_srcarg_string_in_buf
316
317 const char *get_ARB1_destarg_varname(Context *ctx, char *buf,
318 const size_t buflen)
319 {
320 const DestArgInfo *arg = &ctx->dest_arg;
321 return get_ARB1_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, buflen);
322 } // get_ARB1_destarg_varname
323
324 const char *get_ARB1_srcarg_varname(Context *ctx, const size_t idx,
325 char *buf, const size_t buflen)
326 {
327 if (idx >= STATICARRAYLEN(ctx->source_args))
328 {
329 fail(ctx, "Too many source args");
330 *buf = '\0';
331 return buf;
332 } // if
333
334 const SourceArgInfo *arg = &ctx->source_args[idx];
335 return get_ARB1_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, buflen);
336 } // get_ARB1_srcarg_varname
337
338
339 const char *make_ARB1_destarg_string(Context *ctx, char *buf,
340 const size_t buflen)
341 {
342 const DestArgInfo *arg = &ctx->dest_arg;
343
344 *buf = '\0';
345
346 const char *sat_str = "";
347 if (arg->result_mod & MOD_SATURATE)
348 {
349 // nv4 can use ".SAT" in all program types.
350 // For less than nv4, the "_SAT" modifier is only available in
351 // fragment shaders. Every thing else will fake it later in
352 // emit_ARB1_dest_modifiers() ...
353 if (support_nv4(ctx))
354 sat_str = ".SAT";
355 else if (shader_is_pixel(ctx))
356 sat_str = "_SAT";
357 } // if
358
359 const char *pp_str = "";
360 if (arg->result_mod & MOD_PP)
361 {
362 // Most ARB1 profiles can't do partial precision (MOD_PP), but that's
363 // okay. The spec says lots of Direct3D implementations ignore the
364 // flag anyhow.
365 if (support_nv4(ctx))
366 pp_str = "H";
367 } // if
368
369 // CENTROID only allowed in DCL opcodes, which shouldn't come through here.
370 assert((arg->result_mod & MOD_CENTROID) == 0);
371
372 char regnum_str[16];
373 const char *regtype_str = get_ARB1_register_string(ctx, arg->regtype,
374 arg->regnum, regnum_str,
375 sizeof (regnum_str));
376 if (regtype_str == NULL)
377 {
378 fail(ctx, "Unknown destination register type.");
379 return buf;
380 } // if
381
382 char writemask_str[6];
383 size_t i = 0;
384 const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
385 if (!scalar && !writemask_xyzw(arg->writemask))
386 {
387 writemask_str[i++] = '.';
388 if (arg->writemask0) writemask_str[i++] = 'x';
389 if (arg->writemask1) writemask_str[i++] = 'y';
390 if (arg->writemask2) writemask_str[i++] = 'z';
391 if (arg->writemask3) writemask_str[i++] = 'w';
392 } // if
393 writemask_str[i] = '\0';
394 assert(i < sizeof (writemask_str));
395
396 //const char *pred_left = "";
397 //const char *pred_right = "";
398 char pred[32] = { '\0' };
399 if (ctx->predicated)
400 {
401 fail(ctx, "dest register predication currently unsupported in arb1");
402 return buf;
403 //pred_left = "(";
404 //pred_right = ") ";
405 make_ARB1_srcarg_string_in_buf(ctx, &ctx->predicate_arg,
406 pred, sizeof (pred));
407 } // if
408
409 snprintf(buf, buflen, "%s%s %s%s%s", pp_str, sat_str,
410 regtype_str, regnum_str, writemask_str);
411 // !!! FIXME: make sure the scratch buffer was large enough.
412 return buf;
413 } // make_ARB1_destarg_string
414
415
416 void emit_ARB1_dest_modifiers(Context *ctx)
417 {
418 const DestArgInfo *arg = &ctx->dest_arg;
419
420 if (arg->result_shift != 0x0)
421 {
422 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
423 const char *multiplier = NULL;
424
425 switch (arg->result_shift)
426 {
427 case 0x1: multiplier = "2.0"; break;
428 case 0x2: multiplier = "4.0"; break;
429 case 0x3: multiplier = "8.0"; break;
430 case 0xD: multiplier = "0.125"; break;
431 case 0xE: multiplier = "0.25"; break;
432 case 0xF: multiplier = "0.5"; break;
433 } // switch
434
435 if (multiplier != NULL)
436 {
437 char var[64]; get_ARB1_destarg_varname(ctx, var, sizeof (var));
438 output_line(ctx, "MUL%s, %s, %s;", dst, var, multiplier);
439 } // if
440 } // if
441
442 if (arg->result_mod & MOD_SATURATE)
443 {
444 // nv4 and/or pixel shaders just used the "SAT" modifier, instead.
445 if ( (!support_nv4(ctx)) && (!shader_is_pixel(ctx)) )
446 {
447 char var[64]; get_ARB1_destarg_varname(ctx, var, sizeof (var));
448 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
449 output_line(ctx, "MIN%s, %s, 1.0;", dst, var);
450 output_line(ctx, "MAX%s, %s, 0.0;", dst, var);
451 } // if
452 } // if
453 } // emit_ARB1_dest_modifiers
454
455
456 const char *make_ARB1_srcarg_string(Context *ctx, const size_t idx,
457 char *buf, const size_t buflen)
458 {
459 if (idx >= STATICARRAYLEN(ctx->source_args))
460 {
461 fail(ctx, "Too many source args");
462 *buf = '\0';
463 return buf;
464 } // if
465
466 const SourceArgInfo *arg = &ctx->source_args[idx];
467 return make_ARB1_srcarg_string_in_buf(ctx, arg, buf, buflen);
468 } // make_ARB1_srcarg_string
469
470 void emit_ARB1_opcode_ds(Context *ctx, const char *opcode)
471 {
472 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
473 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
474 output_line(ctx, "%s%s, %s;", opcode, dst, src0);
475 emit_ARB1_dest_modifiers(ctx);
476 } // emit_ARB1_opcode_ds
477
478 void emit_ARB1_opcode_dss(Context *ctx, const char *opcode)
479 {
480 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
481 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
482 char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
483 output_line(ctx, "%s%s, %s, %s;", opcode, dst, src0, src1);
484 emit_ARB1_dest_modifiers(ctx);
485 } // emit_ARB1_opcode_dss
486
487 void emit_ARB1_opcode_dsss(Context *ctx, const char *opcode)
488 {
489 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
490 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
491 char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
492 char src2[64]; make_ARB1_srcarg_string(ctx, 2, src2, sizeof (src2));
493 output_line(ctx, "%s%s, %s, %s, %s;", opcode, dst, src0, src1, src2);
494 emit_ARB1_dest_modifiers(ctx);
495 } // emit_ARB1_opcode_dsss
496
497
498 #define EMIT_ARB1_OPCODE_FUNC(op) \
499 void emit_ARB1_##op(Context *ctx) { \
500 emit_ARB1_opcode(ctx, #op); \
501 }
502 #define EMIT_ARB1_OPCODE_D_FUNC(op) \
503 void emit_ARB1_##op(Context *ctx) { \
504 emit_ARB1_opcode_d(ctx, #op); \
505 }
506 #define EMIT_ARB1_OPCODE_S_FUNC(op) \
507 void emit_ARB1_##op(Context *ctx) { \
508 emit_ARB1_opcode_s(ctx, #op); \
509 }
510 #define EMIT_ARB1_OPCODE_SS_FUNC(op) \
511 void emit_ARB1_##op(Context *ctx) { \
512 emit_ARB1_opcode_ss(ctx, #op); \
513 }
514 #define EMIT_ARB1_OPCODE_DS_FUNC(op) \
515 void emit_ARB1_##op(Context *ctx) { \
516 emit_ARB1_opcode_ds(ctx, #op); \
517 }
518 #define EMIT_ARB1_OPCODE_DSS_FUNC(op) \
519 void emit_ARB1_##op(Context *ctx) { \
520 emit_ARB1_opcode_dss(ctx, #op); \
521 }
522 #define EMIT_ARB1_OPCODE_DSSS_FUNC(op) \
523 void emit_ARB1_##op(Context *ctx) { \
524 emit_ARB1_opcode_dsss(ctx, #op); \
525 }
526 #define EMIT_ARB1_OPCODE_DSSSS_FUNC(op) \
527 void emit_ARB1_##op(Context *ctx) { \
528 emit_ARB1_opcode_dssss(ctx, #op); \
529 }
530 #define EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(op) \
531 void emit_ARB1_##op(Context *ctx) { \
532 failf(ctx, #op " unimplemented in %s profile", ctx->profile->name); \
533 }
534
535
536 void emit_ARB1_start(Context *ctx, const char *profilestr)
537 {
538 const char *shader_str = NULL;
539 const char *shader_full_str = NULL;
540 if (shader_is_vertex(ctx))
541 {
542 shader_str = "vp";
543 shader_full_str = "vertex";
544 } // if
545 else if (shader_is_pixel(ctx))
546 {
547 shader_str = "fp";
548 shader_full_str = "fragment";
549 } // else if
550 else
551 {
552 failf(ctx, "Shader type %u unsupported in this profile.",
553 (uint) ctx->shader_type);
554 return;
555 } // if
556
557 set_output(ctx, &ctx->preflight);
558
559 if (strcmp(profilestr, MOJOSHADER_PROFILE_ARB1) == 0)
560 output_line(ctx, "!!ARB%s1.0", shader_str);
561
562 #if SUPPORT_PROFILE_ARB1_NV
563 else if (strcmp(profilestr, MOJOSHADER_PROFILE_NV2) == 0)
564 {
565 ctx->profile_supports_nv2 = 1;
566 output_line(ctx, "!!ARB%s1.0", shader_str);
567 output_line(ctx, "OPTION NV_%s_program2;", shader_full_str);
568 } // else if
569
570 else if (strcmp(profilestr, MOJOSHADER_PROFILE_NV3) == 0)
571 {
572 // there's no NV_fragment_program3, so just use 2.
573 const int ver = shader_is_pixel(ctx) ? 2 : 3;
574 ctx->profile_supports_nv2 = 1;
575 ctx->profile_supports_nv3 = 1;
576 output_line(ctx, "!!ARB%s1.0", shader_str);
577 output_line(ctx, "OPTION NV_%s_program%d;", shader_full_str, ver);
578 } // else if
579
580 else if (strcmp(profilestr, MOJOSHADER_PROFILE_NV4) == 0)
581 {
582 ctx->profile_supports_nv2 = 1;
583 ctx->profile_supports_nv3 = 1;
584 ctx->profile_supports_nv4 = 1;
585 output_line(ctx, "!!NV%s4.0", shader_str);
586 } // else if
587 #endif
588
589 else
590 {
591 failf(ctx, "Profile '%s' unsupported or unknown.", profilestr);
592 } // else
593
594 set_output(ctx, &ctx->mainline);
595 } // emit_ARB1_start
596
597 void emit_ARB1_end(Context *ctx)
598 {
599 // ps_1_* writes color to r0 instead oC0. We move it to the right place.
600 // We don't have to worry about a RET opcode messing this up, since
601 // RET isn't available before ps_2_0.
602 if (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 2, 0))
603 {
604 set_used_register(ctx, REG_TYPE_COLOROUT, 0, 1);
605 output_line(ctx, "MOV oC0, r0;");
606 } // if
607
608 output_line(ctx, "END");
609 } // emit_ARB1_end
610
611 void emit_ARB1_phase(Context *ctx)
612 {
613 // no-op in arb1.
614 } // emit_ARB1_phase
615
616 static inline const char *arb1_float_temp(const Context *ctx)
617 {
618 // nv4 lets you specify data type.
619 return (support_nv4(ctx)) ? "FLOAT TEMP" : "TEMP";
620 } // arb1_float_temp
621
622 void emit_ARB1_finalize(Context *ctx)
623 {
624 push_output(ctx, &ctx->preflight);
625
626 if (shader_is_vertex(ctx) && !ctx->arb1_wrote_position)
627 output_line(ctx, "OPTION ARB_position_invariant;");
628
629 if (shader_is_pixel(ctx) && ctx->have_multi_color_outputs)
630 output_line(ctx, "OPTION ARB_draw_buffers;");
631
632 pop_output(ctx);
633
634 const char *tmpstr = arb1_float_temp(ctx);
635 int i;
636 push_output(ctx, &ctx->globals);
637 for (i = 0; i < ctx->max_scratch_registers; i++)
638 {
639 char buf[64];
640 allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
641 output_line(ctx, "%s %s;", tmpstr, buf);
642 } // for
643
644 // nv2 fragment programs (and anything nv4) have a real REP/ENDREP.
645 if ( (support_nv2(ctx)) && (!shader_is_pixel(ctx)) && (!support_nv4(ctx)) )
646 {
647 // set up temps for nv2 REP/ENDREP emulation through branching.
648 for (i = 0; i < ctx->max_reps; i++)
649 output_line(ctx, "TEMP rep%d;", i);
650 } // if
651
652 pop_output(ctx);
653 assert(ctx->scratch_registers == ctx->max_scratch_registers);
654 } // emit_ARB1_finalize
655
656 void emit_ARB1_global(Context *ctx, RegisterType regtype, int regnum)
657 {
658 // !!! FIXME: dependency on ARB1 profile. // !!! FIXME about FIXME: huh?
659 char varname[64];
660 get_ARB1_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
661
662 push_output(ctx, &ctx->globals);
663 switch (regtype)
664 {
665 case REG_TYPE_ADDRESS:
666 if (shader_is_pixel(ctx)) // actually REG_TYPE_TEXTURE.
667 {
668 // We have to map texture registers to temps for ps_1_1, since
669 // they work like temps, initialize with tex coords, and the
670 // ps_1_1 TEX opcode expects to overwrite it.
671 if (!shader_version_atleast(ctx, 1, 4))
672 {
673 output_line(ctx, "%s %s;", arb1_float_temp(ctx), varname);
674 push_output(ctx, &ctx->mainline_top);
675 output_line(ctx, "MOV %s, fragment.texcoord[%d];",
676 varname, regnum);
677 pop_output(ctx);
678 } // if
679 break;
680 } // if
681
682 // nv4 replaced address registers with generic int registers.
683 if (support_nv4(ctx))
684 output_line(ctx, "INT TEMP %s;", varname);
685 else
686 {
687 // nv2 has four-component address already, but stock arb1 has
688 // to emulate it in a temporary, and move components to the
689 // scalar ADDRESS register on demand.
690 output_line(ctx, "ADDRESS %s;", varname);
691 if (!support_nv2(ctx))
692 output_line(ctx, "TEMP addr%d;", regnum);
693 } // else
694 break;
695
696 //case REG_TYPE_PREDICATE:
697 // output_line(ctx, "bvec4 %s;", varname);
698 // break;
699 case REG_TYPE_TEMP:
700 output_line(ctx, "%s %s;", arb1_float_temp(ctx), varname);
701 break;
702 //case REG_TYPE_LOOP:
703 // break; // no-op. We declare these in for loops at the moment.
704 //case REG_TYPE_LABEL:
705 // break; // no-op. If we see it here, it means we optimized it out.
706 default:
707 fail(ctx, "BUG: we used a register we don't know how to define.");
708 break;
709 } // switch
710 pop_output(ctx);
711 } // emit_ARB1_global
712
713 void emit_ARB1_array(Context *ctx, VariableList *var)
714 {
715 // All uniforms are now packed tightly into the program.local array,
716 // instead of trying to map them to the d3d registers. So this needs to
717 // map to the next piece of the array we haven't used yet. Thankfully,
718 // arb1 lets you make a PARAM array that maps to a subset of another
719 // array; we don't need to do offsets, since myarray[0] can map to
720 // program.local[5] without any extra math from us.
721 const int base = var->index;
722 const int size = var->count;
723 const int arb1base = ctx->uniform_float4_count +
724 ctx->uniform_int4_count +
725 ctx->uniform_bool_count;
726 char varname[64];
727 get_ARB1_const_array_varname_in_buf(ctx, base, size, varname, sizeof (varname));
728 push_output(ctx, &ctx->globals);
729 output_line(ctx, "PARAM %s[%d] = { program.local[%d..%d] };", varname,
730 size, arb1base, (arb1base + size) - 1);
731 pop_output(ctx);
732 var->emit_position = arb1base;
733 } // emit_ARB1_array
734
735 void emit_ARB1_const_array(Context *ctx, const ConstantsList *clist,
736 int base, int size)
737 {
738 char varname[64];
739 get_ARB1_const_array_varname_in_buf(ctx, base, size, varname, sizeof (varname));
740 int i;
741
742 push_output(ctx, &ctx->globals);
743 output_line(ctx, "PARAM %s[%d] = {", varname, size);
744 ctx->indent++;
745
746 for (i = 0; i < size; i++)
747 {
748 while (clist->constant.type != MOJOSHADER_UNIFORM_FLOAT)
749 clist = clist->next;
750 assert(clist->constant.index == (base + i));
751
752 char val0[32];
753 char val1[32];
754 char val2[32];
755 char val3[32];
756 floatstr(ctx, val0, sizeof (val0), clist->constant.value.f[0], 1);
757 floatstr(ctx, val1, sizeof (val1), clist->constant.value.f[1], 1);
758 floatstr(ctx, val2, sizeof (val2), clist->constant.value.f[2], 1);
759 floatstr(ctx, val3, sizeof (val3), clist->constant.value.f[3], 1);
760
761 output_line(ctx, "{ %s, %s, %s, %s }%s", val0, val1, val2, val3,
762 (i < (size-1)) ? "," : "");
763
764 clist = clist->next;
765 } // for
766
767 ctx->indent--;
768 output_line(ctx, "};");
769 pop_output(ctx);
770 } // emit_ARB1_const_array
771
772 void emit_ARB1_uniform(Context *ctx, RegisterType regtype, int regnum,
773 const VariableList *var)
774 {
775 // We pack these down into the program.local array, so if we only use
776 // register c439, it'll actually map to program.local[0]. This will
777 // prevent overflows when we actually have enough resources to run.
778
779 const char *arrayname = "program.local";
780 int index = 0;
781
782 char varname[64];
783 get_ARB1_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
784
785 push_output(ctx, &ctx->globals);
786
787 if (var == NULL)
788 {
789 // all types share one array (rather, all types convert to float4).
790 index = ctx->uniform_float4_count + ctx->uniform_int4_count +
791 ctx->uniform_bool_count;
792 } // if
793
794 else
795 {
796 const int arraybase = var->index;
797 if (var->constant)
798 {
799 const int arraysize = var->count;
800 arrayname = get_ARB1_const_array_varname_in_buf(ctx, arraybase,
801 arraysize, (char *) alloca(64), 64);
802 index = (regnum - arraybase);
803 } // if
804 else
805 {
806 assert(var->emit_position != -1);
807 index = (regnum - arraybase) + var->emit_position;
808 } // else
809 } // else
810
811 output_line(ctx, "PARAM %s = %s[%d];", varname, arrayname, index);
812 pop_output(ctx);
813 } // emit_ARB1_uniform
814
815 void emit_ARB1_sampler(Context *ctx,int stage,TextureType ttype,int tb)
816 {
817 // this is mostly a no-op...you don't predeclare samplers in arb1.
818
819 if (tb) // This sampler used a ps_1_1 TEXBEM opcode?
820 {
821 const int index = ctx->uniform_float4_count + ctx->uniform_int4_count +
822 ctx->uniform_bool_count;
823 char var[64];
824 get_ARB1_varname_in_buf(ctx, REG_TYPE_SAMPLER, stage, var, sizeof(var));
825 push_output(ctx, &ctx->globals);
826 output_line(ctx, "PARAM %s_texbem = program.local[%d];", var, index);
827 output_line(ctx, "PARAM %s_texbeml = program.local[%d];", var, index+1);
828 pop_output(ctx);
829 ctx->uniform_float4_count += 2;
830 } // if
831 } // emit_ARB1_sampler
832
833 // !!! FIXME: a lot of cut-and-paste here from emit_GLSL_attribute().
834 void emit_ARB1_attribute(Context *ctx, RegisterType regtype, int regnum,
835 MOJOSHADER_usage usage, int index, int wmask,
836 int flags)
837 {
838 // !!! FIXME: this function doesn't deal with write masks at all yet!
839 const char *usage_str = NULL;
840 const char *arrayleft = "";
841 const char *arrayright = "";
842 char index_str[16] = { '\0' };
843
844 char varname[64];
845 get_ARB1_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
846
847 //assert((flags & MOD_PP) == 0); // !!! FIXME: is PP allowed?
848
849 if (index != 0) // !!! FIXME: a lot of these MUST be zero.
850 snprintf(index_str, sizeof (index_str), "%u", (uint) index);
851
852 if (shader_is_vertex(ctx))
853 {
854 // pre-vs3 output registers.
855 // these don't ever happen in DCL opcodes, I think. Map to vs_3_*
856 // output registers.
857 if (!shader_version_atleast(ctx, 3, 0))
858 {
859 if (regtype == REG_TYPE_RASTOUT)
860 {
861 regtype = REG_TYPE_OUTPUT;
862 index = regnum;
863 switch ((const RastOutType) regnum)
864 {
865 case RASTOUT_TYPE_POSITION:
866 usage = MOJOSHADER_USAGE_POSITION;
867 break;
868 case RASTOUT_TYPE_FOG:
869 usage = MOJOSHADER_USAGE_FOG;
870 break;
871 case RASTOUT_TYPE_POINT_SIZE:
872 usage = MOJOSHADER_USAGE_POINTSIZE;
873 break;
874 } // switch
875 } // if
876
877 else if (regtype == REG_TYPE_ATTROUT)
878 {
879 regtype = REG_TYPE_OUTPUT;
880 usage = MOJOSHADER_USAGE_COLOR;
881 index = regnum;
882 } // else if
883
884 else if (regtype == REG_TYPE_TEXCRDOUT)
885 {
886 regtype = REG_TYPE_OUTPUT;
887 usage = MOJOSHADER_USAGE_TEXCOORD;
888 index = regnum;
889 } // else if
890 } // if
891
892 // to avoid limitations of various GL entry points for input
893 // attributes (glSecondaryColorPointer() can only take 3 component
894 // items, glVertexPointer() can't do GL_UNSIGNED_BYTE, many other
895 // issues), we set up all inputs as generic vertex attributes, so we
896 // can pass data in just about any form, and ignore the built-in GLSL
897 // attributes like gl_SecondaryColor. Output needs to use the the
898 // built-ins, though, but we don't have to worry about the GL entry
899 // point limitations there.
900
901 if (regtype == REG_TYPE_INPUT)
902 {
903 const int attr = ctx->assigned_vertex_attributes++;
904 push_output(ctx, &ctx->globals);
905 output_line(ctx, "ATTRIB %s = vertex.attrib[%d];", varname, attr);
906 pop_output(ctx);
907 } // if
908
909 else if (regtype == REG_TYPE_OUTPUT)
910 {
911 switch (usage)
912 {
913 case MOJOSHADER_USAGE_POSITION:
914 ctx->arb1_wrote_position = 1;
915 usage_str = "result.position";
916 break;
917 case MOJOSHADER_USAGE_POINTSIZE:
918 usage_str = "result.pointsize";
919 break;
920 case MOJOSHADER_USAGE_COLOR:
921 index_str[0] = '\0'; // no explicit number.
922 if (index == 0)
923 usage_str = "result.color.primary";
924 else if (index == 1)
925 usage_str = "result.color.secondary";
926 break;
927 case MOJOSHADER_USAGE_FOG:
928 usage_str = "result.fogcoord";
929 break;
930 case MOJOSHADER_USAGE_TEXCOORD:
931 snprintf(index_str, sizeof (index_str), "%u", (uint) index);
932 usage_str = "result.texcoord";
933 arrayleft = "[";
934 arrayright = "]";
935 break;
936 default:
937 // !!! FIXME: we need to deal with some more built-in varyings here.
938 break;
939 } // switch
940
941 // !!! FIXME: the #define is a little hacky, but it means we don't
942 // !!! FIXME: have to track these separately if this works.
943 push_output(ctx, &ctx->globals);
944 // no mapping to built-in var? Just make it a regular global, pray.
945 if (usage_str == NULL)
946 output_line(ctx, "%s %s;", arb1_float_temp(ctx), varname);
947 else
948 {
949 output_line(ctx, "OUTPUT %s = %s%s%s%s;", varname, usage_str,
950 arrayleft, index_str, arrayright);
951 } // else
952 pop_output(ctx);
953 } // else if
954
955 else
956 {
957 fail(ctx, "unknown vertex shader attribute register");
958 } // else
959 } // if
960
961 else if (shader_is_pixel(ctx))
962 {
963 const char *paramtype_str = "ATTRIB";
964
965 // samplers DCLs get handled in emit_ARB1_sampler().
966
967 if (flags & MOD_CENTROID)
968 {
969 if (!support_nv4(ctx)) // GL_NV_fragment_program4 adds centroid.
970 {
971 // !!! FIXME: should we just wing it without centroid here?
972 failf(ctx, "centroid unsupported in %s profile",
973 ctx->profile->name);
974 return;
975 } // if
976
977 paramtype_str = "CENTROID ATTRIB";
978 } // if
979
980 if (regtype == REG_TYPE_COLOROUT)
981 {
982 paramtype_str = "OUTPUT";
983 usage_str = "result.color";
984 if (ctx->have_multi_color_outputs)
985 {
986 // We have to gamble that you have GL_ARB_draw_buffers.
987 // You probably do at this point if you have a sane setup.
988 snprintf(index_str, sizeof (index_str), "%u", (uint) regnum);
989 arrayleft = "[";
990 arrayright = "]";
991 } // if
992 } // if
993
994 else if (regtype == REG_TYPE_DEPTHOUT)
995 {
996 paramtype_str = "OUTPUT";
997 usage_str = "result.depth";
998 } // else if
999
1000 // !!! FIXME: can you actualy have a texture register with COLOR usage?
1001 else if ((regtype == REG_TYPE_TEXTURE) || (regtype == REG_TYPE_INPUT))
1002 {
1003 if (usage == MOJOSHADER_USAGE_TEXCOORD)
1004 {
1005 // ps_1_1 does a different hack for this attribute.
1006 // Refer to emit_ARB1_global()'s REG_TYPE_TEXTURE code.
1007 if (shader_version_atleast(ctx, 1, 4))
1008 {
1009 snprintf(index_str, sizeof (index_str), "%u", (uint) index);
1010 usage_str = "fragment.texcoord";
1011 arrayleft = "[";
1012 arrayright = "]";
1013 } // if
1014 } // if
1015
1016 else if (usage == MOJOSHADER_USAGE_COLOR)
1017 {
1018 index_str[0] = '\0'; // no explicit number.
1019 if (index == 0)
1020 usage_str = "fragment.color.primary";
1021 else if (index == 1)
1022 usage_str = "fragment.color.secondary";
1023 else
1024 fail(ctx, "unsupported color index");
1025 } // else if
1026 } // else if
1027
1028 else if (regtype == REG_TYPE_MISCTYPE)
1029 {
1030 const MiscTypeType mt = (MiscTypeType) regnum;
1031 if (mt == MISCTYPE_TYPE_FACE)
1032 {
1033 if (support_nv4(ctx)) // FINALLY, a vFace equivalent in nv4!
1034 {
1035 index_str[0] = '\0'; // no explicit number.
1036 usage_str = "fragment.facing";
1037 } // if
1038 else
1039 {
1040 failf(ctx, "vFace unsupported in %s profile",
1041 ctx->profile->name);
1042 } // else
1043 } // if
1044 else if (mt == MISCTYPE_TYPE_POSITION)
1045 {
1046 index_str[0] = '\0'; // no explicit number.
1047 usage_str = "fragment.position"; // !!! FIXME: is this the same coord space as D3D?
1048 } // else if
1049 else
1050 {
1051 fail(ctx, "BUG: unhandled misc register");
1052 } // else
1053 } // else if
1054
1055 else
1056 {
1057 fail(ctx, "unknown pixel shader attribute register");
1058 } // else
1059
1060 if (usage_str != NULL)
1061 {
1062 push_output(ctx, &ctx->globals);
1063 output_line(ctx, "%s %s = %s%s%s%s;", paramtype_str, varname,
1064 usage_str, arrayleft, index_str, arrayright);
1065 pop_output(ctx);
1066 } // if
1067 } // else if
1068
1069 else
1070 {
1071 fail(ctx, "Unknown shader type"); // state machine should catch this.
1072 } // else
1073 } // emit_ARB1_attribute
1074
1075 void emit_ARB1_RESERVED(Context *ctx) { /* no-op. */ }
1076
1077 void emit_ARB1_NOP(Context *ctx)
1078 {
1079 // There is no NOP in arb1. Just don't output anything here.
1080 } // emit_ARB1_NOP
1081
1082 EMIT_ARB1_OPCODE_DS_FUNC(MOV)
1083 EMIT_ARB1_OPCODE_DSS_FUNC(ADD)
1084 EMIT_ARB1_OPCODE_DSS_FUNC(SUB)
1085 EMIT_ARB1_OPCODE_DSSS_FUNC(MAD)
1086 EMIT_ARB1_OPCODE_DSS_FUNC(MUL)
1087 EMIT_ARB1_OPCODE_DS_FUNC(RCP)
1088
1089 void emit_ARB1_RSQ(Context *ctx)
1090 {
1091 // nv4 doesn't force abs() on this, so negative values will generate NaN.
1092 // The spec says you should force the abs() yourself.
1093 if (!support_nv4(ctx))
1094 {
1095 emit_ARB1_opcode_ds(ctx, "RSQ"); // pre-nv4 implies ABS.
1096 return;
1097 } // if
1098
1099 // we can optimize this to use nv2's |abs| construct in some cases.
1100 if ( (ctx->source_args[0].src_mod == SRCMOD_NONE) ||
1101 (ctx->source_args[0].src_mod == SRCMOD_NEGATE) ||
1102 (ctx->source_args[0].src_mod == SRCMOD_ABSNEGATE) )
1103 ctx->source_args[0].src_mod = SRCMOD_ABS;
1104
1105 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
1106 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
1107
1108 if (ctx->source_args[0].src_mod == SRCMOD_ABS)
1109 output_line(ctx, "RSQ%s, %s;", dst, src0);
1110 else
1111 {
1112 char buf[64]; allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
1113 output_line(ctx, "ABS %s, %s;", buf, src0);
1114 output_line(ctx, "RSQ%s, %s.x;", dst, buf);
1115 } // else
1116
1117 emit_ARB1_dest_modifiers(ctx);
1118 } // emit_ARB1_RSQ
1119
1120 EMIT_ARB1_OPCODE_DSS_FUNC(DP3)
1121 EMIT_ARB1_OPCODE_DSS_FUNC(DP4)
1122 EMIT_ARB1_OPCODE_DSS_FUNC(MIN)
1123 EMIT_ARB1_OPCODE_DSS_FUNC(MAX)
1124 EMIT_ARB1_OPCODE_DSS_FUNC(SLT)
1125 EMIT_ARB1_OPCODE_DSS_FUNC(SGE)
1126
1127 void emit_ARB1_EXP(Context *ctx) { emit_ARB1_opcode_ds(ctx, "EX2"); }
1128
1129 static void arb1_log(Context *ctx, const char *opcode)
1130 {
1131 // !!! FIXME: SRCMOD_NEGATE can be made into SRCMOD_ABS here, too
1132 // we can optimize this to use nv2's |abs| construct in some cases.
1133 if ( (ctx->source_args[0].src_mod == SRCMOD_NONE) ||
1134 (ctx->source_args[0].src_mod == SRCMOD_ABSNEGATE) )
1135 ctx->source_args[0].src_mod = SRCMOD_ABS;
1136
1137 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
1138 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
1139
1140 if (ctx->source_args[0].src_mod == SRCMOD_ABS)
1141 output_line(ctx, "%s%s, %s;", opcode, dst, src0);
1142 else
1143 {
1144 char buf[64]; allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
1145 output_line(ctx, "ABS %s, %s;", buf, src0);
1146 output_line(ctx, "%s%s, %s.x;", opcode, dst, buf);
1147 } // else
1148
1149 emit_ARB1_dest_modifiers(ctx);
1150 } // arb1_log
1151
1152
1153 void emit_ARB1_LOG(Context *ctx)
1154 {
1155 arb1_log(ctx, "LG2");
1156 } // emit_ARB1_LOG
1157
1158
1159 EMIT_ARB1_OPCODE_DS_FUNC(LIT)
1160 EMIT_ARB1_OPCODE_DSS_FUNC(DST)
1161
1162 void emit_ARB1_LRP(Context *ctx)
1163 {
1164 if (shader_is_pixel(ctx)) // fragment shaders have a matching LRP opcode.
1165 emit_ARB1_opcode_dsss(ctx, "LRP");
1166 else
1167 {
1168 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
1169 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
1170 char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
1171 char src2[64]; make_ARB1_srcarg_string(ctx, 2, src2, sizeof (src2));
1172 char buf[64]; allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
1173
1174 // LRP is: dest = src2 + src0 * (src1 - src2)
1175 output_line(ctx, "SUB %s, %s, %s;", buf, src1, src2);
1176 output_line(ctx, "MAD%s, %s, %s, %s;", dst, buf, src0, src2);
1177 emit_ARB1_dest_modifiers(ctx);
1178 } // else
1179 } // emit_ARB1_LRP
1180
1181 EMIT_ARB1_OPCODE_DS_FUNC(FRC)
1182
1183 static void arb1_MxXy(Context *ctx, const int x, const int y)
1184 {
1185 DestArgInfo *dstarg = &ctx->dest_arg;
1186 const int origmask = dstarg->writemask;
1187 char src0[64];
1188 int i;
1189
1190 make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
1191
1192 for (i = 0; i < y; i++)
1193 {
1194 char dst[64];
1195 char row[64];
1196 make_ARB1_srcarg_string(ctx, i + 1, row, sizeof (row));
1197 set_dstarg_writemask(dstarg, 1 << i);
1198 make_ARB1_destarg_string(ctx, dst, sizeof (dst));
1199 output_line(ctx, "DP%d%s, %s, %s;", x, dst, src0, row);
1200 } // for
1201
1202 set_dstarg_writemask(dstarg, origmask);
1203 emit_ARB1_dest_modifiers(ctx);
1204 } // arb1_MxXy
1205
1206 void emit_ARB1_M4X4(Context *ctx) { arb1_MxXy(ctx, 4, 4); }
1207 void emit_ARB1_M4X3(Context *ctx) { arb1_MxXy(ctx, 4, 3); }
1208 void emit_ARB1_M3X4(Context *ctx) { arb1_MxXy(ctx, 3, 4); }
1209 void emit_ARB1_M3X3(Context *ctx) { arb1_MxXy(ctx, 3, 3); }
1210 void emit_ARB1_M3X2(Context *ctx) { arb1_MxXy(ctx, 3, 2); }
1211
1212 void emit_ARB1_CALL(Context *ctx)
1213 {
1214 if (!support_nv2(ctx)) // no branching in stock ARB1.
1215 {
1216 failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
1217 return;
1218 } // if
1219
1220 char labelstr[64];
1221 get_ARB1_srcarg_varname(ctx, 0, labelstr, sizeof (labelstr));
1222 output_line(ctx, "CAL %s;", labelstr);
1223 } // emit_ARB1_CALL
1224
1225 void emit_ARB1_CALLNZ(Context *ctx)
1226 {
1227 // !!! FIXME: if src1 is a constbool that's true, we can remove the
1228 // !!! FIXME: if. If it's false, we can make this a no-op.
1229
1230 if (!support_nv2(ctx)) // no branching in stock ARB1.
1231 failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
1232 else
1233 {
1234 // !!! FIXME: double-check this.
1235 char labelstr[64];
1236 char scratch[64];
1237 char src1[64];
1238 get_ARB1_srcarg_varname(ctx, 0, labelstr, sizeof (labelstr));
1239 get_ARB1_srcarg_varname(ctx, 1, src1, sizeof (src1));
1240 allocate_ARB1_scratch_reg_name(ctx, scratch, sizeof (scratch));
1241 output_line(ctx, "MOVC %s, %s;", scratch, src1);
1242 output_line(ctx, "CAL %s (NE.x);", labelstr);
1243 } // else
1244 } // emit_ARB1_CALLNZ
1245
1246 // !!! FIXME: needs BRA in nv2, LOOP in nv2 fragment progs, and REP in nv4.
1247 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(LOOP)
1248
1249 void emit_ARB1_RET(Context *ctx)
1250 {
1251 // don't fail() if no nv2...maybe we're just ending the mainline?
1252 // if we're ending a LABEL that had no CALL, this would all be written
1253 // to ctx->ignore anyhow, so this should be "safe" ... arb1 profile will
1254 // just end up throwing all this code out.
1255 if (support_nv2(ctx)) // no branching in stock ARB1.
1256 output_line(ctx, "RET;");
1257 set_output(ctx, &ctx->mainline); // in case we were ignoring this function.
1258 } // emit_ARB1_RET
1259
1260
1261 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(ENDLOOP)
1262
1263 void emit_ARB1_LABEL(Context *ctx)
1264 {
1265 if (!support_nv2(ctx)) // no branching in stock ARB1.
1266 return; // don't fail()...maybe we never use it, but do fail in CALL.
1267
1268 const int label = ctx->source_args[0].regnum;
1269 RegisterList *reg = reglist_find(&ctx->used_registers, REG_TYPE_LABEL, label);
1270
1271 // MSDN specs say CALL* has to come before the LABEL, so we know if we
1272 // can ditch the entire function here as unused.
1273 if (reg == NULL)
1274 set_output(ctx, &ctx->ignore); // Func not used. Parse, but don't output.
1275
1276 // !!! FIXME: it would be nice if we could determine if a function is
1277 // !!! FIXME: only called once and, if so, forcibly inline it.
1278
1279 //const char *uses_loopreg = ((reg) && (reg->misc == 1)) ? "int aL" : "";
1280 char labelstr[64];
1281 get_ARB1_srcarg_varname(ctx, 0, labelstr, sizeof (labelstr));
1282 output_line(ctx, "%s:", labelstr);
1283 } // emit_ARB1_LABEL
1284
1285
1286 void emit_ARB1_POW(Context *ctx)
1287 {
1288 // we can optimize this to use nv2's |abs| construct in some cases.
1289 if ( (ctx->source_args[0].src_mod == SRCMOD_NONE) ||
1290 (ctx->source_args[0].src_mod == SRCMOD_ABSNEGATE) )
1291 ctx->source_args[0].src_mod = SRCMOD_ABS;
1292
1293 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
1294 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
1295 char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
1296
1297 if (ctx->source_args[0].src_mod == SRCMOD_ABS)
1298 output_line(ctx, "POW%s, %s, %s;", dst, src0, src1);
1299 else
1300 {
1301 char buf[64]; allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
1302 output_line(ctx, "ABS %s, %s;", buf, src0);
1303 output_line(ctx, "POW%s, %s.x, %s;", dst, buf, src1);
1304 } // else
1305
1306 emit_ARB1_dest_modifiers(ctx);
1307 } // emit_ARB1_POW
1308
1309 void emit_ARB1_CRS(Context *ctx) { emit_ARB1_opcode_dss(ctx, "XPD"); }
1310
1311 void emit_ARB1_SGN(Context *ctx)
1312 {
1313 if (support_nv2(ctx))
1314 emit_ARB1_opcode_ds(ctx, "SSG");
1315 else
1316 {
1317 char dst[64];
1318 char src0[64];
1319 char scratch1[64];
1320 char scratch2[64];
1321 make_ARB1_destarg_string(ctx, dst, sizeof (dst));
1322 make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
1323 allocate_ARB1_scratch_reg_name(ctx, scratch1, sizeof (scratch1));
1324 allocate_ARB1_scratch_reg_name(ctx, scratch2, sizeof (scratch2));
1325 output_line(ctx, "SLT %s, %s, 0.0;", scratch1, src0);
1326 output_line(ctx, "SLT %s, -%s, 0.0;", scratch2, src0);
1327 output_line(ctx, "ADD%s -%s, %s;", dst, scratch1, scratch2);
1328 emit_ARB1_dest_modifiers(ctx);
1329 } // else
1330 } // emit_ARB1_SGN
1331
1332 EMIT_ARB1_OPCODE_DS_FUNC(ABS)
1333
1334 void emit_ARB1_NRM(Context *ctx)
1335 {
1336 // nv2 fragment programs (and anything nv4) have a real NRM.
1337 if ( (support_nv4(ctx)) || ((support_nv2(ctx)) && (shader_is_pixel(ctx))) )
1338 emit_ARB1_opcode_ds(ctx, "NRM");
1339 else
1340 {
1341 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
1342 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
1343 char buf[64]; allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
1344 output_line(ctx, "DP3 %s.w, %s, %s;", buf, src0, src0);
1345 output_line(ctx, "RSQ %s.w, %s.w;", buf, buf);
1346 output_line(ctx, "MUL%s, %s.w, %s;", dst, buf, src0);
1347 emit_ARB1_dest_modifiers(ctx);
1348 } // else
1349 } // emit_ARB1_NRM
1350
1351
1352 void emit_ARB1_SINCOS(Context *ctx)
1353 {
1354 // we don't care about the temp registers that <= sm2 demands; ignore them.
1355 const int mask = ctx->dest_arg.writemask;
1356
1357 // arb1 fragment programs and everything nv4 have sin/cos/sincos opcodes.
1358 if ((shader_is_pixel(ctx)) || (support_nv4(ctx)))
1359 {
1360 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
1361 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
1362 if (writemask_x(mask))
1363 output_line(ctx, "COS%s, %s;", dst, src0);
1364 else if (writemask_y(mask))
1365 output_line(ctx, "SIN%s, %s;", dst, src0);
1366 else if (writemask_xy(mask))
1367 output_line(ctx, "SCS%s, %s;", dst, src0);
1368 } // if
1369
1370 // nv2+ profiles have sin and cos opcodes.
1371 else if (support_nv2(ctx))
1372 {
1373 char dst[64]; get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
1374 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
1375 if (writemask_x(mask))
1376 output_line(ctx, "COS %s.x, %s;", dst, src0);
1377 else if (writemask_y(mask))
1378 output_line(ctx, "SIN %s.y, %s;", dst, src0);
1379 else if (writemask_xy(mask))
1380 {
1381 output_line(ctx, "SIN %s.x, %s;", dst, src0);
1382 output_line(ctx, "COS %s.y, %s;", dst, src0);
1383 } // else if
1384 } // if
1385
1386 else // big nasty.
1387 {
1388 char dst[64]; get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
1389 char src0[64]; get_ARB1_srcarg_varname(ctx, 0, src0, sizeof (src0));
1390 const int need_sin = (writemask_x(mask) || writemask_xy(mask));
1391 const int need_cos = (writemask_y(mask) || writemask_xy(mask));
1392 char scratch[64];
1393
1394 if (need_sin || need_cos)
1395 allocate_ARB1_scratch_reg_name(ctx, scratch, sizeof (scratch));
1396
1397 // These sin() and cos() approximations originally found here:
1398 // http://www.devmaster.net/forums/showthread.php?t=5784
1399 //
1400 // const float B = 4.0f / M_PI;
1401 // const float C = -4.0f / (M_PI * M_PI);
1402 // float y = B * x + C * x * fabs(x);
1403 //
1404 // // optional better precision...
1405 // const float P = 0.225f;
1406 // y = P * (y * fabs(y) - y) + y;
1407 //
1408 //
1409 // That first thing can be reduced to:
1410 // const float y = ((1.2732395447351626861510701069801f * x) +
1411 // ((-0.40528473456935108577551785283891f * x) * fabs(x)));
1412
1413 if (need_sin)
1414 {
1415 // !!! FIXME: use SRCMOD_ABS here?
1416 output_line(ctx, "ABS %s.x, %s.x;", dst, src0);
1417 output_line(ctx, "MUL %s.x, %s.x, -0.40528473456935108577551785283891;", dst, dst);
1418 output_line(ctx, "MUL %s.x, %s.x, 1.2732395447351626861510701069801;", scratch, src0);
1419 output_line(ctx, "MAD %s.x, %s.x, %s.x, %s.x;", dst, dst, src0, scratch);
1420 } // if
1421
1422 // cosine is sin(x + M_PI/2), but you have to wrap x to pi:
1423 // if (x+(M_PI/2) > M_PI)
1424 // x -= 2 * M_PI;
1425 //
1426 // which is...
1427 // if (x+(1.57079637050628662109375) > 3.1415927410125732421875)
1428 // x += -6.283185482025146484375;
1429
1430 if (need_cos)
1431 {
1432 output_line(ctx, "ADD %s.x, %s.x, 1.57079637050628662109375;", scratch, src0);
1433 output_line(ctx, "SGE %s.y, %s.x, 3.1415927410125732421875;", scratch, scratch);
1434 output_line(ctx, "MAD %s.x, %s.y, -6.283185482025146484375, %s.x;", scratch, scratch, scratch);
1435 output_line(ctx, "ABS %s.x, %s.x;", dst, src0);
1436 output_line(ctx, "MUL %s.x, %s.x, -0.40528473456935108577551785283891;", dst, dst);
1437 output_line(ctx, "MUL %s.x, %s.x, 1.2732395447351626861510701069801;", scratch, src0);
1438 output_line(ctx, "MAD %s.y, %s.x, %s.x, %s.x;", dst, dst, src0, scratch);
1439 } // if
1440 } // else
1441
1442 // !!! FIXME: might not have done anything. Don't emit if we didn't.
1443 if (!(ctx->isfail))
1444 emit_ARB1_dest_modifiers(ctx);
1445 } // emit_ARB1_SINCOS
1446
1447
1448 void emit_ARB1_REP(Context *ctx)
1449 {
1450 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
1451
1452 // nv2 fragment programs (and everything nv4) have a real REP.
1453 if ( (support_nv4(ctx)) || ((support_nv2(ctx)) && (shader_is_pixel(ctx))) )
1454 output_line(ctx, "REP %s;", src0);
1455
1456 else if (support_nv2(ctx))
1457 {
1458 // no REP, but we can use branches.
1459 char failbranch[32];
1460 char topbranch[32];
1461 const int toplabel = allocate_branch_label(ctx);
1462 const int faillabel = allocate_branch_label(ctx);
1463 get_ARB1_branch_label_name(ctx,faillabel,failbranch,sizeof(failbranch));
1464 get_ARB1_branch_label_name(ctx,toplabel,topbranch,sizeof(topbranch));
1465
1466 assert(((size_t) ctx->branch_labels_stack_index) <
1467 STATICARRAYLEN(ctx->branch_labels_stack)-1);
1468
1469 ctx->branch_labels_stack[ctx->branch_labels_stack_index++] = toplabel;
1470 ctx->branch_labels_stack[ctx->branch_labels_stack_index++] = faillabel;
1471
1472 char scratch[32];
1473 snprintf(scratch, sizeof (scratch), "rep%d", ctx->reps);
1474 output_line(ctx, "MOVC %s.x, %s;", scratch, src0);
1475 output_line(ctx, "BRA %s (LE.x);", failbranch);
1476 output_line(ctx, "%s:", topbranch);
1477 } // else if
1478
1479 else // stock ARB1 has no branching.
1480 {
1481 fail(ctx, "branching unsupported in this profile");
1482 } // else
1483 } // emit_ARB1_REP
1484
1485
1486 void emit_ARB1_ENDREP(Context *ctx)
1487 {
1488 // nv2 fragment programs (and everything nv4) have a real ENDREP.
1489 if ( (support_nv4(ctx)) || ((support_nv2(ctx)) && (shader_is_pixel(ctx))) )
1490 output_line(ctx, "ENDREP;");
1491
1492 else if (support_nv2(ctx))
1493 {
1494 // no ENDREP, but we can use branches.
1495 assert(ctx->branch_labels_stack_index >= 2);
1496
1497 char failbranch[32];
1498 char topbranch[32];
1499 const int faillabel = ctx->branch_labels_stack[--ctx->branch_labels_stack_index];
1500 const int toplabel = ctx->branch_labels_stack[--ctx->branch_labels_stack_index];
1501 get_ARB1_branch_label_name(ctx,faillabel,failbranch,sizeof(failbranch));
1502 get_ARB1_branch_label_name(ctx,toplabel,topbranch,sizeof(topbranch));
1503
1504 char scratch[32];
1505 snprintf(scratch, sizeof (scratch), "rep%d", ctx->reps);
1506 output_line(ctx, "SUBC %s.x, %s.x, 1.0;", scratch, scratch);
1507 output_line(ctx, "BRA %s (GT.x);", topbranch);
1508 output_line(ctx, "%s:", failbranch);
1509 } // else if
1510
1511 else // stock ARB1 has no branching.
1512 {
1513 fail(ctx, "branching unsupported in this profile");
1514 } // else
1515 } // emit_ARB1_ENDREP
1516
1517
1518 void nv2_if(Context *ctx)
1519 {
1520 // The condition code register MUST be set up before this!
1521 // nv2 fragment programs (and everything nv4) have a real IF.
1522 if ( (support_nv4(ctx)) || (shader_is_pixel(ctx)) )
1523 output_line(ctx, "IF EQ.x;");
1524 else
1525 {
1526 // there's no IF construct, but we can use a branch to a label.
1527 char failbranch[32];
1528 const int label = allocate_branch_label(ctx);
1529 get_ARB1_branch_label_name(ctx, label, failbranch, sizeof (failbranch));
1530
1531 assert(((size_t) ctx->branch_labels_stack_index)
1532 < STATICARRAYLEN(ctx->branch_labels_stack));
1533
1534 ctx->branch_labels_stack[ctx->branch_labels_stack_index++] = label;
1535
1536 // !!! FIXME: should this be NE? (EQ would jump to the ELSE for the IF condition, right?).
1537 output_line(ctx, "BRA %s (EQ.x);", failbranch);
1538 } // else
1539 } // nv2_if
1540
1541
1542 void emit_ARB1_IF(Context *ctx)
1543 {
1544 if (support_nv2(ctx))
1545 {
1546 char buf[64]; allocate_ARB1_scratch_reg_name(ctx, buf, sizeof (buf));
1547 char src0[64]; get_ARB1_srcarg_varname(ctx, 0, src0, sizeof (src0));
1548 output_line(ctx, "MOVC %s.x, %s;", buf, src0);
1549 nv2_if(ctx);
1550 } // if
1551
1552 else // stock ARB1 has no branching.
1553 {
1554 failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
1555 } // else
1556 } // emit_ARB1_IF
1557
1558
1559 void emit_ARB1_ELSE(Context *ctx)
1560 {
1561 // nv2 fragment programs (and everything nv4) have a real ELSE.
1562 if ( (support_nv4(ctx)) || ((support_nv2(ctx)) && (shader_is_pixel(ctx))) )
1563 output_line(ctx, "ELSE;");
1564
1565 else if (support_nv2(ctx))
1566 {
1567 // there's no ELSE construct, but we can use a branch to a label.
1568 assert(ctx->branch_labels_stack_index > 0);
1569
1570 // At the end of the IF block, unconditionally jump to the ENDIF.
1571 const int endlabel = allocate_branch_label(ctx);
1572 char endbranch[32];
1573 get_ARB1_branch_label_name(ctx,endlabel,endbranch,sizeof (endbranch));
1574 output_line(ctx, "BRA %s;", endbranch);
1575
1576 // Now mark the ELSE section with a lable.
1577 const int elselabel = ctx->branch_labels_stack[ctx->branch_labels_stack_index-1];
1578 char elsebranch[32];
1579 get_ARB1_branch_label_name(ctx,elselabel,elsebranch,sizeof(elsebranch));
1580 output_line(ctx, "%s:", elsebranch);
1581
1582 // Replace the ELSE label with the ENDIF on the label stack.
1583 ctx->branch_labels_stack[ctx->branch_labels_stack_index-1] = endlabel;
1584 } // else if
1585
1586 else // stock ARB1 has no branching.
1587 {
1588 failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
1589 } // else
1590 } // emit_ARB1_ELSE
1591
1592
1593 void emit_ARB1_ENDIF(Context *ctx)
1594 {
1595 // nv2 fragment programs (and everything nv4) have a real ENDIF.
1596 if ( (support_nv4(ctx)) || ((support_nv2(ctx)) && (shader_is_pixel(ctx))) )
1597 output_line(ctx, "ENDIF;");
1598
1599 else if (support_nv2(ctx))
1600 {
1601 // there's no ENDIF construct, but we can use a branch to a label.
1602 assert(ctx->branch_labels_stack_index > 0);
1603 const int endlabel = ctx->branch_labels_stack[--ctx->branch_labels_stack_index];
1604 char endbranch[32];
1605 get_ARB1_branch_label_name(ctx,endlabel,endbranch,sizeof (endbranch));
1606 output_line(ctx, "%s:", endbranch);
1607 } // if
1608
1609 else // stock ARB1 has no branching.
1610 {
1611 failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
1612 } // else
1613 } // emit_ARB1_ENDIF
1614
1615
1616 void emit_ARB1_BREAK(Context *ctx)
1617 {
1618 // nv2 fragment programs (and everything nv4) have a real BREAK.
1619 if ( (support_nv4(ctx)) || ((support_nv2(ctx)) && (shader_is_pixel(ctx))) )
1620 output_line(ctx, "BRK;");
1621
1622 else if (support_nv2(ctx))
1623 {
1624 // no BREAK, but we can use branches.
1625 assert(ctx->branch_labels_stack_index >= 2);
1626 const int faillabel = ctx->branch_labels_stack[ctx->branch_labels_stack_index];
1627 char failbranch[32];
1628 get_ARB1_branch_label_name(ctx,faillabel,failbranch,sizeof(failbranch));
1629 output_line(ctx, "BRA %s;", failbranch);
1630 } // else if
1631
1632 else // stock ARB1 has no branching.
1633 {
1634 failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
1635 } // else
1636 } // emit_ARB1_BREAK
1637
1638
1639 void emit_ARB1_MOVA(Context *ctx)
1640 {
1641 // nv2 and nv3 can use the ARR opcode.
1642 // But nv4 removed ARR (and ADDRESS registers!). Just ROUND to an INT.
1643 if (support_nv4(ctx))
1644 emit_ARB1_opcode_ds(ctx, "ROUND.S"); // !!! FIXME: don't use a modifier here.
1645 else if ((support_nv2(ctx)) || (support_nv3(ctx)))
1646 emit_ARB1_opcode_ds(ctx, "ARR");
1647 else
1648 {
1649 char src0[64];
1650 char scratch[64];
1651 char addr[32];
1652
1653 make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
1654 allocate_ARB1_scratch_reg_name(ctx, scratch, sizeof (scratch));
1655 snprintf(addr, sizeof (addr), "addr%d", ctx->dest_arg.regnum);
1656
1657 // !!! FIXME: we can optimize this if src_mod is ABS or ABSNEGATE.
1658
1659 // ARL uses floor(), but D3D expects round-to-nearest.
1660 // There is probably a more efficient way to do this.
1661 if (shader_is_pixel(ctx)) // CMP only exists in fragment programs. :/
1662 output_line(ctx, "CMP %s, %s, -1.0, 1.0;", scratch, src0);
1663 else
1664 {
1665 output_line(ctx, "SLT %s, %s, 0.0;", scratch, src0);
1666 output_line(ctx, "MAD %s, %s, -2.0, 1.0;", scratch, scratch);
1667 } // else
1668
1669 output_line(ctx, "ABS %s, %s;", addr, src0);
1670 output_line(ctx, "ADD %s, %s, 0.5;", addr, addr);
1671 output_line(ctx, "FLR %s, %s;", addr, addr);
1672 output_line(ctx, "MUL %s, %s, %s;", addr, addr, scratch);
1673
1674 // we don't handle these right now, since emit_ARB1_dest_modifiers(ctx)
1675 // wants to look at dest_arg, not our temp register.
1676 assert(ctx->dest_arg.result_mod == 0);
1677 assert(ctx->dest_arg.result_shift == 0);
1678
1679 // we assign to the actual address register as needed.
1680 ctx->last_address_reg_component = -1;
1681 } // else
1682 } // emit_ARB1_MOVA
1683
1684
1685 void emit_ARB1_TEXKILL(Context *ctx)
1686 {
1687 // d3d kills on xyz, arb1 kills on xyzw. Fix the swizzle.
1688 // We just map the x component to w. If it's negative, the fragment
1689 // would discard anyhow, otherwise, it'll pass through okay. This saves
1690 // us a temp register.
1691 char dst[64];
1692 get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
1693 output_line(ctx, "KIL %s.xyzx;", dst);
1694 } // emit_ARB1_TEXKILL
1695
1696 static void arb1_texbem(Context *ctx, const int luminance)
1697 {
1698 // !!! FIXME: this code counts on the register not having swizzles, etc.
1699 const int stage = ctx->dest_arg.regnum;
1700 char dst[64]; get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
1701 char src[64]; get_ARB1_srcarg_varname(ctx, 0, src, sizeof (src));
1702 char tmp[64]; allocate_ARB1_scratch_reg_name(ctx, tmp, sizeof (tmp));
1703 char sampler[64];
1704 get_ARB1_varname_in_buf(ctx, REG_TYPE_SAMPLER, stage,
1705 sampler, sizeof (sampler));
1706
1707 output_line(ctx, "MUL %s, %s_texbem.xzyw, %s.xyxy;", tmp, sampler, src);
1708 output_line(ctx, "ADD %s.xy, %s.xzxx, %s.ywxx;", tmp, tmp, tmp);
1709 output_line(ctx, "ADD %s.xy, %s, %s;", tmp, tmp, dst);
1710 output_line(ctx, "TEX %s, %s, texture[%d], 2D;", dst, tmp, stage);
1711
1712 if (luminance) // TEXBEML, not just TEXBEM?
1713 {
1714 output_line(ctx, "MAD %s, %s.zzzz, %s_texbeml.xxxx, %s_texbeml.yyyy;",
1715 tmp, src, sampler, sampler);
1716 output_line(ctx, "MUL %s, %s, %s;", dst, dst, tmp);
1717 } // if
1718
1719 emit_ARB1_dest_modifiers(ctx);
1720 } // arb1_texbem
1721
1722 void emit_ARB1_TEXBEM(Context *ctx)
1723 {
1724 arb1_texbem(ctx, 0);
1725 } // emit_ARB1_TEXBEM
1726
1727 void emit_ARB1_TEXBEML(Context *ctx)
1728 {
1729 arb1_texbem(ctx, 1);
1730 } // emit_ARB1_TEXBEML
1731
1732 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2AR)
1733 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2GB)
1734
1735
1736 void emit_ARB1_TEXM3X2PAD(Context *ctx)
1737 {
1738 // no-op ... work happens in emit_ARB1_TEXM3X2TEX().
1739 } // emit_ARB1_TEXM3X2PAD
1740
1741 void emit_ARB1_TEXM3X2TEX(Context *ctx)
1742 {
1743 if (ctx->texm3x2pad_src0 == -1)
1744 return;
1745
1746 char dst[64];
1747 char src0[64];
1748 char src1[64];
1749 char src2[64];
1750
1751 // !!! FIXME: this code counts on the register not having swizzles, etc.
1752 const int stage = ctx->dest_arg.regnum;
1753 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x2pad_src0,
1754 src0, sizeof (src0));
1755 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x2pad_dst0,
1756 src1, sizeof (src1));
1757 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
1758 src2, sizeof (src2));
1759 get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
1760
1761 output_line(ctx, "DP3 %s.y, %s, %s;", dst, src2, dst);
1762 output_line(ctx, "DP3 %s.x, %s, %s;", dst, src0, src1);
1763 output_line(ctx, "TEX %s, %s, texture[%d], 2D;", dst, dst, stage);
1764 emit_ARB1_dest_modifiers(ctx);
1765 } // emit_ARB1_TEXM3X2TEX
1766
1767
1768 void emit_ARB1_TEXM3X3PAD(Context *ctx)
1769 {
1770 // no-op ... work happens in emit_ARB1_TEXM3X3*().
1771 } // emit_ARB1_TEXM3X3PAD
1772
1773
1774 void emit_ARB1_TEXM3X3TEX(Context *ctx)
1775 {
1776 if (ctx->texm3x3pad_src1 == -1)
1777 return;
1778
1779 char dst[64];
1780 char src0[64];
1781 char src1[64];
1782 char src2[64];
1783 char src3[64];
1784 char src4[64];
1785
1786 // !!! FIXME: this code counts on the register not having swizzles, etc.
1787 const int stage = ctx->dest_arg.regnum;
1788 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
1789 src0, sizeof (src0));
1790 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
1791 src1, sizeof (src1));
1792 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
1793 src2, sizeof (src2));
1794 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
1795 src3, sizeof (src3));
1796 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
1797 src4, sizeof (src4));
1798 get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
1799
1800 RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, stage);
1801 const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
1802 const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "CUBE" : "3D";
1803
1804 output_line(ctx, "DP3 %s.z, %s, %s;", dst, dst, src4);
1805 output_line(ctx, "DP3 %s.x, %s, %s;", dst, src0, src1);
1806 output_line(ctx, "DP3 %s.y, %s, %s;", dst, src2, src3);
1807 output_line(ctx, "TEX %s, %s, texture[%d], %s;", dst, dst, stage, ttypestr);
1808 emit_ARB1_dest_modifiers(ctx);
1809 } // emit_ARB1_TEXM3X3TEX
1810
1811 void emit_ARB1_TEXM3X3SPEC(Context *ctx)
1812 {
1813 if (ctx->texm3x3pad_src1 == -1)
1814 return;
1815
1816 char dst[64];
1817 char src0[64];
1818 char src1[64];
1819 char src2[64];
1820 char src3[64];
1821 char src4[64];
1822 char src5[64];
1823 char tmp[64];
1824 char tmp2[64];
1825
1826 // !!! FIXME: this code counts on the register not having swizzles, etc.
1827 const int stage = ctx->dest_arg.regnum;
1828 allocate_ARB1_scratch_reg_name(ctx, tmp, sizeof (tmp));
1829 allocate_ARB1_scratch_reg_name(ctx, tmp2, sizeof (tmp2));
1830 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
1831 src0, sizeof (src0));
1832 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
1833 src1, sizeof (src1));
1834 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
1835 src2, sizeof (src2));
1836 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
1837 src3, sizeof (src3));
1838 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
1839 src4, sizeof (src4));
1840 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[1].regnum,
1841 src5, sizeof (src5));
1842 get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
1843
1844 RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, stage);
1845 const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
1846 const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "CUBE" : "3D";
1847
1848 output_line(ctx, "DP3 %s.z, %s, %s;", dst, dst, src4);
1849 output_line(ctx, "DP3 %s.x, %s, %s;", dst, src0, src1);
1850 output_line(ctx, "DP3 %s.y, %s, %s;", dst, src2, src3);
1851 output_line(ctx, "MUL %s, %s, %s;", tmp, dst, dst); // normal * normal
1852 output_line(ctx, "MUL %s, %s, %s;", tmp2, dst, src5); // normal * eyeray
1853
1854 // !!! FIXME: This is goofy. There's got to be a way to do vector-wide
1855 // !!! FIXME: divides or reciprocals...right?
1856 output_line(ctx, "RCP %s.x, %s.x;", tmp2, tmp2);
1857 output_line(ctx, "RCP %s.y, %s.y;", tmp2, tmp2);
1858 output_line(ctx, "RCP %s.z, %s.z;", tmp2, tmp2);
1859 output_line(ctx, "RCP %s.w, %s.w;", tmp2, tmp2);
1860 output_line(ctx, "MUL %s, %s, %s;", tmp, tmp, tmp2);
1861
1862 output_line(ctx, "MUL %s, %s, { 2.0, 2.0, 2.0, 2.0 };", tmp, tmp);
1863 output_line(ctx, "MAD %s, %s, %s, -%s;", tmp, tmp, dst, src5);
1864 output_line(ctx, "TEX %s, %s, texture[%d], %s;", dst, tmp, stage, ttypestr);
1865 emit_ARB1_dest_modifiers(ctx);
1866 } // emit_ARB1_TEXM3X3SPEC
1867
1868 void emit_ARB1_TEXM3X3VSPEC(Context *ctx)
1869 {
1870 if (ctx->texm3x3pad_src1 == -1)
1871 return;
1872
1873 char dst[64];
1874 char src0[64];
1875 char src1[64];
1876 char src2[64];
1877 char src3[64];
1878 char src4[64];
1879 char tmp[64];
1880 char tmp2[64];
1881 char tmp3[64];
1882
1883 // !!! FIXME: this code counts on the register not having swizzles, etc.
1884 const int stage = ctx->dest_arg.regnum;
1885 allocate_ARB1_scratch_reg_name(ctx, tmp, sizeof (tmp));
1886 allocate_ARB1_scratch_reg_name(ctx, tmp2, sizeof (tmp2));
1887 allocate_ARB1_scratch_reg_name(ctx, tmp3, sizeof (tmp3));
1888 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
1889 src0, sizeof (src0));
1890 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
1891 src1, sizeof (src1));
1892 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
1893 src2, sizeof (src2));
1894 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
1895 src3, sizeof (src3));
1896 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
1897 src4, sizeof (src4));
1898 get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
1899
1900 RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, stage);
1901 const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
1902 const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "CUBE" : "3D";
1903
1904 output_line(ctx, "MOV %s.x, %s.w;", tmp3, src0);
1905 output_line(ctx, "MOV %s.y, %s.w;", tmp3, src2);
1906 output_line(ctx, "MOV %s.z, %s.w;", tmp3, dst);
1907 output_line(ctx, "DP3 %s.z, %s, %s;", dst, dst, src4);
1908 output_line(ctx, "DP3 %s.x, %s, %s;", dst, src0, src1);
1909 output_line(ctx, "DP3 %s.y, %s, %s;", dst, src2, src3);
1910 output_line(ctx, "MUL %s, %s, %s;", tmp, dst, dst); // normal * normal
1911 output_line(ctx, "MUL %s, %s, %s;", tmp2, dst, tmp3); // normal * eyeray
1912
1913 // !!! FIXME: This is goofy. There's got to be a way to do vector-wide
1914 // !!! FIXME: divides or reciprocals...right?
1915 output_line(ctx, "RCP %s.x, %s.x;", tmp2, tmp2);
1916 output_line(ctx, "RCP %s.y, %s.y;", tmp2, tmp2);
1917 output_line(ctx, "RCP %s.z, %s.z;", tmp2, tmp2);
1918 output_line(ctx, "RCP %s.w, %s.w;", tmp2, tmp2);
1919 output_line(ctx, "MUL %s, %s, %s;", tmp, tmp, tmp2);
1920
1921 output_line(ctx, "MUL %s, %s, { 2.0, 2.0, 2.0, 2.0 };", tmp, tmp);
1922 output_line(ctx, "MAD %s, %s, %s, -%s;", tmp, tmp, dst, tmp3);
1923 output_line(ctx, "TEX %s, %s, texture[%d], %s;", dst, tmp, stage, ttypestr);
1924 emit_ARB1_dest_modifiers(ctx);
1925 } // emit_ARB1_TEXM3X3VSPEC
1926
1927 void emit_ARB1_EXPP(Context *ctx) { emit_ARB1_opcode_ds(ctx, "EX2"); }
1928 void emit_ARB1_LOGP(Context *ctx) { arb1_log(ctx, "LG2"); }
1929
1930 void emit_ARB1_CND(Context *ctx)
1931 {
1932 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
1933 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
1934 char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
1935 char src2[64]; make_ARB1_srcarg_string(ctx, 2, src2, sizeof (src2));
1936 char tmp[64]; allocate_ARB1_scratch_reg_name(ctx, tmp, sizeof (tmp));
1937
1938 // CND compares against 0.5, but we need to compare against 0.0...
1939 // ...subtract to make up the difference.
1940 output_line(ctx, "SUB %s, %s, { 0.5, 0.5, 0.5, 0.5 };", tmp, src0);
1941 // D3D tests (src0 >= 0.0), but ARB1 tests (src0 < 0.0) ... so just
1942 // switch src1 and src2 to get the same results.
1943 output_line(ctx, "CMP%s, %s, %s, %s;", dst, tmp, src2, src1);
1944 emit_ARB1_dest_modifiers(ctx);
1945 } // emit_ARB1_CND
1946
1947 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2RGB)
1948 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXDP3TEX)
1949 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXM3X2DEPTH)
1950 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXDP3)
1951
1952 void emit_ARB1_TEXM3X3(Context *ctx)
1953 {
1954 if (ctx->texm3x3pad_src1 == -1)
1955 return;
1956
1957 char dst[64];
1958 char src0[64];
1959 char src1[64];
1960 char src2[64];
1961 char src3[64];
1962 char src4[64];
1963
1964 // !!! FIXME: this code counts on the register not having swizzles, etc.
1965 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
1966 src0, sizeof (src0));
1967 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
1968 src1, sizeof (src1));
1969 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
1970 src2, sizeof (src2));
1971 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
1972 src3, sizeof (src3));
1973 get_ARB1_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
1974 src4, sizeof (src4));
1975 get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
1976
1977 output_line(ctx, "DP3 %s.z, %s, %s;", dst, dst, src4);
1978 output_line(ctx, "DP3 %s.x, %s, %s;", dst, src0, src1);
1979 output_line(ctx, "DP3 %s.y, %s, %s;", dst, src2, src3);
1980 output_line(ctx, "MOV %s.w, { 1.0, 1.0, 1.0, 1.0 };", dst);
1981 emit_ARB1_dest_modifiers(ctx);
1982 } // emit_ARB1_TEXM3X3
1983
1984 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXDEPTH)
1985
1986 void emit_ARB1_CMP(Context *ctx)
1987 {
1988 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
1989 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
1990 char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
1991 char src2[64]; make_ARB1_srcarg_string(ctx, 2, src2, sizeof (src2));
1992 // D3D tests (src0 >= 0.0), but ARB1 tests (src0 < 0.0) ... so just
1993 // switch src1 and src2 to get the same results.
1994 output_line(ctx, "CMP%s, %s, %s, %s;", dst, src0, src2, src1);
1995 emit_ARB1_dest_modifiers(ctx);
1996 } // emit_ARB1_CMP
1997
1998 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(BEM)
1999
2000
2001 void emit_ARB1_DP2ADD(Context *ctx)
2002 {
2003 if (support_nv4(ctx)) // nv4 has a built-in equivalent to DP2ADD.
2004 emit_ARB1_opcode_dsss(ctx, "DP2A");
2005 else
2006 {
2007 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
2008 char src0[64]; make_ARB1_srcarg_string(ctx, 0, src0, sizeof (src0));
2009 char src1[64]; make_ARB1_srcarg_string(ctx, 1, src1, sizeof (src1));
2010 char src2[64]; make_ARB1_srcarg_string(ctx, 2, src2, sizeof (src2));
2011 char scratch[64];
2012
2013 // DP2ADD is:
2014 // dst = (src0.r * src1.r) + (src0.g * src1.g) + src2.replicate_swiz
2015 allocate_ARB1_scratch_reg_name(ctx, scratch, sizeof (scratch));
2016 output_line(ctx, "MUL %s, %s, %s;", scratch, src0, src1);
2017 output_line(ctx, "ADD %s, %s.x, %s.y;", scratch, scratch, scratch);
2018 output_line(ctx, "ADD%s, %s.x, %s;", dst, scratch, src2);
2019 emit_ARB1_dest_modifiers(ctx);
2020 } // else
2021 } // emit_ARB1_DP2ADD
2022
2023
2024 void emit_ARB1_DSX(Context *ctx)
2025 {
2026 if (support_nv2(ctx)) // nv2 has a built-in equivalent to DSX.
2027 emit_ARB1_opcode_ds(ctx, "DDX");
2028 else
2029 failf(ctx, "DSX unsupported in %s profile", ctx->profile->name);
2030 } // emit_ARB1_DSX
2031
2032
2033 void emit_ARB1_DSY(Context *ctx)
2034 {
2035 if (support_nv2(ctx)) // nv2 has a built-in equivalent to DSY.
2036 emit_ARB1_opcode_ds(ctx, "DDY");
2037 else
2038 failf(ctx, "DSY unsupported in %s profile", ctx->profile->name);
2039 } // emit_ARB1_DSY
2040
2041 static void arb1_texld(Context *ctx, const char *opcode, const int texldd)
2042 {
2043 // !!! FIXME: Hack: "TEXH" is invalid in nv4. Fix this more cleanly.
2044 if ((ctx->dest_arg.result_mod & MOD_PP) && (support_nv4(ctx)))
2045 ctx->dest_arg.result_mod &= ~MOD_PP;
2046
2047 char dst[64]; make_ARB1_destarg_string(ctx, dst, sizeof (dst));
2048
2049 const int sm1 = !shader_version_atleast(ctx, 1, 4);
2050 const int regnum = sm1 ? ctx->dest_arg.regnum : ctx->source_args[1].regnum;
2051 RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, regnum);
2052
2053 const char *ttype = NULL;
2054 char src0[64];
2055 if (sm1)
2056 get_ARB1_destarg_varname(ctx, src0, sizeof (src0));
2057 else
2058 get_ARB1_srcarg_varname(ctx, 0, src0, sizeof (src0));
2059 //char src1[64]; get_ARB1_srcarg_varname(ctx, 1, src1, sizeof (src1)); // !!! FIXME: SRC_MOD?
2060
2061 char src2[64] = { 0 };
2062 char src3[64] = { 0 };
2063
2064 if (texldd)
2065 {
2066 make_ARB1_srcarg_string(ctx, 2, src2, sizeof (src2));
2067 make_ARB1_srcarg_string(ctx, 3, src3, sizeof (src3));
2068 } // if
2069
2070 // !!! FIXME: this should be in state_TEXLD, not in the arb1/glsl emitters.
2071 if (sreg == NULL)
2072 {
2073 fail(ctx, "TEXLD using undeclared sampler");
2074 return;
2075 } // if
2076
2077 // SM1 only specifies dst, so don't check swizzle there.
2078 if ( !sm1 && (!no_swizzle(ctx->source_args[1].swizzle)) )
2079 {
2080 // !!! FIXME: does this ever actually happen?
2081 fail(ctx, "BUG: can't handle TEXLD with sampler swizzle at the moment");
2082 } // if
2083
2084 switch ((const TextureType) sreg->index)
2085 {
2086 case TEXTURE_TYPE_2D: ttype = "2D"; break; // !!! FIXME: "RECT"?
2087 case TEXTURE_TYPE_CUBE: ttype = "CUBE"; break;
2088 case TEXTURE_TYPE_VOLUME: ttype = "3D"; break;
2089 default: fail(ctx, "unknown texture type"); return;
2090 } // switch
2091
2092 if (texldd)
2093 {
2094 output_line(ctx, "%s%s, %s, %s, %s, texture[%d], %s;", opcode, dst,
2095 src0, src2, src3, regnum, ttype);
2096 } // if
2097 else
2098 {
2099 output_line(ctx, "%s%s, %s, texture[%d], %s;", opcode, dst, src0,
2100 regnum, ttype);
2101 } // else
2102 } // arb1_texld
2103
2104
2105 void emit_ARB1_TEXLDD(Context *ctx)
2106 {
2107 // With GL_NV_fragment_program2, we can use the TXD opcode.
2108 // In stock arb1, we can settle for a standard texld, which isn't
2109 // perfect, but oh well.
2110 if (support_nv2(ctx))
2111 arb1_texld(ctx, "TXD", 1);
2112 else
2113 arb1_texld(ctx, "TEX", 0);
2114 } // emit_ARB1_TEXLDD
2115
2116
2117 void emit_ARB1_TEXLDL(Context *ctx)
2118 {
2119 if ((shader_is_vertex(ctx)) && (!support_nv3(ctx)))
2120 {
2121 failf(ctx, "Vertex shader TEXLDL unsupported in %s profile",
2122 ctx->profile->name);
2123 return;
2124 } // if
2125
2126 else if ((shader_is_pixel(ctx)) && (!support_nv2(ctx)))
2127 {
2128 failf(ctx, "Pixel shader TEXLDL unsupported in %s profile",
2129 ctx->profile->name);
2130 return;
2131 } // if
2132
2133 // !!! FIXME: this doesn't map exactly to TEXLDL. Review this.
2134 arb1_texld(ctx, "TXL", 0);
2135 } // emit_ARB1_TEXLDL
2136
2137
2138 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(BREAKP)
2139 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(BREAKC)
2140
2141 void emit_ARB1_IFC(Context *ctx)
2142 {
2143 if (support_nv2(ctx))
2144 {
2145 const char *comps[] = {
2146 "", "SGTC", "SEQC", "SGEC", "SGTC", "SNEC", "SLEC"
2147 };
2148
2149 if (ctx->instruction_controls >= STATICARRAYLEN(comps))
2150 {
2151 fail(ctx, "unknown comparison control");
2152 return;
2153 } // if
2154
2155 char src0[64];
2156 char src1[64];
2157 char scratch[64];
2158
2159 const char *comp = comps[ctx->instruction_controls];
2160 get_ARB1_srcarg_varname(ctx, 0, src0, sizeof (src0));
2161 get_ARB1_srcarg_varname(ctx, 1, src1, sizeof (src1));
2162 allocate_ARB1_scratch_reg_name(ctx, scratch, sizeof (scratch));
2163 output_line(ctx, "%s %s.x, %s, %s;", comp, scratch, src0, src1);
2164 nv2_if(ctx);
2165 } // if
2166
2167 else // stock ARB1 has no branching.
2168 {
2169 failf(ctx, "branching unsupported in %s profile", ctx->profile->name);
2170 } // else
2171 } // emit_ARB1_IFC
2172
2173
2174 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(SETP)
2175
2176 void emit_ARB1_DEF(Context *ctx)
2177 {
2178 const float *val = (const float *) ctx->dwords; // !!! FIXME: could be int?
2179 char dst[64]; get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
2180 char val0[32]; floatstr(ctx, val0, sizeof (val0), val[0], 1);
2181 char val1[32]; floatstr(ctx, val1, sizeof (val1), val[1], 1);
2182 char val2[32]; floatstr(ctx, val2, sizeof (val2), val[2], 1);
2183 char val3[32]; floatstr(ctx, val3, sizeof (val3), val[3], 1);
2184
2185 push_output(ctx, &ctx->globals);
2186 output_line(ctx, "PARAM %s = { %s, %s, %s, %s };",
2187 dst, val0, val1, val2, val3);
2188 pop_output(ctx);
2189 } // emit_ARB1_DEF
2190
2191 void emit_ARB1_DEFI(Context *ctx)
2192 {
2193 char dst[64]; get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
2194 const int32 *x = (const int32 *) ctx->dwords;
2195 push_output(ctx, &ctx->globals);
2196 output_line(ctx, "PARAM %s = { %d, %d, %d, %d };",
2197 dst, (int) x[0], (int) x[1], (int) x[2], (int) x[3]);
2198 pop_output(ctx);
2199 } // emit_ARB1_DEFI
2200
2201 void emit_ARB1_DEFB(Context *ctx)
2202 {
2203 char dst[64]; get_ARB1_destarg_varname(ctx, dst, sizeof (dst));
2204 push_output(ctx, &ctx->globals);
2205 output_line(ctx, "PARAM %s = %d;", dst, ctx->dwords[0] ? 1 : 0);
2206 pop_output(ctx);
2207 } // emit_ARB1_DEFB
2208
2209 void emit_ARB1_DCL(Context *ctx)
2210 {
2211 // no-op. We do this in our emit_attribute() and emit_uniform().
2212 } // emit_ARB1_DCL
2213
2214 EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC(TEXCRD)
2215
2216 void emit_ARB1_TEXLD(Context *ctx)
2217 {
2218 if (!shader_version_atleast(ctx, 1, 4))
2219 {
2220 arb1_texld(ctx, "TEX", 0);
2221 return;
2222 } // if
2223
2224 else if (!shader_version_atleast(ctx, 2, 0))
2225 {
2226 // ps_1_4 is different, too!
2227 fail(ctx, "TEXLD == Shader Model 1.4 unimplemented."); // !!! FIXME
2228 return;
2229 } // if
2230
2231 // !!! FIXME: do texldb and texldp map between OpenGL and D3D correctly?
2232 if (ctx->instruction_controls == CONTROL_TEXLD)
2233 arb1_texld(ctx, "TEX", 0);
2234 else if (ctx->instruction_controls == CONTROL_TEXLDP)
2235 arb1_texld(ctx, "TXP", 0);
2236 else if (ctx->instruction_controls == CONTROL_TEXLDB)
2237 arb1_texld(ctx, "TXB", 0);
2238 } // emit_ARB1_TEXLD
2239
2240 #undef EMIT_ARB1_OPCODE_FUNC
2241 #undef EMIT_ARB1_OPCODE_D_FUNC
2242 #undef EMIT_ARB1_OPCODE_S_FUNC
2243 #undef EMIT_ARB1_OPCODE_SS_FUNC
2244 #undef EMIT_ARB1_OPCODE_DS_FUNC
2245 #undef EMIT_ARB1_OPCODE_DSS_FUNC
2246 #undef EMIT_ARB1_OPCODE_DSSS_FUNC
2247 #undef EMIT_ARB1_OPCODE_DSSSS_FUNC
2248 #undef EMIT_ARB1_OPCODE_UNIMPLEMENTED_FUNC
2249
2250 #endif // SUPPORT_PROFILE_ARB1
2251
2252 #pragma GCC visibility pop