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"
53 using namespace ir_builder
;
56 * Note on texture units:
58 * The number of texture units supported by fixed-function fragment
59 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
60 * That's because there's a one-to-one correspondence between texture
61 * coordinates and samplers in fixed-function processing.
63 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
64 * sets of texcoords, so is fixed-function fragment processing.
66 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
70 struct texenvprog_cache_item
74 struct gl_shader_program
*data
;
75 struct texenvprog_cache_item
*next
;
79 texenv_doing_secondary_color(struct gl_context
*ctx
)
81 if (ctx
->Light
.Enabled
&&
82 (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
))
85 if (ctx
->Fog
.ColorSumEnabled
)
93 __extension__ GLubyte Source
:4; /**< SRC_x */
94 __extension__ GLubyte Operand
:3; /**< OPR_x */
96 GLubyte Source
; /**< SRC_x */
97 GLubyte Operand
; /**< OPR_x */
102 GLuint nr_enabled_units
:8;
103 GLuint enabled_units
:8;
104 GLuint separate_specular
:1;
105 GLuint fog_enabled
:1;
106 GLuint fog_mode
:2; /**< FOG_x */
107 GLuint inputs_available
:12;
108 GLuint num_draw_buffers
:4;
110 /* NOTE: This array of structs must be last! (see "keySize" below) */
113 GLuint source_index
:4; /**< TEXTURE_x_INDEX */
115 GLuint ScaleShiftRGB
:2;
116 GLuint ScaleShiftA
:2;
118 GLuint NumArgsRGB
:3; /**< up to MAX_COMBINER_TERMS */
119 GLuint ModeRGB
:5; /**< MODE_x */
121 GLuint NumArgsA
:3; /**< up to MAX_COMBINER_TERMS */
122 GLuint ModeA
:5; /**< MODE_x */
124 struct mode_opt OptRGB
[MAX_COMBINER_TERMS
];
125 struct mode_opt OptA
[MAX_COMBINER_TERMS
];
126 } unit
[MAX_TEXTURE_UNITS
];
132 #define FOG_UNKNOWN 3
134 static GLuint
translate_fog_mode( GLenum mode
)
137 case GL_LINEAR
: return FOG_LINEAR
;
138 case GL_EXP
: return FOG_EXP
;
139 case GL_EXP2
: return FOG_EXP2
;
140 default: return FOG_UNKNOWN
;
144 #define OPR_SRC_COLOR 0
145 #define OPR_ONE_MINUS_SRC_COLOR 1
146 #define OPR_SRC_ALPHA 2
147 #define OPR_ONE_MINUS_SRC_ALPHA 3
150 #define OPR_UNKNOWN 7
152 static GLuint
translate_operand( GLenum operand
)
155 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
156 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
157 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
158 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
159 case GL_ZERO
: return OPR_ZERO
;
160 case GL_ONE
: return OPR_ONE
;
167 #define SRC_TEXTURE 0
168 #define SRC_TEXTURE0 1
169 #define SRC_TEXTURE1 2
170 #define SRC_TEXTURE2 3
171 #define SRC_TEXTURE3 4
172 #define SRC_TEXTURE4 5
173 #define SRC_TEXTURE5 6
174 #define SRC_TEXTURE6 7
175 #define SRC_TEXTURE7 8
176 #define SRC_CONSTANT 9
177 #define SRC_PRIMARY_COLOR 10
178 #define SRC_PREVIOUS 11
180 #define SRC_UNKNOWN 15
182 static GLuint
translate_source( GLenum src
)
185 case GL_TEXTURE
: return SRC_TEXTURE
;
193 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
194 case GL_CONSTANT
: return SRC_CONSTANT
;
195 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
196 case GL_PREVIOUS
: return SRC_PREVIOUS
;
205 #define MODE_REPLACE 0 /* r = a0 */
206 #define MODE_MODULATE 1 /* r = a0 * a1 */
207 #define MODE_ADD 2 /* r = a0 + a1 */
208 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
209 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
210 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
211 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
212 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
213 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
214 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
215 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
216 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
217 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
218 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
219 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
220 #define MODE_UNKNOWN 16
223 * Translate GL combiner state into a MODE_x value
225 static GLuint
translate_mode( GLenum envMode
, GLenum mode
)
228 case GL_REPLACE
: return MODE_REPLACE
;
229 case GL_MODULATE
: return MODE_MODULATE
;
231 if (envMode
== GL_COMBINE4_NV
)
232 return MODE_ADD_PRODUCTS
;
236 if (envMode
== GL_COMBINE4_NV
)
237 return MODE_ADD_PRODUCTS_SIGNED
;
239 return MODE_ADD_SIGNED
;
240 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
241 case GL_SUBTRACT
: return MODE_SUBTRACT
;
242 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
243 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
244 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
245 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
246 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
247 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
248 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
257 * Do we need to clamp the results of the given texture env/combine mode?
258 * If the inputs to the mode are in [0,1] we don't always have to clamp
262 need_saturate( GLuint mode
)
267 case MODE_INTERPOLATE
:
270 case MODE_ADD_SIGNED
:
273 case MODE_DOT3_RGB_EXT
:
275 case MODE_DOT3_RGBA_EXT
:
276 case MODE_MODULATE_ADD_ATI
:
277 case MODE_MODULATE_SIGNED_ADD_ATI
:
278 case MODE_MODULATE_SUBTRACT_ATI
:
279 case MODE_ADD_PRODUCTS
:
280 case MODE_ADD_PRODUCTS_SIGNED
:
288 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
291 * Identify all possible varying inputs. The fragment program will
292 * never reference non-varying inputs, but will track them via state
295 * This function figures out all the inputs that the fragment program
296 * has access to. The bitmask is later reduced to just those which
297 * are actually referenced.
299 static GLbitfield
get_fp_input_mask( struct gl_context
*ctx
)
302 const GLboolean vertexShader
=
303 (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] &&
304 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->LinkStatus
&&
305 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->_LinkedShaders
[MESA_SHADER_VERTEX
]);
306 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
307 GLbitfield fp_inputs
= 0x0;
309 if (ctx
->VertexProgram
._Overriden
) {
310 /* Somebody's messing with the vertex program and we don't have
311 * a clue what's happening. Assume that it could be producing
312 * all possible outputs.
316 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
317 /* _NEW_RENDERMODE */
318 fp_inputs
= (VARYING_BIT_COL0
| VARYING_BIT_TEX0
);
320 else if (!(vertexProgram
|| vertexShader
)) {
321 /* Fixed function vertex logic */
322 /* _NEW_VARYING_VP_INPUTS */
323 GLbitfield64 varying_inputs
= ctx
->varying_vp_inputs
;
325 /* These get generated in the setup routine regardless of the
329 if (ctx
->Point
.PointSprite
)
330 varying_inputs
|= VARYING_BITS_TEX_ANY
;
332 /* First look at what values may be computed by the generated
336 if (ctx
->Light
.Enabled
) {
337 fp_inputs
|= VARYING_BIT_COL0
;
339 if (texenv_doing_secondary_color(ctx
))
340 fp_inputs
|= VARYING_BIT_COL1
;
344 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
345 ctx
->Texture
._TexMatEnabled
) << VARYING_SLOT_TEX0
;
347 /* Then look at what might be varying as a result of enabled
350 if (varying_inputs
& VERT_BIT_COLOR0
)
351 fp_inputs
|= VARYING_BIT_COL0
;
352 if (varying_inputs
& VERT_BIT_COLOR1
)
353 fp_inputs
|= VARYING_BIT_COL1
;
355 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
356 << VARYING_SLOT_TEX0
);
360 /* calculate from vp->outputs */
361 struct gl_program
*vprog
;
362 GLbitfield64 vp_outputs
;
364 /* Choose GLSL vertex shader over ARB vertex program. Need this
365 * since vertex shader state validation comes after fragment state
366 * validation (see additional comments in state.c).
369 vprog
= ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
371 vprog
= &ctx
->VertexProgram
.Current
->Base
;
373 vp_outputs
= vprog
->OutputsWritten
;
375 /* These get generated in the setup routine regardless of the
379 if (ctx
->Point
.PointSprite
)
380 vp_outputs
|= VARYING_BITS_TEX_ANY
;
382 if (vp_outputs
& (1 << VARYING_SLOT_COL0
))
383 fp_inputs
|= VARYING_BIT_COL0
;
384 if (vp_outputs
& (1 << VARYING_SLOT_COL1
))
385 fp_inputs
|= VARYING_BIT_COL1
;
387 fp_inputs
|= (((vp_outputs
& VARYING_BITS_TEX_ANY
) >> VARYING_SLOT_TEX0
)
388 << VARYING_SLOT_TEX0
);
396 * Examine current texture environment state and generate a unique
397 * key to identify it.
399 static GLuint
make_state_key( struct gl_context
*ctx
, struct state_key
*key
)
402 GLbitfield inputs_referenced
= VARYING_BIT_COL0
;
403 const GLbitfield inputs_available
= get_fp_input_mask( ctx
);
406 memset(key
, 0, sizeof(*key
));
409 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
410 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
411 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
412 const struct gl_tex_env_combine_state
*comb
= texUnit
->_CurrentCombine
;
413 const struct gl_sampler_object
*samp
;
416 if (!texUnit
->_Current
|| !texUnit
->Enabled
)
419 samp
= _mesa_get_samplerobj(ctx
, i
);
420 format
= _mesa_texture_base_format(texObj
);
422 key
->unit
[i
].enabled
= 1;
423 key
->enabled_units
|= (1<<i
);
424 key
->nr_enabled_units
= i
+ 1;
425 inputs_referenced
|= VARYING_BIT_TEX(i
);
427 key
->unit
[i
].source_index
= _mesa_tex_target_to_index(ctx
,
430 key
->unit
[i
].shadow
=
431 ((samp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
432 ((format
== GL_DEPTH_COMPONENT
) ||
433 (format
== GL_DEPTH_STENCIL_EXT
)));
435 key
->unit
[i
].NumArgsRGB
= comb
->_NumArgsRGB
;
436 key
->unit
[i
].NumArgsA
= comb
->_NumArgsA
;
438 key
->unit
[i
].ModeRGB
=
439 translate_mode(texUnit
->EnvMode
, comb
->ModeRGB
);
441 translate_mode(texUnit
->EnvMode
, comb
->ModeA
);
443 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
444 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
446 for (j
= 0; j
< MAX_COMBINER_TERMS
; j
++) {
447 key
->unit
[i
].OptRGB
[j
].Operand
= translate_operand(comb
->OperandRGB
[j
]);
448 key
->unit
[i
].OptA
[j
].Operand
= translate_operand(comb
->OperandA
[j
]);
449 key
->unit
[i
].OptRGB
[j
].Source
= translate_source(comb
->SourceRGB
[j
]);
450 key
->unit
[i
].OptA
[j
].Source
= translate_source(comb
->SourceA
[j
]);
454 /* _NEW_LIGHT | _NEW_FOG */
455 if (texenv_doing_secondary_color(ctx
)) {
456 key
->separate_specular
= 1;
457 inputs_referenced
|= VARYING_BIT_COL1
;
461 if (ctx
->Fog
.Enabled
) {
462 key
->fog_enabled
= 1;
463 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
464 inputs_referenced
|= VARYING_BIT_FOGC
; /* maybe */
468 key
->num_draw_buffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
471 if (ctx
->Color
.AlphaEnabled
&& key
->num_draw_buffers
== 0) {
472 /* if alpha test is enabled we need to emit at least one color */
473 key
->num_draw_buffers
= 1;
476 key
->inputs_available
= (inputs_available
& inputs_referenced
);
478 /* compute size of state key, ignoring unused texture units */
479 keySize
= sizeof(*key
) - sizeof(key
->unit
)
480 + key
->nr_enabled_units
* sizeof(key
->unit
[0]);
486 /** State used to build the fragment program:
488 class texenv_fragment_program
: public ir_factory
{
490 struct gl_shader_program
*shader_program
;
491 struct gl_shader
*shader
;
492 exec_list
*top_instructions
;
493 struct state_key
*state
;
495 ir_variable
*src_texture
[MAX_TEXTURE_COORD_UNITS
];
496 /* Reg containing each texture unit's sampled texture color,
500 /* Texcoord override from bumpmapping. */
501 ir_variable
*texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
503 /* Reg containing texcoord for a texture unit,
504 * needed for bump mapping, else undef.
507 ir_rvalue
*src_previous
; /**< Reg containing color from previous
508 * stage. May need to be decl'd.
513 get_current_attrib(texenv_fragment_program
*p
, GLuint attrib
)
515 ir_variable
*current
;
518 current
= p
->shader
->symbols
->get_variable("gl_CurrentAttribFragMESA");
520 current
->data
.max_array_access
= MAX2(current
->data
.max_array_access
, attrib
);
521 val
= new(p
->mem_ctx
) ir_dereference_variable(current
);
522 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(attrib
);
523 return new(p
->mem_ctx
) ir_dereference_array(val
, index
);
527 get_gl_Color(texenv_fragment_program
*p
)
529 if (p
->state
->inputs_available
& VARYING_BIT_COL0
) {
530 ir_variable
*var
= p
->shader
->symbols
->get_variable("gl_Color");
532 return new(p
->mem_ctx
) ir_dereference_variable(var
);
534 return get_current_attrib(p
, VERT_ATTRIB_COLOR0
);
539 get_source(texenv_fragment_program
*p
,
540 GLuint src
, GLuint unit
)
543 ir_dereference
*deref
;
547 return new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
557 return new(p
->mem_ctx
)
558 ir_dereference_variable(p
->src_texture
[src
- SRC_TEXTURE0
]);
561 var
= p
->shader
->symbols
->get_variable("gl_TextureEnvColor");
563 deref
= new(p
->mem_ctx
) ir_dereference_variable(var
);
564 var
->data
.max_array_access
= MAX2(var
->data
.max_array_access
, unit
);
565 return new(p
->mem_ctx
) ir_dereference_array(deref
,
566 new(p
->mem_ctx
) ir_constant(unit
));
568 case SRC_PRIMARY_COLOR
:
569 var
= p
->shader
->symbols
->get_variable("gl_Color");
571 return new(p
->mem_ctx
) ir_dereference_variable(var
);
574 return new(p
->mem_ctx
) ir_constant(0.0f
);
577 if (!p
->src_previous
) {
578 return get_gl_Color(p
);
580 return p
->src_previous
->clone(p
->mem_ctx
, NULL
);
590 emit_combine_source(texenv_fragment_program
*p
,
597 src
= get_source(p
, source
, unit
);
600 case OPR_ONE_MINUS_SRC_COLOR
:
601 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), src
);
604 return src
->type
->is_scalar() ? src
: swizzle_w(src
);
606 case OPR_ONE_MINUS_SRC_ALPHA
: {
607 ir_rvalue
*const scalar
= src
->type
->is_scalar() ? src
: swizzle_w(src
);
609 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), scalar
);
613 return new(p
->mem_ctx
) ir_constant(0.0f
);
615 return new(p
->mem_ctx
) ir_constant(1.0f
);
625 * Check if the RGB and Alpha sources and operands match for the given
626 * texture unit's combinder state. When the RGB and A sources and
627 * operands match, we can emit fewer instructions.
629 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
631 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
633 for (i
= 0; i
< numArgs
; i
++) {
634 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
637 switch (key
->unit
[unit
].OptA
[i
].Operand
) {
639 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
647 case OPR_ONE_MINUS_SRC_ALPHA
:
648 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
649 case OPR_ONE_MINUS_SRC_COLOR
:
650 case OPR_ONE_MINUS_SRC_ALPHA
:
657 return GL_FALSE
; /* impossible */
665 smear(ir_rvalue
*val
)
667 if (!val
->type
->is_scalar())
670 return swizzle_xxxx(val
);
674 emit_combine(texenv_fragment_program
*p
,
678 const struct mode_opt
*opt
)
680 ir_rvalue
*src
[MAX_COMBINER_TERMS
];
681 ir_rvalue
*tmp0
, *tmp1
;
684 assert(nr
<= MAX_COMBINER_TERMS
);
686 for (i
= 0; i
< nr
; i
++)
687 src
[i
] = emit_combine_source( p
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
694 return mul(src
[0], src
[1]);
697 return add(src
[0], src
[1]);
699 case MODE_ADD_SIGNED
:
700 return add(add(src
[0], src
[1]), new(p
->mem_ctx
) ir_constant(-0.5f
));
702 case MODE_INTERPOLATE
:
703 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
704 tmp0
= mul(src
[0], src
[2]);
705 tmp1
= mul(src
[1], sub(new(p
->mem_ctx
) ir_constant(1.0f
),
706 src
[2]->clone(p
->mem_ctx
, NULL
)));
707 return add(tmp0
, tmp1
);
710 return sub(src
[0], src
[1]);
713 case MODE_DOT3_RGBA_EXT
:
714 case MODE_DOT3_RGB_EXT
:
715 case MODE_DOT3_RGB
: {
716 tmp0
= mul(src
[0], new(p
->mem_ctx
) ir_constant(2.0f
));
717 tmp0
= add(tmp0
, new(p
->mem_ctx
) ir_constant(-1.0f
));
719 tmp1
= mul(src
[1], new(p
->mem_ctx
) ir_constant(2.0f
));
720 tmp1
= add(tmp1
, new(p
->mem_ctx
) ir_constant(-1.0f
));
722 return dot(swizzle_xyz(smear(tmp0
)), swizzle_xyz(smear(tmp1
)));
724 case MODE_MODULATE_ADD_ATI
:
725 return add(mul(src
[0], src
[2]), src
[1]);
727 case MODE_MODULATE_SIGNED_ADD_ATI
:
728 return add(add(mul(src
[0], src
[2]), src
[1]),
729 new(p
->mem_ctx
) ir_constant(-0.5f
));
731 case MODE_MODULATE_SUBTRACT_ATI
:
732 return sub(mul(src
[0], src
[2]), src
[1]);
734 case MODE_ADD_PRODUCTS
:
735 return add(mul(src
[0], src
[1]), mul(src
[2], src
[3]));
737 case MODE_ADD_PRODUCTS_SIGNED
:
738 return add(add(mul(src
[0], src
[1]), mul(src
[2], src
[3])),
739 new(p
->mem_ctx
) ir_constant(-0.5f
));
747 * Generate instructions for one texture unit's env/combiner mode.
750 emit_texenv(texenv_fragment_program
*p
, GLuint unit
)
752 const struct state_key
*key
= p
->state
;
753 GLboolean rgb_saturate
, alpha_saturate
;
754 GLuint rgb_shift
, alpha_shift
;
756 if (!key
->unit
[unit
].enabled
) {
757 return get_source(p
, SRC_PREVIOUS
, 0);
760 switch (key
->unit
[unit
].ModeRGB
) {
761 case MODE_DOT3_RGB_EXT
:
762 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
765 case MODE_DOT3_RGBA_EXT
:
770 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
771 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
775 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
776 * We don't want to clamp twice.
779 rgb_saturate
= GL_FALSE
; /* saturate after rgb shift */
780 else if (need_saturate(key
->unit
[unit
].ModeRGB
))
781 rgb_saturate
= GL_TRUE
;
783 rgb_saturate
= GL_FALSE
;
786 alpha_saturate
= GL_FALSE
; /* saturate after alpha shift */
787 else if (need_saturate(key
->unit
[unit
].ModeA
))
788 alpha_saturate
= GL_TRUE
;
790 alpha_saturate
= GL_FALSE
;
792 ir_variable
*temp_var
= p
->make_temp(glsl_type::vec4_type
, "texenv_combine");
793 ir_dereference
*deref
;
796 /* Emit the RGB and A combine ops
798 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
799 args_match(key
, unit
)) {
800 val
= emit_combine(p
, unit
,
801 key
->unit
[unit
].NumArgsRGB
,
802 key
->unit
[unit
].ModeRGB
,
803 key
->unit
[unit
].OptRGB
);
808 p
->emit(assign(temp_var
, val
));
810 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
811 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
812 ir_rvalue
*val
= emit_combine(p
, unit
,
813 key
->unit
[unit
].NumArgsRGB
,
814 key
->unit
[unit
].ModeRGB
,
815 key
->unit
[unit
].OptRGB
);
819 p
->emit(assign(temp_var
, val
));
822 /* Need to do something to stop from re-emitting identical
823 * argument calculations here:
825 val
= emit_combine(p
, unit
,
826 key
->unit
[unit
].NumArgsRGB
,
827 key
->unit
[unit
].ModeRGB
,
828 key
->unit
[unit
].OptRGB
);
829 val
= swizzle_xyz(smear(val
));
832 p
->emit(assign(temp_var
, val
, WRITEMASK_XYZ
));
834 val
= emit_combine(p
, unit
,
835 key
->unit
[unit
].NumArgsA
,
836 key
->unit
[unit
].ModeA
,
837 key
->unit
[unit
].OptA
);
838 val
= swizzle_w(smear(val
));
841 p
->emit(assign(temp_var
, val
, WRITEMASK_W
));
844 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
846 /* Deal with the final shift:
848 if (alpha_shift
|| rgb_shift
) {
851 if (rgb_shift
== alpha_shift
) {
852 shift
= new(p
->mem_ctx
) ir_constant((float)(1 << rgb_shift
));
855 ir_constant_data const_data
;
857 const_data
.f
[0] = float(1 << rgb_shift
);
858 const_data
.f
[1] = float(1 << rgb_shift
);
859 const_data
.f
[2] = float(1 << rgb_shift
);
860 const_data
.f
[3] = float(1 << alpha_shift
);
862 shift
= new(p
->mem_ctx
) ir_constant(glsl_type::vec4_type
,
866 return saturate(mul(deref
, shift
));
874 * Generate instruction for getting a texture source term.
876 static void load_texture( texenv_fragment_program
*p
, GLuint unit
)
878 ir_dereference
*deref
;
880 if (p
->src_texture
[unit
])
883 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
886 if (!(p
->state
->inputs_available
& (VARYING_BIT_TEX0
<< unit
))) {
887 texcoord
= get_current_attrib(p
, VERT_ATTRIB_TEX0
+ unit
);
888 } else if (p
->texcoord_tex
[unit
]) {
889 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(p
->texcoord_tex
[unit
]);
891 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
893 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
894 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(unit
);
895 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
896 tc_array
->data
.max_array_access
= MAX2(tc_array
->data
.max_array_access
, unit
);
899 if (!p
->state
->unit
[unit
].enabled
) {
900 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
902 p
->emit(p
->src_texture
[unit
]);
904 p
->emit(assign(p
->src_texture
[unit
], new(p
->mem_ctx
) ir_constant(0.0f
)));
908 const glsl_type
*sampler_type
= NULL
;
912 case TEXTURE_1D_INDEX
:
913 if (p
->state
->unit
[unit
].shadow
)
914 sampler_type
= glsl_type::sampler1DShadow_type
;
916 sampler_type
= glsl_type::sampler1D_type
;
919 case TEXTURE_1D_ARRAY_INDEX
:
920 if (p
->state
->unit
[unit
].shadow
)
921 sampler_type
= glsl_type::sampler1DArrayShadow_type
;
923 sampler_type
= glsl_type::sampler1DArray_type
;
926 case TEXTURE_2D_INDEX
:
927 if (p
->state
->unit
[unit
].shadow
)
928 sampler_type
= glsl_type::sampler2DShadow_type
;
930 sampler_type
= glsl_type::sampler2D_type
;
933 case TEXTURE_2D_ARRAY_INDEX
:
934 if (p
->state
->unit
[unit
].shadow
)
935 sampler_type
= glsl_type::sampler2DArrayShadow_type
;
937 sampler_type
= glsl_type::sampler2DArray_type
;
940 case TEXTURE_RECT_INDEX
:
941 if (p
->state
->unit
[unit
].shadow
)
942 sampler_type
= glsl_type::sampler2DRectShadow_type
;
944 sampler_type
= glsl_type::sampler2DRect_type
;
947 case TEXTURE_3D_INDEX
:
948 assert(!p
->state
->unit
[unit
].shadow
);
949 sampler_type
= glsl_type::sampler3D_type
;
952 case TEXTURE_CUBE_INDEX
:
953 if (p
->state
->unit
[unit
].shadow
)
954 sampler_type
= glsl_type::samplerCubeShadow_type
;
956 sampler_type
= glsl_type::samplerCube_type
;
959 case TEXTURE_EXTERNAL_INDEX
:
960 assert(!p
->state
->unit
[unit
].shadow
);
961 sampler_type
= glsl_type::samplerExternalOES_type
;
966 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
969 ir_texture
*tex
= new(p
->mem_ctx
) ir_texture(ir_tex
);
972 char *sampler_name
= ralloc_asprintf(p
->mem_ctx
, "sampler_%d", unit
);
973 ir_variable
*sampler
= new(p
->mem_ctx
) ir_variable(sampler_type
,
976 p
->top_instructions
->push_head(sampler
);
978 /* Set the texture unit for this sampler in the same way that
979 * layout(binding=X) would.
981 sampler
->data
.explicit_binding
= true;
982 sampler
->data
.binding
= unit
;
984 deref
= new(p
->mem_ctx
) ir_dereference_variable(sampler
);
985 tex
->set_sampler(deref
, glsl_type::vec4_type
);
987 tex
->coordinate
= new(p
->mem_ctx
) ir_swizzle(texcoord
, 0, 1, 2, 3, coords
);
989 if (p
->state
->unit
[unit
].shadow
) {
990 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
991 tex
->shadow_comparitor
= new(p
->mem_ctx
) ir_swizzle(texcoord
,
997 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
998 tex
->projector
= swizzle_w(texcoord
);
1000 p
->emit(assign(p
->src_texture
[unit
], tex
));
1004 load_texenv_source(texenv_fragment_program
*p
,
1005 GLuint src
, GLuint unit
)
1009 load_texture(p
, unit
);
1020 load_texture(p
, src
- SRC_TEXTURE0
);
1024 /* not a texture src - do nothing */
1031 * Generate instructions for loading all texture source terms.
1034 load_texunit_sources( texenv_fragment_program
*p
, GLuint unit
)
1036 const struct state_key
*key
= p
->state
;
1039 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1040 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1043 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1044 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1051 * Applies the fog calculations.
1053 * This is basically like the ARB_fragment_prorgam fog options. Note
1054 * that ffvertex_prog.c produces fogcoord for us when
1055 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1058 emit_fog_instructions(texenv_fragment_program
*p
,
1059 ir_rvalue
*fragcolor
)
1061 struct state_key
*key
= p
->state
;
1062 ir_rvalue
*f
, *temp
;
1063 ir_variable
*params
, *oparams
;
1064 ir_variable
*fogcoord
;
1066 /* Temporary storage for the whole fog result. Fog calculations
1067 * only affect rgb so we're hanging on to the .a value of fragcolor
1070 ir_variable
*fog_result
= p
->make_temp(glsl_type::vec4_type
, "fog_result");
1071 p
->emit(assign(fog_result
, fragcolor
));
1073 fragcolor
= swizzle_xyz(fog_result
);
1075 oparams
= p
->shader
->symbols
->get_variable("gl_FogParamsOptimizedMESA");
1077 fogcoord
= p
->shader
->symbols
->get_variable("gl_FogFragCoord");
1079 params
= p
->shader
->symbols
->get_variable("gl_Fog");
1081 f
= new(p
->mem_ctx
) ir_dereference_variable(fogcoord
);
1083 ir_variable
*f_var
= p
->make_temp(glsl_type::float_type
, "fog_factor");
1085 switch (key
->fog_mode
) {
1087 /* f = (end - z) / (end - start)
1089 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1090 * (end / (end - start)) so we can generate a single MAD.
1092 f
= add(mul(f
, swizzle_x(oparams
)), swizzle_y(oparams
));
1095 /* f = e^(-(density * fogcoord))
1097 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1098 * use EXP2 which is generally the native instruction without
1099 * having to do any further math on the fog density uniform.
1101 f
= mul(f
, swizzle_z(oparams
));
1102 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1103 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1106 /* f = e^(-(density * fogcoord)^2)
1108 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1109 * can do this like FOG_EXP but with a squaring after the
1110 * multiply by density.
1112 ir_variable
*temp_var
= p
->make_temp(glsl_type::float_type
, "fog_temp");
1113 p
->emit(assign(temp_var
, mul(f
, swizzle_w(oparams
))));
1115 f
= mul(temp_var
, temp_var
);
1116 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1117 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1121 p
->emit(assign(f_var
, saturate(f
)));
1123 f
= sub(new(p
->mem_ctx
) ir_constant(1.0f
), f_var
);
1124 temp
= new(p
->mem_ctx
) ir_dereference_variable(params
);
1125 temp
= new(p
->mem_ctx
) ir_dereference_record(temp
, "color");
1126 temp
= mul(swizzle_xyz(temp
), f
);
1128 p
->emit(assign(fog_result
, add(temp
, mul(fragcolor
, f_var
)), WRITEMASK_XYZ
));
1130 return new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1134 emit_instructions(texenv_fragment_program
*p
)
1136 struct state_key
*key
= p
->state
;
1139 if (key
->enabled_units
) {
1140 /* First pass - to support texture_env_crossbar, first identify
1141 * all referenced texture sources and emit texld instructions
1144 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1145 if (key
->unit
[unit
].enabled
) {
1146 load_texunit_sources(p
, unit
);
1149 /* Second pass - emit combine instructions to build final color:
1151 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1152 if (key
->unit
[unit
].enabled
) {
1153 p
->src_previous
= emit_texenv(p
, unit
);
1158 ir_rvalue
*cf
= get_source(p
, SRC_PREVIOUS
, 0);
1160 if (key
->separate_specular
) {
1161 ir_variable
*spec_result
= p
->make_temp(glsl_type::vec4_type
,
1163 p
->emit(assign(spec_result
, cf
));
1165 ir_rvalue
*secondary
;
1166 if (p
->state
->inputs_available
& VARYING_BIT_COL1
) {
1168 p
->shader
->symbols
->get_variable("gl_SecondaryColor");
1170 secondary
= swizzle_xyz(var
);
1172 secondary
= swizzle_xyz(get_current_attrib(p
, VERT_ATTRIB_COLOR1
));
1175 p
->emit(assign(spec_result
, add(swizzle_xyz(spec_result
), secondary
),
1178 cf
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1181 if (key
->fog_enabled
) {
1182 cf
= emit_fog_instructions(p
, cf
);
1185 ir_variable
*frag_color
= p
->shader
->symbols
->get_variable("gl_FragColor");
1187 p
->emit(assign(frag_color
, cf
));
1191 * Generate a new fragment program which implements the context's
1192 * current texture env/combine mode.
1194 static struct gl_shader_program
*
1195 create_new_program(struct gl_context
*ctx
, struct state_key
*key
)
1197 texenv_fragment_program p
;
1199 _mesa_glsl_parse_state
*state
;
1201 p
.mem_ctx
= ralloc_context(NULL
);
1202 p
.shader
= ctx
->Driver
.NewShader(ctx
, 0, GL_FRAGMENT_SHADER
);
1203 p
.shader
->ir
= new(p
.shader
) exec_list
;
1204 state
= new(p
.shader
) _mesa_glsl_parse_state(ctx
, MESA_SHADER_FRAGMENT
,
1206 p
.shader
->symbols
= state
->symbols
;
1207 p
.top_instructions
= p
.shader
->ir
;
1208 p
.instructions
= p
.shader
->ir
;
1210 p
.shader_program
= _mesa_new_shader_program(0);
1212 /* Tell the linker to ignore the fact that we're building a
1213 * separate shader, in case we're in a GLES2 context that would
1214 * normally reject that. The real problem is that we're building a
1215 * fixed function program in a GLES2 context at all, but that's a
1216 * big mess to clean up.
1218 p
.shader_program
->SeparateShader
= GL_TRUE
;
1220 state
->language_version
= 130;
1221 state
->es_shader
= false;
1222 if (_mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
)
1223 state
->OES_EGL_image_external_enable
= true;
1224 _mesa_glsl_initialize_types(state
);
1225 _mesa_glsl_initialize_variables(p
.instructions
, state
);
1227 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1228 p
.src_texture
[unit
] = NULL
;
1229 p
.texcoord_tex
[unit
] = NULL
;
1232 p
.src_previous
= NULL
;
1234 ir_function
*main_f
= new(p
.mem_ctx
) ir_function("main");
1236 state
->symbols
->add_function(main_f
);
1238 ir_function_signature
*main_sig
=
1239 new(p
.mem_ctx
) ir_function_signature(glsl_type::void_type
);
1240 main_sig
->is_defined
= true;
1241 main_f
->add_signature(main_sig
);
1243 p
.instructions
= &main_sig
->body
;
1244 if (key
->num_draw_buffers
)
1245 emit_instructions(&p
);
1247 validate_ir_tree(p
.shader
->ir
);
1249 const struct gl_shader_compiler_options
*options
=
1250 &ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_FRAGMENT
];
1252 while (do_common_optimization(p
.shader
->ir
, false, false, options
,
1253 ctx
->Const
.NativeIntegers
))
1255 reparent_ir(p
.shader
->ir
, p
.shader
->ir
);
1257 p
.shader
->CompileStatus
= true;
1258 p
.shader
->Version
= state
->language_version
;
1259 p
.shader
->uses_builtin_functions
= state
->uses_builtin_functions
;
1260 p
.shader_program
->Shaders
=
1261 (gl_shader
**)malloc(sizeof(*p
.shader_program
->Shaders
));
1262 p
.shader_program
->Shaders
[0] = p
.shader
;
1263 p
.shader_program
->NumShaders
= 1;
1265 _mesa_glsl_link_shader(ctx
, p
.shader_program
);
1267 if (!p
.shader_program
->LinkStatus
)
1268 _mesa_problem(ctx
, "Failed to link fixed function fragment shader: %s\n",
1269 p
.shader_program
->InfoLog
);
1271 ralloc_free(p
.mem_ctx
);
1272 return p
.shader_program
;
1278 * Return a fragment program which implements the current
1279 * fixed-function texture, fog and color-sum operations.
1281 struct gl_shader_program
*
1282 _mesa_get_fixed_func_fragment_program(struct gl_context
*ctx
)
1284 struct gl_shader_program
*shader_program
;
1285 struct state_key key
;
1288 keySize
= make_state_key(ctx
, &key
);
1290 shader_program
= (struct gl_shader_program
*)
1291 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1294 if (!shader_program
) {
1295 shader_program
= create_new_program(ctx
, &key
);
1297 _mesa_shader_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1298 &key
, keySize
, shader_program
);
1301 return shader_program
;