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 "../glsl/glsl_types.h"
46 #include "../glsl/ir.h"
47 #include "../glsl/glsl_symbol_table.h"
48 #include "../glsl/glsl_parser_extras.h"
49 #include "../glsl/ir_optimization.h"
50 #include "../glsl/ir_print_visitor.h"
51 #include "../program/ir_to_mesa.h"
54 * Note on texture units:
56 * The number of texture units supported by fixed-function fragment
57 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
58 * That's because there's a one-to-one correspondence between texture
59 * coordinates and samplers in fixed-function processing.
61 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
62 * sets of texcoords, so is fixed-function fragment processing.
64 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
68 struct texenvprog_cache_item
72 struct gl_shader_program
*data
;
73 struct texenvprog_cache_item
*next
;
77 texenv_doing_secondary_color(struct gl_context
*ctx
)
79 if (ctx
->Light
.Enabled
&&
80 (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
))
83 if (ctx
->Fog
.ColorSumEnabled
)
91 __extension__ GLubyte Source
:4; /**< SRC_x */
92 __extension__ GLubyte Operand
:3; /**< OPR_x */
94 GLubyte Source
; /**< SRC_x */
95 GLubyte Operand
; /**< OPR_x */
100 GLuint nr_enabled_units
:8;
101 GLuint enabled_units
:8;
102 GLuint separate_specular
:1;
103 GLuint fog_enabled
:1;
104 GLuint fog_mode
:2; /**< FOG_x */
105 GLuint inputs_available
:12;
106 GLuint num_draw_buffers
:4;
108 /* NOTE: This array of structs must be last! (see "keySize" below) */
111 GLuint source_index
:4; /**< TEXTURE_x_INDEX */
113 GLuint ScaleShiftRGB
:2;
114 GLuint ScaleShiftA
:2;
116 GLuint NumArgsRGB
:3; /**< up to MAX_COMBINER_TERMS */
117 GLuint ModeRGB
:5; /**< MODE_x */
119 GLuint NumArgsA
:3; /**< up to MAX_COMBINER_TERMS */
120 GLuint ModeA
:5; /**< MODE_x */
122 struct mode_opt OptRGB
[MAX_COMBINER_TERMS
];
123 struct mode_opt OptA
[MAX_COMBINER_TERMS
];
124 } unit
[MAX_TEXTURE_UNITS
];
130 #define FOG_UNKNOWN 3
132 static GLuint
translate_fog_mode( GLenum mode
)
135 case GL_LINEAR
: return FOG_LINEAR
;
136 case GL_EXP
: return FOG_EXP
;
137 case GL_EXP2
: return FOG_EXP2
;
138 default: return FOG_UNKNOWN
;
142 #define OPR_SRC_COLOR 0
143 #define OPR_ONE_MINUS_SRC_COLOR 1
144 #define OPR_SRC_ALPHA 2
145 #define OPR_ONE_MINUS_SRC_ALPHA 3
148 #define OPR_UNKNOWN 7
150 static GLuint
translate_operand( GLenum operand
)
153 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
154 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
155 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
156 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
157 case GL_ZERO
: return OPR_ZERO
;
158 case GL_ONE
: return OPR_ONE
;
165 #define SRC_TEXTURE 0
166 #define SRC_TEXTURE0 1
167 #define SRC_TEXTURE1 2
168 #define SRC_TEXTURE2 3
169 #define SRC_TEXTURE3 4
170 #define SRC_TEXTURE4 5
171 #define SRC_TEXTURE5 6
172 #define SRC_TEXTURE6 7
173 #define SRC_TEXTURE7 8
174 #define SRC_CONSTANT 9
175 #define SRC_PRIMARY_COLOR 10
176 #define SRC_PREVIOUS 11
178 #define SRC_UNKNOWN 15
180 static GLuint
translate_source( GLenum src
)
183 case GL_TEXTURE
: return SRC_TEXTURE
;
191 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
192 case GL_CONSTANT
: return SRC_CONSTANT
;
193 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
194 case GL_PREVIOUS
: return SRC_PREVIOUS
;
203 #define MODE_REPLACE 0 /* r = a0 */
204 #define MODE_MODULATE 1 /* r = a0 * a1 */
205 #define MODE_ADD 2 /* r = a0 + a1 */
206 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
207 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
208 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
209 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
210 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
211 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
212 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
213 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
214 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
215 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
216 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
217 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
218 #define MODE_BUMP_ENVMAP_ATI 15 /* special */
219 #define MODE_UNKNOWN 16
222 * Translate GL combiner state into a MODE_x value
224 static GLuint
translate_mode( GLenum envMode
, GLenum mode
)
227 case GL_REPLACE
: return MODE_REPLACE
;
228 case GL_MODULATE
: return MODE_MODULATE
;
230 if (envMode
== GL_COMBINE4_NV
)
231 return MODE_ADD_PRODUCTS
;
235 if (envMode
== GL_COMBINE4_NV
)
236 return MODE_ADD_PRODUCTS_SIGNED
;
238 return MODE_ADD_SIGNED
;
239 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
240 case GL_SUBTRACT
: return MODE_SUBTRACT
;
241 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
242 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
243 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
244 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
245 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
246 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
247 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
248 case GL_BUMP_ENVMAP_ATI
: return MODE_BUMP_ENVMAP_ATI
;
257 * Do we need to clamp the results of the given texture env/combine mode?
258 * If the inputs to the mode are in [0,1] we don't always have to clamp
262 need_saturate( GLuint mode
)
267 case MODE_INTERPOLATE
:
270 case MODE_ADD_SIGNED
:
273 case MODE_DOT3_RGB_EXT
:
275 case MODE_DOT3_RGBA_EXT
:
276 case MODE_MODULATE_ADD_ATI
:
277 case MODE_MODULATE_SIGNED_ADD_ATI
:
278 case MODE_MODULATE_SUBTRACT_ATI
:
279 case MODE_ADD_PRODUCTS
:
280 case MODE_ADD_PRODUCTS_SIGNED
:
281 case MODE_BUMP_ENVMAP_ATI
:
292 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
294 static GLuint
translate_tex_src_bit( GLbitfield bit
)
297 return _mesa_ffs(bit
) - 1;
301 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
302 #define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
305 * Identify all possible varying inputs. The fragment program will
306 * never reference non-varying inputs, but will track them via state
309 * This function figures out all the inputs that the fragment program
310 * has access to. The bitmask is later reduced to just those which
311 * are actually referenced.
313 static GLbitfield
get_fp_input_mask( struct gl_context
*ctx
)
316 const GLboolean vertexShader
=
317 (ctx
->Shader
.CurrentVertexProgram
&&
318 ctx
->Shader
.CurrentVertexProgram
->LinkStatus
&&
319 ctx
->Shader
.CurrentVertexProgram
->_LinkedShaders
[MESA_SHADER_VERTEX
]);
320 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
321 GLbitfield fp_inputs
= 0x0;
323 if (ctx
->VertexProgram
._Overriden
) {
324 /* Somebody's messing with the vertex program and we don't have
325 * a clue what's happening. Assume that it could be producing
326 * all possible outputs.
330 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
331 /* _NEW_RENDERMODE */
332 fp_inputs
= (FRAG_BIT_COL0
| FRAG_BIT_TEX0
);
334 else if (!(vertexProgram
|| vertexShader
)) {
335 /* Fixed function vertex logic */
337 GLbitfield varying_inputs
= ctx
->varying_vp_inputs
;
339 /* These get generated in the setup routine regardless of the
343 if (ctx
->Point
.PointSprite
)
344 varying_inputs
|= FRAG_BITS_TEX_ANY
;
346 /* First look at what values may be computed by the generated
350 if (ctx
->Light
.Enabled
) {
351 fp_inputs
|= FRAG_BIT_COL0
;
353 if (texenv_doing_secondary_color(ctx
))
354 fp_inputs
|= FRAG_BIT_COL1
;
358 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
359 ctx
->Texture
._TexMatEnabled
) << FRAG_ATTRIB_TEX0
;
361 /* Then look at what might be varying as a result of enabled
364 if (varying_inputs
& VERT_BIT_COLOR0
)
365 fp_inputs
|= FRAG_BIT_COL0
;
366 if (varying_inputs
& VERT_BIT_COLOR1
)
367 fp_inputs
|= FRAG_BIT_COL1
;
369 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
370 << FRAG_ATTRIB_TEX0
);
374 /* calculate from vp->outputs */
375 struct gl_program
*vprog
;
376 GLbitfield64 vp_outputs
;
378 /* Choose GLSL vertex shader over ARB vertex program. Need this
379 * since vertex shader state validation comes after fragment state
380 * validation (see additional comments in state.c).
383 vprog
= ctx
->Shader
.CurrentVertexProgram
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
385 vprog
= &ctx
->VertexProgram
.Current
->Base
;
387 vp_outputs
= vprog
->OutputsWritten
;
389 /* These get generated in the setup routine regardless of the
393 if (ctx
->Point
.PointSprite
)
394 vp_outputs
|= FRAG_BITS_TEX_ANY
;
396 if (vp_outputs
& (1 << VERT_RESULT_COL0
))
397 fp_inputs
|= FRAG_BIT_COL0
;
398 if (vp_outputs
& (1 << VERT_RESULT_COL1
))
399 fp_inputs
|= FRAG_BIT_COL1
;
401 fp_inputs
|= (((vp_outputs
& VERT_RESULT_TEX_ANY
) >> VERT_RESULT_TEX0
)
402 << FRAG_ATTRIB_TEX0
);
410 * Examine current texture environment state and generate a unique
411 * key to identify it.
413 static GLuint
make_state_key( struct gl_context
*ctx
, struct state_key
*key
)
416 GLbitfield inputs_referenced
= FRAG_BIT_COL0
;
417 const GLbitfield inputs_available
= get_fp_input_mask( ctx
);
420 memset(key
, 0, sizeof(*key
));
423 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
424 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
425 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
426 const struct gl_tex_env_combine_state
*comb
= texUnit
->_CurrentCombine
;
429 if (!texUnit
->_ReallyEnabled
|| !texUnit
->Enabled
)
432 format
= texObj
->Image
[0][texObj
->BaseLevel
]->_BaseFormat
;
434 key
->unit
[i
].enabled
= 1;
435 key
->enabled_units
|= (1<<i
);
436 key
->nr_enabled_units
= i
+ 1;
437 inputs_referenced
|= FRAG_BIT_TEX(i
);
439 key
->unit
[i
].source_index
=
440 translate_tex_src_bit(texUnit
->_ReallyEnabled
);
442 key
->unit
[i
].shadow
=
443 ((texObj
->Sampler
.CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
444 ((format
== GL_DEPTH_COMPONENT
) ||
445 (format
== GL_DEPTH_STENCIL_EXT
)));
447 key
->unit
[i
].NumArgsRGB
= comb
->_NumArgsRGB
;
448 key
->unit
[i
].NumArgsA
= comb
->_NumArgsA
;
450 key
->unit
[i
].ModeRGB
=
451 translate_mode(texUnit
->EnvMode
, comb
->ModeRGB
);
453 translate_mode(texUnit
->EnvMode
, comb
->ModeA
);
455 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
456 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
458 for (j
= 0; j
< MAX_COMBINER_TERMS
; j
++) {
459 key
->unit
[i
].OptRGB
[j
].Operand
= translate_operand(comb
->OperandRGB
[j
]);
460 key
->unit
[i
].OptA
[j
].Operand
= translate_operand(comb
->OperandA
[j
]);
461 key
->unit
[i
].OptRGB
[j
].Source
= translate_source(comb
->SourceRGB
[j
]);
462 key
->unit
[i
].OptA
[j
].Source
= translate_source(comb
->SourceA
[j
]);
465 if (key
->unit
[i
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
466 /* requires some special translation */
467 key
->unit
[i
].NumArgsRGB
= 2;
468 key
->unit
[i
].ScaleShiftRGB
= 0;
469 key
->unit
[i
].OptRGB
[0].Operand
= OPR_SRC_COLOR
;
470 key
->unit
[i
].OptRGB
[0].Source
= SRC_TEXTURE
;
471 key
->unit
[i
].OptRGB
[1].Operand
= OPR_SRC_COLOR
;
472 key
->unit
[i
].OptRGB
[1].Source
= texUnit
->BumpTarget
- GL_TEXTURE0
+ SRC_TEXTURE0
;
476 /* _NEW_LIGHT | _NEW_FOG */
477 if (texenv_doing_secondary_color(ctx
)) {
478 key
->separate_specular
= 1;
479 inputs_referenced
|= FRAG_BIT_COL1
;
483 if (ctx
->Fog
.Enabled
) {
484 key
->fog_enabled
= 1;
485 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
486 inputs_referenced
|= FRAG_BIT_FOGC
; /* maybe */
490 key
->num_draw_buffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
493 if (ctx
->Color
.AlphaEnabled
&& key
->num_draw_buffers
== 0) {
494 /* if alpha test is enabled we need to emit at least one color */
495 key
->num_draw_buffers
= 1;
498 key
->inputs_available
= (inputs_available
& inputs_referenced
);
500 /* compute size of state key, ignoring unused texture units */
501 keySize
= sizeof(*key
) - sizeof(key
->unit
)
502 + key
->nr_enabled_units
* sizeof(key
->unit
[0]);
508 /** State used to build the fragment program:
510 struct texenv_fragment_program
{
511 struct gl_shader_program
*shader_program
;
512 struct gl_shader
*shader
;
513 struct gl_fragment_program
*program
;
514 exec_list
*instructions
;
515 exec_list
*top_instructions
;
517 struct state_key
*state
;
519 GLbitfield alu_temps
; /**< Track texture indirections, see spec. */
520 GLbitfield temps_output
; /**< Track texture indirections, see spec. */
521 GLbitfield temp_in_use
; /**< Tracks temporary regs which are in use. */
524 ir_variable
*src_texture
[MAX_TEXTURE_COORD_UNITS
];
525 /* Reg containing each texture unit's sampled texture color,
529 /* Texcoord override from bumpmapping. */
530 struct ir_variable
*texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
532 /* Reg containing texcoord for a texture unit,
533 * needed for bump mapping, else undef.
536 ir_rvalue
*src_previous
; /**< Reg containing color from previous
537 * stage. May need to be decl'd.
540 GLuint last_tex_stage
; /**< Number of last enabled texture unit */
544 get_current_attrib(struct texenv_fragment_program
*p
, GLuint attrib
)
546 ir_variable
*current
;
549 current
= p
->shader
->symbols
->get_variable("gl_CurrentAttribFragMESA");
550 current
->max_array_access
= MAX2(current
->max_array_access
, attrib
);
551 val
= new(p
->mem_ctx
) ir_dereference_variable(current
);
552 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(attrib
);
553 return new(p
->mem_ctx
) ir_dereference_array(val
, index
);
557 get_gl_Color(struct texenv_fragment_program
*p
)
559 if (p
->state
->inputs_available
& FRAG_BIT_COL0
) {
560 ir_variable
*var
= p
->shader
->symbols
->get_variable("gl_Color");
562 return new(p
->mem_ctx
) ir_dereference_variable(var
);
564 return get_current_attrib(p
, VERT_ATTRIB_COLOR0
);
569 get_source(struct texenv_fragment_program
*p
,
570 GLuint src
, GLuint unit
)
573 ir_dereference
*deref
;
577 return new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
587 return new(p
->mem_ctx
)
588 ir_dereference_variable(p
->src_texture
[src
- SRC_TEXTURE0
]);
591 var
= p
->shader
->symbols
->get_variable("gl_TextureEnvColor");
593 deref
= new(p
->mem_ctx
) ir_dereference_variable(var
);
594 var
->max_array_access
= MAX2(var
->max_array_access
, unit
);
595 return new(p
->mem_ctx
) ir_dereference_array(deref
,
596 new(p
->mem_ctx
) ir_constant(unit
));
598 case SRC_PRIMARY_COLOR
:
599 var
= p
->shader
->symbols
->get_variable("gl_Color");
601 return new(p
->mem_ctx
) ir_dereference_variable(var
);
604 return new(p
->mem_ctx
) ir_constant(0.0f
);
607 if (!p
->src_previous
) {
608 return get_gl_Color(p
);
610 return p
->src_previous
->clone(p
->mem_ctx
, NULL
);
620 emit_combine_source(struct texenv_fragment_program
*p
,
627 src
= get_source(p
, source
, unit
);
630 case OPR_ONE_MINUS_SRC_COLOR
:
631 return new(p
->mem_ctx
) ir_expression(ir_binop_sub
,
632 new(p
->mem_ctx
) ir_constant(1.0f
),
636 return new(p
->mem_ctx
) ir_swizzle(src
, 3, 3, 3, 3, 1);
638 case OPR_ONE_MINUS_SRC_ALPHA
:
639 return new(p
->mem_ctx
) ir_expression(ir_binop_sub
,
640 new(p
->mem_ctx
) ir_constant(1.0f
),
641 new(p
->mem_ctx
) ir_swizzle(src
,
645 return new(p
->mem_ctx
) ir_constant(0.0f
);
647 return new(p
->mem_ctx
) ir_constant(1.0f
);
657 * Check if the RGB and Alpha sources and operands match for the given
658 * texture unit's combinder state. When the RGB and A sources and
659 * operands match, we can emit fewer instructions.
661 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
663 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
665 for (i
= 0; i
< numArgs
; i
++) {
666 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
669 switch (key
->unit
[unit
].OptA
[i
].Operand
) {
671 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
679 case OPR_ONE_MINUS_SRC_ALPHA
:
680 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
681 case OPR_ONE_MINUS_SRC_COLOR
:
682 case OPR_ONE_MINUS_SRC_ALPHA
:
689 return GL_FALSE
; /* impossible */
697 smear(struct texenv_fragment_program
*p
, ir_rvalue
*val
)
699 if (!val
->type
->is_scalar())
702 return new(p
->mem_ctx
) ir_swizzle(val
, 0, 0, 0, 0, 4);
706 emit_combine(struct texenv_fragment_program
*p
,
710 const struct mode_opt
*opt
)
712 ir_rvalue
*src
[MAX_COMBINER_TERMS
];
713 ir_rvalue
*tmp0
, *tmp1
;
716 assert(nr
<= MAX_COMBINER_TERMS
);
718 for (i
= 0; i
< nr
; i
++)
719 src
[i
] = emit_combine_source( p
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
726 return new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0], src
[1]);
729 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, src
[0], src
[1]);
731 case MODE_ADD_SIGNED
:
732 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, src
[0], src
[1]);
733 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
,
734 new(p
->mem_ctx
) ir_constant(-0.5f
));
736 case MODE_INTERPOLATE
:
737 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
738 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0], src
[2]);
740 tmp1
= new(p
->mem_ctx
) ir_expression(ir_binop_sub
,
741 new(p
->mem_ctx
) ir_constant(1.0f
),
742 src
[2]->clone(p
->mem_ctx
, NULL
));
743 tmp1
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[1], tmp1
);
745 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
, tmp1
);
748 return new(p
->mem_ctx
) ir_expression(ir_binop_sub
, src
[0], src
[1]);
751 case MODE_DOT3_RGBA_EXT
:
752 case MODE_DOT3_RGB_EXT
:
753 case MODE_DOT3_RGB
: {
754 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0],
755 new(p
->mem_ctx
) ir_constant(2.0f
));
756 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
,
757 new(p
->mem_ctx
) ir_constant(-1.0f
));
758 tmp0
= new(p
->mem_ctx
) ir_swizzle(smear(p
, tmp0
), 0, 1, 2, 3, 3);
760 tmp1
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[1],
761 new(p
->mem_ctx
) ir_constant(2.0f
));
762 tmp1
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp1
,
763 new(p
->mem_ctx
) ir_constant(-1.0f
));
764 tmp1
= new(p
->mem_ctx
) ir_swizzle(smear(p
, tmp1
), 0, 1, 2, 3, 3);
766 return new(p
->mem_ctx
) ir_expression(ir_binop_dot
, tmp0
, tmp1
);
768 case MODE_MODULATE_ADD_ATI
:
769 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0], src
[2]);
770 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
, src
[1]);
772 case MODE_MODULATE_SIGNED_ADD_ATI
:
773 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0], src
[2]);
774 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
, src
[1]);
775 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
,
776 new(p
->mem_ctx
) ir_constant(-0.5f
));
778 case MODE_MODULATE_SUBTRACT_ATI
:
779 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0], src
[2]);
780 return new(p
->mem_ctx
) ir_expression(ir_binop_sub
, tmp0
, src
[1]);
782 case MODE_ADD_PRODUCTS
:
783 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0], src
[1]);
784 tmp1
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[2], src
[3]);
785 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
, tmp1
);
787 case MODE_ADD_PRODUCTS_SIGNED
:
788 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0], src
[1]);
789 tmp1
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[2], src
[3]);
790 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
, tmp1
);
791 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
,
792 new(p
->mem_ctx
) ir_constant(-0.5f
));
794 case MODE_BUMP_ENVMAP_ATI
:
795 /* special - not handled here */
805 saturate(struct texenv_fragment_program
*p
, ir_rvalue
*val
)
807 val
= new(p
->mem_ctx
) ir_expression(ir_binop_min
, val
,
808 new(p
->mem_ctx
) ir_constant(1.0f
));
809 return new(p
->mem_ctx
) ir_expression(ir_binop_max
, val
,
810 new(p
->mem_ctx
) ir_constant(0.0f
));
814 * Generate instructions for one texture unit's env/combiner mode.
817 emit_texenv(struct texenv_fragment_program
*p
, GLuint unit
)
819 const struct state_key
*key
= p
->state
;
820 GLboolean rgb_saturate
, alpha_saturate
;
821 GLuint rgb_shift
, alpha_shift
;
823 if (!key
->unit
[unit
].enabled
) {
824 return get_source(p
, SRC_PREVIOUS
, 0);
826 if (key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
827 /* this isn't really a env stage delivering a color and handled elsewhere */
828 return get_source(p
, SRC_PREVIOUS
, 0);
831 switch (key
->unit
[unit
].ModeRGB
) {
832 case MODE_DOT3_RGB_EXT
:
833 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
836 case MODE_DOT3_RGBA_EXT
:
841 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
842 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
846 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
847 * We don't want to clamp twice.
850 rgb_saturate
= GL_FALSE
; /* saturate after rgb shift */
851 else if (need_saturate(key
->unit
[unit
].ModeRGB
))
852 rgb_saturate
= GL_TRUE
;
854 rgb_saturate
= GL_FALSE
;
857 alpha_saturate
= GL_FALSE
; /* saturate after alpha shift */
858 else if (need_saturate(key
->unit
[unit
].ModeA
))
859 alpha_saturate
= GL_TRUE
;
861 alpha_saturate
= GL_FALSE
;
863 ir_variable
*temp_var
= new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
866 p
->instructions
->push_tail(temp_var
);
868 ir_dereference
*deref
;
869 ir_assignment
*assign
;
872 /* Emit the RGB and A combine ops
874 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
875 args_match(key
, unit
)) {
876 val
= emit_combine(p
, unit
,
877 key
->unit
[unit
].NumArgsRGB
,
878 key
->unit
[unit
].ModeRGB
,
879 key
->unit
[unit
].OptRGB
);
882 val
= saturate(p
, val
);
884 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
885 assign
= new(p
->mem_ctx
) ir_assignment(deref
, val
);
886 p
->instructions
->push_tail(assign
);
888 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
889 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
890 ir_rvalue
*val
= emit_combine(p
, unit
,
891 key
->unit
[unit
].NumArgsRGB
,
892 key
->unit
[unit
].ModeRGB
,
893 key
->unit
[unit
].OptRGB
);
896 val
= saturate(p
, val
);
897 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
898 assign
= new(p
->mem_ctx
) ir_assignment(deref
, val
);
899 p
->instructions
->push_tail(assign
);
902 /* Need to do something to stop from re-emitting identical
903 * argument calculations here:
905 val
= emit_combine(p
, unit
,
906 key
->unit
[unit
].NumArgsRGB
,
907 key
->unit
[unit
].ModeRGB
,
908 key
->unit
[unit
].OptRGB
);
910 val
= new(p
->mem_ctx
) ir_swizzle(val
, 0, 1, 2, 3, 3);
912 val
= saturate(p
, val
);
913 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
914 assign
= new(p
->mem_ctx
) ir_assignment(deref
, val
, NULL
, WRITEMASK_XYZ
);
915 p
->instructions
->push_tail(assign
);
917 val
= emit_combine(p
, unit
,
918 key
->unit
[unit
].NumArgsA
,
919 key
->unit
[unit
].ModeA
,
920 key
->unit
[unit
].OptA
);
922 val
= new(p
->mem_ctx
) ir_swizzle(val
, 3, 3, 3, 3, 1);
924 val
= saturate(p
, val
);
925 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
926 assign
= new(p
->mem_ctx
) ir_assignment(deref
, val
, NULL
, WRITEMASK_W
);
927 p
->instructions
->push_tail(assign
);
930 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
932 /* Deal with the final shift:
934 if (alpha_shift
|| rgb_shift
) {
937 if (rgb_shift
== alpha_shift
) {
938 shift
= new(p
->mem_ctx
) ir_constant((float)(1 << rgb_shift
));
941 float const_data
[4] = {
947 shift
= new(p
->mem_ctx
) ir_constant(glsl_type::vec4_type
,
948 (ir_constant_data
*)const_data
);
951 return saturate(p
, new(p
->mem_ctx
) ir_expression(ir_binop_mul
,
960 * Generate instruction for getting a texture source term.
962 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
964 ir_dereference
*deref
;
965 ir_assignment
*assign
;
967 if (p
->src_texture
[unit
])
970 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
973 if (!(p
->state
->inputs_available
& (FRAG_BIT_TEX0
<< unit
))) {
974 texcoord
= get_current_attrib(p
, VERT_ATTRIB_TEX0
+ unit
);
975 } else if (p
->texcoord_tex
[unit
]) {
976 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(p
->texcoord_tex
[unit
]);
978 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
980 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
981 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(unit
);
982 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
983 tc_array
->max_array_access
= MAX2(tc_array
->max_array_access
, unit
);
986 if (!p
->state
->unit
[unit
].enabled
) {
987 p
->src_texture
[unit
] = new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
990 p
->instructions
->push_tail(p
->src_texture
[unit
]);
992 deref
= new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
993 assign
= new(p
->mem_ctx
) ir_assignment(deref
,
994 new(p
->mem_ctx
) ir_constant(0.0f
));
995 p
->instructions
->push_tail(assign
);
999 const glsl_type
*sampler_type
= NULL
;
1002 switch (texTarget
) {
1003 case TEXTURE_1D_INDEX
:
1004 if (p
->state
->unit
[unit
].shadow
)
1005 sampler_type
= p
->shader
->symbols
->get_type("sampler1DShadow");
1007 sampler_type
= p
->shader
->symbols
->get_type("sampler1D");
1010 case TEXTURE_1D_ARRAY_INDEX
:
1011 if (p
->state
->unit
[unit
].shadow
)
1012 sampler_type
= p
->shader
->symbols
->get_type("sampler1DArrayShadow");
1014 sampler_type
= p
->shader
->symbols
->get_type("sampler1DArray");
1017 case TEXTURE_2D_INDEX
:
1018 if (p
->state
->unit
[unit
].shadow
)
1019 sampler_type
= p
->shader
->symbols
->get_type("sampler2DShadow");
1021 sampler_type
= p
->shader
->symbols
->get_type("sampler2D");
1024 case TEXTURE_2D_ARRAY_INDEX
:
1025 if (p
->state
->unit
[unit
].shadow
)
1026 sampler_type
= p
->shader
->symbols
->get_type("sampler2DArrayShadow");
1028 sampler_type
= p
->shader
->symbols
->get_type("sampler2DArray");
1031 case TEXTURE_RECT_INDEX
:
1032 if (p
->state
->unit
[unit
].shadow
)
1033 sampler_type
= p
->shader
->symbols
->get_type("sampler2DRectShadow");
1035 sampler_type
= p
->shader
->symbols
->get_type("sampler2DRect");
1038 case TEXTURE_3D_INDEX
:
1039 assert(!p
->state
->unit
[unit
].shadow
);
1040 sampler_type
= p
->shader
->symbols
->get_type("sampler3D");
1043 case TEXTURE_CUBE_INDEX
:
1044 if (p
->state
->unit
[unit
].shadow
)
1045 sampler_type
= p
->shader
->symbols
->get_type("samplerCubeShadow");
1047 sampler_type
= p
->shader
->symbols
->get_type("samplerCube");
1050 case TEXTURE_EXTERNAL_INDEX
:
1051 assert(!p
->state
->unit
[unit
].shadow
);
1052 sampler_type
= p
->shader
->symbols
->get_type("samplerExternalOES");
1057 p
->src_texture
[unit
] = new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
1060 p
->instructions
->push_tail(p
->src_texture
[unit
]);
1062 ir_texture
*tex
= new(p
->mem_ctx
) ir_texture(ir_tex
);
1065 char *sampler_name
= ralloc_asprintf(p
->mem_ctx
, "sampler_%d", unit
);
1066 ir_variable
*sampler
= new(p
->mem_ctx
) ir_variable(sampler_type
,
1069 p
->top_instructions
->push_head(sampler
);
1070 deref
= new(p
->mem_ctx
) ir_dereference_variable(sampler
);
1071 tex
->set_sampler(deref
, glsl_type::vec4_type
);
1073 tex
->coordinate
= new(p
->mem_ctx
) ir_swizzle(texcoord
, 0, 1, 2, 3, coords
);
1075 if (p
->state
->unit
[unit
].shadow
) {
1076 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1077 tex
->shadow_comparitor
= new(p
->mem_ctx
) ir_swizzle(texcoord
,
1083 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1084 tex
->projector
= new(p
->mem_ctx
) ir_swizzle(texcoord
, 3, 0, 0, 0, 1);
1086 deref
= new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
1087 assign
= new(p
->mem_ctx
) ir_assignment(deref
, tex
);
1088 p
->instructions
->push_tail(assign
);
1092 load_texenv_source(struct texenv_fragment_program
*p
,
1093 GLuint src
, GLuint unit
)
1097 load_texture(p
, unit
);
1108 load_texture(p
, src
- SRC_TEXTURE0
);
1112 /* not a texture src - do nothing */
1119 * Generate instructions for loading all texture source terms.
1122 load_texunit_sources( struct texenv_fragment_program
*p
, GLuint unit
)
1124 const struct state_key
*key
= p
->state
;
1127 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1128 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1131 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1132 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1139 * Generate instructions for loading bump map textures.
1142 load_texunit_bumpmap( struct texenv_fragment_program
*p
, GLuint unit
)
1144 const struct state_key
*key
= p
->state
;
1145 GLuint bumpedUnitNr
= key
->unit
[unit
].OptRGB
[1].Source
- SRC_TEXTURE0
;
1147 ir_rvalue
*texcoord
;
1148 ir_variable
*rot_mat_0_var
, *rot_mat_1_var
;
1149 ir_dereference_variable
*rot_mat_0
, *rot_mat_1
;
1151 rot_mat_0_var
= p
->shader
->symbols
->get_variable("gl_BumpRotMatrix0MESA");
1152 rot_mat_1_var
= p
->shader
->symbols
->get_variable("gl_BumpRotMatrix1MESA");
1153 rot_mat_0
= new(p
->mem_ctx
) ir_dereference_variable(rot_mat_0_var
);
1154 rot_mat_1
= new(p
->mem_ctx
) ir_dereference_variable(rot_mat_1_var
);
1156 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
1158 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
1159 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(bumpedUnitNr
);
1160 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
1161 tc_array
->max_array_access
= MAX2(tc_array
->max_array_access
, unit
);
1163 load_texenv_source( p
, unit
+ SRC_TEXTURE0
, unit
);
1165 /* Apply rot matrix and add coords to be available in next phase.
1166 * dest = Arg1 + (Arg0.xx * rotMat0) + (Arg0.yy * rotMat1)
1167 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1169 ir_dereference
*deref
;
1170 ir_assignment
*assign
;
1171 ir_rvalue
*bump_x
, *bump_y
;
1173 texcoord
= smear(p
, texcoord
);
1175 /* bump_texcoord = texcoord */
1176 ir_variable
*bumped
= new(p
->mem_ctx
) ir_variable(texcoord
->type
,
1179 p
->instructions
->push_tail(bumped
);
1181 deref
= new(p
->mem_ctx
) ir_dereference_variable(bumped
);
1182 assign
= new(p
->mem_ctx
) ir_assignment(deref
, texcoord
);
1183 p
->instructions
->push_tail(assign
);
1185 /* bump_texcoord.xy += arg0.x * rotmat0 + arg0.y * rotmat1 */
1186 bump
= get_source(p
, key
->unit
[unit
].OptRGB
[0].Source
, unit
);
1187 bump_x
= new(p
->mem_ctx
) ir_swizzle(bump
, 0, 0, 0, 0, 1);
1188 bump
= bump
->clone(p
->mem_ctx
, NULL
);
1189 bump_y
= new(p
->mem_ctx
) ir_swizzle(bump
, 1, 0, 0, 0, 1);
1191 bump_x
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, bump_x
, rot_mat_0
);
1192 bump_y
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, bump_y
, rot_mat_1
);
1194 ir_expression
*expr
;
1195 expr
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, bump_x
, bump_y
);
1197 deref
= new(p
->mem_ctx
) ir_dereference_variable(bumped
);
1198 expr
= new(p
->mem_ctx
) ir_expression(ir_binop_add
,
1199 new(p
->mem_ctx
) ir_swizzle(deref
,
1204 deref
= new(p
->mem_ctx
) ir_dereference_variable(bumped
);
1205 assign
= new(p
->mem_ctx
) ir_assignment(deref
, expr
, NULL
, WRITEMASK_XY
);
1206 p
->instructions
->push_tail(assign
);
1208 p
->texcoord_tex
[bumpedUnitNr
] = bumped
;
1212 * Applies the fog calculations.
1214 * This is basically like the ARB_fragment_prorgam fog options. Note
1215 * that ffvertex_prog.c produces fogcoord for us when
1216 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1219 emit_fog_instructions(struct texenv_fragment_program
*p
,
1220 ir_rvalue
*fragcolor
)
1222 struct state_key
*key
= p
->state
;
1223 ir_rvalue
*f
, *temp
;
1224 ir_variable
*params
, *oparams
;
1225 ir_variable
*fogcoord
;
1226 ir_assignment
*assign
;
1228 /* Temporary storage for the whole fog result. Fog calculations
1229 * only affect rgb so we're hanging on to the .a value of fragcolor
1232 ir_variable
*fog_result
= new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
1235 p
->instructions
->push_tail(fog_result
);
1236 temp
= new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1237 assign
= new(p
->mem_ctx
) ir_assignment(temp
, fragcolor
);
1238 p
->instructions
->push_tail(assign
);
1240 temp
= new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1241 fragcolor
= new(p
->mem_ctx
) ir_swizzle(temp
, 0, 1, 2, 3, 3);
1243 oparams
= p
->shader
->symbols
->get_variable("gl_FogParamsOptimizedMESA");
1244 fogcoord
= p
->shader
->symbols
->get_variable("gl_FogFragCoord");
1245 params
= p
->shader
->symbols
->get_variable("gl_Fog");
1246 f
= new(p
->mem_ctx
) ir_dereference_variable(fogcoord
);
1248 ir_variable
*f_var
= new(p
->mem_ctx
) ir_variable(glsl_type::float_type
,
1249 "fog_factor", ir_var_auto
);
1250 p
->instructions
->push_tail(f_var
);
1252 switch (key
->fog_mode
) {
1254 /* f = (end - z) / (end - start)
1256 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1257 * (end / (end - start)) so we can generate a single MAD.
1259 temp
= new(p
->mem_ctx
) ir_dereference_variable(oparams
);
1260 temp
= new(p
->mem_ctx
) ir_swizzle(temp
, 0, 0, 0, 0, 1);
1261 f
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, f
, temp
);
1263 temp
= new(p
->mem_ctx
) ir_dereference_variable(oparams
);
1264 temp
= new(p
->mem_ctx
) ir_swizzle(temp
, 1, 0, 0, 0, 1);
1265 f
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, f
, temp
);
1268 /* f = e^(-(density * fogcoord))
1270 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1271 * use EXP2 which is generally the native instruction without
1272 * having to do any further math on the fog density uniform.
1274 temp
= new(p
->mem_ctx
) ir_dereference_variable(oparams
);
1275 temp
= new(p
->mem_ctx
) ir_swizzle(temp
, 2, 0, 0, 0, 1);
1276 f
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, f
, temp
);
1277 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1278 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1281 /* f = e^(-(density * fogcoord)^2)
1283 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1284 * can do this like FOG_EXP but with a squaring after the
1285 * multiply by density.
1287 ir_variable
*temp_var
= new(p
->mem_ctx
) ir_variable(glsl_type::float_type
,
1290 p
->instructions
->push_tail(temp_var
);
1292 temp
= new(p
->mem_ctx
) ir_dereference_variable(oparams
);
1293 temp
= new(p
->mem_ctx
) ir_swizzle(temp
, 3, 0, 0, 0, 1);
1294 f
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
,
1297 temp
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
1298 ir_assignment
*assign
= new(p
->mem_ctx
) ir_assignment(temp
, f
);
1299 p
->instructions
->push_tail(assign
);
1301 f
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
1302 temp
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
1303 f
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, f
, temp
);
1304 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1305 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1311 temp
= new(p
->mem_ctx
) ir_dereference_variable(f_var
);
1312 assign
= new(p
->mem_ctx
) ir_assignment(temp
, f
);
1313 p
->instructions
->push_tail(assign
);
1315 f
= new(p
->mem_ctx
) ir_dereference_variable(f_var
);
1316 f
= new(p
->mem_ctx
) ir_expression(ir_binop_sub
,
1317 new(p
->mem_ctx
) ir_constant(1.0f
),
1319 temp
= new(p
->mem_ctx
) ir_dereference_variable(params
);
1320 temp
= new(p
->mem_ctx
) ir_dereference_record(temp
, "color");
1321 temp
= new(p
->mem_ctx
) ir_swizzle(temp
, 0, 1, 2, 3, 3);
1322 temp
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, temp
, f
);
1324 f
= new(p
->mem_ctx
) ir_dereference_variable(f_var
);
1325 f
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, fragcolor
, f
);
1326 f
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, temp
, f
);
1328 ir_dereference
*deref
= new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1329 assign
= new(p
->mem_ctx
) ir_assignment(deref
, f
, NULL
, WRITEMASK_XYZ
);
1330 p
->instructions
->push_tail(assign
);
1332 return new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1336 emit_instructions(struct texenv_fragment_program
*p
)
1338 struct state_key
*key
= p
->state
;
1341 if (key
->enabled_units
) {
1342 /* Zeroth pass - bump map textures first */
1343 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1344 if (key
->unit
[unit
].enabled
&&
1345 key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1346 load_texunit_bumpmap(p
, unit
);
1350 /* First pass - to support texture_env_crossbar, first identify
1351 * all referenced texture sources and emit texld instructions
1354 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1355 if (key
->unit
[unit
].enabled
) {
1356 load_texunit_sources(p
, unit
);
1357 p
->last_tex_stage
= unit
;
1360 /* Second pass - emit combine instructions to build final color:
1362 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1363 if (key
->unit
[unit
].enabled
) {
1364 p
->src_previous
= emit_texenv(p
, unit
);
1369 ir_rvalue
*cf
= get_source(p
, SRC_PREVIOUS
, 0);
1370 ir_dereference_variable
*deref
;
1371 ir_assignment
*assign
;
1373 if (key
->separate_specular
) {
1375 ir_variable
*spec_result
= new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
1379 p
->instructions
->push_tail(spec_result
);
1381 deref
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1382 assign
= new(p
->mem_ctx
) ir_assignment(deref
, cf
);
1383 p
->instructions
->push_tail(assign
);
1385 deref
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1386 tmp0
= new(p
->mem_ctx
) ir_swizzle(deref
, 0, 1, 2, 3, 3);
1388 ir_rvalue
*secondary
;
1389 if (p
->state
->inputs_available
& FRAG_BIT_COL1
) {
1391 p
->shader
->symbols
->get_variable("gl_SecondaryColor");
1393 secondary
= new(p
->mem_ctx
) ir_dereference_variable(var
);
1395 secondary
= get_current_attrib(p
, VERT_ATTRIB_COLOR1
);
1397 secondary
= new(p
->mem_ctx
) ir_swizzle(secondary
, 0, 1, 2, 3, 3);
1399 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
, secondary
);
1401 deref
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1402 assign
= new(p
->mem_ctx
) ir_assignment(deref
, tmp0
, NULL
, WRITEMASK_XYZ
);
1403 p
->instructions
->push_tail(assign
);
1405 cf
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1408 if (key
->fog_enabled
) {
1409 cf
= emit_fog_instructions(p
, cf
);
1412 ir_variable
*frag_color
= p
->shader
->symbols
->get_variable("gl_FragColor");
1414 deref
= new(p
->mem_ctx
) ir_dereference_variable(frag_color
);
1415 assign
= new(p
->mem_ctx
) ir_assignment(deref
, cf
);
1416 p
->instructions
->push_tail(assign
);
1420 * Generate a new fragment program which implements the context's
1421 * current texture env/combine mode.
1423 static struct gl_shader_program
*
1424 create_new_program(struct gl_context
*ctx
, struct state_key
*key
)
1426 struct texenv_fragment_program p
;
1428 _mesa_glsl_parse_state
*state
;
1430 memset(&p
, 0, sizeof(p
));
1431 p
.mem_ctx
= ralloc_context(NULL
);
1432 p
.shader
= ctx
->Driver
.NewShader(ctx
, 0, GL_FRAGMENT_SHADER
);
1433 p
.shader
->ir
= new(p
.shader
) exec_list
;
1434 state
= new(p
.shader
) _mesa_glsl_parse_state(ctx
, GL_FRAGMENT_SHADER
,
1436 p
.shader
->symbols
= state
->symbols
;
1437 p
.top_instructions
= p
.shader
->ir
;
1438 p
.instructions
= p
.shader
->ir
;
1440 p
.shader_program
= ctx
->Driver
.NewShaderProgram(ctx
, 0);
1442 /* Tell the linker to ignore the fact that we're building a
1443 * separate shader, in case we're in a GLES2 context that would
1444 * normally reject that. The real problem is that we're building a
1445 * fixed function program in a GLES2 context at all, but that's a
1446 * big mess to clean up.
1448 p
.shader_program
->InternalSeparateShader
= GL_TRUE
;
1450 state
->language_version
= 130;
1451 if (ctx
->Extensions
.OES_EGL_image_external
)
1452 state
->OES_EGL_image_external_enable
= true;
1453 _mesa_glsl_initialize_types(state
);
1454 _mesa_glsl_initialize_variables(p
.instructions
, state
);
1456 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1457 p
.src_texture
[unit
] = NULL
;
1458 p
.texcoord_tex
[unit
] = NULL
;
1461 p
.src_previous
= NULL
;
1463 p
.last_tex_stage
= 0;
1465 ir_function
*main_f
= new(p
.mem_ctx
) ir_function("main");
1466 p
.instructions
->push_tail(main_f
);
1467 state
->symbols
->add_function(main_f
);
1469 ir_function_signature
*main_sig
=
1470 new(p
.mem_ctx
) ir_function_signature(p
.shader
->symbols
->get_type("void"));
1471 main_sig
->is_defined
= true;
1472 main_f
->add_signature(main_sig
);
1474 p
.instructions
= &main_sig
->body
;
1475 if (key
->num_draw_buffers
)
1476 emit_instructions(&p
);
1478 validate_ir_tree(p
.shader
->ir
);
1480 while (do_common_optimization(p
.shader
->ir
, false, false, 32))
1482 reparent_ir(p
.shader
->ir
, p
.shader
->ir
);
1484 p
.shader
->CompileStatus
= true;
1485 p
.shader
->Version
= state
->language_version
;
1486 p
.shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
1487 p
.shader_program
->Shaders
=
1488 (gl_shader
**)malloc(sizeof(*p
.shader_program
->Shaders
));
1489 p
.shader_program
->Shaders
[0] = p
.shader
;
1490 p
.shader_program
->NumShaders
= 1;
1492 _mesa_glsl_link_shader(ctx
, p
.shader_program
);
1494 /* Set the sampler uniforms, and relink to get them into the linked
1497 struct gl_program
*fp
;
1498 fp
= p
.shader_program
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
;
1500 for (unsigned int i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1501 char *name
= ralloc_asprintf(p
.mem_ctx
, "sampler_%d", i
);
1502 int loc
= _mesa_get_uniform_location(ctx
, p
.shader_program
, name
);
1504 /* Avoid using _mesa_uniform() because it flags state
1505 * updates, so if we're generating this shader_program in a
1506 * state update, we end up recursing. Instead, just set the
1507 * value, which is picked up at re-link.
1509 loc
= (loc
& 0xffff) + (loc
>> 16);
1510 int sampler
= fp
->Parameters
->ParameterValues
[loc
][0].f
;
1512 fp
->SamplerUnits
[sampler
] = i
;
1515 _mesa_update_shader_textures_used(fp
);
1516 (void) ctx
->Driver
.ProgramStringNotify(ctx
, fp
->Target
, fp
);
1518 if (!p
.shader_program
->LinkStatus
)
1519 _mesa_problem(ctx
, "Failed to link fixed function fragment shader: %s\n",
1520 p
.shader_program
->InfoLog
);
1522 ralloc_free(p
.mem_ctx
);
1523 return p
.shader_program
;
1529 * Return a fragment program which implements the current
1530 * fixed-function texture, fog and color-sum operations.
1532 struct gl_shader_program
*
1533 _mesa_get_fixed_func_fragment_program(struct gl_context
*ctx
)
1535 struct gl_shader_program
*shader_program
;
1536 struct state_key key
;
1539 keySize
= make_state_key(ctx
, &key
);
1541 shader_program
= (struct gl_shader_program
*)
1542 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1545 if (!shader_program
) {
1546 shader_program
= create_new_program(ctx
, &key
);
1548 _mesa_shader_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1549 &key
, keySize
, shader_program
);
1552 return shader_program
;