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/glsl_symbol_table.h"
49 #include "../glsl/glsl_parser_extras.h"
50 #include "../glsl/ir_optimization.h"
51 #include "../glsl/ir_print_visitor.h"
52 #include "../program/ir_to_mesa.h"
55 * Note on texture units:
57 * The number of texture units supported by fixed-function fragment
58 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
59 * That's because there's a one-to-one correspondence between texture
60 * coordinates and samplers in fixed-function processing.
62 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
63 * sets of texcoords, so is fixed-function fragment processing.
65 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
69 struct texenvprog_cache_item
73 struct gl_shader_program
*data
;
74 struct texenvprog_cache_item
*next
;
78 texenv_doing_secondary_color(struct gl_context
*ctx
)
80 if (ctx
->Light
.Enabled
&&
81 (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
))
84 if (ctx
->Fog
.ColorSumEnabled
)
92 __extension__ GLubyte Source
:4; /**< SRC_x */
93 __extension__ GLubyte Operand
:3; /**< OPR_x */
95 GLubyte Source
; /**< SRC_x */
96 GLubyte Operand
; /**< OPR_x */
101 GLuint nr_enabled_units
:8;
102 GLuint enabled_units
:8;
103 GLuint separate_specular
:1;
104 GLuint fog_enabled
:1;
105 GLuint fog_mode
:2; /**< FOG_x */
106 GLuint inputs_available
:12;
107 GLuint num_draw_buffers
:4;
109 /* NOTE: This array of structs must be last! (see "keySize" below) */
112 GLuint source_index
:4; /**< TEXTURE_x_INDEX */
114 GLuint ScaleShiftRGB
:2;
115 GLuint ScaleShiftA
:2;
117 GLuint NumArgsRGB
:3; /**< up to MAX_COMBINER_TERMS */
118 GLuint ModeRGB
:5; /**< MODE_x */
120 GLuint NumArgsA
:3; /**< up to MAX_COMBINER_TERMS */
121 GLuint ModeA
:5; /**< MODE_x */
123 struct mode_opt OptRGB
[MAX_COMBINER_TERMS
];
124 struct mode_opt OptA
[MAX_COMBINER_TERMS
];
125 } unit
[MAX_TEXTURE_UNITS
];
131 #define FOG_UNKNOWN 3
133 static GLuint
translate_fog_mode( GLenum mode
)
136 case GL_LINEAR
: return FOG_LINEAR
;
137 case GL_EXP
: return FOG_EXP
;
138 case GL_EXP2
: return FOG_EXP2
;
139 default: return FOG_UNKNOWN
;
143 #define OPR_SRC_COLOR 0
144 #define OPR_ONE_MINUS_SRC_COLOR 1
145 #define OPR_SRC_ALPHA 2
146 #define OPR_ONE_MINUS_SRC_ALPHA 3
149 #define OPR_UNKNOWN 7
151 static GLuint
translate_operand( GLenum operand
)
154 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
155 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
156 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
157 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
158 case GL_ZERO
: return OPR_ZERO
;
159 case GL_ONE
: return OPR_ONE
;
166 #define SRC_TEXTURE 0
167 #define SRC_TEXTURE0 1
168 #define SRC_TEXTURE1 2
169 #define SRC_TEXTURE2 3
170 #define SRC_TEXTURE3 4
171 #define SRC_TEXTURE4 5
172 #define SRC_TEXTURE5 6
173 #define SRC_TEXTURE6 7
174 #define SRC_TEXTURE7 8
175 #define SRC_CONSTANT 9
176 #define SRC_PRIMARY_COLOR 10
177 #define SRC_PREVIOUS 11
179 #define SRC_UNKNOWN 15
181 static GLuint
translate_source( GLenum src
)
184 case GL_TEXTURE
: return SRC_TEXTURE
;
192 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
193 case GL_CONSTANT
: return SRC_CONSTANT
;
194 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
195 case GL_PREVIOUS
: return SRC_PREVIOUS
;
204 #define MODE_REPLACE 0 /* r = a0 */
205 #define MODE_MODULATE 1 /* r = a0 * a1 */
206 #define MODE_ADD 2 /* r = a0 + a1 */
207 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
208 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
209 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
210 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
211 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
212 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
213 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
214 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
215 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
216 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
217 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
218 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
219 #define MODE_BUMP_ENVMAP_ATI 15 /* special */
220 #define MODE_UNKNOWN 16
223 * Translate GL combiner state into a MODE_x value
225 static GLuint
translate_mode( GLenum envMode
, GLenum mode
)
228 case GL_REPLACE
: return MODE_REPLACE
;
229 case GL_MODULATE
: return MODE_MODULATE
;
231 if (envMode
== GL_COMBINE4_NV
)
232 return MODE_ADD_PRODUCTS
;
236 if (envMode
== GL_COMBINE4_NV
)
237 return MODE_ADD_PRODUCTS_SIGNED
;
239 return MODE_ADD_SIGNED
;
240 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
241 case GL_SUBTRACT
: return MODE_SUBTRACT
;
242 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
243 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
244 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
245 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
246 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
247 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
248 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
249 case GL_BUMP_ENVMAP_ATI
: return MODE_BUMP_ENVMAP_ATI
;
258 * Do we need to clamp the results of the given texture env/combine mode?
259 * If the inputs to the mode are in [0,1] we don't always have to clamp
263 need_saturate( GLuint mode
)
268 case MODE_INTERPOLATE
:
271 case MODE_ADD_SIGNED
:
274 case MODE_DOT3_RGB_EXT
:
276 case MODE_DOT3_RGBA_EXT
:
277 case MODE_MODULATE_ADD_ATI
:
278 case MODE_MODULATE_SIGNED_ADD_ATI
:
279 case MODE_MODULATE_SUBTRACT_ATI
:
280 case MODE_ADD_PRODUCTS
:
281 case MODE_ADD_PRODUCTS_SIGNED
:
282 case MODE_BUMP_ENVMAP_ATI
:
293 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
295 static GLuint
translate_tex_src_bit( GLbitfield bit
)
298 return _mesa_ffs(bit
) - 1;
302 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
303 #define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
306 * Identify all possible varying inputs. The fragment program will
307 * never reference non-varying inputs, but will track them via state
310 * This function figures out all the inputs that the fragment program
311 * has access to. The bitmask is later reduced to just those which
312 * are actually referenced.
314 static GLbitfield
get_fp_input_mask( struct gl_context
*ctx
)
317 const GLboolean vertexShader
=
318 (ctx
->Shader
.CurrentVertexProgram
&&
319 ctx
->Shader
.CurrentVertexProgram
->LinkStatus
&&
320 ctx
->Shader
.CurrentVertexProgram
->_LinkedShaders
[MESA_SHADER_VERTEX
]);
321 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
322 GLbitfield fp_inputs
= 0x0;
324 if (ctx
->VertexProgram
._Overriden
) {
325 /* Somebody's messing with the vertex program and we don't have
326 * a clue what's happening. Assume that it could be producing
327 * all possible outputs.
331 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
332 /* _NEW_RENDERMODE */
333 fp_inputs
= (FRAG_BIT_COL0
| FRAG_BIT_TEX0
);
335 else if (!(vertexProgram
|| vertexShader
)) {
336 /* Fixed function vertex logic */
338 GLbitfield64 varying_inputs
= ctx
->varying_vp_inputs
;
340 /* These get generated in the setup routine regardless of the
344 if (ctx
->Point
.PointSprite
)
345 varying_inputs
|= FRAG_BITS_TEX_ANY
;
347 /* First look at what values may be computed by the generated
351 if (ctx
->Light
.Enabled
) {
352 fp_inputs
|= FRAG_BIT_COL0
;
354 if (texenv_doing_secondary_color(ctx
))
355 fp_inputs
|= FRAG_BIT_COL1
;
359 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
360 ctx
->Texture
._TexMatEnabled
) << FRAG_ATTRIB_TEX0
;
362 /* Then look at what might be varying as a result of enabled
365 if (varying_inputs
& VERT_BIT_COLOR0
)
366 fp_inputs
|= FRAG_BIT_COL0
;
367 if (varying_inputs
& VERT_BIT_COLOR1
)
368 fp_inputs
|= FRAG_BIT_COL1
;
370 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
371 << FRAG_ATTRIB_TEX0
);
375 /* calculate from vp->outputs */
376 struct gl_program
*vprog
;
377 GLbitfield64 vp_outputs
;
379 /* Choose GLSL vertex shader over ARB vertex program. Need this
380 * since vertex shader state validation comes after fragment state
381 * validation (see additional comments in state.c).
384 vprog
= ctx
->Shader
.CurrentVertexProgram
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
386 vprog
= &ctx
->VertexProgram
.Current
->Base
;
388 vp_outputs
= vprog
->OutputsWritten
;
390 /* These get generated in the setup routine regardless of the
394 if (ctx
->Point
.PointSprite
)
395 vp_outputs
|= FRAG_BITS_TEX_ANY
;
397 if (vp_outputs
& (1 << VERT_RESULT_COL0
))
398 fp_inputs
|= FRAG_BIT_COL0
;
399 if (vp_outputs
& (1 << VERT_RESULT_COL1
))
400 fp_inputs
|= FRAG_BIT_COL1
;
402 fp_inputs
|= (((vp_outputs
& VERT_RESULT_TEX_ANY
) >> VERT_RESULT_TEX0
)
403 << FRAG_ATTRIB_TEX0
);
411 * Examine current texture environment state and generate a unique
412 * key to identify it.
414 static GLuint
make_state_key( struct gl_context
*ctx
, struct state_key
*key
)
417 GLbitfield inputs_referenced
= FRAG_BIT_COL0
;
418 const GLbitfield inputs_available
= get_fp_input_mask( ctx
);
421 memset(key
, 0, sizeof(*key
));
424 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
425 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
426 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
427 const struct gl_tex_env_combine_state
*comb
= texUnit
->_CurrentCombine
;
430 if (!texUnit
->_ReallyEnabled
|| !texUnit
->Enabled
)
433 format
= texObj
->Image
[0][texObj
->BaseLevel
]->_BaseFormat
;
435 key
->unit
[i
].enabled
= 1;
436 key
->enabled_units
|= (1<<i
);
437 key
->nr_enabled_units
= i
+ 1;
438 inputs_referenced
|= FRAG_BIT_TEX(i
);
440 key
->unit
[i
].source_index
=
441 translate_tex_src_bit(texUnit
->_ReallyEnabled
);
443 key
->unit
[i
].shadow
=
444 ((texObj
->Sampler
.CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
445 ((format
== GL_DEPTH_COMPONENT
) ||
446 (format
== GL_DEPTH_STENCIL_EXT
)));
448 key
->unit
[i
].NumArgsRGB
= comb
->_NumArgsRGB
;
449 key
->unit
[i
].NumArgsA
= comb
->_NumArgsA
;
451 key
->unit
[i
].ModeRGB
=
452 translate_mode(texUnit
->EnvMode
, comb
->ModeRGB
);
454 translate_mode(texUnit
->EnvMode
, comb
->ModeA
);
456 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
457 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
459 for (j
= 0; j
< MAX_COMBINER_TERMS
; j
++) {
460 key
->unit
[i
].OptRGB
[j
].Operand
= translate_operand(comb
->OperandRGB
[j
]);
461 key
->unit
[i
].OptA
[j
].Operand
= translate_operand(comb
->OperandA
[j
]);
462 key
->unit
[i
].OptRGB
[j
].Source
= translate_source(comb
->SourceRGB
[j
]);
463 key
->unit
[i
].OptA
[j
].Source
= translate_source(comb
->SourceA
[j
]);
466 if (key
->unit
[i
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
467 /* requires some special translation */
468 key
->unit
[i
].NumArgsRGB
= 2;
469 key
->unit
[i
].ScaleShiftRGB
= 0;
470 key
->unit
[i
].OptRGB
[0].Operand
= OPR_SRC_COLOR
;
471 key
->unit
[i
].OptRGB
[0].Source
= SRC_TEXTURE
;
472 key
->unit
[i
].OptRGB
[1].Operand
= OPR_SRC_COLOR
;
473 key
->unit
[i
].OptRGB
[1].Source
= texUnit
->BumpTarget
- GL_TEXTURE0
+ SRC_TEXTURE0
;
477 /* _NEW_LIGHT | _NEW_FOG */
478 if (texenv_doing_secondary_color(ctx
)) {
479 key
->separate_specular
= 1;
480 inputs_referenced
|= FRAG_BIT_COL1
;
484 if (ctx
->Fog
.Enabled
) {
485 key
->fog_enabled
= 1;
486 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
487 inputs_referenced
|= FRAG_BIT_FOGC
; /* maybe */
491 key
->num_draw_buffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
494 if (ctx
->Color
.AlphaEnabled
&& key
->num_draw_buffers
== 0) {
495 /* if alpha test is enabled we need to emit at least one color */
496 key
->num_draw_buffers
= 1;
499 key
->inputs_available
= (inputs_available
& inputs_referenced
);
501 /* compute size of state key, ignoring unused texture units */
502 keySize
= sizeof(*key
) - sizeof(key
->unit
)
503 + key
->nr_enabled_units
* sizeof(key
->unit
[0]);
509 /** State used to build the fragment program:
511 struct texenv_fragment_program
{
512 struct gl_shader_program
*shader_program
;
513 struct gl_shader
*shader
;
514 struct gl_fragment_program
*program
;
515 exec_list
*instructions
;
516 exec_list
*top_instructions
;
518 struct state_key
*state
;
520 GLbitfield alu_temps
; /**< Track texture indirections, see spec. */
521 GLbitfield temps_output
; /**< Track texture indirections, see spec. */
522 GLbitfield temp_in_use
; /**< Tracks temporary regs which are in use. */
525 ir_variable
*src_texture
[MAX_TEXTURE_COORD_UNITS
];
526 /* Reg containing each texture unit's sampled texture color,
530 /* Texcoord override from bumpmapping. */
531 struct ir_variable
*texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
533 /* Reg containing texcoord for a texture unit,
534 * needed for bump mapping, else undef.
537 ir_rvalue
*src_previous
; /**< Reg containing color from previous
538 * stage. May need to be decl'd.
541 GLuint last_tex_stage
; /**< Number of last enabled texture unit */
545 get_current_attrib(struct texenv_fragment_program
*p
, GLuint attrib
)
547 ir_variable
*current
;
550 current
= p
->shader
->symbols
->get_variable("gl_CurrentAttribFragMESA");
551 current
->max_array_access
= MAX2(current
->max_array_access
, attrib
);
552 val
= new(p
->mem_ctx
) ir_dereference_variable(current
);
553 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(attrib
);
554 return new(p
->mem_ctx
) ir_dereference_array(val
, index
);
558 get_gl_Color(struct texenv_fragment_program
*p
)
560 if (p
->state
->inputs_available
& FRAG_BIT_COL0
) {
561 ir_variable
*var
= p
->shader
->symbols
->get_variable("gl_Color");
563 return new(p
->mem_ctx
) ir_dereference_variable(var
);
565 return get_current_attrib(p
, VERT_ATTRIB_COLOR0
);
570 get_source(struct texenv_fragment_program
*p
,
571 GLuint src
, GLuint unit
)
574 ir_dereference
*deref
;
578 return new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
588 return new(p
->mem_ctx
)
589 ir_dereference_variable(p
->src_texture
[src
- SRC_TEXTURE0
]);
592 var
= p
->shader
->symbols
->get_variable("gl_TextureEnvColor");
594 deref
= new(p
->mem_ctx
) ir_dereference_variable(var
);
595 var
->max_array_access
= MAX2(var
->max_array_access
, unit
);
596 return new(p
->mem_ctx
) ir_dereference_array(deref
,
597 new(p
->mem_ctx
) ir_constant(unit
));
599 case SRC_PRIMARY_COLOR
:
600 var
= p
->shader
->symbols
->get_variable("gl_Color");
602 return new(p
->mem_ctx
) ir_dereference_variable(var
);
605 return new(p
->mem_ctx
) ir_constant(0.0f
);
608 if (!p
->src_previous
) {
609 return get_gl_Color(p
);
611 return p
->src_previous
->clone(p
->mem_ctx
, NULL
);
621 emit_combine_source(struct texenv_fragment_program
*p
,
628 src
= get_source(p
, source
, unit
);
631 case OPR_ONE_MINUS_SRC_COLOR
:
632 return new(p
->mem_ctx
) ir_expression(ir_binop_sub
,
633 new(p
->mem_ctx
) ir_constant(1.0f
),
637 return src
->type
->is_scalar()
638 ? src
: (ir_rvalue
*) new(p
->mem_ctx
) ir_swizzle(src
, 3, 3, 3, 3, 1);
640 case OPR_ONE_MINUS_SRC_ALPHA
: {
641 ir_rvalue
*const scalar
= (src
->type
->is_scalar())
642 ? src
: (ir_rvalue
*) new(p
->mem_ctx
) ir_swizzle(src
, 3, 3, 3, 3, 1);
644 return new(p
->mem_ctx
) ir_expression(ir_binop_sub
,
645 new(p
->mem_ctx
) ir_constant(1.0f
),
650 return new(p
->mem_ctx
) ir_constant(0.0f
);
652 return new(p
->mem_ctx
) ir_constant(1.0f
);
662 * Check if the RGB and Alpha sources and operands match for the given
663 * texture unit's combinder state. When the RGB and A sources and
664 * operands match, we can emit fewer instructions.
666 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
668 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
670 for (i
= 0; i
< numArgs
; i
++) {
671 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
674 switch (key
->unit
[unit
].OptA
[i
].Operand
) {
676 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
684 case OPR_ONE_MINUS_SRC_ALPHA
:
685 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
686 case OPR_ONE_MINUS_SRC_COLOR
:
687 case OPR_ONE_MINUS_SRC_ALPHA
:
694 return GL_FALSE
; /* impossible */
702 smear(struct texenv_fragment_program
*p
, ir_rvalue
*val
)
704 if (!val
->type
->is_scalar())
707 return new(p
->mem_ctx
) ir_swizzle(val
, 0, 0, 0, 0, 4);
711 emit_combine(struct texenv_fragment_program
*p
,
715 const struct mode_opt
*opt
)
717 ir_rvalue
*src
[MAX_COMBINER_TERMS
];
718 ir_rvalue
*tmp0
, *tmp1
;
721 assert(nr
<= MAX_COMBINER_TERMS
);
723 for (i
= 0; i
< nr
; i
++)
724 src
[i
] = emit_combine_source( p
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
731 return new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0], src
[1]);
734 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, src
[0], src
[1]);
736 case MODE_ADD_SIGNED
:
737 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, src
[0], src
[1]);
738 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
,
739 new(p
->mem_ctx
) ir_constant(-0.5f
));
741 case MODE_INTERPOLATE
:
742 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
743 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0], src
[2]);
745 tmp1
= new(p
->mem_ctx
) ir_expression(ir_binop_sub
,
746 new(p
->mem_ctx
) ir_constant(1.0f
),
747 src
[2]->clone(p
->mem_ctx
, NULL
));
748 tmp1
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[1], tmp1
);
750 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
, tmp1
);
753 return new(p
->mem_ctx
) ir_expression(ir_binop_sub
, src
[0], src
[1]);
756 case MODE_DOT3_RGBA_EXT
:
757 case MODE_DOT3_RGB_EXT
:
758 case MODE_DOT3_RGB
: {
759 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0],
760 new(p
->mem_ctx
) ir_constant(2.0f
));
761 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
,
762 new(p
->mem_ctx
) ir_constant(-1.0f
));
763 tmp0
= new(p
->mem_ctx
) ir_swizzle(smear(p
, tmp0
), 0, 1, 2, 3, 3);
765 tmp1
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[1],
766 new(p
->mem_ctx
) ir_constant(2.0f
));
767 tmp1
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp1
,
768 new(p
->mem_ctx
) ir_constant(-1.0f
));
769 tmp1
= new(p
->mem_ctx
) ir_swizzle(smear(p
, tmp1
), 0, 1, 2, 3, 3);
771 return new(p
->mem_ctx
) ir_expression(ir_binop_dot
, tmp0
, tmp1
);
773 case MODE_MODULATE_ADD_ATI
:
774 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0], src
[2]);
775 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
, src
[1]);
777 case MODE_MODULATE_SIGNED_ADD_ATI
:
778 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0], src
[2]);
779 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
, src
[1]);
780 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
,
781 new(p
->mem_ctx
) ir_constant(-0.5f
));
783 case MODE_MODULATE_SUBTRACT_ATI
:
784 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0], src
[2]);
785 return new(p
->mem_ctx
) ir_expression(ir_binop_sub
, tmp0
, src
[1]);
787 case MODE_ADD_PRODUCTS
:
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 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
, tmp1
);
792 case MODE_ADD_PRODUCTS_SIGNED
:
793 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[0], src
[1]);
794 tmp1
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, src
[2], src
[3]);
795 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
, tmp1
);
796 return new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
,
797 new(p
->mem_ctx
) ir_constant(-0.5f
));
799 case MODE_BUMP_ENVMAP_ATI
:
800 /* special - not handled here */
810 saturate(struct texenv_fragment_program
*p
, ir_rvalue
*val
)
812 val
= new(p
->mem_ctx
) ir_expression(ir_binop_min
, val
,
813 new(p
->mem_ctx
) ir_constant(1.0f
));
814 return new(p
->mem_ctx
) ir_expression(ir_binop_max
, val
,
815 new(p
->mem_ctx
) ir_constant(0.0f
));
819 * Generate instructions for one texture unit's env/combiner mode.
822 emit_texenv(struct texenv_fragment_program
*p
, GLuint unit
)
824 const struct state_key
*key
= p
->state
;
825 GLboolean rgb_saturate
, alpha_saturate
;
826 GLuint rgb_shift
, alpha_shift
;
828 if (!key
->unit
[unit
].enabled
) {
829 return get_source(p
, SRC_PREVIOUS
, 0);
831 if (key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
832 /* this isn't really a env stage delivering a color and handled elsewhere */
833 return get_source(p
, SRC_PREVIOUS
, 0);
836 switch (key
->unit
[unit
].ModeRGB
) {
837 case MODE_DOT3_RGB_EXT
:
838 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
841 case MODE_DOT3_RGBA_EXT
:
846 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
847 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
851 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
852 * We don't want to clamp twice.
855 rgb_saturate
= GL_FALSE
; /* saturate after rgb shift */
856 else if (need_saturate(key
->unit
[unit
].ModeRGB
))
857 rgb_saturate
= GL_TRUE
;
859 rgb_saturate
= GL_FALSE
;
862 alpha_saturate
= GL_FALSE
; /* saturate after alpha shift */
863 else if (need_saturate(key
->unit
[unit
].ModeA
))
864 alpha_saturate
= GL_TRUE
;
866 alpha_saturate
= GL_FALSE
;
868 ir_variable
*temp_var
= new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
871 p
->instructions
->push_tail(temp_var
);
873 ir_dereference
*deref
;
874 ir_assignment
*assign
;
877 /* Emit the RGB and A combine ops
879 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
880 args_match(key
, unit
)) {
881 val
= emit_combine(p
, unit
,
882 key
->unit
[unit
].NumArgsRGB
,
883 key
->unit
[unit
].ModeRGB
,
884 key
->unit
[unit
].OptRGB
);
887 val
= saturate(p
, val
);
889 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
890 assign
= new(p
->mem_ctx
) ir_assignment(deref
, val
);
891 p
->instructions
->push_tail(assign
);
893 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
894 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
895 ir_rvalue
*val
= emit_combine(p
, unit
,
896 key
->unit
[unit
].NumArgsRGB
,
897 key
->unit
[unit
].ModeRGB
,
898 key
->unit
[unit
].OptRGB
);
901 val
= saturate(p
, val
);
902 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
903 assign
= new(p
->mem_ctx
) ir_assignment(deref
, val
);
904 p
->instructions
->push_tail(assign
);
907 /* Need to do something to stop from re-emitting identical
908 * argument calculations here:
910 val
= emit_combine(p
, unit
,
911 key
->unit
[unit
].NumArgsRGB
,
912 key
->unit
[unit
].ModeRGB
,
913 key
->unit
[unit
].OptRGB
);
915 val
= new(p
->mem_ctx
) ir_swizzle(val
, 0, 1, 2, 3, 3);
917 val
= saturate(p
, val
);
918 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
919 assign
= new(p
->mem_ctx
) ir_assignment(deref
, val
, NULL
, WRITEMASK_XYZ
);
920 p
->instructions
->push_tail(assign
);
922 val
= emit_combine(p
, unit
,
923 key
->unit
[unit
].NumArgsA
,
924 key
->unit
[unit
].ModeA
,
925 key
->unit
[unit
].OptA
);
927 val
= new(p
->mem_ctx
) ir_swizzle(val
, 3, 3, 3, 3, 1);
929 val
= saturate(p
, val
);
930 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
931 assign
= new(p
->mem_ctx
) ir_assignment(deref
, val
, NULL
, WRITEMASK_W
);
932 p
->instructions
->push_tail(assign
);
935 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
937 /* Deal with the final shift:
939 if (alpha_shift
|| rgb_shift
) {
942 if (rgb_shift
== alpha_shift
) {
943 shift
= new(p
->mem_ctx
) ir_constant((float)(1 << rgb_shift
));
946 float const_data
[4] = {
952 shift
= new(p
->mem_ctx
) ir_constant(glsl_type::vec4_type
,
953 (ir_constant_data
*)const_data
);
956 return saturate(p
, new(p
->mem_ctx
) ir_expression(ir_binop_mul
,
965 * Generate instruction for getting a texture source term.
967 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
969 ir_dereference
*deref
;
970 ir_assignment
*assign
;
972 if (p
->src_texture
[unit
])
975 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
978 if (!(p
->state
->inputs_available
& (FRAG_BIT_TEX0
<< unit
))) {
979 texcoord
= get_current_attrib(p
, VERT_ATTRIB_TEX0
+ unit
);
980 } else if (p
->texcoord_tex
[unit
]) {
981 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(p
->texcoord_tex
[unit
]);
983 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
985 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
986 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(unit
);
987 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
988 tc_array
->max_array_access
= MAX2(tc_array
->max_array_access
, unit
);
991 if (!p
->state
->unit
[unit
].enabled
) {
992 p
->src_texture
[unit
] = new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
995 p
->instructions
->push_tail(p
->src_texture
[unit
]);
997 deref
= new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
998 assign
= new(p
->mem_ctx
) ir_assignment(deref
,
999 new(p
->mem_ctx
) ir_constant(0.0f
));
1000 p
->instructions
->push_tail(assign
);
1004 const glsl_type
*sampler_type
= NULL
;
1007 switch (texTarget
) {
1008 case TEXTURE_1D_INDEX
:
1009 if (p
->state
->unit
[unit
].shadow
)
1010 sampler_type
= p
->shader
->symbols
->get_type("sampler1DShadow");
1012 sampler_type
= p
->shader
->symbols
->get_type("sampler1D");
1015 case TEXTURE_1D_ARRAY_INDEX
:
1016 if (p
->state
->unit
[unit
].shadow
)
1017 sampler_type
= p
->shader
->symbols
->get_type("sampler1DArrayShadow");
1019 sampler_type
= p
->shader
->symbols
->get_type("sampler1DArray");
1022 case TEXTURE_2D_INDEX
:
1023 if (p
->state
->unit
[unit
].shadow
)
1024 sampler_type
= p
->shader
->symbols
->get_type("sampler2DShadow");
1026 sampler_type
= p
->shader
->symbols
->get_type("sampler2D");
1029 case TEXTURE_2D_ARRAY_INDEX
:
1030 if (p
->state
->unit
[unit
].shadow
)
1031 sampler_type
= p
->shader
->symbols
->get_type("sampler2DArrayShadow");
1033 sampler_type
= p
->shader
->symbols
->get_type("sampler2DArray");
1036 case TEXTURE_RECT_INDEX
:
1037 if (p
->state
->unit
[unit
].shadow
)
1038 sampler_type
= p
->shader
->symbols
->get_type("sampler2DRectShadow");
1040 sampler_type
= p
->shader
->symbols
->get_type("sampler2DRect");
1043 case TEXTURE_3D_INDEX
:
1044 assert(!p
->state
->unit
[unit
].shadow
);
1045 sampler_type
= p
->shader
->symbols
->get_type("sampler3D");
1048 case TEXTURE_CUBE_INDEX
:
1049 if (p
->state
->unit
[unit
].shadow
)
1050 sampler_type
= p
->shader
->symbols
->get_type("samplerCubeShadow");
1052 sampler_type
= p
->shader
->symbols
->get_type("samplerCube");
1055 case TEXTURE_EXTERNAL_INDEX
:
1056 assert(!p
->state
->unit
[unit
].shadow
);
1057 sampler_type
= p
->shader
->symbols
->get_type("samplerExternalOES");
1062 p
->src_texture
[unit
] = new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
1065 p
->instructions
->push_tail(p
->src_texture
[unit
]);
1067 ir_texture
*tex
= new(p
->mem_ctx
) ir_texture(ir_tex
);
1070 char *sampler_name
= ralloc_asprintf(p
->mem_ctx
, "sampler_%d", unit
);
1071 ir_variable
*sampler
= new(p
->mem_ctx
) ir_variable(sampler_type
,
1074 p
->top_instructions
->push_head(sampler
);
1075 deref
= new(p
->mem_ctx
) ir_dereference_variable(sampler
);
1076 tex
->set_sampler(deref
, glsl_type::vec4_type
);
1078 tex
->coordinate
= new(p
->mem_ctx
) ir_swizzle(texcoord
, 0, 1, 2, 3, coords
);
1080 if (p
->state
->unit
[unit
].shadow
) {
1081 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1082 tex
->shadow_comparitor
= new(p
->mem_ctx
) ir_swizzle(texcoord
,
1088 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1089 tex
->projector
= new(p
->mem_ctx
) ir_swizzle(texcoord
, 3, 0, 0, 0, 1);
1091 deref
= new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
1092 assign
= new(p
->mem_ctx
) ir_assignment(deref
, tex
);
1093 p
->instructions
->push_tail(assign
);
1097 load_texenv_source(struct texenv_fragment_program
*p
,
1098 GLuint src
, GLuint unit
)
1102 load_texture(p
, unit
);
1113 load_texture(p
, src
- SRC_TEXTURE0
);
1117 /* not a texture src - do nothing */
1124 * Generate instructions for loading all texture source terms.
1127 load_texunit_sources( struct texenv_fragment_program
*p
, GLuint unit
)
1129 const struct state_key
*key
= p
->state
;
1132 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1133 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1136 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1137 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1144 * Generate instructions for loading bump map textures.
1147 load_texunit_bumpmap( struct texenv_fragment_program
*p
, GLuint unit
)
1149 const struct state_key
*key
= p
->state
;
1150 GLuint bumpedUnitNr
= key
->unit
[unit
].OptRGB
[1].Source
- SRC_TEXTURE0
;
1152 ir_rvalue
*texcoord
;
1153 ir_variable
*rot_mat_0_var
, *rot_mat_1_var
;
1154 ir_dereference_variable
*rot_mat_0
, *rot_mat_1
;
1156 rot_mat_0_var
= p
->shader
->symbols
->get_variable("gl_BumpRotMatrix0MESA");
1157 rot_mat_1_var
= p
->shader
->symbols
->get_variable("gl_BumpRotMatrix1MESA");
1158 rot_mat_0
= new(p
->mem_ctx
) ir_dereference_variable(rot_mat_0_var
);
1159 rot_mat_1
= new(p
->mem_ctx
) ir_dereference_variable(rot_mat_1_var
);
1161 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
1163 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
1164 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(bumpedUnitNr
);
1165 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
1166 tc_array
->max_array_access
= MAX2(tc_array
->max_array_access
, unit
);
1168 load_texenv_source( p
, unit
+ SRC_TEXTURE0
, unit
);
1170 /* Apply rot matrix and add coords to be available in next phase.
1171 * dest = Arg1 + (Arg0.xx * rotMat0) + (Arg0.yy * rotMat1)
1172 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1174 ir_dereference
*deref
;
1175 ir_assignment
*assign
;
1176 ir_rvalue
*bump_x
, *bump_y
;
1178 texcoord
= smear(p
, texcoord
);
1180 /* bump_texcoord = texcoord */
1181 ir_variable
*bumped
= new(p
->mem_ctx
) ir_variable(texcoord
->type
,
1184 p
->instructions
->push_tail(bumped
);
1186 deref
= new(p
->mem_ctx
) ir_dereference_variable(bumped
);
1187 assign
= new(p
->mem_ctx
) ir_assignment(deref
, texcoord
);
1188 p
->instructions
->push_tail(assign
);
1190 /* bump_texcoord.xy += arg0.x * rotmat0 + arg0.y * rotmat1 */
1191 bump
= get_source(p
, key
->unit
[unit
].OptRGB
[0].Source
, unit
);
1192 bump_x
= new(p
->mem_ctx
) ir_swizzle(bump
, 0, 0, 0, 0, 1);
1193 bump
= bump
->clone(p
->mem_ctx
, NULL
);
1194 bump_y
= new(p
->mem_ctx
) ir_swizzle(bump
, 1, 0, 0, 0, 1);
1196 bump_x
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, bump_x
, rot_mat_0
);
1197 bump_y
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, bump_y
, rot_mat_1
);
1199 ir_expression
*expr
;
1200 expr
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, bump_x
, bump_y
);
1202 deref
= new(p
->mem_ctx
) ir_dereference_variable(bumped
);
1203 expr
= new(p
->mem_ctx
) ir_expression(ir_binop_add
,
1204 new(p
->mem_ctx
) ir_swizzle(deref
,
1209 deref
= new(p
->mem_ctx
) ir_dereference_variable(bumped
);
1210 assign
= new(p
->mem_ctx
) ir_assignment(deref
, expr
, NULL
, WRITEMASK_XY
);
1211 p
->instructions
->push_tail(assign
);
1213 p
->texcoord_tex
[bumpedUnitNr
] = bumped
;
1217 * Applies the fog calculations.
1219 * This is basically like the ARB_fragment_prorgam fog options. Note
1220 * that ffvertex_prog.c produces fogcoord for us when
1221 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1224 emit_fog_instructions(struct texenv_fragment_program
*p
,
1225 ir_rvalue
*fragcolor
)
1227 struct state_key
*key
= p
->state
;
1228 ir_rvalue
*f
, *temp
;
1229 ir_variable
*params
, *oparams
;
1230 ir_variable
*fogcoord
;
1231 ir_assignment
*assign
;
1233 /* Temporary storage for the whole fog result. Fog calculations
1234 * only affect rgb so we're hanging on to the .a value of fragcolor
1237 ir_variable
*fog_result
= new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
1240 p
->instructions
->push_tail(fog_result
);
1241 temp
= new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1242 assign
= new(p
->mem_ctx
) ir_assignment(temp
, fragcolor
);
1243 p
->instructions
->push_tail(assign
);
1245 temp
= new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1246 fragcolor
= new(p
->mem_ctx
) ir_swizzle(temp
, 0, 1, 2, 3, 3);
1248 oparams
= p
->shader
->symbols
->get_variable("gl_FogParamsOptimizedMESA");
1249 fogcoord
= p
->shader
->symbols
->get_variable("gl_FogFragCoord");
1250 params
= p
->shader
->symbols
->get_variable("gl_Fog");
1251 f
= new(p
->mem_ctx
) ir_dereference_variable(fogcoord
);
1253 ir_variable
*f_var
= new(p
->mem_ctx
) ir_variable(glsl_type::float_type
,
1254 "fog_factor", ir_var_auto
);
1255 p
->instructions
->push_tail(f_var
);
1257 switch (key
->fog_mode
) {
1259 /* f = (end - z) / (end - start)
1261 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1262 * (end / (end - start)) so we can generate a single MAD.
1264 temp
= new(p
->mem_ctx
) ir_dereference_variable(oparams
);
1265 temp
= new(p
->mem_ctx
) ir_swizzle(temp
, 0, 0, 0, 0, 1);
1266 f
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, f
, temp
);
1268 temp
= new(p
->mem_ctx
) ir_dereference_variable(oparams
);
1269 temp
= new(p
->mem_ctx
) ir_swizzle(temp
, 1, 0, 0, 0, 1);
1270 f
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, f
, temp
);
1273 /* f = e^(-(density * fogcoord))
1275 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1276 * use EXP2 which is generally the native instruction without
1277 * having to do any further math on the fog density uniform.
1279 temp
= new(p
->mem_ctx
) ir_dereference_variable(oparams
);
1280 temp
= new(p
->mem_ctx
) ir_swizzle(temp
, 2, 0, 0, 0, 1);
1281 f
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, f
, temp
);
1282 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1283 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1286 /* f = e^(-(density * fogcoord)^2)
1288 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1289 * can do this like FOG_EXP but with a squaring after the
1290 * multiply by density.
1292 ir_variable
*temp_var
= new(p
->mem_ctx
) ir_variable(glsl_type::float_type
,
1295 p
->instructions
->push_tail(temp_var
);
1297 temp
= new(p
->mem_ctx
) ir_dereference_variable(oparams
);
1298 temp
= new(p
->mem_ctx
) ir_swizzle(temp
, 3, 0, 0, 0, 1);
1299 f
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
,
1302 temp
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
1303 ir_assignment
*assign
= new(p
->mem_ctx
) ir_assignment(temp
, f
);
1304 p
->instructions
->push_tail(assign
);
1306 f
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
1307 temp
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
1308 f
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, f
, temp
);
1309 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1310 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1316 temp
= new(p
->mem_ctx
) ir_dereference_variable(f_var
);
1317 assign
= new(p
->mem_ctx
) ir_assignment(temp
, f
);
1318 p
->instructions
->push_tail(assign
);
1320 f
= new(p
->mem_ctx
) ir_dereference_variable(f_var
);
1321 f
= new(p
->mem_ctx
) ir_expression(ir_binop_sub
,
1322 new(p
->mem_ctx
) ir_constant(1.0f
),
1324 temp
= new(p
->mem_ctx
) ir_dereference_variable(params
);
1325 temp
= new(p
->mem_ctx
) ir_dereference_record(temp
, "color");
1326 temp
= new(p
->mem_ctx
) ir_swizzle(temp
, 0, 1, 2, 3, 3);
1327 temp
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, temp
, f
);
1329 f
= new(p
->mem_ctx
) ir_dereference_variable(f_var
);
1330 f
= new(p
->mem_ctx
) ir_expression(ir_binop_mul
, fragcolor
, f
);
1331 f
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, temp
, f
);
1333 ir_dereference
*deref
= new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1334 assign
= new(p
->mem_ctx
) ir_assignment(deref
, f
, NULL
, WRITEMASK_XYZ
);
1335 p
->instructions
->push_tail(assign
);
1337 return new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1341 emit_instructions(struct texenv_fragment_program
*p
)
1343 struct state_key
*key
= p
->state
;
1346 if (key
->enabled_units
) {
1347 /* Zeroth pass - bump map textures first */
1348 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1349 if (key
->unit
[unit
].enabled
&&
1350 key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1351 load_texunit_bumpmap(p
, unit
);
1355 /* First pass - to support texture_env_crossbar, first identify
1356 * all referenced texture sources and emit texld instructions
1359 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1360 if (key
->unit
[unit
].enabled
) {
1361 load_texunit_sources(p
, unit
);
1362 p
->last_tex_stage
= unit
;
1365 /* Second pass - emit combine instructions to build final color:
1367 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1368 if (key
->unit
[unit
].enabled
) {
1369 p
->src_previous
= emit_texenv(p
, unit
);
1374 ir_rvalue
*cf
= get_source(p
, SRC_PREVIOUS
, 0);
1375 ir_dereference_variable
*deref
;
1376 ir_assignment
*assign
;
1378 if (key
->separate_specular
) {
1380 ir_variable
*spec_result
= new(p
->mem_ctx
) ir_variable(glsl_type::vec4_type
,
1384 p
->instructions
->push_tail(spec_result
);
1386 deref
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1387 assign
= new(p
->mem_ctx
) ir_assignment(deref
, cf
);
1388 p
->instructions
->push_tail(assign
);
1390 deref
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1391 tmp0
= new(p
->mem_ctx
) ir_swizzle(deref
, 0, 1, 2, 3, 3);
1393 ir_rvalue
*secondary
;
1394 if (p
->state
->inputs_available
& FRAG_BIT_COL1
) {
1396 p
->shader
->symbols
->get_variable("gl_SecondaryColor");
1398 secondary
= new(p
->mem_ctx
) ir_dereference_variable(var
);
1400 secondary
= get_current_attrib(p
, VERT_ATTRIB_COLOR1
);
1402 secondary
= new(p
->mem_ctx
) ir_swizzle(secondary
, 0, 1, 2, 3, 3);
1404 tmp0
= new(p
->mem_ctx
) ir_expression(ir_binop_add
, tmp0
, secondary
);
1406 deref
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1407 assign
= new(p
->mem_ctx
) ir_assignment(deref
, tmp0
, NULL
, WRITEMASK_XYZ
);
1408 p
->instructions
->push_tail(assign
);
1410 cf
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1413 if (key
->fog_enabled
) {
1414 cf
= emit_fog_instructions(p
, cf
);
1417 ir_variable
*frag_color
= p
->shader
->symbols
->get_variable("gl_FragColor");
1419 deref
= new(p
->mem_ctx
) ir_dereference_variable(frag_color
);
1420 assign
= new(p
->mem_ctx
) ir_assignment(deref
, cf
);
1421 p
->instructions
->push_tail(assign
);
1425 * Generate a new fragment program which implements the context's
1426 * current texture env/combine mode.
1428 static struct gl_shader_program
*
1429 create_new_program(struct gl_context
*ctx
, struct state_key
*key
)
1431 struct texenv_fragment_program p
;
1433 _mesa_glsl_parse_state
*state
;
1435 memset(&p
, 0, sizeof(p
));
1436 p
.mem_ctx
= ralloc_context(NULL
);
1437 p
.shader
= ctx
->Driver
.NewShader(ctx
, 0, GL_FRAGMENT_SHADER
);
1438 p
.shader
->ir
= new(p
.shader
) exec_list
;
1439 state
= new(p
.shader
) _mesa_glsl_parse_state(ctx
, GL_FRAGMENT_SHADER
,
1441 p
.shader
->symbols
= state
->symbols
;
1442 p
.top_instructions
= p
.shader
->ir
;
1443 p
.instructions
= p
.shader
->ir
;
1445 p
.shader_program
= ctx
->Driver
.NewShaderProgram(ctx
, 0);
1447 /* Tell the linker to ignore the fact that we're building a
1448 * separate shader, in case we're in a GLES2 context that would
1449 * normally reject that. The real problem is that we're building a
1450 * fixed function program in a GLES2 context at all, but that's a
1451 * big mess to clean up.
1453 p
.shader_program
->InternalSeparateShader
= GL_TRUE
;
1455 state
->language_version
= 130;
1456 if (ctx
->Extensions
.OES_EGL_image_external
)
1457 state
->OES_EGL_image_external_enable
= true;
1458 _mesa_glsl_initialize_types(state
);
1459 _mesa_glsl_initialize_variables(p
.instructions
, state
);
1461 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1462 p
.src_texture
[unit
] = NULL
;
1463 p
.texcoord_tex
[unit
] = NULL
;
1466 p
.src_previous
= NULL
;
1468 p
.last_tex_stage
= 0;
1470 ir_function
*main_f
= new(p
.mem_ctx
) ir_function("main");
1471 p
.instructions
->push_tail(main_f
);
1472 state
->symbols
->add_function(main_f
);
1474 ir_function_signature
*main_sig
=
1475 new(p
.mem_ctx
) ir_function_signature(p
.shader
->symbols
->get_type("void"));
1476 main_sig
->is_defined
= true;
1477 main_f
->add_signature(main_sig
);
1479 p
.instructions
= &main_sig
->body
;
1480 if (key
->num_draw_buffers
)
1481 emit_instructions(&p
);
1483 validate_ir_tree(p
.shader
->ir
);
1485 while (do_common_optimization(p
.shader
->ir
, false, false, 32))
1487 reparent_ir(p
.shader
->ir
, p
.shader
->ir
);
1489 p
.shader
->CompileStatus
= true;
1490 p
.shader
->Version
= state
->language_version
;
1491 p
.shader
->num_builtins_to_link
= state
->num_builtins_to_link
;
1492 p
.shader_program
->Shaders
=
1493 (gl_shader
**)malloc(sizeof(*p
.shader_program
->Shaders
));
1494 p
.shader_program
->Shaders
[0] = p
.shader
;
1495 p
.shader_program
->NumShaders
= 1;
1497 _mesa_glsl_link_shader(ctx
, p
.shader_program
);
1499 /* Set the sampler uniforms, and relink to get them into the linked
1502 struct gl_shader
*const fs
=
1503 p
.shader_program
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
1504 struct gl_program
*const fp
= fs
->Program
;
1506 _mesa_generate_parameters_list_for_uniforms(p
.shader_program
, fs
,
1509 _mesa_associate_uniform_storage(ctx
, p
.shader_program
, fp
->Parameters
);
1511 for (unsigned int i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1512 /* Enough space for 'sampler_999\0'.
1516 snprintf(name
, sizeof(name
), "sampler_%d", i
);
1518 int loc
= _mesa_get_uniform_location(ctx
, p
.shader_program
, name
);
1523 /* Avoid using _mesa_uniform() because it flags state
1524 * updates, so if we're generating this shader_program in a
1525 * state update, we end up recursing. Instead, just set the
1526 * value, which is picked up at re-link.
1528 _mesa_uniform_split_location_offset(loc
, &base
, &idx
);
1531 struct gl_uniform_storage
*const storage
=
1532 &p
.shader_program
->UniformStorage
[base
];
1534 /* Update the storage, the SamplerUnits in the shader program, and
1535 * the SamplerUnits in the assembly shader.
1537 storage
->storage
[idx
].i
= i
;
1538 fp
->SamplerUnits
[storage
->sampler
] = i
;
1539 p
.shader_program
->SamplerUnits
[storage
->sampler
] = i
;
1540 _mesa_propagate_uniforms_to_driver_storage(storage
, 0, 1);
1543 _mesa_update_shader_textures_used(p
.shader_program
, fp
);
1544 (void) ctx
->Driver
.ProgramStringNotify(ctx
, fp
->Target
, fp
);
1546 if (!p
.shader_program
->LinkStatus
)
1547 _mesa_problem(ctx
, "Failed to link fixed function fragment shader: %s\n",
1548 p
.shader_program
->InfoLog
);
1550 ralloc_free(p
.mem_ctx
);
1551 return p
.shader_program
;
1557 * Return a fragment program which implements the current
1558 * fixed-function texture, fog and color-sum operations.
1560 struct gl_shader_program
*
1561 _mesa_get_fixed_func_fragment_program(struct gl_context
*ctx
)
1563 struct gl_shader_program
*shader_program
;
1564 struct state_key key
;
1567 keySize
= make_state_key(ctx
, &key
);
1569 shader_program
= (struct gl_shader_program
*)
1570 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1573 if (!shader_program
) {
1574 shader_program
= create_new_program(ctx
, &key
);
1576 _mesa_shader_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1577 &key
, keySize
, shader_program
);
1580 return shader_program
;