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 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;
102 /* NOTE: This array of structs must be last! (see "keySize" below) */
105 GLuint source_index
:3; /**< TEXTURE_x_INDEX */
107 GLuint ScaleShiftRGB
:2;
108 GLuint ScaleShiftA
:2;
110 GLuint NumArgsRGB
:3; /**< up to MAX_COMBINER_TERMS */
111 GLuint ModeRGB
:5; /**< MODE_x */
113 GLuint NumArgsA
:3; /**< up to MAX_COMBINER_TERMS */
114 GLuint ModeA
:5; /**< MODE_x */
116 struct mode_opt OptRGB
[MAX_COMBINER_TERMS
];
117 struct mode_opt OptA
[MAX_COMBINER_TERMS
];
118 } unit
[MAX_TEXTURE_UNITS
];
124 #define FOG_UNKNOWN 3
126 static GLuint
translate_fog_mode( GLenum mode
)
129 case GL_LINEAR
: return FOG_LINEAR
;
130 case GL_EXP
: return FOG_EXP
;
131 case GL_EXP2
: return FOG_EXP2
;
132 default: return FOG_UNKNOWN
;
136 #define OPR_SRC_COLOR 0
137 #define OPR_ONE_MINUS_SRC_COLOR 1
138 #define OPR_SRC_ALPHA 2
139 #define OPR_ONE_MINUS_SRC_ALPHA 3
142 #define OPR_UNKNOWN 7
144 static GLuint
translate_operand( GLenum operand
)
147 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
148 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
149 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
150 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
151 case GL_ZERO
: return OPR_ZERO
;
152 case GL_ONE
: return OPR_ONE
;
159 #define SRC_TEXTURE 0
160 #define SRC_TEXTURE0 1
161 #define SRC_TEXTURE1 2
162 #define SRC_TEXTURE2 3
163 #define SRC_TEXTURE3 4
164 #define SRC_TEXTURE4 5
165 #define SRC_TEXTURE5 6
166 #define SRC_TEXTURE6 7
167 #define SRC_TEXTURE7 8
168 #define SRC_CONSTANT 9
169 #define SRC_PRIMARY_COLOR 10
170 #define SRC_PREVIOUS 11
172 #define SRC_UNKNOWN 15
174 static GLuint
translate_source( GLenum src
)
177 case GL_TEXTURE
: return SRC_TEXTURE
;
185 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
186 case GL_CONSTANT
: return SRC_CONSTANT
;
187 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
188 case GL_PREVIOUS
: return SRC_PREVIOUS
;
197 #define MODE_REPLACE 0 /* r = a0 */
198 #define MODE_MODULATE 1 /* r = a0 * a1 */
199 #define MODE_ADD 2 /* r = a0 + a1 */
200 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
201 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
202 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
203 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
204 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
205 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
206 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
207 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
208 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
209 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
210 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
211 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
212 #define MODE_BUMP_ENVMAP_ATI 15 /* special */
213 #define MODE_UNKNOWN 16
216 * Translate GL combiner state into a MODE_x value
218 static GLuint
translate_mode( GLenum envMode
, GLenum mode
)
221 case GL_REPLACE
: return MODE_REPLACE
;
222 case GL_MODULATE
: return MODE_MODULATE
;
224 if (envMode
== GL_COMBINE4_NV
)
225 return MODE_ADD_PRODUCTS
;
229 if (envMode
== GL_COMBINE4_NV
)
230 return MODE_ADD_PRODUCTS_SIGNED
;
232 return MODE_ADD_SIGNED
;
233 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
234 case GL_SUBTRACT
: return MODE_SUBTRACT
;
235 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
236 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
237 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
238 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
239 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
240 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
241 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
242 case GL_BUMP_ENVMAP_ATI
: return MODE_BUMP_ENVMAP_ATI
;
251 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
253 static GLuint
translate_tex_src_bit( GLbitfield bit
)
256 return _mesa_ffs(bit
) - 1;
260 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
261 #define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
264 * Identify all possible varying inputs. The fragment program will
265 * never reference non-varying inputs, but will track them via state
268 * This function figures out all the inputs that the fragment program
269 * has access to. The bitmask is later reduced to just those which
270 * are actually referenced.
272 static GLbitfield
get_fp_input_mask( GLcontext
*ctx
)
275 const GLboolean vertexShader
= (ctx
->Shader
.CurrentProgram
&&
276 ctx
->Shader
.CurrentProgram
->LinkStatus
&&
277 ctx
->Shader
.CurrentProgram
->VertexProgram
);
278 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
279 GLbitfield fp_inputs
= 0x0;
281 if (ctx
->VertexProgram
._Overriden
) {
282 /* Somebody's messing with the vertex program and we don't have
283 * a clue what's happening. Assume that it could be producing
284 * all possible outputs.
288 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
289 /* _NEW_RENDERMODE */
290 fp_inputs
= (FRAG_BIT_COL0
| FRAG_BIT_TEX0
);
292 else if (!(vertexProgram
|| vertexShader
) ||
293 !ctx
->VertexProgram
._Current
) {
294 /* Fixed function vertex logic */
296 GLbitfield varying_inputs
= ctx
->varying_vp_inputs
;
298 /* These get generated in the setup routine regardless of the
302 if (ctx
->Point
.PointSprite
)
303 varying_inputs
|= FRAG_BITS_TEX_ANY
;
305 /* First look at what values may be computed by the generated
309 if (ctx
->Light
.Enabled
) {
310 fp_inputs
|= FRAG_BIT_COL0
;
312 if (texenv_doing_secondary_color(ctx
))
313 fp_inputs
|= FRAG_BIT_COL1
;
317 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
318 ctx
->Texture
._TexMatEnabled
) << FRAG_ATTRIB_TEX0
;
320 /* Then look at what might be varying as a result of enabled
323 if (varying_inputs
& VERT_BIT_COLOR0
)
324 fp_inputs
|= FRAG_BIT_COL0
;
325 if (varying_inputs
& VERT_BIT_COLOR1
)
326 fp_inputs
|= FRAG_BIT_COL1
;
328 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
329 << FRAG_ATTRIB_TEX0
);
333 /* calculate from vp->outputs */
334 struct gl_vertex_program
*vprog
;
335 GLbitfield vp_outputs
;
337 /* Choose GLSL vertex shader over ARB vertex program. Need this
338 * since vertex shader state validation comes after fragment state
339 * validation (see additional comments in state.c).
342 vprog
= ctx
->Shader
.CurrentProgram
->VertexProgram
;
344 vprog
= ctx
->VertexProgram
.Current
;
346 vp_outputs
= vprog
->Base
.OutputsWritten
;
348 /* These get generated in the setup routine regardless of the
352 if (ctx
->Point
.PointSprite
)
353 vp_outputs
|= FRAG_BITS_TEX_ANY
;
355 if (vp_outputs
& (1 << VERT_RESULT_COL0
))
356 fp_inputs
|= FRAG_BIT_COL0
;
357 if (vp_outputs
& (1 << VERT_RESULT_COL1
))
358 fp_inputs
|= FRAG_BIT_COL1
;
360 fp_inputs
|= (((vp_outputs
& VERT_RESULT_TEX_ANY
) >> VERT_RESULT_TEX0
)
361 << FRAG_ATTRIB_TEX0
);
369 * Examine current texture environment state and generate a unique
370 * key to identify it.
372 static GLuint
make_state_key( GLcontext
*ctx
, struct state_key
*key
)
375 GLbitfield inputs_referenced
= FRAG_BIT_COL0
;
376 const GLbitfield inputs_available
= get_fp_input_mask( ctx
);
379 memset(key
, 0, sizeof(*key
));
382 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
383 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
384 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
385 const struct gl_tex_env_combine_state
*comb
= texUnit
->_CurrentCombine
;
388 if (!texUnit
->_ReallyEnabled
|| !texUnit
->Enabled
)
391 format
= texObj
->Image
[0][texObj
->BaseLevel
]->_BaseFormat
;
393 key
->unit
[i
].enabled
= 1;
394 key
->enabled_units
|= (1<<i
);
395 key
->nr_enabled_units
= i
+ 1;
396 inputs_referenced
|= FRAG_BIT_TEX(i
);
398 key
->unit
[i
].source_index
=
399 translate_tex_src_bit(texUnit
->_ReallyEnabled
);
401 key
->unit
[i
].shadow
= ((texObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
402 ((format
== GL_DEPTH_COMPONENT
) ||
403 (format
== GL_DEPTH_STENCIL_EXT
)));
405 key
->unit
[i
].NumArgsRGB
= comb
->_NumArgsRGB
;
406 key
->unit
[i
].NumArgsA
= comb
->_NumArgsA
;
408 key
->unit
[i
].ModeRGB
=
409 translate_mode(texUnit
->EnvMode
, comb
->ModeRGB
);
411 translate_mode(texUnit
->EnvMode
, comb
->ModeA
);
413 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
414 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
416 for (j
= 0; j
< MAX_COMBINER_TERMS
; j
++) {
417 key
->unit
[i
].OptRGB
[j
].Operand
= translate_operand(comb
->OperandRGB
[j
]);
418 key
->unit
[i
].OptA
[j
].Operand
= translate_operand(comb
->OperandA
[j
]);
419 key
->unit
[i
].OptRGB
[j
].Source
= translate_source(comb
->SourceRGB
[j
]);
420 key
->unit
[i
].OptA
[j
].Source
= translate_source(comb
->SourceA
[j
]);
423 if (key
->unit
[i
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
424 /* requires some special translation */
425 key
->unit
[i
].NumArgsRGB
= 2;
426 key
->unit
[i
].ScaleShiftRGB
= 0;
427 key
->unit
[i
].OptRGB
[0].Operand
= OPR_SRC_COLOR
;
428 key
->unit
[i
].OptRGB
[0].Source
= SRC_TEXTURE
;
429 key
->unit
[i
].OptRGB
[1].Operand
= OPR_SRC_COLOR
;
430 key
->unit
[i
].OptRGB
[1].Source
= texUnit
->BumpTarget
- GL_TEXTURE0
+ SRC_TEXTURE0
;
434 /* _NEW_LIGHT | _NEW_FOG */
435 if (texenv_doing_secondary_color(ctx
)) {
436 key
->separate_specular
= 1;
437 inputs_referenced
|= FRAG_BIT_COL1
;
441 if (ctx
->Fog
.Enabled
) {
442 key
->fog_enabled
= 1;
443 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
444 inputs_referenced
|= FRAG_BIT_FOGC
; /* maybe */
447 key
->inputs_available
= (inputs_available
& inputs_referenced
);
449 /* compute size of state key, ignoring unused texture units */
450 keySize
= sizeof(*key
) - sizeof(key
->unit
)
451 + key
->nr_enabled_units
* sizeof(key
->unit
[0]);
458 * Use uregs to represent registers internally, translate to Mesa's
459 * expected formats on emit.
461 * NOTE: These are passed by value extensively in this file rather
462 * than as usual by pointer reference. If this disturbs you, try
463 * remembering they are just 32bits in size.
465 * GCC is smart enough to deal with these dword-sized structures in
466 * much the same way as if I had defined them as dwords and was using
467 * macros to access and set the fields. This is much nicer and easier
478 static const struct ureg undef
= {
487 /** State used to build the fragment program:
489 struct texenv_fragment_program
{
490 struct gl_fragment_program
*program
;
491 struct state_key
*state
;
493 GLbitfield alu_temps
; /**< Track texture indirections, see spec. */
494 GLbitfield temps_output
; /**< Track texture indirections, see spec. */
495 GLbitfield temp_in_use
; /**< Tracks temporary regs which are in use. */
498 struct ureg src_texture
[MAX_TEXTURE_COORD_UNITS
];
499 /* Reg containing each texture unit's sampled texture color,
503 struct ureg texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
504 /* Reg containing texcoord for a texture unit,
505 * needed for bump mapping, else undef.
508 struct ureg src_previous
; /**< Reg containing color from previous
509 * stage. May need to be decl'd.
512 GLuint last_tex_stage
; /**< Number of last enabled texture unit */
521 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
527 reg
.swz
= SWIZZLE_NOOP
;
532 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
534 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
537 GET_SWZ(reg
.swz
, w
));
542 static struct ureg
swizzle1( struct ureg reg
, int x
)
544 return swizzle(reg
, x
, x
, x
, x
);
547 static struct ureg
negate( struct ureg reg
)
553 static GLboolean
is_undef( struct ureg reg
)
555 return reg
.file
== PROGRAM_UNDEFINED
;
559 static struct ureg
get_temp( struct texenv_fragment_program
*p
)
563 /* First try and reuse temps which have been used already:
565 bit
= _mesa_ffs( ~p
->temp_in_use
& p
->alu_temps
);
567 /* Then any unused temporary:
570 bit
= _mesa_ffs( ~p
->temp_in_use
);
573 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
577 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
578 p
->program
->Base
.NumTemporaries
= bit
;
580 p
->temp_in_use
|= 1<<(bit
-1);
581 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
584 static struct ureg
get_tex_temp( struct texenv_fragment_program
*p
)
588 /* First try to find available temp not previously used (to avoid
589 * starting a new texture indirection). According to the spec, the
590 * ~p->temps_output isn't necessary, but will keep it there for
593 bit
= _mesa_ffs( ~p
->temp_in_use
& ~p
->alu_temps
& ~p
->temps_output
);
595 /* Then any unused temporary:
598 bit
= _mesa_ffs( ~p
->temp_in_use
);
601 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
605 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
606 p
->program
->Base
.NumTemporaries
= bit
;
608 p
->temp_in_use
|= 1<<(bit
-1);
609 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
613 /** Mark a temp reg as being no longer allocatable. */
614 static void reserve_temp( struct texenv_fragment_program
*p
, struct ureg r
)
616 if (r
.file
== PROGRAM_TEMPORARY
)
617 p
->temps_output
|= (1 << r
.idx
);
621 static void release_temps(GLcontext
*ctx
, struct texenv_fragment_program
*p
)
623 GLuint max_temp
= ctx
->Const
.FragmentProgram
.MaxTemps
;
625 /* KW: To support tex_env_crossbar, don't release the registers in
628 if (max_temp
>= sizeof(int) * 8)
629 p
->temp_in_use
= p
->temps_output
;
631 p
->temp_in_use
= ~((1<<max_temp
)-1) | p
->temps_output
;
635 static struct ureg
register_param5( struct texenv_fragment_program
*p
,
642 gl_state_index tokens
[STATE_LENGTH
];
649 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
650 return make_ureg(PROGRAM_STATE_VAR
, idx
);
654 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
655 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
656 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
657 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
659 static GLuint
frag_to_vert_attrib( GLuint attrib
)
662 case FRAG_ATTRIB_COL0
: return VERT_ATTRIB_COLOR0
;
663 case FRAG_ATTRIB_COL1
: return VERT_ATTRIB_COLOR1
;
665 assert(attrib
>= FRAG_ATTRIB_TEX0
);
666 assert(attrib
<= FRAG_ATTRIB_TEX7
);
667 return attrib
- FRAG_ATTRIB_TEX0
+ VERT_ATTRIB_TEX0
;
672 static struct ureg
register_input( struct texenv_fragment_program
*p
, GLuint input
)
674 if (p
->state
->inputs_available
& (1<<input
)) {
675 p
->program
->Base
.InputsRead
|= (1 << input
);
676 return make_ureg(PROGRAM_INPUT
, input
);
679 GLuint idx
= frag_to_vert_attrib( input
);
680 return register_param3( p
, STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, idx
);
685 static void emit_arg( struct prog_src_register
*reg
,
688 reg
->File
= ureg
.file
;
689 reg
->Index
= ureg
.idx
;
690 reg
->Swizzle
= ureg
.swz
;
691 reg
->Negate
= ureg
.negatebase
? NEGATE_XYZW
: NEGATE_NONE
;
695 static void emit_dst( struct prog_dst_register
*dst
,
696 struct ureg ureg
, GLuint mask
)
698 dst
->File
= ureg
.file
;
699 dst
->Index
= ureg
.idx
;
700 dst
->WriteMask
= mask
;
701 dst
->CondMask
= COND_TR
; /* always pass cond test */
702 dst
->CondSwizzle
= SWIZZLE_NOOP
;
705 static struct prog_instruction
*
706 emit_op(struct texenv_fragment_program
*p
,
715 const GLuint nr
= p
->program
->Base
.NumInstructions
++;
716 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
718 assert(nr
< MAX_INSTRUCTIONS
);
720 _mesa_init_instructions(inst
, 1);
723 emit_arg( &inst
->SrcReg
[0], src0
);
724 emit_arg( &inst
->SrcReg
[1], src1
);
725 emit_arg( &inst
->SrcReg
[2], src2
);
727 inst
->SaturateMode
= saturate
? SATURATE_ZERO_ONE
: SATURATE_OFF
;
729 emit_dst( &inst
->DstReg
, dest
, mask
);
732 /* Accounting for indirection tracking:
734 if (dest
.file
== PROGRAM_TEMPORARY
)
735 p
->temps_output
|= 1 << dest
.idx
;
742 static struct ureg
emit_arith( struct texenv_fragment_program
*p
,
751 emit_op(p
, op
, dest
, mask
, saturate
, src0
, src1
, src2
);
753 /* Accounting for indirection tracking:
755 if (src0
.file
== PROGRAM_TEMPORARY
)
756 p
->alu_temps
|= 1 << src0
.idx
;
758 if (!is_undef(src1
) && src1
.file
== PROGRAM_TEMPORARY
)
759 p
->alu_temps
|= 1 << src1
.idx
;
761 if (!is_undef(src2
) && src2
.file
== PROGRAM_TEMPORARY
)
762 p
->alu_temps
|= 1 << src2
.idx
;
764 if (dest
.file
== PROGRAM_TEMPORARY
)
765 p
->alu_temps
|= 1 << dest
.idx
;
767 p
->program
->Base
.NumAluInstructions
++;
771 static struct ureg
emit_texld( struct texenv_fragment_program
*p
,
780 struct prog_instruction
*inst
= emit_op( p
, op
,
782 GL_FALSE
, /* don't saturate? */
787 inst
->TexSrcTarget
= tex_idx
;
788 inst
->TexSrcUnit
= tex_unit
;
789 inst
->TexShadow
= tex_shadow
;
791 p
->program
->Base
.NumTexInstructions
++;
793 /* Accounting for indirection tracking:
795 reserve_temp(p
, dest
);
798 /* Is this a texture indirection?
800 if ((coord
.file
== PROGRAM_TEMPORARY
&&
801 (p
->temps_output
& (1<<coord
.idx
))) ||
802 (dest
.file
== PROGRAM_TEMPORARY
&&
803 (p
->alu_temps
& (1<<dest
.idx
)))) {
804 p
->program
->Base
.NumTexIndirections
++;
805 p
->temps_output
= 1<<coord
.idx
;
807 assert(0); /* KW: texture env crossbar */
815 static struct ureg
register_const4f( struct texenv_fragment_program
*p
,
828 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
830 r
= make_ureg(PROGRAM_CONSTANT
, idx
);
835 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
836 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
837 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
838 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
841 static struct ureg
get_one( struct texenv_fragment_program
*p
)
843 if (is_undef(p
->one
))
844 p
->one
= register_scalar_const(p
, 1.0);
848 static struct ureg
get_half( struct texenv_fragment_program
*p
)
850 if (is_undef(p
->half
))
851 p
->half
= register_scalar_const(p
, 0.5);
855 static struct ureg
get_zero( struct texenv_fragment_program
*p
)
857 if (is_undef(p
->zero
))
858 p
->zero
= register_scalar_const(p
, 0.0);
863 static void program_error( struct texenv_fragment_program
*p
, const char *msg
)
865 _mesa_problem(NULL
, msg
);
869 static struct ureg
get_source( struct texenv_fragment_program
*p
,
870 GLuint src
, GLuint unit
)
874 assert(!is_undef(p
->src_texture
[unit
]));
875 return p
->src_texture
[unit
];
885 assert(!is_undef(p
->src_texture
[src
- SRC_TEXTURE0
]));
886 return p
->src_texture
[src
- SRC_TEXTURE0
];
889 return register_param2(p
, STATE_TEXENV_COLOR
, unit
);
891 case SRC_PRIMARY_COLOR
:
892 return register_input(p
, FRAG_ATTRIB_COL0
);
898 if (is_undef(p
->src_previous
))
899 return register_input(p
, FRAG_ATTRIB_COL0
);
901 return p
->src_previous
;
909 static struct ureg
emit_combine_source( struct texenv_fragment_program
*p
,
915 struct ureg arg
, src
, one
;
917 src
= get_source(p
, source
, unit
);
920 case OPR_ONE_MINUS_SRC_COLOR
:
922 * Emit tmp = 1.0 - arg.xyzw
926 return emit_arith( p
, OPCODE_SUB
, arg
, mask
, 0, one
, src
, undef
);
929 if (mask
== WRITEMASK_W
)
932 return swizzle1( src
, SWIZZLE_W
);
933 case OPR_ONE_MINUS_SRC_ALPHA
:
935 * Emit tmp = 1.0 - arg.wwww
939 return emit_arith(p
, OPCODE_SUB
, arg
, mask
, 0,
940 one
, swizzle1(src
, SWIZZLE_W
), undef
);
954 * Check if the RGB and Alpha sources and operands match for the given
955 * texture unit's combinder state. When the RGB and A sources and
956 * operands match, we can emit fewer instructions.
958 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
960 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
962 for (i
= 0; i
< numArgs
; i
++) {
963 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
966 switch (key
->unit
[unit
].OptA
[i
].Operand
) {
968 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
976 case OPR_ONE_MINUS_SRC_ALPHA
:
977 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
978 case OPR_ONE_MINUS_SRC_COLOR
:
979 case OPR_ONE_MINUS_SRC_ALPHA
:
986 return GL_FALSE
; /* impossible */
993 static struct ureg
emit_combine( struct texenv_fragment_program
*p
,
1000 const struct mode_opt
*opt
)
1002 struct ureg src
[MAX_COMBINER_TERMS
];
1003 struct ureg tmp
, half
;
1006 assert(nr
<= MAX_COMBINER_TERMS
);
1008 tmp
= undef
; /* silence warning (bug 5318) */
1010 for (i
= 0; i
< nr
; i
++)
1011 src
[i
] = emit_combine_source( p
, mask
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
1015 if (mask
== WRITEMASK_XYZW
&& !saturate
)
1018 return emit_arith( p
, OPCODE_MOV
, dest
, mask
, saturate
, src
[0], undef
, undef
);
1020 return emit_arith( p
, OPCODE_MUL
, dest
, mask
, saturate
,
1021 src
[0], src
[1], undef
);
1023 return emit_arith( p
, OPCODE_ADD
, dest
, mask
, saturate
,
1024 src
[0], src
[1], undef
);
1025 case MODE_ADD_SIGNED
:
1026 /* tmp = arg0 + arg1
1030 tmp
= get_temp( p
);
1031 emit_arith( p
, OPCODE_ADD
, tmp
, mask
, 0, src
[0], src
[1], undef
);
1032 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp
, half
, undef
);
1034 case MODE_INTERPOLATE
:
1035 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1037 return emit_arith( p
, OPCODE_LRP
, dest
, mask
, saturate
, src
[2], src
[0], src
[1] );
1040 return emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, src
[0], src
[1], undef
);
1042 case MODE_DOT3_RGBA
:
1043 case MODE_DOT3_RGBA_EXT
:
1044 case MODE_DOT3_RGB_EXT
:
1045 case MODE_DOT3_RGB
: {
1046 struct ureg tmp0
= get_temp( p
);
1047 struct ureg tmp1
= get_temp( p
);
1048 struct ureg neg1
= register_scalar_const(p
, -1);
1049 struct ureg two
= register_scalar_const(p
, 2);
1051 /* tmp0 = 2*src0 - 1
1054 * dst = tmp0 dot3 tmp1
1056 emit_arith( p
, OPCODE_MAD
, tmp0
, WRITEMASK_XYZW
, 0,
1059 if (_mesa_memcmp(&src
[0], &src
[1], sizeof(struct ureg
)) == 0)
1062 emit_arith( p
, OPCODE_MAD
, tmp1
, WRITEMASK_XYZW
, 0,
1064 emit_arith( p
, OPCODE_DP3
, dest
, mask
, saturate
, tmp0
, tmp1
, undef
);
1067 case MODE_MODULATE_ADD_ATI
:
1068 /* Arg0 * Arg2 + Arg1 */
1069 return emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
,
1070 src
[0], src
[2], src
[1] );
1071 case MODE_MODULATE_SIGNED_ADD_ATI
: {
1072 /* Arg0 * Arg2 + Arg1 - 0.5 */
1073 struct ureg tmp0
= get_temp(p
);
1075 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[0], src
[2], src
[1] );
1076 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
1079 case MODE_MODULATE_SUBTRACT_ATI
:
1080 /* Arg0 * Arg2 - Arg1 */
1081 emit_arith( p
, OPCODE_MAD
, dest
, mask
, 0, src
[0], src
[2], negate(src
[1]) );
1083 case MODE_ADD_PRODUCTS
:
1084 /* Arg0 * Arg1 + Arg2 * Arg3 */
1086 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
, dest
, mask
, saturate
, src
[2], src
[3], tmp0
);
1091 case MODE_ADD_PRODUCTS_SIGNED
:
1092 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1094 struct ureg tmp0
= get_temp(p
);
1096 emit_arith( p
, OPCODE_MUL
, tmp0
, mask
, 0, src
[0], src
[1], undef
);
1097 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[2], src
[3], tmp0
);
1098 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
1101 case MODE_BUMP_ENVMAP_ATI
:
1102 /* special - not handled here */
1113 * Generate instructions for one texture unit's env/combiner mode.
1116 emit_texenv(struct texenv_fragment_program
*p
, GLuint unit
)
1118 const struct state_key
*key
= p
->state
;
1120 GLuint rgb_shift
, alpha_shift
;
1121 struct ureg out
, dest
;
1123 if (!key
->unit
[unit
].enabled
) {
1124 return get_source(p
, SRC_PREVIOUS
, 0);
1126 if (key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1127 /* this isn't really a env stage delivering a color and handled elsewhere */
1128 return get_source(p
, SRC_PREVIOUS
, 0);
1131 switch (key
->unit
[unit
].ModeRGB
) {
1132 case MODE_DOT3_RGB_EXT
:
1133 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
1136 case MODE_DOT3_RGBA_EXT
:
1141 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
1142 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
1146 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1147 * We don't want to clamp twice.
1149 saturate
= !(rgb_shift
|| alpha_shift
);
1151 /* If this is the very last calculation, emit direct to output reg:
1153 if (key
->separate_specular
||
1154 unit
!= p
->last_tex_stage
||
1157 dest
= get_temp( p
);
1159 dest
= make_ureg(PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
);
1161 /* Emit the RGB and A combine ops
1163 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
1164 args_match(key
, unit
)) {
1165 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
1167 key
->unit
[unit
].NumArgsRGB
,
1168 key
->unit
[unit
].ModeRGB
,
1169 key
->unit
[unit
].OptRGB
);
1171 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
1172 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
1173 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
1175 key
->unit
[unit
].NumArgsRGB
,
1176 key
->unit
[unit
].ModeRGB
,
1177 key
->unit
[unit
].OptRGB
);
1180 /* Need to do something to stop from re-emitting identical
1181 * argument calculations here:
1183 out
= emit_combine( p
, dest
, WRITEMASK_XYZ
, saturate
,
1185 key
->unit
[unit
].NumArgsRGB
,
1186 key
->unit
[unit
].ModeRGB
,
1187 key
->unit
[unit
].OptRGB
);
1188 out
= emit_combine( p
, dest
, WRITEMASK_W
, saturate
,
1190 key
->unit
[unit
].NumArgsA
,
1191 key
->unit
[unit
].ModeA
,
1192 key
->unit
[unit
].OptA
);
1195 /* Deal with the final shift:
1197 if (alpha_shift
|| rgb_shift
) {
1200 saturate
= GL_TRUE
; /* always saturate at this point */
1202 if (rgb_shift
== alpha_shift
) {
1203 shift
= register_scalar_const(p
, (GLfloat
)(1<<rgb_shift
));
1206 shift
= register_const4f(p
,
1207 (GLfloat
)(1<<rgb_shift
),
1208 (GLfloat
)(1<<rgb_shift
),
1209 (GLfloat
)(1<<rgb_shift
),
1210 (GLfloat
)(1<<alpha_shift
));
1212 return emit_arith( p
, OPCODE_MUL
, dest
, WRITEMASK_XYZW
,
1213 saturate
, out
, shift
, undef
);
1221 * Generate instruction for getting a texture source term.
1223 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
1225 if (is_undef(p
->src_texture
[unit
])) {
1226 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
1227 struct ureg texcoord
;
1228 struct ureg tmp
= get_tex_temp( p
);
1230 if (is_undef(p
->texcoord_tex
[unit
])) {
1231 texcoord
= register_input(p
, FRAG_ATTRIB_TEX0
+unit
);
1234 /* might want to reuse this reg for tex output actually */
1235 texcoord
= p
->texcoord_tex
[unit
];
1238 /* TODO: Use D0_MASK_XY where possible.
1240 if (p
->state
->unit
[unit
].enabled
) {
1241 GLboolean shadow
= GL_FALSE
;
1243 if (p
->state
->unit
[unit
].shadow
) {
1244 p
->program
->Base
.ShadowSamplers
|= 1 << unit
;
1248 p
->src_texture
[unit
] = emit_texld( p
, OPCODE_TXP
,
1249 tmp
, WRITEMASK_XYZW
,
1250 unit
, texTarget
, shadow
,
1253 p
->program
->Base
.SamplersUsed
|= (1 << unit
);
1254 /* This identity mapping should already be in place
1255 * (see _mesa_init_program_struct()) but let's be safe.
1257 p
->program
->Base
.SamplerUnits
[unit
] = unit
;
1260 p
->src_texture
[unit
] = get_zero(p
);
1264 static GLboolean
load_texenv_source( struct texenv_fragment_program
*p
,
1265 GLuint src
, GLuint unit
)
1269 load_texture(p
, unit
);
1280 load_texture(p
, src
- SRC_TEXTURE0
);
1284 /* not a texture src - do nothing */
1293 * Generate instructions for loading all texture source terms.
1296 load_texunit_sources( struct texenv_fragment_program
*p
, GLuint unit
)
1298 const struct state_key
*key
= p
->state
;
1301 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1302 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1305 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1306 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1313 * Generate instructions for loading bump map textures.
1316 load_texunit_bumpmap( struct texenv_fragment_program
*p
, GLuint unit
)
1318 const struct state_key
*key
= p
->state
;
1319 GLuint bumpedUnitNr
= key
->unit
[unit
].OptRGB
[1].Source
- SRC_TEXTURE0
;
1320 struct ureg texcDst
, bumpMapRes
;
1321 struct ureg constdudvcolor
= register_const4f(p
, 0.0, 0.0, 0.0, 1.0);
1322 struct ureg texcSrc
= register_input(p
, FRAG_ATTRIB_TEX0
+ bumpedUnitNr
);
1323 struct ureg rotMat0
= register_param3( p
, STATE_INTERNAL
, STATE_ROT_MATRIX_0
, unit
);
1324 struct ureg rotMat1
= register_param3( p
, STATE_INTERNAL
, STATE_ROT_MATRIX_1
, unit
);
1326 load_texenv_source( p
, unit
+ SRC_TEXTURE0
, unit
);
1328 bumpMapRes
= get_source(p
, key
->unit
[unit
].OptRGB
[0].Source
, unit
);
1329 texcDst
= get_tex_temp( p
);
1330 p
->texcoord_tex
[bumpedUnitNr
] = texcDst
;
1332 /* Apply rot matrix and add coords to be available in next phase.
1333 * dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1)
1334 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1336 emit_arith( p
, OPCODE_MAD
, texcDst
, WRITEMASK_XYZW
, 0,
1337 swizzle1(bumpMapRes
, SWIZZLE_X
), rotMat0
, texcSrc
);
1338 emit_arith( p
, OPCODE_MAD
, texcDst
, WRITEMASK_XYZW
, 0,
1339 swizzle1(bumpMapRes
, SWIZZLE_Y
), rotMat1
, texcDst
);
1341 /* Move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1342 * enough to access this later, should optimize away.
1344 emit_arith( p
, OPCODE_MOV
, bumpMapRes
, WRITEMASK_XYZW
, 0,
1345 constdudvcolor
, undef
, undef
);
1351 * Generate a new fragment program which implements the context's
1352 * current texture env/combine mode.
1355 create_new_program(GLcontext
*ctx
, struct state_key
*key
,
1356 struct gl_fragment_program
*program
)
1358 struct prog_instruction instBuffer
[MAX_INSTRUCTIONS
];
1359 struct texenv_fragment_program p
;
1361 struct ureg cf
, out
;
1363 _mesa_memset(&p
, 0, sizeof(p
));
1365 p
.program
= program
;
1367 /* During code generation, use locally-allocated instruction buffer,
1368 * then alloc dynamic storage below.
1370 p
.program
->Base
.Instructions
= instBuffer
;
1371 p
.program
->Base
.Target
= GL_FRAGMENT_PROGRAM_ARB
;
1372 p
.program
->Base
.String
= NULL
;
1373 p
.program
->Base
.NumTexIndirections
= 1; /* is this right? */
1374 p
.program
->Base
.NumTexInstructions
= 0;
1375 p
.program
->Base
.NumAluInstructions
= 0;
1376 p
.program
->Base
.NumInstructions
= 0;
1377 p
.program
->Base
.NumTemporaries
= 0;
1378 p
.program
->Base
.NumParameters
= 0;
1379 p
.program
->Base
.NumAttributes
= 0;
1380 p
.program
->Base
.NumAddressRegs
= 0;
1381 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1382 p
.program
->Base
.InputsRead
= 0x0;
1383 p
.program
->Base
.OutputsWritten
= 1 << FRAG_RESULT_COLOR
;
1385 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1386 p
.src_texture
[unit
] = undef
;
1387 p
.texcoord_tex
[unit
] = undef
;
1390 p
.src_previous
= undef
;
1395 p
.last_tex_stage
= 0;
1396 release_temps(ctx
, &p
);
1398 if (key
->enabled_units
) {
1399 GLboolean needbumpstage
= GL_FALSE
;
1401 /* Zeroth pass - bump map textures first */
1402 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1403 if (key
->unit
[unit
].enabled
&&
1404 key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1405 needbumpstage
= GL_TRUE
;
1406 load_texunit_bumpmap( &p
, unit
);
1409 p
.program
->Base
.NumTexIndirections
++;
1411 /* First pass - to support texture_env_crossbar, first identify
1412 * all referenced texture sources and emit texld instructions
1415 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1416 if (key
->unit
[unit
].enabled
) {
1417 load_texunit_sources( &p
, unit
);
1418 p
.last_tex_stage
= unit
;
1421 /* Second pass - emit combine instructions to build final color:
1423 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1424 if (key
->unit
[unit
].enabled
) {
1425 p
.src_previous
= emit_texenv( &p
, unit
);
1426 reserve_temp(&p
, p
.src_previous
); /* don't re-use this temp reg */
1427 release_temps(ctx
, &p
); /* release all temps */
1431 cf
= get_source( &p
, SRC_PREVIOUS
, 0 );
1432 out
= make_ureg( PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
);
1434 if (key
->separate_specular
) {
1435 /* Emit specular add.
1437 struct ureg s
= register_input(&p
, FRAG_ATTRIB_COL1
);
1438 emit_arith( &p
, OPCODE_ADD
, out
, WRITEMASK_XYZ
, 0, cf
, s
, undef
);
1439 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_W
, 0, cf
, undef
, undef
);
1441 else if (_mesa_memcmp(&cf
, &out
, sizeof(cf
)) != 0) {
1442 /* Will wind up in here if no texture enabled or a couple of
1443 * other scenarios (GL_REPLACE for instance).
1445 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_XYZW
, 0, cf
, undef
, undef
);
1450 emit_arith( &p
, OPCODE_END
, undef
, WRITEMASK_XYZW
, 0, undef
, undef
, undef
);
1452 if (key
->fog_enabled
) {
1453 /* Pull fog mode from GLcontext, the value in the state key is
1454 * a reduced value and not what is expected in FogOption
1456 p
.program
->FogOption
= ctx
->Fog
.Mode
;
1457 p
.program
->Base
.InputsRead
|= FRAG_BIT_FOGC
;
1460 p
.program
->FogOption
= GL_NONE
;
1463 if (p
.program
->Base
.NumTexIndirections
> ctx
->Const
.FragmentProgram
.MaxTexIndirections
)
1464 program_error(&p
, "Exceeded max nr indirect texture lookups");
1466 if (p
.program
->Base
.NumTexInstructions
> ctx
->Const
.FragmentProgram
.MaxTexInstructions
)
1467 program_error(&p
, "Exceeded max TEX instructions");
1469 if (p
.program
->Base
.NumAluInstructions
> ctx
->Const
.FragmentProgram
.MaxAluInstructions
)
1470 program_error(&p
, "Exceeded max ALU instructions");
1472 ASSERT(p
.program
->Base
.NumInstructions
<= MAX_INSTRUCTIONS
);
1474 /* Allocate final instruction array */
1475 p
.program
->Base
.Instructions
1476 = _mesa_alloc_instructions(p
.program
->Base
.NumInstructions
);
1477 if (!p
.program
->Base
.Instructions
) {
1478 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1479 "generating tex env program");
1482 _mesa_copy_instructions(p
.program
->Base
.Instructions
, instBuffer
,
1483 p
.program
->Base
.NumInstructions
);
1485 if (p
.program
->FogOption
) {
1486 _mesa_append_fog_code(ctx
, p
.program
);
1487 p
.program
->FogOption
= GL_NONE
;
1491 /* Notify driver the fragment program has (actually) changed.
1493 if (ctx
->Driver
.ProgramStringNotify
) {
1494 ctx
->Driver
.ProgramStringNotify( ctx
, GL_FRAGMENT_PROGRAM_ARB
,
1499 _mesa_print_program(&p
.program
->Base
);
1506 * Return a fragment program which implements the current
1507 * fixed-function texture, fog and color-sum operations.
1509 struct gl_fragment_program
*
1510 _mesa_get_fixed_func_fragment_program(GLcontext
*ctx
)
1512 struct gl_fragment_program
*prog
;
1513 struct state_key key
;
1516 keySize
= make_state_key(ctx
, &key
);
1518 prog
= (struct gl_fragment_program
*)
1519 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1523 prog
= (struct gl_fragment_program
*)
1524 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
1526 create_new_program(ctx
, &key
, prog
);
1528 _mesa_program_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1529 &key
, keySize
, &prog
->Base
);