1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
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 VMWARE 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 **************************************************************************/
28 #include "main/glheader.h"
29 #include "main/macros.h"
30 #include "main/mtypes.h"
31 #include "main/enums.h"
33 #include "intel_screen.h"
34 #include "intel_tex.h"
36 #include "i830_context.h"
40 /* ================================================================
41 * Texture combine functions
44 pass_through(GLuint
* state
, GLuint blendUnit
)
46 state
[0] = (_3DSTATE_MAP_BLEND_OP_CMD(blendUnit
) |
48 ENABLE_TEXOUTPUT_WRT_SEL
|
49 TEXOP_OUTPUT_CURRENT
|
50 DISABLE_TEX_CNTRL_STAGE
|
51 TEXOP_SCALE_1X
| TEXOP_MODIFY_PARMS
| TEXBLENDOP_ARG1
);
52 state
[1] = (_3DSTATE_MAP_BLEND_OP_CMD(blendUnit
) |
54 ENABLE_TEXOUTPUT_WRT_SEL
|
55 TEXOP_OUTPUT_CURRENT
|
56 TEXOP_SCALE_1X
| TEXOP_MODIFY_PARMS
| TEXBLENDOP_ARG1
);
57 state
[2] = (_3DSTATE_MAP_BLEND_ARG_CMD(blendUnit
) |
60 TEXBLENDARG_MODIFY_PARMS
| TEXBLENDARG_CURRENT
);
61 state
[3] = (_3DSTATE_MAP_BLEND_ARG_CMD(blendUnit
) |
64 TEXBLENDARG_MODIFY_PARMS
| TEXBLENDARG_CURRENT
);
70 emit_factor(GLuint blendUnit
, GLuint
* state
, GLuint count
,
71 const GLfloat
* factor
)
77 fprintf(stderr
, "emit constant %d: %.2f %.2f %.2f %.2f\n",
78 blendUnit
, factor
[0], factor
[1], factor
[2], factor
[3]);
80 UNCLAMPED_FLOAT_TO_UBYTE(r
, factor
[0]);
81 UNCLAMPED_FLOAT_TO_UBYTE(g
, factor
[1]);
82 UNCLAMPED_FLOAT_TO_UBYTE(b
, factor
[2]);
83 UNCLAMPED_FLOAT_TO_UBYTE(a
, factor
[3]);
85 col
= ((a
<< 24) | (r
<< 16) | (g
<< 8) | b
);
87 state
[count
++] = _3DSTATE_COLOR_FACTOR_N_CMD(blendUnit
);
95 GetTexelOp(GLint unit
)
99 return TEXBLENDARG_TEXEL0
;
101 return TEXBLENDARG_TEXEL1
;
103 return TEXBLENDARG_TEXEL2
;
105 return TEXBLENDARG_TEXEL3
;
107 return TEXBLENDARG_TEXEL0
;
113 * Calculate the hardware instuctions to setup the current texture enviromnemt
114 * settings. Since \c gl_texture_unit::_CurrentCombine is used, both
115 * "classic" texture enviroments and GL_ARB_texture_env_combine type texture
116 * environments are treated identically.
119 * This function should return \c bool. When \c false is returned,
120 * it means that an environment is selected that the hardware cannot do. This
121 * is the way the Radeon and R200 drivers work.
124 * Looking at i830_3d_regs.h, it seems the i830 can do part of
125 * GL_ATI_texture_env_combine3. It can handle using \c GL_ONE and
126 * \c GL_ZERO as combine inputs (which the code already supports). It can
127 * also handle the \c GL_MODULATE_ADD_ATI mode. Is it worth investigating
128 * partial support for the extension?
131 i830SetTexEnvCombine(struct i830_context
* i830
,
132 const struct gl_tex_env_combine_state
* combine
,
134 GLuint texel_op
, GLuint
* state
, const GLfloat
* factor
)
136 const GLuint numColorArgs
= combine
->_NumArgsRGB
;
137 GLuint numAlphaArgs
= combine
->_NumArgsA
;
145 bool need_factor
= 0;
148 static const GLuint tex_blend_rgb
[3] = {
149 TEXPIPE_COLOR
| TEXBLEND_ARG1
| TEXBLENDARG_MODIFY_PARMS
,
150 TEXPIPE_COLOR
| TEXBLEND_ARG2
| TEXBLENDARG_MODIFY_PARMS
,
151 TEXPIPE_COLOR
| TEXBLEND_ARG0
| TEXBLENDARG_MODIFY_PARMS
,
153 static const GLuint tex_blend_a
[3] = {
154 TEXPIPE_ALPHA
| TEXBLEND_ARG1
| TEXBLENDARG_MODIFY_PARMS
,
155 TEXPIPE_ALPHA
| TEXBLEND_ARG2
| TEXBLENDARG_MODIFY_PARMS
,
156 TEXPIPE_ALPHA
| TEXBLEND_ARG0
| TEXBLENDARG_MODIFY_PARMS
,
159 if (INTEL_DEBUG
& DEBUG_TEXTURE
)
160 fprintf(stderr
, "%s\n", __func__
);
163 /* The EXT version of the DOT3 extension does not support the
164 * scale factor, but the ARB version (and the version in OpenGL
167 switch (combine
->ModeRGB
) {
168 case GL_DOT3_RGB_EXT
:
169 alpha_shift
= combine
->ScaleShiftA
;
173 case GL_DOT3_RGBA_EXT
:
179 rgb_shift
= combine
->ScaleShiftRGB
;
180 alpha_shift
= combine
->ScaleShiftA
;
185 switch (combine
->ModeRGB
) {
187 blendop
= TEXBLENDOP_ARG1
;
190 blendop
= TEXBLENDOP_MODULATE
;
193 blendop
= TEXBLENDOP_ADD
;
196 blendop
= TEXBLENDOP_ADDSIGNED
;
199 blendop
= TEXBLENDOP_BLEND
;
202 blendop
= TEXBLENDOP_SUBTRACT
;
204 case GL_DOT3_RGB_EXT
:
206 blendop
= TEXBLENDOP_DOT3
;
208 case GL_DOT3_RGBA_EXT
:
210 blendop
= TEXBLENDOP_DOT4
;
213 return pass_through(state
, blendUnit
);
216 blendop
|= (rgb_shift
<< TEXOP_SCALE_SHIFT
);
219 /* Handle RGB args */
220 for (i
= 0; i
< 3; i
++) {
221 switch (combine
->SourceRGB
[i
]) {
223 args_RGB
[i
] = texel_op
;
229 args_RGB
[i
] = GetTexelOp(combine
->SourceRGB
[i
] - GL_TEXTURE0
);
232 args_RGB
[i
] = TEXBLENDARG_FACTOR_N
;
235 case GL_PRIMARY_COLOR
:
236 args_RGB
[i
] = TEXBLENDARG_DIFFUSE
;
239 args_RGB
[i
] = TEXBLENDARG_CURRENT
;
242 return pass_through(state
, blendUnit
);
245 switch (combine
->OperandRGB
[i
]) {
249 case GL_ONE_MINUS_SRC_COLOR
:
250 args_RGB
[i
] |= TEXBLENDARG_INV_ARG
;
253 args_RGB
[i
] |= TEXBLENDARG_REPLICATE_ALPHA
;
255 case GL_ONE_MINUS_SRC_ALPHA
:
256 args_RGB
[i
] |= (TEXBLENDARG_REPLICATE_ALPHA
| TEXBLENDARG_INV_ARG
);
259 return pass_through(state
, blendUnit
);
264 /* Need to knobble the alpha calculations of TEXBLENDOP_DOT4 to
265 * match the spec. Can't use DOT3 as it won't propogate values
266 * into alpha as required:
268 * Note - the global factor is set up with alpha == .5, so
269 * the alpha part of the DOT4 calculation should be zero.
271 if (combine
->ModeRGB
== GL_DOT3_RGBA_EXT
||
272 combine
->ModeRGB
== GL_DOT3_RGBA
) {
273 ablendop
= TEXBLENDOP_DOT4
;
275 args_A
[0] = TEXBLENDARG_FACTOR
; /* the global factor */
276 args_A
[1] = TEXBLENDARG_FACTOR
;
277 args_A
[2] = TEXBLENDARG_FACTOR
;
280 switch (combine
->ModeA
) {
282 ablendop
= TEXBLENDOP_ARG1
;
285 ablendop
= TEXBLENDOP_MODULATE
;
288 ablendop
= TEXBLENDOP_ADD
;
291 ablendop
= TEXBLENDOP_ADDSIGNED
;
294 ablendop
= TEXBLENDOP_BLEND
;
297 ablendop
= TEXBLENDOP_SUBTRACT
;
300 return pass_through(state
, blendUnit
);
304 ablendop
|= (alpha_shift
<< TEXOP_SCALE_SHIFT
);
307 for (i
= 0; i
< 3; i
++) {
308 switch (combine
->SourceA
[i
]) {
310 args_A
[i
] = texel_op
;
316 args_A
[i
] = GetTexelOp(combine
->SourceA
[i
] - GL_TEXTURE0
);
319 args_A
[i
] = TEXBLENDARG_FACTOR_N
;
322 case GL_PRIMARY_COLOR
:
323 args_A
[i
] = TEXBLENDARG_DIFFUSE
;
326 args_A
[i
] = TEXBLENDARG_CURRENT
;
329 return pass_through(state
, blendUnit
);
332 switch (combine
->OperandA
[i
]) {
336 case GL_ONE_MINUS_SRC_ALPHA
:
337 args_A
[i
] |= TEXBLENDARG_INV_ARG
;
340 return pass_through(state
, blendUnit
);
347 /* Native Arg1 == Arg0 in GL_EXT_texture_env_combine spec */
348 /* Native Arg2 == Arg1 in GL_EXT_texture_env_combine spec */
349 /* Native Arg0 == Arg2 in GL_EXT_texture_env_combine spec */
351 /* When we render we need to figure out which is the last really enabled
352 * tex unit, and put last stage on it
356 /* Build color & alpha pipelines */
359 state
[used
++] = (_3DSTATE_MAP_BLEND_OP_CMD(blendUnit
) |
361 ENABLE_TEXOUTPUT_WRT_SEL
|
362 TEXOP_OUTPUT_CURRENT
|
363 DISABLE_TEX_CNTRL_STAGE
| TEXOP_MODIFY_PARMS
| blendop
);
364 state
[used
++] = (_3DSTATE_MAP_BLEND_OP_CMD(blendUnit
) |
366 ENABLE_TEXOUTPUT_WRT_SEL
|
367 TEXOP_OUTPUT_CURRENT
| TEXOP_MODIFY_PARMS
| ablendop
);
369 for (i
= 0; i
< numColorArgs
; i
++) {
370 state
[used
++] = (_3DSTATE_MAP_BLEND_ARG_CMD(blendUnit
) |
371 tex_blend_rgb
[i
] | args_RGB
[i
]);
374 for (i
= 0; i
< numAlphaArgs
; i
++) {
375 state
[used
++] = (_3DSTATE_MAP_BLEND_ARG_CMD(blendUnit
) |
376 tex_blend_a
[i
] | args_A
[i
]);
381 return emit_factor(blendUnit
, state
, used
, factor
);
388 emit_texblend(struct i830_context
*i830
, GLuint unit
, GLuint blendUnit
,
391 struct gl_fixedfunc_texture_unit
*texUnit
=
392 &i830
->intel
.ctx
.Texture
.FixedFuncUnit
[unit
];
393 GLuint tmp
[I830_TEXBLEND_SIZE
], tmp_sz
;
397 fprintf(stderr
, "%s unit %d\n", __func__
, unit
);
399 /* Update i830->state.TexBlend
401 tmp_sz
= i830SetTexEnvCombine(i830
, texUnit
->_CurrentCombine
, blendUnit
,
402 GetTexelOp(unit
), tmp
, texUnit
->EnvColor
);
405 tmp
[0] |= TEXOP_LAST_STAGE
;
407 if (tmp_sz
!= i830
->state
.TexBlendWordsUsed
[blendUnit
] ||
408 memcmp(tmp
, i830
->state
.TexBlend
[blendUnit
],
409 tmp_sz
* sizeof(GLuint
))) {
411 I830_STATECHANGE(i830
, I830_UPLOAD_TEXBLEND(blendUnit
));
412 memcpy(i830
->state
.TexBlend
[blendUnit
], tmp
, tmp_sz
* sizeof(GLuint
));
413 i830
->state
.TexBlendWordsUsed
[blendUnit
] = tmp_sz
;
416 I830_ACTIVESTATE(i830
, I830_UPLOAD_TEXBLEND(blendUnit
), true);
420 emit_passthrough(struct i830_context
*i830
)
422 GLuint tmp
[I830_TEXBLEND_SIZE
], tmp_sz
;
425 tmp_sz
= pass_through(tmp
, unit
);
426 tmp
[0] |= TEXOP_LAST_STAGE
;
428 if (tmp_sz
!= i830
->state
.TexBlendWordsUsed
[unit
] ||
429 memcmp(tmp
, i830
->state
.TexBlend
[unit
], tmp_sz
* sizeof(GLuint
))) {
431 I830_STATECHANGE(i830
, I830_UPLOAD_TEXBLEND(unit
));
432 memcpy(i830
->state
.TexBlend
[unit
], tmp
, tmp_sz
* sizeof(GLuint
));
433 i830
->state
.TexBlendWordsUsed
[unit
] = tmp_sz
;
436 I830_ACTIVESTATE(i830
, I830_UPLOAD_TEXBLEND(unit
), true);
440 i830EmitTextureBlend(struct i830_context
*i830
)
442 struct gl_context
*ctx
= &i830
->intel
.ctx
;
443 GLuint unit
, blendunit
= 0;
445 I830_ACTIVESTATE(i830
, I830_UPLOAD_TEXBLEND_ALL
, false);
447 if (ctx
->Texture
._MaxEnabledTexImageUnit
!= -1) {
448 for (unit
= 0; unit
<= ctx
->Texture
._MaxEnabledTexImageUnit
; unit
++)
449 if (ctx
->Texture
.Unit
[unit
]._Current
)
450 emit_texblend(i830
, unit
, blendunit
++,
451 unit
== ctx
->Texture
._MaxEnabledTexImageUnit
);
453 emit_passthrough(i830
);