1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
5 * Copyright 2009 VMware, Inc. All Rights Reserved.
6 * Copyright © 2010-2011 Intel Corporation
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 **************************************************************************/
34 #include "main/context.h"
35 #include "main/macros.h"
36 #include "main/samplerobj.h"
37 #include "program/program.h"
38 #include "program/prog_parameter.h"
39 #include "program/prog_cache.h"
40 #include "program/prog_instruction.h"
41 #include "program/prog_print.h"
42 #include "program/prog_statevars.h"
43 #include "program/programopt.h"
44 #include "texenvprogram.h"
47 #include "main/uniforms.h"
48 #include "../glsl/glsl_types.h"
49 #include "../glsl/ir.h"
50 #include "../glsl/ir_builder.h"
51 #include "../glsl/glsl_symbol_table.h"
52 #include "../glsl/glsl_parser_extras.h"
53 #include "../glsl/ir_optimization.h"
54 #include "../program/ir_to_mesa.h"
56 using namespace ir_builder
;
59 * Note on texture units:
61 * The number of texture units supported by fixed-function fragment
62 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
63 * That's because there's a one-to-one correspondence between texture
64 * coordinates and samplers in fixed-function processing.
66 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
67 * sets of texcoords, so is fixed-function fragment processing.
69 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
73 struct texenvprog_cache_item
77 struct gl_shader_program
*data
;
78 struct texenvprog_cache_item
*next
;
82 texenv_doing_secondary_color(struct gl_context
*ctx
)
84 if (ctx
->Light
.Enabled
&&
85 (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
))
88 if (ctx
->Fog
.ColorSumEnabled
)
96 __extension__ GLubyte Source
:4; /**< SRC_x */
97 __extension__ GLubyte Operand
:3; /**< OPR_x */
99 GLubyte Source
; /**< SRC_x */
100 GLubyte Operand
; /**< OPR_x */
105 GLuint nr_enabled_units
:8;
106 GLuint enabled_units
:8;
107 GLuint separate_specular
:1;
108 GLuint fog_enabled
:1;
109 GLuint fog_mode
:2; /**< FOG_x */
110 GLuint inputs_available
:12;
111 GLuint num_draw_buffers
:4;
113 /* NOTE: This array of structs must be last! (see "keySize" below) */
116 GLuint source_index
:4; /**< TEXTURE_x_INDEX */
118 GLuint ScaleShiftRGB
:2;
119 GLuint ScaleShiftA
:2;
121 GLuint NumArgsRGB
:3; /**< up to MAX_COMBINER_TERMS */
122 GLuint ModeRGB
:5; /**< MODE_x */
124 GLuint NumArgsA
:3; /**< up to MAX_COMBINER_TERMS */
125 GLuint ModeA
:5; /**< MODE_x */
127 struct mode_opt OptRGB
[MAX_COMBINER_TERMS
];
128 struct mode_opt OptA
[MAX_COMBINER_TERMS
];
129 } unit
[MAX_TEXTURE_UNITS
];
135 #define FOG_UNKNOWN 3
137 static GLuint
translate_fog_mode( GLenum mode
)
140 case GL_LINEAR
: return FOG_LINEAR
;
141 case GL_EXP
: return FOG_EXP
;
142 case GL_EXP2
: return FOG_EXP2
;
143 default: return FOG_UNKNOWN
;
147 #define OPR_SRC_COLOR 0
148 #define OPR_ONE_MINUS_SRC_COLOR 1
149 #define OPR_SRC_ALPHA 2
150 #define OPR_ONE_MINUS_SRC_ALPHA 3
153 #define OPR_UNKNOWN 7
155 static GLuint
translate_operand( GLenum operand
)
158 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
159 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
160 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
161 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
162 case GL_ZERO
: return OPR_ZERO
;
163 case GL_ONE
: return OPR_ONE
;
170 #define SRC_TEXTURE 0
171 #define SRC_TEXTURE0 1
172 #define SRC_TEXTURE1 2
173 #define SRC_TEXTURE2 3
174 #define SRC_TEXTURE3 4
175 #define SRC_TEXTURE4 5
176 #define SRC_TEXTURE5 6
177 #define SRC_TEXTURE6 7
178 #define SRC_TEXTURE7 8
179 #define SRC_CONSTANT 9
180 #define SRC_PRIMARY_COLOR 10
181 #define SRC_PREVIOUS 11
183 #define SRC_UNKNOWN 15
185 static GLuint
translate_source( GLenum src
)
188 case GL_TEXTURE
: return SRC_TEXTURE
;
196 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
197 case GL_CONSTANT
: return SRC_CONSTANT
;
198 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
199 case GL_PREVIOUS
: return SRC_PREVIOUS
;
208 #define MODE_REPLACE 0 /* r = a0 */
209 #define MODE_MODULATE 1 /* r = a0 * a1 */
210 #define MODE_ADD 2 /* r = a0 + a1 */
211 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
212 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
213 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
214 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
215 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
216 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
217 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
218 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
219 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
220 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
221 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
222 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
223 #define MODE_BUMP_ENVMAP_ATI 15 /* special */
224 #define MODE_UNKNOWN 16
227 * Translate GL combiner state into a MODE_x value
229 static GLuint
translate_mode( GLenum envMode
, GLenum mode
)
232 case GL_REPLACE
: return MODE_REPLACE
;
233 case GL_MODULATE
: return MODE_MODULATE
;
235 if (envMode
== GL_COMBINE4_NV
)
236 return MODE_ADD_PRODUCTS
;
240 if (envMode
== GL_COMBINE4_NV
)
241 return MODE_ADD_PRODUCTS_SIGNED
;
243 return MODE_ADD_SIGNED
;
244 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
245 case GL_SUBTRACT
: return MODE_SUBTRACT
;
246 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
247 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
248 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
249 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
250 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
251 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
252 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
253 case GL_BUMP_ENVMAP_ATI
: return MODE_BUMP_ENVMAP_ATI
;
262 * Do we need to clamp the results of the given texture env/combine mode?
263 * If the inputs to the mode are in [0,1] we don't always have to clamp
267 need_saturate( GLuint mode
)
272 case MODE_INTERPOLATE
:
275 case MODE_ADD_SIGNED
:
278 case MODE_DOT3_RGB_EXT
:
280 case MODE_DOT3_RGBA_EXT
:
281 case MODE_MODULATE_ADD_ATI
:
282 case MODE_MODULATE_SIGNED_ADD_ATI
:
283 case MODE_MODULATE_SUBTRACT_ATI
:
284 case MODE_ADD_PRODUCTS
:
285 case MODE_ADD_PRODUCTS_SIGNED
:
286 case MODE_BUMP_ENVMAP_ATI
:
294 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
297 * Identify all possible varying inputs. The fragment program will
298 * never reference non-varying inputs, but will track them via state
301 * This function figures out all the inputs that the fragment program
302 * has access to. The bitmask is later reduced to just those which
303 * are actually referenced.
305 static GLbitfield
get_fp_input_mask( struct gl_context
*ctx
)
308 const GLboolean vertexShader
=
309 (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] &&
310 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->LinkStatus
&&
311 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->_LinkedShaders
[MESA_SHADER_VERTEX
]);
312 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
313 GLbitfield fp_inputs
= 0x0;
315 if (ctx
->VertexProgram
._Overriden
) {
316 /* Somebody's messing with the vertex program and we don't have
317 * a clue what's happening. Assume that it could be producing
318 * all possible outputs.
322 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
323 /* _NEW_RENDERMODE */
324 fp_inputs
= (VARYING_BIT_COL0
| VARYING_BIT_TEX0
);
326 else if (!(vertexProgram
|| vertexShader
)) {
327 /* Fixed function vertex logic */
328 /* _NEW_VARYING_VP_INPUTS */
329 GLbitfield64 varying_inputs
= ctx
->varying_vp_inputs
;
331 /* These get generated in the setup routine regardless of the
335 if (ctx
->Point
.PointSprite
)
336 varying_inputs
|= VARYING_BITS_TEX_ANY
;
338 /* First look at what values may be computed by the generated
342 if (ctx
->Light
.Enabled
) {
343 fp_inputs
|= VARYING_BIT_COL0
;
345 if (texenv_doing_secondary_color(ctx
))
346 fp_inputs
|= VARYING_BIT_COL1
;
350 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
351 ctx
->Texture
._TexMatEnabled
) << VARYING_SLOT_TEX0
;
353 /* Then look at what might be varying as a result of enabled
356 if (varying_inputs
& VERT_BIT_COLOR0
)
357 fp_inputs
|= VARYING_BIT_COL0
;
358 if (varying_inputs
& VERT_BIT_COLOR1
)
359 fp_inputs
|= VARYING_BIT_COL1
;
361 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
362 << VARYING_SLOT_TEX0
);
366 /* calculate from vp->outputs */
367 struct gl_program
*vprog
;
368 GLbitfield64 vp_outputs
;
370 /* Choose GLSL vertex shader over ARB vertex program. Need this
371 * since vertex shader state validation comes after fragment state
372 * validation (see additional comments in state.c).
375 vprog
= ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
]->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
377 vprog
= &ctx
->VertexProgram
.Current
->Base
;
379 vp_outputs
= vprog
->OutputsWritten
;
381 /* These get generated in the setup routine regardless of the
385 if (ctx
->Point
.PointSprite
)
386 vp_outputs
|= VARYING_BITS_TEX_ANY
;
388 if (vp_outputs
& (1 << VARYING_SLOT_COL0
))
389 fp_inputs
|= VARYING_BIT_COL0
;
390 if (vp_outputs
& (1 << VARYING_SLOT_COL1
))
391 fp_inputs
|= VARYING_BIT_COL1
;
393 fp_inputs
|= (((vp_outputs
& VARYING_BITS_TEX_ANY
) >> VARYING_SLOT_TEX0
)
394 << VARYING_SLOT_TEX0
);
402 * Examine current texture environment state and generate a unique
403 * key to identify it.
405 static GLuint
make_state_key( struct gl_context
*ctx
, struct state_key
*key
)
408 GLbitfield inputs_referenced
= VARYING_BIT_COL0
;
409 const GLbitfield inputs_available
= get_fp_input_mask( ctx
);
412 memset(key
, 0, sizeof(*key
));
415 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
416 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
417 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
418 const struct gl_tex_env_combine_state
*comb
= texUnit
->_CurrentCombine
;
419 const struct gl_sampler_object
*samp
;
422 if (!texUnit
->_Current
|| !texUnit
->Enabled
)
425 samp
= _mesa_get_samplerobj(ctx
, i
);
426 format
= texObj
->Image
[0][texObj
->BaseLevel
]->_BaseFormat
;
428 key
->unit
[i
].enabled
= 1;
429 key
->enabled_units
|= (1<<i
);
430 key
->nr_enabled_units
= i
+ 1;
431 inputs_referenced
|= VARYING_BIT_TEX(i
);
433 key
->unit
[i
].source_index
= _mesa_tex_target_to_index(ctx
,
436 key
->unit
[i
].shadow
=
437 ((samp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
438 ((format
== GL_DEPTH_COMPONENT
) ||
439 (format
== GL_DEPTH_STENCIL_EXT
)));
441 key
->unit
[i
].NumArgsRGB
= comb
->_NumArgsRGB
;
442 key
->unit
[i
].NumArgsA
= comb
->_NumArgsA
;
444 key
->unit
[i
].ModeRGB
=
445 translate_mode(texUnit
->EnvMode
, comb
->ModeRGB
);
447 translate_mode(texUnit
->EnvMode
, comb
->ModeA
);
449 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
450 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
452 for (j
= 0; j
< MAX_COMBINER_TERMS
; j
++) {
453 key
->unit
[i
].OptRGB
[j
].Operand
= translate_operand(comb
->OperandRGB
[j
]);
454 key
->unit
[i
].OptA
[j
].Operand
= translate_operand(comb
->OperandA
[j
]);
455 key
->unit
[i
].OptRGB
[j
].Source
= translate_source(comb
->SourceRGB
[j
]);
456 key
->unit
[i
].OptA
[j
].Source
= translate_source(comb
->SourceA
[j
]);
459 if (key
->unit
[i
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
460 /* requires some special translation */
461 key
->unit
[i
].NumArgsRGB
= 2;
462 key
->unit
[i
].ScaleShiftRGB
= 0;
463 key
->unit
[i
].OptRGB
[0].Operand
= OPR_SRC_COLOR
;
464 key
->unit
[i
].OptRGB
[0].Source
= SRC_TEXTURE
;
465 key
->unit
[i
].OptRGB
[1].Operand
= OPR_SRC_COLOR
;
466 key
->unit
[i
].OptRGB
[1].Source
= texUnit
->BumpTarget
- GL_TEXTURE0
+ SRC_TEXTURE0
;
470 /* _NEW_LIGHT | _NEW_FOG */
471 if (texenv_doing_secondary_color(ctx
)) {
472 key
->separate_specular
= 1;
473 inputs_referenced
|= VARYING_BIT_COL1
;
477 if (ctx
->Fog
.Enabled
) {
478 key
->fog_enabled
= 1;
479 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
480 inputs_referenced
|= VARYING_BIT_FOGC
; /* maybe */
484 key
->num_draw_buffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
487 if (ctx
->Color
.AlphaEnabled
&& key
->num_draw_buffers
== 0) {
488 /* if alpha test is enabled we need to emit at least one color */
489 key
->num_draw_buffers
= 1;
492 key
->inputs_available
= (inputs_available
& inputs_referenced
);
494 /* compute size of state key, ignoring unused texture units */
495 keySize
= sizeof(*key
) - sizeof(key
->unit
)
496 + key
->nr_enabled_units
* sizeof(key
->unit
[0]);
502 /** State used to build the fragment program:
504 class texenv_fragment_program
: public ir_factory
{
506 struct gl_shader_program
*shader_program
;
507 struct gl_shader
*shader
;
508 exec_list
*top_instructions
;
509 struct state_key
*state
;
511 ir_variable
*src_texture
[MAX_TEXTURE_COORD_UNITS
];
512 /* Reg containing each texture unit's sampled texture color,
516 /* Texcoord override from bumpmapping. */
517 ir_variable
*texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
519 /* Reg containing texcoord for a texture unit,
520 * needed for bump mapping, else undef.
523 ir_rvalue
*src_previous
; /**< Reg containing color from previous
524 * stage. May need to be decl'd.
529 get_current_attrib(texenv_fragment_program
*p
, GLuint attrib
)
531 ir_variable
*current
;
534 current
= p
->shader
->symbols
->get_variable("gl_CurrentAttribFragMESA");
536 current
->data
.max_array_access
= MAX2(current
->data
.max_array_access
, attrib
);
537 val
= new(p
->mem_ctx
) ir_dereference_variable(current
);
538 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(attrib
);
539 return new(p
->mem_ctx
) ir_dereference_array(val
, index
);
543 get_gl_Color(texenv_fragment_program
*p
)
545 if (p
->state
->inputs_available
& VARYING_BIT_COL0
) {
546 ir_variable
*var
= p
->shader
->symbols
->get_variable("gl_Color");
548 return new(p
->mem_ctx
) ir_dereference_variable(var
);
550 return get_current_attrib(p
, VERT_ATTRIB_COLOR0
);
555 get_source(texenv_fragment_program
*p
,
556 GLuint src
, GLuint unit
)
559 ir_dereference
*deref
;
563 return new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
573 return new(p
->mem_ctx
)
574 ir_dereference_variable(p
->src_texture
[src
- SRC_TEXTURE0
]);
577 var
= p
->shader
->symbols
->get_variable("gl_TextureEnvColor");
579 deref
= new(p
->mem_ctx
) ir_dereference_variable(var
);
580 var
->data
.max_array_access
= MAX2(var
->data
.max_array_access
, unit
);
581 return new(p
->mem_ctx
) ir_dereference_array(deref
,
582 new(p
->mem_ctx
) ir_constant(unit
));
584 case SRC_PRIMARY_COLOR
:
585 var
= p
->shader
->symbols
->get_variable("gl_Color");
587 return new(p
->mem_ctx
) ir_dereference_variable(var
);
590 return new(p
->mem_ctx
) ir_constant(0.0f
);
593 if (!p
->src_previous
) {
594 return get_gl_Color(p
);
596 return p
->src_previous
->clone(p
->mem_ctx
, NULL
);
606 emit_combine_source(texenv_fragment_program
*p
,
613 src
= get_source(p
, source
, unit
);
616 case OPR_ONE_MINUS_SRC_COLOR
:
617 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), src
);
620 return src
->type
->is_scalar() ? src
: swizzle_w(src
);
622 case OPR_ONE_MINUS_SRC_ALPHA
: {
623 ir_rvalue
*const scalar
= src
->type
->is_scalar() ? src
: swizzle_w(src
);
625 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), scalar
);
629 return new(p
->mem_ctx
) ir_constant(0.0f
);
631 return new(p
->mem_ctx
) ir_constant(1.0f
);
641 * Check if the RGB and Alpha sources and operands match for the given
642 * texture unit's combinder state. When the RGB and A sources and
643 * operands match, we can emit fewer instructions.
645 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
647 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
649 for (i
= 0; i
< numArgs
; i
++) {
650 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
653 switch (key
->unit
[unit
].OptA
[i
].Operand
) {
655 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
663 case OPR_ONE_MINUS_SRC_ALPHA
:
664 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
665 case OPR_ONE_MINUS_SRC_COLOR
:
666 case OPR_ONE_MINUS_SRC_ALPHA
:
673 return GL_FALSE
; /* impossible */
681 smear(texenv_fragment_program
*p
, ir_rvalue
*val
)
683 if (!val
->type
->is_scalar())
686 return swizzle_xxxx(val
);
690 emit_combine(texenv_fragment_program
*p
,
694 const struct mode_opt
*opt
)
696 ir_rvalue
*src
[MAX_COMBINER_TERMS
];
697 ir_rvalue
*tmp0
, *tmp1
;
700 assert(nr
<= MAX_COMBINER_TERMS
);
702 for (i
= 0; i
< nr
; i
++)
703 src
[i
] = emit_combine_source( p
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
710 return mul(src
[0], src
[1]);
713 return add(src
[0], src
[1]);
715 case MODE_ADD_SIGNED
:
716 return add(add(src
[0], src
[1]), new(p
->mem_ctx
) ir_constant(-0.5f
));
718 case MODE_INTERPOLATE
:
719 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
720 tmp0
= mul(src
[0], src
[2]);
721 tmp1
= mul(src
[1], sub(new(p
->mem_ctx
) ir_constant(1.0f
),
722 src
[2]->clone(p
->mem_ctx
, NULL
)));
723 return add(tmp0
, tmp1
);
726 return sub(src
[0], src
[1]);
729 case MODE_DOT3_RGBA_EXT
:
730 case MODE_DOT3_RGB_EXT
:
731 case MODE_DOT3_RGB
: {
732 tmp0
= mul(src
[0], new(p
->mem_ctx
) ir_constant(2.0f
));
733 tmp0
= add(tmp0
, new(p
->mem_ctx
) ir_constant(-1.0f
));
735 tmp1
= mul(src
[1], new(p
->mem_ctx
) ir_constant(2.0f
));
736 tmp1
= add(tmp1
, new(p
->mem_ctx
) ir_constant(-1.0f
));
738 return dot(swizzle_xyz(smear(p
, tmp0
)), swizzle_xyz(smear(p
, tmp1
)));
740 case MODE_MODULATE_ADD_ATI
:
741 return add(mul(src
[0], src
[2]), src
[1]);
743 case MODE_MODULATE_SIGNED_ADD_ATI
:
744 return add(add(mul(src
[0], src
[2]), src
[1]),
745 new(p
->mem_ctx
) ir_constant(-0.5f
));
747 case MODE_MODULATE_SUBTRACT_ATI
:
748 return sub(mul(src
[0], src
[2]), src
[1]);
750 case MODE_ADD_PRODUCTS
:
751 return add(mul(src
[0], src
[1]), mul(src
[2], src
[3]));
753 case MODE_ADD_PRODUCTS_SIGNED
:
754 return add(add(mul(src
[0], src
[1]), mul(src
[2], src
[3])),
755 new(p
->mem_ctx
) ir_constant(-0.5f
));
757 case MODE_BUMP_ENVMAP_ATI
:
758 /* special - not handled here */
768 * Generate instructions for one texture unit's env/combiner mode.
771 emit_texenv(texenv_fragment_program
*p
, GLuint unit
)
773 const struct state_key
*key
= p
->state
;
774 GLboolean rgb_saturate
, alpha_saturate
;
775 GLuint rgb_shift
, alpha_shift
;
777 if (!key
->unit
[unit
].enabled
) {
778 return get_source(p
, SRC_PREVIOUS
, 0);
780 if (key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
781 /* this isn't really a env stage delivering a color and handled elsewhere */
782 return get_source(p
, SRC_PREVIOUS
, 0);
785 switch (key
->unit
[unit
].ModeRGB
) {
786 case MODE_DOT3_RGB_EXT
:
787 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
790 case MODE_DOT3_RGBA_EXT
:
795 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
796 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
800 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
801 * We don't want to clamp twice.
804 rgb_saturate
= GL_FALSE
; /* saturate after rgb shift */
805 else if (need_saturate(key
->unit
[unit
].ModeRGB
))
806 rgb_saturate
= GL_TRUE
;
808 rgb_saturate
= GL_FALSE
;
811 alpha_saturate
= GL_FALSE
; /* saturate after alpha shift */
812 else if (need_saturate(key
->unit
[unit
].ModeA
))
813 alpha_saturate
= GL_TRUE
;
815 alpha_saturate
= GL_FALSE
;
817 ir_variable
*temp_var
= p
->make_temp(glsl_type::vec4_type
, "texenv_combine");
818 ir_dereference
*deref
;
821 /* Emit the RGB and A combine ops
823 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
824 args_match(key
, unit
)) {
825 val
= emit_combine(p
, unit
,
826 key
->unit
[unit
].NumArgsRGB
,
827 key
->unit
[unit
].ModeRGB
,
828 key
->unit
[unit
].OptRGB
);
833 p
->emit(assign(temp_var
, val
));
835 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
836 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
837 ir_rvalue
*val
= emit_combine(p
, unit
,
838 key
->unit
[unit
].NumArgsRGB
,
839 key
->unit
[unit
].ModeRGB
,
840 key
->unit
[unit
].OptRGB
);
844 p
->emit(assign(temp_var
, val
));
847 /* Need to do something to stop from re-emitting identical
848 * argument calculations here:
850 val
= emit_combine(p
, unit
,
851 key
->unit
[unit
].NumArgsRGB
,
852 key
->unit
[unit
].ModeRGB
,
853 key
->unit
[unit
].OptRGB
);
854 val
= swizzle_xyz(smear(p
, val
));
857 p
->emit(assign(temp_var
, val
, WRITEMASK_XYZ
));
859 val
= emit_combine(p
, unit
,
860 key
->unit
[unit
].NumArgsA
,
861 key
->unit
[unit
].ModeA
,
862 key
->unit
[unit
].OptA
);
863 val
= swizzle_w(smear(p
, val
));
866 p
->emit(assign(temp_var
, val
, WRITEMASK_W
));
869 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
871 /* Deal with the final shift:
873 if (alpha_shift
|| rgb_shift
) {
876 if (rgb_shift
== alpha_shift
) {
877 shift
= new(p
->mem_ctx
) ir_constant((float)(1 << rgb_shift
));
880 float const_data
[4] = {
881 float(1 << rgb_shift
),
882 float(1 << rgb_shift
),
883 float(1 << rgb_shift
),
884 float(1 << alpha_shift
)
886 shift
= new(p
->mem_ctx
) ir_constant(glsl_type::vec4_type
,
887 (ir_constant_data
*)const_data
);
890 return saturate(mul(deref
, shift
));
898 * Generate instruction for getting a texture source term.
900 static void load_texture( texenv_fragment_program
*p
, GLuint unit
)
902 ir_dereference
*deref
;
904 if (p
->src_texture
[unit
])
907 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
910 if (!(p
->state
->inputs_available
& (VARYING_BIT_TEX0
<< unit
))) {
911 texcoord
= get_current_attrib(p
, VERT_ATTRIB_TEX0
+ unit
);
912 } else if (p
->texcoord_tex
[unit
]) {
913 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(p
->texcoord_tex
[unit
]);
915 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
917 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
918 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(unit
);
919 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
920 tc_array
->data
.max_array_access
= MAX2(tc_array
->data
.max_array_access
, unit
);
923 if (!p
->state
->unit
[unit
].enabled
) {
924 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
926 p
->emit(p
->src_texture
[unit
]);
928 p
->emit(assign(p
->src_texture
[unit
], new(p
->mem_ctx
) ir_constant(0.0f
)));
932 const glsl_type
*sampler_type
= NULL
;
936 case TEXTURE_1D_INDEX
:
937 if (p
->state
->unit
[unit
].shadow
)
938 sampler_type
= p
->shader
->symbols
->get_type("sampler1DShadow");
940 sampler_type
= p
->shader
->symbols
->get_type("sampler1D");
943 case TEXTURE_1D_ARRAY_INDEX
:
944 if (p
->state
->unit
[unit
].shadow
)
945 sampler_type
= p
->shader
->symbols
->get_type("sampler1DArrayShadow");
947 sampler_type
= p
->shader
->symbols
->get_type("sampler1DArray");
950 case TEXTURE_2D_INDEX
:
951 if (p
->state
->unit
[unit
].shadow
)
952 sampler_type
= p
->shader
->symbols
->get_type("sampler2DShadow");
954 sampler_type
= p
->shader
->symbols
->get_type("sampler2D");
957 case TEXTURE_2D_ARRAY_INDEX
:
958 if (p
->state
->unit
[unit
].shadow
)
959 sampler_type
= p
->shader
->symbols
->get_type("sampler2DArrayShadow");
961 sampler_type
= p
->shader
->symbols
->get_type("sampler2DArray");
964 case TEXTURE_RECT_INDEX
:
965 if (p
->state
->unit
[unit
].shadow
)
966 sampler_type
= p
->shader
->symbols
->get_type("sampler2DRectShadow");
968 sampler_type
= p
->shader
->symbols
->get_type("sampler2DRect");
971 case TEXTURE_3D_INDEX
:
972 assert(!p
->state
->unit
[unit
].shadow
);
973 sampler_type
= p
->shader
->symbols
->get_type("sampler3D");
976 case TEXTURE_CUBE_INDEX
:
977 if (p
->state
->unit
[unit
].shadow
)
978 sampler_type
= p
->shader
->symbols
->get_type("samplerCubeShadow");
980 sampler_type
= p
->shader
->symbols
->get_type("samplerCube");
983 case TEXTURE_EXTERNAL_INDEX
:
984 assert(!p
->state
->unit
[unit
].shadow
);
985 sampler_type
= p
->shader
->symbols
->get_type("samplerExternalOES");
990 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
993 ir_texture
*tex
= new(p
->mem_ctx
) ir_texture(ir_tex
);
996 char *sampler_name
= ralloc_asprintf(p
->mem_ctx
, "sampler_%d", unit
);
997 ir_variable
*sampler
= new(p
->mem_ctx
) ir_variable(sampler_type
,
1000 p
->top_instructions
->push_head(sampler
);
1002 /* Set the texture unit for this sampler. The linker will pick this value
1003 * up and do-the-right-thing.
1005 * NOTE: The cast to int is important. Without it, the constant will have
1006 * type uint, and things later on may get confused.
1008 sampler
->constant_value
= new(p
->mem_ctx
) ir_constant(int(unit
));
1010 deref
= new(p
->mem_ctx
) ir_dereference_variable(sampler
);
1011 tex
->set_sampler(deref
, glsl_type::vec4_type
);
1013 tex
->coordinate
= new(p
->mem_ctx
) ir_swizzle(texcoord
, 0, 1, 2, 3, coords
);
1015 if (p
->state
->unit
[unit
].shadow
) {
1016 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1017 tex
->shadow_comparitor
= new(p
->mem_ctx
) ir_swizzle(texcoord
,
1023 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1024 tex
->projector
= swizzle_w(texcoord
);
1026 p
->emit(assign(p
->src_texture
[unit
], tex
));
1030 load_texenv_source(texenv_fragment_program
*p
,
1031 GLuint src
, GLuint unit
)
1035 load_texture(p
, unit
);
1046 load_texture(p
, src
- SRC_TEXTURE0
);
1050 /* not a texture src - do nothing */
1057 * Generate instructions for loading all texture source terms.
1060 load_texunit_sources( texenv_fragment_program
*p
, GLuint unit
)
1062 const struct state_key
*key
= p
->state
;
1065 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1066 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1069 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1070 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1077 * Generate instructions for loading bump map textures.
1080 load_texunit_bumpmap( texenv_fragment_program
*p
, GLuint unit
)
1082 const struct state_key
*key
= p
->state
;
1083 GLuint bumpedUnitNr
= key
->unit
[unit
].OptRGB
[1].Source
- SRC_TEXTURE0
;
1085 ir_rvalue
*texcoord
;
1086 ir_variable
*rot_mat_0
, *rot_mat_1
;
1088 rot_mat_0
= p
->shader
->symbols
->get_variable("gl_BumpRotMatrix0MESA");
1090 rot_mat_1
= p
->shader
->symbols
->get_variable("gl_BumpRotMatrix1MESA");
1093 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
1095 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
1096 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(bumpedUnitNr
);
1097 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
1098 tc_array
->data
.max_array_access
= MAX2(tc_array
->data
.max_array_access
, unit
);
1100 load_texenv_source( p
, unit
+ SRC_TEXTURE0
, unit
);
1102 /* Apply rot matrix and add coords to be available in next phase.
1103 * dest = Arg1 + (Arg0.xx * rotMat0) + (Arg0.yy * rotMat1)
1104 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1106 ir_rvalue
*bump_x
, *bump_y
;
1108 texcoord
= smear(p
, texcoord
);
1110 /* bump_texcoord = texcoord */
1111 ir_variable
*bumped
= p
->make_temp(texcoord
->type
, "bump_texcoord");
1113 p
->emit(assign(bumped
, texcoord
));
1115 /* bump_texcoord.xy += arg0.x * rotmat0 + arg0.y * rotmat1 */
1116 bump
= get_source(p
, key
->unit
[unit
].OptRGB
[0].Source
, unit
);
1117 bump_x
= mul(swizzle_x(bump
), rot_mat_0
);
1118 bump_y
= mul(swizzle_y(bump
->clone(p
->mem_ctx
, NULL
)), rot_mat_1
);
1120 p
->emit(assign(bumped
, add(swizzle_xy(bumped
), add(bump_x
, bump_y
)),
1123 p
->texcoord_tex
[bumpedUnitNr
] = bumped
;
1127 * Applies the fog calculations.
1129 * This is basically like the ARB_fragment_prorgam fog options. Note
1130 * that ffvertex_prog.c produces fogcoord for us when
1131 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1134 emit_fog_instructions(texenv_fragment_program
*p
,
1135 ir_rvalue
*fragcolor
)
1137 struct state_key
*key
= p
->state
;
1138 ir_rvalue
*f
, *temp
;
1139 ir_variable
*params
, *oparams
;
1140 ir_variable
*fogcoord
;
1142 /* Temporary storage for the whole fog result. Fog calculations
1143 * only affect rgb so we're hanging on to the .a value of fragcolor
1146 ir_variable
*fog_result
= p
->make_temp(glsl_type::vec4_type
, "fog_result");
1147 p
->emit(assign(fog_result
, fragcolor
));
1149 fragcolor
= swizzle_xyz(fog_result
);
1151 oparams
= p
->shader
->symbols
->get_variable("gl_FogParamsOptimizedMESA");
1153 fogcoord
= p
->shader
->symbols
->get_variable("gl_FogFragCoord");
1155 params
= p
->shader
->symbols
->get_variable("gl_Fog");
1157 f
= new(p
->mem_ctx
) ir_dereference_variable(fogcoord
);
1159 ir_variable
*f_var
= p
->make_temp(glsl_type::float_type
, "fog_factor");
1161 switch (key
->fog_mode
) {
1163 /* f = (end - z) / (end - start)
1165 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1166 * (end / (end - start)) so we can generate a single MAD.
1168 f
= add(mul(f
, swizzle_x(oparams
)), swizzle_y(oparams
));
1171 /* f = e^(-(density * fogcoord))
1173 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1174 * use EXP2 which is generally the native instruction without
1175 * having to do any further math on the fog density uniform.
1177 f
= mul(f
, swizzle_z(oparams
));
1178 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1179 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1182 /* f = e^(-(density * fogcoord)^2)
1184 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1185 * can do this like FOG_EXP but with a squaring after the
1186 * multiply by density.
1188 ir_variable
*temp_var
= p
->make_temp(glsl_type::float_type
, "fog_temp");
1189 p
->emit(assign(temp_var
, mul(f
, swizzle_w(oparams
))));
1191 f
= mul(temp_var
, temp_var
);
1192 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1193 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1197 p
->emit(assign(f_var
, saturate(f
)));
1199 f
= sub(new(p
->mem_ctx
) ir_constant(1.0f
), f_var
);
1200 temp
= new(p
->mem_ctx
) ir_dereference_variable(params
);
1201 temp
= new(p
->mem_ctx
) ir_dereference_record(temp
, "color");
1202 temp
= mul(swizzle_xyz(temp
), f
);
1204 p
->emit(assign(fog_result
, add(temp
, mul(fragcolor
, f_var
)), WRITEMASK_XYZ
));
1206 return new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1210 emit_instructions(texenv_fragment_program
*p
)
1212 struct state_key
*key
= p
->state
;
1215 if (key
->enabled_units
) {
1216 /* Zeroth pass - bump map textures first */
1217 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1218 if (key
->unit
[unit
].enabled
&&
1219 key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1220 load_texunit_bumpmap(p
, unit
);
1224 /* First pass - to support texture_env_crossbar, first identify
1225 * all referenced texture sources and emit texld instructions
1228 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1229 if (key
->unit
[unit
].enabled
) {
1230 load_texunit_sources(p
, unit
);
1233 /* Second pass - emit combine instructions to build final color:
1235 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1236 if (key
->unit
[unit
].enabled
) {
1237 p
->src_previous
= emit_texenv(p
, unit
);
1242 ir_rvalue
*cf
= get_source(p
, SRC_PREVIOUS
, 0);
1244 if (key
->separate_specular
) {
1245 ir_variable
*spec_result
= p
->make_temp(glsl_type::vec4_type
,
1247 p
->emit(assign(spec_result
, cf
));
1249 ir_rvalue
*secondary
;
1250 if (p
->state
->inputs_available
& VARYING_BIT_COL1
) {
1252 p
->shader
->symbols
->get_variable("gl_SecondaryColor");
1254 secondary
= swizzle_xyz(var
);
1256 secondary
= swizzle_xyz(get_current_attrib(p
, VERT_ATTRIB_COLOR1
));
1259 p
->emit(assign(spec_result
, add(swizzle_xyz(spec_result
), secondary
),
1262 cf
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1265 if (key
->fog_enabled
) {
1266 cf
= emit_fog_instructions(p
, cf
);
1269 ir_variable
*frag_color
= p
->shader
->symbols
->get_variable("gl_FragColor");
1271 p
->emit(assign(frag_color
, cf
));
1275 * Generate a new fragment program which implements the context's
1276 * current texture env/combine mode.
1278 static struct gl_shader_program
*
1279 create_new_program(struct gl_context
*ctx
, struct state_key
*key
)
1281 texenv_fragment_program p
;
1283 _mesa_glsl_parse_state
*state
;
1285 p
.mem_ctx
= ralloc_context(NULL
);
1286 p
.shader
= ctx
->Driver
.NewShader(ctx
, 0, GL_FRAGMENT_SHADER
);
1287 p
.shader
->ir
= new(p
.shader
) exec_list
;
1288 state
= new(p
.shader
) _mesa_glsl_parse_state(ctx
, MESA_SHADER_FRAGMENT
,
1290 p
.shader
->symbols
= state
->symbols
;
1291 p
.top_instructions
= p
.shader
->ir
;
1292 p
.instructions
= p
.shader
->ir
;
1294 p
.shader_program
= ctx
->Driver
.NewShaderProgram(ctx
, 0);
1296 /* Tell the linker to ignore the fact that we're building a
1297 * separate shader, in case we're in a GLES2 context that would
1298 * normally reject that. The real problem is that we're building a
1299 * fixed function program in a GLES2 context at all, but that's a
1300 * big mess to clean up.
1302 p
.shader_program
->SeparateShader
= GL_TRUE
;
1304 state
->language_version
= 130;
1305 state
->es_shader
= false;
1306 if (_mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
)
1307 state
->OES_EGL_image_external_enable
= true;
1308 _mesa_glsl_initialize_types(state
);
1309 _mesa_glsl_initialize_variables(p
.instructions
, state
);
1311 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1312 p
.src_texture
[unit
] = NULL
;
1313 p
.texcoord_tex
[unit
] = NULL
;
1316 p
.src_previous
= NULL
;
1318 ir_function
*main_f
= new(p
.mem_ctx
) ir_function("main");
1320 state
->symbols
->add_function(main_f
);
1322 ir_function_signature
*main_sig
=
1323 new(p
.mem_ctx
) ir_function_signature(p
.shader
->symbols
->get_type("void"));
1324 main_sig
->is_defined
= true;
1325 main_f
->add_signature(main_sig
);
1327 p
.instructions
= &main_sig
->body
;
1328 if (key
->num_draw_buffers
)
1329 emit_instructions(&p
);
1331 validate_ir_tree(p
.shader
->ir
);
1333 const struct gl_shader_compiler_options
*options
=
1334 &ctx
->ShaderCompilerOptions
[MESA_SHADER_FRAGMENT
];
1336 while (do_common_optimization(p
.shader
->ir
, false, false, options
,
1337 ctx
->Const
.NativeIntegers
))
1339 reparent_ir(p
.shader
->ir
, p
.shader
->ir
);
1341 p
.shader
->CompileStatus
= true;
1342 p
.shader
->Version
= state
->language_version
;
1343 p
.shader
->uses_builtin_functions
= state
->uses_builtin_functions
;
1344 p
.shader_program
->Shaders
=
1345 (gl_shader
**)malloc(sizeof(*p
.shader_program
->Shaders
));
1346 p
.shader_program
->Shaders
[0] = p
.shader
;
1347 p
.shader_program
->NumShaders
= 1;
1349 _mesa_glsl_link_shader(ctx
, p
.shader_program
);
1351 if (!p
.shader_program
->LinkStatus
)
1352 _mesa_problem(ctx
, "Failed to link fixed function fragment shader: %s\n",
1353 p
.shader_program
->InfoLog
);
1355 ralloc_free(p
.mem_ctx
);
1356 return p
.shader_program
;
1362 * Return a fragment program which implements the current
1363 * fixed-function texture, fog and color-sum operations.
1365 struct gl_shader_program
*
1366 _mesa_get_fixed_func_fragment_program(struct gl_context
*ctx
)
1368 struct gl_shader_program
*shader_program
;
1369 struct state_key key
;
1372 keySize
= make_state_key(ctx
, &key
);
1374 shader_program
= (struct gl_shader_program
*)
1375 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1378 if (!shader_program
) {
1379 shader_program
= create_new_program(ctx
, &key
);
1381 _mesa_shader_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1382 &key
, keySize
, shader_program
);
1385 return shader_program
;