glsl: Simplify the built-in function linking code.
[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/context.h"
35 #include "main/macros.h"
36 #include "main/samplerobj.h"
37 #include "program/program.h"
38 #include "program/prog_parameter.h"
39 #include "program/prog_cache.h"
40 #include "program/prog_instruction.h"
41 #include "program/prog_print.h"
42 #include "program/prog_statevars.h"
43 #include "program/programopt.h"
44 #include "texenvprogram.h"
45 }
46 #include "main/uniforms.h"
47 #include "../glsl/glsl_types.h"
48 #include "../glsl/ir.h"
49 #include "../glsl/ir_builder.h"
50 #include "../glsl/glsl_symbol_table.h"
51 #include "../glsl/glsl_parser_extras.h"
52 #include "../glsl/ir_optimization.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
307 /**
308 * Identify all possible varying inputs. The fragment program will
309 * never reference non-varying inputs, but will track them via state
310 * constants instead.
311 *
312 * This function figures out all the inputs that the fragment program
313 * has access to. The bitmask is later reduced to just those which
314 * are actually referenced.
315 */
316 static GLbitfield get_fp_input_mask( struct gl_context *ctx )
317 {
318 /* _NEW_PROGRAM */
319 const GLboolean vertexShader =
320 (ctx->Shader.CurrentVertexProgram &&
321 ctx->Shader.CurrentVertexProgram->LinkStatus &&
322 ctx->Shader.CurrentVertexProgram->_LinkedShaders[MESA_SHADER_VERTEX]);
323 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
324 GLbitfield fp_inputs = 0x0;
325
326 if (ctx->VertexProgram._Overriden) {
327 /* Somebody's messing with the vertex program and we don't have
328 * a clue what's happening. Assume that it could be producing
329 * all possible outputs.
330 */
331 fp_inputs = ~0;
332 }
333 else if (ctx->RenderMode == GL_FEEDBACK) {
334 /* _NEW_RENDERMODE */
335 fp_inputs = (VARYING_BIT_COL0 | VARYING_BIT_TEX0);
336 }
337 else if (!(vertexProgram || vertexShader)) {
338 /* Fixed function vertex logic */
339 /* _NEW_VARYING_VP_INPUTS */
340 GLbitfield64 varying_inputs = ctx->varying_vp_inputs;
341
342 /* These get generated in the setup routine regardless of the
343 * vertex program:
344 */
345 /* _NEW_POINT */
346 if (ctx->Point.PointSprite)
347 varying_inputs |= VARYING_BITS_TEX_ANY;
348
349 /* First look at what values may be computed by the generated
350 * vertex program:
351 */
352 /* _NEW_LIGHT */
353 if (ctx->Light.Enabled) {
354 fp_inputs |= VARYING_BIT_COL0;
355
356 if (texenv_doing_secondary_color(ctx))
357 fp_inputs |= VARYING_BIT_COL1;
358 }
359
360 /* _NEW_TEXTURE */
361 fp_inputs |= (ctx->Texture._TexGenEnabled |
362 ctx->Texture._TexMatEnabled) << VARYING_SLOT_TEX0;
363
364 /* Then look at what might be varying as a result of enabled
365 * arrays, etc:
366 */
367 if (varying_inputs & VERT_BIT_COLOR0)
368 fp_inputs |= VARYING_BIT_COL0;
369 if (varying_inputs & VERT_BIT_COLOR1)
370 fp_inputs |= VARYING_BIT_COL1;
371
372 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
373 << VARYING_SLOT_TEX0);
374
375 }
376 else {
377 /* calculate from vp->outputs */
378 struct gl_program *vprog;
379 GLbitfield64 vp_outputs;
380
381 /* Choose GLSL vertex shader over ARB vertex program. Need this
382 * since vertex shader state validation comes after fragment state
383 * validation (see additional comments in state.c).
384 */
385 if (vertexShader)
386 vprog = ctx->Shader.CurrentVertexProgram->_LinkedShaders[MESA_SHADER_VERTEX]->Program;
387 else
388 vprog = &ctx->VertexProgram.Current->Base;
389
390 vp_outputs = vprog->OutputsWritten;
391
392 /* These get generated in the setup routine regardless of the
393 * vertex program:
394 */
395 /* _NEW_POINT */
396 if (ctx->Point.PointSprite)
397 vp_outputs |= VARYING_BITS_TEX_ANY;
398
399 if (vp_outputs & (1 << VARYING_SLOT_COL0))
400 fp_inputs |= VARYING_BIT_COL0;
401 if (vp_outputs & (1 << VARYING_SLOT_COL1))
402 fp_inputs |= VARYING_BIT_COL1;
403
404 fp_inputs |= (((vp_outputs & VARYING_BITS_TEX_ANY) >> VARYING_SLOT_TEX0)
405 << VARYING_SLOT_TEX0);
406 }
407
408 return fp_inputs;
409 }
410
411
412 /**
413 * Examine current texture environment state and generate a unique
414 * key to identify it.
415 */
416 static GLuint make_state_key( struct gl_context *ctx, struct state_key *key )
417 {
418 GLuint i, j;
419 GLbitfield inputs_referenced = VARYING_BIT_COL0;
420 const GLbitfield inputs_available = get_fp_input_mask( ctx );
421 GLuint keySize;
422
423 memset(key, 0, sizeof(*key));
424
425 /* _NEW_TEXTURE */
426 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
427 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
428 const struct gl_texture_object *texObj = texUnit->_Current;
429 const struct gl_tex_env_combine_state *comb = texUnit->_CurrentCombine;
430 const struct gl_sampler_object *samp;
431 GLenum format;
432
433 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
434 continue;
435
436 samp = _mesa_get_samplerobj(ctx, i);
437 format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
438
439 key->unit[i].enabled = 1;
440 key->enabled_units |= (1<<i);
441 key->nr_enabled_units = i + 1;
442 inputs_referenced |= VARYING_BIT_TEX(i);
443
444 key->unit[i].source_index =
445 translate_tex_src_bit(texUnit->_ReallyEnabled);
446
447 key->unit[i].shadow =
448 ((samp->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
449 ((format == GL_DEPTH_COMPONENT) ||
450 (format == GL_DEPTH_STENCIL_EXT)));
451
452 key->unit[i].NumArgsRGB = comb->_NumArgsRGB;
453 key->unit[i].NumArgsA = comb->_NumArgsA;
454
455 key->unit[i].ModeRGB =
456 translate_mode(texUnit->EnvMode, comb->ModeRGB);
457 key->unit[i].ModeA =
458 translate_mode(texUnit->EnvMode, comb->ModeA);
459
460 key->unit[i].ScaleShiftRGB = comb->ScaleShiftRGB;
461 key->unit[i].ScaleShiftA = comb->ScaleShiftA;
462
463 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
464 key->unit[i].OptRGB[j].Operand = translate_operand(comb->OperandRGB[j]);
465 key->unit[i].OptA[j].Operand = translate_operand(comb->OperandA[j]);
466 key->unit[i].OptRGB[j].Source = translate_source(comb->SourceRGB[j]);
467 key->unit[i].OptA[j].Source = translate_source(comb->SourceA[j]);
468 }
469
470 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
471 /* requires some special translation */
472 key->unit[i].NumArgsRGB = 2;
473 key->unit[i].ScaleShiftRGB = 0;
474 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
475 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
476 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
477 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
478 }
479 }
480
481 /* _NEW_LIGHT | _NEW_FOG */
482 if (texenv_doing_secondary_color(ctx)) {
483 key->separate_specular = 1;
484 inputs_referenced |= VARYING_BIT_COL1;
485 }
486
487 /* _NEW_FOG */
488 if (ctx->Fog.Enabled) {
489 key->fog_enabled = 1;
490 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
491 inputs_referenced |= VARYING_BIT_FOGC; /* maybe */
492 }
493
494 /* _NEW_BUFFERS */
495 key->num_draw_buffers = ctx->DrawBuffer->_NumColorDrawBuffers;
496
497 /* _NEW_COLOR */
498 if (ctx->Color.AlphaEnabled && key->num_draw_buffers == 0) {
499 /* if alpha test is enabled we need to emit at least one color */
500 key->num_draw_buffers = 1;
501 }
502
503 key->inputs_available = (inputs_available & inputs_referenced);
504
505 /* compute size of state key, ignoring unused texture units */
506 keySize = sizeof(*key) - sizeof(key->unit)
507 + key->nr_enabled_units * sizeof(key->unit[0]);
508
509 return keySize;
510 }
511
512
513 /** State used to build the fragment program:
514 */
515 class texenv_fragment_program : public ir_factory {
516 public:
517 struct gl_shader_program *shader_program;
518 struct gl_shader *shader;
519 exec_list *top_instructions;
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(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(texenv_fragment_program *p)
554 {
555 if (p->state->inputs_available & VARYING_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(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(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(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(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(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 = p->make_temp(glsl_type::vec4_type, "texenv_combine");
828 ir_dereference *deref;
829 ir_rvalue *val;
830
831 /* Emit the RGB and A combine ops
832 */
833 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
834 args_match(key, unit)) {
835 val = emit_combine(p, unit,
836 key->unit[unit].NumArgsRGB,
837 key->unit[unit].ModeRGB,
838 key->unit[unit].OptRGB);
839 val = smear(p, val);
840 if (rgb_saturate)
841 val = saturate(val);
842
843 p->emit(assign(temp_var, val));
844 }
845 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
846 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
847 ir_rvalue *val = emit_combine(p, unit,
848 key->unit[unit].NumArgsRGB,
849 key->unit[unit].ModeRGB,
850 key->unit[unit].OptRGB);
851 val = smear(p, val);
852 if (rgb_saturate)
853 val = saturate(val);
854 p->emit(assign(temp_var, val));
855 }
856 else {
857 /* Need to do something to stop from re-emitting identical
858 * argument calculations here:
859 */
860 val = emit_combine(p, unit,
861 key->unit[unit].NumArgsRGB,
862 key->unit[unit].ModeRGB,
863 key->unit[unit].OptRGB);
864 val = swizzle_xyz(smear(p, val));
865 if (rgb_saturate)
866 val = saturate(val);
867 p->emit(assign(temp_var, val, WRITEMASK_XYZ));
868
869 val = emit_combine(p, unit,
870 key->unit[unit].NumArgsA,
871 key->unit[unit].ModeA,
872 key->unit[unit].OptA);
873 val = swizzle_w(smear(p, val));
874 if (alpha_saturate)
875 val = saturate(val);
876 p->emit(assign(temp_var, val, WRITEMASK_W));
877 }
878
879 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
880
881 /* Deal with the final shift:
882 */
883 if (alpha_shift || rgb_shift) {
884 ir_constant *shift;
885
886 if (rgb_shift == alpha_shift) {
887 shift = new(p->mem_ctx) ir_constant((float)(1 << rgb_shift));
888 }
889 else {
890 float const_data[4] = {
891 float(1 << rgb_shift),
892 float(1 << rgb_shift),
893 float(1 << rgb_shift),
894 float(1 << alpha_shift)
895 };
896 shift = new(p->mem_ctx) ir_constant(glsl_type::vec4_type,
897 (ir_constant_data *)const_data);
898 }
899
900 return saturate(mul(deref, shift));
901 }
902 else
903 return deref;
904 }
905
906
907 /**
908 * Generate instruction for getting a texture source term.
909 */
910 static void load_texture( texenv_fragment_program *p, GLuint unit )
911 {
912 ir_dereference *deref;
913
914 if (p->src_texture[unit])
915 return;
916
917 const GLuint texTarget = p->state->unit[unit].source_index;
918 ir_rvalue *texcoord;
919
920 if (!(p->state->inputs_available & (VARYING_BIT_TEX0 << unit))) {
921 texcoord = get_current_attrib(p, VERT_ATTRIB_TEX0 + unit);
922 } else if (p->texcoord_tex[unit]) {
923 texcoord = new(p->mem_ctx) ir_dereference_variable(p->texcoord_tex[unit]);
924 } else {
925 ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
926 assert(tc_array);
927 texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
928 ir_rvalue *index = new(p->mem_ctx) ir_constant(unit);
929 texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
930 tc_array->max_array_access = MAX2(tc_array->max_array_access, unit);
931 }
932
933 if (!p->state->unit[unit].enabled) {
934 p->src_texture[unit] = p->make_temp(glsl_type::vec4_type,
935 "dummy_tex");
936 p->emit(p->src_texture[unit]);
937
938 p->emit(assign(p->src_texture[unit], new(p->mem_ctx) ir_constant(0.0f)));
939 return ;
940 }
941
942 const glsl_type *sampler_type = NULL;
943 int coords = 0;
944
945 switch (texTarget) {
946 case TEXTURE_1D_INDEX:
947 if (p->state->unit[unit].shadow)
948 sampler_type = p->shader->symbols->get_type("sampler1DShadow");
949 else
950 sampler_type = p->shader->symbols->get_type("sampler1D");
951 coords = 1;
952 break;
953 case TEXTURE_1D_ARRAY_INDEX:
954 if (p->state->unit[unit].shadow)
955 sampler_type = p->shader->symbols->get_type("sampler1DArrayShadow");
956 else
957 sampler_type = p->shader->symbols->get_type("sampler1DArray");
958 coords = 2;
959 break;
960 case TEXTURE_2D_INDEX:
961 if (p->state->unit[unit].shadow)
962 sampler_type = p->shader->symbols->get_type("sampler2DShadow");
963 else
964 sampler_type = p->shader->symbols->get_type("sampler2D");
965 coords = 2;
966 break;
967 case TEXTURE_2D_ARRAY_INDEX:
968 if (p->state->unit[unit].shadow)
969 sampler_type = p->shader->symbols->get_type("sampler2DArrayShadow");
970 else
971 sampler_type = p->shader->symbols->get_type("sampler2DArray");
972 coords = 3;
973 break;
974 case TEXTURE_RECT_INDEX:
975 if (p->state->unit[unit].shadow)
976 sampler_type = p->shader->symbols->get_type("sampler2DRectShadow");
977 else
978 sampler_type = p->shader->symbols->get_type("sampler2DRect");
979 coords = 2;
980 break;
981 case TEXTURE_3D_INDEX:
982 assert(!p->state->unit[unit].shadow);
983 sampler_type = p->shader->symbols->get_type("sampler3D");
984 coords = 3;
985 break;
986 case TEXTURE_CUBE_INDEX:
987 if (p->state->unit[unit].shadow)
988 sampler_type = p->shader->symbols->get_type("samplerCubeShadow");
989 else
990 sampler_type = p->shader->symbols->get_type("samplerCube");
991 coords = 3;
992 break;
993 case TEXTURE_EXTERNAL_INDEX:
994 assert(!p->state->unit[unit].shadow);
995 sampler_type = p->shader->symbols->get_type("samplerExternalOES");
996 coords = 2;
997 break;
998 }
999
1000 p->src_texture[unit] = p->make_temp(glsl_type::vec4_type,
1001 "tex");
1002
1003 ir_texture *tex = new(p->mem_ctx) ir_texture(ir_tex);
1004
1005
1006 char *sampler_name = ralloc_asprintf(p->mem_ctx, "sampler_%d", unit);
1007 ir_variable *sampler = new(p->mem_ctx) ir_variable(sampler_type,
1008 sampler_name,
1009 ir_var_uniform);
1010 p->top_instructions->push_head(sampler);
1011
1012 /* Set the texture unit for this sampler. The linker will pick this value
1013 * up and do-the-right-thing.
1014 *
1015 * NOTE: The cast to int is important. Without it, the constant will have
1016 * type uint, and things later on may get confused.
1017 */
1018 sampler->constant_value = new(p->mem_ctx) ir_constant(int(unit));
1019
1020 deref = new(p->mem_ctx) ir_dereference_variable(sampler);
1021 tex->set_sampler(deref, glsl_type::vec4_type);
1022
1023 tex->coordinate = new(p->mem_ctx) ir_swizzle(texcoord, 0, 1, 2, 3, coords);
1024
1025 if (p->state->unit[unit].shadow) {
1026 texcoord = texcoord->clone(p->mem_ctx, NULL);
1027 tex->shadow_comparitor = new(p->mem_ctx) ir_swizzle(texcoord,
1028 coords, 0, 0, 0,
1029 1);
1030 coords++;
1031 }
1032
1033 texcoord = texcoord->clone(p->mem_ctx, NULL);
1034 tex->projector = swizzle_w(texcoord);
1035
1036 p->emit(assign(p->src_texture[unit], tex));
1037 }
1038
1039 static void
1040 load_texenv_source(texenv_fragment_program *p,
1041 GLuint src, GLuint unit)
1042 {
1043 switch (src) {
1044 case SRC_TEXTURE:
1045 load_texture(p, unit);
1046 break;
1047
1048 case SRC_TEXTURE0:
1049 case SRC_TEXTURE1:
1050 case SRC_TEXTURE2:
1051 case SRC_TEXTURE3:
1052 case SRC_TEXTURE4:
1053 case SRC_TEXTURE5:
1054 case SRC_TEXTURE6:
1055 case SRC_TEXTURE7:
1056 load_texture(p, src - SRC_TEXTURE0);
1057 break;
1058
1059 default:
1060 /* not a texture src - do nothing */
1061 break;
1062 }
1063 }
1064
1065
1066 /**
1067 * Generate instructions for loading all texture source terms.
1068 */
1069 static GLboolean
1070 load_texunit_sources( texenv_fragment_program *p, GLuint unit )
1071 {
1072 const struct state_key *key = p->state;
1073 GLuint i;
1074
1075 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1076 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
1077 }
1078
1079 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1080 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1081 }
1082
1083 return GL_TRUE;
1084 }
1085
1086 /**
1087 * Generate instructions for loading bump map textures.
1088 */
1089 static void
1090 load_texunit_bumpmap( texenv_fragment_program *p, GLuint unit )
1091 {
1092 const struct state_key *key = p->state;
1093 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1094 ir_rvalue *bump;
1095 ir_rvalue *texcoord;
1096 ir_variable *rot_mat_0, *rot_mat_1;
1097
1098 rot_mat_0 = p->shader->symbols->get_variable("gl_BumpRotMatrix0MESA");
1099 rot_mat_1 = p->shader->symbols->get_variable("gl_BumpRotMatrix1MESA");
1100
1101 ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
1102 assert(tc_array);
1103 texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
1104 ir_rvalue *index = new(p->mem_ctx) ir_constant(bumpedUnitNr);
1105 texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
1106 tc_array->max_array_access = MAX2(tc_array->max_array_access, unit);
1107
1108 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1109
1110 /* Apply rot matrix and add coords to be available in next phase.
1111 * dest = Arg1 + (Arg0.xx * rotMat0) + (Arg0.yy * rotMat1)
1112 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1113 */
1114 ir_rvalue *bump_x, *bump_y;
1115
1116 texcoord = smear(p, texcoord);
1117
1118 /* bump_texcoord = texcoord */
1119 ir_variable *bumped = p->make_temp(texcoord->type, "bump_texcoord");
1120 p->emit(bumped);
1121 p->emit(assign(bumped, texcoord));
1122
1123 /* bump_texcoord.xy += arg0.x * rotmat0 + arg0.y * rotmat1 */
1124 bump = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1125 bump_x = mul(swizzle_x(bump), rot_mat_0);
1126 bump_y = mul(swizzle_y(bump->clone(p->mem_ctx, NULL)), rot_mat_1);
1127
1128 p->emit(assign(bumped, add(swizzle_xy(bumped), add(bump_x, bump_y)),
1129 WRITEMASK_XY));
1130
1131 p->texcoord_tex[bumpedUnitNr] = bumped;
1132 }
1133
1134 /**
1135 * Applies the fog calculations.
1136 *
1137 * This is basically like the ARB_fragment_prorgam fog options. Note
1138 * that ffvertex_prog.c produces fogcoord for us when
1139 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1140 */
1141 static ir_rvalue *
1142 emit_fog_instructions(texenv_fragment_program *p,
1143 ir_rvalue *fragcolor)
1144 {
1145 struct state_key *key = p->state;
1146 ir_rvalue *f, *temp;
1147 ir_variable *params, *oparams;
1148 ir_variable *fogcoord;
1149
1150 /* Temporary storage for the whole fog result. Fog calculations
1151 * only affect rgb so we're hanging on to the .a value of fragcolor
1152 * this way.
1153 */
1154 ir_variable *fog_result = p->make_temp(glsl_type::vec4_type, "fog_result");
1155 p->emit(assign(fog_result, fragcolor));
1156
1157 fragcolor = swizzle_xyz(fog_result);
1158
1159 oparams = p->shader->symbols->get_variable("gl_FogParamsOptimizedMESA");
1160 fogcoord = p->shader->symbols->get_variable("gl_FogFragCoord");
1161 params = p->shader->symbols->get_variable("gl_Fog");
1162 f = new(p->mem_ctx) ir_dereference_variable(fogcoord);
1163
1164 ir_variable *f_var = p->make_temp(glsl_type::float_type, "fog_factor");
1165
1166 switch (key->fog_mode) {
1167 case FOG_LINEAR:
1168 /* f = (end - z) / (end - start)
1169 *
1170 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1171 * (end / (end - start)) so we can generate a single MAD.
1172 */
1173 f = add(mul(f, swizzle_x(oparams)), swizzle_y(oparams));
1174 break;
1175 case FOG_EXP:
1176 /* f = e^(-(density * fogcoord))
1177 *
1178 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1179 * use EXP2 which is generally the native instruction without
1180 * having to do any further math on the fog density uniform.
1181 */
1182 f = mul(f, swizzle_z(oparams));
1183 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
1184 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
1185 break;
1186 case FOG_EXP2:
1187 /* f = e^(-(density * fogcoord)^2)
1188 *
1189 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1190 * can do this like FOG_EXP but with a squaring after the
1191 * multiply by density.
1192 */
1193 ir_variable *temp_var = p->make_temp(glsl_type::float_type, "fog_temp");
1194 p->emit(assign(temp_var, mul(f, swizzle_w(oparams))));
1195
1196 f = mul(temp_var, temp_var);
1197 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
1198 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
1199 break;
1200 }
1201
1202 p->emit(assign(f_var, saturate(f)));
1203
1204 f = sub(new(p->mem_ctx) ir_constant(1.0f), f_var);
1205 temp = new(p->mem_ctx) ir_dereference_variable(params);
1206 temp = new(p->mem_ctx) ir_dereference_record(temp, "color");
1207 temp = mul(swizzle_xyz(temp), f);
1208
1209 p->emit(assign(fog_result, add(temp, mul(fragcolor, f_var)), WRITEMASK_XYZ));
1210
1211 return new(p->mem_ctx) ir_dereference_variable(fog_result);
1212 }
1213
1214 static void
1215 emit_instructions(texenv_fragment_program *p)
1216 {
1217 struct state_key *key = p->state;
1218 GLuint unit;
1219
1220 if (key->enabled_units) {
1221 /* Zeroth pass - bump map textures first */
1222 for (unit = 0; unit < key->nr_enabled_units; unit++) {
1223 if (key->unit[unit].enabled &&
1224 key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1225 load_texunit_bumpmap(p, unit);
1226 }
1227 }
1228
1229 /* First pass - to support texture_env_crossbar, first identify
1230 * all referenced texture sources and emit texld instructions
1231 * for each:
1232 */
1233 for (unit = 0; unit < key->nr_enabled_units; unit++)
1234 if (key->unit[unit].enabled) {
1235 load_texunit_sources(p, unit);
1236 }
1237
1238 /* Second pass - emit combine instructions to build final color:
1239 */
1240 for (unit = 0; unit < key->nr_enabled_units; unit++) {
1241 if (key->unit[unit].enabled) {
1242 p->src_previous = emit_texenv(p, unit);
1243 }
1244 }
1245 }
1246
1247 ir_rvalue *cf = get_source(p, SRC_PREVIOUS, 0);
1248
1249 if (key->separate_specular) {
1250 ir_variable *spec_result = p->make_temp(glsl_type::vec4_type,
1251 "specular_add");
1252 p->emit(assign(spec_result, cf));
1253
1254 ir_rvalue *secondary;
1255 if (p->state->inputs_available & VARYING_BIT_COL1) {
1256 ir_variable *var =
1257 p->shader->symbols->get_variable("gl_SecondaryColor");
1258 assert(var);
1259 secondary = swizzle_xyz(var);
1260 } else {
1261 secondary = swizzle_xyz(get_current_attrib(p, VERT_ATTRIB_COLOR1));
1262 }
1263
1264 p->emit(assign(spec_result, add(swizzle_xyz(spec_result), secondary),
1265 WRITEMASK_XYZ));
1266
1267 cf = new(p->mem_ctx) ir_dereference_variable(spec_result);
1268 }
1269
1270 if (key->fog_enabled) {
1271 cf = emit_fog_instructions(p, cf);
1272 }
1273
1274 ir_variable *frag_color = p->shader->symbols->get_variable("gl_FragColor");
1275 assert(frag_color);
1276 p->emit(assign(frag_color, cf));
1277 }
1278
1279 /**
1280 * Generate a new fragment program which implements the context's
1281 * current texture env/combine mode.
1282 */
1283 static struct gl_shader_program *
1284 create_new_program(struct gl_context *ctx, struct state_key *key)
1285 {
1286 texenv_fragment_program p;
1287 unsigned int unit;
1288 _mesa_glsl_parse_state *state;
1289
1290 p.mem_ctx = ralloc_context(NULL);
1291 p.shader = ctx->Driver.NewShader(ctx, 0, GL_FRAGMENT_SHADER);
1292 p.shader->ir = new(p.shader) exec_list;
1293 state = new(p.shader) _mesa_glsl_parse_state(ctx, GL_FRAGMENT_SHADER,
1294 p.shader);
1295 p.shader->symbols = state->symbols;
1296 p.top_instructions = p.shader->ir;
1297 p.instructions = p.shader->ir;
1298 p.state = key;
1299 p.shader_program = ctx->Driver.NewShaderProgram(ctx, 0);
1300
1301 /* Tell the linker to ignore the fact that we're building a
1302 * separate shader, in case we're in a GLES2 context that would
1303 * normally reject that. The real problem is that we're building a
1304 * fixed function program in a GLES2 context at all, but that's a
1305 * big mess to clean up.
1306 */
1307 p.shader_program->InternalSeparateShader = GL_TRUE;
1308
1309 state->language_version = 130;
1310 state->es_shader = false;
1311 if (_mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external)
1312 state->OES_EGL_image_external_enable = true;
1313 _mesa_glsl_initialize_types(state);
1314 _mesa_glsl_initialize_variables(p.instructions, state);
1315
1316 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
1317 p.src_texture[unit] = NULL;
1318 p.texcoord_tex[unit] = NULL;
1319 }
1320
1321 p.src_previous = NULL;
1322
1323 ir_function *main_f = new(p.mem_ctx) ir_function("main");
1324 p.emit(main_f);
1325 state->symbols->add_function(main_f);
1326
1327 ir_function_signature *main_sig =
1328 new(p.mem_ctx) ir_function_signature(p.shader->symbols->get_type("void"));
1329 main_sig->is_defined = true;
1330 main_f->add_signature(main_sig);
1331
1332 p.instructions = &main_sig->body;
1333 if (key->num_draw_buffers)
1334 emit_instructions(&p);
1335
1336 validate_ir_tree(p.shader->ir);
1337
1338 const struct gl_shader_compiler_options *options =
1339 &ctx->ShaderCompilerOptions[MESA_SHADER_FRAGMENT];
1340
1341 while (do_common_optimization(p.shader->ir, false, false, 32, options))
1342 ;
1343 reparent_ir(p.shader->ir, p.shader->ir);
1344
1345 p.shader->CompileStatus = true;
1346 p.shader->Version = state->language_version;
1347 p.shader->uses_builtin_functions = state->uses_builtin_functions;
1348 p.shader_program->Shaders =
1349 (gl_shader **)malloc(sizeof(*p.shader_program->Shaders));
1350 p.shader_program->Shaders[0] = p.shader;
1351 p.shader_program->NumShaders = 1;
1352
1353 _mesa_glsl_link_shader(ctx, p.shader_program);
1354
1355 if (!p.shader_program->LinkStatus)
1356 _mesa_problem(ctx, "Failed to link fixed function fragment shader: %s\n",
1357 p.shader_program->InfoLog);
1358
1359 ralloc_free(p.mem_ctx);
1360 return p.shader_program;
1361 }
1362
1363 extern "C" {
1364
1365 /**
1366 * Return a fragment program which implements the current
1367 * fixed-function texture, fog and color-sum operations.
1368 */
1369 struct gl_shader_program *
1370 _mesa_get_fixed_func_fragment_program(struct gl_context *ctx)
1371 {
1372 struct gl_shader_program *shader_program;
1373 struct state_key key;
1374 GLuint keySize;
1375
1376 keySize = make_state_key(ctx, &key);
1377
1378 shader_program = (struct gl_shader_program *)
1379 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1380 &key, keySize);
1381
1382 if (!shader_program) {
1383 shader_program = create_new_program(ctx, &key);
1384
1385 _mesa_shader_cache_insert(ctx, ctx->FragmentProgram.Cache,
1386 &key, keySize, shader_program);
1387 }
1388
1389 return shader_program;
1390 }
1391
1392 }