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"
46 #include "main/uniforms.h"
47 #include "../glsl/glsl_types.h"
48 #include "../glsl/ir.h"
49 #include "../glsl/ir_builder.h"
50 #include "../glsl/glsl_symbol_table.h"
51 #include "../glsl/glsl_parser_extras.h"
52 #include "../glsl/ir_optimization.h"
53 #include "../program/ir_to_mesa.h"
55 using namespace ir_builder
;
58 * Note on texture units:
60 * The number of texture units supported by fixed-function fragment
61 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
62 * That's because there's a one-to-one correspondence between texture
63 * coordinates and samplers in fixed-function processing.
65 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
66 * sets of texcoords, so is fixed-function fragment processing.
68 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
72 struct texenvprog_cache_item
76 struct gl_shader_program
*data
;
77 struct texenvprog_cache_item
*next
;
81 texenv_doing_secondary_color(struct gl_context
*ctx
)
83 if (ctx
->Light
.Enabled
&&
84 (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
))
87 if (ctx
->Fog
.ColorSumEnabled
)
95 __extension__ GLubyte Source
:4; /**< SRC_x */
96 __extension__ GLubyte Operand
:3; /**< OPR_x */
98 GLubyte Source
; /**< SRC_x */
99 GLubyte Operand
; /**< OPR_x */
104 GLuint nr_enabled_units
:8;
105 GLuint enabled_units
:8;
106 GLuint separate_specular
:1;
107 GLuint fog_enabled
:1;
108 GLuint fog_mode
:2; /**< FOG_x */
109 GLuint inputs_available
:12;
110 GLuint num_draw_buffers
:4;
112 /* NOTE: This array of structs must be last! (see "keySize" below) */
115 GLuint source_index
:4; /**< TEXTURE_x_INDEX */
117 GLuint ScaleShiftRGB
:2;
118 GLuint ScaleShiftA
:2;
120 GLuint NumArgsRGB
:3; /**< up to MAX_COMBINER_TERMS */
121 GLuint ModeRGB
:5; /**< MODE_x */
123 GLuint NumArgsA
:3; /**< up to MAX_COMBINER_TERMS */
124 GLuint ModeA
:5; /**< MODE_x */
126 struct mode_opt OptRGB
[MAX_COMBINER_TERMS
];
127 struct mode_opt OptA
[MAX_COMBINER_TERMS
];
128 } unit
[MAX_TEXTURE_UNITS
];
134 #define FOG_UNKNOWN 3
136 static GLuint
translate_fog_mode( GLenum mode
)
139 case GL_LINEAR
: return FOG_LINEAR
;
140 case GL_EXP
: return FOG_EXP
;
141 case GL_EXP2
: return FOG_EXP2
;
142 default: return FOG_UNKNOWN
;
146 #define OPR_SRC_COLOR 0
147 #define OPR_ONE_MINUS_SRC_COLOR 1
148 #define OPR_SRC_ALPHA 2
149 #define OPR_ONE_MINUS_SRC_ALPHA 3
152 #define OPR_UNKNOWN 7
154 static GLuint
translate_operand( GLenum operand
)
157 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
158 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
159 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
160 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
161 case GL_ZERO
: return OPR_ZERO
;
162 case GL_ONE
: return OPR_ONE
;
169 #define SRC_TEXTURE 0
170 #define SRC_TEXTURE0 1
171 #define SRC_TEXTURE1 2
172 #define SRC_TEXTURE2 3
173 #define SRC_TEXTURE3 4
174 #define SRC_TEXTURE4 5
175 #define SRC_TEXTURE5 6
176 #define SRC_TEXTURE6 7
177 #define SRC_TEXTURE7 8
178 #define SRC_CONSTANT 9
179 #define SRC_PRIMARY_COLOR 10
180 #define SRC_PREVIOUS 11
182 #define SRC_UNKNOWN 15
184 static GLuint
translate_source( GLenum src
)
187 case GL_TEXTURE
: return SRC_TEXTURE
;
195 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
196 case GL_CONSTANT
: return SRC_CONSTANT
;
197 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
198 case GL_PREVIOUS
: return SRC_PREVIOUS
;
207 #define MODE_REPLACE 0 /* r = a0 */
208 #define MODE_MODULATE 1 /* r = a0 * a1 */
209 #define MODE_ADD 2 /* r = a0 + a1 */
210 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
211 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
212 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
213 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
214 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
215 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
216 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
217 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
218 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
219 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
220 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
221 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
222 #define MODE_BUMP_ENVMAP_ATI 15 /* special */
223 #define MODE_UNKNOWN 16
226 * Translate GL combiner state into a MODE_x value
228 static GLuint
translate_mode( GLenum envMode
, GLenum mode
)
231 case GL_REPLACE
: return MODE_REPLACE
;
232 case GL_MODULATE
: return MODE_MODULATE
;
234 if (envMode
== GL_COMBINE4_NV
)
235 return MODE_ADD_PRODUCTS
;
239 if (envMode
== GL_COMBINE4_NV
)
240 return MODE_ADD_PRODUCTS_SIGNED
;
242 return MODE_ADD_SIGNED
;
243 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
244 case GL_SUBTRACT
: return MODE_SUBTRACT
;
245 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
246 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
247 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
248 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
249 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
250 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
251 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
252 case GL_BUMP_ENVMAP_ATI
: return MODE_BUMP_ENVMAP_ATI
;
261 * Do we need to clamp the results of the given texture env/combine mode?
262 * If the inputs to the mode are in [0,1] we don't always have to clamp
266 need_saturate( GLuint mode
)
271 case MODE_INTERPOLATE
:
274 case MODE_ADD_SIGNED
:
277 case MODE_DOT3_RGB_EXT
:
279 case MODE_DOT3_RGBA_EXT
:
280 case MODE_MODULATE_ADD_ATI
:
281 case MODE_MODULATE_SIGNED_ADD_ATI
:
282 case MODE_MODULATE_SUBTRACT_ATI
:
283 case MODE_ADD_PRODUCTS
:
284 case MODE_ADD_PRODUCTS_SIGNED
:
285 case MODE_BUMP_ENVMAP_ATI
:
296 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
298 static GLuint
translate_tex_src_bit( GLbitfield bit
)
305 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
308 * Identify all possible varying inputs. The fragment program will
309 * never reference non-varying inputs, but will track them via state
312 * This function figures out all the inputs that the fragment program
313 * has access to. The bitmask is later reduced to just those which
314 * are actually referenced.
316 static GLbitfield
get_fp_input_mask( struct gl_context
*ctx
)
319 const GLboolean vertexShader
=
320 (ctx
->Shader
.CurrentVertexProgram
&&
321 ctx
->Shader
.CurrentVertexProgram
->LinkStatus
&&
322 ctx
->Shader
.CurrentVertexProgram
->_LinkedShaders
[MESA_SHADER_VERTEX
]);
323 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
324 GLbitfield fp_inputs
= 0x0;
326 if (ctx
->VertexProgram
._Overriden
) {
327 /* Somebody's messing with the vertex program and we don't have
328 * a clue what's happening. Assume that it could be producing
329 * all possible outputs.
333 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
334 /* _NEW_RENDERMODE */
335 fp_inputs
= (VARYING_BIT_COL0
| VARYING_BIT_TEX0
);
337 else if (!(vertexProgram
|| vertexShader
)) {
338 /* Fixed function vertex logic */
339 /* _NEW_VARYING_VP_INPUTS */
340 GLbitfield64 varying_inputs
= ctx
->varying_vp_inputs
;
342 /* These get generated in the setup routine regardless of the
346 if (ctx
->Point
.PointSprite
)
347 varying_inputs
|= VARYING_BITS_TEX_ANY
;
349 /* First look at what values may be computed by the generated
353 if (ctx
->Light
.Enabled
) {
354 fp_inputs
|= VARYING_BIT_COL0
;
356 if (texenv_doing_secondary_color(ctx
))
357 fp_inputs
|= VARYING_BIT_COL1
;
361 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
362 ctx
->Texture
._TexMatEnabled
) << VARYING_SLOT_TEX0
;
364 /* Then look at what might be varying as a result of enabled
367 if (varying_inputs
& VERT_BIT_COLOR0
)
368 fp_inputs
|= VARYING_BIT_COL0
;
369 if (varying_inputs
& VERT_BIT_COLOR1
)
370 fp_inputs
|= VARYING_BIT_COL1
;
372 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
373 << VARYING_SLOT_TEX0
);
377 /* calculate from vp->outputs */
378 struct gl_program
*vprog
;
379 GLbitfield64 vp_outputs
;
381 /* Choose GLSL vertex shader over ARB vertex program. Need this
382 * since vertex shader state validation comes after fragment state
383 * validation (see additional comments in state.c).
386 vprog
= ctx
->Shader
.CurrentVertexProgram
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
;
388 vprog
= &ctx
->VertexProgram
.Current
->Base
;
390 vp_outputs
= vprog
->OutputsWritten
;
392 /* These get generated in the setup routine regardless of the
396 if (ctx
->Point
.PointSprite
)
397 vp_outputs
|= VARYING_BITS_TEX_ANY
;
399 if (vp_outputs
& (1 << VARYING_SLOT_COL0
))
400 fp_inputs
|= VARYING_BIT_COL0
;
401 if (vp_outputs
& (1 << VARYING_SLOT_COL1
))
402 fp_inputs
|= VARYING_BIT_COL1
;
404 fp_inputs
|= (((vp_outputs
& VARYING_BITS_TEX_ANY
) >> VARYING_SLOT_TEX0
)
405 << VARYING_SLOT_TEX0
);
413 * Examine current texture environment state and generate a unique
414 * key to identify it.
416 static GLuint
make_state_key( struct gl_context
*ctx
, struct state_key
*key
)
419 GLbitfield inputs_referenced
= VARYING_BIT_COL0
;
420 const GLbitfield inputs_available
= get_fp_input_mask( ctx
);
423 memset(key
, 0, sizeof(*key
));
426 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
427 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
428 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
429 const struct gl_tex_env_combine_state
*comb
= texUnit
->_CurrentCombine
;
430 const struct gl_sampler_object
*samp
;
433 if (!texUnit
->_ReallyEnabled
|| !texUnit
->Enabled
)
436 samp
= _mesa_get_samplerobj(ctx
, i
);
437 format
= texObj
->Image
[0][texObj
->BaseLevel
]->_BaseFormat
;
439 key
->unit
[i
].enabled
= 1;
440 key
->enabled_units
|= (1<<i
);
441 key
->nr_enabled_units
= i
+ 1;
442 inputs_referenced
|= VARYING_BIT_TEX(i
);
444 key
->unit
[i
].source_index
=
445 translate_tex_src_bit(texUnit
->_ReallyEnabled
);
447 key
->unit
[i
].shadow
=
448 ((samp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
449 ((format
== GL_DEPTH_COMPONENT
) ||
450 (format
== GL_DEPTH_STENCIL_EXT
)));
452 key
->unit
[i
].NumArgsRGB
= comb
->_NumArgsRGB
;
453 key
->unit
[i
].NumArgsA
= comb
->_NumArgsA
;
455 key
->unit
[i
].ModeRGB
=
456 translate_mode(texUnit
->EnvMode
, comb
->ModeRGB
);
458 translate_mode(texUnit
->EnvMode
, comb
->ModeA
);
460 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
461 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
463 for (j
= 0; j
< MAX_COMBINER_TERMS
; j
++) {
464 key
->unit
[i
].OptRGB
[j
].Operand
= translate_operand(comb
->OperandRGB
[j
]);
465 key
->unit
[i
].OptA
[j
].Operand
= translate_operand(comb
->OperandA
[j
]);
466 key
->unit
[i
].OptRGB
[j
].Source
= translate_source(comb
->SourceRGB
[j
]);
467 key
->unit
[i
].OptA
[j
].Source
= translate_source(comb
->SourceA
[j
]);
470 if (key
->unit
[i
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
471 /* requires some special translation */
472 key
->unit
[i
].NumArgsRGB
= 2;
473 key
->unit
[i
].ScaleShiftRGB
= 0;
474 key
->unit
[i
].OptRGB
[0].Operand
= OPR_SRC_COLOR
;
475 key
->unit
[i
].OptRGB
[0].Source
= SRC_TEXTURE
;
476 key
->unit
[i
].OptRGB
[1].Operand
= OPR_SRC_COLOR
;
477 key
->unit
[i
].OptRGB
[1].Source
= texUnit
->BumpTarget
- GL_TEXTURE0
+ SRC_TEXTURE0
;
481 /* _NEW_LIGHT | _NEW_FOG */
482 if (texenv_doing_secondary_color(ctx
)) {
483 key
->separate_specular
= 1;
484 inputs_referenced
|= VARYING_BIT_COL1
;
488 if (ctx
->Fog
.Enabled
) {
489 key
->fog_enabled
= 1;
490 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
491 inputs_referenced
|= VARYING_BIT_FOGC
; /* maybe */
495 key
->num_draw_buffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
498 if (ctx
->Color
.AlphaEnabled
&& key
->num_draw_buffers
== 0) {
499 /* if alpha test is enabled we need to emit at least one color */
500 key
->num_draw_buffers
= 1;
503 key
->inputs_available
= (inputs_available
& inputs_referenced
);
505 /* compute size of state key, ignoring unused texture units */
506 keySize
= sizeof(*key
) - sizeof(key
->unit
)
507 + key
->nr_enabled_units
* sizeof(key
->unit
[0]);
513 /** State used to build the fragment program:
515 class texenv_fragment_program
: public ir_factory
{
517 struct gl_shader_program
*shader_program
;
518 struct gl_shader
*shader
;
519 exec_list
*top_instructions
;
520 struct state_key
*state
;
522 ir_variable
*src_texture
[MAX_TEXTURE_COORD_UNITS
];
523 /* Reg containing each texture unit's sampled texture color,
527 /* Texcoord override from bumpmapping. */
528 ir_variable
*texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
530 /* Reg containing texcoord for a texture unit,
531 * needed for bump mapping, else undef.
534 ir_rvalue
*src_previous
; /**< Reg containing color from previous
535 * stage. May need to be decl'd.
540 get_current_attrib(texenv_fragment_program
*p
, GLuint attrib
)
542 ir_variable
*current
;
545 current
= p
->shader
->symbols
->get_variable("gl_CurrentAttribFragMESA");
547 current
->data
.max_array_access
= MAX2(current
->data
.max_array_access
, attrib
);
548 val
= new(p
->mem_ctx
) ir_dereference_variable(current
);
549 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(attrib
);
550 return new(p
->mem_ctx
) ir_dereference_array(val
, index
);
554 get_gl_Color(texenv_fragment_program
*p
)
556 if (p
->state
->inputs_available
& VARYING_BIT_COL0
) {
557 ir_variable
*var
= p
->shader
->symbols
->get_variable("gl_Color");
559 return new(p
->mem_ctx
) ir_dereference_variable(var
);
561 return get_current_attrib(p
, VERT_ATTRIB_COLOR0
);
566 get_source(texenv_fragment_program
*p
,
567 GLuint src
, GLuint unit
)
570 ir_dereference
*deref
;
574 return new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
584 return new(p
->mem_ctx
)
585 ir_dereference_variable(p
->src_texture
[src
- SRC_TEXTURE0
]);
588 var
= p
->shader
->symbols
->get_variable("gl_TextureEnvColor");
590 deref
= new(p
->mem_ctx
) ir_dereference_variable(var
);
591 var
->data
.max_array_access
= MAX2(var
->data
.max_array_access
, unit
);
592 return new(p
->mem_ctx
) ir_dereference_array(deref
,
593 new(p
->mem_ctx
) ir_constant(unit
));
595 case SRC_PRIMARY_COLOR
:
596 var
= p
->shader
->symbols
->get_variable("gl_Color");
598 return new(p
->mem_ctx
) ir_dereference_variable(var
);
601 return new(p
->mem_ctx
) ir_constant(0.0f
);
604 if (!p
->src_previous
) {
605 return get_gl_Color(p
);
607 return p
->src_previous
->clone(p
->mem_ctx
, NULL
);
617 emit_combine_source(texenv_fragment_program
*p
,
624 src
= get_source(p
, source
, unit
);
627 case OPR_ONE_MINUS_SRC_COLOR
:
628 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), src
);
631 return src
->type
->is_scalar() ? src
: swizzle_w(src
);
633 case OPR_ONE_MINUS_SRC_ALPHA
: {
634 ir_rvalue
*const scalar
= src
->type
->is_scalar() ? src
: swizzle_w(src
);
636 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), scalar
);
640 return new(p
->mem_ctx
) ir_constant(0.0f
);
642 return new(p
->mem_ctx
) ir_constant(1.0f
);
652 * Check if the RGB and Alpha sources and operands match for the given
653 * texture unit's combinder state. When the RGB and A sources and
654 * operands match, we can emit fewer instructions.
656 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
658 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
660 for (i
= 0; i
< numArgs
; i
++) {
661 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
664 switch (key
->unit
[unit
].OptA
[i
].Operand
) {
666 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
674 case OPR_ONE_MINUS_SRC_ALPHA
:
675 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
676 case OPR_ONE_MINUS_SRC_COLOR
:
677 case OPR_ONE_MINUS_SRC_ALPHA
:
684 return GL_FALSE
; /* impossible */
692 smear(texenv_fragment_program
*p
, ir_rvalue
*val
)
694 if (!val
->type
->is_scalar())
697 return swizzle_xxxx(val
);
701 emit_combine(texenv_fragment_program
*p
,
705 const struct mode_opt
*opt
)
707 ir_rvalue
*src
[MAX_COMBINER_TERMS
];
708 ir_rvalue
*tmp0
, *tmp1
;
711 assert(nr
<= MAX_COMBINER_TERMS
);
713 for (i
= 0; i
< nr
; i
++)
714 src
[i
] = emit_combine_source( p
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
721 return mul(src
[0], src
[1]);
724 return add(src
[0], src
[1]);
726 case MODE_ADD_SIGNED
:
727 return add(add(src
[0], src
[1]), new(p
->mem_ctx
) ir_constant(-0.5f
));
729 case MODE_INTERPOLATE
:
730 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
731 tmp0
= mul(src
[0], src
[2]);
732 tmp1
= mul(src
[1], sub(new(p
->mem_ctx
) ir_constant(1.0f
),
733 src
[2]->clone(p
->mem_ctx
, NULL
)));
734 return add(tmp0
, tmp1
);
737 return sub(src
[0], src
[1]);
740 case MODE_DOT3_RGBA_EXT
:
741 case MODE_DOT3_RGB_EXT
:
742 case MODE_DOT3_RGB
: {
743 tmp0
= mul(src
[0], new(p
->mem_ctx
) ir_constant(2.0f
));
744 tmp0
= add(tmp0
, new(p
->mem_ctx
) ir_constant(-1.0f
));
746 tmp1
= mul(src
[1], new(p
->mem_ctx
) ir_constant(2.0f
));
747 tmp1
= add(tmp1
, new(p
->mem_ctx
) ir_constant(-1.0f
));
749 return dot(swizzle_xyz(smear(p
, tmp0
)), swizzle_xyz(smear(p
, tmp1
)));
751 case MODE_MODULATE_ADD_ATI
:
752 return add(mul(src
[0], src
[2]), src
[1]);
754 case MODE_MODULATE_SIGNED_ADD_ATI
:
755 return add(add(mul(src
[0], src
[2]), src
[1]),
756 new(p
->mem_ctx
) ir_constant(-0.5f
));
758 case MODE_MODULATE_SUBTRACT_ATI
:
759 return sub(mul(src
[0], src
[2]), src
[1]);
761 case MODE_ADD_PRODUCTS
:
762 return add(mul(src
[0], src
[1]), mul(src
[2], src
[3]));
764 case MODE_ADD_PRODUCTS_SIGNED
:
765 return add(add(mul(src
[0], src
[1]), mul(src
[2], src
[3])),
766 new(p
->mem_ctx
) ir_constant(-0.5f
));
768 case MODE_BUMP_ENVMAP_ATI
:
769 /* special - not handled here */
779 * Generate instructions for one texture unit's env/combiner mode.
782 emit_texenv(texenv_fragment_program
*p
, GLuint unit
)
784 const struct state_key
*key
= p
->state
;
785 GLboolean rgb_saturate
, alpha_saturate
;
786 GLuint rgb_shift
, alpha_shift
;
788 if (!key
->unit
[unit
].enabled
) {
789 return get_source(p
, SRC_PREVIOUS
, 0);
791 if (key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
792 /* this isn't really a env stage delivering a color and handled elsewhere */
793 return get_source(p
, SRC_PREVIOUS
, 0);
796 switch (key
->unit
[unit
].ModeRGB
) {
797 case MODE_DOT3_RGB_EXT
:
798 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
801 case MODE_DOT3_RGBA_EXT
:
806 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
807 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
811 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
812 * We don't want to clamp twice.
815 rgb_saturate
= GL_FALSE
; /* saturate after rgb shift */
816 else if (need_saturate(key
->unit
[unit
].ModeRGB
))
817 rgb_saturate
= GL_TRUE
;
819 rgb_saturate
= GL_FALSE
;
822 alpha_saturate
= GL_FALSE
; /* saturate after alpha shift */
823 else if (need_saturate(key
->unit
[unit
].ModeA
))
824 alpha_saturate
= GL_TRUE
;
826 alpha_saturate
= GL_FALSE
;
828 ir_variable
*temp_var
= p
->make_temp(glsl_type::vec4_type
, "texenv_combine");
829 ir_dereference
*deref
;
832 /* Emit the RGB and A combine ops
834 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
835 args_match(key
, unit
)) {
836 val
= emit_combine(p
, unit
,
837 key
->unit
[unit
].NumArgsRGB
,
838 key
->unit
[unit
].ModeRGB
,
839 key
->unit
[unit
].OptRGB
);
844 p
->emit(assign(temp_var
, val
));
846 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
847 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
848 ir_rvalue
*val
= emit_combine(p
, unit
,
849 key
->unit
[unit
].NumArgsRGB
,
850 key
->unit
[unit
].ModeRGB
,
851 key
->unit
[unit
].OptRGB
);
855 p
->emit(assign(temp_var
, val
));
858 /* Need to do something to stop from re-emitting identical
859 * argument calculations here:
861 val
= emit_combine(p
, unit
,
862 key
->unit
[unit
].NumArgsRGB
,
863 key
->unit
[unit
].ModeRGB
,
864 key
->unit
[unit
].OptRGB
);
865 val
= swizzle_xyz(smear(p
, val
));
868 p
->emit(assign(temp_var
, val
, WRITEMASK_XYZ
));
870 val
= emit_combine(p
, unit
,
871 key
->unit
[unit
].NumArgsA
,
872 key
->unit
[unit
].ModeA
,
873 key
->unit
[unit
].OptA
);
874 val
= swizzle_w(smear(p
, val
));
877 p
->emit(assign(temp_var
, val
, WRITEMASK_W
));
880 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
882 /* Deal with the final shift:
884 if (alpha_shift
|| rgb_shift
) {
887 if (rgb_shift
== alpha_shift
) {
888 shift
= new(p
->mem_ctx
) ir_constant((float)(1 << rgb_shift
));
891 float const_data
[4] = {
892 float(1 << rgb_shift
),
893 float(1 << rgb_shift
),
894 float(1 << rgb_shift
),
895 float(1 << alpha_shift
)
897 shift
= new(p
->mem_ctx
) ir_constant(glsl_type::vec4_type
,
898 (ir_constant_data
*)const_data
);
901 return saturate(mul(deref
, shift
));
909 * Generate instruction for getting a texture source term.
911 static void load_texture( texenv_fragment_program
*p
, GLuint unit
)
913 ir_dereference
*deref
;
915 if (p
->src_texture
[unit
])
918 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
921 if (!(p
->state
->inputs_available
& (VARYING_BIT_TEX0
<< unit
))) {
922 texcoord
= get_current_attrib(p
, VERT_ATTRIB_TEX0
+ unit
);
923 } else if (p
->texcoord_tex
[unit
]) {
924 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(p
->texcoord_tex
[unit
]);
926 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
928 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
929 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(unit
);
930 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
931 tc_array
->data
.max_array_access
= MAX2(tc_array
->data
.max_array_access
, unit
);
934 if (!p
->state
->unit
[unit
].enabled
) {
935 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
937 p
->emit(p
->src_texture
[unit
]);
939 p
->emit(assign(p
->src_texture
[unit
], new(p
->mem_ctx
) ir_constant(0.0f
)));
943 const glsl_type
*sampler_type
= NULL
;
947 case TEXTURE_1D_INDEX
:
948 if (p
->state
->unit
[unit
].shadow
)
949 sampler_type
= p
->shader
->symbols
->get_type("sampler1DShadow");
951 sampler_type
= p
->shader
->symbols
->get_type("sampler1D");
954 case TEXTURE_1D_ARRAY_INDEX
:
955 if (p
->state
->unit
[unit
].shadow
)
956 sampler_type
= p
->shader
->symbols
->get_type("sampler1DArrayShadow");
958 sampler_type
= p
->shader
->symbols
->get_type("sampler1DArray");
961 case TEXTURE_2D_INDEX
:
962 if (p
->state
->unit
[unit
].shadow
)
963 sampler_type
= p
->shader
->symbols
->get_type("sampler2DShadow");
965 sampler_type
= p
->shader
->symbols
->get_type("sampler2D");
968 case TEXTURE_2D_ARRAY_INDEX
:
969 if (p
->state
->unit
[unit
].shadow
)
970 sampler_type
= p
->shader
->symbols
->get_type("sampler2DArrayShadow");
972 sampler_type
= p
->shader
->symbols
->get_type("sampler2DArray");
975 case TEXTURE_RECT_INDEX
:
976 if (p
->state
->unit
[unit
].shadow
)
977 sampler_type
= p
->shader
->symbols
->get_type("sampler2DRectShadow");
979 sampler_type
= p
->shader
->symbols
->get_type("sampler2DRect");
982 case TEXTURE_3D_INDEX
:
983 assert(!p
->state
->unit
[unit
].shadow
);
984 sampler_type
= p
->shader
->symbols
->get_type("sampler3D");
987 case TEXTURE_CUBE_INDEX
:
988 if (p
->state
->unit
[unit
].shadow
)
989 sampler_type
= p
->shader
->symbols
->get_type("samplerCubeShadow");
991 sampler_type
= p
->shader
->symbols
->get_type("samplerCube");
994 case TEXTURE_EXTERNAL_INDEX
:
995 assert(!p
->state
->unit
[unit
].shadow
);
996 sampler_type
= p
->shader
->symbols
->get_type("samplerExternalOES");
1001 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
1004 ir_texture
*tex
= new(p
->mem_ctx
) ir_texture(ir_tex
);
1007 char *sampler_name
= ralloc_asprintf(p
->mem_ctx
, "sampler_%d", unit
);
1008 ir_variable
*sampler
= new(p
->mem_ctx
) ir_variable(sampler_type
,
1011 p
->top_instructions
->push_head(sampler
);
1013 /* Set the texture unit for this sampler. The linker will pick this value
1014 * up and do-the-right-thing.
1016 * NOTE: The cast to int is important. Without it, the constant will have
1017 * type uint, and things later on may get confused.
1019 sampler
->constant_value
= new(p
->mem_ctx
) ir_constant(int(unit
));
1021 deref
= new(p
->mem_ctx
) ir_dereference_variable(sampler
);
1022 tex
->set_sampler(deref
, glsl_type::vec4_type
);
1024 tex
->coordinate
= new(p
->mem_ctx
) ir_swizzle(texcoord
, 0, 1, 2, 3, coords
);
1026 if (p
->state
->unit
[unit
].shadow
) {
1027 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1028 tex
->shadow_comparitor
= new(p
->mem_ctx
) ir_swizzle(texcoord
,
1034 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
1035 tex
->projector
= swizzle_w(texcoord
);
1037 p
->emit(assign(p
->src_texture
[unit
], tex
));
1041 load_texenv_source(texenv_fragment_program
*p
,
1042 GLuint src
, GLuint unit
)
1046 load_texture(p
, unit
);
1057 load_texture(p
, src
- SRC_TEXTURE0
);
1061 /* not a texture src - do nothing */
1068 * Generate instructions for loading all texture source terms.
1071 load_texunit_sources( texenv_fragment_program
*p
, GLuint unit
)
1073 const struct state_key
*key
= p
->state
;
1076 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1077 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1080 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1081 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1088 * Generate instructions for loading bump map textures.
1091 load_texunit_bumpmap( texenv_fragment_program
*p
, GLuint unit
)
1093 const struct state_key
*key
= p
->state
;
1094 GLuint bumpedUnitNr
= key
->unit
[unit
].OptRGB
[1].Source
- SRC_TEXTURE0
;
1096 ir_rvalue
*texcoord
;
1097 ir_variable
*rot_mat_0
, *rot_mat_1
;
1099 rot_mat_0
= p
->shader
->symbols
->get_variable("gl_BumpRotMatrix0MESA");
1101 rot_mat_1
= p
->shader
->symbols
->get_variable("gl_BumpRotMatrix1MESA");
1104 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
1106 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
1107 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(bumpedUnitNr
);
1108 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
1109 tc_array
->data
.max_array_access
= MAX2(tc_array
->data
.max_array_access
, unit
);
1111 load_texenv_source( p
, unit
+ SRC_TEXTURE0
, unit
);
1113 /* Apply rot matrix and add coords to be available in next phase.
1114 * dest = Arg1 + (Arg0.xx * rotMat0) + (Arg0.yy * rotMat1)
1115 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1117 ir_rvalue
*bump_x
, *bump_y
;
1119 texcoord
= smear(p
, texcoord
);
1121 /* bump_texcoord = texcoord */
1122 ir_variable
*bumped
= p
->make_temp(texcoord
->type
, "bump_texcoord");
1124 p
->emit(assign(bumped
, texcoord
));
1126 /* bump_texcoord.xy += arg0.x * rotmat0 + arg0.y * rotmat1 */
1127 bump
= get_source(p
, key
->unit
[unit
].OptRGB
[0].Source
, unit
);
1128 bump_x
= mul(swizzle_x(bump
), rot_mat_0
);
1129 bump_y
= mul(swizzle_y(bump
->clone(p
->mem_ctx
, NULL
)), rot_mat_1
);
1131 p
->emit(assign(bumped
, add(swizzle_xy(bumped
), add(bump_x
, bump_y
)),
1134 p
->texcoord_tex
[bumpedUnitNr
] = bumped
;
1138 * Applies the fog calculations.
1140 * This is basically like the ARB_fragment_prorgam fog options. Note
1141 * that ffvertex_prog.c produces fogcoord for us when
1142 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
1145 emit_fog_instructions(texenv_fragment_program
*p
,
1146 ir_rvalue
*fragcolor
)
1148 struct state_key
*key
= p
->state
;
1149 ir_rvalue
*f
, *temp
;
1150 ir_variable
*params
, *oparams
;
1151 ir_variable
*fogcoord
;
1153 /* Temporary storage for the whole fog result. Fog calculations
1154 * only affect rgb so we're hanging on to the .a value of fragcolor
1157 ir_variable
*fog_result
= p
->make_temp(glsl_type::vec4_type
, "fog_result");
1158 p
->emit(assign(fog_result
, fragcolor
));
1160 fragcolor
= swizzle_xyz(fog_result
);
1162 oparams
= p
->shader
->symbols
->get_variable("gl_FogParamsOptimizedMESA");
1164 fogcoord
= p
->shader
->symbols
->get_variable("gl_FogFragCoord");
1166 params
= p
->shader
->symbols
->get_variable("gl_Fog");
1168 f
= new(p
->mem_ctx
) ir_dereference_variable(fogcoord
);
1170 ir_variable
*f_var
= p
->make_temp(glsl_type::float_type
, "fog_factor");
1172 switch (key
->fog_mode
) {
1174 /* f = (end - z) / (end - start)
1176 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
1177 * (end / (end - start)) so we can generate a single MAD.
1179 f
= add(mul(f
, swizzle_x(oparams
)), swizzle_y(oparams
));
1182 /* f = e^(-(density * fogcoord))
1184 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
1185 * use EXP2 which is generally the native instruction without
1186 * having to do any further math on the fog density uniform.
1188 f
= mul(f
, swizzle_z(oparams
));
1189 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1190 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1193 /* f = e^(-(density * fogcoord)^2)
1195 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
1196 * can do this like FOG_EXP but with a squaring after the
1197 * multiply by density.
1199 ir_variable
*temp_var
= p
->make_temp(glsl_type::float_type
, "fog_temp");
1200 p
->emit(assign(temp_var
, mul(f
, swizzle_w(oparams
))));
1202 f
= mul(temp_var
, temp_var
);
1203 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
1204 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
1208 p
->emit(assign(f_var
, saturate(f
)));
1210 f
= sub(new(p
->mem_ctx
) ir_constant(1.0f
), f_var
);
1211 temp
= new(p
->mem_ctx
) ir_dereference_variable(params
);
1212 temp
= new(p
->mem_ctx
) ir_dereference_record(temp
, "color");
1213 temp
= mul(swizzle_xyz(temp
), f
);
1215 p
->emit(assign(fog_result
, add(temp
, mul(fragcolor
, f_var
)), WRITEMASK_XYZ
));
1217 return new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
1221 emit_instructions(texenv_fragment_program
*p
)
1223 struct state_key
*key
= p
->state
;
1226 if (key
->enabled_units
) {
1227 /* Zeroth pass - bump map textures first */
1228 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1229 if (key
->unit
[unit
].enabled
&&
1230 key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1231 load_texunit_bumpmap(p
, unit
);
1235 /* First pass - to support texture_env_crossbar, first identify
1236 * all referenced texture sources and emit texld instructions
1239 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1240 if (key
->unit
[unit
].enabled
) {
1241 load_texunit_sources(p
, unit
);
1244 /* Second pass - emit combine instructions to build final color:
1246 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
1247 if (key
->unit
[unit
].enabled
) {
1248 p
->src_previous
= emit_texenv(p
, unit
);
1253 ir_rvalue
*cf
= get_source(p
, SRC_PREVIOUS
, 0);
1255 if (key
->separate_specular
) {
1256 ir_variable
*spec_result
= p
->make_temp(glsl_type::vec4_type
,
1258 p
->emit(assign(spec_result
, cf
));
1260 ir_rvalue
*secondary
;
1261 if (p
->state
->inputs_available
& VARYING_BIT_COL1
) {
1263 p
->shader
->symbols
->get_variable("gl_SecondaryColor");
1265 secondary
= swizzle_xyz(var
);
1267 secondary
= swizzle_xyz(get_current_attrib(p
, VERT_ATTRIB_COLOR1
));
1270 p
->emit(assign(spec_result
, add(swizzle_xyz(spec_result
), secondary
),
1273 cf
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1276 if (key
->fog_enabled
) {
1277 cf
= emit_fog_instructions(p
, cf
);
1280 ir_variable
*frag_color
= p
->shader
->symbols
->get_variable("gl_FragColor");
1282 p
->emit(assign(frag_color
, cf
));
1286 * Generate a new fragment program which implements the context's
1287 * current texture env/combine mode.
1289 static struct gl_shader_program
*
1290 create_new_program(struct gl_context
*ctx
, struct state_key
*key
)
1292 texenv_fragment_program p
;
1294 _mesa_glsl_parse_state
*state
;
1296 p
.mem_ctx
= ralloc_context(NULL
);
1297 p
.shader
= ctx
->Driver
.NewShader(ctx
, 0, GL_FRAGMENT_SHADER
);
1298 p
.shader
->ir
= new(p
.shader
) exec_list
;
1299 state
= new(p
.shader
) _mesa_glsl_parse_state(ctx
, MESA_SHADER_FRAGMENT
,
1301 p
.shader
->symbols
= state
->symbols
;
1302 p
.top_instructions
= p
.shader
->ir
;
1303 p
.instructions
= p
.shader
->ir
;
1305 p
.shader_program
= ctx
->Driver
.NewShaderProgram(ctx
, 0);
1307 /* Tell the linker to ignore the fact that we're building a
1308 * separate shader, in case we're in a GLES2 context that would
1309 * normally reject that. The real problem is that we're building a
1310 * fixed function program in a GLES2 context at all, but that's a
1311 * big mess to clean up.
1313 p
.shader_program
->InternalSeparateShader
= GL_TRUE
;
1315 state
->language_version
= 130;
1316 state
->es_shader
= false;
1317 if (_mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
)
1318 state
->OES_EGL_image_external_enable
= true;
1319 _mesa_glsl_initialize_types(state
);
1320 _mesa_glsl_initialize_variables(p
.instructions
, state
);
1322 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1323 p
.src_texture
[unit
] = NULL
;
1324 p
.texcoord_tex
[unit
] = NULL
;
1327 p
.src_previous
= NULL
;
1329 ir_function
*main_f
= new(p
.mem_ctx
) ir_function("main");
1331 state
->symbols
->add_function(main_f
);
1333 ir_function_signature
*main_sig
=
1334 new(p
.mem_ctx
) ir_function_signature(p
.shader
->symbols
->get_type("void"));
1335 main_sig
->is_defined
= true;
1336 main_f
->add_signature(main_sig
);
1338 p
.instructions
= &main_sig
->body
;
1339 if (key
->num_draw_buffers
)
1340 emit_instructions(&p
);
1342 validate_ir_tree(p
.shader
->ir
);
1344 const struct gl_shader_compiler_options
*options
=
1345 &ctx
->ShaderCompilerOptions
[MESA_SHADER_FRAGMENT
];
1347 while (do_common_optimization(p
.shader
->ir
, false, false, 32, options
))
1349 reparent_ir(p
.shader
->ir
, p
.shader
->ir
);
1351 p
.shader
->CompileStatus
= true;
1352 p
.shader
->Version
= state
->language_version
;
1353 p
.shader
->uses_builtin_functions
= state
->uses_builtin_functions
;
1354 p
.shader_program
->Shaders
=
1355 (gl_shader
**)malloc(sizeof(*p
.shader_program
->Shaders
));
1356 p
.shader_program
->Shaders
[0] = p
.shader
;
1357 p
.shader_program
->NumShaders
= 1;
1359 _mesa_glsl_link_shader(ctx
, p
.shader_program
);
1361 if (!p
.shader_program
->LinkStatus
)
1362 _mesa_problem(ctx
, "Failed to link fixed function fragment shader: %s\n",
1363 p
.shader_program
->InfoLog
);
1365 ralloc_free(p
.mem_ctx
);
1366 return p
.shader_program
;
1372 * Return a fragment program which implements the current
1373 * fixed-function texture, fog and color-sum operations.
1375 struct gl_shader_program
*
1376 _mesa_get_fixed_func_fragment_program(struct gl_context
*ctx
)
1378 struct gl_shader_program
*shader_program
;
1379 struct state_key key
;
1382 keySize
= make_state_key(ctx
, &key
);
1384 shader_program
= (struct gl_shader_program
*)
1385 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1388 if (!shader_program
) {
1389 shader_program
= create_new_program(ctx
, &key
);
1391 _mesa_shader_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1392 &key
, keySize
, shader_program
);
1395 return shader_program
;