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