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