1 /* $Id: swrast.h,v 1.34 2003/01/14 04:55:47 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
29 * \file swrast/swrast.h
30 * \brief Defines basic structures for sw_rasterizer.
31 * \author Keith Whitwell <keith@tungstengraphics.com>
41 * \brief Data-structure to handle vertices in the software rasterizer.
43 * The software rasterizer now uses this format for vertices. Thus a
44 * 'RasterSetup' stage or other translation is required between the
45 * tnl module and the swrast rasterization functions. This serves to
46 * isolate the swrast module from the internals of the tnl module, and
47 * improve its usefulness as a fallback mechanism for hardware
50 * Full software drivers:
51 * - Register the rastersetup and triangle functions from
52 * utils/software_helper.
53 * - On statechange, update the rasterization pointers in that module.
55 * Rasterization hardware drivers:
56 * - Keep native rastersetup.
57 * - Implement native twoside,offset and unfilled triangle setup.
58 * - Implement a translator from native vertices to swrast vertices.
59 * - On partial fallback (mix of accelerated and unaccelerated
60 * prims), call a pass-through function which translates native
61 * vertices to SWvertices and calls the appropriate swrast function.
62 * - On total fallback (vertex format insufficient for state or all
63 * primitives unaccelerated), hook in swrast_setup instead.
66 /** win[0], win[1] are the screen-coords of SWvertex. win[2] is the
67 * z-coord. what is win[3]? */
69 GLfloat texcoord
[MAX_TEXTURE_COORD_UNITS
][4];
80 * \brief Contains data for either a horizontal line or a set of
81 * pixels that are passed through a pipeline of functions before being
84 * The sw_span structure describes the colors, Z, fogcoord, texcoords,
85 * etc for either a horizontal run or a set of independent pixels. We
86 * can either specify a base/step to indicate interpolated values, or
87 * fill in arrays of values. The interpMask and arrayMask bitfields
88 * indicate which are active.
90 * With this structure it's easy to hand-off span rasterization to
91 * subroutines instead of doing it all inline in the triangle functions
93 * It also cleans up the local variable namespace a great deal.
95 * It would be interesting to experiment with multiprocessor rasterization
96 * with this structure. The triangle rasterizer could simply emit a
97 * stream of these structures which would be consumed by one or more
98 * span-processing threads which could run in parallel.
103 * \defgroup SpanFlags SPAN_XXX-flags
104 * Bitmasks to indicate which span_arrays need to be computed
105 * (sw_span::interpMask) or have already been filled
106 * (sw_span::arrayMask)
109 #define SPAN_RGBA 0x001
110 #define SPAN_SPEC 0x002
111 #define SPAN_INDEX 0x004
113 #define SPAN_FOG 0x010
114 #define SPAN_TEXTURE 0x020
115 #define SPAN_INT_TEXTURE 0x040
116 #define SPAN_LAMBDA 0x080
117 #define SPAN_COVERAGE 0x100
118 #define SPAN_FLAT 0x200 /**< flat shading? */
119 /** sw_span::arrayMask only - for span_arrays::x, span_arrays::y */
120 #define SPAN_XY 0x400
121 #define SPAN_MASK 0x800 /**< sw_span::arrayMask only */
126 * \struct span_arrays
127 * \brief Arrays of fragment values.
129 * These will either be computed from the x/xStep values above or
130 * filled in by glDraw/CopyPixels, etc.
133 GLchan rgb
[MAX_WIDTH
][3];
134 GLchan rgba
[MAX_WIDTH
][4];
135 GLuint index
[MAX_WIDTH
];
136 GLchan spec
[MAX_WIDTH
][4]; /* specular color */
137 GLint x
[MAX_WIDTH
]; /**< X/Y used for point/line rendering only */
138 GLint y
[MAX_WIDTH
]; /**< X/Y used for point/line rendering only */
139 GLdepth z
[MAX_WIDTH
];
140 GLfloat fog
[MAX_WIDTH
];
141 GLfloat texcoords
[MAX_TEXTURE_COORD_UNITS
][MAX_WIDTH
][4];
142 GLfloat lambda
[MAX_TEXTURE_COORD_UNITS
][MAX_WIDTH
];
143 GLfloat coverage
[MAX_WIDTH
];
145 /** This mask indicates if fragment is alive or culled */
146 GLubyte mask
[MAX_WIDTH
];
153 /** Only need to process pixels between start <= i < end */
154 /** At this time, start is always zero. */
157 /** This flag indicates that mask[] array is effectively filled with ones */
160 /** either GL_POLYGON, GL_LINE, GL_POLYGON, GL_BITMAP */
163 /** 0 = front-facing span, 1 = back-facing span (for two-sided stencil) */
167 * This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates
168 * which of the x/xStep variables are relevant.
172 #if CHAN_TYPE == GL_FLOAT
173 GLfloat red
, redStep
;
174 GLfloat green
, greenStep
;
175 GLfloat blue
, blueStep
;
176 GLfloat alpha
, alphaStep
;
177 GLfloat specRed
, specRedStep
;
178 GLfloat specGreen
, specGreenStep
;
179 GLfloat specBlue
, specBlueStep
;
180 #else /* CHAN_TYPE == GL_UNSIGNED_BYTE or GL_UNSIGNED SHORT */
181 GLfixed red
, redStep
;
182 GLfixed green
, greenStep
;
183 GLfixed blue
, blueStep
;
184 GLfixed alpha
, alphaStep
;
185 GLfixed specRed
, specRedStep
;
186 GLfixed specGreen
, specGreenStep
;
187 GLfixed specBlue
, specBlueStep
;
189 GLfixed index
, indexStep
;
191 GLfloat fog
, fogStep
;
192 GLfloat tex
[MAX_TEXTURE_COORD_UNITS
][4]; /* s, t, r, q */
193 GLfloat texStepX
[MAX_TEXTURE_COORD_UNITS
][4];
194 GLfloat texStepY
[MAX_TEXTURE_COORD_UNITS
][4];
195 GLfixed intTex
[2], intTexStep
[2]; /* s, t only */
198 * This bitmask (of \link SpanFlags SPAN_* flags\endlink) indicates
199 * which of the fragment arrays in the span_arrays struct are relevant.
204 * We store the arrays of fragment values in a separate struct so
205 * that we can allocate sw_span structs on the stack without using
206 * a lot of memory. The span_arrays struct is about 400KB while the
207 * sw_span struct is only about 512 bytes.
209 struct span_arrays
*array
;
213 #define INIT_SPAN(S, PRIMITIVE, END, INTERP_MASK, ARRAY_MASK) \
215 (S).primitive = (PRIMITIVE); \
216 (S).interpMask = (INTERP_MASK); \
217 (S).arrayMask = (ARRAY_MASK); \
221 (S).array = SWRAST_CONTEXT(ctx)->SpanArrays; \
226 struct swrast_device_driver
;
229 /* These are the public-access functions exported from swrast.
232 _swrast_alloc_buffers( GLframebuffer
*buffer
);
235 _swrast_use_read_buffer( GLcontext
*ctx
);
238 _swrast_use_draw_buffer( GLcontext
*ctx
);
241 _swrast_CreateContext( GLcontext
*ctx
);
244 _swrast_DestroyContext( GLcontext
*ctx
);
246 /* Get a (non-const) reference to the device driver struct for swrast.
248 extern struct swrast_device_driver
*
249 _swrast_GetDeviceDriverReference( GLcontext
*ctx
);
252 _swrast_Bitmap( GLcontext
*ctx
,
254 GLsizei width
, GLsizei height
,
255 const struct gl_pixelstore_attrib
*unpack
,
256 const GLubyte
*bitmap
);
259 _swrast_CopyPixels( GLcontext
*ctx
,
260 GLint srcx
, GLint srcy
,
261 GLint destx
, GLint desty
,
262 GLsizei width
, GLsizei height
,
266 _swrast_DrawPixels( GLcontext
*ctx
,
268 GLsizei width
, GLsizei height
,
269 GLenum format
, GLenum type
,
270 const struct gl_pixelstore_attrib
*unpack
,
271 const GLvoid
*pixels
);
274 _swrast_ReadPixels( GLcontext
*ctx
,
275 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
276 GLenum format
, GLenum type
,
277 const struct gl_pixelstore_attrib
*unpack
,
281 _swrast_Clear( GLcontext
*ctx
, GLbitfield mask
, GLboolean all
,
282 GLint x
, GLint y
, GLint width
, GLint height
);
285 _swrast_Accum( GLcontext
*ctx
, GLenum op
,
286 GLfloat value
, GLint xpos
, GLint ypos
,
287 GLint width
, GLint height
);
291 _swrast_DrawBuffer( GLcontext
*ctx
, GLenum mode
);
294 /* Reset the stipple counter
297 _swrast_ResetLineStipple( GLcontext
*ctx
);
299 /* These will always render the correct point/line/triangle for the
302 * For flatshaded primitives, the provoking vertex is the final one.
305 _swrast_Point( GLcontext
*ctx
, const SWvertex
*v
);
308 _swrast_Line( GLcontext
*ctx
, const SWvertex
*v0
, const SWvertex
*v1
);
311 _swrast_Triangle( GLcontext
*ctx
, const SWvertex
*v0
,
312 const SWvertex
*v1
, const SWvertex
*v2
);
315 _swrast_Quad( GLcontext
*ctx
,
316 const SWvertex
*v0
, const SWvertex
*v1
,
317 const SWvertex
*v2
, const SWvertex
*v3
);
320 _swrast_flush( GLcontext
*ctx
);
323 _swrast_render_primitive( GLcontext
*ctx
, GLenum mode
);
326 _swrast_render_start( GLcontext
*ctx
);
329 _swrast_render_finish( GLcontext
*ctx
);
331 /* Tell the software rasterizer about core state changes.
334 _swrast_InvalidateState( GLcontext
*ctx
, GLuint new_state
);
336 /* Configure software rasterizer to match hardware rasterizer characteristics:
339 _swrast_allow_vertex_fog( GLcontext
*ctx
, GLboolean value
);
342 _swrast_allow_pixel_fog( GLcontext
*ctx
, GLboolean value
);
347 _swrast_print_vertex( GLcontext
*ctx
, const SWvertex
*v
);
351 * Imaging fallbacks (a better solution should be found, perhaps
352 * moving all the imaging fallback code to a new module)
355 _swrast_CopyConvolutionFilter2D(GLcontext
*ctx
, GLenum target
,
356 GLenum internalFormat
,
357 GLint x
, GLint y
, GLsizei width
,
360 _swrast_CopyConvolutionFilter1D(GLcontext
*ctx
, GLenum target
,
361 GLenum internalFormat
,
362 GLint x
, GLint y
, GLsizei width
);
364 _swrast_CopyColorSubTable( GLcontext
*ctx
,GLenum target
, GLsizei start
,
365 GLint x
, GLint y
, GLsizei width
);
367 _swrast_CopyColorTable( GLcontext
*ctx
,
368 GLenum target
, GLenum internalformat
,
369 GLint x
, GLint y
, GLsizei width
);
373 * Texture fallbacks, Brian Paul. Could also live in a new module
374 * with the rest of the texture store fallbacks?
377 _swrast_copy_teximage1d(GLcontext
*ctx
, GLenum target
, GLint level
,
378 GLenum internalFormat
,
379 GLint x
, GLint y
, GLsizei width
, GLint border
);
382 _swrast_copy_teximage2d(GLcontext
*ctx
, GLenum target
, GLint level
,
383 GLenum internalFormat
,
384 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
389 _swrast_copy_texsubimage1d(GLcontext
*ctx
, GLenum target
, GLint level
,
390 GLint xoffset
, GLint x
, GLint y
, GLsizei width
);
393 _swrast_copy_texsubimage2d(GLcontext
*ctx
,
394 GLenum target
, GLint level
,
395 GLint xoffset
, GLint yoffset
,
396 GLint x
, GLint y
, GLsizei width
, GLsizei height
);
399 _swrast_copy_texsubimage3d(GLcontext
*ctx
,
400 GLenum target
, GLint level
,
401 GLint xoffset
, GLint yoffset
, GLint zoffset
,
402 GLint x
, GLint y
, GLsizei width
, GLsizei height
);
406 /* The driver interface for the software rasterizer.
407 * Unless otherwise noted, all functions are mandatory.
409 struct swrast_device_driver
{
411 void (*SetBuffer
)( GLcontext
*ctx
, GLframebuffer
*buffer
, GLuint bufferBit
);
413 * Specifies the current buffer for span/pixel writing/reading.
414 * buffer indicates which window to write to / read from. Normally,
415 * this'll be the buffer currently bound to the context, but it doesn't
417 * bufferBit indicates which color buffer, one of:
418 * FRONT_LEFT_BIT - this buffer always exists
419 * BACK_LEFT_BIT - when double buffering
420 * FRONT_RIGHT_BIT - when using stereo
421 * BACK_RIGHT_BIT - when using stereo and double buffering
422 * AUXn_BIT - if aux buffers are implemented
427 *** Functions for synchronizing access to the framebuffer:
430 void (*SpanRenderStart
)(GLcontext
*ctx
);
431 void (*SpanRenderFinish
)(GLcontext
*ctx
);
434 * Called before and after all rendering operations, including DrawPixels,
435 * ReadPixels, Bitmap, span functions, and CopyTexImage, etc commands.
436 * These are a suitable place for grabbing/releasing hardware locks.
438 * NOTE: The swrast triangle/line/point routines *DO NOT* call
439 * these functions. Locking in that case must be organized by the
440 * driver by other mechanisms.
444 *** Functions for writing pixels to the frame buffer:
447 void (*WriteRGBASpan
)( const GLcontext
*ctx
,
448 GLuint n
, GLint x
, GLint y
,
449 CONST GLchan rgba
[][4], const GLubyte mask
[] );
450 void (*WriteRGBSpan
)( const GLcontext
*ctx
,
451 GLuint n
, GLint x
, GLint y
,
452 CONST GLchan rgb
[][3], const GLubyte mask
[] );
453 /* Write a horizontal run of RGBA or RGB pixels.
454 * If mask is NULL, draw all pixels.
455 * If mask is not null, only draw pixel [i] when mask [i] is true.
458 void (*WriteMonoRGBASpan
)( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
459 const GLchan color
[4], const GLubyte mask
[] );
460 /* Write a horizontal run of RGBA pixels all with the same color.
461 * If mask is NULL, draw all pixels.
462 * If mask is not null, only draw pixel [i] when mask [i] is true.
465 void (*WriteRGBAPixels
)( const GLcontext
*ctx
,
466 GLuint n
, const GLint x
[], const GLint y
[],
467 CONST GLchan rgba
[][4], const GLubyte mask
[] );
468 /* Write array of RGBA pixels at random locations.
471 void (*WriteMonoRGBAPixels
)( const GLcontext
*ctx
,
472 GLuint n
, const GLint x
[], const GLint y
[],
473 const GLchan color
[4], const GLubyte mask
[] );
474 /* Write an array of mono-RGBA pixels at random locations.
477 void (*WriteCI32Span
)( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
478 const GLuint index
[], const GLubyte mask
[] );
479 void (*WriteCI8Span
)( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
480 const GLubyte index
[], const GLubyte mask
[] );
481 /* Write a horizontal run of CI pixels. One function is for 32bpp
482 * indexes and the other for 8bpp pixels (the common case). You mus
483 * implement both for color index mode.
484 * If mask is NULL, draw all pixels.
485 * If mask is not null, only draw pixel [i] when mask [i] is true.
488 void (*WriteMonoCISpan
)( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
489 GLuint colorIndex
, const GLubyte mask
[] );
490 /* Write a horizontal run of color index pixels using the color index
491 * last specified by the Index() function.
492 * If mask is NULL, draw all pixels.
493 * If mask is not null, only draw pixel [i] when mask [i] is true.
496 void (*WriteCI32Pixels
)( const GLcontext
*ctx
,
497 GLuint n
, const GLint x
[], const GLint y
[],
498 const GLuint index
[], const GLubyte mask
[] );
500 * Write a random array of CI pixels.
503 void (*WriteMonoCIPixels
)( const GLcontext
*ctx
,
504 GLuint n
, const GLint x
[], const GLint y
[],
505 GLuint colorIndex
, const GLubyte mask
[] );
506 /* Write a random array of color index pixels using the color index
507 * last specified by the Index() function.
512 *** Functions to read pixels from frame buffer:
515 void (*ReadCI32Span
)( const GLcontext
*ctx
,
516 GLuint n
, GLint x
, GLint y
, GLuint index
[] );
517 /* Read a horizontal run of color index pixels.
520 void (*ReadRGBASpan
)( const GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
522 /* Read a horizontal run of RGBA pixels.
525 void (*ReadCI32Pixels
)( const GLcontext
*ctx
,
526 GLuint n
, const GLint x
[], const GLint y
[],
527 GLuint indx
[], const GLubyte mask
[] );
528 /* Read a random array of CI pixels.
531 void (*ReadRGBAPixels
)( const GLcontext
*ctx
,
532 GLuint n
, const GLint x
[], const GLint y
[],
533 GLchan rgba
[][4], const GLubyte mask
[] );
534 /* Read a random array of RGBA pixels.
540 *** For supporting hardware Z buffers:
541 *** Either ALL or NONE of these functions must be implemented!
542 *** NOTE that Each depth value is a 32-bit GLuint. If the depth
543 *** buffer is less than 32 bits deep then the extra upperbits are zero.
546 void (*WriteDepthSpan
)( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
547 const GLdepth depth
[], const GLubyte mask
[] );
548 /* Write a horizontal span of values into the depth buffer. Only write
549 * depth[i] value if mask[i] is nonzero.
552 void (*ReadDepthSpan
)( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
554 /* Read a horizontal span of values from the depth buffer.
558 void (*WriteDepthPixels
)( GLcontext
*ctx
, GLuint n
,
559 const GLint x
[], const GLint y
[],
560 const GLdepth depth
[], const GLubyte mask
[] );
561 /* Write an array of randomly positioned depth values into the
562 * depth buffer. Only write depth[i] value if mask[i] is nonzero.
565 void (*ReadDepthPixels
)( GLcontext
*ctx
, GLuint n
,
566 const GLint x
[], const GLint y
[],
568 /* Read an array of randomly positioned depth values from the depth buffer.
574 *** For supporting hardware stencil buffers:
575 *** Either ALL or NONE of these functions must be implemented!
578 void (*WriteStencilSpan
)( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
579 const GLstencil stencil
[], const GLubyte mask
[] );
580 /* Write a horizontal span of stencil values into the stencil buffer.
581 * If mask is NULL, write all stencil values.
582 * Else, only write stencil[i] if mask[i] is non-zero.
585 void (*ReadStencilSpan
)( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
586 GLstencil stencil
[] );
587 /* Read a horizontal span of stencil values from the stencil buffer.
590 void (*WriteStencilPixels
)( GLcontext
*ctx
, GLuint n
,
591 const GLint x
[], const GLint y
[],
592 const GLstencil stencil
[],
593 const GLubyte mask
[] );
594 /* Write an array of stencil values into the stencil buffer.
595 * If mask is NULL, write all stencil values.
596 * Else, only write stencil[i] if mask[i] is non-zero.
599 void (*ReadStencilPixels
)( GLcontext
*ctx
, GLuint n
,
600 const GLint x
[], const GLint y
[],
601 GLstencil stencil
[] );
602 /* Read an array of stencil values from the stencil buffer.