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