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/texenvprogram.h"
36 #include "main/texobj.h"
37 #include "main/uniforms.h"
38 #include "glsl/ir_builder.h"
39 #include "glsl/ir_optimization.h"
40 #include "glsl/glsl_parser_extras.h"
41 #include "glsl/glsl_symbol_table.h"
42 #include "glsl/glsl_types.h"
43 #include "program/ir_to_mesa.h"
44 #include "program/program.h"
45 #include "program/programopt.h"
46 #include "program/prog_cache.h"
47 #include "program/prog_instruction.h"
48 #include "program/prog_parameter.h"
49 #include "program/prog_print.h"
50 #include "program/prog_statevars.h"
52 using namespace ir_builder
;
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_UNKNOWN 16
222 * Translate GL combiner state into a MODE_x value
224 static GLuint
translate_mode( GLenum envMode
, GLenum mode
)
227 case GL_REPLACE
: return MODE_REPLACE
;
228 case GL_MODULATE
: return MODE_MODULATE
;
230 if (envMode
== GL_COMBINE4_NV
)
231 return MODE_ADD_PRODUCTS
;
235 if (envMode
== GL_COMBINE4_NV
)
236 return MODE_ADD_PRODUCTS_SIGNED
;
238 return MODE_ADD_SIGNED
;
239 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
240 case GL_SUBTRACT
: return MODE_SUBTRACT
;
241 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
242 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
243 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
244 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
245 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
246 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
247 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
256 * Do we need to clamp the results of the given texture env/combine mode?
257 * If the inputs to the mode are in [0,1] we don't always have to clamp
261 need_saturate( GLuint mode
)
266 case MODE_INTERPOLATE
:
269 case MODE_ADD_SIGNED
:
272 case MODE_DOT3_RGB_EXT
:
274 case MODE_DOT3_RGBA_EXT
:
275 case MODE_MODULATE_ADD_ATI
:
276 case MODE_MODULATE_SIGNED_ADD_ATI
:
277 case MODE_MODULATE_SUBTRACT_ATI
:
278 case MODE_ADD_PRODUCTS
:
279 case MODE_ADD_PRODUCTS_SIGNED
:
287 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
290 * Identify all possible varying inputs. The fragment program will
291 * never reference non-varying inputs, but will track them via state
294 * This function figures out all the inputs that the fragment program
295 * has access to. The bitmask is later reduced to just those which
296 * are actually referenced.
298 static GLbitfield
get_fp_input_mask( struct gl_context
*ctx
)
301 const GLboolean vertexShader
=
302 (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] &&
303 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->LinkStatus
&&
304 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->_LinkedShaders
[MESA_SHADER_VERTEX
]);
305 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
306 GLbitfield fp_inputs
= 0x0;
308 if (ctx
->VertexProgram
._Overriden
) {
309 /* Somebody's messing with the vertex program and we don't have
310 * a clue what's happening. Assume that it could be producing
311 * all possible outputs.
315 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
316 /* _NEW_RENDERMODE */
317 fp_inputs
= (VARYING_BIT_COL0
| VARYING_BIT_TEX0
);
319 else if (!(vertexProgram
|| vertexShader
)) {
320 /* Fixed function vertex logic */
321 /* _NEW_VARYING_VP_INPUTS */
322 GLbitfield64 varying_inputs
= ctx
->varying_vp_inputs
;
324 /* These get generated in the setup routine regardless of the
328 if (ctx
->Point
.PointSprite
)
329 varying_inputs
|= VARYING_BITS_TEX_ANY
;
331 /* First look at what values may be computed by the generated
335 if (ctx
->Light
.Enabled
) {
336 fp_inputs
|= VARYING_BIT_COL0
;
338 if (texenv_doing_secondary_color(ctx
))
339 fp_inputs
|= VARYING_BIT_COL1
;
343 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
344 ctx
->Texture
._TexMatEnabled
) << VARYING_SLOT_TEX0
;
346 /* Then look at what might be varying as a result of enabled
349 if (varying_inputs
& VERT_BIT_COLOR0
)
350 fp_inputs
|= VARYING_BIT_COL0
;
351 if (varying_inputs
& VERT_BIT_COLOR1
)
352 fp_inputs
|= VARYING_BIT_COL1
;
354 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
355 << VARYING_SLOT_TEX0
);
359 /* calculate from vp->outputs */
360 struct gl_program
*vprog
;
361 GLbitfield64 vp_outputs
;
363 /* Choose GLSL vertex shader over ARB vertex program. Need this
364 * since vertex shader state validation comes after fragment state
365 * validation (see additional comments in state.c).
368 vprog
= ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
370 vprog
= &ctx
->VertexProgram
.Current
->Base
;
372 vp_outputs
= vprog
->OutputsWritten
;
374 /* These get generated in the setup routine regardless of the
378 if (ctx
->Point
.PointSprite
)
379 vp_outputs
|= VARYING_BITS_TEX_ANY
;
381 if (vp_outputs
& (1 << VARYING_SLOT_COL0
))
382 fp_inputs
|= VARYING_BIT_COL0
;
383 if (vp_outputs
& (1 << VARYING_SLOT_COL1
))
384 fp_inputs
|= VARYING_BIT_COL1
;
386 fp_inputs
|= (((vp_outputs
& VARYING_BITS_TEX_ANY
) >> VARYING_SLOT_TEX0
)
387 << VARYING_SLOT_TEX0
);
395 * Examine current texture environment state and generate a unique
396 * key to identify it.
398 static GLuint
make_state_key( struct gl_context
*ctx
, struct state_key
*key
)
401 GLbitfield inputs_referenced
= VARYING_BIT_COL0
;
402 const GLbitfield inputs_available
= get_fp_input_mask( ctx
);
405 memset(key
, 0, sizeof(*key
));
408 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
409 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
410 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
411 const struct gl_tex_env_combine_state
*comb
= texUnit
->_CurrentCombine
;
412 const struct gl_sampler_object
*samp
;
415 if (!texUnit
->_Current
|| !texUnit
->Enabled
)
418 samp
= _mesa_get_samplerobj(ctx
, i
);
419 format
= _mesa_texture_base_format(texObj
);
421 key
->unit
[i
].enabled
= 1;
422 key
->enabled_units
|= (1<<i
);
423 key
->nr_enabled_units
= i
+ 1;
424 inputs_referenced
|= VARYING_BIT_TEX(i
);
426 key
->unit
[i
].source_index
= _mesa_tex_target_to_index(ctx
,
429 key
->unit
[i
].shadow
=
430 ((samp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
431 ((format
== GL_DEPTH_COMPONENT
) ||
432 (format
== GL_DEPTH_STENCIL_EXT
)));
434 key
->unit
[i
].NumArgsRGB
= comb
->_NumArgsRGB
;
435 key
->unit
[i
].NumArgsA
= comb
->_NumArgsA
;
437 key
->unit
[i
].ModeRGB
=
438 translate_mode(texUnit
->EnvMode
, comb
->ModeRGB
);
440 translate_mode(texUnit
->EnvMode
, comb
->ModeA
);
442 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
443 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
445 for (j
= 0; j
< MAX_COMBINER_TERMS
; j
++) {
446 key
->unit
[i
].OptRGB
[j
].Operand
= translate_operand(comb
->OperandRGB
[j
]);
447 key
->unit
[i
].OptA
[j
].Operand
= translate_operand(comb
->OperandA
[j
]);
448 key
->unit
[i
].OptRGB
[j
].Source
= translate_source(comb
->SourceRGB
[j
]);
449 key
->unit
[i
].OptA
[j
].Source
= translate_source(comb
->SourceA
[j
]);
453 /* _NEW_LIGHT | _NEW_FOG */
454 if (texenv_doing_secondary_color(ctx
)) {
455 key
->separate_specular
= 1;
456 inputs_referenced
|= VARYING_BIT_COL1
;
460 if (ctx
->Fog
.Enabled
) {
461 key
->fog_enabled
= 1;
462 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
463 inputs_referenced
|= VARYING_BIT_FOGC
; /* maybe */
467 key
->num_draw_buffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
470 if (ctx
->Color
.AlphaEnabled
&& key
->num_draw_buffers
== 0) {
471 /* if alpha test is enabled we need to emit at least one color */
472 key
->num_draw_buffers
= 1;
475 key
->inputs_available
= (inputs_available
& inputs_referenced
);
477 /* compute size of state key, ignoring unused texture units */
478 keySize
= sizeof(*key
) - sizeof(key
->unit
)
479 + key
->nr_enabled_units
* sizeof(key
->unit
[0]);
485 /** State used to build the fragment program:
487 class texenv_fragment_program
: public ir_factory
{
489 struct gl_shader_program
*shader_program
;
490 struct gl_shader
*shader
;
491 exec_list
*top_instructions
;
492 struct state_key
*state
;
494 ir_variable
*src_texture
[MAX_TEXTURE_COORD_UNITS
];
495 /* Reg containing each texture unit's sampled texture color,
499 /* Texcoord override from bumpmapping. */
500 ir_variable
*texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
502 /* Reg containing texcoord for a texture unit,
503 * needed for bump mapping, else undef.
506 ir_rvalue
*src_previous
; /**< Reg containing color from previous
507 * stage. May need to be decl'd.
512 get_current_attrib(texenv_fragment_program
*p
, GLuint attrib
)
514 ir_variable
*current
;
517 current
= p
->shader
->symbols
->get_variable("gl_CurrentAttribFragMESA");
519 current
->data
.max_array_access
= MAX2(current
->data
.max_array_access
, attrib
);
520 val
= new(p
->mem_ctx
) ir_dereference_variable(current
);
521 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(attrib
);
522 return new(p
->mem_ctx
) ir_dereference_array(val
, index
);
526 get_gl_Color(texenv_fragment_program
*p
)
528 if (p
->state
->inputs_available
& VARYING_BIT_COL0
) {
529 ir_variable
*var
= p
->shader
->symbols
->get_variable("gl_Color");
531 return new(p
->mem_ctx
) ir_dereference_variable(var
);
533 return get_current_attrib(p
, VERT_ATTRIB_COLOR0
);
538 get_source(texenv_fragment_program
*p
,
539 GLuint src
, GLuint unit
)
542 ir_dereference
*deref
;
546 return new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
556 return new(p
->mem_ctx
)
557 ir_dereference_variable(p
->src_texture
[src
- SRC_TEXTURE0
]);
560 var
= p
->shader
->symbols
->get_variable("gl_TextureEnvColor");
562 deref
= new(p
->mem_ctx
) ir_dereference_variable(var
);
563 var
->data
.max_array_access
= MAX2(var
->data
.max_array_access
, unit
);
564 return new(p
->mem_ctx
) ir_dereference_array(deref
,
565 new(p
->mem_ctx
) ir_constant(unit
));
567 case SRC_PRIMARY_COLOR
:
568 var
= p
->shader
->symbols
->get_variable("gl_Color");
570 return new(p
->mem_ctx
) ir_dereference_variable(var
);
573 return new(p
->mem_ctx
) ir_constant(0.0f
);
576 if (!p
->src_previous
) {
577 return get_gl_Color(p
);
579 return p
->src_previous
->clone(p
->mem_ctx
, NULL
);
589 emit_combine_source(texenv_fragment_program
*p
,
596 src
= get_source(p
, source
, unit
);
599 case OPR_ONE_MINUS_SRC_COLOR
:
600 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), src
);
603 return src
->type
->is_scalar() ? src
: swizzle_w(src
);
605 case OPR_ONE_MINUS_SRC_ALPHA
: {
606 ir_rvalue
*const scalar
= src
->type
->is_scalar() ? src
: swizzle_w(src
);
608 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), scalar
);
612 return new(p
->mem_ctx
) ir_constant(0.0f
);
614 return new(p
->mem_ctx
) ir_constant(1.0f
);
624 * Check if the RGB and Alpha sources and operands match for the given
625 * texture unit's combinder state. When the RGB and A sources and
626 * operands match, we can emit fewer instructions.
628 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
630 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
632 for (i
= 0; i
< numArgs
; i
++) {
633 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
636 switch (key
->unit
[unit
].OptA
[i
].Operand
) {
638 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
646 case OPR_ONE_MINUS_SRC_ALPHA
:
647 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
648 case OPR_ONE_MINUS_SRC_COLOR
:
649 case OPR_ONE_MINUS_SRC_ALPHA
:
656 return GL_FALSE
; /* impossible */
664 smear(ir_rvalue
*val
)
666 if (!val
->type
->is_scalar())
669 return swizzle_xxxx(val
);
673 emit_combine(texenv_fragment_program
*p
,
677 const struct mode_opt
*opt
)
679 ir_rvalue
*src
[MAX_COMBINER_TERMS
];
680 ir_rvalue
*tmp0
, *tmp1
;
683 assert(nr
<= MAX_COMBINER_TERMS
);
685 for (i
= 0; i
< nr
; i
++)
686 src
[i
] = emit_combine_source( p
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
693 return mul(src
[0], src
[1]);
696 return add(src
[0], src
[1]);
698 case MODE_ADD_SIGNED
:
699 return add(add(src
[0], src
[1]), new(p
->mem_ctx
) ir_constant(-0.5f
));
701 case MODE_INTERPOLATE
:
702 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
703 tmp0
= mul(src
[0], src
[2]);
704 tmp1
= mul(src
[1], sub(new(p
->mem_ctx
) ir_constant(1.0f
),
705 src
[2]->clone(p
->mem_ctx
, NULL
)));
706 return add(tmp0
, tmp1
);
709 return sub(src
[0], src
[1]);
712 case MODE_DOT3_RGBA_EXT
:
713 case MODE_DOT3_RGB_EXT
:
714 case MODE_DOT3_RGB
: {
715 tmp0
= mul(src
[0], new(p
->mem_ctx
) ir_constant(2.0f
));
716 tmp0
= add(tmp0
, new(p
->mem_ctx
) ir_constant(-1.0f
));
718 tmp1
= mul(src
[1], new(p
->mem_ctx
) ir_constant(2.0f
));
719 tmp1
= add(tmp1
, new(p
->mem_ctx
) ir_constant(-1.0f
));
721 return dot(swizzle_xyz(smear(tmp0
)), swizzle_xyz(smear(tmp1
)));
723 case MODE_MODULATE_ADD_ATI
:
724 return add(mul(src
[0], src
[2]), src
[1]);
726 case MODE_MODULATE_SIGNED_ADD_ATI
:
727 return add(add(mul(src
[0], src
[2]), src
[1]),
728 new(p
->mem_ctx
) ir_constant(-0.5f
));
730 case MODE_MODULATE_SUBTRACT_ATI
:
731 return sub(mul(src
[0], src
[2]), src
[1]);
733 case MODE_ADD_PRODUCTS
:
734 return add(mul(src
[0], src
[1]), mul(src
[2], src
[3]));
736 case MODE_ADD_PRODUCTS_SIGNED
:
737 return add(add(mul(src
[0], src
[1]), mul(src
[2], src
[3])),
738 new(p
->mem_ctx
) ir_constant(-0.5f
));
746 * Generate instructions for one texture unit's env/combiner mode.
749 emit_texenv(texenv_fragment_program
*p
, GLuint unit
)
751 const struct state_key
*key
= p
->state
;
752 GLboolean rgb_saturate
, alpha_saturate
;
753 GLuint rgb_shift
, alpha_shift
;
755 if (!key
->unit
[unit
].enabled
) {
756 return get_source(p
, SRC_PREVIOUS
, 0);
759 switch (key
->unit
[unit
].ModeRGB
) {
760 case MODE_DOT3_RGB_EXT
:
761 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
764 case MODE_DOT3_RGBA_EXT
:
769 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
770 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
774 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
775 * We don't want to clamp twice.
778 rgb_saturate
= GL_FALSE
; /* saturate after rgb shift */
779 else if (need_saturate(key
->unit
[unit
].ModeRGB
))
780 rgb_saturate
= GL_TRUE
;
782 rgb_saturate
= GL_FALSE
;
785 alpha_saturate
= GL_FALSE
; /* saturate after alpha shift */
786 else if (need_saturate(key
->unit
[unit
].ModeA
))
787 alpha_saturate
= GL_TRUE
;
789 alpha_saturate
= GL_FALSE
;
791 ir_variable
*temp_var
= p
->make_temp(glsl_type::vec4_type
, "texenv_combine");
792 ir_dereference
*deref
;
795 /* Emit the RGB and A combine ops
797 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
798 args_match(key
, unit
)) {
799 val
= emit_combine(p
, unit
,
800 key
->unit
[unit
].NumArgsRGB
,
801 key
->unit
[unit
].ModeRGB
,
802 key
->unit
[unit
].OptRGB
);
807 p
->emit(assign(temp_var
, val
));
809 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
810 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
811 ir_rvalue
*val
= emit_combine(p
, unit
,
812 key
->unit
[unit
].NumArgsRGB
,
813 key
->unit
[unit
].ModeRGB
,
814 key
->unit
[unit
].OptRGB
);
818 p
->emit(assign(temp_var
, val
));
821 /* Need to do something to stop from re-emitting identical
822 * argument calculations here:
824 val
= emit_combine(p
, unit
,
825 key
->unit
[unit
].NumArgsRGB
,
826 key
->unit
[unit
].ModeRGB
,
827 key
->unit
[unit
].OptRGB
);
828 val
= swizzle_xyz(smear(val
));
831 p
->emit(assign(temp_var
, val
, WRITEMASK_XYZ
));
833 val
= emit_combine(p
, unit
,
834 key
->unit
[unit
].NumArgsA
,
835 key
->unit
[unit
].ModeA
,
836 key
->unit
[unit
].OptA
);
837 val
= swizzle_w(smear(val
));
840 p
->emit(assign(temp_var
, val
, WRITEMASK_W
));
843 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
845 /* Deal with the final shift:
847 if (alpha_shift
|| rgb_shift
) {
850 if (rgb_shift
== alpha_shift
) {
851 shift
= new(p
->mem_ctx
) ir_constant((float)(1 << rgb_shift
));
854 ir_constant_data const_data
;
856 const_data
.f
[0] = float(1 << rgb_shift
);
857 const_data
.f
[1] = float(1 << rgb_shift
);
858 const_data
.f
[2] = float(1 << rgb_shift
);
859 const_data
.f
[3] = float(1 << alpha_shift
);
861 shift
= new(p
->mem_ctx
) ir_constant(glsl_type::vec4_type
,
865 return saturate(mul(deref
, shift
));
873 * Generate instruction for getting a texture source term.
875 static void load_texture( texenv_fragment_program
*p
, GLuint unit
)
877 ir_dereference
*deref
;
879 if (p
->src_texture
[unit
])
882 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
885 if (!(p
->state
->inputs_available
& (VARYING_BIT_TEX0
<< unit
))) {
886 texcoord
= get_current_attrib(p
, VERT_ATTRIB_TEX0
+ unit
);
887 } else if (p
->texcoord_tex
[unit
]) {
888 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(p
->texcoord_tex
[unit
]);
890 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
892 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
893 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(unit
);
894 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
895 tc_array
->data
.max_array_access
= MAX2(tc_array
->data
.max_array_access
, unit
);
898 if (!p
->state
->unit
[unit
].enabled
) {
899 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
901 p
->emit(p
->src_texture
[unit
]);
903 p
->emit(assign(p
->src_texture
[unit
], new(p
->mem_ctx
) ir_constant(0.0f
)));
907 const glsl_type
*sampler_type
= NULL
;
911 case TEXTURE_1D_INDEX
:
912 if (p
->state
->unit
[unit
].shadow
)
913 sampler_type
= glsl_type::sampler1DShadow_type
;
915 sampler_type
= glsl_type::sampler1D_type
;
918 case TEXTURE_1D_ARRAY_INDEX
:
919 if (p
->state
->unit
[unit
].shadow
)
920 sampler_type
= glsl_type::sampler1DArrayShadow_type
;
922 sampler_type
= glsl_type::sampler1DArray_type
;
925 case TEXTURE_2D_INDEX
:
926 if (p
->state
->unit
[unit
].shadow
)
927 sampler_type
= glsl_type::sampler2DShadow_type
;
929 sampler_type
= glsl_type::sampler2D_type
;
932 case TEXTURE_2D_ARRAY_INDEX
:
933 if (p
->state
->unit
[unit
].shadow
)
934 sampler_type
= glsl_type::sampler2DArrayShadow_type
;
936 sampler_type
= glsl_type::sampler2DArray_type
;
939 case TEXTURE_RECT_INDEX
:
940 if (p
->state
->unit
[unit
].shadow
)
941 sampler_type
= glsl_type::sampler2DRectShadow_type
;
943 sampler_type
= glsl_type::sampler2DRect_type
;
946 case TEXTURE_3D_INDEX
:
947 assert(!p
->state
->unit
[unit
].shadow
);
948 sampler_type
= glsl_type::sampler3D_type
;
951 case TEXTURE_CUBE_INDEX
:
952 if (p
->state
->unit
[unit
].shadow
)
953 sampler_type
= glsl_type::samplerCubeShadow_type
;
955 sampler_type
= glsl_type::samplerCube_type
;
958 case TEXTURE_EXTERNAL_INDEX
:
959 assert(!p
->state
->unit
[unit
].shadow
);
960 sampler_type
= glsl_type::samplerExternalOES_type
;
965 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
968 ir_texture
*tex
= new(p
->mem_ctx
) ir_texture(ir_tex
);
971 char *sampler_name
= ralloc_asprintf(p
->mem_ctx
, "sampler_%d", unit
);
972 ir_variable
*sampler
= new(p
->mem_ctx
) ir_variable(sampler_type
,
975 p
->top_instructions
->push_head(sampler
);
977 /* Set the texture unit for this sampler. The linker will pick this value
978 * up and do-the-right-thing.
980 * NOTE: The cast to int is important. Without it, the constant will have
981 * type uint, and things later on may get confused.
983 sampler
->constant_value
= new(p
->mem_ctx
) ir_constant(int(unit
));
985 deref
= new(p
->mem_ctx
) ir_dereference_variable(sampler
);
986 tex
->set_sampler(deref
, glsl_type::vec4_type
);
988 tex
->coordinate
= new(p
->mem_ctx
) ir_swizzle(texcoord
, 0, 1, 2, 3, coords
);
990 if (p
->state
->unit
[unit
].shadow
) {
991 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
992 tex
->shadow_comparitor
= new(p
->mem_ctx
) ir_swizzle(texcoord
,
998 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
999 tex
->projector
= swizzle_w(texcoord
);
1001 p
->emit(assign(p
->src_texture
[unit
], tex
));
1005 load_texenv_source(texenv_fragment_program
*p
,
1006 GLuint src
, GLuint unit
)
1010 load_texture(p
, unit
);
1021 load_texture(p
, src
- SRC_TEXTURE0
);
1025 /* not a texture src - do nothing */
1032 * Generate instructions for loading all texture source terms.
1035 load_texunit_sources( texenv_fragment_program
*p
, GLuint unit
)
1037 const struct state_key
*key
= p
->state
;
1040 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1041 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1044 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1045 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1052 * Applies the fog calculations.
1054 * This is basically like the ARB_fragment_prorgam fog options. Note
1055 * that ffvertex_prog.c produces fogcoord for us when
1056 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1059 emit_fog_instructions(texenv_fragment_program
*p
,
1060 ir_rvalue
*fragcolor
)
1062 struct state_key
*key
= p
->state
;
1063 ir_rvalue
*f
, *temp
;
1064 ir_variable
*params
, *oparams
;
1065 ir_variable
*fogcoord
;
1067 /* Temporary storage for the whole fog result. Fog calculations
1068 * only affect rgb so we're hanging on to the .a value of fragcolor
1071 ir_variable
*fog_result
= p
->make_temp(glsl_type::vec4_type
, "fog_result");
1072 p
->emit(assign(fog_result
, fragcolor
));
1074 fragcolor
= swizzle_xyz(fog_result
);
1076 oparams
= p
->shader
->symbols
->get_variable("gl_FogParamsOptimizedMESA");
1078 fogcoord
= p
->shader
->symbols
->get_variable("gl_FogFragCoord");
1080 params
= p
->shader
->symbols
->get_variable("gl_Fog");
1082 f
= new(p
->mem_ctx
) ir_dereference_variable(fogcoord
);
1084 ir_variable
*f_var
= p
->make_temp(glsl_type::float_type
, "fog_factor");
1086 switch (key
->fog_mode
) {
1088 /* f = (end - z) / (end - start)
1090 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1091 * (end / (end - start)) so we can generate a single MAD.
1093 f
= add(mul(f
, swizzle_x(oparams
)), swizzle_y(oparams
));
1096 /* f = e^(-(density * fogcoord))
1098 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1099 * use EXP2 which is generally the native instruction without
1100 * having to do any further math on the fog density uniform.
1102 f
= mul(f
, swizzle_z(oparams
));
1103 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1104 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1107 /* f = e^(-(density * fogcoord)^2)
1109 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1110 * can do this like FOG_EXP but with a squaring after the
1111 * multiply by density.
1113 ir_variable
*temp_var
= p
->make_temp(glsl_type::float_type
, "fog_temp");
1114 p
->emit(assign(temp_var
, mul(f
, swizzle_w(oparams
))));
1116 f
= mul(temp_var
, temp_var
);
1117 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1118 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1122 p
->emit(assign(f_var
, saturate(f
)));
1124 f
= sub(new(p
->mem_ctx
) ir_constant(1.0f
), f_var
);
1125 temp
= new(p
->mem_ctx
) ir_dereference_variable(params
);
1126 temp
= new(p
->mem_ctx
) ir_dereference_record(temp
, "color");
1127 temp
= mul(swizzle_xyz(temp
), f
);
1129 p
->emit(assign(fog_result
, add(temp
, mul(fragcolor
, f_var
)), WRITEMASK_XYZ
));
1131 return new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1135 emit_instructions(texenv_fragment_program
*p
)
1137 struct state_key
*key
= p
->state
;
1140 if (key
->enabled_units
) {
1141 /* First pass - to support texture_env_crossbar, first identify
1142 * all referenced texture sources and emit texld instructions
1145 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1146 if (key
->unit
[unit
].enabled
) {
1147 load_texunit_sources(p
, unit
);
1150 /* Second pass - emit combine instructions to build final color:
1152 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1153 if (key
->unit
[unit
].enabled
) {
1154 p
->src_previous
= emit_texenv(p
, unit
);
1159 ir_rvalue
*cf
= get_source(p
, SRC_PREVIOUS
, 0);
1161 if (key
->separate_specular
) {
1162 ir_variable
*spec_result
= p
->make_temp(glsl_type::vec4_type
,
1164 p
->emit(assign(spec_result
, cf
));
1166 ir_rvalue
*secondary
;
1167 if (p
->state
->inputs_available
& VARYING_BIT_COL1
) {
1169 p
->shader
->symbols
->get_variable("gl_SecondaryColor");
1171 secondary
= swizzle_xyz(var
);
1173 secondary
= swizzle_xyz(get_current_attrib(p
, VERT_ATTRIB_COLOR1
));
1176 p
->emit(assign(spec_result
, add(swizzle_xyz(spec_result
), secondary
),
1179 cf
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1182 if (key
->fog_enabled
) {
1183 cf
= emit_fog_instructions(p
, cf
);
1186 ir_variable
*frag_color
= p
->shader
->symbols
->get_variable("gl_FragColor");
1188 p
->emit(assign(frag_color
, cf
));
1192 * Generate a new fragment program which implements the context's
1193 * current texture env/combine mode.
1195 static struct gl_shader_program
*
1196 create_new_program(struct gl_context
*ctx
, struct state_key
*key
)
1198 texenv_fragment_program p
;
1200 _mesa_glsl_parse_state
*state
;
1202 p
.mem_ctx
= ralloc_context(NULL
);
1203 p
.shader
= ctx
->Driver
.NewShader(ctx
, 0, GL_FRAGMENT_SHADER
);
1204 p
.shader
->ir
= new(p
.shader
) exec_list
;
1205 state
= new(p
.shader
) _mesa_glsl_parse_state(ctx
, MESA_SHADER_FRAGMENT
,
1207 p
.shader
->symbols
= state
->symbols
;
1208 p
.top_instructions
= p
.shader
->ir
;
1209 p
.instructions
= p
.shader
->ir
;
1211 p
.shader_program
= ctx
->Driver
.NewShaderProgram(0);
1213 /* Tell the linker to ignore the fact that we're building a
1214 * separate shader, in case we're in a GLES2 context that would
1215 * normally reject that. The real problem is that we're building a
1216 * fixed function program in a GLES2 context at all, but that's a
1217 * big mess to clean up.
1219 p
.shader_program
->SeparateShader
= GL_TRUE
;
1221 state
->language_version
= 130;
1222 state
->es_shader
= false;
1223 if (_mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
)
1224 state
->OES_EGL_image_external_enable
= true;
1225 _mesa_glsl_initialize_types(state
);
1226 _mesa_glsl_initialize_variables(p
.instructions
, state
);
1228 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1229 p
.src_texture
[unit
] = NULL
;
1230 p
.texcoord_tex
[unit
] = NULL
;
1233 p
.src_previous
= NULL
;
1235 ir_function
*main_f
= new(p
.mem_ctx
) ir_function("main");
1237 state
->symbols
->add_function(main_f
);
1239 ir_function_signature
*main_sig
=
1240 new(p
.mem_ctx
) ir_function_signature(glsl_type::void_type
);
1241 main_sig
->is_defined
= true;
1242 main_f
->add_signature(main_sig
);
1244 p
.instructions
= &main_sig
->body
;
1245 if (key
->num_draw_buffers
)
1246 emit_instructions(&p
);
1248 validate_ir_tree(p
.shader
->ir
);
1250 const struct gl_shader_compiler_options
*options
=
1251 &ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_FRAGMENT
];
1253 while (do_common_optimization(p
.shader
->ir
, false, false, options
,
1254 ctx
->Const
.NativeIntegers
))
1256 reparent_ir(p
.shader
->ir
, p
.shader
->ir
);
1258 p
.shader
->CompileStatus
= true;
1259 p
.shader
->Version
= state
->language_version
;
1260 p
.shader
->uses_builtin_functions
= state
->uses_builtin_functions
;
1261 p
.shader_program
->Shaders
=
1262 (gl_shader
**)malloc(sizeof(*p
.shader_program
->Shaders
));
1263 p
.shader_program
->Shaders
[0] = p
.shader
;
1264 p
.shader_program
->NumShaders
= 1;
1266 _mesa_glsl_link_shader(ctx
, p
.shader_program
);
1268 if (!p
.shader_program
->LinkStatus
)
1269 _mesa_problem(ctx
, "Failed to link fixed function fragment shader: %s\n",
1270 p
.shader_program
->InfoLog
);
1272 ralloc_free(p
.mem_ctx
);
1273 return p
.shader_program
;
1279 * Return a fragment program which implements the current
1280 * fixed-function texture, fog and color-sum operations.
1282 struct gl_shader_program
*
1283 _mesa_get_fixed_func_fragment_program(struct gl_context
*ctx
)
1285 struct gl_shader_program
*shader_program
;
1286 struct state_key key
;
1289 keySize
= make_state_key(ctx
, &key
);
1291 shader_program
= (struct gl_shader_program
*)
1292 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1295 if (!shader_program
) {
1296 shader_program
= create_new_program(ctx
, &key
);
1298 _mesa_shader_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1299 &key
, keySize
, shader_program
);
1302 return shader_program
;