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