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