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