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