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 struct texenv_fragment_program
{
515 struct gl_shader_program
*shader_program
;
516 struct gl_shader
*shader
;
517 exec_list
*instructions
;
518 exec_list
*top_instructions
;
520 struct state_key
*state
;
522 ir_variable
*src_texture
[MAX_TEXTURE_COORD_UNITS
];
523 /* Reg containing each texture unit's sampled texture color,
527 /* Texcoord override from bumpmapping. */
528 ir_variable
*texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
530 /* Reg containing texcoord for a texture unit,
531 * needed for bump mapping, else undef.
534 ir_rvalue
*src_previous
; /**< Reg containing color from previous
535 * stage. May need to be decl'd.
540 get_current_attrib(struct texenv_fragment_program
*p
, GLuint attrib
)
542 ir_variable
*current
;
545 current
= p
->shader
->symbols
->get_variable("gl_CurrentAttribFragMESA");
546 current
->max_array_access
= MAX2(current
->max_array_access
, attrib
);
547 val
= new(p
->mem_ctx
) ir_dereference_variable(current
);
548 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(attrib
);
549 return new(p
->mem_ctx
) ir_dereference_array(val
, index
);
553 get_gl_Color(struct texenv_fragment_program
*p
)
555 if (p
->state
->inputs_available
& FRAG_BIT_COL0
) {
556 ir_variable
*var
= p
->shader
->symbols
->get_variable("gl_Color");
558 return new(p
->mem_ctx
) ir_dereference_variable(var
);
560 return get_current_attrib(p
, VERT_ATTRIB_COLOR0
);
565 get_source(struct texenv_fragment_program
*p
,
566 GLuint src
, GLuint unit
)
569 ir_dereference
*deref
;
573 return new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
583 return new(p
->mem_ctx
)
584 ir_dereference_variable(p
->src_texture
[src
- SRC_TEXTURE0
]);
587 var
= p
->shader
->symbols
->get_variable("gl_TextureEnvColor");
589 deref
= new(p
->mem_ctx
) ir_dereference_variable(var
);
590 var
->max_array_access
= MAX2(var
->max_array_access
, unit
);
591 return new(p
->mem_ctx
) ir_dereference_array(deref
,
592 new(p
->mem_ctx
) ir_constant(unit
));
594 case SRC_PRIMARY_COLOR
:
595 var
= p
->shader
->symbols
->get_variable("gl_Color");
597 return new(p
->mem_ctx
) ir_dereference_variable(var
);
600 return new(p
->mem_ctx
) ir_constant(0.0f
);
603 if (!p
->src_previous
) {
604 return get_gl_Color(p
);
606 return p
->src_previous
->clone(p
->mem_ctx
, NULL
);
616 emit_combine_source(struct texenv_fragment_program
*p
,
623 src
= get_source(p
, source
, unit
);
626 case OPR_ONE_MINUS_SRC_COLOR
:
627 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), src
);
630 return src
->type
->is_scalar() ? src
: swizzle_w(src
);
632 case OPR_ONE_MINUS_SRC_ALPHA
: {
633 ir_rvalue
*const scalar
= src
->type
->is_scalar() ? src
: swizzle_w(src
);
635 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), scalar
);
639 return new(p
->mem_ctx
) ir_constant(0.0f
);
641 return new(p
->mem_ctx
) ir_constant(1.0f
);
651 * Check if the RGB and Alpha sources and operands match for the given
652 * texture unit's combinder state. When the RGB and A sources and
653 * operands match, we can emit fewer instructions.
655 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
657 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
659 for (i
= 0; i
< numArgs
; i
++) {
660 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
663 switch (key
->unit
[unit
].OptA
[i
].Operand
) {
665 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
673 case OPR_ONE_MINUS_SRC_ALPHA
:
674 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
675 case OPR_ONE_MINUS_SRC_COLOR
:
676 case OPR_ONE_MINUS_SRC_ALPHA
:
683 return GL_FALSE
; /* impossible */
691 smear(struct texenv_fragment_program
*p
, ir_rvalue
*val
)
693 if (!val
->type
->is_scalar())
696 return swizzle_xxxx(val
);
700 emit_combine(struct texenv_fragment_program
*p
,
704 const struct mode_opt
*opt
)
706 ir_rvalue
*src
[MAX_COMBINER_TERMS
];
707 ir_rvalue
*tmp0
, *tmp1
;
710 assert(nr
<= MAX_COMBINER_TERMS
);
712 for (i
= 0; i
< nr
; i
++)
713 src
[i
] = emit_combine_source( p
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
720 return mul(src
[0], src
[1]);
723 return add(src
[0], src
[1]);
725 case MODE_ADD_SIGNED
:
726 return add(add(src
[0], src
[1]), new(p
->mem_ctx
) ir_constant(-0.5f
));
728 case MODE_INTERPOLATE
:
729 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
730 tmp0
= mul(src
[0], src
[2]);
731 tmp1
= mul(src
[1], sub(new(p
->mem_ctx
) ir_constant(1.0f
),
732 src
[2]->clone(p
->mem_ctx
, NULL
)));
733 return add(tmp0
, tmp1
);
736 return sub(src
[0], src
[1]);
739 case MODE_DOT3_RGBA_EXT
:
740 case MODE_DOT3_RGB_EXT
:
741 case MODE_DOT3_RGB
: {
742 tmp0
= mul(src
[0], new(p
->mem_ctx
) ir_constant(2.0f
));
743 tmp0
= add(tmp0
, new(p
->mem_ctx
) ir_constant(-1.0f
));
745 tmp1
= mul(src
[1], new(p
->mem_ctx
) ir_constant(2.0f
));
746 tmp1
= add(tmp1
, new(p
->mem_ctx
) ir_constant(-1.0f
));
748 return dot(swizzle_xyz(smear(p
, tmp0
)), swizzle_xyz(smear(p
, tmp1
)));
750 case MODE_MODULATE_ADD_ATI
:
751 return add(mul(src
[0], src
[2]), src
[1]);
753 case MODE_MODULATE_SIGNED_ADD_ATI
:
754 return add(add(mul(src
[0], src
[2]), src
[1]),
755 new(p
->mem_ctx
) ir_constant(-0.5f
));
757 case MODE_MODULATE_SUBTRACT_ATI
:
758 return sub(mul(src
[0], src
[2]), src
[1]);
760 case MODE_ADD_PRODUCTS
:
761 return add(mul(src
[0], src
[1]), mul(src
[2], src
[3]));
763 case MODE_ADD_PRODUCTS_SIGNED
:
764 return add(add(mul(src
[0], src
[1]), mul(src
[2], src
[3])),
765 new(p
->mem_ctx
) ir_constant(-0.5f
));
767 case MODE_BUMP_ENVMAP_ATI
:
768 /* special - not handled here */
778 * Generate instructions for one texture unit's env/combiner mode.
781 emit_texenv(struct texenv_fragment_program
*p
, GLuint unit
)
783 const struct state_key
*key
= p
->state
;
784 GLboolean rgb_saturate
, alpha_saturate
;
785 GLuint rgb_shift
, alpha_shift
;
787 if (!key
->unit
[unit
].enabled
) {
788 return get_source(p
, SRC_PREVIOUS
, 0);
790 if (key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
791 /* this isn't really a env stage delivering a color and handled elsewhere */
792 return get_source(p
, SRC_PREVIOUS
, 0);
795 switch (key
->unit
[unit
].ModeRGB
) {
796 case MODE_DOT3_RGB_EXT
:
797 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
800 case MODE_DOT3_RGBA_EXT
:
805 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
806 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
810 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
811 * We don't want to clamp twice.
814 rgb_saturate
= GL_FALSE
; /* saturate after rgb shift */
815 else if (need_saturate(key
->unit
[unit
].ModeRGB
))
816 rgb_saturate
= GL_TRUE
;
818 rgb_saturate
= GL_FALSE
;
821 alpha_saturate
= GL_FALSE
; /* saturate after alpha shift */
822 else if (need_saturate(key
->unit
[unit
].ModeA
))
823 alpha_saturate
= GL_TRUE
;
825 alpha_saturate
= GL_FALSE
;
827 ir_variable
*temp_var
= new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
830 p
->instructions
->push_tail(temp_var
);
832 ir_dereference
*deref
;
833 ir_assignment
*assign
;
836 /* Emit the RGB and A combine ops
838 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
839 args_match(key
, unit
)) {
840 val
= emit_combine(p
, unit
,
841 key
->unit
[unit
].NumArgsRGB
,
842 key
->unit
[unit
].ModeRGB
,
843 key
->unit
[unit
].OptRGB
);
848 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
849 assign
= new(p
->mem_ctx
) ir_assignment(deref
, val
);
850 p
->instructions
->push_tail(assign
);
852 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
853 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
854 ir_rvalue
*val
= emit_combine(p
, unit
,
855 key
->unit
[unit
].NumArgsRGB
,
856 key
->unit
[unit
].ModeRGB
,
857 key
->unit
[unit
].OptRGB
);
861 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
862 assign
= new(p
->mem_ctx
) ir_assignment(deref
, val
);
863 p
->instructions
->push_tail(assign
);
866 /* Need to do something to stop from re-emitting identical
867 * argument calculations here:
869 val
= emit_combine(p
, unit
,
870 key
->unit
[unit
].NumArgsRGB
,
871 key
->unit
[unit
].ModeRGB
,
872 key
->unit
[unit
].OptRGB
);
873 val
= swizzle_xyz(smear(p
, val
));
876 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
877 assign
= new(p
->mem_ctx
) ir_assignment(deref
, val
, NULL
, WRITEMASK_XYZ
);
878 p
->instructions
->push_tail(assign
);
880 val
= emit_combine(p
, unit
,
881 key
->unit
[unit
].NumArgsA
,
882 key
->unit
[unit
].ModeA
,
883 key
->unit
[unit
].OptA
);
884 val
= swizzle_w(smear(p
, val
));
887 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
888 assign
= new(p
->mem_ctx
) ir_assignment(deref
, val
, NULL
, WRITEMASK_W
);
889 p
->instructions
->push_tail(assign
);
892 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
894 /* Deal with the final shift:
896 if (alpha_shift
|| rgb_shift
) {
899 if (rgb_shift
== alpha_shift
) {
900 shift
= new(p
->mem_ctx
) ir_constant((float)(1 << rgb_shift
));
903 float const_data
[4] = {
909 shift
= new(p
->mem_ctx
) ir_constant(glsl_type::vec4_type
,
910 (ir_constant_data
*)const_data
);
913 return saturate(mul(deref
, shift
));
921 * Generate instruction for getting a texture source term.
923 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
925 ir_dereference
*deref
;
926 ir_assignment
*assign
;
928 if (p
->src_texture
[unit
])
931 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
934 if (!(p
->state
->inputs_available
& (FRAG_BIT_TEX0
<< unit
))) {
935 texcoord
= get_current_attrib(p
, VERT_ATTRIB_TEX0
+ unit
);
936 } else if (p
->texcoord_tex
[unit
]) {
937 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(p
->texcoord_tex
[unit
]);
939 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
941 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
942 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(unit
);
943 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
944 tc_array
->max_array_access
= MAX2(tc_array
->max_array_access
, unit
);
947 if (!p
->state
->unit
[unit
].enabled
) {
948 p
->src_texture
[unit
] = new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
951 p
->instructions
->push_tail(p
->src_texture
[unit
]);
953 deref
= new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
954 assign
= new(p
->mem_ctx
) ir_assignment(deref
,
955 new(p
->mem_ctx
) ir_constant(0.0f
));
956 p
->instructions
->push_tail(assign
);
960 const glsl_type
*sampler_type
= NULL
;
964 case TEXTURE_1D_INDEX
:
965 if (p
->state
->unit
[unit
].shadow
)
966 sampler_type
= p
->shader
->symbols
->get_type("sampler1DShadow");
968 sampler_type
= p
->shader
->symbols
->get_type("sampler1D");
971 case TEXTURE_1D_ARRAY_INDEX
:
972 if (p
->state
->unit
[unit
].shadow
)
973 sampler_type
= p
->shader
->symbols
->get_type("sampler1DArrayShadow");
975 sampler_type
= p
->shader
->symbols
->get_type("sampler1DArray");
978 case TEXTURE_2D_INDEX
:
979 if (p
->state
->unit
[unit
].shadow
)
980 sampler_type
= p
->shader
->symbols
->get_type("sampler2DShadow");
982 sampler_type
= p
->shader
->symbols
->get_type("sampler2D");
985 case TEXTURE_2D_ARRAY_INDEX
:
986 if (p
->state
->unit
[unit
].shadow
)
987 sampler_type
= p
->shader
->symbols
->get_type("sampler2DArrayShadow");
989 sampler_type
= p
->shader
->symbols
->get_type("sampler2DArray");
992 case TEXTURE_RECT_INDEX
:
993 if (p
->state
->unit
[unit
].shadow
)
994 sampler_type
= p
->shader
->symbols
->get_type("sampler2DRectShadow");
996 sampler_type
= p
->shader
->symbols
->get_type("sampler2DRect");
999 case TEXTURE_3D_INDEX
:
1000 assert(!p
->state
->unit
[unit
].shadow
);
1001 sampler_type
= p
->shader
->symbols
->get_type("sampler3D");
1004 case TEXTURE_CUBE_INDEX
:
1005 if (p
->state
->unit
[unit
].shadow
)
1006 sampler_type
= p
->shader
->symbols
->get_type("samplerCubeShadow");
1008 sampler_type
= p
->shader
->symbols
->get_type("samplerCube");
1011 case TEXTURE_EXTERNAL_INDEX
:
1012 assert(!p
->state
->unit
[unit
].shadow
);
1013 sampler_type
= p
->shader
->symbols
->get_type("samplerExternalOES");
1018 p
->src_texture
[unit
] = new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
1021 p
->instructions
->push_tail(p
->src_texture
[unit
]);
1023 ir_texture
*tex
= new(p
->mem_ctx
) ir_texture(ir_tex
);
1026 char *sampler_name
= ralloc_asprintf(p
->mem_ctx
, "sampler_%d", unit
);
1027 ir_variable
*sampler
= new(p
->mem_ctx
) ir_variable(sampler_type
,
1030 p
->top_instructions
->push_head(sampler
);
1031 deref
= new(p
->mem_ctx
) ir_dereference_variable(sampler
);
1032 tex
->set_sampler(deref
, glsl_type::vec4_type
);
1034 tex
->coordinate
= new(p
->mem_ctx
) ir_swizzle(texcoord
, 0, 1, 2, 3, coords
);
1036 if (p
->state
->unit
[unit
].shadow
) {
1037 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1038 tex
->shadow_comparitor
= new(p
->mem_ctx
) ir_swizzle(texcoord
,
1044 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1045 tex
->projector
= swizzle_w(texcoord
);
1047 deref
= new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
1048 assign
= new(p
->mem_ctx
) ir_assignment(deref
, tex
);
1049 p
->instructions
->push_tail(assign
);
1053 load_texenv_source(struct texenv_fragment_program
*p
,
1054 GLuint src
, GLuint unit
)
1058 load_texture(p
, unit
);
1069 load_texture(p
, src
- SRC_TEXTURE0
);
1073 /* not a texture src - do nothing */
1080 * Generate instructions for loading all texture source terms.
1083 load_texunit_sources( struct texenv_fragment_program
*p
, GLuint unit
)
1085 const struct state_key
*key
= p
->state
;
1088 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1089 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1092 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1093 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1100 * Generate instructions for loading bump map textures.
1103 load_texunit_bumpmap( struct texenv_fragment_program
*p
, GLuint unit
)
1105 const struct state_key
*key
= p
->state
;
1106 GLuint bumpedUnitNr
= key
->unit
[unit
].OptRGB
[1].Source
- SRC_TEXTURE0
;
1108 ir_rvalue
*texcoord
;
1109 ir_variable
*rot_mat_0
, *rot_mat_1
;
1111 rot_mat_0
= p
->shader
->symbols
->get_variable("gl_BumpRotMatrix0MESA");
1112 rot_mat_1
= p
->shader
->symbols
->get_variable("gl_BumpRotMatrix1MESA");
1114 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
1116 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
1117 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(bumpedUnitNr
);
1118 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
1119 tc_array
->max_array_access
= MAX2(tc_array
->max_array_access
, unit
);
1121 load_texenv_source( p
, unit
+ SRC_TEXTURE0
, unit
);
1123 /* Apply rot matrix and add coords to be available in next phase.
1124 * dest = Arg1 + (Arg0.xx * rotMat0) + (Arg0.yy * rotMat1)
1125 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1127 ir_dereference
*deref
;
1128 ir_assignment
*assign
;
1129 ir_rvalue
*bump_x
, *bump_y
;
1131 texcoord
= smear(p
, texcoord
);
1133 /* bump_texcoord = texcoord */
1134 ir_variable
*bumped
= new(p
->mem_ctx
) ir_variable(texcoord
->type
,
1137 p
->instructions
->push_tail(bumped
);
1139 deref
= new(p
->mem_ctx
) ir_dereference_variable(bumped
);
1140 assign
= new(p
->mem_ctx
) ir_assignment(deref
, texcoord
);
1141 p
->instructions
->push_tail(assign
);
1143 /* bump_texcoord.xy += arg0.x * rotmat0 + arg0.y * rotmat1 */
1144 bump
= get_source(p
, key
->unit
[unit
].OptRGB
[0].Source
, unit
);
1145 bump_x
= mul(swizzle_x(bump
), rot_mat_0
);
1146 bump_y
= mul(swizzle_y(bump
->clone(p
->mem_ctx
, NULL
)), rot_mat_1
);
1148 ir_expression
*expr
;
1150 expr
= add(swizzle_xy(bumped
), add(bump_x
, bump_y
));
1152 deref
= new(p
->mem_ctx
) ir_dereference_variable(bumped
);
1153 assign
= new(p
->mem_ctx
) ir_assignment(deref
, expr
, NULL
, WRITEMASK_XY
);
1154 p
->instructions
->push_tail(assign
);
1156 p
->texcoord_tex
[bumpedUnitNr
] = bumped
;
1160 * Applies the fog calculations.
1162 * This is basically like the ARB_fragment_prorgam fog options. Note
1163 * that ffvertex_prog.c produces fogcoord for us when
1164 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1167 emit_fog_instructions(struct texenv_fragment_program
*p
,
1168 ir_rvalue
*fragcolor
)
1170 struct state_key
*key
= p
->state
;
1171 ir_rvalue
*f
, *temp
;
1172 ir_variable
*params
, *oparams
;
1173 ir_variable
*fogcoord
;
1174 ir_assignment
*assign
;
1176 /* Temporary storage for the whole fog result. Fog calculations
1177 * only affect rgb so we're hanging on to the .a value of fragcolor
1180 ir_variable
*fog_result
= new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
1183 p
->instructions
->push_tail(fog_result
);
1184 temp
= new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1185 assign
= new(p
->mem_ctx
) ir_assignment(temp
, fragcolor
);
1186 p
->instructions
->push_tail(assign
);
1188 fragcolor
= swizzle_xyz(fog_result
);
1190 oparams
= p
->shader
->symbols
->get_variable("gl_FogParamsOptimizedMESA");
1191 fogcoord
= p
->shader
->symbols
->get_variable("gl_FogFragCoord");
1192 params
= p
->shader
->symbols
->get_variable("gl_Fog");
1193 f
= new(p
->mem_ctx
) ir_dereference_variable(fogcoord
);
1195 ir_variable
*f_var
= new(p
->mem_ctx
) ir_variable(glsl_type::float_type
,
1196 "fog_factor", ir_var_auto
);
1197 p
->instructions
->push_tail(f_var
);
1199 switch (key
->fog_mode
) {
1201 /* f = (end - z) / (end - start)
1203 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1204 * (end / (end - start)) so we can generate a single MAD.
1206 f
= add(mul(f
, swizzle_x(oparams
)), swizzle_y(oparams
));
1209 /* f = e^(-(density * fogcoord))
1211 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1212 * use EXP2 which is generally the native instruction without
1213 * having to do any further math on the fog density uniform.
1215 f
= mul(f
, swizzle_z(oparams
));
1216 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1217 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1220 /* f = e^(-(density * fogcoord)^2)
1222 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1223 * can do this like FOG_EXP but with a squaring after the
1224 * multiply by density.
1226 ir_variable
*temp_var
= new(p
->mem_ctx
) ir_variable(glsl_type::float_type
,
1229 p
->instructions
->push_tail(temp_var
);
1231 f
= mul(f
, swizzle_w(oparams
));
1233 temp
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
1234 ir_assignment
*assign
= new(p
->mem_ctx
) ir_assignment(temp
, f
);
1235 p
->instructions
->push_tail(assign
);
1237 f
= mul(temp_var
, temp_var
);
1238 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1239 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1245 temp
= new(p
->mem_ctx
) ir_dereference_variable(f_var
);
1246 assign
= new(p
->mem_ctx
) ir_assignment(temp
, f
);
1247 p
->instructions
->push_tail(assign
);
1249 f
= sub(new(p
->mem_ctx
) ir_constant(1.0f
), f_var
);
1250 temp
= new(p
->mem_ctx
) ir_dereference_variable(params
);
1251 temp
= new(p
->mem_ctx
) ir_dereference_record(temp
, "color");
1252 temp
= mul(swizzle_xyz(temp
), f
);
1254 f
= add(temp
, mul(fragcolor
, f_var
));
1256 ir_dereference
*deref
= new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1257 assign
= new(p
->mem_ctx
) ir_assignment(deref
, f
, NULL
, WRITEMASK_XYZ
);
1258 p
->instructions
->push_tail(assign
);
1260 return new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1264 emit_instructions(struct texenv_fragment_program
*p
)
1266 struct state_key
*key
= p
->state
;
1269 if (key
->enabled_units
) {
1270 /* Zeroth pass - bump map textures first */
1271 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1272 if (key
->unit
[unit
].enabled
&&
1273 key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1274 load_texunit_bumpmap(p
, unit
);
1278 /* First pass - to support texture_env_crossbar, first identify
1279 * all referenced texture sources and emit texld instructions
1282 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1283 if (key
->unit
[unit
].enabled
) {
1284 load_texunit_sources(p
, unit
);
1287 /* Second pass - emit combine instructions to build final color:
1289 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1290 if (key
->unit
[unit
].enabled
) {
1291 p
->src_previous
= emit_texenv(p
, unit
);
1296 ir_rvalue
*cf
= get_source(p
, SRC_PREVIOUS
, 0);
1297 ir_dereference_variable
*deref
;
1298 ir_assignment
*assign
;
1300 if (key
->separate_specular
) {
1302 ir_variable
*spec_result
= new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
1306 p
->instructions
->push_tail(spec_result
);
1308 deref
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1309 assign
= new(p
->mem_ctx
) ir_assignment(deref
, cf
);
1310 p
->instructions
->push_tail(assign
);
1312 ir_rvalue
*secondary
;
1313 if (p
->state
->inputs_available
& FRAG_BIT_COL1
) {
1315 p
->shader
->symbols
->get_variable("gl_SecondaryColor");
1317 secondary
= swizzle_xyz(var
);
1319 secondary
= swizzle_xyz(get_current_attrib(p
, VERT_ATTRIB_COLOR1
));
1322 tmp0
= add(swizzle_xyz(spec_result
), secondary
);
1324 deref
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1325 assign
= new(p
->mem_ctx
) ir_assignment(deref
, tmp0
, NULL
, WRITEMASK_XYZ
);
1326 p
->instructions
->push_tail(assign
);
1328 cf
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1331 if (key
->fog_enabled
) {
1332 cf
= emit_fog_instructions(p
, cf
);
1335 ir_variable
*frag_color
= p
->shader
->symbols
->get_variable("gl_FragColor");
1337 deref
= new(p
->mem_ctx
) ir_dereference_variable(frag_color
);
1338 assign
= new(p
->mem_ctx
) ir_assignment(deref
, cf
);
1339 p
->instructions
->push_tail(assign
);
1343 * Generate a new fragment program which implements the context's
1344 * current texture env/combine mode.
1346 static struct gl_shader_program
*
1347 create_new_program(struct gl_context
*ctx
, struct state_key
*key
)
1349 struct texenv_fragment_program p
;
1351 _mesa_glsl_parse_state
*state
;
1353 memset(&p
, 0, sizeof(p
));
1354 p
.mem_ctx
= ralloc_context(NULL
);
1355 p
.shader
= ctx
->Driver
.NewShader(ctx
, 0, GL_FRAGMENT_SHADER
);
1356 p
.shader
->ir
= new(p
.shader
) exec_list
;
1357 state
= new(p
.shader
) _mesa_glsl_parse_state(ctx
, GL_FRAGMENT_SHADER
,
1359 p
.shader
->symbols
= state
->symbols
;
1360 p
.top_instructions
= p
.shader
->ir
;
1361 p
.instructions
= p
.shader
->ir
;
1363 p
.shader_program
= ctx
->Driver
.NewShaderProgram(ctx
, 0);
1365 /* Tell the linker to ignore the fact that we're building a
1366 * separate shader, in case we're in a GLES2 context that would
1367 * normally reject that. The real problem is that we're building a
1368 * fixed function program in a GLES2 context at all, but that's a
1369 * big mess to clean up.
1371 p
.shader_program
->InternalSeparateShader
= GL_TRUE
;
1373 state
->language_version
= 130;
1374 if (ctx
->Extensions
.OES_EGL_image_external
)
1375 state
->OES_EGL_image_external_enable
= true;
1376 _mesa_glsl_initialize_types(state
);
1377 _mesa_glsl_initialize_variables(p
.instructions
, state
);
1379 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1380 p
.src_texture
[unit
] = NULL
;
1381 p
.texcoord_tex
[unit
] = NULL
;
1384 p
.src_previous
= NULL
;
1386 ir_function
*main_f
= new(p
.mem_ctx
) ir_function("main");
1387 p
.instructions
->push_tail(main_f
);
1388 state
->symbols
->add_function(main_f
);
1390 ir_function_signature
*main_sig
=
1391 new(p
.mem_ctx
) ir_function_signature(p
.shader
->symbols
->get_type("void"));
1392 main_sig
->is_defined
= true;
1393 main_f
->add_signature(main_sig
);
1395 p
.instructions
= &main_sig
->body
;
1396 if (key
->num_draw_buffers
)
1397 emit_instructions(&p
);
1399 validate_ir_tree(p
.shader
->ir
);
1401 while (do_common_optimization(p
.shader
->ir
, false, false, 32))
1403 reparent_ir(p
.shader
->ir
, p
.shader
->ir
);
1405 p
.shader
->CompileStatus
= true;
1406 p
.shader
->Version
= state
->language_version
;
1407 p
.shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
1408 p
.shader_program
->Shaders
=
1409 (gl_shader
**)malloc(sizeof(*p
.shader_program
->Shaders
));
1410 p
.shader_program
->Shaders
[0] = p
.shader
;
1411 p
.shader_program
->NumShaders
= 1;
1413 _mesa_glsl_link_shader(ctx
, p
.shader_program
);
1415 /* Set the sampler uniforms, and relink to get them into the linked
1418 struct gl_shader
*const fs
=
1419 p
.shader_program
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
1420 struct gl_program
*const fp
= fs
->Program
;
1422 _mesa_generate_parameters_list_for_uniforms(p
.shader_program
, fs
,
1425 _mesa_associate_uniform_storage(ctx
, p
.shader_program
, fp
->Parameters
);
1427 for (unsigned int i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1428 /* Enough space for 'sampler_999\0'.
1432 snprintf(name
, sizeof(name
), "sampler_%d", i
);
1434 int loc
= _mesa_get_uniform_location(ctx
, p
.shader_program
, name
);
1439 /* Avoid using _mesa_uniform() because it flags state
1440 * updates, so if we're generating this shader_program in a
1441 * state update, we end up recursing. Instead, just set the
1442 * value, which is picked up at re-link.
1444 _mesa_uniform_split_location_offset(loc
, &base
, &idx
);
1447 struct gl_uniform_storage
*const storage
=
1448 &p
.shader_program
->UniformStorage
[base
];
1450 /* Update the storage, the SamplerUnits in the shader program, and
1451 * the SamplerUnits in the assembly shader.
1453 storage
->storage
[idx
].i
= i
;
1454 fp
->SamplerUnits
[storage
->sampler
] = i
;
1455 p
.shader_program
->SamplerUnits
[storage
->sampler
] = i
;
1456 _mesa_propagate_uniforms_to_driver_storage(storage
, 0, 1);
1459 _mesa_update_shader_textures_used(p
.shader_program
, fp
);
1460 (void) ctx
->Driver
.ProgramStringNotify(ctx
, fp
->Target
, fp
);
1462 if (!p
.shader_program
->LinkStatus
)
1463 _mesa_problem(ctx
, "Failed to link fixed function fragment shader: %s\n",
1464 p
.shader_program
->InfoLog
);
1466 ralloc_free(p
.mem_ctx
);
1467 return p
.shader_program
;
1473 * Return a fragment program which implements the current
1474 * fixed-function texture, fog and color-sum operations.
1476 struct gl_shader_program
*
1477 _mesa_get_fixed_func_fragment_program(struct gl_context
*ctx
)
1479 struct gl_shader_program
*shader_program
;
1480 struct state_key key
;
1483 keySize
= make_state_key(ctx
, &key
);
1485 shader_program
= (struct gl_shader_program
*)
1486 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1489 if (!shader_program
) {
1490 shader_program
= create_new_program(ctx
, &key
);
1492 _mesa_shader_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1493 &key
, keySize
, shader_program
);
1496 return shader_program
;