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