[g3dvl] move z-coord generation for multiple render targets into vertex shader
[mesa.git] / src / gallium / state_trackers / xorg / xorg_dri2.c
1 /*
2 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
3 * 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
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 *
26 * Author: Alan Hourihane <alanh@tungstengraphics.com>
27 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
28 *
29 */
30
31 #include "xorg-server.h"
32 #include "xf86.h"
33 #include "xf86_OSproc.h"
34
35 #include "xorg_tracker.h"
36 #include "xorg_exa.h"
37
38 #include "dri2.h"
39
40 #include "pipe/p_state.h"
41 #include "util/u_inlines.h"
42
43 #include "util/u_format.h"
44
45 #include "state_tracker/drm_driver.h"
46
47 /* Make all the #if cases in the code esier to read */
48 #ifndef DRI2INFOREC_VERSION
49 #define DRI2INFOREC_VERSION 1
50 #endif
51
52 #if DRI2INFOREC_VERSION == 2
53 static Bool set_format_in_do_create_buffer;
54 #endif
55
56 typedef struct {
57 PixmapPtr pPixmap;
58 struct pipe_resource *tex;
59 struct pipe_fence_handle *fence;
60 } *BufferPrivatePtr;
61
62 static Bool
63 dri2_do_create_buffer(DrawablePtr pDraw, DRI2BufferPtr buffer, unsigned int format)
64 {
65 struct pipe_resource *tex = NULL;
66 ScreenPtr pScreen = pDraw->pScreen;
67 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
68 modesettingPtr ms = modesettingPTR(pScrn);
69 struct exa_pixmap_priv *exa_priv;
70 BufferPrivatePtr private = buffer->driverPrivate;
71 PixmapPtr pPixmap;
72 struct winsys_handle whandle;
73
74 if (pDraw->type == DRAWABLE_PIXMAP)
75 pPixmap = (PixmapPtr) pDraw;
76 else
77 pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr) pDraw);
78 exa_priv = exaGetPixmapDriverPrivate(pPixmap);
79
80
81 switch (buffer->attachment) {
82 default:
83 if (buffer->attachment != DRI2BufferFakeFrontLeft ||
84 pDraw->type != DRAWABLE_PIXMAP) {
85 private->pPixmap = (*pScreen->CreatePixmap)(pScreen, pDraw->width,
86 pDraw->height,
87 pDraw->depth,
88 0);
89 }
90 break;
91 case DRI2BufferFrontLeft:
92 break;
93 case DRI2BufferStencil:
94 #if DRI2INFOREC_VERSION >= 3
95 case DRI2BufferDepthStencil:
96 #else
97 /* Works on old X servers because sanity checking is for the weak */
98 case 9:
99 #endif
100 if (exa_priv->depth_stencil_tex &&
101 !util_format_is_depth_or_stencil(exa_priv->depth_stencil_tex->format))
102 exa_priv->depth_stencil_tex = NULL;
103 /* Fall through */
104 case DRI2BufferDepth:
105 if (exa_priv->depth_stencil_tex)
106 pipe_resource_reference(&tex, exa_priv->depth_stencil_tex);
107 else {
108 struct pipe_resource template;
109 unsigned depthBits = (format != 0) ? format : pDraw->depth;
110 memset(&template, 0, sizeof(template));
111 template.target = PIPE_TEXTURE_2D;
112 if (buffer->attachment == DRI2BufferDepth) {
113 switch(depthBits) {
114 case 16:
115 template.format = PIPE_FORMAT_Z16_UNORM;
116 break;
117 case 32:
118 template.format = PIPE_FORMAT_Z32_UNORM;
119 break;
120 default:
121 template.format = ms->ds_depth_bits_last ?
122 PIPE_FORMAT_Z24X8_UNORM : PIPE_FORMAT_X8Z24_UNORM;
123 break;
124 }
125 } else {
126 template.format = ms->ds_depth_bits_last ?
127 PIPE_FORMAT_Z24_UNORM_S8_USCALED : PIPE_FORMAT_S8_USCALED_Z24_UNORM;
128 }
129 template.width0 = pDraw->width;
130 template.height0 = pDraw->height;
131 template.depth0 = 1;
132 template.last_level = 0;
133 template.bind = PIPE_BIND_DEPTH_STENCIL |
134 PIPE_BIND_SHARED;
135 tex = ms->screen->resource_create(ms->screen, &template);
136 pipe_resource_reference(&exa_priv->depth_stencil_tex, tex);
137 }
138 break;
139 }
140
141 if (!private->pPixmap) {
142 private->pPixmap = pPixmap;
143 pPixmap->refcnt++;
144 }
145
146 if (!tex) {
147 /* First call to make sure we have a pixmap private */
148 exaMoveInPixmap(private->pPixmap);
149 xorg_exa_set_shared_usage(private->pPixmap);
150 pScreen->ModifyPixmapHeader(private->pPixmap, 0, 0, 0, 0, 0, NULL);
151 /* Second call to make sure texture has valid contents */
152 exaMoveInPixmap(private->pPixmap);
153 tex = xorg_exa_get_texture(private->pPixmap);
154 }
155
156 if (!tex)
157 FatalError("NO TEXTURE IN DRI2\n");
158
159 memset(&whandle, 0, sizeof(whandle));
160 whandle.type = DRM_API_HANDLE_TYPE_SHARED;
161
162 ms->screen->resource_get_handle(ms->screen, tex, &whandle);
163
164 buffer->name = whandle.handle;
165 buffer->pitch = whandle.stride;
166 buffer->cpp = 4;
167 buffer->driverPrivate = private;
168 buffer->flags = 0; /* not tiled */
169 #if DRI2INFOREC_VERSION == 2
170 /* ABI forwards/backwards compatibility */
171 if (set_format_in_do_create_buffer)
172 ((DRI2Buffer2Ptr)buffer)->format = 0;
173 #elif DRI2INFOREC_VERSION >= 3
174 buffer->format = 0;
175 #endif
176 private->tex = tex;
177
178 return TRUE;
179 }
180
181 static void
182 dri2_do_destroy_buffer(DrawablePtr pDraw, DRI2BufferPtr buffer)
183 {
184 ScreenPtr pScreen = pDraw->pScreen;
185 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
186 modesettingPtr ms = modesettingPTR(pScrn);
187 BufferPrivatePtr private = buffer->driverPrivate;
188 struct exa_pixmap_priv *exa_priv = exaGetPixmapDriverPrivate(private->pPixmap);
189
190 pipe_resource_reference(&private->tex, NULL);
191 ms->screen->fence_reference(ms->screen, &private->fence, NULL);
192 pipe_resource_reference(&exa_priv->depth_stencil_tex, NULL);
193 (*pScreen->DestroyPixmap)(private->pPixmap);
194 }
195
196 #if DRI2INFOREC_VERSION >= 2
197
198 static DRI2Buffer2Ptr
199 dri2_create_buffer(DrawablePtr pDraw, unsigned int attachment, unsigned int format)
200 {
201 DRI2Buffer2Ptr buffer;
202 BufferPrivatePtr private;
203
204 buffer = calloc(1, sizeof *buffer);
205 if (!buffer)
206 return NULL;
207
208 private = calloc(1, sizeof *private);
209 if (!private) {
210 goto fail;
211 }
212
213 buffer->attachment = attachment;
214 buffer->driverPrivate = private;
215
216 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
217 if (dri2_do_create_buffer(pDraw, (DRI2BufferPtr)buffer, format))
218 return buffer;
219
220 free(private);
221 fail:
222 free(buffer);
223 return NULL;
224 }
225
226 static void
227 dri2_destroy_buffer(DrawablePtr pDraw, DRI2Buffer2Ptr buffer)
228 {
229 /* So far it is safe to downcast a DRI2Buffer2Ptr to DRI2BufferPtr */
230 dri2_do_destroy_buffer(pDraw, (DRI2BufferPtr)buffer);
231
232 free(buffer->driverPrivate);
233 free(buffer);
234 }
235
236 #endif /* DRI2INFOREC_VERSION >= 2 */
237
238 #if DRI2INFOREC_VERSION <= 2
239
240 static DRI2BufferPtr
241 dri2_create_buffers(DrawablePtr pDraw, unsigned int *attachments, int count)
242 {
243 BufferPrivatePtr privates;
244 DRI2BufferPtr buffers;
245 int i;
246
247 buffers = calloc(count, sizeof *buffers);
248 if (!buffers)
249 goto fail_buffers;
250
251 privates = calloc(count, sizeof *privates);
252 if (!privates)
253 goto fail_privates;
254
255 for (i = 0; i < count; i++) {
256 buffers[i].attachment = attachments[i];
257 buffers[i].driverPrivate = &privates[i];
258
259 if (!dri2_do_create_buffer(pDraw, &buffers[i], 0))
260 goto fail;
261 }
262
263 return buffers;
264
265 fail:
266 free(privates);
267 fail_privates:
268 free(buffers);
269 fail_buffers:
270 return NULL;
271 }
272
273 static void
274 dri2_destroy_buffers(DrawablePtr pDraw, DRI2BufferPtr buffers, int count)
275 {
276 int i;
277
278 for (i = 0; i < count; i++) {
279 dri2_do_destroy_buffer(pDraw, &buffers[i]);
280 }
281
282 if (buffers) {
283 free(buffers[0].driverPrivate);
284 free(buffers);
285 }
286 }
287
288 #endif /* DRI2INFOREC_VERSION <= 2 */
289
290 static void
291 dri2_copy_region(DrawablePtr pDraw, RegionPtr pRegion,
292 DRI2BufferPtr pDestBuffer, DRI2BufferPtr pSrcBuffer)
293 {
294 ScreenPtr pScreen = pDraw->pScreen;
295 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
296 modesettingPtr ms = modesettingPTR(pScrn);
297 BufferPrivatePtr dst_priv = pDestBuffer->driverPrivate;
298 BufferPrivatePtr src_priv = pSrcBuffer->driverPrivate;
299 DrawablePtr src_draw;
300 DrawablePtr dst_draw;
301 GCPtr gc;
302 RegionPtr copy_clip;
303 Bool save_accel;
304 CustomizerPtr cust = ms->cust;
305
306 /*
307 * In driCreateBuffers we dewrap windows into the
308 * backing pixmaps in order to get to the texture.
309 * We need to use the real drawable in CopyArea
310 * so that cliprects and offsets are correct.
311 */
312 src_draw = (pSrcBuffer->attachment == DRI2BufferFrontLeft) ? pDraw :
313 &src_priv->pPixmap->drawable;
314 dst_draw = (pDestBuffer->attachment == DRI2BufferFrontLeft) ? pDraw :
315 &dst_priv->pPixmap->drawable;
316
317 /*
318 * The clients implements glXWaitX with a copy front to fake and then
319 * waiting on the server to signal its completion of it. While
320 * glXWaitGL is a client side flush and a copy from fake to front.
321 * This is how it is done in the DRI2 protocol, how ever depending
322 * which type of drawables the server does things a bit differently
323 * then what the protocol says as the fake and front are the same.
324 *
325 * for pixmaps glXWaitX is a server flush.
326 * for pixmaps glXWaitGL is a client flush.
327 * for windows glXWaitX is a copy from front to fake then a server flush.
328 * for windows glXWaitGL is a client flush then a copy from fake to front.
329 *
330 * XXX in the windows case this code always flushes but that isn't a
331 * must in the glXWaitGL case but we don't know if this is a glXWaitGL
332 * or a glFlush/glFinish call.
333 */
334 if (dst_priv->pPixmap == src_priv->pPixmap) {
335 /* pixmap glXWaitX */
336 if (pSrcBuffer->attachment == DRI2BufferFrontLeft &&
337 pDestBuffer->attachment == DRI2BufferFakeFrontLeft) {
338 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS, NULL);
339 return;
340 }
341 /* pixmap glXWaitGL */
342 if (pDestBuffer->attachment == DRI2BufferFrontLeft &&
343 pSrcBuffer->attachment == DRI2BufferFakeFrontLeft) {
344 return;
345 } else {
346 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
347 "copying between the same pixmap\n");
348 }
349 }
350
351 gc = GetScratchGC(pDraw->depth, pScreen);
352 copy_clip = REGION_CREATE(pScreen, NULL, 0);
353 REGION_COPY(pScreen, copy_clip, pRegion);
354 (*gc->funcs->ChangeClip) (gc, CT_REGION, copy_clip, 0);
355 ValidateGC(dst_draw, gc);
356
357 /* If this is a full buffer swap, throttle on the previous one */
358 if (ms->swapThrottling &&
359 dst_priv->fence && REGION_NUM_RECTS(pRegion) == 1) {
360 BoxPtr extents = REGION_EXTENTS(pScreen, pRegion);
361
362 if (extents->x1 == 0 && extents->y1 == 0 &&
363 extents->x2 == pDraw->width && extents->y2 == pDraw->height) {
364 ms->screen->fence_finish(ms->screen, dst_priv->fence, 0);
365 ms->screen->fence_reference(ms->screen, &dst_priv->fence, NULL);
366 }
367 }
368
369 /* Try to make sure the blit will be accelerated */
370 save_accel = ms->exa->accel;
371 ms->exa->accel = TRUE;
372
373 /* In case it won't be though, make sure the GPU copy contents of the
374 * source pixmap will be used for the software fallback - presumably the
375 * client modified them before calling in here.
376 */
377 exaMoveInPixmap(src_priv->pPixmap);
378 DamageRegionAppend(src_draw, pRegion);
379 DamageRegionProcessPending(src_draw);
380
381 if (cust && cust->winsys_context_throttle)
382 cust->winsys_context_throttle(cust, ms->ctx, THROTTLE_SWAP);
383
384 (*gc->ops->CopyArea)(src_draw, dst_draw, gc,
385 0, 0, pDraw->width, pDraw->height, 0, 0);
386 ms->exa->accel = save_accel;
387
388 FreeScratchGC(gc);
389
390 ms->ctx->flush(ms->ctx, PIPE_FLUSH_SWAPBUFFERS,
391 (pDestBuffer->attachment == DRI2BufferFrontLeft
392 && ms->swapThrottling) ?
393 &dst_priv->fence : NULL);
394
395 if (cust && cust->winsys_context_throttle)
396 cust->winsys_context_throttle(cust, ms->ctx, THROTTLE_RENDER);
397
398 }
399
400 Bool
401 xorg_dri2_init(ScreenPtr pScreen)
402 {
403 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
404 modesettingPtr ms = modesettingPTR(pScrn);
405 DRI2InfoRec dri2info;
406 #if DRI2INFOREC_VERSION >= 2
407 int major, minor;
408
409 if (xf86LoaderCheckSymbol("DRI2Version")) {
410 DRI2Version(&major, &minor);
411 } else {
412 /* Assume version 1.0 */
413 major = 1;
414 minor = 0;
415 }
416 #endif
417
418 dri2info.version = min(DRI2INFOREC_VERSION, 3);
419 dri2info.fd = ms->fd;
420
421 dri2info.driverName = pScrn->driverName;
422 dri2info.deviceName = "/dev/dri/card0"; /* FIXME */
423
424 #if DRI2INFOREC_VERSION >= 2
425 dri2info.CreateBuffer = dri2_create_buffer;
426 dri2info.DestroyBuffer = dri2_destroy_buffer;
427 #endif
428
429 /* For X servers in the 1.6.x series there where two DRI2 version.
430 * This allows us to build one binary that works on both servers.
431 */
432 #if DRI2INFOREC_VERSION == 2
433 if (minor == 0) {
434 set_format_in_do_create_buffer = FALSE;
435 dri2info.CreateBuffers = dri2_create_buffers;
436 dri2info.DestroyBuffers = dri2_destroy_buffers;
437 } else
438 set_format_in_do_create_buffer = FALSE;
439 #endif
440
441 /* For version 1 set these unconditionaly. */
442 #if DRI2INFOREC_VERSION == 1
443 dri2info.CreateBuffers = dri2_create_buffers;
444 dri2info.DestroyBuffers = dri2_destroy_buffers;
445 #endif
446 dri2info.CopyRegion = dri2_copy_region;
447 dri2info.Wait = NULL;
448
449 ms->d_depth_bits_last =
450 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_Z24X8_UNORM,
451 PIPE_TEXTURE_2D,
452 0,
453 PIPE_BIND_DEPTH_STENCIL, 0);
454 ms->ds_depth_bits_last =
455 ms->screen->is_format_supported(ms->screen, PIPE_FORMAT_Z24_UNORM_S8_USCALED,
456 PIPE_TEXTURE_2D,
457 0,
458 PIPE_BIND_DEPTH_STENCIL, 0);
459
460 return DRI2ScreenInit(pScreen, &dri2info);
461 }
462
463 void
464 xorg_dri2_close(ScreenPtr pScreen)
465 {
466 DRI2CloseScreen(pScreen);
467 }
468
469 /* vim: set sw=4 ts=8 sts=4: */