intel: use new mipmap generation hooks in driver.
[mesa.git] / src / mesa / drivers / dri / intel / 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_fbo.h"
34 #include "intel_screen.h"
35 #include "intel_span.h"
36 #include "intel_regions.h"
37 #include "intel_ioctl.h"
38 #include "intel_tex.h"
39
40 #include "swrast/swrast.h"
41
42 /*
43 break intelWriteRGBASpan_ARGB8888
44 */
45
46 #undef DBG
47 #define DBG 0
48
49 #define LOCAL_VARS \
50 struct intel_context *intel = intel_context(ctx); \
51 struct intel_renderbuffer *irb = intel_renderbuffer(rb); \
52 const GLint yScale = irb->RenderToTexture ? 1 : -1; \
53 const GLint yBias = irb->RenderToTexture ? 0 : irb->Base.Height - 1; \
54 GLubyte *buf = (GLubyte *) irb->pfMap \
55 + (intel->drawY * irb->pfPitch + intel->drawX) * irb->region->cpp;\
56 GLuint p; \
57 assert(irb->pfMap);\
58 (void) p;
59
60 /* XXX FBO: this is identical to the macro in spantmp2.h except we get
61 * the cliprect info from the context, not the driDrawable.
62 * Move this into spantmp2.h someday.
63 */
64 #define HW_CLIPLOOP() \
65 do { \
66 int _nc = intel->numClipRects; \
67 while ( _nc-- ) { \
68 int minx = intel->pClipRects[_nc].x1 - intel->drawX; \
69 int miny = intel->pClipRects[_nc].y1 - intel->drawY; \
70 int maxx = intel->pClipRects[_nc].x2 - intel->drawX; \
71 int maxy = intel->pClipRects[_nc].y2 - intel->drawY;
72
73
74
75
76 #define Y_FLIP(_y) ((_y) * yScale + yBias)
77
78 #define HW_LOCK()
79
80 #define HW_UNLOCK()
81
82 /* 16 bit, RGB565 color spanline and pixel functions
83 */
84 #define SPANTMP_PIXEL_FMT GL_RGB
85 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_5_6_5
86
87 #define TAG(x) intel##x##_RGB565
88 #define TAG2(x,y) intel##x##_RGB565##y
89 #define GET_PTR(X,Y) (buf + ((Y) * irb->pfPitch + (X)) * 2)
90 #include "spantmp2.h"
91
92 /* 32 bit, ARGB8888 color spanline and pixel functions
93 */
94 #define SPANTMP_PIXEL_FMT GL_BGRA
95 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
96
97 #define TAG(x) intel##x##_ARGB8888
98 #define TAG2(x,y) intel##x##_ARGB8888##y
99 #define GET_PTR(X,Y) (buf + ((Y) * irb->pfPitch + (X)) * 4)
100 #include "spantmp2.h"
101
102
103 #define LOCAL_DEPTH_VARS \
104 struct intel_context *intel = intel_context(ctx); \
105 struct intel_renderbuffer *irb = intel_renderbuffer(rb); \
106 const GLuint pitch = irb->pfPitch/***XXX region->pitch*/; /* in pixels */ \
107 const GLint yScale = irb->RenderToTexture ? 1 : -1; \
108 const GLint yBias = irb->RenderToTexture ? 0 : irb->Base.Height - 1; \
109 char *buf = (char *) irb->pfMap/*XXX use region->map*/ + \
110 (intel->drawY * pitch + intel->drawX) * irb->region->cpp;
111
112
113 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
114
115 /**
116 ** 16-bit depthbuffer functions.
117 **/
118 #define WRITE_DEPTH( _x, _y, d ) \
119 ((GLushort *)buf)[(_x) + (_y) * pitch] = d;
120
121 #define READ_DEPTH( d, _x, _y ) \
122 d = ((GLushort *)buf)[(_x) + (_y) * pitch];
123
124
125 #define TAG(x) intel##x##_z16
126 #include "depthtmp.h"
127
128
129 /**
130 ** 24/8-bit interleaved depth/stencil functions
131 ** Note: we're actually reading back combined depth+stencil values.
132 ** The wrappers in main/depthstencil.c are used to extract the depth
133 ** and stencil values.
134 **/
135 /* Change ZZZS -> SZZZ */
136 #define WRITE_DEPTH( _x, _y, d ) { \
137 GLuint tmp = ((d) >> 8) | ((d) << 24); \
138 ((GLuint *)buf)[(_x) + (_y) * pitch] = tmp; \
139 }
140
141 /* Change SZZZ -> ZZZS */
142 #define READ_DEPTH( d, _x, _y ) { \
143 GLuint tmp = ((GLuint *)buf)[(_x) + (_y) * pitch]; \
144 d = (tmp << 8) | (tmp >> 24); \
145 }
146
147 #define TAG(x) intel##x##_z24_s8
148 #include "depthtmp.h"
149
150
151 /**
152 ** 8-bit stencil function (XXX FBO: This is obsolete)
153 **/
154 #define WRITE_STENCIL( _x, _y, d ) { \
155 GLuint tmp = ((GLuint *)buf)[(_x) + (_y) * pitch]; \
156 tmp &= 0xffffff; \
157 tmp |= ((d) << 24); \
158 ((GLuint *) buf)[(_x) + (_y) * pitch] = tmp; \
159 }
160
161 #define READ_STENCIL( d, _x, _y ) \
162 d = ((GLuint *)buf)[(_x) + (_y) * pitch] >> 24;
163
164 #define TAG(x) intel##x##_z24_s8
165 #include "stenciltmp.h"
166
167
168
169 /**
170 * Map or unmap all the renderbuffers which we may need during
171 * software rendering.
172 * XXX in the future, we could probably convey extra information to
173 * reduce the number of mappings needed. I.e. if doing a glReadPixels
174 * from the depth buffer, we really only need one mapping.
175 *
176 * XXX Rewrite this function someday.
177 * We can probably just loop over all the renderbuffer attachments,
178 * map/unmap all of them, and not worry about the _ColorDrawBuffers
179 * _ColorReadBuffer, _DepthBuffer or _StencilBuffer fields.
180 */
181 static void
182 intel_map_unmap_buffers(struct intel_context *intel, GLboolean map)
183 {
184 GLcontext *ctx = &intel->ctx;
185 GLuint i, j;
186 struct intel_renderbuffer *irb;
187
188 /* color draw buffers */
189 for (j = 0; j < ctx->DrawBuffer->_NumColorDrawBuffers; j++) {
190 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[j];
191 irb = intel_renderbuffer(rb);
192 if (irb) {
193 /* this is a user-created intel_renderbuffer */
194 if (irb->region) {
195 if (map)
196 intel_region_map(intel, irb->region);
197 else
198 intel_region_unmap(intel, irb->region);
199 irb->pfMap = irb->region->map;
200 irb->pfPitch = irb->region->pitch;
201 }
202 }
203 }
204
205 /* check for render to textures */
206 for (i = 0; i < BUFFER_COUNT; i++) {
207 struct gl_renderbuffer_attachment *att =
208 ctx->DrawBuffer->Attachment + i;
209 struct gl_texture_object *tex = att->Texture;
210 if (tex) {
211 /* render to texture */
212 ASSERT(att->Renderbuffer);
213 if (map) {
214 struct gl_texture_image *texImg;
215 texImg = tex->Image[att->CubeMapFace][att->TextureLevel];
216 intel_tex_map_images(intel, intel_texture_object(tex));
217 }
218 else {
219 intel_tex_unmap_images(intel, intel_texture_object(tex));
220 }
221 }
222 }
223
224 /* color read buffers */
225 irb = intel_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
226 if (irb && irb->region) {
227 if (map)
228 intel_region_map(intel, irb->region);
229 else
230 intel_region_unmap(intel, irb->region);
231 irb->pfMap = irb->region->map;
232 irb->pfPitch = irb->region->pitch;
233 }
234
235 /* Account for front/back color page flipping.
236 * The span routines use the pfMap and pfPitch fields which will
237 * swap the front/back region map/pitch if we're page flipped.
238 * Do this after mapping, above, so the map field is valid.
239 */
240 #if 0
241 if (map && ctx->DrawBuffer->Name == 0) {
242 struct intel_renderbuffer *irbFront
243 = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_FRONT_LEFT);
244 struct intel_renderbuffer *irbBack
245 = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_BACK_LEFT);
246 if (irbBack) {
247 /* double buffered */
248 if (intel->sarea->pf_current_page == 0) {
249 irbFront->pfMap = irbFront->region->map;
250 irbFront->pfPitch = irbFront->region->pitch;
251 irbBack->pfMap = irbBack->region->map;
252 irbBack->pfPitch = irbBack->region->pitch;
253 }
254 else {
255 irbFront->pfMap = irbBack->region->map;
256 irbFront->pfPitch = irbBack->region->pitch;
257 irbBack->pfMap = irbFront->region->map;
258 irbBack->pfPitch = irbFront->region->pitch;
259 }
260 }
261 }
262 #endif
263
264 /* depth buffer (Note wrapper!) */
265 if (ctx->DrawBuffer->_DepthBuffer) {
266 irb = intel_renderbuffer(ctx->DrawBuffer->_DepthBuffer->Wrapped);
267 if (irb && irb->region) {
268 if (map) {
269 intel_region_map(intel, irb->region);
270 irb->pfMap = irb->region->map;
271 irb->pfPitch = irb->region->pitch;
272 }
273 else {
274 intel_region_unmap(intel, irb->region);
275 irb->pfMap = irb->region->map;
276 irb->pfPitch = irb->region->pitch;
277 }
278 }
279 }
280
281 /* stencil buffer (Note wrapper!) */
282 if (ctx->DrawBuffer->_StencilBuffer) {
283 irb = intel_renderbuffer(ctx->DrawBuffer->_StencilBuffer->Wrapped);
284 if (irb && irb->region) {
285 if (map) {
286 intel_region_map(intel, irb->region);
287 irb->pfMap = irb->region->map;
288 irb->pfPitch = irb->region->pitch;
289 }
290 else {
291 intel_region_unmap(intel, irb->region);
292 irb->pfMap = irb->region->map;
293 irb->pfPitch = irb->region->pitch;
294 }
295 }
296 }
297 }
298
299
300
301 /**
302 * Prepare for softare rendering. Map current read/draw framebuffers'
303 * renderbuffes and all currently bound texture objects.
304 *
305 * Old note: Moved locking out to get reasonable span performance.
306 */
307 void
308 intelSpanRenderStart(GLcontext * ctx)
309 {
310 struct intel_context *intel = intel_context(ctx);
311 GLuint i;
312
313 intelFinish(&intel->ctx);
314 LOCK_HARDWARE(intel);
315
316 #if 0
317 /* Just map the framebuffer and all textures. Bufmgr code will
318 * take care of waiting on the necessary fences:
319 */
320 intel_region_map(intel, intel->front_region);
321 intel_region_map(intel, intel->back_region);
322 intel_region_map(intel, intel->depth_region);
323 #endif
324
325 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
326 if (ctx->Texture.Unit[i]._ReallyEnabled) {
327 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
328 intel_tex_map_images(intel, intel_texture_object(texObj));
329 }
330 }
331
332 intel_map_unmap_buffers(intel, GL_TRUE);
333 }
334
335 /**
336 * Called when done softare rendering. Unmap the buffers we mapped in
337 * the above function.
338 */
339 void
340 intelSpanRenderFinish(GLcontext * ctx)
341 {
342 struct intel_context *intel = intel_context(ctx);
343 GLuint i;
344
345 _swrast_flush(ctx);
346
347 /* Now unmap the framebuffer:
348 */
349 #if 0
350 intel_region_unmap(intel, intel->front_region);
351 intel_region_unmap(intel, intel->back_region);
352 intel_region_unmap(intel, intel->depth_region);
353 #endif
354
355 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
356 if (ctx->Texture.Unit[i]._ReallyEnabled) {
357 struct gl_texture_object *texObj = ctx->Texture.Unit[i]._Current;
358 intel_tex_unmap_images(intel, intel_texture_object(texObj));
359 }
360 }
361
362 intel_map_unmap_buffers(intel, GL_FALSE);
363
364 UNLOCK_HARDWARE(intel);
365 }
366
367
368 void
369 intelInitSpanFuncs(GLcontext * ctx)
370 {
371 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
372 swdd->SpanRenderStart = intelSpanRenderStart;
373 swdd->SpanRenderFinish = intelSpanRenderFinish;
374 }
375
376
377 /**
378 * Plug in appropriate span read/write functions for the given renderbuffer.
379 * These are used for the software fallbacks.
380 */
381 void
382 intel_set_span_functions(struct gl_renderbuffer *rb)
383 {
384 if (rb->_ActualFormat == GL_RGB5) {
385 /* 565 RGB */
386 intelInitPointers_RGB565(rb);
387 }
388 else if (rb->_ActualFormat == GL_RGBA8) {
389 /* 8888 RGBA */
390 intelInitPointers_ARGB8888(rb);
391 }
392 else if (rb->_ActualFormat == GL_DEPTH_COMPONENT16) {
393 intelInitDepthPointers_z16(rb);
394 }
395 else if (rb->_ActualFormat == GL_DEPTH_COMPONENT24 || /* XXX FBO remove */
396 rb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT) {
397 intelInitDepthPointers_z24_s8(rb);
398 }
399 else if (rb->_ActualFormat == GL_STENCIL_INDEX8_EXT) { /* XXX FBO remove */
400 intelInitStencilPointers_z24_s8(rb);
401 }
402 else {
403 _mesa_problem(NULL,
404 "Unexpected _ActualFormat in intelSetSpanFunctions");
405 }
406 }