nouveau_context.c revision c695e80017373caf6e8d7139b8d3df2d945ccce5
1/* 2 * Copyright (C) 2009-2010 Francisco Jerez. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial 15 * portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 * 25 */ 26 27#include "nouveau_driver.h" 28#include "nouveau_context.h" 29#include "nouveau_bufferobj.h" 30#include "nouveau_fbo.h" 31 32#include "main/dd.h" 33#include "main/framebuffer.h" 34#include "main/light.h" 35#include "main/state.h" 36#include "drivers/common/meta.h" 37#include "drivers/common/driverfuncs.h" 38#include "swrast/swrast.h" 39#include "swrast/s_context.h" 40#include "vbo/vbo.h" 41#include "tnl/tnl.h" 42#include "tnl/t_context.h" 43 44#define need_GL_EXT_framebuffer_object 45#define need_GL_EXT_fog_coord 46#define need_GL_EXT_secondary_color 47 48#include "main/remap_helper.h" 49 50static const struct dri_extension nouveau_extensions[] = { 51 { "GL_ARB_multitexture", NULL }, 52 { "GL_ARB_texture_env_add", NULL }, 53 { "GL_ARB_texture_env_combine", NULL }, 54 { "GL_ARB_texture_env_dot3", NULL }, 55 { "GL_ARB_texture_mirrored_repeat", NULL }, 56 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions }, 57 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions }, 58 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions }, 59 { "GL_EXT_stencil_wrap", NULL }, 60 { "GL_EXT_texture_lod_bias", NULL }, 61 { "GL_NV_blend_square", NULL }, 62 { "GL_SGIS_generate_mipmap", NULL }, 63 { NULL, NULL } 64}; 65 66static void 67nouveau_channel_flush_notify(struct nouveau_channel *chan) 68{ 69 struct nouveau_context *nctx = chan->user_private; 70 GLcontext *ctx = &nctx->base; 71 72 if (nctx->fallback < SWRAST) 73 nouveau_bo_state_emit(ctx); 74} 75 76GLboolean 77nouveau_context_create(const __GLcontextModes *visual, __DRIcontext *dri_ctx, 78 void *share_ctx) 79{ 80 __DRIscreen *dri_screen = dri_ctx->driScreenPriv; 81 struct nouveau_screen *screen = dri_screen->private; 82 struct nouveau_context *nctx; 83 GLcontext *ctx; 84 85 ctx = screen->driver->context_create(screen, visual, share_ctx); 86 if (!ctx) 87 return GL_FALSE; 88 89 nctx = to_nouveau_context(ctx); 90 nctx->dri_context = dri_ctx; 91 dri_ctx->driverPrivate = ctx; 92 93 return GL_TRUE; 94} 95 96GLboolean 97nouveau_context_init(GLcontext *ctx, struct nouveau_screen *screen, 98 const GLvisual *visual, GLcontext *share_ctx) 99{ 100 struct nouveau_context *nctx = to_nouveau_context(ctx); 101 struct dd_function_table functions; 102 int ret; 103 104 nctx->screen = screen; 105 nctx->fallback = HWTNL; 106 107 /* Initialize the function pointers. */ 108 _mesa_init_driver_functions(&functions); 109 nouveau_driver_functions_init(&functions); 110 nouveau_bufferobj_functions_init(&functions); 111 nouveau_texture_functions_init(&functions); 112 nouveau_fbo_functions_init(&functions); 113 114 /* Initialize the mesa context. */ 115 _mesa_initialize_context(ctx, visual, share_ctx, &functions, NULL); 116 117 nouveau_state_init(ctx); 118 nouveau_bo_state_init(ctx); 119 _mesa_meta_init(ctx); 120 _swrast_CreateContext(ctx); 121 _vbo_CreateContext(ctx); 122 _tnl_CreateContext(ctx); 123 nouveau_span_functions_init(ctx); 124 _mesa_allow_light_in_model(ctx, GL_FALSE); 125 126 /* Allocate a hardware channel. */ 127 ret = nouveau_channel_alloc(context_dev(ctx), 0xbeef0201, 0xbeef0202, 128 &nctx->hw.chan); 129 if (ret) { 130 nouveau_error("Error initializing the FIFO.\n"); 131 return GL_FALSE; 132 } 133 134 nctx->hw.chan->flush_notify = nouveau_channel_flush_notify; 135 nctx->hw.chan->user_private = nctx; 136 137 /* Enable any supported extensions. */ 138 driInitExtensions(ctx, nouveau_extensions, GL_TRUE); 139 140 return GL_TRUE; 141} 142 143void 144nouveau_context_deinit(GLcontext *ctx) 145{ 146 struct nouveau_context *nctx = to_nouveau_context(ctx); 147 148 if (TNL_CONTEXT(ctx)) 149 _tnl_DestroyContext(ctx); 150 151 if (vbo_context(ctx)) 152 _vbo_DestroyContext(ctx); 153 154 if (SWRAST_CONTEXT(ctx)) 155 _swrast_DestroyContext(ctx); 156 157 if (ctx->Meta) 158 _mesa_meta_free(ctx); 159 160 if (nctx->hw.chan) 161 nouveau_channel_free(&nctx->hw.chan); 162 163 nouveau_bo_state_destroy(ctx); 164 _mesa_free_context_data(ctx); 165} 166 167void 168nouveau_context_destroy(__DRIcontext *dri_ctx) 169{ 170 struct nouveau_context *nctx = dri_ctx->driverPrivate; 171 GLcontext *ctx = &nctx->base; 172 173 context_drv(ctx)->context_destroy(ctx); 174} 175 176void 177nouveau_update_renderbuffers(__DRIcontext *dri_ctx, __DRIdrawable *draw) 178{ 179 GLcontext *ctx = dri_ctx->driverPrivate; 180 __DRIscreen *screen = dri_ctx->driScreenPriv; 181 struct gl_framebuffer *fb = draw->driverPrivate; 182 unsigned int attachments[10]; 183 __DRIbuffer *buffers = NULL; 184 int i = 0, count, ret; 185 186 if (draw->lastStamp == *draw->pStamp) 187 return; 188 draw->lastStamp = *draw->pStamp; 189 190 attachments[i++] = __DRI_BUFFER_FRONT_LEFT; 191 if (fb->Visual.doubleBufferMode) 192 attachments[i++] = __DRI_BUFFER_BACK_LEFT; 193 if (fb->Visual.haveDepthBuffer && fb->Visual.haveStencilBuffer) 194 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL; 195 else if (fb->Visual.haveDepthBuffer) 196 attachments[i++] = __DRI_BUFFER_DEPTH; 197 else if (fb->Visual.haveStencilBuffer) 198 attachments[i++] = __DRI_BUFFER_STENCIL; 199 200 buffers = (*screen->dri2.loader->getBuffers)(draw, &draw->w, &draw->h, 201 attachments, i, &count, 202 draw->loaderPrivate); 203 if (buffers == NULL) 204 return; 205 206 for (i = 0; i < count; i++) { 207 struct gl_renderbuffer *rb; 208 struct nouveau_surface *s; 209 uint32_t old_handle; 210 int index; 211 212 switch (buffers[i].attachment) { 213 case __DRI_BUFFER_FRONT_LEFT: 214 case __DRI_BUFFER_FAKE_FRONT_LEFT: 215 index = BUFFER_FRONT_LEFT; 216 break; 217 case __DRI_BUFFER_BACK_LEFT: 218 index = BUFFER_BACK_LEFT; 219 break; 220 case __DRI_BUFFER_DEPTH: 221 case __DRI_BUFFER_DEPTH_STENCIL: 222 index = BUFFER_DEPTH; 223 break; 224 case __DRI_BUFFER_STENCIL: 225 index = BUFFER_STENCIL; 226 break; 227 default: 228 assert(0); 229 } 230 231 rb = fb->Attachment[index].Renderbuffer; 232 s = &to_nouveau_renderbuffer(rb)->surface; 233 234 s->width = draw->w; 235 s->height = draw->h; 236 s->pitch = buffers[i].pitch; 237 s->cpp = buffers[i].cpp; 238 239 /* Don't bother to reopen the bo if it happens to be 240 * the same. */ 241 if (s->bo) { 242 ret = nouveau_bo_handle_get(s->bo, &old_handle); 243 assert(!ret); 244 } 245 246 if (!s->bo || old_handle != buffers[i].name) { 247 nouveau_bo_ref(NULL, &s->bo); 248 ret = nouveau_bo_handle_ref(context_dev(ctx), 249 buffers[i].name, &s->bo); 250 assert(!ret); 251 } 252 } 253 254 _mesa_resize_framebuffer(NULL, fb, draw->w, draw->h); 255} 256 257static void 258update_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw, 259 int *stamp) 260{ 261 GLcontext *ctx = dri_ctx->driverPrivate; 262 struct gl_framebuffer *fb = draw->driverPrivate; 263 264 *stamp = *draw->pStamp; 265 266 nouveau_update_renderbuffers(dri_ctx, draw); 267 _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h); 268 269 context_dirty(ctx, FRAMEBUFFER); 270} 271 272GLboolean 273nouveau_context_make_current(__DRIcontext *dri_ctx, __DRIdrawable *dri_draw, 274 __DRIdrawable *dri_read) 275{ 276 if (dri_ctx) { 277 struct nouveau_context *nctx = dri_ctx->driverPrivate; 278 GLcontext *ctx = &nctx->base; 279 280 /* Ask the X server for new renderbuffers. */ 281 if (dri_draw->driverPrivate != ctx->WinSysDrawBuffer) 282 update_framebuffer(dri_ctx, dri_draw, 283 &dri_ctx->dri2.draw_stamp); 284 285 if (dri_draw != dri_read && 286 dri_read->driverPrivate != ctx->WinSysReadBuffer) 287 update_framebuffer(dri_ctx, dri_read, 288 &dri_ctx->dri2.read_stamp); 289 290 /* Pass it down to mesa. */ 291 _mesa_make_current(ctx, dri_draw->driverPrivate, 292 dri_read->driverPrivate); 293 _mesa_update_state(ctx); 294 295 FIRE_RING(context_chan(ctx)); 296 297 } else { 298 _mesa_make_current(NULL, NULL, NULL); 299 } 300 301 return GL_TRUE; 302} 303 304GLboolean 305nouveau_context_unbind(__DRIcontext *dri_ctx) 306{ 307 return GL_TRUE; 308} 309 310void 311nouveau_fallback(GLcontext *ctx, enum nouveau_fallback mode) 312{ 313 struct nouveau_context *nctx = to_nouveau_context(ctx); 314 315 nctx->fallback = MAX2(HWTNL, mode); 316 317 if (mode < SWRAST) 318 nouveau_state_emit(ctx); 319 else 320 FIRE_RING(context_chan(ctx)); 321} 322 323void 324nouveau_validate_framebuffer(GLcontext *ctx) 325{ 326 __DRIcontext *dri_ctx = to_nouveau_context(ctx)->dri_context; 327 __DRIdrawable *dri_draw = dri_ctx->driDrawablePriv; 328 __DRIdrawable *dri_read = dri_ctx->driReadablePriv; 329 330 if (ctx->DrawBuffer->Name == 0 && 331 dri_ctx->dri2.draw_stamp != *dri_draw->pStamp) 332 update_framebuffer(dri_ctx, dri_draw, 333 &dri_ctx->dri2.draw_stamp); 334 335 if (ctx->ReadBuffer->Name == 0 && dri_draw != dri_read && 336 dri_ctx->dri2.read_stamp != *dri_read->pStamp) 337 update_framebuffer(dri_ctx, dri_read, 338 &dri_ctx->dri2.read_stamp); 339 340 if (nouveau_next_dirty_state(ctx) >= 0) { 341 nouveau_state_emit(ctx); 342 FIRE_RING(context_chan(ctx)); 343 } 344} 345