Debian Codegraph @

profiles/mojoshader_profile_glsl.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_GLSL
16
17 #define EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(op) \
18 void emit_GLSL_##op(Context *ctx) { \
19 fail(ctx, #op " unimplemented in glsl profile"); \
20 }
21
22 static inline const char *get_GLSL_register_string(Context *ctx,
23 const RegisterType regtype, const int regnum,
24 char *regnum_str, const size_t regnum_size)
25 {
26 // turns out these are identical at the moment.
27 return get_D3D_register_string(ctx,regtype,regnum,regnum_str,regnum_size);
28 } // get_GLSL_register_string
29
30 const char *get_GLSL_uniform_type(Context *ctx, const RegisterType rtype)
31 {
32 switch (rtype)
33 {
34 case REG_TYPE_CONST: return "vec4";
35 case REG_TYPE_CONSTINT: return "ivec4";
36 case REG_TYPE_CONSTBOOL: return "bool";
37 default: fail(ctx, "BUG: used a uniform we don't know how to define.");
38 } // switch
39
40 return NULL;
41 } // get_GLSL_uniform_type
42
43 const char *get_GLSL_varname_in_buf(Context *ctx, RegisterType rt,
44 int regnum, char *buf,
45 const size_t len)
46 {
47 char regnum_str[16];
48 const char *regtype_str = get_GLSL_register_string(ctx, rt, regnum,
49 regnum_str, sizeof (regnum_str));
50 snprintf(buf,len,"%s_%s%s", ctx->shader_type_str, regtype_str, regnum_str);
51 return buf;
52 } // get_GLSL_varname_in_buf
53
54
55 const char *get_GLSL_varname(Context *ctx, RegisterType rt, int regnum)
56 {
57 char buf[64];
58 get_GLSL_varname_in_buf(ctx, rt, regnum, buf, sizeof (buf));
59 return StrDup(ctx, buf);
60 } // get_GLSL_varname
61
62
63 static inline const char *get_GLSL_const_array_varname_in_buf(Context *ctx,
64 const int base, const int size,
65 char *buf, const size_t buflen)
66 {
67 const char *type = ctx->shader_type_str;
68 snprintf(buf, buflen, "%s_const_array_%d_%d", type, base, size);
69 return buf;
70 } // get_GLSL_const_array_varname_in_buf
71
72 const char *get_GLSL_const_array_varname(Context *ctx, int base, int size)
73 {
74 char buf[64];
75 get_GLSL_const_array_varname_in_buf(ctx, base, size, buf, sizeof (buf));
76 return StrDup(ctx, buf);
77 } // get_GLSL_const_array_varname
78
79
80 static inline const char *get_GLSL_input_array_varname(Context *ctx,
81 char *buf, const size_t buflen)
82 {
83 snprintf(buf, buflen, "%s", "vertex_input_array");
84 return buf;
85 } // get_GLSL_input_array_varname
86
87
88 const char *get_GLSL_uniform_array_varname(Context *ctx,
89 const RegisterType regtype,
90 char *buf, const size_t len)
91 {
92 const char *shadertype = ctx->shader_type_str;
93 const char *type = get_GLSL_uniform_type(ctx, regtype);
94 snprintf(buf, len, "%s_uniforms_%s", shadertype, type);
95 return buf;
96 } // get_GLSL_uniform_array_varname
97
98 const char *get_GLSL_destarg_varname(Context *ctx, char *buf, size_t len)
99 {
100 const DestArgInfo *arg = &ctx->dest_arg;
101 return get_GLSL_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, len);
102 } // get_GLSL_destarg_varname
103
104 const char *get_GLSL_srcarg_varname(Context *ctx, const size_t idx,
105 char *buf, size_t len)
106 {
107 if (idx >= STATICARRAYLEN(ctx->source_args))
108 {
109 fail(ctx, "Too many source args");
110 *buf = '\0';
111 return buf;
112 } // if
113
114 const SourceArgInfo *arg = &ctx->source_args[idx];
115 return get_GLSL_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, len);
116 } // get_GLSL_srcarg_varname
117
118
119 const char *make_GLSL_destarg_assign(Context *, char *, const size_t,
120 const char *, ...) ISPRINTF(4,5);
121
122 const char *make_GLSL_destarg_assign(Context *ctx, char *buf,
123 const size_t buflen,
124 const char *fmt, ...)
125 {
126 int need_parens = 0;
127 const DestArgInfo *arg = &ctx->dest_arg;
128
129 if (arg->writemask == 0)
130 {
131 *buf = '\0';
132 return buf; // no writemask? It's a no-op.
133 } // if
134
135 char clampbuf[32] = { '\0' };
136 const char *clampleft = "";
137 const char *clampright = "";
138 if (arg->result_mod & MOD_SATURATE)
139 {
140 const int vecsize = vecsize_from_writemask(arg->writemask);
141 clampleft = "clamp(";
142 if (vecsize == 1)
143 clampright = ", 0.0, 1.0)";
144 else
145 {
146 snprintf(clampbuf, sizeof (clampbuf),
147 ", vec%d(0.0), vec%d(1.0))", vecsize, vecsize);
148 clampright = clampbuf;
149 } // else
150 } // if
151
152 // MSDN says MOD_PP is a hint and many implementations ignore it. So do we.
153
154 // CENTROID only allowed in DCL opcodes, which shouldn't come through here.
155 assert((arg->result_mod & MOD_CENTROID) == 0);
156
157 if (ctx->predicated)
158 {
159 fail(ctx, "predicated destinations unsupported"); // !!! FIXME
160 *buf = '\0';
161 return buf;
162 } // if
163
164 char operation[256];
165 va_list ap;
166 va_start(ap, fmt);
167 const int len = vsnprintf(operation, sizeof (operation), fmt, ap);
168 va_end(ap);
169 if (len >= sizeof (operation))
170 {
171 fail(ctx, "operation string too large"); // I'm lazy. :P
172 *buf = '\0';
173 return buf;
174 } // if
175
176 const char *result_shift_str = "";
177 switch (arg->result_shift)
178 {
179 case 0x1: result_shift_str = " * 2.0"; break;
180 case 0x2: result_shift_str = " * 4.0"; break;
181 case 0x3: result_shift_str = " * 8.0"; break;
182 case 0xD: result_shift_str = " / 8.0"; break;
183 case 0xE: result_shift_str = " / 4.0"; break;
184 case 0xF: result_shift_str = " / 2.0"; break;
185 } // switch
186 need_parens |= (result_shift_str[0] != '\0');
187
188 char regnum_str[16];
189 const char *regtype_str = get_GLSL_register_string(ctx, arg->regtype,
190 arg->regnum, regnum_str,
191 sizeof (regnum_str));
192 char writemask_str[6];
193 size_t i = 0;
194 const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
195 if (!scalar && !writemask_xyzw(arg->writemask))
196 {
197 writemask_str[i++] = '.';
198 if (arg->writemask0) writemask_str[i++] = 'x';
199 if (arg->writemask1) writemask_str[i++] = 'y';
200 if (arg->writemask2) writemask_str[i++] = 'z';
201 if (arg->writemask3) writemask_str[i++] = 'w';
202 } // if
203 writemask_str[i] = '\0';
204 assert(i < sizeof (writemask_str));
205
206 const char *leftparen = (need_parens) ? "(" : "";
207 const char *rightparen = (need_parens) ? ")" : "";
208
209 snprintf(buf, buflen, "%s_%s%s%s = %s%s%s%s%s%s;",
210 ctx->shader_type_str, regtype_str, regnum_str, writemask_str,
211 clampleft, leftparen, operation, rightparen, result_shift_str,
212 clampright);
213 // !!! FIXME: make sure the scratch buffer was large enough.
214 return buf;
215 } // make_GLSL_destarg_assign
216
217
218 char *make_GLSL_swizzle_string(char *swiz_str, const size_t strsize,
219 const int swizzle, const int writemask)
220 {
221 size_t i = 0;
222 if ( (!no_swizzle(swizzle)) || (!writemask_xyzw(writemask)) )
223 {
224 const int writemask0 = (writemask >> 0) & 0x1;
225 const int writemask1 = (writemask >> 1) & 0x1;
226 const int writemask2 = (writemask >> 2) & 0x1;
227 const int writemask3 = (writemask >> 3) & 0x1;
228
229 const int swizzle_x = (swizzle >> 0) & 0x3;
230 const int swizzle_y = (swizzle >> 2) & 0x3;
231 const int swizzle_z = (swizzle >> 4) & 0x3;
232 const int swizzle_w = (swizzle >> 6) & 0x3;
233
234 swiz_str[i++] = '.';
235 if (writemask0) swiz_str[i++] = swizzle_channels[swizzle_x];
236 if (writemask1) swiz_str[i++] = swizzle_channels[swizzle_y];
237 if (writemask2) swiz_str[i++] = swizzle_channels[swizzle_z];
238 if (writemask3) swiz_str[i++] = swizzle_channels[swizzle_w];
239 } // if
240 assert(i < strsize);
241 swiz_str[i] = '\0';
242 return swiz_str;
243 } // make_GLSL_swizzle_string
244
245
246 const char *make_GLSL_srcarg_string(Context *ctx, const size_t idx,
247 const int writemask, char *buf,
248 const size_t buflen)
249 {
250 *buf = '\0';
251
252 if (idx >= STATICARRAYLEN(ctx->source_args))
253 {
254 fail(ctx, "Too many source args");
255 return buf;
256 } // if
257
258 const SourceArgInfo *arg = &ctx->source_args[idx];
259
260 const char *premod_str = "";
261 const char *postmod_str = "";
262 switch (arg->src_mod)
263 {
264 case SRCMOD_NEGATE:
265 premod_str = "-";
266 break;
267
268 case SRCMOD_BIASNEGATE:
269 premod_str = "-(";
270 postmod_str = " - 0.5)";
271 break;
272
273 case SRCMOD_BIAS:
274 premod_str = "(";
275 postmod_str = " - 0.5)";
276 break;
277
278 case SRCMOD_SIGNNEGATE:
279 premod_str = "-((";
280 postmod_str = " - 0.5) * 2.0)";
281 break;
282
283 case SRCMOD_SIGN:
284 premod_str = "((";
285 postmod_str = " - 0.5) * 2.0)";
286 break;
287
288 case SRCMOD_COMPLEMENT:
289 premod_str = "(1.0 - ";
290 postmod_str = ")";
291 break;
292
293 case SRCMOD_X2NEGATE:
294 premod_str = "-(";
295 postmod_str = " * 2.0)";
296 break;
297
298 case SRCMOD_X2:
299 premod_str = "(";
300 postmod_str = " * 2.0)";
301 break;
302
303 case SRCMOD_DZ:
304 fail(ctx, "SRCMOD_DZ unsupported"); return buf; // !!! FIXME
305 postmod_str = "_dz";
306 break;
307
308 case SRCMOD_DW:
309 fail(ctx, "SRCMOD_DW unsupported"); return buf; // !!! FIXME
310 postmod_str = "_dw";
311 break;
312
313 case SRCMOD_ABSNEGATE:
314 premod_str = "-abs(";
315 postmod_str = ")";
316 break;
317
318 case SRCMOD_ABS:
319 premod_str = "abs(";
320 postmod_str = ")";
321 break;
322
323 case SRCMOD_NOT:
324 premod_str = "!";
325 break;
326
327 case SRCMOD_NONE:
328 case SRCMOD_TOTAL:
329 break; // stop compiler whining.
330 } // switch
331
332 const char *regtype_str = NULL;
333
334 if (!arg->relative)
335 {
336 regtype_str = get_GLSL_varname_in_buf(ctx, arg->regtype, arg->regnum,
337 (char *) alloca(64), 64);
338 } // if
339
340 const char *rel_lbracket = "";
341 char rel_offset[32] = { '\0' };
342 const char *rel_rbracket = "";
343 char rel_swizzle[4] = { '\0' };
344 const char *rel_regtype_str = "";
345 if (arg->relative)
346 {
347 if (arg->regtype == REG_TYPE_INPUT)
348 regtype_str=get_GLSL_input_array_varname(ctx,(char*)alloca(64),64);
349 else
350 {
351 assert(arg->regtype == REG_TYPE_CONST);
352 const int arrayidx = arg->relative_array->index;
353 const int offset = arg->regnum - arrayidx;
354 assert(offset >= 0);
355 if (arg->relative_array->constant)
356 {
357 const int arraysize = arg->relative_array->count;
358 regtype_str = get_GLSL_const_array_varname_in_buf(ctx,
359 arrayidx, arraysize, (char *) alloca(64), 64);
360 if (offset != 0)
361 snprintf(rel_offset, sizeof (rel_offset), "%d + ", offset);
362 } // if
363 else
364 {
365 regtype_str = get_GLSL_uniform_array_varname(ctx, arg->regtype,
366 (char *) alloca(64), 64);
367 if (offset == 0)
368 {
369 snprintf(rel_offset, sizeof (rel_offset),
370 "ARRAYBASE_%d + ", arrayidx);
371 } // if
372 else
373 {
374 snprintf(rel_offset, sizeof (rel_offset),
375 "(ARRAYBASE_%d + %d) + ", arrayidx, offset);
376 } // else
377 } // else
378 } // else
379
380 rel_lbracket = "[";
381
382 if (arg->relative_regtype == REG_TYPE_LOOP)
383 {
384 rel_regtype_str = "aL";
385 rel_swizzle[0] = '\0';
386 rel_swizzle[1] = '\0';
387 rel_swizzle[2] = '\0';
388 } // if
389 else
390 {
391 rel_regtype_str = get_GLSL_varname_in_buf(ctx, arg->relative_regtype,
392 arg->relative_regnum,
393 (char *) alloca(64), 64);
394 rel_swizzle[0] = '.';
395 rel_swizzle[1] = swizzle_channels[arg->relative_component];
396 rel_swizzle[2] = '\0';
397 } // else
398 rel_rbracket = "]";
399 } // if
400
401 char swiz_str[6] = { '\0' };
402 if (!isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum))
403 {
404 make_GLSL_swizzle_string(swiz_str, sizeof (swiz_str),
405 arg->swizzle, writemask);
406 } // if
407
408 if (regtype_str == NULL)
409 {
410 fail(ctx, "Unknown source register type.");
411 return buf;
412 } // if
413
414 snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s",
415 premod_str, regtype_str, rel_lbracket, rel_offset,
416 rel_regtype_str, rel_swizzle, rel_rbracket, swiz_str,
417 postmod_str);
418 // !!! FIXME: make sure the scratch buffer was large enough.
419 return buf;
420 } // make_GLSL_srcarg_string
421
422 // generate some convenience functions.
423 #define MAKE_GLSL_SRCARG_STRING_(mask, bitmask) \
424 static inline const char *make_GLSL_srcarg_string_##mask(Context *ctx, \
425 const size_t idx, char *buf, \
426 const size_t buflen) { \
427 return make_GLSL_srcarg_string(ctx, idx, bitmask, buf, buflen); \
428 }
429 MAKE_GLSL_SRCARG_STRING_(x, (1 << 0))
430 MAKE_GLSL_SRCARG_STRING_(y, (1 << 1))
431 MAKE_GLSL_SRCARG_STRING_(z, (1 << 2))
432 MAKE_GLSL_SRCARG_STRING_(w, (1 << 3))
433 MAKE_GLSL_SRCARG_STRING_(scalar, (1 << 0))
434 MAKE_GLSL_SRCARG_STRING_(full, 0xF)
435 MAKE_GLSL_SRCARG_STRING_(masked, ctx->dest_arg.writemask)
436 MAKE_GLSL_SRCARG_STRING_(vec3, 0x7)
437 MAKE_GLSL_SRCARG_STRING_(vec2, 0x3)
438 #undef MAKE_GLSL_SRCARG_STRING_
439
440 // special cases for comparison opcodes...
441
442 const char *get_GLSL_comparison_string_scalar(Context *ctx)
443 {
444 const char *comps[] = { "", ">", "==", ">=", "<", "!=", "<=" };
445 if (ctx->instruction_controls >= STATICARRAYLEN(comps))
446 {
447 fail(ctx, "unknown comparison control");
448 return "";
449 } // if
450
451 return comps[ctx->instruction_controls];
452 } // get_GLSL_comparison_string_scalar
453
454 const char *get_GLSL_comparison_string_vector(Context *ctx)
455 {
456 const char *comps[] = {
457 "", "greaterThan", "equal", "greaterThanEqual", "lessThan",
458 "notEqual", "lessThanEqual"
459 };
460
461 if (ctx->instruction_controls >= STATICARRAYLEN(comps))
462 {
463 fail(ctx, "unknown comparison control");
464 return "";
465 } // if
466
467 return comps[ctx->instruction_controls];
468 } // get_GLSL_comparison_string_vector
469
470 // special extensions needed for texldd/texldl...
471
472 static void prepend_glsl_texlod_extensions(Context *ctx)
473 {
474 // !!! FIXME:
475 // GLSL 1.30 introduced textureGrad() for this, but it looks like the
476 // functions are overloaded instead of texture2DGrad() (etc).
477
478 // !!! FIXME:
479 // The spec says we can't use GLSL's texture*Lod() built-ins from fragment
480 // shaders for some inexplicable reason.
481 // For now, you'll just have to suffer with the potentially wrong mipmap
482 // until I can figure something out.
483
484 // ARB_shader_texture_lod and EXT_gpu_shader4 added texture2DLod/Grad*(),
485 // so we'll use them if available. Failing that, we'll just fallback
486 // to a regular texture2D call and hope the mipmap it chooses is close
487 // enough.
488 if (!ctx->glsl_generated_texlod_setup && !support_glsles3(ctx))
489 {
490 ctx->glsl_generated_texlod_setup = 1;
491 push_output(ctx, &ctx->preflight);
492 output_line(ctx, "#if GL_ARB_shader_texture_lod");
493 output_line(ctx, "#extension GL_ARB_shader_texture_lod : enable");
494 output_line(ctx, "#define texture2DGrad texture2DGradARB");
495 output_line(ctx, "#define texture2DProjGrad texture2DProjARB");
496 output_line(ctx, "#elif GL_EXT_gpu_shader4");
497 output_line(ctx, "#extension GL_EXT_gpu_shader4 : enable");
498 output_line(ctx, "#else");
499 output_line(ctx, "#define texture2DGrad(a,b,c,d) texture2D(a,b)");
500 output_line(ctx, "#define texture2DProjGrad(a,b,c,d) texture2DProj(a,b)");
501 if (shader_is_pixel(ctx))
502 output_line(ctx, "#define texture2DLod(a,b,c) texture2D(a,b)");
503 output_line(ctx, "#endif");
504 output_blank_line(ctx);
505 pop_output(ctx);
506 } // if
507 } // prepend_glsl_texlod_extensions
508
509
510 void emit_GLSL_start(Context *ctx, const char *profilestr)
511 {
512 if (!shader_is_vertex(ctx) && !shader_is_pixel(ctx))
513 {
514 failf(ctx, "Shader type %u unsupported in this profile.",
515 (uint) ctx->shader_type);
516 return;
517 } // if
518
519 else if (strcmp(profilestr, MOJOSHADER_PROFILE_GLSL) == 0)
520 {
521 // No gl_FragData[] before GLSL 1.10, so we have to force the version.
522 push_output(ctx, &ctx->preflight);
523 output_line(ctx, "#version 110");
524 pop_output(ctx);
525 } // else if
526
527 #if SUPPORT_PROFILE_GLSL120
528 else if (strcmp(profilestr, MOJOSHADER_PROFILE_GLSL120) == 0)
529 {
530 ctx->profile_supports_glsl120 = 1;
531 push_output(ctx, &ctx->preflight);
532 output_line(ctx, "#version 120");
533 pop_output(ctx);
534 } // else if
535 #endif
536
537 #if SUPPORT_PROFILE_GLSLES3
538 else if (strcmp(profilestr, MOJOSHADER_PROFILE_GLSLES3) == 0)
539 {
540 ctx->profile_supports_glsles3 = 1;
541 push_output(ctx, &ctx->preflight);
542 output_line(ctx, "#version 300 es");
543 output_line(ctx, "#define texture2D texture");
544 output_line(ctx, "#define texture2DLod textureLod");
545 output_line(ctx, "#define texture2DProj textureProj");
546 output_line(ctx, "#define texture2DGrad textureGrad");
547 output_line(ctx, "#define texture2DProjGrad textureProjGrad");
548 output_line(ctx, "#define texture3D texture");
549 output_line(ctx, "#define texture3DLod textureLod");
550 output_line(ctx, "#define textureCube texture");
551 output_line(ctx, "#define textureCubeLod textureLod");
552 output_line(ctx, "#define texture2DRect texture");
553 output_line(ctx, "#define texture2DRectProj textureProj");
554 output_line(ctx, "#define texture2DRectLod textureLod");
555 if (shader_is_vertex(ctx))
556 output_line(ctx, "precision highp float;");
557 else
558 output_line(ctx, "precision mediump float;");
559 output_line(ctx, "precision mediump int;");
560 pop_output(ctx);
561 } // else if
562 #endif
563
564 #if SUPPORT_PROFILE_GLSLES
565 else if (strcmp(profilestr, MOJOSHADER_PROFILE_GLSLES) == 0)
566 {
567 ctx->profile_supports_glsles = 1;
568 push_output(ctx, &ctx->preflight);
569 output_line(ctx, "#version 100");
570 if (shader_is_vertex(ctx))
571 output_line(ctx, "precision highp float;");
572 else
573 output_line(ctx, "precision mediump float;");
574 output_line(ctx, "precision mediump int;");
575 pop_output(ctx);
576 } // else if
577 #endif
578
579 else
580 {
581 failf(ctx, "Profile '%s' unsupported or unknown.", profilestr);
582 return;
583 } // else
584
585 push_output(ctx, &ctx->mainline_intro);
586 output_line(ctx, "void main()");
587 output_line(ctx, "{");
588 pop_output(ctx);
589
590 set_output(ctx, &ctx->mainline);
591 ctx->indent++;
592 } // emit_GLSL_start
593
594 void emit_GLSL_RET(Context *ctx);
595 void emit_GLSL_end(Context *ctx)
596 {
597 // ps_1_* writes color to r0 instead oC0. We move it to the right place.
598 // We don't have to worry about a RET opcode messing this up, since
599 // RET isn't available before ps_2_0.
600 if (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 2, 0))
601 {
602 const char *shstr = ctx->shader_type_str;
603 set_used_register(ctx, REG_TYPE_COLOROUT, 0, 1);
604 output_line(ctx, "%s_oC0 = %s_r0;", shstr, shstr);
605 } // if
606 else if (shader_is_vertex(ctx))
607 {
608 #ifdef MOJOSHADER_FLIP_RENDERTARGET
609 output_line(ctx, "gl_Position.y = gl_Position.y * vpFlip;");
610 #endif
611 #ifdef MOJOSHADER_DEPTH_CLIPPING
612 output_line(ctx, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;");
613 #endif
614 } // else if
615
616 // force a RET opcode if we're at the end of the stream without one.
617 if (ctx->previous_opcode != OPCODE_RET)
618 emit_GLSL_RET(ctx);
619 } // emit_GLSL_end
620
621 void emit_GLSL_phase(Context *ctx)
622 {
623 // no-op in GLSL.
624 } // emit_GLSL_phase
625
626 void output_GLSL_uniform_array(Context *ctx, const RegisterType regtype,
627 const int size)
628 {
629 if (size > 0)
630 {
631 char buf[64];
632 get_GLSL_uniform_array_varname(ctx, regtype, buf, sizeof (buf));
633 const char *typ;
634 switch (regtype)
635 {
636 case REG_TYPE_CONST: typ = "vec4"; break;
637 case REG_TYPE_CONSTINT: typ ="ivec4"; break;
638 case REG_TYPE_CONSTBOOL: typ = "bool"; break;
639 default:
640 {
641 fail(ctx, "BUG: used a uniform we don't know how to define.");
642 return;
643 } // default
644 } // switch
645 output_line(ctx, "uniform %s %s[%d];", typ, buf, size);
646 } // if
647 } // output_GLSL_uniform_array
648
649 void emit_GLSL_finalize(Context *ctx)
650 {
651 // throw some blank lines around to make source more readable.
652 push_output(ctx, &ctx->globals);
653 output_blank_line(ctx);
654 pop_output(ctx);
655
656 // If we had a relative addressing of REG_TYPE_INPUT, we need to build
657 // an array for it at the start of main(). GLSL doesn't let you specify
658 // arrays of attributes.
659 //vec4 blah_array[BIGGEST_ARRAY];
660 if (ctx->have_relative_input_registers) // !!! FIXME
661 fail(ctx, "Relative addressing of input registers not supported.");
662
663 push_output(ctx, &ctx->preflight);
664 output_GLSL_uniform_array(ctx, REG_TYPE_CONST, ctx->uniform_float4_count);
665 output_GLSL_uniform_array(ctx, REG_TYPE_CONSTINT, ctx->uniform_int4_count);
666 output_GLSL_uniform_array(ctx, REG_TYPE_CONSTBOOL, ctx->uniform_bool_count);
667 #ifdef MOJOSHADER_FLIP_RENDERTARGET
668 if (shader_is_vertex(ctx))
669 output_line(ctx, "uniform float vpFlip;");
670 #endif
671 if (ctx->need_max_float)
672 output_line(ctx, "const float FLT_MAX = 1e38;");
673 pop_output(ctx);
674 } // emit_GLSL_finalize
675
676 void emit_GLSL_global(Context *ctx, RegisterType regtype, int regnum)
677 {
678 char varname[64];
679 get_GLSL_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
680
681 push_output(ctx, &ctx->globals);
682 switch (regtype)
683 {
684 case REG_TYPE_ADDRESS:
685 if (shader_is_vertex(ctx))
686 output_line(ctx, "ivec4 %s;", varname);
687 else if (shader_is_pixel(ctx)) // actually REG_TYPE_TEXTURE.
688 {
689 // We have to map texture registers to temps for ps_1_1, since
690 // they work like temps, initialize with tex coords, and the
691 // ps_1_1 TEX opcode expects to overwrite it.
692 if (!shader_version_atleast(ctx, 1, 4))
693 {
694 // GLSL ES does not have gl_TexCoord!
695 // Also, gl_TexCoord[4+] is unreliable!
696 #if SUPPORT_PROFILE_GLSLES
697 const int skipGLTexCoord = support_glsles(ctx) || (regnum >= 4);
698 #else
699 const int skipGLTexCoord = (regnum >= 4);
700 #endif
701 if (skipGLTexCoord)
702 output_line(ctx, "vec4 %s = io_%i_%i;",
703 varname, MOJOSHADER_USAGE_TEXCOORD, regnum);
704 else
705 output_line(ctx, "vec4 %s = gl_TexCoord[%d];",
706 varname, regnum);
707 } // if
708 } // else if
709 break;
710 case REG_TYPE_PREDICATE:
711 output_line(ctx, "bvec4 %s;", varname);
712 break;
713 case REG_TYPE_TEMP:
714 output_line(ctx, "vec4 %s;", varname);
715 break;
716 case REG_TYPE_LOOP:
717 break; // no-op. We declare these in for loops at the moment.
718 case REG_TYPE_LABEL:
719 break; // no-op. If we see it here, it means we optimized it out.
720 default:
721 fail(ctx, "BUG: we used a register we don't know how to define.");
722 break;
723 } // switch
724 pop_output(ctx);
725 } // emit_GLSL_global
726
727 void emit_GLSL_array(Context *ctx, VariableList *var)
728 {
729 // All uniforms (except constant arrays, which only get pushed once at
730 // compile time) are now packed into a single array, so we can batch
731 // the uniform transfers. So this doesn't actually define an array
732 // here; the one, big array is emitted during finalization instead.
733 // However, we need to #define the offset into the one, big array here,
734 // and let dereferences use that #define.
735 const int base = var->index;
736 const int glslbase = ctx->uniform_float4_count;
737 push_output(ctx, &ctx->globals);
738 output_line(ctx, "#define ARRAYBASE_%d %d", base, glslbase);
739 pop_output(ctx);
740 var->emit_position = glslbase;
741 } // emit_GLSL_array
742
743 void emit_GLSL_const_array(Context *ctx, const ConstantsList *clist,
744 int base, int size)
745 {
746 char varname[64];
747 get_GLSL_const_array_varname_in_buf(ctx,base,size,varname,sizeof(varname));
748
749 #if 0
750 // !!! FIXME: fails on Nvidia's and Apple's GL, even with #version 120.
751 // !!! FIXME: (the 1.20 spec says it should work, though, I think...)
752 if (support_glsl120(ctx))
753 {
754 // GLSL 1.20 can do constant arrays.
755 const char *cstr = NULL;
756 push_output(ctx, &ctx->globals);
757 output_line(ctx, "const vec4 %s[%d] = vec4[%d](", varname, size, size);
758 ctx->indent++;
759
760 int i;
761 for (i = 0; i < size; i++)
762 {
763 while (clist->constant.type != MOJOSHADER_UNIFORM_FLOAT)
764 clist = clist->next;
765 assert(clist->constant.index == (base + i));
766
767 char val0[32];
768 char val1[32];
769 char val2[32];
770 char val3[32];
771 floatstr(ctx, val0, sizeof (val0), clist->constant.value.f[0], 1);
772 floatstr(ctx, val1, sizeof (val1), clist->constant.value.f[1], 1);
773 floatstr(ctx, val2, sizeof (val2), clist->constant.value.f[2], 1);
774 floatstr(ctx, val3, sizeof (val3), clist->constant.value.f[3], 1);
775
776 output_line(ctx, "vec4(%s, %s, %s, %s)%s", val0, val1, val2, val3,
777 (i < (size-1)) ? "," : "");
778
779 clist = clist->next;
780 } // for
781
782 ctx->indent--;
783 output_line(ctx, ");");
784 pop_output(ctx);
785 } // if
786
787 else
788 #endif
789 {
790 // stock GLSL 1.0 can't do constant arrays, so make a uniform array
791 // and have the OpenGL glue assign it at link time. Lame!
792 push_output(ctx, &ctx->globals);
793 output_line(ctx, "uniform vec4 %s[%d];", varname, size);
794 pop_output(ctx);
795 } // else
796 } // emit_GLSL_const_array
797
798 void emit_GLSL_uniform(Context *ctx, RegisterType regtype, int regnum,
799 const VariableList *var)
800 {
801 // Now that we're pushing all the uniforms as one big array, pack these
802 // down, so if we only use register c439, it'll actually map to
803 // glsl_uniforms_vec4[0]. As we push one big array, this will prevent
804 // uploading unused data.
805
806 char varname[64];
807 char name[64];
808 int index = 0;
809
810 get_GLSL_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
811
812 push_output(ctx, &ctx->globals);
813
814 if (var == NULL)
815 {
816 get_GLSL_uniform_array_varname(ctx, regtype, name, sizeof (name));
817
818 if (regtype == REG_TYPE_CONST)
819 index = ctx->uniform_float4_count;
820 else if (regtype == REG_TYPE_CONSTINT)
821 index = ctx->uniform_int4_count;
822 else if (regtype == REG_TYPE_CONSTBOOL)
823 index = ctx->uniform_bool_count;
824 else // get_GLSL_uniform_array_varname() would have called fail().
825 assert(!(ctx->isfail));
826
827 output_line(ctx, "#define %s %s[%d]", varname, name, index);
828 } // if
829
830 else
831 {
832 const int arraybase = var->index;
833 if (var->constant)
834 {
835 get_GLSL_const_array_varname_in_buf(ctx, arraybase, var->count,
836 name, sizeof (name));
837 index = (regnum - arraybase);
838 } // if
839 else
840 {
841 assert(var->emit_position != -1);
842 get_GLSL_uniform_array_varname(ctx, regtype, name, sizeof (name));
843 index = (regnum - arraybase) + var->emit_position;
844 } // else
845
846 output_line(ctx, "#define %s %s[%d]", varname, name, index);
847 } // else
848
849 pop_output(ctx);
850 } // emit_GLSL_uniform
851
852 void emit_GLSL_sampler(Context *ctx,int stage,TextureType ttype,int tb)
853 {
854 const char *type = "";
855 switch (ttype)
856 {
857 case TEXTURE_TYPE_2D: type = "sampler2D"; break;
858 case TEXTURE_TYPE_CUBE: type = "samplerCube"; break;
859 case TEXTURE_TYPE_VOLUME: type = "sampler3D"; break;
860 default: fail(ctx, "BUG: used a sampler we don't know how to define.");
861 } // switch
862
863 char var[64];
864 get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, stage, var, sizeof (var));
865
866 push_output(ctx, &ctx->globals);
867 output_line(ctx, "uniform %s %s;", type, var);
868 if (tb) // This sampler used a ps_1_1 TEXBEM opcode?
869 {
870 char name[64];
871 const int index = ctx->uniform_float4_count;
872 ctx->uniform_float4_count += 2;
873 get_GLSL_uniform_array_varname(ctx, REG_TYPE_CONST, name, sizeof (name));
874 output_line(ctx, "#define %s_texbem %s[%d]", var, name, index);
875 output_line(ctx, "#define %s_texbeml %s[%d]", var, name, index+1);
876 } // if
877 pop_output(ctx);
878 } // emit_GLSL_sampler
879
880 void emit_GLSL_attribute(Context *ctx, RegisterType regtype, int regnum,
881 MOJOSHADER_usage usage, int index, int wmask,
882 int flags)
883 {
884 // !!! FIXME: this function doesn't deal with write masks at all yet!
885 const char *qualifier_in;
886 const char *qualifier_out;
887 const char *usage_str = NULL;
888 const char *arrayleft = "";
889 const char *arrayright = "";
890 char index_str[16] = { '\0' };
891 char var[64];
892
893 get_GLSL_varname_in_buf(ctx, regtype, regnum, var, sizeof (var));
894
895 //assert((flags & MOD_PP) == 0); // !!! FIXME: is PP allowed?
896
897 if (index != 0) // !!! FIXME: a lot of these MUST be zero.
898 snprintf(index_str, sizeof (index_str), "%u", (uint) index);
899
900 if (shader_is_vertex(ctx))
901 {
902 if (support_glsles3(ctx))
903 {
904 qualifier_in = "in";
905 qualifier_out = "out";
906 }
907 else
908 {
909 qualifier_in = "attribute";
910 qualifier_out = "varying";
911 }
912
913 // pre-vs3 output registers.
914 // these don't ever happen in DCL opcodes, I think. Map to vs_3_*
915 // output registers.
916 if (!shader_version_atleast(ctx, 3, 0))
917 {
918 if (regtype == REG_TYPE_RASTOUT)
919 {
920 regtype = REG_TYPE_OUTPUT;
921 index = regnum;
922 switch ((const RastOutType) regnum)
923 {
924 case RASTOUT_TYPE_POSITION:
925 usage = MOJOSHADER_USAGE_POSITION;
926 break;
927 case RASTOUT_TYPE_FOG:
928 usage = MOJOSHADER_USAGE_FOG;
929 break;
930 case RASTOUT_TYPE_POINT_SIZE:
931 usage = MOJOSHADER_USAGE_POINTSIZE;
932 break;
933 } // switch
934 } // if
935
936 else if (regtype == REG_TYPE_ATTROUT)
937 {
938 regtype = REG_TYPE_OUTPUT;
939 usage = MOJOSHADER_USAGE_COLOR;
940 index = regnum;
941 } // else if
942
943 else if (regtype == REG_TYPE_TEXCRDOUT)
944 {
945 regtype = REG_TYPE_OUTPUT;
946 usage = MOJOSHADER_USAGE_TEXCOORD;
947 index = regnum;
948 } // else if
949 } // if
950
951 // to avoid limitations of various GL entry points for input
952 // attributes (glSecondaryColorPointer() can only take 3 component
953 // items, glVertexPointer() can't do GL_UNSIGNED_BYTE, many other
954 // issues), we set up all inputs as generic vertex attributes, so we
955 // can pass data in just about any form, and ignore the built-in GLSL
956 // attributes like gl_SecondaryColor. Output needs to use the the
957 // built-ins, though, but we don't have to worry about the GL entry
958 // point limitations there.
959
960 if (regtype == REG_TYPE_INPUT)
961 {
962 push_output(ctx, &ctx->globals);
963 output_line(ctx, "%s vec4 %s;", qualifier_in, var);
964 pop_output(ctx);
965 } // if
966
967 else if (regtype == REG_TYPE_OUTPUT)
968 {
969 switch (usage)
970 {
971 case MOJOSHADER_USAGE_POSITION:
972 if (index == 0)
973 {
974 usage_str = "gl_Position";
975 } // if
976 break;
977 case MOJOSHADER_USAGE_POINTSIZE:
978 if (index == 0)
979 {
980 usage_str = "gl_PointSize";
981 } // if
982 else
983 {
984 push_output(ctx, &ctx->globals);
985 #if SUPPORT_PROFILE_GLSLES
986 if (support_glsles(ctx))
987 output_line(ctx, "%s highp float io_%i_%i;", qualifier_out, usage, index);
988 else
989 #endif
990 output_line(ctx, "varying float io_%i_%i;", usage, index);
991 output_line(ctx, "#define %s io_%i_%i", var, usage, index);
992 pop_output(ctx);
993 return;
994 }
995 break;
996 case MOJOSHADER_USAGE_COLOR:
997 #if SUPPORT_PROFILE_GLSLES
998 if (support_glsles(ctx))
999 break; // GLSL ES does not have gl_FrontColor
1000 #endif
1001 index_str[0] = '\0'; // no explicit number.
1002 if (index == 0)
1003 {
1004 usage_str = "gl_FrontColor";
1005 } // if
1006 else if (index == 1)
1007 {
1008 usage_str = "gl_FrontSecondaryColor";
1009 } // else if
1010 break;
1011 case MOJOSHADER_USAGE_FOG:
1012 #if SUPPORT_PROFILE_GLSLES
1013 if (support_glsles(ctx))
1014 break; // GLSL ES does not have gl_FogFragCoord
1015 #endif
1016 if (index == 0)
1017 {
1018 usage_str = "gl_FogFragCoord";
1019 } // if
1020 else
1021 {
1022 push_output(ctx, &ctx->globals);
1023 #if SUPPORT_PROFILE_GLSLES
1024 if (support_glsles(ctx))
1025 output_line(ctx, "%s highp float io_%i_%i;", qualifier_out, usage, index);
1026 else
1027 #endif
1028 output_line(ctx, "varying float io_%i_%i;", usage, index);
1029 output_line(ctx, "#define %s io_%i_%i", var, usage, index);
1030 pop_output(ctx);
1031 return;
1032 }
1033 break;
1034 case MOJOSHADER_USAGE_TEXCOORD:
1035 #if SUPPORT_PROFILE_GLSLES
1036 if (support_glsles(ctx))
1037 break; // GLSL ES does not have gl_TexCoord
1038 #endif
1039 if (index >= 4)
1040 break; // gl_TexCoord[4+] is unreliable!
1041 snprintf(index_str, sizeof (index_str), "%u", (uint) index);
1042 usage_str = "gl_TexCoord";
1043 arrayleft = "[";
1044 arrayright = "]";
1045 break;
1046 default:
1047 // !!! FIXME: we need to deal with some more built-in varyings here.
1048 break;
1049 } // switch
1050
1051 // !!! FIXME: the #define is a little hacky, but it means we don't
1052 // !!! FIXME: have to track these separately if this works.
1053 push_output(ctx, &ctx->globals);
1054 // no mapping to built-in var? Just make it a regular global, pray.
1055 if (usage_str == NULL)
1056 {
1057 #if SUPPORT_PROFILE_GLSLES
1058 if (support_glsles(ctx))
1059 output_line(ctx, "%s highp vec4 io_%i_%i;", qualifier_out, usage, index);
1060 else
1061 #endif
1062 output_line(ctx, "varying vec4 io_%i_%i;", usage, index);
1063 output_line(ctx, "#define %s io_%i_%i", var, usage, index);
1064 } // if
1065 else
1066 {
1067 output_line(ctx, "#define %s %s%s%s%s", var, usage_str,
1068 arrayleft, index_str, arrayright);
1069 } // else
1070 pop_output(ctx);
1071 } // else if
1072
1073 else
1074 {
1075 fail(ctx, "unknown vertex shader attribute register");
1076 } // else
1077 } // if
1078
1079 else if (shader_is_pixel(ctx))
1080 {
1081 if (support_glsles3(ctx))
1082 qualifier_in = "in";
1083 else
1084 qualifier_in = "varying";
1085
1086 // samplers DCLs get handled in emit_GLSL_sampler().
1087
1088 if (flags & MOD_CENTROID) // !!! FIXME
1089 {
1090 failf(ctx, "centroid unsupported in %s profile", ctx->profile->name);
1091 return;
1092 } // if
1093
1094 if (regtype == REG_TYPE_COLOROUT)
1095 {
1096 // gl_FragColor/FragData were deprecated in favor of manually
1097 // defining your own output variables, GLSL ES 3.x enforces the new
1098 // syntax, so let's use it.
1099 if (support_glsles3(ctx))
1100 {
1101 usage_str = "_gl_FragData";
1102 snprintf(index_str, sizeof (index_str), "_%u", (uint) regnum );
1103
1104 push_output(ctx, &ctx->globals);
1105 output_line(ctx, "layout(location = %u) out highp vec4 %s%s;", regnum, usage_str, index_str);
1106 pop_output(ctx);
1107 }
1108 else
1109 {
1110 if (!ctx->have_multi_color_outputs)
1111 usage_str = "gl_FragColor"; // maybe faster?
1112 else
1113 {
1114 snprintf(index_str, sizeof (index_str), "%u", (uint) regnum);
1115 usage_str = "gl_FragData";
1116 arrayleft = "[";
1117 arrayright = "]";
1118 } // else
1119 } // else
1120 } // if
1121
1122 else if (regtype == REG_TYPE_DEPTHOUT)
1123 usage_str = "gl_FragDepth";
1124
1125 // !!! FIXME: can you actualy have a texture register with COLOR usage?
1126 else if ((regtype == REG_TYPE_TEXTURE) || (regtype == REG_TYPE_INPUT))
1127 {
1128 #if SUPPORT_PROFILE_GLSLES
1129 if (!support_glsles(ctx))
1130 {
1131 #endif
1132 if (usage == MOJOSHADER_USAGE_TEXCOORD)
1133 {
1134 // ps_1_1 does a different hack for this attribute.
1135 // Refer to emit_GLSL_global()'s REG_TYPE_ADDRESS code.
1136 if (!shader_version_atleast(ctx, 1, 4))
1137 return;
1138 else if (index < 4) // gl_TexCoord[4+] is unreliable!
1139 {
1140 snprintf(index_str, sizeof (index_str), "%u", (uint) index);
1141 usage_str = "gl_TexCoord";
1142 arrayleft = "[";
1143 arrayright = "]";
1144 } // else if
1145 } // if
1146
1147 else if (usage == MOJOSHADER_USAGE_COLOR)
1148 {
1149 index_str[0] = '\0'; // no explicit number.
1150 if (index == 0)
1151 {
1152 usage_str = "gl_Color";
1153 } // if
1154 else if (index == 1)
1155 {
1156 usage_str = "gl_SecondaryColor";
1157 } // else if
1158 // FIXME: Does this even matter when we have varyings? -flibit
1159 // else
1160 // fail(ctx, "unsupported color index");
1161 } // else if
1162 #if SUPPORT_PROFILE_GLSLES
1163 } // if
1164 #endif
1165 } // else if
1166
1167 else if (regtype == REG_TYPE_MISCTYPE)
1168 {
1169 const MiscTypeType mt = (MiscTypeType) regnum;
1170 if (mt == MISCTYPE_TYPE_FACE)
1171 {
1172 push_output(ctx, &ctx->mainline_intro);
1173 ctx->indent++;
1174 output_line(ctx, "float %s = gl_FrontFacing ? 1.0 : -1.0;", var);
1175 pop_output(ctx);
1176 return;
1177 } // if
1178 else if (mt == MISCTYPE_TYPE_POSITION)
1179 {
1180 push_output(ctx, &ctx->globals);
1181 output_line(ctx, "uniform vec2 vposFlip;");
1182 pop_output(ctx);
1183
1184 // TODO: For half-pixel offset compensation, floor() this value!
1185 push_output(ctx, &ctx->mainline_intro);
1186 ctx->indent++;
1187 output_line(ctx, "vec4 %s = vec4(gl_FragCoord.x, (gl_FragCoord.y * vposFlip.x) + vposFlip.y, gl_FragCoord.z, gl_FragCoord.w);", var);
1188 pop_output(ctx);
1189 return;
1190 } // else if
1191 else
1192 {
1193 fail(ctx, "BUG: unhandled misc register");
1194 } // else
1195 } // else if
1196
1197 else
1198 {
1199 fail(ctx, "unknown pixel shader attribute register");
1200 } // else
1201
1202 push_output(ctx, &ctx->globals);
1203 // no mapping to built-in var? Just make it a regular global, pray.
1204 if (usage_str == NULL)
1205 {
1206 #if SUPPORT_PROFILE_GLSLES
1207 if (support_glsles(ctx))
1208 output_line(ctx, "%s highp vec4 io_%i_%i;", qualifier_in, usage, index);
1209 else
1210 #endif
1211 output_line(ctx, "varying vec4 io_%i_%i;", usage, index);
1212 output_line(ctx, "#define %s io_%i_%i", var, usage, index);
1213 } // if
1214 else
1215 {
1216 output_line(ctx, "#define %s %s%s%s%s", var, usage_str,
1217 arrayleft, index_str, arrayright);
1218 } // else
1219 pop_output(ctx);
1220 } // else if
1221
1222 else
1223 {
1224 fail(ctx, "Unknown shader type"); // state machine should catch this.
1225 } // else
1226 } // emit_GLSL_attribute
1227
1228 void emit_GLSL_NOP(Context *ctx)
1229 {
1230 // no-op is a no-op. :)
1231 } // emit_GLSL_NOP
1232
1233 void emit_GLSL_MOV(Context *ctx)
1234 {
1235 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1236 char code[128];
1237 make_GLSL_destarg_assign(ctx, code, sizeof (code), "%s", src0);
1238 output_line(ctx, "%s", code);
1239 } // emit_GLSL_MOV
1240
1241 void emit_GLSL_ADD(Context *ctx)
1242 {
1243 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1244 char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
1245 char code[128];
1246 make_GLSL_destarg_assign(ctx, code, sizeof (code), "%s + %s", src0, src1);
1247 output_line(ctx, "%s", code);
1248 } // emit_GLSL_ADD
1249
1250 void emit_GLSL_SUB(Context *ctx)
1251 {
1252 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1253 char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
1254 char code[128];
1255 make_GLSL_destarg_assign(ctx, code, sizeof (code), "%s - %s", src0, src1);
1256 output_line(ctx, "%s", code);
1257 } // emit_GLSL_SUB
1258
1259 void emit_GLSL_MAD(Context *ctx)
1260 {
1261 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1262 char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
1263 char src2[64]; make_GLSL_srcarg_string_masked(ctx, 2, src2, sizeof (src2));
1264 char code[128];
1265 make_GLSL_destarg_assign(ctx, code, sizeof (code), "(%s * %s) + %s", src0, src1, src2);
1266 output_line(ctx, "%s", code);
1267 } // emit_GLSL_MAD
1268
1269 void emit_GLSL_MUL(Context *ctx)
1270 {
1271 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1272 char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
1273 char code[128];
1274 make_GLSL_destarg_assign(ctx, code, sizeof (code), "%s * %s", src0, src1);
1275 output_line(ctx, "%s", code);
1276 } // emit_GLSL_MUL
1277
1278 void emit_GLSL_RCP(Context *ctx)
1279 {
1280 const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
1281 char cast[16] = { '\0' };
1282 if (vecsize != 1)
1283 snprintf(cast, sizeof (cast), "vec%d", vecsize);
1284 char src0[64]; make_GLSL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0));
1285 char code[128];
1286 ctx->need_max_float = 1;
1287 make_GLSL_destarg_assign(ctx, code, sizeof (code), "%s((%s == 0.0) ? FLT_MAX : 1.0 / %s)", cast, src0, src0);
1288 output_line(ctx, "%s", code);
1289 } // emit_GLSL_RCP
1290
1291 void emit_GLSL_RSQ(Context *ctx)
1292 {
1293 const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
1294 char cast[16] = { '\0' };
1295 if (vecsize != 1)
1296 snprintf(cast, sizeof (cast), "vec%d", vecsize);
1297
1298 char src0[64]; make_GLSL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0));
1299 char code[128];
1300 ctx->need_max_float = 1;
1301 make_GLSL_destarg_assign(ctx, code, sizeof (code), "%s((%s == 0.0) ? FLT_MAX : inversesqrt(abs(%s)))", cast, src0, src0);
1302 output_line(ctx, "%s", code);
1303 } // emit_GLSL_RSQ
1304
1305 void emit_GLSL_dotprod(Context *ctx, const char *src0, const char *src1,
1306 const char *extra)
1307 {
1308 const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
1309 char castleft[16] = { '\0' };
1310 const char *castright = "";
1311 if (vecsize != 1)
1312 {
1313 snprintf(castleft, sizeof (castleft), "vec%d(", vecsize);
1314 castright = ")";
1315 } // if
1316
1317 char code[128];
1318 make_GLSL_destarg_assign(ctx, code, sizeof (code), "%sdot(%s, %s)%s%s",
1319 castleft, src0, src1, extra, castright);
1320 output_line(ctx, "%s", code);
1321 } // emit_GLSL_dotprod
1322
1323 void emit_GLSL_DP3(Context *ctx)
1324 {
1325 char src0[64]; make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
1326 char src1[64]; make_GLSL_srcarg_string_vec3(ctx, 1, src1, sizeof (src1));
1327 emit_GLSL_dotprod(ctx, src0, src1, "");
1328 } // emit_GLSL_DP3
1329
1330 void emit_GLSL_DP4(Context *ctx)
1331 {
1332 char src0[64]; make_GLSL_srcarg_string_full(ctx, 0, src0, sizeof (src0));
1333 char src1[64]; make_GLSL_srcarg_string_full(ctx, 1, src1, sizeof (src1));
1334 emit_GLSL_dotprod(ctx, src0, src1, "");
1335 } // emit_GLSL_DP4
1336
1337 void emit_GLSL_MIN(Context *ctx)
1338 {
1339 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1340 char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
1341 char code[128];
1342 make_GLSL_destarg_assign(ctx, code, sizeof (code), "min(%s, %s)", src0, src1);
1343 output_line(ctx, "%s", code);
1344 } // emit_GLSL_MIN
1345
1346 void emit_GLSL_MAX(Context *ctx)
1347 {
1348 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1349 char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
1350 char code[128];
1351 make_GLSL_destarg_assign(ctx, code, sizeof (code), "max(%s, %s)", src0, src1);
1352 output_line(ctx, "%s", code);
1353 } // emit_GLSL_MAX
1354
1355 void emit_GLSL_SLT(Context *ctx)
1356 {
1357 const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
1358 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1359 char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
1360 char code[128];
1361
1362 // float(bool) or vec(bvec) results in 0.0 or 1.0, like SLT wants.
1363 if (vecsize == 1)
1364 make_GLSL_destarg_assign(ctx, code, sizeof (code), "float(%s < %s)", src0, src1);
1365 else
1366 {
1367 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1368 "vec%d(lessThan(%s, %s))",
1369 vecsize, src0, src1);
1370 } // else
1371 output_line(ctx, "%s", code);
1372 } // emit_GLSL_SLT
1373
1374 void emit_GLSL_SGE(Context *ctx)
1375 {
1376 const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
1377 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1378 char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
1379 char code[128];
1380
1381 // float(bool) or vec(bvec) results in 0.0 or 1.0, like SGE wants.
1382 if (vecsize == 1)
1383 {
1384 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1385 "float(%s >= %s)", src0, src1);
1386 } // if
1387 else
1388 {
1389 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1390 "vec%d(greaterThanEqual(%s, %s))",
1391 vecsize, src0, src1);
1392 } // else
1393 output_line(ctx, "%s", code);
1394 } // emit_GLSL_SGE
1395
1396 void emit_GLSL_EXP(Context *ctx)
1397 {
1398 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1399 char code[128];
1400 make_GLSL_destarg_assign(ctx, code, sizeof (code), "exp2(%s)", src0);
1401 output_line(ctx, "%s", code);
1402 } // emit_GLSL_EXP
1403
1404 void emit_GLSL_LOG(Context *ctx)
1405 {
1406 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1407 char code[128];
1408 make_GLSL_destarg_assign(ctx, code, sizeof (code), "log2(%s)", src0);
1409 output_line(ctx, "%s", code);
1410 } // emit_GLSL_LOG
1411
1412 void emit_GLSL_LIT_helper(Context *ctx)
1413 {
1414 const char *maxp = "127.9961"; // value from the dx9 reference.
1415
1416 if (ctx->glsl_generated_lit_helper)
1417 return;
1418
1419 ctx->glsl_generated_lit_helper = 1;
1420
1421 push_output(ctx, &ctx->helpers);
1422 output_line(ctx, "vec4 LIT(const vec4 src)");
1423 output_line(ctx, "{"); ctx->indent++;
1424 output_line(ctx, "float power = clamp(src.w, -%s, %s);",maxp,maxp);
1425 output_line(ctx, "vec4 retval = vec4(1.0, 0.0, 0.0, 1.0);");
1426 output_line(ctx, "if (src.x > 0.0) {"); ctx->indent++;
1427 output_line(ctx, "retval.y = src.x;");
1428 output_line(ctx, "if (src.y > 0.0) {"); ctx->indent++;
1429 output_line(ctx, "retval.z = pow(src.y, power);"); ctx->indent--;
1430 output_line(ctx, "}"); ctx->indent--;
1431 output_line(ctx, "}");
1432 output_line(ctx, "return retval;"); ctx->indent--;
1433 output_line(ctx, "}");
1434 output_blank_line(ctx);
1435 pop_output(ctx);
1436 } // emit_GLSL_LIT_helper
1437
1438 void emit_GLSL_LIT(Context *ctx)
1439 {
1440 char src0[64]; make_GLSL_srcarg_string_full(ctx, 0, src0, sizeof (src0));
1441 char code[128];
1442 emit_GLSL_LIT_helper(ctx);
1443 make_GLSL_destarg_assign(ctx, code, sizeof (code), "LIT(%s)", src0);
1444 output_line(ctx, "%s", code);
1445 } // emit_GLSL_LIT
1446
1447 void emit_GLSL_DST(Context *ctx)
1448 {
1449 // !!! FIXME: needs to take ctx->dst_arg.writemask into account.
1450 char src0_y[64]; make_GLSL_srcarg_string_y(ctx, 0, src0_y, sizeof (src0_y));
1451 char src1_y[64]; make_GLSL_srcarg_string_y(ctx, 1, src1_y, sizeof (src1_y));
1452 char src0_z[64]; make_GLSL_srcarg_string_z(ctx, 0, src0_z, sizeof (src0_z));
1453 char src1_w[64]; make_GLSL_srcarg_string_w(ctx, 1, src1_w, sizeof (src1_w));
1454
1455 char code[128];
1456 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1457 "vec4(1.0, %s * %s, %s, %s)",
1458 src0_y, src1_y, src0_z, src1_w);
1459 output_line(ctx, "%s", code);
1460 } // emit_GLSL_DST
1461
1462 void emit_GLSL_LRP(Context *ctx)
1463 {
1464 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1465 char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
1466 char src2[64]; make_GLSL_srcarg_string_masked(ctx, 2, src2, sizeof (src2));
1467 char code[128];
1468 make_GLSL_destarg_assign(ctx, code, sizeof (code), "mix(%s, %s, %s)",
1469 src2, src1, src0);
1470 output_line(ctx, "%s", code);
1471 } // emit_GLSL_LRP
1472
1473 void emit_GLSL_FRC(Context *ctx)
1474 {
1475 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1476 char code[128];
1477 make_GLSL_destarg_assign(ctx, code, sizeof (code), "fract(%s)", src0);
1478 output_line(ctx, "%s", code);
1479 } // emit_GLSL_FRC
1480
1481 void emit_GLSL_M4X4(Context *ctx)
1482 {
1483 char src0[64]; make_GLSL_srcarg_string_full(ctx, 0, src0, sizeof (src0));
1484 char row0[64]; make_GLSL_srcarg_string_full(ctx, 1, row0, sizeof (row0));
1485 char row1[64]; make_GLSL_srcarg_string_full(ctx, 2, row1, sizeof (row1));
1486 char row2[64]; make_GLSL_srcarg_string_full(ctx, 3, row2, sizeof (row2));
1487 char row3[64]; make_GLSL_srcarg_string_full(ctx, 4, row3, sizeof (row3));
1488 char code[256];
1489 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1490 "vec4(dot(%s, %s), dot(%s, %s), dot(%s, %s), dot(%s, %s))",
1491 src0, row0, src0, row1, src0, row2, src0, row3);
1492 output_line(ctx, "%s", code);
1493 } // emit_GLSL_M4X4
1494
1495 void emit_GLSL_M4X3(Context *ctx)
1496 {
1497 char src0[64]; make_GLSL_srcarg_string_full(ctx, 0, src0, sizeof (src0));
1498 char row0[64]; make_GLSL_srcarg_string_full(ctx, 1, row0, sizeof (row0));
1499 char row1[64]; make_GLSL_srcarg_string_full(ctx, 2, row1, sizeof (row1));
1500 char row2[64]; make_GLSL_srcarg_string_full(ctx, 3, row2, sizeof (row2));
1501 char code[256];
1502 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1503 "vec3(dot(%s, %s), dot(%s, %s), dot(%s, %s))",
1504 src0, row0, src0, row1, src0, row2);
1505 output_line(ctx, "%s", code);
1506 } // emit_GLSL_M4X3
1507
1508 void emit_GLSL_M3X4(Context *ctx)
1509 {
1510 char src0[64]; make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
1511 char row0[64]; make_GLSL_srcarg_string_vec3(ctx, 1, row0, sizeof (row0));
1512 char row1[64]; make_GLSL_srcarg_string_vec3(ctx, 2, row1, sizeof (row1));
1513 char row2[64]; make_GLSL_srcarg_string_vec3(ctx, 3, row2, sizeof (row2));
1514 char row3[64]; make_GLSL_srcarg_string_vec3(ctx, 4, row3, sizeof (row3));
1515
1516 char code[256];
1517 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1518 "vec4(dot(%s, %s), dot(%s, %s), "
1519 "dot(%s, %s), dot(%s, %s))",
1520 src0, row0, src0, row1,
1521 src0, row2, src0, row3);
1522 output_line(ctx, "%s", code);
1523 } // emit_GLSL_M3X4
1524
1525 void emit_GLSL_M3X3(Context *ctx)
1526 {
1527 char src0[64]; make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
1528 char row0[64]; make_GLSL_srcarg_string_vec3(ctx, 1, row0, sizeof (row0));
1529 char row1[64]; make_GLSL_srcarg_string_vec3(ctx, 2, row1, sizeof (row1));
1530 char row2[64]; make_GLSL_srcarg_string_vec3(ctx, 3, row2, sizeof (row2));
1531 char code[256];
1532 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1533 "vec3(dot(%s, %s), dot(%s, %s), dot(%s, %s))",
1534 src0, row0, src0, row1, src0, row2);
1535 output_line(ctx, "%s", code);
1536 } // emit_GLSL_M3X3
1537
1538 void emit_GLSL_M3X2(Context *ctx)
1539 {
1540 char src0[64]; make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
1541 char row0[64]; make_GLSL_srcarg_string_vec3(ctx, 1, row0, sizeof (row0));
1542 char row1[64]; make_GLSL_srcarg_string_vec3(ctx, 2, row1, sizeof (row1));
1543
1544 char code[256];
1545 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1546 "vec2(dot(%s, %s), dot(%s, %s))",
1547 src0, row0, src0, row1);
1548 output_line(ctx, "%s", code);
1549 } // emit_GLSL_M3X2
1550
1551 void emit_GLSL_CALL(Context *ctx)
1552 {
1553 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1554 if (ctx->loops > 0)
1555 output_line(ctx, "%s(aL);", src0);
1556 else
1557 output_line(ctx, "%s();", src0);
1558 } // emit_GLSL_CALL
1559
1560 void emit_GLSL_CALLNZ(Context *ctx)
1561 {
1562 // !!! FIXME: if src1 is a constbool that's true, we can remove the
1563 // !!! FIXME: if. If it's false, we can make this a no-op.
1564 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1565 char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
1566
1567 if (ctx->loops > 0)
1568 output_line(ctx, "if (%s) { %s(aL); }", src1, src0);
1569 else
1570 output_line(ctx, "if (%s) { %s(); }", src1, src0);
1571 } // emit_GLSL_CALLNZ
1572
1573 void emit_GLSL_LOOP(Context *ctx)
1574 {
1575 // !!! FIXME: swizzle?
1576 char var[64]; get_GLSL_srcarg_varname(ctx, 1, var, sizeof (var));
1577 assert(ctx->source_args[0].regnum == 0); // in case they add aL1 someday.
1578 output_line(ctx, "{");
1579 ctx->indent++;
1580 output_line(ctx, "const int aLend = %s.x + %s.y;", var, var);
1581 output_line(ctx, "for (int aL = %s.y; aL < aLend; aL += %s.z) {", var, var);
1582 ctx->indent++;
1583 } // emit_GLSL_LOOP
1584
1585 void emit_GLSL_RET(Context *ctx)
1586 {
1587 // thankfully, the MSDN specs say a RET _has_ to end a function...no
1588 // early returns. So if you hit one, you know you can safely close
1589 // a high-level function.
1590 ctx->indent--;
1591 output_line(ctx, "}");
1592 output_blank_line(ctx);
1593 set_output(ctx, &ctx->subroutines); // !!! FIXME: is this for LABEL? Maybe set it there so we don't allocate unnecessarily.
1594 } // emit_GLSL_RET
1595
1596 void emit_GLSL_ENDLOOP(Context *ctx)
1597 {
1598 ctx->indent--;
1599 output_line(ctx, "}");
1600 ctx->indent--;
1601 output_line(ctx, "}");
1602 } // emit_GLSL_ENDLOOP
1603
1604 void emit_GLSL_LABEL(Context *ctx)
1605 {
1606 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1607 const int label = ctx->source_args[0].regnum;
1608 RegisterList *reg = reglist_find(&ctx->used_registers, REG_TYPE_LABEL, label);
1609 assert(ctx->output == ctx->subroutines); // not mainline, etc.
1610 assert(ctx->indent == 0); // we shouldn't be in the middle of a function.
1611
1612 // MSDN specs say CALL* has to come before the LABEL, so we know if we
1613 // can ditch the entire function here as unused.
1614 if (reg == NULL)
1615 set_output(ctx, &ctx->ignore); // Func not used. Parse, but don't output.
1616
1617 // !!! FIXME: it would be nice if we could determine if a function is
1618 // !!! FIXME: only called once and, if so, forcibly inline it.
1619
1620 const char *uses_loopreg = ((reg) && (reg->misc == 1)) ? "int aL" : "";
1621 output_line(ctx, "void %s(%s)", src0, uses_loopreg);
1622 output_line(ctx, "{");
1623 ctx->indent++;
1624 } // emit_GLSL_LABEL
1625
1626 void emit_GLSL_DCL(Context *ctx)
1627 {
1628 // no-op. We do this in our emit_attribute() and emit_uniform().
1629 } // emit_GLSL_DCL
1630
1631 void emit_GLSL_POW(Context *ctx)
1632 {
1633 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1634 char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
1635 char code[128];
1636 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1637 "pow(abs(%s), %s)", src0, src1);
1638 output_line(ctx, "%s", code);
1639 } // emit_GLSL_POW
1640
1641 void emit_GLSL_CRS(Context *ctx)
1642 {
1643 // !!! FIXME: needs to take ctx->dst_arg.writemask into account.
1644 char src0[64]; make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
1645 char src1[64]; make_GLSL_srcarg_string_vec3(ctx, 1, src1, sizeof (src1));
1646 char code[128];
1647 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1648 "cross(%s, %s)", src0, src1);
1649 output_line(ctx, "%s", code);
1650 } // emit_GLSL_CRS
1651
1652 void emit_GLSL_SGN(Context *ctx)
1653 {
1654 // (we don't need the temporary registers specified for the D3D opcode.)
1655 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1656 char code[128];
1657 make_GLSL_destarg_assign(ctx, code, sizeof (code), "sign(%s)", src0);
1658 output_line(ctx, "%s", code);
1659 } // emit_GLSL_SGN
1660
1661 void emit_GLSL_ABS(Context *ctx)
1662 {
1663 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1664 char code[128];
1665 make_GLSL_destarg_assign(ctx, code, sizeof (code), "abs(%s)", src0);
1666 output_line(ctx, "%s", code);
1667 } // emit_GLSL_ABS
1668
1669 void emit_GLSL_NRM(Context *ctx)
1670 {
1671 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1672 char code[128];
1673 make_GLSL_destarg_assign(ctx, code, sizeof (code), "normalize(%s)", src0);
1674 output_line(ctx, "%s", code);
1675 } // emit_GLSL_NRM
1676
1677 void emit_GLSL_SINCOS(Context *ctx)
1678 {
1679 // we don't care about the temp registers that <= sm2 demands; ignore them.
1680 // sm2 also talks about what components are left untouched vs. undefined,
1681 // but we just leave those all untouched with GLSL write masks (which
1682 // would fulfill the "undefined" requirement, too).
1683 const int mask = ctx->dest_arg.writemask;
1684 char src0[64]; make_GLSL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0));
1685 char code[128] = { '\0' };
1686
1687 if (writemask_x(mask))
1688 make_GLSL_destarg_assign(ctx, code, sizeof (code), "cos(%s)", src0);
1689 else if (writemask_y(mask))
1690 make_GLSL_destarg_assign(ctx, code, sizeof (code), "sin(%s)", src0);
1691 else if (writemask_xy(mask))
1692 {
1693 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1694 "vec2(cos(%s), sin(%s))", src0, src0);
1695 } // else if
1696
1697 output_line(ctx, "%s", code);
1698 } // emit_GLSL_SINCOS
1699
1700 void emit_GLSL_REP(Context *ctx)
1701 {
1702 // !!! FIXME:
1703 // msdn docs say legal loop values are 0 to 255. We can check DEFI values
1704 // at parse time, but if they are pulling a value from a uniform, do
1705 // we clamp here?
1706 // !!! FIXME: swizzle is legal here, right?
1707 char src0[64]; make_GLSL_srcarg_string_x(ctx, 0, src0, sizeof (src0));
1708 const uint rep = (uint) ctx->reps;
1709 output_line(ctx, "for (int rep%u = 0; rep%u < %s; rep%u++) {",
1710 rep, rep, src0, rep);
1711 ctx->indent++;
1712 } // emit_GLSL_REP
1713
1714 void emit_GLSL_ENDREP(Context *ctx)
1715 {
1716 ctx->indent--;
1717 output_line(ctx, "}");
1718 } // emit_GLSL_ENDREP
1719
1720 void emit_GLSL_IF(Context *ctx)
1721 {
1722 char src0[64]; make_GLSL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0));
1723 output_line(ctx, "if (%s) {", src0);
1724 ctx->indent++;
1725 } // emit_GLSL_IF
1726
1727 void emit_GLSL_IFC(Context *ctx)
1728 {
1729 const char *comp = get_GLSL_comparison_string_scalar(ctx);
1730 char src0[64]; make_GLSL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0));
1731 char src1[64]; make_GLSL_srcarg_string_scalar(ctx, 1, src1, sizeof (src1));
1732 output_line(ctx, "if (%s %s %s) {", src0, comp, src1);
1733 ctx->indent++;
1734 } // emit_GLSL_IFC
1735
1736 void emit_GLSL_ELSE(Context *ctx)
1737 {
1738 ctx->indent--;
1739 output_line(ctx, "} else {");
1740 ctx->indent++;
1741 } // emit_GLSL_ELSE
1742
1743 void emit_GLSL_ENDIF(Context *ctx)
1744 {
1745 ctx->indent--;
1746 output_line(ctx, "}");
1747 } // emit_GLSL_ENDIF
1748
1749 void emit_GLSL_BREAK(Context *ctx)
1750 {
1751 output_line(ctx, "break;");
1752 } // emit_GLSL_BREAK
1753
1754 void emit_GLSL_BREAKC(Context *ctx)
1755 {
1756 const char *comp = get_GLSL_comparison_string_scalar(ctx);
1757 char src0[64]; make_GLSL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0));
1758 char src1[64]; make_GLSL_srcarg_string_scalar(ctx, 1, src1, sizeof (src1));
1759 output_line(ctx, "if (%s %s %s) { break; }", src0, comp, src1);
1760 } // emit_GLSL_BREAKC
1761
1762 void emit_GLSL_MOVA(Context *ctx)
1763 {
1764 const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
1765 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
1766 char code[128];
1767
1768 if (vecsize == 1)
1769 {
1770 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1771 "int(floor(abs(%s) + 0.5) * sign(%s))",
1772 src0, src0);
1773 } // if
1774
1775 else
1776 {
1777 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1778 "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s))",
1779 vecsize, src0, vecsize, src0);
1780 } // else
1781
1782 output_line(ctx, "%s", code);
1783 } // emit_GLSL_MOVA
1784
1785 void emit_GLSL_DEFB(Context *ctx)
1786 {
1787 char varname[64]; get_GLSL_destarg_varname(ctx, varname, sizeof (varname));
1788 push_output(ctx, &ctx->globals);
1789 output_line(ctx, "const bool %s = %s;",
1790 varname, ctx->dwords[0] ? "true" : "false");
1791 pop_output(ctx);
1792 } // emit_GLSL_DEFB
1793
1794 void emit_GLSL_DEFI(Context *ctx)
1795 {
1796 char varname[64]; get_GLSL_destarg_varname(ctx, varname, sizeof (varname));
1797 const int32 *x = (const int32 *) ctx->dwords;
1798 push_output(ctx, &ctx->globals);
1799 output_line(ctx, "const ivec4 %s = ivec4(%d, %d, %d, %d);",
1800 varname, (int) x[0], (int) x[1], (int) x[2], (int) x[3]);
1801 pop_output(ctx);
1802 } // emit_GLSL_DEFI
1803
1804 EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXCRD)
1805
1806 void emit_GLSL_TEXKILL(Context *ctx)
1807 {
1808 char dst[64]; get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
1809 output_line(ctx, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;", dst);
1810 } // emit_GLSL_TEXKILL
1811
1812 void emit_GLSL_TEXLD(Context *ctx)
1813 {
1814 if (!shader_version_atleast(ctx, 1, 4))
1815 {
1816 DestArgInfo *info = &ctx->dest_arg;
1817 char dst[64];
1818 char sampler[64];
1819 char code[128] = {0};
1820
1821 RegisterList *sreg;
1822 sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER, info->regnum);
1823 const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
1824
1825 // !!! FIXME: this code counts on the register not having swizzles, etc.
1826 get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
1827 get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
1828 sampler, sizeof (sampler));
1829
1830 if (ttype == TEXTURE_TYPE_2D)
1831 {
1832 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1833 "texture2D(%s, %s.xy)",
1834 sampler, dst);
1835 }
1836 else if (ttype == TEXTURE_TYPE_CUBE)
1837 {
1838 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1839 "textureCube(%s, %s.xyz)",
1840 sampler, dst);
1841 }
1842 else if (ttype == TEXTURE_TYPE_VOLUME)
1843 {
1844 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1845 "texture3D(%s, %s.xyz)",
1846 sampler, dst);
1847 }
1848 else
1849 {
1850 fail(ctx, "unexpected texture type");
1851 } // else
1852 output_line(ctx, "%s", code);
1853 } // if
1854
1855 else if (!shader_version_atleast(ctx, 2, 0))
1856 {
1857 // ps_1_4 is different, too!
1858 fail(ctx, "TEXLD == Shader Model 1.4 unimplemented."); // !!! FIXME
1859 return;
1860 } // else if
1861
1862 else
1863 {
1864 const SourceArgInfo *samp_arg = &ctx->source_args[1];
1865 RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER,
1866 samp_arg->regnum);
1867 const char *funcname = NULL;
1868 char src0[64] = { '\0' };
1869 char src1[64]; get_GLSL_srcarg_varname(ctx, 1, src1, sizeof (src1)); // !!! FIXME: SRC_MOD?
1870
1871 if (sreg == NULL)
1872 {
1873 fail(ctx, "TEXLD using undeclared sampler");
1874 return;
1875 } // if
1876
1877 // !!! FIXME: does the d3d bias value map directly to GLSL?
1878 const char *biassep = "";
1879 char bias[64] = { '\0' };
1880 if (ctx->instruction_controls == CONTROL_TEXLDB)
1881 {
1882 biassep = ", ";
1883 make_GLSL_srcarg_string_w(ctx, 0, bias, sizeof (bias));
1884 } // if
1885
1886 switch ((const TextureType) sreg->index)
1887 {
1888 case TEXTURE_TYPE_2D:
1889 if (ctx->instruction_controls == CONTROL_TEXLDP)
1890 {
1891 funcname = "texture2DProj";
1892 make_GLSL_srcarg_string_full(ctx, 0, src0, sizeof (src0));
1893 } // if
1894 else // texld/texldb
1895 {
1896 funcname = "texture2D";
1897 make_GLSL_srcarg_string_vec2(ctx, 0, src0, sizeof (src0));
1898 } // else
1899 break;
1900 case TEXTURE_TYPE_CUBE:
1901 if (ctx->instruction_controls == CONTROL_TEXLDP)
1902 fail(ctx, "TEXLDP on a cubemap"); // !!! FIXME: is this legal?
1903 funcname = "textureCube";
1904 make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
1905 break;
1906 case TEXTURE_TYPE_VOLUME:
1907 if (ctx->instruction_controls == CONTROL_TEXLDP)
1908 {
1909 funcname = "texture3DProj";
1910 make_GLSL_srcarg_string_full(ctx, 0, src0, sizeof (src0));
1911 } // if
1912 else // texld/texldb
1913 {
1914 funcname = "texture3D";
1915 make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
1916 } // else
1917 break;
1918 default:
1919 fail(ctx, "unknown texture type");
1920 return;
1921 } // switch
1922
1923 assert(!isscalar(ctx, ctx->shader_type, samp_arg->regtype, samp_arg->regnum));
1924 char swiz_str[6] = { '\0' };
1925 make_GLSL_swizzle_string(swiz_str, sizeof (swiz_str),
1926 samp_arg->swizzle, ctx->dest_arg.writemask);
1927
1928 char code[128];
1929 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1930 "%s(%s, %s%s%s)%s", funcname,
1931 src1, src0, biassep, bias, swiz_str);
1932
1933 output_line(ctx, "%s", code);
1934 } // else
1935 } // emit_GLSL_TEXLD
1936
1937
1938 void emit_GLSL_TEXBEM(Context *ctx)
1939 {
1940 DestArgInfo *info = &ctx->dest_arg;
1941 char dst[64]; get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
1942 char src[64]; get_GLSL_srcarg_varname(ctx, 0, src, sizeof (src));
1943 char sampler[64];
1944 char code[512];
1945
1946 // !!! FIXME: this code counts on the register not having swizzles, etc.
1947 get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
1948 sampler, sizeof (sampler));
1949
1950 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1951 "texture2D(%s, vec2(%s.x + (%s_texbem.x * %s.x) + (%s_texbem.z * %s.y),"
1952 " %s.y + (%s_texbem.y * %s.x) + (%s_texbem.w * %s.y)))",
1953 sampler,
1954 dst, sampler, src, sampler, src,
1955 dst, sampler, src, sampler, src);
1956
1957 output_line(ctx, "%s", code);
1958 } // emit_GLSL_TEXBEM
1959
1960
1961 void emit_GLSL_TEXBEML(Context *ctx)
1962 {
1963 // !!! FIXME: this code counts on the register not having swizzles, etc.
1964 DestArgInfo *info = &ctx->dest_arg;
1965 char dst[64]; get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
1966 char src[64]; get_GLSL_srcarg_varname(ctx, 0, src, sizeof (src));
1967 char sampler[64];
1968 char code[512];
1969
1970 get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
1971 sampler, sizeof (sampler));
1972
1973 make_GLSL_destarg_assign(ctx, code, sizeof (code),
1974 "(texture2D(%s, vec2(%s.x + (%s_texbem.x * %s.x) + (%s_texbem.z * %s.y),"
1975 " %s.y + (%s_texbem.y * %s.x) + (%s_texbem.w * %s.y)))) *"
1976 " ((%s.z * %s_texbeml.x) + %s_texbem.y)",
1977 sampler,
1978 dst, sampler, src, sampler, src,
1979 dst, sampler, src, sampler, src,
1980 src, sampler, sampler);
1981
1982 output_line(ctx, "%s", code);
1983 } // emit_GLSL_TEXBEML
1984
1985 EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2AR) // !!! FIXME
1986 EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2GB) // !!! FIXME
1987
1988
1989 void emit_GLSL_TEXM3X2PAD(Context *ctx)
1990 {
1991 // no-op ... work happens in emit_GLSL_TEXM3X2TEX().
1992 } // emit_GLSL_TEXM3X2PAD
1993
1994 void emit_GLSL_TEXM3X2TEX(Context *ctx)
1995 {
1996 if (ctx->texm3x2pad_src0 == -1)
1997 return;
1998
1999 DestArgInfo *info = &ctx->dest_arg;
2000 char dst[64];
2001 char src0[64];
2002 char src1[64];
2003 char src2[64];
2004 char sampler[64];
2005 char code[512];
2006
2007 // !!! FIXME: this code counts on the register not having swizzles, etc.
2008 get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
2009 sampler, sizeof (sampler));
2010 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x2pad_src0,
2011 src0, sizeof (src0));
2012 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x2pad_dst0,
2013 src1, sizeof (src1));
2014 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
2015 src2, sizeof (src2));
2016 get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
2017
2018 make_GLSL_destarg_assign(ctx, code, sizeof (code),
2019 "texture2D(%s, vec2(dot(%s.xyz, %s.xyz), dot(%s.xyz, %s.xyz)))",
2020 sampler, src0, src1, src2, dst);
2021
2022 output_line(ctx, "%s", code);
2023 } // emit_GLSL_TEXM3X2TEX
2024
2025 void emit_GLSL_TEXM3X3PAD(Context *ctx)
2026 {
2027 // no-op ... work happens in emit_GLSL_TEXM3X3*().
2028 } // emit_GLSL_TEXM3X3PAD
2029
2030 void emit_GLSL_TEXM3X3TEX(Context *ctx)
2031 {
2032 if (ctx->texm3x3pad_src1 == -1)
2033 return;
2034
2035 DestArgInfo *info = &ctx->dest_arg;
2036 char dst[64];
2037 char src0[64];
2038 char src1[64];
2039 char src2[64];
2040 char src3[64];
2041 char src4[64];
2042 char sampler[64];
2043 char code[512];
2044
2045 // !!! FIXME: this code counts on the register not having swizzles, etc.
2046 get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
2047 sampler, sizeof (sampler));
2048
2049 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
2050 src0, sizeof (src0));
2051 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
2052 src1, sizeof (src1));
2053 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
2054 src2, sizeof (src2));
2055 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
2056 src3, sizeof (src3));
2057 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
2058 src4, sizeof (src4));
2059 get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
2060
2061 RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER,
2062 info->regnum);
2063 const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
2064 const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "Cube" : "3D";
2065
2066 make_GLSL_destarg_assign(ctx, code, sizeof (code),
2067 "texture%s(%s,"
2068 " vec3(dot(%s.xyz, %s.xyz),"
2069 " dot(%s.xyz, %s.xyz),"
2070 " dot(%s.xyz, %s.xyz)))",
2071 ttypestr, sampler, src0, src1, src2, src3, dst, src4);
2072
2073 output_line(ctx, "%s", code);
2074 } // emit_GLSL_TEXM3X3TEX
2075
2076 void emit_GLSL_TEXM3X3SPEC_helper(Context *ctx)
2077 {
2078 if (ctx->glsl_generated_texm3x3spec_helper)
2079 return;
2080
2081 ctx->glsl_generated_texm3x3spec_helper = 1;
2082
2083 push_output(ctx, &ctx->helpers);
2084 output_line(ctx, "vec3 TEXM3X3SPEC_reflection(const vec3 normal, const vec3 eyeray)");
2085 output_line(ctx, "{"); ctx->indent++;
2086 output_line(ctx, "return (2.0 * ((normal * eyeray) / (normal * normal)) * normal) - eyeray;"); ctx->indent--;
2087 output_line(ctx, "}");
2088 output_blank_line(ctx);
2089 pop_output(ctx);
2090 } // emit_GLSL_TEXM3X3SPEC_helper
2091
2092 void emit_GLSL_TEXM3X3SPEC(Context *ctx)
2093 {
2094 if (ctx->texm3x3pad_src1 == -1)
2095 return;
2096
2097 DestArgInfo *info = &ctx->dest_arg;
2098 char dst[64];
2099 char src0[64];
2100 char src1[64];
2101 char src2[64];
2102 char src3[64];
2103 char src4[64];
2104 char src5[64];
2105 char sampler[64];
2106 char code[512];
2107
2108 emit_GLSL_TEXM3X3SPEC_helper(ctx);
2109
2110 // !!! FIXME: this code counts on the register not having swizzles, etc.
2111 get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
2112 sampler, sizeof (sampler));
2113
2114 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
2115 src0, sizeof (src0));
2116 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
2117 src1, sizeof (src1));
2118 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
2119 src2, sizeof (src2));
2120 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
2121 src3, sizeof (src3));
2122 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
2123 src4, sizeof (src4));
2124 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[1].regnum,
2125 src5, sizeof (src5));
2126 get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
2127
2128 RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER,
2129 info->regnum);
2130 const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
2131 const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "Cube" : "3D";
2132
2133 make_GLSL_destarg_assign(ctx, code, sizeof (code),
2134 "texture%s(%s, "
2135 "TEXM3X3SPEC_reflection("
2136 "vec3("
2137 "dot(%s.xyz, %s.xyz), "
2138 "dot(%s.xyz, %s.xyz), "
2139 "dot(%s.xyz, %s.xyz)"
2140 "),"
2141 "%s.xyz,"
2142 ")"
2143 ")",
2144 ttypestr, sampler, src0, src1, src2, src3, dst, src4, src5);
2145
2146 output_line(ctx, "%s", code);
2147 } // emit_GLSL_TEXM3X3SPEC
2148
2149 void emit_GLSL_TEXM3X3VSPEC(Context *ctx)
2150 {
2151 if (ctx->texm3x3pad_src1 == -1)
2152 return;
2153
2154 DestArgInfo *info = &ctx->dest_arg;
2155 char dst[64];
2156 char src0[64];
2157 char src1[64];
2158 char src2[64];
2159 char src3[64];
2160 char src4[64];
2161 char sampler[64];
2162 char code[512];
2163
2164 emit_GLSL_TEXM3X3SPEC_helper(ctx);
2165
2166 // !!! FIXME: this code counts on the register not having swizzles, etc.
2167 get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, info->regnum,
2168 sampler, sizeof (sampler));
2169
2170 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
2171 src0, sizeof (src0));
2172 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
2173 src1, sizeof (src1));
2174 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
2175 src2, sizeof (src2));
2176 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
2177 src3, sizeof (src3));
2178 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
2179 src4, sizeof (src4));
2180 get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
2181
2182 RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER,
2183 info->regnum);
2184 const TextureType ttype = (TextureType) (sreg ? sreg->index : 0);
2185 const char *ttypestr = (ttype == TEXTURE_TYPE_CUBE) ? "Cube" : "3D";
2186
2187 make_GLSL_destarg_assign(ctx, code, sizeof (code),
2188 "texture%s(%s, "
2189 "TEXM3X3SPEC_reflection("
2190 "vec3("
2191 "dot(%s.xyz, %s.xyz), "
2192 "dot(%s.xyz, %s.xyz), "
2193 "dot(%s.xyz, %s.xyz)"
2194 "), "
2195 "vec3(%s.w, %s.w, %s.w)"
2196 ")"
2197 ")",
2198 ttypestr, sampler, src0, src1, src2, src3, dst, src4, src0, src2, dst);
2199
2200 output_line(ctx, "%s", code);
2201 } // emit_GLSL_TEXM3X3VSPEC
2202
2203 void emit_GLSL_EXPP(Context *ctx)
2204 {
2205 // !!! FIXME: msdn's asm docs don't list this opcode, I'll have to check the driver documentation.
2206 emit_GLSL_EXP(ctx); // I guess this is just partial precision EXP?
2207 } // emit_GLSL_EXPP
2208
2209 void emit_GLSL_LOGP(Context *ctx)
2210 {
2211 // LOGP is just low-precision LOG, but we'll take the higher precision.
2212 emit_GLSL_LOG(ctx);
2213 } // emit_GLSL_LOGP
2214
2215 // common code between CMP and CND.
2216 void emit_GLSL_comparison_operations(Context *ctx, const char *cmp)
2217 {
2218 int i, j;
2219 DestArgInfo *dst = &ctx->dest_arg;
2220 const SourceArgInfo *srcarg0 = &ctx->source_args[0];
2221 const int origmask = dst->writemask;
2222 int used_swiz[4] = { 0, 0, 0, 0 };
2223 const int writemask[4] = { dst->writemask0, dst->writemask1,
2224 dst->writemask2, dst->writemask3 };
2225 const int src0swiz[4] = { srcarg0->swizzle_x, srcarg0->swizzle_y,
2226 srcarg0->swizzle_z, srcarg0->swizzle_w };
2227
2228 for (i = 0; i < 4; i++)
2229 {
2230 int mask = (1 << i);
2231
2232 if (!writemask[i]) continue;
2233 if (used_swiz[i]) continue;
2234
2235 // This is a swizzle we haven't checked yet.
2236 used_swiz[i] = 1;
2237
2238 // see if there are any other elements swizzled to match (.yyyy)
2239 for (j = i + 1; j < 4; j++)
2240 {
2241 if (!writemask[j]) continue;
2242 if (src0swiz[i] != src0swiz[j]) continue;
2243 mask |= (1 << j);
2244 used_swiz[j] = 1;
2245 } // for
2246
2247 // okay, (mask) should be the writemask of swizzles we like.
2248
2249 //return make_GLSL_srcarg_string(ctx, idx, (1 << 0));
2250
2251 char src0[64];
2252 char src1[64];
2253 char src2[64];
2254 make_GLSL_srcarg_string(ctx, 0, (1 << i), src0, sizeof (src0));
2255 make_GLSL_srcarg_string(ctx, 1, mask, src1, sizeof (src1));
2256 make_GLSL_srcarg_string(ctx, 2, mask, src2, sizeof (src2));
2257
2258 set_dstarg_writemask(dst, mask);
2259
2260 char code[128];
2261 make_GLSL_destarg_assign(ctx, code, sizeof (code),
2262 "((%s %s) ? %s : %s)",
2263 src0, cmp, src1, src2);
2264 output_line(ctx, "%s", code);
2265 } // for
2266
2267 set_dstarg_writemask(dst, origmask);
2268 } // emit_GLSL_comparison_operations
2269
2270 void emit_GLSL_CND(Context *ctx)
2271 {
2272 emit_GLSL_comparison_operations(ctx, "> 0.5");
2273 } // emit_GLSL_CND
2274
2275 void emit_GLSL_DEF(Context *ctx)
2276 {
2277 const float *val = (const float *) ctx->dwords; // !!! FIXME: could be int?
2278 char varname[64]; get_GLSL_destarg_varname(ctx, varname, sizeof (varname));
2279 char val0[32]; floatstr(ctx, val0, sizeof (val0), val[0], 1);
2280 char val1[32]; floatstr(ctx, val1, sizeof (val1), val[1], 1);
2281 char val2[32]; floatstr(ctx, val2, sizeof (val2), val[2], 1);
2282 char val3[32]; floatstr(ctx, val3, sizeof (val3), val[3], 1);
2283
2284 push_output(ctx, &ctx->globals);
2285 output_line(ctx, "const vec4 %s = vec4(%s, %s, %s, %s);",
2286 varname, val0, val1, val2, val3);
2287 pop_output(ctx);
2288 } // emit_GLSL_DEF
2289
2290 EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXREG2RGB) // !!! FIXME
2291 EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXDP3TEX) // !!! FIXME
2292 EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXM3X2DEPTH) // !!! FIXME
2293 EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXDP3) // !!! FIXME
2294
2295 void emit_GLSL_TEXM3X3(Context *ctx)
2296 {
2297 if (ctx->texm3x3pad_src1 == -1)
2298 return;
2299
2300 char dst[64];
2301 char src0[64];
2302 char src1[64];
2303 char src2[64];
2304 char src3[64];
2305 char src4[64];
2306 char code[512];
2307
2308 // !!! FIXME: this code counts on the register not having swizzles, etc.
2309 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst0,
2310 src0, sizeof (src0));
2311 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src0,
2312 src1, sizeof (src1));
2313 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_dst1,
2314 src2, sizeof (src2));
2315 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->texm3x3pad_src1,
2316 src3, sizeof (src3));
2317 get_GLSL_varname_in_buf(ctx, REG_TYPE_TEXTURE, ctx->source_args[0].regnum,
2318 src4, sizeof (src4));
2319 get_GLSL_destarg_varname(ctx, dst, sizeof (dst));
2320
2321 make_GLSL_destarg_assign(ctx, code, sizeof (code),
2322 "vec4(dot(%s.xyz, %s.xyz), dot(%s.xyz, %s.xyz), dot(%s.xyz, %s.xyz), 1.0)",
2323 src0, src1, src2, src3, dst, src4);
2324
2325 output_line(ctx, "%s", code);
2326 } // emit_GLSL_TEXM3X3
2327
2328 EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(TEXDEPTH) // !!! FIXME
2329
2330 void emit_GLSL_CMP(Context *ctx)
2331 {
2332 emit_GLSL_comparison_operations(ctx, ">= 0.0");
2333 } // emit_GLSL_CMP
2334
2335 EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(BEM) // !!! FIXME
2336
2337 void emit_GLSL_DP2ADD(Context *ctx)
2338 {
2339 char src0[64]; make_GLSL_srcarg_string_vec2(ctx, 0, src0, sizeof (src0));
2340 char src1[64]; make_GLSL_srcarg_string_vec2(ctx, 1, src1, sizeof (src1));
2341 char src2[64]; make_GLSL_srcarg_string_scalar(ctx, 2, src2, sizeof (src2));
2342 char extra[128]; snprintf(extra, sizeof (extra), " + %s", src2);
2343 emit_GLSL_dotprod(ctx, src0, src1, extra);
2344 } // emit_GLSL_DP2ADD
2345
2346 void emit_GLSL_DSX(Context *ctx)
2347 {
2348 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
2349 char code[128];
2350 make_GLSL_destarg_assign(ctx, code, sizeof (code), "dFdx(%s)", src0);
2351 output_line(ctx, "%s", code);
2352 } // emit_GLSL_DSX
2353
2354 void emit_GLSL_DSY(Context *ctx)
2355 {
2356 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
2357 char code[128];
2358 make_GLSL_destarg_assign(ctx, code, sizeof (code), "dFdy(%s)", src0);
2359 output_line(ctx, "%s", code);
2360 } // emit_GLSL_DSY
2361
2362 void emit_GLSL_TEXLDD(Context *ctx)
2363 {
2364 const SourceArgInfo *samp_arg = &ctx->source_args[1];
2365 RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER,
2366 samp_arg->regnum);
2367 const char *funcname = NULL;
2368 char src0[64] = { '\0' };
2369 char src1[64]; get_GLSL_srcarg_varname(ctx, 1, src1, sizeof (src1)); // !!! FIXME: SRC_MOD?
2370 char src2[64] = { '\0' };
2371 char src3[64] = { '\0' };
2372
2373 if (sreg == NULL)
2374 {
2375 fail(ctx, "TEXLDD using undeclared sampler");
2376 return;
2377 } // if
2378
2379 switch ((const TextureType) sreg->index)
2380 {
2381 case TEXTURE_TYPE_2D:
2382 funcname = "texture2D";
2383 make_GLSL_srcarg_string_vec2(ctx, 0, src0, sizeof (src0));
2384 make_GLSL_srcarg_string_vec2(ctx, 2, src2, sizeof (src2));
2385 make_GLSL_srcarg_string_vec2(ctx, 3, src3, sizeof (src3));
2386 break;
2387 case TEXTURE_TYPE_CUBE:
2388 funcname = "textureCube";
2389 make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
2390 make_GLSL_srcarg_string_vec3(ctx, 2, src2, sizeof (src2));
2391 make_GLSL_srcarg_string_vec3(ctx, 3, src3, sizeof (src3));
2392 break;
2393 case TEXTURE_TYPE_VOLUME:
2394 funcname = "texture3D";
2395 make_GLSL_srcarg_string_vec3(ctx, 0, src0, sizeof (src0));
2396 make_GLSL_srcarg_string_vec3(ctx, 2, src2, sizeof (src2));
2397 make_GLSL_srcarg_string_vec3(ctx, 3, src3, sizeof (src3));
2398 break;
2399 default:
2400 fail(ctx, "unknown texture type");
2401 return;
2402 } // switch
2403
2404 assert(!isscalar(ctx, ctx->shader_type, samp_arg->regtype, samp_arg->regnum));
2405 char swiz_str[6] = { '\0' };
2406 make_GLSL_swizzle_string(swiz_str, sizeof (swiz_str),
2407 samp_arg->swizzle, ctx->dest_arg.writemask);
2408
2409 char code[128];
2410 make_GLSL_destarg_assign(ctx, code, sizeof (code),
2411 "%sGrad(%s, %s, %s, %s)%s", funcname,
2412 src1, src0, src2, src3, swiz_str);
2413
2414 prepend_glsl_texlod_extensions(ctx);
2415 output_line(ctx, "%s", code);
2416 } // emit_GLSL_TEXLDD
2417
2418 void emit_GLSL_SETP(Context *ctx)
2419 {
2420 const int vecsize = vecsize_from_writemask(ctx->dest_arg.writemask);
2421 char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0));
2422 char src1[64]; make_GLSL_srcarg_string_masked(ctx, 1, src1, sizeof (src1));
2423 char code[128];
2424
2425 // destination is always predicate register (which is type bvec4).
2426 if (vecsize == 1)
2427 {
2428 const char *comp = get_GLSL_comparison_string_scalar(ctx);
2429 make_GLSL_destarg_assign(ctx, code, sizeof (code),
2430 "(%s %s %s)", src0, comp, src1);
2431 } // if
2432 else
2433 {
2434 const char *comp = get_GLSL_comparison_string_vector(ctx);
2435 make_GLSL_destarg_assign(ctx, code, sizeof (code),
2436 "%s(%s, %s)", comp, src0, src1);
2437 } // else
2438
2439 output_line(ctx, "%s", code);
2440 } // emit_GLSL_SETP
2441
2442 void emit_GLSL_TEXLDL(Context *ctx)
2443 {
2444 const SourceArgInfo *samp_arg = &ctx->source_args[1];
2445 RegisterList *sreg = reglist_find(&ctx->samplers, REG_TYPE_SAMPLER,
2446 samp_arg->regnum);
2447 const char *pattern = NULL;
2448 char src0[64];
2449 char src1[64];
2450 make_GLSL_srcarg_string_full(ctx, 0, src0, sizeof (src0));
2451 get_GLSL_srcarg_varname(ctx, 1, src1, sizeof (src1)); // !!! FIXME: SRC_MOD?
2452
2453 if (sreg == NULL)
2454 {
2455 fail(ctx, "TEXLDL using undeclared sampler");
2456 return;
2457 } // if
2458
2459 // HLSL tex2dlod accepts (sampler, uv.xyz, uv.w) where uv.w is the LOD
2460 // GLSL seems to want the dimensionality to match the sampler (.xy vs .xyz)
2461 // so we vary the swizzle accordingly
2462 switch ((const TextureType) sreg->index)
2463 {
2464 case TEXTURE_TYPE_2D:
2465 pattern = "texture2DLod(%s, %s.xy, %s.w)%s";
2466 break;
2467 case TEXTURE_TYPE_CUBE:
2468 pattern = "textureCubeLod(%s, %s.xyz, %s.w)%s";
2469 break;
2470 case TEXTURE_TYPE_VOLUME:
2471 pattern = "texture3DLod(%s, %s.xyz, %s.w)%s";
2472 break;
2473 default:
2474 fail(ctx, "unknown texture type");
2475 return;
2476 } // switch
2477
2478 assert(!isscalar(ctx, ctx->shader_type, samp_arg->regtype, samp_arg->regnum));
2479 char swiz_str[6] = { '\0' };
2480 make_GLSL_swizzle_string(swiz_str, sizeof (swiz_str),
2481 samp_arg->swizzle, ctx->dest_arg.writemask);
2482
2483 char code[128];
2484 make_GLSL_destarg_assign(ctx, code, sizeof(code),
2485 pattern, src1, src0, src0, swiz_str);
2486
2487 prepend_glsl_texlod_extensions(ctx);
2488 output_line(ctx, "%s", code);
2489 } // emit_GLSL_TEXLDL
2490
2491 void emit_GLSL_BREAKP(Context *ctx)
2492 {
2493 char src0[64]; make_GLSL_srcarg_string_scalar(ctx, 0, src0, sizeof (src0));
2494 output_line(ctx, "if (%s) { break; }", src0);
2495 } // emit_GLSL_BREAKP
2496
2497 void emit_GLSL_RESERVED(Context *ctx)
2498 {
2499 // do nothing; fails in the state machine.
2500 } // emit_GLSL_RESERVED
2501
2502 #endif // SUPPORT_PROFILE_GLSL
2503
2504 #pragma GCC visibility pop