c0030bc56878a5eb4326bf87786795b27fd9d752
[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/texenvprogram.h"
36 #include "main/texobj.h"
37 #include "main/uniforms.h"
38 #include "glsl/ir_builder.h"
39 #include "glsl/ir_optimization.h"
40 #include "glsl/glsl_parser_extras.h"
41 #include "glsl/glsl_symbol_table.h"
42 #include "glsl/glsl_types.h"
43 #include "program/ir_to_mesa.h"
44 #include "program/program.h"
45 #include "program/programopt.h"
46 #include "program/prog_cache.h"
47 #include "program/prog_instruction.h"
48 #include "program/prog_parameter.h"
49 #include "program/prog_print.h"
50 #include "program/prog_statevars.h"
51
52 using namespace ir_builder;
53
54 /*
55 * Note on texture units:
56 *
57 * The number of texture units supported by fixed-function fragment
58 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
59 * That's because there's a one-to-one correspondence between texture
60 * coordinates and samplers in fixed-function processing.
61 *
62 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
63 * sets of texcoords, so is fixed-function fragment processing.
64 *
65 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
66 */
67
68
69 struct texenvprog_cache_item
70 {
71 GLuint hash;
72 void *key;
73 struct gl_shader_program *data;
74 struct texenvprog_cache_item *next;
75 };
76
77 static GLboolean
78 texenv_doing_secondary_color(struct gl_context *ctx)
79 {
80 if (ctx->Light.Enabled &&
81 (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR))
82 return GL_TRUE;
83
84 if (ctx->Fog.ColorSumEnabled)
85 return GL_TRUE;
86
87 return GL_FALSE;
88 }
89
90 struct mode_opt {
91 #ifdef __GNUC__
92 __extension__ GLubyte Source:4; /**< SRC_x */
93 __extension__ GLubyte Operand:3; /**< OPR_x */
94 #else
95 GLubyte Source; /**< SRC_x */
96 GLubyte Operand; /**< OPR_x */
97 #endif
98 };
99
100 struct state_key {
101 GLuint nr_enabled_units:8;
102 GLuint enabled_units:8;
103 GLuint separate_specular:1;
104 GLuint fog_enabled: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_UNITS];
126 };
127
128 #define FOG_LINEAR 0
129 #define FOG_EXP 1
130 #define FOG_EXP2 2
131 #define FOG_UNKNOWN 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_UNKNOWN;
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] &&
303 ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX]->LinkStatus &&
304 ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX]->_LinkedShaders[MESA_SHADER_VERTEX]);
305 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
306 GLbitfield fp_inputs = 0x0;
307
308 if (ctx->VertexProgram._Overriden) {
309 /* Somebody's messing with the vertex program and we don't have
310 * a clue what's happening. Assume that it could be producing
311 * all possible outputs.
312 */
313 fp_inputs = ~0;
314 }
315 else if (ctx->RenderMode == GL_FEEDBACK) {
316 /* _NEW_RENDERMODE */
317 fp_inputs = (VARYING_BIT_COL0 | VARYING_BIT_TEX0);
318 }
319 else if (!(vertexProgram || vertexShader)) {
320 /* Fixed function vertex logic */
321 /* _NEW_VARYING_VP_INPUTS */
322 GLbitfield64 varying_inputs = ctx->varying_vp_inputs;
323
324 /* These get generated in the setup routine regardless of the
325 * vertex program:
326 */
327 /* _NEW_POINT */
328 if (ctx->Point.PointSprite)
329 varying_inputs |= VARYING_BITS_TEX_ANY;
330
331 /* First look at what values may be computed by the generated
332 * vertex program:
333 */
334 /* _NEW_LIGHT */
335 if (ctx->Light.Enabled) {
336 fp_inputs |= VARYING_BIT_COL0;
337
338 if (texenv_doing_secondary_color(ctx))
339 fp_inputs |= VARYING_BIT_COL1;
340 }
341
342 /* _NEW_TEXTURE */
343 fp_inputs |= (ctx->Texture._TexGenEnabled |
344 ctx->Texture._TexMatEnabled) << VARYING_SLOT_TEX0;
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 fp_inputs |= VARYING_BIT_COL0;
351 if (varying_inputs & VERT_BIT_COLOR1)
352 fp_inputs |= VARYING_BIT_COL1;
353
354 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
355 << VARYING_SLOT_TEX0);
356
357 }
358 else {
359 /* calculate from vp->outputs */
360 struct gl_program *vprog;
361 GLbitfield64 vp_outputs;
362
363 /* Choose GLSL vertex shader over ARB vertex program. Need this
364 * since vertex shader state validation comes after fragment state
365 * validation (see additional comments in state.c).
366 */
367 if (vertexShader)
368 vprog = ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX]->_LinkedShaders[MESA_SHADER_VERTEX]->Program;
369 else
370 vprog = &ctx->VertexProgram.Current->Base;
371
372 vp_outputs = vprog->OutputsWritten;
373
374 /* These get generated in the setup routine regardless of the
375 * vertex program:
376 */
377 /* _NEW_POINT */
378 if (ctx->Point.PointSprite)
379 vp_outputs |= VARYING_BITS_TEX_ANY;
380
381 if (vp_outputs & (1 << VARYING_SLOT_COL0))
382 fp_inputs |= VARYING_BIT_COL0;
383 if (vp_outputs & (1 << VARYING_SLOT_COL1))
384 fp_inputs |= VARYING_BIT_COL1;
385
386 fp_inputs |= (((vp_outputs & VARYING_BITS_TEX_ANY) >> VARYING_SLOT_TEX0)
387 << VARYING_SLOT_TEX0);
388 }
389
390 return fp_inputs;
391 }
392
393
394 /**
395 * Examine current texture environment state and generate a unique
396 * key to identify it.
397 */
398 static GLuint make_state_key( struct gl_context *ctx, struct state_key *key )
399 {
400 GLuint i, j;
401 GLbitfield inputs_referenced = VARYING_BIT_COL0;
402 const GLbitfield inputs_available = get_fp_input_mask( ctx );
403 GLuint keySize;
404
405 memset(key, 0, sizeof(*key));
406
407 /* _NEW_TEXTURE */
408 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
409 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
410 const struct gl_texture_object *texObj = texUnit->_Current;
411 const struct gl_tex_env_combine_state *comb = texUnit->_CurrentCombine;
412 const struct gl_sampler_object *samp;
413 GLenum format;
414
415 if (!texUnit->_Current || !texUnit->Enabled)
416 continue;
417
418 samp = _mesa_get_samplerobj(ctx, i);
419 format = _mesa_texture_base_format(texObj);
420
421 key->unit[i].enabled = 1;
422 key->enabled_units |= (1<<i);
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_enabled = 1;
462 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
463 inputs_referenced |= VARYING_BIT_FOGC; /* maybe */
464 }
465
466 /* _NEW_BUFFERS */
467 key->num_draw_buffers = ctx->DrawBuffer->_NumColorDrawBuffers;
468
469 /* _NEW_COLOR */
470 if (ctx->Color.AlphaEnabled && key->num_draw_buffers == 0) {
471 /* if alpha test is enabled we need to emit at least one color */
472 key->num_draw_buffers = 1;
473 }
474
475 key->inputs_available = (inputs_available & inputs_referenced);
476
477 /* compute size of state key, ignoring unused texture units */
478 keySize = sizeof(*key) - sizeof(key->unit)
479 + key->nr_enabled_units * sizeof(key->unit[0]);
480
481 return keySize;
482 }
483
484
485 /** State used to build the fragment program:
486 */
487 class texenv_fragment_program : public ir_factory {
488 public:
489 struct gl_shader_program *shader_program;
490 struct gl_shader *shader;
491 exec_list *top_instructions;
492 struct state_key *state;
493
494 ir_variable *src_texture[MAX_TEXTURE_COORD_UNITS];
495 /* Reg containing each texture unit's sampled texture color,
496 * else undef.
497 */
498
499 /* Texcoord override from bumpmapping. */
500 ir_variable *texcoord_tex[MAX_TEXTURE_COORD_UNITS];
501
502 /* Reg containing texcoord for a texture unit,
503 * needed for bump mapping, else undef.
504 */
505
506 ir_rvalue *src_previous; /**< Reg containing color from previous
507 * stage. May need to be decl'd.
508 */
509 };
510
511 static ir_rvalue *
512 get_current_attrib(texenv_fragment_program *p, GLuint attrib)
513 {
514 ir_variable *current;
515 ir_rvalue *val;
516
517 current = p->shader->symbols->get_variable("gl_CurrentAttribFragMESA");
518 assert(current);
519 current->data.max_array_access = MAX2(current->data.max_array_access, attrib);
520 val = new(p->mem_ctx) ir_dereference_variable(current);
521 ir_rvalue *index = new(p->mem_ctx) ir_constant(attrib);
522 return new(p->mem_ctx) ir_dereference_array(val, index);
523 }
524
525 static ir_rvalue *
526 get_gl_Color(texenv_fragment_program *p)
527 {
528 if (p->state->inputs_available & VARYING_BIT_COL0) {
529 ir_variable *var = p->shader->symbols->get_variable("gl_Color");
530 assert(var);
531 return new(p->mem_ctx) ir_dereference_variable(var);
532 } else {
533 return get_current_attrib(p, VERT_ATTRIB_COLOR0);
534 }
535 }
536
537 static ir_rvalue *
538 get_source(texenv_fragment_program *p,
539 GLuint src, GLuint unit)
540 {
541 ir_variable *var;
542 ir_dereference *deref;
543
544 switch (src) {
545 case SRC_TEXTURE:
546 return new(p->mem_ctx) ir_dereference_variable(p->src_texture[unit]);
547
548 case SRC_TEXTURE0:
549 case SRC_TEXTURE1:
550 case SRC_TEXTURE2:
551 case SRC_TEXTURE3:
552 case SRC_TEXTURE4:
553 case SRC_TEXTURE5:
554 case SRC_TEXTURE6:
555 case SRC_TEXTURE7:
556 return new(p->mem_ctx)
557 ir_dereference_variable(p->src_texture[src - SRC_TEXTURE0]);
558
559 case SRC_CONSTANT:
560 var = p->shader->symbols->get_variable("gl_TextureEnvColor");
561 assert(var);
562 deref = new(p->mem_ctx) ir_dereference_variable(var);
563 var->data.max_array_access = MAX2(var->data.max_array_access, unit);
564 return new(p->mem_ctx) ir_dereference_array(deref,
565 new(p->mem_ctx) ir_constant(unit));
566
567 case SRC_PRIMARY_COLOR:
568 var = p->shader->symbols->get_variable("gl_Color");
569 assert(var);
570 return new(p->mem_ctx) ir_dereference_variable(var);
571
572 case SRC_ZERO:
573 return new(p->mem_ctx) ir_constant(0.0f);
574
575 case SRC_PREVIOUS:
576 if (!p->src_previous) {
577 return get_gl_Color(p);
578 } else {
579 return p->src_previous->clone(p->mem_ctx, NULL);
580 }
581
582 default:
583 assert(0);
584 return NULL;
585 }
586 }
587
588 static ir_rvalue *
589 emit_combine_source(texenv_fragment_program *p,
590 GLuint unit,
591 GLuint source,
592 GLuint operand)
593 {
594 ir_rvalue *src;
595
596 src = get_source(p, source, unit);
597
598 switch (operand) {
599 case OPR_ONE_MINUS_SRC_COLOR:
600 return sub(new(p->mem_ctx) ir_constant(1.0f), src);
601
602 case OPR_SRC_ALPHA:
603 return src->type->is_scalar() ? src : swizzle_w(src);
604
605 case OPR_ONE_MINUS_SRC_ALPHA: {
606 ir_rvalue *const scalar = src->type->is_scalar() ? src : swizzle_w(src);
607
608 return sub(new(p->mem_ctx) ir_constant(1.0f), scalar);
609 }
610
611 case OPR_ZERO:
612 return new(p->mem_ctx) ir_constant(0.0f);
613 case OPR_ONE:
614 return new(p->mem_ctx) ir_constant(1.0f);
615 case OPR_SRC_COLOR:
616 return src;
617 default:
618 assert(0);
619 return src;
620 }
621 }
622
623 /**
624 * Check if the RGB and Alpha sources and operands match for the given
625 * texture unit's combinder state. When the RGB and A sources and
626 * operands match, we can emit fewer instructions.
627 */
628 static GLboolean args_match( const struct state_key *key, GLuint unit )
629 {
630 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
631
632 for (i = 0; i < numArgs; i++) {
633 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
634 return GL_FALSE;
635
636 switch (key->unit[unit].OptA[i].Operand) {
637 case OPR_SRC_ALPHA:
638 switch (key->unit[unit].OptRGB[i].Operand) {
639 case OPR_SRC_COLOR:
640 case OPR_SRC_ALPHA:
641 break;
642 default:
643 return GL_FALSE;
644 }
645 break;
646 case OPR_ONE_MINUS_SRC_ALPHA:
647 switch (key->unit[unit].OptRGB[i].Operand) {
648 case OPR_ONE_MINUS_SRC_COLOR:
649 case OPR_ONE_MINUS_SRC_ALPHA:
650 break;
651 default:
652 return GL_FALSE;
653 }
654 break;
655 default:
656 return GL_FALSE; /* impossible */
657 }
658 }
659
660 return GL_TRUE;
661 }
662
663 static ir_rvalue *
664 smear(ir_rvalue *val)
665 {
666 if (!val->type->is_scalar())
667 return val;
668
669 return swizzle_xxxx(val);
670 }
671
672 static ir_rvalue *
673 emit_combine(texenv_fragment_program *p,
674 GLuint unit,
675 GLuint nr,
676 GLuint mode,
677 const struct mode_opt *opt)
678 {
679 ir_rvalue *src[MAX_COMBINER_TERMS];
680 ir_rvalue *tmp0, *tmp1;
681 GLuint i;
682
683 assert(nr <= MAX_COMBINER_TERMS);
684
685 for (i = 0; i < nr; i++)
686 src[i] = emit_combine_source( p, unit, opt[i].Source, opt[i].Operand );
687
688 switch (mode) {
689 case MODE_REPLACE:
690 return src[0];
691
692 case MODE_MODULATE:
693 return mul(src[0], src[1]);
694
695 case MODE_ADD:
696 return add(src[0], src[1]);
697
698 case MODE_ADD_SIGNED:
699 return add(add(src[0], src[1]), new(p->mem_ctx) ir_constant(-0.5f));
700
701 case MODE_INTERPOLATE:
702 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
703 tmp0 = mul(src[0], src[2]);
704 tmp1 = mul(src[1], sub(new(p->mem_ctx) ir_constant(1.0f),
705 src[2]->clone(p->mem_ctx, NULL)));
706 return add(tmp0, tmp1);
707
708 case MODE_SUBTRACT:
709 return sub(src[0], src[1]);
710
711 case MODE_DOT3_RGBA:
712 case MODE_DOT3_RGBA_EXT:
713 case MODE_DOT3_RGB_EXT:
714 case MODE_DOT3_RGB: {
715 tmp0 = mul(src[0], new(p->mem_ctx) ir_constant(2.0f));
716 tmp0 = add(tmp0, new(p->mem_ctx) ir_constant(-1.0f));
717
718 tmp1 = mul(src[1], new(p->mem_ctx) ir_constant(2.0f));
719 tmp1 = add(tmp1, new(p->mem_ctx) ir_constant(-1.0f));
720
721 return dot(swizzle_xyz(smear(tmp0)), swizzle_xyz(smear(tmp1)));
722 }
723 case MODE_MODULATE_ADD_ATI:
724 return add(mul(src[0], src[2]), src[1]);
725
726 case MODE_MODULATE_SIGNED_ADD_ATI:
727 return add(add(mul(src[0], src[2]), src[1]),
728 new(p->mem_ctx) ir_constant(-0.5f));
729
730 case MODE_MODULATE_SUBTRACT_ATI:
731 return sub(mul(src[0], src[2]), src[1]);
732
733 case MODE_ADD_PRODUCTS:
734 return add(mul(src[0], src[1]), mul(src[2], src[3]));
735
736 case MODE_ADD_PRODUCTS_SIGNED:
737 return add(add(mul(src[0], src[1]), mul(src[2], src[3])),
738 new(p->mem_ctx) ir_constant(-0.5f));
739 default:
740 assert(0);
741 return src[0];
742 }
743 }
744
745 /**
746 * Generate instructions for one texture unit's env/combiner mode.
747 */
748 static ir_rvalue *
749 emit_texenv(texenv_fragment_program *p, GLuint unit)
750 {
751 const struct state_key *key = p->state;
752 GLboolean rgb_saturate, alpha_saturate;
753 GLuint rgb_shift, alpha_shift;
754
755 if (!key->unit[unit].enabled) {
756 return get_source(p, SRC_PREVIOUS, 0);
757 }
758
759 switch (key->unit[unit].ModeRGB) {
760 case MODE_DOT3_RGB_EXT:
761 alpha_shift = key->unit[unit].ScaleShiftA;
762 rgb_shift = 0;
763 break;
764 case MODE_DOT3_RGBA_EXT:
765 alpha_shift = 0;
766 rgb_shift = 0;
767 break;
768 default:
769 rgb_shift = key->unit[unit].ScaleShiftRGB;
770 alpha_shift = key->unit[unit].ScaleShiftA;
771 break;
772 }
773
774 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
775 * We don't want to clamp twice.
776 */
777 if (rgb_shift)
778 rgb_saturate = GL_FALSE; /* saturate after rgb shift */
779 else if (need_saturate(key->unit[unit].ModeRGB))
780 rgb_saturate = GL_TRUE;
781 else
782 rgb_saturate = GL_FALSE;
783
784 if (alpha_shift)
785 alpha_saturate = GL_FALSE; /* saturate after alpha shift */
786 else if (need_saturate(key->unit[unit].ModeA))
787 alpha_saturate = GL_TRUE;
788 else
789 alpha_saturate = GL_FALSE;
790
791 ir_variable *temp_var = p->make_temp(glsl_type::vec4_type, "texenv_combine");
792 ir_dereference *deref;
793 ir_rvalue *val;
794
795 /* Emit the RGB and A combine ops
796 */
797 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
798 args_match(key, unit)) {
799 val = emit_combine(p, unit,
800 key->unit[unit].NumArgsRGB,
801 key->unit[unit].ModeRGB,
802 key->unit[unit].OptRGB);
803 val = smear(val);
804 if (rgb_saturate)
805 val = saturate(val);
806
807 p->emit(assign(temp_var, val));
808 }
809 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
810 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
811 ir_rvalue *val = emit_combine(p, unit,
812 key->unit[unit].NumArgsRGB,
813 key->unit[unit].ModeRGB,
814 key->unit[unit].OptRGB);
815 val = smear(val);
816 if (rgb_saturate)
817 val = saturate(val);
818 p->emit(assign(temp_var, val));
819 }
820 else {
821 /* Need to do something to stop from re-emitting identical
822 * argument calculations here:
823 */
824 val = emit_combine(p, unit,
825 key->unit[unit].NumArgsRGB,
826 key->unit[unit].ModeRGB,
827 key->unit[unit].OptRGB);
828 val = swizzle_xyz(smear(val));
829 if (rgb_saturate)
830 val = saturate(val);
831 p->emit(assign(temp_var, val, WRITEMASK_XYZ));
832
833 val = emit_combine(p, unit,
834 key->unit[unit].NumArgsA,
835 key->unit[unit].ModeA,
836 key->unit[unit].OptA);
837 val = swizzle_w(smear(val));
838 if (alpha_saturate)
839 val = saturate(val);
840 p->emit(assign(temp_var, val, WRITEMASK_W));
841 }
842
843 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
844
845 /* Deal with the final shift:
846 */
847 if (alpha_shift || rgb_shift) {
848 ir_constant *shift;
849
850 if (rgb_shift == alpha_shift) {
851 shift = new(p->mem_ctx) ir_constant((float)(1 << rgb_shift));
852 }
853 else {
854 ir_constant_data const_data;
855
856 const_data.f[0] = float(1 << rgb_shift);
857 const_data.f[1] = float(1 << rgb_shift);
858 const_data.f[2] = float(1 << rgb_shift);
859 const_data.f[3] = float(1 << alpha_shift);
860
861 shift = new(p->mem_ctx) ir_constant(glsl_type::vec4_type,
862 &const_data);
863 }
864
865 return saturate(mul(deref, shift));
866 }
867 else
868 return deref;
869 }
870
871
872 /**
873 * Generate instruction for getting a texture source term.
874 */
875 static void load_texture( texenv_fragment_program *p, GLuint unit )
876 {
877 ir_dereference *deref;
878
879 if (p->src_texture[unit])
880 return;
881
882 const GLuint texTarget = p->state->unit[unit].source_index;
883 ir_rvalue *texcoord;
884
885 if (!(p->state->inputs_available & (VARYING_BIT_TEX0 << unit))) {
886 texcoord = get_current_attrib(p, VERT_ATTRIB_TEX0 + unit);
887 } else if (p->texcoord_tex[unit]) {
888 texcoord = new(p->mem_ctx) ir_dereference_variable(p->texcoord_tex[unit]);
889 } else {
890 ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
891 assert(tc_array);
892 texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
893 ir_rvalue *index = new(p->mem_ctx) ir_constant(unit);
894 texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
895 tc_array->data.max_array_access = MAX2(tc_array->data.max_array_access, unit);
896 }
897
898 if (!p->state->unit[unit].enabled) {
899 p->src_texture[unit] = p->make_temp(glsl_type::vec4_type,
900 "dummy_tex");
901 p->emit(p->src_texture[unit]);
902
903 p->emit(assign(p->src_texture[unit], new(p->mem_ctx) ir_constant(0.0f)));
904 return ;
905 }
906
907 const glsl_type *sampler_type = NULL;
908 int coords = 0;
909
910 switch (texTarget) {
911 case TEXTURE_1D_INDEX:
912 if (p->state->unit[unit].shadow)
913 sampler_type = glsl_type::sampler1DShadow_type;
914 else
915 sampler_type = glsl_type::sampler1D_type;
916 coords = 1;
917 break;
918 case TEXTURE_1D_ARRAY_INDEX:
919 if (p->state->unit[unit].shadow)
920 sampler_type = glsl_type::sampler1DArrayShadow_type;
921 else
922 sampler_type = glsl_type::sampler1DArray_type;
923 coords = 2;
924 break;
925 case TEXTURE_2D_INDEX:
926 if (p->state->unit[unit].shadow)
927 sampler_type = glsl_type::sampler2DShadow_type;
928 else
929 sampler_type = glsl_type::sampler2D_type;
930 coords = 2;
931 break;
932 case TEXTURE_2D_ARRAY_INDEX:
933 if (p->state->unit[unit].shadow)
934 sampler_type = glsl_type::sampler2DArrayShadow_type;
935 else
936 sampler_type = glsl_type::sampler2DArray_type;
937 coords = 3;
938 break;
939 case TEXTURE_RECT_INDEX:
940 if (p->state->unit[unit].shadow)
941 sampler_type = glsl_type::sampler2DRectShadow_type;
942 else
943 sampler_type = glsl_type::sampler2DRect_type;
944 coords = 2;
945 break;
946 case TEXTURE_3D_INDEX:
947 assert(!p->state->unit[unit].shadow);
948 sampler_type = glsl_type::sampler3D_type;
949 coords = 3;
950 break;
951 case TEXTURE_CUBE_INDEX:
952 if (p->state->unit[unit].shadow)
953 sampler_type = glsl_type::samplerCubeShadow_type;
954 else
955 sampler_type = glsl_type::samplerCube_type;
956 coords = 3;
957 break;
958 case TEXTURE_EXTERNAL_INDEX:
959 assert(!p->state->unit[unit].shadow);
960 sampler_type = glsl_type::samplerExternalOES_type;
961 coords = 2;
962 break;
963 }
964
965 p->src_texture[unit] = p->make_temp(glsl_type::vec4_type,
966 "tex");
967
968 ir_texture *tex = new(p->mem_ctx) ir_texture(ir_tex);
969
970
971 char *sampler_name = ralloc_asprintf(p->mem_ctx, "sampler_%d", unit);
972 ir_variable *sampler = new(p->mem_ctx) ir_variable(sampler_type,
973 sampler_name,
974 ir_var_uniform);
975 p->top_instructions->push_head(sampler);
976
977 /* Set the texture unit for this sampler. The linker will pick this value
978 * up and do-the-right-thing.
979 *
980 * NOTE: The cast to int is important. Without it, the constant will have
981 * type uint, and things later on may get confused.
982 */
983 sampler->constant_value = new(p->mem_ctx) ir_constant(int(unit));
984
985 deref = new(p->mem_ctx) ir_dereference_variable(sampler);
986 tex->set_sampler(deref, glsl_type::vec4_type);
987
988 tex->coordinate = new(p->mem_ctx) ir_swizzle(texcoord, 0, 1, 2, 3, coords);
989
990 if (p->state->unit[unit].shadow) {
991 texcoord = texcoord->clone(p->mem_ctx, NULL);
992 tex->shadow_comparitor = new(p->mem_ctx) ir_swizzle(texcoord,
993 coords, 0, 0, 0,
994 1);
995 coords++;
996 }
997
998 texcoord = texcoord->clone(p->mem_ctx, NULL);
999 tex->projector = swizzle_w(texcoord);
1000
1001 p->emit(assign(p->src_texture[unit], tex));
1002 }
1003
1004 static void
1005 load_texenv_source(texenv_fragment_program *p,
1006 GLuint src, GLuint unit)
1007 {
1008 switch (src) {
1009 case SRC_TEXTURE:
1010 load_texture(p, unit);
1011 break;
1012
1013 case SRC_TEXTURE0:
1014 case SRC_TEXTURE1:
1015 case SRC_TEXTURE2:
1016 case SRC_TEXTURE3:
1017 case SRC_TEXTURE4:
1018 case SRC_TEXTURE5:
1019 case SRC_TEXTURE6:
1020 case SRC_TEXTURE7:
1021 load_texture(p, src - SRC_TEXTURE0);
1022 break;
1023
1024 default:
1025 /* not a texture src - do nothing */
1026 break;
1027 }
1028 }
1029
1030
1031 /**
1032 * Generate instructions for loading all texture source terms.
1033 */
1034 static GLboolean
1035 load_texunit_sources( texenv_fragment_program *p, GLuint unit )
1036 {
1037 const struct state_key *key = p->state;
1038 GLuint i;
1039
1040 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1041 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
1042 }
1043
1044 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1045 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1046 }
1047
1048 return GL_TRUE;
1049 }
1050
1051 /**
1052 * Applies the fog calculations.
1053 *
1054 * This is basically like the ARB_fragment_prorgam fog options. Note
1055 * that ffvertex_prog.c produces fogcoord for us when
1056 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1057 */
1058 static ir_rvalue *
1059 emit_fog_instructions(texenv_fragment_program *p,
1060 ir_rvalue *fragcolor)
1061 {
1062 struct state_key *key = p->state;
1063 ir_rvalue *f, *temp;
1064 ir_variable *params, *oparams;
1065 ir_variable *fogcoord;
1066
1067 /* Temporary storage for the whole fog result. Fog calculations
1068 * only affect rgb so we're hanging on to the .a value of fragcolor
1069 * this way.
1070 */
1071 ir_variable *fog_result = p->make_temp(glsl_type::vec4_type, "fog_result");
1072 p->emit(assign(fog_result, fragcolor));
1073
1074 fragcolor = swizzle_xyz(fog_result);
1075
1076 oparams = p->shader->symbols->get_variable("gl_FogParamsOptimizedMESA");
1077 assert(oparams);
1078 fogcoord = p->shader->symbols->get_variable("gl_FogFragCoord");
1079 assert(fogcoord);
1080 params = p->shader->symbols->get_variable("gl_Fog");
1081 assert(params);
1082 f = new(p->mem_ctx) ir_dereference_variable(fogcoord);
1083
1084 ir_variable *f_var = p->make_temp(glsl_type::float_type, "fog_factor");
1085
1086 switch (key->fog_mode) {
1087 case FOG_LINEAR:
1088 /* f = (end - z) / (end - start)
1089 *
1090 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1091 * (end / (end - start)) so we can generate a single MAD.
1092 */
1093 f = add(mul(f, swizzle_x(oparams)), swizzle_y(oparams));
1094 break;
1095 case FOG_EXP:
1096 /* f = e^(-(density * fogcoord))
1097 *
1098 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1099 * use EXP2 which is generally the native instruction without
1100 * having to do any further math on the fog density uniform.
1101 */
1102 f = mul(f, swizzle_z(oparams));
1103 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
1104 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
1105 break;
1106 case FOG_EXP2:
1107 /* f = e^(-(density * fogcoord)^2)
1108 *
1109 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1110 * can do this like FOG_EXP but with a squaring after the
1111 * multiply by density.
1112 */
1113 ir_variable *temp_var = p->make_temp(glsl_type::float_type, "fog_temp");
1114 p->emit(assign(temp_var, mul(f, swizzle_w(oparams))));
1115
1116 f = mul(temp_var, temp_var);
1117 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
1118 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
1119 break;
1120 }
1121
1122 p->emit(assign(f_var, saturate(f)));
1123
1124 f = sub(new(p->mem_ctx) ir_constant(1.0f), f_var);
1125 temp = new(p->mem_ctx) ir_dereference_variable(params);
1126 temp = new(p->mem_ctx) ir_dereference_record(temp, "color");
1127 temp = mul(swizzle_xyz(temp), f);
1128
1129 p->emit(assign(fog_result, add(temp, mul(fragcolor, f_var)), WRITEMASK_XYZ));
1130
1131 return new(p->mem_ctx) ir_dereference_variable(fog_result);
1132 }
1133
1134 static void
1135 emit_instructions(texenv_fragment_program *p)
1136 {
1137 struct state_key *key = p->state;
1138 GLuint unit;
1139
1140 if (key->enabled_units) {
1141 /* First pass - to support texture_env_crossbar, first identify
1142 * all referenced texture sources and emit texld instructions
1143 * for each:
1144 */
1145 for (unit = 0; unit < key->nr_enabled_units; unit++)
1146 if (key->unit[unit].enabled) {
1147 load_texunit_sources(p, unit);
1148 }
1149
1150 /* Second pass - emit combine instructions to build final color:
1151 */
1152 for (unit = 0; unit < key->nr_enabled_units; unit++) {
1153 if (key->unit[unit].enabled) {
1154 p->src_previous = emit_texenv(p, unit);
1155 }
1156 }
1157 }
1158
1159 ir_rvalue *cf = get_source(p, SRC_PREVIOUS, 0);
1160
1161 if (key->separate_specular) {
1162 ir_variable *spec_result = p->make_temp(glsl_type::vec4_type,
1163 "specular_add");
1164 p->emit(assign(spec_result, cf));
1165
1166 ir_rvalue *secondary;
1167 if (p->state->inputs_available & VARYING_BIT_COL1) {
1168 ir_variable *var =
1169 p->shader->symbols->get_variable("gl_SecondaryColor");
1170 assert(var);
1171 secondary = swizzle_xyz(var);
1172 } else {
1173 secondary = swizzle_xyz(get_current_attrib(p, VERT_ATTRIB_COLOR1));
1174 }
1175
1176 p->emit(assign(spec_result, add(swizzle_xyz(spec_result), secondary),
1177 WRITEMASK_XYZ));
1178
1179 cf = new(p->mem_ctx) ir_dereference_variable(spec_result);
1180 }
1181
1182 if (key->fog_enabled) {
1183 cf = emit_fog_instructions(p, cf);
1184 }
1185
1186 ir_variable *frag_color = p->shader->symbols->get_variable("gl_FragColor");
1187 assert(frag_color);
1188 p->emit(assign(frag_color, cf));
1189 }
1190
1191 /**
1192 * Generate a new fragment program which implements the context's
1193 * current texture env/combine mode.
1194 */
1195 static struct gl_shader_program *
1196 create_new_program(struct gl_context *ctx, struct state_key *key)
1197 {
1198 texenv_fragment_program p;
1199 unsigned int unit;
1200 _mesa_glsl_parse_state *state;
1201
1202 p.mem_ctx = ralloc_context(NULL);
1203 p.shader = ctx->Driver.NewShader(ctx, 0, GL_FRAGMENT_SHADER);
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 = ctx->Driver.NewShaderProgram(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 state->language_version = 130;
1222 state->es_shader = false;
1223 if (_mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external)
1224 state->OES_EGL_image_external_enable = true;
1225 _mesa_glsl_initialize_types(state);
1226 _mesa_glsl_initialize_variables(p.instructions, state);
1227
1228 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
1229 p.src_texture[unit] = NULL;
1230 p.texcoord_tex[unit] = NULL;
1231 }
1232
1233 p.src_previous = NULL;
1234
1235 ir_function *main_f = new(p.mem_ctx) ir_function("main");
1236 p.emit(main_f);
1237 state->symbols->add_function(main_f);
1238
1239 ir_function_signature *main_sig =
1240 new(p.mem_ctx) ir_function_signature(glsl_type::void_type);
1241 main_sig->is_defined = true;
1242 main_f->add_signature(main_sig);
1243
1244 p.instructions = &main_sig->body;
1245 if (key->num_draw_buffers)
1246 emit_instructions(&p);
1247
1248 validate_ir_tree(p.shader->ir);
1249
1250 const struct gl_shader_compiler_options *options =
1251 &ctx->Const.ShaderCompilerOptions[MESA_SHADER_FRAGMENT];
1252
1253 while (do_common_optimization(p.shader->ir, false, false, options,
1254 ctx->Const.NativeIntegers))
1255 ;
1256 reparent_ir(p.shader->ir, p.shader->ir);
1257
1258 p.shader->CompileStatus = true;
1259 p.shader->Version = state->language_version;
1260 p.shader->uses_builtin_functions = state->uses_builtin_functions;
1261 p.shader_program->Shaders =
1262 (gl_shader **)malloc(sizeof(*p.shader_program->Shaders));
1263 p.shader_program->Shaders[0] = p.shader;
1264 p.shader_program->NumShaders = 1;
1265
1266 _mesa_glsl_link_shader(ctx, p.shader_program);
1267
1268 if (!p.shader_program->LinkStatus)
1269 _mesa_problem(ctx, "Failed to link fixed function fragment shader: %s\n",
1270 p.shader_program->InfoLog);
1271
1272 ralloc_free(p.mem_ctx);
1273 return p.shader_program;
1274 }
1275
1276 extern "C" {
1277
1278 /**
1279 * Return a fragment program which implements the current
1280 * fixed-function texture, fog and color-sum operations.
1281 */
1282 struct gl_shader_program *
1283 _mesa_get_fixed_func_fragment_program(struct gl_context *ctx)
1284 {
1285 struct gl_shader_program *shader_program;
1286 struct state_key key;
1287 GLuint keySize;
1288
1289 keySize = make_state_key(ctx, &key);
1290
1291 shader_program = (struct gl_shader_program *)
1292 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1293 &key, keySize);
1294
1295 if (!shader_program) {
1296 shader_program = create_new_program(ctx, &key);
1297
1298 _mesa_shader_cache_insert(ctx, ctx->FragmentProgram.Cache,
1299 &key, keySize, shader_program);
1300 }
1301
1302 return shader_program;
1303 }
1304
1305 }