Merge remote branch 'origin/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 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 struct intel_renderbuffer *rb;
163
164 if (isPixmap) {
165 return GL_FALSE; /* not implemented */
166 }
167 else {
168 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
169 mesaVis->depthBits != 24);
170 gl_format rgbFormat;
171
172 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
173
174 if (!fb)
175 return GL_FALSE;
176
177 _mesa_initialize_framebuffer(fb, mesaVis);
178
179 if (mesaVis->redBits == 5)
180 rgbFormat = MESA_FORMAT_RGB565;
181 else if (mesaVis->alphaBits == 0)
182 rgbFormat = MESA_FORMAT_XRGB8888;
183 else
184 rgbFormat = MESA_FORMAT_ARGB8888;
185
186 /* setup the hardware-based renderbuffers */
187 rb = intel_create_renderbuffer(rgbFormat);
188 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
189
190 if (mesaVis->doubleBufferMode) {
191 rb = intel_create_renderbuffer(rgbFormat);
192 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
193 }
194
195 if (mesaVis->depthBits == 24) {
196 if (mesaVis->stencilBits == 8) {
197 /* combined depth/stencil buffer */
198 struct intel_renderbuffer *depthStencilRb
199 = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
200 /* note: bind RB to two attachment points */
201 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthStencilRb->Base);
202 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &depthStencilRb->Base);
203 } else {
204 struct intel_renderbuffer *depthRb
205 = intel_create_renderbuffer(MESA_FORMAT_X8_Z24);
206 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
207 }
208 }
209 else if (mesaVis->depthBits == 16) {
210 /* just 16-bit depth buffer, no hw stencil */
211 struct intel_renderbuffer *depthRb
212 = intel_create_renderbuffer(MESA_FORMAT_Z16);
213 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
214 }
215
216 /* now add any/all software-based renderbuffers we may need */
217 _mesa_add_soft_renderbuffers(fb,
218 GL_FALSE, /* never sw color */
219 GL_FALSE, /* never sw depth */
220 swStencil, mesaVis->accumRedBits > 0,
221 GL_FALSE, /* never sw alpha */
222 GL_FALSE /* never sw aux */ );
223 driDrawPriv->driverPrivate = fb;
224
225 return GL_TRUE;
226 }
227 }
228
229 static void
230 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
231 {
232 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
233
234 _mesa_reference_framebuffer(&fb, NULL);
235 }
236
237 /* There are probably better ways to do this, such as an
238 * init-designated function to register chipids and createcontext
239 * functions.
240 */
241 extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
242 __DRIcontext * driContextPriv,
243 void *sharedContextPrivate);
244
245 extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
246 __DRIcontext * driContextPriv,
247 void *sharedContextPrivate);
248 extern GLboolean brwCreateContext(const __GLcontextModes * mesaVis,
249 __DRIcontext * driContextPriv,
250 void *sharedContextPrivate);
251
252 static GLboolean
253 intelCreateContext(const __GLcontextModes * mesaVis,
254 __DRIcontext * driContextPriv,
255 void *sharedContextPrivate)
256 {
257 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
258 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
259
260 #ifdef I915
261 if (IS_9XX(intelScreen->deviceID)) {
262 if (!IS_965(intelScreen->deviceID)) {
263 return i915CreateContext(mesaVis, driContextPriv,
264 sharedContextPrivate);
265 }
266 } else {
267 intelScreen->no_vbo = GL_TRUE;
268 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
269 }
270 #else
271 if (IS_965(intelScreen->deviceID))
272 return brwCreateContext(mesaVis, driContextPriv, sharedContextPrivate);
273 #endif
274 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
275 return GL_FALSE;
276 }
277
278 static GLboolean
279 intel_init_bufmgr(intelScreenPrivate *intelScreen)
280 {
281 __DRIscreen *spriv = intelScreen->driScrnPriv;
282 int num_fences = 0;
283
284 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
285
286 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
287 /* Otherwise, use the classic buffer manager. */
288 if (intelScreen->bufmgr == NULL) {
289 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
290 __func__, __LINE__);
291 return GL_FALSE;
292 }
293
294 if (intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences))
295 intelScreen->kernel_exec_fencing = !!num_fences;
296 else
297 intelScreen->kernel_exec_fencing = GL_FALSE;
298
299 return GL_TRUE;
300 }
301
302 /**
303 * This is the driver specific part of the createNewScreen entry point.
304 * Called when using DRI2.
305 *
306 * \return the __GLcontextModes supported by this driver
307 */
308 static const
309 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
310 {
311 intelScreenPrivate *intelScreen;
312 GLenum fb_format[3];
313 GLenum fb_type[3];
314 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
315 * support pageflipping at all.
316 */
317 static const GLenum back_buffer_modes[] = {
318 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
319 };
320 uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
321 int color;
322 __DRIconfig **configs = NULL;
323
324 /* Allocate the private area */
325 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
326 if (!intelScreen) {
327 fprintf(stderr, "\nERROR! Allocating private area failed\n");
328 return GL_FALSE;
329 }
330 /* parse information in __driConfigOptions */
331 driParseOptionInfo(&intelScreen->optionCache,
332 __driConfigOptions, __driNConfigOptions);
333
334 intelScreen->driScrnPriv = psp;
335 psp->private = (void *) intelScreen;
336
337 intelScreen->drmMinor = psp->drm_version.minor;
338
339 /* Determine chipset ID */
340 if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
341 &intelScreen->deviceID))
342 return GL_FALSE;
343
344 if (!intel_init_bufmgr(intelScreen))
345 return GL_FALSE;
346
347 intelScreen->irq_active = 1;
348 psp->extensions = intelScreenExtensions;
349
350 depth_bits[0] = 0;
351 stencil_bits[0] = 0;
352 depth_bits[1] = 16;
353 stencil_bits[1] = 0;
354 depth_bits[2] = 24;
355 stencil_bits[2] = 0;
356 depth_bits[3] = 24;
357 stencil_bits[3] = 8;
358
359 msaa_samples_array[0] = 0;
360
361 fb_format[0] = GL_RGB;
362 fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
363
364 fb_format[1] = GL_BGR;
365 fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
366
367 fb_format[2] = GL_BGRA;
368 fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
369
370 depth_bits[0] = 0;
371 stencil_bits[0] = 0;
372
373 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
374 __DRIconfig **new_configs;
375 int depth_factor;
376
377 /* With DRI2 right now, GetBuffers always returns a depth/stencil buffer
378 * with the same cpp as the drawable. So we can't support depth cpp !=
379 * color cpp currently.
380 */
381 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
382 depth_bits[1] = 16;
383 stencil_bits[1] = 0;
384
385 depth_factor = 2;
386 } else {
387 depth_bits[1] = 24;
388 stencil_bits[1] = 0;
389 depth_bits[2] = 24;
390 stencil_bits[2] = 8;
391
392 depth_factor = 3;
393 }
394 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
395 depth_bits,
396 stencil_bits,
397 depth_factor,
398 back_buffer_modes,
399 ARRAY_SIZE(back_buffer_modes),
400 msaa_samples_array,
401 ARRAY_SIZE(msaa_samples_array));
402 if (configs == NULL)
403 configs = new_configs;
404 else
405 configs = driConcatConfigs(configs, new_configs);
406 }
407
408 if (configs == NULL) {
409 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
410 __LINE__);
411 return NULL;
412 }
413
414 return (const __DRIconfig **)configs;
415 }
416
417 const struct __DriverAPIRec driDriverAPI = {
418 .DestroyScreen = intelDestroyScreen,
419 .CreateContext = intelCreateContext,
420 .DestroyContext = intelDestroyContext,
421 .CreateBuffer = intelCreateBuffer,
422 .DestroyBuffer = intelDestroyBuffer,
423 .MakeCurrent = intelMakeCurrent,
424 .UnbindContext = intelUnbindContext,
425 .InitScreen2 = intelInitScreen2,
426 };
427
428 /* This is the table of extensions that the loader will dlsym() for. */
429 PUBLIC const __DRIextension *__driDriverExtensions[] = {
430 &driCoreExtension.base,
431 &driDRI2Extension.base,
432 NULL
433 };