3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include "s_context.h"
33 #include "s_stencil.h"
38 * Helper function called from _swrast_write_zoomed_rgba/rgb/index_span().
41 zoom_span( GLcontext
*ctx
, const struct sw_span
*span
,
42 const GLvoid
*src
, GLint y0
, GLenum format
, GLint skipPixels
)
45 GLint c0
, c1
, skipCol
;
47 const GLuint maxWidth
= MIN2( ctx
->DrawBuffer
->Width
, MAX_WIDTH
);
48 struct sw_span zoomed
;
49 struct span_arrays zoomed_arrays
; /* this is big! */
51 /* no pixel arrays! must be horizontal spans. */
52 ASSERT((span
->arrayMask
& SPAN_XY
) == 0);
53 ASSERT(span
->primitive
== GL_BITMAP
);
55 INIT_SPAN(zoomed
, GL_BITMAP
, 0, 0, 0);
56 zoomed
.array
= &zoomed_arrays
;
58 /* copy fog interp info */
59 zoomed
.fog
= span
->fog
;
60 zoomed
.fogStep
= span
->fogStep
;
61 /* XXX copy texcoord info? */
63 if (format
== GL_RGBA
|| format
== GL_RGB
) {
66 zoomed
.zStep
= span
->zStep
;
67 /* we'll generate an array of colorss */
68 zoomed
.interpMask
= span
->interpMask
& ~SPAN_RGBA
;
69 zoomed
.arrayMask
|= SPAN_RGBA
;
71 else if (format
== GL_COLOR_INDEX
) {
74 zoomed
.zStep
= span
->zStep
;
75 /* we'll generate an array of color indexes */
76 zoomed
.interpMask
= span
->interpMask
& ~SPAN_INDEX
;
77 zoomed
.arrayMask
|= SPAN_INDEX
;
80 assert(format
== GL_DEPTH_COMPONENT
);
82 zoomed
.red
= span
->red
;
83 zoomed
.green
= span
->green
;
84 zoomed
.blue
= span
->blue
;
85 zoomed
.alpha
= span
->alpha
;
86 zoomed
.redStep
= span
->redStep
;
87 zoomed
.greenStep
= span
->greenStep
;
88 zoomed
.blueStep
= span
->blueStep
;
89 zoomed
.alphaStep
= span
->alphaStep
;
90 /* we'll generate an array of depth values */
91 zoomed
.interpMask
= span
->interpMask
& ~SPAN_Z
;
92 zoomed
.arrayMask
|= SPAN_Z
;
96 * Compute which columns to draw: [c0, c1)
98 c0
= (GLint
) (span
->x
+ skipPixels
* ctx
->Pixel
.ZoomX
);
99 c1
= (GLint
) (span
->x
+ (skipPixels
+ span
->end
) * ctx
->Pixel
.ZoomX
);
118 zoomed
.end
= c1
- c0
;
121 if (zoomed
.end
> maxWidth
)
122 zoomed
.end
= maxWidth
;
125 * Compute which rows to draw: [r0, r1)
128 r0
= y0
+ (GLint
) (row
* ctx
->Pixel
.ZoomY
);
129 r1
= y0
+ (GLint
) ((row
+1) * ctx
->Pixel
.ZoomY
);
144 * Trivial clip rejection testing.
146 if (r1
< 0) /* below window */
148 if (r0
>= (GLint
) ctx
->DrawBuffer
->Height
) /* above window */
150 if (c1
< 0) /* left of window */
152 if (c0
>= (GLint
) ctx
->DrawBuffer
->Width
) /* right of window */
155 /* zoom the span horizontally */
156 if (format
== GL_RGBA
) {
157 const GLchan (*rgba
)[4] = (const GLchan (*)[4]) src
;
158 if (ctx
->Pixel
.ZoomX
== -1.0F
) {
160 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
161 i
= span
->end
- (j
+ skipCol
) - 1;
162 COPY_CHAN4(zoomed
.array
->rgba
[j
], rgba
[i
]);
166 /* general solution */
167 const GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
168 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
169 i
= (GLint
) ((j
+ skipCol
) * xscale
);
170 if (ctx
->Pixel
.ZoomX
< 0.0) {
172 i
= span
->end
+ i
- 1;
175 ASSERT(i
< (GLint
) span
->end
);
176 COPY_CHAN4(zoomed
.array
->rgba
[j
], rgba
[i
]);
180 else if (format
== GL_RGB
) {
181 const GLchan (*rgb
)[3] = (const GLchan (*)[3]) src
;
182 if (ctx
->Pixel
.ZoomX
== -1.0F
) {
184 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
185 i
= span
->end
- (j
+ skipCol
) - 1;
186 zoomed
.array
->rgba
[j
][0] = rgb
[i
][0];
187 zoomed
.array
->rgba
[j
][1] = rgb
[i
][1];
188 zoomed
.array
->rgba
[j
][2] = rgb
[i
][2];
189 zoomed
.array
->rgba
[j
][3] = CHAN_MAX
;
193 /* general solution */
194 const GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
195 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
196 i
= (GLint
) ((j
+ skipCol
) * xscale
);
197 if (ctx
->Pixel
.ZoomX
< 0.0) {
199 i
= span
->end
+ i
- 1;
202 ASSERT(i
< (GLint
) span
->end
);
203 zoomed
.array
->rgba
[j
][0] = rgb
[i
][0];
204 zoomed
.array
->rgba
[j
][1] = rgb
[i
][1];
205 zoomed
.array
->rgba
[j
][2] = rgb
[i
][2];
206 zoomed
.array
->rgba
[j
][3] = CHAN_MAX
;
210 else if (format
== GL_COLOR_INDEX
) {
211 const GLuint
*indexes
= (const GLuint
*) src
;
212 if (ctx
->Pixel
.ZoomX
== -1.0F
) {
214 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
215 i
= span
->end
- (j
+ skipCol
) - 1;
216 zoomed
.array
->index
[j
] = indexes
[i
];
220 /* general solution */
221 const GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
222 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
223 i
= (GLint
) ((j
+ skipCol
) * xscale
);
224 if (ctx
->Pixel
.ZoomX
< 0.0) {
226 i
= span
->end
+ i
- 1;
229 ASSERT(i
< (GLint
) span
->end
);
230 zoomed
.array
->index
[j
] = indexes
[i
];
235 const GLdepth
*zValues
= (const GLuint
*) src
;
236 assert(format
== GL_DEPTH_COMPONENT
);
237 if (ctx
->Pixel
.ZoomX
== -1.0F
) {
239 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
240 i
= span
->end
- (j
+ skipCol
) - 1;
241 zoomed
.array
->z
[j
] = zValues
[i
];
245 /* general solution */
246 const GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
247 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
248 i
= (GLint
) ((j
+ skipCol
) * xscale
);
249 if (ctx
->Pixel
.ZoomX
< 0.0) {
251 i
= span
->end
+ i
- 1;
254 ASSERT(i
< (GLint
) span
->end
);
255 zoomed
.array
->z
[j
] = zValues
[i
];
258 /* Now, fall into either the RGB or COLOR_INDEX path below */
259 if (ctx
->Visual
.rgbMode
)
262 format
= GL_COLOR_INDEX
;
266 /* write the span in rows [r0, r1) */
267 if (format
== GL_RGBA
|| format
== GL_RGB
) {
268 /* Writing the span may modify the colors, so make a backup now if we're
269 * going to call _swrast_write_zoomed_span() more than once.
270 * Also, clipping may change the span end value, so store it as well.
272 GLchan rgbaSave
[MAX_WIDTH
][4];
273 const GLint end
= zoomed
.end
; /* save */
275 MEMCPY(rgbaSave
, zoomed
.array
->rgba
, zoomed
.end
* 4 * sizeof(GLchan
));
277 for (zoomed
.y
= r0
; zoomed
.y
< r1
; zoomed
.y
++) {
278 _swrast_write_rgba_span(ctx
, &zoomed
);
279 zoomed
.end
= end
; /* restore */
281 /* restore the colors */
282 MEMCPY(zoomed
.array
->rgba
, rgbaSave
, zoomed
.end
*4 * sizeof(GLchan
));
286 else if (format
== GL_COLOR_INDEX
) {
287 GLuint indexSave
[MAX_WIDTH
];
288 const GLint end
= zoomed
.end
; /* save */
290 MEMCPY(indexSave
, zoomed
.array
->index
, zoomed
.end
* sizeof(GLuint
));
292 for (zoomed
.y
= r0
; zoomed
.y
< r1
; zoomed
.y
++) {
293 _swrast_write_index_span(ctx
, &zoomed
);
294 zoomed
.end
= end
; /* restore */
296 /* restore the colors */
297 MEMCPY(zoomed
.array
->index
, indexSave
, zoomed
.end
* sizeof(GLuint
));
305 _swrast_write_zoomed_rgba_span( GLcontext
*ctx
, const struct sw_span
*span
,
306 CONST GLchan rgba
[][4], GLint y0
,
309 zoom_span(ctx
, span
, (const GLvoid
*) rgba
, y0
, GL_RGBA
, skipPixels
);
314 _swrast_write_zoomed_rgb_span( GLcontext
*ctx
, const struct sw_span
*span
,
315 CONST GLchan rgb
[][3], GLint y0
,
318 zoom_span(ctx
, span
, (const GLvoid
*) rgb
, y0
, GL_RGB
, skipPixels
);
323 _swrast_write_zoomed_index_span( GLcontext
*ctx
, const struct sw_span
*span
,
324 GLint y0
, GLint skipPixels
)
326 zoom_span(ctx
, span
, (const GLvoid
*) span
->array
->index
, y0
,
327 GL_COLOR_INDEX
, skipPixels
);
332 _swrast_write_zoomed_depth_span( GLcontext
*ctx
, const struct sw_span
*span
,
333 GLint y0
, GLint skipPixels
)
335 zoom_span(ctx
, span
, (const GLvoid
*) span
->array
->z
, y0
,
336 GL_DEPTH_COMPONENT
, skipPixels
);
341 * As above, but write stencil values.
344 _swrast_write_zoomed_stencil_span( GLcontext
*ctx
,
345 GLuint n
, GLint x
, GLint y
,
346 const GLstencil stencil
[], GLint y0
,
350 GLint r0
, r1
, row
, r
;
352 GLstencil zstencil
[MAX_WIDTH
]; /* zoomed stencil values */
353 GLint maxwidth
= MIN2( ctx
->DrawBuffer
->Width
, MAX_WIDTH
);
355 (void) skipPixels
; /* XXX this shouldn't be ignored */
357 /* compute width of output row */
358 m
= (GLint
) FABSF( n
* ctx
->Pixel
.ZoomX
);
362 if (ctx
->Pixel
.ZoomX
<0.0) {
363 /* adjust x coordinate for left/right mirroring */
367 /* compute which rows to draw */
369 r0
= y0
+ (GLint
) (row
* ctx
->Pixel
.ZoomY
);
370 r1
= y0
+ (GLint
) ((row
+1) * ctx
->Pixel
.ZoomY
);
380 /* return early if r0...r1 is above or below window */
385 if (r0
>= (GLint
) ctx
->DrawBuffer
->Height
&&
386 r1
>= (GLint
) ctx
->DrawBuffer
->Height
) {
391 /* check if left edge is outside window */
397 /* make sure span isn't too long or short */
405 ASSERT( m
<= MAX_WIDTH
);
407 /* zoom the span horizontally */
408 if (ctx
->Pixel
.ZoomX
==-1.0F
) {
411 i
= n
- (j
+skipcol
) - 1;
412 zstencil
[j
] = stencil
[i
];
416 GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
418 i
= (GLint
) ((j
+skipcol
) * xscale
);
419 if (i
<0) i
= n
+ i
- 1;
420 zstencil
[j
] = stencil
[i
];
425 for (r
=r0
; r
<r1
; r
++) {
426 _swrast_write_stencil_span( ctx
, m
, x
+skipcol
, r
, zstencil
);