intel: Remove client-side vblank code
[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 const __DRIextension *intelScreenExtensions[] = {
117 &driReadDrawableExtension,
118 &intelTexOffsetExtension.base,
119 &intelTexBufferExtension.base,
120 NULL
121 };
122
123 static GLboolean
124 intel_get_param(__DRIscreen *psp, int param, int *value)
125 {
126 int ret;
127 struct drm_i915_getparam gp;
128
129 gp.param = param;
130 gp.value = value;
131
132 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
133 if (ret) {
134 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
135 return GL_FALSE;
136 }
137
138 return GL_TRUE;
139 }
140
141 static void
142 intelDestroyScreen(__DRIscreen * sPriv)
143 {
144 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
145
146 dri_bufmgr_destroy(intelScreen->bufmgr);
147 driDestroyOptionInfo(&intelScreen->optionCache);
148
149 FREE(intelScreen);
150 sPriv->private = NULL;
151 }
152
153
154 /**
155 * This is called when we need to set up GL rendering to a new X window.
156 */
157 static GLboolean
158 intelCreateBuffer(__DRIscreen * driScrnPriv,
159 __DRIdrawable * driDrawPriv,
160 const __GLcontextModes * mesaVis, GLboolean isPixmap)
161 {
162 if (isPixmap) {
163 return GL_FALSE; /* not implemented */
164 }
165 else {
166 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
167 mesaVis->depthBits != 24);
168 gl_format rgbFormat;
169
170 struct intel_framebuffer *intel_fb = CALLOC_STRUCT(intel_framebuffer);
171
172 if (!intel_fb)
173 return GL_FALSE;
174
175 _mesa_initialize_framebuffer(&intel_fb->Base, mesaVis);
176
177 if (mesaVis->redBits == 5)
178 rgbFormat = MESA_FORMAT_RGB565;
179 else if (mesaVis->alphaBits == 0)
180 rgbFormat = MESA_FORMAT_XRGB8888;
181 else
182 rgbFormat = MESA_FORMAT_ARGB8888;
183
184 /* setup the hardware-based renderbuffers */
185 intel_fb->color_rb[0] = intel_create_renderbuffer(rgbFormat);
186 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT,
187 &intel_fb->color_rb[0]->Base);
188
189 if (mesaVis->doubleBufferMode) {
190 intel_fb->color_rb[1] = intel_create_renderbuffer(rgbFormat);
191
192 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT,
193 &intel_fb->color_rb[1]->Base);
194
195 }
196
197 if (mesaVis->depthBits == 24) {
198 if (mesaVis->stencilBits == 8) {
199 /* combined depth/stencil buffer */
200 struct intel_renderbuffer *depthStencilRb
201 = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
202 /* note: bind RB to two attachment points */
203 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
204 &depthStencilRb->Base);
205 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_STENCIL,
206 &depthStencilRb->Base);
207 } else {
208 struct intel_renderbuffer *depthRb
209 = intel_create_renderbuffer(MESA_FORMAT_X8_Z24);
210 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
211 &depthRb->Base);
212 }
213 }
214 else if (mesaVis->depthBits == 16) {
215 /* just 16-bit depth buffer, no hw stencil */
216 struct intel_renderbuffer *depthRb
217 = intel_create_renderbuffer(MESA_FORMAT_Z16);
218 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH, &depthRb->Base);
219 }
220
221 /* now add any/all software-based renderbuffers we may need */
222 _mesa_add_soft_renderbuffers(&intel_fb->Base,
223 GL_FALSE, /* never sw color */
224 GL_FALSE, /* never sw depth */
225 swStencil, mesaVis->accumRedBits > 0,
226 GL_FALSE, /* never sw alpha */
227 GL_FALSE /* never sw aux */ );
228 driDrawPriv->driverPrivate = (void *) intel_fb;
229
230 return GL_TRUE;
231 }
232 }
233
234 static void
235 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
236 {
237 struct intel_framebuffer *intel_fb = driDrawPriv->driverPrivate;
238 struct intel_renderbuffer *depth_rb;
239 struct intel_renderbuffer *stencil_rb;
240
241 if (intel_fb) {
242 if (intel_fb->color_rb[0]) {
243 intel_renderbuffer_set_region(intel_fb->color_rb[0], NULL);
244 }
245
246 if (intel_fb->color_rb[1]) {
247 intel_renderbuffer_set_region(intel_fb->color_rb[1], NULL);
248 }
249
250 depth_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
251 if (depth_rb) {
252 intel_renderbuffer_set_region(depth_rb, NULL);
253 }
254
255 stencil_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
256 if (stencil_rb) {
257 intel_renderbuffer_set_region(stencil_rb, NULL);
258 }
259 }
260
261 _mesa_reference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)), NULL);
262 }
263
264 /* There are probably better ways to do this, such as an
265 * init-designated function to register chipids and createcontext
266 * functions.
267 */
268 extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
269 __DRIcontext * driContextPriv,
270 void *sharedContextPrivate);
271
272 extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
273 __DRIcontext * driContextPriv,
274 void *sharedContextPrivate);
275 extern GLboolean brwCreateContext(const __GLcontextModes * mesaVis,
276 __DRIcontext * driContextPriv,
277 void *sharedContextPrivate);
278
279 static GLboolean
280 intelCreateContext(const __GLcontextModes * mesaVis,
281 __DRIcontext * driContextPriv,
282 void *sharedContextPrivate)
283 {
284 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
285 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
286
287 #ifdef I915
288 if (IS_9XX(intelScreen->deviceID)) {
289 if (!IS_965(intelScreen->deviceID)) {
290 return i915CreateContext(mesaVis, driContextPriv,
291 sharedContextPrivate);
292 }
293 } else {
294 intelScreen->no_vbo = GL_TRUE;
295 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
296 }
297 #else
298 if (IS_965(intelScreen->deviceID))
299 return brwCreateContext(mesaVis, driContextPriv, sharedContextPrivate);
300 #endif
301 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
302 return GL_FALSE;
303 }
304
305 static GLboolean
306 intel_init_bufmgr(intelScreenPrivate *intelScreen)
307 {
308 int gem_kernel = 0;
309 struct drm_i915_getparam gp;
310 __DRIscreen *spriv = intelScreen->driScrnPriv;
311 int num_fences = 0;
312
313 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
314
315 gp.param = I915_PARAM_HAS_GEM;
316 gp.value = &gem_kernel;
317
318 (void) drmCommandWriteRead(spriv->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
319
320 /* If we've got a new enough DDX that's initializing GEM and giving us
321 * object handles for the shared buffers, use that.
322 */
323 if (!intelScreen->driScrnPriv->dri2.enabled &&
324 intelScreen->driScrnPriv->ddx_version.minor < 9) {
325 fprintf(stderr, "[%s:%u] Error initializing GEM.\n",
326 __func__, __LINE__);
327 return GL_FALSE;
328 }
329
330 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
331 /* Otherwise, use the classic buffer manager. */
332 if (intelScreen->bufmgr == NULL) {
333 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
334 __func__, __LINE__);
335 return GL_FALSE;
336 }
337
338 if (intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences))
339 intelScreen->kernel_exec_fencing = !!num_fences;
340 else
341 intelScreen->kernel_exec_fencing = GL_FALSE;
342
343 return GL_TRUE;
344 }
345
346 struct intel_context *intelScreenContext(intelScreenPrivate *intelScreen)
347 {
348 /*
349 * This should probably change to have the screen allocate a dummy
350 * context at screen creation. For now just use the current context.
351 */
352
353 GET_CURRENT_CONTEXT(ctx);
354 if (ctx == NULL) {
355 _mesa_problem(NULL, "No current context in intelScreenContext\n");
356 return NULL;
357 }
358 return intel_context(ctx);
359 }
360
361 /**
362 * This is the driver specific part of the createNewScreen entry point.
363 * Called when using DRI2.
364 *
365 * \return the __GLcontextModes supported by this driver
366 */
367 static const
368 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
369 {
370 intelScreenPrivate *intelScreen;
371 GLenum fb_format[3];
372 GLenum fb_type[3];
373 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
374 * support pageflipping at all.
375 */
376 static const GLenum back_buffer_modes[] = {
377 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
378 };
379 uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
380 int color;
381 __DRIconfig **configs = NULL;
382
383 /* Allocate the private area */
384 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
385 if (!intelScreen) {
386 fprintf(stderr, "\nERROR! Allocating private area failed\n");
387 return GL_FALSE;
388 }
389 /* parse information in __driConfigOptions */
390 driParseOptionInfo(&intelScreen->optionCache,
391 __driConfigOptions, __driNConfigOptions);
392
393 intelScreen->driScrnPriv = psp;
394 psp->private = (void *) intelScreen;
395
396 intelScreen->drmMinor = psp->drm_version.minor;
397
398 /* Determine chipset ID */
399 if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
400 &intelScreen->deviceID))
401 return GL_FALSE;
402
403 if (!intel_init_bufmgr(intelScreen))
404 return GL_FALSE;
405
406 intelScreen->irq_active = 1;
407 psp->extensions = intelScreenExtensions;
408
409 depth_bits[0] = 0;
410 stencil_bits[0] = 0;
411 depth_bits[1] = 16;
412 stencil_bits[1] = 0;
413 depth_bits[2] = 24;
414 stencil_bits[2] = 0;
415 depth_bits[3] = 24;
416 stencil_bits[3] = 8;
417
418 msaa_samples_array[0] = 0;
419
420 fb_format[0] = GL_RGB;
421 fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
422
423 fb_format[1] = GL_BGR;
424 fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
425
426 fb_format[2] = GL_BGRA;
427 fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
428
429 depth_bits[0] = 0;
430 stencil_bits[0] = 0;
431
432 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
433 __DRIconfig **new_configs;
434 int depth_factor;
435
436 /* With DRI2 right now, GetBuffers always returns a depth/stencil buffer
437 * with the same cpp as the drawable. So we can't support depth cpp !=
438 * color cpp currently.
439 */
440 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
441 depth_bits[1] = 16;
442 stencil_bits[1] = 0;
443
444 depth_factor = 2;
445 } else {
446 depth_bits[1] = 24;
447 stencil_bits[1] = 0;
448 depth_bits[2] = 24;
449 stencil_bits[2] = 8;
450
451 depth_factor = 3;
452 }
453 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
454 depth_bits,
455 stencil_bits,
456 depth_factor,
457 back_buffer_modes,
458 ARRAY_SIZE(back_buffer_modes),
459 msaa_samples_array,
460 ARRAY_SIZE(msaa_samples_array));
461 if (configs == NULL)
462 configs = new_configs;
463 else
464 configs = driConcatConfigs(configs, new_configs);
465 }
466
467 if (configs == NULL) {
468 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
469 __LINE__);
470 return NULL;
471 }
472
473 return (const __DRIconfig **)configs;
474 }
475
476 const struct __DriverAPIRec driDriverAPI = {
477 .DestroyScreen = intelDestroyScreen,
478 .CreateContext = intelCreateContext,
479 .DestroyContext = intelDestroyContext,
480 .CreateBuffer = intelCreateBuffer,
481 .DestroyBuffer = intelDestroyBuffer,
482 .MakeCurrent = intelMakeCurrent,
483 .UnbindContext = intelUnbindContext,
484 .InitScreen2 = intelInitScreen2,
485 };
486
487 /* This is the table of extensions that the loader will dlsym() for. */
488 PUBLIC const __DRIextension *__driDriverExtensions[] = {
489 &driCoreExtension.base,
490 &driDRI2Extension.base,
491 NULL
492 };