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 **************************************************************************/
31 #include "shader/program.h"
32 #include "shader/prog_parameter.h"
33 #include "shader/prog_cache.h"
34 #include "shader/prog_instruction.h"
35 #include "shader/prog_print.h"
36 #include "shader/prog_statevars.h"
37 #include "shader/programopt.h"
38 #include "texenvprogram.h"
42 * Note on texture units:
44 * The number of texture units supported by fixed-function fragment
45 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
46 * That's because there's a one-to-one correspondence between texture
47 * coordinates and samplers in fixed-function processing.
49 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
50 * sets of texcoords, so is fixed-function fragment processing.
52 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
56 struct texenvprog_cache_item
60 struct gl_fragment_program *data;
61 struct texenvprog_cache_item *next;
65 texenv_doing_secondary_color(GLcontext *ctx)
67 if (ctx->Light.Enabled &&
68 (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR))
71 if (ctx->Fog.ColorSumEnabled)
78 * Up to nine instructions per tex unit, plus fog, specular color.
80 #define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
82 #define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
86 __extension__ GLubyte Source:4; /**< SRC_x */
87 __extension__ GLubyte Operand:3; /**< OPR_x */
89 GLuint Source; /**< SRC_x */
90 GLuint Operand; /**< OPR_x */
95 GLuint nr_enabled_units:8;
96 GLuint enabled_units:8;
97 GLuint separate_specular:1;
99 GLuint fog_mode:2; /**< FOG_x */
100 GLuint inputs_available:12;
104 GLuint source_index:3; /**< TEXTURE_x_INDEX */
106 GLuint ScaleShiftRGB:2;
107 GLuint ScaleShiftA:2;
109 GLuint NumArgsRGB:3; /**< up to MAX_COMBINER_TERMS */
110 GLuint ModeRGB:5; /**< MODE_x */
112 GLuint NumArgsA:3; /**< up to MAX_COMBINER_TERMS */
113 GLuint ModeA:5; /**< MODE_x */
115 struct mode_opt OptRGB[MAX_COMBINER_TERMS];
116 struct mode_opt OptA[MAX_COMBINER_TERMS];
117 } unit[MAX_TEXTURE_UNITS];
123 #define FOG_UNKNOWN 3
125 static GLuint translate_fog_mode( GLenum mode )
128 case GL_LINEAR: return FOG_LINEAR;
129 case GL_EXP: return FOG_EXP;
130 case GL_EXP2: return FOG_EXP2;
131 default: return FOG_UNKNOWN;
135 #define OPR_SRC_COLOR 0
136 #define OPR_ONE_MINUS_SRC_COLOR 1
137 #define OPR_SRC_ALPHA 2
138 #define OPR_ONE_MINUS_SRC_ALPHA 3
141 #define OPR_UNKNOWN 7
143 static GLuint translate_operand( GLenum operand )
146 case GL_SRC_COLOR: return OPR_SRC_COLOR;
147 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
148 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
149 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
150 case GL_ZERO: return OPR_ZERO;
151 case GL_ONE: return OPR_ONE;
158 #define SRC_TEXTURE 0
159 #define SRC_TEXTURE0 1
160 #define SRC_TEXTURE1 2
161 #define SRC_TEXTURE2 3
162 #define SRC_TEXTURE3 4
163 #define SRC_TEXTURE4 5
164 #define SRC_TEXTURE5 6
165 #define SRC_TEXTURE6 7
166 #define SRC_TEXTURE7 8
167 #define SRC_CONSTANT 9
168 #define SRC_PRIMARY_COLOR 10
169 #define SRC_PREVIOUS 11
171 #define SRC_UNKNOWN 15
173 static GLuint translate_source( GLenum src )
176 case GL_TEXTURE: return SRC_TEXTURE;
184 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
185 case GL_CONSTANT: return SRC_CONSTANT;
186 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
187 case GL_PREVIOUS: return SRC_PREVIOUS;
196 #define MODE_REPLACE 0 /* r = a0 */
197 #define MODE_MODULATE 1 /* r = a0 * a1 */
198 #define MODE_ADD 2 /* r = a0 + a1 */
199 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
200 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
201 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
202 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
203 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
204 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
205 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
206 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
207 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
208 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
209 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
210 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
211 #define MODE_BUMP_ENVMAP_ATI 15 /* special */
212 #define MODE_UNKNOWN 16
215 * Translate GL combiner state into a MODE_x value
217 static GLuint translate_mode( GLenum envMode, GLenum mode )
220 case GL_REPLACE: return MODE_REPLACE;
221 case GL_MODULATE: return MODE_MODULATE;
223 if (envMode == GL_COMBINE4_NV)
224 return MODE_ADD_PRODUCTS;
228 if (envMode == GL_COMBINE4_NV)
229 return MODE_ADD_PRODUCTS_SIGNED;
231 return MODE_ADD_SIGNED;
232 case GL_INTERPOLATE: return MODE_INTERPOLATE;
233 case GL_SUBTRACT: return MODE_SUBTRACT;
234 case GL_DOT3_RGB: return MODE_DOT3_RGB;
235 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
236 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
237 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
238 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
239 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
240 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
241 case GL_BUMP_ENVMAP_ATI: return MODE_BUMP_ENVMAP_ATI;
250 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
252 static GLuint translate_tex_src_bit( GLbitfield bit )
255 return _mesa_ffs(bit) - 1;
259 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
260 #define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
263 * Identify all possible varying inputs. The fragment program will
264 * never reference non-varying inputs, but will track them via state
267 * This function figures out all the inputs that the fragment program
268 * has access to. The bitmask is later reduced to just those which
269 * are actually referenced.
271 static GLbitfield get_fp_input_mask( GLcontext *ctx )
274 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
275 ctx->Shader.CurrentProgram->LinkStatus &&
276 ctx->Shader.CurrentProgram->VertexProgram);
277 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
278 GLbitfield fp_inputs = 0x0;
280 if (ctx->VertexProgram._Overriden) {
281 /* Somebody's messing with the vertex program and we don't have
282 * a clue what's happening. Assume that it could be producing
283 * all possible outputs.
287 else if (ctx->RenderMode == GL_FEEDBACK) {
288 /* _NEW_RENDERMODE */
289 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
291 else if (!(vertexProgram || vertexShader) ||
292 !ctx->VertexProgram._Current) {
293 /* Fixed function vertex logic */
295 GLbitfield varying_inputs = ctx->varying_vp_inputs;
297 /* These get generated in the setup routine regardless of the
301 if (ctx->Point.PointSprite)
302 varying_inputs |= FRAG_BITS_TEX_ANY;
304 /* First look at what values may be computed by the generated
308 if (ctx->Light.Enabled) {
309 fp_inputs |= FRAG_BIT_COL0;
311 if (texenv_doing_secondary_color(ctx))
312 fp_inputs |= FRAG_BIT_COL1;
316 fp_inputs |= (ctx->Texture._TexGenEnabled |
317 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
319 /* Then look at what might be varying as a result of enabled
322 if (varying_inputs & VERT_BIT_COLOR0)
323 fp_inputs |= FRAG_BIT_COL0;
324 if (varying_inputs & VERT_BIT_COLOR1)
325 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))
355 fp_inputs |= FRAG_BIT_COL0;
356 if (vp_outputs & (1 << VERT_RESULT_COL1))
357 fp_inputs |= FRAG_BIT_COL1;
359 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
360 << FRAG_ATTRIB_TEX0);
368 * Examine current texture environment state and generate a unique
369 * key to identify it.
371 static void make_state_key( GLcontext *ctx, struct state_key *key )
374 GLbitfield inputs_referenced = FRAG_BIT_COL0;
375 const GLbitfield inputs_available = get_fp_input_mask( ctx );
377 memset(key, 0, sizeof(*key));
380 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
381 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
382 const struct gl_texture_object *texObj = texUnit->_Current;
383 const struct gl_tex_env_combine_state *comb = texUnit->_CurrentCombine;
386 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
389 format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
391 key->unit[i].enabled = 1;
392 key->enabled_units |= (1<<i);
393 key->nr_enabled_units = i+1;
394 inputs_referenced |= FRAG_BIT_TEX(i);
396 key->unit[i].source_index =
397 translate_tex_src_bit(texUnit->_ReallyEnabled);
399 key->unit[i].shadow = ((texObj->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
400 ((format == GL_DEPTH_COMPONENT) ||
401 (format == GL_DEPTH_STENCIL_EXT)));
403 key->unit[i].NumArgsRGB = comb->_NumArgsRGB;
404 key->unit[i].NumArgsA = comb->_NumArgsA;
406 key->unit[i].ModeRGB =
407 translate_mode(texUnit->EnvMode, comb->ModeRGB);
409 translate_mode(texUnit->EnvMode, comb->ModeA);
411 key->unit[i].ScaleShiftRGB = comb->ScaleShiftRGB;
412 key->unit[i].ScaleShiftA = comb->ScaleShiftA;
414 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
415 key->unit[i].OptRGB[j].Operand = translate_operand(comb->OperandRGB[j]);
416 key->unit[i].OptA[j].Operand = translate_operand(comb->OperandA[j]);
417 key->unit[i].OptRGB[j].Source = translate_source(comb->SourceRGB[j]);
418 key->unit[i].OptA[j].Source = translate_source(comb->SourceA[j]);
421 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
422 /* requires some special translation */
423 key->unit[i].NumArgsRGB = 2;
424 key->unit[i].ScaleShiftRGB = 0;
425 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
426 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
427 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
428 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
432 /* _NEW_LIGHT | _NEW_FOG */
433 if (texenv_doing_secondary_color(ctx)) {
434 key->separate_specular = 1;
435 inputs_referenced |= FRAG_BIT_COL1;
439 if (ctx->Fog.Enabled) {
440 key->fog_enabled = 1;
441 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
442 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
445 key->inputs_available = (inputs_available & inputs_referenced);
449 * Use uregs to represent registers internally, translate to Mesa's
450 * expected formats on emit.
452 * NOTE: These are passed by value extensively in this file rather
453 * than as usual by pointer reference. If this disturbs you, try
454 * remembering they are just 32bits in size.
456 * GCC is smart enough to deal with these dword-sized structures in
457 * much the same way as if I had defined them as dwords and was using
458 * macros to access and set the fields. This is much nicer and easier
469 static const struct ureg undef = {
478 /** State used to build the fragment program:
480 struct texenv_fragment_program {
481 struct gl_fragment_program *program;
482 struct state_key *state;
484 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
485 GLbitfield temps_output; /**< Track texture indirections, see spec. */
486 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
489 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
490 /* Reg containing each texture unit's sampled texture color,
494 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
495 /* Reg containing texcoord for a texture unit,
496 * needed for bump mapping, else undef.
499 struct ureg src_previous; /**< Reg containing color from previous
500 * stage. May need to be decl'd.
503 GLuint last_tex_stage; /**< Number of last enabled texture unit */
512 static struct ureg make_ureg(GLuint file, GLuint idx)
518 reg.swz = SWIZZLE_NOOP;
523 static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
525 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
528 GET_SWZ(reg.swz, w));
533 static struct ureg swizzle1( struct ureg reg, int x )
535 return swizzle(reg, x, x, x, x);
538 static struct ureg negate( struct ureg reg )
544 static GLboolean is_undef( struct ureg reg )
546 return reg.file == PROGRAM_UNDEFINED;
550 static struct ureg get_temp( struct texenv_fragment_program *p )
554 /* First try and reuse temps which have been used already:
556 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
558 /* Then any unused temporary:
561 bit = _mesa_ffs( ~p->temp_in_use );
564 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
568 if ((GLuint) bit > p->program->Base.NumTemporaries)
569 p->program->Base.NumTemporaries = bit;
571 p->temp_in_use |= 1<<(bit-1);
572 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
575 static struct ureg get_tex_temp( struct texenv_fragment_program *p )
579 /* First try to find available temp not previously used (to avoid
580 * starting a new texture indirection). According to the spec, the
581 * ~p->temps_output isn't necessary, but will keep it there for
584 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
586 /* Then any unused temporary:
589 bit = _mesa_ffs( ~p->temp_in_use );
592 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
596 if ((GLuint) bit > p->program->Base.NumTemporaries)
597 p->program->Base.NumTemporaries = bit;
599 p->temp_in_use |= 1<<(bit-1);
600 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
604 /** Mark a temp reg as being no longer allocatable. */
605 static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
607 if (r.file == PROGRAM_TEMPORARY)
608 p->temps_output |= (1 << r.idx);
612 static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
614 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
616 /* KW: To support tex_env_crossbar, don't release the registers in
619 if (max_temp >= sizeof(int) * 8)
620 p->temp_in_use = p->temps_output;
622 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
626 static struct ureg register_param5( struct texenv_fragment_program *p,
633 gl_state_index tokens[STATE_LENGTH];
640 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
641 return make_ureg(PROGRAM_STATE_VAR, idx);
645 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
646 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
647 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
648 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
650 static GLuint frag_to_vert_attrib( GLuint attrib )
653 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
654 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
656 assert(attrib >= FRAG_ATTRIB_TEX0);
657 assert(attrib <= FRAG_ATTRIB_TEX7);
658 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
663 static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
665 if (p->state->inputs_available & (1<<input)) {
666 p->program->Base.InputsRead |= (1 << input);
667 return make_ureg(PROGRAM_INPUT, input);
670 GLuint idx = frag_to_vert_attrib( input );
671 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
676 static void emit_arg( struct prog_src_register *reg,
679 reg->File = ureg.file;
680 reg->Index = ureg.idx;
681 reg->Swizzle = ureg.swz;
682 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
686 static void emit_dst( struct prog_dst_register *dst,
687 struct ureg ureg, GLuint mask )
689 dst->File = ureg.file;
690 dst->Index = ureg.idx;
691 dst->WriteMask = mask;
692 dst->CondMask = COND_TR; /* always pass cond test */
693 dst->CondSwizzle = SWIZZLE_NOOP;
696 static struct prog_instruction *
697 emit_op(struct texenv_fragment_program *p,
706 const GLuint nr = p->program->Base.NumInstructions++;
707 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
709 assert(nr < MAX_INSTRUCTIONS);
711 _mesa_init_instructions(inst, 1);
714 emit_arg( &inst->SrcReg[0], src0 );
715 emit_arg( &inst->SrcReg[1], src1 );
716 emit_arg( &inst->SrcReg[2], src2 );
718 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
720 emit_dst( &inst->DstReg, dest, mask );
723 /* Accounting for indirection tracking:
725 if (dest.file == PROGRAM_TEMPORARY)
726 p->temps_output |= 1 << dest.idx;
733 static struct ureg emit_arith( struct texenv_fragment_program *p,
742 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
744 /* Accounting for indirection tracking:
746 if (src0.file == PROGRAM_TEMPORARY)
747 p->alu_temps |= 1 << src0.idx;
749 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
750 p->alu_temps |= 1 << src1.idx;
752 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
753 p->alu_temps |= 1 << src2.idx;
755 if (dest.file == PROGRAM_TEMPORARY)
756 p->alu_temps |= 1 << dest.idx;
758 p->program->Base.NumAluInstructions++;
762 static struct ureg emit_texld( struct texenv_fragment_program *p,
771 struct prog_instruction *inst = emit_op( p, op,
773 GL_FALSE, /* don't saturate? */
778 inst->TexSrcTarget = tex_idx;
779 inst->TexSrcUnit = tex_unit;
780 inst->TexShadow = tex_shadow;
782 p->program->Base.NumTexInstructions++;
784 /* Accounting for indirection tracking:
786 reserve_temp(p, dest);
789 /* Is this a texture indirection?
791 if ((coord.file == PROGRAM_TEMPORARY &&
792 (p->temps_output & (1<<coord.idx))) ||
793 (dest.file == PROGRAM_TEMPORARY &&
794 (p->alu_temps & (1<<dest.idx)))) {
795 p->program->Base.NumTexIndirections++;
796 p->temps_output = 1<<coord.idx;
798 assert(0); /* KW: texture env crossbar */
806 static struct ureg register_const4f( struct texenv_fragment_program *p,
819 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
821 r = make_ureg(PROGRAM_CONSTANT, idx);
826 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
827 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
828 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
829 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
832 static struct ureg get_one( struct texenv_fragment_program *p )
834 if (is_undef(p->one))
835 p->one = register_scalar_const(p, 1.0);
839 static struct ureg get_half( struct texenv_fragment_program *p )
841 if (is_undef(p->half))
842 p->half = register_scalar_const(p, 0.5);
846 static struct ureg get_zero( struct texenv_fragment_program *p )
848 if (is_undef(p->zero))
849 p->zero = register_scalar_const(p, 0.0);
854 static void program_error( struct texenv_fragment_program *p, const char *msg )
856 _mesa_problem(NULL, msg);
860 static struct ureg get_source( struct texenv_fragment_program *p,
861 GLuint src, GLuint unit )
865 assert(!is_undef(p->src_texture[unit]));
866 return p->src_texture[unit];
876 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
877 return p->src_texture[src - SRC_TEXTURE0];
880 return register_param2(p, STATE_TEXENV_COLOR, unit);
882 case SRC_PRIMARY_COLOR:
883 return register_input(p, FRAG_ATTRIB_COL0);
889 if (is_undef(p->src_previous))
890 return register_input(p, FRAG_ATTRIB_COL0);
892 return p->src_previous;
900 static struct ureg emit_combine_source( struct texenv_fragment_program *p,
906 struct ureg arg, src, one;
908 src = get_source(p, source, unit);
911 case OPR_ONE_MINUS_SRC_COLOR:
913 * Emit tmp = 1.0 - arg.xyzw
917 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
920 if (mask == WRITEMASK_W)
923 return swizzle1( src, SWIZZLE_W );
924 case OPR_ONE_MINUS_SRC_ALPHA:
926 * Emit tmp = 1.0 - arg.wwww
930 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
931 one, swizzle1(src, SWIZZLE_W), undef);
945 * Check if the RGB and Alpha sources and operands match for the given
946 * texture unit's combinder state. When the RGB and A sources and
947 * operands match, we can emit fewer instructions.
949 static GLboolean args_match( const struct state_key *key, GLuint unit )
951 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
953 for (i = 0; i < numArgs; i++) {
954 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
957 switch (key->unit[unit].OptA[i].Operand) {
959 switch (key->unit[unit].OptRGB[i].Operand) {
967 case OPR_ONE_MINUS_SRC_ALPHA:
968 switch (key->unit[unit].OptRGB[i].Operand) {
969 case OPR_ONE_MINUS_SRC_COLOR:
970 case OPR_ONE_MINUS_SRC_ALPHA:
977 return GL_FALSE; /* impossible */
984 static struct ureg emit_combine( struct texenv_fragment_program *p,
991 const struct mode_opt *opt)
993 struct ureg src[MAX_COMBINER_TERMS];
994 struct ureg tmp, half;
997 assert(nr <= MAX_COMBINER_TERMS);
999 tmp = undef; /* silence warning (bug 5318) */
1001 for (i = 0; i < nr; i++)
1002 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
1006 if (mask == WRITEMASK_XYZW && !saturate)
1009 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
1011 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
1012 src[0], src[1], undef );
1014 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
1015 src[0], src[1], undef );
1016 case MODE_ADD_SIGNED:
1017 /* tmp = arg0 + arg1
1021 tmp = get_temp( p );
1022 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1023 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
1025 case MODE_INTERPOLATE:
1026 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1028 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
1031 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
1033 case MODE_DOT3_RGBA:
1034 case MODE_DOT3_RGBA_EXT:
1035 case MODE_DOT3_RGB_EXT:
1036 case MODE_DOT3_RGB: {
1037 struct ureg tmp0 = get_temp( p );
1038 struct ureg tmp1 = get_temp( p );
1039 struct ureg neg1 = register_scalar_const(p, -1);
1040 struct ureg two = register_scalar_const(p, 2);
1042 /* tmp0 = 2*src0 - 1
1045 * dst = tmp0 dot3 tmp1
1047 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
1050 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
1053 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
1055 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
1058 case MODE_MODULATE_ADD_ATI:
1059 /* Arg0 * Arg2 + Arg1 */
1060 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
1061 src[0], src[2], src[1] );
1062 case MODE_MODULATE_SIGNED_ADD_ATI: {
1063 /* Arg0 * Arg2 + Arg1 - 0.5 */
1064 struct ureg tmp0 = get_temp(p);
1066 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1067 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1070 case MODE_MODULATE_SUBTRACT_ATI:
1071 /* Arg0 * Arg2 - Arg1 */
1072 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
1074 case MODE_ADD_PRODUCTS:
1075 /* Arg0 * Arg1 + Arg2 * Arg3 */
1077 struct ureg tmp0 = get_temp(p);
1078 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1079 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1082 case MODE_ADD_PRODUCTS_SIGNED:
1083 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1085 struct ureg tmp0 = get_temp(p);
1087 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1088 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1089 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1092 case MODE_BUMP_ENVMAP_ATI:
1093 /* special - not handled here */
1104 * Generate instructions for one texture unit's env/combiner mode.
1107 emit_texenv(struct texenv_fragment_program *p, GLuint unit)
1109 const struct state_key *key = p->state;
1111 GLuint rgb_shift, alpha_shift;
1112 struct ureg out, dest;
1114 if (!key->unit[unit].enabled) {
1115 return get_source(p, SRC_PREVIOUS, 0);
1117 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1118 /* this isn't really a env stage delivering a color and handled elsewhere */
1119 return get_source(p, SRC_PREVIOUS, 0);
1122 switch (key->unit[unit].ModeRGB) {
1123 case MODE_DOT3_RGB_EXT:
1124 alpha_shift = key->unit[unit].ScaleShiftA;
1127 case MODE_DOT3_RGBA_EXT:
1132 rgb_shift = key->unit[unit].ScaleShiftRGB;
1133 alpha_shift = key->unit[unit].ScaleShiftA;
1137 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1138 * We don't want to clamp twice.
1140 saturate = !(rgb_shift || alpha_shift);
1142 /* If this is the very last calculation, emit direct to output reg:
1144 if (key->separate_specular ||
1145 unit != p->last_tex_stage ||
1148 dest = get_temp( p );
1150 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
1152 /* Emit the RGB and A combine ops
1154 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1155 args_match(key, unit)) {
1156 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1158 key->unit[unit].NumArgsRGB,
1159 key->unit[unit].ModeRGB,
1160 key->unit[unit].OptRGB);
1162 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
1163 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
1164 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1166 key->unit[unit].NumArgsRGB,
1167 key->unit[unit].ModeRGB,
1168 key->unit[unit].OptRGB);
1171 /* Need to do something to stop from re-emitting identical
1172 * argument calculations here:
1174 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1176 key->unit[unit].NumArgsRGB,
1177 key->unit[unit].ModeRGB,
1178 key->unit[unit].OptRGB);
1179 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1181 key->unit[unit].NumArgsA,
1182 key->unit[unit].ModeA,
1183 key->unit[unit].OptA);
1186 /* Deal with the final shift:
1188 if (alpha_shift || rgb_shift) {
1191 saturate = GL_TRUE; /* always saturate at this point */
1193 if (rgb_shift == alpha_shift) {
1194 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
1197 shift = register_const4f(p,
1198 (GLfloat)(1<<rgb_shift),
1199 (GLfloat)(1<<rgb_shift),
1200 (GLfloat)(1<<rgb_shift),
1201 (GLfloat)(1<<alpha_shift));
1203 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
1204 saturate, out, shift, undef );
1212 * Generate instruction for getting a texture source term.
1214 static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1216 if (is_undef(p->src_texture[unit])) {
1217 const GLuint texTarget = p->state->unit[unit].source_index;
1218 struct ureg texcoord;
1219 struct ureg tmp = get_tex_temp( p );
1221 if (is_undef(p->texcoord_tex[unit])) {
1222 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1225 /* might want to reuse this reg for tex output actually */
1226 texcoord = p->texcoord_tex[unit];
1229 /* TODO: Use D0_MASK_XY where possible.
1231 if (p->state->unit[unit].enabled) {
1232 GLboolean shadow = GL_FALSE;
1234 if (p->state->unit[unit].shadow) {
1235 p->program->Base.ShadowSamplers |= 1 << unit;
1239 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1240 tmp, WRITEMASK_XYZW,
1241 unit, texTarget, shadow,
1244 p->program->Base.SamplersUsed |= (1 << unit);
1245 /* This identity mapping should already be in place
1246 * (see _mesa_init_program_struct()) but let's be safe.
1248 p->program->Base.SamplerUnits[unit] = unit;
1251 p->src_texture[unit] = get_zero(p);
1255 static GLboolean load_texenv_source( struct texenv_fragment_program *p,
1256 GLuint src, GLuint unit )
1260 load_texture(p, unit);
1271 load_texture(p, src - SRC_TEXTURE0);
1275 /* not a texture src - do nothing */
1284 * Generate instructions for loading all texture source terms.
1287 load_texunit_sources( struct texenv_fragment_program *p, GLuint unit )
1289 const struct state_key *key = p->state;
1292 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1293 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
1296 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1297 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1304 * Generate instructions for loading bump map textures.
1307 load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
1309 const struct state_key *key = p->state;
1310 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1311 struct ureg texcDst, bumpMapRes;
1312 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1313 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1314 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1315 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1317 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1319 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1320 texcDst = get_tex_temp( p );
1321 p->texcoord_tex[bumpedUnitNr] = texcDst;
1323 /* Apply rot matrix and add coords to be available in next phase.
1324 * dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1)
1325 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1327 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1328 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1329 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1330 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1332 /* Move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1333 * enough to access this later, should optimize away.
1335 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0,
1336 constdudvcolor, undef, undef );
1342 * Generate a new fragment program which implements the context's
1343 * current texture env/combine mode.
1346 create_new_program(GLcontext *ctx, struct state_key *key,
1347 struct gl_fragment_program *program)
1349 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
1350 struct texenv_fragment_program p;
1352 struct ureg cf, out;
1354 _mesa_memset(&p, 0, sizeof(p));
1356 p.program = program;
1358 /* During code generation, use locally-allocated instruction buffer,
1359 * then alloc dynamic storage below.
1361 p.program->Base.Instructions = instBuffer;
1362 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
1363 p.program->Base.String = NULL;
1364 p.program->Base.NumTexIndirections = 1; /* is this right? */
1365 p.program->Base.NumTexInstructions = 0;
1366 p.program->Base.NumAluInstructions = 0;
1367 p.program->Base.NumInstructions = 0;
1368 p.program->Base.NumTemporaries = 0;
1369 p.program->Base.NumParameters = 0;
1370 p.program->Base.NumAttributes = 0;
1371 p.program->Base.NumAddressRegs = 0;
1372 p.program->Base.Parameters = _mesa_new_parameter_list();
1373 p.program->Base.InputsRead = 0x0;
1374 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
1376 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
1377 p.src_texture[unit] = undef;
1378 p.texcoord_tex[unit] = undef;
1381 p.src_previous = undef;
1386 p.last_tex_stage = 0;
1387 release_temps(ctx, &p);
1389 if (key->enabled_units) {
1390 GLboolean needbumpstage = GL_FALSE;
1392 /* Zeroth pass - bump map textures first */
1393 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
1394 if (key->unit[unit].enabled &&
1395 key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1396 needbumpstage = GL_TRUE;
1397 load_texunit_bumpmap( &p, unit );
1400 p.program->Base.NumTexIndirections++;
1402 /* First pass - to support texture_env_crossbar, first identify
1403 * all referenced texture sources and emit texld instructions
1406 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
1407 if (key->unit[unit].enabled) {
1408 load_texunit_sources( &p, unit );
1409 p.last_tex_stage = unit;
1412 /* Second pass - emit combine instructions to build final color:
1414 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
1415 if (key->unit[unit].enabled) {
1416 p.src_previous = emit_texenv( &p, unit );
1417 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
1418 release_temps(ctx, &p); /* release all temps */
1422 cf = get_source( &p, SRC_PREVIOUS, 0 );
1423 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
1425 if (key->separate_specular) {
1426 /* Emit specular add.
1428 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
1429 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1430 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
1432 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
1433 /* Will wind up in here if no texture enabled or a couple of
1434 * other scenarios (GL_REPLACE for instance).
1436 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
1441 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
1443 if (key->fog_enabled) {
1444 /* Pull fog mode from GLcontext, the value in the state key is
1445 * a reduced value and not what is expected in FogOption
1447 p.program->FogOption = ctx->Fog.Mode;
1448 p.program->Base.InputsRead |= FRAG_BIT_FOGC;
1451 p.program->FogOption = GL_NONE;
1454 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
1455 program_error(&p, "Exceeded max nr indirect texture lookups");
1457 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
1458 program_error(&p, "Exceeded max TEX instructions");
1460 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
1461 program_error(&p, "Exceeded max ALU instructions");
1463 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
1465 /* Allocate final instruction array */
1466 p.program->Base.Instructions
1467 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1468 if (!p.program->Base.Instructions) {
1469 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1470 "generating tex env program");
1473 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1474 p.program->Base.NumInstructions);
1476 if (p.program->FogOption) {
1477 _mesa_append_fog_code(ctx, p.program);
1478 p.program->FogOption = GL_NONE;
1482 /* Notify driver the fragment program has (actually) changed.
1484 if (ctx->Driver.ProgramStringNotify) {
1485 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1490 _mesa_print_program(&p.program->Base);
1497 * Return a fragment program which implements the current
1498 * fixed-function texture, fog and color-sum operations.
1500 struct gl_fragment_program *
1501 _mesa_get_fixed_func_fragment_program(GLcontext *ctx)
1503 struct gl_fragment_program *prog;
1504 struct state_key key;
1506 make_state_key(ctx, &key);
1508 prog = (struct gl_fragment_program *)
1509 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1513 prog = (struct gl_fragment_program *)
1514 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1516 create_new_program(ctx, &key, prog);
1518 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1519 &key, sizeof(key), &prog->Base);