glsl: Add common swizzles to ir_builder.
[mesa.git] / src / mesa / main / ff_fragment_shader.cpp
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 * Copyright 2009 VMware, Inc. All Rights Reserved.
6 * Copyright © 2010-2011 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30 extern "C" {
31 #include "glheader.h"
32 #include "imports.h"
33 #include "mtypes.h"
34 #include "main/uniforms.h"
35 #include "main/macros.h"
36 #include "program/program.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_cache.h"
39 #include "program/prog_instruction.h"
40 #include "program/prog_print.h"
41 #include "program/prog_statevars.h"
42 #include "program/programopt.h"
43 #include "texenvprogram.h"
44 }
45 #include "main/uniforms.h"
46 #include "../glsl/glsl_types.h"
47 #include "../glsl/ir.h"
48 #include "../glsl/ir_builder.h"
49 #include "../glsl/glsl_symbol_table.h"
50 #include "../glsl/glsl_parser_extras.h"
51 #include "../glsl/ir_optimization.h"
52 #include "../glsl/ir_print_visitor.h"
53 #include "../program/ir_to_mesa.h"
54
55 using namespace ir_builder;
56
57 /*
58 * Note on texture units:
59 *
60 * The number of texture units supported by fixed-function fragment
61 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
62 * That's because there's a one-to-one correspondence between texture
63 * coordinates and samplers in fixed-function processing.
64 *
65 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
66 * sets of texcoords, so is fixed-function fragment processing.
67 *
68 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
69 */
70
71
72 struct texenvprog_cache_item
73 {
74 GLuint hash;
75 void *key;
76 struct gl_shader_program *data;
77 struct texenvprog_cache_item *next;
78 };
79
80 static GLboolean
81 texenv_doing_secondary_color(struct gl_context *ctx)
82 {
83 if (ctx->Light.Enabled &&
84 (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR))
85 return GL_TRUE;
86
87 if (ctx->Fog.ColorSumEnabled)
88 return GL_TRUE;
89
90 return GL_FALSE;
91 }
92
93 struct mode_opt {
94 #ifdef __GNUC__
95 __extension__ GLubyte Source:4; /**< SRC_x */
96 __extension__ GLubyte Operand:3; /**< OPR_x */
97 #else
98 GLubyte Source; /**< SRC_x */
99 GLubyte Operand; /**< OPR_x */
100 #endif
101 };
102
103 struct state_key {
104 GLuint nr_enabled_units:8;
105 GLuint enabled_units:8;
106 GLuint separate_specular:1;
107 GLuint fog_enabled:1;
108 GLuint fog_mode:2; /**< FOG_x */
109 GLuint inputs_available:12;
110 GLuint num_draw_buffers:4;
111
112 /* NOTE: This array of structs must be last! (see "keySize" below) */
113 struct {
114 GLuint enabled:1;
115 GLuint source_index:4; /**< TEXTURE_x_INDEX */
116 GLuint shadow:1;
117 GLuint ScaleShiftRGB:2;
118 GLuint ScaleShiftA:2;
119
120 GLuint NumArgsRGB:3; /**< up to MAX_COMBINER_TERMS */
121 GLuint ModeRGB:5; /**< MODE_x */
122
123 GLuint NumArgsA:3; /**< up to MAX_COMBINER_TERMS */
124 GLuint ModeA:5; /**< MODE_x */
125
126 struct mode_opt OptRGB[MAX_COMBINER_TERMS];
127 struct mode_opt OptA[MAX_COMBINER_TERMS];
128 } unit[MAX_TEXTURE_UNITS];
129 };
130
131 #define FOG_LINEAR 0
132 #define FOG_EXP 1
133 #define FOG_EXP2 2
134 #define FOG_UNKNOWN 3
135
136 static GLuint translate_fog_mode( GLenum mode )
137 {
138 switch (mode) {
139 case GL_LINEAR: return FOG_LINEAR;
140 case GL_EXP: return FOG_EXP;
141 case GL_EXP2: return FOG_EXP2;
142 default: return FOG_UNKNOWN;
143 }
144 }
145
146 #define OPR_SRC_COLOR 0
147 #define OPR_ONE_MINUS_SRC_COLOR 1
148 #define OPR_SRC_ALPHA 2
149 #define OPR_ONE_MINUS_SRC_ALPHA 3
150 #define OPR_ZERO 4
151 #define OPR_ONE 5
152 #define OPR_UNKNOWN 7
153
154 static GLuint translate_operand( GLenum operand )
155 {
156 switch (operand) {
157 case GL_SRC_COLOR: return OPR_SRC_COLOR;
158 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
159 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
160 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
161 case GL_ZERO: return OPR_ZERO;
162 case GL_ONE: return OPR_ONE;
163 default:
164 assert(0);
165 return OPR_UNKNOWN;
166 }
167 }
168
169 #define SRC_TEXTURE 0
170 #define SRC_TEXTURE0 1
171 #define SRC_TEXTURE1 2
172 #define SRC_TEXTURE2 3
173 #define SRC_TEXTURE3 4
174 #define SRC_TEXTURE4 5
175 #define SRC_TEXTURE5 6
176 #define SRC_TEXTURE6 7
177 #define SRC_TEXTURE7 8
178 #define SRC_CONSTANT 9
179 #define SRC_PRIMARY_COLOR 10
180 #define SRC_PREVIOUS 11
181 #define SRC_ZERO 12
182 #define SRC_UNKNOWN 15
183
184 static GLuint translate_source( GLenum src )
185 {
186 switch (src) {
187 case GL_TEXTURE: return SRC_TEXTURE;
188 case GL_TEXTURE0:
189 case GL_TEXTURE1:
190 case GL_TEXTURE2:
191 case GL_TEXTURE3:
192 case GL_TEXTURE4:
193 case GL_TEXTURE5:
194 case GL_TEXTURE6:
195 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
196 case GL_CONSTANT: return SRC_CONSTANT;
197 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
198 case GL_PREVIOUS: return SRC_PREVIOUS;
199 case GL_ZERO:
200 return SRC_ZERO;
201 default:
202 assert(0);
203 return SRC_UNKNOWN;
204 }
205 }
206
207 #define MODE_REPLACE 0 /* r = a0 */
208 #define MODE_MODULATE 1 /* r = a0 * a1 */
209 #define MODE_ADD 2 /* r = a0 + a1 */
210 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
211 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
212 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
213 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
214 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
215 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
216 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
217 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
218 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
219 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
220 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
221 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
222 #define MODE_BUMP_ENVMAP_ATI 15 /* special */
223 #define MODE_UNKNOWN 16
224
225 /**
226 * Translate GL combiner state into a MODE_x value
227 */
228 static GLuint translate_mode( GLenum envMode, GLenum mode )
229 {
230 switch (mode) {
231 case GL_REPLACE: return MODE_REPLACE;
232 case GL_MODULATE: return MODE_MODULATE;
233 case GL_ADD:
234 if (envMode == GL_COMBINE4_NV)
235 return MODE_ADD_PRODUCTS;
236 else
237 return MODE_ADD;
238 case GL_ADD_SIGNED:
239 if (envMode == GL_COMBINE4_NV)
240 return MODE_ADD_PRODUCTS_SIGNED;
241 else
242 return MODE_ADD_SIGNED;
243 case GL_INTERPOLATE: return MODE_INTERPOLATE;
244 case GL_SUBTRACT: return MODE_SUBTRACT;
245 case GL_DOT3_RGB: return MODE_DOT3_RGB;
246 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
247 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
248 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
249 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
250 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
251 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
252 case GL_BUMP_ENVMAP_ATI: return MODE_BUMP_ENVMAP_ATI;
253 default:
254 assert(0);
255 return MODE_UNKNOWN;
256 }
257 }
258
259
260 /**
261 * Do we need to clamp the results of the given texture env/combine mode?
262 * If the inputs to the mode are in [0,1] we don't always have to clamp
263 * the results.
264 */
265 static GLboolean
266 need_saturate( GLuint mode )
267 {
268 switch (mode) {
269 case MODE_REPLACE:
270 case MODE_MODULATE:
271 case MODE_INTERPOLATE:
272 return GL_FALSE;
273 case MODE_ADD:
274 case MODE_ADD_SIGNED:
275 case MODE_SUBTRACT:
276 case MODE_DOT3_RGB:
277 case MODE_DOT3_RGB_EXT:
278 case MODE_DOT3_RGBA:
279 case MODE_DOT3_RGBA_EXT:
280 case MODE_MODULATE_ADD_ATI:
281 case MODE_MODULATE_SIGNED_ADD_ATI:
282 case MODE_MODULATE_SUBTRACT_ATI:
283 case MODE_ADD_PRODUCTS:
284 case MODE_ADD_PRODUCTS_SIGNED:
285 case MODE_BUMP_ENVMAP_ATI:
286 return GL_TRUE;
287 default:
288 assert(0);
289 return GL_FALSE;
290 }
291 }
292
293
294
295 /**
296 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
297 */
298 static GLuint translate_tex_src_bit( GLbitfield bit )
299 {
300 ASSERT(bit);
301 return ffs(bit) - 1;
302 }
303
304
305 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
306 #define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
307
308 /**
309 * Identify all possible varying inputs. The fragment program will
310 * never reference non-varying inputs, but will track them via state
311 * constants instead.
312 *
313 * This function figures out all the inputs that the fragment program
314 * has access to. The bitmask is later reduced to just those which
315 * are actually referenced.
316 */
317 static GLbitfield get_fp_input_mask( struct gl_context *ctx )
318 {
319 /* _NEW_PROGRAM */
320 const GLboolean vertexShader =
321 (ctx->Shader.CurrentVertexProgram &&
322 ctx->Shader.CurrentVertexProgram->LinkStatus &&
323 ctx->Shader.CurrentVertexProgram->_LinkedShaders[MESA_SHADER_VERTEX]);
324 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
325 GLbitfield fp_inputs = 0x0;
326
327 if (ctx->VertexProgram._Overriden) {
328 /* Somebody's messing with the vertex program and we don't have
329 * a clue what's happening. Assume that it could be producing
330 * all possible outputs.
331 */
332 fp_inputs = ~0;
333 }
334 else if (ctx->RenderMode == GL_FEEDBACK) {
335 /* _NEW_RENDERMODE */
336 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
337 }
338 else if (!(vertexProgram || vertexShader)) {
339 /* Fixed function vertex logic */
340 /* _NEW_ARRAY */
341 GLbitfield64 varying_inputs = ctx->varying_vp_inputs;
342
343 /* These get generated in the setup routine regardless of the
344 * vertex program:
345 */
346 /* _NEW_POINT */
347 if (ctx->Point.PointSprite)
348 varying_inputs |= FRAG_BITS_TEX_ANY;
349
350 /* First look at what values may be computed by the generated
351 * vertex program:
352 */
353 /* _NEW_LIGHT */
354 if (ctx->Light.Enabled) {
355 fp_inputs |= FRAG_BIT_COL0;
356
357 if (texenv_doing_secondary_color(ctx))
358 fp_inputs |= FRAG_BIT_COL1;
359 }
360
361 /* _NEW_TEXTURE */
362 fp_inputs |= (ctx->Texture._TexGenEnabled |
363 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
364
365 /* Then look at what might be varying as a result of enabled
366 * arrays, etc:
367 */
368 if (varying_inputs & VERT_BIT_COLOR0)
369 fp_inputs |= FRAG_BIT_COL0;
370 if (varying_inputs & VERT_BIT_COLOR1)
371 fp_inputs |= FRAG_BIT_COL1;
372
373 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
374 << FRAG_ATTRIB_TEX0);
375
376 }
377 else {
378 /* calculate from vp->outputs */
379 struct gl_program *vprog;
380 GLbitfield64 vp_outputs;
381
382 /* Choose GLSL vertex shader over ARB vertex program. Need this
383 * since vertex shader state validation comes after fragment state
384 * validation (see additional comments in state.c).
385 */
386 if (vertexShader)
387 vprog = ctx->Shader.CurrentVertexProgram->_LinkedShaders[MESA_SHADER_VERTEX]->Program;
388 else
389 vprog = &ctx->VertexProgram.Current->Base;
390
391 vp_outputs = vprog->OutputsWritten;
392
393 /* These get generated in the setup routine regardless of the
394 * vertex program:
395 */
396 /* _NEW_POINT */
397 if (ctx->Point.PointSprite)
398 vp_outputs |= FRAG_BITS_TEX_ANY;
399
400 if (vp_outputs & (1 << VERT_RESULT_COL0))
401 fp_inputs |= FRAG_BIT_COL0;
402 if (vp_outputs & (1 << VERT_RESULT_COL1))
403 fp_inputs |= FRAG_BIT_COL1;
404
405 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
406 << FRAG_ATTRIB_TEX0);
407 }
408
409 return fp_inputs;
410 }
411
412
413 /**
414 * Examine current texture environment state and generate a unique
415 * key to identify it.
416 */
417 static GLuint make_state_key( struct gl_context *ctx, struct state_key *key )
418 {
419 GLuint i, j;
420 GLbitfield inputs_referenced = FRAG_BIT_COL0;
421 const GLbitfield inputs_available = get_fp_input_mask( ctx );
422 GLuint keySize;
423
424 memset(key, 0, sizeof(*key));
425
426 /* _NEW_TEXTURE */
427 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
428 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
429 const struct gl_texture_object *texObj = texUnit->_Current;
430 const struct gl_tex_env_combine_state *comb = texUnit->_CurrentCombine;
431 GLenum format;
432
433 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
434 continue;
435
436 format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
437
438 key->unit[i].enabled = 1;
439 key->enabled_units |= (1<<i);
440 key->nr_enabled_units = i + 1;
441 inputs_referenced |= FRAG_BIT_TEX(i);
442
443 key->unit[i].source_index =
444 translate_tex_src_bit(texUnit->_ReallyEnabled);
445
446 key->unit[i].shadow =
447 ((texObj->Sampler.CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
448 ((format == GL_DEPTH_COMPONENT) ||
449 (format == GL_DEPTH_STENCIL_EXT)));
450
451 key->unit[i].NumArgsRGB = comb->_NumArgsRGB;
452 key->unit[i].NumArgsA = comb->_NumArgsA;
453
454 key->unit[i].ModeRGB =
455 translate_mode(texUnit->EnvMode, comb->ModeRGB);
456 key->unit[i].ModeA =
457 translate_mode(texUnit->EnvMode, comb->ModeA);
458
459 key->unit[i].ScaleShiftRGB = comb->ScaleShiftRGB;
460 key->unit[i].ScaleShiftA = comb->ScaleShiftA;
461
462 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
463 key->unit[i].OptRGB[j].Operand = translate_operand(comb->OperandRGB[j]);
464 key->unit[i].OptA[j].Operand = translate_operand(comb->OperandA[j]);
465 key->unit[i].OptRGB[j].Source = translate_source(comb->SourceRGB[j]);
466 key->unit[i].OptA[j].Source = translate_source(comb->SourceA[j]);
467 }
468
469 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
470 /* requires some special translation */
471 key->unit[i].NumArgsRGB = 2;
472 key->unit[i].ScaleShiftRGB = 0;
473 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
474 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
475 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
476 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
477 }
478 }
479
480 /* _NEW_LIGHT | _NEW_FOG */
481 if (texenv_doing_secondary_color(ctx)) {
482 key->separate_specular = 1;
483 inputs_referenced |= FRAG_BIT_COL1;
484 }
485
486 /* _NEW_FOG */
487 if (ctx->Fog.Enabled) {
488 key->fog_enabled = 1;
489 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
490 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
491 }
492
493 /* _NEW_BUFFERS */
494 key->num_draw_buffers = ctx->DrawBuffer->_NumColorDrawBuffers;
495
496 /* _NEW_COLOR */
497 if (ctx->Color.AlphaEnabled && key->num_draw_buffers == 0) {
498 /* if alpha test is enabled we need to emit at least one color */
499 key->num_draw_buffers = 1;
500 }
501
502 key->inputs_available = (inputs_available & inputs_referenced);
503
504 /* compute size of state key, ignoring unused texture units */
505 keySize = sizeof(*key) - sizeof(key->unit)
506 + key->nr_enabled_units * sizeof(key->unit[0]);
507
508 return keySize;
509 }
510
511
512 /** State used to build the fragment program:
513 */
514 struct texenv_fragment_program {
515 struct gl_shader_program *shader_program;
516 struct gl_shader *shader;
517 exec_list *instructions;
518 exec_list *top_instructions;
519 void *mem_ctx;
520 struct state_key *state;
521
522 ir_variable *src_texture[MAX_TEXTURE_COORD_UNITS];
523 /* Reg containing each texture unit's sampled texture color,
524 * else undef.
525 */
526
527 /* Texcoord override from bumpmapping. */
528 ir_variable *texcoord_tex[MAX_TEXTURE_COORD_UNITS];
529
530 /* Reg containing texcoord for a texture unit,
531 * needed for bump mapping, else undef.
532 */
533
534 ir_rvalue *src_previous; /**< Reg containing color from previous
535 * stage. May need to be decl'd.
536 */
537 };
538
539 static ir_rvalue *
540 get_current_attrib(struct texenv_fragment_program *p, GLuint attrib)
541 {
542 ir_variable *current;
543 ir_rvalue *val;
544
545 current = p->shader->symbols->get_variable("gl_CurrentAttribFragMESA");
546 current->max_array_access = MAX2(current->max_array_access, attrib);
547 val = new(p->mem_ctx) ir_dereference_variable(current);
548 ir_rvalue *index = new(p->mem_ctx) ir_constant(attrib);
549 return new(p->mem_ctx) ir_dereference_array(val, index);
550 }
551
552 static ir_rvalue *
553 get_gl_Color(struct texenv_fragment_program *p)
554 {
555 if (p->state->inputs_available & FRAG_BIT_COL0) {
556 ir_variable *var = p->shader->symbols->get_variable("gl_Color");
557 assert(var);
558 return new(p->mem_ctx) ir_dereference_variable(var);
559 } else {
560 return get_current_attrib(p, VERT_ATTRIB_COLOR0);
561 }
562 }
563
564 static ir_rvalue *
565 get_source(struct texenv_fragment_program *p,
566 GLuint src, GLuint unit)
567 {
568 ir_variable *var;
569 ir_dereference *deref;
570
571 switch (src) {
572 case SRC_TEXTURE:
573 return new(p->mem_ctx) ir_dereference_variable(p->src_texture[unit]);
574
575 case SRC_TEXTURE0:
576 case SRC_TEXTURE1:
577 case SRC_TEXTURE2:
578 case SRC_TEXTURE3:
579 case SRC_TEXTURE4:
580 case SRC_TEXTURE5:
581 case SRC_TEXTURE6:
582 case SRC_TEXTURE7:
583 return new(p->mem_ctx)
584 ir_dereference_variable(p->src_texture[src - SRC_TEXTURE0]);
585
586 case SRC_CONSTANT:
587 var = p->shader->symbols->get_variable("gl_TextureEnvColor");
588 assert(var);
589 deref = new(p->mem_ctx) ir_dereference_variable(var);
590 var->max_array_access = MAX2(var->max_array_access, unit);
591 return new(p->mem_ctx) ir_dereference_array(deref,
592 new(p->mem_ctx) ir_constant(unit));
593
594 case SRC_PRIMARY_COLOR:
595 var = p->shader->symbols->get_variable("gl_Color");
596 assert(var);
597 return new(p->mem_ctx) ir_dereference_variable(var);
598
599 case SRC_ZERO:
600 return new(p->mem_ctx) ir_constant(0.0f);
601
602 case SRC_PREVIOUS:
603 if (!p->src_previous) {
604 return get_gl_Color(p);
605 } else {
606 return p->src_previous->clone(p->mem_ctx, NULL);
607 }
608
609 default:
610 assert(0);
611 return NULL;
612 }
613 }
614
615 static ir_rvalue *
616 emit_combine_source(struct texenv_fragment_program *p,
617 GLuint unit,
618 GLuint source,
619 GLuint operand)
620 {
621 ir_rvalue *src;
622
623 src = get_source(p, source, unit);
624
625 switch (operand) {
626 case OPR_ONE_MINUS_SRC_COLOR:
627 return sub(new(p->mem_ctx) ir_constant(1.0f), src);
628
629 case OPR_SRC_ALPHA:
630 return src->type->is_scalar() ? src : swizzle_w(src);
631
632 case OPR_ONE_MINUS_SRC_ALPHA: {
633 ir_rvalue *const scalar = src->type->is_scalar() ? src : swizzle_w(src);
634
635 return sub(new(p->mem_ctx) ir_constant(1.0f), scalar);
636 }
637
638 case OPR_ZERO:
639 return new(p->mem_ctx) ir_constant(0.0f);
640 case OPR_ONE:
641 return new(p->mem_ctx) ir_constant(1.0f);
642 case OPR_SRC_COLOR:
643 return src;
644 default:
645 assert(0);
646 return src;
647 }
648 }
649
650 /**
651 * Check if the RGB and Alpha sources and operands match for the given
652 * texture unit's combinder state. When the RGB and A sources and
653 * operands match, we can emit fewer instructions.
654 */
655 static GLboolean args_match( const struct state_key *key, GLuint unit )
656 {
657 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
658
659 for (i = 0; i < numArgs; i++) {
660 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
661 return GL_FALSE;
662
663 switch (key->unit[unit].OptA[i].Operand) {
664 case OPR_SRC_ALPHA:
665 switch (key->unit[unit].OptRGB[i].Operand) {
666 case OPR_SRC_COLOR:
667 case OPR_SRC_ALPHA:
668 break;
669 default:
670 return GL_FALSE;
671 }
672 break;
673 case OPR_ONE_MINUS_SRC_ALPHA:
674 switch (key->unit[unit].OptRGB[i].Operand) {
675 case OPR_ONE_MINUS_SRC_COLOR:
676 case OPR_ONE_MINUS_SRC_ALPHA:
677 break;
678 default:
679 return GL_FALSE;
680 }
681 break;
682 default:
683 return GL_FALSE; /* impossible */
684 }
685 }
686
687 return GL_TRUE;
688 }
689
690 static ir_rvalue *
691 smear(struct texenv_fragment_program *p, ir_rvalue *val)
692 {
693 if (!val->type->is_scalar())
694 return val;
695
696 return swizzle_xxxx(val);
697 }
698
699 static ir_rvalue *
700 emit_combine(struct texenv_fragment_program *p,
701 GLuint unit,
702 GLuint nr,
703 GLuint mode,
704 const struct mode_opt *opt)
705 {
706 ir_rvalue *src[MAX_COMBINER_TERMS];
707 ir_rvalue *tmp0, *tmp1;
708 GLuint i;
709
710 assert(nr <= MAX_COMBINER_TERMS);
711
712 for (i = 0; i < nr; i++)
713 src[i] = emit_combine_source( p, unit, opt[i].Source, opt[i].Operand );
714
715 switch (mode) {
716 case MODE_REPLACE:
717 return src[0];
718
719 case MODE_MODULATE:
720 return mul(src[0], src[1]);
721
722 case MODE_ADD:
723 return add(src[0], src[1]);
724
725 case MODE_ADD_SIGNED:
726 return add(add(src[0], src[1]), new(p->mem_ctx) ir_constant(-0.5f));
727
728 case MODE_INTERPOLATE:
729 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
730 tmp0 = mul(src[0], src[2]);
731 tmp1 = mul(src[1], sub(new(p->mem_ctx) ir_constant(1.0f),
732 src[2]->clone(p->mem_ctx, NULL)));
733 return add(tmp0, tmp1);
734
735 case MODE_SUBTRACT:
736 return sub(src[0], src[1]);
737
738 case MODE_DOT3_RGBA:
739 case MODE_DOT3_RGBA_EXT:
740 case MODE_DOT3_RGB_EXT:
741 case MODE_DOT3_RGB: {
742 tmp0 = mul(src[0], new(p->mem_ctx) ir_constant(2.0f));
743 tmp0 = add(tmp0, new(p->mem_ctx) ir_constant(-1.0f));
744
745 tmp1 = mul(src[1], new(p->mem_ctx) ir_constant(2.0f));
746 tmp1 = add(tmp1, new(p->mem_ctx) ir_constant(-1.0f));
747
748 return dot(swizzle_xyz(smear(p, tmp0)), swizzle_xyz(smear(p, tmp1)));
749 }
750 case MODE_MODULATE_ADD_ATI:
751 return add(mul(src[0], src[2]), src[1]);
752
753 case MODE_MODULATE_SIGNED_ADD_ATI:
754 return add(add(mul(src[0], src[2]), src[1]),
755 new(p->mem_ctx) ir_constant(-0.5f));
756
757 case MODE_MODULATE_SUBTRACT_ATI:
758 return sub(mul(src[0], src[2]), src[1]);
759
760 case MODE_ADD_PRODUCTS:
761 return add(mul(src[0], src[1]), mul(src[2], src[3]));
762
763 case MODE_ADD_PRODUCTS_SIGNED:
764 return add(add(mul(src[0], src[1]), mul(src[2], src[3])),
765 new(p->mem_ctx) ir_constant(-0.5f));
766
767 case MODE_BUMP_ENVMAP_ATI:
768 /* special - not handled here */
769 assert(0);
770 return src[0];
771 default:
772 assert(0);
773 return src[0];
774 }
775 }
776
777 /**
778 * Generate instructions for one texture unit's env/combiner mode.
779 */
780 static ir_rvalue *
781 emit_texenv(struct texenv_fragment_program *p, GLuint unit)
782 {
783 const struct state_key *key = p->state;
784 GLboolean rgb_saturate, alpha_saturate;
785 GLuint rgb_shift, alpha_shift;
786
787 if (!key->unit[unit].enabled) {
788 return get_source(p, SRC_PREVIOUS, 0);
789 }
790 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
791 /* this isn't really a env stage delivering a color and handled elsewhere */
792 return get_source(p, SRC_PREVIOUS, 0);
793 }
794
795 switch (key->unit[unit].ModeRGB) {
796 case MODE_DOT3_RGB_EXT:
797 alpha_shift = key->unit[unit].ScaleShiftA;
798 rgb_shift = 0;
799 break;
800 case MODE_DOT3_RGBA_EXT:
801 alpha_shift = 0;
802 rgb_shift = 0;
803 break;
804 default:
805 rgb_shift = key->unit[unit].ScaleShiftRGB;
806 alpha_shift = key->unit[unit].ScaleShiftA;
807 break;
808 }
809
810 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
811 * We don't want to clamp twice.
812 */
813 if (rgb_shift)
814 rgb_saturate = GL_FALSE; /* saturate after rgb shift */
815 else if (need_saturate(key->unit[unit].ModeRGB))
816 rgb_saturate = GL_TRUE;
817 else
818 rgb_saturate = GL_FALSE;
819
820 if (alpha_shift)
821 alpha_saturate = GL_FALSE; /* saturate after alpha shift */
822 else if (need_saturate(key->unit[unit].ModeA))
823 alpha_saturate = GL_TRUE;
824 else
825 alpha_saturate = GL_FALSE;
826
827 ir_variable *temp_var = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
828 "texenv_combine",
829 ir_var_temporary);
830 p->instructions->push_tail(temp_var);
831
832 ir_dereference *deref;
833 ir_assignment *assign;
834 ir_rvalue *val;
835
836 /* Emit the RGB and A combine ops
837 */
838 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
839 args_match(key, unit)) {
840 val = emit_combine(p, unit,
841 key->unit[unit].NumArgsRGB,
842 key->unit[unit].ModeRGB,
843 key->unit[unit].OptRGB);
844 val = smear(p, val);
845 if (rgb_saturate)
846 val = saturate(val);
847
848 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
849 assign = new(p->mem_ctx) ir_assignment(deref, val);
850 p->instructions->push_tail(assign);
851 }
852 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
853 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
854 ir_rvalue *val = emit_combine(p, unit,
855 key->unit[unit].NumArgsRGB,
856 key->unit[unit].ModeRGB,
857 key->unit[unit].OptRGB);
858 val = smear(p, val);
859 if (rgb_saturate)
860 val = saturate(val);
861 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
862 assign = new(p->mem_ctx) ir_assignment(deref, val);
863 p->instructions->push_tail(assign);
864 }
865 else {
866 /* Need to do something to stop from re-emitting identical
867 * argument calculations here:
868 */
869 val = emit_combine(p, unit,
870 key->unit[unit].NumArgsRGB,
871 key->unit[unit].ModeRGB,
872 key->unit[unit].OptRGB);
873 val = swizzle_xyz(smear(p, val));
874 if (rgb_saturate)
875 val = saturate(val);
876 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
877 assign = new(p->mem_ctx) ir_assignment(deref, val, NULL, WRITEMASK_XYZ);
878 p->instructions->push_tail(assign);
879
880 val = emit_combine(p, unit,
881 key->unit[unit].NumArgsA,
882 key->unit[unit].ModeA,
883 key->unit[unit].OptA);
884 val = swizzle_w(smear(p, val));
885 if (alpha_saturate)
886 val = saturate(val);
887 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
888 assign = new(p->mem_ctx) ir_assignment(deref, val, NULL, WRITEMASK_W);
889 p->instructions->push_tail(assign);
890 }
891
892 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
893
894 /* Deal with the final shift:
895 */
896 if (alpha_shift || rgb_shift) {
897 ir_constant *shift;
898
899 if (rgb_shift == alpha_shift) {
900 shift = new(p->mem_ctx) ir_constant((float)(1 << rgb_shift));
901 }
902 else {
903 float const_data[4] = {
904 1 << rgb_shift,
905 1 << rgb_shift,
906 1 << rgb_shift,
907 1 << alpha_shift
908 };
909 shift = new(p->mem_ctx) ir_constant(glsl_type::vec4_type,
910 (ir_constant_data *)const_data);
911 }
912
913 return saturate(mul(deref, shift));
914 }
915 else
916 return deref;
917 }
918
919
920 /**
921 * Generate instruction for getting a texture source term.
922 */
923 static void load_texture( struct texenv_fragment_program *p, GLuint unit )
924 {
925 ir_dereference *deref;
926 ir_assignment *assign;
927
928 if (p->src_texture[unit])
929 return;
930
931 const GLuint texTarget = p->state->unit[unit].source_index;
932 ir_rvalue *texcoord;
933
934 if (!(p->state->inputs_available & (FRAG_BIT_TEX0 << unit))) {
935 texcoord = get_current_attrib(p, VERT_ATTRIB_TEX0 + unit);
936 } else if (p->texcoord_tex[unit]) {
937 texcoord = new(p->mem_ctx) ir_dereference_variable(p->texcoord_tex[unit]);
938 } else {
939 ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
940 assert(tc_array);
941 texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
942 ir_rvalue *index = new(p->mem_ctx) ir_constant(unit);
943 texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
944 tc_array->max_array_access = MAX2(tc_array->max_array_access, unit);
945 }
946
947 if (!p->state->unit[unit].enabled) {
948 p->src_texture[unit] = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
949 "dummy_tex",
950 ir_var_temporary);
951 p->instructions->push_tail(p->src_texture[unit]);
952
953 deref = new(p->mem_ctx) ir_dereference_variable(p->src_texture[unit]);
954 assign = new(p->mem_ctx) ir_assignment(deref,
955 new(p->mem_ctx) ir_constant(0.0f));
956 p->instructions->push_tail(assign);
957 return ;
958 }
959
960 const glsl_type *sampler_type = NULL;
961 int coords = 0;
962
963 switch (texTarget) {
964 case TEXTURE_1D_INDEX:
965 if (p->state->unit[unit].shadow)
966 sampler_type = p->shader->symbols->get_type("sampler1DShadow");
967 else
968 sampler_type = p->shader->symbols->get_type("sampler1D");
969 coords = 1;
970 break;
971 case TEXTURE_1D_ARRAY_INDEX:
972 if (p->state->unit[unit].shadow)
973 sampler_type = p->shader->symbols->get_type("sampler1DArrayShadow");
974 else
975 sampler_type = p->shader->symbols->get_type("sampler1DArray");
976 coords = 2;
977 break;
978 case TEXTURE_2D_INDEX:
979 if (p->state->unit[unit].shadow)
980 sampler_type = p->shader->symbols->get_type("sampler2DShadow");
981 else
982 sampler_type = p->shader->symbols->get_type("sampler2D");
983 coords = 2;
984 break;
985 case TEXTURE_2D_ARRAY_INDEX:
986 if (p->state->unit[unit].shadow)
987 sampler_type = p->shader->symbols->get_type("sampler2DArrayShadow");
988 else
989 sampler_type = p->shader->symbols->get_type("sampler2DArray");
990 coords = 3;
991 break;
992 case TEXTURE_RECT_INDEX:
993 if (p->state->unit[unit].shadow)
994 sampler_type = p->shader->symbols->get_type("sampler2DRectShadow");
995 else
996 sampler_type = p->shader->symbols->get_type("sampler2DRect");
997 coords = 2;
998 break;
999 case TEXTURE_3D_INDEX:
1000 assert(!p->state->unit[unit].shadow);
1001 sampler_type = p->shader->symbols->get_type("sampler3D");
1002 coords = 3;
1003 break;
1004 case TEXTURE_CUBE_INDEX:
1005 if (p->state->unit[unit].shadow)
1006 sampler_type = p->shader->symbols->get_type("samplerCubeShadow");
1007 else
1008 sampler_type = p->shader->symbols->get_type("samplerCube");
1009 coords = 3;
1010 break;
1011 case TEXTURE_EXTERNAL_INDEX:
1012 assert(!p->state->unit[unit].shadow);
1013 sampler_type = p->shader->symbols->get_type("samplerExternalOES");
1014 coords = 2;
1015 break;
1016 }
1017
1018 p->src_texture[unit] = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
1019 "tex",
1020 ir_var_temporary);
1021 p->instructions->push_tail(p->src_texture[unit]);
1022
1023 ir_texture *tex = new(p->mem_ctx) ir_texture(ir_tex);
1024
1025
1026 char *sampler_name = ralloc_asprintf(p->mem_ctx, "sampler_%d", unit);
1027 ir_variable *sampler = new(p->mem_ctx) ir_variable(sampler_type,
1028 sampler_name,
1029 ir_var_uniform);
1030 p->top_instructions->push_head(sampler);
1031 deref = new(p->mem_ctx) ir_dereference_variable(sampler);
1032 tex->set_sampler(deref, glsl_type::vec4_type);
1033
1034 tex->coordinate = new(p->mem_ctx) ir_swizzle(texcoord, 0, 1, 2, 3, coords);
1035
1036 if (p->state->unit[unit].shadow) {
1037 texcoord = texcoord->clone(p->mem_ctx, NULL);
1038 tex->shadow_comparitor = new(p->mem_ctx) ir_swizzle(texcoord,
1039 coords, 0, 0, 0,
1040 1);
1041 coords++;
1042 }
1043
1044 texcoord = texcoord->clone(p->mem_ctx, NULL);
1045 tex->projector = swizzle_w(texcoord);
1046
1047 deref = new(p->mem_ctx) ir_dereference_variable(p->src_texture[unit]);
1048 assign = new(p->mem_ctx) ir_assignment(deref, tex);
1049 p->instructions->push_tail(assign);
1050 }
1051
1052 static void
1053 load_texenv_source(struct texenv_fragment_program *p,
1054 GLuint src, GLuint unit)
1055 {
1056 switch (src) {
1057 case SRC_TEXTURE:
1058 load_texture(p, unit);
1059 break;
1060
1061 case SRC_TEXTURE0:
1062 case SRC_TEXTURE1:
1063 case SRC_TEXTURE2:
1064 case SRC_TEXTURE3:
1065 case SRC_TEXTURE4:
1066 case SRC_TEXTURE5:
1067 case SRC_TEXTURE6:
1068 case SRC_TEXTURE7:
1069 load_texture(p, src - SRC_TEXTURE0);
1070 break;
1071
1072 default:
1073 /* not a texture src - do nothing */
1074 break;
1075 }
1076 }
1077
1078
1079 /**
1080 * Generate instructions for loading all texture source terms.
1081 */
1082 static GLboolean
1083 load_texunit_sources( struct texenv_fragment_program *p, GLuint unit )
1084 {
1085 const struct state_key *key = p->state;
1086 GLuint i;
1087
1088 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1089 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
1090 }
1091
1092 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1093 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1094 }
1095
1096 return GL_TRUE;
1097 }
1098
1099 /**
1100 * Generate instructions for loading bump map textures.
1101 */
1102 static void
1103 load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
1104 {
1105 const struct state_key *key = p->state;
1106 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1107 ir_rvalue *bump;
1108 ir_rvalue *texcoord;
1109 ir_variable *rot_mat_0, *rot_mat_1;
1110
1111 rot_mat_0 = p->shader->symbols->get_variable("gl_BumpRotMatrix0MESA");
1112 rot_mat_1 = p->shader->symbols->get_variable("gl_BumpRotMatrix1MESA");
1113
1114 ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
1115 assert(tc_array);
1116 texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
1117 ir_rvalue *index = new(p->mem_ctx) ir_constant(bumpedUnitNr);
1118 texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
1119 tc_array->max_array_access = MAX2(tc_array->max_array_access, unit);
1120
1121 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1122
1123 /* Apply rot matrix and add coords to be available in next phase.
1124 * dest = Arg1 + (Arg0.xx * rotMat0) + (Arg0.yy * rotMat1)
1125 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1126 */
1127 ir_dereference *deref;
1128 ir_assignment *assign;
1129 ir_rvalue *bump_x, *bump_y;
1130
1131 texcoord = smear(p, texcoord);
1132
1133 /* bump_texcoord = texcoord */
1134 ir_variable *bumped = new(p->mem_ctx) ir_variable(texcoord->type,
1135 "bump_texcoord",
1136 ir_var_temporary);
1137 p->instructions->push_tail(bumped);
1138
1139 deref = new(p->mem_ctx) ir_dereference_variable(bumped);
1140 assign = new(p->mem_ctx) ir_assignment(deref, texcoord);
1141 p->instructions->push_tail(assign);
1142
1143 /* bump_texcoord.xy += arg0.x * rotmat0 + arg0.y * rotmat1 */
1144 bump = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1145 bump_x = mul(swizzle_x(bump), rot_mat_0);
1146 bump_y = mul(swizzle_y(bump->clone(p->mem_ctx, NULL)), rot_mat_1);
1147
1148 ir_expression *expr;
1149
1150 expr = add(swizzle_xy(bumped), add(bump_x, bump_y));
1151
1152 deref = new(p->mem_ctx) ir_dereference_variable(bumped);
1153 assign = new(p->mem_ctx) ir_assignment(deref, expr, NULL, WRITEMASK_XY);
1154 p->instructions->push_tail(assign);
1155
1156 p->texcoord_tex[bumpedUnitNr] = bumped;
1157 }
1158
1159 /**
1160 * Applies the fog calculations.
1161 *
1162 * This is basically like the ARB_fragment_prorgam fog options. Note
1163 * that ffvertex_prog.c produces fogcoord for us when
1164 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1165 */
1166 static ir_rvalue *
1167 emit_fog_instructions(struct texenv_fragment_program *p,
1168 ir_rvalue *fragcolor)
1169 {
1170 struct state_key *key = p->state;
1171 ir_rvalue *f, *temp;
1172 ir_variable *params, *oparams;
1173 ir_variable *fogcoord;
1174 ir_assignment *assign;
1175
1176 /* Temporary storage for the whole fog result. Fog calculations
1177 * only affect rgb so we're hanging on to the .a value of fragcolor
1178 * this way.
1179 */
1180 ir_variable *fog_result = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
1181 "fog_result",
1182 ir_var_auto);
1183 p->instructions->push_tail(fog_result);
1184 temp = new(p->mem_ctx) ir_dereference_variable(fog_result);
1185 assign = new(p->mem_ctx) ir_assignment(temp, fragcolor);
1186 p->instructions->push_tail(assign);
1187
1188 fragcolor = swizzle_xyz(fog_result);
1189
1190 oparams = p->shader->symbols->get_variable("gl_FogParamsOptimizedMESA");
1191 fogcoord = p->shader->symbols->get_variable("gl_FogFragCoord");
1192 params = p->shader->symbols->get_variable("gl_Fog");
1193 f = new(p->mem_ctx) ir_dereference_variable(fogcoord);
1194
1195 ir_variable *f_var = new(p->mem_ctx) ir_variable(glsl_type::float_type,
1196 "fog_factor", ir_var_auto);
1197 p->instructions->push_tail(f_var);
1198
1199 switch (key->fog_mode) {
1200 case FOG_LINEAR:
1201 /* f = (end - z) / (end - start)
1202 *
1203 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1204 * (end / (end - start)) so we can generate a single MAD.
1205 */
1206 f = add(mul(f, swizzle_x(oparams)), swizzle_y(oparams));
1207 break;
1208 case FOG_EXP:
1209 /* f = e^(-(density * fogcoord))
1210 *
1211 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1212 * use EXP2 which is generally the native instruction without
1213 * having to do any further math on the fog density uniform.
1214 */
1215 f = mul(f, swizzle_z(oparams));
1216 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
1217 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
1218 break;
1219 case FOG_EXP2:
1220 /* f = e^(-(density * fogcoord)^2)
1221 *
1222 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1223 * can do this like FOG_EXP but with a squaring after the
1224 * multiply by density.
1225 */
1226 ir_variable *temp_var = new(p->mem_ctx) ir_variable(glsl_type::float_type,
1227 "fog_temp",
1228 ir_var_auto);
1229 p->instructions->push_tail(temp_var);
1230
1231 f = mul(f, swizzle_w(oparams));
1232
1233 temp = new(p->mem_ctx) ir_dereference_variable(temp_var);
1234 ir_assignment *assign = new(p->mem_ctx) ir_assignment(temp, f);
1235 p->instructions->push_tail(assign);
1236
1237 f = mul(temp_var, temp_var);
1238 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
1239 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
1240 break;
1241 }
1242
1243 f = saturate(f);
1244
1245 temp = new(p->mem_ctx) ir_dereference_variable(f_var);
1246 assign = new(p->mem_ctx) ir_assignment(temp, f);
1247 p->instructions->push_tail(assign);
1248
1249 f = sub(new(p->mem_ctx) ir_constant(1.0f), f_var);
1250 temp = new(p->mem_ctx) ir_dereference_variable(params);
1251 temp = new(p->mem_ctx) ir_dereference_record(temp, "color");
1252 temp = mul(swizzle_xyz(temp), f);
1253
1254 f = add(temp, mul(fragcolor, f_var));
1255
1256 ir_dereference *deref = new(p->mem_ctx) ir_dereference_variable(fog_result);
1257 assign = new(p->mem_ctx) ir_assignment(deref, f, NULL, WRITEMASK_XYZ);
1258 p->instructions->push_tail(assign);
1259
1260 return new(p->mem_ctx) ir_dereference_variable(fog_result);
1261 }
1262
1263 static void
1264 emit_instructions(struct texenv_fragment_program *p)
1265 {
1266 struct state_key *key = p->state;
1267 GLuint unit;
1268
1269 if (key->enabled_units) {
1270 /* Zeroth pass - bump map textures first */
1271 for (unit = 0; unit < key->nr_enabled_units; unit++) {
1272 if (key->unit[unit].enabled &&
1273 key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1274 load_texunit_bumpmap(p, unit);
1275 }
1276 }
1277
1278 /* First pass - to support texture_env_crossbar, first identify
1279 * all referenced texture sources and emit texld instructions
1280 * for each:
1281 */
1282 for (unit = 0; unit < key->nr_enabled_units; unit++)
1283 if (key->unit[unit].enabled) {
1284 load_texunit_sources(p, unit);
1285 }
1286
1287 /* Second pass - emit combine instructions to build final color:
1288 */
1289 for (unit = 0; unit < key->nr_enabled_units; unit++) {
1290 if (key->unit[unit].enabled) {
1291 p->src_previous = emit_texenv(p, unit);
1292 }
1293 }
1294 }
1295
1296 ir_rvalue *cf = get_source(p, SRC_PREVIOUS, 0);
1297 ir_dereference_variable *deref;
1298 ir_assignment *assign;
1299
1300 if (key->separate_specular) {
1301 ir_rvalue *tmp0;
1302 ir_variable *spec_result = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
1303 "specular_add",
1304 ir_var_temporary);
1305
1306 p->instructions->push_tail(spec_result);
1307
1308 deref = new(p->mem_ctx) ir_dereference_variable(spec_result);
1309 assign = new(p->mem_ctx) ir_assignment(deref, cf);
1310 p->instructions->push_tail(assign);
1311
1312 ir_rvalue *secondary;
1313 if (p->state->inputs_available & FRAG_BIT_COL1) {
1314 ir_variable *var =
1315 p->shader->symbols->get_variable("gl_SecondaryColor");
1316 assert(var);
1317 secondary = swizzle_xyz(var);
1318 } else {
1319 secondary = swizzle_xyz(get_current_attrib(p, VERT_ATTRIB_COLOR1));
1320 }
1321
1322 tmp0 = add(swizzle_xyz(spec_result), secondary);
1323
1324 deref = new(p->mem_ctx) ir_dereference_variable(spec_result);
1325 assign = new(p->mem_ctx) ir_assignment(deref, tmp0, NULL, WRITEMASK_XYZ);
1326 p->instructions->push_tail(assign);
1327
1328 cf = new(p->mem_ctx) ir_dereference_variable(spec_result);
1329 }
1330
1331 if (key->fog_enabled) {
1332 cf = emit_fog_instructions(p, cf);
1333 }
1334
1335 ir_variable *frag_color = p->shader->symbols->get_variable("gl_FragColor");
1336 assert(frag_color);
1337 deref = new(p->mem_ctx) ir_dereference_variable(frag_color);
1338 assign = new(p->mem_ctx) ir_assignment(deref, cf);
1339 p->instructions->push_tail(assign);
1340 }
1341
1342 /**
1343 * Generate a new fragment program which implements the context's
1344 * current texture env/combine mode.
1345 */
1346 static struct gl_shader_program *
1347 create_new_program(struct gl_context *ctx, struct state_key *key)
1348 {
1349 struct texenv_fragment_program p;
1350 unsigned int unit;
1351 _mesa_glsl_parse_state *state;
1352
1353 memset(&p, 0, sizeof(p));
1354 p.mem_ctx = ralloc_context(NULL);
1355 p.shader = ctx->Driver.NewShader(ctx, 0, GL_FRAGMENT_SHADER);
1356 p.shader->ir = new(p.shader) exec_list;
1357 state = new(p.shader) _mesa_glsl_parse_state(ctx, GL_FRAGMENT_SHADER,
1358 p.shader);
1359 p.shader->symbols = state->symbols;
1360 p.top_instructions = p.shader->ir;
1361 p.instructions = p.shader->ir;
1362 p.state = key;
1363 p.shader_program = ctx->Driver.NewShaderProgram(ctx, 0);
1364
1365 /* Tell the linker to ignore the fact that we're building a
1366 * separate shader, in case we're in a GLES2 context that would
1367 * normally reject that. The real problem is that we're building a
1368 * fixed function program in a GLES2 context at all, but that's a
1369 * big mess to clean up.
1370 */
1371 p.shader_program->InternalSeparateShader = GL_TRUE;
1372
1373 state->language_version = 130;
1374 if (ctx->Extensions.OES_EGL_image_external)
1375 state->OES_EGL_image_external_enable = true;
1376 _mesa_glsl_initialize_types(state);
1377 _mesa_glsl_initialize_variables(p.instructions, state);
1378
1379 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
1380 p.src_texture[unit] = NULL;
1381 p.texcoord_tex[unit] = NULL;
1382 }
1383
1384 p.src_previous = NULL;
1385
1386 ir_function *main_f = new(p.mem_ctx) ir_function("main");
1387 p.instructions->push_tail(main_f);
1388 state->symbols->add_function(main_f);
1389
1390 ir_function_signature *main_sig =
1391 new(p.mem_ctx) ir_function_signature(p.shader->symbols->get_type("void"));
1392 main_sig->is_defined = true;
1393 main_f->add_signature(main_sig);
1394
1395 p.instructions = &main_sig->body;
1396 if (key->num_draw_buffers)
1397 emit_instructions(&p);
1398
1399 validate_ir_tree(p.shader->ir);
1400
1401 while (do_common_optimization(p.shader->ir, false, false, 32))
1402 ;
1403 reparent_ir(p.shader->ir, p.shader->ir);
1404
1405 p.shader->CompileStatus = true;
1406 p.shader->Version = state->language_version;
1407 p.shader->num_builtins_to_link = state->num_builtins_to_link;
1408 p.shader_program->Shaders =
1409 (gl_shader **)malloc(sizeof(*p.shader_program->Shaders));
1410 p.shader_program->Shaders[0] = p.shader;
1411 p.shader_program->NumShaders = 1;
1412
1413 _mesa_glsl_link_shader(ctx, p.shader_program);
1414
1415 /* Set the sampler uniforms, and relink to get them into the linked
1416 * program.
1417 */
1418 struct gl_shader *const fs =
1419 p.shader_program->_LinkedShaders[MESA_SHADER_FRAGMENT];
1420 struct gl_program *const fp = fs->Program;
1421
1422 _mesa_generate_parameters_list_for_uniforms(p.shader_program, fs,
1423 fp->Parameters);
1424
1425 _mesa_associate_uniform_storage(ctx, p.shader_program, fp->Parameters);
1426
1427 for (unsigned int i = 0; i < MAX_TEXTURE_UNITS; i++) {
1428 /* Enough space for 'sampler_999\0'.
1429 */
1430 char name[12];
1431
1432 snprintf(name, sizeof(name), "sampler_%d", i);
1433
1434 int loc = _mesa_get_uniform_location(ctx, p.shader_program, name);
1435 if (loc != -1) {
1436 unsigned base;
1437 unsigned idx;
1438
1439 /* Avoid using _mesa_uniform() because it flags state
1440 * updates, so if we're generating this shader_program in a
1441 * state update, we end up recursing. Instead, just set the
1442 * value, which is picked up at re-link.
1443 */
1444 _mesa_uniform_split_location_offset(loc, &base, &idx);
1445 assert(idx == 0);
1446
1447 struct gl_uniform_storage *const storage =
1448 &p.shader_program->UniformStorage[base];
1449
1450 /* Update the storage, the SamplerUnits in the shader program, and
1451 * the SamplerUnits in the assembly shader.
1452 */
1453 storage->storage[idx].i = i;
1454 fp->SamplerUnits[storage->sampler] = i;
1455 p.shader_program->SamplerUnits[storage->sampler] = i;
1456 _mesa_propagate_uniforms_to_driver_storage(storage, 0, 1);
1457 }
1458 }
1459 _mesa_update_shader_textures_used(p.shader_program, fp);
1460 (void) ctx->Driver.ProgramStringNotify(ctx, fp->Target, fp);
1461
1462 if (!p.shader_program->LinkStatus)
1463 _mesa_problem(ctx, "Failed to link fixed function fragment shader: %s\n",
1464 p.shader_program->InfoLog);
1465
1466 ralloc_free(p.mem_ctx);
1467 return p.shader_program;
1468 }
1469
1470 extern "C" {
1471
1472 /**
1473 * Return a fragment program which implements the current
1474 * fixed-function texture, fog and color-sum operations.
1475 */
1476 struct gl_shader_program *
1477 _mesa_get_fixed_func_fragment_program(struct gl_context *ctx)
1478 {
1479 struct gl_shader_program *shader_program;
1480 struct state_key key;
1481 GLuint keySize;
1482
1483 keySize = make_state_key(ctx, &key);
1484
1485 shader_program = (struct gl_shader_program *)
1486 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1487 &key, keySize);
1488
1489 if (!shader_program) {
1490 shader_program = create_new_program(ctx, &key);
1491
1492 _mesa_shader_cache_insert(ctx, ctx->FragmentProgram.Cache,
1493 &key, keySize, shader_program);
1494 }
1495
1496 return shader_program;
1497 }
1498
1499 }