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