mesa: move some format helper functions to glformats.c
[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_VARYING_VP_INPUTS */
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 = p->make_temp(glsl_type::vec4_type, "texenv_combine");
827 ir_dereference *deref;
828 ir_rvalue *val;
829
830 /* Emit the RGB and A combine ops
831 */
832 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
833 args_match(key, unit)) {
834 val = emit_combine(p, unit,
835 key->unit[unit].NumArgsRGB,
836 key->unit[unit].ModeRGB,
837 key->unit[unit].OptRGB);
838 val = smear(p, val);
839 if (rgb_saturate)
840 val = saturate(val);
841
842 p->emit(assign(temp_var, val));
843 }
844 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
845 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
846 ir_rvalue *val = emit_combine(p, unit,
847 key->unit[unit].NumArgsRGB,
848 key->unit[unit].ModeRGB,
849 key->unit[unit].OptRGB);
850 val = smear(p, val);
851 if (rgb_saturate)
852 val = saturate(val);
853 p->emit(assign(temp_var, val));
854 }
855 else {
856 /* Need to do something to stop from re-emitting identical
857 * argument calculations here:
858 */
859 val = emit_combine(p, unit,
860 key->unit[unit].NumArgsRGB,
861 key->unit[unit].ModeRGB,
862 key->unit[unit].OptRGB);
863 val = swizzle_xyz(smear(p, val));
864 if (rgb_saturate)
865 val = saturate(val);
866 p->emit(assign(temp_var, val, WRITEMASK_XYZ));
867
868 val = emit_combine(p, unit,
869 key->unit[unit].NumArgsA,
870 key->unit[unit].ModeA,
871 key->unit[unit].OptA);
872 val = swizzle_w(smear(p, val));
873 if (alpha_saturate)
874 val = saturate(val);
875 p->emit(assign(temp_var, val, WRITEMASK_W));
876 }
877
878 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
879
880 /* Deal with the final shift:
881 */
882 if (alpha_shift || rgb_shift) {
883 ir_constant *shift;
884
885 if (rgb_shift == alpha_shift) {
886 shift = new(p->mem_ctx) ir_constant((float)(1 << rgb_shift));
887 }
888 else {
889 float const_data[4] = {
890 1 << rgb_shift,
891 1 << rgb_shift,
892 1 << rgb_shift,
893 1 << alpha_shift
894 };
895 shift = new(p->mem_ctx) ir_constant(glsl_type::vec4_type,
896 (ir_constant_data *)const_data);
897 }
898
899 return saturate(mul(deref, shift));
900 }
901 else
902 return deref;
903 }
904
905
906 /**
907 * Generate instruction for getting a texture source term.
908 */
909 static void load_texture( struct texenv_fragment_program *p, GLuint unit )
910 {
911 ir_dereference *deref;
912
913 if (p->src_texture[unit])
914 return;
915
916 const GLuint texTarget = p->state->unit[unit].source_index;
917 ir_rvalue *texcoord;
918
919 if (!(p->state->inputs_available & (FRAG_BIT_TEX0 << unit))) {
920 texcoord = get_current_attrib(p, VERT_ATTRIB_TEX0 + unit);
921 } else if (p->texcoord_tex[unit]) {
922 texcoord = new(p->mem_ctx) ir_dereference_variable(p->texcoord_tex[unit]);
923 } else {
924 ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
925 assert(tc_array);
926 texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
927 ir_rvalue *index = new(p->mem_ctx) ir_constant(unit);
928 texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
929 tc_array->max_array_access = MAX2(tc_array->max_array_access, unit);
930 }
931
932 if (!p->state->unit[unit].enabled) {
933 p->src_texture[unit] = p->make_temp(glsl_type::vec4_type,
934 "dummy_tex");
935 p->emit(p->src_texture[unit]);
936
937 p->emit(assign(p->src_texture[unit], new(p->mem_ctx) ir_constant(0.0f)));
938 return ;
939 }
940
941 const glsl_type *sampler_type = NULL;
942 int coords = 0;
943
944 switch (texTarget) {
945 case TEXTURE_1D_INDEX:
946 if (p->state->unit[unit].shadow)
947 sampler_type = p->shader->symbols->get_type("sampler1DShadow");
948 else
949 sampler_type = p->shader->symbols->get_type("sampler1D");
950 coords = 1;
951 break;
952 case TEXTURE_1D_ARRAY_INDEX:
953 if (p->state->unit[unit].shadow)
954 sampler_type = p->shader->symbols->get_type("sampler1DArrayShadow");
955 else
956 sampler_type = p->shader->symbols->get_type("sampler1DArray");
957 coords = 2;
958 break;
959 case TEXTURE_2D_INDEX:
960 if (p->state->unit[unit].shadow)
961 sampler_type = p->shader->symbols->get_type("sampler2DShadow");
962 else
963 sampler_type = p->shader->symbols->get_type("sampler2D");
964 coords = 2;
965 break;
966 case TEXTURE_2D_ARRAY_INDEX:
967 if (p->state->unit[unit].shadow)
968 sampler_type = p->shader->symbols->get_type("sampler2DArrayShadow");
969 else
970 sampler_type = p->shader->symbols->get_type("sampler2DArray");
971 coords = 3;
972 break;
973 case TEXTURE_RECT_INDEX:
974 if (p->state->unit[unit].shadow)
975 sampler_type = p->shader->symbols->get_type("sampler2DRectShadow");
976 else
977 sampler_type = p->shader->symbols->get_type("sampler2DRect");
978 coords = 2;
979 break;
980 case TEXTURE_3D_INDEX:
981 assert(!p->state->unit[unit].shadow);
982 sampler_type = p->shader->symbols->get_type("sampler3D");
983 coords = 3;
984 break;
985 case TEXTURE_CUBE_INDEX:
986 if (p->state->unit[unit].shadow)
987 sampler_type = p->shader->symbols->get_type("samplerCubeShadow");
988 else
989 sampler_type = p->shader->symbols->get_type("samplerCube");
990 coords = 3;
991 break;
992 case TEXTURE_EXTERNAL_INDEX:
993 assert(!p->state->unit[unit].shadow);
994 sampler_type = p->shader->symbols->get_type("samplerExternalOES");
995 coords = 2;
996 break;
997 }
998
999 p->src_texture[unit] = p->make_temp(glsl_type::vec4_type,
1000 "tex");
1001
1002 ir_texture *tex = new(p->mem_ctx) ir_texture(ir_tex);
1003
1004
1005 char *sampler_name = ralloc_asprintf(p->mem_ctx, "sampler_%d", unit);
1006 ir_variable *sampler = new(p->mem_ctx) ir_variable(sampler_type,
1007 sampler_name,
1008 ir_var_uniform);
1009 p->top_instructions->push_head(sampler);
1010
1011 /* Set the texture unit for this sampler. The linker will pick this value
1012 * up and do-the-right-thing.
1013 *
1014 * NOTE: The cast to int is important. Without it, the constant will have
1015 * type uint, and things later on may get confused.
1016 */
1017 sampler->constant_value = new(p->mem_ctx) ir_constant(int(unit));
1018
1019 deref = new(p->mem_ctx) ir_dereference_variable(sampler);
1020 tex->set_sampler(deref, glsl_type::vec4_type);
1021
1022 tex->coordinate = new(p->mem_ctx) ir_swizzle(texcoord, 0, 1, 2, 3, coords);
1023
1024 if (p->state->unit[unit].shadow) {
1025 texcoord = texcoord->clone(p->mem_ctx, NULL);
1026 tex->shadow_comparitor = new(p->mem_ctx) ir_swizzle(texcoord,
1027 coords, 0, 0, 0,
1028 1);
1029 coords++;
1030 }
1031
1032 texcoord = texcoord->clone(p->mem_ctx, NULL);
1033 tex->projector = swizzle_w(texcoord);
1034
1035 p->emit(assign(p->src_texture[unit], tex));
1036 }
1037
1038 static void
1039 load_texenv_source(struct texenv_fragment_program *p,
1040 GLuint src, GLuint unit)
1041 {
1042 switch (src) {
1043 case SRC_TEXTURE:
1044 load_texture(p, unit);
1045 break;
1046
1047 case SRC_TEXTURE0:
1048 case SRC_TEXTURE1:
1049 case SRC_TEXTURE2:
1050 case SRC_TEXTURE3:
1051 case SRC_TEXTURE4:
1052 case SRC_TEXTURE5:
1053 case SRC_TEXTURE6:
1054 case SRC_TEXTURE7:
1055 load_texture(p, src - SRC_TEXTURE0);
1056 break;
1057
1058 default:
1059 /* not a texture src - do nothing */
1060 break;
1061 }
1062 }
1063
1064
1065 /**
1066 * Generate instructions for loading all texture source terms.
1067 */
1068 static GLboolean
1069 load_texunit_sources( struct texenv_fragment_program *p, GLuint unit )
1070 {
1071 const struct state_key *key = p->state;
1072 GLuint i;
1073
1074 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1075 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
1076 }
1077
1078 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1079 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1080 }
1081
1082 return GL_TRUE;
1083 }
1084
1085 /**
1086 * Generate instructions for loading bump map textures.
1087 */
1088 static void
1089 load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
1090 {
1091 const struct state_key *key = p->state;
1092 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1093 ir_rvalue *bump;
1094 ir_rvalue *texcoord;
1095 ir_variable *rot_mat_0, *rot_mat_1;
1096
1097 rot_mat_0 = p->shader->symbols->get_variable("gl_BumpRotMatrix0MESA");
1098 rot_mat_1 = p->shader->symbols->get_variable("gl_BumpRotMatrix1MESA");
1099
1100 ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
1101 assert(tc_array);
1102 texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
1103 ir_rvalue *index = new(p->mem_ctx) ir_constant(bumpedUnitNr);
1104 texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
1105 tc_array->max_array_access = MAX2(tc_array->max_array_access, unit);
1106
1107 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1108
1109 /* Apply rot matrix and add coords to be available in next phase.
1110 * dest = Arg1 + (Arg0.xx * rotMat0) + (Arg0.yy * rotMat1)
1111 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1112 */
1113 ir_rvalue *bump_x, *bump_y;
1114
1115 texcoord = smear(p, texcoord);
1116
1117 /* bump_texcoord = texcoord */
1118 ir_variable *bumped = p->make_temp(texcoord->type, "bump_texcoord");
1119 p->emit(bumped);
1120 p->emit(assign(bumped, texcoord));
1121
1122 /* bump_texcoord.xy += arg0.x * rotmat0 + arg0.y * rotmat1 */
1123 bump = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1124 bump_x = mul(swizzle_x(bump), rot_mat_0);
1125 bump_y = mul(swizzle_y(bump->clone(p->mem_ctx, NULL)), rot_mat_1);
1126
1127 p->emit(assign(bumped, add(swizzle_xy(bumped), add(bump_x, bump_y)),
1128 WRITEMASK_XY));
1129
1130 p->texcoord_tex[bumpedUnitNr] = bumped;
1131 }
1132
1133 /**
1134 * Applies the fog calculations.
1135 *
1136 * This is basically like the ARB_fragment_prorgam fog options. Note
1137 * that ffvertex_prog.c produces fogcoord for us when
1138 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1139 */
1140 static ir_rvalue *
1141 emit_fog_instructions(struct texenv_fragment_program *p,
1142 ir_rvalue *fragcolor)
1143 {
1144 struct state_key *key = p->state;
1145 ir_rvalue *f, *temp;
1146 ir_variable *params, *oparams;
1147 ir_variable *fogcoord;
1148
1149 /* Temporary storage for the whole fog result. Fog calculations
1150 * only affect rgb so we're hanging on to the .a value of fragcolor
1151 * this way.
1152 */
1153 ir_variable *fog_result = p->make_temp(glsl_type::vec4_type, "fog_result");
1154 p->emit(assign(fog_result, fragcolor));
1155
1156 fragcolor = swizzle_xyz(fog_result);
1157
1158 oparams = p->shader->symbols->get_variable("gl_FogParamsOptimizedMESA");
1159 fogcoord = p->shader->symbols->get_variable("gl_FogFragCoord");
1160 params = p->shader->symbols->get_variable("gl_Fog");
1161 f = new(p->mem_ctx) ir_dereference_variable(fogcoord);
1162
1163 ir_variable *f_var = p->make_temp(glsl_type::float_type, "fog_factor");
1164
1165 switch (key->fog_mode) {
1166 case FOG_LINEAR:
1167 /* f = (end - z) / (end - start)
1168 *
1169 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1170 * (end / (end - start)) so we can generate a single MAD.
1171 */
1172 f = add(mul(f, swizzle_x(oparams)), swizzle_y(oparams));
1173 break;
1174 case FOG_EXP:
1175 /* f = e^(-(density * fogcoord))
1176 *
1177 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1178 * use EXP2 which is generally the native instruction without
1179 * having to do any further math on the fog density uniform.
1180 */
1181 f = mul(f, swizzle_z(oparams));
1182 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
1183 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
1184 break;
1185 case FOG_EXP2:
1186 /* f = e^(-(density * fogcoord)^2)
1187 *
1188 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1189 * can do this like FOG_EXP but with a squaring after the
1190 * multiply by density.
1191 */
1192 ir_variable *temp_var = p->make_temp(glsl_type::float_type, "fog_temp");
1193 p->emit(assign(temp_var, mul(f, swizzle_w(oparams))));
1194
1195 f = mul(temp_var, temp_var);
1196 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
1197 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
1198 break;
1199 }
1200
1201 p->emit(assign(f_var, saturate(f)));
1202
1203 f = sub(new(p->mem_ctx) ir_constant(1.0f), f_var);
1204 temp = new(p->mem_ctx) ir_dereference_variable(params);
1205 temp = new(p->mem_ctx) ir_dereference_record(temp, "color");
1206 temp = mul(swizzle_xyz(temp), f);
1207
1208 p->emit(assign(fog_result, add(temp, mul(fragcolor, f_var)), WRITEMASK_XYZ));
1209
1210 return new(p->mem_ctx) ir_dereference_variable(fog_result);
1211 }
1212
1213 static void
1214 emit_instructions(struct texenv_fragment_program *p)
1215 {
1216 struct state_key *key = p->state;
1217 GLuint unit;
1218
1219 if (key->enabled_units) {
1220 /* Zeroth pass - bump map textures first */
1221 for (unit = 0; unit < key->nr_enabled_units; unit++) {
1222 if (key->unit[unit].enabled &&
1223 key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1224 load_texunit_bumpmap(p, unit);
1225 }
1226 }
1227
1228 /* First pass - to support texture_env_crossbar, first identify
1229 * all referenced texture sources and emit texld instructions
1230 * for each:
1231 */
1232 for (unit = 0; unit < key->nr_enabled_units; unit++)
1233 if (key->unit[unit].enabled) {
1234 load_texunit_sources(p, unit);
1235 }
1236
1237 /* Second pass - emit combine instructions to build final color:
1238 */
1239 for (unit = 0; unit < key->nr_enabled_units; unit++) {
1240 if (key->unit[unit].enabled) {
1241 p->src_previous = emit_texenv(p, unit);
1242 }
1243 }
1244 }
1245
1246 ir_rvalue *cf = get_source(p, SRC_PREVIOUS, 0);
1247
1248 if (key->separate_specular) {
1249 ir_variable *spec_result = p->make_temp(glsl_type::vec4_type,
1250 "specular_add");
1251 p->emit(assign(spec_result, cf));
1252
1253 ir_rvalue *secondary;
1254 if (p->state->inputs_available & FRAG_BIT_COL1) {
1255 ir_variable *var =
1256 p->shader->symbols->get_variable("gl_SecondaryColor");
1257 assert(var);
1258 secondary = swizzle_xyz(var);
1259 } else {
1260 secondary = swizzle_xyz(get_current_attrib(p, VERT_ATTRIB_COLOR1));
1261 }
1262
1263 p->emit(assign(spec_result, add(swizzle_xyz(spec_result), secondary),
1264 WRITEMASK_XYZ));
1265
1266 cf = new(p->mem_ctx) ir_dereference_variable(spec_result);
1267 }
1268
1269 if (key->fog_enabled) {
1270 cf = emit_fog_instructions(p, cf);
1271 }
1272
1273 ir_variable *frag_color = p->shader->symbols->get_variable("gl_FragColor");
1274 assert(frag_color);
1275 p->emit(assign(frag_color, cf));
1276 }
1277
1278 /**
1279 * Generate a new fragment program which implements the context's
1280 * current texture env/combine mode.
1281 */
1282 static struct gl_shader_program *
1283 create_new_program(struct gl_context *ctx, struct state_key *key)
1284 {
1285 texenv_fragment_program p;
1286 unsigned int unit;
1287 _mesa_glsl_parse_state *state;
1288
1289 p.mem_ctx = ralloc_context(NULL);
1290 p.shader = ctx->Driver.NewShader(ctx, 0, GL_FRAGMENT_SHADER);
1291 p.shader->ir = new(p.shader) exec_list;
1292 state = new(p.shader) _mesa_glsl_parse_state(ctx, GL_FRAGMENT_SHADER,
1293 p.shader);
1294 p.shader->symbols = state->symbols;
1295 p.top_instructions = p.shader->ir;
1296 p.instructions = p.shader->ir;
1297 p.state = key;
1298 p.shader_program = ctx->Driver.NewShaderProgram(ctx, 0);
1299
1300 /* Tell the linker to ignore the fact that we're building a
1301 * separate shader, in case we're in a GLES2 context that would
1302 * normally reject that. The real problem is that we're building a
1303 * fixed function program in a GLES2 context at all, but that's a
1304 * big mess to clean up.
1305 */
1306 p.shader_program->InternalSeparateShader = GL_TRUE;
1307
1308 state->language_version = 130;
1309 if (ctx->Extensions.OES_EGL_image_external)
1310 state->OES_EGL_image_external_enable = true;
1311 _mesa_glsl_initialize_types(state);
1312 _mesa_glsl_initialize_variables(p.instructions, state);
1313
1314 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
1315 p.src_texture[unit] = NULL;
1316 p.texcoord_tex[unit] = NULL;
1317 }
1318
1319 p.src_previous = NULL;
1320
1321 ir_function *main_f = new(p.mem_ctx) ir_function("main");
1322 p.emit(main_f);
1323 state->symbols->add_function(main_f);
1324
1325 ir_function_signature *main_sig =
1326 new(p.mem_ctx) ir_function_signature(p.shader->symbols->get_type("void"));
1327 main_sig->is_defined = true;
1328 main_f->add_signature(main_sig);
1329
1330 p.instructions = &main_sig->body;
1331 if (key->num_draw_buffers)
1332 emit_instructions(&p);
1333
1334 validate_ir_tree(p.shader->ir);
1335
1336 while (do_common_optimization(p.shader->ir, false, false, 32))
1337 ;
1338 reparent_ir(p.shader->ir, p.shader->ir);
1339
1340 p.shader->CompileStatus = true;
1341 p.shader->Version = state->language_version;
1342 p.shader->num_builtins_to_link = state->num_builtins_to_link;
1343 p.shader_program->Shaders =
1344 (gl_shader **)malloc(sizeof(*p.shader_program->Shaders));
1345 p.shader_program->Shaders[0] = p.shader;
1346 p.shader_program->NumShaders = 1;
1347
1348 _mesa_glsl_link_shader(ctx, p.shader_program);
1349
1350 /* Set the sampler uniforms, and relink to get them into the linked
1351 * program.
1352 */
1353 struct gl_shader *const fs =
1354 p.shader_program->_LinkedShaders[MESA_SHADER_FRAGMENT];
1355 struct gl_program *const fp = fs->Program;
1356
1357 _mesa_generate_parameters_list_for_uniforms(p.shader_program, fs,
1358 fp->Parameters);
1359
1360 _mesa_associate_uniform_storage(ctx, p.shader_program, fp->Parameters);
1361
1362 _mesa_update_shader_textures_used(p.shader_program, fp);
1363 (void) ctx->Driver.ProgramStringNotify(ctx, fp->Target, fp);
1364
1365 if (!p.shader_program->LinkStatus)
1366 _mesa_problem(ctx, "Failed to link fixed function fragment shader: %s\n",
1367 p.shader_program->InfoLog);
1368
1369 ralloc_free(p.mem_ctx);
1370 return p.shader_program;
1371 }
1372
1373 extern "C" {
1374
1375 /**
1376 * Return a fragment program which implements the current
1377 * fixed-function texture, fog and color-sum operations.
1378 */
1379 struct gl_shader_program *
1380 _mesa_get_fixed_func_fragment_program(struct gl_context *ctx)
1381 {
1382 struct gl_shader_program *shader_program;
1383 struct state_key key;
1384 GLuint keySize;
1385
1386 keySize = make_state_key(ctx, &key);
1387
1388 shader_program = (struct gl_shader_program *)
1389 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1390 &key, keySize);
1391
1392 if (!shader_program) {
1393 shader_program = create_new_program(ctx, &key);
1394
1395 _mesa_shader_cache_insert(ctx, ctx->FragmentProgram.Cache,
1396 &key, keySize, shader_program);
1397 }
1398
1399 return shader_program;
1400 }
1401
1402 }