1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2009 VMware, Inc. All Rights Reserved.
6 * Copyright © 2010-2011 Intel Corporation
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:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
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 TUNGSTEN GRAPHICS 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.
28 **************************************************************************/
34 #include "main/uniforms.h"
35 #include "main/macros.h"
36 #include "program/program.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_cache.h"
39 #include "program/prog_instruction.h"
40 #include "program/prog_print.h"
41 #include "program/prog_statevars.h"
42 #include "program/programopt.h"
43 #include "texenvprogram.h"
45 #include "main/uniforms.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 "../glsl/ir_print_visitor.h"
53 #include "../program/ir_to_mesa.h"
55 using namespace ir_builder
;
58 * Note on texture units:
60 * The number of texture units supported by fixed-function fragment
61 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
62 * That's because there's a one-to-one correspondence between texture
63 * coordinates and samplers in fixed-function processing.
65 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
66 * sets of texcoords, so is fixed-function fragment processing.
68 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
72 struct texenvprog_cache_item
76 struct gl_shader_program
*data
;
77 struct texenvprog_cache_item
*next
;
81 texenv_doing_secondary_color(struct gl_context
*ctx
)
83 if (ctx
->Light
.Enabled
&&
84 (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
))
87 if (ctx
->Fog
.ColorSumEnabled
)
95 __extension__ GLubyte Source
:4; /**< SRC_x */
96 __extension__ GLubyte Operand
:3; /**< OPR_x */
98 GLubyte Source
; /**< SRC_x */
99 GLubyte Operand
; /**< OPR_x */
104 GLuint nr_enabled_units
:8;
105 GLuint enabled_units
:8;
106 GLuint separate_specular
:1;
107 GLuint fog_enabled
:1;
108 GLuint fog_mode
:2; /**< FOG_x */
109 GLuint inputs_available
:12;
110 GLuint num_draw_buffers
:4;
112 /* NOTE: This array of structs must be last! (see "keySize" below) */
115 GLuint source_index
:4; /**< TEXTURE_x_INDEX */
117 GLuint ScaleShiftRGB
:2;
118 GLuint ScaleShiftA
:2;
120 GLuint NumArgsRGB
:3; /**< up to MAX_COMBINER_TERMS */
121 GLuint ModeRGB
:5; /**< MODE_x */
123 GLuint NumArgsA
:3; /**< up to MAX_COMBINER_TERMS */
124 GLuint ModeA
:5; /**< MODE_x */
126 struct mode_opt OptRGB
[MAX_COMBINER_TERMS
];
127 struct mode_opt OptA
[MAX_COMBINER_TERMS
];
128 } unit
[MAX_TEXTURE_UNITS
];
134 #define FOG_UNKNOWN 3
136 static GLuint
translate_fog_mode( GLenum mode
)
139 case GL_LINEAR
: return FOG_LINEAR
;
140 case GL_EXP
: return FOG_EXP
;
141 case GL_EXP2
: return FOG_EXP2
;
142 default: return FOG_UNKNOWN
;
146 #define OPR_SRC_COLOR 0
147 #define OPR_ONE_MINUS_SRC_COLOR 1
148 #define OPR_SRC_ALPHA 2
149 #define OPR_ONE_MINUS_SRC_ALPHA 3
152 #define OPR_UNKNOWN 7
154 static GLuint
translate_operand( GLenum operand
)
157 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
158 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
159 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
160 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
161 case GL_ZERO
: return OPR_ZERO
;
162 case GL_ONE
: return OPR_ONE
;
169 #define SRC_TEXTURE 0
170 #define SRC_TEXTURE0 1
171 #define SRC_TEXTURE1 2
172 #define SRC_TEXTURE2 3
173 #define SRC_TEXTURE3 4
174 #define SRC_TEXTURE4 5
175 #define SRC_TEXTURE5 6
176 #define SRC_TEXTURE6 7
177 #define SRC_TEXTURE7 8
178 #define SRC_CONSTANT 9
179 #define SRC_PRIMARY_COLOR 10
180 #define SRC_PREVIOUS 11
182 #define SRC_UNKNOWN 15
184 static GLuint
translate_source( GLenum src
)
187 case GL_TEXTURE
: return SRC_TEXTURE
;
195 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
196 case GL_CONSTANT
: return SRC_CONSTANT
;
197 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
198 case GL_PREVIOUS
: return SRC_PREVIOUS
;
207 #define MODE_REPLACE 0 /* r = a0 */
208 #define MODE_MODULATE 1 /* r = a0 * a1 */
209 #define MODE_ADD 2 /* r = a0 + a1 */
210 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
211 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
212 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
213 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
214 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
215 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
216 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
217 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
218 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
219 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
220 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
221 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
222 #define MODE_BUMP_ENVMAP_ATI 15 /* special */
223 #define MODE_UNKNOWN 16
226 * Translate GL combiner state into a MODE_x value
228 static GLuint
translate_mode( GLenum envMode
, GLenum mode
)
231 case GL_REPLACE
: return MODE_REPLACE
;
232 case GL_MODULATE
: return MODE_MODULATE
;
234 if (envMode
== GL_COMBINE4_NV
)
235 return MODE_ADD_PRODUCTS
;
239 if (envMode
== GL_COMBINE4_NV
)
240 return MODE_ADD_PRODUCTS_SIGNED
;
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 case GL_BUMP_ENVMAP_ATI
: return MODE_BUMP_ENVMAP_ATI
;
261 * Do we need to clamp the results of the given texture env/combine mode?
262 * If the inputs to the mode are in [0,1] we don't always have to clamp
266 need_saturate( GLuint mode
)
271 case MODE_INTERPOLATE
:
274 case MODE_ADD_SIGNED
:
277 case MODE_DOT3_RGB_EXT
:
279 case MODE_DOT3_RGBA_EXT
:
280 case MODE_MODULATE_ADD_ATI
:
281 case MODE_MODULATE_SIGNED_ADD_ATI
:
282 case MODE_MODULATE_SUBTRACT_ATI
:
283 case MODE_ADD_PRODUCTS
:
284 case MODE_ADD_PRODUCTS_SIGNED
:
285 case MODE_BUMP_ENVMAP_ATI
:
296 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
298 static GLuint
translate_tex_src_bit( GLbitfield bit
)
305 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
306 #define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
309 * Identify all possible varying inputs. The fragment program will
310 * never reference non-varying inputs, but will track them via state
313 * This function figures out all the inputs that the fragment program
314 * has access to. The bitmask is later reduced to just those which
315 * are actually referenced.
317 static GLbitfield
get_fp_input_mask( struct gl_context
*ctx
)
320 const GLboolean vertexShader
=
321 (ctx
->Shader
.CurrentVertexProgram
&&
322 ctx
->Shader
.CurrentVertexProgram
->LinkStatus
&&
323 ctx
->Shader
.CurrentVertexProgram
->_LinkedShaders
[MESA_SHADER_VERTEX
]);
324 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
325 GLbitfield fp_inputs
= 0x0;
327 if (ctx
->VertexProgram
._Overriden
) {
328 /* Somebody's messing with the vertex program and we don't have
329 * a clue what's happening. Assume that it could be producing
330 * all possible outputs.
334 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
335 /* _NEW_RENDERMODE */
336 fp_inputs
= (FRAG_BIT_COL0
| FRAG_BIT_TEX0
);
338 else if (!(vertexProgram
|| vertexShader
)) {
339 /* Fixed function vertex logic */
341 GLbitfield64 varying_inputs
= ctx
->varying_vp_inputs
;
343 /* These get generated in the setup routine regardless of the
347 if (ctx
->Point
.PointSprite
)
348 varying_inputs
|= FRAG_BITS_TEX_ANY
;
350 /* First look at what values may be computed by the generated
354 if (ctx
->Light
.Enabled
) {
355 fp_inputs
|= FRAG_BIT_COL0
;
357 if (texenv_doing_secondary_color(ctx
))
358 fp_inputs
|= FRAG_BIT_COL1
;
362 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
363 ctx
->Texture
._TexMatEnabled
) << FRAG_ATTRIB_TEX0
;
365 /* Then look at what might be varying as a result of enabled
368 if (varying_inputs
& VERT_BIT_COLOR0
)
369 fp_inputs
|= FRAG_BIT_COL0
;
370 if (varying_inputs
& VERT_BIT_COLOR1
)
371 fp_inputs
|= FRAG_BIT_COL1
;
373 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
374 << FRAG_ATTRIB_TEX0
);
378 /* calculate from vp->outputs */
379 struct gl_program
*vprog
;
380 GLbitfield64 vp_outputs
;
382 /* Choose GLSL vertex shader over ARB vertex program. Need this
383 * since vertex shader state validation comes after fragment state
384 * validation (see additional comments in state.c).
387 vprog
= ctx
->Shader
.CurrentVertexProgram
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
389 vprog
= &ctx
->VertexProgram
.Current
->Base
;
391 vp_outputs
= vprog
->OutputsWritten
;
393 /* These get generated in the setup routine regardless of the
397 if (ctx
->Point
.PointSprite
)
398 vp_outputs
|= FRAG_BITS_TEX_ANY
;
400 if (vp_outputs
& (1 << VERT_RESULT_COL0
))
401 fp_inputs
|= FRAG_BIT_COL0
;
402 if (vp_outputs
& (1 << VERT_RESULT_COL1
))
403 fp_inputs
|= FRAG_BIT_COL1
;
405 fp_inputs
|= (((vp_outputs
& VERT_RESULT_TEX_ANY
) >> VERT_RESULT_TEX0
)
406 << FRAG_ATTRIB_TEX0
);
414 * Examine current texture environment state and generate a unique
415 * key to identify it.
417 static GLuint
make_state_key( struct gl_context
*ctx
, struct state_key
*key
)
420 GLbitfield inputs_referenced
= FRAG_BIT_COL0
;
421 const GLbitfield inputs_available
= get_fp_input_mask( ctx
);
424 memset(key
, 0, sizeof(*key
));
427 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
428 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
429 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
430 const struct gl_tex_env_combine_state
*comb
= texUnit
->_CurrentCombine
;
433 if (!texUnit
->_ReallyEnabled
|| !texUnit
->Enabled
)
436 format
= texObj
->Image
[0][texObj
->BaseLevel
]->_BaseFormat
;
438 key
->unit
[i
].enabled
= 1;
439 key
->enabled_units
|= (1<<i
);
440 key
->nr_enabled_units
= i
+ 1;
441 inputs_referenced
|= FRAG_BIT_TEX(i
);
443 key
->unit
[i
].source_index
=
444 translate_tex_src_bit(texUnit
->_ReallyEnabled
);
446 key
->unit
[i
].shadow
=
447 ((texObj
->Sampler
.CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
448 ((format
== GL_DEPTH_COMPONENT
) ||
449 (format
== GL_DEPTH_STENCIL_EXT
)));
451 key
->unit
[i
].NumArgsRGB
= comb
->_NumArgsRGB
;
452 key
->unit
[i
].NumArgsA
= comb
->_NumArgsA
;
454 key
->unit
[i
].ModeRGB
=
455 translate_mode(texUnit
->EnvMode
, comb
->ModeRGB
);
457 translate_mode(texUnit
->EnvMode
, comb
->ModeA
);
459 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
460 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
462 for (j
= 0; j
< MAX_COMBINER_TERMS
; j
++) {
463 key
->unit
[i
].OptRGB
[j
].Operand
= translate_operand(comb
->OperandRGB
[j
]);
464 key
->unit
[i
].OptA
[j
].Operand
= translate_operand(comb
->OperandA
[j
]);
465 key
->unit
[i
].OptRGB
[j
].Source
= translate_source(comb
->SourceRGB
[j
]);
466 key
->unit
[i
].OptA
[j
].Source
= translate_source(comb
->SourceA
[j
]);
469 if (key
->unit
[i
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
470 /* requires some special translation */
471 key
->unit
[i
].NumArgsRGB
= 2;
472 key
->unit
[i
].ScaleShiftRGB
= 0;
473 key
->unit
[i
].OptRGB
[0].Operand
= OPR_SRC_COLOR
;
474 key
->unit
[i
].OptRGB
[0].Source
= SRC_TEXTURE
;
475 key
->unit
[i
].OptRGB
[1].Operand
= OPR_SRC_COLOR
;
476 key
->unit
[i
].OptRGB
[1].Source
= texUnit
->BumpTarget
- GL_TEXTURE0
+ SRC_TEXTURE0
;
480 /* _NEW_LIGHT | _NEW_FOG */
481 if (texenv_doing_secondary_color(ctx
)) {
482 key
->separate_specular
= 1;
483 inputs_referenced
|= FRAG_BIT_COL1
;
487 if (ctx
->Fog
.Enabled
) {
488 key
->fog_enabled
= 1;
489 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
490 inputs_referenced
|= FRAG_BIT_FOGC
; /* maybe */
494 key
->num_draw_buffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
497 if (ctx
->Color
.AlphaEnabled
&& key
->num_draw_buffers
== 0) {
498 /* if alpha test is enabled we need to emit at least one color */
499 key
->num_draw_buffers
= 1;
502 key
->inputs_available
= (inputs_available
& inputs_referenced
);
504 /* compute size of state key, ignoring unused texture units */
505 keySize
= sizeof(*key
) - sizeof(key
->unit
)
506 + key
->nr_enabled_units
* sizeof(key
->unit
[0]);
512 /** State used to build the fragment program:
514 class texenv_fragment_program
: public ir_factory
{
516 struct gl_shader_program
*shader_program
;
517 struct gl_shader
*shader
;
518 exec_list
*top_instructions
;
519 struct state_key
*state
;
521 ir_variable
*src_texture
[MAX_TEXTURE_COORD_UNITS
];
522 /* Reg containing each texture unit's sampled texture color,
526 /* Texcoord override from bumpmapping. */
527 ir_variable
*texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
529 /* Reg containing texcoord for a texture unit,
530 * needed for bump mapping, else undef.
533 ir_rvalue
*src_previous
; /**< Reg containing color from previous
534 * stage. May need to be decl'd.
539 get_current_attrib(struct texenv_fragment_program
*p
, GLuint attrib
)
541 ir_variable
*current
;
544 current
= p
->shader
->symbols
->get_variable("gl_CurrentAttribFragMESA");
545 current
->max_array_access
= MAX2(current
->max_array_access
, attrib
);
546 val
= new(p
->mem_ctx
) ir_dereference_variable(current
);
547 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(attrib
);
548 return new(p
->mem_ctx
) ir_dereference_array(val
, index
);
552 get_gl_Color(struct texenv_fragment_program
*p
)
554 if (p
->state
->inputs_available
& FRAG_BIT_COL0
) {
555 ir_variable
*var
= p
->shader
->symbols
->get_variable("gl_Color");
557 return new(p
->mem_ctx
) ir_dereference_variable(var
);
559 return get_current_attrib(p
, VERT_ATTRIB_COLOR0
);
564 get_source(struct texenv_fragment_program
*p
,
565 GLuint src
, GLuint unit
)
568 ir_dereference
*deref
;
572 return new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
582 return new(p
->mem_ctx
)
583 ir_dereference_variable(p
->src_texture
[src
- SRC_TEXTURE0
]);
586 var
= p
->shader
->symbols
->get_variable("gl_TextureEnvColor");
588 deref
= new(p
->mem_ctx
) ir_dereference_variable(var
);
589 var
->max_array_access
= MAX2(var
->max_array_access
, unit
);
590 return new(p
->mem_ctx
) ir_dereference_array(deref
,
591 new(p
->mem_ctx
) ir_constant(unit
));
593 case SRC_PRIMARY_COLOR
:
594 var
= p
->shader
->symbols
->get_variable("gl_Color");
596 return new(p
->mem_ctx
) ir_dereference_variable(var
);
599 return new(p
->mem_ctx
) ir_constant(0.0f
);
602 if (!p
->src_previous
) {
603 return get_gl_Color(p
);
605 return p
->src_previous
->clone(p
->mem_ctx
, NULL
);
615 emit_combine_source(struct texenv_fragment_program
*p
,
622 src
= get_source(p
, source
, unit
);
625 case OPR_ONE_MINUS_SRC_COLOR
:
626 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), src
);
629 return src
->type
->is_scalar() ? src
: swizzle_w(src
);
631 case OPR_ONE_MINUS_SRC_ALPHA
: {
632 ir_rvalue
*const scalar
= src
->type
->is_scalar() ? src
: swizzle_w(src
);
634 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), scalar
);
638 return new(p
->mem_ctx
) ir_constant(0.0f
);
640 return new(p
->mem_ctx
) ir_constant(1.0f
);
650 * Check if the RGB and Alpha sources and operands match for the given
651 * texture unit's combinder state. When the RGB and A sources and
652 * operands match, we can emit fewer instructions.
654 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
656 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
658 for (i
= 0; i
< numArgs
; i
++) {
659 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
662 switch (key
->unit
[unit
].OptA
[i
].Operand
) {
664 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
672 case OPR_ONE_MINUS_SRC_ALPHA
:
673 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
674 case OPR_ONE_MINUS_SRC_COLOR
:
675 case OPR_ONE_MINUS_SRC_ALPHA
:
682 return GL_FALSE
; /* impossible */
690 smear(struct texenv_fragment_program
*p
, ir_rvalue
*val
)
692 if (!val
->type
->is_scalar())
695 return swizzle_xxxx(val
);
699 emit_combine(struct texenv_fragment_program
*p
,
703 const struct mode_opt
*opt
)
705 ir_rvalue
*src
[MAX_COMBINER_TERMS
];
706 ir_rvalue
*tmp0
, *tmp1
;
709 assert(nr
<= MAX_COMBINER_TERMS
);
711 for (i
= 0; i
< nr
; i
++)
712 src
[i
] = emit_combine_source( p
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
719 return mul(src
[0], src
[1]);
722 return add(src
[0], src
[1]);
724 case MODE_ADD_SIGNED
:
725 return add(add(src
[0], src
[1]), new(p
->mem_ctx
) ir_constant(-0.5f
));
727 case MODE_INTERPOLATE
:
728 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
729 tmp0
= mul(src
[0], src
[2]);
730 tmp1
= mul(src
[1], sub(new(p
->mem_ctx
) ir_constant(1.0f
),
731 src
[2]->clone(p
->mem_ctx
, NULL
)));
732 return add(tmp0
, tmp1
);
735 return sub(src
[0], src
[1]);
738 case MODE_DOT3_RGBA_EXT
:
739 case MODE_DOT3_RGB_EXT
:
740 case MODE_DOT3_RGB
: {
741 tmp0
= mul(src
[0], new(p
->mem_ctx
) ir_constant(2.0f
));
742 tmp0
= add(tmp0
, new(p
->mem_ctx
) ir_constant(-1.0f
));
744 tmp1
= mul(src
[1], new(p
->mem_ctx
) ir_constant(2.0f
));
745 tmp1
= add(tmp1
, new(p
->mem_ctx
) ir_constant(-1.0f
));
747 return dot(swizzle_xyz(smear(p
, tmp0
)), swizzle_xyz(smear(p
, tmp1
)));
749 case MODE_MODULATE_ADD_ATI
:
750 return add(mul(src
[0], src
[2]), src
[1]);
752 case MODE_MODULATE_SIGNED_ADD_ATI
:
753 return add(add(mul(src
[0], src
[2]), src
[1]),
754 new(p
->mem_ctx
) ir_constant(-0.5f
));
756 case MODE_MODULATE_SUBTRACT_ATI
:
757 return sub(mul(src
[0], src
[2]), src
[1]);
759 case MODE_ADD_PRODUCTS
:
760 return add(mul(src
[0], src
[1]), mul(src
[2], src
[3]));
762 case MODE_ADD_PRODUCTS_SIGNED
:
763 return add(add(mul(src
[0], src
[1]), mul(src
[2], src
[3])),
764 new(p
->mem_ctx
) ir_constant(-0.5f
));
766 case MODE_BUMP_ENVMAP_ATI
:
767 /* special - not handled here */
777 * Generate instructions for one texture unit's env/combiner mode.
780 emit_texenv(struct texenv_fragment_program
*p
, GLuint unit
)
782 const struct state_key
*key
= p
->state
;
783 GLboolean rgb_saturate
, alpha_saturate
;
784 GLuint rgb_shift
, alpha_shift
;
786 if (!key
->unit
[unit
].enabled
) {
787 return get_source(p
, SRC_PREVIOUS
, 0);
789 if (key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
790 /* this isn't really a env stage delivering a color and handled elsewhere */
791 return get_source(p
, SRC_PREVIOUS
, 0);
794 switch (key
->unit
[unit
].ModeRGB
) {
795 case MODE_DOT3_RGB_EXT
:
796 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
799 case MODE_DOT3_RGBA_EXT
:
804 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
805 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
809 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
810 * We don't want to clamp twice.
813 rgb_saturate
= GL_FALSE
; /* saturate after rgb shift */
814 else if (need_saturate(key
->unit
[unit
].ModeRGB
))
815 rgb_saturate
= GL_TRUE
;
817 rgb_saturate
= GL_FALSE
;
820 alpha_saturate
= GL_FALSE
; /* saturate after alpha shift */
821 else if (need_saturate(key
->unit
[unit
].ModeA
))
822 alpha_saturate
= GL_TRUE
;
824 alpha_saturate
= GL_FALSE
;
826 ir_variable
*temp_var
= new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
831 ir_dereference
*deref
;
834 /* Emit the RGB and A combine ops
836 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
837 args_match(key
, unit
)) {
838 val
= emit_combine(p
, unit
,
839 key
->unit
[unit
].NumArgsRGB
,
840 key
->unit
[unit
].ModeRGB
,
841 key
->unit
[unit
].OptRGB
);
846 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
847 p
->emit(new(p
->mem_ctx
) ir_assignment(deref
, val
));
849 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
850 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
851 ir_rvalue
*val
= emit_combine(p
, unit
,
852 key
->unit
[unit
].NumArgsRGB
,
853 key
->unit
[unit
].ModeRGB
,
854 key
->unit
[unit
].OptRGB
);
858 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
859 p
->emit(new(p
->mem_ctx
) ir_assignment(deref
, val
));
862 /* Need to do something to stop from re-emitting identical
863 * argument calculations here:
865 val
= emit_combine(p
, unit
,
866 key
->unit
[unit
].NumArgsRGB
,
867 key
->unit
[unit
].ModeRGB
,
868 key
->unit
[unit
].OptRGB
);
869 val
= swizzle_xyz(smear(p
, val
));
872 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
873 p
->emit(new(p
->mem_ctx
) ir_assignment(deref
, val
, NULL
, WRITEMASK_XYZ
));
875 val
= emit_combine(p
, unit
,
876 key
->unit
[unit
].NumArgsA
,
877 key
->unit
[unit
].ModeA
,
878 key
->unit
[unit
].OptA
);
879 val
= swizzle_w(smear(p
, val
));
882 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
883 p
->emit(new(p
->mem_ctx
) ir_assignment(deref
, val
, NULL
, WRITEMASK_W
));
886 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
888 /* Deal with the final shift:
890 if (alpha_shift
|| rgb_shift
) {
893 if (rgb_shift
== alpha_shift
) {
894 shift
= new(p
->mem_ctx
) ir_constant((float)(1 << rgb_shift
));
897 float const_data
[4] = {
903 shift
= new(p
->mem_ctx
) ir_constant(glsl_type::vec4_type
,
904 (ir_constant_data
*)const_data
);
907 return saturate(mul(deref
, shift
));
915 * Generate instruction for getting a texture source term.
917 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
919 ir_dereference
*deref
;
921 if (p
->src_texture
[unit
])
924 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
927 if (!(p
->state
->inputs_available
& (FRAG_BIT_TEX0
<< unit
))) {
928 texcoord
= get_current_attrib(p
, VERT_ATTRIB_TEX0
+ unit
);
929 } else if (p
->texcoord_tex
[unit
]) {
930 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(p
->texcoord_tex
[unit
]);
932 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
934 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
935 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(unit
);
936 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
937 tc_array
->max_array_access
= MAX2(tc_array
->max_array_access
, unit
);
940 if (!p
->state
->unit
[unit
].enabled
) {
941 p
->src_texture
[unit
] = new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
944 p
->emit(p
->src_texture
[unit
]);
946 deref
= new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
947 p
->emit(new(p
->mem_ctx
) ir_assignment(deref
,
948 new(p
->mem_ctx
) ir_constant(0.0f
)));
952 const glsl_type
*sampler_type
= NULL
;
956 case TEXTURE_1D_INDEX
:
957 if (p
->state
->unit
[unit
].shadow
)
958 sampler_type
= p
->shader
->symbols
->get_type("sampler1DShadow");
960 sampler_type
= p
->shader
->symbols
->get_type("sampler1D");
963 case TEXTURE_1D_ARRAY_INDEX
:
964 if (p
->state
->unit
[unit
].shadow
)
965 sampler_type
= p
->shader
->symbols
->get_type("sampler1DArrayShadow");
967 sampler_type
= p
->shader
->symbols
->get_type("sampler1DArray");
970 case TEXTURE_2D_INDEX
:
971 if (p
->state
->unit
[unit
].shadow
)
972 sampler_type
= p
->shader
->symbols
->get_type("sampler2DShadow");
974 sampler_type
= p
->shader
->symbols
->get_type("sampler2D");
977 case TEXTURE_2D_ARRAY_INDEX
:
978 if (p
->state
->unit
[unit
].shadow
)
979 sampler_type
= p
->shader
->symbols
->get_type("sampler2DArrayShadow");
981 sampler_type
= p
->shader
->symbols
->get_type("sampler2DArray");
984 case TEXTURE_RECT_INDEX
:
985 if (p
->state
->unit
[unit
].shadow
)
986 sampler_type
= p
->shader
->symbols
->get_type("sampler2DRectShadow");
988 sampler_type
= p
->shader
->symbols
->get_type("sampler2DRect");
991 case TEXTURE_3D_INDEX
:
992 assert(!p
->state
->unit
[unit
].shadow
);
993 sampler_type
= p
->shader
->symbols
->get_type("sampler3D");
996 case TEXTURE_CUBE_INDEX
:
997 if (p
->state
->unit
[unit
].shadow
)
998 sampler_type
= p
->shader
->symbols
->get_type("samplerCubeShadow");
1000 sampler_type
= p
->shader
->symbols
->get_type("samplerCube");
1003 case TEXTURE_EXTERNAL_INDEX
:
1004 assert(!p
->state
->unit
[unit
].shadow
);
1005 sampler_type
= p
->shader
->symbols
->get_type("samplerExternalOES");
1010 p
->src_texture
[unit
] = new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
1013 p
->emit(p
->src_texture
[unit
]);
1015 ir_texture
*tex
= new(p
->mem_ctx
) ir_texture(ir_tex
);
1018 char *sampler_name
= ralloc_asprintf(p
->mem_ctx
, "sampler_%d", unit
);
1019 ir_variable
*sampler
= new(p
->mem_ctx
) ir_variable(sampler_type
,
1022 p
->top_instructions
->push_head(sampler
);
1023 deref
= new(p
->mem_ctx
) ir_dereference_variable(sampler
);
1024 tex
->set_sampler(deref
, glsl_type::vec4_type
);
1026 tex
->coordinate
= new(p
->mem_ctx
) ir_swizzle(texcoord
, 0, 1, 2, 3, coords
);
1028 if (p
->state
->unit
[unit
].shadow
) {
1029 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1030 tex
->shadow_comparitor
= new(p
->mem_ctx
) ir_swizzle(texcoord
,
1036 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1037 tex
->projector
= swizzle_w(texcoord
);
1039 deref
= new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
1040 p
->emit(new(p
->mem_ctx
) ir_assignment(deref
, tex
));
1044 load_texenv_source(struct texenv_fragment_program
*p
,
1045 GLuint src
, GLuint unit
)
1049 load_texture(p
, unit
);
1060 load_texture(p
, src
- SRC_TEXTURE0
);
1064 /* not a texture src - do nothing */
1071 * Generate instructions for loading all texture source terms.
1074 load_texunit_sources( struct texenv_fragment_program
*p
, GLuint unit
)
1076 const struct state_key
*key
= p
->state
;
1079 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1080 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1083 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1084 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1091 * Generate instructions for loading bump map textures.
1094 load_texunit_bumpmap( struct texenv_fragment_program
*p
, GLuint unit
)
1096 const struct state_key
*key
= p
->state
;
1097 GLuint bumpedUnitNr
= key
->unit
[unit
].OptRGB
[1].Source
- SRC_TEXTURE0
;
1099 ir_rvalue
*texcoord
;
1100 ir_variable
*rot_mat_0
, *rot_mat_1
;
1102 rot_mat_0
= p
->shader
->symbols
->get_variable("gl_BumpRotMatrix0MESA");
1103 rot_mat_1
= p
->shader
->symbols
->get_variable("gl_BumpRotMatrix1MESA");
1105 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
1107 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
1108 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(bumpedUnitNr
);
1109 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
1110 tc_array
->max_array_access
= MAX2(tc_array
->max_array_access
, unit
);
1112 load_texenv_source( p
, unit
+ SRC_TEXTURE0
, unit
);
1114 /* Apply rot matrix and add coords to be available in next phase.
1115 * dest = Arg1 + (Arg0.xx * rotMat0) + (Arg0.yy * rotMat1)
1116 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1118 ir_dereference
*deref
;
1119 ir_rvalue
*bump_x
, *bump_y
;
1121 texcoord
= smear(p
, texcoord
);
1123 /* bump_texcoord = texcoord */
1124 ir_variable
*bumped
= new(p
->mem_ctx
) ir_variable(texcoord
->type
,
1129 deref
= new(p
->mem_ctx
) ir_dereference_variable(bumped
);
1130 p
->emit(new(p
->mem_ctx
) ir_assignment(deref
, texcoord
));
1132 /* bump_texcoord.xy += arg0.x * rotmat0 + arg0.y * rotmat1 */
1133 bump
= get_source(p
, key
->unit
[unit
].OptRGB
[0].Source
, unit
);
1134 bump_x
= mul(swizzle_x(bump
), rot_mat_0
);
1135 bump_y
= mul(swizzle_y(bump
->clone(p
->mem_ctx
, NULL
)), rot_mat_1
);
1137 ir_expression
*expr
;
1139 expr
= add(swizzle_xy(bumped
), add(bump_x
, bump_y
));
1141 deref
= new(p
->mem_ctx
) ir_dereference_variable(bumped
);
1142 p
->emit(new(p
->mem_ctx
) ir_assignment(deref
, expr
, NULL
, WRITEMASK_XY
));
1144 p
->texcoord_tex
[bumpedUnitNr
] = bumped
;
1148 * Applies the fog calculations.
1150 * This is basically like the ARB_fragment_prorgam fog options. Note
1151 * that ffvertex_prog.c produces fogcoord for us when
1152 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1155 emit_fog_instructions(struct texenv_fragment_program
*p
,
1156 ir_rvalue
*fragcolor
)
1158 struct state_key
*key
= p
->state
;
1159 ir_rvalue
*f
, *temp
;
1160 ir_variable
*params
, *oparams
;
1161 ir_variable
*fogcoord
;
1163 /* Temporary storage for the whole fog result. Fog calculations
1164 * only affect rgb so we're hanging on to the .a value of fragcolor
1167 ir_variable
*fog_result
= new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
1170 p
->emit(fog_result
);
1171 temp
= new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1172 p
->emit(new(p
->mem_ctx
) ir_assignment(temp
, fragcolor
));
1174 fragcolor
= swizzle_xyz(fog_result
);
1176 oparams
= p
->shader
->symbols
->get_variable("gl_FogParamsOptimizedMESA");
1177 fogcoord
= p
->shader
->symbols
->get_variable("gl_FogFragCoord");
1178 params
= p
->shader
->symbols
->get_variable("gl_Fog");
1179 f
= new(p
->mem_ctx
) ir_dereference_variable(fogcoord
);
1181 ir_variable
*f_var
= new(p
->mem_ctx
) ir_variable(glsl_type::float_type
,
1182 "fog_factor", ir_var_auto
);
1185 switch (key
->fog_mode
) {
1187 /* f = (end - z) / (end - start)
1189 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1190 * (end / (end - start)) so we can generate a single MAD.
1192 f
= add(mul(f
, swizzle_x(oparams
)), swizzle_y(oparams
));
1195 /* f = e^(-(density * fogcoord))
1197 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1198 * use EXP2 which is generally the native instruction without
1199 * having to do any further math on the fog density uniform.
1201 f
= mul(f
, swizzle_z(oparams
));
1202 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1203 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1206 /* f = e^(-(density * fogcoord)^2)
1208 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1209 * can do this like FOG_EXP but with a squaring after the
1210 * multiply by density.
1212 ir_variable
*temp_var
= new(p
->mem_ctx
) ir_variable(glsl_type::float_type
,
1217 f
= mul(f
, swizzle_w(oparams
));
1219 temp
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
1220 p
->emit(new(p
->mem_ctx
) ir_assignment(temp
, f
));
1222 f
= mul(temp_var
, temp_var
);
1223 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1224 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1230 temp
= new(p
->mem_ctx
) ir_dereference_variable(f_var
);
1231 p
->emit(new(p
->mem_ctx
) ir_assignment(temp
, f
));
1233 f
= sub(new(p
->mem_ctx
) ir_constant(1.0f
), f_var
);
1234 temp
= new(p
->mem_ctx
) ir_dereference_variable(params
);
1235 temp
= new(p
->mem_ctx
) ir_dereference_record(temp
, "color");
1236 temp
= mul(swizzle_xyz(temp
), f
);
1238 f
= add(temp
, mul(fragcolor
, f_var
));
1240 ir_dereference
*deref
= new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1241 p
->emit(new(p
->mem_ctx
) ir_assignment(deref
, f
, NULL
, WRITEMASK_XYZ
));
1243 return new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1247 emit_instructions(struct texenv_fragment_program
*p
)
1249 struct state_key
*key
= p
->state
;
1252 if (key
->enabled_units
) {
1253 /* Zeroth pass - bump map textures first */
1254 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1255 if (key
->unit
[unit
].enabled
&&
1256 key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1257 load_texunit_bumpmap(p
, unit
);
1261 /* First pass - to support texture_env_crossbar, first identify
1262 * all referenced texture sources and emit texld instructions
1265 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1266 if (key
->unit
[unit
].enabled
) {
1267 load_texunit_sources(p
, unit
);
1270 /* Second pass - emit combine instructions to build final color:
1272 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1273 if (key
->unit
[unit
].enabled
) {
1274 p
->src_previous
= emit_texenv(p
, unit
);
1279 ir_rvalue
*cf
= get_source(p
, SRC_PREVIOUS
, 0);
1280 ir_dereference_variable
*deref
;
1282 if (key
->separate_specular
) {
1284 ir_variable
*spec_result
= new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
1288 p
->emit(spec_result
);
1290 deref
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1291 p
->emit(new(p
->mem_ctx
) ir_assignment(deref
, cf
));
1293 ir_rvalue
*secondary
;
1294 if (p
->state
->inputs_available
& FRAG_BIT_COL1
) {
1296 p
->shader
->symbols
->get_variable("gl_SecondaryColor");
1298 secondary
= swizzle_xyz(var
);
1300 secondary
= swizzle_xyz(get_current_attrib(p
, VERT_ATTRIB_COLOR1
));
1303 tmp0
= add(swizzle_xyz(spec_result
), secondary
);
1305 deref
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1306 p
->emit(new(p
->mem_ctx
) ir_assignment(deref
, tmp0
, NULL
, WRITEMASK_XYZ
));
1308 cf
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1311 if (key
->fog_enabled
) {
1312 cf
= emit_fog_instructions(p
, cf
);
1315 ir_variable
*frag_color
= p
->shader
->symbols
->get_variable("gl_FragColor");
1317 deref
= new(p
->mem_ctx
) ir_dereference_variable(frag_color
);
1318 p
->emit(new(p
->mem_ctx
) ir_assignment(deref
, cf
));
1322 * Generate a new fragment program which implements the context's
1323 * current texture env/combine mode.
1325 static struct gl_shader_program
*
1326 create_new_program(struct gl_context
*ctx
, struct state_key
*key
)
1328 texenv_fragment_program p
;
1330 _mesa_glsl_parse_state
*state
;
1332 p
.mem_ctx
= ralloc_context(NULL
);
1333 p
.shader
= ctx
->Driver
.NewShader(ctx
, 0, GL_FRAGMENT_SHADER
);
1334 p
.shader
->ir
= new(p
.shader
) exec_list
;
1335 state
= new(p
.shader
) _mesa_glsl_parse_state(ctx
, GL_FRAGMENT_SHADER
,
1337 p
.shader
->symbols
= state
->symbols
;
1338 p
.top_instructions
= p
.shader
->ir
;
1339 p
.instructions
= p
.shader
->ir
;
1341 p
.shader_program
= ctx
->Driver
.NewShaderProgram(ctx
, 0);
1343 /* Tell the linker to ignore the fact that we're building a
1344 * separate shader, in case we're in a GLES2 context that would
1345 * normally reject that. The real problem is that we're building a
1346 * fixed function program in a GLES2 context at all, but that's a
1347 * big mess to clean up.
1349 p
.shader_program
->InternalSeparateShader
= GL_TRUE
;
1351 state
->language_version
= 130;
1352 if (ctx
->Extensions
.OES_EGL_image_external
)
1353 state
->OES_EGL_image_external_enable
= true;
1354 _mesa_glsl_initialize_types(state
);
1355 _mesa_glsl_initialize_variables(p
.instructions
, state
);
1357 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1358 p
.src_texture
[unit
] = NULL
;
1359 p
.texcoord_tex
[unit
] = NULL
;
1362 p
.src_previous
= NULL
;
1364 ir_function
*main_f
= new(p
.mem_ctx
) ir_function("main");
1366 state
->symbols
->add_function(main_f
);
1368 ir_function_signature
*main_sig
=
1369 new(p
.mem_ctx
) ir_function_signature(p
.shader
->symbols
->get_type("void"));
1370 main_sig
->is_defined
= true;
1371 main_f
->add_signature(main_sig
);
1373 p
.instructions
= &main_sig
->body
;
1374 if (key
->num_draw_buffers
)
1375 emit_instructions(&p
);
1377 validate_ir_tree(p
.shader
->ir
);
1379 while (do_common_optimization(p
.shader
->ir
, false, false, 32))
1381 reparent_ir(p
.shader
->ir
, p
.shader
->ir
);
1383 p
.shader
->CompileStatus
= true;
1384 p
.shader
->Version
= state
->language_version
;
1385 p
.shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
1386 p
.shader_program
->Shaders
=
1387 (gl_shader
**)malloc(sizeof(*p
.shader_program
->Shaders
));
1388 p
.shader_program
->Shaders
[0] = p
.shader
;
1389 p
.shader_program
->NumShaders
= 1;
1391 _mesa_glsl_link_shader(ctx
, p
.shader_program
);
1393 /* Set the sampler uniforms, and relink to get them into the linked
1396 struct gl_shader
*const fs
=
1397 p
.shader_program
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
1398 struct gl_program
*const fp
= fs
->Program
;
1400 _mesa_generate_parameters_list_for_uniforms(p
.shader_program
, fs
,
1403 _mesa_associate_uniform_storage(ctx
, p
.shader_program
, fp
->Parameters
);
1405 for (unsigned int i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1406 /* Enough space for 'sampler_999\0'.
1410 snprintf(name
, sizeof(name
), "sampler_%d", i
);
1412 int loc
= _mesa_get_uniform_location(ctx
, p
.shader_program
, name
);
1417 /* Avoid using _mesa_uniform() because it flags state
1418 * updates, so if we're generating this shader_program in a
1419 * state update, we end up recursing. Instead, just set the
1420 * value, which is picked up at re-link.
1422 _mesa_uniform_split_location_offset(loc
, &base
, &idx
);
1425 struct gl_uniform_storage
*const storage
=
1426 &p
.shader_program
->UniformStorage
[base
];
1428 /* Update the storage, the SamplerUnits in the shader program, and
1429 * the SamplerUnits in the assembly shader.
1431 storage
->storage
[idx
].i
= i
;
1432 fp
->SamplerUnits
[storage
->sampler
] = i
;
1433 p
.shader_program
->SamplerUnits
[storage
->sampler
] = i
;
1434 _mesa_propagate_uniforms_to_driver_storage(storage
, 0, 1);
1437 _mesa_update_shader_textures_used(p
.shader_program
, fp
);
1438 (void) ctx
->Driver
.ProgramStringNotify(ctx
, fp
->Target
, fp
);
1440 if (!p
.shader_program
->LinkStatus
)
1441 _mesa_problem(ctx
, "Failed to link fixed function fragment shader: %s\n",
1442 p
.shader_program
->InfoLog
);
1444 ralloc_free(p
.mem_ctx
);
1445 return p
.shader_program
;
1451 * Return a fragment program which implements the current
1452 * fixed-function texture, fog and color-sum operations.
1454 struct gl_shader_program
*
1455 _mesa_get_fixed_func_fragment_program(struct gl_context
*ctx
)
1457 struct gl_shader_program
*shader_program
;
1458 struct state_key key
;
1461 keySize
= make_state_key(ctx
, &key
);
1463 shader_program
= (struct gl_shader_program
*)
1464 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1467 if (!shader_program
) {
1468 shader_program
= create_new_program(ctx
, &key
);
1470 _mesa_shader_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1471 &key
, keySize
, shader_program
);
1474 return shader_program
;