2 * Copyright (C) 2009-2010 Francisco Jerez.
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:
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.
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.
27 #include "nouveau_driver.h"
28 #include "nouveau_context.h"
29 #include "nouveau_bufferobj.h"
30 #include "nouveau_fbo.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"
42 #include "tnl/t_context.h"
44 #define need_GL_EXT_framebuffer_object
45 #define need_GL_EXT_fog_coord
47 #include "main/remap_helper.h"
49 static const struct dri_extension nouveau_extensions
[] = {
50 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions
},
51 { "GL_ARB_multitexture", NULL
},
52 { "GL_EXT_texture_lod_bias", NULL
},
53 { "GL_SGIS_generate_mipmap", NULL
},
54 { "GL_ARB_texture_env_combine", NULL
},
55 { "GL_ARB_texture_env_dot3", NULL
},
56 { "GL_ARB_texture_env_add", NULL
},
57 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions
},
62 nouveau_channel_flush_notify(struct nouveau_channel
*chan
)
64 struct nouveau_context
*nctx
= chan
->user_private
;
65 GLcontext
*ctx
= &nctx
->base
;
67 if (nctx
->fallback
< SWRAST
&& ctx
->DrawBuffer
)
68 nouveau_state_emit(&nctx
->base
);
72 nouveau_context_create(const __GLcontextModes
*visual
, __DRIcontext
*dri_ctx
,
75 __DRIscreen
*dri_screen
= dri_ctx
->driScreenPriv
;
76 struct nouveau_screen
*screen
= dri_screen
->private;
77 struct nouveau_context
*nctx
;
80 ctx
= screen
->driver
->context_create(screen
, visual
, share_ctx
);
84 nctx
= to_nouveau_context(ctx
);
85 nctx
->dri_context
= dri_ctx
;
86 dri_ctx
->driverPrivate
= ctx
;
92 nouveau_context_init(GLcontext
*ctx
, struct nouveau_screen
*screen
,
93 const GLvisual
*visual
, GLcontext
*share_ctx
)
95 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
96 struct dd_function_table functions
;
99 nctx
->screen
= screen
;
100 nctx
->fallback
= HWTNL
;
102 /* Initialize the function pointers. */
103 _mesa_init_driver_functions(&functions
);
104 nouveau_driver_functions_init(&functions
);
105 nouveau_bufferobj_functions_init(&functions
);
106 nouveau_texture_functions_init(&functions
);
107 nouveau_fbo_functions_init(&functions
);
109 /* Initialize the mesa context. */
110 _mesa_initialize_context(ctx
, visual
, share_ctx
, &functions
, NULL
);
112 nouveau_state_init(ctx
);
113 nouveau_bo_state_init(ctx
);
114 _mesa_meta_init(ctx
);
115 _swrast_CreateContext(ctx
);
116 _vbo_CreateContext(ctx
);
117 _tnl_CreateContext(ctx
);
118 nouveau_span_functions_init(ctx
);
119 _mesa_allow_light_in_model(ctx
, GL_FALSE
);
121 /* Allocate a hardware channel. */
122 ret
= nouveau_channel_alloc(context_dev(ctx
), 0xbeef0201, 0xbeef0202,
125 nouveau_error("Error initializing the FIFO.\n");
129 nctx
->hw
.chan
->flush_notify
= nouveau_channel_flush_notify
;
130 nctx
->hw
.chan
->user_private
= nctx
;
132 /* Enable any supported extensions. */
133 driInitExtensions(ctx
, nouveau_extensions
, GL_TRUE
);
139 nouveau_context_deinit(GLcontext
*ctx
)
141 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
143 if (TNL_CONTEXT(ctx
))
144 _tnl_DestroyContext(ctx
);
146 if (vbo_context(ctx
))
147 _vbo_DestroyContext(ctx
);
149 if (SWRAST_CONTEXT(ctx
))
150 _swrast_DestroyContext(ctx
);
153 _mesa_meta_free(ctx
);
156 nouveau_channel_free(&nctx
->hw
.chan
);
158 nouveau_bo_state_destroy(ctx
);
159 _mesa_free_context_data(ctx
);
163 nouveau_context_destroy(__DRIcontext
*dri_ctx
)
165 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
166 GLcontext
*ctx
= &nctx
->base
;
168 context_drv(ctx
)->context_destroy(ctx
);
172 nouveau_update_renderbuffers(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
)
174 GLcontext
*ctx
= dri_ctx
->driverPrivate
;
175 __DRIscreen
*screen
= dri_ctx
->driScreenPriv
;
176 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
177 unsigned int attachments
[10];
178 __DRIbuffer
*buffers
= NULL
;
179 int i
= 0, count
, ret
;
181 if (draw
->lastStamp
== *draw
->pStamp
)
183 draw
->lastStamp
= *draw
->pStamp
;
185 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
186 if (fb
->Visual
.doubleBufferMode
)
187 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
188 if (fb
->Visual
.haveDepthBuffer
&& fb
->Visual
.haveStencilBuffer
)
189 attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
190 else if (fb
->Visual
.haveDepthBuffer
)
191 attachments
[i
++] = __DRI_BUFFER_DEPTH
;
192 else if (fb
->Visual
.haveStencilBuffer
)
193 attachments
[i
++] = __DRI_BUFFER_STENCIL
;
195 buffers
= (*screen
->dri2
.loader
->getBuffers
)(draw
, &draw
->w
, &draw
->h
,
196 attachments
, i
, &count
,
197 draw
->loaderPrivate
);
201 for (i
= 0; i
< count
; i
++) {
202 struct gl_renderbuffer
*rb
;
203 struct nouveau_surface
*s
;
207 switch (buffers
[i
].attachment
) {
208 case __DRI_BUFFER_FRONT_LEFT
:
209 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
210 index
= BUFFER_FRONT_LEFT
;
212 case __DRI_BUFFER_BACK_LEFT
:
213 index
= BUFFER_BACK_LEFT
;
215 case __DRI_BUFFER_DEPTH
:
216 case __DRI_BUFFER_DEPTH_STENCIL
:
217 index
= BUFFER_DEPTH
;
219 case __DRI_BUFFER_STENCIL
:
220 index
= BUFFER_STENCIL
;
226 rb
= fb
->Attachment
[index
].Renderbuffer
;
227 s
= &to_nouveau_renderbuffer(rb
)->surface
;
231 s
->pitch
= buffers
[i
].pitch
;
232 s
->cpp
= buffers
[i
].cpp
;
234 /* Don't bother to reopen the bo if it happens to be
237 ret
= nouveau_bo_handle_get(s
->bo
, &old_handle
);
241 if (!s
->bo
|| old_handle
!= buffers
[i
].name
) {
242 nouveau_bo_ref(NULL
, &s
->bo
);
243 ret
= nouveau_bo_handle_ref(context_dev(ctx
),
244 buffers
[i
].name
, &s
->bo
);
249 _mesa_resize_framebuffer(NULL
, fb
, draw
->w
, draw
->h
);
253 update_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
256 GLcontext
*ctx
= dri_ctx
->driverPrivate
;
257 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
259 *stamp
= *draw
->pStamp
;
261 nouveau_update_renderbuffers(dri_ctx
, draw
);
262 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
264 context_dirty(ctx
, FRAMEBUFFER
);
268 nouveau_context_make_current(__DRIcontext
*dri_ctx
, __DRIdrawable
*dri_draw
,
269 __DRIdrawable
*dri_read
)
272 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
273 GLcontext
*ctx
= &nctx
->base
;
275 /* Ask the X server for new renderbuffers. */
276 if (dri_draw
->driverPrivate
!= ctx
->WinSysDrawBuffer
)
277 update_framebuffer(dri_ctx
, dri_draw
,
278 &dri_ctx
->dri2
.draw_stamp
);
280 if (dri_draw
!= dri_read
&&
281 dri_read
->driverPrivate
!= ctx
->WinSysReadBuffer
)
282 update_framebuffer(dri_ctx
, dri_read
,
283 &dri_ctx
->dri2
.read_stamp
);
285 /* Pass it down to mesa. */
286 _mesa_make_current(ctx
, dri_draw
->driverPrivate
,
287 dri_read
->driverPrivate
);
288 _mesa_update_state(ctx
);
290 FIRE_RING(context_chan(ctx
));
293 _mesa_make_current(NULL
, NULL
, NULL
);
300 nouveau_context_unbind(__DRIcontext
*dri_ctx
)
306 nouveau_fallback(GLcontext
*ctx
, enum nouveau_fallback mode
)
308 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
310 nctx
->fallback
= MAX2(HWTNL
, mode
);
313 nouveau_state_emit(ctx
);
315 FIRE_RING(context_chan(ctx
));
319 nouveau_validate_framebuffer(GLcontext
*ctx
)
321 __DRIcontext
*dri_ctx
= to_nouveau_context(ctx
)->dri_context
;
322 __DRIdrawable
*dri_draw
= dri_ctx
->driDrawablePriv
;
323 __DRIdrawable
*dri_read
= dri_ctx
->driReadablePriv
;
325 if (ctx
->DrawBuffer
->Name
== 0 &&
326 dri_ctx
->dri2
.draw_stamp
!= *dri_draw
->pStamp
)
327 update_framebuffer(dri_ctx
, dri_draw
,
328 &dri_ctx
->dri2
.draw_stamp
);
330 if (ctx
->ReadBuffer
->Name
== 0 && dri_draw
!= dri_read
&&
331 dri_ctx
->dri2
.read_stamp
!= *dri_read
->pStamp
)
332 update_framebuffer(dri_ctx
, dri_read
,
333 &dri_ctx
->dri2
.read_stamp
);
335 if (nouveau_next_dirty_state(ctx
) >= 0)
336 FIRE_RING(context_chan(ctx
));