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