Make sure bmBufferOffset is called for all active buffers every time
[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 "swrast/swrast.h"
39
40 #undef DBG
41 #define DBG 0
42
43 #define LOCAL_VARS \
44 struct intel_context *intel = intel_context(ctx); \
45 __DRIdrawablePrivate *dPriv = intel->driDrawable; \
46 driRenderbuffer *drb = (driRenderbuffer *) rb; \
47 GLuint pitch = drb->pitch; \
48 GLuint height = dPriv->h; \
49 char *buf = (char *) drb->Base.Data + \
50 dPriv->x * drb->cpp + \
51 dPriv->y * pitch; \
52 GLushort p; \
53 (void) buf; (void) p
54
55 #define LOCAL_DEPTH_VARS \
56 struct intel_context *intel = intel_context(ctx); \
57 __DRIdrawablePrivate *dPriv = intel->driDrawable; \
58 driRenderbuffer *drb = (driRenderbuffer *) rb; \
59 GLuint pitch = drb->pitch; \
60 GLuint height = dPriv->h; \
61 char *buf = (char *) drb->Base.Data + \
62 dPriv->x * drb->cpp + \
63 dPriv->y * pitch
64
65 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
66
67 #define INIT_MONO_PIXEL(p,color)\
68 p = INTEL_PACKCOLOR565(color[0],color[1],color[2])
69
70 #define Y_FLIP(_y) (height - _y - 1)
71
72 #define HW_LOCK()
73
74 #define HW_UNLOCK()
75
76 /* 16 bit, 565 rgb color spanline and pixel functions
77 */
78 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
79 *(GLushort *)(buf + _x*2 + _y*pitch) = ( (((int)r & 0xf8) << 8) | \
80 (((int)g & 0xfc) << 3) | \
81 (((int)b & 0xf8) >> 3))
82 #define WRITE_PIXEL( _x, _y, p ) \
83 *(GLushort *)(buf + _x*2 + _y*pitch) = p
84
85 #define READ_RGBA( rgba, _x, _y ) \
86 do { \
87 GLushort p = *(GLushort *)(buf + _x*2 + _y*pitch); \
88 rgba[0] = (((p >> 11) & 0x1f) * 255) / 31; \
89 rgba[1] = (((p >> 5) & 0x3f) * 255) / 63; \
90 rgba[2] = (((p >> 0) & 0x1f) * 255) / 31; \
91 rgba[3] = 255; \
92 } while(0)
93
94 #define TAG(x) intel##x##_565
95 #include "spantmp.h"
96
97 /* 15 bit, 555 rgb color spanline and pixel functions
98 */
99 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
100 *(GLushort *)(buf + _x*2 + _y*pitch) = (((r & 0xf8) << 7) | \
101 ((g & 0xf8) << 3) | \
102 ((b & 0xf8) >> 3))
103
104 #define WRITE_PIXEL( _x, _y, p ) \
105 *(GLushort *)(buf + _x*2 + _y*pitch) = p
106
107 #define READ_RGBA( rgba, _x, _y ) \
108 do { \
109 GLushort p = *(GLushort *)(buf + _x*2 + _y*pitch); \
110 rgba[0] = (p >> 7) & 0xf8; \
111 rgba[1] = (p >> 3) & 0xf8; \
112 rgba[2] = (p << 3) & 0xf8; \
113 rgba[3] = 255; \
114 } while(0)
115
116 #define TAG(x) intel##x##_555
117 #include "spantmp.h"
118
119 /* 16 bit depthbuffer functions.
120 */
121 #define WRITE_DEPTH( _x, _y, d ) \
122 *(GLushort *)(buf + (_x)*2 + (_y)*pitch) = d;
123
124 #define READ_DEPTH( d, _x, _y ) \
125 d = *(GLushort *)(buf + (_x)*2 + (_y)*pitch);
126
127
128 #define TAG(x) intel##x##_z16
129 #include "depthtmp.h"
130
131
132 #undef LOCAL_VARS
133 #define LOCAL_VARS \
134 struct intel_context *intel = intel_context(ctx); \
135 __DRIdrawablePrivate *dPriv = intel->driDrawable; \
136 driRenderbuffer *drb = (driRenderbuffer *) rb; \
137 GLuint pitch = drb->pitch; \
138 GLuint height = dPriv->h; \
139 char *buf = (char *)drb->Base.Data + \
140 dPriv->x * drb->cpp + \
141 dPriv->y * pitch; \
142 GLuint p; \
143 (void) buf; (void) p
144
145 #undef INIT_MONO_PIXEL
146 #define INIT_MONO_PIXEL(p,color)\
147 p = INTEL_PACKCOLOR8888(color[0],color[1],color[2],color[3])
148
149 /* 32 bit, 8888 argb color spanline and pixel functions
150 */
151 #define WRITE_RGBA(_x, _y, r, g, b, a) \
152 *(GLuint *)(buf + _x*4 + _y*pitch) = ((r << 16) | \
153 (g << 8) | \
154 (b << 0) | \
155 (a << 24) )
156
157 #define WRITE_PIXEL(_x, _y, p) \
158 *(GLuint *)(buf + _x*4 + _y*pitch) = p
159
160
161 #define READ_RGBA(rgba, _x, _y) \
162 do { \
163 GLuint p = *(GLuint *)(buf + _x*4 + _y*pitch); \
164 rgba[0] = (p >> 16) & 0xff; \
165 rgba[1] = (p >> 8) & 0xff; \
166 rgba[2] = (p >> 0) & 0xff; \
167 rgba[3] = (p >> 24) & 0xff; \
168 } while (0)
169
170 #define TAG(x) intel##x##_8888
171 #include "spantmp.h"
172
173
174 /* 24/8 bit interleaved depth/stencil functions
175 */
176 #define WRITE_DEPTH( _x, _y, d ) { \
177 GLuint tmp = *(GLuint *)(buf + (_x)*4 + (_y)*pitch); \
178 tmp &= 0xff000000; \
179 tmp |= (d) & 0xffffff; \
180 *(GLuint *)(buf + (_x)*4 + (_y)*pitch) = tmp; \
181 }
182
183 #define READ_DEPTH( d, _x, _y ) \
184 d = *(GLuint *)(buf + (_x)*4 + (_y)*pitch) & 0xffffff;
185
186
187 #define TAG(x) intel##x##_z24_s8
188 #include "depthtmp.h"
189
190 #define WRITE_STENCIL( _x, _y, d ) { \
191 GLuint tmp = *(GLuint *)(buf + (_x)*4 + (_y)*pitch); \
192 tmp &= 0xffffff; \
193 tmp |= ((d)<<24); \
194 *(GLuint *)(buf + (_x)*4 + (_y)*pitch) = tmp; \
195 }
196
197 #define READ_STENCIL( d, _x, _y ) \
198 d = *(GLuint *)(buf + (_x)*4 + (_y)*pitch) >> 24;
199
200 #define TAG(x) intel##x##_z24_s8
201 #include "stenciltmp.h"
202
203
204 /* Move locking out to get reasonable span performance.
205 */
206 void intelSpanRenderStart( GLcontext *ctx )
207 {
208 struct intel_context *intel = intel_context(ctx);
209
210 LOCK_HARDWARE(intel);
211
212 /* Just map the framebuffer and all textures. Bufmgr code will
213 * take care of waiting on the necessary fences:
214 */
215 intel_region_map(intel, intel->front_region);
216 intel_region_map(intel, intel->back_region);
217 intel_region_map(intel, intel->depth_region);
218 }
219
220 void intelSpanRenderFinish( GLcontext *ctx )
221 {
222 struct intel_context *intel = intel_context( ctx );
223
224 _swrast_flush( ctx );
225
226 /* Now unmap the framebuffer:
227 */
228 intel_region_unmap(intel, intel->front_region);
229 intel_region_unmap(intel, intel->back_region);
230 intel_region_unmap(intel, intel->depth_region);
231
232 UNLOCK_HARDWARE( intel );
233 }
234
235 void intelInitSpanFuncs( GLcontext *ctx )
236 {
237 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
238 swdd->SpanRenderStart = intelSpanRenderStart;
239 swdd->SpanRenderFinish = intelSpanRenderFinish;
240 }
241
242
243 /**
244 * Plug in the Get/Put routines for the given driRenderbuffer.
245 */
246 void
247 intelSetSpanFunctions(driRenderbuffer *drb, const GLvisual *vis)
248 {
249 if (drb->Base.InternalFormat == GL_RGBA) {
250 if (vis->redBits == 5 && vis->greenBits == 5 && vis->blueBits == 5) {
251 intelInitPointers_555(&drb->Base);
252 }
253 else if (vis->redBits == 5 && vis->greenBits == 6 && vis->blueBits == 5) {
254 intelInitPointers_565(&drb->Base);
255 }
256 else {
257 assert(vis->redBits == 8);
258 assert(vis->greenBits == 8);
259 assert(vis->blueBits == 8);
260 intelInitPointers_8888(&drb->Base);
261 }
262 }
263 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT16) {
264 intelInitDepthPointers_z16(&drb->Base);
265 }
266 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT24) {
267 intelInitDepthPointers_z24_s8(&drb->Base);
268 }
269 else if (drb->Base.InternalFormat == GL_STENCIL_INDEX8_EXT) {
270 intelInitStencilPointers_z24_s8(&drb->Base);
271 }
272 }