Merge branch 'mesa_7_7_branch'
[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
33 #include "utils.h"
34 #include "xmlpool.h"
35
36 #include "intel_batchbuffer.h"
37 #include "intel_buffers.h"
38 #include "intel_bufmgr.h"
39 #include "intel_chipset.h"
40 #include "intel_extensions.h"
41 #include "intel_fbo.h"
42 #include "intel_regions.h"
43 #include "intel_screen.h"
44 #include "intel_span.h"
45 #include "intel_tex.h"
46
47 #include "i915_drm.h"
48 #include "i830_dri.h"
49
50 #define DRI_CONF_TEXTURE_TILING(def) \
51 DRI_CONF_OPT_BEGIN(texture_tiling, bool, def) \
52 DRI_CONF_DESC(en, "Enable texture tiling") \
53 DRI_CONF_OPT_END \
54
55 PUBLIC const char __driConfigOptions[] =
56 DRI_CONF_BEGIN
57 DRI_CONF_SECTION_PERFORMANCE
58 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
59 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
60 * DRI_CONF_BO_REUSE_ALL
61 */
62 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
63 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
64 DRI_CONF_ENUM(0, "Disable buffer object reuse")
65 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
66 DRI_CONF_DESC_END
67 DRI_CONF_OPT_END
68
69 #ifdef I915
70 DRI_CONF_TEXTURE_TILING(false)
71 #else
72 DRI_CONF_TEXTURE_TILING(true)
73 #endif
74
75 DRI_CONF_OPT_BEGIN(early_z, bool, false)
76 DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
77 DRI_CONF_OPT_END
78
79 DRI_CONF_OPT_BEGIN(fragment_shader, bool, false)
80 DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
81 DRI_CONF_OPT_END
82
83 DRI_CONF_SECTION_END
84 DRI_CONF_SECTION_QUALITY
85 DRI_CONF_FORCE_S3TC_ENABLE(false)
86 DRI_CONF_ALLOW_LARGE_TEXTURES(2)
87 DRI_CONF_SECTION_END
88 DRI_CONF_SECTION_DEBUG
89 DRI_CONF_NO_RAST(false)
90 DRI_CONF_ALWAYS_FLUSH_BATCH(false)
91 DRI_CONF_ALWAYS_FLUSH_CACHE(false)
92
93 DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
94 DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
95 DRI_CONF_OPT_END
96 DRI_CONF_SECTION_END
97 DRI_CONF_END;
98
99 const GLuint __driNConfigOptions = 11;
100
101 #ifdef USE_NEW_INTERFACE
102 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
103 #endif /*USE_NEW_INTERFACE */
104
105 static const __DRItexOffsetExtension intelTexOffsetExtension = {
106 { __DRI_TEX_OFFSET },
107 intelSetTexOffset,
108 };
109
110 static const __DRItexBufferExtension intelTexBufferExtension = {
111 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
112 intelSetTexBuffer,
113 intelSetTexBuffer2,
114 };
115
116 static void
117 intelDRI2Flush(__DRIdrawable *drawable)
118 {
119 struct intel_context *intel = drawable->driContextPriv->driverPrivate;
120
121 if (intel->gen < 4)
122 INTEL_FIREVERTICES(intel);
123
124 if (intel->batch->map != intel->batch->ptr)
125 intel_batchbuffer_flush(intel->batch);
126 }
127
128 static void
129 intelDRI2FlushInvalidate(__DRIdrawable *drawable)
130 {
131 struct intel_context *intel = drawable->driContextPriv->driverPrivate;
132
133 intelDRI2Flush(drawable);
134 drawable->validBuffers = GL_FALSE;
135
136 /* We're using FlushInvalidate as an indicator that a frame is
137 * done. It's only called immediately after SwapBuffers, so it
138 * won't affect front-buffer rendering or applications explicitly
139 * managing swap regions using MESA_copy_buffer.
140 *
141 * Wait for the swapbuffers before the one we just emitted, so we don't
142 * get too many swaps outstanding for apps that are GPU-heavy but not
143 * CPU-heavy.
144 *
145 * Unfortunately, we don't have a handle to the batch containing the swap,
146 * and getting our hands on that doesn't seem worth it, so we just use the
147 * first batch we emitted after the last swap.
148 */
149 if (intel->first_post_swapbuffers_batch != NULL) {
150 drm_intel_bo_wait_rendering(intel->first_post_swapbuffers_batch);
151 drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
152 intel->first_post_swapbuffers_batch = NULL;
153 }
154 }
155
156 static const struct __DRI2flushExtensionRec intelFlushExtension = {
157 { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
158 intelDRI2Flush,
159 intelDRI2FlushInvalidate,
160 };
161
162 static const __DRIextension *intelScreenExtensions[] = {
163 &driReadDrawableExtension,
164 &intelTexOffsetExtension.base,
165 &intelTexBufferExtension.base,
166 &intelFlushExtension.base,
167 NULL
168 };
169
170 static GLboolean
171 intel_get_param(__DRIscreen *psp, int param, int *value)
172 {
173 int ret;
174 struct drm_i915_getparam gp;
175
176 gp.param = param;
177 gp.value = value;
178
179 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
180 if (ret) {
181 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
182 return GL_FALSE;
183 }
184
185 return GL_TRUE;
186 }
187
188 static void
189 intelDestroyScreen(__DRIscreen * sPriv)
190 {
191 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
192
193 dri_bufmgr_destroy(intelScreen->bufmgr);
194 driDestroyOptionInfo(&intelScreen->optionCache);
195
196 FREE(intelScreen);
197 sPriv->private = NULL;
198 }
199
200
201 /**
202 * This is called when we need to set up GL rendering to a new X window.
203 */
204 static GLboolean
205 intelCreateBuffer(__DRIscreen * driScrnPriv,
206 __DRIdrawable * driDrawPriv,
207 const __GLcontextModes * mesaVis, GLboolean isPixmap)
208 {
209 struct intel_renderbuffer *rb;
210
211 if (isPixmap) {
212 return GL_FALSE; /* not implemented */
213 }
214 else {
215 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
216 mesaVis->depthBits != 24);
217 gl_format rgbFormat;
218
219 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
220
221 if (!fb)
222 return GL_FALSE;
223
224 _mesa_initialize_framebuffer(fb, mesaVis);
225
226 if (mesaVis->redBits == 5)
227 rgbFormat = MESA_FORMAT_RGB565;
228 else if (mesaVis->alphaBits == 0)
229 rgbFormat = MESA_FORMAT_XRGB8888;
230 else
231 rgbFormat = MESA_FORMAT_ARGB8888;
232
233 /* setup the hardware-based renderbuffers */
234 rb = intel_create_renderbuffer(rgbFormat);
235 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
236
237 if (mesaVis->doubleBufferMode) {
238 rb = intel_create_renderbuffer(rgbFormat);
239 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
240 }
241
242 if (mesaVis->depthBits == 24) {
243 if (mesaVis->stencilBits == 8) {
244 /* combined depth/stencil buffer */
245 struct intel_renderbuffer *depthStencilRb
246 = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
247 /* note: bind RB to two attachment points */
248 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthStencilRb->Base);
249 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &depthStencilRb->Base);
250 } else {
251 struct intel_renderbuffer *depthRb
252 = intel_create_renderbuffer(MESA_FORMAT_X8_Z24);
253 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
254 }
255 }
256 else if (mesaVis->depthBits == 16) {
257 /* just 16-bit depth buffer, no hw stencil */
258 struct intel_renderbuffer *depthRb
259 = intel_create_renderbuffer(MESA_FORMAT_Z16);
260 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
261 }
262
263 /* now add any/all software-based renderbuffers we may need */
264 _mesa_add_soft_renderbuffers(fb,
265 GL_FALSE, /* never sw color */
266 GL_FALSE, /* never sw depth */
267 swStencil, mesaVis->accumRedBits > 0,
268 GL_FALSE, /* never sw alpha */
269 GL_FALSE /* never sw aux */ );
270 driDrawPriv->driverPrivate = fb;
271
272 return GL_TRUE;
273 }
274 }
275
276 static void
277 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
278 {
279 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
280
281 _mesa_reference_framebuffer(&fb, NULL);
282 }
283
284 /* There are probably better ways to do this, such as an
285 * init-designated function to register chipids and createcontext
286 * functions.
287 */
288 extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
289 __DRIcontext * driContextPriv,
290 void *sharedContextPrivate);
291
292 extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
293 __DRIcontext * driContextPriv,
294 void *sharedContextPrivate);
295 extern GLboolean brwCreateContext(const __GLcontextModes * mesaVis,
296 __DRIcontext * driContextPriv,
297 void *sharedContextPrivate);
298
299 static GLboolean
300 intelCreateContext(const __GLcontextModes * mesaVis,
301 __DRIcontext * driContextPriv,
302 void *sharedContextPrivate)
303 {
304 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
305 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
306
307 #ifdef I915
308 if (IS_9XX(intelScreen->deviceID)) {
309 if (!IS_965(intelScreen->deviceID)) {
310 return i915CreateContext(mesaVis, driContextPriv,
311 sharedContextPrivate);
312 }
313 } else {
314 intelScreen->no_vbo = GL_TRUE;
315 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
316 }
317 #else
318 if (IS_965(intelScreen->deviceID))
319 return brwCreateContext(mesaVis, driContextPriv, sharedContextPrivate);
320 #endif
321 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
322 return GL_FALSE;
323 }
324
325 static GLboolean
326 intel_init_bufmgr(intelScreenPrivate *intelScreen)
327 {
328 __DRIscreen *spriv = intelScreen->driScrnPriv;
329 int num_fences = 0;
330
331 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
332
333 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
334 /* Otherwise, use the classic buffer manager. */
335 if (intelScreen->bufmgr == NULL) {
336 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
337 __func__, __LINE__);
338 return GL_FALSE;
339 }
340
341 if (intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences))
342 intelScreen->kernel_exec_fencing = !!num_fences;
343 else
344 intelScreen->kernel_exec_fencing = GL_FALSE;
345
346 return GL_TRUE;
347 }
348
349 /**
350 * This is the driver specific part of the createNewScreen entry point.
351 * Called when using DRI2.
352 *
353 * \return the __GLcontextModes supported by this driver
354 */
355 static const
356 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
357 {
358 intelScreenPrivate *intelScreen;
359 GLenum fb_format[3];
360 GLenum fb_type[3];
361
362 static const GLenum back_buffer_modes[] = {
363 GLX_NONE, GLX_SWAP_UNDEFINED_OML,
364 GLX_SWAP_EXCHANGE_OML, GLX_SWAP_COPY_OML
365 };
366 uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
367 int color;
368 __DRIconfig **configs = NULL;
369
370 /* Allocate the private area */
371 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
372 if (!intelScreen) {
373 fprintf(stderr, "\nERROR! Allocating private area failed\n");
374 return GL_FALSE;
375 }
376 /* parse information in __driConfigOptions */
377 driParseOptionInfo(&intelScreen->optionCache,
378 __driConfigOptions, __driNConfigOptions);
379
380 intelScreen->driScrnPriv = psp;
381 psp->private = (void *) intelScreen;
382
383 intelScreen->drmMinor = psp->drm_version.minor;
384
385 /* Determine chipset ID */
386 if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
387 &intelScreen->deviceID))
388 return GL_FALSE;
389
390 if (!intel_init_bufmgr(intelScreen))
391 return GL_FALSE;
392
393 intelScreen->irq_active = 1;
394 psp->extensions = intelScreenExtensions;
395
396 depth_bits[0] = 0;
397 stencil_bits[0] = 0;
398 depth_bits[1] = 16;
399 stencil_bits[1] = 0;
400 depth_bits[2] = 24;
401 stencil_bits[2] = 0;
402 depth_bits[3] = 24;
403 stencil_bits[3] = 8;
404
405 msaa_samples_array[0] = 0;
406
407 fb_format[0] = GL_RGB;
408 fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
409
410 fb_format[1] = GL_BGR;
411 fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
412
413 fb_format[2] = GL_BGRA;
414 fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
415
416 depth_bits[0] = 0;
417 stencil_bits[0] = 0;
418
419 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
420 __DRIconfig **new_configs;
421 int depth_factor;
422
423 /* With DRI2 right now, GetBuffers always returns a depth/stencil buffer
424 * with the same cpp as the drawable. So we can't support depth cpp !=
425 * color cpp currently.
426 */
427 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
428 depth_bits[1] = 16;
429 stencil_bits[1] = 0;
430
431 depth_factor = 2;
432 } else {
433 depth_bits[1] = 24;
434 stencil_bits[1] = 0;
435 depth_bits[2] = 24;
436 stencil_bits[2] = 8;
437
438 depth_factor = 3;
439 }
440 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
441 depth_bits,
442 stencil_bits,
443 depth_factor,
444 back_buffer_modes,
445 ARRAY_SIZE(back_buffer_modes),
446 msaa_samples_array,
447 ARRAY_SIZE(msaa_samples_array));
448 if (configs == NULL)
449 configs = new_configs;
450 else
451 configs = driConcatConfigs(configs, new_configs);
452 }
453
454 if (configs == NULL) {
455 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
456 __LINE__);
457 return NULL;
458 }
459
460 return (const __DRIconfig **)configs;
461 }
462
463 const struct __DriverAPIRec driDriverAPI = {
464 .DestroyScreen = intelDestroyScreen,
465 .CreateContext = intelCreateContext,
466 .DestroyContext = intelDestroyContext,
467 .CreateBuffer = intelCreateBuffer,
468 .DestroyBuffer = intelDestroyBuffer,
469 .MakeCurrent = intelMakeCurrent,
470 .UnbindContext = intelUnbindContext,
471 .InitScreen2 = intelInitScreen2,
472 };
473
474 /* This is the table of extensions that the loader will dlsym() for. */
475 PUBLIC const __DRIextension *__driDriverExtensions[] = {
476 &driCoreExtension.base,
477 &driDRI2Extension.base,
478 NULL
479 };