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 "program/program.h"
32 #include "program/prog_parameter.h"
33 #include "program/prog_cache.h"
34 #include "program/prog_instruction.h"
35 #include "program/prog_print.h"
36 #include "program/prog_statevars.h"
37 #include "program/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 GLubyte Source
; /**< SRC_x */
90 GLubyte 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;
101 GLuint num_draw_buffers
:4;
103 /* NOTE: This array of structs must be last! (see "keySize" below) */
106 GLuint source_index
:3; /**< TEXTURE_x_INDEX */
108 GLuint ScaleShiftRGB
:2;
109 GLuint ScaleShiftA
:2;
111 GLuint NumArgsRGB
:3; /**< up to MAX_COMBINER_TERMS */
112 GLuint ModeRGB
:5; /**< MODE_x */
114 GLuint NumArgsA
:3; /**< up to MAX_COMBINER_TERMS */
115 GLuint ModeA
:5; /**< MODE_x */
117 GLuint texture_cyl_wrap
:1; /**< For gallium test/debug only */
119 struct mode_opt OptRGB
[MAX_COMBINER_TERMS
];
120 struct mode_opt OptA
[MAX_COMBINER_TERMS
];
121 } unit
[MAX_TEXTURE_UNITS
];
127 #define FOG_UNKNOWN 3
129 static GLuint
translate_fog_mode( GLenum mode
)
132 case GL_LINEAR
: return FOG_LINEAR
;
133 case GL_EXP
: return FOG_EXP
;
134 case GL_EXP2
: return FOG_EXP2
;
135 default: return FOG_UNKNOWN
;
139 #define OPR_SRC_COLOR 0
140 #define OPR_ONE_MINUS_SRC_COLOR 1
141 #define OPR_SRC_ALPHA 2
142 #define OPR_ONE_MINUS_SRC_ALPHA 3
145 #define OPR_UNKNOWN 7
147 static GLuint
translate_operand( GLenum operand
)
150 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
151 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
152 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
153 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
154 case GL_ZERO
: return OPR_ZERO
;
155 case GL_ONE
: return OPR_ONE
;
162 #define SRC_TEXTURE 0
163 #define SRC_TEXTURE0 1
164 #define SRC_TEXTURE1 2
165 #define SRC_TEXTURE2 3
166 #define SRC_TEXTURE3 4
167 #define SRC_TEXTURE4 5
168 #define SRC_TEXTURE5 6
169 #define SRC_TEXTURE6 7
170 #define SRC_TEXTURE7 8
171 #define SRC_CONSTANT 9
172 #define SRC_PRIMARY_COLOR 10
173 #define SRC_PREVIOUS 11
175 #define SRC_UNKNOWN 15
177 static GLuint
translate_source( GLenum src
)
180 case GL_TEXTURE
: return SRC_TEXTURE
;
188 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
189 case GL_CONSTANT
: return SRC_CONSTANT
;
190 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
191 case GL_PREVIOUS
: return SRC_PREVIOUS
;
200 #define MODE_REPLACE 0 /* r = a0 */
201 #define MODE_MODULATE 1 /* r = a0 * a1 */
202 #define MODE_ADD 2 /* r = a0 + a1 */
203 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
204 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
205 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
206 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
207 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
208 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
209 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
210 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
211 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
212 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
213 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
214 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
215 #define MODE_BUMP_ENVMAP_ATI 15 /* special */
216 #define MODE_UNKNOWN 16
219 * Translate GL combiner state into a MODE_x value
221 static GLuint
translate_mode( GLenum envMode
, GLenum mode
)
224 case GL_REPLACE
: return MODE_REPLACE
;
225 case GL_MODULATE
: return MODE_MODULATE
;
227 if (envMode
== GL_COMBINE4_NV
)
228 return MODE_ADD_PRODUCTS
;
232 if (envMode
== GL_COMBINE4_NV
)
233 return MODE_ADD_PRODUCTS_SIGNED
;
235 return MODE_ADD_SIGNED
;
236 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
237 case GL_SUBTRACT
: return MODE_SUBTRACT
;
238 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
239 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
240 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
241 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
242 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
243 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
244 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
245 case GL_BUMP_ENVMAP_ATI
: return MODE_BUMP_ENVMAP_ATI
;
254 * Do we need to clamp the results of the given texture env/combine mode?
255 * If the inputs to the mode are in [0,1] we don't always have to clamp
259 need_saturate( GLuint mode
)
264 case MODE_INTERPOLATE
:
267 case MODE_ADD_SIGNED
:
270 case MODE_DOT3_RGB_EXT
:
272 case MODE_DOT3_RGBA_EXT
:
273 case MODE_MODULATE_ADD_ATI
:
274 case MODE_MODULATE_SIGNED_ADD_ATI
:
275 case MODE_MODULATE_SUBTRACT_ATI
:
276 case MODE_ADD_PRODUCTS
:
277 case MODE_ADD_PRODUCTS_SIGNED
:
278 case MODE_BUMP_ENVMAP_ATI
:
289 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
291 static GLuint
translate_tex_src_bit( GLbitfield bit
)
294 return _mesa_ffs(bit
) - 1;
298 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
299 #define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
302 * Identify all possible varying inputs. The fragment program will
303 * never reference non-varying inputs, but will track them via state
306 * This function figures out all the inputs that the fragment program
307 * has access to. The bitmask is later reduced to just those which
308 * are actually referenced.
310 static GLbitfield
get_fp_input_mask( GLcontext
*ctx
)
313 const GLboolean vertexShader
= (ctx
->Shader
.CurrentProgram
&&
314 ctx
->Shader
.CurrentProgram
->LinkStatus
&&
315 ctx
->Shader
.CurrentProgram
->VertexProgram
);
316 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
317 GLbitfield fp_inputs
= 0x0;
319 if (ctx
->VertexProgram
._Overriden
) {
320 /* Somebody's messing with the vertex program and we don't have
321 * a clue what's happening. Assume that it could be producing
322 * all possible outputs.
326 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
327 /* _NEW_RENDERMODE */
328 fp_inputs
= (FRAG_BIT_COL0
| FRAG_BIT_TEX0
);
330 else if (!(vertexProgram
|| vertexShader
) ||
331 !ctx
->VertexProgram
._Current
) {
332 /* Fixed function vertex logic */
334 GLbitfield varying_inputs
= ctx
->varying_vp_inputs
;
336 /* These get generated in the setup routine regardless of the
340 if (ctx
->Point
.PointSprite
)
341 varying_inputs
|= FRAG_BITS_TEX_ANY
;
343 /* First look at what values may be computed by the generated
347 if (ctx
->Light
.Enabled
) {
348 fp_inputs
|= FRAG_BIT_COL0
;
350 if (texenv_doing_secondary_color(ctx
))
351 fp_inputs
|= FRAG_BIT_COL1
;
355 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
356 ctx
->Texture
._TexMatEnabled
) << FRAG_ATTRIB_TEX0
;
358 /* Then look at what might be varying as a result of enabled
361 if (varying_inputs
& VERT_BIT_COLOR0
)
362 fp_inputs
|= FRAG_BIT_COL0
;
363 if (varying_inputs
& VERT_BIT_COLOR1
)
364 fp_inputs
|= FRAG_BIT_COL1
;
366 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
367 << FRAG_ATTRIB_TEX0
);
371 /* calculate from vp->outputs */
372 struct gl_vertex_program
*vprog
;
373 GLbitfield64 vp_outputs
;
375 /* Choose GLSL vertex shader over ARB vertex program. Need this
376 * since vertex shader state validation comes after fragment state
377 * validation (see additional comments in state.c).
380 vprog
= ctx
->Shader
.CurrentProgram
->VertexProgram
;
382 vprog
= ctx
->VertexProgram
.Current
;
384 vp_outputs
= vprog
->Base
.OutputsWritten
;
386 /* These get generated in the setup routine regardless of the
390 if (ctx
->Point
.PointSprite
)
391 vp_outputs
|= FRAG_BITS_TEX_ANY
;
393 if (vp_outputs
& (1 << VERT_RESULT_COL0
))
394 fp_inputs
|= FRAG_BIT_COL0
;
395 if (vp_outputs
& (1 << VERT_RESULT_COL1
))
396 fp_inputs
|= FRAG_BIT_COL1
;
398 fp_inputs
|= (((vp_outputs
& VERT_RESULT_TEX_ANY
) >> VERT_RESULT_TEX0
)
399 << FRAG_ATTRIB_TEX0
);
407 * Examine current texture environment state and generate a unique
408 * key to identify it.
410 static GLuint
make_state_key( GLcontext
*ctx
, struct state_key
*key
)
413 GLbitfield inputs_referenced
= FRAG_BIT_COL0
;
414 const GLbitfield inputs_available
= get_fp_input_mask( ctx
);
417 memset(key
, 0, sizeof(*key
));
420 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
421 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
422 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
423 const struct gl_tex_env_combine_state
*comb
= texUnit
->_CurrentCombine
;
426 if (!texUnit
->_ReallyEnabled
|| !texUnit
->Enabled
)
429 format
= texObj
->Image
[0][texObj
->BaseLevel
]->_BaseFormat
;
431 key
->unit
[i
].enabled
= 1;
432 key
->enabled_units
|= (1<<i
);
433 key
->nr_enabled_units
= i
+ 1;
434 inputs_referenced
|= FRAG_BIT_TEX(i
);
436 key
->unit
[i
].source_index
=
437 translate_tex_src_bit(texUnit
->_ReallyEnabled
);
439 key
->unit
[i
].shadow
= ((texObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
440 ((format
== GL_DEPTH_COMPONENT
) ||
441 (format
== GL_DEPTH_STENCIL_EXT
)));
443 key
->unit
[i
].NumArgsRGB
= comb
->_NumArgsRGB
;
444 key
->unit
[i
].NumArgsA
= comb
->_NumArgsA
;
446 key
->unit
[i
].ModeRGB
=
447 translate_mode(texUnit
->EnvMode
, comb
->ModeRGB
);
449 translate_mode(texUnit
->EnvMode
, comb
->ModeA
);
451 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
452 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
454 for (j
= 0; j
< MAX_COMBINER_TERMS
; j
++) {
455 key
->unit
[i
].OptRGB
[j
].Operand
= translate_operand(comb
->OperandRGB
[j
]);
456 key
->unit
[i
].OptA
[j
].Operand
= translate_operand(comb
->OperandA
[j
]);
457 key
->unit
[i
].OptRGB
[j
].Source
= translate_source(comb
->SourceRGB
[j
]);
458 key
->unit
[i
].OptA
[j
].Source
= translate_source(comb
->SourceA
[j
]);
461 if (key
->unit
[i
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
462 /* requires some special translation */
463 key
->unit
[i
].NumArgsRGB
= 2;
464 key
->unit
[i
].ScaleShiftRGB
= 0;
465 key
->unit
[i
].OptRGB
[0].Operand
= OPR_SRC_COLOR
;
466 key
->unit
[i
].OptRGB
[0].Source
= SRC_TEXTURE
;
467 key
->unit
[i
].OptRGB
[1].Operand
= OPR_SRC_COLOR
;
468 key
->unit
[i
].OptRGB
[1].Source
= texUnit
->BumpTarget
- GL_TEXTURE0
+ SRC_TEXTURE0
;
471 /* this is a back-door for enabling cylindrical texture wrap mode */
472 if (texObj
->Priority
== 0.125)
473 key
->unit
[i
].texture_cyl_wrap
= 1;
476 /* _NEW_LIGHT | _NEW_FOG */
477 if (texenv_doing_secondary_color(ctx
)) {
478 key
->separate_specular
= 1;
479 inputs_referenced
|= FRAG_BIT_COL1
;
483 if (ctx
->Fog
.Enabled
) {
484 key
->fog_enabled
= 1;
485 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
486 inputs_referenced
|= FRAG_BIT_FOGC
; /* maybe */
490 key
->num_draw_buffers
= ctx
->DrawBuffer
->_NumColorDrawBuffers
;
492 key
->inputs_available
= (inputs_available
& inputs_referenced
);
494 /* compute size of state key, ignoring unused texture units */
495 keySize
= sizeof(*key
) - sizeof(key
->unit
)
496 + key
->nr_enabled_units
* sizeof(key
->unit
[0]);
503 * Use uregs to represent registers internally, translate to Mesa's
504 * expected formats on emit.
506 * NOTE: These are passed by value extensively in this file rather
507 * than as usual by pointer reference. If this disturbs you, try
508 * remembering they are just 32bits in size.
510 * GCC is smart enough to deal with these dword-sized structures in
511 * much the same way as if I had defined them as dwords and was using
512 * macros to access and set the fields. This is much nicer and easier
523 static const struct ureg undef
= {
532 /** State used to build the fragment program:
534 struct texenv_fragment_program
{
535 struct gl_fragment_program
*program
;
536 struct state_key
*state
;
538 GLbitfield alu_temps
; /**< Track texture indirections, see spec. */
539 GLbitfield temps_output
; /**< Track texture indirections, see spec. */
540 GLbitfield temp_in_use
; /**< Tracks temporary regs which are in use. */
543 struct ureg src_texture
[MAX_TEXTURE_COORD_UNITS
];
544 /* Reg containing each texture unit's sampled texture color,
548 struct ureg texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
549 /* Reg containing texcoord for a texture unit,
550 * needed for bump mapping, else undef.
553 struct ureg src_previous
; /**< Reg containing color from previous
554 * stage. May need to be decl'd.
557 GLuint last_tex_stage
; /**< Number of last enabled texture unit */
566 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
572 reg
.swz
= SWIZZLE_NOOP
;
577 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
579 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
582 GET_SWZ(reg
.swz
, w
));
587 static struct ureg
swizzle1( struct ureg reg
, int x
)
589 return swizzle(reg
, x
, x
, x
, x
);
592 static struct ureg
negate( struct ureg reg
)
598 static GLboolean
is_undef( struct ureg reg
)
600 return reg
.file
== PROGRAM_UNDEFINED
;
604 static struct ureg
get_temp( struct texenv_fragment_program
*p
)
608 /* First try and reuse temps which have been used already:
610 bit
= _mesa_ffs( ~p
->temp_in_use
& p
->alu_temps
);
612 /* Then any unused temporary:
615 bit
= _mesa_ffs( ~p
->temp_in_use
);
618 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
622 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
623 p
->program
->Base
.NumTemporaries
= bit
;
625 p
->temp_in_use
|= 1<<(bit
-1);
626 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
629 static struct ureg
get_tex_temp( struct texenv_fragment_program
*p
)
633 /* First try to find available temp not previously used (to avoid
634 * starting a new texture indirection). According to the spec, the
635 * ~p->temps_output isn't necessary, but will keep it there for
638 bit
= _mesa_ffs( ~p
->temp_in_use
& ~p
->alu_temps
& ~p
->temps_output
);
640 /* Then any unused temporary:
643 bit
= _mesa_ffs( ~p
->temp_in_use
);
646 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
650 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
651 p
->program
->Base
.NumTemporaries
= bit
;
653 p
->temp_in_use
|= 1<<(bit
-1);
654 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
658 /** Mark a temp reg as being no longer allocatable. */
659 static void reserve_temp( struct texenv_fragment_program
*p
, struct ureg r
)
661 if (r
.file
== PROGRAM_TEMPORARY
)
662 p
->temps_output
|= (1 << r
.idx
);
666 static void release_temps(GLcontext
*ctx
, struct texenv_fragment_program
*p
)
668 GLuint max_temp
= ctx
->Const
.FragmentProgram
.MaxTemps
;
670 /* KW: To support tex_env_crossbar, don't release the registers in
673 if (max_temp
>= sizeof(int) * 8)
674 p
->temp_in_use
= p
->temps_output
;
676 p
->temp_in_use
= ~((1<<max_temp
)-1) | p
->temps_output
;
680 static struct ureg
register_param5( struct texenv_fragment_program
*p
,
687 gl_state_index tokens
[STATE_LENGTH
];
694 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
695 return make_ureg(PROGRAM_STATE_VAR
, idx
);
699 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
700 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
701 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
702 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
704 static GLuint
frag_to_vert_attrib( GLuint attrib
)
707 case FRAG_ATTRIB_COL0
: return VERT_ATTRIB_COLOR0
;
708 case FRAG_ATTRIB_COL1
: return VERT_ATTRIB_COLOR1
;
710 assert(attrib
>= FRAG_ATTRIB_TEX0
);
711 assert(attrib
<= FRAG_ATTRIB_TEX7
);
712 return attrib
- FRAG_ATTRIB_TEX0
+ VERT_ATTRIB_TEX0
;
717 static struct ureg
register_input( struct texenv_fragment_program
*p
, GLuint input
)
719 if (p
->state
->inputs_available
& (1<<input
)) {
720 p
->program
->Base
.InputsRead
|= (1 << input
);
721 return make_ureg(PROGRAM_INPUT
, input
);
724 GLuint idx
= frag_to_vert_attrib( input
);
725 return register_param3( p
, STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, idx
);
730 static void emit_arg( struct prog_src_register
*reg
,
733 reg
->File
= ureg
.file
;
734 reg
->Index
= ureg
.idx
;
735 reg
->Swizzle
= ureg
.swz
;
736 reg
->Negate
= ureg
.negatebase
? NEGATE_XYZW
: NEGATE_NONE
;
740 static void emit_dst( struct prog_dst_register
*dst
,
741 struct ureg ureg
, GLuint mask
)
743 dst
->File
= ureg
.file
;
744 dst
->Index
= ureg
.idx
;
745 dst
->WriteMask
= mask
;
746 dst
->CondMask
= COND_TR
; /* always pass cond test */
747 dst
->CondSwizzle
= SWIZZLE_NOOP
;
750 static struct prog_instruction
*
751 emit_op(struct texenv_fragment_program
*p
,
760 const GLuint nr
= p
->program
->Base
.NumInstructions
++;
761 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
763 assert(nr
< MAX_INSTRUCTIONS
);
765 _mesa_init_instructions(inst
, 1);
768 emit_arg( &inst
->SrcReg
[0], src0
);
769 emit_arg( &inst
->SrcReg
[1], src1
);
770 emit_arg( &inst
->SrcReg
[2], src2
);
772 inst
->SaturateMode
= saturate
? SATURATE_ZERO_ONE
: SATURATE_OFF
;
774 emit_dst( &inst
->DstReg
, dest
, mask
);
777 /* Accounting for indirection tracking:
779 if (dest
.file
== PROGRAM_TEMPORARY
)
780 p
->temps_output
|= 1 << dest
.idx
;
787 static struct ureg
emit_arith( struct texenv_fragment_program
*p
,
796 emit_op(p
, op
, dest
, mask
, saturate
, src0
, src1
, src2
);
798 /* Accounting for indirection tracking:
800 if (src0
.file
== PROGRAM_TEMPORARY
)
801 p
->alu_temps
|= 1 << src0
.idx
;
803 if (!is_undef(src1
) && src1
.file
== PROGRAM_TEMPORARY
)
804 p
->alu_temps
|= 1 << src1
.idx
;
806 if (!is_undef(src2
) && src2
.file
== PROGRAM_TEMPORARY
)
807 p
->alu_temps
|= 1 << src2
.idx
;
809 if (dest
.file
== PROGRAM_TEMPORARY
)
810 p
->alu_temps
|= 1 << dest
.idx
;
812 p
->program
->Base
.NumAluInstructions
++;
816 static struct ureg
emit_texld( struct texenv_fragment_program
*p
,
825 struct prog_instruction
*inst
= emit_op( p
, op
,
827 GL_FALSE
, /* don't saturate? */
832 inst
->TexSrcTarget
= tex_idx
;
833 inst
->TexSrcUnit
= tex_unit
;
834 inst
->TexShadow
= tex_shadow
;
836 p
->program
->Base
.NumTexInstructions
++;
838 /* Accounting for indirection tracking:
840 reserve_temp(p
, dest
);
843 /* Is this a texture indirection?
845 if ((coord
.file
== PROGRAM_TEMPORARY
&&
846 (p
->temps_output
& (1<<coord
.idx
))) ||
847 (dest
.file
== PROGRAM_TEMPORARY
&&
848 (p
->alu_temps
& (1<<dest
.idx
)))) {
849 p
->program
->Base
.NumTexIndirections
++;
850 p
->temps_output
= 1<<coord
.idx
;
852 assert(0); /* KW: texture env crossbar */
860 static struct ureg
register_const4f( struct texenv_fragment_program
*p
,
873 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
875 r
= make_ureg(PROGRAM_CONSTANT
, idx
);
880 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
881 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
882 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
883 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
886 static struct ureg
get_one( struct texenv_fragment_program
*p
)
888 if (is_undef(p
->one
))
889 p
->one
= register_scalar_const(p
, 1.0);
893 static struct ureg
get_half( struct texenv_fragment_program
*p
)
895 if (is_undef(p
->half
))
896 p
->half
= register_scalar_const(p
, 0.5);
900 static struct ureg
get_zero( struct texenv_fragment_program
*p
)
902 if (is_undef(p
->zero
))
903 p
->zero
= register_scalar_const(p
, 0.0);
908 static void program_error( struct texenv_fragment_program
*p
, const char *msg
)
910 _mesa_problem(NULL
, msg
);
914 static struct ureg
get_source( struct texenv_fragment_program
*p
,
915 GLuint src
, GLuint unit
)
919 assert(!is_undef(p
->src_texture
[unit
]));
920 return p
->src_texture
[unit
];
930 assert(!is_undef(p
->src_texture
[src
- SRC_TEXTURE0
]));
931 return p
->src_texture
[src
- SRC_TEXTURE0
];
934 return register_param2(p
, STATE_TEXENV_COLOR
, unit
);
936 case SRC_PRIMARY_COLOR
:
937 return register_input(p
, FRAG_ATTRIB_COL0
);
943 if (is_undef(p
->src_previous
))
944 return register_input(p
, FRAG_ATTRIB_COL0
);
946 return p
->src_previous
;
954 static struct ureg
emit_combine_source( struct texenv_fragment_program
*p
,
960 struct ureg arg
, src
, one
;
962 src
= get_source(p
, source
, unit
);
965 case OPR_ONE_MINUS_SRC_COLOR
:
967 * Emit tmp = 1.0 - arg.xyzw
971 return emit_arith( p
, OPCODE_SUB
, arg
, mask
, 0, one
, src
, undef
);
974 if (mask
== WRITEMASK_W
)
977 return swizzle1( src
, SWIZZLE_W
);
978 case OPR_ONE_MINUS_SRC_ALPHA
:
980 * Emit tmp = 1.0 - arg.wwww
984 return emit_arith(p
, OPCODE_SUB
, arg
, mask
, 0,
985 one
, swizzle1(src
, SWIZZLE_W
), undef
);
999 * Check if the RGB and Alpha sources and operands match for the given
1000 * texture unit's combinder state. When the RGB and A sources and
1001 * operands match, we can emit fewer instructions.
1003 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
1005 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
1007 for (i
= 0; i
< numArgs
; i
++) {
1008 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
1011 switch (key
->unit
[unit
].OptA
[i
].Operand
) {
1013 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
1021 case OPR_ONE_MINUS_SRC_ALPHA
:
1022 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
1023 case OPR_ONE_MINUS_SRC_COLOR
:
1024 case OPR_ONE_MINUS_SRC_ALPHA
:
1031 return GL_FALSE
; /* impossible */
1038 static struct ureg
emit_combine( struct texenv_fragment_program
*p
,
1045 const struct mode_opt
*opt
)
1047 struct ureg src
[MAX_COMBINER_TERMS
];
1048 struct ureg tmp
, half
;
1051 assert(nr
<= MAX_COMBINER_TERMS
);
1053 for (i
= 0; i
< nr
; i
++)
1054 src
[i
] = emit_combine_source( p
, mask
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
1058 if (mask
== WRITEMASK_XYZW
&& !saturate
)
1061 return emit_arith( p
, OPCODE_MOV
, dest
, mask
, saturate
, src
[0], undef
, undef
);
1063 return emit_arith( p
, OPCODE_MUL
, dest
, mask
, saturate
,
1064 src
[0], src
[1], undef
);
1066 return emit_arith( p
, OPCODE_ADD
, dest
, mask
, saturate
,
1067 src
[0], src
[1], undef
);
1068 case MODE_ADD_SIGNED
:
1069 /* tmp = arg0 + arg1
1073 tmp
= get_temp( p
);
1074 emit_arith( p
, OPCODE_ADD
, tmp
, mask
, 0, src
[0], src
[1], undef
);
1075 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp
, half
, undef
);
1077 case MODE_INTERPOLATE
:
1078 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1080 return emit_arith( p
, OPCODE_LRP
, dest
, mask
, saturate
, src
[2], src
[0], src
[1] );
1083 return emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, src
[0], src
[1], undef
);
1085 case MODE_DOT3_RGBA
:
1086 case MODE_DOT3_RGBA_EXT
:
1087 case MODE_DOT3_RGB_EXT
:
1088 case MODE_DOT3_RGB
: {
1089 struct ureg tmp0
= get_temp( p
);
1090 struct ureg tmp1
= get_temp( p
);
1091 struct ureg neg1
= register_scalar_const(p
, -1);
1092 struct ureg two
= register_scalar_const(p
, 2);
1094 /* tmp0 = 2*src0 - 1
1097 * dst = tmp0 dot3 tmp1
1099 emit_arith( p
, OPCODE_MAD
, tmp0
, WRITEMASK_XYZW
, 0,
1102 if (memcmp(&src
[0], &src
[1], sizeof(struct ureg
)) == 0)
1105 emit_arith( p
, OPCODE_MAD
, tmp1
, WRITEMASK_XYZW
, 0,
1107 emit_arith( p
, OPCODE_DP3
, dest
, mask
, saturate
, tmp0
, tmp1
, undef
);
1110 case MODE_MODULATE_ADD_ATI
:
1111 /* Arg0 * Arg2 + Arg1 */
1112 return emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
,
1113 src
[0], src
[2], src
[1] );
1114 case MODE_MODULATE_SIGNED_ADD_ATI
: {
1115 /* Arg0 * Arg2 + Arg1 - 0.5 */
1116 struct ureg tmp0
= get_temp(p
);
1118 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[0], src
[2], src
[1] );
1119 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
1122 case MODE_MODULATE_SUBTRACT_ATI
:
1123 /* Arg0 * Arg2 - Arg1 */
1124 emit_arith( p
, OPCODE_MAD
, dest
, mask
, 0, src
[0], src
[2], negate(src
[1]) );
1126 case MODE_ADD_PRODUCTS
:
1127 /* Arg0 * Arg1 + Arg2 * Arg3 */
1129 struct ureg tmp0
= get_temp(p
);
1130 emit_arith( p
, OPCODE_MUL
, tmp0
, mask
, 0, src
[0], src
[1], undef
);
1131 emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
, src
[2], src
[3], tmp0
);
1134 case MODE_ADD_PRODUCTS_SIGNED
:
1135 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1137 struct ureg tmp0
= get_temp(p
);
1139 emit_arith( p
, OPCODE_MUL
, tmp0
, mask
, 0, src
[0], src
[1], undef
);
1140 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[2], src
[3], tmp0
);
1141 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
1144 case MODE_BUMP_ENVMAP_ATI
:
1145 /* special - not handled here */
1156 * Generate instructions for one texture unit's env/combiner mode.
1159 emit_texenv(struct texenv_fragment_program
*p
, GLuint unit
)
1161 const struct state_key
*key
= p
->state
;
1162 GLboolean rgb_saturate
, alpha_saturate
;
1163 GLuint rgb_shift
, alpha_shift
;
1164 struct ureg out
, dest
;
1166 if (!key
->unit
[unit
].enabled
) {
1167 return get_source(p
, SRC_PREVIOUS
, 0);
1169 if (key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1170 /* this isn't really a env stage delivering a color and handled elsewhere */
1171 return get_source(p
, SRC_PREVIOUS
, 0);
1174 switch (key
->unit
[unit
].ModeRGB
) {
1175 case MODE_DOT3_RGB_EXT
:
1176 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
1179 case MODE_DOT3_RGBA_EXT
:
1184 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
1185 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
1189 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1190 * We don't want to clamp twice.
1193 rgb_saturate
= GL_FALSE
; /* saturate after rgb shift */
1194 else if (need_saturate(key
->unit
[unit
].ModeRGB
))
1195 rgb_saturate
= GL_TRUE
;
1197 rgb_saturate
= GL_FALSE
;
1200 alpha_saturate
= GL_FALSE
; /* saturate after alpha shift */
1201 else if (need_saturate(key
->unit
[unit
].ModeA
))
1202 alpha_saturate
= GL_TRUE
;
1204 alpha_saturate
= GL_FALSE
;
1206 /* If this is the very last calculation (and various other conditions
1207 * are met), emit directly to the color output register. Otherwise,
1208 * emit to a temporary register.
1210 if (key
->separate_specular
||
1211 unit
!= p
->last_tex_stage
||
1213 key
->num_draw_buffers
!= 1 ||
1215 dest
= get_temp( p
);
1217 dest
= make_ureg(PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
);
1219 /* Emit the RGB and A combine ops
1221 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
1222 args_match(key
, unit
)) {
1223 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, rgb_saturate
,
1225 key
->unit
[unit
].NumArgsRGB
,
1226 key
->unit
[unit
].ModeRGB
,
1227 key
->unit
[unit
].OptRGB
);
1229 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
1230 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
1231 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, rgb_saturate
,
1233 key
->unit
[unit
].NumArgsRGB
,
1234 key
->unit
[unit
].ModeRGB
,
1235 key
->unit
[unit
].OptRGB
);
1238 /* Need to do something to stop from re-emitting identical
1239 * argument calculations here:
1241 out
= emit_combine( p
, dest
, WRITEMASK_XYZ
, rgb_saturate
,
1243 key
->unit
[unit
].NumArgsRGB
,
1244 key
->unit
[unit
].ModeRGB
,
1245 key
->unit
[unit
].OptRGB
);
1246 out
= emit_combine( p
, dest
, WRITEMASK_W
, alpha_saturate
,
1248 key
->unit
[unit
].NumArgsA
,
1249 key
->unit
[unit
].ModeA
,
1250 key
->unit
[unit
].OptA
);
1253 /* Deal with the final shift:
1255 if (alpha_shift
|| rgb_shift
) {
1257 GLboolean saturate
= GL_TRUE
; /* always saturate at this point */
1259 if (rgb_shift
== alpha_shift
) {
1260 shift
= register_scalar_const(p
, (GLfloat
)(1<<rgb_shift
));
1263 shift
= register_const4f(p
,
1264 (GLfloat
)(1<<rgb_shift
),
1265 (GLfloat
)(1<<rgb_shift
),
1266 (GLfloat
)(1<<rgb_shift
),
1267 (GLfloat
)(1<<alpha_shift
));
1269 return emit_arith( p
, OPCODE_MUL
, dest
, WRITEMASK_XYZW
,
1270 saturate
, out
, shift
, undef
);
1278 * Generate instruction for getting a texture source term.
1280 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
1282 if (is_undef(p
->src_texture
[unit
])) {
1283 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
1284 struct ureg texcoord
;
1285 struct ureg tmp
= get_tex_temp( p
);
1287 if (is_undef(p
->texcoord_tex
[unit
])) {
1288 texcoord
= register_input(p
, FRAG_ATTRIB_TEX0
+unit
);
1291 /* might want to reuse this reg for tex output actually */
1292 texcoord
= p
->texcoord_tex
[unit
];
1295 /* TODO: Use D0_MASK_XY where possible.
1297 if (p
->state
->unit
[unit
].enabled
) {
1298 GLboolean shadow
= GL_FALSE
;
1300 if (p
->state
->unit
[unit
].shadow
) {
1301 p
->program
->Base
.ShadowSamplers
|= 1 << unit
;
1305 p
->src_texture
[unit
] = emit_texld( p
, OPCODE_TXP
,
1306 tmp
, WRITEMASK_XYZW
,
1307 unit
, texTarget
, shadow
,
1310 p
->program
->Base
.SamplersUsed
|= (1 << unit
);
1311 /* This identity mapping should already be in place
1312 * (see _mesa_init_program_struct()) but let's be safe.
1314 p
->program
->Base
.SamplerUnits
[unit
] = unit
;
1317 p
->src_texture
[unit
] = get_zero(p
);
1319 if (p
->state
->unit
[unit
].texture_cyl_wrap
) {
1320 /* set flag which is checked by Mesa->Gallium program translation */
1321 p
->program
->Base
.InputFlags
[0] |= PROG_PARAM_BIT_CYL_WRAP
;
1327 static GLboolean
load_texenv_source( struct texenv_fragment_program
*p
,
1328 GLuint src
, GLuint unit
)
1332 load_texture(p
, unit
);
1343 load_texture(p
, src
- SRC_TEXTURE0
);
1347 /* not a texture src - do nothing */
1356 * Generate instructions for loading all texture source terms.
1359 load_texunit_sources( struct texenv_fragment_program
*p
, GLuint unit
)
1361 const struct state_key
*key
= p
->state
;
1364 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1365 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1368 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1369 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1376 * Generate instructions for loading bump map textures.
1379 load_texunit_bumpmap( struct texenv_fragment_program
*p
, GLuint unit
)
1381 const struct state_key
*key
= p
->state
;
1382 GLuint bumpedUnitNr
= key
->unit
[unit
].OptRGB
[1].Source
- SRC_TEXTURE0
;
1383 struct ureg texcDst
, bumpMapRes
;
1384 struct ureg constdudvcolor
= register_const4f(p
, 0.0, 0.0, 0.0, 1.0);
1385 struct ureg texcSrc
= register_input(p
, FRAG_ATTRIB_TEX0
+ bumpedUnitNr
);
1386 struct ureg rotMat0
= register_param3( p
, STATE_INTERNAL
, STATE_ROT_MATRIX_0
, unit
);
1387 struct ureg rotMat1
= register_param3( p
, STATE_INTERNAL
, STATE_ROT_MATRIX_1
, unit
);
1389 load_texenv_source( p
, unit
+ SRC_TEXTURE0
, unit
);
1391 bumpMapRes
= get_source(p
, key
->unit
[unit
].OptRGB
[0].Source
, unit
);
1392 texcDst
= get_tex_temp( p
);
1393 p
->texcoord_tex
[bumpedUnitNr
] = texcDst
;
1395 /* Apply rot matrix and add coords to be available in next phase.
1396 * dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1)
1397 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1399 emit_arith( p
, OPCODE_MAD
, texcDst
, WRITEMASK_XYZW
, 0,
1400 swizzle1(bumpMapRes
, SWIZZLE_X
), rotMat0
, texcSrc
);
1401 emit_arith( p
, OPCODE_MAD
, texcDst
, WRITEMASK_XYZW
, 0,
1402 swizzle1(bumpMapRes
, SWIZZLE_Y
), rotMat1
, texcDst
);
1404 /* Move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1405 * enough to access this later, should optimize away.
1407 emit_arith( p
, OPCODE_MOV
, bumpMapRes
, WRITEMASK_XYZW
, 0,
1408 constdudvcolor
, undef
, undef
);
1414 * Generate a new fragment program which implements the context's
1415 * current texture env/combine mode.
1418 create_new_program(GLcontext
*ctx
, struct state_key
*key
,
1419 struct gl_fragment_program
*program
)
1421 struct prog_instruction instBuffer
[MAX_INSTRUCTIONS
];
1422 struct texenv_fragment_program p
;
1424 struct ureg cf
, out
;
1427 memset(&p
, 0, sizeof(p
));
1429 p
.program
= program
;
1431 /* During code generation, use locally-allocated instruction buffer,
1432 * then alloc dynamic storage below.
1434 p
.program
->Base
.Instructions
= instBuffer
;
1435 p
.program
->Base
.Target
= GL_FRAGMENT_PROGRAM_ARB
;
1436 p
.program
->Base
.String
= NULL
;
1437 p
.program
->Base
.NumTexIndirections
= 1; /* is this right? */
1438 p
.program
->Base
.NumTexInstructions
= 0;
1439 p
.program
->Base
.NumAluInstructions
= 0;
1440 p
.program
->Base
.NumInstructions
= 0;
1441 p
.program
->Base
.NumTemporaries
= 0;
1442 p
.program
->Base
.NumParameters
= 0;
1443 p
.program
->Base
.NumAttributes
= 0;
1444 p
.program
->Base
.NumAddressRegs
= 0;
1445 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1446 p
.program
->Base
.InputsRead
= 0x0;
1448 if (key
->num_draw_buffers
== 1)
1449 p
.program
->Base
.OutputsWritten
= 1 << FRAG_RESULT_COLOR
;
1451 for (i
= 0; i
< key
->num_draw_buffers
; i
++)
1452 p
.program
->Base
.OutputsWritten
|= (1 << (FRAG_RESULT_DATA0
+ i
));
1455 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1456 p
.src_texture
[unit
] = undef
;
1457 p
.texcoord_tex
[unit
] = undef
;
1460 p
.src_previous
= undef
;
1465 p
.last_tex_stage
= 0;
1466 release_temps(ctx
, &p
);
1468 if (key
->enabled_units
) {
1469 GLboolean needbumpstage
= GL_FALSE
;
1471 /* Zeroth pass - bump map textures first */
1472 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1473 if (key
->unit
[unit
].enabled
&&
1474 key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1475 needbumpstage
= GL_TRUE
;
1476 load_texunit_bumpmap( &p
, unit
);
1479 p
.program
->Base
.NumTexIndirections
++;
1481 /* First pass - to support texture_env_crossbar, first identify
1482 * all referenced texture sources and emit texld instructions
1485 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1486 if (key
->unit
[unit
].enabled
) {
1487 load_texunit_sources( &p
, unit
);
1488 p
.last_tex_stage
= unit
;
1491 /* Second pass - emit combine instructions to build final color:
1493 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1494 if (key
->unit
[unit
].enabled
) {
1495 p
.src_previous
= emit_texenv( &p
, unit
);
1496 reserve_temp(&p
, p
.src_previous
); /* don't re-use this temp reg */
1497 release_temps(ctx
, &p
); /* release all temps */
1501 cf
= get_source( &p
, SRC_PREVIOUS
, 0 );
1503 for (i
= 0; i
< key
->num_draw_buffers
; i
++) {
1504 if (key
->num_draw_buffers
== 1)
1505 out
= make_ureg( PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
);
1507 out
= make_ureg( PROGRAM_OUTPUT
, FRAG_RESULT_DATA0
+ i
);
1510 if (key
->separate_specular
) {
1511 /* Emit specular add.
1513 struct ureg s
= register_input(&p
, FRAG_ATTRIB_COL1
);
1514 emit_arith( &p
, OPCODE_ADD
, out
, WRITEMASK_XYZ
, 0, cf
, s
, undef
);
1515 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_W
, 0, cf
, undef
, undef
);
1517 else if (memcmp(&cf
, &out
, sizeof(cf
)) != 0) {
1518 /* Will wind up in here if no texture enabled or a couple of
1519 * other scenarios (GL_REPLACE for instance).
1521 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_XYZW
, 0, cf
, undef
, undef
);
1526 emit_arith( &p
, OPCODE_END
, undef
, WRITEMASK_XYZW
, 0, undef
, undef
, undef
);
1528 if (key
->fog_enabled
) {
1529 /* Pull fog mode from GLcontext, the value in the state key is
1530 * a reduced value and not what is expected in FogOption
1532 p
.program
->FogOption
= ctx
->Fog
.Mode
;
1533 p
.program
->Base
.InputsRead
|= FRAG_BIT_FOGC
;
1536 p
.program
->FogOption
= GL_NONE
;
1539 if (p
.program
->Base
.NumTexIndirections
> ctx
->Const
.FragmentProgram
.MaxTexIndirections
)
1540 program_error(&p
, "Exceeded max nr indirect texture lookups");
1542 if (p
.program
->Base
.NumTexInstructions
> ctx
->Const
.FragmentProgram
.MaxTexInstructions
)
1543 program_error(&p
, "Exceeded max TEX instructions");
1545 if (p
.program
->Base
.NumAluInstructions
> ctx
->Const
.FragmentProgram
.MaxAluInstructions
)
1546 program_error(&p
, "Exceeded max ALU instructions");
1548 ASSERT(p
.program
->Base
.NumInstructions
<= MAX_INSTRUCTIONS
);
1550 /* Allocate final instruction array */
1551 p
.program
->Base
.Instructions
1552 = _mesa_alloc_instructions(p
.program
->Base
.NumInstructions
);
1553 if (!p
.program
->Base
.Instructions
) {
1554 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1555 "generating tex env program");
1558 _mesa_copy_instructions(p
.program
->Base
.Instructions
, instBuffer
,
1559 p
.program
->Base
.NumInstructions
);
1561 if (p
.program
->FogOption
) {
1562 _mesa_append_fog_code(ctx
, p
.program
);
1563 p
.program
->FogOption
= GL_NONE
;
1567 /* Notify driver the fragment program has (actually) changed.
1569 if (ctx
->Driver
.ProgramStringNotify
) {
1570 GLboolean ok
= ctx
->Driver
.ProgramStringNotify(ctx
,
1571 GL_FRAGMENT_PROGRAM_ARB
,
1573 /* Driver should be able to handle any texenv programs as long as
1574 * the driver correctly reported max number of texture units correctly,
1578 (void) ok
; /* silence unused var warning */
1582 _mesa_print_program(&p
.program
->Base
);
1589 * Return a fragment program which implements the current
1590 * fixed-function texture, fog and color-sum operations.
1592 struct gl_fragment_program
*
1593 _mesa_get_fixed_func_fragment_program(GLcontext
*ctx
)
1595 struct gl_fragment_program
*prog
;
1596 struct state_key key
;
1599 keySize
= make_state_key(ctx
, &key
);
1601 prog
= (struct gl_fragment_program
*)
1602 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1606 prog
= (struct gl_fragment_program
*)
1607 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
1609 create_new_program(ctx
, &key
, prog
);
1611 _mesa_program_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1612 &key
, keySize
, &prog
->Base
);