glsl: Make a little tracking class for emitting IR lists.
[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 class texenv_fragment_program : public ir_factory {
515 public:
516 struct gl_shader_program *shader_program;
517 struct gl_shader *shader;
518 exec_list *top_instructions;
519 struct state_key *state;
520
521 ir_variable *src_texture[MAX_TEXTURE_COORD_UNITS];
522 /* Reg containing each texture unit's sampled texture color,
523 * else undef.
524 */
525
526 /* Texcoord override from bumpmapping. */
527 ir_variable *texcoord_tex[MAX_TEXTURE_COORD_UNITS];
528
529 /* Reg containing texcoord for a texture unit,
530 * needed for bump mapping, else undef.
531 */
532
533 ir_rvalue *src_previous; /**< Reg containing color from previous
534 * stage. May need to be decl'd.
535 */
536 };
537
538 static ir_rvalue *
539 get_current_attrib(struct texenv_fragment_program *p, GLuint attrib)
540 {
541 ir_variable *current;
542 ir_rvalue *val;
543
544 current = p->shader->symbols->get_variable("gl_CurrentAttribFragMESA");
545 current->max_array_access = MAX2(current->max_array_access, attrib);
546 val = new(p->mem_ctx) ir_dereference_variable(current);
547 ir_rvalue *index = new(p->mem_ctx) ir_constant(attrib);
548 return new(p->mem_ctx) ir_dereference_array(val, index);
549 }
550
551 static ir_rvalue *
552 get_gl_Color(struct texenv_fragment_program *p)
553 {
554 if (p->state->inputs_available & FRAG_BIT_COL0) {
555 ir_variable *var = p->shader->symbols->get_variable("gl_Color");
556 assert(var);
557 return new(p->mem_ctx) ir_dereference_variable(var);
558 } else {
559 return get_current_attrib(p, VERT_ATTRIB_COLOR0);
560 }
561 }
562
563 static ir_rvalue *
564 get_source(struct texenv_fragment_program *p,
565 GLuint src, GLuint unit)
566 {
567 ir_variable *var;
568 ir_dereference *deref;
569
570 switch (src) {
571 case SRC_TEXTURE:
572 return new(p->mem_ctx) ir_dereference_variable(p->src_texture[unit]);
573
574 case SRC_TEXTURE0:
575 case SRC_TEXTURE1:
576 case SRC_TEXTURE2:
577 case SRC_TEXTURE3:
578 case SRC_TEXTURE4:
579 case SRC_TEXTURE5:
580 case SRC_TEXTURE6:
581 case SRC_TEXTURE7:
582 return new(p->mem_ctx)
583 ir_dereference_variable(p->src_texture[src - SRC_TEXTURE0]);
584
585 case SRC_CONSTANT:
586 var = p->shader->symbols->get_variable("gl_TextureEnvColor");
587 assert(var);
588 deref = new(p->mem_ctx) ir_dereference_variable(var);
589 var->max_array_access = MAX2(var->max_array_access, unit);
590 return new(p->mem_ctx) ir_dereference_array(deref,
591 new(p->mem_ctx) ir_constant(unit));
592
593 case SRC_PRIMARY_COLOR:
594 var = p->shader->symbols->get_variable("gl_Color");
595 assert(var);
596 return new(p->mem_ctx) ir_dereference_variable(var);
597
598 case SRC_ZERO:
599 return new(p->mem_ctx) ir_constant(0.0f);
600
601 case SRC_PREVIOUS:
602 if (!p->src_previous) {
603 return get_gl_Color(p);
604 } else {
605 return p->src_previous->clone(p->mem_ctx, NULL);
606 }
607
608 default:
609 assert(0);
610 return NULL;
611 }
612 }
613
614 static ir_rvalue *
615 emit_combine_source(struct texenv_fragment_program *p,
616 GLuint unit,
617 GLuint source,
618 GLuint operand)
619 {
620 ir_rvalue *src;
621
622 src = get_source(p, source, unit);
623
624 switch (operand) {
625 case OPR_ONE_MINUS_SRC_COLOR:
626 return sub(new(p->mem_ctx) ir_constant(1.0f), src);
627
628 case OPR_SRC_ALPHA:
629 return src->type->is_scalar() ? src : swizzle_w(src);
630
631 case OPR_ONE_MINUS_SRC_ALPHA: {
632 ir_rvalue *const scalar = src->type->is_scalar() ? src : swizzle_w(src);
633
634 return sub(new(p->mem_ctx) ir_constant(1.0f), scalar);
635 }
636
637 case OPR_ZERO:
638 return new(p->mem_ctx) ir_constant(0.0f);
639 case OPR_ONE:
640 return new(p->mem_ctx) ir_constant(1.0f);
641 case OPR_SRC_COLOR:
642 return src;
643 default:
644 assert(0);
645 return src;
646 }
647 }
648
649 /**
650 * Check if the RGB and Alpha sources and operands match for the given
651 * texture unit's combinder state. When the RGB and A sources and
652 * operands match, we can emit fewer instructions.
653 */
654 static GLboolean args_match( const struct state_key *key, GLuint unit )
655 {
656 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
657
658 for (i = 0; i < numArgs; i++) {
659 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
660 return GL_FALSE;
661
662 switch (key->unit[unit].OptA[i].Operand) {
663 case OPR_SRC_ALPHA:
664 switch (key->unit[unit].OptRGB[i].Operand) {
665 case OPR_SRC_COLOR:
666 case OPR_SRC_ALPHA:
667 break;
668 default:
669 return GL_FALSE;
670 }
671 break;
672 case OPR_ONE_MINUS_SRC_ALPHA:
673 switch (key->unit[unit].OptRGB[i].Operand) {
674 case OPR_ONE_MINUS_SRC_COLOR:
675 case OPR_ONE_MINUS_SRC_ALPHA:
676 break;
677 default:
678 return GL_FALSE;
679 }
680 break;
681 default:
682 return GL_FALSE; /* impossible */
683 }
684 }
685
686 return GL_TRUE;
687 }
688
689 static ir_rvalue *
690 smear(struct texenv_fragment_program *p, ir_rvalue *val)
691 {
692 if (!val->type->is_scalar())
693 return val;
694
695 return swizzle_xxxx(val);
696 }
697
698 static ir_rvalue *
699 emit_combine(struct texenv_fragment_program *p,
700 GLuint unit,
701 GLuint nr,
702 GLuint mode,
703 const struct mode_opt *opt)
704 {
705 ir_rvalue *src[MAX_COMBINER_TERMS];
706 ir_rvalue *tmp0, *tmp1;
707 GLuint i;
708
709 assert(nr <= MAX_COMBINER_TERMS);
710
711 for (i = 0; i < nr; i++)
712 src[i] = emit_combine_source( p, unit, opt[i].Source, opt[i].Operand );
713
714 switch (mode) {
715 case MODE_REPLACE:
716 return src[0];
717
718 case MODE_MODULATE:
719 return mul(src[0], src[1]);
720
721 case MODE_ADD:
722 return add(src[0], src[1]);
723
724 case MODE_ADD_SIGNED:
725 return add(add(src[0], src[1]), new(p->mem_ctx) ir_constant(-0.5f));
726
727 case MODE_INTERPOLATE:
728 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
729 tmp0 = mul(src[0], src[2]);
730 tmp1 = mul(src[1], sub(new(p->mem_ctx) ir_constant(1.0f),
731 src[2]->clone(p->mem_ctx, NULL)));
732 return add(tmp0, tmp1);
733
734 case MODE_SUBTRACT:
735 return sub(src[0], src[1]);
736
737 case MODE_DOT3_RGBA:
738 case MODE_DOT3_RGBA_EXT:
739 case MODE_DOT3_RGB_EXT:
740 case MODE_DOT3_RGB: {
741 tmp0 = mul(src[0], new(p->mem_ctx) ir_constant(2.0f));
742 tmp0 = add(tmp0, new(p->mem_ctx) ir_constant(-1.0f));
743
744 tmp1 = mul(src[1], new(p->mem_ctx) ir_constant(2.0f));
745 tmp1 = add(tmp1, new(p->mem_ctx) ir_constant(-1.0f));
746
747 return dot(swizzle_xyz(smear(p, tmp0)), swizzle_xyz(smear(p, tmp1)));
748 }
749 case MODE_MODULATE_ADD_ATI:
750 return add(mul(src[0], src[2]), src[1]);
751
752 case MODE_MODULATE_SIGNED_ADD_ATI:
753 return add(add(mul(src[0], src[2]), src[1]),
754 new(p->mem_ctx) ir_constant(-0.5f));
755
756 case MODE_MODULATE_SUBTRACT_ATI:
757 return sub(mul(src[0], src[2]), src[1]);
758
759 case MODE_ADD_PRODUCTS:
760 return add(mul(src[0], src[1]), mul(src[2], src[3]));
761
762 case MODE_ADD_PRODUCTS_SIGNED:
763 return add(add(mul(src[0], src[1]), mul(src[2], src[3])),
764 new(p->mem_ctx) ir_constant(-0.5f));
765
766 case MODE_BUMP_ENVMAP_ATI:
767 /* special - not handled here */
768 assert(0);
769 return src[0];
770 default:
771 assert(0);
772 return src[0];
773 }
774 }
775
776 /**
777 * Generate instructions for one texture unit's env/combiner mode.
778 */
779 static ir_rvalue *
780 emit_texenv(struct texenv_fragment_program *p, GLuint unit)
781 {
782 const struct state_key *key = p->state;
783 GLboolean rgb_saturate, alpha_saturate;
784 GLuint rgb_shift, alpha_shift;
785
786 if (!key->unit[unit].enabled) {
787 return get_source(p, SRC_PREVIOUS, 0);
788 }
789 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
790 /* this isn't really a env stage delivering a color and handled elsewhere */
791 return get_source(p, SRC_PREVIOUS, 0);
792 }
793
794 switch (key->unit[unit].ModeRGB) {
795 case MODE_DOT3_RGB_EXT:
796 alpha_shift = key->unit[unit].ScaleShiftA;
797 rgb_shift = 0;
798 break;
799 case MODE_DOT3_RGBA_EXT:
800 alpha_shift = 0;
801 rgb_shift = 0;
802 break;
803 default:
804 rgb_shift = key->unit[unit].ScaleShiftRGB;
805 alpha_shift = key->unit[unit].ScaleShiftA;
806 break;
807 }
808
809 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
810 * We don't want to clamp twice.
811 */
812 if (rgb_shift)
813 rgb_saturate = GL_FALSE; /* saturate after rgb shift */
814 else if (need_saturate(key->unit[unit].ModeRGB))
815 rgb_saturate = GL_TRUE;
816 else
817 rgb_saturate = GL_FALSE;
818
819 if (alpha_shift)
820 alpha_saturate = GL_FALSE; /* saturate after alpha shift */
821 else if (need_saturate(key->unit[unit].ModeA))
822 alpha_saturate = GL_TRUE;
823 else
824 alpha_saturate = GL_FALSE;
825
826 ir_variable *temp_var = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
827 "texenv_combine",
828 ir_var_temporary);
829 p->emit(temp_var);
830
831 ir_dereference *deref;
832 ir_rvalue *val;
833
834 /* Emit the RGB and A combine ops
835 */
836 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
837 args_match(key, unit)) {
838 val = emit_combine(p, unit,
839 key->unit[unit].NumArgsRGB,
840 key->unit[unit].ModeRGB,
841 key->unit[unit].OptRGB);
842 val = smear(p, val);
843 if (rgb_saturate)
844 val = saturate(val);
845
846 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
847 p->emit(new(p->mem_ctx) ir_assignment(deref, val));
848 }
849 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
850 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
851 ir_rvalue *val = emit_combine(p, unit,
852 key->unit[unit].NumArgsRGB,
853 key->unit[unit].ModeRGB,
854 key->unit[unit].OptRGB);
855 val = smear(p, val);
856 if (rgb_saturate)
857 val = saturate(val);
858 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
859 p->emit(new(p->mem_ctx) ir_assignment(deref, val));
860 }
861 else {
862 /* Need to do something to stop from re-emitting identical
863 * argument calculations here:
864 */
865 val = emit_combine(p, unit,
866 key->unit[unit].NumArgsRGB,
867 key->unit[unit].ModeRGB,
868 key->unit[unit].OptRGB);
869 val = swizzle_xyz(smear(p, val));
870 if (rgb_saturate)
871 val = saturate(val);
872 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
873 p->emit(new(p->mem_ctx) ir_assignment(deref, val, NULL, WRITEMASK_XYZ));
874
875 val = emit_combine(p, unit,
876 key->unit[unit].NumArgsA,
877 key->unit[unit].ModeA,
878 key->unit[unit].OptA);
879 val = swizzle_w(smear(p, val));
880 if (alpha_saturate)
881 val = saturate(val);
882 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
883 p->emit(new(p->mem_ctx) ir_assignment(deref, val, NULL, WRITEMASK_W));
884 }
885
886 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
887
888 /* Deal with the final shift:
889 */
890 if (alpha_shift || rgb_shift) {
891 ir_constant *shift;
892
893 if (rgb_shift == alpha_shift) {
894 shift = new(p->mem_ctx) ir_constant((float)(1 << rgb_shift));
895 }
896 else {
897 float const_data[4] = {
898 1 << rgb_shift,
899 1 << rgb_shift,
900 1 << rgb_shift,
901 1 << alpha_shift
902 };
903 shift = new(p->mem_ctx) ir_constant(glsl_type::vec4_type,
904 (ir_constant_data *)const_data);
905 }
906
907 return saturate(mul(deref, shift));
908 }
909 else
910 return deref;
911 }
912
913
914 /**
915 * Generate instruction for getting a texture source term.
916 */
917 static void load_texture( struct texenv_fragment_program *p, GLuint unit )
918 {
919 ir_dereference *deref;
920
921 if (p->src_texture[unit])
922 return;
923
924 const GLuint texTarget = p->state->unit[unit].source_index;
925 ir_rvalue *texcoord;
926
927 if (!(p->state->inputs_available & (FRAG_BIT_TEX0 << unit))) {
928 texcoord = get_current_attrib(p, VERT_ATTRIB_TEX0 + unit);
929 } else if (p->texcoord_tex[unit]) {
930 texcoord = new(p->mem_ctx) ir_dereference_variable(p->texcoord_tex[unit]);
931 } else {
932 ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
933 assert(tc_array);
934 texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
935 ir_rvalue *index = new(p->mem_ctx) ir_constant(unit);
936 texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
937 tc_array->max_array_access = MAX2(tc_array->max_array_access, unit);
938 }
939
940 if (!p->state->unit[unit].enabled) {
941 p->src_texture[unit] = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
942 "dummy_tex",
943 ir_var_temporary);
944 p->emit(p->src_texture[unit]);
945
946 deref = new(p->mem_ctx) ir_dereference_variable(p->src_texture[unit]);
947 p->emit(new(p->mem_ctx) ir_assignment(deref,
948 new(p->mem_ctx) ir_constant(0.0f)));
949 return ;
950 }
951
952 const glsl_type *sampler_type = NULL;
953 int coords = 0;
954
955 switch (texTarget) {
956 case TEXTURE_1D_INDEX:
957 if (p->state->unit[unit].shadow)
958 sampler_type = p->shader->symbols->get_type("sampler1DShadow");
959 else
960 sampler_type = p->shader->symbols->get_type("sampler1D");
961 coords = 1;
962 break;
963 case TEXTURE_1D_ARRAY_INDEX:
964 if (p->state->unit[unit].shadow)
965 sampler_type = p->shader->symbols->get_type("sampler1DArrayShadow");
966 else
967 sampler_type = p->shader->symbols->get_type("sampler1DArray");
968 coords = 2;
969 break;
970 case TEXTURE_2D_INDEX:
971 if (p->state->unit[unit].shadow)
972 sampler_type = p->shader->symbols->get_type("sampler2DShadow");
973 else
974 sampler_type = p->shader->symbols->get_type("sampler2D");
975 coords = 2;
976 break;
977 case TEXTURE_2D_ARRAY_INDEX:
978 if (p->state->unit[unit].shadow)
979 sampler_type = p->shader->symbols->get_type("sampler2DArrayShadow");
980 else
981 sampler_type = p->shader->symbols->get_type("sampler2DArray");
982 coords = 3;
983 break;
984 case TEXTURE_RECT_INDEX:
985 if (p->state->unit[unit].shadow)
986 sampler_type = p->shader->symbols->get_type("sampler2DRectShadow");
987 else
988 sampler_type = p->shader->symbols->get_type("sampler2DRect");
989 coords = 2;
990 break;
991 case TEXTURE_3D_INDEX:
992 assert(!p->state->unit[unit].shadow);
993 sampler_type = p->shader->symbols->get_type("sampler3D");
994 coords = 3;
995 break;
996 case TEXTURE_CUBE_INDEX:
997 if (p->state->unit[unit].shadow)
998 sampler_type = p->shader->symbols->get_type("samplerCubeShadow");
999 else
1000 sampler_type = p->shader->symbols->get_type("samplerCube");
1001 coords = 3;
1002 break;
1003 case TEXTURE_EXTERNAL_INDEX:
1004 assert(!p->state->unit[unit].shadow);
1005 sampler_type = p->shader->symbols->get_type("samplerExternalOES");
1006 coords = 2;
1007 break;
1008 }
1009
1010 p->src_texture[unit] = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
1011 "tex",
1012 ir_var_temporary);
1013 p->emit(p->src_texture[unit]);
1014
1015 ir_texture *tex = new(p->mem_ctx) ir_texture(ir_tex);
1016
1017
1018 char *sampler_name = ralloc_asprintf(p->mem_ctx, "sampler_%d", unit);
1019 ir_variable *sampler = new(p->mem_ctx) ir_variable(sampler_type,
1020 sampler_name,
1021 ir_var_uniform);
1022 p->top_instructions->push_head(sampler);
1023 deref = new(p->mem_ctx) ir_dereference_variable(sampler);
1024 tex->set_sampler(deref, glsl_type::vec4_type);
1025
1026 tex->coordinate = new(p->mem_ctx) ir_swizzle(texcoord, 0, 1, 2, 3, coords);
1027
1028 if (p->state->unit[unit].shadow) {
1029 texcoord = texcoord->clone(p->mem_ctx, NULL);
1030 tex->shadow_comparitor = new(p->mem_ctx) ir_swizzle(texcoord,
1031 coords, 0, 0, 0,
1032 1);
1033 coords++;
1034 }
1035
1036 texcoord = texcoord->clone(p->mem_ctx, NULL);
1037 tex->projector = swizzle_w(texcoord);
1038
1039 deref = new(p->mem_ctx) ir_dereference_variable(p->src_texture[unit]);
1040 p->emit(new(p->mem_ctx) ir_assignment(deref, tex));
1041 }
1042
1043 static void
1044 load_texenv_source(struct texenv_fragment_program *p,
1045 GLuint src, GLuint unit)
1046 {
1047 switch (src) {
1048 case SRC_TEXTURE:
1049 load_texture(p, unit);
1050 break;
1051
1052 case SRC_TEXTURE0:
1053 case SRC_TEXTURE1:
1054 case SRC_TEXTURE2:
1055 case SRC_TEXTURE3:
1056 case SRC_TEXTURE4:
1057 case SRC_TEXTURE5:
1058 case SRC_TEXTURE6:
1059 case SRC_TEXTURE7:
1060 load_texture(p, src - SRC_TEXTURE0);
1061 break;
1062
1063 default:
1064 /* not a texture src - do nothing */
1065 break;
1066 }
1067 }
1068
1069
1070 /**
1071 * Generate instructions for loading all texture source terms.
1072 */
1073 static GLboolean
1074 load_texunit_sources( struct texenv_fragment_program *p, GLuint unit )
1075 {
1076 const struct state_key *key = p->state;
1077 GLuint i;
1078
1079 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1080 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
1081 }
1082
1083 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1084 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1085 }
1086
1087 return GL_TRUE;
1088 }
1089
1090 /**
1091 * Generate instructions for loading bump map textures.
1092 */
1093 static void
1094 load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
1095 {
1096 const struct state_key *key = p->state;
1097 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1098 ir_rvalue *bump;
1099 ir_rvalue *texcoord;
1100 ir_variable *rot_mat_0, *rot_mat_1;
1101
1102 rot_mat_0 = p->shader->symbols->get_variable("gl_BumpRotMatrix0MESA");
1103 rot_mat_1 = p->shader->symbols->get_variable("gl_BumpRotMatrix1MESA");
1104
1105 ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
1106 assert(tc_array);
1107 texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
1108 ir_rvalue *index = new(p->mem_ctx) ir_constant(bumpedUnitNr);
1109 texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
1110 tc_array->max_array_access = MAX2(tc_array->max_array_access, unit);
1111
1112 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1113
1114 /* Apply rot matrix and add coords to be available in next phase.
1115 * dest = Arg1 + (Arg0.xx * rotMat0) + (Arg0.yy * rotMat1)
1116 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1117 */
1118 ir_dereference *deref;
1119 ir_rvalue *bump_x, *bump_y;
1120
1121 texcoord = smear(p, texcoord);
1122
1123 /* bump_texcoord = texcoord */
1124 ir_variable *bumped = new(p->mem_ctx) ir_variable(texcoord->type,
1125 "bump_texcoord",
1126 ir_var_temporary);
1127 p->emit(bumped);
1128
1129 deref = new(p->mem_ctx) ir_dereference_variable(bumped);
1130 p->emit(new(p->mem_ctx) ir_assignment(deref, texcoord));
1131
1132 /* bump_texcoord.xy += arg0.x * rotmat0 + arg0.y * rotmat1 */
1133 bump = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1134 bump_x = mul(swizzle_x(bump), rot_mat_0);
1135 bump_y = mul(swizzle_y(bump->clone(p->mem_ctx, NULL)), rot_mat_1);
1136
1137 ir_expression *expr;
1138
1139 expr = add(swizzle_xy(bumped), add(bump_x, bump_y));
1140
1141 deref = new(p->mem_ctx) ir_dereference_variable(bumped);
1142 p->emit(new(p->mem_ctx) ir_assignment(deref, expr, NULL, WRITEMASK_XY));
1143
1144 p->texcoord_tex[bumpedUnitNr] = bumped;
1145 }
1146
1147 /**
1148 * Applies the fog calculations.
1149 *
1150 * This is basically like the ARB_fragment_prorgam fog options. Note
1151 * that ffvertex_prog.c produces fogcoord for us when
1152 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1153 */
1154 static ir_rvalue *
1155 emit_fog_instructions(struct texenv_fragment_program *p,
1156 ir_rvalue *fragcolor)
1157 {
1158 struct state_key *key = p->state;
1159 ir_rvalue *f, *temp;
1160 ir_variable *params, *oparams;
1161 ir_variable *fogcoord;
1162
1163 /* Temporary storage for the whole fog result. Fog calculations
1164 * only affect rgb so we're hanging on to the .a value of fragcolor
1165 * this way.
1166 */
1167 ir_variable *fog_result = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
1168 "fog_result",
1169 ir_var_auto);
1170 p->emit(fog_result);
1171 temp = new(p->mem_ctx) ir_dereference_variable(fog_result);
1172 p->emit(new(p->mem_ctx) ir_assignment(temp, fragcolor));
1173
1174 fragcolor = swizzle_xyz(fog_result);
1175
1176 oparams = p->shader->symbols->get_variable("gl_FogParamsOptimizedMESA");
1177 fogcoord = p->shader->symbols->get_variable("gl_FogFragCoord");
1178 params = p->shader->symbols->get_variable("gl_Fog");
1179 f = new(p->mem_ctx) ir_dereference_variable(fogcoord);
1180
1181 ir_variable *f_var = new(p->mem_ctx) ir_variable(glsl_type::float_type,
1182 "fog_factor", ir_var_auto);
1183 p->emit(f_var);
1184
1185 switch (key->fog_mode) {
1186 case FOG_LINEAR:
1187 /* f = (end - z) / (end - start)
1188 *
1189 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1190 * (end / (end - start)) so we can generate a single MAD.
1191 */
1192 f = add(mul(f, swizzle_x(oparams)), swizzle_y(oparams));
1193 break;
1194 case FOG_EXP:
1195 /* f = e^(-(density * fogcoord))
1196 *
1197 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1198 * use EXP2 which is generally the native instruction without
1199 * having to do any further math on the fog density uniform.
1200 */
1201 f = mul(f, swizzle_z(oparams));
1202 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
1203 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
1204 break;
1205 case FOG_EXP2:
1206 /* f = e^(-(density * fogcoord)^2)
1207 *
1208 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1209 * can do this like FOG_EXP but with a squaring after the
1210 * multiply by density.
1211 */
1212 ir_variable *temp_var = new(p->mem_ctx) ir_variable(glsl_type::float_type,
1213 "fog_temp",
1214 ir_var_auto);
1215 p->emit(temp_var);
1216
1217 f = mul(f, swizzle_w(oparams));
1218
1219 temp = new(p->mem_ctx) ir_dereference_variable(temp_var);
1220 p->emit(new(p->mem_ctx) ir_assignment(temp, f));
1221
1222 f = mul(temp_var, temp_var);
1223 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
1224 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
1225 break;
1226 }
1227
1228 f = saturate(f);
1229
1230 temp = new(p->mem_ctx) ir_dereference_variable(f_var);
1231 p->emit(new(p->mem_ctx) ir_assignment(temp, f));
1232
1233 f = sub(new(p->mem_ctx) ir_constant(1.0f), f_var);
1234 temp = new(p->mem_ctx) ir_dereference_variable(params);
1235 temp = new(p->mem_ctx) ir_dereference_record(temp, "color");
1236 temp = mul(swizzle_xyz(temp), f);
1237
1238 f = add(temp, mul(fragcolor, f_var));
1239
1240 ir_dereference *deref = new(p->mem_ctx) ir_dereference_variable(fog_result);
1241 p->emit(new(p->mem_ctx) ir_assignment(deref, f, NULL, WRITEMASK_XYZ));
1242
1243 return new(p->mem_ctx) ir_dereference_variable(fog_result);
1244 }
1245
1246 static void
1247 emit_instructions(struct texenv_fragment_program *p)
1248 {
1249 struct state_key *key = p->state;
1250 GLuint unit;
1251
1252 if (key->enabled_units) {
1253 /* Zeroth pass - bump map textures first */
1254 for (unit = 0; unit < key->nr_enabled_units; unit++) {
1255 if (key->unit[unit].enabled &&
1256 key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1257 load_texunit_bumpmap(p, unit);
1258 }
1259 }
1260
1261 /* First pass - to support texture_env_crossbar, first identify
1262 * all referenced texture sources and emit texld instructions
1263 * for each:
1264 */
1265 for (unit = 0; unit < key->nr_enabled_units; unit++)
1266 if (key->unit[unit].enabled) {
1267 load_texunit_sources(p, unit);
1268 }
1269
1270 /* Second pass - emit combine instructions to build final color:
1271 */
1272 for (unit = 0; unit < key->nr_enabled_units; unit++) {
1273 if (key->unit[unit].enabled) {
1274 p->src_previous = emit_texenv(p, unit);
1275 }
1276 }
1277 }
1278
1279 ir_rvalue *cf = get_source(p, SRC_PREVIOUS, 0);
1280 ir_dereference_variable *deref;
1281
1282 if (key->separate_specular) {
1283 ir_rvalue *tmp0;
1284 ir_variable *spec_result = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
1285 "specular_add",
1286 ir_var_temporary);
1287
1288 p->emit(spec_result);
1289
1290 deref = new(p->mem_ctx) ir_dereference_variable(spec_result);
1291 p->emit(new(p->mem_ctx) ir_assignment(deref, cf));
1292
1293 ir_rvalue *secondary;
1294 if (p->state->inputs_available & FRAG_BIT_COL1) {
1295 ir_variable *var =
1296 p->shader->symbols->get_variable("gl_SecondaryColor");
1297 assert(var);
1298 secondary = swizzle_xyz(var);
1299 } else {
1300 secondary = swizzle_xyz(get_current_attrib(p, VERT_ATTRIB_COLOR1));
1301 }
1302
1303 tmp0 = add(swizzle_xyz(spec_result), secondary);
1304
1305 deref = new(p->mem_ctx) ir_dereference_variable(spec_result);
1306 p->emit(new(p->mem_ctx) ir_assignment(deref, tmp0, NULL, WRITEMASK_XYZ));
1307
1308 cf = new(p->mem_ctx) ir_dereference_variable(spec_result);
1309 }
1310
1311 if (key->fog_enabled) {
1312 cf = emit_fog_instructions(p, cf);
1313 }
1314
1315 ir_variable *frag_color = p->shader->symbols->get_variable("gl_FragColor");
1316 assert(frag_color);
1317 deref = new(p->mem_ctx) ir_dereference_variable(frag_color);
1318 p->emit(new(p->mem_ctx) ir_assignment(deref, cf));
1319 }
1320
1321 /**
1322 * Generate a new fragment program which implements the context's
1323 * current texture env/combine mode.
1324 */
1325 static struct gl_shader_program *
1326 create_new_program(struct gl_context *ctx, struct state_key *key)
1327 {
1328 texenv_fragment_program p;
1329 unsigned int unit;
1330 _mesa_glsl_parse_state *state;
1331
1332 p.mem_ctx = ralloc_context(NULL);
1333 p.shader = ctx->Driver.NewShader(ctx, 0, GL_FRAGMENT_SHADER);
1334 p.shader->ir = new(p.shader) exec_list;
1335 state = new(p.shader) _mesa_glsl_parse_state(ctx, GL_FRAGMENT_SHADER,
1336 p.shader);
1337 p.shader->symbols = state->symbols;
1338 p.top_instructions = p.shader->ir;
1339 p.instructions = p.shader->ir;
1340 p.state = key;
1341 p.shader_program = ctx->Driver.NewShaderProgram(ctx, 0);
1342
1343 /* Tell the linker to ignore the fact that we're building a
1344 * separate shader, in case we're in a GLES2 context that would
1345 * normally reject that. The real problem is that we're building a
1346 * fixed function program in a GLES2 context at all, but that's a
1347 * big mess to clean up.
1348 */
1349 p.shader_program->InternalSeparateShader = GL_TRUE;
1350
1351 state->language_version = 130;
1352 if (ctx->Extensions.OES_EGL_image_external)
1353 state->OES_EGL_image_external_enable = true;
1354 _mesa_glsl_initialize_types(state);
1355 _mesa_glsl_initialize_variables(p.instructions, state);
1356
1357 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
1358 p.src_texture[unit] = NULL;
1359 p.texcoord_tex[unit] = NULL;
1360 }
1361
1362 p.src_previous = NULL;
1363
1364 ir_function *main_f = new(p.mem_ctx) ir_function("main");
1365 p.emit(main_f);
1366 state->symbols->add_function(main_f);
1367
1368 ir_function_signature *main_sig =
1369 new(p.mem_ctx) ir_function_signature(p.shader->symbols->get_type("void"));
1370 main_sig->is_defined = true;
1371 main_f->add_signature(main_sig);
1372
1373 p.instructions = &main_sig->body;
1374 if (key->num_draw_buffers)
1375 emit_instructions(&p);
1376
1377 validate_ir_tree(p.shader->ir);
1378
1379 while (do_common_optimization(p.shader->ir, false, false, 32))
1380 ;
1381 reparent_ir(p.shader->ir, p.shader->ir);
1382
1383 p.shader->CompileStatus = true;
1384 p.shader->Version = state->language_version;
1385 p.shader->num_builtins_to_link = state->num_builtins_to_link;
1386 p.shader_program->Shaders =
1387 (gl_shader **)malloc(sizeof(*p.shader_program->Shaders));
1388 p.shader_program->Shaders[0] = p.shader;
1389 p.shader_program->NumShaders = 1;
1390
1391 _mesa_glsl_link_shader(ctx, p.shader_program);
1392
1393 /* Set the sampler uniforms, and relink to get them into the linked
1394 * program.
1395 */
1396 struct gl_shader *const fs =
1397 p.shader_program->_LinkedShaders[MESA_SHADER_FRAGMENT];
1398 struct gl_program *const fp = fs->Program;
1399
1400 _mesa_generate_parameters_list_for_uniforms(p.shader_program, fs,
1401 fp->Parameters);
1402
1403 _mesa_associate_uniform_storage(ctx, p.shader_program, fp->Parameters);
1404
1405 for (unsigned int i = 0; i < MAX_TEXTURE_UNITS; i++) {
1406 /* Enough space for 'sampler_999\0'.
1407 */
1408 char name[12];
1409
1410 snprintf(name, sizeof(name), "sampler_%d", i);
1411
1412 int loc = _mesa_get_uniform_location(ctx, p.shader_program, name);
1413 if (loc != -1) {
1414 unsigned base;
1415 unsigned idx;
1416
1417 /* Avoid using _mesa_uniform() because it flags state
1418 * updates, so if we're generating this shader_program in a
1419 * state update, we end up recursing. Instead, just set the
1420 * value, which is picked up at re-link.
1421 */
1422 _mesa_uniform_split_location_offset(loc, &base, &idx);
1423 assert(idx == 0);
1424
1425 struct gl_uniform_storage *const storage =
1426 &p.shader_program->UniformStorage[base];
1427
1428 /* Update the storage, the SamplerUnits in the shader program, and
1429 * the SamplerUnits in the assembly shader.
1430 */
1431 storage->storage[idx].i = i;
1432 fp->SamplerUnits[storage->sampler] = i;
1433 p.shader_program->SamplerUnits[storage->sampler] = i;
1434 _mesa_propagate_uniforms_to_driver_storage(storage, 0, 1);
1435 }
1436 }
1437 _mesa_update_shader_textures_used(p.shader_program, fp);
1438 (void) ctx->Driver.ProgramStringNotify(ctx, fp->Target, fp);
1439
1440 if (!p.shader_program->LinkStatus)
1441 _mesa_problem(ctx, "Failed to link fixed function fragment shader: %s\n",
1442 p.shader_program->InfoLog);
1443
1444 ralloc_free(p.mem_ctx);
1445 return p.shader_program;
1446 }
1447
1448 extern "C" {
1449
1450 /**
1451 * Return a fragment program which implements the current
1452 * fixed-function texture, fog and color-sum operations.
1453 */
1454 struct gl_shader_program *
1455 _mesa_get_fixed_func_fragment_program(struct gl_context *ctx)
1456 {
1457 struct gl_shader_program *shader_program;
1458 struct state_key key;
1459 GLuint keySize;
1460
1461 keySize = make_state_key(ctx, &key);
1462
1463 shader_program = (struct gl_shader_program *)
1464 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1465 &key, keySize);
1466
1467 if (!shader_program) {
1468 shader_program = create_new_program(ctx, &key);
1469
1470 _mesa_shader_cache_insert(ctx, ctx->FragmentProgram.Cache,
1471 &key, keySize, shader_program);
1472 }
1473
1474 return shader_program;
1475 }
1476
1477 }