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 * Do we need to clamp the results of the given texture env/combine mode?
252 * If the inputs to the mode are in [0,1] we don't always have to clamp
256 need_saturate( GLuint mode
)
261 case MODE_INTERPOLATE
:
264 case MODE_ADD_SIGNED
:
267 case MODE_DOT3_RGB_EXT
:
269 case MODE_DOT3_RGBA_EXT
:
270 case MODE_MODULATE_ADD_ATI
:
271 case MODE_MODULATE_SIGNED_ADD_ATI
:
272 case MODE_MODULATE_SUBTRACT_ATI
:
273 case MODE_ADD_PRODUCTS
:
274 case MODE_ADD_PRODUCTS_SIGNED
:
275 case MODE_BUMP_ENVMAP_ATI
:
286 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
288 static GLuint
translate_tex_src_bit( GLbitfield bit
)
291 return _mesa_ffs(bit
) - 1;
295 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
296 #define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
299 * Identify all possible varying inputs. The fragment program will
300 * never reference non-varying inputs, but will track them via state
303 * This function figures out all the inputs that the fragment program
304 * has access to. The bitmask is later reduced to just those which
305 * are actually referenced.
307 static GLbitfield
get_fp_input_mask( GLcontext
*ctx
)
310 const GLboolean vertexShader
= (ctx
->Shader
.CurrentProgram
&&
311 ctx
->Shader
.CurrentProgram
->LinkStatus
&&
312 ctx
->Shader
.CurrentProgram
->VertexProgram
);
313 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
314 GLbitfield fp_inputs
= 0x0;
316 if (ctx
->VertexProgram
._Overriden
) {
317 /* Somebody's messing with the vertex program and we don't have
318 * a clue what's happening. Assume that it could be producing
319 * all possible outputs.
323 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
324 /* _NEW_RENDERMODE */
325 fp_inputs
= (FRAG_BIT_COL0
| FRAG_BIT_TEX0
);
327 else if (!(vertexProgram
|| vertexShader
) ||
328 !ctx
->VertexProgram
._Current
) {
329 /* Fixed function vertex logic */
331 GLbitfield varying_inputs
= ctx
->varying_vp_inputs
;
333 /* These get generated in the setup routine regardless of the
337 if (ctx
->Point
.PointSprite
)
338 varying_inputs
|= FRAG_BITS_TEX_ANY
;
340 /* First look at what values may be computed by the generated
344 if (ctx
->Light
.Enabled
) {
345 fp_inputs
|= FRAG_BIT_COL0
;
347 if (texenv_doing_secondary_color(ctx
))
348 fp_inputs
|= FRAG_BIT_COL1
;
352 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
353 ctx
->Texture
._TexMatEnabled
) << FRAG_ATTRIB_TEX0
;
355 /* Then look at what might be varying as a result of enabled
358 if (varying_inputs
& VERT_BIT_COLOR0
)
359 fp_inputs
|= FRAG_BIT_COL0
;
360 if (varying_inputs
& VERT_BIT_COLOR1
)
361 fp_inputs
|= FRAG_BIT_COL1
;
363 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
364 << FRAG_ATTRIB_TEX0
);
368 /* calculate from vp->outputs */
369 struct gl_vertex_program
*vprog
;
370 GLbitfield64 vp_outputs
;
372 /* Choose GLSL vertex shader over ARB vertex program. Need this
373 * since vertex shader state validation comes after fragment state
374 * validation (see additional comments in state.c).
377 vprog
= ctx
->Shader
.CurrentProgram
->VertexProgram
;
379 vprog
= ctx
->VertexProgram
.Current
;
381 vp_outputs
= vprog
->Base
.OutputsWritten
;
383 /* These get generated in the setup routine regardless of the
387 if (ctx
->Point
.PointSprite
)
388 vp_outputs
|= FRAG_BITS_TEX_ANY
;
390 if (vp_outputs
& (1 << VERT_RESULT_COL0
))
391 fp_inputs
|= FRAG_BIT_COL0
;
392 if (vp_outputs
& (1 << VERT_RESULT_COL1
))
393 fp_inputs
|= FRAG_BIT_COL1
;
395 fp_inputs
|= (((vp_outputs
& VERT_RESULT_TEX_ANY
) >> VERT_RESULT_TEX0
)
396 << FRAG_ATTRIB_TEX0
);
404 * Examine current texture environment state and generate a unique
405 * key to identify it.
407 static GLuint
make_state_key( GLcontext
*ctx
, struct state_key
*key
)
410 GLbitfield inputs_referenced
= FRAG_BIT_COL0
;
411 const GLbitfield inputs_available
= get_fp_input_mask( ctx
);
414 memset(key
, 0, sizeof(*key
));
417 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
418 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
419 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
420 const struct gl_tex_env_combine_state
*comb
= texUnit
->_CurrentCombine
;
423 if (!texUnit
->_ReallyEnabled
|| !texUnit
->Enabled
)
426 format
= texObj
->Image
[0][texObj
->BaseLevel
]->_BaseFormat
;
428 key
->unit
[i
].enabled
= 1;
429 key
->enabled_units
|= (1<<i
);
430 key
->nr_enabled_units
= i
+ 1;
431 inputs_referenced
|= FRAG_BIT_TEX(i
);
433 key
->unit
[i
].source_index
=
434 translate_tex_src_bit(texUnit
->_ReallyEnabled
);
436 key
->unit
[i
].shadow
= ((texObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
437 ((format
== GL_DEPTH_COMPONENT
) ||
438 (format
== GL_DEPTH_STENCIL_EXT
)));
440 key
->unit
[i
].NumArgsRGB
= comb
->_NumArgsRGB
;
441 key
->unit
[i
].NumArgsA
= comb
->_NumArgsA
;
443 key
->unit
[i
].ModeRGB
=
444 translate_mode(texUnit
->EnvMode
, comb
->ModeRGB
);
446 translate_mode(texUnit
->EnvMode
, comb
->ModeA
);
448 key
->unit
[i
].ScaleShiftRGB
= comb
->ScaleShiftRGB
;
449 key
->unit
[i
].ScaleShiftA
= comb
->ScaleShiftA
;
451 for (j
= 0; j
< MAX_COMBINER_TERMS
; j
++) {
452 key
->unit
[i
].OptRGB
[j
].Operand
= translate_operand(comb
->OperandRGB
[j
]);
453 key
->unit
[i
].OptA
[j
].Operand
= translate_operand(comb
->OperandA
[j
]);
454 key
->unit
[i
].OptRGB
[j
].Source
= translate_source(comb
->SourceRGB
[j
]);
455 key
->unit
[i
].OptA
[j
].Source
= translate_source(comb
->SourceA
[j
]);
458 if (key
->unit
[i
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
459 /* requires some special translation */
460 key
->unit
[i
].NumArgsRGB
= 2;
461 key
->unit
[i
].ScaleShiftRGB
= 0;
462 key
->unit
[i
].OptRGB
[0].Operand
= OPR_SRC_COLOR
;
463 key
->unit
[i
].OptRGB
[0].Source
= SRC_TEXTURE
;
464 key
->unit
[i
].OptRGB
[1].Operand
= OPR_SRC_COLOR
;
465 key
->unit
[i
].OptRGB
[1].Source
= texUnit
->BumpTarget
- GL_TEXTURE0
+ SRC_TEXTURE0
;
469 /* _NEW_LIGHT | _NEW_FOG */
470 if (texenv_doing_secondary_color(ctx
)) {
471 key
->separate_specular
= 1;
472 inputs_referenced
|= FRAG_BIT_COL1
;
476 if (ctx
->Fog
.Enabled
) {
477 key
->fog_enabled
= 1;
478 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
479 inputs_referenced
|= FRAG_BIT_FOGC
; /* maybe */
482 key
->inputs_available
= (inputs_available
& inputs_referenced
);
484 /* compute size of state key, ignoring unused texture units */
485 keySize
= sizeof(*key
) - sizeof(key
->unit
)
486 + key
->nr_enabled_units
* sizeof(key
->unit
[0]);
493 * Use uregs to represent registers internally, translate to Mesa's
494 * expected formats on emit.
496 * NOTE: These are passed by value extensively in this file rather
497 * than as usual by pointer reference. If this disturbs you, try
498 * remembering they are just 32bits in size.
500 * GCC is smart enough to deal with these dword-sized structures in
501 * much the same way as if I had defined them as dwords and was using
502 * macros to access and set the fields. This is much nicer and easier
513 static const struct ureg undef
= {
522 /** State used to build the fragment program:
524 struct texenv_fragment_program
{
525 struct gl_fragment_program
*program
;
526 struct state_key
*state
;
528 GLbitfield alu_temps
; /**< Track texture indirections, see spec. */
529 GLbitfield temps_output
; /**< Track texture indirections, see spec. */
530 GLbitfield temp_in_use
; /**< Tracks temporary regs which are in use. */
533 struct ureg src_texture
[MAX_TEXTURE_COORD_UNITS
];
534 /* Reg containing each texture unit's sampled texture color,
538 struct ureg texcoord_tex
[MAX_TEXTURE_COORD_UNITS
];
539 /* Reg containing texcoord for a texture unit,
540 * needed for bump mapping, else undef.
543 struct ureg src_previous
; /**< Reg containing color from previous
544 * stage. May need to be decl'd.
547 GLuint last_tex_stage
; /**< Number of last enabled texture unit */
556 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
562 reg
.swz
= SWIZZLE_NOOP
;
567 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
569 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
572 GET_SWZ(reg
.swz
, w
));
577 static struct ureg
swizzle1( struct ureg reg
, int x
)
579 return swizzle(reg
, x
, x
, x
, x
);
582 static struct ureg
negate( struct ureg reg
)
588 static GLboolean
is_undef( struct ureg reg
)
590 return reg
.file
== PROGRAM_UNDEFINED
;
594 static struct ureg
get_temp( struct texenv_fragment_program
*p
)
598 /* First try and reuse temps which have been used already:
600 bit
= _mesa_ffs( ~p
->temp_in_use
& p
->alu_temps
);
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));
619 static struct ureg
get_tex_temp( struct texenv_fragment_program
*p
)
623 /* First try to find available temp not previously used (to avoid
624 * starting a new texture indirection). According to the spec, the
625 * ~p->temps_output isn't necessary, but will keep it there for
628 bit
= _mesa_ffs( ~p
->temp_in_use
& ~p
->alu_temps
& ~p
->temps_output
);
630 /* Then any unused temporary:
633 bit
= _mesa_ffs( ~p
->temp_in_use
);
636 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
640 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
641 p
->program
->Base
.NumTemporaries
= bit
;
643 p
->temp_in_use
|= 1<<(bit
-1);
644 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
648 /** Mark a temp reg as being no longer allocatable. */
649 static void reserve_temp( struct texenv_fragment_program
*p
, struct ureg r
)
651 if (r
.file
== PROGRAM_TEMPORARY
)
652 p
->temps_output
|= (1 << r
.idx
);
656 static void release_temps(GLcontext
*ctx
, struct texenv_fragment_program
*p
)
658 GLuint max_temp
= ctx
->Const
.FragmentProgram
.MaxTemps
;
660 /* KW: To support tex_env_crossbar, don't release the registers in
663 if (max_temp
>= sizeof(int) * 8)
664 p
->temp_in_use
= p
->temps_output
;
666 p
->temp_in_use
= ~((1<<max_temp
)-1) | p
->temps_output
;
670 static struct ureg
register_param5( struct texenv_fragment_program
*p
,
677 gl_state_index tokens
[STATE_LENGTH
];
684 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
685 return make_ureg(PROGRAM_STATE_VAR
, idx
);
689 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
690 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
691 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
692 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
694 static GLuint
frag_to_vert_attrib( GLuint attrib
)
697 case FRAG_ATTRIB_COL0
: return VERT_ATTRIB_COLOR0
;
698 case FRAG_ATTRIB_COL1
: return VERT_ATTRIB_COLOR1
;
700 assert(attrib
>= FRAG_ATTRIB_TEX0
);
701 assert(attrib
<= FRAG_ATTRIB_TEX7
);
702 return attrib
- FRAG_ATTRIB_TEX0
+ VERT_ATTRIB_TEX0
;
707 static struct ureg
register_input( struct texenv_fragment_program
*p
, GLuint input
)
709 if (p
->state
->inputs_available
& (1<<input
)) {
710 p
->program
->Base
.InputsRead
|= (1 << input
);
711 return make_ureg(PROGRAM_INPUT
, input
);
714 GLuint idx
= frag_to_vert_attrib( input
);
715 return register_param3( p
, STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, idx
);
720 static void emit_arg( struct prog_src_register
*reg
,
723 reg
->File
= ureg
.file
;
724 reg
->Index
= ureg
.idx
;
725 reg
->Swizzle
= ureg
.swz
;
726 reg
->Negate
= ureg
.negatebase
? NEGATE_XYZW
: NEGATE_NONE
;
730 static void emit_dst( struct prog_dst_register
*dst
,
731 struct ureg ureg
, GLuint mask
)
733 dst
->File
= ureg
.file
;
734 dst
->Index
= ureg
.idx
;
735 dst
->WriteMask
= mask
;
736 dst
->CondMask
= COND_TR
; /* always pass cond test */
737 dst
->CondSwizzle
= SWIZZLE_NOOP
;
740 static struct prog_instruction
*
741 emit_op(struct texenv_fragment_program
*p
,
750 const GLuint nr
= p
->program
->Base
.NumInstructions
++;
751 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
753 assert(nr
< MAX_INSTRUCTIONS
);
755 _mesa_init_instructions(inst
, 1);
758 emit_arg( &inst
->SrcReg
[0], src0
);
759 emit_arg( &inst
->SrcReg
[1], src1
);
760 emit_arg( &inst
->SrcReg
[2], src2
);
762 inst
->SaturateMode
= saturate
? SATURATE_ZERO_ONE
: SATURATE_OFF
;
764 emit_dst( &inst
->DstReg
, dest
, mask
);
767 /* Accounting for indirection tracking:
769 if (dest
.file
== PROGRAM_TEMPORARY
)
770 p
->temps_output
|= 1 << dest
.idx
;
777 static struct ureg
emit_arith( struct texenv_fragment_program
*p
,
786 emit_op(p
, op
, dest
, mask
, saturate
, src0
, src1
, src2
);
788 /* Accounting for indirection tracking:
790 if (src0
.file
== PROGRAM_TEMPORARY
)
791 p
->alu_temps
|= 1 << src0
.idx
;
793 if (!is_undef(src1
) && src1
.file
== PROGRAM_TEMPORARY
)
794 p
->alu_temps
|= 1 << src1
.idx
;
796 if (!is_undef(src2
) && src2
.file
== PROGRAM_TEMPORARY
)
797 p
->alu_temps
|= 1 << src2
.idx
;
799 if (dest
.file
== PROGRAM_TEMPORARY
)
800 p
->alu_temps
|= 1 << dest
.idx
;
802 p
->program
->Base
.NumAluInstructions
++;
806 static struct ureg
emit_texld( struct texenv_fragment_program
*p
,
815 struct prog_instruction
*inst
= emit_op( p
, op
,
817 GL_FALSE
, /* don't saturate? */
822 inst
->TexSrcTarget
= tex_idx
;
823 inst
->TexSrcUnit
= tex_unit
;
824 inst
->TexShadow
= tex_shadow
;
826 p
->program
->Base
.NumTexInstructions
++;
828 /* Accounting for indirection tracking:
830 reserve_temp(p
, dest
);
833 /* Is this a texture indirection?
835 if ((coord
.file
== PROGRAM_TEMPORARY
&&
836 (p
->temps_output
& (1<<coord
.idx
))) ||
837 (dest
.file
== PROGRAM_TEMPORARY
&&
838 (p
->alu_temps
& (1<<dest
.idx
)))) {
839 p
->program
->Base
.NumTexIndirections
++;
840 p
->temps_output
= 1<<coord
.idx
;
842 assert(0); /* KW: texture env crossbar */
850 static struct ureg
register_const4f( struct texenv_fragment_program
*p
,
863 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
865 r
= make_ureg(PROGRAM_CONSTANT
, idx
);
870 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
871 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
872 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
873 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
876 static struct ureg
get_one( struct texenv_fragment_program
*p
)
878 if (is_undef(p
->one
))
879 p
->one
= register_scalar_const(p
, 1.0);
883 static struct ureg
get_half( struct texenv_fragment_program
*p
)
885 if (is_undef(p
->half
))
886 p
->half
= register_scalar_const(p
, 0.5);
890 static struct ureg
get_zero( struct texenv_fragment_program
*p
)
892 if (is_undef(p
->zero
))
893 p
->zero
= register_scalar_const(p
, 0.0);
898 static void program_error( struct texenv_fragment_program
*p
, const char *msg
)
900 _mesa_problem(NULL
, msg
);
904 static struct ureg
get_source( struct texenv_fragment_program
*p
,
905 GLuint src
, GLuint unit
)
909 assert(!is_undef(p
->src_texture
[unit
]));
910 return p
->src_texture
[unit
];
920 assert(!is_undef(p
->src_texture
[src
- SRC_TEXTURE0
]));
921 return p
->src_texture
[src
- SRC_TEXTURE0
];
924 return register_param2(p
, STATE_TEXENV_COLOR
, unit
);
926 case SRC_PRIMARY_COLOR
:
927 return register_input(p
, FRAG_ATTRIB_COL0
);
933 if (is_undef(p
->src_previous
))
934 return register_input(p
, FRAG_ATTRIB_COL0
);
936 return p
->src_previous
;
944 static struct ureg
emit_combine_source( struct texenv_fragment_program
*p
,
950 struct ureg arg
, src
, one
;
952 src
= get_source(p
, source
, unit
);
955 case OPR_ONE_MINUS_SRC_COLOR
:
957 * Emit tmp = 1.0 - arg.xyzw
961 return emit_arith( p
, OPCODE_SUB
, arg
, mask
, 0, one
, src
, undef
);
964 if (mask
== WRITEMASK_W
)
967 return swizzle1( src
, SWIZZLE_W
);
968 case OPR_ONE_MINUS_SRC_ALPHA
:
970 * Emit tmp = 1.0 - arg.wwww
974 return emit_arith(p
, OPCODE_SUB
, arg
, mask
, 0,
975 one
, swizzle1(src
, SWIZZLE_W
), undef
);
989 * Check if the RGB and Alpha sources and operands match for the given
990 * texture unit's combinder state. When the RGB and A sources and
991 * operands match, we can emit fewer instructions.
993 static GLboolean
args_match( const struct state_key
*key
, GLuint unit
)
995 GLuint i
, numArgs
= key
->unit
[unit
].NumArgsRGB
;
997 for (i
= 0; i
< numArgs
; i
++) {
998 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
1001 switch (key
->unit
[unit
].OptA
[i
].Operand
) {
1003 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
1011 case OPR_ONE_MINUS_SRC_ALPHA
:
1012 switch (key
->unit
[unit
].OptRGB
[i
].Operand
) {
1013 case OPR_ONE_MINUS_SRC_COLOR
:
1014 case OPR_ONE_MINUS_SRC_ALPHA
:
1021 return GL_FALSE
; /* impossible */
1028 static struct ureg
emit_combine( struct texenv_fragment_program
*p
,
1035 const struct mode_opt
*opt
)
1037 struct ureg src
[MAX_COMBINER_TERMS
];
1038 struct ureg tmp
, half
;
1041 assert(nr
<= MAX_COMBINER_TERMS
);
1043 tmp
= undef
; /* silence warning (bug 5318) */
1045 for (i
= 0; i
< nr
; i
++)
1046 src
[i
] = emit_combine_source( p
, mask
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
1050 if (mask
== WRITEMASK_XYZW
&& !saturate
)
1053 return emit_arith( p
, OPCODE_MOV
, dest
, mask
, saturate
, src
[0], undef
, undef
);
1055 return emit_arith( p
, OPCODE_MUL
, dest
, mask
, saturate
,
1056 src
[0], src
[1], undef
);
1058 return emit_arith( p
, OPCODE_ADD
, dest
, mask
, saturate
,
1059 src
[0], src
[1], undef
);
1060 case MODE_ADD_SIGNED
:
1061 /* tmp = arg0 + arg1
1065 tmp
= get_temp( p
);
1066 emit_arith( p
, OPCODE_ADD
, tmp
, mask
, 0, src
[0], src
[1], undef
);
1067 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp
, half
, undef
);
1069 case MODE_INTERPOLATE
:
1070 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1072 return emit_arith( p
, OPCODE_LRP
, dest
, mask
, saturate
, src
[2], src
[0], src
[1] );
1075 return emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, src
[0], src
[1], undef
);
1077 case MODE_DOT3_RGBA
:
1078 case MODE_DOT3_RGBA_EXT
:
1079 case MODE_DOT3_RGB_EXT
:
1080 case MODE_DOT3_RGB
: {
1081 struct ureg tmp0
= get_temp( p
);
1082 struct ureg tmp1
= get_temp( p
);
1083 struct ureg neg1
= register_scalar_const(p
, -1);
1084 struct ureg two
= register_scalar_const(p
, 2);
1086 /* tmp0 = 2*src0 - 1
1089 * dst = tmp0 dot3 tmp1
1091 emit_arith( p
, OPCODE_MAD
, tmp0
, WRITEMASK_XYZW
, 0,
1094 if (_mesa_memcmp(&src
[0], &src
[1], sizeof(struct ureg
)) == 0)
1097 emit_arith( p
, OPCODE_MAD
, tmp1
, WRITEMASK_XYZW
, 0,
1099 emit_arith( p
, OPCODE_DP3
, dest
, mask
, saturate
, tmp0
, tmp1
, undef
);
1102 case MODE_MODULATE_ADD_ATI
:
1103 /* Arg0 * Arg2 + Arg1 */
1104 return emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
,
1105 src
[0], src
[2], src
[1] );
1106 case MODE_MODULATE_SIGNED_ADD_ATI
: {
1107 /* Arg0 * Arg2 + Arg1 - 0.5 */
1108 struct ureg tmp0
= get_temp(p
);
1110 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[0], src
[2], src
[1] );
1111 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
1114 case MODE_MODULATE_SUBTRACT_ATI
:
1115 /* Arg0 * Arg2 - Arg1 */
1116 emit_arith( p
, OPCODE_MAD
, dest
, mask
, 0, src
[0], src
[2], negate(src
[1]) );
1118 case MODE_ADD_PRODUCTS
:
1119 /* Arg0 * Arg1 + Arg2 * Arg3 */
1121 struct ureg tmp0
= get_temp(p
);
1122 emit_arith( p
, OPCODE_MUL
, tmp0
, mask
, 0, src
[0], src
[1], undef
);
1123 emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
, src
[2], src
[3], tmp0
);
1126 case MODE_ADD_PRODUCTS_SIGNED
:
1127 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1129 struct ureg tmp0
= get_temp(p
);
1131 emit_arith( p
, OPCODE_MUL
, tmp0
, mask
, 0, src
[0], src
[1], undef
);
1132 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[2], src
[3], tmp0
);
1133 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
1136 case MODE_BUMP_ENVMAP_ATI
:
1137 /* special - not handled here */
1148 * Generate instructions for one texture unit's env/combiner mode.
1151 emit_texenv(struct texenv_fragment_program
*p
, GLuint unit
)
1153 const struct state_key
*key
= p
->state
;
1154 GLboolean rgb_saturate
, alpha_saturate
;
1155 GLuint rgb_shift
, alpha_shift
;
1156 struct ureg out
, dest
;
1158 if (!key
->unit
[unit
].enabled
) {
1159 return get_source(p
, SRC_PREVIOUS
, 0);
1161 if (key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1162 /* this isn't really a env stage delivering a color and handled elsewhere */
1163 return get_source(p
, SRC_PREVIOUS
, 0);
1166 switch (key
->unit
[unit
].ModeRGB
) {
1167 case MODE_DOT3_RGB_EXT
:
1168 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
1171 case MODE_DOT3_RGBA_EXT
:
1176 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
1177 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
1181 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1182 * We don't want to clamp twice.
1185 rgb_saturate
= GL_FALSE
; /* saturate after rgb shift */
1186 else if (need_saturate(key
->unit
[unit
].ModeRGB
))
1187 rgb_saturate
= GL_TRUE
;
1189 rgb_saturate
= GL_FALSE
;
1192 alpha_saturate
= GL_FALSE
; /* saturate after alpha shift */
1193 else if (need_saturate(key
->unit
[unit
].ModeA
))
1194 alpha_saturate
= GL_TRUE
;
1196 alpha_saturate
= GL_FALSE
;
1198 /* If this is the very last calculation, emit direct to output reg:
1200 if (key
->separate_specular
||
1201 unit
!= p
->last_tex_stage
||
1204 dest
= get_temp( p
);
1206 dest
= make_ureg(PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
);
1208 /* Emit the RGB and A combine ops
1210 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
1211 args_match(key
, unit
)) {
1212 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, rgb_saturate
,
1214 key
->unit
[unit
].NumArgsRGB
,
1215 key
->unit
[unit
].ModeRGB
,
1216 key
->unit
[unit
].OptRGB
);
1218 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
1219 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
1220 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, rgb_saturate
,
1222 key
->unit
[unit
].NumArgsRGB
,
1223 key
->unit
[unit
].ModeRGB
,
1224 key
->unit
[unit
].OptRGB
);
1227 /* Need to do something to stop from re-emitting identical
1228 * argument calculations here:
1230 out
= emit_combine( p
, dest
, WRITEMASK_XYZ
, rgb_saturate
,
1232 key
->unit
[unit
].NumArgsRGB
,
1233 key
->unit
[unit
].ModeRGB
,
1234 key
->unit
[unit
].OptRGB
);
1235 out
= emit_combine( p
, dest
, WRITEMASK_W
, alpha_saturate
,
1237 key
->unit
[unit
].NumArgsA
,
1238 key
->unit
[unit
].ModeA
,
1239 key
->unit
[unit
].OptA
);
1242 /* Deal with the final shift:
1244 if (alpha_shift
|| rgb_shift
) {
1246 GLboolean saturate
= GL_TRUE
; /* always saturate at this point */
1248 if (rgb_shift
== alpha_shift
) {
1249 shift
= register_scalar_const(p
, (GLfloat
)(1<<rgb_shift
));
1252 shift
= register_const4f(p
,
1253 (GLfloat
)(1<<rgb_shift
),
1254 (GLfloat
)(1<<rgb_shift
),
1255 (GLfloat
)(1<<rgb_shift
),
1256 (GLfloat
)(1<<alpha_shift
));
1258 return emit_arith( p
, OPCODE_MUL
, dest
, WRITEMASK_XYZW
,
1259 saturate
, out
, shift
, undef
);
1267 * Generate instruction for getting a texture source term.
1269 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
1271 if (is_undef(p
->src_texture
[unit
])) {
1272 const GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
1273 struct ureg texcoord
;
1274 struct ureg tmp
= get_tex_temp( p
);
1276 if (is_undef(p
->texcoord_tex
[unit
])) {
1277 texcoord
= register_input(p
, FRAG_ATTRIB_TEX0
+unit
);
1280 /* might want to reuse this reg for tex output actually */
1281 texcoord
= p
->texcoord_tex
[unit
];
1284 /* TODO: Use D0_MASK_XY where possible.
1286 if (p
->state
->unit
[unit
].enabled
) {
1287 GLboolean shadow
= GL_FALSE
;
1289 if (p
->state
->unit
[unit
].shadow
) {
1290 p
->program
->Base
.ShadowSamplers
|= 1 << unit
;
1294 p
->src_texture
[unit
] = emit_texld( p
, OPCODE_TXP
,
1295 tmp
, WRITEMASK_XYZW
,
1296 unit
, texTarget
, shadow
,
1299 p
->program
->Base
.SamplersUsed
|= (1 << unit
);
1300 /* This identity mapping should already be in place
1301 * (see _mesa_init_program_struct()) but let's be safe.
1303 p
->program
->Base
.SamplerUnits
[unit
] = unit
;
1306 p
->src_texture
[unit
] = get_zero(p
);
1310 static GLboolean
load_texenv_source( struct texenv_fragment_program
*p
,
1311 GLuint src
, GLuint unit
)
1315 load_texture(p
, unit
);
1326 load_texture(p
, src
- SRC_TEXTURE0
);
1330 /* not a texture src - do nothing */
1339 * Generate instructions for loading all texture source terms.
1342 load_texunit_sources( struct texenv_fragment_program
*p
, GLuint unit
)
1344 const struct state_key
*key
= p
->state
;
1347 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1348 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1351 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1352 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1359 * Generate instructions for loading bump map textures.
1362 load_texunit_bumpmap( struct texenv_fragment_program
*p
, GLuint unit
)
1364 const struct state_key
*key
= p
->state
;
1365 GLuint bumpedUnitNr
= key
->unit
[unit
].OptRGB
[1].Source
- SRC_TEXTURE0
;
1366 struct ureg texcDst
, bumpMapRes
;
1367 struct ureg constdudvcolor
= register_const4f(p
, 0.0, 0.0, 0.0, 1.0);
1368 struct ureg texcSrc
= register_input(p
, FRAG_ATTRIB_TEX0
+ bumpedUnitNr
);
1369 struct ureg rotMat0
= register_param3( p
, STATE_INTERNAL
, STATE_ROT_MATRIX_0
, unit
);
1370 struct ureg rotMat1
= register_param3( p
, STATE_INTERNAL
, STATE_ROT_MATRIX_1
, unit
);
1372 load_texenv_source( p
, unit
+ SRC_TEXTURE0
, unit
);
1374 bumpMapRes
= get_source(p
, key
->unit
[unit
].OptRGB
[0].Source
, unit
);
1375 texcDst
= get_tex_temp( p
);
1376 p
->texcoord_tex
[bumpedUnitNr
] = texcDst
;
1378 /* Apply rot matrix and add coords to be available in next phase.
1379 * dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1)
1380 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1382 emit_arith( p
, OPCODE_MAD
, texcDst
, WRITEMASK_XYZW
, 0,
1383 swizzle1(bumpMapRes
, SWIZZLE_X
), rotMat0
, texcSrc
);
1384 emit_arith( p
, OPCODE_MAD
, texcDst
, WRITEMASK_XYZW
, 0,
1385 swizzle1(bumpMapRes
, SWIZZLE_Y
), rotMat1
, texcDst
);
1387 /* Move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1388 * enough to access this later, should optimize away.
1390 emit_arith( p
, OPCODE_MOV
, bumpMapRes
, WRITEMASK_XYZW
, 0,
1391 constdudvcolor
, undef
, undef
);
1397 * Generate a new fragment program which implements the context's
1398 * current texture env/combine mode.
1401 create_new_program(GLcontext
*ctx
, struct state_key
*key
,
1402 struct gl_fragment_program
*program
)
1404 struct prog_instruction instBuffer
[MAX_INSTRUCTIONS
];
1405 struct texenv_fragment_program p
;
1407 struct ureg cf
, out
;
1409 _mesa_memset(&p
, 0, sizeof(p
));
1411 p
.program
= program
;
1413 /* During code generation, use locally-allocated instruction buffer,
1414 * then alloc dynamic storage below.
1416 p
.program
->Base
.Instructions
= instBuffer
;
1417 p
.program
->Base
.Target
= GL_FRAGMENT_PROGRAM_ARB
;
1418 p
.program
->Base
.String
= NULL
;
1419 p
.program
->Base
.NumTexIndirections
= 1; /* is this right? */
1420 p
.program
->Base
.NumTexInstructions
= 0;
1421 p
.program
->Base
.NumAluInstructions
= 0;
1422 p
.program
->Base
.NumInstructions
= 0;
1423 p
.program
->Base
.NumTemporaries
= 0;
1424 p
.program
->Base
.NumParameters
= 0;
1425 p
.program
->Base
.NumAttributes
= 0;
1426 p
.program
->Base
.NumAddressRegs
= 0;
1427 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1428 p
.program
->Base
.InputsRead
= 0x0;
1429 p
.program
->Base
.OutputsWritten
= 1 << FRAG_RESULT_COLOR
;
1431 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
1432 p
.src_texture
[unit
] = undef
;
1433 p
.texcoord_tex
[unit
] = undef
;
1436 p
.src_previous
= undef
;
1441 p
.last_tex_stage
= 0;
1442 release_temps(ctx
, &p
);
1444 if (key
->enabled_units
) {
1445 GLboolean needbumpstage
= GL_FALSE
;
1447 /* Zeroth pass - bump map textures first */
1448 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1449 if (key
->unit
[unit
].enabled
&&
1450 key
->unit
[unit
].ModeRGB
== MODE_BUMP_ENVMAP_ATI
) {
1451 needbumpstage
= GL_TRUE
;
1452 load_texunit_bumpmap( &p
, unit
);
1455 p
.program
->Base
.NumTexIndirections
++;
1457 /* First pass - to support texture_env_crossbar, first identify
1458 * all referenced texture sources and emit texld instructions
1461 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1462 if (key
->unit
[unit
].enabled
) {
1463 load_texunit_sources( &p
, unit
);
1464 p
.last_tex_stage
= unit
;
1467 /* Second pass - emit combine instructions to build final color:
1469 for (unit
= 0; unit
< key
->nr_enabled_units
; unit
++)
1470 if (key
->unit
[unit
].enabled
) {
1471 p
.src_previous
= emit_texenv( &p
, unit
);
1472 reserve_temp(&p
, p
.src_previous
); /* don't re-use this temp reg */
1473 release_temps(ctx
, &p
); /* release all temps */
1477 cf
= get_source( &p
, SRC_PREVIOUS
, 0 );
1478 out
= make_ureg( PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
);
1480 if (key
->separate_specular
) {
1481 /* Emit specular add.
1483 struct ureg s
= register_input(&p
, FRAG_ATTRIB_COL1
);
1484 emit_arith( &p
, OPCODE_ADD
, out
, WRITEMASK_XYZ
, 0, cf
, s
, undef
);
1485 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_W
, 0, cf
, undef
, undef
);
1487 else if (_mesa_memcmp(&cf
, &out
, sizeof(cf
)) != 0) {
1488 /* Will wind up in here if no texture enabled or a couple of
1489 * other scenarios (GL_REPLACE for instance).
1491 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_XYZW
, 0, cf
, undef
, undef
);
1496 emit_arith( &p
, OPCODE_END
, undef
, WRITEMASK_XYZW
, 0, undef
, undef
, undef
);
1498 if (key
->fog_enabled
) {
1499 /* Pull fog mode from GLcontext, the value in the state key is
1500 * a reduced value and not what is expected in FogOption
1502 p
.program
->FogOption
= ctx
->Fog
.Mode
;
1503 p
.program
->Base
.InputsRead
|= FRAG_BIT_FOGC
;
1506 p
.program
->FogOption
= GL_NONE
;
1509 if (p
.program
->Base
.NumTexIndirections
> ctx
->Const
.FragmentProgram
.MaxTexIndirections
)
1510 program_error(&p
, "Exceeded max nr indirect texture lookups");
1512 if (p
.program
->Base
.NumTexInstructions
> ctx
->Const
.FragmentProgram
.MaxTexInstructions
)
1513 program_error(&p
, "Exceeded max TEX instructions");
1515 if (p
.program
->Base
.NumAluInstructions
> ctx
->Const
.FragmentProgram
.MaxAluInstructions
)
1516 program_error(&p
, "Exceeded max ALU instructions");
1518 ASSERT(p
.program
->Base
.NumInstructions
<= MAX_INSTRUCTIONS
);
1520 /* Allocate final instruction array */
1521 p
.program
->Base
.Instructions
1522 = _mesa_alloc_instructions(p
.program
->Base
.NumInstructions
);
1523 if (!p
.program
->Base
.Instructions
) {
1524 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1525 "generating tex env program");
1528 _mesa_copy_instructions(p
.program
->Base
.Instructions
, instBuffer
,
1529 p
.program
->Base
.NumInstructions
);
1531 if (p
.program
->FogOption
) {
1532 _mesa_append_fog_code(ctx
, p
.program
);
1533 p
.program
->FogOption
= GL_NONE
;
1537 /* Notify driver the fragment program has (actually) changed.
1539 if (ctx
->Driver
.ProgramStringNotify
) {
1540 ctx
->Driver
.ProgramStringNotify( ctx
, GL_FRAGMENT_PROGRAM_ARB
,
1545 _mesa_print_program(&p
.program
->Base
);
1552 * Return a fragment program which implements the current
1553 * fixed-function texture, fog and color-sum operations.
1555 struct gl_fragment_program
*
1556 _mesa_get_fixed_func_fragment_program(GLcontext
*ctx
)
1558 struct gl_fragment_program
*prog
;
1559 struct state_key key
;
1562 keySize
= make_state_key(ctx
, &key
);
1564 prog
= (struct gl_fragment_program
*)
1565 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1569 prog
= (struct gl_fragment_program
*)
1570 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
1572 create_new_program(ctx
, &key
, prog
);
1574 _mesa_program_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1575 &key
, keySize
, &prog
->Base
);