Support loop, conditional update fix
[mesa.git] / src / mesa / drivers / dri / i965 / intel_span.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "glheader.h"
29 #include "macros.h"
30 #include "mtypes.h"
31 #include "colormac.h"
32
33 #include "intel_screen.h"
34 #include "intel_regions.h"
35 #include "intel_span.h"
36 #include "intel_ioctl.h"
37 #include "intel_tex.h"
38 #include "intel_batchbuffer.h"
39 #include "swrast/swrast.h"
40
41 #undef DBG
42 #define DBG 0
43
44 #define LOCAL_VARS \
45 struct intel_context *intel = intel_context(ctx); \
46 __DRIdrawablePrivate *dPriv = intel->driDrawable; \
47 driRenderbuffer *drb = (driRenderbuffer *) rb; \
48 GLuint pitch = drb->pitch; \
49 GLuint height = dPriv->h; \
50 char *buf = (char *) drb->Base.Data + \
51 dPriv->x * drb->cpp + \
52 dPriv->y * pitch; \
53 GLushort p; \
54 (void) buf; (void) p
55
56 #define LOCAL_DEPTH_VARS \
57 struct intel_context *intel = intel_context(ctx); \
58 __DRIdrawablePrivate *dPriv = intel->driDrawable; \
59 driRenderbuffer *drb = (driRenderbuffer *) rb; \
60 GLuint pitch = drb->pitch; \
61 GLuint height = dPriv->h; \
62 char *buf = (char *) drb->Base.Data + \
63 dPriv->x * drb->cpp + \
64 dPriv->y * pitch
65
66 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
67
68 #define INIT_MONO_PIXEL(p,color)\
69 p = INTEL_PACKCOLOR565(color[0],color[1],color[2])
70
71 #define Y_FLIP(_y) (height - _y - 1)
72
73 #define HW_LOCK()
74
75 #define HW_UNLOCK()
76
77 /* 16 bit, 565 rgb color spanline and pixel functions
78 */
79 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
80 *(GLushort *)(buf + _x*2 + _y*pitch) = ( (((int)r & 0xf8) << 8) | \
81 (((int)g & 0xfc) << 3) | \
82 (((int)b & 0xf8) >> 3))
83 #define WRITE_PIXEL( _x, _y, p ) \
84 *(GLushort *)(buf + _x*2 + _y*pitch) = p
85
86 #define READ_RGBA( rgba, _x, _y ) \
87 do { \
88 GLushort p = *(GLushort *)(buf + _x*2 + _y*pitch); \
89 rgba[0] = (((p >> 11) & 0x1f) * 255) / 31; \
90 rgba[1] = (((p >> 5) & 0x3f) * 255) / 63; \
91 rgba[2] = (((p >> 0) & 0x1f) * 255) / 31; \
92 rgba[3] = 255; \
93 } while(0)
94
95 #define TAG(x) intel##x##_565
96 #include "spantmp.h"
97
98 /* 15 bit, 555 rgb color spanline and pixel functions
99 */
100 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
101 *(GLushort *)(buf + _x*2 + _y*pitch) = (((r & 0xf8) << 7) | \
102 ((g & 0xf8) << 3) | \
103 ((b & 0xf8) >> 3))
104
105 #define WRITE_PIXEL( _x, _y, p ) \
106 *(GLushort *)(buf + _x*2 + _y*pitch) = p
107
108 #define READ_RGBA( rgba, _x, _y ) \
109 do { \
110 GLushort p = *(GLushort *)(buf + _x*2 + _y*pitch); \
111 rgba[0] = (p >> 7) & 0xf8; \
112 rgba[1] = (p >> 3) & 0xf8; \
113 rgba[2] = (p << 3) & 0xf8; \
114 rgba[3] = 255; \
115 } while(0)
116
117 #define TAG(x) intel##x##_555
118 #include "spantmp.h"
119
120 /* 16 bit depthbuffer functions.
121 */
122 #define WRITE_DEPTH( _x, _y, d ) \
123 *(GLushort *)(buf + (_x)*2 + (_y)*pitch) = d;
124
125 #define READ_DEPTH( d, _x, _y ) \
126 d = *(GLushort *)(buf + (_x)*2 + (_y)*pitch);
127
128
129 #define TAG(x) intel##x##_z16
130 #include "depthtmp.h"
131
132
133 #undef LOCAL_VARS
134 #define LOCAL_VARS \
135 struct intel_context *intel = intel_context(ctx); \
136 __DRIdrawablePrivate *dPriv = intel->driDrawable; \
137 driRenderbuffer *drb = (driRenderbuffer *) rb; \
138 GLuint pitch = drb->pitch; \
139 GLuint height = dPriv->h; \
140 char *buf = (char *)drb->Base.Data + \
141 dPriv->x * drb->cpp + \
142 dPriv->y * pitch; \
143 GLuint p; \
144 (void) buf; (void) p
145
146 #undef INIT_MONO_PIXEL
147 #define INIT_MONO_PIXEL(p,color)\
148 p = INTEL_PACKCOLOR8888(color[0],color[1],color[2],color[3])
149
150 /* 32 bit, 8888 argb color spanline and pixel functions
151 */
152 #define WRITE_RGBA(_x, _y, r, g, b, a) \
153 *(GLuint *)(buf + _x*4 + _y*pitch) = ((r << 16) | \
154 (g << 8) | \
155 (b << 0) | \
156 (a << 24) )
157
158 #define WRITE_PIXEL(_x, _y, p) \
159 *(GLuint *)(buf + _x*4 + _y*pitch) = p
160
161
162 #define READ_RGBA(rgba, _x, _y) \
163 do { \
164 GLuint p = *(GLuint *)(buf + _x*4 + _y*pitch); \
165 rgba[0] = (p >> 16) & 0xff; \
166 rgba[1] = (p >> 8) & 0xff; \
167 rgba[2] = (p >> 0) & 0xff; \
168 rgba[3] = (p >> 24) & 0xff; \
169 } while (0)
170
171 #define TAG(x) intel##x##_8888
172 #include "spantmp.h"
173
174
175 /* 24/8 bit interleaved depth/stencil functions
176 */
177 #define WRITE_DEPTH( _x, _y, d ) { \
178 GLuint tmp = *(GLuint *)(buf + (_x)*4 + (_y)*pitch); \
179 tmp &= 0xff000000; \
180 tmp |= (d) & 0xffffff; \
181 *(GLuint *)(buf + (_x)*4 + (_y)*pitch) = tmp; \
182 }
183
184 #define READ_DEPTH( d, _x, _y ) \
185 d = *(GLuint *)(buf + (_x)*4 + (_y)*pitch) & 0xffffff;
186
187
188 #define TAG(x) intel##x##_z24_s8
189 #include "depthtmp.h"
190
191 #define WRITE_STENCIL( _x, _y, d ) { \
192 GLuint tmp = *(GLuint *)(buf + (_x)*4 + (_y)*pitch); \
193 tmp &= 0xffffff; \
194 tmp |= ((d)<<24); \
195 *(GLuint *)(buf + (_x)*4 + (_y)*pitch) = tmp; \
196 }
197
198 #define READ_STENCIL( d, _x, _y ) \
199 d = *(GLuint *)(buf + (_x)*4 + (_y)*pitch) >> 24;
200
201 #define TAG(x) intel##x##_z24_s8
202 #include "stenciltmp.h"
203
204
205 /* Move locking out to get reasonable span performance.
206 */
207 void intelSpanRenderStart( GLcontext *ctx )
208 {
209 struct intel_context *intel = intel_context(ctx);
210
211 if (intel->need_flush) {
212 LOCK_HARDWARE(intel);
213 intel->vtbl.emit_flush(intel, 0);
214 intel_batchbuffer_flush(intel->batch);
215 intel->need_flush = 0;
216 UNLOCK_HARDWARE(intel);
217 intelFinish(&intel->ctx);
218 }
219
220
221 LOCK_HARDWARE(intel);
222
223 /* Just map the framebuffer and all textures. Bufmgr code will
224 * take care of waiting on the necessary fences:
225 */
226 intel_region_map(intel, intel->front_region);
227 intel_region_map(intel, intel->back_region);
228 intel_region_map(intel, intel->depth_region);
229 }
230
231 void intelSpanRenderFinish( GLcontext *ctx )
232 {
233 struct intel_context *intel = intel_context( ctx );
234
235 _swrast_flush( ctx );
236
237 /* Now unmap the framebuffer:
238 */
239 intel_region_unmap(intel, intel->front_region);
240 intel_region_unmap(intel, intel->back_region);
241 intel_region_unmap(intel, intel->depth_region);
242
243 UNLOCK_HARDWARE( intel );
244 }
245
246 void intelInitSpanFuncs( GLcontext *ctx )
247 {
248 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
249 swdd->SpanRenderStart = intelSpanRenderStart;
250 swdd->SpanRenderFinish = intelSpanRenderFinish;
251 }
252
253
254 /**
255 * Plug in the Get/Put routines for the given driRenderbuffer.
256 */
257 void
258 intelSetSpanFunctions(driRenderbuffer *drb, const GLvisual *vis)
259 {
260 if (drb->Base.InternalFormat == GL_RGBA) {
261 if (vis->redBits == 5 && vis->greenBits == 5 && vis->blueBits == 5) {
262 intelInitPointers_555(&drb->Base);
263 }
264 else if (vis->redBits == 5 && vis->greenBits == 6 && vis->blueBits == 5) {
265 intelInitPointers_565(&drb->Base);
266 }
267 else {
268 assert(vis->redBits == 8);
269 assert(vis->greenBits == 8);
270 assert(vis->blueBits == 8);
271 intelInitPointers_8888(&drb->Base);
272 }
273 }
274 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT16) {
275 intelInitDepthPointers_z16(&drb->Base);
276 }
277 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT24) {
278 intelInitDepthPointers_z24_s8(&drb->Base);
279 }
280 else if (drb->Base.InternalFormat == GL_STENCIL_INDEX8_EXT) {
281 intelInitStencilPointers_z24_s8(&drb->Base);
282 }
283 }