1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 **************************************************************************/
34 #include "main/context.h"
35 #include "main/macros.h"
36 #include "main/samplerobj.h"
37 #include "program/program.h"
38 #include "program/prog_parameter.h"
39 #include "program/prog_cache.h"
40 #include "program/prog_instruction.h"
41 #include "program/prog_print.h"
42 #include "program/prog_statevars.h"
43 #include "program/programopt.h"
44 #include "texenvprogram.h"
47 #include "main/uniforms.h"
48 #include "../glsl/glsl_types.h"
49 #include "../glsl/ir.h"
50 #include "../glsl/ir_builder.h"
51 #include "../glsl/glsl_symbol_table.h"
52 #include "../glsl/glsl_parser_extras.h"
53 #include "../glsl/ir_optimization.h"
54 #include "../program/ir_to_mesa.h"
56 using namespace ir_builder
;
59 * Note on texture units:
61 * The number of texture units supported by fixed-function fragment
62 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
63 * That's because there's a one-to-one correspondence between texture
64 * coordinates and samplers in fixed-function processing.
66 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
67 * sets of texcoords, so is fixed-function fragment processing.
69 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
73 struct texenvprog_cache_item
77 struct gl_shader_program
*data
;
78 struct texenvprog_cache_item
*next
;
82 texenv_doing_secondary_color(struct gl_context
*ctx
)
84 if (ctx
->Light
.Enabled
&&
85 (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
))
88 if (ctx
->Fog
.ColorSumEnabled
)
96 __extension__ GLubyte Source
:4; /**< SRC_x */
97 __extension__ GLubyte Operand
:3; /**< OPR_x */
99 GLubyte Source
; /**< SRC_x */
100 GLubyte Operand
; /**< OPR_x */
105 GLuint nr_enabled_units
:8;
106 GLuint enabled_units
:8;
107 GLuint separate_specular
:1;
108 GLuint fog_enabled
:1;
109 GLuint fog_mode
:2; /**< FOG_x */
110 GLuint inputs_available
:12;
111 GLuint num_draw_buffers
:4;
113 /* NOTE: This array of structs must be last! (see "keySize" below) */
116 GLuint source_index
:4; /**< TEXTURE_x_INDEX */
118 GLuint ScaleShiftRGB
:2;
119 GLuint ScaleShiftA
:2;
121 GLuint NumArgsRGB
:3; /**< up to MAX_COMBINER_TERMS */
122 GLuint ModeRGB
:5; /**< MODE_x */
124 GLuint NumArgsA
:3; /**< up to MAX_COMBINER_TERMS */
125 GLuint ModeA
:5; /**< MODE_x */
127 struct mode_opt OptRGB
[MAX_COMBINER_TERMS
];
128 struct mode_opt OptA
[MAX_COMBINER_TERMS
];
129 } unit
[MAX_TEXTURE_UNITS
];
135 #define FOG_UNKNOWN 3
137 static GLuint
translate_fog_mode( GLenum mode
)
140 case GL_LINEAR
: return FOG_LINEAR
;
141 case GL_EXP
: return FOG_EXP
;
142 case GL_EXP2
: return FOG_EXP2
;
143 default: return FOG_UNKNOWN
;
147 #define OPR_SRC_COLOR 0
148 #define OPR_ONE_MINUS_SRC_COLOR 1
149 #define OPR_SRC_ALPHA 2
150 #define OPR_ONE_MINUS_SRC_ALPHA 3
153 #define OPR_UNKNOWN 7
155 static GLuint
translate_operand( GLenum operand
)
158 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
159 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
160 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
161 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
162 case GL_ZERO
: return OPR_ZERO
;
163 case GL_ONE
: return OPR_ONE
;
170 #define SRC_TEXTURE 0
171 #define SRC_TEXTURE0 1
172 #define SRC_TEXTURE1 2
173 #define SRC_TEXTURE2 3
174 #define SRC_TEXTURE3 4
175 #define SRC_TEXTURE4 5
176 #define SRC_TEXTURE5 6
177 #define SRC_TEXTURE6 7
178 #define SRC_TEXTURE7 8
179 #define SRC_CONSTANT 9
180 #define SRC_PRIMARY_COLOR 10
181 #define SRC_PREVIOUS 11
183 #define SRC_UNKNOWN 15
185 static GLuint
translate_source( GLenum src
)
188 case GL_TEXTURE
: return SRC_TEXTURE
;
196 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
197 case GL_CONSTANT
: return SRC_CONSTANT
;
198 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
199 case GL_PREVIOUS
: return SRC_PREVIOUS
;
208 #define MODE_REPLACE 0 /* r = a0 */
209 #define MODE_MODULATE 1 /* r = a0 * a1 */
210 #define MODE_ADD 2 /* r = a0 + a1 */
211 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
212 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
213 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
214 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
215 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
216 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
217 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
218 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
219 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
220 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
221 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
222 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
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
;
260 * Do we need to clamp the results of the given texture env/combine mode?
261 * If the inputs to the mode are in [0,1] we don't always have to clamp
265 need_saturate( GLuint mode
)
270 case MODE_INTERPOLATE
:
273 case MODE_ADD_SIGNED
:
276 case MODE_DOT3_RGB_EXT
:
278 case MODE_DOT3_RGBA_EXT
:
279 case MODE_MODULATE_ADD_ATI
:
280 case MODE_MODULATE_SIGNED_ADD_ATI
:
281 case MODE_MODULATE_SUBTRACT_ATI
:
282 case MODE_ADD_PRODUCTS
:
283 case MODE_ADD_PRODUCTS_SIGNED
:
291 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
294 * Identify all possible varying inputs. The fragment program will
295 * never reference non-varying inputs, but will track them via state
298 * This function figures out all the inputs that the fragment program
299 * has access to. The bitmask is later reduced to just those which
300 * are actually referenced.
302 static GLbitfield
get_fp_input_mask( struct gl_context
*ctx
)
305 const GLboolean vertexShader
=
306 (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] &&
307 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->LinkStatus
&&
308 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->_LinkedShaders
[MESA_SHADER_VERTEX
]);
309 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
310 GLbitfield fp_inputs
= 0x0;
312 if (ctx
->VertexProgram
._Overriden
) {
313 /* Somebody's messing with the vertex program and we don't have
314 * a clue what's happening. Assume that it could be producing
315 * all possible outputs.
319 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
320 /* _NEW_RENDERMODE */
321 fp_inputs
= (VARYING_BIT_COL0
| VARYING_BIT_TEX0
);
323 else if (!(vertexProgram
|| vertexShader
)) {
324 /* Fixed function vertex logic */
325 /* _NEW_VARYING_VP_INPUTS */
326 GLbitfield64 varying_inputs
= ctx
->varying_vp_inputs
;
328 /* These get generated in the setup routine regardless of the
332 if (ctx
->Point
.PointSprite
)
333 varying_inputs
|= VARYING_BITS_TEX_ANY
;
335 /* First look at what values may be computed by the generated
339 if (ctx
->Light
.Enabled
) {
340 fp_inputs
|= VARYING_BIT_COL0
;
342 if (texenv_doing_secondary_color(ctx
))
343 fp_inputs
|= VARYING_BIT_COL1
;
347 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
348 ctx
->Texture
._TexMatEnabled
) << VARYING_SLOT_TEX0
;
350 /* Then look at what might be varying as a result of enabled
353 if (varying_inputs
& VERT_BIT_COLOR0
)
354 fp_inputs
|= VARYING_BIT_COL0
;
355 if (varying_inputs
& VERT_BIT_COLOR1
)
356 fp_inputs
|= VARYING_BIT_COL1
;
358 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
359 << VARYING_SLOT_TEX0
);
363 /* calculate from vp->outputs */
364 struct gl_program
*vprog
;
365 GLbitfield64 vp_outputs
;
367 /* Choose GLSL vertex shader over ARB vertex program. Need this
368 * since vertex shader state validation comes after fragment state
369 * validation (see additional comments in state.c).
372 vprog
= ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
374 vprog
= &ctx
->VertexProgram
.Current
->Base
;
376 vp_outputs
= vprog
->OutputsWritten
;
378 /* These get generated in the setup routine regardless of the
382 if (ctx
->Point
.PointSprite
)
383 vp_outputs
|= VARYING_BITS_TEX_ANY
;
385 if (vp_outputs
& (1 << VARYING_SLOT_COL0
))
386 fp_inputs
|= VARYING_BIT_COL0
;
387 if (vp_outputs
& (1 << VARYING_SLOT_COL1
))
388 fp_inputs
|= VARYING_BIT_COL1
;
390 fp_inputs
|= (((vp_outputs
& VARYING_BITS_TEX_ANY
) >> VARYING_SLOT_TEX0
)
391 << VARYING_SLOT_TEX0
);
399 * Examine current texture environment state and generate a unique
400 * key to identify it.
402 static GLuint
make_state_key( struct gl_context
*ctx
, struct state_key
*key
)
405 GLbitfield inputs_referenced
= VARYING_BIT_COL0
;
406 const GLbitfield inputs_available
= get_fp_input_mask( ctx
);
409 memset(key
, 0, sizeof(*key
));
412 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
413 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
414 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
415 const struct gl_tex_env_combine_state
*comb
= texUnit
->_CurrentCombine
;
416 const struct gl_sampler_object
*samp
;
419 if (!texUnit
->_Current
|| !texUnit
->Enabled
)
422 samp
= _mesa_get_samplerobj(ctx
, i
);
423 format
= texObj
->Image
[0][texObj
->BaseLevel
]->_BaseFormat
;
425 key
->unit
[i
].enabled
= 1;
426 key
->enabled_units
|= (1<<i
);
427 key
->nr_enabled_units
= i
+ 1;
428 inputs_referenced
|= VARYING_BIT_TEX(i
);
430 key
->unit
[i
].source_index
= _mesa_tex_target_to_index(ctx
,
433 key
->unit
[i
].shadow
=
434 ((samp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
435 ((format
== GL_DEPTH_COMPONENT
) ||
436 (format
== GL_DEPTH_STENCIL_EXT
)));
438 key
->unit
[i
].NumArgsRGB
= comb
->_NumArgsRGB
;
439 key
->unit
[i
].NumArgsA
= comb
->_NumArgsA
;
441 key
->unit
[i
].ModeRGB
=
442 translate_mode(texUnit
->EnvMode
, comb
->ModeRGB
);
444 translate_mode(texUnit
->EnvMode
, comb
->ModeA
);
446 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
447 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
449 for (j
= 0; j
< MAX_COMBINER_TERMS
; j
++) {
450 key
->unit
[i
].OptRGB
[j
].Operand
= translate_operand(comb
->OperandRGB
[j
]);
451 key
->unit
[i
].OptA
[j
].Operand
= translate_operand(comb
->OperandA
[j
]);
452 key
->unit
[i
].OptRGB
[j
].Source
= translate_source(comb
->SourceRGB
[j
]);
453 key
->unit
[i
].OptA
[j
].Source
= translate_source(comb
->SourceA
[j
]);
457 /* _NEW_LIGHT | _NEW_FOG */
458 if (texenv_doing_secondary_color(ctx
)) {
459 key
->separate_specular
= 1;
460 inputs_referenced
|= VARYING_BIT_COL1
;
464 if (ctx
->Fog
.Enabled
) {
465 key
->fog_enabled
= 1;
466 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
467 inputs_referenced
|= VARYING_BIT_FOGC
; /* maybe */
471 key
->num_draw_buffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
474 if (ctx
->Color
.AlphaEnabled
&& key
->num_draw_buffers
== 0) {
475 /* if alpha test is enabled we need to emit at least one color */
476 key
->num_draw_buffers
= 1;
479 key
->inputs_available
= (inputs_available
& inputs_referenced
);
481 /* compute size of state key, ignoring unused texture units */
482 keySize
= sizeof(*key
) - sizeof(key
->unit
)
483 + key
->nr_enabled_units
* sizeof(key
->unit
[0]);
489 /** State used to build the fragment program:
491 class texenv_fragment_program
: public ir_factory
{
493 struct gl_shader_program
*shader_program
;
494 struct gl_shader
*shader
;
495 exec_list
*top_instructions
;
496 struct state_key
*state
;
498 ir_variable
*src_texture
[MAX_TEXTURE_COORD_UNITS
];
499 /* Reg containing each texture unit's sampled texture color,
503 /* Texcoord override from bumpmapping. */
504 ir_variable
*texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
506 /* Reg containing texcoord for a texture unit,
507 * needed for bump mapping, else undef.
510 ir_rvalue
*src_previous
; /**< Reg containing color from previous
511 * stage. May need to be decl'd.
516 get_current_attrib(texenv_fragment_program
*p
, GLuint attrib
)
518 ir_variable
*current
;
521 current
= p
->shader
->symbols
->get_variable("gl_CurrentAttribFragMESA");
523 current
->data
.max_array_access
= MAX2(current
->data
.max_array_access
, attrib
);
524 val
= new(p
->mem_ctx
) ir_dereference_variable(current
);
525 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(attrib
);
526 return new(p
->mem_ctx
) ir_dereference_array(val
, index
);
530 get_gl_Color(texenv_fragment_program
*p
)
532 if (p
->state
->inputs_available
& VARYING_BIT_COL0
) {
533 ir_variable
*var
= p
->shader
->symbols
->get_variable("gl_Color");
535 return new(p
->mem_ctx
) ir_dereference_variable(var
);
537 return get_current_attrib(p
, VERT_ATTRIB_COLOR0
);
542 get_source(texenv_fragment_program
*p
,
543 GLuint src
, GLuint unit
)
546 ir_dereference
*deref
;
550 return new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
560 return new(p
->mem_ctx
)
561 ir_dereference_variable(p
->src_texture
[src
- SRC_TEXTURE0
]);
564 var
= p
->shader
->symbols
->get_variable("gl_TextureEnvColor");
566 deref
= new(p
->mem_ctx
) ir_dereference_variable(var
);
567 var
->data
.max_array_access
= MAX2(var
->data
.max_array_access
, unit
);
568 return new(p
->mem_ctx
) ir_dereference_array(deref
,
569 new(p
->mem_ctx
) ir_constant(unit
));
571 case SRC_PRIMARY_COLOR
:
572 var
= p
->shader
->symbols
->get_variable("gl_Color");
574 return new(p
->mem_ctx
) ir_dereference_variable(var
);
577 return new(p
->mem_ctx
) ir_constant(0.0f
);
580 if (!p
->src_previous
) {
581 return get_gl_Color(p
);
583 return p
->src_previous
->clone(p
->mem_ctx
, NULL
);
593 emit_combine_source(texenv_fragment_program
*p
,
600 src
= get_source(p
, source
, unit
);
603 case OPR_ONE_MINUS_SRC_COLOR
:
604 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), src
);
607 return src
->type
->is_scalar() ? src
: swizzle_w(src
);
609 case OPR_ONE_MINUS_SRC_ALPHA
: {
610 ir_rvalue
*const scalar
= src
->type
->is_scalar() ? src
: swizzle_w(src
);
612 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), scalar
);
616 return new(p
->mem_ctx
) ir_constant(0.0f
);
618 return new(p
->mem_ctx
) ir_constant(1.0f
);
628 * Check if the RGB and Alpha sources and operands match for the given
629 * texture unit's combinder state. When the RGB and A sources and
630 * operands match, we can emit fewer instructions.
632 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
634 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
636 for (i
= 0; i
< numArgs
; i
++) {
637 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
640 switch (key
->unit
[unit
].OptA
[i
].Operand
) {
642 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
650 case OPR_ONE_MINUS_SRC_ALPHA
:
651 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
652 case OPR_ONE_MINUS_SRC_COLOR
:
653 case OPR_ONE_MINUS_SRC_ALPHA
:
660 return GL_FALSE
; /* impossible */
668 smear(texenv_fragment_program
*p
, ir_rvalue
*val
)
670 if (!val
->type
->is_scalar())
673 return swizzle_xxxx(val
);
677 emit_combine(texenv_fragment_program
*p
,
681 const struct mode_opt
*opt
)
683 ir_rvalue
*src
[MAX_COMBINER_TERMS
];
684 ir_rvalue
*tmp0
, *tmp1
;
687 assert(nr
<= MAX_COMBINER_TERMS
);
689 for (i
= 0; i
< nr
; i
++)
690 src
[i
] = emit_combine_source( p
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
697 return mul(src
[0], src
[1]);
700 return add(src
[0], src
[1]);
702 case MODE_ADD_SIGNED
:
703 return add(add(src
[0], src
[1]), new(p
->mem_ctx
) ir_constant(-0.5f
));
705 case MODE_INTERPOLATE
:
706 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
707 tmp0
= mul(src
[0], src
[2]);
708 tmp1
= mul(src
[1], sub(new(p
->mem_ctx
) ir_constant(1.0f
),
709 src
[2]->clone(p
->mem_ctx
, NULL
)));
710 return add(tmp0
, tmp1
);
713 return sub(src
[0], src
[1]);
716 case MODE_DOT3_RGBA_EXT
:
717 case MODE_DOT3_RGB_EXT
:
718 case MODE_DOT3_RGB
: {
719 tmp0
= mul(src
[0], new(p
->mem_ctx
) ir_constant(2.0f
));
720 tmp0
= add(tmp0
, new(p
->mem_ctx
) ir_constant(-1.0f
));
722 tmp1
= mul(src
[1], new(p
->mem_ctx
) ir_constant(2.0f
));
723 tmp1
= add(tmp1
, new(p
->mem_ctx
) ir_constant(-1.0f
));
725 return dot(swizzle_xyz(smear(p
, tmp0
)), swizzle_xyz(smear(p
, tmp1
)));
727 case MODE_MODULATE_ADD_ATI
:
728 return add(mul(src
[0], src
[2]), src
[1]);
730 case MODE_MODULATE_SIGNED_ADD_ATI
:
731 return add(add(mul(src
[0], src
[2]), src
[1]),
732 new(p
->mem_ctx
) ir_constant(-0.5f
));
734 case MODE_MODULATE_SUBTRACT_ATI
:
735 return sub(mul(src
[0], src
[2]), src
[1]);
737 case MODE_ADD_PRODUCTS
:
738 return add(mul(src
[0], src
[1]), mul(src
[2], src
[3]));
740 case MODE_ADD_PRODUCTS_SIGNED
:
741 return add(add(mul(src
[0], src
[1]), mul(src
[2], src
[3])),
742 new(p
->mem_ctx
) ir_constant(-0.5f
));
750 * Generate instructions for one texture unit's env/combiner mode.
753 emit_texenv(texenv_fragment_program
*p
, GLuint unit
)
755 const struct state_key
*key
= p
->state
;
756 GLboolean rgb_saturate
, alpha_saturate
;
757 GLuint rgb_shift
, alpha_shift
;
759 if (!key
->unit
[unit
].enabled
) {
760 return get_source(p
, SRC_PREVIOUS
, 0);
763 switch (key
->unit
[unit
].ModeRGB
) {
764 case MODE_DOT3_RGB_EXT
:
765 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
768 case MODE_DOT3_RGBA_EXT
:
773 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
774 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
778 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
779 * We don't want to clamp twice.
782 rgb_saturate
= GL_FALSE
; /* saturate after rgb shift */
783 else if (need_saturate(key
->unit
[unit
].ModeRGB
))
784 rgb_saturate
= GL_TRUE
;
786 rgb_saturate
= GL_FALSE
;
789 alpha_saturate
= GL_FALSE
; /* saturate after alpha shift */
790 else if (need_saturate(key
->unit
[unit
].ModeA
))
791 alpha_saturate
= GL_TRUE
;
793 alpha_saturate
= GL_FALSE
;
795 ir_variable
*temp_var
= p
->make_temp(glsl_type::vec4_type
, "texenv_combine");
796 ir_dereference
*deref
;
799 /* Emit the RGB and A combine ops
801 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
802 args_match(key
, unit
)) {
803 val
= emit_combine(p
, unit
,
804 key
->unit
[unit
].NumArgsRGB
,
805 key
->unit
[unit
].ModeRGB
,
806 key
->unit
[unit
].OptRGB
);
811 p
->emit(assign(temp_var
, val
));
813 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
814 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
815 ir_rvalue
*val
= emit_combine(p
, unit
,
816 key
->unit
[unit
].NumArgsRGB
,
817 key
->unit
[unit
].ModeRGB
,
818 key
->unit
[unit
].OptRGB
);
822 p
->emit(assign(temp_var
, val
));
825 /* Need to do something to stop from re-emitting identical
826 * argument calculations here:
828 val
= emit_combine(p
, unit
,
829 key
->unit
[unit
].NumArgsRGB
,
830 key
->unit
[unit
].ModeRGB
,
831 key
->unit
[unit
].OptRGB
);
832 val
= swizzle_xyz(smear(p
, val
));
835 p
->emit(assign(temp_var
, val
, WRITEMASK_XYZ
));
837 val
= emit_combine(p
, unit
,
838 key
->unit
[unit
].NumArgsA
,
839 key
->unit
[unit
].ModeA
,
840 key
->unit
[unit
].OptA
);
841 val
= swizzle_w(smear(p
, val
));
844 p
->emit(assign(temp_var
, val
, WRITEMASK_W
));
847 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
849 /* Deal with the final shift:
851 if (alpha_shift
|| rgb_shift
) {
854 if (rgb_shift
== alpha_shift
) {
855 shift
= new(p
->mem_ctx
) ir_constant((float)(1 << rgb_shift
));
858 ir_constant_data const_data
;
860 const_data
.f
[0] = float(1 << rgb_shift
);
861 const_data
.f
[1] = float(1 << rgb_shift
);
862 const_data
.f
[2] = float(1 << rgb_shift
);
863 const_data
.f
[3] = float(1 << alpha_shift
);
865 shift
= new(p
->mem_ctx
) ir_constant(glsl_type::vec4_type
,
869 return saturate(mul(deref
, shift
));
877 * Generate instruction for getting a texture source term.
879 static void load_texture( texenv_fragment_program
*p
, GLuint unit
)
881 ir_dereference
*deref
;
883 if (p
->src_texture
[unit
])
886 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
889 if (!(p
->state
->inputs_available
& (VARYING_BIT_TEX0
<< unit
))) {
890 texcoord
= get_current_attrib(p
, VERT_ATTRIB_TEX0
+ unit
);
891 } else if (p
->texcoord_tex
[unit
]) {
892 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(p
->texcoord_tex
[unit
]);
894 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
896 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
897 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(unit
);
898 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
899 tc_array
->data
.max_array_access
= MAX2(tc_array
->data
.max_array_access
, unit
);
902 if (!p
->state
->unit
[unit
].enabled
) {
903 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
905 p
->emit(p
->src_texture
[unit
]);
907 p
->emit(assign(p
->src_texture
[unit
], new(p
->mem_ctx
) ir_constant(0.0f
)));
911 const glsl_type
*sampler_type
= NULL
;
915 case TEXTURE_1D_INDEX
:
916 if (p
->state
->unit
[unit
].shadow
)
917 sampler_type
= glsl_type::sampler1DShadow_type
;
919 sampler_type
= glsl_type::sampler1D_type
;
922 case TEXTURE_1D_ARRAY_INDEX
:
923 if (p
->state
->unit
[unit
].shadow
)
924 sampler_type
= glsl_type::sampler1DArrayShadow_type
;
926 sampler_type
= glsl_type::sampler1DArray_type
;
929 case TEXTURE_2D_INDEX
:
930 if (p
->state
->unit
[unit
].shadow
)
931 sampler_type
= glsl_type::sampler2DShadow_type
;
933 sampler_type
= glsl_type::sampler2D_type
;
936 case TEXTURE_2D_ARRAY_INDEX
:
937 if (p
->state
->unit
[unit
].shadow
)
938 sampler_type
= glsl_type::sampler2DArrayShadow_type
;
940 sampler_type
= glsl_type::sampler2DArray_type
;
943 case TEXTURE_RECT_INDEX
:
944 if (p
->state
->unit
[unit
].shadow
)
945 sampler_type
= glsl_type::sampler2DRectShadow_type
;
947 sampler_type
= glsl_type::sampler2DRect_type
;
950 case TEXTURE_3D_INDEX
:
951 assert(!p
->state
->unit
[unit
].shadow
);
952 sampler_type
= glsl_type::sampler3D_type
;
955 case TEXTURE_CUBE_INDEX
:
956 if (p
->state
->unit
[unit
].shadow
)
957 sampler_type
= glsl_type::samplerCubeShadow_type
;
959 sampler_type
= glsl_type::samplerCube_type
;
962 case TEXTURE_EXTERNAL_INDEX
:
963 assert(!p
->state
->unit
[unit
].shadow
);
964 sampler_type
= glsl_type::samplerExternalOES_type
;
969 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
972 ir_texture
*tex
= new(p
->mem_ctx
) ir_texture(ir_tex
);
975 char *sampler_name
= ralloc_asprintf(p
->mem_ctx
, "sampler_%d", unit
);
976 ir_variable
*sampler
= new(p
->mem_ctx
) ir_variable(sampler_type
,
979 p
->top_instructions
->push_head(sampler
);
981 /* Set the texture unit for this sampler. The linker will pick this value
982 * up and do-the-right-thing.
984 * NOTE: The cast to int is important. Without it, the constant will have
985 * type uint, and things later on may get confused.
987 sampler
->constant_value
= new(p
->mem_ctx
) ir_constant(int(unit
));
989 deref
= new(p
->mem_ctx
) ir_dereference_variable(sampler
);
990 tex
->set_sampler(deref
, glsl_type::vec4_type
);
992 tex
->coordinate
= new(p
->mem_ctx
) ir_swizzle(texcoord
, 0, 1, 2, 3, coords
);
994 if (p
->state
->unit
[unit
].shadow
) {
995 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
996 tex
->shadow_comparitor
= new(p
->mem_ctx
) ir_swizzle(texcoord
,
1002 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1003 tex
->projector
= swizzle_w(texcoord
);
1005 p
->emit(assign(p
->src_texture
[unit
], tex
));
1009 load_texenv_source(texenv_fragment_program
*p
,
1010 GLuint src
, GLuint unit
)
1014 load_texture(p
, unit
);
1025 load_texture(p
, src
- SRC_TEXTURE0
);
1029 /* not a texture src - do nothing */
1036 * Generate instructions for loading all texture source terms.
1039 load_texunit_sources( texenv_fragment_program
*p
, GLuint unit
)
1041 const struct state_key
*key
= p
->state
;
1044 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1045 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1048 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1049 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1056 * Applies the fog calculations.
1058 * This is basically like the ARB_fragment_prorgam fog options. Note
1059 * that ffvertex_prog.c produces fogcoord for us when
1060 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1063 emit_fog_instructions(texenv_fragment_program
*p
,
1064 ir_rvalue
*fragcolor
)
1066 struct state_key
*key
= p
->state
;
1067 ir_rvalue
*f
, *temp
;
1068 ir_variable
*params
, *oparams
;
1069 ir_variable
*fogcoord
;
1071 /* Temporary storage for the whole fog result. Fog calculations
1072 * only affect rgb so we're hanging on to the .a value of fragcolor
1075 ir_variable
*fog_result
= p
->make_temp(glsl_type::vec4_type
, "fog_result");
1076 p
->emit(assign(fog_result
, fragcolor
));
1078 fragcolor
= swizzle_xyz(fog_result
);
1080 oparams
= p
->shader
->symbols
->get_variable("gl_FogParamsOptimizedMESA");
1082 fogcoord
= p
->shader
->symbols
->get_variable("gl_FogFragCoord");
1084 params
= p
->shader
->symbols
->get_variable("gl_Fog");
1086 f
= new(p
->mem_ctx
) ir_dereference_variable(fogcoord
);
1088 ir_variable
*f_var
= p
->make_temp(glsl_type::float_type
, "fog_factor");
1090 switch (key
->fog_mode
) {
1092 /* f = (end - z) / (end - start)
1094 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1095 * (end / (end - start)) so we can generate a single MAD.
1097 f
= add(mul(f
, swizzle_x(oparams
)), swizzle_y(oparams
));
1100 /* f = e^(-(density * fogcoord))
1102 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1103 * use EXP2 which is generally the native instruction without
1104 * having to do any further math on the fog density uniform.
1106 f
= mul(f
, swizzle_z(oparams
));
1107 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1108 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1111 /* f = e^(-(density * fogcoord)^2)
1113 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1114 * can do this like FOG_EXP but with a squaring after the
1115 * multiply by density.
1117 ir_variable
*temp_var
= p
->make_temp(glsl_type::float_type
, "fog_temp");
1118 p
->emit(assign(temp_var
, mul(f
, swizzle_w(oparams
))));
1120 f
= mul(temp_var
, temp_var
);
1121 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1122 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1126 p
->emit(assign(f_var
, saturate(f
)));
1128 f
= sub(new(p
->mem_ctx
) ir_constant(1.0f
), f_var
);
1129 temp
= new(p
->mem_ctx
) ir_dereference_variable(params
);
1130 temp
= new(p
->mem_ctx
) ir_dereference_record(temp
, "color");
1131 temp
= mul(swizzle_xyz(temp
), f
);
1133 p
->emit(assign(fog_result
, add(temp
, mul(fragcolor
, f_var
)), WRITEMASK_XYZ
));
1135 return new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1139 emit_instructions(texenv_fragment_program
*p
)
1141 struct state_key
*key
= p
->state
;
1144 if (key
->enabled_units
) {
1145 /* First pass - to support texture_env_crossbar, first identify
1146 * all referenced texture sources and emit texld instructions
1149 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1150 if (key
->unit
[unit
].enabled
) {
1151 load_texunit_sources(p
, unit
);
1154 /* Second pass - emit combine instructions to build final color:
1156 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1157 if (key
->unit
[unit
].enabled
) {
1158 p
->src_previous
= emit_texenv(p
, unit
);
1163 ir_rvalue
*cf
= get_source(p
, SRC_PREVIOUS
, 0);
1165 if (key
->separate_specular
) {
1166 ir_variable
*spec_result
= p
->make_temp(glsl_type::vec4_type
,
1168 p
->emit(assign(spec_result
, cf
));
1170 ir_rvalue
*secondary
;
1171 if (p
->state
->inputs_available
& VARYING_BIT_COL1
) {
1173 p
->shader
->symbols
->get_variable("gl_SecondaryColor");
1175 secondary
= swizzle_xyz(var
);
1177 secondary
= swizzle_xyz(get_current_attrib(p
, VERT_ATTRIB_COLOR1
));
1180 p
->emit(assign(spec_result
, add(swizzle_xyz(spec_result
), secondary
),
1183 cf
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1186 if (key
->fog_enabled
) {
1187 cf
= emit_fog_instructions(p
, cf
);
1190 ir_variable
*frag_color
= p
->shader
->symbols
->get_variable("gl_FragColor");
1192 p
->emit(assign(frag_color
, cf
));
1196 * Generate a new fragment program which implements the context's
1197 * current texture env/combine mode.
1199 static struct gl_shader_program
*
1200 create_new_program(struct gl_context
*ctx
, struct state_key
*key
)
1202 texenv_fragment_program p
;
1204 _mesa_glsl_parse_state
*state
;
1206 p
.mem_ctx
= ralloc_context(NULL
);
1207 p
.shader
= ctx
->Driver
.NewShader(ctx
, 0, GL_FRAGMENT_SHADER
);
1208 p
.shader
->ir
= new(p
.shader
) exec_list
;
1209 state
= new(p
.shader
) _mesa_glsl_parse_state(ctx
, MESA_SHADER_FRAGMENT
,
1211 p
.shader
->symbols
= state
->symbols
;
1212 p
.top_instructions
= p
.shader
->ir
;
1213 p
.instructions
= p
.shader
->ir
;
1215 p
.shader_program
= ctx
->Driver
.NewShaderProgram(ctx
, 0);
1217 /* Tell the linker to ignore the fact that we're building a
1218 * separate shader, in case we're in a GLES2 context that would
1219 * normally reject that. The real problem is that we're building a
1220 * fixed function program in a GLES2 context at all, but that's a
1221 * big mess to clean up.
1223 p
.shader_program
->SeparateShader
= GL_TRUE
;
1225 state
->language_version
= 130;
1226 state
->es_shader
= false;
1227 if (_mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
)
1228 state
->OES_EGL_image_external_enable
= true;
1229 _mesa_glsl_initialize_types(state
);
1230 _mesa_glsl_initialize_variables(p
.instructions
, state
);
1232 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1233 p
.src_texture
[unit
] = NULL
;
1234 p
.texcoord_tex
[unit
] = NULL
;
1237 p
.src_previous
= NULL
;
1239 ir_function
*main_f
= new(p
.mem_ctx
) ir_function("main");
1241 state
->symbols
->add_function(main_f
);
1243 ir_function_signature
*main_sig
=
1244 new(p
.mem_ctx
) ir_function_signature(glsl_type::void_type
);
1245 main_sig
->is_defined
= true;
1246 main_f
->add_signature(main_sig
);
1248 p
.instructions
= &main_sig
->body
;
1249 if (key
->num_draw_buffers
)
1250 emit_instructions(&p
);
1252 validate_ir_tree(p
.shader
->ir
);
1254 const struct gl_shader_compiler_options
*options
=
1255 &ctx
->ShaderCompilerOptions
[MESA_SHADER_FRAGMENT
];
1257 while (do_common_optimization(p
.shader
->ir
, false, false, options
,
1258 ctx
->Const
.NativeIntegers
))
1260 reparent_ir(p
.shader
->ir
, p
.shader
->ir
);
1262 p
.shader
->CompileStatus
= true;
1263 p
.shader
->Version
= state
->language_version
;
1264 p
.shader
->uses_builtin_functions
= state
->uses_builtin_functions
;
1265 p
.shader_program
->Shaders
=
1266 (gl_shader
**)malloc(sizeof(*p
.shader_program
->Shaders
));
1267 p
.shader_program
->Shaders
[0] = p
.shader
;
1268 p
.shader_program
->NumShaders
= 1;
1270 _mesa_glsl_link_shader(ctx
, p
.shader_program
);
1272 if (!p
.shader_program
->LinkStatus
)
1273 _mesa_problem(ctx
, "Failed to link fixed function fragment shader: %s\n",
1274 p
.shader_program
->InfoLog
);
1276 ralloc_free(p
.mem_ctx
);
1277 return p
.shader_program
;
1283 * Return a fragment program which implements the current
1284 * fixed-function texture, fog and color-sum operations.
1286 struct gl_shader_program
*
1287 _mesa_get_fixed_func_fragment_program(struct gl_context
*ctx
)
1289 struct gl_shader_program
*shader_program
;
1290 struct state_key key
;
1293 keySize
= make_state_key(ctx
, &key
);
1295 shader_program
= (struct gl_shader_program
*)
1296 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1299 if (!shader_program
) {
1300 shader_program
= create_new_program(ctx
, &key
);
1302 _mesa_shader_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1303 &key
, keySize
, shader_program
);
1306 return shader_program
;