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"
46 * Note on texture units:
48 * The number of texture units supported by fixed-function fragment
49 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
50 * That's because there's a one-to-one correspondence between texture
51 * coordinates and samplers in fixed-function processing.
53 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
54 * sets of texcoords, so is fixed-function fragment processing.
56 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
60 struct texenvprog_cache_item
64 struct gl_fragment_program
*data
;
65 struct texenvprog_cache_item
*next
;
70 * Up to nine instructions per tex unit, plus fog, specular color.
72 #define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
74 #define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
82 GLuint nr_enabled_units
:8;
83 GLuint enabled_units
:8;
84 GLuint separate_specular
:1;
87 GLuint inputs_available
:12;
91 GLuint source_index
:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
93 GLuint ScaleShiftRGB
:2;
98 struct mode_opt OptRGB
[MAX_TERMS
];
102 struct mode_opt OptA
[MAX_TERMS
];
109 #define FOG_UNKNOWN 3
111 static GLuint
translate_fog_mode( GLenum mode
)
114 case GL_LINEAR
: return FOG_LINEAR
;
115 case GL_EXP
: return FOG_EXP
;
116 case GL_EXP2
: return FOG_EXP2
;
117 default: return FOG_UNKNOWN
;
121 #define OPR_SRC_COLOR 0
122 #define OPR_ONE_MINUS_SRC_COLOR 1
123 #define OPR_SRC_ALPHA 2
124 #define OPR_ONE_MINUS_SRC_ALPHA 3
127 #define OPR_UNKNOWN 7
129 static GLuint
translate_operand( GLenum operand
)
132 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
133 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
134 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
135 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
136 case GL_ZERO
: return OPR_ZERO
;
137 case GL_ONE
: return OPR_ONE
;
144 #define SRC_TEXTURE 0
145 #define SRC_TEXTURE0 1
146 #define SRC_TEXTURE1 2
147 #define SRC_TEXTURE2 3
148 #define SRC_TEXTURE3 4
149 #define SRC_TEXTURE4 5
150 #define SRC_TEXTURE5 6
151 #define SRC_TEXTURE6 7
152 #define SRC_TEXTURE7 8
153 #define SRC_CONSTANT 9
154 #define SRC_PRIMARY_COLOR 10
155 #define SRC_PREVIOUS 11
157 #define SRC_UNKNOWN 15
159 static GLuint
translate_source( GLenum src
)
162 case GL_TEXTURE
: return SRC_TEXTURE
;
170 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
171 case GL_CONSTANT
: return SRC_CONSTANT
;
172 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
173 case GL_PREVIOUS
: return SRC_PREVIOUS
;
182 #define MODE_REPLACE 0 /* r = a0 */
183 #define MODE_MODULATE 1 /* r = a0 * a1 */
184 #define MODE_ADD 2 /* r = a0 + a1 */
185 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
186 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
187 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
188 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
189 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
190 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
191 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
192 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
193 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
194 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
195 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
196 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
197 #define MODE_UNKNOWN 15
200 * Translate GL combiner state into a MODE_x value
202 static GLuint
translate_mode( GLenum envMode
, GLenum mode
)
205 case GL_REPLACE
: return MODE_REPLACE
;
206 case GL_MODULATE
: return MODE_MODULATE
;
208 if (envMode
== GL_COMBINE4_NV
)
209 return MODE_ADD_PRODUCTS
;
213 if (envMode
== GL_COMBINE4_NV
)
214 return MODE_ADD_PRODUCTS_SIGNED
;
216 return MODE_ADD_SIGNED
;
217 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
218 case GL_SUBTRACT
: return MODE_SUBTRACT
;
219 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
220 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
221 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
222 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
223 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
224 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
225 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
232 #define TEXTURE_UNKNOWN_INDEX 7
233 static GLuint
translate_tex_src_bit( GLbitfield bit
)
235 /* make sure number of switch cases is correct */
236 assert(NUM_TEXTURE_TARGETS
== 7);
238 case TEXTURE_1D_BIT
: return TEXTURE_1D_INDEX
;
239 case TEXTURE_2D_BIT
: return TEXTURE_2D_INDEX
;
240 case TEXTURE_RECT_BIT
: return TEXTURE_RECT_INDEX
;
241 case TEXTURE_3D_BIT
: return TEXTURE_3D_INDEX
;
242 case TEXTURE_CUBE_BIT
: return TEXTURE_CUBE_INDEX
;
243 case TEXTURE_1D_ARRAY_BIT
: return TEXTURE_1D_ARRAY_INDEX
;
244 case TEXTURE_2D_ARRAY_BIT
: return TEXTURE_2D_ARRAY_INDEX
;
247 return TEXTURE_UNKNOWN_INDEX
;
251 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
252 #define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
255 * Identify all possible varying inputs. The fragment program will
256 * never reference non-varying inputs, but will track them via state
259 * This function figures out all the inputs that the fragment program
260 * has access to. The bitmask is later reduced to just those which
261 * are actually referenced.
263 static GLbitfield
get_fp_input_mask( GLcontext
*ctx
)
265 const GLboolean vertexShader
= (ctx
->Shader
.CurrentProgram
&&
266 ctx
->Shader
.CurrentProgram
->VertexProgram
);
267 const GLboolean vertexProgram
= ctx
->VertexProgram
._Enabled
;
268 GLbitfield fp_inputs
= 0x0;
270 if (ctx
->VertexProgram
._Overriden
) {
271 /* Somebody's messing with the vertex program and we don't have
272 * a clue what's happening. Assume that it could be producing
273 * all possible outputs.
277 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
278 fp_inputs
= (FRAG_BIT_COL0
| FRAG_BIT_TEX0
);
280 else if (!(vertexProgram
|| vertexShader
) ||
281 !ctx
->VertexProgram
._Current
) {
282 /* Fixed function vertex logic */
283 GLbitfield varying_inputs
= ctx
->varying_vp_inputs
;
285 /* These get generated in the setup routine regardless of the
288 if (ctx
->Point
.PointSprite
)
289 varying_inputs
|= FRAG_BITS_TEX_ANY
;
291 /* First look at what values may be computed by the generated
294 if (ctx
->Light
.Enabled
) {
295 fp_inputs
|= FRAG_BIT_COL0
;
297 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
)
298 fp_inputs
|= FRAG_BIT_COL1
;
301 fp_inputs
|= (ctx
->Texture
._TexGenEnabled
|
302 ctx
->Texture
._TexMatEnabled
) << FRAG_ATTRIB_TEX0
;
304 /* Then look at what might be varying as a result of enabled
307 if (varying_inputs
& VERT_BIT_COLOR0
) fp_inputs
|= FRAG_BIT_COL0
;
308 if (varying_inputs
& VERT_BIT_COLOR1
) fp_inputs
|= FRAG_BIT_COL1
;
310 fp_inputs
|= (((varying_inputs
& VERT_BIT_TEX_ANY
) >> VERT_ATTRIB_TEX0
)
311 << FRAG_ATTRIB_TEX0
);
315 /* calculate from vp->outputs */
316 struct gl_vertex_program
*vprog
;
317 GLbitfield vp_outputs
;
319 /* Choose GLSL vertex shader over ARB vertex program. Need this
320 * since vertex shader state validation comes after fragment state
321 * validation (see additional comments in state.c).
324 vprog
= ctx
->Shader
.CurrentProgram
->VertexProgram
;
326 vprog
= ctx
->VertexProgram
._Current
;
328 vp_outputs
= vprog
->Base
.OutputsWritten
;
330 /* These get generated in the setup routine regardless of the
333 if (ctx
->Point
.PointSprite
)
334 vp_outputs
|= FRAG_BITS_TEX_ANY
;
336 if (vp_outputs
& (1 << VERT_RESULT_COL0
)) fp_inputs
|= FRAG_BIT_COL0
;
337 if (vp_outputs
& (1 << VERT_RESULT_COL1
)) fp_inputs
|= FRAG_BIT_COL1
;
339 fp_inputs
|= (((vp_outputs
& VERT_RESULT_TEX_ANY
) >> VERT_RESULT_TEX0
)
340 << FRAG_ATTRIB_TEX0
);
348 * Examine current texture environment state and generate a unique
349 * key to identify it.
351 static void make_state_key( GLcontext
*ctx
, struct state_key
*key
)
354 GLbitfield inputs_referenced
= FRAG_BIT_COL0
;
355 GLbitfield inputs_available
= get_fp_input_mask( ctx
);
357 memset(key
, 0, sizeof(*key
));
359 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
360 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
363 if (!texUnit
->_ReallyEnabled
|| !texUnit
->Enabled
)
366 format
= texUnit
->_Current
->Image
[0][texUnit
->_Current
->BaseLevel
]->_BaseFormat
;
368 key
->unit
[i
].enabled
= 1;
369 key
->enabled_units
|= (1<<i
);
370 key
->nr_enabled_units
= i
+1;
371 inputs_referenced
|= FRAG_BIT_TEX(i
);
373 key
->unit
[i
].source_index
=
374 translate_tex_src_bit(texUnit
->_ReallyEnabled
);
375 key
->unit
[i
].shadow
= ((texUnit
->_Current
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
376 ((format
== GL_DEPTH_COMPONENT
) ||
377 (format
== GL_DEPTH_STENCIL_EXT
)));
379 key
->unit
[i
].NumArgsRGB
= texUnit
->_CurrentCombine
->_NumArgsRGB
;
380 key
->unit
[i
].NumArgsA
= texUnit
->_CurrentCombine
->_NumArgsA
;
382 key
->unit
[i
].ModeRGB
=
383 translate_mode(texUnit
->EnvMode
, texUnit
->_CurrentCombine
->ModeRGB
);
385 translate_mode(texUnit
->EnvMode
, texUnit
->_CurrentCombine
->ModeA
);
387 key
->unit
[i
].ScaleShiftRGB
= texUnit
->_CurrentCombine
->ScaleShiftRGB
;
388 key
->unit
[i
].ScaleShiftA
= texUnit
->_CurrentCombine
->ScaleShiftA
;
390 for (j
= 0; j
< MAX_TERMS
; j
++) {
391 key
->unit
[i
].OptRGB
[j
].Operand
=
392 translate_operand(texUnit
->_CurrentCombine
->OperandRGB
[j
]);
393 key
->unit
[i
].OptA
[j
].Operand
=
394 translate_operand(texUnit
->_CurrentCombine
->OperandA
[j
]);
395 key
->unit
[i
].OptRGB
[j
].Source
=
396 translate_source(texUnit
->_CurrentCombine
->SourceRGB
[j
]);
397 key
->unit
[i
].OptA
[j
].Source
=
398 translate_source(texUnit
->_CurrentCombine
->SourceA
[j
]);
402 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
403 key
->separate_specular
= 1;
404 inputs_referenced
|= FRAG_BIT_COL1
;
407 if (ctx
->Fog
.Enabled
) {
408 key
->fog_enabled
= 1;
409 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
410 inputs_referenced
|= FRAG_BIT_FOGC
; /* maybe */
413 key
->inputs_available
= (inputs_available
& inputs_referenced
);
417 * Use uregs to represent registers internally, translate to Mesa's
418 * expected formats on emit.
420 * NOTE: These are passed by value extensively in this file rather
421 * than as usual by pointer reference. If this disturbs you, try
422 * remembering they are just 32bits in size.
424 * GCC is smart enough to deal with these dword-sized structures in
425 * much the same way as if I had defined them as dwords and was using
426 * macros to access and set the fields. This is much nicer and easier
439 static const struct ureg undef
= {
450 /** State used to build the fragment program:
452 struct texenv_fragment_program
{
453 struct gl_fragment_program
*program
;
455 struct state_key
*state
;
457 GLbitfield alu_temps
; /**< Track texture indirections, see spec. */
458 GLbitfield temps_output
; /**< Track texture indirections, see spec. */
459 GLbitfield temp_in_use
; /**< Tracks temporary regs which are in use. */
462 struct ureg src_texture
[MAX_TEXTURE_COORD_UNITS
];
463 /* Reg containing each texture unit's sampled texture color,
467 struct ureg src_previous
; /**< Reg containing color from previous
468 * stage. May need to be decl'd.
471 GLuint last_tex_stage
; /**< Number of last enabled texture unit */
480 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
488 reg
.swz
= SWIZZLE_NOOP
;
493 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
495 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
498 GET_SWZ(reg
.swz
, w
));
503 static struct ureg
swizzle1( struct ureg reg
, int x
)
505 return swizzle(reg
, x
, x
, x
, x
);
508 static struct ureg
negate( struct ureg reg
)
514 static GLboolean
is_undef( struct ureg reg
)
516 return reg
.file
== PROGRAM_UNDEFINED
;
520 static struct ureg
get_temp( struct texenv_fragment_program
*p
)
524 /* First try and reuse temps which have been used already:
526 bit
= _mesa_ffs( ~p
->temp_in_use
& p
->alu_temps
);
528 /* Then any unused temporary:
531 bit
= _mesa_ffs( ~p
->temp_in_use
);
534 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
538 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
539 p
->program
->Base
.NumTemporaries
= bit
;
541 p
->temp_in_use
|= 1<<(bit
-1);
542 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
545 static struct ureg
get_tex_temp( struct texenv_fragment_program
*p
)
549 /* First try to find available temp not previously used (to avoid
550 * starting a new texture indirection). According to the spec, the
551 * ~p->temps_output isn't necessary, but will keep it there for
554 bit
= _mesa_ffs( ~p
->temp_in_use
& ~p
->alu_temps
& ~p
->temps_output
);
556 /* Then any unused temporary:
559 bit
= _mesa_ffs( ~p
->temp_in_use
);
562 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
566 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
567 p
->program
->Base
.NumTemporaries
= bit
;
569 p
->temp_in_use
|= 1<<(bit
-1);
570 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
574 /** Mark a temp reg as being no longer allocatable. */
575 static void reserve_temp( struct texenv_fragment_program
*p
, struct ureg r
)
577 if (r
.file
== PROGRAM_TEMPORARY
)
578 p
->temps_output
|= (1 << r
.idx
);
582 static void release_temps(GLcontext
*ctx
, struct texenv_fragment_program
*p
)
584 GLuint max_temp
= ctx
->Const
.FragmentProgram
.MaxTemps
;
586 /* KW: To support tex_env_crossbar, don't release the registers in
589 if (max_temp
>= sizeof(int) * 8)
590 p
->temp_in_use
= p
->temps_output
;
592 p
->temp_in_use
= ~((1<<max_temp
)-1) | p
->temps_output
;
596 static struct ureg
register_param5( struct texenv_fragment_program
*p
,
603 gl_state_index tokens
[STATE_LENGTH
];
610 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
611 return make_ureg(PROGRAM_STATE_VAR
, idx
);
615 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
616 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
617 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
618 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
620 static GLuint
frag_to_vert_attrib( GLuint attrib
)
623 case FRAG_ATTRIB_COL0
: return VERT_ATTRIB_COLOR0
;
624 case FRAG_ATTRIB_COL1
: return VERT_ATTRIB_COLOR1
;
626 assert(attrib
>= FRAG_ATTRIB_TEX0
);
627 assert(attrib
<= FRAG_ATTRIB_TEX7
);
628 return attrib
- FRAG_ATTRIB_TEX0
+ VERT_ATTRIB_TEX0
;
633 static struct ureg
register_input( struct texenv_fragment_program
*p
, GLuint input
)
635 if (p
->state
->inputs_available
& (1<<input
)) {
636 p
->program
->Base
.InputsRead
|= (1 << input
);
637 return make_ureg(PROGRAM_INPUT
, input
);
640 GLuint idx
= frag_to_vert_attrib( input
);
641 return register_param3( p
, STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, idx
);
646 static void emit_arg( struct prog_src_register
*reg
,
649 reg
->File
= ureg
.file
;
650 reg
->Index
= ureg
.idx
;
651 reg
->Swizzle
= ureg
.swz
;
652 reg
->NegateBase
= ureg
.negatebase
? 0xf : 0x0;
654 reg
->NegateAbs
= ureg
.negateabs
;
657 static void emit_dst( struct prog_dst_register
*dst
,
658 struct ureg ureg
, GLuint mask
)
660 dst
->File
= ureg
.file
;
661 dst
->Index
= ureg
.idx
;
662 dst
->WriteMask
= mask
;
663 dst
->CondMask
= COND_TR
; /* always pass cond test */
664 dst
->CondSwizzle
= SWIZZLE_NOOP
;
667 static struct prog_instruction
*
668 emit_op(struct texenv_fragment_program
*p
,
677 GLuint nr
= p
->program
->Base
.NumInstructions
++;
678 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
680 assert(nr
< MAX_INSTRUCTIONS
);
682 _mesa_init_instructions(inst
, 1);
685 emit_arg( &inst
->SrcReg
[0], src0
);
686 emit_arg( &inst
->SrcReg
[1], src1
);
687 emit_arg( &inst
->SrcReg
[2], src2
);
689 inst
->SaturateMode
= saturate
? SATURATE_ZERO_ONE
: SATURATE_OFF
;
691 emit_dst( &inst
->DstReg
, dest
, mask
);
694 /* Accounting for indirection tracking:
696 if (dest
.file
== PROGRAM_TEMPORARY
)
697 p
->temps_output
|= 1 << dest
.idx
;
704 static struct ureg
emit_arith( struct texenv_fragment_program
*p
,
713 emit_op(p
, op
, dest
, mask
, saturate
, src0
, src1
, src2
);
715 /* Accounting for indirection tracking:
717 if (src0
.file
== PROGRAM_TEMPORARY
)
718 p
->alu_temps
|= 1 << src0
.idx
;
720 if (!is_undef(src1
) && src1
.file
== PROGRAM_TEMPORARY
)
721 p
->alu_temps
|= 1 << src1
.idx
;
723 if (!is_undef(src2
) && src2
.file
== PROGRAM_TEMPORARY
)
724 p
->alu_temps
|= 1 << src2
.idx
;
726 if (dest
.file
== PROGRAM_TEMPORARY
)
727 p
->alu_temps
|= 1 << dest
.idx
;
729 p
->program
->Base
.NumAluInstructions
++;
733 static struct ureg
emit_texld( struct texenv_fragment_program
*p
,
742 struct prog_instruction
*inst
= emit_op( p
, op
,
744 GL_FALSE
, /* don't saturate? */
749 inst
->TexSrcTarget
= tex_idx
;
750 inst
->TexSrcUnit
= tex_unit
;
751 inst
->TexShadow
= tex_shadow
;
753 p
->program
->Base
.NumTexInstructions
++;
755 /* Accounting for indirection tracking:
757 reserve_temp(p
, dest
);
759 /* Is this a texture indirection?
761 if ((coord
.file
== PROGRAM_TEMPORARY
&&
762 (p
->temps_output
& (1<<coord
.idx
))) ||
763 (dest
.file
== PROGRAM_TEMPORARY
&&
764 (p
->alu_temps
& (1<<dest
.idx
)))) {
765 p
->program
->Base
.NumTexIndirections
++;
766 p
->temps_output
= 1<<coord
.idx
;
768 assert(0); /* KW: texture env crossbar */
775 static struct ureg
register_const4f( struct texenv_fragment_program
*p
,
788 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
790 r
= make_ureg(PROGRAM_CONSTANT
, idx
);
795 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
796 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
797 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
798 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
801 static struct ureg
get_one( struct texenv_fragment_program
*p
)
803 if (is_undef(p
->one
))
804 p
->one
= register_scalar_const(p
, 1.0);
808 static struct ureg
get_half( struct texenv_fragment_program
*p
)
810 if (is_undef(p
->half
))
811 p
->half
= register_scalar_const(p
, 0.5);
815 static struct ureg
get_zero( struct texenv_fragment_program
*p
)
817 if (is_undef(p
->zero
))
818 p
->zero
= register_scalar_const(p
, 0.0);
823 static void program_error( struct texenv_fragment_program
*p
, const char *msg
)
825 _mesa_problem(NULL
, msg
);
829 static struct ureg
get_source( struct texenv_fragment_program
*p
,
830 GLuint src
, GLuint unit
)
834 assert(!is_undef(p
->src_texture
[unit
]));
835 return p
->src_texture
[unit
];
845 assert(!is_undef(p
->src_texture
[src
- SRC_TEXTURE0
]));
846 return p
->src_texture
[src
- SRC_TEXTURE0
];
849 return register_param2(p
, STATE_TEXENV_COLOR
, unit
);
851 case SRC_PRIMARY_COLOR
:
852 return register_input(p
, FRAG_ATTRIB_COL0
);
858 if (is_undef(p
->src_previous
))
859 return register_input(p
, FRAG_ATTRIB_COL0
);
861 return p
->src_previous
;
868 static struct ureg
emit_combine_source( struct texenv_fragment_program
*p
,
874 struct ureg arg
, src
, one
;
876 src
= get_source(p
, source
, unit
);
879 case OPR_ONE_MINUS_SRC_COLOR
:
881 * Emit tmp = 1.0 - arg.xyzw
885 return emit_arith( p
, OPCODE_SUB
, arg
, mask
, 0, one
, src
, undef
);
888 if (mask
== WRITEMASK_W
)
891 return swizzle1( src
, SWIZZLE_W
);
892 case OPR_ONE_MINUS_SRC_ALPHA
:
894 * Emit tmp = 1.0 - arg.wwww
898 return emit_arith(p
, OPCODE_SUB
, arg
, mask
, 0,
899 one
, swizzle1(src
, SWIZZLE_W
), undef
);
912 static GLboolean
args_match( struct state_key
*key
, GLuint unit
)
914 GLuint i
, nr
= key
->unit
[unit
].NumArgsRGB
;
916 for (i
= 0 ; i
< nr
; i
++) {
917 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
920 switch(key
->unit
[unit
].OptA
[i
].Operand
) {
922 switch(key
->unit
[unit
].OptRGB
[i
].Operand
) {
930 case OPR_ONE_MINUS_SRC_ALPHA
:
931 switch(key
->unit
[unit
].OptRGB
[i
].Operand
) {
932 case OPR_ONE_MINUS_SRC_COLOR
:
933 case OPR_ONE_MINUS_SRC_ALPHA
:
940 return GL_FALSE
; /* impossible */
947 static struct ureg
emit_combine( struct texenv_fragment_program
*p
,
954 const struct mode_opt
*opt
)
956 struct ureg src
[MAX_TERMS
];
957 struct ureg tmp
, half
;
960 assert(nr
<= MAX_TERMS
);
962 tmp
= undef
; /* silence warning (bug 5318) */
964 for (i
= 0; i
< nr
; i
++)
965 src
[i
] = emit_combine_source( p
, mask
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
969 if (mask
== WRITEMASK_XYZW
&& !saturate
)
972 return emit_arith( p
, OPCODE_MOV
, dest
, mask
, saturate
, src
[0], undef
, undef
);
974 return emit_arith( p
, OPCODE_MUL
, dest
, mask
, saturate
,
975 src
[0], src
[1], undef
);
977 return emit_arith( p
, OPCODE_ADD
, dest
, mask
, saturate
,
978 src
[0], src
[1], undef
);
979 case MODE_ADD_SIGNED
:
985 emit_arith( p
, OPCODE_ADD
, tmp
, mask
, 0, src
[0], src
[1], undef
);
986 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp
, half
, undef
);
988 case MODE_INTERPOLATE
:
989 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
991 return emit_arith( p
, OPCODE_LRP
, dest
, mask
, saturate
, src
[2], src
[0], src
[1] );
994 return emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, src
[0], src
[1], undef
);
997 case MODE_DOT3_RGBA_EXT
:
998 case MODE_DOT3_RGB_EXT
:
999 case MODE_DOT3_RGB
: {
1000 struct ureg tmp0
= get_temp( p
);
1001 struct ureg tmp1
= get_temp( p
);
1002 struct ureg neg1
= register_scalar_const(p
, -1);
1003 struct ureg two
= register_scalar_const(p
, 2);
1005 /* tmp0 = 2*src0 - 1
1008 * dst = tmp0 dot3 tmp1
1010 emit_arith( p
, OPCODE_MAD
, tmp0
, WRITEMASK_XYZW
, 0,
1013 if (_mesa_memcmp(&src
[0], &src
[1], sizeof(struct ureg
)) == 0)
1016 emit_arith( p
, OPCODE_MAD
, tmp1
, WRITEMASK_XYZW
, 0,
1018 emit_arith( p
, OPCODE_DP3
, dest
, mask
, saturate
, tmp0
, tmp1
, undef
);
1021 case MODE_MODULATE_ADD_ATI
:
1022 /* Arg0 * Arg2 + Arg1 */
1023 return emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
,
1024 src
[0], src
[2], src
[1] );
1025 case MODE_MODULATE_SIGNED_ADD_ATI
: {
1026 /* Arg0 * Arg2 + Arg1 - 0.5 */
1027 struct ureg tmp0
= get_temp(p
);
1029 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[0], src
[2], src
[1] );
1030 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
1033 case MODE_MODULATE_SUBTRACT_ATI
:
1034 /* Arg0 * Arg2 - Arg1 */
1035 emit_arith( p
, OPCODE_MAD
, dest
, mask
, 0, src
[0], src
[2], negate(src
[1]) );
1037 case MODE_ADD_PRODUCTS
:
1038 /* Arg0 * Arg1 + Arg2 * Arg3 */
1040 struct ureg tmp0
= get_temp(p
);
1041 emit_arith( p
, OPCODE_MUL
, tmp0
, mask
, 0, src
[0], src
[1], undef
);
1042 emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
, src
[2], src
[3], tmp0
);
1045 case MODE_ADD_PRODUCTS_SIGNED
:
1046 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1048 struct ureg tmp0
= get_temp(p
);
1050 emit_arith( p
, OPCODE_MUL
, tmp0
, mask
, 0, src
[0], src
[1], undef
);
1051 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[2], src
[3], tmp0
);
1052 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
1063 * Generate instructions for one texture unit's env/combiner mode.
1066 emit_texenv(struct texenv_fragment_program
*p
, GLuint unit
)
1068 struct state_key
*key
= p
->state
;
1069 GLboolean saturate
= (unit
< p
->last_tex_stage
);
1070 GLuint rgb_shift
, alpha_shift
;
1071 struct ureg out
, shift
;
1074 if (!key
->unit
[unit
].enabled
) {
1075 return get_source(p
, SRC_PREVIOUS
, 0);
1078 switch (key
->unit
[unit
].ModeRGB
) {
1079 case MODE_DOT3_RGB_EXT
:
1080 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
1083 case MODE_DOT3_RGBA_EXT
:
1088 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
1089 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
1093 /* If this is the very last calculation, emit direct to output reg:
1095 if (key
->separate_specular
||
1096 unit
!= p
->last_tex_stage
||
1099 dest
= get_temp( p
);
1101 dest
= make_ureg(PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
);
1103 /* Emit the RGB and A combine ops
1105 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
1106 args_match(key
, unit
)) {
1107 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
1109 key
->unit
[unit
].NumArgsRGB
,
1110 key
->unit
[unit
].ModeRGB
,
1111 key
->unit
[unit
].OptRGB
);
1113 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
1114 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
1116 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
1118 key
->unit
[unit
].NumArgsRGB
,
1119 key
->unit
[unit
].ModeRGB
,
1120 key
->unit
[unit
].OptRGB
);
1123 /* Need to do something to stop from re-emitting identical
1124 * argument calculations here:
1126 out
= emit_combine( p
, dest
, WRITEMASK_XYZ
, saturate
,
1128 key
->unit
[unit
].NumArgsRGB
,
1129 key
->unit
[unit
].ModeRGB
,
1130 key
->unit
[unit
].OptRGB
);
1131 out
= emit_combine( p
, dest
, WRITEMASK_W
, saturate
,
1133 key
->unit
[unit
].NumArgsA
,
1134 key
->unit
[unit
].ModeA
,
1135 key
->unit
[unit
].OptA
);
1138 /* Deal with the final shift:
1140 if (alpha_shift
|| rgb_shift
) {
1141 if (rgb_shift
== alpha_shift
) {
1142 shift
= register_scalar_const(p
, (GLfloat
)(1<<rgb_shift
));
1145 shift
= register_const4f(p
,
1146 (GLfloat
)(1<<rgb_shift
),
1147 (GLfloat
)(1<<rgb_shift
),
1148 (GLfloat
)(1<<rgb_shift
),
1149 (GLfloat
)(1<<alpha_shift
));
1151 return emit_arith( p
, OPCODE_MUL
, dest
, WRITEMASK_XYZW
,
1152 saturate
, out
, shift
, undef
);
1160 * Generate instruction for getting a texture source term.
1162 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
1164 if (is_undef(p
->src_texture
[unit
])) {
1165 GLuint texTarget
= p
->state
->unit
[unit
].source_index
;
1166 struct ureg texcoord
= register_input(p
, FRAG_ATTRIB_TEX0
+unit
);
1167 struct ureg tmp
= get_tex_temp( p
);
1169 if (texTarget
== TEXTURE_UNKNOWN_INDEX
)
1170 program_error(p
, "TexSrcBit");
1172 /* TODO: Use D0_MASK_XY where possible.
1174 if (p
->state
->unit
[unit
].enabled
) {
1175 GLboolean shadow
= GL_FALSE
;
1177 if (p
->state
->unit
[unit
].shadow
) {
1178 p
->program
->Base
.ShadowSamplers
|= 1 << unit
;
1182 p
->src_texture
[unit
] = emit_texld( p
, OPCODE_TXP
,
1183 tmp
, WRITEMASK_XYZW
,
1184 unit
, texTarget
, shadow
,
1187 p
->program
->Base
.SamplersUsed
|= (1 << unit
);
1188 /* This identity mapping should already be in place
1189 * (see _mesa_init_program_struct()) but let's be safe.
1191 p
->program
->Base
.SamplerUnits
[unit
] = unit
;
1194 p
->src_texture
[unit
] = get_zero(p
);
1198 static GLboolean
load_texenv_source( struct texenv_fragment_program
*p
,
1199 GLuint src
, GLuint unit
)
1203 load_texture(p
, unit
);
1214 load_texture(p
, src
- SRC_TEXTURE0
);
1218 /* not a texture src - do nothing */
1227 * Generate instructions for loading all texture source terms.
1230 load_texunit_sources( struct texenv_fragment_program
*p
, int unit
)
1232 struct state_key
*key
= p
->state
;
1235 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1236 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1239 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1240 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1248 * Generate a new fragment program which implements the context's
1249 * current texture env/combine mode.
1252 create_new_program(GLcontext
*ctx
, struct state_key
*key
,
1253 struct gl_fragment_program
*program
)
1255 struct prog_instruction instBuffer
[MAX_INSTRUCTIONS
];
1256 struct texenv_fragment_program p
;
1258 struct ureg cf
, out
;
1260 _mesa_memset(&p
, 0, sizeof(p
));
1263 p
.program
= program
;
1265 /* During code generation, use locally-allocated instruction buffer,
1266 * then alloc dynamic storage below.
1268 p
.program
->Base
.Instructions
= instBuffer
;
1269 p
.program
->Base
.Target
= GL_FRAGMENT_PROGRAM_ARB
;
1270 p
.program
->Base
.NumTexIndirections
= 1; /* correct? */
1271 p
.program
->Base
.NumTexInstructions
= 0;
1272 p
.program
->Base
.NumAluInstructions
= 0;
1273 p
.program
->Base
.String
= NULL
;
1274 p
.program
->Base
.NumInstructions
=
1275 p
.program
->Base
.NumTemporaries
=
1276 p
.program
->Base
.NumParameters
=
1277 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1278 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1280 p
.program
->Base
.InputsRead
= 0;
1281 p
.program
->Base
.OutputsWritten
= 1 << FRAG_RESULT_COLOR
;
1283 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1284 p
.src_texture
[unit
] = undef
;
1286 p
.src_previous
= undef
;
1291 p
.last_tex_stage
= 0;
1292 release_temps(ctx
, &p
);
1294 if (key
->enabled_units
) {
1295 /* First pass - to support texture_env_crossbar, first identify
1296 * all referenced texture sources and emit texld instructions
1299 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1300 if (key
->unit
[unit
].enabled
) {
1301 load_texunit_sources( &p
, unit
);
1302 p
.last_tex_stage
= unit
;
1305 /* Second pass - emit combine instructions to build final color:
1307 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1308 if (key
->enabled_units
& (1<<unit
)) {
1309 p
.src_previous
= emit_texenv( &p
, unit
);
1310 reserve_temp(&p
, p
.src_previous
); /* don't re-use this temp reg */
1311 release_temps(ctx
, &p
); /* release all temps */
1315 cf
= get_source( &p
, SRC_PREVIOUS
, 0 );
1316 out
= make_ureg( PROGRAM_OUTPUT
, FRAG_RESULT_COLOR
);
1318 if (key
->separate_specular
) {
1319 /* Emit specular add.
1321 struct ureg s
= register_input(&p
, FRAG_ATTRIB_COL1
);
1322 emit_arith( &p
, OPCODE_ADD
, out
, WRITEMASK_XYZ
, 0, cf
, s
, undef
);
1323 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_W
, 0, cf
, undef
, undef
);
1325 else if (_mesa_memcmp(&cf
, &out
, sizeof(cf
)) != 0) {
1326 /* Will wind up in here if no texture enabled or a couple of
1327 * other scenarios (GL_REPLACE for instance).
1329 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_XYZW
, 0, cf
, undef
, undef
);
1334 emit_arith( &p
, OPCODE_END
, undef
, WRITEMASK_XYZW
, 0, undef
, undef
, undef
);
1336 if (key
->fog_enabled
) {
1337 /* Pull fog mode from GLcontext, the value in the state key is
1338 * a reduced value and not what is expected in FogOption
1340 p
.program
->FogOption
= ctx
->Fog
.Mode
;
1341 p
.program
->Base
.InputsRead
|= FRAG_BIT_FOGC
; /* XXX new */
1343 p
.program
->FogOption
= GL_NONE
;
1345 if (p
.program
->Base
.NumTexIndirections
> ctx
->Const
.FragmentProgram
.MaxTexIndirections
)
1346 program_error(&p
, "Exceeded max nr indirect texture lookups");
1348 if (p
.program
->Base
.NumTexInstructions
> ctx
->Const
.FragmentProgram
.MaxTexInstructions
)
1349 program_error(&p
, "Exceeded max TEX instructions");
1351 if (p
.program
->Base
.NumAluInstructions
> ctx
->Const
.FragmentProgram
.MaxAluInstructions
)
1352 program_error(&p
, "Exceeded max ALU instructions");
1354 ASSERT(p
.program
->Base
.NumInstructions
<= MAX_INSTRUCTIONS
);
1356 /* Allocate final instruction array */
1357 p
.program
->Base
.Instructions
1358 = _mesa_alloc_instructions(p
.program
->Base
.NumInstructions
);
1359 if (!p
.program
->Base
.Instructions
) {
1360 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1361 "generating tex env program");
1364 _mesa_copy_instructions(p
.program
->Base
.Instructions
, instBuffer
,
1365 p
.program
->Base
.NumInstructions
);
1367 if (p
.program
->FogOption
) {
1368 _mesa_append_fog_code(ctx
, p
.program
);
1369 p
.program
->FogOption
= GL_NONE
;
1373 /* Notify driver the fragment program has (actually) changed.
1375 if (ctx
->Driver
.ProgramStringNotify
) {
1376 ctx
->Driver
.ProgramStringNotify( ctx
, GL_FRAGMENT_PROGRAM_ARB
,
1381 _mesa_print_program(&p
.program
->Base
);
1388 * Return a fragment program which implements the current
1389 * fixed-function texture, fog and color-sum operations.
1391 struct gl_fragment_program
*
1392 _mesa_get_fixed_func_fragment_program(GLcontext
*ctx
)
1394 struct gl_fragment_program
*prog
;
1395 struct state_key key
;
1397 make_state_key(ctx
, &key
);
1399 prog
= (struct gl_fragment_program
*)
1400 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1404 prog
= (struct gl_fragment_program
*)
1405 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
1407 create_new_program(ctx
, &key
, prog
);
1409 _mesa_program_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1410 &key
, sizeof(key
), &prog
->Base
);