1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
32 #include "shader/program.h"
33 #include "shader/prog_parameter.h"
34 #include "shader/prog_cache.h"
35 #include "shader/prog_instruction.h"
36 #include "shader/prog_print.h"
37 #include "shader/prog_statevars.h"
38 #include "shader/programopt.h"
39 #include "texenvprogram.h"
43 * Note on texture units:
45 * The number of texture units supported by fixed-function fragment
46 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
47 * That's because there's a one-to-one correspondence between texture
48 * coordinates and samplers in fixed-function processing.
50 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
51 * sets of texcoords, so is fixed-function fragment processing.
53 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
57 struct texenvprog_cache_item
61 struct gl_fragment_program *data;
62 struct texenvprog_cache_item *next;
66 texenv_doing_secondary_color(GLcontext *ctx)
68 if (ctx->Light.Enabled &&
69 (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR))
72 if (ctx->Fog.ColorSumEnabled)
79 * Up to nine instructions per tex unit, plus fog, specular color.
81 #define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
83 #define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
91 GLuint nr_enabled_units:8;
92 GLuint enabled_units:8;
93 GLuint separate_specular:1;
96 GLuint inputs_available:12;
100 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
102 GLuint ScaleShiftRGB:2;
103 GLuint ScaleShiftA:2;
107 struct mode_opt OptRGB[MAX_COMBINER_TERMS];
111 struct mode_opt OptA[MAX_COMBINER_TERMS];
118 #define FOG_UNKNOWN 3
120 static GLuint translate_fog_mode( GLenum mode )
123 case GL_LINEAR: return FOG_LINEAR;
124 case GL_EXP: return FOG_EXP;
125 case GL_EXP2: return FOG_EXP2;
126 default: return FOG_UNKNOWN;
130 #define OPR_SRC_COLOR 0
131 #define OPR_ONE_MINUS_SRC_COLOR 1
132 #define OPR_SRC_ALPHA 2
133 #define OPR_ONE_MINUS_SRC_ALPHA 3
136 #define OPR_UNKNOWN 7
138 static GLuint translate_operand( GLenum operand )
141 case GL_SRC_COLOR: return OPR_SRC_COLOR;
142 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
143 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
144 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
145 case GL_ZERO: return OPR_ZERO;
146 case GL_ONE: return OPR_ONE;
153 #define SRC_TEXTURE 0
154 #define SRC_TEXTURE0 1
155 #define SRC_TEXTURE1 2
156 #define SRC_TEXTURE2 3
157 #define SRC_TEXTURE3 4
158 #define SRC_TEXTURE4 5
159 #define SRC_TEXTURE5 6
160 #define SRC_TEXTURE6 7
161 #define SRC_TEXTURE7 8
162 #define SRC_CONSTANT 9
163 #define SRC_PRIMARY_COLOR 10
164 #define SRC_PREVIOUS 11
166 #define SRC_UNKNOWN 15
168 static GLuint translate_source( GLenum src )
171 case GL_TEXTURE: return SRC_TEXTURE;
179 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
180 case GL_CONSTANT: return SRC_CONSTANT;
181 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
182 case GL_PREVIOUS: return SRC_PREVIOUS;
191 #define MODE_REPLACE 0 /* r = a0 */
192 #define MODE_MODULATE 1 /* r = a0 * a1 */
193 #define MODE_ADD 2 /* r = a0 + a1 */
194 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
195 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
196 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
197 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
198 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
199 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
200 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
201 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
202 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
203 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
204 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
205 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
206 #define MODE_BUMP_ENVMAP_ATI 15 /* special */
207 #define MODE_UNKNOWN 16
210 * Translate GL combiner state into a MODE_x value
212 static GLuint translate_mode( GLenum envMode, GLenum mode )
215 case GL_REPLACE: return MODE_REPLACE;
216 case GL_MODULATE: return MODE_MODULATE;
218 if (envMode == GL_COMBINE4_NV)
219 return MODE_ADD_PRODUCTS;
223 if (envMode == GL_COMBINE4_NV)
224 return MODE_ADD_PRODUCTS_SIGNED;
226 return MODE_ADD_SIGNED;
227 case GL_INTERPOLATE: return MODE_INTERPOLATE;
228 case GL_SUBTRACT: return MODE_SUBTRACT;
229 case GL_DOT3_RGB: return MODE_DOT3_RGB;
230 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
231 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
232 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
233 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
234 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
235 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
236 case GL_BUMP_ENVMAP_ATI: return MODE_BUMP_ENVMAP_ATI;
243 #define TEXTURE_UNKNOWN_INDEX 7
244 static GLuint translate_tex_src_bit( GLbitfield bit )
246 /* make sure number of switch cases is correct */
247 assert(NUM_TEXTURE_TARGETS == 7);
249 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
250 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
251 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
252 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
253 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
254 case TEXTURE_1D_ARRAY_BIT: return TEXTURE_1D_ARRAY_INDEX;
255 case TEXTURE_2D_ARRAY_BIT: return TEXTURE_2D_ARRAY_INDEX;
258 return TEXTURE_UNKNOWN_INDEX;
262 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
263 #define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
266 * Identify all possible varying inputs. The fragment program will
267 * never reference non-varying inputs, but will track them via state
270 * This function figures out all the inputs that the fragment program
271 * has access to. The bitmask is later reduced to just those which
272 * are actually referenced.
274 static GLbitfield get_fp_input_mask( GLcontext *ctx )
277 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
278 ctx->Shader.CurrentProgram->VertexProgram);
279 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
280 GLbitfield fp_inputs = 0x0;
282 if (ctx->VertexProgram._Overriden) {
283 /* Somebody's messing with the vertex program and we don't have
284 * a clue what's happening. Assume that it could be producing
285 * all possible outputs.
289 else if (ctx->RenderMode == GL_FEEDBACK) {
290 /* _NEW_RENDERMODE */
291 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
293 else if (!(vertexProgram || vertexShader) ||
294 !ctx->VertexProgram._Current) {
295 /* Fixed function vertex logic */
297 GLbitfield varying_inputs = ctx->varying_vp_inputs;
299 /* These get generated in the setup routine regardless of the
303 if (ctx->Point.PointSprite)
304 varying_inputs |= FRAG_BITS_TEX_ANY;
306 /* First look at what values may be computed by the generated
310 if (ctx->Light.Enabled) {
311 fp_inputs |= FRAG_BIT_COL0;
313 if (texenv_doing_secondary_color(ctx))
314 fp_inputs |= FRAG_BIT_COL1;
318 fp_inputs |= (ctx->Texture._TexGenEnabled |
319 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
321 /* Then look at what might be varying as a result of enabled
324 if (varying_inputs & VERT_BIT_COLOR0) fp_inputs |= FRAG_BIT_COL0;
325 if (varying_inputs & VERT_BIT_COLOR1) fp_inputs |= FRAG_BIT_COL1;
327 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
328 << FRAG_ATTRIB_TEX0);
332 /* calculate from vp->outputs */
333 struct gl_vertex_program *vprog;
334 GLbitfield vp_outputs;
336 /* Choose GLSL vertex shader over ARB vertex program. Need this
337 * since vertex shader state validation comes after fragment state
338 * validation (see additional comments in state.c).
341 vprog = ctx->Shader.CurrentProgram->VertexProgram;
343 vprog = ctx->VertexProgram._Current;
345 vp_outputs = vprog->Base.OutputsWritten;
347 /* These get generated in the setup routine regardless of the
351 if (ctx->Point.PointSprite)
352 vp_outputs |= FRAG_BITS_TEX_ANY;
354 if (vp_outputs & (1 << VERT_RESULT_COL0)) fp_inputs |= FRAG_BIT_COL0;
355 if (vp_outputs & (1 << VERT_RESULT_COL1)) fp_inputs |= FRAG_BIT_COL1;
357 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
358 << FRAG_ATTRIB_TEX0);
366 * Examine current texture environment state and generate a unique
367 * key to identify it.
369 static void make_state_key( GLcontext *ctx, struct state_key *key )
372 GLbitfield inputs_referenced = FRAG_BIT_COL0;
373 GLbitfield inputs_available = get_fp_input_mask( ctx );
375 memset(key, 0, sizeof(*key));
378 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
379 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
382 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
385 format = texUnit->_Current->Image[0][texUnit->_Current->BaseLevel]->_BaseFormat;
387 key->unit[i].enabled = 1;
388 key->enabled_units |= (1<<i);
389 key->nr_enabled_units = i+1;
390 inputs_referenced |= FRAG_BIT_TEX(i);
392 key->unit[i].source_index =
393 translate_tex_src_bit(texUnit->_ReallyEnabled);
394 key->unit[i].shadow = ((texUnit->_Current->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
395 ((format == GL_DEPTH_COMPONENT) ||
396 (format == GL_DEPTH_STENCIL_EXT)));
398 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
399 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
401 key->unit[i].ModeRGB =
402 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeRGB);
404 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeA);
406 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
407 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
409 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
410 key->unit[i].OptRGB[j].Operand =
411 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
412 key->unit[i].OptA[j].Operand =
413 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
414 key->unit[i].OptRGB[j].Source =
415 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
416 key->unit[i].OptA[j].Source =
417 translate_source(texUnit->_CurrentCombine->SourceA[j]);
420 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
421 /* requires some special translation */
422 key->unit[i].NumArgsRGB = 2;
423 key->unit[i].ScaleShiftRGB = 0;
424 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
425 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
426 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
427 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
431 /* _NEW_LIGHT | _NEW_FOG */
432 if (texenv_doing_secondary_color(ctx)) {
433 key->separate_specular = 1;
434 inputs_referenced |= FRAG_BIT_COL1;
438 if (ctx->Fog.Enabled) {
439 key->fog_enabled = 1;
440 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
441 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
444 key->inputs_available = (inputs_available & inputs_referenced);
448 * Use uregs to represent registers internally, translate to Mesa's
449 * expected formats on emit.
451 * NOTE: These are passed by value extensively in this file rather
452 * than as usual by pointer reference. If this disturbs you, try
453 * remembering they are just 32bits in size.
455 * GCC is smart enough to deal with these dword-sized structures in
456 * much the same way as if I had defined them as dwords and was using
457 * macros to access and set the fields. This is much nicer and easier
470 static const struct ureg undef = {
481 /** State used to build the fragment program:
483 struct texenv_fragment_program {
484 struct gl_fragment_program *program;
486 struct state_key *state;
488 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
489 GLbitfield temps_output; /**< Track texture indirections, see spec. */
490 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
493 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
494 /* Reg containing each texture unit's sampled texture color,
498 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
499 /* Reg containing texcoord for a texture unit,
500 * needed for bump mapping, else undef.
503 struct ureg src_previous; /**< Reg containing color from previous
504 * stage. May need to be decl'd.
507 GLuint last_tex_stage; /**< Number of last enabled texture unit */
516 static struct ureg make_ureg(GLuint file, GLuint idx)
524 reg.swz = SWIZZLE_NOOP;
529 static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
531 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
534 GET_SWZ(reg.swz, w));
539 static struct ureg swizzle1( struct ureg reg, int x )
541 return swizzle(reg, x, x, x, x);
544 static struct ureg negate( struct ureg reg )
550 static GLboolean is_undef( struct ureg reg )
552 return reg.file == PROGRAM_UNDEFINED;
556 static struct ureg get_temp( struct texenv_fragment_program *p )
560 /* First try and reuse temps which have been used already:
562 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
564 /* Then any unused temporary:
567 bit = _mesa_ffs( ~p->temp_in_use );
570 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
574 if ((GLuint) bit > p->program->Base.NumTemporaries)
575 p->program->Base.NumTemporaries = bit;
577 p->temp_in_use |= 1<<(bit-1);
578 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
581 static struct ureg get_tex_temp( struct texenv_fragment_program *p )
585 /* First try to find available temp not previously used (to avoid
586 * starting a new texture indirection). According to the spec, the
587 * ~p->temps_output isn't necessary, but will keep it there for
590 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
592 /* Then any unused temporary:
595 bit = _mesa_ffs( ~p->temp_in_use );
598 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
602 if ((GLuint) bit > p->program->Base.NumTemporaries)
603 p->program->Base.NumTemporaries = bit;
605 p->temp_in_use |= 1<<(bit-1);
606 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
610 /** Mark a temp reg as being no longer allocatable. */
611 static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
613 if (r.file == PROGRAM_TEMPORARY)
614 p->temps_output |= (1 << r.idx);
618 static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
620 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
622 /* KW: To support tex_env_crossbar, don't release the registers in
625 if (max_temp >= sizeof(int) * 8)
626 p->temp_in_use = p->temps_output;
628 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
632 static struct ureg register_param5( struct texenv_fragment_program *p,
639 gl_state_index tokens[STATE_LENGTH];
646 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
647 return make_ureg(PROGRAM_STATE_VAR, idx);
651 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
652 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
653 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
654 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
656 static GLuint frag_to_vert_attrib( GLuint attrib )
659 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
660 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
662 assert(attrib >= FRAG_ATTRIB_TEX0);
663 assert(attrib <= FRAG_ATTRIB_TEX7);
664 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
669 static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
671 if (p->state->inputs_available & (1<<input)) {
672 p->program->Base.InputsRead |= (1 << input);
673 return make_ureg(PROGRAM_INPUT, input);
676 GLuint idx = frag_to_vert_attrib( input );
677 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
682 static void emit_arg( struct prog_src_register *reg,
685 reg->File = ureg.file;
686 reg->Index = ureg.idx;
687 reg->Swizzle = ureg.swz;
688 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
692 static void emit_dst( struct prog_dst_register *dst,
693 struct ureg ureg, GLuint mask )
695 dst->File = ureg.file;
696 dst->Index = ureg.idx;
697 dst->WriteMask = mask;
698 dst->CondMask = COND_TR; /* always pass cond test */
699 dst->CondSwizzle = SWIZZLE_NOOP;
702 static struct prog_instruction *
703 emit_op(struct texenv_fragment_program *p,
712 GLuint nr = p->program->Base.NumInstructions++;
713 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
715 assert(nr < MAX_INSTRUCTIONS);
717 _mesa_init_instructions(inst, 1);
720 emit_arg( &inst->SrcReg[0], src0 );
721 emit_arg( &inst->SrcReg[1], src1 );
722 emit_arg( &inst->SrcReg[2], src2 );
724 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
726 emit_dst( &inst->DstReg, dest, mask );
729 /* Accounting for indirection tracking:
731 if (dest.file == PROGRAM_TEMPORARY)
732 p->temps_output |= 1 << dest.idx;
739 static struct ureg emit_arith( struct texenv_fragment_program *p,
748 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
750 /* Accounting for indirection tracking:
752 if (src0.file == PROGRAM_TEMPORARY)
753 p->alu_temps |= 1 << src0.idx;
755 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
756 p->alu_temps |= 1 << src1.idx;
758 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
759 p->alu_temps |= 1 << src2.idx;
761 if (dest.file == PROGRAM_TEMPORARY)
762 p->alu_temps |= 1 << dest.idx;
764 p->program->Base.NumAluInstructions++;
768 static struct ureg emit_texld( struct texenv_fragment_program *p,
777 struct prog_instruction *inst = emit_op( p, op,
779 GL_FALSE, /* don't saturate? */
784 inst->TexSrcTarget = tex_idx;
785 inst->TexSrcUnit = tex_unit;
786 inst->TexShadow = tex_shadow;
788 p->program->Base.NumTexInstructions++;
790 /* Accounting for indirection tracking:
792 reserve_temp(p, dest);
795 /* Is this a texture indirection?
797 if ((coord.file == PROGRAM_TEMPORARY &&
798 (p->temps_output & (1<<coord.idx))) ||
799 (dest.file == PROGRAM_TEMPORARY &&
800 (p->alu_temps & (1<<dest.idx)))) {
801 p->program->Base.NumTexIndirections++;
802 p->temps_output = 1<<coord.idx;
804 assert(0); /* KW: texture env crossbar */
812 static struct ureg register_const4f( struct texenv_fragment_program *p,
825 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
827 r = make_ureg(PROGRAM_CONSTANT, idx);
832 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
833 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
834 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
835 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
838 static struct ureg get_one( struct texenv_fragment_program *p )
840 if (is_undef(p->one))
841 p->one = register_scalar_const(p, 1.0);
845 static struct ureg get_half( struct texenv_fragment_program *p )
847 if (is_undef(p->half))
848 p->half = register_scalar_const(p, 0.5);
852 static struct ureg get_zero( struct texenv_fragment_program *p )
854 if (is_undef(p->zero))
855 p->zero = register_scalar_const(p, 0.0);
860 static void program_error( struct texenv_fragment_program *p, const char *msg )
862 _mesa_problem(NULL, msg);
866 static struct ureg get_source( struct texenv_fragment_program *p,
867 GLuint src, GLuint unit )
871 assert(!is_undef(p->src_texture[unit]));
872 return p->src_texture[unit];
882 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
883 return p->src_texture[src - SRC_TEXTURE0];
886 return register_param2(p, STATE_TEXENV_COLOR, unit);
888 case SRC_PRIMARY_COLOR:
889 return register_input(p, FRAG_ATTRIB_COL0);
895 if (is_undef(p->src_previous))
896 return register_input(p, FRAG_ATTRIB_COL0);
898 return p->src_previous;
906 static struct ureg emit_combine_source( struct texenv_fragment_program *p,
912 struct ureg arg, src, one;
914 src = get_source(p, source, unit);
917 case OPR_ONE_MINUS_SRC_COLOR:
919 * Emit tmp = 1.0 - arg.xyzw
923 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
926 if (mask == WRITEMASK_W)
929 return swizzle1( src, SWIZZLE_W );
930 case OPR_ONE_MINUS_SRC_ALPHA:
932 * Emit tmp = 1.0 - arg.wwww
936 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
937 one, swizzle1(src, SWIZZLE_W), undef);
950 static GLboolean args_match( struct state_key *key, GLuint unit )
952 GLuint i, nr = key->unit[unit].NumArgsRGB;
954 for (i = 0 ; i < nr ; i++) {
955 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
958 switch(key->unit[unit].OptA[i].Operand) {
960 switch(key->unit[unit].OptRGB[i].Operand) {
968 case OPR_ONE_MINUS_SRC_ALPHA:
969 switch(key->unit[unit].OptRGB[i].Operand) {
970 case OPR_ONE_MINUS_SRC_COLOR:
971 case OPR_ONE_MINUS_SRC_ALPHA:
978 return GL_FALSE; /* impossible */
985 static struct ureg emit_combine( struct texenv_fragment_program *p,
992 const struct mode_opt *opt)
994 struct ureg src[MAX_COMBINER_TERMS];
995 struct ureg tmp, half;
998 assert(nr <= MAX_COMBINER_TERMS);
1000 tmp = undef; /* silence warning (bug 5318) */
1002 for (i = 0; i < nr; i++)
1003 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
1007 if (mask == WRITEMASK_XYZW && !saturate)
1010 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
1012 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
1013 src[0], src[1], undef );
1015 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
1016 src[0], src[1], undef );
1017 case MODE_ADD_SIGNED:
1018 /* tmp = arg0 + arg1
1022 tmp = get_temp( p );
1023 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1024 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
1026 case MODE_INTERPOLATE:
1027 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1029 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
1032 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
1034 case MODE_DOT3_RGBA:
1035 case MODE_DOT3_RGBA_EXT:
1036 case MODE_DOT3_RGB_EXT:
1037 case MODE_DOT3_RGB: {
1038 struct ureg tmp0 = get_temp( p );
1039 struct ureg tmp1 = get_temp( p );
1040 struct ureg neg1 = register_scalar_const(p, -1);
1041 struct ureg two = register_scalar_const(p, 2);
1043 /* tmp0 = 2*src0 - 1
1046 * dst = tmp0 dot3 tmp1
1048 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
1051 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
1054 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
1056 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
1059 case MODE_MODULATE_ADD_ATI:
1060 /* Arg0 * Arg2 + Arg1 */
1061 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
1062 src[0], src[2], src[1] );
1063 case MODE_MODULATE_SIGNED_ADD_ATI: {
1064 /* Arg0 * Arg2 + Arg1 - 0.5 */
1065 struct ureg tmp0 = get_temp(p);
1067 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1068 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1071 case MODE_MODULATE_SUBTRACT_ATI:
1072 /* Arg0 * Arg2 - Arg1 */
1073 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
1075 case MODE_ADD_PRODUCTS:
1076 /* Arg0 * Arg1 + Arg2 * Arg3 */
1078 struct ureg tmp0 = get_temp(p);
1079 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1080 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1083 case MODE_ADD_PRODUCTS_SIGNED:
1084 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1086 struct ureg tmp0 = get_temp(p);
1088 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1089 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1090 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1093 case MODE_BUMP_ENVMAP_ATI:
1094 /* special - not handled here */
1105 * Generate instructions for one texture unit's env/combiner mode.
1108 emit_texenv(struct texenv_fragment_program *p, GLuint unit)
1110 struct state_key *key = p->state;
1111 GLboolean saturate = (unit < p->last_tex_stage);
1112 GLuint rgb_shift, alpha_shift;
1113 struct ureg out, shift;
1116 if (!key->unit[unit].enabled) {
1117 return get_source(p, SRC_PREVIOUS, 0);
1119 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1120 /* this isn't really a env stage delivering a color and handled elsewhere */
1121 return get_source(p, SRC_PREVIOUS, 0);
1124 switch (key->unit[unit].ModeRGB) {
1125 case MODE_DOT3_RGB_EXT:
1126 alpha_shift = key->unit[unit].ScaleShiftA;
1129 case MODE_DOT3_RGBA_EXT:
1134 rgb_shift = key->unit[unit].ScaleShiftRGB;
1135 alpha_shift = key->unit[unit].ScaleShiftA;
1139 /* If this is the very last calculation, emit direct to output reg:
1141 if (key->separate_specular ||
1142 unit != p->last_tex_stage ||
1145 dest = get_temp( p );
1147 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
1149 /* Emit the RGB and A combine ops
1151 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1152 args_match(key, unit)) {
1153 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1155 key->unit[unit].NumArgsRGB,
1156 key->unit[unit].ModeRGB,
1157 key->unit[unit].OptRGB);
1159 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
1160 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
1162 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1164 key->unit[unit].NumArgsRGB,
1165 key->unit[unit].ModeRGB,
1166 key->unit[unit].OptRGB);
1169 /* Need to do something to stop from re-emitting identical
1170 * argument calculations here:
1172 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1174 key->unit[unit].NumArgsRGB,
1175 key->unit[unit].ModeRGB,
1176 key->unit[unit].OptRGB);
1177 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1179 key->unit[unit].NumArgsA,
1180 key->unit[unit].ModeA,
1181 key->unit[unit].OptA);
1184 /* Deal with the final shift:
1186 if (alpha_shift || rgb_shift) {
1187 if (rgb_shift == alpha_shift) {
1188 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
1191 shift = register_const4f(p,
1192 (GLfloat)(1<<rgb_shift),
1193 (GLfloat)(1<<rgb_shift),
1194 (GLfloat)(1<<rgb_shift),
1195 (GLfloat)(1<<alpha_shift));
1197 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
1198 saturate, out, shift, undef );
1206 * Generate instruction for getting a texture source term.
1208 static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1210 if (is_undef(p->src_texture[unit])) {
1211 GLuint texTarget = p->state->unit[unit].source_index;
1212 struct ureg texcoord;
1213 struct ureg tmp = get_tex_temp( p );
1215 if (is_undef(p->texcoord_tex[unit])) {
1216 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1219 /* might want to reuse this reg for tex output actually */
1220 texcoord = p->texcoord_tex[unit];
1223 if (texTarget == TEXTURE_UNKNOWN_INDEX)
1224 program_error(p, "TexSrcBit");
1226 /* TODO: Use D0_MASK_XY where possible.
1228 if (p->state->unit[unit].enabled) {
1229 GLboolean shadow = GL_FALSE;
1231 if (p->state->unit[unit].shadow) {
1232 p->program->Base.ShadowSamplers |= 1 << unit;
1236 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1237 tmp, WRITEMASK_XYZW,
1238 unit, texTarget, shadow,
1241 p->program->Base.SamplersUsed |= (1 << unit);
1242 /* This identity mapping should already be in place
1243 * (see _mesa_init_program_struct()) but let's be safe.
1245 p->program->Base.SamplerUnits[unit] = unit;
1248 p->src_texture[unit] = get_zero(p);
1252 static GLboolean load_texenv_source( struct texenv_fragment_program *p,
1253 GLuint src, GLuint unit )
1257 load_texture(p, unit);
1268 load_texture(p, src - SRC_TEXTURE0);
1272 /* not a texture src - do nothing */
1281 * Generate instructions for loading all texture source terms.
1284 load_texunit_sources( struct texenv_fragment_program *p, int unit )
1286 struct state_key *key = p->state;
1289 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1290 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
1293 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1294 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1301 * Generate instructions for loading bump map textures.
1304 load_texunit_bumpmap( struct texenv_fragment_program *p, int unit )
1306 struct state_key *key = p->state;
1307 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1308 struct ureg texcDst, bumpMapRes;
1309 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1310 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1311 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1312 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1314 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1316 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1317 texcDst = get_tex_temp( p );
1318 p->texcoord_tex[bumpedUnitNr] = texcDst;
1320 /* apply rot matrix and add coords to be available in next phase */
1321 /* dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1) */
1322 /* note only 2 coords are affected the rest are left unchanged (mul by 0) */
1323 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1324 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1325 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1326 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1328 /* move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1329 enough to access this later, should optimize away */
1330 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0, constdudvcolor, undef, undef );
1336 * Generate a new fragment program which implements the context's
1337 * current texture env/combine mode.
1340 create_new_program(GLcontext *ctx, struct state_key *key,
1341 struct gl_fragment_program *program)
1343 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
1344 struct texenv_fragment_program p;
1346 struct ureg cf, out;
1348 _mesa_memset(&p, 0, sizeof(p));
1351 p.program = program;
1353 /* During code generation, use locally-allocated instruction buffer,
1354 * then alloc dynamic storage below.
1356 p.program->Base.Instructions = instBuffer;
1357 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
1358 p.program->Base.NumTexIndirections = 1;
1359 p.program->Base.NumTexInstructions = 0;
1360 p.program->Base.NumAluInstructions = 0;
1361 p.program->Base.String = NULL;
1362 p.program->Base.NumInstructions =
1363 p.program->Base.NumTemporaries =
1364 p.program->Base.NumParameters =
1365 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
1366 p.program->Base.Parameters = _mesa_new_parameter_list();
1368 p.program->Base.InputsRead = 0;
1369 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
1371 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
1372 p.src_texture[unit] = undef;
1373 p.texcoord_tex[unit] = undef;
1376 p.src_previous = undef;
1381 p.last_tex_stage = 0;
1382 release_temps(ctx, &p);
1384 if (key->enabled_units) {
1385 GLboolean needbumpstage = GL_FALSE;
1386 /* Zeroth pass - bump map textures first */
1387 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1388 if (key->unit[unit].enabled && key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1389 needbumpstage = GL_TRUE;
1390 load_texunit_bumpmap( &p, unit );
1393 p.program->Base.NumTexIndirections++;
1395 /* First pass - to support texture_env_crossbar, first identify
1396 * all referenced texture sources and emit texld instructions
1399 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1400 if (key->unit[unit].enabled) {
1401 load_texunit_sources( &p, unit );
1402 p.last_tex_stage = unit;
1405 /* Second pass - emit combine instructions to build final color:
1407 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
1408 if (key->enabled_units & (1<<unit)) {
1409 p.src_previous = emit_texenv( &p, unit );
1410 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
1411 release_temps(ctx, &p); /* release all temps */
1415 cf = get_source( &p, SRC_PREVIOUS, 0 );
1416 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
1418 if (key->separate_specular) {
1419 /* Emit specular add.
1421 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
1422 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1423 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
1425 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
1426 /* Will wind up in here if no texture enabled or a couple of
1427 * other scenarios (GL_REPLACE for instance).
1429 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
1434 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
1436 if (key->fog_enabled) {
1437 /* Pull fog mode from GLcontext, the value in the state key is
1438 * a reduced value and not what is expected in FogOption
1440 p.program->FogOption = ctx->Fog.Mode;
1441 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
1443 p.program->FogOption = GL_NONE;
1445 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
1446 program_error(&p, "Exceeded max nr indirect texture lookups");
1448 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
1449 program_error(&p, "Exceeded max TEX instructions");
1451 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
1452 program_error(&p, "Exceeded max ALU instructions");
1454 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
1456 /* Allocate final instruction array */
1457 p.program->Base.Instructions
1458 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1459 if (!p.program->Base.Instructions) {
1460 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1461 "generating tex env program");
1464 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1465 p.program->Base.NumInstructions);
1467 if (p.program->FogOption) {
1468 _mesa_append_fog_code(ctx, p.program);
1469 p.program->FogOption = GL_NONE;
1473 /* Notify driver the fragment program has (actually) changed.
1475 if (ctx->Driver.ProgramStringNotify) {
1476 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1481 _mesa_print_program(&p.program->Base);
1488 * Return a fragment program which implements the current
1489 * fixed-function texture, fog and color-sum operations.
1491 struct gl_fragment_program *
1492 _mesa_get_fixed_func_fragment_program(GLcontext *ctx)
1494 struct gl_fragment_program *prog;
1495 struct state_key key;
1497 make_state_key(ctx, &key);
1499 prog = (struct gl_fragment_program *)
1500 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1504 prog = (struct gl_fragment_program *)
1505 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1507 create_new_program(ctx, &key, prog);
1509 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1510 &key, sizeof(key), &prog->Base);