2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include "s_context.h"
32 #include "s_stencil.h"
37 * Compute the bounds of the region resulting from zooming a pixel span.
38 * The resulting region will be entirely inside the window/scissor bounds
39 * so no additional clipping is needed.
40 * \param imageX, imageY position of the overall image being drawn
41 * \param spanX, spanY position of span being drawing
42 * \param x0, x1 returned X bounds of zoomed region [x0, x1)
43 * \param y0, y1 returned Y bounds of zoomed region [y0, y1)
44 * \return GL_TRUE if any zoomed pixels visible, GL_FALSE if totally clipped
47 compute_zoomed_bounds(GLcontext
*ctx
, GLint imageX
, GLint imageY
,
48 GLint spanX
, GLint spanY
, GLint width
,
49 GLint
*x0
, GLint
*x1
, GLint
*y0
, GLint
*y1
)
51 const struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
54 ASSERT(spanX
>= imageX
);
55 ASSERT(spanY
>= imageY
);
58 * Compute destination columns: [c0, c1)
60 c0
= imageX
+ (GLint
) ((spanX
- imageX
) * ctx
->Pixel
.ZoomX
);
61 c1
= imageX
+ (GLint
) ((spanX
+ width
- imageX
) * ctx
->Pixel
.ZoomX
);
68 c0
= CLAMP(c0
, fb
->_Xmin
, fb
->_Xmax
);
69 c1
= CLAMP(c1
, fb
->_Xmin
, fb
->_Xmax
);
71 return GL_FALSE
; /* no width */
75 * Compute destination rows: [r0, r1)
77 r0
= imageY
+ (GLint
) ((spanY
- imageY
) * ctx
->Pixel
.ZoomY
);
78 r1
= imageY
+ (GLint
) ((spanY
+ 1 - imageY
) * ctx
->Pixel
.ZoomY
);
85 r0
= CLAMP(r0
, fb
->_Ymin
, fb
->_Ymax
);
86 r1
= CLAMP(r1
, fb
->_Ymin
, fb
->_Ymax
);
88 return GL_FALSE
; /* no height */
101 * Can use this for unzooming X or Y values.
104 unzoom_x(GLfloat zoomX
, GLint imageX
, GLint zx
)
107 zx = imageX + (x - imageX) * zoomX;
108 zx - imageX = (x - imageX) * zoomX;
109 (zx - imageX) / zoomX = x - imageX;
111 GLint x
= imageX
+ (GLint
) ((zx
- imageX
) / zoomX
);
118 * Helper function called from _swrast_write_zoomed_rgba/rgb/
119 * index/depth_span().
122 zoom_span( GLcontext
*ctx
, GLint imgX
, GLint imgY
, const SWspan
*span
,
123 const GLvoid
*src
, GLenum format
)
126 SWspanarrays zoomed_arrays
; /* this is big! */
127 GLint x0
, x1
, y0
, y1
;
130 if (!compute_zoomed_bounds(ctx
, imgX
, imgY
, span
->x
, span
->y
, span
->end
,
131 &x0
, &x1
, &y0
, &y1
)) {
132 return; /* totally clipped */
135 zoomedWidth
= x1
- x0
;
136 ASSERT(zoomedWidth
> 0);
137 ASSERT(zoomedWidth
<= MAX_WIDTH
);
139 /* no pixel arrays! must be horizontal spans. */
140 ASSERT((span
->arrayMask
& SPAN_XY
) == 0);
141 ASSERT(span
->primitive
== GL_BITMAP
);
143 INIT_SPAN(zoomed
, GL_BITMAP
, 0, 0, 0);
145 zoomed
.end
= zoomedWidth
;
146 zoomed
.array
= &zoomed_arrays
;
147 zoomed_arrays
.ChanType
= span
->array
->ChanType
;
149 #if CHAN_TYPE == GL_UNSIGNED_BYTE
150 zoomed_arrays
.rgba
= zoomed_arrays
.color
.sz1
.rgba
;
151 zoomed_arrays
.spec
= zoomed_arrays
.color
.sz1
.spec
;
152 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
153 zoomed_arrays
.rgba
= zoomed_arrays
.color
.sz2
.rgba
;
154 zoomed_arrays
.spec
= zoomed_arrays
.color
.sz2
.spec
;
156 zoomed_arrays
.rgba
= zoomed_arrays
.color
.sz4
.rgba
;
157 zoomed_arrays
.spec
= zoomed_arrays
.color
.sz4
.spec
;
161 /* copy fog interp info */
162 zoomed
.fog
= span
->fog
;
163 zoomed
.fogStep
= span
->fogStep
;
164 /* XXX copy texcoord info? */
166 if (format
== GL_RGBA
|| format
== GL_RGB
) {
169 zoomed
.zStep
= span
->zStep
;
170 /* we'll generate an array of colorss */
171 zoomed
.interpMask
= span
->interpMask
& ~SPAN_RGBA
;
172 zoomed
.arrayMask
|= SPAN_RGBA
;
173 ASSERT(span
->arrayMask
& SPAN_RGBA
);
175 else if (format
== GL_COLOR_INDEX
) {
178 zoomed
.zStep
= span
->zStep
;
179 /* we'll generate an array of color indexes */
180 zoomed
.interpMask
= span
->interpMask
& ~SPAN_INDEX
;
181 zoomed
.arrayMask
|= SPAN_INDEX
;
182 ASSERT(span
->arrayMask
& SPAN_INDEX
);
184 else if (format
== GL_DEPTH_COMPONENT
) {
185 /* Copy color info */
186 zoomed
.red
= span
->red
;
187 zoomed
.green
= span
->green
;
188 zoomed
.blue
= span
->blue
;
189 zoomed
.alpha
= span
->alpha
;
190 zoomed
.redStep
= span
->redStep
;
191 zoomed
.greenStep
= span
->greenStep
;
192 zoomed
.blueStep
= span
->blueStep
;
193 zoomed
.alphaStep
= span
->alphaStep
;
194 /* we'll generate an array of depth values */
195 zoomed
.interpMask
= span
->interpMask
& ~SPAN_Z
;
196 zoomed
.arrayMask
|= SPAN_Z
;
197 ASSERT(span
->arrayMask
& SPAN_Z
);
200 _mesa_problem(ctx
, "Bad format in zoom_span");
204 /* zoom the span horizontally */
205 if (format
== GL_RGBA
) {
206 if (zoomed
.array
->ChanType
== GL_UNSIGNED_BYTE
) {
207 const GLubyte (*rgba
)[4] = (const GLubyte (*)[4]) src
;
209 for (i
= 0; i
< zoomedWidth
; i
++) {
210 GLint j
= unzoom_x(ctx
->Pixel
.ZoomX
, imgX
, x0
+ i
) - span
->x
;
212 ASSERT(j
< span
->end
);
213 COPY_4UBV(zoomed
.array
->color
.sz1
.rgba
[i
], rgba
[j
]);
216 else if (zoomed
.array
->ChanType
== GL_UNSIGNED_SHORT
) {
217 const GLushort (*rgba
)[4] = (const GLushort (*)[4]) src
;
219 for (i
= 0; i
< zoomedWidth
; i
++) {
220 GLint j
= unzoom_x(ctx
->Pixel
.ZoomX
, imgX
, x0
+ i
) - span
->x
;
222 ASSERT(j
< span
->end
);
223 COPY_4V(zoomed
.array
->color
.sz2
.rgba
[i
], rgba
[j
]);
227 const GLfloat (*rgba
)[4] = (const GLfloat (*)[4]) src
;
229 for (i
= 0; i
< zoomedWidth
; i
++) {
230 GLint j
= unzoom_x(ctx
->Pixel
.ZoomX
, imgX
, x0
+ i
) - span
->x
;
232 ASSERT(j
< span
->end
);
233 COPY_4V(zoomed
.array
->color
.sz4
.rgba
[i
], rgba
[j
]);
237 else if (format
== GL_RGB
) {
238 if (zoomed
.array
->ChanType
== GL_UNSIGNED_BYTE
) {
239 const GLubyte (*rgb
)[3] = (const GLubyte (*)[3]) src
;
241 for (i
= 0; i
< zoomedWidth
; i
++) {
242 GLint j
= unzoom_x(ctx
->Pixel
.ZoomX
, imgX
, x0
+ i
) - span
->x
;
244 ASSERT(j
< span
->end
);
245 zoomed
.array
->color
.sz1
.rgba
[i
][0] = rgb
[j
][0];
246 zoomed
.array
->color
.sz1
.rgba
[i
][1] = rgb
[j
][1];
247 zoomed
.array
->color
.sz1
.rgba
[i
][2] = rgb
[j
][2];
248 zoomed
.array
->color
.sz1
.rgba
[i
][3] = 0xff;
251 else if (zoomed
.array
->ChanType
== GL_UNSIGNED_SHORT
) {
252 const GLushort (*rgb
)[3] = (const GLushort (*)[3]) src
;
254 for (i
= 0; i
< zoomedWidth
; i
++) {
255 GLint j
= unzoom_x(ctx
->Pixel
.ZoomX
, imgX
, x0
+ i
) - span
->x
;
257 ASSERT(j
< span
->end
);
258 zoomed
.array
->color
.sz2
.rgba
[i
][0] = rgb
[j
][0];
259 zoomed
.array
->color
.sz2
.rgba
[i
][1] = rgb
[j
][1];
260 zoomed
.array
->color
.sz2
.rgba
[i
][2] = rgb
[j
][2];
261 zoomed
.array
->color
.sz2
.rgba
[i
][3] = 0xffff;
265 const GLfloat (*rgb
)[3] = (const GLfloat (*)[3]) src
;
267 for (i
= 0; i
< zoomedWidth
; i
++) {
268 GLint j
= unzoom_x(ctx
->Pixel
.ZoomX
, imgX
, x0
+ i
) - span
->x
;
270 ASSERT(j
< span
->end
);
271 zoomed
.array
->color
.sz4
.rgba
[i
][0] = rgb
[j
][0];
272 zoomed
.array
->color
.sz4
.rgba
[i
][1] = rgb
[j
][1];
273 zoomed
.array
->color
.sz4
.rgba
[i
][2] = rgb
[j
][2];
274 zoomed
.array
->color
.sz4
.rgba
[i
][3] = 1.0F
;
278 else if (format
== GL_COLOR_INDEX
) {
279 const GLuint
*indexes
= (const GLuint
*) src
;
281 for (i
= 0; i
< zoomedWidth
; i
++) {
282 GLint j
= unzoom_x(ctx
->Pixel
.ZoomX
, imgX
, x0
+ i
) - span
->x
;
284 ASSERT(j
< span
->end
);
285 zoomed
.array
->index
[i
] = indexes
[j
];
288 else if (format
== GL_DEPTH_COMPONENT
) {
289 const GLuint
*zValues
= (const GLuint
*) src
;
291 for (i
= 0; i
< zoomedWidth
; i
++) {
292 GLint j
= unzoom_x(ctx
->Pixel
.ZoomX
, imgX
, x0
+ i
) - span
->x
;
294 ASSERT(j
< span
->end
);
295 zoomed
.array
->z
[i
] = zValues
[j
];
297 /* Now, fall into either the RGB or COLOR_INDEX path below */
298 format
= ctx
->Visual
.rgbMode
? GL_RGBA
: GL_COLOR_INDEX
;
301 /* write the span in rows [r0, r1) */
302 if (format
== GL_RGBA
|| format
== GL_RGB
) {
303 /* Writing the span may modify the colors, so make a backup now if we're
304 * going to call _swrast_write_zoomed_span() more than once.
305 * Also, clipping may change the span end value, so store it as well.
307 const GLint end
= zoomed
.end
; /* save */
308 /* use specular color array for temp storage */
309 void *rgbaSave
= zoomed
.array
->spec
;
310 const GLint pixelSize
=
311 (zoomed
.array
->ChanType
== GL_UNSIGNED_BYTE
) ? 4 * sizeof(GLubyte
) :
312 ((zoomed
.array
->ChanType
== GL_UNSIGNED_SHORT
) ? 4 * sizeof(GLushort
)
313 : 4 * sizeof(GLfloat
));
315 MEMCPY(rgbaSave
, zoomed
.array
->rgba
, zoomed
.end
* pixelSize
);
317 for (zoomed
.y
= y0
; zoomed
.y
< y1
; zoomed
.y
++) {
318 _swrast_write_rgba_span(ctx
, &zoomed
);
319 zoomed
.end
= end
; /* restore */
321 /* restore the colors */
322 MEMCPY(zoomed
.array
->rgba
, rgbaSave
, zoomed
.end
* pixelSize
);
326 else if (format
== GL_COLOR_INDEX
) {
327 /* use specular color array for temp storage */
328 GLuint
*indexSave
= (GLuint
*) zoomed
.array
->spec
;
329 const GLint end
= zoomed
.end
; /* save */
331 MEMCPY(indexSave
, zoomed
.array
->index
, zoomed
.end
* sizeof(GLuint
));
333 for (zoomed
.y
= y0
; zoomed
.y
< y1
; zoomed
.y
++) {
334 _swrast_write_index_span(ctx
, &zoomed
);
335 zoomed
.end
= end
; /* restore */
337 /* restore the colors */
338 MEMCPY(zoomed
.array
->index
, indexSave
, zoomed
.end
* sizeof(GLuint
));
346 _swrast_write_zoomed_rgba_span(GLcontext
*ctx
, GLint imgX
, GLint imgY
,
347 const SWspan
*span
, const GLvoid
*rgba
)
349 zoom_span(ctx
, imgX
, imgY
, span
, rgba
, GL_RGBA
);
354 _swrast_write_zoomed_rgb_span(GLcontext
*ctx
, GLint imgX
, GLint imgY
,
355 const SWspan
*span
, const GLvoid
*rgb
)
357 zoom_span(ctx
, imgX
, imgY
, span
, rgb
, GL_RGB
);
362 _swrast_write_zoomed_index_span(GLcontext
*ctx
, GLint imgX
, GLint imgY
,
365 zoom_span(ctx
, imgX
, imgY
, span
,
366 (const GLvoid
*) span
->array
->index
, GL_COLOR_INDEX
);
371 _swrast_write_zoomed_depth_span(GLcontext
*ctx
, GLint imgX
, GLint imgY
,
374 zoom_span(ctx
, imgX
, imgY
, span
,
375 (const GLvoid
*) span
->array
->z
, GL_DEPTH_COMPONENT
);
380 * Zoom/write stencil values.
381 * No per-fragment operations are applied.
384 _swrast_write_zoomed_stencil_span(GLcontext
*ctx
, GLint imgX
, GLint imgY
,
385 GLint width
, GLint spanX
, GLint spanY
,
386 const GLstencil stencil
[])
388 GLstencil zoomedVals
[MAX_WIDTH
];
389 GLint x0
, x1
, y0
, y1
, y
;
390 GLint i
, zoomedWidth
;
392 if (!compute_zoomed_bounds(ctx
, imgX
, imgY
, spanX
, spanY
, width
,
393 &x0
, &x1
, &y0
, &y1
)) {
394 return; /* totally clipped */
397 zoomedWidth
= x1
- x0
;
398 ASSERT(zoomedWidth
> 0);
399 ASSERT(zoomedWidth
<= MAX_WIDTH
);
401 /* zoom the span horizontally */
402 for (i
= 0; i
< zoomedWidth
; i
++) {
403 GLint j
= unzoom_x(ctx
->Pixel
.ZoomX
, imgX
, x0
+ i
) - spanX
;
406 zoomedVals
[i
] = stencil
[j
];
409 /* write the zoomed spans */
410 for (y
= y0
; y
< y1
; y
++) {
411 _swrast_write_stencil_span(ctx
, zoomedWidth
, x0
, y
, zoomedVals
);
417 * Zoom/write z values (16 or 32-bit).
418 * No per-fragment operations are applied.
421 _swrast_write_zoomed_z_span(GLcontext
*ctx
, GLint imgX
, GLint imgY
,
422 GLint width
, GLint spanX
, GLint spanY
,
425 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_DepthBuffer
;
426 GLushort zoomedVals16
[MAX_WIDTH
];
427 GLuint zoomedVals32
[MAX_WIDTH
];
428 GLint x0
, x1
, y0
, y1
, y
;
429 GLint i
, zoomedWidth
;
431 if (!compute_zoomed_bounds(ctx
, imgX
, imgY
, spanX
, spanY
, width
,
432 &x0
, &x1
, &y0
, &y1
)) {
433 return; /* totally clipped */
436 zoomedWidth
= x1
- x0
;
437 ASSERT(zoomedWidth
> 0);
438 ASSERT(zoomedWidth
<= MAX_WIDTH
);
440 /* zoom the span horizontally */
441 if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
442 for (i
= 0; i
< zoomedWidth
; i
++) {
443 GLint j
= unzoom_x(ctx
->Pixel
.ZoomX
, imgX
, x0
+ i
) - spanX
;
446 zoomedVals16
[i
] = ((GLushort
*) z
)[j
];
451 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
452 for (i
= 0; i
< zoomedWidth
; i
++) {
453 GLint j
= unzoom_x(ctx
->Pixel
.ZoomX
, imgX
, x0
+ i
) - spanX
;
456 zoomedVals32
[i
] = ((GLuint
*) z
)[j
];
461 /* write the zoomed spans */
462 for (y
= y0
; y
< y1
; y
++) {
463 rb
->PutRow(ctx
, rb
, zoomedWidth
, x0
, y
, z
, NULL
);