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