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 **************************************************************************/
30 #include "main/glheader.h"
31 #include "main/context.h"
32 #include "main/imports.h"
33 #include "main/macros.h"
34 #include "main/samplerobj.h"
35 #include "main/shaderobj.h"
36 #include "main/texenvprogram.h"
37 #include "main/texobj.h"
38 #include "main/uniforms.h"
39 #include "compiler/glsl/ir_builder.h"
40 #include "compiler/glsl/ir_optimization.h"
41 #include "compiler/glsl/glsl_parser_extras.h"
42 #include "compiler/glsl/glsl_symbol_table.h"
43 #include "compiler/glsl_types.h"
44 #include "program/ir_to_mesa.h"
45 #include "program/program.h"
46 #include "program/programopt.h"
47 #include "program/prog_cache.h"
48 #include "program/prog_instruction.h"
49 #include "program/prog_parameter.h"
50 #include "program/prog_print.h"
51 #include "program/prog_statevars.h"
52 #include "util/bitscan.h"
54 using namespace ir_builder
;
57 * Note on texture units:
59 * The number of texture units supported by fixed-function fragment
60 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
61 * That's because there's a one-to-one correspondence between texture
62 * coordinates and samplers in fixed-function processing.
64 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
65 * sets of texcoords, so is fixed-function fragment processing.
67 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
71 struct texenvprog_cache_item
75 struct gl_shader_program
*data
;
76 struct texenvprog_cache_item
*next
;
80 texenv_doing_secondary_color(struct gl_context
*ctx
)
82 if (ctx
->Light
.Enabled
&&
83 (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
))
86 if (ctx
->Fog
.ColorSumEnabled
)
94 __extension__ GLubyte Source
:4; /**< SRC_x */
95 __extension__ GLubyte Operand
:3; /**< OPR_x */
97 GLubyte Source
; /**< SRC_x */
98 GLubyte Operand
; /**< OPR_x */
103 GLuint nr_enabled_units
:8;
104 GLuint enabled_units
:8;
105 GLuint separate_specular
:1;
106 GLuint fog_enabled
:1;
107 GLuint fog_mode
:2; /**< FOG_x */
108 GLuint inputs_available
:12;
109 GLuint num_draw_buffers
:4;
111 /* NOTE: This array of structs must be last! (see "keySize" below) */
114 GLuint source_index
:4; /**< TEXTURE_x_INDEX */
116 GLuint ScaleShiftRGB
:2;
117 GLuint ScaleShiftA
:2;
119 GLuint NumArgsRGB
:3; /**< up to MAX_COMBINER_TERMS */
120 GLuint ModeRGB
:5; /**< MODE_x */
122 GLuint NumArgsA
:3; /**< up to MAX_COMBINER_TERMS */
123 GLuint ModeA
:5; /**< MODE_x */
125 struct mode_opt OptRGB
[MAX_COMBINER_TERMS
];
126 struct mode_opt OptA
[MAX_COMBINER_TERMS
];
127 } unit
[MAX_TEXTURE_UNITS
];
133 #define FOG_UNKNOWN 3
135 static GLuint
translate_fog_mode( GLenum mode
)
138 case GL_LINEAR
: return FOG_LINEAR
;
139 case GL_EXP
: return FOG_EXP
;
140 case GL_EXP2
: return FOG_EXP2
;
141 default: return FOG_UNKNOWN
;
145 #define OPR_SRC_COLOR 0
146 #define OPR_ONE_MINUS_SRC_COLOR 1
147 #define OPR_SRC_ALPHA 2
148 #define OPR_ONE_MINUS_SRC_ALPHA 3
151 #define OPR_UNKNOWN 7
153 static GLuint
translate_operand( GLenum operand
)
156 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
157 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
158 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
159 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
160 case GL_ZERO
: return OPR_ZERO
;
161 case GL_ONE
: return OPR_ONE
;
168 #define SRC_TEXTURE 0
169 #define SRC_TEXTURE0 1
170 #define SRC_TEXTURE1 2
171 #define SRC_TEXTURE2 3
172 #define SRC_TEXTURE3 4
173 #define SRC_TEXTURE4 5
174 #define SRC_TEXTURE5 6
175 #define SRC_TEXTURE6 7
176 #define SRC_TEXTURE7 8
177 #define SRC_CONSTANT 9
178 #define SRC_PRIMARY_COLOR 10
179 #define SRC_PREVIOUS 11
181 #define SRC_UNKNOWN 15
183 static GLuint
translate_source( GLenum src
)
186 case GL_TEXTURE
: return SRC_TEXTURE
;
194 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
195 case GL_CONSTANT
: return SRC_CONSTANT
;
196 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
197 case GL_PREVIOUS
: return SRC_PREVIOUS
;
206 #define MODE_REPLACE 0 /* r = a0 */
207 #define MODE_MODULATE 1 /* r = a0 * a1 */
208 #define MODE_ADD 2 /* r = a0 + a1 */
209 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
210 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
211 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
212 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
213 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
214 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
215 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
216 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
217 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
218 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
219 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
220 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
221 #define MODE_UNKNOWN 16
224 * Translate GL combiner state into a MODE_x value
226 static GLuint
translate_mode( GLenum envMode
, GLenum mode
)
229 case GL_REPLACE
: return MODE_REPLACE
;
230 case GL_MODULATE
: return MODE_MODULATE
;
232 if (envMode
== GL_COMBINE4_NV
)
233 return MODE_ADD_PRODUCTS
;
237 if (envMode
== GL_COMBINE4_NV
)
238 return MODE_ADD_PRODUCTS_SIGNED
;
240 return MODE_ADD_SIGNED
;
241 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
242 case GL_SUBTRACT
: return MODE_SUBTRACT
;
243 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
244 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
245 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
246 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
247 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
248 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
249 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_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
:
289 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
292 * Identify all possible varying inputs. The fragment program will
293 * never reference non-varying inputs, but will track them via state
296 * This function figures out all the inputs that the fragment program
297 * has access to. The bitmask is later reduced to just those which
298 * are actually referenced.
300 static GLbitfield
get_fp_input_mask( struct gl_context
*ctx
)
303 const GLboolean vertexShader
=
304 (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] &&
305 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->LinkStatus
&&
306 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->_LinkedShaders
[MESA_SHADER_VERTEX
]);
307 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
308 GLbitfield fp_inputs
= 0x0;
310 if (ctx
->VertexProgram
._Overriden
) {
311 /* Somebody's messing with the vertex program and we don't have
312 * a clue what's happening. Assume that it could be producing
313 * all possible outputs.
317 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
318 /* _NEW_RENDERMODE */
319 fp_inputs
= (VARYING_BIT_COL0
| VARYING_BIT_TEX0
);
321 else if (!(vertexProgram
|| vertexShader
)) {
322 /* Fixed function vertex logic */
323 /* _NEW_VARYING_VP_INPUTS */
324 GLbitfield64 varying_inputs
= ctx
->varying_vp_inputs
;
326 /* These get generated in the setup routine regardless of the
330 if (ctx
->Point
.PointSprite
)
331 varying_inputs
|= VARYING_BITS_TEX_ANY
;
333 /* First look at what values may be computed by the generated
337 if (ctx
->Light
.Enabled
) {
338 fp_inputs
|= VARYING_BIT_COL0
;
340 if (texenv_doing_secondary_color(ctx
))
341 fp_inputs
|= VARYING_BIT_COL1
;
345 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
346 ctx
->Texture
._TexMatEnabled
) << VARYING_SLOT_TEX0
;
348 /* Then look at what might be varying as a result of enabled
351 if (varying_inputs
& VERT_BIT_COLOR0
)
352 fp_inputs
|= VARYING_BIT_COL0
;
353 if (varying_inputs
& VERT_BIT_COLOR1
)
354 fp_inputs
|= VARYING_BIT_COL1
;
356 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
357 << VARYING_SLOT_TEX0
);
361 /* calculate from vp->outputs */
362 struct gl_program
*vprog
;
363 GLbitfield64 vp_outputs
;
365 /* Choose GLSL vertex shader over ARB vertex program. Need this
366 * since vertex shader state validation comes after fragment state
367 * validation (see additional comments in state.c).
370 vprog
= ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
372 vprog
= &ctx
->VertexProgram
.Current
->Base
;
374 vp_outputs
= vprog
->OutputsWritten
;
376 /* These get generated in the setup routine regardless of the
380 if (ctx
->Point
.PointSprite
)
381 vp_outputs
|= VARYING_BITS_TEX_ANY
;
383 if (vp_outputs
& (1 << VARYING_SLOT_COL0
))
384 fp_inputs
|= VARYING_BIT_COL0
;
385 if (vp_outputs
& (1 << VARYING_SLOT_COL1
))
386 fp_inputs
|= VARYING_BIT_COL1
;
388 fp_inputs
|= (((vp_outputs
& VARYING_BITS_TEX_ANY
) >> VARYING_SLOT_TEX0
)
389 << VARYING_SLOT_TEX0
);
397 * Examine current texture environment state and generate a unique
398 * key to identify it.
400 static GLuint
make_state_key( struct gl_context
*ctx
, struct state_key
*key
)
403 GLbitfield inputs_referenced
= VARYING_BIT_COL0
;
404 const GLbitfield inputs_available
= get_fp_input_mask( ctx
);
408 memset(key
, 0, sizeof(*key
));
411 mask
= ctx
->Texture
._EnabledCoordUnits
;
413 const int i
= u_bit_scan(&mask
);
414 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
415 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
416 const struct gl_tex_env_combine_state
*comb
= texUnit
->_CurrentCombine
;
417 const struct gl_sampler_object
*samp
;
423 samp
= _mesa_get_samplerobj(ctx
, i
);
424 format
= _mesa_texture_base_format(texObj
);
426 key
->unit
[i
].enabled
= 1;
427 key
->enabled_units
|= (1<<i
);
428 key
->nr_enabled_units
= i
+ 1;
429 inputs_referenced
|= VARYING_BIT_TEX(i
);
431 key
->unit
[i
].source_index
= _mesa_tex_target_to_index(ctx
,
434 key
->unit
[i
].shadow
=
435 ((samp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
436 ((format
== GL_DEPTH_COMPONENT
) ||
437 (format
== GL_DEPTH_STENCIL_EXT
)));
439 key
->unit
[i
].NumArgsRGB
= comb
->_NumArgsRGB
;
440 key
->unit
[i
].NumArgsA
= comb
->_NumArgsA
;
442 key
->unit
[i
].ModeRGB
=
443 translate_mode(texUnit
->EnvMode
, comb
->ModeRGB
);
445 translate_mode(texUnit
->EnvMode
, comb
->ModeA
);
447 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
448 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
450 for (j
= 0; j
< MAX_COMBINER_TERMS
; j
++) {
451 key
->unit
[i
].OptRGB
[j
].Operand
= translate_operand(comb
->OperandRGB
[j
]);
452 key
->unit
[i
].OptA
[j
].Operand
= translate_operand(comb
->OperandA
[j
]);
453 key
->unit
[i
].OptRGB
[j
].Source
= translate_source(comb
->SourceRGB
[j
]);
454 key
->unit
[i
].OptA
[j
].Source
= translate_source(comb
->SourceA
[j
]);
458 /* _NEW_LIGHT | _NEW_FOG */
459 if (texenv_doing_secondary_color(ctx
)) {
460 key
->separate_specular
= 1;
461 inputs_referenced
|= VARYING_BIT_COL1
;
465 if (ctx
->Fog
.Enabled
) {
466 key
->fog_enabled
= 1;
467 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
468 inputs_referenced
|= VARYING_BIT_FOGC
; /* maybe */
472 key
->num_draw_buffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
475 if (ctx
->Color
.AlphaEnabled
&& key
->num_draw_buffers
== 0) {
476 /* if alpha test is enabled we need to emit at least one color */
477 key
->num_draw_buffers
= 1;
480 key
->inputs_available
= (inputs_available
& inputs_referenced
);
482 /* compute size of state key, ignoring unused texture units */
483 keySize
= sizeof(*key
) - sizeof(key
->unit
)
484 + key
->nr_enabled_units
* sizeof(key
->unit
[0]);
490 /** State used to build the fragment program:
492 class texenv_fragment_program
: public ir_factory
{
494 struct gl_shader_program
*shader_program
;
495 struct gl_shader
*shader
;
496 exec_list
*top_instructions
;
497 struct state_key
*state
;
499 ir_variable
*src_texture
[MAX_TEXTURE_COORD_UNITS
];
500 /* Reg containing each texture unit's sampled texture color,
504 /* Texcoord override from bumpmapping. */
505 ir_variable
*texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
507 /* Reg containing texcoord for a texture unit,
508 * needed for bump mapping, else undef.
511 ir_rvalue
*src_previous
; /**< Reg containing color from previous
512 * stage. May need to be decl'd.
517 get_current_attrib(texenv_fragment_program
*p
, GLuint attrib
)
519 ir_variable
*current
;
522 current
= p
->shader
->symbols
->get_variable("gl_CurrentAttribFragMESA");
524 current
->data
.max_array_access
= MAX2(current
->data
.max_array_access
, (int)attrib
);
525 val
= new(p
->mem_ctx
) ir_dereference_variable(current
);
526 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(attrib
);
527 return new(p
->mem_ctx
) ir_dereference_array(val
, index
);
531 get_gl_Color(texenv_fragment_program
*p
)
533 if (p
->state
->inputs_available
& VARYING_BIT_COL0
) {
534 ir_variable
*var
= p
->shader
->symbols
->get_variable("gl_Color");
536 return new(p
->mem_ctx
) ir_dereference_variable(var
);
538 return get_current_attrib(p
, VERT_ATTRIB_COLOR0
);
543 get_source(texenv_fragment_program
*p
,
544 GLuint src
, GLuint unit
)
547 ir_dereference
*deref
;
551 return new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
561 return new(p
->mem_ctx
)
562 ir_dereference_variable(p
->src_texture
[src
- SRC_TEXTURE0
]);
565 var
= p
->shader
->symbols
->get_variable("gl_TextureEnvColor");
567 deref
= new(p
->mem_ctx
) ir_dereference_variable(var
);
568 var
->data
.max_array_access
= MAX2(var
->data
.max_array_access
, (int)unit
);
569 return new(p
->mem_ctx
) ir_dereference_array(deref
,
570 new(p
->mem_ctx
) ir_constant(unit
));
572 case SRC_PRIMARY_COLOR
:
573 var
= p
->shader
->symbols
->get_variable("gl_Color");
575 return new(p
->mem_ctx
) ir_dereference_variable(var
);
578 return new(p
->mem_ctx
) ir_constant(0.0f
);
581 if (!p
->src_previous
) {
582 return get_gl_Color(p
);
584 return p
->src_previous
->clone(p
->mem_ctx
, NULL
);
594 emit_combine_source(texenv_fragment_program
*p
,
601 src
= get_source(p
, source
, unit
);
604 case OPR_ONE_MINUS_SRC_COLOR
:
605 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), src
);
608 return src
->type
->is_scalar() ? src
: swizzle_w(src
);
610 case OPR_ONE_MINUS_SRC_ALPHA
: {
611 ir_rvalue
*const scalar
= src
->type
->is_scalar() ? src
: swizzle_w(src
);
613 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), scalar
);
617 return new(p
->mem_ctx
) ir_constant(0.0f
);
619 return new(p
->mem_ctx
) ir_constant(1.0f
);
629 * Check if the RGB and Alpha sources and operands match for the given
630 * texture unit's combinder state. When the RGB and A sources and
631 * operands match, we can emit fewer instructions.
633 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
635 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
637 for (i
= 0; i
< numArgs
; i
++) {
638 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
641 switch (key
->unit
[unit
].OptA
[i
].Operand
) {
643 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
651 case OPR_ONE_MINUS_SRC_ALPHA
:
652 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
653 case OPR_ONE_MINUS_SRC_COLOR
:
654 case OPR_ONE_MINUS_SRC_ALPHA
:
661 return GL_FALSE
; /* impossible */
669 smear(ir_rvalue
*val
)
671 if (!val
->type
->is_scalar())
674 return swizzle_xxxx(val
);
678 emit_combine(texenv_fragment_program
*p
,
682 const struct mode_opt
*opt
)
684 ir_rvalue
*src
[MAX_COMBINER_TERMS
];
685 ir_rvalue
*tmp0
, *tmp1
;
688 assert(nr
<= MAX_COMBINER_TERMS
);
690 for (i
= 0; i
< nr
; i
++)
691 src
[i
] = emit_combine_source( p
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
698 return mul(src
[0], src
[1]);
701 return add(src
[0], src
[1]);
703 case MODE_ADD_SIGNED
:
704 return add(add(src
[0], src
[1]), new(p
->mem_ctx
) ir_constant(-0.5f
));
706 case MODE_INTERPOLATE
:
707 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
708 tmp0
= mul(src
[0], src
[2]);
709 tmp1
= mul(src
[1], sub(new(p
->mem_ctx
) ir_constant(1.0f
),
710 src
[2]->clone(p
->mem_ctx
, NULL
)));
711 return add(tmp0
, tmp1
);
714 return sub(src
[0], src
[1]);
717 case MODE_DOT3_RGBA_EXT
:
718 case MODE_DOT3_RGB_EXT
:
719 case MODE_DOT3_RGB
: {
720 tmp0
= mul(src
[0], new(p
->mem_ctx
) ir_constant(2.0f
));
721 tmp0
= add(tmp0
, new(p
->mem_ctx
) ir_constant(-1.0f
));
723 tmp1
= mul(src
[1], new(p
->mem_ctx
) ir_constant(2.0f
));
724 tmp1
= add(tmp1
, new(p
->mem_ctx
) ir_constant(-1.0f
));
726 return dot(swizzle_xyz(smear(tmp0
)), swizzle_xyz(smear(tmp1
)));
728 case MODE_MODULATE_ADD_ATI
:
729 return add(mul(src
[0], src
[2]), src
[1]);
731 case MODE_MODULATE_SIGNED_ADD_ATI
:
732 return add(add(mul(src
[0], src
[2]), src
[1]),
733 new(p
->mem_ctx
) ir_constant(-0.5f
));
735 case MODE_MODULATE_SUBTRACT_ATI
:
736 return sub(mul(src
[0], src
[2]), src
[1]);
738 case MODE_ADD_PRODUCTS
:
739 return add(mul(src
[0], src
[1]), mul(src
[2], src
[3]));
741 case MODE_ADD_PRODUCTS_SIGNED
:
742 return add(add(mul(src
[0], src
[1]), mul(src
[2], src
[3])),
743 new(p
->mem_ctx
) ir_constant(-0.5f
));
751 * Generate instructions for one texture unit's env/combiner mode.
754 emit_texenv(texenv_fragment_program
*p
, GLuint unit
)
756 const struct state_key
*key
= p
->state
;
757 GLboolean rgb_saturate
, alpha_saturate
;
758 GLuint rgb_shift
, alpha_shift
;
760 if (!key
->unit
[unit
].enabled
) {
761 return get_source(p
, SRC_PREVIOUS
, 0);
764 switch (key
->unit
[unit
].ModeRGB
) {
765 case MODE_DOT3_RGB_EXT
:
766 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
769 case MODE_DOT3_RGBA_EXT
:
774 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
775 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
779 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
780 * We don't want to clamp twice.
783 rgb_saturate
= GL_FALSE
; /* saturate after rgb shift */
784 else if (need_saturate(key
->unit
[unit
].ModeRGB
))
785 rgb_saturate
= GL_TRUE
;
787 rgb_saturate
= GL_FALSE
;
790 alpha_saturate
= GL_FALSE
; /* saturate after alpha shift */
791 else if (need_saturate(key
->unit
[unit
].ModeA
))
792 alpha_saturate
= GL_TRUE
;
794 alpha_saturate
= GL_FALSE
;
796 ir_variable
*temp_var
= p
->make_temp(glsl_type::vec4_type
, "texenv_combine");
797 ir_dereference
*deref
;
800 /* Emit the RGB and A combine ops
802 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
803 args_match(key
, unit
)) {
804 val
= emit_combine(p
, unit
,
805 key
->unit
[unit
].NumArgsRGB
,
806 key
->unit
[unit
].ModeRGB
,
807 key
->unit
[unit
].OptRGB
);
812 p
->emit(assign(temp_var
, val
));
814 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
815 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
816 ir_rvalue
*val
= emit_combine(p
, unit
,
817 key
->unit
[unit
].NumArgsRGB
,
818 key
->unit
[unit
].ModeRGB
,
819 key
->unit
[unit
].OptRGB
);
823 p
->emit(assign(temp_var
, val
));
826 /* Need to do something to stop from re-emitting identical
827 * argument calculations here:
829 val
= emit_combine(p
, unit
,
830 key
->unit
[unit
].NumArgsRGB
,
831 key
->unit
[unit
].ModeRGB
,
832 key
->unit
[unit
].OptRGB
);
833 val
= swizzle_xyz(smear(val
));
836 p
->emit(assign(temp_var
, val
, WRITEMASK_XYZ
));
838 val
= emit_combine(p
, unit
,
839 key
->unit
[unit
].NumArgsA
,
840 key
->unit
[unit
].ModeA
,
841 key
->unit
[unit
].OptA
);
842 val
= swizzle_w(smear(val
));
845 p
->emit(assign(temp_var
, val
, WRITEMASK_W
));
848 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
850 /* Deal with the final shift:
852 if (alpha_shift
|| rgb_shift
) {
855 if (rgb_shift
== alpha_shift
) {
856 shift
= new(p
->mem_ctx
) ir_constant((float)(1 << rgb_shift
));
859 ir_constant_data const_data
;
861 const_data
.f
[0] = float(1 << rgb_shift
);
862 const_data
.f
[1] = float(1 << rgb_shift
);
863 const_data
.f
[2] = float(1 << rgb_shift
);
864 const_data
.f
[3] = float(1 << alpha_shift
);
866 shift
= new(p
->mem_ctx
) ir_constant(glsl_type::vec4_type
,
870 return saturate(mul(deref
, shift
));
878 * Generate instruction for getting a texture source term.
880 static void load_texture( texenv_fragment_program
*p
, GLuint unit
)
882 ir_dereference
*deref
;
884 if (p
->src_texture
[unit
])
887 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
890 if (!(p
->state
->inputs_available
& (VARYING_BIT_TEX0
<< unit
))) {
891 texcoord
= get_current_attrib(p
, VERT_ATTRIB_TEX0
+ unit
);
892 } else if (p
->texcoord_tex
[unit
]) {
893 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(p
->texcoord_tex
[unit
]);
895 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
897 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
898 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(unit
);
899 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
900 tc_array
->data
.max_array_access
= MAX2(tc_array
->data
.max_array_access
, (int)unit
);
903 if (!p
->state
->unit
[unit
].enabled
) {
904 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
906 p
->emit(p
->src_texture
[unit
]);
908 p
->emit(assign(p
->src_texture
[unit
], new(p
->mem_ctx
) ir_constant(0.0f
)));
912 const glsl_type
*sampler_type
= NULL
;
916 case TEXTURE_1D_INDEX
:
917 if (p
->state
->unit
[unit
].shadow
)
918 sampler_type
= glsl_type::sampler1DShadow_type
;
920 sampler_type
= glsl_type::sampler1D_type
;
923 case TEXTURE_1D_ARRAY_INDEX
:
924 if (p
->state
->unit
[unit
].shadow
)
925 sampler_type
= glsl_type::sampler1DArrayShadow_type
;
927 sampler_type
= glsl_type::sampler1DArray_type
;
930 case TEXTURE_2D_INDEX
:
931 if (p
->state
->unit
[unit
].shadow
)
932 sampler_type
= glsl_type::sampler2DShadow_type
;
934 sampler_type
= glsl_type::sampler2D_type
;
937 case TEXTURE_2D_ARRAY_INDEX
:
938 if (p
->state
->unit
[unit
].shadow
)
939 sampler_type
= glsl_type::sampler2DArrayShadow_type
;
941 sampler_type
= glsl_type::sampler2DArray_type
;
944 case TEXTURE_RECT_INDEX
:
945 if (p
->state
->unit
[unit
].shadow
)
946 sampler_type
= glsl_type::sampler2DRectShadow_type
;
948 sampler_type
= glsl_type::sampler2DRect_type
;
951 case TEXTURE_3D_INDEX
:
952 assert(!p
->state
->unit
[unit
].shadow
);
953 sampler_type
= glsl_type::sampler3D_type
;
956 case TEXTURE_CUBE_INDEX
:
957 if (p
->state
->unit
[unit
].shadow
)
958 sampler_type
= glsl_type::samplerCubeShadow_type
;
960 sampler_type
= glsl_type::samplerCube_type
;
963 case TEXTURE_EXTERNAL_INDEX
:
964 assert(!p
->state
->unit
[unit
].shadow
);
965 sampler_type
= glsl_type::samplerExternalOES_type
;
970 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
973 ir_texture
*tex
= new(p
->mem_ctx
) ir_texture(ir_tex
);
976 char *sampler_name
= ralloc_asprintf(p
->mem_ctx
, "sampler_%d", unit
);
977 ir_variable
*sampler
= new(p
->mem_ctx
) ir_variable(sampler_type
,
980 p
->top_instructions
->push_head(sampler
);
982 /* Set the texture unit for this sampler in the same way that
983 * layout(binding=X) would.
985 sampler
->data
.explicit_binding
= true;
986 sampler
->data
.binding
= unit
;
988 deref
= new(p
->mem_ctx
) ir_dereference_variable(sampler
);
989 tex
->set_sampler(deref
, glsl_type::vec4_type
);
991 tex
->coordinate
= new(p
->mem_ctx
) ir_swizzle(texcoord
, 0, 1, 2, 3, coords
);
993 if (p
->state
->unit
[unit
].shadow
) {
994 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
995 tex
->shadow_comparitor
= new(p
->mem_ctx
) ir_swizzle(texcoord
,
1001 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1002 tex
->projector
= swizzle_w(texcoord
);
1004 p
->emit(assign(p
->src_texture
[unit
], tex
));
1008 load_texenv_source(texenv_fragment_program
*p
,
1009 GLuint src
, GLuint unit
)
1013 load_texture(p
, unit
);
1024 load_texture(p
, src
- SRC_TEXTURE0
);
1028 /* not a texture src - do nothing */
1035 * Generate instructions for loading all texture source terms.
1038 load_texunit_sources( texenv_fragment_program
*p
, GLuint unit
)
1040 const struct state_key
*key
= p
->state
;
1043 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1044 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1047 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1048 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1055 * Applies the fog calculations.
1057 * This is basically like the ARB_fragment_prorgam fog options. Note
1058 * that ffvertex_prog.c produces fogcoord for us when
1059 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1062 emit_fog_instructions(texenv_fragment_program
*p
,
1063 ir_rvalue
*fragcolor
)
1065 struct state_key
*key
= p
->state
;
1066 ir_rvalue
*f
, *temp
;
1067 ir_variable
*params
, *oparams
;
1068 ir_variable
*fogcoord
;
1070 /* Temporary storage for the whole fog result. Fog calculations
1071 * only affect rgb so we're hanging on to the .a value of fragcolor
1074 ir_variable
*fog_result
= p
->make_temp(glsl_type::vec4_type
, "fog_result");
1075 p
->emit(assign(fog_result
, fragcolor
));
1077 fragcolor
= swizzle_xyz(fog_result
);
1079 oparams
= p
->shader
->symbols
->get_variable("gl_FogParamsOptimizedMESA");
1081 fogcoord
= p
->shader
->symbols
->get_variable("gl_FogFragCoord");
1083 params
= p
->shader
->symbols
->get_variable("gl_Fog");
1085 f
= new(p
->mem_ctx
) ir_dereference_variable(fogcoord
);
1087 ir_variable
*f_var
= p
->make_temp(glsl_type::float_type
, "fog_factor");
1089 switch (key
->fog_mode
) {
1091 /* f = (end - z) / (end - start)
1093 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1094 * (end / (end - start)) so we can generate a single MAD.
1096 f
= add(mul(f
, swizzle_x(oparams
)), swizzle_y(oparams
));
1099 /* f = e^(-(density * fogcoord))
1101 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1102 * use EXP2 which is generally the native instruction without
1103 * having to do any further math on the fog density uniform.
1105 f
= mul(f
, swizzle_z(oparams
));
1106 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1107 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1110 /* f = e^(-(density * fogcoord)^2)
1112 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1113 * can do this like FOG_EXP but with a squaring after the
1114 * multiply by density.
1116 ir_variable
*temp_var
= p
->make_temp(glsl_type::float_type
, "fog_temp");
1117 p
->emit(assign(temp_var
, mul(f
, swizzle_w(oparams
))));
1119 f
= mul(temp_var
, temp_var
);
1120 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1121 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1125 p
->emit(assign(f_var
, saturate(f
)));
1127 f
= sub(new(p
->mem_ctx
) ir_constant(1.0f
), f_var
);
1128 temp
= new(p
->mem_ctx
) ir_dereference_variable(params
);
1129 temp
= new(p
->mem_ctx
) ir_dereference_record(temp
, "color");
1130 temp
= mul(swizzle_xyz(temp
), f
);
1132 p
->emit(assign(fog_result
, add(temp
, mul(fragcolor
, f_var
)), WRITEMASK_XYZ
));
1134 return new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1138 emit_instructions(texenv_fragment_program
*p
)
1140 struct state_key
*key
= p
->state
;
1143 if (key
->enabled_units
) {
1144 /* First pass - to support texture_env_crossbar, first identify
1145 * all referenced texture sources and emit texld instructions
1148 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1149 if (key
->unit
[unit
].enabled
) {
1150 load_texunit_sources(p
, unit
);
1153 /* Second pass - emit combine instructions to build final color:
1155 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1156 if (key
->unit
[unit
].enabled
) {
1157 p
->src_previous
= emit_texenv(p
, unit
);
1162 ir_rvalue
*cf
= get_source(p
, SRC_PREVIOUS
, 0);
1164 if (key
->separate_specular
) {
1165 ir_variable
*spec_result
= p
->make_temp(glsl_type::vec4_type
,
1167 p
->emit(assign(spec_result
, cf
));
1169 ir_rvalue
*secondary
;
1170 if (p
->state
->inputs_available
& VARYING_BIT_COL1
) {
1172 p
->shader
->symbols
->get_variable("gl_SecondaryColor");
1174 secondary
= swizzle_xyz(var
);
1176 secondary
= swizzle_xyz(get_current_attrib(p
, VERT_ATTRIB_COLOR1
));
1179 p
->emit(assign(spec_result
, add(swizzle_xyz(spec_result
), secondary
),
1182 cf
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1185 if (key
->fog_enabled
) {
1186 cf
= emit_fog_instructions(p
, cf
);
1189 ir_variable
*frag_color
= p
->shader
->symbols
->get_variable("gl_FragColor");
1191 p
->emit(assign(frag_color
, cf
));
1195 * Generate a new fragment program which implements the context's
1196 * current texture env/combine mode.
1198 static struct gl_shader_program
*
1199 create_new_program(struct gl_context
*ctx
, struct state_key
*key
)
1201 texenv_fragment_program p
;
1203 _mesa_glsl_parse_state
*state
;
1205 p
.mem_ctx
= ralloc_context(NULL
);
1206 p
.shader
= _mesa_new_shader(0, MESA_SHADER_FRAGMENT
);
1207 p
.shader
->ir
= new(p
.shader
) exec_list
;
1208 state
= new(p
.shader
) _mesa_glsl_parse_state(ctx
, MESA_SHADER_FRAGMENT
,
1210 p
.shader
->symbols
= state
->symbols
;
1211 p
.top_instructions
= p
.shader
->ir
;
1212 p
.instructions
= p
.shader
->ir
;
1214 p
.shader_program
= _mesa_new_shader_program(0);
1216 /* Tell the linker to ignore the fact that we're building a
1217 * separate shader, in case we're in a GLES2 context that would
1218 * normally reject that. The real problem is that we're building a
1219 * fixed function program in a GLES2 context at all, but that's a
1220 * big mess to clean up.
1222 p
.shader_program
->SeparateShader
= GL_TRUE
;
1224 state
->language_version
= 130;
1225 state
->es_shader
= false;
1226 if (_mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
)
1227 state
->OES_EGL_image_external_enable
= true;
1228 _mesa_glsl_initialize_types(state
);
1229 _mesa_glsl_initialize_variables(p
.instructions
, state
);
1231 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1232 p
.src_texture
[unit
] = NULL
;
1233 p
.texcoord_tex
[unit
] = NULL
;
1236 p
.src_previous
= NULL
;
1238 ir_function
*main_f
= new(p
.mem_ctx
) ir_function("main");
1240 state
->symbols
->add_function(main_f
);
1242 ir_function_signature
*main_sig
=
1243 new(p
.mem_ctx
) ir_function_signature(glsl_type::void_type
);
1244 main_sig
->is_defined
= true;
1245 main_f
->add_signature(main_sig
);
1247 p
.instructions
= &main_sig
->body
;
1248 if (key
->num_draw_buffers
)
1249 emit_instructions(&p
);
1251 validate_ir_tree(p
.shader
->ir
);
1253 const struct gl_shader_compiler_options
*options
=
1254 &ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_FRAGMENT
];
1256 while (do_common_optimization(p
.shader
->ir
, false, false, options
,
1257 ctx
->Const
.NativeIntegers
))
1259 reparent_ir(p
.shader
->ir
, p
.shader
->ir
);
1261 p
.shader
->CompileStatus
= true;
1262 p
.shader
->Version
= state
->language_version
;
1263 p
.shader
->uses_builtin_functions
= state
->uses_builtin_functions
;
1264 p
.shader_program
->Shaders
=
1265 (gl_shader
**)malloc(sizeof(*p
.shader_program
->Shaders
));
1266 p
.shader_program
->Shaders
[0] = p
.shader
;
1267 p
.shader_program
->NumShaders
= 1;
1269 _mesa_glsl_link_shader(ctx
, p
.shader_program
);
1271 if (!p
.shader_program
->LinkStatus
)
1272 _mesa_problem(ctx
, "Failed to link fixed function fragment shader: %s\n",
1273 p
.shader_program
->InfoLog
);
1275 ralloc_free(p
.mem_ctx
);
1276 return p
.shader_program
;
1282 * Return a fragment program which implements the current
1283 * fixed-function texture, fog and color-sum operations.
1285 struct gl_shader_program
*
1286 _mesa_get_fixed_func_fragment_program(struct gl_context
*ctx
)
1288 struct gl_shader_program
*shader_program
;
1289 struct state_key key
;
1292 keySize
= make_state_key(ctx
, &key
);
1294 shader_program
= (struct gl_shader_program
*)
1295 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1298 if (!shader_program
) {
1299 shader_program
= create_new_program(ctx
, &key
);
1301 _mesa_shader_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1302 &key
, keySize
, shader_program
);
1305 return shader_program
;