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