Debian Codegraph @

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

raw
1 /* $Id: pb.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 /*
24 * Pixel buffer:
25 *
26 * As fragments are produced (by point, line, and bitmap drawing) they
27 * are accumlated in a buffer. When the buffer is full or has to be
28 * flushed (glEnd), we apply all enabled rasterization functions to the
29 * pixels and write the results to the display buffer. The goal is to
30 * maximize the number of pixels processed inside loops and to minimize
31 * the number of function calls.
32 */
33
34
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include "alpha.h"
39 #include "alphabuf.h"
40 #include "blend.h"
41 #include "depth.h"
42 #include "fog.h"
43 #include "logic.h"
44 #include "macros.h"
45 #include "masking.h"
46 #include "pb.h"
47 #include "scissor.h"
48 #include "stencil.h"
49 #include "texture.h"
50 #include "types.h"
51
52
53
54 /*
55 * When the pixel buffer is full, or needs to be flushed, call this
56 * function. All the pixels in the pixel buffer will be subjected
57 * to texturing, scissoring, stippling, alpha testing, stenciling,
58 * depth testing, blending, and finally written to the frame buffer.
59 */
60 void gl_flush_pb( GLcontext *ctx )
61 {
62 struct pixel_buffer* PB = ctx->PB;
63
64 DEFARRAY(GLubyte,mask,PB_SIZE+4); /* add 4 for manually unrolled loop, below */
65 DEFARRAY(GLubyte, rsave, PB_SIZE);
66 DEFARRAY(GLubyte, gsave, PB_SIZE);
67 DEFARRAY(GLubyte, bsave, PB_SIZE);
68 DEFARRAY(GLubyte, asave, PB_SIZE);
69
70 if (PB->count==0) goto CleanUp;
71
72 /* initialize mask array and clip pixels simultaneously */
73 {
74 GLint xmin = ctx->Buffer->Xmin;
75 GLint xmax = ctx->Buffer->Xmax;
76 GLint ymin = ctx->Buffer->Ymin;
77 GLint ymax = ctx->Buffer->Ymax;
78 GLint *x = PB->x;
79 GLint *y = PB->y;
80 GLuint i = 0;
81 /* manually unrolled loop, OK to go past PB->count */
82 do {
83 mask[i] = (x[i]>=xmin) & (x[i]<=xmax) & (y[i]>=ymin) & (y[i]<=ymax);
84 i++;
85 mask[i] = (x[i]>=xmin) & (x[i]<=xmax) & (y[i]>=ymin) & (y[i]<=ymax);
86 i++;
87 mask[i] = (x[i]>=xmin) & (x[i]<=xmax) & (y[i]>=ymin) & (y[i]<=ymax);
88 i++;
89 mask[i] = (x[i]>=xmin) & (x[i]<=xmax) & (y[i]>=ymin) & (y[i]<=ymax);
90 i++;
91 } while (i<PB->count);
92 }
93
94 if (ctx->Visual->RGBAflag) {
95 /* RGBA COLOR PIXELS */
96 if (PB->mono && ctx->MutablePixels) {
97 /* Copy flat color to all pixels */
98 MEMSET( PB->r, PB->color[0], PB->count );
99 MEMSET( PB->g, PB->color[1], PB->count );
100 MEMSET( PB->b, PB->color[2], PB->count );
101 MEMSET( PB->a, PB->color[3], PB->count );
102 }
103
104 /* If each pixel can be of a different color... */
105 if (ctx->MutablePixels || !PB->mono) {
106
107 if (ctx->Texture.Enabled & TEXTURE_3D) {
108 /* TODO: need texture lambda valus */
109 gl_texture_pixels_3d( ctx, PB->count, PB->s, PB->t, PB->u, NULL,
110 PB->r, PB->g, PB->b, PB->a);
111 }
112 else if (ctx->Texture.Enabled & TEXTURE_2D) {
113 /* TODO: need texture lambda valus */
114 gl_texture_pixels_2d( ctx, PB->count, PB->s, PB->t, NULL,
115 PB->r, PB->g, PB->b, PB->a);
116 }
117 else if (ctx->Texture.Enabled & TEXTURE_1D) {
118 /* TODO: need texture lambda values */
119 gl_texture_pixels_1d( ctx, PB->count, PB->s, NULL,
120 PB->r, PB->g, PB->b, PB->a );
121 }
122
123 if (ctx->Fog.Enabled
124 && (ctx->Hint.Fog==GL_NICEST || PB->primitive==GL_BITMAP)) {
125 gl_fog_color_pixels( ctx, PB->count, PB->z,
126 PB->r, PB->g, PB->b, PB->a );
127 }
128
129 /* Scissoring already done above */
130
131 if (ctx->Color.AlphaEnabled) {
132 if (gl_alpha_test( ctx, PB->count, PB->a, mask )==0) {
133 goto CleanUp;
134 }
135 }
136
137 if (ctx->Stencil.Enabled) {
138 /* first stencil test */
139 if (gl_stencil_pixels( ctx, PB->count, PB->x, PB->y, mask )==0) {
140 goto CleanUp;
141 }
142 /* depth buffering w/ stencil */
143 gl_depth_stencil_pixels( ctx, PB->count, PB->x, PB->y, PB->z, mask );
144 }
145 else if (ctx->Depth.Test) {
146 /* regular depth testing */
147 (*ctx->Driver.DepthTestPixels)( ctx, PB->count, PB->x, PB->y, PB->z, mask );
148 }
149
150 if (ctx->RasterMask & NO_DRAW_BIT) {
151 goto CleanUp;
152 }
153
154 if (ctx->RasterMask & FRONT_AND_BACK_BIT) {
155 /* make a copy of the colors */
156 MEMCPY( rsave, PB->r, PB->count * sizeof(GLubyte) );
157 MEMCPY( gsave, PB->r, PB->count * sizeof(GLubyte) );
158 MEMCPY( bsave, PB->r, PB->count * sizeof(GLubyte) );
159 MEMCPY( asave, PB->r, PB->count * sizeof(GLubyte) );
160 }
161
162 if (ctx->Color.SWLogicOpEnabled) {
163 gl_logicop_rgba_pixels( ctx, PB->count, PB->x, PB->y,
164 PB->r, PB->g, PB->b, PB->a, mask);
165 }
166 else if (ctx->Color.BlendEnabled) {
167 gl_blend_pixels( ctx, PB->count, PB->x, PB->y,
168 PB->r, PB->g, PB->b, PB->a, mask);
169 }
170
171 if (ctx->Color.SWmasking) {
172 gl_mask_color_pixels( ctx, PB->count, PB->x, PB->y,
173 PB->r, PB->g, PB->b, PB->a, mask );
174 }
175
176 /* write pixels */
177 (*ctx->Driver.WriteColorPixels)( ctx, PB->count, PB->x, PB->y,
178 PB->r, PB->g, PB->b, PB->a, mask );
179 if (ctx->RasterMask & ALPHABUF_BIT) {
180 gl_write_alpha_pixels( ctx, PB->count, PB->x, PB->y, PB->a, mask );
181 }
182
183 if (ctx->RasterMask & FRONT_AND_BACK_BIT) {
184 /*** Also draw to back buffer ***/
185 (*ctx->Driver.SetBuffer)( ctx, GL_BACK );
186 if (ctx->Color.SWLogicOpEnabled) {
187 gl_logicop_rgba_pixels( ctx, PB->count, PB->x, PB->y,
188 PB->r, PB->g, PB->b, PB->a, mask);
189 }
190 else if (ctx->Color.BlendEnabled) {
191 gl_blend_pixels( ctx, PB->count, PB->x, PB->y,
192 rsave, gsave, bsave, asave, mask );
193 }
194 if (ctx->Color.SWmasking) {
195 gl_mask_color_pixels( ctx, PB->count, PB->x, PB->y,
196 rsave, gsave, bsave, asave, mask);
197 }
198 (*ctx->Driver.WriteColorPixels)( ctx, PB->count, PB->x, PB->y,
199 rsave, gsave, bsave, asave, mask);
200 if (ctx->RasterMask & ALPHABUF_BIT) {
201 ctx->Buffer->Alpha = ctx->Buffer->BackAlpha;
202 gl_write_alpha_pixels( ctx, PB->count, PB->x, PB->y,
203 asave, mask );
204 ctx->Buffer->Alpha = ctx->Buffer->FrontAlpha;
205 }
206 (*ctx->Driver.SetBuffer)( ctx, GL_FRONT );
207 /*** ALL DONE ***/
208 }
209 }
210 else {
211 /* Same color for all pixels */
212
213 /* Scissoring already done above */
214
215 if (ctx->Color.AlphaEnabled) {
216 if (gl_alpha_test( ctx, PB->count, PB->a, mask )==0) {
217 goto CleanUp;
218 }
219 }
220
221 if (ctx->Stencil.Enabled) {
222 /* first stencil test */
223 if (gl_stencil_pixels( ctx, PB->count, PB->x, PB->y, mask )==0) {
224 goto CleanUp;
225 }
226 /* depth buffering w/ stencil */
227 gl_depth_stencil_pixels( ctx, PB->count, PB->x, PB->y, PB->z, mask );
228 }
229 else if (ctx->Depth.Test) {
230 /* regular depth testing */
231 (*ctx->Driver.DepthTestPixels)( ctx, PB->count, PB->x, PB->y, PB->z, mask );
232 }
233
234 if (ctx->RasterMask & NO_DRAW_BIT) {
235 goto CleanUp;
236 }
237
238 /* write pixels */
239 {
240 GLubyte red, green, blue, alpha;
241 red = PB->color[0];
242 green = PB->color[1];
243 blue = PB->color[2];
244 alpha = PB->color[3];
245 (*ctx->Driver.Color)( ctx, red, green, blue, alpha );
246 }
247 (*ctx->Driver.WriteMonocolorPixels)( ctx, PB->count, PB->x, PB->y, mask );
248 if (ctx->RasterMask & ALPHABUF_BIT) {
249 gl_write_mono_alpha_pixels( ctx, PB->count, PB->x, PB->y,
250 PB->color[3], mask );
251 }
252
253 if (ctx->RasterMask & FRONT_AND_BACK_BIT) {
254 /*** Also render to back buffer ***/
255 (*ctx->Driver.SetBuffer)( ctx, GL_BACK );
256 (*ctx->Driver.WriteMonocolorPixels)( ctx, PB->count, PB->x, PB->y, mask );
257 if (ctx->RasterMask & ALPHABUF_BIT) {
258 ctx->Buffer->Alpha = ctx->Buffer->BackAlpha;
259 gl_write_mono_alpha_pixels( ctx, PB->count, PB->x, PB->y,
260 PB->color[3], mask );
261 ctx->Buffer->Alpha = ctx->Buffer->FrontAlpha;
262 }
263 (*ctx->Driver.SetBuffer)( ctx, GL_FRONT );
264 }
265 /*** ALL DONE ***/
266 }
267 }
268 else {
269 /* COLOR INDEX PIXELS */
270
271 /* If we may be writting pixels with different indexes... */
272 if (PB->mono && ctx->MutablePixels) {
273 /* copy index to all pixels */
274 GLuint n = PB->count, indx = PB->index;
275 GLuint *pbindex = PB->i;
276 do {
277 *pbindex++ = indx;
278 n--;
279 } while (n);
280 }
281
282 if (ctx->MutablePixels || !PB->mono) {
283 /* Pixel color index may be modified */
284 GLuint isave[PB_SIZE];
285
286 if (ctx->Fog.Enabled
287 && (ctx->Hint.Fog==GL_NICEST || PB->primitive==GL_BITMAP)) {
288 gl_fog_index_pixels( ctx, PB->count, PB->z, PB->i );
289 }
290
291 /* Scissoring already done above */
292
293 if (ctx->Stencil.Enabled) {
294 /* first stencil test */
295 if (gl_stencil_pixels( ctx, PB->count, PB->x, PB->y, mask )==0) {
296 goto CleanUp;
297 }
298 /* depth buffering w/ stencil */
299 gl_depth_stencil_pixels( ctx, PB->count, PB->x, PB->y, PB->z, mask );
300 }
301 else if (ctx->Depth.Test) {
302 /* regular depth testing */
303 (*ctx->Driver.DepthTestPixels)( ctx, PB->count, PB->x, PB->y, PB->z, mask );
304 }
305
306 if (ctx->RasterMask & NO_DRAW_BIT) {
307 goto CleanUp;
308 }
309
310 if (ctx->RasterMask & FRONT_AND_BACK_BIT) {
311 /* make a copy of the indexes */
312 MEMCPY( isave, PB->i, PB->count * sizeof(GLuint) );
313 }
314
315 if (ctx->Color.SWLogicOpEnabled) {
316 gl_logicop_ci_pixels( ctx, PB->count, PB->x, PB->y, PB->i, mask );
317 }
318
319 if (ctx->Color.SWmasking) {
320 gl_mask_index_pixels( ctx, PB->count, PB->x, PB->y, PB->i, mask );
321 }
322
323 /* write pixels */
324 (*ctx->Driver.WriteIndexPixels)( ctx, PB->count, PB->x, PB->y,
325 PB->i, mask );
326
327 if (ctx->RasterMask & FRONT_AND_BACK_BIT) {
328 /*** Also write to back buffer ***/
329 (*ctx->Driver.SetBuffer)( ctx, GL_BACK );
330 MEMCPY( PB->i, isave, PB->count * sizeof(GLuint) );
331 if (ctx->Color.SWLogicOpEnabled) {
332 gl_logicop_ci_pixels( ctx, PB->count, PB->x, PB->y, PB->i, mask );
333 }
334 if (ctx->Color.SWmasking) {
335 gl_mask_index_pixels( ctx, PB->count, PB->x, PB->y,
336 PB->i, mask );
337 }
338 (*ctx->Driver.WriteIndexPixels)( ctx, PB->count, PB->x, PB->y,
339 PB->i, mask );
340 (*ctx->Driver.SetBuffer)( ctx, GL_FRONT );
341 }
342
343 /*** ALL DONE ***/
344 }
345 else {
346 /* Same color index for all pixels */
347
348 /* Scissoring already done above */
349
350 if (ctx->Stencil.Enabled) {
351 /* first stencil test */
352 if (gl_stencil_pixels( ctx, PB->count, PB->x, PB->y, mask )==0) {
353 goto CleanUp;
354 }
355 /* depth buffering w/ stencil */
356 gl_depth_stencil_pixels( ctx, PB->count, PB->x, PB->y, PB->z, mask );
357 }
358 else if (ctx->Depth.Test) {
359 /* regular depth testing */
360 (*ctx->Driver.DepthTestPixels)( ctx, PB->count, PB->x, PB->y, PB->z, mask );
361 }
362
363 if (ctx->RasterMask & NO_DRAW_BIT) {
364 goto CleanUp;
365 }
366
367 /* write pixels */
368 (*ctx->Driver.Index)( ctx, PB->index );
369 (*ctx->Driver.WriteMonoindexPixels)( ctx, PB->count, PB->x, PB->y, mask );
370
371 if (ctx->RasterMask & FRONT_AND_BACK_BIT) {
372 /*** Also write to back buffer ***/
373 (*ctx->Driver.SetBuffer)( ctx, GL_BACK );
374 (*ctx->Driver.WriteMonoindexPixels)( ctx, PB->count, PB->x, PB->y, mask );
375 (*ctx->Driver.SetBuffer)( ctx, GL_FRONT );
376 }
377 /*** ALL DONE ***/
378 }
379 }
380
381 CleanUp:
382 PB->count = 0;
383 UNDEFARRAY(mask);
384 UNDEFARRAY(rsave);
385 UNDEFARRAY(gsave);
386 UNDEFARRAY(bsave);
387 UNDEFARRAY(asave);
388 }
389
390