1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 #include "simple_list.h"
33 #include "texformat.h"
38 #include "intel_screen.h"
39 #include "intel_tex.h"
41 #include "i830_context.h"
45 /* ================================================================
46 * Texture combine functions
49 pass_through(GLuint
* state
, GLuint blendUnit
)
51 state
[0] = (_3DSTATE_MAP_BLEND_OP_CMD(blendUnit
) |
53 ENABLE_TEXOUTPUT_WRT_SEL
|
54 TEXOP_OUTPUT_CURRENT
|
55 DISABLE_TEX_CNTRL_STAGE
|
56 TEXOP_SCALE_1X
| TEXOP_MODIFY_PARMS
| TEXBLENDOP_ARG1
);
57 state
[1] = (_3DSTATE_MAP_BLEND_OP_CMD(blendUnit
) |
59 ENABLE_TEXOUTPUT_WRT_SEL
|
60 TEXOP_OUTPUT_CURRENT
|
61 TEXOP_SCALE_1X
| TEXOP_MODIFY_PARMS
| TEXBLENDOP_ARG1
);
62 state
[2] = (_3DSTATE_MAP_BLEND_ARG_CMD(blendUnit
) |
65 TEXBLENDARG_MODIFY_PARMS
| TEXBLENDARG_CURRENT
);
66 state
[3] = (_3DSTATE_MAP_BLEND_ARG_CMD(blendUnit
) |
69 TEXBLENDARG_MODIFY_PARMS
| TEXBLENDARG_CURRENT
);
75 emit_factor(GLuint blendUnit
, GLuint
* state
, GLuint count
,
76 const GLfloat
* factor
)
82 fprintf(stderr
, "emit constant %d: %.2f %.2f %.2f %.2f\n",
83 blendUnit
, factor
[0], factor
[1], factor
[2], factor
[3]);
85 UNCLAMPED_FLOAT_TO_UBYTE(r
, factor
[0]);
86 UNCLAMPED_FLOAT_TO_UBYTE(g
, factor
[1]);
87 UNCLAMPED_FLOAT_TO_UBYTE(b
, factor
[2]);
88 UNCLAMPED_FLOAT_TO_UBYTE(a
, factor
[3]);
90 col
= ((a
<< 24) | (r
<< 16) | (g
<< 8) | b
);
92 state
[count
++] = _3DSTATE_COLOR_FACTOR_N_CMD(blendUnit
);
100 GetTexelOp(GLint unit
)
104 return TEXBLENDARG_TEXEL0
;
106 return TEXBLENDARG_TEXEL1
;
108 return TEXBLENDARG_TEXEL2
;
110 return TEXBLENDARG_TEXEL3
;
112 return TEXBLENDARG_TEXEL0
;
118 * Calculate the hardware instuctions to setup the current texture enviromnemt
119 * settings. Since \c gl_texture_unit::_CurrentCombine is used, both
120 * "classic" texture enviroments and GL_ARB_texture_env_combine type texture
121 * environments are treated identically.
124 * This function should return \c GLboolean. When \c GL_FALSE is returned,
125 * it means that an environment is selected that the hardware cannot do. This
126 * is the way the Radeon and R200 drivers work.
129 * Looking at i830_3d_regs.h, it seems the i830 can do part of
130 * GL_ATI_texture_env_combine3. It can handle using \c GL_ONE and
131 * \c GL_ZERO as combine inputs (which the code already supports). It can
132 * also handle the \c GL_MODULATE_ADD_ATI mode. Is it worth investigating
133 * partial support for the extension?
136 i830SetTexEnvCombine(struct i830_context
* i830
,
137 const struct gl_tex_env_combine_state
* combine
,
139 GLuint texel_op
, GLuint
* state
, const GLfloat
* factor
)
141 const GLuint numColorArgs
= combine
->_NumArgsRGB
;
142 const GLuint numAlphaArgs
= combine
->_NumArgsA
;
150 GLboolean need_factor
= 0;
153 static const GLuint tex_blend_rgb
[3] = {
154 TEXPIPE_COLOR
| TEXBLEND_ARG1
| TEXBLENDARG_MODIFY_PARMS
,
155 TEXPIPE_COLOR
| TEXBLEND_ARG2
| TEXBLENDARG_MODIFY_PARMS
,
156 TEXPIPE_COLOR
| TEXBLEND_ARG0
| TEXBLENDARG_MODIFY_PARMS
,
158 static const GLuint tex_blend_a
[3] = {
159 TEXPIPE_ALPHA
| TEXBLEND_ARG1
| TEXBLENDARG_MODIFY_PARMS
,
160 TEXPIPE_ALPHA
| TEXBLEND_ARG2
| TEXBLENDARG_MODIFY_PARMS
,
161 TEXPIPE_ALPHA
| TEXBLEND_ARG0
| TEXBLENDARG_MODIFY_PARMS
,
164 if (INTEL_DEBUG
& DEBUG_TEXTURE
)
165 fprintf(stderr
, "%s\n", __FUNCTION__
);
168 /* The EXT version of the DOT3 extension does not support the
169 * scale factor, but the ARB version (and the version in OpenGL
172 switch (combine
->ModeRGB
) {
173 case GL_DOT3_RGB_EXT
:
174 alpha_shift
= combine
->ScaleShiftA
;
178 case GL_DOT3_RGBA_EXT
:
184 rgb_shift
= combine
->ScaleShiftRGB
;
185 alpha_shift
= combine
->ScaleShiftA
;
190 switch (combine
->ModeRGB
) {
192 blendop
= TEXBLENDOP_ARG1
;
195 blendop
= TEXBLENDOP_MODULATE
;
198 blendop
= TEXBLENDOP_ADD
;
201 blendop
= TEXBLENDOP_ADDSIGNED
;
204 blendop
= TEXBLENDOP_BLEND
;
207 blendop
= TEXBLENDOP_SUBTRACT
;
209 case GL_DOT3_RGB_EXT
:
211 blendop
= TEXBLENDOP_DOT3
;
213 case GL_DOT3_RGBA_EXT
:
215 blendop
= TEXBLENDOP_DOT3
;
218 return pass_through(state
, blendUnit
);
221 blendop
|= (rgb_shift
<< TEXOP_SCALE_SHIFT
);
224 /* Handle RGB args */
225 for (i
= 0; i
< 3; i
++) {
226 switch (combine
->SourceRGB
[i
]) {
228 args_RGB
[i
] = texel_op
;
234 args_RGB
[i
] = GetTexelOp(combine
->SourceRGB
[i
] - GL_TEXTURE0
);
237 args_RGB
[i
] = TEXBLENDARG_FACTOR_N
;
240 case GL_PRIMARY_COLOR
:
241 args_RGB
[i
] = TEXBLENDARG_DIFFUSE
;
244 args_RGB
[i
] = TEXBLENDARG_CURRENT
;
247 return pass_through(state
, blendUnit
);
250 switch (combine
->OperandRGB
[i
]) {
254 case GL_ONE_MINUS_SRC_COLOR
:
255 args_RGB
[i
] |= TEXBLENDARG_INV_ARG
;
258 args_RGB
[i
] |= TEXBLENDARG_REPLICATE_ALPHA
;
260 case GL_ONE_MINUS_SRC_ALPHA
:
261 args_RGB
[i
] |= (TEXBLENDARG_REPLICATE_ALPHA
| TEXBLENDARG_INV_ARG
);
264 return pass_through(state
, blendUnit
);
269 /* Need to knobble the alpha calculations of TEXBLENDOP_DOT4 to
270 * match the spec. Can't use DOT3 as it won't propogate values
271 * into alpha as required:
273 * Note - the global factor is set up with alpha == .5, so
274 * the alpha part of the DOT4 calculation should be zero.
276 if (combine
->ModeRGB
== GL_DOT3_RGBA_EXT
||
277 combine
->ModeRGB
== GL_DOT3_RGBA
) {
278 ablendop
= TEXBLENDOP_DOT4
;
279 args_A
[0] = TEXBLENDARG_FACTOR
; /* the global factor */
280 args_A
[1] = TEXBLENDARG_FACTOR
;
281 args_A
[2] = TEXBLENDARG_FACTOR
;
284 switch (combine
->ModeA
) {
286 ablendop
= TEXBLENDOP_ARG1
;
289 ablendop
= TEXBLENDOP_MODULATE
;
292 ablendop
= TEXBLENDOP_ADD
;
295 ablendop
= TEXBLENDOP_ADDSIGNED
;
298 ablendop
= TEXBLENDOP_BLEND
;
301 ablendop
= TEXBLENDOP_SUBTRACT
;
304 return pass_through(state
, blendUnit
);
308 ablendop
|= (alpha_shift
<< TEXOP_SCALE_SHIFT
);
311 for (i
= 0; i
< 3; i
++) {
312 switch (combine
->SourceA
[i
]) {
314 args_A
[i
] = texel_op
;
320 args_A
[i
] = GetTexelOp(combine
->SourceA
[i
] - GL_TEXTURE0
);
323 args_A
[i
] = TEXBLENDARG_FACTOR_N
;
326 case GL_PRIMARY_COLOR
:
327 args_A
[i
] = TEXBLENDARG_DIFFUSE
;
330 args_A
[i
] = TEXBLENDARG_CURRENT
;
333 return pass_through(state
, blendUnit
);
336 switch (combine
->OperandA
[i
]) {
340 case GL_ONE_MINUS_SRC_ALPHA
:
341 args_A
[i
] |= TEXBLENDARG_INV_ARG
;
344 return pass_through(state
, blendUnit
);
351 /* Native Arg1 == Arg0 in GL_EXT_texture_env_combine spec */
352 /* Native Arg2 == Arg1 in GL_EXT_texture_env_combine spec */
353 /* Native Arg0 == Arg2 in GL_EXT_texture_env_combine spec */
355 /* When we render we need to figure out which is the last really enabled
356 * tex unit, and put last stage on it
360 /* Build color & alpha pipelines */
363 state
[used
++] = (_3DSTATE_MAP_BLEND_OP_CMD(blendUnit
) |
365 ENABLE_TEXOUTPUT_WRT_SEL
|
366 TEXOP_OUTPUT_CURRENT
|
367 DISABLE_TEX_CNTRL_STAGE
| TEXOP_MODIFY_PARMS
| blendop
);
368 state
[used
++] = (_3DSTATE_MAP_BLEND_OP_CMD(blendUnit
) |
370 ENABLE_TEXOUTPUT_WRT_SEL
|
371 TEXOP_OUTPUT_CURRENT
| TEXOP_MODIFY_PARMS
| ablendop
);
373 for (i
= 0; i
< numColorArgs
; i
++) {
374 state
[used
++] = (_3DSTATE_MAP_BLEND_ARG_CMD(blendUnit
) |
375 tex_blend_rgb
[i
] | args_RGB
[i
]);
378 for (i
= 0; i
< numAlphaArgs
; i
++) {
379 state
[used
++] = (_3DSTATE_MAP_BLEND_ARG_CMD(blendUnit
) |
380 tex_blend_a
[i
] | args_A
[i
]);
385 return emit_factor(blendUnit
, state
, used
, factor
);
392 emit_texblend(struct i830_context
*i830
, GLuint unit
, GLuint blendUnit
,
393 GLboolean last_stage
)
395 struct gl_texture_unit
*texUnit
= &i830
->intel
.ctx
.Texture
.Unit
[unit
];
396 GLuint tmp
[I830_TEXBLEND_SIZE
], tmp_sz
;
400 fprintf(stderr
, "%s unit %d\n", __FUNCTION__
, unit
);
402 /* Update i830->state.TexBlend
404 tmp_sz
= i830SetTexEnvCombine(i830
, texUnit
->_CurrentCombine
, blendUnit
,
405 GetTexelOp(unit
), tmp
, texUnit
->EnvColor
);
408 tmp
[0] |= TEXOP_LAST_STAGE
;
410 if (tmp_sz
!= i830
->state
.TexBlendWordsUsed
[blendUnit
] ||
411 memcmp(tmp
, i830
->state
.TexBlend
[blendUnit
],
412 tmp_sz
* sizeof(GLuint
))) {
414 I830_STATECHANGE(i830
, I830_UPLOAD_TEXBLEND(blendUnit
));
415 memcpy(i830
->state
.TexBlend
[blendUnit
], tmp
, tmp_sz
* sizeof(GLuint
));
416 i830
->state
.TexBlendWordsUsed
[blendUnit
] = tmp_sz
;
419 I830_ACTIVESTATE(i830
, I830_UPLOAD_TEXBLEND(blendUnit
), GL_TRUE
);
423 emit_passthrough(struct i830_context
*i830
)
425 GLuint tmp
[I830_TEXBLEND_SIZE
], tmp_sz
;
428 tmp_sz
= pass_through(tmp
, unit
);
429 tmp
[0] |= TEXOP_LAST_STAGE
;
431 if (tmp_sz
!= i830
->state
.TexBlendWordsUsed
[unit
] ||
432 memcmp(tmp
, i830
->state
.TexBlend
[unit
], tmp_sz
* sizeof(GLuint
))) {
434 I830_STATECHANGE(i830
, I830_UPLOAD_TEXBLEND(unit
));
435 memcpy(i830
->state
.TexBlend
[unit
], tmp
, tmp_sz
* sizeof(GLuint
));
436 i830
->state
.TexBlendWordsUsed
[unit
] = tmp_sz
;
439 I830_ACTIVESTATE(i830
, I830_UPLOAD_TEXBLEND(unit
), GL_TRUE
);
443 i830EmitTextureBlend(struct i830_context
*i830
)
445 GLcontext
*ctx
= &i830
->intel
.ctx
;
446 GLuint unit
, last_stage
= 0, blendunit
= 0;
448 I830_ACTIVESTATE(i830
, I830_UPLOAD_TEXBLEND_ALL
, GL_FALSE
);
450 if (ctx
->Texture
._EnabledUnits
) {
451 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
452 if (ctx
->Texture
.Unit
[unit
]._ReallyEnabled
)
455 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
456 if (ctx
->Texture
.Unit
[unit
]._ReallyEnabled
)
457 emit_texblend(i830
, unit
, blendunit
++, last_stage
== unit
);
460 emit_passthrough(i830
);