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/state.h"
37 #include "main/texenvprogram.h"
38 #include "main/texobj.h"
39 #include "main/uniforms.h"
40 #include "compiler/glsl/ir_builder.h"
41 #include "compiler/glsl/ir_optimization.h"
42 #include "compiler/glsl/glsl_parser_extras.h"
43 #include "compiler/glsl/glsl_symbol_table.h"
44 #include "compiler/glsl_types.h"
45 #include "program/ir_to_mesa.h"
46 #include "program/program.h"
47 #include "program/programopt.h"
48 #include "program/prog_cache.h"
49 #include "program/prog_instruction.h"
50 #include "program/prog_parameter.h"
51 #include "program/prog_print.h"
52 #include "program/prog_statevars.h"
53 #include "util/bitscan.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.
73 texenv_doing_secondary_color(struct gl_context
*ctx
)
75 if (ctx
->Light
.Enabled
&&
76 (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
))
79 if (ctx
->Fog
.ColorSumEnabled
)
86 GLuint nr_enabled_units
:4;
87 GLuint separate_specular
:1;
88 GLuint fog_mode
:2; /**< FOG_x */
89 GLuint inputs_available
:12;
90 GLuint num_draw_buffers
:4;
92 /* NOTE: This array of structs must be last! (see "keySize" below) */
95 GLuint source_index
:4; /**< TEXTURE_x_INDEX */
99 * These are taken from struct gl_tex_env_combine_packed
104 GLuint ScaleShiftRGB
:2;
105 GLuint ScaleShiftA
:2;
108 struct gl_tex_env_argument ArgsRGB
[MAX_COMBINER_TERMS
];
109 struct gl_tex_env_argument ArgsA
[MAX_COMBINER_TERMS
];
111 } unit
[MAX_TEXTURE_COORD_UNITS
];
116 * Do we need to clamp the results of the given texture env/combine mode?
117 * If the inputs to the mode are in [0,1] we don't always have to clamp
121 need_saturate( GLuint mode
)
124 case TEXENV_MODE_REPLACE
:
125 case TEXENV_MODE_MODULATE
:
126 case TEXENV_MODE_INTERPOLATE
:
128 case TEXENV_MODE_ADD
:
129 case TEXENV_MODE_ADD_SIGNED
:
130 case TEXENV_MODE_SUBTRACT
:
131 case TEXENV_MODE_DOT3_RGB
:
132 case TEXENV_MODE_DOT3_RGB_EXT
:
133 case TEXENV_MODE_DOT3_RGBA
:
134 case TEXENV_MODE_DOT3_RGBA_EXT
:
135 case TEXENV_MODE_MODULATE_ADD_ATI
:
136 case TEXENV_MODE_MODULATE_SIGNED_ADD_ATI
:
137 case TEXENV_MODE_MODULATE_SUBTRACT_ATI
:
138 case TEXENV_MODE_ADD_PRODUCTS_NV
:
139 case TEXENV_MODE_ADD_PRODUCTS_SIGNED_NV
:
147 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
150 * Identify all possible varying inputs. The fragment program will
151 * never reference non-varying inputs, but will track them via state
154 * This function figures out all the inputs that the fragment program
155 * has access to and filters input bitmask.
157 static GLbitfield
filter_fp_input_mask( GLbitfield fp_inputs
,
158 struct gl_context
*ctx
)
160 if (ctx
->VertexProgram
._Overriden
) {
161 /* Somebody's messing with the vertex program and we don't have
162 * a clue what's happening. Assume that it could be producing
163 * all possible outputs.
168 if (ctx
->RenderMode
== GL_FEEDBACK
) {
169 /* _NEW_RENDERMODE */
170 return fp_inputs
& (VARYING_BIT_COL0
| VARYING_BIT_TEX0
);
174 const GLboolean vertexShader
=
175 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] != NULL
;
176 const GLboolean vertexProgram
= _mesa_arb_vertex_program_enabled(ctx
);
178 if (!(vertexProgram
|| vertexShader
)) {
179 /* Fixed function vertex logic */
180 GLbitfield possible_inputs
= 0;
182 /* _NEW_VARYING_VP_INPUTS */
183 GLbitfield varying_inputs
= ctx
->varying_vp_inputs
;
185 /* These get generated in the setup routine regardless of the
189 if (ctx
->Point
.PointSprite
) {
190 /* All texture varyings are possible to use */
191 possible_inputs
= VARYING_BITS_TEX_ANY
;
194 /* _NEW_TEXTURE_STATE */
195 const GLbitfield possible_tex_inputs
=
196 ctx
->Texture
._TexGenEnabled
|
197 ctx
->Texture
._TexMatEnabled
|
198 ((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
);
200 possible_inputs
= (possible_tex_inputs
<< VARYING_SLOT_TEX0
);
203 /* First look at what values may be computed by the generated
207 if (ctx
->Light
.Enabled
) {
208 possible_inputs
|= VARYING_BIT_COL0
;
210 if (texenv_doing_secondary_color(ctx
))
211 possible_inputs
|= VARYING_BIT_COL1
;
214 /* Then look at what might be varying as a result of enabled
217 if (varying_inputs
& VERT_BIT_COLOR0
)
218 possible_inputs
|= VARYING_BIT_COL0
;
219 if (varying_inputs
& VERT_BIT_COLOR1
)
220 possible_inputs
|= VARYING_BIT_COL1
;
222 return fp_inputs
& possible_inputs
;
225 /* calculate from vp->outputs */
226 struct gl_program
*vprog
;
228 /* Choose GLSL vertex shader over ARB vertex program. Need this
229 * since vertex shader state validation comes after fragment state
230 * validation (see additional comments in state.c).
233 vprog
= ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
];
235 vprog
= ctx
->VertexProgram
.Current
;
237 GLbitfield possible_inputs
= vprog
->info
.outputs_written
;
239 /* These get generated in the setup routine regardless of the
243 if (ctx
->Point
.PointSprite
) {
244 /* All texture varyings are possible to use */
245 possible_inputs
|= VARYING_BITS_TEX_ANY
;
248 return fp_inputs
& possible_inputs
;
253 * Examine current texture environment state and generate a unique
254 * key to identify it.
256 static GLuint
make_state_key( struct gl_context
*ctx
, struct state_key
*key
)
258 GLbitfield inputs_referenced
= VARYING_BIT_COL0
;
262 memset(key
, 0, sizeof(*key
));
264 /* _NEW_TEXTURE_OBJECT */
265 mask
= ctx
->Texture
._EnabledCoordUnits
;
268 i
= u_bit_scan(&mask
);
269 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
270 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
271 const struct gl_tex_env_combine_packed
*comb
=
272 &ctx
->Texture
.FixedFuncUnit
[i
]._CurrentCombinePacked
;
277 key
->unit
[i
].enabled
= 1;
278 inputs_referenced
|= VARYING_BIT_TEX(i
);
280 key
->unit
[i
].source_index
= texObj
->TargetIndex
;
282 const struct gl_sampler_object
*samp
= _mesa_get_samplerobj(ctx
, i
);
283 if (samp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) {
284 const GLenum format
= _mesa_texture_base_format(texObj
);
285 key
->unit
[i
].shadow
= (format
== GL_DEPTH_COMPONENT
||
286 format
== GL_DEPTH_STENCIL_EXT
);
289 key
->unit
[i
].ModeRGB
= comb
->ModeRGB
;
290 key
->unit
[i
].ModeA
= comb
->ModeA
;
291 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
292 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
293 key
->unit
[i
].NumArgsRGB
= comb
->NumArgsRGB
;
294 key
->unit
[i
].NumArgsA
= comb
->NumArgsA
;
296 memcpy(key
->unit
[i
].ArgsRGB
, comb
->ArgsRGB
, sizeof comb
->ArgsRGB
);
297 memcpy(key
->unit
[i
].ArgsA
, comb
->ArgsA
, sizeof comb
->ArgsA
);
300 key
->nr_enabled_units
= i
+ 1;
302 /* _NEW_LIGHT | _NEW_FOG */
303 if (texenv_doing_secondary_color(ctx
)) {
304 key
->separate_specular
= 1;
305 inputs_referenced
|= VARYING_BIT_COL1
;
309 key
->fog_mode
= ctx
->Fog
._PackedEnabledMode
;
312 key
->num_draw_buffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
315 if (ctx
->Color
.AlphaEnabled
&& key
->num_draw_buffers
== 0) {
316 /* if alpha test is enabled we need to emit at least one color */
317 key
->num_draw_buffers
= 1;
320 key
->inputs_available
= filter_fp_input_mask(inputs_referenced
, ctx
);
322 /* compute size of state key, ignoring unused texture units */
323 keySize
= sizeof(*key
) - sizeof(key
->unit
)
324 + key
->nr_enabled_units
* sizeof(key
->unit
[0]);
330 /** State used to build the fragment program:
332 class texenv_fragment_program
: public ir_factory
{
334 struct gl_shader_program
*shader_program
;
335 struct gl_shader
*shader
;
336 exec_list
*top_instructions
;
337 struct state_key
*state
;
339 ir_variable
*src_texture
[MAX_TEXTURE_COORD_UNITS
];
340 /* Reg containing each texture unit's sampled texture color,
344 /* Texcoord override from bumpmapping. */
345 ir_variable
*texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
347 /* Reg containing texcoord for a texture unit,
348 * needed for bump mapping, else undef.
351 ir_rvalue
*src_previous
; /**< Reg containing color from previous
352 * stage. May need to be decl'd.
357 get_current_attrib(texenv_fragment_program
*p
, GLuint attrib
)
359 ir_variable
*current
;
362 current
= p
->shader
->symbols
->get_variable("gl_CurrentAttribFragMESA");
364 current
->data
.max_array_access
= MAX2(current
->data
.max_array_access
, (int)attrib
);
365 val
= new(p
->mem_ctx
) ir_dereference_variable(current
);
366 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(attrib
);
367 return new(p
->mem_ctx
) ir_dereference_array(val
, index
);
371 get_gl_Color(texenv_fragment_program
*p
)
373 if (p
->state
->inputs_available
& VARYING_BIT_COL0
) {
374 ir_variable
*var
= p
->shader
->symbols
->get_variable("gl_Color");
376 return new(p
->mem_ctx
) ir_dereference_variable(var
);
378 return get_current_attrib(p
, VERT_ATTRIB_COLOR0
);
383 get_source(texenv_fragment_program
*p
,
384 GLuint src
, GLuint unit
)
387 ir_dereference
*deref
;
390 case TEXENV_SRC_TEXTURE
:
391 return new(p
->mem_ctx
) ir_dereference_variable(p
->src_texture
[unit
]);
393 case TEXENV_SRC_TEXTURE0
:
394 case TEXENV_SRC_TEXTURE1
:
395 case TEXENV_SRC_TEXTURE2
:
396 case TEXENV_SRC_TEXTURE3
:
397 case TEXENV_SRC_TEXTURE4
:
398 case TEXENV_SRC_TEXTURE5
:
399 case TEXENV_SRC_TEXTURE6
:
400 case TEXENV_SRC_TEXTURE7
:
401 return new(p
->mem_ctx
)
402 ir_dereference_variable(p
->src_texture
[src
- TEXENV_SRC_TEXTURE0
]);
404 case TEXENV_SRC_CONSTANT
:
405 var
= p
->shader
->symbols
->get_variable("gl_TextureEnvColor");
407 deref
= new(p
->mem_ctx
) ir_dereference_variable(var
);
408 var
->data
.max_array_access
= MAX2(var
->data
.max_array_access
, (int)unit
);
409 return new(p
->mem_ctx
) ir_dereference_array(deref
,
410 new(p
->mem_ctx
) ir_constant(unit
));
412 case TEXENV_SRC_PRIMARY_COLOR
:
413 var
= p
->shader
->symbols
->get_variable("gl_Color");
415 return new(p
->mem_ctx
) ir_dereference_variable(var
);
417 case TEXENV_SRC_ZERO
:
418 return new(p
->mem_ctx
) ir_constant(0.0f
);
421 return new(p
->mem_ctx
) ir_constant(1.0f
);
423 case TEXENV_SRC_PREVIOUS
:
424 if (!p
->src_previous
) {
425 return get_gl_Color(p
);
427 return p
->src_previous
->clone(p
->mem_ctx
, NULL
);
437 emit_combine_source(texenv_fragment_program
*p
,
444 src
= get_source(p
, source
, unit
);
447 case TEXENV_OPR_ONE_MINUS_COLOR
:
448 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), src
);
450 case TEXENV_OPR_ALPHA
:
451 return src
->type
->is_scalar() ? src
: swizzle_w(src
);
453 case TEXENV_OPR_ONE_MINUS_ALPHA
: {
454 ir_rvalue
*const scalar
= src
->type
->is_scalar() ? src
: swizzle_w(src
);
456 return sub(new(p
->mem_ctx
) ir_constant(1.0f
), scalar
);
459 case TEXENV_OPR_COLOR
:
469 * Check if the RGB and Alpha sources and operands match for the given
470 * texture unit's combinder state. When the RGB and A sources and
471 * operands match, we can emit fewer instructions.
473 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
475 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
477 for (i
= 0; i
< numArgs
; i
++) {
478 if (key
->unit
[unit
].ArgsA
[i
].Source
!= key
->unit
[unit
].ArgsRGB
[i
].Source
)
481 switch (key
->unit
[unit
].ArgsA
[i
].Operand
) {
482 case TEXENV_OPR_ALPHA
:
483 switch (key
->unit
[unit
].ArgsRGB
[i
].Operand
) {
484 case TEXENV_OPR_COLOR
:
485 case TEXENV_OPR_ALPHA
:
491 case TEXENV_OPR_ONE_MINUS_ALPHA
:
492 switch (key
->unit
[unit
].ArgsRGB
[i
].Operand
) {
493 case TEXENV_OPR_ONE_MINUS_COLOR
:
494 case TEXENV_OPR_ONE_MINUS_ALPHA
:
501 return GL_FALSE
; /* impossible */
509 smear(ir_rvalue
*val
)
511 if (!val
->type
->is_scalar())
514 return swizzle_xxxx(val
);
518 emit_combine(texenv_fragment_program
*p
,
522 const struct gl_tex_env_argument
*opt
)
524 ir_rvalue
*src
[MAX_COMBINER_TERMS
];
525 ir_rvalue
*tmp0
, *tmp1
;
528 assert(nr
<= MAX_COMBINER_TERMS
);
530 for (i
= 0; i
< nr
; i
++)
531 src
[i
] = emit_combine_source( p
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
534 case TEXENV_MODE_REPLACE
:
537 case TEXENV_MODE_MODULATE
:
538 return mul(src
[0], src
[1]);
540 case TEXENV_MODE_ADD
:
541 return add(src
[0], src
[1]);
543 case TEXENV_MODE_ADD_SIGNED
:
544 return add(add(src
[0], src
[1]), new(p
->mem_ctx
) ir_constant(-0.5f
));
546 case TEXENV_MODE_INTERPOLATE
:
547 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
548 tmp0
= mul(src
[0], src
[2]);
549 tmp1
= mul(src
[1], sub(new(p
->mem_ctx
) ir_constant(1.0f
),
550 src
[2]->clone(p
->mem_ctx
, NULL
)));
551 return add(tmp0
, tmp1
);
553 case TEXENV_MODE_SUBTRACT
:
554 return sub(src
[0], src
[1]);
556 case TEXENV_MODE_DOT3_RGBA
:
557 case TEXENV_MODE_DOT3_RGBA_EXT
:
558 case TEXENV_MODE_DOT3_RGB_EXT
:
559 case TEXENV_MODE_DOT3_RGB
: {
560 tmp0
= mul(src
[0], new(p
->mem_ctx
) ir_constant(2.0f
));
561 tmp0
= add(tmp0
, new(p
->mem_ctx
) ir_constant(-1.0f
));
563 tmp1
= mul(src
[1], new(p
->mem_ctx
) ir_constant(2.0f
));
564 tmp1
= add(tmp1
, new(p
->mem_ctx
) ir_constant(-1.0f
));
566 return dot(swizzle_xyz(smear(tmp0
)), swizzle_xyz(smear(tmp1
)));
568 case TEXENV_MODE_MODULATE_ADD_ATI
:
569 return add(mul(src
[0], src
[2]), src
[1]);
571 case TEXENV_MODE_MODULATE_SIGNED_ADD_ATI
:
572 return add(add(mul(src
[0], src
[2]), src
[1]),
573 new(p
->mem_ctx
) ir_constant(-0.5f
));
575 case TEXENV_MODE_MODULATE_SUBTRACT_ATI
:
576 return sub(mul(src
[0], src
[2]), src
[1]);
578 case TEXENV_MODE_ADD_PRODUCTS_NV
:
579 return add(mul(src
[0], src
[1]), mul(src
[2], src
[3]));
581 case TEXENV_MODE_ADD_PRODUCTS_SIGNED_NV
:
582 return add(add(mul(src
[0], src
[1]), mul(src
[2], src
[3])),
583 new(p
->mem_ctx
) ir_constant(-0.5f
));
591 * Generate instructions for one texture unit's env/combiner mode.
594 emit_texenv(texenv_fragment_program
*p
, GLuint unit
)
596 const struct state_key
*key
= p
->state
;
597 GLboolean rgb_saturate
, alpha_saturate
;
598 GLuint rgb_shift
, alpha_shift
;
600 if (!key
->unit
[unit
].enabled
) {
601 return get_source(p
, TEXENV_SRC_PREVIOUS
, 0);
604 switch (key
->unit
[unit
].ModeRGB
) {
605 case TEXENV_MODE_DOT3_RGB_EXT
:
606 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
609 case TEXENV_MODE_DOT3_RGBA_EXT
:
614 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
615 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
619 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
620 * We don't want to clamp twice.
623 rgb_saturate
= GL_FALSE
; /* saturate after rgb shift */
624 else if (need_saturate(key
->unit
[unit
].ModeRGB
))
625 rgb_saturate
= GL_TRUE
;
627 rgb_saturate
= GL_FALSE
;
630 alpha_saturate
= GL_FALSE
; /* saturate after alpha shift */
631 else if (need_saturate(key
->unit
[unit
].ModeA
))
632 alpha_saturate
= GL_TRUE
;
634 alpha_saturate
= GL_FALSE
;
636 ir_variable
*temp_var
= p
->make_temp(glsl_type::vec4_type
, "texenv_combine");
637 ir_dereference
*deref
;
640 /* Emit the RGB and A combine ops
642 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
643 args_match(key
, unit
)) {
644 val
= emit_combine(p
, unit
,
645 key
->unit
[unit
].NumArgsRGB
,
646 key
->unit
[unit
].ModeRGB
,
647 key
->unit
[unit
].ArgsRGB
);
652 p
->emit(assign(temp_var
, val
));
654 else if (key
->unit
[unit
].ModeRGB
== TEXENV_MODE_DOT3_RGBA_EXT
||
655 key
->unit
[unit
].ModeRGB
== TEXENV_MODE_DOT3_RGBA
) {
656 ir_rvalue
*val
= emit_combine(p
, unit
,
657 key
->unit
[unit
].NumArgsRGB
,
658 key
->unit
[unit
].ModeRGB
,
659 key
->unit
[unit
].ArgsRGB
);
663 p
->emit(assign(temp_var
, val
));
666 /* Need to do something to stop from re-emitting identical
667 * argument calculations here:
669 val
= emit_combine(p
, unit
,
670 key
->unit
[unit
].NumArgsRGB
,
671 key
->unit
[unit
].ModeRGB
,
672 key
->unit
[unit
].ArgsRGB
);
673 val
= swizzle_xyz(smear(val
));
676 p
->emit(assign(temp_var
, val
, WRITEMASK_XYZ
));
678 val
= emit_combine(p
, unit
,
679 key
->unit
[unit
].NumArgsA
,
680 key
->unit
[unit
].ModeA
,
681 key
->unit
[unit
].ArgsA
);
682 val
= swizzle_w(smear(val
));
685 p
->emit(assign(temp_var
, val
, WRITEMASK_W
));
688 deref
= new(p
->mem_ctx
) ir_dereference_variable(temp_var
);
690 /* Deal with the final shift:
692 if (alpha_shift
|| rgb_shift
) {
695 if (rgb_shift
== alpha_shift
) {
696 shift
= new(p
->mem_ctx
) ir_constant((float)(1 << rgb_shift
));
699 ir_constant_data const_data
;
701 const_data
.f
[0] = float(1 << rgb_shift
);
702 const_data
.f
[1] = float(1 << rgb_shift
);
703 const_data
.f
[2] = float(1 << rgb_shift
);
704 const_data
.f
[3] = float(1 << alpha_shift
);
706 shift
= new(p
->mem_ctx
) ir_constant(glsl_type::vec4_type
,
710 return saturate(mul(deref
, shift
));
718 * Generate instruction for getting a texture source term.
720 static void load_texture( texenv_fragment_program
*p
, GLuint unit
)
722 ir_dereference
*deref
;
724 if (p
->src_texture
[unit
])
727 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
730 if (!(p
->state
->inputs_available
& (VARYING_BIT_TEX0
<< unit
))) {
731 texcoord
= get_current_attrib(p
, VERT_ATTRIB_TEX0
+ unit
);
732 } else if (p
->texcoord_tex
[unit
]) {
733 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(p
->texcoord_tex
[unit
]);
735 ir_variable
*tc_array
= p
->shader
->symbols
->get_variable("gl_TexCoord");
737 texcoord
= new(p
->mem_ctx
) ir_dereference_variable(tc_array
);
738 ir_rvalue
*index
= new(p
->mem_ctx
) ir_constant(unit
);
739 texcoord
= new(p
->mem_ctx
) ir_dereference_array(texcoord
, index
);
740 tc_array
->data
.max_array_access
= MAX2(tc_array
->data
.max_array_access
, (int)unit
);
743 if (!p
->state
->unit
[unit
].enabled
) {
744 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
746 p
->emit(p
->src_texture
[unit
]);
748 p
->emit(assign(p
->src_texture
[unit
], new(p
->mem_ctx
) ir_constant(0.0f
)));
752 const glsl_type
*sampler_type
= NULL
;
756 case TEXTURE_1D_INDEX
:
757 if (p
->state
->unit
[unit
].shadow
)
758 sampler_type
= glsl_type::sampler1DShadow_type
;
760 sampler_type
= glsl_type::sampler1D_type
;
763 case TEXTURE_1D_ARRAY_INDEX
:
764 if (p
->state
->unit
[unit
].shadow
)
765 sampler_type
= glsl_type::sampler1DArrayShadow_type
;
767 sampler_type
= glsl_type::sampler1DArray_type
;
770 case TEXTURE_2D_INDEX
:
771 if (p
->state
->unit
[unit
].shadow
)
772 sampler_type
= glsl_type::sampler2DShadow_type
;
774 sampler_type
= glsl_type::sampler2D_type
;
777 case TEXTURE_2D_ARRAY_INDEX
:
778 if (p
->state
->unit
[unit
].shadow
)
779 sampler_type
= glsl_type::sampler2DArrayShadow_type
;
781 sampler_type
= glsl_type::sampler2DArray_type
;
784 case TEXTURE_RECT_INDEX
:
785 if (p
->state
->unit
[unit
].shadow
)
786 sampler_type
= glsl_type::sampler2DRectShadow_type
;
788 sampler_type
= glsl_type::sampler2DRect_type
;
791 case TEXTURE_3D_INDEX
:
792 assert(!p
->state
->unit
[unit
].shadow
);
793 sampler_type
= glsl_type::sampler3D_type
;
796 case TEXTURE_CUBE_INDEX
:
797 if (p
->state
->unit
[unit
].shadow
)
798 sampler_type
= glsl_type::samplerCubeShadow_type
;
800 sampler_type
= glsl_type::samplerCube_type
;
803 case TEXTURE_EXTERNAL_INDEX
:
804 assert(!p
->state
->unit
[unit
].shadow
);
805 sampler_type
= glsl_type::samplerExternalOES_type
;
810 p
->src_texture
[unit
] = p
->make_temp(glsl_type::vec4_type
,
813 ir_texture
*tex
= new(p
->mem_ctx
) ir_texture(ir_tex
);
816 char *sampler_name
= ralloc_asprintf(p
->mem_ctx
, "sampler_%d", unit
);
817 ir_variable
*sampler
= new(p
->mem_ctx
) ir_variable(sampler_type
,
820 p
->top_instructions
->push_head(sampler
);
822 /* Set the texture unit for this sampler in the same way that
823 * layout(binding=X) would.
825 sampler
->data
.explicit_binding
= true;
826 sampler
->data
.binding
= unit
;
828 deref
= new(p
->mem_ctx
) ir_dereference_variable(sampler
);
829 tex
->set_sampler(deref
, glsl_type::vec4_type
);
831 tex
->coordinate
= new(p
->mem_ctx
) ir_swizzle(texcoord
, 0, 1, 2, 3, coords
);
833 if (p
->state
->unit
[unit
].shadow
) {
834 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
835 tex
->shadow_comparator
= new(p
->mem_ctx
) ir_swizzle(texcoord
,
841 texcoord
= texcoord
->clone(p
->mem_ctx
, NULL
);
842 tex
->projector
= swizzle_w(texcoord
);
844 p
->emit(assign(p
->src_texture
[unit
], tex
));
848 load_texenv_source(texenv_fragment_program
*p
,
849 GLuint src
, GLuint unit
)
852 case TEXENV_SRC_TEXTURE
:
853 load_texture(p
, unit
);
856 case TEXENV_SRC_TEXTURE0
:
857 case TEXENV_SRC_TEXTURE1
:
858 case TEXENV_SRC_TEXTURE2
:
859 case TEXENV_SRC_TEXTURE3
:
860 case TEXENV_SRC_TEXTURE4
:
861 case TEXENV_SRC_TEXTURE5
:
862 case TEXENV_SRC_TEXTURE6
:
863 case TEXENV_SRC_TEXTURE7
:
864 load_texture(p
, src
- TEXENV_SRC_TEXTURE0
);
868 /* not a texture src - do nothing */
875 * Generate instructions for loading all texture source terms.
878 load_texunit_sources( texenv_fragment_program
*p
, GLuint unit
)
880 const struct state_key
*key
= p
->state
;
883 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
884 load_texenv_source( p
, key
->unit
[unit
].ArgsRGB
[i
].Source
, unit
);
887 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
888 load_texenv_source( p
, key
->unit
[unit
].ArgsA
[i
].Source
, unit
);
895 * Applies the fog calculations.
897 * This is basically like the ARB_fragment_prorgam fog options. Note
898 * that ffvertex_prog.c produces fogcoord for us when
899 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
902 emit_fog_instructions(texenv_fragment_program
*p
,
903 ir_rvalue
*fragcolor
)
905 struct state_key
*key
= p
->state
;
907 ir_variable
*params
, *oparams
;
908 ir_variable
*fogcoord
;
910 /* Temporary storage for the whole fog result. Fog calculations
911 * only affect rgb so we're hanging on to the .a value of fragcolor
914 ir_variable
*fog_result
= p
->make_temp(glsl_type::vec4_type
, "fog_result");
915 p
->emit(assign(fog_result
, fragcolor
));
917 fragcolor
= swizzle_xyz(fog_result
);
919 oparams
= p
->shader
->symbols
->get_variable("gl_FogParamsOptimizedMESA");
921 fogcoord
= p
->shader
->symbols
->get_variable("gl_FogFragCoord");
923 params
= p
->shader
->symbols
->get_variable("gl_Fog");
925 f
= new(p
->mem_ctx
) ir_dereference_variable(fogcoord
);
927 ir_variable
*f_var
= p
->make_temp(glsl_type::float_type
, "fog_factor");
929 switch (key
->fog_mode
) {
931 /* f = (end - z) / (end - start)
933 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
934 * (end / (end - start)) so we can generate a single MAD.
936 f
= add(mul(f
, swizzle_x(oparams
)), swizzle_y(oparams
));
939 /* f = e^(-(density * fogcoord))
941 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
942 * use EXP2 which is generally the native instruction without
943 * having to do any further math on the fog density uniform.
945 f
= mul(f
, swizzle_z(oparams
));
946 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
947 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
950 /* f = e^(-(density * fogcoord)^2)
952 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
953 * can do this like FOG_EXP but with a squaring after the
954 * multiply by density.
956 ir_variable
*temp_var
= p
->make_temp(glsl_type::float_type
, "fog_temp");
957 p
->emit(assign(temp_var
, mul(f
, swizzle_w(oparams
))));
959 f
= mul(temp_var
, temp_var
);
960 f
= new(p
->mem_ctx
) ir_expression(ir_unop_neg
, f
);
961 f
= new(p
->mem_ctx
) ir_expression(ir_unop_exp2
, f
);
965 p
->emit(assign(f_var
, saturate(f
)));
967 f
= sub(new(p
->mem_ctx
) ir_constant(1.0f
), f_var
);
968 temp
= new(p
->mem_ctx
) ir_dereference_variable(params
);
969 temp
= new(p
->mem_ctx
) ir_dereference_record(temp
, "color");
970 temp
= mul(swizzle_xyz(temp
), f
);
972 p
->emit(assign(fog_result
, add(temp
, mul(fragcolor
, f_var
)), WRITEMASK_XYZ
));
974 return new(p
->mem_ctx
) ir_dereference_variable(fog_result
);
978 emit_instructions(texenv_fragment_program
*p
)
980 struct state_key
*key
= p
->state
;
983 if (key
->nr_enabled_units
) {
984 /* First pass - to support texture_env_crossbar, first identify
985 * all referenced texture sources and emit texld instructions
988 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
989 if (key
->unit
[unit
].enabled
) {
990 load_texunit_sources(p
, unit
);
993 /* Second pass - emit combine instructions to build final color:
995 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++) {
996 if (key
->unit
[unit
].enabled
) {
997 p
->src_previous
= emit_texenv(p
, unit
);
1002 ir_rvalue
*cf
= get_source(p
, TEXENV_SRC_PREVIOUS
, 0);
1004 if (key
->separate_specular
) {
1005 ir_variable
*spec_result
= p
->make_temp(glsl_type::vec4_type
,
1007 p
->emit(assign(spec_result
, cf
));
1009 ir_rvalue
*secondary
;
1010 if (p
->state
->inputs_available
& VARYING_BIT_COL1
) {
1012 p
->shader
->symbols
->get_variable("gl_SecondaryColor");
1014 secondary
= swizzle_xyz(var
);
1016 secondary
= swizzle_xyz(get_current_attrib(p
, VERT_ATTRIB_COLOR1
));
1019 p
->emit(assign(spec_result
, add(swizzle_xyz(spec_result
), secondary
),
1022 cf
= new(p
->mem_ctx
) ir_dereference_variable(spec_result
);
1025 if (key
->fog_mode
) {
1026 cf
= emit_fog_instructions(p
, cf
);
1029 ir_variable
*frag_color
= p
->shader
->symbols
->get_variable("gl_FragColor");
1031 p
->emit(assign(frag_color
, cf
));
1035 * Generate a new fragment program which implements the context's
1036 * current texture env/combine mode.
1038 static struct gl_shader_program
*
1039 create_new_program(struct gl_context
*ctx
, struct state_key
*key
)
1041 texenv_fragment_program p
;
1043 _mesa_glsl_parse_state
*state
;
1045 p
.mem_ctx
= ralloc_context(NULL
);
1046 p
.shader
= _mesa_new_shader(0, MESA_SHADER_FRAGMENT
);
1048 p
.shader
->SourceChecksum
= 0xf18ed; /* fixed */
1050 p
.shader
->ir
= new(p
.shader
) exec_list
;
1051 state
= new(p
.shader
) _mesa_glsl_parse_state(ctx
, MESA_SHADER_FRAGMENT
,
1053 p
.shader
->symbols
= state
->symbols
;
1054 p
.top_instructions
= p
.shader
->ir
;
1055 p
.instructions
= p
.shader
->ir
;
1057 p
.shader_program
= _mesa_new_shader_program(0);
1059 /* Tell the linker to ignore the fact that we're building a
1060 * separate shader, in case we're in a GLES2 context that would
1061 * normally reject that. The real problem is that we're building a
1062 * fixed function program in a GLES2 context at all, but that's a
1063 * big mess to clean up.
1065 p
.shader_program
->SeparateShader
= GL_TRUE
;
1067 /* The legacy GLSL shadow functions follow the depth texture
1068 * mode and return vec4. The GLSL 1.30 shadow functions return float and
1069 * ignore the depth texture mode. That's a shader and state dependency
1070 * that's difficult to deal with. st/mesa uses a simple but not
1071 * completely correct solution: if the shader declares GLSL >= 1.30 and
1072 * the depth texture mode is GL_ALPHA (000X), it sets the XXXX swizzle
1073 * instead. Thus, the GLSL 1.30 shadow function will get the result in .x
1074 * and legacy shadow functions will get it in .w as expected.
1075 * For the fixed-function fragment shader, use 120 to get correct behavior
1078 state
->language_version
= 120;
1080 state
->es_shader
= false;
1081 if (_mesa_is_gles(ctx
) && ctx
->Extensions
.OES_EGL_image_external
)
1082 state
->OES_EGL_image_external_enable
= true;
1083 _mesa_glsl_initialize_types(state
);
1084 _mesa_glsl_initialize_variables(p
.instructions
, state
);
1086 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1087 p
.src_texture
[unit
] = NULL
;
1088 p
.texcoord_tex
[unit
] = NULL
;
1091 p
.src_previous
= NULL
;
1093 ir_function
*main_f
= new(p
.mem_ctx
) ir_function("main");
1095 state
->symbols
->add_function(main_f
);
1097 ir_function_signature
*main_sig
=
1098 new(p
.mem_ctx
) ir_function_signature(glsl_type::void_type
);
1099 main_sig
->is_defined
= true;
1100 main_f
->add_signature(main_sig
);
1102 p
.instructions
= &main_sig
->body
;
1103 if (key
->num_draw_buffers
)
1104 emit_instructions(&p
);
1106 validate_ir_tree(p
.shader
->ir
);
1108 const struct gl_shader_compiler_options
*options
=
1109 &ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_FRAGMENT
];
1111 /* Conservative approach: Don't optimize here, the linker does it too. */
1112 if (!ctx
->Const
.GLSLOptimizeConservatively
) {
1113 while (do_common_optimization(p
.shader
->ir
, false, false, options
,
1114 ctx
->Const
.NativeIntegers
))
1118 reparent_ir(p
.shader
->ir
, p
.shader
->ir
);
1120 p
.shader
->CompileStatus
= COMPILE_SUCCESS
;
1121 p
.shader
->Version
= state
->language_version
;
1122 p
.shader_program
->Shaders
=
1123 (gl_shader
**)malloc(sizeof(*p
.shader_program
->Shaders
));
1124 p
.shader_program
->Shaders
[0] = p
.shader
;
1125 p
.shader_program
->NumShaders
= 1;
1127 _mesa_glsl_link_shader(ctx
, p
.shader_program
);
1129 if (!p
.shader_program
->data
->LinkStatus
)
1130 _mesa_problem(ctx
, "Failed to link fixed function fragment shader: %s\n",
1131 p
.shader_program
->data
->InfoLog
);
1133 ralloc_free(p
.mem_ctx
);
1134 return p
.shader_program
;
1140 * Return a fragment program which implements the current
1141 * fixed-function texture, fog and color-sum operations.
1143 struct gl_shader_program
*
1144 _mesa_get_fixed_func_fragment_program(struct gl_context
*ctx
)
1146 struct gl_shader_program
*shader_program
;
1147 struct state_key key
;
1150 keySize
= make_state_key(ctx
, &key
);
1152 shader_program
= (struct gl_shader_program
*)
1153 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1156 if (!shader_program
) {
1157 shader_program
= create_new_program(ctx
, &key
);
1159 _mesa_shader_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1160 &key
, keySize
, shader_program
);
1163 return shader_program
;