Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/blend.c

raw
1 /* $Id: blend.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.2
6 * Copyright (C) 1995-1997 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #ifdef DEBUG
24 # include <assert.h>
25 #endif
26 #include <stdlib.h>
27 #include "alphabuf.h"
28 #include "blend.h"
29 #include "context.h"
30 #include "dlist.h"
31 #include "macros.h"
32 #include "pb.h"
33 #include "span.h"
34 #include "types.h"
35
36
37
38 void gl_BlendFunc( GLcontext* ctx, GLenum sfactor, GLenum dfactor )
39 {
40 if (INSIDE_BEGIN_END(ctx)) {
41 gl_error( ctx, GL_INVALID_OPERATION, "glBlendFunc" );
42 return;
43 }
44
45 switch (sfactor) {
46 case GL_ZERO:
47 case GL_ONE:
48 case GL_DST_COLOR:
49 case GL_ONE_MINUS_DST_COLOR:
50 case GL_SRC_ALPHA:
51 case GL_ONE_MINUS_SRC_ALPHA:
52 case GL_DST_ALPHA:
53 case GL_ONE_MINUS_DST_ALPHA:
54 case GL_SRC_ALPHA_SATURATE:
55 case GL_CONSTANT_COLOR:
56 case GL_ONE_MINUS_CONSTANT_COLOR:
57 case GL_CONSTANT_ALPHA:
58 case GL_ONE_MINUS_CONSTANT_ALPHA:
59 ctx->Color.BlendSrc = sfactor;
60 break;
61 default:
62 gl_error( ctx, GL_INVALID_ENUM, "glBlendFunc(sfactor)" );
63 return;
64 }
65
66 switch (dfactor) {
67 case GL_ZERO:
68 case GL_ONE:
69 case GL_SRC_COLOR:
70 case GL_ONE_MINUS_SRC_COLOR:
71 case GL_SRC_ALPHA:
72 case GL_ONE_MINUS_SRC_ALPHA:
73 case GL_DST_ALPHA:
74 case GL_ONE_MINUS_DST_ALPHA:
75 case GL_CONSTANT_COLOR:
76 case GL_ONE_MINUS_CONSTANT_COLOR:
77 case GL_CONSTANT_ALPHA:
78 case GL_ONE_MINUS_CONSTANT_ALPHA:
79 ctx->Color.BlendDst = dfactor;
80 break;
81 default:
82 gl_error( ctx, GL_INVALID_ENUM, "glBlendFunc(dfactor)" );
83 }
84
85 ctx->NewState |= NEW_RASTER_OPS;
86 }
87
88
89
90 /* This is really an extension function! */
91 void gl_BlendEquation( GLcontext* ctx, GLenum mode )
92 {
93 if (INSIDE_BEGIN_END(ctx)) {
94 gl_error( ctx, GL_INVALID_OPERATION, "glBlendEquation" );
95 return;
96 }
97
98 switch (mode) {
99 case GL_MIN_EXT:
100 case GL_MAX_EXT:
101 case GL_LOGIC_OP:
102 case GL_FUNC_ADD_EXT:
103 case GL_FUNC_SUBTRACT_EXT:
104 case GL_FUNC_REVERSE_SUBTRACT_EXT:
105 ctx->Color.BlendEquation = mode;
106 break;
107 default:
108 gl_error( ctx, GL_INVALID_ENUM, "glBlendEquation" );
109 return;
110 }
111
112 /* This is needed to support 1.1's RGB logic ops AND
113 * 1.0's blending logicops.
114 */
115 if (mode==GL_LOGIC_OP && ctx->Color.BlendEnabled) {
116 ctx->Color.ColorLogicOpEnabled = GL_TRUE;
117 }
118 else {
119 ctx->Color.ColorLogicOpEnabled = GL_FALSE;
120 }
121
122 ctx->NewState |= NEW_RASTER_OPS;
123 }
124
125
126
127 void gl_BlendColor( GLcontext* ctx, GLclampf red, GLclampf green,
128 GLclampf blue, GLclampf alpha )
129 {
130 ctx->Color.BlendColor[0] = CLAMP( red, 0.0, 1.0 );
131 ctx->Color.BlendColor[1] = CLAMP( green, 0.0, 1.0 );
132 ctx->Color.BlendColor[2] = CLAMP( blue, 0.0, 1.0 );
133 ctx->Color.BlendColor[3] = CLAMP( alpha, 0.0, 1.0 );
134 }
135
136
137
138 /*
139 * Do the real work of gl_blend_span() and gl_blend_pixels().
140 * Input: n - number of pixels
141 * mask - the usual write mask
142 * In/Out: red, green, blue, alpha - the incoming and modified pixels
143 * Input: rdest, gdest, bdest, adest - the pixels from the dest color buffer
144 */
145 static void do_blend( GLcontext* ctx, GLuint n, const GLubyte mask[],
146 GLubyte red[], GLubyte green[],
147 GLubyte blue[], GLubyte alpha[],
148 const GLubyte rdest[], const GLubyte gdest[],
149 const GLubyte bdest[], const GLubyte adest[] )
150 {
151 GLuint i;
152
153 /* Common cases: */
154
155 if (ctx->Color.BlendEquation==GL_FUNC_ADD_EXT
156 && ctx->Color.BlendSrc==GL_SRC_ALPHA
157 && ctx->Color.BlendDst==GL_ONE_MINUS_SRC_ALPHA) {
158 /* Alpha blending */
159 GLfloat ascale = 256.0f * ctx->Visual->InvAlphaScale;
160 GLint rmax = (GLint) ctx->Visual->RedScale;
161 GLint gmax = (GLint) ctx->Visual->GreenScale;
162 GLint bmax = (GLint) ctx->Visual->BlueScale;
163 GLint amax = (GLint) ctx->Visual->AlphaScale;
164 for (i=0;i<n;i++) {
165 if (mask[i]) {
166 GLint r, g, b, a;
167 GLint t = (GLint) ( alpha[i] * ascale ); /* t in [0,256] */
168 GLint s = 256 - t;
169 r = (red[i] * t + rdest[i] * s) >> 8;
170 g = (green[i] * t + gdest[i] * s) >> 8;
171 b = (blue[i] * t + bdest[i] * s) >> 8;
172 a = (alpha[i] * t + adest[i] * s) >> 8;
173 red[i] = MIN2( r, rmax );
174 green[i] = MIN2( g, gmax );
175 blue[i] = MIN2( b, bmax );
176 alpha[i] = MIN2( a, amax );
177 }
178 }
179 }
180
181 /* General cases: */
182
183 else if (ctx->Color.BlendEquation==GL_FUNC_ADD_EXT
184 || ctx->Color.BlendEquation==GL_FUNC_SUBTRACT_EXT
185 || ctx->Color.BlendEquation==GL_FUNC_REVERSE_SUBTRACT_EXT) {
186 GLfloat rmax = ctx->Visual->RedScale;
187 GLfloat gmax = ctx->Visual->GreenScale;
188 GLfloat bmax = ctx->Visual->BlueScale;
189 GLfloat amax = ctx->Visual->AlphaScale;
190 GLfloat rscale = 1.0f / rmax;
191 GLfloat gscale = 1.0f / gmax;
192 GLfloat bscale = 1.0f / bmax;
193 GLfloat ascale = 1.0f / amax;
194
195 for (i=0;i<n;i++) {
196 if (mask[i]) {
197 GLint Rs, Gs, Bs, As; /* Source colors */
198 GLint Rd, Gd, Bd, Ad; /* Dest colors */
199 GLfloat sR, sG, sB, sA; /* Source scaling */
200 GLfloat dR, dG, dB, dA; /* Dest scaling */
201 GLfloat r, g, b, a;
202
203 /* Source Color */
204 Rs = red[i];
205 Gs = green[i];
206 Bs = blue[i];
207 As = alpha[i];
208
209 /* Frame buffer color */
210 Rd = rdest[i];
211 Gd = gdest[i];
212 Bd = bdest[i];
213 Ad = adest[i];
214
215 /* Source scaling */
216 switch (ctx->Color.BlendSrc) {
217 case GL_ZERO:
218 sR = sG = sB = sA = 0.0F;
219 break;
220 case GL_ONE:
221 sR = sG = sB = sA = 1.0F;
222 break;
223 case GL_DST_COLOR:
224 sR = (GLfloat) Rd * rscale;
225 sG = (GLfloat) Gd * gscale;
226 sB = (GLfloat) Bd * bscale;
227 sA = (GLfloat) Ad * ascale;
228 break;
229 case GL_ONE_MINUS_DST_COLOR:
230 sR = 1.0F - (GLfloat) Rd * rscale;
231 sG = 1.0F - (GLfloat) Gd * gscale;
232 sB = 1.0F - (GLfloat) Bd * bscale;
233 sA = 1.0F - (GLfloat) Ad * ascale;
234 break;
235 case GL_SRC_ALPHA:
236 sR = sG = sB = sA = (GLfloat) As * ascale;
237 break;
238 case GL_ONE_MINUS_SRC_ALPHA:
239 sR = sG = sB = sA = (GLfloat) 1.0F - (GLfloat) As * ascale;
240 break;
241 case GL_DST_ALPHA:
242 sR = sG = sB = sA =(GLfloat) Ad * ascale;
243 break;
244 case GL_ONE_MINUS_DST_ALPHA:
245 sR = sG = sB = sA = 1.0F - (GLfloat) Ad * ascale;
246 break;
247 case GL_SRC_ALPHA_SATURATE:
248 if (As < 1.0F - (GLfloat) Ad * ascale) {
249 sR = sG = sB = (GLfloat) As * ascale;
250 }
251 else {
252 sR = sG = sB = 1.0F - (GLfloat) Ad * ascale;
253 }
254 sA = 1.0;
255 break;
256 case GL_CONSTANT_COLOR:
257 sR = ctx->Color.BlendColor[0];
258 sG = ctx->Color.BlendColor[1];
259 sB = ctx->Color.BlendColor[2];
260 sA = ctx->Color.BlendColor[3];
261 break;
262 case GL_ONE_MINUS_CONSTANT_COLOR:
263 sR = 1.0F - ctx->Color.BlendColor[0];
264 sG = 1.0F - ctx->Color.BlendColor[1];
265 sB = 1.0F - ctx->Color.BlendColor[2];
266 sA = 1.0F - ctx->Color.BlendColor[3];
267 break;
268 case GL_CONSTANT_ALPHA:
269 sR = sG = sB = sA = ctx->Color.BlendColor[3];
270 break;
271 case GL_ONE_MINUS_CONSTANT_ALPHA:
272 sR = sG = sB = sA = 1.0F - ctx->Color.BlendColor[3];
273 break;
274 default:
275 /* this should never happen */
276 abort();
277 }
278
279 /* Dest scaling */
280 switch (ctx->Color.BlendDst) {
281 case GL_ZERO:
282 dR = dG = dB = dA = 0.0F;
283 break;
284 case GL_ONE:
285 dR = dG = dB = dA = 1.0F;
286 break;
287 case GL_SRC_COLOR:
288 dR = (GLfloat) Rs * rscale;
289 dG = (GLfloat) Gs * gscale;
290 dB = (GLfloat) Bs * bscale;
291 dA = (GLfloat) As * ascale;
292 break;
293 case GL_ONE_MINUS_SRC_COLOR:
294 dR = 1.0F - (GLfloat) Rs * rscale;
295 dG = 1.0F - (GLfloat) Gs * gscale;
296 dB = 1.0F - (GLfloat) Bs * bscale;
297 dA = 1.0F - (GLfloat) As * ascale;
298 break;
299 case GL_SRC_ALPHA:
300 dR = dG = dB = dA = (GLfloat) As * ascale;
301 break;
302 case GL_ONE_MINUS_SRC_ALPHA:
303 dR = dG = dB = dA = (GLfloat) 1.0F - (GLfloat) As * ascale;
304 break;
305 case GL_DST_ALPHA:
306 dR = dG = dB = dA = (GLfloat) Ad * ascale;
307 break;
308 case GL_ONE_MINUS_DST_ALPHA:
309 dR = dG = dB = dA = 1.0F - (GLfloat) Ad * ascale;
310 break;
311 case GL_CONSTANT_COLOR:
312 dR = ctx->Color.BlendColor[0];
313 dG = ctx->Color.BlendColor[1];
314 dB = ctx->Color.BlendColor[2];
315 dA = ctx->Color.BlendColor[3];
316 break;
317 case GL_ONE_MINUS_CONSTANT_COLOR:
318 dR = 1.0F - ctx->Color.BlendColor[0];
319 dG = 1.0F - ctx->Color.BlendColor[1];
320 dB = 1.0F - ctx->Color.BlendColor[2];
321 dA = 1.0F - ctx->Color.BlendColor[3];
322 break;
323 case GL_CONSTANT_ALPHA:
324 dR = dG = dB = dA = ctx->Color.BlendColor[3];
325 break;
326 case GL_ONE_MINUS_CONSTANT_ALPHA:
327 dR = dG = dB = dA = 1.0F - ctx->Color.BlendColor[3] * ascale;
328 break;
329 default:
330 /* this should never happen */
331 abort();
332 }
333
334 #ifdef DEBUG
335 assert( sR>= 0.0 && sR<=1.0 );
336 assert( sG>= 0.0 && sG<=1.0 );
337 assert( sB>= 0.0 && sB<=1.0 );
338 assert( sA>= 0.0 && sA<=1.0 );
339 assert( dR>= 0.0 && dR<=1.0 );
340 assert( dG>= 0.0 && dG<=1.0 );
341 assert( dB>= 0.0 && dB<=1.0 );
342 assert( dA>= 0.0 && dA<=1.0 );
343 #endif
344
345 /* compute blended color */
346 if (ctx->Color.BlendEquation==GL_FUNC_ADD_EXT) {
347 r = Rs * sR + Rd * dR;
348 g = Gs * sG + Gd * dG;
349 b = Bs * sB + Bd * dB;
350 a = As * sA + Ad * dA;
351 }
352 else if (ctx->Color.BlendEquation==GL_FUNC_SUBTRACT_EXT) {
353 r = Rs * sR - Rd * dR;
354 g = Gs * sG - Gd * dG;
355 b = Bs * sB - Bd * dB;
356 a = As * sA - Ad * dA;
357 }
358 else if (ctx->Color.BlendEquation==GL_FUNC_REVERSE_SUBTRACT_EXT) {
359 r = Rd * dR - Rs * sR;
360 g = Gd * dG - Gs * sG;
361 b = Bd * dB - Bs * sB;
362 a = Ad * dA - As * sA;
363 }
364 red[i] = (GLint) CLAMP( r, 0.0F, rmax );
365 green[i] = (GLint) CLAMP( g, 0.0F, gmax );
366 blue[i] = (GLint) CLAMP( b, 0.0F, bmax );
367 alpha[i] = (GLint) CLAMP( a, 0.0F, amax );
368 }
369 }
370 }
371 else if (ctx->Color.BlendEquation==GL_MIN_EXT) {
372 /* EXTENSION */
373 for (i=0;i<n;i++) {
374 if (mask[i]) {
375 red[i] = MIN2( red[i], rdest[i] );
376 green[i] = MIN2( green[i], gdest[i] );
377 blue[i] = MIN2( blue[i], bdest[i] );
378 alpha[i] = MIN2( alpha[i], adest[i] );
379 }
380 }
381 }
382 else if (ctx->Color.BlendEquation==GL_MAX_EXT) {
383 /* EXTENSION */
384 for (i=0;i<n;i++) {
385 if (mask[i]) {
386 red[i] = MAX2( red[i], rdest[i] );
387 green[i] = MAX2( green[i], gdest[i] );
388 blue[i] = MAX2( blue[i], bdest[i] );
389 alpha[i] = MAX2( alpha[i], adest[i] );
390 }
391 }
392 }
393
394 }
395
396
397
398
399
400 /*
401 * Apply the blending operator to a span of pixels.
402 * Input: n - number of pixels in span
403 * x, y - location of leftmost pixel in span in window coords.
404 * mask - boolean mask indicating which pixels to blend.
405 * In/Out: red, green, blue, alpha - pixel values
406 */
407 void gl_blend_span( GLcontext* ctx, GLuint n, GLint x, GLint y,
408 GLubyte red[], GLubyte green[],
409 GLubyte blue[], GLubyte alpha[],
410 GLubyte mask[] )
411 {
412 GLubyte rdest[MAX_WIDTH], gdest[MAX_WIDTH];
413 GLubyte bdest[MAX_WIDTH], adest[MAX_WIDTH];
414
415 /* Check if device driver can do the work */
416 if (ctx->Color.BlendEquation==GL_LOGIC_OP && !ctx->Color.SWLogicOpEnabled) {
417 return;
418 }
419
420 /* Read span of current frame buffer pixels */
421 gl_read_color_span( ctx, n, x, y, rdest, gdest, bdest, adest );
422
423 do_blend( ctx, n, mask, red, green, blue, alpha, rdest, gdest, bdest, adest );
424 }
425
426
427
428
429
430 /*
431 * Apply the blending operator to an array of pixels.
432 * Input: n - number of pixels in span
433 * x, y - array of pixel locations
434 * mask - boolean mask indicating which pixels to blend.
435 * In/Out: red, green, blue, alpha - array of pixel values
436 */
437 void gl_blend_pixels( GLcontext* ctx,
438 GLuint n, const GLint x[], const GLint y[],
439 GLubyte red[], GLubyte green[],
440 GLubyte blue[], GLubyte alpha[],
441 GLubyte mask[] )
442 {
443 GLubyte rdest[PB_SIZE], gdest[PB_SIZE], bdest[PB_SIZE], adest[PB_SIZE];
444
445 /* Check if device driver can do the work */
446 if (ctx->Color.BlendEquation==GL_LOGIC_OP && !ctx->Color.SWLogicOpEnabled) {
447 return;
448 }
449
450 /* Read pixels from current color buffer */
451 (*ctx->Driver.ReadColorPixels)( ctx, n, x, y, rdest, gdest, bdest, adest, mask );
452 if (ctx->RasterMask & ALPHABUF_BIT) {
453 gl_read_alpha_pixels( ctx, n, x, y, adest, mask );
454 }
455
456 do_blend( ctx, n, mask, red, green, blue, alpha, rdest, gdest, bdest, adest );
457 }