7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 * Check if given blend source factor is legal.
43 * \return GL_TRUE if legal, GL_FALSE otherwise.
46 legal_src_factor(const struct gl_context
*ctx
, GLenum factor
)
50 case GL_ONE_MINUS_SRC_COLOR
:
51 return ctx
->Extensions
.NV_blend_square
;
55 case GL_ONE_MINUS_DST_COLOR
:
57 case GL_ONE_MINUS_SRC_ALPHA
:
59 case GL_ONE_MINUS_DST_ALPHA
:
60 case GL_SRC_ALPHA_SATURATE
:
61 case GL_CONSTANT_COLOR
:
62 case GL_ONE_MINUS_CONSTANT_COLOR
:
63 case GL_CONSTANT_ALPHA
:
64 case GL_ONE_MINUS_CONSTANT_ALPHA
:
73 * Check if given blend destination factor is legal.
74 * \return GL_TRUE if legal, GL_FALSE otherwise.
77 legal_dst_factor(const struct gl_context
*ctx
, GLenum factor
)
81 case GL_ONE_MINUS_DST_COLOR
:
82 return ctx
->Extensions
.NV_blend_square
;
86 case GL_ONE_MINUS_SRC_COLOR
:
88 case GL_ONE_MINUS_SRC_ALPHA
:
90 case GL_ONE_MINUS_DST_ALPHA
:
91 case GL_CONSTANT_COLOR
:
92 case GL_ONE_MINUS_CONSTANT_COLOR
:
93 case GL_CONSTANT_ALPHA
:
94 case GL_ONE_MINUS_CONSTANT_ALPHA
:
103 * Check if src/dest RGB/A blend factors are legal. If not generate
105 * \return GL_TRUE if factors are legal, GL_FALSE otherwise.
108 validate_blend_factors(struct gl_context
*ctx
, const char *func
,
109 GLenum sfactorRGB
, GLenum dfactorRGB
,
110 GLenum sfactorA
, GLenum dfactorA
)
112 if (!legal_src_factor(ctx
, sfactorRGB
)) {
113 _mesa_error(ctx
, GL_INVALID_ENUM
,
114 "%s(sfactorRGB = %s)", func
,
115 _mesa_lookup_enum_by_nr(sfactorRGB
));
119 if (!legal_dst_factor(ctx
, dfactorRGB
)) {
120 _mesa_error(ctx
, GL_INVALID_ENUM
,
121 "%s(dfactorRGB = %s)", func
,
122 _mesa_lookup_enum_by_nr(dfactorRGB
));
126 if (sfactorA
!= sfactorRGB
&& !legal_src_factor(ctx
, sfactorA
)) {
127 _mesa_error(ctx
, GL_INVALID_ENUM
,
128 "%s(sfactorA = %s)", func
,
129 _mesa_lookup_enum_by_nr(sfactorA
));
133 if (dfactorA
!= dfactorRGB
&& !legal_dst_factor(ctx
, dfactorA
)) {
134 _mesa_error(ctx
, GL_INVALID_ENUM
,
135 "%s(dfactorA = %s)", func
,
136 _mesa_lookup_enum_by_nr(dfactorA
));
145 * Specify the blending operation.
147 * \param sfactor source factor operator.
148 * \param dfactor destination factor operator.
150 * \sa glBlendFunc, glBlendFuncSeparateEXT
153 _mesa_BlendFunc( GLenum sfactor
, GLenum dfactor
)
155 _mesa_BlendFuncSeparateEXT(sfactor
, dfactor
, sfactor
, dfactor
);
160 * Set the separate blend source/dest factors for all draw buffers.
162 * \param sfactorRGB RGB source factor operator.
163 * \param dfactorRGB RGB destination factor operator.
164 * \param sfactorA alpha source factor operator.
165 * \param dfactorA alpha destination factor operator.
168 _mesa_BlendFuncSeparateEXT( GLenum sfactorRGB
, GLenum dfactorRGB
,
169 GLenum sfactorA
, GLenum dfactorA
)
171 GET_CURRENT_CONTEXT(ctx
);
172 ASSERT_OUTSIDE_BEGIN_END(ctx
);
174 if (MESA_VERBOSE
& VERBOSE_API
)
175 _mesa_debug(ctx
, "glBlendFuncSeparate %s %s %s %s\n",
176 _mesa_lookup_enum_by_nr(sfactorRGB
),
177 _mesa_lookup_enum_by_nr(dfactorRGB
),
178 _mesa_lookup_enum_by_nr(sfactorA
),
179 _mesa_lookup_enum_by_nr(dfactorA
));
181 if (!validate_blend_factors(ctx
, "glBlendFuncSeparate",
182 sfactorRGB
, dfactorRGB
,
183 sfactorA
, dfactorA
)) {
187 if (ctx
->Color
.SrcRGB
== sfactorRGB
&&
188 ctx
->Color
.DstRGB
== dfactorRGB
&&
189 ctx
->Color
.SrcA
== sfactorA
&&
190 ctx
->Color
.DstA
== dfactorA
) {
194 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
196 ctx
->Color
.SrcRGB
= sfactorRGB
;
197 ctx
->Color
.DstRGB
= dfactorRGB
;
198 ctx
->Color
.SrcA
= sfactorA
;
199 ctx
->Color
.DstA
= dfactorA
;
200 if (ctx
->Driver
.BlendFuncSeparate
) {
201 ctx
->Driver
.BlendFuncSeparate(ctx
, sfactorRGB
, dfactorRGB
,
209 * Check if given blend equation is legal.
210 * \return GL_TRUE if legal, GL_FALSE otherwise.
213 legal_blend_equation(const struct gl_context
*ctx
, GLenum mode
)
217 case GL_FUNC_SUBTRACT
:
218 case GL_FUNC_REVERSE_SUBTRACT
:
222 return ctx
->Extensions
.EXT_blend_minmax
;
229 /* This is really an extension function! */
231 _mesa_BlendEquation( GLenum mode
)
233 GET_CURRENT_CONTEXT(ctx
);
234 ASSERT_OUTSIDE_BEGIN_END(ctx
);
236 if (MESA_VERBOSE
& VERBOSE_API
)
237 _mesa_debug(ctx
, "glBlendEquation(%s)\n",
238 _mesa_lookup_enum_by_nr(mode
));
240 if (!legal_blend_equation(ctx
, mode
)) {
241 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquation");
245 if (ctx
->Color
.EquationRGB
== mode
&& ctx
->Color
.EquationA
== mode
) {
249 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
250 ctx
->Color
.EquationRGB
= mode
;
251 ctx
->Color
.EquationA
= mode
;
253 if (ctx
->Driver
.BlendEquationSeparate
)
254 (*ctx
->Driver
.BlendEquationSeparate
)( ctx
, mode
, mode
);
258 _mesa_BlendEquationSeparateEXT( GLenum modeRGB
, GLenum modeA
)
260 GET_CURRENT_CONTEXT(ctx
);
261 ASSERT_OUTSIDE_BEGIN_END(ctx
);
263 if (MESA_VERBOSE
& VERBOSE_API
)
264 _mesa_debug(ctx
, "glBlendEquationSeparateEXT(%s %s)\n",
265 _mesa_lookup_enum_by_nr(modeRGB
),
266 _mesa_lookup_enum_by_nr(modeA
));
268 if ( (modeRGB
!= modeA
) && !ctx
->Extensions
.EXT_blend_equation_separate
) {
269 _mesa_error(ctx
, GL_INVALID_OPERATION
,
270 "glBlendEquationSeparateEXT not supported by driver");
274 if (!legal_blend_equation(ctx
, modeRGB
)) {
275 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationSeparateEXT(modeRGB)");
279 if (!legal_blend_equation(ctx
, modeA
)) {
280 _mesa_error(ctx
, GL_INVALID_ENUM
, "glBlendEquationSeparateEXT(modeA)");
284 if (ctx
->Color
.EquationRGB
== modeRGB
&& ctx
->Color
.EquationA
== modeA
) {
288 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
289 ctx
->Color
.EquationRGB
= modeRGB
;
290 ctx
->Color
.EquationA
= modeA
;
292 if (ctx
->Driver
.BlendEquationSeparate
)
293 ctx
->Driver
.BlendEquationSeparate(ctx
, modeRGB
, modeA
);
297 #endif /* _HAVE_FULL_GL */
301 * Set the blending color.
303 * \param red red color component.
304 * \param green green color component.
305 * \param blue blue color component.
306 * \param alpha alpha color component.
308 * \sa glBlendColor().
310 * Clamps the parameters and updates gl_colorbuffer_attrib::BlendColor. On a
311 * change, flushes the vertices and notifies the driver via
312 * dd_function_table::BlendColor callback.
315 _mesa_BlendColor( GLclampf red
, GLclampf green
, GLclampf blue
, GLclampf alpha
)
318 GET_CURRENT_CONTEXT(ctx
);
319 ASSERT_OUTSIDE_BEGIN_END(ctx
);
326 if (TEST_EQ_4V(tmp
, ctx
->Color
.BlendColor
))
329 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
330 COPY_4FV( ctx
->Color
.BlendColor
, tmp
);
332 if (ctx
->Driver
.BlendColor
)
333 (*ctx
->Driver
.BlendColor
)(ctx
, ctx
->Color
.BlendColor
);
338 * Specify the alpha test function.
340 * \param func alpha comparison function.
341 * \param ref reference value.
343 * Verifies the parameters and updates gl_colorbuffer_attrib.
344 * On a change, flushes the vertices and notifies the driver via
345 * dd_function_table::AlphaFunc callback.
348 _mesa_AlphaFunc( GLenum func
, GLclampf ref
)
350 GET_CURRENT_CONTEXT(ctx
);
351 ASSERT_OUTSIDE_BEGIN_END(ctx
);
353 if (MESA_VERBOSE
& VERBOSE_API
)
354 _mesa_debug(ctx
, "glAlphaFunc(%s, %f)\n",
355 _mesa_lookup_enum_by_nr(func
), ref
);
366 if (ctx
->Color
.AlphaFunc
== func
&& ctx
->Color
.AlphaRef
== ref
)
367 return; /* no change */
369 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
370 ctx
->Color
.AlphaFunc
= func
;
371 ctx
->Color
.AlphaRef
= ref
;
373 if (ctx
->Driver
.AlphaFunc
)
374 ctx
->Driver
.AlphaFunc(ctx
, func
, ctx
->Color
.AlphaRef
);
378 _mesa_error( ctx
, GL_INVALID_ENUM
, "glAlphaFunc(func)" );
385 * Specify a logic pixel operation for color index rendering.
387 * \param opcode operation.
389 * Verifies that \p opcode is a valid enum and updates
390 gl_colorbuffer_attrib::LogicOp.
391 * On a change, flushes the vertices and notifies the driver via the
392 * dd_function_table::LogicOpcode callback.
395 _mesa_LogicOp( GLenum opcode
)
397 GET_CURRENT_CONTEXT(ctx
);
398 ASSERT_OUTSIDE_BEGIN_END(ctx
);
400 if (MESA_VERBOSE
& VERBOSE_API
)
401 _mesa_debug(ctx
, "glLogicOp(%s)\n", _mesa_lookup_enum_by_nr(opcode
));
407 case GL_COPY_INVERTED
:
417 case GL_AND_INVERTED
:
422 _mesa_error( ctx
, GL_INVALID_ENUM
, "glLogicOp" );
426 if (ctx
->Color
.LogicOp
== opcode
)
429 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
430 ctx
->Color
.LogicOp
= opcode
;
432 if (ctx
->Driver
.LogicOpcode
)
433 ctx
->Driver
.LogicOpcode( ctx
, opcode
);
438 _mesa_IndexMask( GLuint mask
)
440 GET_CURRENT_CONTEXT(ctx
);
441 ASSERT_OUTSIDE_BEGIN_END(ctx
);
443 if (ctx
->Color
.IndexMask
== mask
)
446 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
447 ctx
->Color
.IndexMask
= mask
;
453 * Enable or disable writing of frame buffer color components.
455 * \param red whether to mask writing of the red color component.
456 * \param green whether to mask writing of the green color component.
457 * \param blue whether to mask writing of the blue color component.
458 * \param alpha whether to mask writing of the alpha color component.
462 * Sets the appropriate value of gl_colorbuffer_attrib::ColorMask. On a
463 * change, flushes the vertices and notifies the driver via the
464 * dd_function_table::ColorMask callback.
467 _mesa_ColorMask( GLboolean red
, GLboolean green
,
468 GLboolean blue
, GLboolean alpha
)
470 GET_CURRENT_CONTEXT(ctx
);
472 ASSERT_OUTSIDE_BEGIN_END(ctx
);
474 if (MESA_VERBOSE
& VERBOSE_API
)
475 _mesa_debug(ctx
, "glColorMask(%d, %d, %d, %d)\n",
476 red
, green
, blue
, alpha
);
478 /* Shouldn't have any information about channel depth in core mesa
479 * -- should probably store these as the native booleans:
481 tmp
[RCOMP
] = red
? 0xff : 0x0;
482 tmp
[GCOMP
] = green
? 0xff : 0x0;
483 tmp
[BCOMP
] = blue
? 0xff : 0x0;
484 tmp
[ACOMP
] = alpha
? 0xff : 0x0;
486 if (!TEST_EQ_4V(tmp
, ctx
->Color
.ColorMask
)) {
487 FLUSH_VERTICES(ctx
, _NEW_COLOR
);
488 COPY_4UBV(ctx
->Color
.ColorMask
, tmp
);
491 if (ctx
->Driver
.ColorMask
)
492 ctx
->Driver
.ColorMask( ctx
, red
, green
, blue
, alpha
);
496 /**********************************************************************/
497 /** \name Initialization */
501 * Initialization of the context's Color attribute group.
503 * \param ctx GL context.
505 * Initializes the related fields in the context color attribute group,
506 * __struct gl_contextRec::Color.
508 void _mesa_init_color( struct gl_context
* ctx
)
510 /* Color buffer group */
511 ctx
->Color
.IndexMask
= ~0u;
512 memset(ctx
->Color
.ColorMask
, 0xff, sizeof(ctx
->Color
.ColorMask
));
513 ctx
->Color
.ClearIndex
= 0;
514 ASSIGN_4V( ctx
->Color
.ClearColor
.f
, 0, 0, 0, 0 );
515 ctx
->Color
.AlphaEnabled
= GL_FALSE
;
516 ctx
->Color
.AlphaFunc
= GL_ALWAYS
;
517 ctx
->Color
.AlphaRef
= 0;
518 ctx
->Color
.BlendEnabled
= 0x0;
519 ctx
->Color
.SrcRGB
= GL_ONE
;
520 ctx
->Color
.DstRGB
= GL_ZERO
;
521 ctx
->Color
.SrcA
= GL_ONE
;
522 ctx
->Color
.DstA
= GL_ZERO
;
523 ctx
->Color
.EquationRGB
= GL_FUNC_ADD
;
524 ctx
->Color
.EquationA
= GL_FUNC_ADD
;
525 ASSIGN_4V( ctx
->Color
.BlendColor
, 0.0, 0.0, 0.0, 0.0 );
526 ctx
->Color
.IndexLogicOpEnabled
= GL_FALSE
;
527 ctx
->Color
.ColorLogicOpEnabled
= GL_FALSE
;
528 ctx
->Color
.LogicOp
= GL_COPY
;
529 ctx
->Color
.DitherFlag
= GL_TRUE
;
531 if (ctx
->Visual
.doubleBufferMode
) {
532 ctx
->Color
.DrawBuffer
= GL_BACK
;
535 ctx
->Color
.DrawBuffer
= GL_FRONT
;