50d6be70d403472831924ea4ef683757146796f6
[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 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:3; /**< 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->VertexProgram);
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 !ctx->VertexProgram._Current) {
336 /* Fixed function vertex logic */
337 /* _NEW_ARRAY */
338 GLbitfield 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_vertex_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->VertexProgram;
385 else
386 vprog = ctx->VertexProgram.Current;
387
388 vp_outputs = vprog->Base.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 = ((texObj->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_source(struct texenv_fragment_program *p,
539 GLuint src, GLuint unit)
540 {
541 ir_variable *var;
542 ir_dereference *deref;
543
544 switch (src) {
545 case SRC_TEXTURE:
546 return new(p->mem_ctx) ir_dereference_variable(p->src_texture[unit]);
547
548 case SRC_TEXTURE0:
549 case SRC_TEXTURE1:
550 case SRC_TEXTURE2:
551 case SRC_TEXTURE3:
552 case SRC_TEXTURE4:
553 case SRC_TEXTURE5:
554 case SRC_TEXTURE6:
555 case SRC_TEXTURE7:
556 return new(p->mem_ctx)
557 ir_dereference_variable(p->src_texture[src - SRC_TEXTURE0]);
558
559 case SRC_CONSTANT:
560 var = p->shader->symbols->get_variable("gl_TextureEnvColor");
561 assert(var);
562 deref = new(p->mem_ctx) ir_dereference_variable(var);
563 var->max_array_access = MAX2(var->max_array_access, unit);
564 return new(p->mem_ctx) ir_dereference_array(deref,
565 new(p->mem_ctx) ir_constant(unit));
566
567 case SRC_PRIMARY_COLOR:
568 var = p->shader->symbols->get_variable("gl_Color");
569 assert(var);
570 return new(p->mem_ctx) ir_dereference_variable(var);
571
572 case SRC_ZERO:
573 return new(p->mem_ctx) ir_constant(0.0f);
574
575 case SRC_PREVIOUS:
576 if (!p->src_previous) {
577 var = p->shader->symbols->get_variable("gl_Color");
578 assert(var);
579 return new(p->mem_ctx) ir_dereference_variable(var);
580 } else {
581 return p->src_previous->clone(p->mem_ctx, NULL);
582 }
583
584 default:
585 assert(0);
586 return NULL;
587 }
588 }
589
590 static ir_rvalue *
591 emit_combine_source(struct texenv_fragment_program *p,
592 GLuint unit,
593 GLuint source,
594 GLuint operand)
595 {
596 ir_rvalue *src;
597
598 src = get_source(p, source, unit);
599
600 switch (operand) {
601 case OPR_ONE_MINUS_SRC_COLOR:
602 return new(p->mem_ctx) ir_expression(ir_binop_sub,
603 new(p->mem_ctx) ir_constant(1.0f),
604 src);
605
606 case OPR_SRC_ALPHA:
607 return new(p->mem_ctx) ir_swizzle(src, 3, 3, 3, 3, 1);
608
609 case OPR_ONE_MINUS_SRC_ALPHA:
610 return new(p->mem_ctx) ir_expression(ir_binop_sub,
611 new(p->mem_ctx) ir_constant(1.0f),
612 new(p->mem_ctx) ir_swizzle(src,
613 3, 3,
614 3, 3, 1));
615 case OPR_ZERO:
616 return new(p->mem_ctx) ir_constant(0.0f);
617 case OPR_ONE:
618 return new(p->mem_ctx) ir_constant(1.0f);
619 case OPR_SRC_COLOR:
620 return src;
621 default:
622 assert(0);
623 return src;
624 }
625 }
626
627 /**
628 * Check if the RGB and Alpha sources and operands match for the given
629 * texture unit's combinder state. When the RGB and A sources and
630 * operands match, we can emit fewer instructions.
631 */
632 static GLboolean args_match( const struct state_key *key, GLuint unit )
633 {
634 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
635
636 for (i = 0; i < numArgs; i++) {
637 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
638 return GL_FALSE;
639
640 switch (key->unit[unit].OptA[i].Operand) {
641 case OPR_SRC_ALPHA:
642 switch (key->unit[unit].OptRGB[i].Operand) {
643 case OPR_SRC_COLOR:
644 case OPR_SRC_ALPHA:
645 break;
646 default:
647 return GL_FALSE;
648 }
649 break;
650 case OPR_ONE_MINUS_SRC_ALPHA:
651 switch (key->unit[unit].OptRGB[i].Operand) {
652 case OPR_ONE_MINUS_SRC_COLOR:
653 case OPR_ONE_MINUS_SRC_ALPHA:
654 break;
655 default:
656 return GL_FALSE;
657 }
658 break;
659 default:
660 return GL_FALSE; /* impossible */
661 }
662 }
663
664 return GL_TRUE;
665 }
666
667 static ir_rvalue *
668 smear(struct texenv_fragment_program *p, ir_rvalue *val)
669 {
670 if (!val->type->is_scalar())
671 return val;
672
673 return new(p->mem_ctx) ir_swizzle(val, 0, 0, 0, 0, 4);
674 }
675
676 static ir_rvalue *
677 emit_combine(struct texenv_fragment_program *p,
678 GLuint unit,
679 GLuint nr,
680 GLuint mode,
681 const struct mode_opt *opt)
682 {
683 ir_rvalue *src[MAX_COMBINER_TERMS];
684 ir_rvalue *tmp0, *tmp1;
685 GLuint i;
686
687 assert(nr <= MAX_COMBINER_TERMS);
688
689 for (i = 0; i < nr; i++)
690 src[i] = emit_combine_source( p, unit, opt[i].Source, opt[i].Operand );
691
692 switch (mode) {
693 case MODE_REPLACE:
694 return src[0];
695
696 case MODE_MODULATE:
697 return new(p->mem_ctx) ir_expression(ir_binop_mul, src[0], src[1]);
698
699 case MODE_ADD:
700 return new(p->mem_ctx) ir_expression(ir_binop_add, src[0], src[1]);
701
702 case MODE_ADD_SIGNED:
703 tmp0 = new(p->mem_ctx) ir_expression(ir_binop_add, src[0], src[1]);
704 return new(p->mem_ctx) ir_expression(ir_binop_add, tmp0,
705 new(p->mem_ctx) ir_constant(-0.5f));
706
707 case MODE_INTERPOLATE:
708 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
709 tmp0 = new(p->mem_ctx) ir_expression(ir_binop_mul, src[0], src[2]);
710
711 tmp1 = new(p->mem_ctx) ir_expression(ir_binop_sub,
712 new(p->mem_ctx) ir_constant(1.0f),
713 src[2]->clone(p->mem_ctx, NULL));
714 tmp1 = new(p->mem_ctx) ir_expression(ir_binop_mul, src[1], tmp1);
715
716 return new(p->mem_ctx) ir_expression(ir_binop_add, tmp0, tmp1);
717
718 case MODE_SUBTRACT:
719 return new(p->mem_ctx) ir_expression(ir_binop_sub, src[0], src[1]);
720
721 case MODE_DOT3_RGBA:
722 case MODE_DOT3_RGBA_EXT:
723 case MODE_DOT3_RGB_EXT:
724 case MODE_DOT3_RGB: {
725 tmp0 = new(p->mem_ctx) ir_expression(ir_binop_mul, src[0],
726 new(p->mem_ctx) ir_constant(2.0f));
727 tmp0 = new(p->mem_ctx) ir_expression(ir_binop_add, tmp0,
728 new(p->mem_ctx) ir_constant(-1.0f));
729 tmp0 = new(p->mem_ctx) ir_swizzle(smear(p, tmp0), 0, 1, 2, 3, 3);
730
731 tmp1 = new(p->mem_ctx) ir_expression(ir_binop_mul, src[1],
732 new(p->mem_ctx) ir_constant(2.0f));
733 tmp1 = new(p->mem_ctx) ir_expression(ir_binop_add, tmp1,
734 new(p->mem_ctx) ir_constant(-1.0f));
735 tmp1 = new(p->mem_ctx) ir_swizzle(smear(p, tmp1), 0, 1, 2, 3, 3);
736
737 return new(p->mem_ctx) ir_expression(ir_binop_dot, tmp0, tmp1);
738 }
739 case MODE_MODULATE_ADD_ATI:
740 tmp0 = new(p->mem_ctx) ir_expression(ir_binop_mul, src[0], src[2]);
741 return new(p->mem_ctx) ir_expression(ir_binop_add, tmp0, src[1]);
742
743 case MODE_MODULATE_SIGNED_ADD_ATI:
744 tmp0 = new(p->mem_ctx) ir_expression(ir_binop_mul, src[0], src[2]);
745 tmp0 = new(p->mem_ctx) ir_expression(ir_binop_add, tmp0, src[1]);
746 return new(p->mem_ctx) ir_expression(ir_binop_add, tmp0,
747 new(p->mem_ctx) ir_constant(-0.5f));
748
749 case MODE_MODULATE_SUBTRACT_ATI:
750 tmp0 = new(p->mem_ctx) ir_expression(ir_binop_mul, src[0], src[2]);
751 return new(p->mem_ctx) ir_expression(ir_binop_sub, tmp0, src[1]);
752
753 case MODE_ADD_PRODUCTS:
754 tmp0 = new(p->mem_ctx) ir_expression(ir_binop_mul, src[0], src[1]);
755 tmp1 = new(p->mem_ctx) ir_expression(ir_binop_mul, src[2], src[3]);
756 return new(p->mem_ctx) ir_expression(ir_binop_add, tmp0, tmp1);
757
758 case MODE_ADD_PRODUCTS_SIGNED:
759 tmp0 = new(p->mem_ctx) ir_expression(ir_binop_mul, src[0], src[1]);
760 tmp1 = new(p->mem_ctx) ir_expression(ir_binop_mul, src[2], src[3]);
761 tmp0 = new(p->mem_ctx) ir_expression(ir_binop_add, tmp0, tmp1);
762 return new(p->mem_ctx) ir_expression(ir_binop_add, tmp0,
763 new(p->mem_ctx) ir_constant(-0.5f));
764
765 case MODE_BUMP_ENVMAP_ATI:
766 /* special - not handled here */
767 assert(0);
768 return src[0];
769 default:
770 assert(0);
771 return src[0];
772 }
773 }
774
775 static ir_rvalue *
776 saturate(struct texenv_fragment_program *p, ir_rvalue *val)
777 {
778 val = new(p->mem_ctx) ir_expression(ir_binop_min, val,
779 new(p->mem_ctx) ir_constant(1.0f));
780 return new(p->mem_ctx) ir_expression(ir_binop_max, val,
781 new(p->mem_ctx) ir_constant(0.0f));
782 }
783
784 /**
785 * Generate instructions for one texture unit's env/combiner mode.
786 */
787 static ir_rvalue *
788 emit_texenv(struct texenv_fragment_program *p, GLuint unit)
789 {
790 const struct state_key *key = p->state;
791 GLboolean rgb_saturate, alpha_saturate;
792 GLuint rgb_shift, alpha_shift;
793
794 if (!key->unit[unit].enabled) {
795 return get_source(p, SRC_PREVIOUS, 0);
796 }
797 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
798 /* this isn't really a env stage delivering a color and handled elsewhere */
799 return get_source(p, SRC_PREVIOUS, 0);
800 }
801
802 switch (key->unit[unit].ModeRGB) {
803 case MODE_DOT3_RGB_EXT:
804 alpha_shift = key->unit[unit].ScaleShiftA;
805 rgb_shift = 0;
806 break;
807 case MODE_DOT3_RGBA_EXT:
808 alpha_shift = 0;
809 rgb_shift = 0;
810 break;
811 default:
812 rgb_shift = key->unit[unit].ScaleShiftRGB;
813 alpha_shift = key->unit[unit].ScaleShiftA;
814 break;
815 }
816
817 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
818 * We don't want to clamp twice.
819 */
820 if (rgb_shift)
821 rgb_saturate = GL_FALSE; /* saturate after rgb shift */
822 else if (need_saturate(key->unit[unit].ModeRGB))
823 rgb_saturate = GL_TRUE;
824 else
825 rgb_saturate = GL_FALSE;
826
827 if (alpha_shift)
828 alpha_saturate = GL_FALSE; /* saturate after alpha shift */
829 else if (need_saturate(key->unit[unit].ModeA))
830 alpha_saturate = GL_TRUE;
831 else
832 alpha_saturate = GL_FALSE;
833
834 ir_variable *temp_var = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
835 "texenv_combine",
836 ir_var_temporary);
837 p->instructions->push_tail(temp_var);
838
839 ir_dereference *deref;
840 ir_assignment *assign;
841 ir_rvalue *val;
842
843 /* Emit the RGB and A combine ops
844 */
845 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
846 args_match(key, unit)) {
847 val = emit_combine(p, unit,
848 key->unit[unit].NumArgsRGB,
849 key->unit[unit].ModeRGB,
850 key->unit[unit].OptRGB);
851 val = smear(p, val);
852 if (rgb_saturate)
853 val = saturate(p, val);
854
855 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
856 assign = new(p->mem_ctx) ir_assignment(deref, val, NULL);
857 p->instructions->push_tail(assign);
858 }
859 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
860 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
861 ir_rvalue *val = emit_combine(p, unit,
862 key->unit[unit].NumArgsRGB,
863 key->unit[unit].ModeRGB,
864 key->unit[unit].OptRGB);
865 val = smear(p, val);
866 if (rgb_saturate)
867 val = saturate(p, val);
868 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
869 assign = new(p->mem_ctx) ir_assignment(deref, val, NULL);
870 p->instructions->push_tail(assign);
871 }
872 else {
873 /* Need to do something to stop from re-emitting identical
874 * argument calculations here:
875 */
876 val = emit_combine(p, unit,
877 key->unit[unit].NumArgsRGB,
878 key->unit[unit].ModeRGB,
879 key->unit[unit].OptRGB);
880 val = smear(p, val);
881 val = new(p->mem_ctx) ir_swizzle(val, 0, 1, 2, 3, 3);
882 if (rgb_saturate)
883 val = saturate(p, val);
884 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
885 assign = new(p->mem_ctx) ir_assignment(deref, val, NULL, WRITEMASK_XYZ);
886 p->instructions->push_tail(assign);
887
888 val = emit_combine(p, unit,
889 key->unit[unit].NumArgsA,
890 key->unit[unit].ModeA,
891 key->unit[unit].OptA);
892 val = smear(p, val);
893 val = new(p->mem_ctx) ir_swizzle(val, 3, 3, 3, 3, 1);
894 if (alpha_saturate)
895 val = saturate(p, val);
896 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
897 assign = new(p->mem_ctx) ir_assignment(deref, val, NULL, WRITEMASK_W);
898 p->instructions->push_tail(assign);
899 }
900
901 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
902
903 /* Deal with the final shift:
904 */
905 if (alpha_shift || rgb_shift) {
906 ir_constant *shift;
907
908 if (rgb_shift == alpha_shift) {
909 shift = new(p->mem_ctx) ir_constant((float)(1 << rgb_shift));
910 }
911 else {
912 float const_data[4] = {
913 1 << rgb_shift,
914 1 << rgb_shift,
915 1 << rgb_shift,
916 1 << alpha_shift
917 };
918 shift = new(p->mem_ctx) ir_constant(glsl_type::vec4_type,
919 (ir_constant_data *)const_data);
920 }
921
922 return saturate(p, new(p->mem_ctx) ir_expression(ir_binop_mul,
923 deref, shift));
924 }
925 else
926 return deref;
927 }
928
929
930 /**
931 * Generate instruction for getting a texture source term.
932 */
933 static void load_texture( struct texenv_fragment_program *p, GLuint unit )
934 {
935 ir_dereference *deref;
936 ir_assignment *assign;
937
938 if (p->src_texture[unit])
939 return;
940
941 const GLuint texTarget = p->state->unit[unit].source_index;
942 ir_rvalue *texcoord;
943
944 if (p->texcoord_tex[unit]) {
945 texcoord = new(p->mem_ctx) ir_dereference_variable(p->texcoord_tex[unit]);
946 }
947 else {
948 ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
949 assert(tc_array);
950 texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
951 ir_rvalue *index = new(p->mem_ctx) ir_constant(unit);
952 texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
953 tc_array->max_array_access = MAX2(tc_array->max_array_access, unit);
954 }
955
956 if (!p->state->unit[unit].enabled) {
957 p->src_texture[unit] = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
958 "dummy_tex",
959 ir_var_temporary);
960 p->instructions->push_tail(p->src_texture[unit]);
961
962 deref = new(p->mem_ctx) ir_dereference_variable(p->src_texture[unit]);
963 assign = new(p->mem_ctx) ir_assignment(deref,
964 new(p->mem_ctx) ir_constant(0.0f),
965 NULL);
966 p->instructions->push_tail(assign);
967 return ;
968 }
969
970 const glsl_type *sampler_type = NULL;
971 int coords = 0;
972
973 switch (texTarget) {
974 case TEXTURE_1D_INDEX:
975 if (p->state->unit[unit].shadow)
976 sampler_type = p->shader->symbols->get_type("sampler1DShadow");
977 else
978 sampler_type = p->shader->symbols->get_type("sampler1D");
979 coords = 1;
980 break;
981 case TEXTURE_1D_ARRAY_INDEX:
982 if (p->state->unit[unit].shadow)
983 sampler_type = p->shader->symbols->get_type("sampler1DArrayShadow");
984 else
985 sampler_type = p->shader->symbols->get_type("sampler1DArray");
986 coords = 2;
987 break;
988 case TEXTURE_2D_INDEX:
989 if (p->state->unit[unit].shadow)
990 sampler_type = p->shader->symbols->get_type("sampler2DShadow");
991 else
992 sampler_type = p->shader->symbols->get_type("sampler2D");
993 coords = 2;
994 break;
995 case TEXTURE_2D_ARRAY_INDEX:
996 if (p->state->unit[unit].shadow)
997 sampler_type = p->shader->symbols->get_type("sampler2DArrayShadow");
998 else
999 sampler_type = p->shader->symbols->get_type("sampler2DArray");
1000 coords = 3;
1001 break;
1002 case TEXTURE_RECT_INDEX:
1003 if (p->state->unit[unit].shadow)
1004 sampler_type = p->shader->symbols->get_type("sampler2DRectShadow");
1005 else
1006 sampler_type = p->shader->symbols->get_type("sampler2DRect");
1007 coords = 2;
1008 break;
1009 case TEXTURE_3D_INDEX:
1010 assert(!p->state->unit[unit].shadow);
1011 sampler_type = p->shader->symbols->get_type("sampler3D");
1012 coords = 3;
1013 break;
1014 case TEXTURE_CUBE_INDEX:
1015 if (p->state->unit[unit].shadow)
1016 sampler_type = p->shader->symbols->get_type("samplerCubeShadow");
1017 else
1018 sampler_type = p->shader->symbols->get_type("samplerCube");
1019 coords = 3;
1020 break;
1021 }
1022
1023 p->src_texture[unit] = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
1024 "tex",
1025 ir_var_temporary);
1026 p->instructions->push_tail(p->src_texture[unit]);
1027
1028 ir_texture *tex = new(p->mem_ctx) ir_texture(ir_tex);
1029
1030
1031 char *sampler_name = ralloc_asprintf(p->mem_ctx, "sampler_%d", unit);
1032 ir_variable *sampler = new(p->mem_ctx) ir_variable(sampler_type,
1033 sampler_name,
1034 ir_var_uniform);
1035 p->top_instructions->push_head(sampler);
1036 deref = new(p->mem_ctx) ir_dereference_variable(sampler);
1037 tex->set_sampler(deref);
1038
1039 tex->coordinate = new(p->mem_ctx) ir_swizzle(texcoord, 0, 1, 2, 3, coords);
1040
1041 if (p->state->unit[unit].shadow) {
1042 texcoord = texcoord->clone(p->mem_ctx, NULL);
1043 tex->shadow_comparitor = new(p->mem_ctx) ir_swizzle(texcoord,
1044 coords, 0, 0, 0,
1045 1);
1046 coords++;
1047 }
1048
1049 texcoord = texcoord->clone(p->mem_ctx, NULL);
1050 tex->projector = new(p->mem_ctx) ir_swizzle(texcoord, 3, 0, 0, 0, 1);
1051
1052 deref = new(p->mem_ctx) ir_dereference_variable(p->src_texture[unit]);
1053 assign = new(p->mem_ctx) ir_assignment(deref, tex, NULL);
1054 p->instructions->push_tail(assign);
1055 }
1056
1057 static void
1058 load_texenv_source(struct texenv_fragment_program *p,
1059 GLuint src, GLuint unit)
1060 {
1061 switch (src) {
1062 case SRC_TEXTURE:
1063 load_texture(p, unit);
1064 break;
1065
1066 case SRC_TEXTURE0:
1067 case SRC_TEXTURE1:
1068 case SRC_TEXTURE2:
1069 case SRC_TEXTURE3:
1070 case SRC_TEXTURE4:
1071 case SRC_TEXTURE5:
1072 case SRC_TEXTURE6:
1073 case SRC_TEXTURE7:
1074 load_texture(p, src - SRC_TEXTURE0);
1075 break;
1076
1077 default:
1078 /* not a texture src - do nothing */
1079 break;
1080 }
1081 }
1082
1083
1084 /**
1085 * Generate instructions for loading all texture source terms.
1086 */
1087 static GLboolean
1088 load_texunit_sources( struct texenv_fragment_program *p, GLuint unit )
1089 {
1090 const struct state_key *key = p->state;
1091 GLuint i;
1092
1093 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1094 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
1095 }
1096
1097 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1098 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1099 }
1100
1101 return GL_TRUE;
1102 }
1103
1104 /**
1105 * Generate instructions for loading bump map textures.
1106 */
1107 static void
1108 load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
1109 {
1110 const struct state_key *key = p->state;
1111 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1112 ir_rvalue *bump;
1113 ir_rvalue *texcoord;
1114 ir_variable *rot_mat_0_var, *rot_mat_1_var;
1115 ir_dereference_variable *rot_mat_0, *rot_mat_1;
1116
1117 rot_mat_0_var = p->shader->symbols->get_variable("gl_MESABumpRotMatrix0");
1118 rot_mat_1_var = p->shader->symbols->get_variable("gl_MESABumpRotMatrix1");
1119 rot_mat_0 = new(p->mem_ctx) ir_dereference_variable(rot_mat_0_var);
1120 rot_mat_1 = new(p->mem_ctx) ir_dereference_variable(rot_mat_1_var);
1121
1122 ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
1123 assert(tc_array);
1124 texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
1125 ir_rvalue *index = new(p->mem_ctx) ir_constant(bumpedUnitNr);
1126 texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
1127 tc_array->max_array_access = MAX2(tc_array->max_array_access, unit);
1128
1129 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1130
1131 /* Apply rot matrix and add coords to be available in next phase.
1132 * dest = Arg1 + (Arg0.xx * rotMat0) + (Arg0.yy * rotMat1)
1133 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1134 */
1135 ir_dereference *deref;
1136 ir_assignment *assign;
1137 ir_rvalue *bump_x, *bump_y;
1138
1139 texcoord = smear(p, texcoord);
1140
1141 /* bump_texcoord = texcoord */
1142 ir_variable *bumped = new(p->mem_ctx) ir_variable(texcoord->type,
1143 "bump_texcoord",
1144 ir_var_temporary);
1145 p->instructions->push_tail(bumped);
1146
1147 deref = new(p->mem_ctx) ir_dereference_variable(bumped);
1148 assign = new(p->mem_ctx) ir_assignment(deref, texcoord, NULL);
1149 p->instructions->push_tail(assign);
1150
1151 /* bump_texcoord.xy += arg0.x * rotmat0 + arg0.y * rotmat1 */
1152 bump = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1153 bump_x = new(p->mem_ctx) ir_swizzle(bump, 0, 0, 0, 0, 1);
1154 bump = bump->clone(p->mem_ctx, NULL);
1155 bump_y = new(p->mem_ctx) ir_swizzle(bump, 1, 0, 0, 0, 1);
1156
1157 bump_x = new(p->mem_ctx) ir_expression(ir_binop_mul, bump_x, rot_mat_0);
1158 bump_y = new(p->mem_ctx) ir_expression(ir_binop_mul, bump_y, rot_mat_1);
1159
1160 ir_expression *expr;
1161 expr = new(p->mem_ctx) ir_expression(ir_binop_add, bump_x, bump_y);
1162
1163 deref = new(p->mem_ctx) ir_dereference_variable(bumped);
1164 expr = new(p->mem_ctx) ir_expression(ir_binop_add,
1165 new(p->mem_ctx) ir_swizzle(deref,
1166 0, 1, 1, 1,
1167 2),
1168 expr);
1169
1170 deref = new(p->mem_ctx) ir_dereference_variable(bumped);
1171 assign = new(p->mem_ctx) ir_assignment(deref, expr, NULL, WRITEMASK_XY);
1172 p->instructions->push_tail(assign);
1173
1174 p->texcoord_tex[bumpedUnitNr] = bumped;
1175 }
1176
1177 /**
1178 * Applies the fog calculations.
1179 *
1180 * This is basically like the ARB_fragment_prorgam fog options. Note
1181 * that ffvertex_prog.c produces fogcoord for us when
1182 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1183 */
1184 static ir_rvalue *
1185 emit_fog_instructions(struct texenv_fragment_program *p,
1186 ir_rvalue *fragcolor)
1187 {
1188 struct state_key *key = p->state;
1189 ir_rvalue *f, *temp;
1190 ir_variable *params, *oparams;
1191 ir_variable *fogcoord;
1192 ir_assignment *assign;
1193
1194 /* Temporary storage for the whole fog result. Fog calculations
1195 * only affect rgb so we're hanging on to the .a value of fragcolor
1196 * this way.
1197 */
1198 ir_variable *fog_result = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
1199 "fog_result",
1200 ir_var_auto);
1201 p->instructions->push_tail(fog_result);
1202 temp = new(p->mem_ctx) ir_dereference_variable(fog_result);
1203 assign = new(p->mem_ctx) ir_assignment(temp, fragcolor, NULL);
1204 p->instructions->push_tail(assign);
1205
1206 temp = new(p->mem_ctx) ir_dereference_variable(fog_result);
1207 fragcolor = new(p->mem_ctx) ir_swizzle(temp, 0, 1, 2, 3, 3);
1208
1209 oparams = p->shader->symbols->get_variable("gl_MESAFogParamsOptimized");
1210 fogcoord = p->shader->symbols->get_variable("gl_FogFragCoord");
1211 params = p->shader->symbols->get_variable("gl_Fog");
1212 f = new(p->mem_ctx) ir_dereference_variable(fogcoord);
1213
1214 ir_variable *f_var = new(p->mem_ctx) ir_variable(glsl_type::float_type,
1215 "fog_factor", ir_var_auto);
1216 p->instructions->push_tail(f_var);
1217
1218 switch (key->fog_mode) {
1219 case FOG_LINEAR:
1220 /* f = (end - z) / (end - start)
1221 *
1222 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1223 * (end / (end - start)) so we can generate a single MAD.
1224 */
1225 temp = new(p->mem_ctx) ir_dereference_variable(oparams);
1226 temp = new(p->mem_ctx) ir_swizzle(temp, 0, 0, 0, 0, 1);
1227 f = new(p->mem_ctx) ir_expression(ir_binop_mul, f, temp);
1228
1229 temp = new(p->mem_ctx) ir_dereference_variable(oparams);
1230 temp = new(p->mem_ctx) ir_swizzle(temp, 1, 0, 0, 0, 1);
1231 f = new(p->mem_ctx) ir_expression(ir_binop_add, f, temp);
1232 break;
1233 case FOG_EXP:
1234 /* f = e^(-(density * fogcoord))
1235 *
1236 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1237 * use EXP2 which is generally the native instruction without
1238 * having to do any further math on the fog density uniform.
1239 */
1240 temp = new(p->mem_ctx) ir_dereference_variable(oparams);
1241 temp = new(p->mem_ctx) ir_swizzle(temp, 2, 0, 0, 0, 1);
1242 f = new(p->mem_ctx) ir_expression(ir_binop_mul, f, temp);
1243 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
1244 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
1245 break;
1246 case FOG_EXP2:
1247 /* f = e^(-(density * fogcoord)^2)
1248 *
1249 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1250 * can do this like FOG_EXP but with a squaring after the
1251 * multiply by density.
1252 */
1253 ir_variable *temp_var = new(p->mem_ctx) ir_variable(glsl_type::float_type,
1254 "fog_temp",
1255 ir_var_auto);
1256 p->instructions->push_tail(temp_var);
1257
1258 temp = new(p->mem_ctx) ir_dereference_variable(oparams);
1259 temp = new(p->mem_ctx) ir_swizzle(temp, 3, 0, 0, 0, 1);
1260 f = new(p->mem_ctx) ir_expression(ir_binop_mul,
1261 f, temp);
1262
1263 temp = new(p->mem_ctx) ir_dereference_variable(temp_var);
1264 ir_assignment *assign = new(p->mem_ctx) ir_assignment(temp, f, NULL);
1265 p->instructions->push_tail(assign);
1266
1267 f = new(p->mem_ctx) ir_dereference_variable(temp_var);
1268 temp = new(p->mem_ctx) ir_dereference_variable(temp_var);
1269 f = new(p->mem_ctx) ir_expression(ir_binop_mul, f, temp);
1270 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
1271 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
1272 break;
1273 }
1274
1275 f = saturate(p, f);
1276
1277 temp = new(p->mem_ctx) ir_dereference_variable(f_var);
1278 assign = new(p->mem_ctx) ir_assignment(temp, f, NULL);
1279 p->instructions->push_tail(assign);
1280
1281 f = new(p->mem_ctx) ir_dereference_variable(f_var);
1282 f = new(p->mem_ctx) ir_expression(ir_binop_sub,
1283 new(p->mem_ctx) ir_constant(1.0f),
1284 f);
1285 temp = new(p->mem_ctx) ir_dereference_variable(params);
1286 temp = new(p->mem_ctx) ir_dereference_record(temp, "color");
1287 temp = new(p->mem_ctx) ir_swizzle(temp, 0, 1, 2, 3, 3);
1288 temp = new(p->mem_ctx) ir_expression(ir_binop_mul, temp, f);
1289
1290 f = new(p->mem_ctx) ir_dereference_variable(f_var);
1291 f = new(p->mem_ctx) ir_expression(ir_binop_mul, fragcolor, f);
1292 f = new(p->mem_ctx) ir_expression(ir_binop_add, temp, f);
1293
1294 ir_dereference *deref = new(p->mem_ctx) ir_dereference_variable(fog_result);
1295 assign = new(p->mem_ctx) ir_assignment(deref, f, NULL, WRITEMASK_XYZ);
1296 p->instructions->push_tail(assign);
1297
1298 return new(p->mem_ctx) ir_dereference_variable(fog_result);
1299 }
1300
1301 static void
1302 emit_instructions(struct texenv_fragment_program *p)
1303 {
1304 struct state_key *key = p->state;
1305 GLuint unit;
1306
1307 if (key->enabled_units) {
1308 /* Zeroth pass - bump map textures first */
1309 for (unit = 0; unit < key->nr_enabled_units; unit++) {
1310 if (key->unit[unit].enabled &&
1311 key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1312 load_texunit_bumpmap(p, unit);
1313 }
1314 }
1315
1316 /* First pass - to support texture_env_crossbar, first identify
1317 * all referenced texture sources and emit texld instructions
1318 * for each:
1319 */
1320 for (unit = 0; unit < key->nr_enabled_units; unit++)
1321 if (key->unit[unit].enabled) {
1322 load_texunit_sources(p, unit);
1323 p->last_tex_stage = unit;
1324 }
1325
1326 /* Second pass - emit combine instructions to build final color:
1327 */
1328 for (unit = 0; unit < key->nr_enabled_units; unit++) {
1329 if (key->unit[unit].enabled) {
1330 p->src_previous = emit_texenv(p, unit);
1331 }
1332 }
1333 }
1334
1335 ir_rvalue *cf = get_source(p, SRC_PREVIOUS, 0);
1336 ir_dereference_variable *deref;
1337 ir_assignment *assign;
1338
1339 if (key->separate_specular) {
1340 ir_rvalue *tmp0, *tmp1;
1341 ir_variable *spec_result = new(p->mem_ctx) ir_variable(glsl_type::vec4_type,
1342 "specular_add",
1343 ir_var_temporary);
1344
1345 p->instructions->push_tail(spec_result);
1346
1347 deref = new(p->mem_ctx) ir_dereference_variable(spec_result);
1348 assign = new(p->mem_ctx) ir_assignment(deref, cf, NULL);
1349 p->instructions->push_tail(assign);
1350
1351 deref = new(p->mem_ctx) ir_dereference_variable(spec_result);
1352 tmp0 = new(p->mem_ctx) ir_swizzle(deref, 0, 1, 2, 3, 3);
1353
1354 ir_variable *secondary =
1355 p->shader->symbols->get_variable("gl_SecondaryColor");
1356 assert(secondary);
1357 deref = new(p->mem_ctx) ir_dereference_variable(secondary);
1358 tmp1 = new(p->mem_ctx) ir_swizzle(deref, 0, 1, 2, 3, 3);
1359
1360 tmp0 = new(p->mem_ctx) ir_expression(ir_binop_add,
1361 tmp0, tmp1);
1362
1363 deref = new(p->mem_ctx) ir_dereference_variable(spec_result);
1364 assign = new(p->mem_ctx) ir_assignment(deref, tmp0, NULL, WRITEMASK_XYZ);
1365 p->instructions->push_tail(assign);
1366
1367 cf = new(p->mem_ctx) ir_dereference_variable(spec_result);
1368 }
1369
1370 if (key->fog_enabled) {
1371 cf = emit_fog_instructions(p, cf);
1372 }
1373
1374 ir_variable *frag_color = p->shader->symbols->get_variable("gl_FragColor");
1375 assert(frag_color);
1376 deref = new(p->mem_ctx) ir_dereference_variable(frag_color);
1377 assign = new(p->mem_ctx) ir_assignment(deref, cf, NULL);
1378 p->instructions->push_tail(assign);
1379 }
1380
1381 /**
1382 * Generate a new fragment program which implements the context's
1383 * current texture env/combine mode.
1384 */
1385 static struct gl_shader_program *
1386 create_new_program(struct gl_context *ctx, struct state_key *key)
1387 {
1388 struct texenv_fragment_program p;
1389 unsigned int unit;
1390 _mesa_glsl_parse_state *state;
1391
1392 memset(&p, 0, sizeof(p));
1393 p.mem_ctx = ralloc_context(NULL);
1394 p.shader = ctx->Driver.NewShader(ctx, 0, GL_FRAGMENT_SHADER);
1395 p.shader->ir = new(p.shader) exec_list;
1396 state = new(p.shader) _mesa_glsl_parse_state(ctx, GL_FRAGMENT_SHADER,
1397 p.shader);
1398 p.shader->symbols = state->symbols;
1399 p.top_instructions = p.shader->ir;
1400 p.instructions = p.shader->ir;
1401 p.state = key;
1402 p.shader_program = ctx->Driver.NewShaderProgram(ctx, 0);
1403
1404 state->language_version = 120;
1405 _mesa_glsl_initialize_types(state);
1406 _mesa_glsl_initialize_variables(p.instructions, state);
1407
1408 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
1409 p.src_texture[unit] = NULL;
1410 p.texcoord_tex[unit] = NULL;
1411 }
1412
1413 p.src_previous = NULL;
1414
1415 p.last_tex_stage = 0;
1416
1417 ir_function *main_f = new(p.mem_ctx) ir_function("main");
1418 p.instructions->push_tail(main_f);
1419 state->symbols->add_function(main_f);
1420
1421 ir_function_signature *main_sig =
1422 new(p.mem_ctx) ir_function_signature(p.shader->symbols->get_type("void"));
1423 main_sig->is_defined = true;
1424 main_f->add_signature(main_sig);
1425
1426 p.instructions = &main_sig->body;
1427 if (key->num_draw_buffers)
1428 emit_instructions(&p);
1429
1430 validate_ir_tree(p.shader->ir);
1431
1432 while (do_common_optimization(p.shader->ir, false, 32))
1433 ;
1434 reparent_ir(p.shader->ir, p.shader->ir);
1435
1436 p.shader->CompileStatus = true;
1437 p.shader->Version = state->language_version;
1438 p.shader->num_builtins_to_link = state->num_builtins_to_link;
1439 p.shader_program->Shaders =
1440 (gl_shader **)malloc(sizeof(*p.shader_program->Shaders));
1441 p.shader_program->Shaders[0] = p.shader;
1442 p.shader_program->NumShaders = 1;
1443
1444 _mesa_glsl_link_shader(ctx, p.shader_program);
1445
1446 /* Set the sampler uniforms, and relink to get them into the linked
1447 * program.
1448 */
1449 struct gl_fragment_program *fp = p.shader_program->FragmentProgram;
1450 for (unsigned int i = 0; i < MAX_TEXTURE_UNITS; i++) {
1451 char *name = ralloc_asprintf(p.mem_ctx, "sampler_%d", i);
1452 int loc = _mesa_get_uniform_location(ctx, p.shader_program, name);
1453 if (loc != -1) {
1454 /* Avoid using _mesa_uniform() because it flags state
1455 * updates, so if we're generating this shader_program in a
1456 * state update, we end up recursing. Instead, just set the
1457 * value, which is picked up at re-link.
1458 */
1459 loc = (loc & 0xffff) + (loc >> 16);
1460 int sampler = fp->Base.Parameters->ParameterValues[loc][0];
1461 fp->Base.SamplerUnits[sampler] = i;
1462 }
1463 }
1464 _mesa_update_shader_textures_used(&fp->Base);
1465 (void) ctx->Driver.ProgramStringNotify(ctx, fp->Base.Target, &fp->Base);
1466
1467 if (!p.shader_program->LinkStatus)
1468 _mesa_problem(ctx, "Failed to link fixed function fragment shader: %s\n",
1469 p.shader_program->InfoLog);
1470
1471 ralloc_free(p.mem_ctx);
1472 return p.shader_program;
1473 }
1474
1475 extern "C" {
1476
1477 /**
1478 * Return a fragment program which implements the current
1479 * fixed-function texture, fog and color-sum operations.
1480 */
1481 struct gl_shader_program *
1482 _mesa_get_fixed_func_fragment_program(struct gl_context *ctx)
1483 {
1484 struct gl_shader_program *shader_program;
1485 struct state_key key;
1486 GLuint keySize;
1487
1488 keySize = make_state_key(ctx, &key);
1489
1490 shader_program = (struct gl_shader_program *)
1491 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1492 &key, keySize);
1493
1494 if (!shader_program) {
1495 shader_program = create_new_program(ctx, &key);
1496
1497 _mesa_shader_cache_insert(ctx, ctx->FragmentProgram.Cache,
1498 &key, keySize, shader_program);
1499 }
1500
1501 return shader_program;
1502 }
1503
1504 }