intel: Implement DRI image extension
[mesa.git] / src / mesa / drivers / dri / intel / intel_screen.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 "main/glheader.h"
29 #include "main/context.h"
30 #include "main/framebuffer.h"
31 #include "main/renderbuffer.h"
32 #include "main/hash.h"
33
34 #include "utils.h"
35 #include "xmlpool.h"
36
37 #include "intel_batchbuffer.h"
38 #include "intel_buffers.h"
39 #include "intel_bufmgr.h"
40 #include "intel_chipset.h"
41 #include "intel_fbo.h"
42 #include "intel_screen.h"
43 #include "intel_tex.h"
44 #include "intel_regions.h"
45
46 #include "i915_drm.h"
47
48 #define DRI_CONF_TEXTURE_TILING(def) \
49 DRI_CONF_OPT_BEGIN(texture_tiling, bool, def) \
50 DRI_CONF_DESC(en, "Enable texture tiling") \
51 DRI_CONF_OPT_END \
52
53 PUBLIC const char __driConfigOptions[] =
54 DRI_CONF_BEGIN
55 DRI_CONF_SECTION_PERFORMANCE
56 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
57 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
58 * DRI_CONF_BO_REUSE_ALL
59 */
60 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
61 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
62 DRI_CONF_ENUM(0, "Disable buffer object reuse")
63 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
64 DRI_CONF_DESC_END
65 DRI_CONF_OPT_END
66
67 #ifdef I915
68 DRI_CONF_TEXTURE_TILING(false)
69 #else
70 DRI_CONF_TEXTURE_TILING(true)
71 #endif
72
73 DRI_CONF_OPT_BEGIN(early_z, bool, false)
74 DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
75 DRI_CONF_OPT_END
76
77 DRI_CONF_OPT_BEGIN(fragment_shader, bool, false)
78 DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
79 DRI_CONF_OPT_END
80
81 DRI_CONF_SECTION_END
82 DRI_CONF_SECTION_QUALITY
83 DRI_CONF_FORCE_S3TC_ENABLE(false)
84 DRI_CONF_ALLOW_LARGE_TEXTURES(2)
85 DRI_CONF_SECTION_END
86 DRI_CONF_SECTION_DEBUG
87 DRI_CONF_NO_RAST(false)
88 DRI_CONF_ALWAYS_FLUSH_BATCH(false)
89 DRI_CONF_ALWAYS_FLUSH_CACHE(false)
90
91 DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
92 DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
93 DRI_CONF_OPT_END
94 DRI_CONF_SECTION_END
95 DRI_CONF_END;
96
97 const GLuint __driNConfigOptions = 11;
98
99 #ifdef USE_NEW_INTERFACE
100 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
101 #endif /*USE_NEW_INTERFACE */
102
103 static const __DRItexOffsetExtension intelTexOffsetExtension = {
104 { __DRI_TEX_OFFSET },
105 intelSetTexOffset,
106 };
107
108 static const __DRItexBufferExtension intelTexBufferExtension = {
109 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
110 intelSetTexBuffer,
111 intelSetTexBuffer2,
112 };
113
114 static void
115 intelDRI2Flush(__DRIdrawable *drawable)
116 {
117 struct intel_context *intel = drawable->driContextPriv->driverPrivate;
118
119 if (intel->gen < 4)
120 INTEL_FIREVERTICES(intel);
121
122 if (intel->batch->map != intel->batch->ptr)
123 intel_batchbuffer_flush(intel->batch);
124 }
125
126 static void
127 intelDRI2Invalidate(__DRIdrawable *drawable)
128 {
129 struct intel_context *intel = drawable->driContextPriv->driverPrivate;
130
131 intel->using_dri2_swapbuffers = GL_TRUE;
132 dri2InvalidateDrawable(drawable);
133 }
134
135 static const struct __DRI2flushExtensionRec intelFlushExtension = {
136 { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
137 intelDRI2Flush,
138 intelDRI2Invalidate,
139 };
140
141 static __DRIimage *
142 intel_create_image_from_name(__DRIcontext *context,
143 int width, int height, int format,
144 int name, int pitch, void *loaderPrivate)
145 {
146 __DRIimage *image;
147 struct intel_context *intel = context->driverPrivate;
148 int cpp;
149
150 image = CALLOC(sizeof *image);
151 if (image == NULL)
152 return NULL;
153
154 switch (format) {
155 case __DRI_IMAGE_FORMAT_RGB565:
156 image->format = MESA_FORMAT_RGB565;
157 image->internal_format = GL_RGB;
158 image->data_type = GL_UNSIGNED_BYTE;
159 break;
160 case __DRI_IMAGE_FORMAT_XRGB8888:
161 image->format = MESA_FORMAT_XRGB8888;
162 image->internal_format = GL_RGB;
163 image->data_type = GL_UNSIGNED_BYTE;
164 break;
165 case __DRI_IMAGE_FORMAT_ARGB8888:
166 image->format = MESA_FORMAT_ARGB8888;
167 image->internal_format = GL_RGBA;
168 image->data_type = GL_UNSIGNED_BYTE;
169 break;
170 default:
171 free(image);
172 return NULL;
173 }
174
175 image->data = loaderPrivate;
176 cpp = _mesa_get_format_bytes(image->format);
177
178 image->region = intel_region_alloc_for_handle(intel, cpp, width, height,
179 pitch, name, "image");
180 if (image->region == NULL) {
181 FREE(image);
182 return NULL;
183 }
184
185 return image;
186 }
187
188 static __DRIimage *
189 intel_create_image_from_renderbuffer(__DRIcontext *context,
190 int renderbuffer, void *loaderPrivate)
191 {
192 __DRIimage *image;
193 struct intel_context *intel = context->driverPrivate;
194 struct gl_renderbuffer *rb;
195 struct intel_renderbuffer *irb;
196
197 rb = intel->ctx.CurrentRenderbuffer;
198 if (!rb) {
199 _mesa_error(&intel->ctx,
200 GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
201 return NULL;
202 }
203
204 irb = intel_renderbuffer(rb);
205 image = CALLOC(sizeof *image);
206 if (image == NULL)
207 return NULL;
208
209 image->internal_format = rb->InternalFormat;
210 image->format = rb->Format;
211 image->data_type = rb->DataType;
212 image->data = loaderPrivate;
213 intel_region_reference(&image->region, irb->region);
214
215 return image;
216 }
217
218 static void
219 intel_destroy_image(__DRIimage *image)
220 {
221 intel_region_release(&image->region);
222 FREE(image);
223 }
224
225 static struct __DRIimageExtensionRec intelImageExtension = {
226 { __DRI_IMAGE, __DRI_IMAGE_VERSION },
227 intel_create_image_from_name,
228 intel_create_image_from_renderbuffer,
229 intel_destroy_image,
230 };
231
232 static const __DRIextension *intelScreenExtensions[] = {
233 &driReadDrawableExtension,
234 &intelTexOffsetExtension.base,
235 &intelTexBufferExtension.base,
236 &intelFlushExtension.base,
237 &intelImageExtension.base,
238 NULL
239 };
240
241 static GLboolean
242 intel_get_param(__DRIscreen *psp, int param, int *value)
243 {
244 int ret;
245 struct drm_i915_getparam gp;
246
247 gp.param = param;
248 gp.value = value;
249
250 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
251 if (ret) {
252 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
253 return GL_FALSE;
254 }
255
256 return GL_TRUE;
257 }
258
259 static void
260 nop_callback(GLuint key, void *data, void *userData)
261 {
262 }
263
264 static void
265 intelDestroyScreen(__DRIscreen * sPriv)
266 {
267 struct intel_screen *intelScreen = sPriv->private;
268
269 dri_bufmgr_destroy(intelScreen->bufmgr);
270 driDestroyOptionInfo(&intelScreen->optionCache);
271
272 /* Some regions may still have references to them at this point, so
273 * flush the hash table to prevent _mesa_DeleteHashTable() from
274 * complaining about the hash not being empty; */
275 _mesa_HashDeleteAll(intelScreen->named_regions, nop_callback, NULL);
276 _mesa_DeleteHashTable(intelScreen->named_regions);
277
278 FREE(intelScreen);
279 sPriv->private = NULL;
280 }
281
282
283 /**
284 * This is called when we need to set up GL rendering to a new X window.
285 */
286 static GLboolean
287 intelCreateBuffer(__DRIscreen * driScrnPriv,
288 __DRIdrawable * driDrawPriv,
289 const __GLcontextModes * mesaVis, GLboolean isPixmap)
290 {
291 struct intel_renderbuffer *rb;
292
293 if (isPixmap) {
294 return GL_FALSE; /* not implemented */
295 }
296 else {
297 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
298 mesaVis->depthBits != 24);
299 gl_format rgbFormat;
300
301 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
302
303 if (!fb)
304 return GL_FALSE;
305
306 _mesa_initialize_window_framebuffer(fb, mesaVis);
307
308 if (mesaVis->redBits == 5)
309 rgbFormat = MESA_FORMAT_RGB565;
310 else if (mesaVis->alphaBits == 0)
311 rgbFormat = MESA_FORMAT_XRGB8888;
312 else
313 rgbFormat = MESA_FORMAT_ARGB8888;
314
315 /* setup the hardware-based renderbuffers */
316 rb = intel_create_renderbuffer(rgbFormat);
317 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
318
319 if (mesaVis->doubleBufferMode) {
320 rb = intel_create_renderbuffer(rgbFormat);
321 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
322 }
323
324 if (mesaVis->depthBits == 24) {
325 if (mesaVis->stencilBits == 8) {
326 /* combined depth/stencil buffer */
327 struct intel_renderbuffer *depthStencilRb
328 = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
329 /* note: bind RB to two attachment points */
330 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthStencilRb->Base);
331 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &depthStencilRb->Base);
332 } else {
333 struct intel_renderbuffer *depthRb
334 = intel_create_renderbuffer(MESA_FORMAT_X8_Z24);
335 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
336 }
337 }
338 else if (mesaVis->depthBits == 16) {
339 /* just 16-bit depth buffer, no hw stencil */
340 struct intel_renderbuffer *depthRb
341 = intel_create_renderbuffer(MESA_FORMAT_Z16);
342 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
343 }
344
345 /* now add any/all software-based renderbuffers we may need */
346 _mesa_add_soft_renderbuffers(fb,
347 GL_FALSE, /* never sw color */
348 GL_FALSE, /* never sw depth */
349 swStencil, mesaVis->accumRedBits > 0,
350 GL_FALSE, /* never sw alpha */
351 GL_FALSE /* never sw aux */ );
352 driDrawPriv->driverPrivate = fb;
353
354 return GL_TRUE;
355 }
356 }
357
358 static void
359 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
360 {
361 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
362
363 _mesa_reference_framebuffer(&fb, NULL);
364 }
365
366 /* There are probably better ways to do this, such as an
367 * init-designated function to register chipids and createcontext
368 * functions.
369 */
370 extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
371 __DRIcontext * driContextPriv,
372 void *sharedContextPrivate);
373
374 extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
375 __DRIcontext * driContextPriv,
376 void *sharedContextPrivate);
377 extern GLboolean brwCreateContext(const __GLcontextModes * mesaVis,
378 __DRIcontext * driContextPriv,
379 void *sharedContextPrivate);
380
381 static GLboolean
382 intelCreateContext(const __GLcontextModes * mesaVis,
383 __DRIcontext * driContextPriv,
384 void *sharedContextPrivate)
385 {
386 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
387 struct intel_screen *intelScreen = sPriv->private;
388
389 #ifdef I915
390 if (IS_9XX(intelScreen->deviceID)) {
391 if (!IS_965(intelScreen->deviceID)) {
392 return i915CreateContext(mesaVis, driContextPriv,
393 sharedContextPrivate);
394 }
395 } else {
396 intelScreen->no_vbo = GL_TRUE;
397 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
398 }
399 #else
400 if (IS_965(intelScreen->deviceID))
401 return brwCreateContext(mesaVis, driContextPriv, sharedContextPrivate);
402 #endif
403 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
404 return GL_FALSE;
405 }
406
407 static GLboolean
408 intel_init_bufmgr(struct intel_screen *intelScreen)
409 {
410 __DRIscreen *spriv = intelScreen->driScrnPriv;
411 int num_fences = 0;
412
413 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
414
415 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
416 /* Otherwise, use the classic buffer manager. */
417 if (intelScreen->bufmgr == NULL) {
418 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
419 __func__, __LINE__);
420 return GL_FALSE;
421 }
422
423 if (intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences))
424 intelScreen->kernel_exec_fencing = !!num_fences;
425 else
426 intelScreen->kernel_exec_fencing = GL_FALSE;
427
428 intelScreen->named_regions = _mesa_NewHashTable();
429
430 return GL_TRUE;
431 }
432
433 /**
434 * This is the driver specific part of the createNewScreen entry point.
435 * Called when using DRI2.
436 *
437 * \return the __GLcontextModes supported by this driver
438 */
439 static const
440 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
441 {
442 struct intel_screen *intelScreen;
443 GLenum fb_format[3];
444 GLenum fb_type[3];
445
446 static const GLenum back_buffer_modes[] = {
447 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
448 };
449 uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
450 int color;
451 __DRIconfig **configs = NULL;
452
453 /* Allocate the private area */
454 intelScreen = CALLOC(sizeof *intelScreen);
455 if (!intelScreen) {
456 fprintf(stderr, "\nERROR! Allocating private area failed\n");
457 return GL_FALSE;
458 }
459 /* parse information in __driConfigOptions */
460 driParseOptionInfo(&intelScreen->optionCache,
461 __driConfigOptions, __driNConfigOptions);
462
463 intelScreen->driScrnPriv = psp;
464 psp->private = (void *) intelScreen;
465
466 /* Determine chipset ID */
467 if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
468 &intelScreen->deviceID))
469 return GL_FALSE;
470
471 if (!intel_init_bufmgr(intelScreen))
472 return GL_FALSE;
473
474 psp->extensions = intelScreenExtensions;
475
476 msaa_samples_array[0] = 0;
477
478 fb_format[0] = GL_RGB;
479 fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
480
481 fb_format[1] = GL_BGR;
482 fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
483
484 fb_format[2] = GL_BGRA;
485 fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
486
487 depth_bits[0] = 0;
488 stencil_bits[0] = 0;
489
490 /* Generate a rich set of useful configs that do not include an
491 * accumulation buffer.
492 */
493 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
494 __DRIconfig **new_configs;
495 int depth_factor;
496
497 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
498 * buffer that has a diffferent number of bits per pixel than the color
499 * buffer. This isn't yet supported here.
500 */
501 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
502 depth_bits[1] = 16;
503 stencil_bits[1] = 0;
504 } else {
505 depth_bits[1] = 24;
506 stencil_bits[1] = 8;
507 }
508
509 depth_factor = 2;
510
511 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
512 depth_bits,
513 stencil_bits,
514 depth_factor,
515 back_buffer_modes,
516 ARRAY_SIZE(back_buffer_modes),
517 msaa_samples_array,
518 ARRAY_SIZE(msaa_samples_array),
519 GL_FALSE);
520 if (configs == NULL)
521 configs = new_configs;
522 else
523 configs = driConcatConfigs(configs, new_configs);
524 }
525
526 /* Generate the minimum possible set of configs that include an
527 * accumulation buffer.
528 */
529 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
530 __DRIconfig **new_configs;
531
532 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
533 depth_bits[0] = 16;
534 stencil_bits[0] = 0;
535 } else {
536 depth_bits[0] = 24;
537 stencil_bits[0] = 8;
538 }
539
540 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
541 depth_bits, stencil_bits, 1,
542 back_buffer_modes + 1, 1,
543 msaa_samples_array, 1,
544 GL_TRUE);
545 if (configs == NULL)
546 configs = new_configs;
547 else
548 configs = driConcatConfigs(configs, new_configs);
549 }
550
551 if (configs == NULL) {
552 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
553 __LINE__);
554 return NULL;
555 }
556
557 return (const __DRIconfig **)configs;
558 }
559
560 const struct __DriverAPIRec driDriverAPI = {
561 .DestroyScreen = intelDestroyScreen,
562 .CreateContext = intelCreateContext,
563 .DestroyContext = intelDestroyContext,
564 .CreateBuffer = intelCreateBuffer,
565 .DestroyBuffer = intelDestroyBuffer,
566 .MakeCurrent = intelMakeCurrent,
567 .UnbindContext = intelUnbindContext,
568 .InitScreen2 = intelInitScreen2,
569 };
570
571 /* This is the table of extensions that the loader will dlsym() for. */
572 PUBLIC const __DRIextension *__driDriverExtensions[] = {
573 &driCoreExtension.base,
574 &driDRI2Extension.base,
575 NULL
576 };