c65631b1c5953bcdb847937b0b115d40fcb9c071
[mesa.git] / src / mesa / swrast / swrast.h
1 /* $Id: swrast.h,v 1.16 2002/01/27 18:32:03 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Authors:
27 * Keith Whitwell <keithw@valinux.com>
28 */
29
30 #ifndef SWRAST_H
31 #define SWRAST_H
32
33 #include "mtypes.h"
34
35
36 /* The software rasterizer now uses this format for vertices. Thus a
37 * 'RasterSetup' stage or other translation is required between the
38 * tnl module and the swrast rasterization functions. This serves to
39 * isolate the swrast module from the internals of the tnl module, and
40 * improve its usefulness as a fallback mechanism for hardware
41 * drivers.
42 *
43 * Full software drivers:
44 * - Register the rastersetup and triangle functions from
45 * utils/software_helper.
46 * - On statechange, update the rasterization pointers in that module.
47 *
48 * Rasterization hardware drivers:
49 * - Keep native rastersetup.
50 * - Implement native twoside,offset and unfilled triangle setup.
51 * - Implement a translator from native vertices to swrast vertices.
52 * - On partial fallback (mix of accelerated and unaccelerated
53 * prims), call a pass-through function which translates native
54 * vertices to SWvertices and calls the appropriate swrast function.
55 * - On total fallback (vertex format insufficient for state or all
56 * primitives unaccelerated), hook in swrast_setup instead.
57 */
58 typedef struct {
59 GLfloat win[4];
60 GLfloat texcoord[MAX_TEXTURE_UNITS][4];
61 GLchan color[4];
62 GLchan specular[4];
63 GLfloat fog;
64 GLuint index;
65 GLfloat pointSize;
66 } SWvertex;
67
68
69 /*
70 * The sw_span structure is used by the triangle template code in
71 * s_tritemp.h. It describes how colors, Z, texcoords, etc are to be
72 * interpolated across each scanline of triangle.
73 * With this structure it's easy to hand-off span rasterization to a
74 * subroutine instead of doing it all inline like we used to do.
75 * It also cleans up the local variable namespace a great deal.
76 *
77 * It would be interesting to experiment with multiprocessor rasterization
78 * with this structure. The triangle rasterizer could simply emit a
79 * stream of these structures which would be consumed by one or more
80 * span-processing threads which could run in parallel.
81 */
82
83
84 /* When the sw_span struct is initialized, these flags indicates
85 * which values are needed for rendering the triangle.
86 */
87 #define SPAN_RGBA 0x001
88 #define SPAN_SPEC 0x002
89 #define SPAN_INDEX 0x004
90 #define SPAN_Z 0x008
91 #define SPAN_FOG 0x010
92 #define SPAN_TEXTURE 0x020
93 #define SPAN_INT_TEXTURE 0x040
94 #define SPAN_LAMBDA 0x080
95 #define SPAN_COVERAGE 0x100
96 #define SPAN_FLAT 0x200 /* flat shading? */
97
98
99 struct sw_span {
100 GLint x, y;
101
102 /* only need to process pixels between start <= i < end */
103 GLuint start, end;
104
105 /* This flag indicates that only a part of the span is visible */
106 GLboolean writeAll;
107
108 /* This bitmask (bitwise-or of SPAN_* flags) indicates which of the
109 * x/xStep variables are relevant.
110 */
111 GLuint interpMask;
112
113 #if CHAN_TYPE == GL_FLOAT
114 GLfloat red, redStep;
115 GLfloat green, greenStep;
116 GLfloat blue, blueStep;
117 GLfloat alpha, alphaStep;
118 GLfloat specRed, specRedStep;
119 GLfloat specGreen, specGreenStep;
120 GLfloat specBlue, specBlueStep;
121 #else /* CHAN_TYPE == */
122 GLfixed red, redStep;
123 GLfixed green, greenStep;
124 GLfixed blue, blueStep;
125 GLfixed alpha, alphaStep;
126 GLfixed specRed, specRedStep;
127 GLfixed specGreen, specGreenStep;
128 GLfixed specBlue, specBlueStep;
129 #endif
130 GLfixed index, indexStep;
131 GLfixed z, zStep;
132 GLfloat fog, fogStep;
133 GLfloat tex[MAX_TEXTURE_UNITS][4], texStep[MAX_TEXTURE_UNITS][4];
134 GLfixed intTex[2], intTexStep[2];
135 /* Needed for texture lambda (LOD) computation */
136 GLfloat rho[MAX_TEXTURE_UNITS];
137 GLfloat texWidth[MAX_TEXTURE_UNITS], texHeight[MAX_TEXTURE_UNITS];
138
139 /* This bitmask (bitwise-or of SPAN_* flags) indicates which of the
140 * fragment arrays are relevant.
141 */
142 GLuint arrayMask;
143
144 /**
145 * Arrays of fragment values. These will either be computed from the
146 * x/xStep values above or loadd from glDrawPixels, etc.
147 */
148 union {
149 GLchan rgb[MAX_WIDTH][3];
150 GLchan rgba[MAX_WIDTH][4];
151 GLuint index[MAX_WIDTH];
152 } color;
153 GLchan specArray[MAX_WIDTH][4];
154 GLdepth zArray[MAX_WIDTH];
155 GLfloat fogArray[MAX_WIDTH];
156 /* Texture (s,t,r). 4th component only used for pixel texture */
157 GLfloat texcoords[MAX_TEXTURE_UNITS][MAX_WIDTH][4];
158 GLfloat lambda[MAX_TEXTURE_UNITS][MAX_WIDTH];
159 GLfloat coverage[MAX_WIDTH];
160
161 /* This mask indicates if fragment is alive or culled */
162 GLubyte mask[MAX_WIDTH];
163
164 #ifdef DEBUG
165 GLboolean filledDepth, filledAlpha;
166 GLboolean filledColor, filledSpecular;
167 GLboolean filledLambda[MAX_TEXTURE_UNITS], filledTex[MAX_TEXTURE_UNITS];
168 #endif
169 };
170
171
172 #define INIT_SPAN(S) \
173 do { \
174 S.interpMask = 0; \
175 S.arrayMask = 0; \
176 S.start = S.end = 0; \
177 } while (0)
178
179
180 #ifdef DEBUG
181 #define SW_SPAN_SET_FLAG(flag) {ASSERT((flag) == GL_FALSE);(flag) = GL_TRUE;}
182 #define SW_SPAN_RESET(span) { \
183 (span).filledDepth = (span).filledAlpha \
184 = (span).filledColor = (span).filledSpecular = GL_FALSE; \
185 MEMSET((span).filledTex, GL_FALSE, \
186 MAX_TEXTURE_UNITS*sizeof(GLboolean)); \
187 MEMSET((span).filledLambda, GL_FALSE, \
188 MAX_TEXTURE_UNITS*sizeof(GLboolean)); \
189 (span).start = 0; (span).writeAll = GL_TRUE;}
190 #else
191 #define SW_SPAN_SET_FLAG(flag) ;
192 #define SW_SPAN_RESET(span) {(span).start = 0;(span).writeAll = GL_TRUE;}
193 #endif
194
195 struct swrast_device_driver;
196
197
198 /* These are the public-access functions exported from swrast.
199 */
200 extern void
201 _swrast_alloc_buffers( GLcontext *ctx );
202
203 extern GLboolean
204 _swrast_CreateContext( GLcontext *ctx );
205
206 extern void
207 _swrast_DestroyContext( GLcontext *ctx );
208
209 /* Get a (non-const) reference to the device driver struct for swrast.
210 */
211 extern struct swrast_device_driver *
212 _swrast_GetDeviceDriverReference( GLcontext *ctx );
213
214 extern void
215 _swrast_Bitmap( GLcontext *ctx,
216 GLint px, GLint py,
217 GLsizei width, GLsizei height,
218 const struct gl_pixelstore_attrib *unpack,
219 const GLubyte *bitmap );
220
221 extern void
222 _swrast_CopyPixels( GLcontext *ctx,
223 GLint srcx, GLint srcy,
224 GLint destx, GLint desty,
225 GLsizei width, GLsizei height,
226 GLenum type );
227
228 extern void
229 _swrast_DrawPixels( GLcontext *ctx,
230 GLint x, GLint y,
231 GLsizei width, GLsizei height,
232 GLenum format, GLenum type,
233 const struct gl_pixelstore_attrib *unpack,
234 const GLvoid *pixels );
235
236 extern void
237 _swrast_ReadPixels( GLcontext *ctx,
238 GLint x, GLint y, GLsizei width, GLsizei height,
239 GLenum format, GLenum type,
240 const struct gl_pixelstore_attrib *unpack,
241 GLvoid *pixels );
242
243 extern void
244 _swrast_Clear( GLcontext *ctx, GLbitfield mask, GLboolean all,
245 GLint x, GLint y, GLint width, GLint height );
246
247 extern void
248 _swrast_Accum( GLcontext *ctx, GLenum op,
249 GLfloat value, GLint xpos, GLint ypos,
250 GLint width, GLint height );
251
252
253 /* Reset the stipple counter
254 */
255 extern void
256 _swrast_ResetLineStipple( GLcontext *ctx );
257
258 /* These will always render the correct point/line/triangle for the
259 * current state.
260 *
261 * For flatshaded primitives, the provoking vertex is the final one.
262 */
263 extern void
264 _swrast_Point( GLcontext *ctx, const SWvertex *v );
265
266 extern void
267 _swrast_Line( GLcontext *ctx, const SWvertex *v0, const SWvertex *v1 );
268
269 extern void
270 _swrast_Triangle( GLcontext *ctx, const SWvertex *v0,
271 const SWvertex *v1, const SWvertex *v2 );
272
273 extern void
274 _swrast_Quad( GLcontext *ctx,
275 const SWvertex *v0, const SWvertex *v1,
276 const SWvertex *v2, const SWvertex *v3);
277
278 extern void
279 _swrast_flush( GLcontext *ctx );
280
281
282 /* Tell the software rasterizer about core state changes.
283 */
284 extern void
285 _swrast_InvalidateState( GLcontext *ctx, GLuint new_state );
286
287 /* Configure software rasterizer to match hardware rasterizer characteristics:
288 */
289 extern void
290 _swrast_allow_vertex_fog( GLcontext *ctx, GLboolean value );
291
292 extern void
293 _swrast_allow_pixel_fog( GLcontext *ctx, GLboolean value );
294
295 /* Debug:
296 */
297 extern void
298 _swrast_print_vertex( GLcontext *ctx, const SWvertex *v );
299
300
301 /*
302 * Imaging fallbacks (a better solution should be found, perhaps
303 * moving all the imaging fallback code to a new module)
304 */
305 void
306 _swrast_CopyConvolutionFilter2D(GLcontext *ctx, GLenum target,
307 GLenum internalFormat,
308 GLint x, GLint y, GLsizei width,
309 GLsizei height);
310 void
311 _swrast_CopyConvolutionFilter1D(GLcontext *ctx, GLenum target,
312 GLenum internalFormat,
313 GLint x, GLint y, GLsizei width);
314 void
315 _swrast_CopyColorSubTable( GLcontext *ctx,GLenum target, GLsizei start,
316 GLint x, GLint y, GLsizei width);
317 void
318 _swrast_CopyColorTable( GLcontext *ctx,
319 GLenum target, GLenum internalformat,
320 GLint x, GLint y, GLsizei width);
321
322
323 /*
324 * Texture fallbacks, Brian Paul. Could also live in a new module
325 * with the rest of the texture store fallbacks?
326 */
327 extern void
328 _swrast_copy_teximage1d(GLcontext *ctx, GLenum target, GLint level,
329 GLenum internalFormat,
330 GLint x, GLint y, GLsizei width, GLint border);
331
332 extern void
333 _swrast_copy_teximage2d(GLcontext *ctx, GLenum target, GLint level,
334 GLenum internalFormat,
335 GLint x, GLint y, GLsizei width, GLsizei height,
336 GLint border);
337
338
339 extern void
340 _swrast_copy_texsubimage1d(GLcontext *ctx, GLenum target, GLint level,
341 GLint xoffset, GLint x, GLint y, GLsizei width);
342
343 extern void
344 _swrast_copy_texsubimage2d(GLcontext *ctx,
345 GLenum target, GLint level,
346 GLint xoffset, GLint yoffset,
347 GLint x, GLint y, GLsizei width, GLsizei height);
348
349 extern void
350 _swrast_copy_texsubimage3d(GLcontext *ctx,
351 GLenum target, GLint level,
352 GLint xoffset, GLint yoffset, GLint zoffset,
353 GLint x, GLint y, GLsizei width, GLsizei height);
354
355
356
357 /* The driver interface for the software rasterizer. Unless otherwise
358 * noted, all functions are mandatory.
359 */
360 struct swrast_device_driver {
361
362 void (*SetReadBuffer)( GLcontext *ctx, GLframebuffer *colorBuffer,
363 GLenum buffer );
364 /*
365 * Specifies the current buffer for span/pixel reading.
366 * colorBuffer will be one of:
367 * GL_FRONT_LEFT - this buffer always exists
368 * GL_BACK_LEFT - when double buffering
369 * GL_FRONT_RIGHT - when using stereo
370 * GL_BACK_RIGHT - when using stereo and double buffering
371 */
372
373
374 /***
375 *** Functions for synchronizing access to the framebuffer:
376 ***/
377
378 void (*SpanRenderStart)(GLcontext *ctx);
379 void (*SpanRenderFinish)(GLcontext *ctx);
380 /* OPTIONAL.
381 *
382 * Called before and after all rendering operations, including DrawPixels,
383 * ReadPixels, Bitmap, span functions, and CopyTexImage, etc commands.
384 * These are a suitable place for grabbing/releasing hardware locks.
385 *
386 * NOTE: The swrast triangle/line/point routines *DO NOT* call
387 * these functions. Locking in that case must be organized by the
388 * driver by other mechanisms.
389 */
390
391 /***
392 *** Functions for writing pixels to the frame buffer:
393 ***/
394
395 void (*WriteRGBASpan)( const GLcontext *ctx,
396 GLuint n, GLint x, GLint y,
397 CONST GLchan rgba[][4], const GLubyte mask[] );
398 void (*WriteRGBSpan)( const GLcontext *ctx,
399 GLuint n, GLint x, GLint y,
400 CONST GLchan rgb[][3], const GLubyte mask[] );
401 /* Write a horizontal run of RGBA or RGB pixels.
402 * If mask is NULL, draw all pixels.
403 * If mask is not null, only draw pixel [i] when mask [i] is true.
404 */
405
406 void (*WriteMonoRGBASpan)( const GLcontext *ctx, GLuint n, GLint x, GLint y,
407 const GLchan color[4], const GLubyte mask[] );
408 /* Write a horizontal run of RGBA pixels all with the same color.
409 */
410
411 void (*WriteRGBAPixels)( const GLcontext *ctx,
412 GLuint n, const GLint x[], const GLint y[],
413 CONST GLchan rgba[][4], const GLubyte mask[] );
414 /* Write array of RGBA pixels at random locations.
415 */
416
417 void (*WriteMonoRGBAPixels)( const GLcontext *ctx,
418 GLuint n, const GLint x[], const GLint y[],
419 const GLchan color[4], const GLubyte mask[] );
420 /* Write an array of mono-RGBA pixels at random locations.
421 */
422
423 void (*WriteCI32Span)( const GLcontext *ctx, GLuint n, GLint x, GLint y,
424 const GLuint index[], const GLubyte mask[] );
425 void (*WriteCI8Span)( const GLcontext *ctx, GLuint n, GLint x, GLint y,
426 const GLubyte index[], const GLubyte mask[] );
427 /* Write a horizontal run of CI pixels. One function is for 32bpp
428 * indexes and the other for 8bpp pixels (the common case). You mus
429 * implement both for color index mode.
430 */
431
432 void (*WriteMonoCISpan)( const GLcontext *ctx, GLuint n, GLint x, GLint y,
433 GLuint colorIndex, const GLubyte mask[] );
434 /* Write a horizontal run of color index pixels using the color index
435 * last specified by the Index() function.
436 */
437
438 void (*WriteCI32Pixels)( const GLcontext *ctx,
439 GLuint n, const GLint x[], const GLint y[],
440 const GLuint index[], const GLubyte mask[] );
441 /*
442 * Write a random array of CI pixels.
443 */
444
445 void (*WriteMonoCIPixels)( const GLcontext *ctx,
446 GLuint n, const GLint x[], const GLint y[],
447 GLuint colorIndex, const GLubyte mask[] );
448 /* Write a random array of color index pixels using the color index
449 * last specified by the Index() function.
450 */
451
452
453 /***
454 *** Functions to read pixels from frame buffer:
455 ***/
456
457 void (*ReadCI32Span)( const GLcontext *ctx,
458 GLuint n, GLint x, GLint y, GLuint index[] );
459 /* Read a horizontal run of color index pixels.
460 */
461
462 void (*ReadRGBASpan)( const GLcontext *ctx, GLuint n, GLint x, GLint y,
463 GLchan rgba[][4] );
464 /* Read a horizontal run of RGBA pixels.
465 */
466
467 void (*ReadCI32Pixels)( const GLcontext *ctx,
468 GLuint n, const GLint x[], const GLint y[],
469 GLuint indx[], const GLubyte mask[] );
470 /* Read a random array of CI pixels.
471 */
472
473 void (*ReadRGBAPixels)( const GLcontext *ctx,
474 GLuint n, const GLint x[], const GLint y[],
475 GLchan rgba[][4], const GLubyte mask[] );
476 /* Read a random array of RGBA pixels.
477 */
478
479
480
481 /***
482 *** For supporting hardware Z buffers:
483 *** Either ALL or NONE of these functions must be implemented!
484 *** NOTE that Each depth value is a 32-bit GLuint. If the depth
485 *** buffer is less than 32 bits deep then the extra upperbits are zero.
486 ***/
487
488 void (*WriteDepthSpan)( GLcontext *ctx, GLuint n, GLint x, GLint y,
489 const GLdepth depth[], const GLubyte mask[] );
490 /* Write a horizontal span of values into the depth buffer. Only write
491 * depth[i] value if mask[i] is nonzero.
492 */
493
494 void (*ReadDepthSpan)( GLcontext *ctx, GLuint n, GLint x, GLint y,
495 GLdepth depth[] );
496 /* Read a horizontal span of values from the depth buffer.
497 */
498
499
500 void (*WriteDepthPixels)( GLcontext *ctx, GLuint n,
501 const GLint x[], const GLint y[],
502 const GLdepth depth[], const GLubyte mask[] );
503 /* Write an array of randomly positioned depth values into the
504 * depth buffer. Only write depth[i] value if mask[i] is nonzero.
505 */
506
507 void (*ReadDepthPixels)( GLcontext *ctx, GLuint n,
508 const GLint x[], const GLint y[],
509 GLdepth depth[] );
510 /* Read an array of randomly positioned depth values from the depth buffer.
511 */
512
513
514
515 /***
516 *** For supporting hardware stencil buffers:
517 *** Either ALL or NONE of these functions must be implemented!
518 ***/
519
520 void (*WriteStencilSpan)( GLcontext *ctx, GLuint n, GLint x, GLint y,
521 const GLstencil stencil[], const GLubyte mask[] );
522 /* Write a horizontal span of stencil values into the stencil buffer.
523 * If mask is NULL, write all stencil values.
524 * Else, only write stencil[i] if mask[i] is non-zero.
525 */
526
527 void (*ReadStencilSpan)( GLcontext *ctx, GLuint n, GLint x, GLint y,
528 GLstencil stencil[] );
529 /* Read a horizontal span of stencil values from the stencil buffer.
530 */
531
532 void (*WriteStencilPixels)( GLcontext *ctx, GLuint n,
533 const GLint x[], const GLint y[],
534 const GLstencil stencil[],
535 const GLubyte mask[] );
536 /* Write an array of stencil values into the stencil buffer.
537 * If mask is NULL, write all stencil values.
538 * Else, only write stencil[i] if mask[i] is non-zero.
539 */
540
541 void (*ReadStencilPixels)( GLcontext *ctx, GLuint n,
542 const GLint x[], const GLint y[],
543 GLstencil stencil[] );
544 /* Read an array of stencil values from the stencil buffer.
545 */
546 };
547
548
549
550 #endif