1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
32 #include "shader/program.h"
33 #include "shader/prog_parameter.h"
34 #include "shader/prog_cache.h"
35 #include "shader/prog_instruction.h"
36 #include "shader/prog_print.h"
37 #include "shader/prog_statevars.h"
38 #include "shader/programopt.h"
39 #include "texenvprogram.h"
43 * Note on texture units:
45 * The number of texture units supported by fixed-function fragment
46 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
47 * That's because there's a one-to-one correspondence between texture
48 * coordinates and samplers in fixed-function processing.
50 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
51 * sets of texcoords, so is fixed-function fragment processing.
53 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
57 struct texenvprog_cache_item
61 struct gl_fragment_program
*data
;
62 struct texenvprog_cache_item
*next
;
66 texenv_doing_secondary_color(GLcontext
*ctx
)
68 if (ctx
->Light
.Enabled
&&
69 (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
))
72 if (ctx
->Fog
.ColorSumEnabled
)
79 * Up to nine instructions per tex unit, plus fog, specular color.
81 #define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
83 #define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
86 GLuint Source
:4; /**< SRC_x */
87 GLuint Operand
:3; /**< OPR_x */
91 GLuint nr_enabled_units
:8;
92 GLuint enabled_units
:8;
93 GLuint separate_specular
:1;
96 GLuint inputs_available
:12;
100 GLuint source_index
:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
102 GLuint ScaleShiftRGB
:2;
103 GLuint ScaleShiftA
:2;
106 GLuint ModeRGB
:5; /**< MODE_x */
107 struct mode_opt OptRGB
[MAX_COMBINER_TERMS
];
110 GLuint ModeA
:5; /**< MODE_x */
111 struct mode_opt OptA
[MAX_COMBINER_TERMS
];
112 } unit
[MAX_TEXTURE_UNITS
];
118 #define FOG_UNKNOWN 3
120 static GLuint
translate_fog_mode( GLenum mode
)
123 case GL_LINEAR
: return FOG_LINEAR
;
124 case GL_EXP
: return FOG_EXP
;
125 case GL_EXP2
: return FOG_EXP2
;
126 default: return FOG_UNKNOWN
;
130 #define OPR_SRC_COLOR 0
131 #define OPR_ONE_MINUS_SRC_COLOR 1
132 #define OPR_SRC_ALPHA 2
133 #define OPR_ONE_MINUS_SRC_ALPHA 3
136 #define OPR_UNKNOWN 7
138 static GLuint
translate_operand( GLenum operand
)
141 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
142 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
143 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
144 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
145 case GL_ZERO
: return OPR_ZERO
;
146 case GL_ONE
: return OPR_ONE
;
153 #define SRC_TEXTURE 0
154 #define SRC_TEXTURE0 1
155 #define SRC_TEXTURE1 2
156 #define SRC_TEXTURE2 3
157 #define SRC_TEXTURE3 4
158 #define SRC_TEXTURE4 5
159 #define SRC_TEXTURE5 6
160 #define SRC_TEXTURE6 7
161 #define SRC_TEXTURE7 8
162 #define SRC_CONSTANT 9
163 #define SRC_PRIMARY_COLOR 10
164 #define SRC_PREVIOUS 11
166 #define SRC_UNKNOWN 15
168 static GLuint
translate_source( GLenum src
)
171 case GL_TEXTURE
: return SRC_TEXTURE
;
179 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
180 case GL_CONSTANT
: return SRC_CONSTANT
;
181 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
182 case GL_PREVIOUS
: return SRC_PREVIOUS
;
191 #define MODE_REPLACE 0 /* r = a0 */
192 #define MODE_MODULATE 1 /* r = a0 * a1 */
193 #define MODE_ADD 2 /* r = a0 + a1 */
194 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
195 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
196 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
197 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
198 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
199 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
200 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
201 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
202 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
203 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
204 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
205 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
206 #define MODE_BUMP_ENVMAP_ATI 15 /* special */
207 #define MODE_UNKNOWN 16
210 * Translate GL combiner state into a MODE_x value
212 static GLuint
translate_mode( GLenum envMode
, GLenum mode
)
215 case GL_REPLACE
: return MODE_REPLACE
;
216 case GL_MODULATE
: return MODE_MODULATE
;
218 if (envMode
== GL_COMBINE4_NV
)
219 return MODE_ADD_PRODUCTS
;
223 if (envMode
== GL_COMBINE4_NV
)
224 return MODE_ADD_PRODUCTS_SIGNED
;
226 return MODE_ADD_SIGNED
;
227 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
228 case GL_SUBTRACT
: return MODE_SUBTRACT
;
229 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
230 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
231 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
232 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
233 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
234 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
235 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
236 case GL_BUMP_ENVMAP_ATI
: return MODE_BUMP_ENVMAP_ATI
;
244 #define TEXTURE_UNKNOWN_INDEX 7
247 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
249 static GLuint
translate_tex_src_bit( GLbitfield bit
)
251 /* make sure number of switch cases is correct */
252 assert(NUM_TEXTURE_TARGETS
== 7);
254 case TEXTURE_1D_BIT
: return TEXTURE_1D_INDEX
;
255 case TEXTURE_2D_BIT
: return TEXTURE_2D_INDEX
;
256 case TEXTURE_RECT_BIT
: return TEXTURE_RECT_INDEX
;
257 case TEXTURE_3D_BIT
: return TEXTURE_3D_INDEX
;
258 case TEXTURE_CUBE_BIT
: return TEXTURE_CUBE_INDEX
;
259 case TEXTURE_1D_ARRAY_BIT
: return TEXTURE_1D_ARRAY_INDEX
;
260 case TEXTURE_2D_ARRAY_BIT
: return TEXTURE_2D_ARRAY_INDEX
;
263 return TEXTURE_UNKNOWN_INDEX
;
268 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
269 #define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
272 * Identify all possible varying inputs. The fragment program will
273 * never reference non-varying inputs, but will track them via state
276 * This function figures out all the inputs that the fragment program
277 * has access to. The bitmask is later reduced to just those which
278 * are actually referenced.
280 static GLbitfield
get_fp_input_mask( GLcontext
*ctx
)
283 const GLboolean vertexShader
= (ctx
->Shader
.CurrentProgram
&&
284 ctx
->Shader
.CurrentProgram
->LinkStatus
&&
285 ctx
->Shader
.CurrentProgram
->VertexProgram
);
286 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
287 GLbitfield fp_inputs
= 0x0;
289 if (ctx
->VertexProgram
._Overriden
) {
290 /* Somebody's messing with the vertex program and we don't have
291 * a clue what's happening. Assume that it could be producing
292 * all possible outputs.
296 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
297 /* _NEW_RENDERMODE */
298 fp_inputs
= (FRAG_BIT_COL0
| FRAG_BIT_TEX0
);
300 else if (!(vertexProgram
|| vertexShader
) ||
301 !ctx
->VertexProgram
._Current
) {
302 /* Fixed function vertex logic */
304 GLbitfield varying_inputs
= ctx
->varying_vp_inputs
;
306 /* These get generated in the setup routine regardless of the
310 if (ctx
->Point
.PointSprite
)
311 varying_inputs
|= FRAG_BITS_TEX_ANY
;
313 /* First look at what values may be computed by the generated
317 if (ctx
->Light
.Enabled
) {
318 fp_inputs
|= FRAG_BIT_COL0
;
320 if (texenv_doing_secondary_color(ctx
))
321 fp_inputs
|= FRAG_BIT_COL1
;
325 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
326 ctx
->Texture
._TexMatEnabled
) << FRAG_ATTRIB_TEX0
;
328 /* Then look at what might be varying as a result of enabled
331 if (varying_inputs
& VERT_BIT_COLOR0
)
332 fp_inputs
|= FRAG_BIT_COL0
;
333 if (varying_inputs
& VERT_BIT_COLOR1
)
334 fp_inputs
|= FRAG_BIT_COL1
;
336 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
337 << FRAG_ATTRIB_TEX0
);
341 /* calculate from vp->outputs */
342 struct gl_vertex_program
*vprog
;
343 GLbitfield vp_outputs
;
345 /* Choose GLSL vertex shader over ARB vertex program. Need this
346 * since vertex shader state validation comes after fragment state
347 * validation (see additional comments in state.c).
350 vprog
= ctx
->Shader
.CurrentProgram
->VertexProgram
;
352 vprog
= ctx
->VertexProgram
.Current
;
354 vp_outputs
= vprog
->Base
.OutputsWritten
;
356 /* These get generated in the setup routine regardless of the
360 if (ctx
->Point
.PointSprite
)
361 vp_outputs
|= FRAG_BITS_TEX_ANY
;
363 if (vp_outputs
& (1 << VERT_RESULT_COL0
))
364 fp_inputs
|= FRAG_BIT_COL0
;
365 if (vp_outputs
& (1 << VERT_RESULT_COL1
))
366 fp_inputs
|= FRAG_BIT_COL1
;
368 fp_inputs
|= (((vp_outputs
& VERT_RESULT_TEX_ANY
) >> VERT_RESULT_TEX0
)
369 << FRAG_ATTRIB_TEX0
);
377 * Examine current texture environment state and generate a unique
378 * key to identify it.
380 static void make_state_key( GLcontext
*ctx
, struct state_key
*key
)
383 GLbitfield inputs_referenced
= FRAG_BIT_COL0
;
384 const GLbitfield inputs_available
= get_fp_input_mask( ctx
);
386 memset(key
, 0, sizeof(*key
));
389 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
390 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
391 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
392 const struct gl_tex_env_combine_state
*comb
= texUnit
->_CurrentCombine
;
395 if (!texUnit
->_ReallyEnabled
|| !texUnit
->Enabled
)
398 format
= texObj
->Image
[0][texObj
->BaseLevel
]->_BaseFormat
;
400 key
->unit
[i
].enabled
= 1;
401 key
->enabled_units
|= (1<<i
);
402 key
->nr_enabled_units
= i
+1;
403 inputs_referenced
|= FRAG_BIT_TEX(i
);
405 key
->unit
[i
].source_index
=
406 translate_tex_src_bit(texUnit
->_ReallyEnabled
);
408 key
->unit
[i
].shadow
= ((texObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
409 ((format
== GL_DEPTH_COMPONENT
) ||
410 (format
== GL_DEPTH_STENCIL_EXT
)));
412 key
->unit
[i
].NumArgsRGB
= comb
->_NumArgsRGB
;
413 key
->unit
[i
].NumArgsA
= comb
->_NumArgsA
;
415 key
->unit
[i
].ModeRGB
=
416 translate_mode(texUnit
->EnvMode
, comb
->ModeRGB
);
418 translate_mode(texUnit
->EnvMode
, comb
->ModeA
);
420 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
421 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
423 for (j
= 0; j
< MAX_COMBINER_TERMS
; j
++) {
424 key
->unit
[i
].OptRGB
[j
].Operand
= translate_operand(comb
->OperandRGB
[j
]);
425 key
->unit
[i
].OptA
[j
].Operand
= translate_operand(comb
->OperandA
[j
]);
426 key
->unit
[i
].OptRGB
[j
].Source
= translate_source(comb
->SourceRGB
[j
]);
427 key
->unit
[i
].OptA
[j
].Source
= translate_source(comb
->SourceA
[j
]);
430 if (key
->unit
[i
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
431 /* requires some special translation */
432 key
->unit
[i
].NumArgsRGB
= 2;
433 key
->unit
[i
].ScaleShiftRGB
= 0;
434 key
->unit
[i
].OptRGB
[0].Operand
= OPR_SRC_COLOR
;
435 key
->unit
[i
].OptRGB
[0].Source
= SRC_TEXTURE
;
436 key
->unit
[i
].OptRGB
[1].Operand
= OPR_SRC_COLOR
;
437 key
->unit
[i
].OptRGB
[1].Source
= texUnit
->BumpTarget
- GL_TEXTURE0
+ SRC_TEXTURE0
;
441 /* _NEW_LIGHT | _NEW_FOG */
442 if (texenv_doing_secondary_color(ctx
)) {
443 key
->separate_specular
= 1;
444 inputs_referenced
|= FRAG_BIT_COL1
;
448 if (ctx
->Fog
.Enabled
) {
449 key
->fog_enabled
= 1;
450 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
451 inputs_referenced
|= FRAG_BIT_FOGC
; /* maybe */
454 key
->inputs_available
= (inputs_available
& inputs_referenced
);
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
480 static const struct ureg undef
= {
491 /** State used to build the fragment program:
493 struct texenv_fragment_program
{
494 struct gl_fragment_program
*program
;
496 struct state_key
*state
;
498 GLbitfield alu_temps
; /**< Track texture indirections, see spec. */
499 GLbitfield temps_output
; /**< Track texture indirections, see spec. */
500 GLbitfield temp_in_use
; /**< Tracks temporary regs which are in use. */
503 struct ureg src_texture
[MAX_TEXTURE_COORD_UNITS
];
504 /* Reg containing each texture unit's sampled texture color,
508 struct ureg texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
509 /* Reg containing texcoord for a texture unit,
510 * needed for bump mapping, else undef.
513 struct ureg src_previous
; /**< Reg containing color from previous
514 * stage. May need to be decl'd.
517 GLuint last_tex_stage
; /**< Number of last enabled texture unit */
526 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
534 reg
.swz
= SWIZZLE_NOOP
;
539 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
541 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
544 GET_SWZ(reg
.swz
, w
));
549 static struct ureg
swizzle1( struct ureg reg
, int x
)
551 return swizzle(reg
, x
, x
, x
, x
);
554 static struct ureg
negate( struct ureg reg
)
560 static GLboolean
is_undef( struct ureg reg
)
562 return reg
.file
== PROGRAM_UNDEFINED
;
566 static struct ureg
get_temp( struct texenv_fragment_program
*p
)
570 /* First try and reuse temps which have been used already:
572 bit
= _mesa_ffs( ~p
->temp_in_use
& p
->alu_temps
);
574 /* Then any unused temporary:
577 bit
= _mesa_ffs( ~p
->temp_in_use
);
580 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
584 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
585 p
->program
->Base
.NumTemporaries
= bit
;
587 p
->temp_in_use
|= 1<<(bit
-1);
588 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
591 static struct ureg
get_tex_temp( struct texenv_fragment_program
*p
)
595 /* First try to find available temp not previously used (to avoid
596 * starting a new texture indirection). According to the spec, the
597 * ~p->temps_output isn't necessary, but will keep it there for
600 bit
= _mesa_ffs( ~p
->temp_in_use
& ~p
->alu_temps
& ~p
->temps_output
);
602 /* Then any unused temporary:
605 bit
= _mesa_ffs( ~p
->temp_in_use
);
608 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
612 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
613 p
->program
->Base
.NumTemporaries
= bit
;
615 p
->temp_in_use
|= 1<<(bit
-1);
616 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
620 /** Mark a temp reg as being no longer allocatable. */
621 static void reserve_temp( struct texenv_fragment_program
*p
, struct ureg r
)
623 if (r
.file
== PROGRAM_TEMPORARY
)
624 p
->temps_output
|= (1 << r
.idx
);
628 static void release_temps(GLcontext
*ctx
, struct texenv_fragment_program
*p
)
630 GLuint max_temp
= ctx
->Const
.FragmentProgram
.MaxTemps
;
632 /* KW: To support tex_env_crossbar, don't release the registers in
635 if (max_temp
>= sizeof(int) * 8)
636 p
->temp_in_use
= p
->temps_output
;
638 p
->temp_in_use
= ~((1<<max_temp
)-1) | p
->temps_output
;
642 static struct ureg
register_param5( struct texenv_fragment_program
*p
,
649 gl_state_index tokens
[STATE_LENGTH
];
656 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
657 return make_ureg(PROGRAM_STATE_VAR
, idx
);
661 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
662 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
663 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
664 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
666 static GLuint
frag_to_vert_attrib( GLuint attrib
)
669 case FRAG_ATTRIB_COL0
: return VERT_ATTRIB_COLOR0
;
670 case FRAG_ATTRIB_COL1
: return VERT_ATTRIB_COLOR1
;
672 assert(attrib
>= FRAG_ATTRIB_TEX0
);
673 assert(attrib
<= FRAG_ATTRIB_TEX7
);
674 return attrib
- FRAG_ATTRIB_TEX0
+ VERT_ATTRIB_TEX0
;
679 static struct ureg
register_input( struct texenv_fragment_program
*p
, GLuint input
)
681 if (p
->state
->inputs_available
& (1<<input
)) {
682 p
->program
->Base
.InputsRead
|= (1 << input
);
683 return make_ureg(PROGRAM_INPUT
, input
);
686 GLuint idx
= frag_to_vert_attrib( input
);
687 return register_param3( p
, STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, idx
);
692 static void emit_arg( struct prog_src_register
*reg
,
695 reg
->File
= ureg
.file
;
696 reg
->Index
= ureg
.idx
;
697 reg
->Swizzle
= ureg
.swz
;
698 reg
->Negate
= ureg
.negatebase
? NEGATE_XYZW
: NEGATE_NONE
;
702 static void emit_dst( struct prog_dst_register
*dst
,
703 struct ureg ureg
, GLuint mask
)
705 dst
->File
= ureg
.file
;
706 dst
->Index
= ureg
.idx
;
707 dst
->WriteMask
= mask
;
708 dst
->CondMask
= COND_TR
; /* always pass cond test */
709 dst
->CondSwizzle
= SWIZZLE_NOOP
;
712 static struct prog_instruction
*
713 emit_op(struct texenv_fragment_program
*p
,
722 GLuint nr
= p
->program
->Base
.NumInstructions
++;
723 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
725 assert(nr
< MAX_INSTRUCTIONS
);
727 _mesa_init_instructions(inst
, 1);
730 emit_arg( &inst
->SrcReg
[0], src0
);
731 emit_arg( &inst
->SrcReg
[1], src1
);
732 emit_arg( &inst
->SrcReg
[2], src2
);
734 inst
->SaturateMode
= saturate
? SATURATE_ZERO_ONE
: SATURATE_OFF
;
736 emit_dst( &inst
->DstReg
, dest
, mask
);
739 /* Accounting for indirection tracking:
741 if (dest
.file
== PROGRAM_TEMPORARY
)
742 p
->temps_output
|= 1 << dest
.idx
;
749 static struct ureg
emit_arith( struct texenv_fragment_program
*p
,
758 emit_op(p
, op
, dest
, mask
, saturate
, src0
, src1
, src2
);
760 /* Accounting for indirection tracking:
762 if (src0
.file
== PROGRAM_TEMPORARY
)
763 p
->alu_temps
|= 1 << src0
.idx
;
765 if (!is_undef(src1
) && src1
.file
== PROGRAM_TEMPORARY
)
766 p
->alu_temps
|= 1 << src1
.idx
;
768 if (!is_undef(src2
) && src2
.file
== PROGRAM_TEMPORARY
)
769 p
->alu_temps
|= 1 << src2
.idx
;
771 if (dest
.file
== PROGRAM_TEMPORARY
)
772 p
->alu_temps
|= 1 << dest
.idx
;
774 p
->program
->Base
.NumAluInstructions
++;
778 static struct ureg
emit_texld( struct texenv_fragment_program
*p
,
787 struct prog_instruction
*inst
= emit_op( p
, op
,
789 GL_FALSE
, /* don't saturate? */
794 inst
->TexSrcTarget
= tex_idx
;
795 inst
->TexSrcUnit
= tex_unit
;
796 inst
->TexShadow
= tex_shadow
;
798 p
->program
->Base
.NumTexInstructions
++;
800 /* Accounting for indirection tracking:
802 reserve_temp(p
, dest
);
805 /* Is this a texture indirection?
807 if ((coord
.file
== PROGRAM_TEMPORARY
&&
808 (p
->temps_output
& (1<<coord
.idx
))) ||
809 (dest
.file
== PROGRAM_TEMPORARY
&&
810 (p
->alu_temps
& (1<<dest
.idx
)))) {
811 p
->program
->Base
.NumTexIndirections
++;
812 p
->temps_output
= 1<<coord
.idx
;
814 assert(0); /* KW: texture env crossbar */
822 static struct ureg
register_const4f( struct texenv_fragment_program
*p
,
835 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
837 r
= make_ureg(PROGRAM_CONSTANT
, idx
);
842 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
843 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
844 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
845 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
848 static struct ureg
get_one( struct texenv_fragment_program
*p
)
850 if (is_undef(p
->one
))
851 p
->one
= register_scalar_const(p
, 1.0);
855 static struct ureg
get_half( struct texenv_fragment_program
*p
)
857 if (is_undef(p
->half
))
858 p
->half
= register_scalar_const(p
, 0.5);
862 static struct ureg
get_zero( struct texenv_fragment_program
*p
)
864 if (is_undef(p
->zero
))
865 p
->zero
= register_scalar_const(p
, 0.0);
870 static void program_error( struct texenv_fragment_program
*p
, const char *msg
)
872 _mesa_problem(NULL
, msg
);
876 static struct ureg
get_source( struct texenv_fragment_program
*p
,
877 GLuint src
, GLuint unit
)
881 assert(!is_undef(p
->src_texture
[unit
]));
882 return p
->src_texture
[unit
];
892 assert(!is_undef(p
->src_texture
[src
- SRC_TEXTURE0
]));
893 return p
->src_texture
[src
- SRC_TEXTURE0
];
896 return register_param2(p
, STATE_TEXENV_COLOR
, unit
);
898 case SRC_PRIMARY_COLOR
:
899 return register_input(p
, FRAG_ATTRIB_COL0
);
905 if (is_undef(p
->src_previous
))
906 return register_input(p
, FRAG_ATTRIB_COL0
);
908 return p
->src_previous
;
916 static struct ureg
emit_combine_source( struct texenv_fragment_program
*p
,
922 struct ureg arg
, src
, one
;
924 src
= get_source(p
, source
, unit
);
927 case OPR_ONE_MINUS_SRC_COLOR
:
929 * Emit tmp = 1.0 - arg.xyzw
933 return emit_arith( p
, OPCODE_SUB
, arg
, mask
, 0, one
, src
, undef
);
936 if (mask
== WRITEMASK_W
)
939 return swizzle1( src
, SWIZZLE_W
);
940 case OPR_ONE_MINUS_SRC_ALPHA
:
942 * Emit tmp = 1.0 - arg.wwww
946 return emit_arith(p
, OPCODE_SUB
, arg
, mask
, 0,
947 one
, swizzle1(src
, SWIZZLE_W
), undef
);
961 * Check if the RGB and Alpha sources and operands match for the given
962 * texture unit's combinder state. When the RGB and A sources and
963 * operands match, we can emit fewer instructions.
965 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
967 GLuint i
, nr
= key
->unit
[unit
].NumArgsRGB
;
969 for (i
= 0 ; i
< nr
; i
++) {
970 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
973 switch(key
->unit
[unit
].OptA
[i
].Operand
) {
975 switch(key
->unit
[unit
].OptRGB
[i
].Operand
) {
983 case OPR_ONE_MINUS_SRC_ALPHA
:
984 switch(key
->unit
[unit
].OptRGB
[i
].Operand
) {
985 case OPR_ONE_MINUS_SRC_COLOR
:
986 case OPR_ONE_MINUS_SRC_ALPHA
:
993 return GL_FALSE
; /* impossible */
1000 static struct ureg
emit_combine( struct texenv_fragment_program
*p
,
1007 const struct mode_opt
*opt
)
1009 struct ureg src
[MAX_COMBINER_TERMS
];
1010 struct ureg tmp
, half
;
1013 assert(nr
<= MAX_COMBINER_TERMS
);
1015 tmp
= undef
; /* silence warning (bug 5318) */
1017 for (i
= 0; i
< nr
; i
++)
1018 src
[i
] = emit_combine_source( p
, mask
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
1022 if (mask
== WRITEMASK_XYZW
&& !saturate
)
1025 return emit_arith( p
, OPCODE_MOV
, dest
, mask
, saturate
, src
[0], undef
, undef
);
1027 return emit_arith( p
, OPCODE_MUL
, dest
, mask
, saturate
,
1028 src
[0], src
[1], undef
);
1030 return emit_arith( p
, OPCODE_ADD
, dest
, mask
, saturate
,
1031 src
[0], src
[1], undef
);
1032 case MODE_ADD_SIGNED
:
1033 /* tmp = arg0 + arg1
1037 tmp
= get_temp( p
);
1038 emit_arith( p
, OPCODE_ADD
, tmp
, mask
, 0, src
[0], src
[1], undef
);
1039 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp
, half
, undef
);
1041 case MODE_INTERPOLATE
:
1042 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1044 return emit_arith( p
, OPCODE_LRP
, dest
, mask
, saturate
, src
[2], src
[0], src
[1] );
1047 return emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, src
[0], src
[1], undef
);
1049 case MODE_DOT3_RGBA
:
1050 case MODE_DOT3_RGBA_EXT
:
1051 case MODE_DOT3_RGB_EXT
:
1052 case MODE_DOT3_RGB
: {
1053 struct ureg tmp0
= get_temp( p
);
1054 struct ureg tmp1
= get_temp( p
);
1055 struct ureg neg1
= register_scalar_const(p
, -1);
1056 struct ureg two
= register_scalar_const(p
, 2);
1058 /* tmp0 = 2*src0 - 1
1061 * dst = tmp0 dot3 tmp1
1063 emit_arith( p
, OPCODE_MAD
, tmp0
, WRITEMASK_XYZW
, 0,
1066 if (_mesa_memcmp(&src
[0], &src
[1], sizeof(struct ureg
)) == 0)
1069 emit_arith( p
, OPCODE_MAD
, tmp1
, WRITEMASK_XYZW
, 0,
1071 emit_arith( p
, OPCODE_DP3
, dest
, mask
, saturate
, tmp0
, tmp1
, undef
);
1074 case MODE_MODULATE_ADD_ATI
:
1075 /* Arg0 * Arg2 + Arg1 */
1076 return emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
,
1077 src
[0], src
[2], src
[1] );
1078 case MODE_MODULATE_SIGNED_ADD_ATI
: {
1079 /* Arg0 * Arg2 + Arg1 - 0.5 */
1080 struct ureg tmp0
= get_temp(p
);
1082 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[0], src
[2], src
[1] );
1083 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
1086 case MODE_MODULATE_SUBTRACT_ATI
:
1087 /* Arg0 * Arg2 - Arg1 */
1088 emit_arith( p
, OPCODE_MAD
, dest
, mask
, 0, src
[0], src
[2], negate(src
[1]) );
1090 case MODE_ADD_PRODUCTS
:
1091 /* Arg0 * Arg1 + Arg2 * Arg3 */
1093 struct ureg tmp0
= get_temp(p
);
1094 emit_arith( p
, OPCODE_MUL
, tmp0
, mask
, 0, src
[0], src
[1], undef
);
1095 emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
, src
[2], src
[3], tmp0
);
1098 case MODE_ADD_PRODUCTS_SIGNED
:
1099 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1101 struct ureg tmp0
= get_temp(p
);
1103 emit_arith( p
, OPCODE_MUL
, tmp0
, mask
, 0, src
[0], src
[1], undef
);
1104 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[2], src
[3], tmp0
);
1105 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
1108 case MODE_BUMP_ENVMAP_ATI
:
1109 /* special - not handled here */
1120 * Generate instructions for one texture unit's env/combiner mode.
1123 emit_texenv(struct texenv_fragment_program
*p
, GLuint unit
)
1125 const struct state_key
*key
= p
->state
;
1127 GLuint rgb_shift
, alpha_shift
;
1128 struct ureg out
, dest
;
1130 if (!key
->unit
[unit
].enabled
) {
1131 return get_source(p
, SRC_PREVIOUS
, 0);
1133 if (key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1134 /* this isn't really a env stage delivering a color and handled elsewhere */
1135 return get_source(p
, SRC_PREVIOUS
, 0);
1138 switch (key
->unit
[unit
].ModeRGB
) {
1139 case MODE_DOT3_RGB_EXT
:
1140 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
1143 case MODE_DOT3_RGBA_EXT
:
1148 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
1149 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
1153 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1154 * We don't want to clamp twice.
1156 saturate
= !(rgb_shift
|| alpha_shift
);
1158 /* If this is the very last calculation, emit direct to output reg:
1160 if (key
->separate_specular
||
1161 unit
!= p
->last_tex_stage
||
1164 dest
= get_temp( p
);
1166 dest
= make_ureg(PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
);
1168 /* Emit the RGB and A combine ops
1170 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
1171 args_match(key
, unit
)) {
1172 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
1174 key
->unit
[unit
].NumArgsRGB
,
1175 key
->unit
[unit
].ModeRGB
,
1176 key
->unit
[unit
].OptRGB
);
1178 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
1179 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
1180 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
1182 key
->unit
[unit
].NumArgsRGB
,
1183 key
->unit
[unit
].ModeRGB
,
1184 key
->unit
[unit
].OptRGB
);
1187 /* Need to do something to stop from re-emitting identical
1188 * argument calculations here:
1190 out
= emit_combine( p
, dest
, WRITEMASK_XYZ
, saturate
,
1192 key
->unit
[unit
].NumArgsRGB
,
1193 key
->unit
[unit
].ModeRGB
,
1194 key
->unit
[unit
].OptRGB
);
1195 out
= emit_combine( p
, dest
, WRITEMASK_W
, saturate
,
1197 key
->unit
[unit
].NumArgsA
,
1198 key
->unit
[unit
].ModeA
,
1199 key
->unit
[unit
].OptA
);
1202 /* Deal with the final shift:
1204 if (alpha_shift
|| rgb_shift
) {
1207 saturate
= GL_TRUE
; /* always saturate at this point */
1209 if (rgb_shift
== alpha_shift
) {
1210 shift
= register_scalar_const(p
, (GLfloat
)(1<<rgb_shift
));
1213 shift
= register_const4f(p
,
1214 (GLfloat
)(1<<rgb_shift
),
1215 (GLfloat
)(1<<rgb_shift
),
1216 (GLfloat
)(1<<rgb_shift
),
1217 (GLfloat
)(1<<alpha_shift
));
1219 return emit_arith( p
, OPCODE_MUL
, dest
, WRITEMASK_XYZW
,
1220 saturate
, out
, shift
, undef
);
1228 * Generate instruction for getting a texture source term.
1230 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
1232 if (is_undef(p
->src_texture
[unit
])) {
1233 GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
1234 struct ureg texcoord
;
1235 struct ureg tmp
= get_tex_temp( p
);
1237 if (is_undef(p
->texcoord_tex
[unit
])) {
1238 texcoord
= register_input(p
, FRAG_ATTRIB_TEX0
+unit
);
1241 /* might want to reuse this reg for tex output actually */
1242 texcoord
= p
->texcoord_tex
[unit
];
1245 if (texTarget
== TEXTURE_UNKNOWN_INDEX
)
1246 program_error(p
, "TexSrcBit");
1248 /* TODO: Use D0_MASK_XY where possible.
1250 if (p
->state
->unit
[unit
].enabled
) {
1251 GLboolean shadow
= GL_FALSE
;
1253 if (p
->state
->unit
[unit
].shadow
) {
1254 p
->program
->Base
.ShadowSamplers
|= 1 << unit
;
1258 p
->src_texture
[unit
] = emit_texld( p
, OPCODE_TXP
,
1259 tmp
, WRITEMASK_XYZW
,
1260 unit
, texTarget
, shadow
,
1263 p
->program
->Base
.SamplersUsed
|= (1 << unit
);
1264 /* This identity mapping should already be in place
1265 * (see _mesa_init_program_struct()) but let's be safe.
1267 p
->program
->Base
.SamplerUnits
[unit
] = unit
;
1270 p
->src_texture
[unit
] = get_zero(p
);
1274 static GLboolean
load_texenv_source( struct texenv_fragment_program
*p
,
1275 GLuint src
, GLuint unit
)
1279 load_texture(p
, unit
);
1290 load_texture(p
, src
- SRC_TEXTURE0
);
1294 /* not a texture src - do nothing */
1303 * Generate instructions for loading all texture source terms.
1306 load_texunit_sources( struct texenv_fragment_program
*p
, int unit
)
1308 const struct state_key
*key
= p
->state
;
1311 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1312 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1315 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1316 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1323 * Generate instructions for loading bump map textures.
1326 load_texunit_bumpmap( struct texenv_fragment_program
*p
, int unit
)
1328 const struct state_key
*key
= p
->state
;
1329 GLuint bumpedUnitNr
= key
->unit
[unit
].OptRGB
[1].Source
- SRC_TEXTURE0
;
1330 struct ureg texcDst
, bumpMapRes
;
1331 struct ureg constdudvcolor
= register_const4f(p
, 0.0, 0.0, 0.0, 1.0);
1332 struct ureg texcSrc
= register_input(p
, FRAG_ATTRIB_TEX0
+ bumpedUnitNr
);
1333 struct ureg rotMat0
= register_param3( p
, STATE_INTERNAL
, STATE_ROT_MATRIX_0
, unit
);
1334 struct ureg rotMat1
= register_param3( p
, STATE_INTERNAL
, STATE_ROT_MATRIX_1
, unit
);
1336 load_texenv_source( p
, unit
+ SRC_TEXTURE0
, unit
);
1338 bumpMapRes
= get_source(p
, key
->unit
[unit
].OptRGB
[0].Source
, unit
);
1339 texcDst
= get_tex_temp( p
);
1340 p
->texcoord_tex
[bumpedUnitNr
] = texcDst
;
1342 /* apply rot matrix and add coords to be available in next phase */
1343 /* dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1) */
1344 /* note only 2 coords are affected the rest are left unchanged (mul by 0) */
1345 emit_arith( p
, OPCODE_MAD
, texcDst
, WRITEMASK_XYZW
, 0,
1346 swizzle1(bumpMapRes
, SWIZZLE_X
), rotMat0
, texcSrc
);
1347 emit_arith( p
, OPCODE_MAD
, texcDst
, WRITEMASK_XYZW
, 0,
1348 swizzle1(bumpMapRes
, SWIZZLE_Y
), rotMat1
, texcDst
);
1350 /* move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1351 enough to access this later, should optimize away */
1352 emit_arith( p
, OPCODE_MOV
, bumpMapRes
, WRITEMASK_XYZW
, 0, constdudvcolor
, undef
, undef
);
1358 * Generate a new fragment program which implements the context's
1359 * current texture env/combine mode.
1362 create_new_program(GLcontext
*ctx
, struct state_key
*key
,
1363 struct gl_fragment_program
*program
)
1365 struct prog_instruction instBuffer
[MAX_INSTRUCTIONS
];
1366 struct texenv_fragment_program p
;
1368 struct ureg cf
, out
;
1370 _mesa_memset(&p
, 0, sizeof(p
));
1373 p
.program
= program
;
1375 /* During code generation, use locally-allocated instruction buffer,
1376 * then alloc dynamic storage below.
1378 p
.program
->Base
.Instructions
= instBuffer
;
1379 p
.program
->Base
.Target
= GL_FRAGMENT_PROGRAM_ARB
;
1380 p
.program
->Base
.NumTexIndirections
= 1;
1381 p
.program
->Base
.NumTexInstructions
= 0;
1382 p
.program
->Base
.NumAluInstructions
= 0;
1383 p
.program
->Base
.String
= NULL
;
1384 p
.program
->Base
.NumInstructions
=
1385 p
.program
->Base
.NumTemporaries
=
1386 p
.program
->Base
.NumParameters
=
1387 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1388 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1390 p
.program
->Base
.InputsRead
= 0;
1391 p
.program
->Base
.OutputsWritten
= 1 << FRAG_RESULT_COLOR
;
1393 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1394 p
.src_texture
[unit
] = undef
;
1395 p
.texcoord_tex
[unit
] = undef
;
1398 p
.src_previous
= undef
;
1403 p
.last_tex_stage
= 0;
1404 release_temps(ctx
, &p
);
1406 if (key
->enabled_units
) {
1407 GLboolean needbumpstage
= GL_FALSE
;
1408 /* Zeroth pass - bump map textures first */
1409 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1410 if (key
->unit
[unit
].enabled
&& key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1411 needbumpstage
= GL_TRUE
;
1412 load_texunit_bumpmap( &p
, unit
);
1415 p
.program
->Base
.NumTexIndirections
++;
1417 /* First pass - to support texture_env_crossbar, first identify
1418 * all referenced texture sources and emit texld instructions
1421 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1422 if (key
->unit
[unit
].enabled
) {
1423 load_texunit_sources( &p
, unit
);
1424 p
.last_tex_stage
= unit
;
1427 /* Second pass - emit combine instructions to build final color:
1429 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1430 if (key
->enabled_units
& (1<<unit
)) {
1431 p
.src_previous
= emit_texenv( &p
, unit
);
1432 reserve_temp(&p
, p
.src_previous
); /* don't re-use this temp reg */
1433 release_temps(ctx
, &p
); /* release all temps */
1437 cf
= get_source( &p
, SRC_PREVIOUS
, 0 );
1438 out
= make_ureg( PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
);
1440 if (key
->separate_specular
) {
1441 /* Emit specular add.
1443 struct ureg s
= register_input(&p
, FRAG_ATTRIB_COL1
);
1444 emit_arith( &p
, OPCODE_ADD
, out
, WRITEMASK_XYZ
, 0, cf
, s
, undef
);
1445 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_W
, 0, cf
, undef
, undef
);
1447 else if (_mesa_memcmp(&cf
, &out
, sizeof(cf
)) != 0) {
1448 /* Will wind up in here if no texture enabled or a couple of
1449 * other scenarios (GL_REPLACE for instance).
1451 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_XYZW
, 0, cf
, undef
, undef
);
1456 emit_arith( &p
, OPCODE_END
, undef
, WRITEMASK_XYZW
, 0, undef
, undef
, undef
);
1458 if (key
->fog_enabled
) {
1459 /* Pull fog mode from GLcontext, the value in the state key is
1460 * a reduced value and not what is expected in FogOption
1462 p
.program
->FogOption
= ctx
->Fog
.Mode
;
1463 p
.program
->Base
.InputsRead
|= FRAG_BIT_FOGC
; /* XXX new */
1465 p
.program
->FogOption
= GL_NONE
;
1467 if (p
.program
->Base
.NumTexIndirections
> ctx
->Const
.FragmentProgram
.MaxTexIndirections
)
1468 program_error(&p
, "Exceeded max nr indirect texture lookups");
1470 if (p
.program
->Base
.NumTexInstructions
> ctx
->Const
.FragmentProgram
.MaxTexInstructions
)
1471 program_error(&p
, "Exceeded max TEX instructions");
1473 if (p
.program
->Base
.NumAluInstructions
> ctx
->Const
.FragmentProgram
.MaxAluInstructions
)
1474 program_error(&p
, "Exceeded max ALU instructions");
1476 ASSERT(p
.program
->Base
.NumInstructions
<= MAX_INSTRUCTIONS
);
1478 /* Allocate final instruction array */
1479 p
.program
->Base
.Instructions
1480 = _mesa_alloc_instructions(p
.program
->Base
.NumInstructions
);
1481 if (!p
.program
->Base
.Instructions
) {
1482 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1483 "generating tex env program");
1486 _mesa_copy_instructions(p
.program
->Base
.Instructions
, instBuffer
,
1487 p
.program
->Base
.NumInstructions
);
1489 if (p
.program
->FogOption
) {
1490 _mesa_append_fog_code(ctx
, p
.program
);
1491 p
.program
->FogOption
= GL_NONE
;
1495 /* Notify driver the fragment program has (actually) changed.
1497 if (ctx
->Driver
.ProgramStringNotify
) {
1498 ctx
->Driver
.ProgramStringNotify( ctx
, GL_FRAGMENT_PROGRAM_ARB
,
1503 _mesa_print_program(&p
.program
->Base
);
1510 * Return a fragment program which implements the current
1511 * fixed-function texture, fog and color-sum operations.
1513 struct gl_fragment_program
*
1514 _mesa_get_fixed_func_fragment_program(GLcontext
*ctx
)
1516 struct gl_fragment_program
*prog
;
1517 struct state_key key
;
1519 make_state_key(ctx
, &key
);
1521 prog
= (struct gl_fragment_program
*)
1522 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1526 prog
= (struct gl_fragment_program
*)
1527 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
1529 create_new_program(ctx
, &key
, prog
);
1531 _mesa_program_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1532 &key
, sizeof(key
), &prog
->Base
);