nouveau_context.c revision 9c1b41879aab2ff7386c547a2ccce7686c018cf5
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 <stdbool.h> 28#include "nouveau_driver.h" 29#include "nouveau_context.h" 30#include "nouveau_bufferobj.h" 31#include "nouveau_fbo.h" 32#include "nv_object.xml.h" 33 34#include "main/dd.h" 35#include "main/framebuffer.h" 36#include "main/fbobject.h" 37#include "main/light.h" 38#include "main/state.h" 39#include "main/version.h" 40#include "drivers/common/meta.h" 41#include "drivers/common/driverfuncs.h" 42#include "swrast/swrast.h" 43#include "swrast/s_context.h" 44#include "vbo/vbo.h" 45#include "tnl/tnl.h" 46#include "tnl/t_context.h" 47 48GLboolean 49nouveau_context_create(gl_api api, 50 const struct gl_config *visual, __DRIcontext *dri_ctx, 51 unsigned major_version, 52 unsigned minor_version, 53 uint32_t flags, 54 unsigned *error, 55 void *share_ctx) 56{ 57 __DRIscreen *dri_screen = dri_ctx->driScreenPriv; 58 struct nouveau_screen *screen = dri_screen->driverPrivate; 59 struct nouveau_context *nctx; 60 struct gl_context *ctx; 61 62 /* API and flag filtering is handled in dri2CreateContextAttribs. 63 */ 64 (void) api; 65 (void) flags; 66 67 ctx = screen->driver->context_create(screen, visual, share_ctx); 68 if (!ctx) { 69 *error = __DRI_CTX_ERROR_NO_MEMORY; 70 return GL_FALSE; 71 } 72 73 nctx = to_nouveau_context(ctx); 74 nctx->dri_context = dri_ctx; 75 dri_ctx->driverPrivate = ctx; 76 77 _mesa_compute_version(ctx); 78 if (ctx->Version < major_version * 10 + minor_version) { 79 nouveau_context_destroy(dri_ctx); 80 *error = __DRI_CTX_ERROR_BAD_VERSION; 81 return GL_FALSE; 82 } 83 84 if (nouveau_bo_new(context_dev(ctx), NOUVEAU_BO_VRAM, 0, 4096, 85 NULL, &nctx->fence)) { 86 nouveau_context_destroy(dri_ctx); 87 *error = __DRI_CTX_ERROR_NO_MEMORY; 88 return GL_FALSE; 89 } 90 91 *error = __DRI_CTX_ERROR_SUCCESS; 92 return GL_TRUE; 93} 94 95GLboolean 96nouveau_context_init(struct gl_context *ctx, struct nouveau_screen *screen, 97 const struct gl_config *visual, struct gl_context *share_ctx) 98{ 99 struct nouveau_context *nctx = to_nouveau_context(ctx); 100 struct dd_function_table functions; 101 int ret; 102 103 nctx->screen = screen; 104 nctx->fallback = HWTNL; 105 106 /* Initialize the function pointers. */ 107 _mesa_init_driver_functions(&functions); 108 nouveau_driver_functions_init(&functions); 109 nouveau_bufferobj_functions_init(&functions); 110 nouveau_texture_functions_init(&functions); 111 nouveau_fbo_functions_init(&functions); 112 113 /* Initialize the mesa context. */ 114 _mesa_initialize_context(ctx, API_OPENGL, visual, 115 share_ctx, &functions, NULL); 116 117 nouveau_state_init(ctx); 118 nouveau_scratch_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_object_new(&context_dev(ctx)->object, 0xbeef0000, 128 NOUVEAU_FIFO_CHANNEL_CLASS, 129 &(struct nv04_fifo){ 130 .vram = 0xbeef0201, 131 .gart = 0xbeef0202 132 }, sizeof(struct nv04_fifo), &nctx->hw.chan); 133 if (ret) { 134 nouveau_error("Error initializing the FIFO.\n"); 135 return GL_FALSE; 136 } 137 138 /* Allocate a client (thread data) */ 139 ret = nouveau_client_new(context_dev(ctx), &nctx->hw.client); 140 if (ret) { 141 nouveau_error("Error creating thread data\n"); 142 return GL_FALSE; 143 } 144 145 /* Allocate a push buffer */ 146 ret = nouveau_pushbuf_new(nctx->hw.client, nctx->hw.chan, 4, 147 512 * 1024, true, &nctx->hw.pushbuf); 148 if (ret) { 149 nouveau_error("Error allocating DMA push buffer\n"); 150 return GL_FALSE; 151 } 152 153 /* Allocate buffer context */ 154 ret = nouveau_bufctx_new(nctx->hw.client, 16, &nctx->hw.bufctx); 155 if (ret) { 156 nouveau_error("Error allocating buffer context\n"); 157 return GL_FALSE; 158 } 159 160 nctx->hw.pushbuf->user_priv = nctx->hw.bufctx; 161 162 /* Allocate NULL object */ 163 ret = nouveau_object_new(nctx->hw.chan, 0x00000000, NV01_NULL_CLASS, 164 NULL, 0, &nctx->hw.null); 165 if (ret) { 166 nouveau_error("Error allocating NULL object\n"); 167 return GL_FALSE; 168 } 169 170 /* Enable any supported extensions. */ 171 ctx->Extensions.EXT_blend_color = true; 172 ctx->Extensions.EXT_blend_minmax = true; 173 ctx->Extensions.EXT_fog_coord = true; 174 ctx->Extensions.EXT_framebuffer_blit = true; 175 ctx->Extensions.EXT_framebuffer_object = true; 176 ctx->Extensions.EXT_packed_depth_stencil = true; 177 ctx->Extensions.EXT_secondary_color = true; 178 ctx->Extensions.EXT_texture_filter_anisotropic = true; 179 ctx->Extensions.NV_blend_square = true; 180 ctx->Extensions.NV_texture_env_combine4 = true; 181 182 return GL_TRUE; 183} 184 185void 186nouveau_context_deinit(struct gl_context *ctx) 187{ 188 struct nouveau_context *nctx = to_nouveau_context(ctx); 189 190 if (TNL_CONTEXT(ctx)) 191 _tnl_DestroyContext(ctx); 192 193 if (vbo_context(ctx)) 194 _vbo_DestroyContext(ctx); 195 196 if (SWRAST_CONTEXT(ctx)) 197 _swrast_DestroyContext(ctx); 198 199 if (ctx->Meta) 200 _mesa_meta_free(ctx); 201 202 nouveau_bufctx_del(&nctx->hw.bufctx); 203 nouveau_pushbuf_del(&nctx->hw.pushbuf); 204 nouveau_client_del(&nctx->hw.client); 205 nouveau_object_del(&nctx->hw.chan); 206 207 nouveau_scratch_destroy(ctx); 208 _mesa_free_context_data(ctx); 209} 210 211void 212nouveau_context_destroy(__DRIcontext *dri_ctx) 213{ 214 struct nouveau_context *nctx = dri_ctx->driverPrivate; 215 struct gl_context *ctx = &nctx->base; 216 217 nouveau_bo_ref(NULL, &nctx->fence); 218 context_drv(ctx)->context_destroy(ctx); 219} 220 221void 222nouveau_update_renderbuffers(__DRIcontext *dri_ctx, __DRIdrawable *draw) 223{ 224 struct gl_context *ctx = dri_ctx->driverPrivate; 225 struct nouveau_context *nctx = to_nouveau_context(ctx); 226 __DRIscreen *screen = dri_ctx->driScreenPriv; 227 struct gl_framebuffer *fb = draw->driverPrivate; 228 struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb); 229 unsigned int attachments[10]; 230 __DRIbuffer *buffers = NULL; 231 int i = 0, count, ret; 232 233 if (draw->lastStamp == draw->dri2.stamp) 234 return; 235 draw->lastStamp = draw->dri2.stamp; 236 237 if (nfb->need_front) 238 attachments[i++] = __DRI_BUFFER_FRONT_LEFT; 239 if (fb->Visual.doubleBufferMode) 240 attachments[i++] = __DRI_BUFFER_BACK_LEFT; 241 if (fb->Visual.haveDepthBuffer && fb->Visual.haveStencilBuffer) 242 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL; 243 else if (fb->Visual.haveDepthBuffer) 244 attachments[i++] = __DRI_BUFFER_DEPTH; 245 else if (fb->Visual.haveStencilBuffer) 246 attachments[i++] = __DRI_BUFFER_STENCIL; 247 248 buffers = (*screen->dri2.loader->getBuffers)(draw, &draw->w, &draw->h, 249 attachments, i, &count, 250 draw->loaderPrivate); 251 if (buffers == NULL) 252 return; 253 254 for (i = 0; i < count; i++) { 255 struct gl_renderbuffer *rb; 256 struct nouveau_surface *s; 257 uint32_t old_name; 258 int index; 259 260 switch (buffers[i].attachment) { 261 case __DRI_BUFFER_FRONT_LEFT: 262 case __DRI_BUFFER_FAKE_FRONT_LEFT: 263 index = BUFFER_FRONT_LEFT; 264 break; 265 case __DRI_BUFFER_BACK_LEFT: 266 index = BUFFER_BACK_LEFT; 267 break; 268 case __DRI_BUFFER_DEPTH: 269 case __DRI_BUFFER_DEPTH_STENCIL: 270 index = BUFFER_DEPTH; 271 break; 272 case __DRI_BUFFER_STENCIL: 273 index = BUFFER_STENCIL; 274 break; 275 default: 276 assert(0); 277 } 278 279 rb = fb->Attachment[index].Renderbuffer; 280 s = &to_nouveau_renderbuffer(rb)->surface; 281 282 s->width = draw->w; 283 s->height = draw->h; 284 s->pitch = buffers[i].pitch; 285 s->cpp = buffers[i].cpp; 286 287 if (index == BUFFER_DEPTH && s->bo) { 288 ret = nouveau_bo_name_get(s->bo, &old_name); 289 /* 290 * Disable fast Z clears in the next frame, the 291 * depth buffer contents are undefined. 292 */ 293 if (!ret && old_name != buffers[i].name) 294 nctx->hierz.clear_seq = 0; 295 } 296 297 nouveau_bo_ref(NULL, &s->bo); 298 ret = nouveau_bo_name_ref(context_dev(ctx), 299 buffers[i].name, &s->bo); 300 assert(!ret); 301 } 302 303 _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h); 304} 305 306static void 307update_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw, 308 int *stamp) 309{ 310 struct gl_context *ctx = dri_ctx->driverPrivate; 311 struct gl_framebuffer *fb = draw->driverPrivate; 312 313 *stamp = draw->dri2.stamp; 314 315 nouveau_update_renderbuffers(dri_ctx, draw); 316 _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h); 317 318 /* Clean up references to the old framebuffer objects. */ 319 context_dirty(ctx, FRAMEBUFFER); 320 nouveau_bufctx_reset(to_nouveau_context(ctx)->hw.bufctx, BUFCTX_FB); 321 PUSH_KICK(context_push(ctx)); 322} 323 324GLboolean 325nouveau_context_make_current(__DRIcontext *dri_ctx, __DRIdrawable *dri_draw, 326 __DRIdrawable *dri_read) 327{ 328 if (dri_ctx) { 329 struct nouveau_context *nctx = dri_ctx->driverPrivate; 330 struct gl_context *ctx = &nctx->base; 331 332 /* Ask the X server for new renderbuffers. */ 333 if (dri_draw->driverPrivate != ctx->WinSysDrawBuffer) 334 update_framebuffer(dri_ctx, dri_draw, 335 &dri_ctx->dri2.draw_stamp); 336 337 if (dri_draw != dri_read && 338 dri_read->driverPrivate != ctx->WinSysReadBuffer) 339 update_framebuffer(dri_ctx, dri_read, 340 &dri_ctx->dri2.read_stamp); 341 342 /* Pass it down to mesa. */ 343 _mesa_make_current(ctx, dri_draw->driverPrivate, 344 dri_read->driverPrivate); 345 _mesa_update_state(ctx); 346 347 } else { 348 _mesa_make_current(NULL, NULL, NULL); 349 } 350 351 return GL_TRUE; 352} 353 354GLboolean 355nouveau_context_unbind(__DRIcontext *dri_ctx) 356{ 357 /* Unset current context and dispatch table */ 358 _mesa_make_current(NULL, NULL, NULL); 359 360 return GL_TRUE; 361} 362 363void 364nouveau_fallback(struct gl_context *ctx, enum nouveau_fallback mode) 365{ 366 struct nouveau_context *nctx = to_nouveau_context(ctx); 367 368 nctx->fallback = MAX2(HWTNL, mode); 369 370 if (mode < SWRAST) { 371 nouveau_state_emit(ctx); 372#if 0 373 nouveau_bo_state_emit(ctx); 374#endif 375 } else { 376 PUSH_KICK(context_push(ctx)); 377 } 378} 379 380static void 381validate_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw, 382 int *stamp) 383{ 384 struct gl_framebuffer *fb = draw->driverPrivate; 385 struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb); 386 GLboolean need_front = 387 (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT || 388 fb->_ColorReadBufferIndex == BUFFER_FRONT_LEFT); 389 390 if (nfb->need_front != need_front) { 391 nfb->need_front = need_front; 392 dri2InvalidateDrawable(draw); 393 } 394 395 if (draw->dri2.stamp != *stamp) 396 update_framebuffer(dri_ctx, draw, stamp); 397} 398 399void 400nouveau_validate_framebuffer(struct gl_context *ctx) 401{ 402 __DRIcontext *dri_ctx = to_nouveau_context(ctx)->dri_context; 403 __DRIdrawable *dri_draw = dri_ctx->driDrawablePriv; 404 __DRIdrawable *dri_read = dri_ctx->driReadablePriv; 405 406 if (_mesa_is_winsys_fbo(ctx->DrawBuffer)) 407 validate_framebuffer(dri_ctx, dri_draw, 408 &dri_ctx->dri2.draw_stamp); 409 410 if (_mesa_is_winsys_fbo(ctx->ReadBuffer)) 411 validate_framebuffer(dri_ctx, dri_read, 412 &dri_ctx->dri2.read_stamp); 413 414 if (ctx->NewState & _NEW_BUFFERS) 415 _mesa_update_state(ctx); 416} 417