Skip to content

Latest commit

 

History

History
2412 lines (2102 loc) · 84.2 KB

mojoshader_profile_glsl.c

File metadata and controls

2412 lines (2102 loc) · 84.2 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
/**
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_profile.h"
Apr 23, 2019
Apr 23, 2019
13
14
#pragma GCC visibility push(hidden)
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#if SUPPORT_PROFILE_GLSL
#define EMIT_GLSL_OPCODE_UNIMPLEMENTED_FUNC(op) \
void emit_GLSL_##op(Context *ctx) { \
fail(ctx, #op " unimplemented in glsl profile"); \
}
static inline const char *get_GLSL_register_string(Context *ctx,
const RegisterType regtype, const int regnum,
char *regnum_str, const size_t regnum_size)
{
// turns out these are identical at the moment.
return get_D3D_register_string(ctx,regtype,regnum,regnum_str,regnum_size);
} // get_GLSL_register_string
const char *get_GLSL_uniform_type(Context *ctx, const RegisterType rtype)
{
switch (rtype)
{
case REG_TYPE_CONST: return "vec4";
case REG_TYPE_CONSTINT: return "ivec4";
case REG_TYPE_CONSTBOOL: return "bool";
default: fail(ctx, "BUG: used a uniform we don't know how to define.");
} // switch
return NULL;
} // get_GLSL_uniform_type
const char *get_GLSL_varname_in_buf(Context *ctx, RegisterType rt,
int regnum, char *buf,
const size_t len)
{
char regnum_str[16];
const char *regtype_str = get_GLSL_register_string(ctx, rt, regnum,
regnum_str, sizeof (regnum_str));
snprintf(buf,len,"%s_%s%s", ctx->shader_type_str, regtype_str, regnum_str);
return buf;
} // get_GLSL_varname_in_buf
const char *get_GLSL_varname(Context *ctx, RegisterType rt, int regnum)
{
char buf[64];
get_GLSL_varname_in_buf(ctx, rt, regnum, buf, sizeof (buf));
return StrDup(ctx, buf);
} // get_GLSL_varname
static inline const char *get_GLSL_const_array_varname_in_buf(Context *ctx,
const int base, const int size,
char *buf, const size_t buflen)
{
const char *type = ctx->shader_type_str;
snprintf(buf, buflen, "%s_const_array_%d_%d", type, base, size);
return buf;
} // get_GLSL_const_array_varname_in_buf
const char *get_GLSL_const_array_varname(Context *ctx, int base, int size)
{
char buf[64];
get_GLSL_const_array_varname_in_buf(ctx, base, size, buf, sizeof (buf));
return StrDup(ctx, buf);
} // get_GLSL_const_array_varname
static inline const char *get_GLSL_input_array_varname(Context *ctx,
char *buf, const size_t buflen)
{
snprintf(buf, buflen, "%s", "vertex_input_array");
return buf;
} // get_GLSL_input_array_varname
const char *get_GLSL_uniform_array_varname(Context *ctx,
const RegisterType regtype,
char *buf, const size_t len)
{
const char *shadertype = ctx->shader_type_str;
const char *type = get_GLSL_uniform_type(ctx, regtype);
snprintf(buf, len, "%s_uniforms_%s", shadertype, type);
return buf;
} // get_GLSL_uniform_array_varname
const char *get_GLSL_destarg_varname(Context *ctx, char *buf, size_t len)
{
const DestArgInfo *arg = &ctx->dest_arg;
return get_GLSL_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, len);
} // get_GLSL_destarg_varname
const char *get_GLSL_srcarg_varname(Context *ctx, const size_t idx,
char *buf, size_t len)
{
if (idx >= STATICARRAYLEN(ctx->source_args))
{
fail(ctx, "Too many source args");
*buf = '\0';
return buf;
} // if
const SourceArgInfo *arg = &ctx->source_args[idx];
return get_GLSL_varname_in_buf(ctx, arg->regtype, arg->regnum, buf, len);
} // get_GLSL_srcarg_varname
const char *make_GLSL_destarg_assign(Context *, char *, const size_t,
const char *, ...) ISPRINTF(4,5);
const char *make_GLSL_destarg_assign(Context *ctx, char *buf,
const size_t buflen,
const char *fmt, ...)
{
int need_parens = 0;
const DestArgInfo *arg = &ctx->dest_arg;
if (arg->writemask == 0)
{
*buf = '\0';
return buf; // no writemask? It's a no-op.
} // if
char clampbuf[32] = { '\0' };
const char *clampleft = "";
const char *clampright = "";
if (arg->result_mod & MOD_SATURATE)
{
const int vecsize = vecsize_from_writemask(arg->writemask);
clampleft = "clamp(";
if (vecsize == 1)
clampright = ", 0.0, 1.0)";
else
{
snprintf(clampbuf, sizeof (clampbuf),
", vec%d(0.0), vec%d(1.0))", vecsize, vecsize);
clampright = clampbuf;
} // else
} // if
// MSDN says MOD_PP is a hint and many implementations ignore it. So do we.
// CENTROID only allowed in DCL opcodes, which shouldn't come through here.
assert((arg->result_mod & MOD_CENTROID) == 0);
if (ctx->predicated)
{
fail(ctx, "predicated destinations unsupported"); // !!! FIXME
*buf = '\0';
return buf;
} // if
char operation[256];
va_list ap;
va_start(ap, fmt);
const int len = vsnprintf(operation, sizeof (operation), fmt, ap);
va_end(ap);
if (len >= sizeof (operation))
{
fail(ctx, "operation string too large"); // I'm lazy. :P
*buf = '\0';
return buf;
} // if
const char *result_shift_str = "";
switch (arg->result_shift)
{
case 0x1: result_shift_str = " * 2.0"; break;
case 0x2: result_shift_str = " * 4.0"; break;
case 0x3: result_shift_str = " * 8.0"; break;
case 0xD: result_shift_str = " / 8.0"; break;
case 0xE: result_shift_str = " / 4.0"; break;
case 0xF: result_shift_str = " / 2.0"; break;
} // switch
need_parens |= (result_shift_str[0] != '\0');
char regnum_str[16];
const char *regtype_str = get_GLSL_register_string(ctx, arg->regtype,
arg->regnum, regnum_str,
sizeof (regnum_str));
char writemask_str[6];
size_t i = 0;
const int scalar = isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum);
if (!scalar && !writemask_xyzw(arg->writemask))
{
writemask_str[i++] = '.';
if (arg->writemask0) writemask_str[i++] = 'x';
if (arg->writemask1) writemask_str[i++] = 'y';
if (arg->writemask2) writemask_str[i++] = 'z';
if (arg->writemask3) writemask_str[i++] = 'w';
} // if
writemask_str[i] = '\0';
assert(i < sizeof (writemask_str));
const char *leftparen = (need_parens) ? "(" : "";
const char *rightparen = (need_parens) ? ")" : "";
snprintf(buf, buflen, "%s_%s%s%s = %s%s%s%s%s%s;",
ctx->shader_type_str, regtype_str, regnum_str, writemask_str,
clampleft, leftparen, operation, rightparen, result_shift_str,
clampright);
// !!! FIXME: make sure the scratch buffer was large enough.
return buf;
} // make_GLSL_destarg_assign
char *make_GLSL_swizzle_string(char *swiz_str, const size_t strsize,
const int swizzle, const int writemask)
{
size_t i = 0;
if ( (!no_swizzle(swizzle)) || (!writemask_xyzw(writemask)) )
{
const int writemask0 = (writemask >> 0) & 0x1;
const int writemask1 = (writemask >> 1) & 0x1;
const int writemask2 = (writemask >> 2) & 0x1;
const int writemask3 = (writemask >> 3) & 0x1;
const int swizzle_x = (swizzle >> 0) & 0x3;
const int swizzle_y = (swizzle >> 2) & 0x3;
const int swizzle_z = (swizzle >> 4) & 0x3;
const int swizzle_w = (swizzle >> 6) & 0x3;
swiz_str[i++] = '.';
if (writemask0) swiz_str[i++] = swizzle_channels[swizzle_x];
if (writemask1) swiz_str[i++] = swizzle_channels[swizzle_y];
if (writemask2) swiz_str[i++] = swizzle_channels[swizzle_z];
if (writemask3) swiz_str[i++] = swizzle_channels[swizzle_w];
} // if
assert(i < strsize);
swiz_str[i] = '\0';
return swiz_str;
} // make_GLSL_swizzle_string
const char *make_GLSL_srcarg_string(Context *ctx, const size_t idx,
const int writemask, char *buf,
const size_t buflen)
{
*buf = '\0';
if (idx >= STATICARRAYLEN(ctx->source_args))
{
fail(ctx, "Too many source args");
return buf;
} // if
const SourceArgInfo *arg = &ctx->source_args[idx];
const char *premod_str = "";
const char *postmod_str = "";
switch (arg->src_mod)
{
case SRCMOD_NEGATE:
premod_str = "-";
break;
case SRCMOD_BIASNEGATE:
premod_str = "-(";
postmod_str = " - 0.5)";
break;
case SRCMOD_BIAS:
premod_str = "(";
postmod_str = " - 0.5)";
break;
case SRCMOD_SIGNNEGATE:
premod_str = "-((";
postmod_str = " - 0.5) * 2.0)";
break;
case SRCMOD_SIGN:
premod_str = "((";
postmod_str = " - 0.5) * 2.0)";
break;
case SRCMOD_COMPLEMENT:
premod_str = "(1.0 - ";
postmod_str = ")";
break;
case SRCMOD_X2NEGATE:
premod_str = "-(";
postmod_str = " * 2.0)";
break;
case SRCMOD_X2:
premod_str = "(";
postmod_str = " * 2.0)";
break;
case SRCMOD_DZ:
fail(ctx, "SRCMOD_DZ unsupported"); return buf; // !!! FIXME
postmod_str = "_dz";
break;
case SRCMOD_DW:
fail(ctx, "SRCMOD_DW unsupported"); return buf; // !!! FIXME
postmod_str = "_dw";
break;
case SRCMOD_ABSNEGATE:
premod_str = "-abs(";
postmod_str = ")";
break;
case SRCMOD_ABS:
premod_str = "abs(";
postmod_str = ")";
break;
case SRCMOD_NOT:
premod_str = "!";
break;
case SRCMOD_NONE:
case SRCMOD_TOTAL:
break; // stop compiler whining.
} // switch
const char *regtype_str = NULL;
if (!arg->relative)
{
regtype_str = get_GLSL_varname_in_buf(ctx, arg->regtype, arg->regnum,
(char *) alloca(64), 64);
} // if
const char *rel_lbracket = "";
char rel_offset[32] = { '\0' };
const char *rel_rbracket = "";
char rel_swizzle[4] = { '\0' };
const char *rel_regtype_str = "";
if (arg->relative)
{
if (arg->regtype == REG_TYPE_INPUT)
regtype_str=get_GLSL_input_array_varname(ctx,(char*)alloca(64),64);
else
{
assert(arg->regtype == REG_TYPE_CONST);
const int arrayidx = arg->relative_array->index;
const int offset = arg->regnum - arrayidx;
assert(offset >= 0);
if (arg->relative_array->constant)
{
const int arraysize = arg->relative_array->count;
regtype_str = get_GLSL_const_array_varname_in_buf(ctx,
arrayidx, arraysize, (char *) alloca(64), 64);
if (offset != 0)
snprintf(rel_offset, sizeof (rel_offset), "%d + ", offset);
} // if
else
{
regtype_str = get_GLSL_uniform_array_varname(ctx, arg->regtype,
(char *) alloca(64), 64);
if (offset == 0)
{
snprintf(rel_offset, sizeof (rel_offset),
"ARRAYBASE_%d + ", arrayidx);
} // if
else
{
snprintf(rel_offset, sizeof (rel_offset),
"(ARRAYBASE_%d + %d) + ", arrayidx, offset);
} // else
} // else
} // else
rel_lbracket = "[";
if (arg->relative_regtype == REG_TYPE_LOOP)
{
rel_regtype_str = "aL";
rel_swizzle[0] = '\0';
rel_swizzle[1] = '\0';
rel_swizzle[2] = '\0';
} // if
else
{
rel_regtype_str = get_GLSL_varname_in_buf(ctx, arg->relative_regtype,
arg->relative_regnum,
(char *) alloca(64), 64);
rel_swizzle[0] = '.';
rel_swizzle[1] = swizzle_channels[arg->relative_component];
rel_swizzle[2] = '\0';
} // else
rel_rbracket = "]";
} // if
char swiz_str[6] = { '\0' };
if (!isscalar(ctx, ctx->shader_type, arg->regtype, arg->regnum))
{
make_GLSL_swizzle_string(swiz_str, sizeof (swiz_str),
arg->swizzle, writemask);
} // if
if (regtype_str == NULL)
{
fail(ctx, "Unknown source register type.");
return buf;
} // if
snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s",
premod_str, regtype_str, rel_lbracket, rel_offset,
rel_regtype_str, rel_swizzle, rel_rbracket, swiz_str,
postmod_str);
// !!! FIXME: make sure the scratch buffer was large enough.
return buf;
} // make_GLSL_srcarg_string
// generate some convenience functions.
#define MAKE_GLSL_SRCARG_STRING_(mask, bitmask) \
static inline const char *make_GLSL_srcarg_string_##mask(Context *ctx, \
const size_t idx, char *buf, \
const size_t buflen) { \
return make_GLSL_srcarg_string(ctx, idx, bitmask, buf, buflen); \
}
MAKE_GLSL_SRCARG_STRING_(x, (1 << 0))
MAKE_GLSL_SRCARG_STRING_(y, (1 << 1))
MAKE_GLSL_SRCARG_STRING_(z, (1 << 2))
MAKE_GLSL_SRCARG_STRING_(w, (1 << 3))
MAKE_GLSL_SRCARG_STRING_(scalar, (1 << 0))
MAKE_GLSL_SRCARG_STRING_(full, 0xF)
MAKE_GLSL_SRCARG_STRING_(masked, ctx->dest_arg.writemask)
MAKE_GLSL_SRCARG_STRING_(vec3, 0x7)
MAKE_GLSL_SRCARG_STRING_(vec2, 0x3)
#undef MAKE_GLSL_SRCARG_STRING_
// special cases for comparison opcodes...
const char *get_GLSL_comparison_string_scalar(Context *ctx)
{
const char *comps[] = { "", ">", "==", ">=", "<", "!=", "<=" };
if (ctx->instruction_controls >= STATICARRAYLEN(comps))
{
fail(ctx, "unknown comparison control");
return "";
} // if
return comps[ctx->instruction_controls];
} // get_GLSL_comparison_string_scalar
const char *get_GLSL_comparison_string_vector(Context *ctx)
{
const char *comps[] = {
"", "greaterThan", "equal", "greaterThanEqual", "lessThan",
"notEqual", "lessThanEqual"
};
if (ctx->instruction_controls >= STATICARRAYLEN(comps))
{
fail(ctx, "unknown comparison control");
return "";
} // if
return comps[ctx->instruction_controls];
} // get_GLSL_comparison_string_vector
Aug 21, 2019
Aug 21, 2019
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// special extensions needed for texldd/texldl...
static void prepend_glsl_texlod_extensions(Context *ctx)
{
// !!! FIXME:
// GLSL 1.30 introduced textureGrad() for this, but it looks like the
// functions are overloaded instead of texture2DGrad() (etc).
// !!! FIXME:
// The spec says we can't use GLSL's texture*Lod() built-ins from fragment
// shaders for some inexplicable reason.
// For now, you'll just have to suffer with the potentially wrong mipmap
// until I can figure something out.
// ARB_shader_texture_lod and EXT_gpu_shader4 added texture2DLod/Grad*(),
// so we'll use them if available. Failing that, we'll just fallback
// to a regular texture2D call and hope the mipmap it chooses is close
// enough.
if (!ctx->glsl_generated_texlod_setup)
{
ctx->glsl_generated_texlod_setup = 1;
push_output(ctx, &ctx->preflight);
output_line(ctx, "#if GL_ARB_shader_texture_lod");
output_line(ctx, "#extension GL_ARB_shader_texture_lod : enable");
output_line(ctx, "#define texture2DGrad texture2DGradARB");
output_line(ctx, "#define texture2DProjGrad texture2DProjARB");
output_line(ctx, "#elif GL_EXT_gpu_shader4");
output_line(ctx, "#extension GL_EXT_gpu_shader4 : enable");
output_line(ctx, "#else");
output_line(ctx, "#define texture2DGrad(a,b,c,d) texture2D(a,b)");
output_line(ctx, "#define texture2DProjGrad(a,b,c,d) texture2DProj(a,b)");
Aug 25, 2019
Aug 25, 2019
501
502
if (shader_is_pixel(ctx))
output_line(ctx, "#define texture2DLod(a,b,c) texture2D(a,b)");
Aug 21, 2019
Aug 21, 2019
503
504
505
506
507
508
output_line(ctx, "#endif");
output_blank_line(ctx);
pop_output(ctx);
} // if
} // prepend_glsl_texlod_extensions
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
void emit_GLSL_start(Context *ctx, const char *profilestr)
{
if (!shader_is_vertex(ctx) && !shader_is_pixel(ctx))
{
failf(ctx, "Shader type %u unsupported in this profile.",
(uint) ctx->shader_type);
return;
} // if
else if (strcmp(profilestr, MOJOSHADER_PROFILE_GLSL) == 0)
{
// No gl_FragData[] before GLSL 1.10, so we have to force the version.
push_output(ctx, &ctx->preflight);
output_line(ctx, "#version 110");
pop_output(ctx);
} // else if
#if SUPPORT_PROFILE_GLSL120
else if (strcmp(profilestr, MOJOSHADER_PROFILE_GLSL120) == 0)
{
ctx->profile_supports_glsl120 = 1;
push_output(ctx, &ctx->preflight);
output_line(ctx, "#version 120");
pop_output(ctx);
} // else if
#endif
#if SUPPORT_PROFILE_GLSLES
else if (strcmp(profilestr, MOJOSHADER_PROFILE_GLSLES) == 0)
{
ctx->profile_supports_glsles = 1;
push_output(ctx, &ctx->preflight);
output_line(ctx, "#version 100");
if (shader_is_vertex(ctx))
output_line(ctx, "precision highp float;");
else
output_line(ctx, "precision mediump float;");
output_line(ctx, "precision mediump int;");
pop_output(ctx);
} // else if
#endif
else
{
failf(ctx, "Profile '%s' unsupported or unknown.", profilestr);
return;
} // else
push_output(ctx, &ctx->mainline_intro);
output_line(ctx, "void main()");
output_line(ctx, "{");
pop_output(ctx);
set_output(ctx, &ctx->mainline);
ctx->indent++;
} // emit_GLSL_start
void emit_GLSL_RET(Context *ctx);
void emit_GLSL_end(Context *ctx)
{
// ps_1_* writes color to r0 instead oC0. We move it to the right place.
// We don't have to worry about a RET opcode messing this up, since
// RET isn't available before ps_2_0.
if (shader_is_pixel(ctx) && !shader_version_atleast(ctx, 2, 0))
{
const char *shstr = ctx->shader_type_str;
set_used_register(ctx, REG_TYPE_COLOROUT, 0, 1);
output_line(ctx, "%s_oC0 = %s_r0;", shstr, shstr);
} // if
else if (shader_is_vertex(ctx))
{
#ifdef MOJOSHADER_FLIP_RENDERTARGET
output_line(ctx, "gl_Position.y = gl_Position.y * vpFlip;");
#endif
#ifdef MOJOSHADER_DEPTH_CLIPPING
output_line(ctx, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;");
#endif
} // else if
// force a RET opcode if we're at the end of the stream without one.
if (ctx->previous_opcode != OPCODE_RET)
emit_GLSL_RET(ctx);
} // emit_GLSL_end
void emit_GLSL_phase(Context *ctx)
{
// no-op in GLSL.
} // emit_GLSL_phase
void output_GLSL_uniform_array(Context *ctx, const RegisterType regtype,
const int size)
{
if (size > 0)
{
char buf[64];
get_GLSL_uniform_array_varname(ctx, regtype, buf, sizeof (buf));
const char *typ;
switch (regtype)
{
case REG_TYPE_CONST: typ = "vec4"; break;
case REG_TYPE_CONSTINT: typ ="ivec4"; break;
case REG_TYPE_CONSTBOOL: typ = "bool"; break;
default:
{
fail(ctx, "BUG: used a uniform we don't know how to define.");
return;
} // default
} // switch
output_line(ctx, "uniform %s %s[%d];", typ, buf, size);
} // if
} // output_GLSL_uniform_array
void emit_GLSL_finalize(Context *ctx)
{
// throw some blank lines around to make source more readable.
push_output(ctx, &ctx->globals);
output_blank_line(ctx);
pop_output(ctx);
// If we had a relative addressing of REG_TYPE_INPUT, we need to build
// an array for it at the start of main(). GLSL doesn't let you specify
// arrays of attributes.
//vec4 blah_array[BIGGEST_ARRAY];
if (ctx->have_relative_input_registers) // !!! FIXME
fail(ctx, "Relative addressing of input registers not supported.");
push_output(ctx, &ctx->preflight);
output_GLSL_uniform_array(ctx, REG_TYPE_CONST, ctx->uniform_float4_count);
output_GLSL_uniform_array(ctx, REG_TYPE_CONSTINT, ctx->uniform_int4_count);
output_GLSL_uniform_array(ctx, REG_TYPE_CONSTBOOL, ctx->uniform_bool_count);
#ifdef MOJOSHADER_FLIP_RENDERTARGET
if (shader_is_vertex(ctx))
output_line(ctx, "uniform float vpFlip;");
#endif
Dec 5, 2019
Dec 5, 2019
644
645
if (ctx->glsl_need_max_float)
output_line(ctx, "const float FLT_MAX = 1e38;");
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
pop_output(ctx);
} // emit_GLSL_finalize
void emit_GLSL_global(Context *ctx, RegisterType regtype, int regnum)
{
char varname[64];
get_GLSL_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
push_output(ctx, &ctx->globals);
switch (regtype)
{
case REG_TYPE_ADDRESS:
if (shader_is_vertex(ctx))
output_line(ctx, "ivec4 %s;", varname);
else if (shader_is_pixel(ctx)) // actually REG_TYPE_TEXTURE.
{
// We have to map texture registers to temps for ps_1_1, since
// they work like temps, initialize with tex coords, and the
// ps_1_1 TEX opcode expects to overwrite it.
if (!shader_version_atleast(ctx, 1, 4))
{
Jul 9, 2019
Jul 9, 2019
667
668
// GLSL ES does not have gl_TexCoord!
// Also, gl_TexCoord[4+] is unreliable!
669
#if SUPPORT_PROFILE_GLSLES
Jul 9, 2019
Jul 9, 2019
670
671
672
673
674
const int skipGLTexCoord = support_glsles(ctx) || (regnum >= 4);
#else
const int skipGLTexCoord = (regnum >= 4);
#endif
if (skipGLTexCoord)
675
676
677
output_line(ctx, "vec4 %s = io_%i_%i;",
varname, MOJOSHADER_USAGE_TEXCOORD, regnum);
else
Jul 9, 2019
Jul 9, 2019
678
679
output_line(ctx, "vec4 %s = gl_TexCoord[%d];",
varname, regnum);
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
} // if
} // else if
break;
case REG_TYPE_PREDICATE:
output_line(ctx, "bvec4 %s;", varname);
break;
case REG_TYPE_TEMP:
output_line(ctx, "vec4 %s;", varname);
break;
case REG_TYPE_LOOP:
break; // no-op. We declare these in for loops at the moment.
case REG_TYPE_LABEL:
break; // no-op. If we see it here, it means we optimized it out.
default:
fail(ctx, "BUG: we used a register we don't know how to define.");
break;
} // switch
pop_output(ctx);
} // emit_GLSL_global
void emit_GLSL_array(Context *ctx, VariableList *var)
{
// All uniforms (except constant arrays, which only get pushed once at
// compile time) are now packed into a single array, so we can batch
// the uniform transfers. So this doesn't actually define an array
// here; the one, big array is emitted during finalization instead.
// However, we need to #define the offset into the one, big array here,
// and let dereferences use that #define.
const int base = var->index;
const int glslbase = ctx->uniform_float4_count;
push_output(ctx, &ctx->globals);
output_line(ctx, "#define ARRAYBASE_%d %d", base, glslbase);
pop_output(ctx);
var->emit_position = glslbase;
} // emit_GLSL_array
void emit_GLSL_const_array(Context *ctx, const ConstantsList *clist,
int base, int size)
{
char varname[64];
get_GLSL_const_array_varname_in_buf(ctx,base,size,varname,sizeof(varname));
#if 0
// !!! FIXME: fails on Nvidia's and Apple's GL, even with #version 120.
// !!! FIXME: (the 1.20 spec says it should work, though, I think...)
if (support_glsl120(ctx))
{
// GLSL 1.20 can do constant arrays.
const char *cstr = NULL;
push_output(ctx, &ctx->globals);
output_line(ctx, "const vec4 %s[%d] = vec4[%d](", varname, size, size);
ctx->indent++;
int i;
for (i = 0; i < size; i++)
{
while (clist->constant.type != MOJOSHADER_UNIFORM_FLOAT)
clist = clist->next;
assert(clist->constant.index == (base + i));
char val0[32];
char val1[32];
char val2[32];
char val3[32];
floatstr(ctx, val0, sizeof (val0), clist->constant.value.f[0], 1);
floatstr(ctx, val1, sizeof (val1), clist->constant.value.f[1], 1);
floatstr(ctx, val2, sizeof (val2), clist->constant.value.f[2], 1);
floatstr(ctx, val3, sizeof (val3), clist->constant.value.f[3], 1);
output_line(ctx, "vec4(%s, %s, %s, %s)%s", val0, val1, val2, val3,
(i < (size-1)) ? "," : "");
clist = clist->next;
} // for
ctx->indent--;
output_line(ctx, ");");
pop_output(ctx);
} // if
else
#endif
{
// stock GLSL 1.0 can't do constant arrays, so make a uniform array
// and have the OpenGL glue assign it at link time. Lame!
push_output(ctx, &ctx->globals);
output_line(ctx, "uniform vec4 %s[%d];", varname, size);
pop_output(ctx);
} // else
} // emit_GLSL_const_array
void emit_GLSL_uniform(Context *ctx, RegisterType regtype, int regnum,
const VariableList *var)
{
// Now that we're pushing all the uniforms as one big array, pack these
// down, so if we only use register c439, it'll actually map to
// glsl_uniforms_vec4[0]. As we push one big array, this will prevent
// uploading unused data.
char varname[64];
char name[64];
int index = 0;
get_GLSL_varname_in_buf(ctx, regtype, regnum, varname, sizeof (varname));
push_output(ctx, &ctx->globals);
if (var == NULL)
{
get_GLSL_uniform_array_varname(ctx, regtype, name, sizeof (name));
if (regtype == REG_TYPE_CONST)
index = ctx->uniform_float4_count;
else if (regtype == REG_TYPE_CONSTINT)
index = ctx->uniform_int4_count;
else if (regtype == REG_TYPE_CONSTBOOL)
index = ctx->uniform_bool_count;
else // get_GLSL_uniform_array_varname() would have called fail().
assert(!(ctx->isfail));
output_line(ctx, "#define %s %s[%d]", varname, name, index);
} // if
else
{
const int arraybase = var->index;
if (var->constant)
{
get_GLSL_const_array_varname_in_buf(ctx, arraybase, var->count,
name, sizeof (name));
index = (regnum - arraybase);
} // if
else
{
assert(var->emit_position != -1);
get_GLSL_uniform_array_varname(ctx, regtype, name, sizeof (name));
index = (regnum - arraybase) + var->emit_position;
} // else
output_line(ctx, "#define %s %s[%d]", varname, name, index);
} // else
pop_output(ctx);
} // emit_GLSL_uniform
void emit_GLSL_sampler(Context *ctx,int stage,TextureType ttype,int tb)
{
const char *type = "";
switch (ttype)
{
case TEXTURE_TYPE_2D: type = "sampler2D"; break;
case TEXTURE_TYPE_CUBE: type = "samplerCube"; break;
case TEXTURE_TYPE_VOLUME: type = "sampler3D"; break;
default: fail(ctx, "BUG: used a sampler we don't know how to define.");
} // switch
char var[64];
get_GLSL_varname_in_buf(ctx, REG_TYPE_SAMPLER, stage, var, sizeof (var));
push_output(ctx, &ctx->globals);
output_line(ctx, "uniform %s %s;", type, var);
if (tb) // This sampler used a ps_1_1 TEXBEM opcode?
{
char name[64];
const int index = ctx->uniform_float4_count;
ctx->uniform_float4_count += 2;
get_GLSL_uniform_array_varname(ctx, REG_TYPE_CONST, name, sizeof (name));
output_line(ctx, "#define %s_texbem %s[%d]", var, name, index);
output_line(ctx, "#define %s_texbeml %s[%d]", var, name, index+1);
} // if
pop_output(ctx);
} // emit_GLSL_sampler
void emit_GLSL_attribute(Context *ctx, RegisterType regtype, int regnum,
MOJOSHADER_usage usage, int index, int wmask,
int flags)
{
// !!! FIXME: this function doesn't deal with write masks at all yet!
const char *usage_str = NULL;
const char *arrayleft = "";
const char *arrayright = "";
char index_str[16] = { '\0' };
char var[64];
get_GLSL_varname_in_buf(ctx, regtype, regnum, var, sizeof (var));
//assert((flags & MOD_PP) == 0); // !!! FIXME: is PP allowed?
if (index != 0) // !!! FIXME: a lot of these MUST be zero.
snprintf(index_str, sizeof (index_str), "%u", (uint) index);
if (shader_is_vertex(ctx))
{
// pre-vs3 output registers.
// these don't ever happen in DCL opcodes, I think. Map to vs_3_*
// output registers.
if (!shader_version_atleast(ctx, 3, 0))
{
if (regtype == REG_TYPE_RASTOUT)
{
regtype = REG_TYPE_OUTPUT;
index = regnum;
switch ((const RastOutType) regnum)
{
case RASTOUT_TYPE_POSITION:
usage = MOJOSHADER_USAGE_POSITION;
break;
case RASTOUT_TYPE_FOG:
usage = MOJOSHADER_USAGE_FOG;
break;
case RASTOUT_TYPE_POINT_SIZE:
usage = MOJOSHADER_USAGE_POINTSIZE;
break;
} // switch
} // if
else if (regtype == REG_TYPE_ATTROUT)
{
regtype = REG_TYPE_OUTPUT;
usage = MOJOSHADER_USAGE_COLOR;
index = regnum;
} // else if
else if (regtype == REG_TYPE_TEXCRDOUT)
{
regtype = REG_TYPE_OUTPUT;
usage = MOJOSHADER_USAGE_TEXCOORD;
index = regnum;
} // else if
} // if
// to avoid limitations of various GL entry points for input
// attributes (glSecondaryColorPointer() can only take 3 component
// items, glVertexPointer() can't do GL_UNSIGNED_BYTE, many other
// issues), we set up all inputs as generic vertex attributes, so we
// can pass data in just about any form, and ignore the built-in GLSL
// attributes like gl_SecondaryColor. Output needs to use the the
// built-ins, though, but we don't have to worry about the GL entry
// point limitations there.
if (regtype == REG_TYPE_INPUT)
{
push_output(ctx, &ctx->globals);
output_line(ctx, "attribute vec4 %s;", var);
pop_output(ctx);
} // if
else if (regtype == REG_TYPE_OUTPUT)
{
switch (usage)
{
case MOJOSHADER_USAGE_POSITION:
if (index == 0)
{
usage_str = "gl_Position";
} // if
break;
case MOJOSHADER_USAGE_POINTSIZE:
usage_str = "gl_PointSize";
break;
case MOJOSHADER_USAGE_COLOR:
#if SUPPORT_PROFILE_GLSLES
if (support_glsles(ctx))
break; // GLSL ES does not have gl_FrontColor
#endif
index_str[0] = '\0'; // no explicit number.
if (index == 0)
{
usage_str = "gl_FrontColor";
} // if
else if (index == 1)
{
usage_str = "gl_FrontSecondaryColor";
} // else if
break;
case MOJOSHADER_USAGE_FOG:
Oct 29, 2019
Oct 29, 2019
956
957
958
959
#if SUPPORT_PROFILE_GLSLES
if (support_glsles(ctx))
break; // GLSL ES does not have gl_FogFragCoord
#endif
Dec 16, 2019
Dec 16, 2019
960
if (index == 0)
Oct 29, 2019
Oct 29, 2019
961
962
963
{
usage_str = "gl_FogFragCoord";
} // if
Oct 30, 2019
Oct 30, 2019
964
965
966
967
968
969
970
971
972
973
974
975
976
else
{
push_output(ctx, &ctx->globals);
#if SUPPORT_PROFILE_GLSLES
if (support_glsles(ctx))
output_line(ctx, "varying highp float io_%i_%i;", usage, index);
else
#endif
output_line(ctx, "varying float io_%i_%i;", usage, index);
output_line(ctx, "#define %s io_%i_%i", var, usage, index);
pop_output(ctx);
return;
}
977
978
979
980
981
982
break;
case MOJOSHADER_USAGE_TEXCOORD:
#if SUPPORT_PROFILE_GLSLES
if (support_glsles(ctx))
break; // GLSL ES does not have gl_TexCoord
#endif
Jul 9, 2019
Jul 9, 2019
983
984
if (index >= 4)
break; // gl_TexCoord[4+] is unreliable!
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
snprintf(index_str, sizeof (index_str), "%u", (uint) index);
usage_str = "gl_TexCoord";
arrayleft = "[";
arrayright = "]";
break;
default:
// !!! FIXME: we need to deal with some more built-in varyings here.
break;
} // switch
// !!! FIXME: the #define is a little hacky, but it means we don't
// !!! FIXME: have to track these separately if this works.
push_output(ctx, &ctx->globals);
// no mapping to built-in var? Just make it a regular global, pray.
if (usage_str == NULL)
{