intel: track move of bo_exec from drivers to bufmgr.
[mesa.git] / src / mesa / drivers / dri / intel / intel_context.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
29 #include "glheader.h"
30 #include "context.h"
31 #include "matrix.h"
32 #include "simple_list.h"
33 #include "extensions.h"
34 #include "framebuffer.h"
35 #include "imports.h"
36 #include "points.h"
37
38 #include "swrast/swrast.h"
39 #include "swrast_setup/swrast_setup.h"
40 #include "tnl/tnl.h"
41
42 #include "tnl/t_pipeline.h"
43 #include "tnl/t_vertex.h"
44
45 #include "drivers/common/driverfuncs.h"
46
47 #include "intel_screen.h"
48
49 #include "i830_dri.h"
50
51 #include "intel_chipset.h"
52 #include "intel_buffers.h"
53 #include "intel_tex.h"
54 #include "intel_batchbuffer.h"
55 #include "intel_blit.h"
56 #include "intel_pixel.h"
57 #include "intel_regions.h"
58 #include "intel_buffer_objects.h"
59 #include "intel_fbo.h"
60 #include "intel_decode.h"
61 #include "intel_bufmgr.h"
62
63 #include "drirenderbuffer.h"
64 #include "vblank.h"
65 #include "utils.h"
66 #include "xmlpool.h" /* for symbolic values of enum-type options */
67 #ifndef INTEL_DEBUG
68 int INTEL_DEBUG = (0);
69 #endif
70
71 #define need_GL_NV_point_sprite
72 #define need_GL_ARB_multisample
73 #define need_GL_ARB_point_parameters
74 #define need_GL_ARB_texture_compression
75 #define need_GL_ARB_vertex_buffer_object
76 #define need_GL_ARB_vertex_program
77 #define need_GL_ARB_window_pos
78 #define need_GL_ARB_occlusion_query
79 #define need_GL_EXT_blend_color
80 #define need_GL_EXT_blend_equation_separate
81 #define need_GL_EXT_blend_func_separate
82 #define need_GL_EXT_blend_minmax
83 #define need_GL_EXT_cull_vertex
84 #define need_GL_EXT_fog_coord
85 #define need_GL_EXT_framebuffer_object
86 #define need_GL_EXT_multi_draw_arrays
87 #define need_GL_EXT_secondary_color
88 #define need_GL_NV_vertex_program
89 #define need_GL_ATI_separate_stencil
90 #define need_GL_EXT_point_parameters
91 #define need_GL_VERSION_2_0
92 #define need_GL_VERSION_2_1
93 #define need_GL_ARB_shader_objects
94 #define need_GL_ARB_vertex_shader
95
96 #include "extension_helper.h"
97
98 #define DRIVER_DATE "20080716"
99 #define DRIVER_DATE_GEM "GEM " DRIVER_DATE
100
101 static const GLubyte *
102 intelGetString(GLcontext * ctx, GLenum name)
103 {
104 const struct intel_context *const intel = intel_context(ctx);
105 const char *chipset;
106 static char buffer[128];
107
108 switch (name) {
109 case GL_VENDOR:
110 return (GLubyte *) "Tungsten Graphics, Inc";
111 break;
112
113 case GL_RENDERER:
114 switch (intel->intelScreen->deviceID) {
115 case PCI_CHIP_845_G:
116 chipset = "Intel(R) 845G";
117 break;
118 case PCI_CHIP_I830_M:
119 chipset = "Intel(R) 830M";
120 break;
121 case PCI_CHIP_I855_GM:
122 chipset = "Intel(R) 852GM/855GM";
123 break;
124 case PCI_CHIP_I865_G:
125 chipset = "Intel(R) 865G";
126 break;
127 case PCI_CHIP_I915_G:
128 chipset = "Intel(R) 915G";
129 break;
130 case PCI_CHIP_E7221_G:
131 chipset = "Intel (R) E7221G (i915)";
132 break;
133 case PCI_CHIP_I915_GM:
134 chipset = "Intel(R) 915GM";
135 break;
136 case PCI_CHIP_I945_G:
137 chipset = "Intel(R) 945G";
138 break;
139 case PCI_CHIP_I945_GM:
140 chipset = "Intel(R) 945GM";
141 break;
142 case PCI_CHIP_I945_GME:
143 chipset = "Intel(R) 945GME";
144 break;
145 case PCI_CHIP_G33_G:
146 chipset = "Intel(R) G33";
147 break;
148 case PCI_CHIP_Q35_G:
149 chipset = "Intel(R) Q35";
150 break;
151 case PCI_CHIP_Q33_G:
152 chipset = "Intel(R) Q33";
153 break;
154 case PCI_CHIP_I965_Q:
155 chipset = "Intel(R) 965Q";
156 break;
157 case PCI_CHIP_I965_G:
158 case PCI_CHIP_I965_G_1:
159 chipset = "Intel(R) 965G";
160 break;
161 case PCI_CHIP_I946_GZ:
162 chipset = "Intel(R) 946GZ";
163 break;
164 case PCI_CHIP_I965_GM:
165 chipset = "Intel(R) 965GM";
166 break;
167 case PCI_CHIP_I965_GME:
168 chipset = "Intel(R) 965GME/GLE";
169 break;
170 case PCI_CHIP_GM45_GM:
171 chipset = "Mobile IntelĀ® GM45 Express Chipset";
172 break;
173 case PCI_CHIP_IGD_E_G:
174 chipset = "Intel(R) Integrated Graphics Device";
175 break;
176 case PCI_CHIP_G45_G:
177 chipset = "Intel(R) G45/G43";
178 break;
179 case PCI_CHIP_Q45_G:
180 chipset = "Intel(R) Q45/Q43";
181 break;
182 default:
183 chipset = "Unknown Intel Chipset";
184 break;
185 }
186
187 (void) driGetRendererString(buffer, chipset,
188 (intel->ttm) ? DRIVER_DATE_GEM : DRIVER_DATE,
189 0);
190 return (GLubyte *) buffer;
191
192 default:
193 return NULL;
194 }
195 }
196
197 void
198 intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
199 {
200 struct intel_framebuffer *intel_fb = drawable->driverPrivate;
201 struct intel_renderbuffer *rb;
202 struct intel_region *region, *depth_region;
203 struct intel_context *intel = context->driverPrivate;
204 __DRIbuffer *buffers;
205 __DRIscreen *screen;
206 int i, count;
207 unsigned int attachments[10];
208 uint32_t name;
209 const char *region_name;
210
211 if (INTEL_DEBUG & DEBUG_DRI)
212 fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
213
214 screen = intel->intelScreen->driScrnPriv;
215
216 i = 0;
217 if (intel_fb->color_rb[0])
218 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
219 if (intel_fb->color_rb[1])
220 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
221 if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH))
222 attachments[i++] = __DRI_BUFFER_DEPTH;
223 if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL))
224 attachments[i++] = __DRI_BUFFER_STENCIL;
225
226 buffers = (*screen->dri2.loader->getBuffers)(drawable,
227 &drawable->w,
228 &drawable->h,
229 attachments, i,
230 &count,
231 drawable->loaderPrivate);
232
233 drawable->x = 0;
234 drawable->y = 0;
235 drawable->backX = 0;
236 drawable->backY = 0;
237 drawable->numClipRects = 1;
238 drawable->pClipRects[0].x1 = 0;
239 drawable->pClipRects[0].y1 = 0;
240 drawable->pClipRects[0].x2 = drawable->w;
241 drawable->pClipRects[0].y2 = drawable->h;
242 drawable->numBackClipRects = 1;
243 drawable->pBackClipRects[0].x1 = 0;
244 drawable->pBackClipRects[0].y1 = 0;
245 drawable->pBackClipRects[0].x2 = drawable->w;
246 drawable->pBackClipRects[0].y2 = drawable->h;
247
248 depth_region = NULL;
249 for (i = 0; i < count; i++) {
250 switch (buffers[i].attachment) {
251 case __DRI_BUFFER_FRONT_LEFT:
252 rb = intel_fb->color_rb[0];
253 region_name = "dri2 front buffer";
254 break;
255
256 case __DRI_BUFFER_BACK_LEFT:
257 rb = intel_fb->color_rb[1];
258 region_name = "dri2 back buffer";
259 break;
260
261 case __DRI_BUFFER_DEPTH:
262 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
263 region_name = "dri2 depth buffer";
264 break;
265
266 case __DRI_BUFFER_STENCIL:
267 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
268 region_name = "dri2 stencil buffer";
269 break;
270
271 case __DRI_BUFFER_ACCUM:
272 default:
273 fprintf(stderr,
274 "unhandled buffer attach event, attacment type %d\n",
275 buffers[i].attachment);
276 return;
277 }
278
279 if (rb->region) {
280 dri_bo_flink(rb->region->buffer, &name);
281 if (name == buffers[i].name)
282 continue;
283 }
284
285 if (INTEL_DEBUG & DEBUG_DRI)
286 fprintf(stderr,
287 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
288 buffers[i].name, buffers[i].attachment,
289 buffers[i].cpp, buffers[i].pitch);
290
291 if (buffers[i].attachment == __DRI_BUFFER_STENCIL && depth_region) {
292 if (INTEL_DEBUG & DEBUG_DRI)
293 fprintf(stderr, "(reusing depth buffer as stencil)\n");
294 intel_region_reference(&region, depth_region);
295 }
296 else
297 region = intel_region_alloc_for_handle(intel, buffers[i].cpp,
298 buffers[i].pitch / buffers[i].cpp,
299 drawable->h,
300 buffers[i].name,
301 region_name);
302
303 if (buffers[i].attachment == __DRI_BUFFER_DEPTH)
304 depth_region = region;
305
306 intel_renderbuffer_set_region(rb, region);
307 intel_region_release(&region);
308 }
309
310 driUpdateFramebufferSize(&intel->ctx, drawable);
311 }
312
313 static void
314 intel_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
315 {
316 struct intel_context *intel = intel_context(ctx);
317 __DRIcontext *driContext = intel->driContext;
318
319 if (!driContext->driScreenPriv->dri2.enabled)
320 return;
321
322 intel_update_renderbuffers(driContext, driContext->driDrawablePriv);
323 if (driContext->driDrawablePriv != driContext->driReadablePriv)
324 intel_update_renderbuffers(driContext, driContext->driReadablePriv);
325
326 ctx->Driver.Viewport = NULL;
327 intel->driDrawable = driContext->driDrawablePriv;
328 intelWindowMoved(intel);
329 intel_draw_buffer(ctx, intel->ctx.DrawBuffer);
330 ctx->Driver.Viewport = intel_viewport;
331 }
332
333 /**
334 * Extension strings exported by the intel driver.
335 *
336 * Extensions supported by all chips supported by i830_dri, i915_dri, or
337 * i965_dri.
338 */
339 static const struct dri_extension card_extensions[] = {
340 {"GL_ARB_multisample", GL_ARB_multisample_functions},
341 {"GL_ARB_multitexture", NULL},
342 {"GL_ARB_point_parameters", GL_ARB_point_parameters_functions},
343 {"GL_NV_point_sprite", GL_NV_point_sprite_functions},
344 {"GL_ARB_texture_border_clamp", NULL},
345 {"GL_ARB_texture_compression", GL_ARB_texture_compression_functions},
346 {"GL_ARB_texture_cube_map", NULL},
347 {"GL_ARB_texture_env_add", NULL},
348 {"GL_ARB_texture_env_combine", NULL},
349 {"GL_ARB_texture_env_crossbar", NULL},
350 {"GL_ARB_texture_env_dot3", NULL},
351 {"GL_ARB_texture_mirrored_repeat", NULL},
352 {"GL_ARB_texture_non_power_of_two", NULL },
353 {"GL_ARB_texture_rectangle", NULL},
354 {"GL_NV_texture_rectangle", NULL},
355 {"GL_EXT_texture_rectangle", NULL},
356 {"GL_ARB_point_parameters", NULL},
357 {"GL_ARB_vertex_buffer_object", GL_ARB_vertex_buffer_object_functions},
358 {"GL_ARB_vertex_program", GL_ARB_vertex_program_functions},
359 {"GL_ARB_window_pos", GL_ARB_window_pos_functions},
360 {"GL_EXT_blend_color", GL_EXT_blend_color_functions},
361 {"GL_EXT_blend_equation_separate",
362 GL_EXT_blend_equation_separate_functions},
363 {"GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions},
364 {"GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions},
365 {"GL_EXT_blend_logic_op", NULL},
366 {"GL_EXT_blend_subtract", NULL},
367 {"GL_EXT_cull_vertex", GL_EXT_cull_vertex_functions},
368 {"GL_EXT_fog_coord", GL_EXT_fog_coord_functions},
369 {"GL_EXT_multi_draw_arrays", GL_EXT_multi_draw_arrays_functions},
370 {"GL_ATI_separate_stencil", GL_ATI_separate_stencil_functions},
371 #if 1 /* XXX FBO temporary? */
372 {"GL_EXT_packed_depth_stencil", NULL},
373 #endif
374 {"GL_EXT_secondary_color", GL_EXT_secondary_color_functions},
375 {"GL_EXT_stencil_wrap", NULL},
376 {"GL_EXT_texture_edge_clamp", NULL},
377 {"GL_EXT_texture_env_combine", NULL},
378 {"GL_EXT_texture_env_dot3", NULL},
379 {"GL_EXT_texture_filter_anisotropic", NULL},
380 {"GL_EXT_texture_lod_bias", NULL},
381 {"GL_3DFX_texture_compression_FXT1", NULL},
382 {"GL_APPLE_client_storage", NULL},
383 {"GL_MESA_pack_invert", NULL},
384 {"GL_MESA_ycbcr_texture", NULL},
385 {"GL_NV_blend_square", NULL},
386 {"GL_NV_vertex_program", GL_NV_vertex_program_functions},
387 {"GL_NV_vertex_program1_1", NULL},
388 { "GL_SGIS_generate_mipmap", NULL },
389 {NULL, NULL}
390 };
391
392 static const struct dri_extension brw_extensions[] = {
393 { "GL_ARB_shading_language_100", GL_VERSION_2_0_functions},
394 { "GL_ARB_shading_language_120", GL_VERSION_2_1_functions},
395 { "GL_ARB_shader_objects", GL_ARB_shader_objects_functions},
396 { "GL_ARB_vertex_shader", GL_ARB_vertex_shader_functions},
397 { "GL_ARB_point_sprite", NULL},
398 { "GL_ARB_fragment_shader", NULL },
399 { "GL_ARB_draw_buffers", NULL },
400 { "GL_ARB_depth_texture", NULL },
401 { "GL_ARB_fragment_program", NULL },
402 { "GL_ARB_shadow", NULL },
403 { "GL_EXT_shadow_funcs", NULL },
404 { "GL_ARB_fragment_program_shadow", NULL },
405 /* ARB extn won't work if not enabled */
406 { "GL_SGIX_depth_texture", NULL },
407 { "GL_EXT_texture_sRGB", NULL},
408 { NULL, NULL }
409 };
410
411 #ifdef I915_MMIO_READ
412 static const struct dri_extension arb_oc_extensions[] = {
413 {"GL_ARB_occlusion_query", GL_ARB_occlusion_query_functions},
414 {NULL, NULL}
415 };
416 #endif
417
418 static const struct dri_extension ttm_extensions[] = {
419 {"GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions},
420 {"GL_ARB_pixel_buffer_object", NULL},
421 {NULL, NULL}
422 };
423
424 /**
425 * Initializes potential list of extensions if ctx == NULL, or actually enables
426 * extensions for a context.
427 */
428 void intelInitExtensions(GLcontext *ctx, GLboolean enable_imaging)
429 {
430 struct intel_context *intel = ctx?intel_context(ctx):NULL;
431
432 /* Disable imaging extension until convolution is working in teximage paths.
433 */
434 enable_imaging = GL_FALSE;
435
436 driInitExtensions(ctx, card_extensions, enable_imaging);
437
438 if (intel == NULL || intel->ttm)
439 driInitExtensions(ctx, ttm_extensions, GL_FALSE);
440
441 #ifdef I915_MMIO_READ
442 if (intel == NULL ||
443 (IS_965(intel->intelScreen->deviceID) &&
444 intel->intelScreen->drmMinor >= 8))
445 driInitExtensions(ctx, arb_oc_extensions, GL_FALSE);
446 #endif
447
448 if (intel == NULL || IS_965(intel->intelScreen->deviceID))
449 driInitExtensions(ctx, brw_extensions, GL_FALSE);
450 }
451
452 static const struct dri_debug_control debug_control[] = {
453 { "tex", DEBUG_TEXTURE},
454 { "state", DEBUG_STATE},
455 { "ioctl", DEBUG_IOCTL},
456 { "blit", DEBUG_BLIT},
457 { "mip", DEBUG_MIPTREE},
458 { "fall", DEBUG_FALLBACKS},
459 { "verb", DEBUG_VERBOSE},
460 { "bat", DEBUG_BATCH},
461 { "pix", DEBUG_PIXEL},
462 { "buf", DEBUG_BUFMGR},
463 { "reg", DEBUG_REGION},
464 { "fbo", DEBUG_FBO},
465 { "lock", DEBUG_LOCK},
466 { "sync", DEBUG_SYNC},
467 { "prim", DEBUG_PRIMS },
468 { "vert", DEBUG_VERTS },
469 { "dri", DEBUG_DRI },
470 { "dma", DEBUG_DMA },
471 { "san", DEBUG_SANITY },
472 { "sleep", DEBUG_SLEEP },
473 { "stats", DEBUG_STATS },
474 { "tile", DEBUG_TILE },
475 { "sing", DEBUG_SINGLE_THREAD },
476 { "thre", DEBUG_SINGLE_THREAD },
477 { "wm", DEBUG_WM },
478 { "urb", DEBUG_URB },
479 { "vs", DEBUG_VS },
480 { NULL, 0 }
481 };
482
483
484 static void
485 intelInvalidateState(GLcontext * ctx, GLuint new_state)
486 {
487 struct intel_context *intel = intel_context(ctx);
488
489 _swrast_InvalidateState(ctx, new_state);
490 _swsetup_InvalidateState(ctx, new_state);
491 _vbo_InvalidateState(ctx, new_state);
492 _tnl_InvalidateState(ctx, new_state);
493 _tnl_invalidate_vertex_state(ctx, new_state);
494
495 intel->NewGLState |= new_state;
496
497 if (intel->vtbl.invalidate_state)
498 intel->vtbl.invalidate_state( intel, new_state );
499 }
500
501
502 void
503 intelFlush(GLcontext * ctx)
504 {
505 struct intel_context *intel = intel_context(ctx);
506
507 if (intel->Fallback)
508 _swrast_flush(ctx);
509
510 if (!IS_965(intel->intelScreen->deviceID))
511 INTEL_FIREVERTICES(intel);
512
513 /* Emit a flush so that any frontbuffer rendering that might have occurred
514 * lands onscreen in a timely manner, even if the X Server doesn't trigger
515 * a flush for us.
516 */
517 intel_batchbuffer_emit_mi_flush(intel->batch);
518
519 if (intel->batch->map != intel->batch->ptr)
520 intel_batchbuffer_flush(intel->batch);
521 }
522
523 void
524 intelFinish(GLcontext * ctx)
525 {
526 struct gl_framebuffer *fb = ctx->DrawBuffer;
527 int i;
528
529 intelFlush(ctx);
530
531 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
532 struct intel_renderbuffer *irb;
533
534 irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
535
536 if (irb->region)
537 dri_bo_wait_rendering(irb->region->buffer);
538 }
539 if (fb->_DepthBuffer) {
540 /* XXX: Wait on buffer idle */
541 }
542 }
543
544 #ifdef I915_MMIO_READ
545 static void
546 intelBeginQuery(GLcontext *ctx, GLenum target, struct gl_query_object *q)
547 {
548 struct intel_context *intel = intel_context( ctx );
549 struct drm_i915_mmio io = {
550 .read_write = I915_MMIO_READ,
551 .reg = MMIO_REGS_PS_DEPTH_COUNT,
552 .data = &q->Result
553 };
554 intel->stats_wm++;
555 intelFinish(&intel->ctx);
556 drmCommandWrite(intel->driFd, DRM_I915_MMIO, &io, sizeof(io));
557 }
558
559 static void
560 intelEndQuery(GLcontext *ctx, GLenum target, struct gl_query_object *q)
561 {
562 struct intel_context *intel = intel_context( ctx );
563 GLuint64EXT tmp;
564 struct drm_i915_mmio io = {
565 .read_write = I915_MMIO_READ,
566 .reg = MMIO_REGS_PS_DEPTH_COUNT,
567 .data = &tmp
568 };
569 intelFinish(&intel->ctx);
570 drmCommandWrite(intel->driFd, DRM_I915_MMIO, &io, sizeof(io));
571 q->Result = tmp - q->Result;
572 q->Ready = GL_TRUE;
573 intel->stats_wm--;
574 }
575 #endif
576
577 void
578 intelInitDriverFunctions(struct dd_function_table *functions)
579 {
580 _mesa_init_driver_functions(functions);
581
582 functions->Flush = intelFlush;
583 functions->Finish = intelFinish;
584 functions->GetString = intelGetString;
585 functions->UpdateState = intelInvalidateState;
586 functions->Viewport = intel_viewport;
587
588 functions->CopyColorTable = _swrast_CopyColorTable;
589 functions->CopyColorSubTable = _swrast_CopyColorSubTable;
590 functions->CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
591 functions->CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
592
593 #ifdef I915_MMIO_READ
594 functions->BeginQuery = intelBeginQuery;
595 functions->EndQuery = intelEndQuery;
596 #endif
597
598 intelInitTextureFuncs(functions);
599 intelInitStateFuncs(functions);
600 intelInitBufferFuncs(functions);
601 intelInitPixelFuncs(functions);
602 }
603
604
605 GLboolean
606 intelInitContext(struct intel_context *intel,
607 const __GLcontextModes * mesaVis,
608 __DRIcontextPrivate * driContextPriv,
609 void *sharedContextPrivate,
610 struct dd_function_table *functions)
611 {
612 GLcontext *ctx = &intel->ctx;
613 GLcontext *shareCtx = (GLcontext *) sharedContextPrivate;
614 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
615 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
616 int fthrottle_mode;
617
618 if (!_mesa_initialize_context(&intel->ctx, mesaVis, shareCtx,
619 functions, (void *) intel)) {
620 _mesa_printf("%s: failed to init mesa context\n", __FUNCTION__);
621 return GL_FALSE;
622 }
623
624 driContextPriv->driverPrivate = intel;
625 intel->intelScreen = intelScreen;
626 intel->driScreen = sPriv;
627 intel->sarea = intelScreen->sarea;
628 intel->driContext = driContextPriv;
629
630 /* Dri stuff */
631 intel->hHWContext = driContextPriv->hHWContext;
632 intel->driFd = sPriv->fd;
633 intel->driHwLock = sPriv->lock;
634
635 intel->width = intelScreen->width;
636 intel->height = intelScreen->height;
637
638 driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
639 intel->driScreen->myNum,
640 IS_965(intelScreen->deviceID) ? "i965" : "i915");
641 if (intelScreen->deviceID == PCI_CHIP_I865_G)
642 intel->maxBatchSize = 4096;
643 else
644 intel->maxBatchSize = BATCH_SZ;
645
646 intel->bufmgr = intelScreen->bufmgr;
647 intel->ttm = intelScreen->ttm;
648 if (intel->ttm) {
649 int bo_reuse_mode;
650
651 bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
652 switch (bo_reuse_mode) {
653 case DRI_CONF_BO_REUSE_DISABLED:
654 break;
655 case DRI_CONF_BO_REUSE_ALL:
656 intel_bufmgr_gem_enable_reuse(intel->bufmgr);
657 break;
658 }
659 }
660
661 ctx->Const.MaxTextureMaxAnisotropy = 2.0;
662
663 /* This doesn't yet catch all non-conformant rendering, but it's a
664 * start.
665 */
666 if (getenv("INTEL_STRICT_CONFORMANCE")) {
667 intel->strict_conformance = 1;
668 }
669
670 if (intel->strict_conformance) {
671 ctx->Const.MinLineWidth = 1.0;
672 ctx->Const.MinLineWidthAA = 1.0;
673 ctx->Const.MaxLineWidth = 1.0;
674 ctx->Const.MaxLineWidthAA = 1.0;
675 ctx->Const.LineWidthGranularity = 1.0;
676 }
677 else {
678 ctx->Const.MinLineWidth = 1.0;
679 ctx->Const.MinLineWidthAA = 1.0;
680 ctx->Const.MaxLineWidth = 5.0;
681 ctx->Const.MaxLineWidthAA = 5.0;
682 ctx->Const.LineWidthGranularity = 0.5;
683 }
684
685 ctx->Const.MinPointSize = 1.0;
686 ctx->Const.MinPointSizeAA = 1.0;
687 ctx->Const.MaxPointSize = 255.0;
688 ctx->Const.MaxPointSizeAA = 3.0;
689 ctx->Const.PointSizeGranularity = 1.0;
690
691 /* reinitialize the context point state.
692 * It depend on constants in __GLcontextRec::Const
693 */
694 _mesa_init_point(ctx);
695
696 ctx->Const.MaxColorAttachments = 4; /* XXX FBO: review this */
697
698 /* Initialize the software rasterizer and helper modules. */
699 _swrast_CreateContext(ctx);
700 _vbo_CreateContext(ctx);
701 _tnl_CreateContext(ctx);
702 _swsetup_CreateContext(ctx);
703
704 /* Configure swrast to match hardware characteristics: */
705 _swrast_allow_pixel_fog(ctx, GL_FALSE);
706 _swrast_allow_vertex_fog(ctx, GL_TRUE);
707
708 intel->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
709 intel->hw_stipple = 1;
710
711 /* XXX FBO: this doesn't seem to be used anywhere */
712 switch (mesaVis->depthBits) {
713 case 0: /* what to do in this case? */
714 case 16:
715 intel->polygon_offset_scale = 1.0;
716 break;
717 case 24:
718 intel->polygon_offset_scale = 2.0; /* req'd to pass glean */
719 break;
720 default:
721 assert(0);
722 break;
723 }
724
725 if (IS_965(intelScreen->deviceID))
726 intel->polygon_offset_scale /= 0xffff;
727
728 intel->RenderIndex = ~0;
729
730 fthrottle_mode = driQueryOptioni(&intel->optionCache, "fthrottle_mode");
731 intel->irqsEmitted = 0;
732
733 intel->do_irqs = (intel->intelScreen->irq_active &&
734 fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS);
735
736 intel->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
737
738 _math_matrix_ctr(&intel->ViewportMatrix);
739
740 if (IS_965(intelScreen->deviceID) && !intel->intelScreen->irq_active) {
741 _mesa_printf("IRQs not active. Exiting\n");
742 exit(1);
743 }
744
745 intelInitExtensions(ctx, GL_FALSE);
746
747 INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
748 if (INTEL_DEBUG & DEBUG_BUFMGR)
749 dri_bufmgr_set_debug(intel->bufmgr, GL_TRUE);
750
751 if (!sPriv->dri2.enabled)
752 intel_recreate_static_regions(intel);
753
754 intel->batch = intel_batchbuffer_alloc(intel);
755
756 intel_bufferobj_init(intel);
757 intel_fbo_init(intel);
758
759 if (intel->ctx.Mesa_DXTn) {
760 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
761 _mesa_enable_extension(ctx, "GL_S3_s3tc");
762 }
763 else if (driQueryOptionb(&intel->optionCache, "force_s3tc_enable")) {
764 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
765 }
766
767 intel->prim.primitive = ~0;
768
769 /* Force all software fallbacks */
770 if (driQueryOptionb(&intel->optionCache, "no_rast")) {
771 fprintf(stderr, "disabling 3D rasterization\n");
772 intel->no_rast = 1;
773 }
774
775 /* Disable all hardware rendering (skip emitting batches and fences/waits
776 * to the kernel)
777 */
778 intel->no_hw = getenv("INTEL_NO_HW") != NULL;
779
780 return GL_TRUE;
781 }
782
783 void
784 intelDestroyContext(__DRIcontextPrivate * driContextPriv)
785 {
786 struct intel_context *intel =
787 (struct intel_context *) driContextPriv->driverPrivate;
788
789 assert(intel); /* should never be null */
790 if (intel) {
791 GLboolean release_texture_heaps;
792
793 INTEL_FIREVERTICES(intel);
794
795 intel->vtbl.destroy(intel);
796
797 release_texture_heaps = (intel->ctx.Shared->RefCount == 1);
798 _swsetup_DestroyContext(&intel->ctx);
799 _tnl_DestroyContext(&intel->ctx);
800 _vbo_DestroyContext(&intel->ctx);
801
802 _swrast_DestroyContext(&intel->ctx);
803 intel->Fallback = 0; /* don't call _swrast_Flush later */
804
805 intel_batchbuffer_free(intel->batch);
806 free(intel->prim.vb);
807
808 if (release_texture_heaps) {
809 /* This share group is about to go away, free our private
810 * texture object data.
811 */
812 if (INTEL_DEBUG & DEBUG_TEXTURE)
813 fprintf(stderr, "do something to free texture heaps\n");
814 }
815
816 /* free the Mesa context */
817 _mesa_free_context_data(&intel->ctx);
818
819 dri_bufmgr_destroy(intel->bufmgr);
820 }
821 }
822
823 GLboolean
824 intelUnbindContext(__DRIcontextPrivate * driContextPriv)
825 {
826 return GL_TRUE;
827 }
828
829 GLboolean
830 intelMakeCurrent(__DRIcontextPrivate * driContextPriv,
831 __DRIdrawablePrivate * driDrawPriv,
832 __DRIdrawablePrivate * driReadPriv)
833 {
834 __DRIscreenPrivate *psp = driDrawPriv->driScreenPriv;
835
836 if (driContextPriv) {
837 struct intel_context *intel =
838 (struct intel_context *) driContextPriv->driverPrivate;
839 struct intel_framebuffer *intel_fb =
840 (struct intel_framebuffer *) driDrawPriv->driverPrivate;
841 GLframebuffer *readFb = (GLframebuffer *) driReadPriv->driverPrivate;
842
843 if (driContextPriv->driScreenPriv->dri2.enabled) {
844 intel_update_renderbuffers(driContextPriv, driDrawPriv);
845 if (driDrawPriv != driReadPriv)
846 intel_update_renderbuffers(driContextPriv, driReadPriv);
847 } else {
848 /* XXX FBO temporary fix-ups! */
849 /* if the renderbuffers don't have regions, init them from the context */
850 struct intel_renderbuffer *irbDepth
851 = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
852 struct intel_renderbuffer *irbStencil
853 = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
854
855 if (intel_fb->color_rb[0]) {
856 intel_renderbuffer_set_region(intel_fb->color_rb[0],
857 intel->front_region);
858 }
859 if (intel_fb->color_rb[1]) {
860 intel_renderbuffer_set_region(intel_fb->color_rb[1],
861 intel->back_region);
862 }
863 #if 0
864 if (intel_fb->color_rb[2]) {
865 intel_renderbuffer_set_region(intel_fb->color_rb[2],
866 intel->third_region);
867 }
868 #endif
869 if (irbDepth) {
870 intel_renderbuffer_set_region(irbDepth, intel->depth_region);
871 }
872 if (irbStencil) {
873 intel_renderbuffer_set_region(irbStencil, intel->depth_region);
874 }
875 }
876
877 /* set GLframebuffer size to match window, if needed */
878 driUpdateFramebufferSize(&intel->ctx, driDrawPriv);
879
880 if (driReadPriv != driDrawPriv) {
881 driUpdateFramebufferSize(&intel->ctx, driReadPriv);
882 }
883
884 _mesa_make_current(&intel->ctx, &intel_fb->Base, readFb);
885
886 /* The drawbuffer won't always be updated by _mesa_make_current:
887 */
888 if (intel->ctx.DrawBuffer == &intel_fb->Base) {
889
890 if (intel->driReadDrawable != driReadPriv)
891 intel->driReadDrawable = driReadPriv;
892
893 if (intel->driDrawable != driDrawPriv) {
894 if (driDrawPriv->swap_interval == (unsigned)-1) {
895 int i;
896
897 driDrawPriv->vblFlags = (intel->intelScreen->irq_active != 0)
898 ? driGetDefaultVBlankFlags(&intel->optionCache)
899 : VBLANK_FLAG_NO_IRQ;
900
901 (*psp->systemTime->getUST) (&intel_fb->swap_ust);
902 driDrawableInitVBlank(driDrawPriv);
903 intel_fb->vbl_waited = driDrawPriv->vblSeq;
904
905 for (i = 0; i < (intel->intelScreen->third.handle ? 3 : 2); i++) {
906 if (intel_fb->color_rb[i])
907 intel_fb->color_rb[i]->vbl_pending = driDrawPriv->vblSeq;
908 }
909 }
910 intel->driDrawable = driDrawPriv;
911 intelWindowMoved(intel);
912 }
913
914 intel_draw_buffer(&intel->ctx, &intel_fb->Base);
915 }
916 }
917 else {
918 _mesa_make_current(NULL, NULL, NULL);
919 }
920
921 return GL_TRUE;
922 }
923
924 static void
925 intelContendedLock(struct intel_context *intel, GLuint flags)
926 {
927 __DRIdrawablePrivate *dPriv = intel->driDrawable;
928 __DRIscreenPrivate *sPriv = intel->driScreen;
929 volatile struct drm_i915_sarea *sarea = intel->sarea;
930 int me = intel->hHWContext;
931
932 drmGetLock(intel->driFd, intel->hHWContext, flags);
933 intel->locked = 1;
934
935 if (INTEL_DEBUG & DEBUG_LOCK)
936 _mesa_printf("%s - got contended lock\n", __progname);
937
938 /* If the window moved, may need to set a new cliprect now.
939 *
940 * NOTE: This releases and regains the hw lock, so all state
941 * checking must be done *after* this call:
942 */
943 if (dPriv)
944 DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
945
946 if (sarea && sarea->ctxOwner != me) {
947 if (INTEL_DEBUG & DEBUG_BUFMGR) {
948 fprintf(stderr, "Lost Context: sarea->ctxOwner %x me %x\n",
949 sarea->ctxOwner, me);
950 }
951 sarea->ctxOwner = me;
952 }
953
954 /* If the last consumer of the texture memory wasn't us, notify the fake
955 * bufmgr and record the new owner. We should have the memory shared
956 * between contexts of a single fake bufmgr, but this will at least make
957 * things correct for now.
958 */
959 if (!intel->ttm && sarea->texAge != intel->hHWContext) {
960 sarea->texAge = intel->hHWContext;
961 intel_bufmgr_fake_contended_lock_take(intel->bufmgr);
962 if (INTEL_DEBUG & DEBUG_BATCH)
963 intel_decode_context_reset();
964 if (INTEL_DEBUG & DEBUG_BUFMGR)
965 fprintf(stderr, "Lost Textures: sarea->texAge %x hw context %x\n",
966 sarea->ctxOwner, intel->hHWContext);
967 }
968
969 if (sarea->width != intel->width || sarea->height != intel->height) {
970 int numClipRects = intel->numClipRects;
971
972 /*
973 * FIXME: Really only need to do this when drawing to a
974 * common back- or front buffer.
975 */
976
977 /*
978 * This will essentially drop the outstanding batchbuffer on
979 * the floor.
980 */
981 intel->numClipRects = 0;
982
983 if (intel->Fallback)
984 _swrast_flush(&intel->ctx);
985
986 if (!IS_965(intel->intelScreen->deviceID))
987 INTEL_FIREVERTICES(intel);
988
989 if (intel->batch->map != intel->batch->ptr)
990 intel_batchbuffer_flush(intel->batch);
991
992 intel->numClipRects = numClipRects;
993
994 /* force window update */
995 intel->lastStamp = 0;
996
997 intel->width = sarea->width;
998 intel->height = sarea->height;
999 }
1000
1001 /* Drawable changed?
1002 */
1003 if (dPriv && intel->lastStamp != dPriv->lastStamp) {
1004 intelWindowMoved(intel);
1005 intel->lastStamp = dPriv->lastStamp;
1006 }
1007 }
1008
1009
1010 _glthread_DECLARE_STATIC_MUTEX(lockMutex);
1011
1012 /* Lock the hardware and validate our state.
1013 */
1014 void LOCK_HARDWARE( struct intel_context *intel )
1015 {
1016 __DRIdrawable *dPriv = intel->driDrawable;
1017 __DRIscreen *sPriv = intel->driScreen;
1018 char __ret = 0;
1019 struct intel_framebuffer *intel_fb = NULL;
1020 struct intel_renderbuffer *intel_rb = NULL;
1021
1022 _glthread_LOCK_MUTEX(lockMutex);
1023 assert(!intel->locked);
1024 intel->locked = 1;
1025
1026 if (intel->driDrawable) {
1027 intel_fb = intel->driDrawable->driverPrivate;
1028
1029 if (intel_fb)
1030 intel_rb =
1031 intel_get_renderbuffer(&intel_fb->Base,
1032 intel_fb->Base._ColorDrawBufferIndexes[0]);
1033 }
1034
1035 if (intel_rb && dPriv->vblFlags &&
1036 !(dPriv->vblFlags & VBLANK_FLAG_NO_IRQ) &&
1037 (intel_fb->vbl_waited - intel_rb->vbl_pending) > (1<<23)) {
1038 drmVBlank vbl;
1039
1040 vbl.request.type = DRM_VBLANK_ABSOLUTE;
1041
1042 if ( dPriv->vblFlags & VBLANK_FLAG_SECONDARY ) {
1043 vbl.request.type |= DRM_VBLANK_SECONDARY;
1044 }
1045
1046 vbl.request.sequence = intel_rb->vbl_pending;
1047 drmWaitVBlank(intel->driFd, &vbl);
1048 intel_fb->vbl_waited = vbl.reply.sequence;
1049 }
1050
1051 if (!sPriv->dri2.enabled) {
1052 DRM_CAS(intel->driHwLock, intel->hHWContext,
1053 (DRM_LOCK_HELD|intel->hHWContext), __ret);
1054
1055 if (__ret)
1056 intelContendedLock( intel, 0 );
1057 }
1058
1059
1060 if (INTEL_DEBUG & DEBUG_LOCK)
1061 _mesa_printf("%s - locked\n", __progname);
1062 }
1063
1064
1065 /* Unlock the hardware using the global current context
1066 */
1067 void UNLOCK_HARDWARE( struct intel_context *intel )
1068 {
1069 __DRIscreen *sPriv = intel->driScreen;
1070
1071 intel->vtbl.note_unlock( intel );
1072 intel->locked = 0;
1073
1074 if (!sPriv->dri2.enabled)
1075 DRM_UNLOCK(intel->driFd, intel->driHwLock, intel->hHWContext);
1076
1077 _glthread_UNLOCK_MUTEX(lockMutex);
1078
1079 if (INTEL_DEBUG & DEBUG_LOCK)
1080 _mesa_printf("%s - unlocked\n", __progname);
1081
1082 /**
1083 * Nothing should be left in batch outside of LOCK/UNLOCK which references
1084 * cliprects.
1085 */
1086 if (intel->batch->cliprect_mode == REFERENCES_CLIPRECTS)
1087 intel_batchbuffer_flush(intel->batch);
1088 }
1089