4773f51627d29c587caba0f4f2d1f08a4b00aeaf
[mesa.git] / src / mesa / drivers / dri / unichrome / via_span.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "glheader.h"
26 #include "macros.h"
27 #include "mtypes.h"
28 #include "colormac.h"
29 #include "via_context.h"
30 #include "via_span.h"
31 #include "via_ioctl.h"
32 #include "swrast/swrast.h"
33
34 #define DBG 0
35
36 #define CLIPPIXEL(_x,_y) (_x >= minx && _x < maxx && \
37 _y >= miny && _y < maxy)
38
39
40 #define CLIPSPAN(_x, _y, _n, _x1, _n1, _i) \
41 if (_y < miny || _y >= maxy) { \
42 _n1 = 0, _x1 = x; \
43 } \
44 else { \
45 _n1 = _n; \
46 _x1 = _x; \
47 if (_x1 < minx) _i += (minx -_x1), n1 -= (minx -_x1), _x1 = minx; \
48 if (_x1 + _n1 >= maxx) n1 -= (_x1 + n1 - maxx); \
49 }
50
51 #define Y_FLIP(_y) (height - _y - 1)
52
53 #define HW_LOCK()
54 #define HW_CLIPLOOP() \
55 do { \
56 __DRIdrawablePrivate *dPriv = vmesa->driDrawable; \
57 int _nc = dPriv->numClipRects; \
58 while (_nc--) { \
59 int minx = dPriv->pClipRects[_nc].x1 - dPriv->x; \
60 int miny = dPriv->pClipRects[_nc].y1 - dPriv->y; \
61 int maxx = dPriv->pClipRects[_nc].x2 - dPriv->x; \
62 int maxy = dPriv->pClipRects[_nc].y2 - dPriv->y;
63
64
65 #define HW_ENDCLIPLOOP() \
66 } \
67 } while (0)
68
69 #define HW_UNLOCK()
70
71 #undef LOCAL_VARS
72 #define LOCAL_VARS \
73 viaContextPtr vmesa = VIA_CONTEXT(ctx); \
74 __DRIdrawablePrivate *dPriv = vmesa->driDrawable; \
75 GLuint draw_pitch = vmesa->drawBuffer->pitch; \
76 GLuint read_pitch = vmesa->readBuffer->pitch; \
77 GLuint height = dPriv->h; \
78 GLint p = 0; \
79 char *buf = (char *)(vmesa->drawBuffer->origMap + vmesa->drawXoff * vmesa->viaScreen->bytesPerPixel); \
80 char *read_buf = (char *)(vmesa->readBuffer->origMap + vmesa->drawXoff * vmesa->viaScreen->bytesPerPixel); \
81 (void) (read_pitch && draw_pitch && buf && read_buf && p);
82
83 /* ================================================================
84 * Color buffer
85 */
86
87 /* 16 bit, RGB565 color spanline and pixel functions
88 */
89 #define GET_SRC_PTR(_x, _y) (read_buf + _x * 2 + _y * read_pitch)
90 #define GET_DST_PTR(_x, _y) ( buf + _x * 2 + _y * draw_pitch)
91 #define SPANTMP_PIXEL_FMT GL_RGB
92 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_5_6_5
93
94 #define TAG(x) via##x##_565
95 #define TAG2(x,y) via##x##_565##y
96 #include "spantmp2.h"
97
98
99 /* 32 bit, ARGB8888 color spanline and pixel functions
100 */
101 #define GET_SRC_PTR(_x, _y) (read_buf + _x * 4 + _y * read_pitch)
102 #define GET_DST_PTR(_x, _y) ( buf + _x * 4 + _y * draw_pitch)
103 #define SPANTMP_PIXEL_FMT GL_BGRA
104 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
105
106 #define TAG(x) via##x##_8888
107 #define TAG2(x,y) via##x##_8888##y
108 #include "spantmp2.h"
109
110
111 /* 16 bit depthbuffer functions.
112 */
113 #define LOCAL_DEPTH_VARS \
114 viaContextPtr vmesa = VIA_CONTEXT(ctx); \
115 __DRIdrawablePrivate *dPriv = vmesa->driDrawable; \
116 GLuint depth_pitch = vmesa->depth.pitch; \
117 GLuint height = dPriv->h; \
118 char *buf = (char *)(vmesa->depth.map + (vmesa->drawXoff * vmesa->depth.bpp/8))
119
120 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
121
122
123 #define WRITE_DEPTH(_x, _y, d) \
124 *(GLushort *)(buf + (_x) * 2 + (_y) * depth_pitch) = d;
125
126 #define READ_DEPTH(d, _x, _y) \
127 d = *(volatile GLushort *)(buf + (_x) * 2 + (_y) * depth_pitch);
128
129 #define TAG(x) via##x##_16
130 #include "depthtmp.h"
131
132 /* 32 bit depthbuffer functions.
133 */
134 #define WRITE_DEPTH(_x, _y, d) \
135 *(GLuint *)(buf + (_x) * 4 + (_y) * depth_pitch) = d;
136
137 #define READ_DEPTH(d, _x, _y) \
138 d = *(volatile GLuint *)(buf + (_x) * 4 + (_y) * depth_pitch);
139
140 #define TAG(x) via##x##_32
141 #include "depthtmp.h"
142
143
144
145 /* 24/8 bit interleaved depth/stencil functions
146 */
147 #define WRITE_DEPTH( _x, _y, d ) { \
148 GLuint tmp = *(GLuint *)(buf + (_x)*4 + (_y)*depth_pitch); \
149 tmp &= 0x000000ff; \
150 tmp |= ((d)<<8); \
151 *(GLuint *)(buf + (_x)*4 + (_y)*depth_pitch) = tmp; \
152 }
153
154 #define READ_DEPTH( d, _x, _y ) \
155 d = (*(GLuint *)(buf + (_x)*4 + (_y)*depth_pitch)) >> 8;
156
157
158 #define TAG(x) via##x##_24_8
159 #include "depthtmp.h"
160
161 #define WRITE_STENCIL( _x, _y, d ) { \
162 GLuint tmp = *(GLuint *)(buf + (_x)*4 + (_y)*depth_pitch); \
163 tmp &= 0xffffff00; \
164 tmp |= (d); \
165 *(GLuint *)(buf + (_x)*4 + (_y)*depth_pitch) = tmp; \
166 }
167
168 #define READ_STENCIL( d, _x, _y ) \
169 d = *(GLuint *)(buf + (_x)*4 + (_y)*depth_pitch) & 0xff;
170
171 #define TAG(x) via##x##_24_8
172 #include "stenciltmp.h"
173
174
175
176 static void viaSetBuffer(GLcontext *ctx, GLframebuffer *colorBuffer,
177 GLuint bufferBit)
178 {
179 viaContextPtr vmesa = VIA_CONTEXT(ctx);
180
181 if (bufferBit == DD_FRONT_LEFT_BIT) {
182 vmesa->drawBuffer = vmesa->readBuffer = &vmesa->front;
183 }
184 else if (bufferBit == DD_BACK_LEFT_BIT) {
185 vmesa->drawBuffer = vmesa->readBuffer = &vmesa->back;
186 }
187 else {
188 ASSERT(0);
189 }
190 }
191
192 /* Move locking out to get reasonable span performance.
193 */
194 void viaSpanRenderStart( GLcontext *ctx )
195 {
196 viaContextPtr vmesa = VIA_CONTEXT(ctx);
197 VIA_FINISH_PRIM(vmesa);
198 LOCK_HARDWARE(vmesa);
199 viaFlushDmaLocked(vmesa, 0);
200 WAIT_IDLE(vmesa);
201 }
202
203 void viaSpanRenderFinish( GLcontext *ctx )
204 {
205 viaContextPtr vmesa = VIA_CONTEXT(ctx);
206 _swrast_flush( ctx );
207 UNLOCK_HARDWARE( vmesa );
208 }
209
210 void viaInitSpanFuncs(GLcontext *ctx)
211 {
212 viaContextPtr vmesa = VIA_CONTEXT(ctx);
213 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
214
215 swdd->SetBuffer = viaSetBuffer;
216 if (vmesa->viaScreen->bitsPerPixel == 16) {
217 viaInitPointers_565( swdd );
218 }
219 else if (vmesa->viaScreen->bitsPerPixel == 32) {
220 viaInitPointers_8888( swdd );
221 }
222 else {
223 assert(0);
224 }
225
226 if (vmesa->glCtx->Visual.depthBits == 16) {
227 swdd->ReadDepthSpan = viaReadDepthSpan_16;
228 swdd->WriteDepthSpan = viaWriteDepthSpan_16;
229 swdd->WriteMonoDepthSpan = viaWriteMonoDepthSpan_16;
230 swdd->ReadDepthPixels = viaReadDepthPixels_16;
231 swdd->WriteDepthPixels = viaWriteDepthPixels_16;
232 }
233 else if (vmesa->glCtx->Visual.depthBits == 24) {
234 swdd->ReadDepthSpan = viaReadDepthSpan_24_8;
235 swdd->WriteDepthSpan = viaWriteDepthSpan_24_8;
236 swdd->WriteMonoDepthSpan = viaWriteMonoDepthSpan_24_8;
237 swdd->ReadDepthPixels = viaReadDepthPixels_24_8;
238 swdd->WriteDepthPixels = viaWriteDepthPixels_24_8;
239
240 swdd->WriteStencilSpan = viaWriteStencilSpan_24_8;
241 swdd->ReadStencilSpan = viaReadStencilSpan_24_8;
242 swdd->WriteStencilPixels = viaWriteStencilPixels_24_8;
243 swdd->ReadStencilPixels = viaReadStencilPixels_24_8;
244 }
245 else if (vmesa->glCtx->Visual.depthBits == 32) {
246 swdd->ReadDepthSpan = viaReadDepthSpan_32;
247 swdd->WriteDepthSpan = viaWriteDepthSpan_32;
248 swdd->WriteMonoDepthSpan = viaWriteMonoDepthSpan_32;
249 swdd->ReadDepthPixels = viaReadDepthPixels_32;
250 swdd->WriteDepthPixels = viaWriteDepthPixels_32;
251 }
252
253 swdd->SpanRenderStart = viaSpanRenderStart;
254 swdd->SpanRenderFinish = viaSpanRenderFinish;
255
256
257 swdd->WriteCI8Span = NULL;
258 swdd->WriteCI32Span = NULL;
259 swdd->WriteMonoCISpan = NULL;
260 swdd->WriteCI32Pixels = NULL;
261 swdd->WriteMonoCIPixels = NULL;
262 swdd->ReadCI32Span = NULL;
263 swdd->ReadCI32Pixels = NULL;
264 }