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
46 #define need_GL_EXT_secondary_color
48 #include "main/remap_helper.h"
50 static const struct dri_extension nouveau_extensions
[] = {
51 { "GL_ARB_multitexture", NULL
},
52 { "GL_ARB_texture_env_add", NULL
},
53 { "GL_ARB_texture_mirrored_repeat", NULL
},
54 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions
},
55 { "GL_EXT_framebuffer_blit", NULL
},
56 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions
},
57 { "GL_EXT_packed_depth_stencil", NULL
},
58 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions
},
59 { "GL_EXT_stencil_wrap", NULL
},
60 { "GL_EXT_texture_env_combine", NULL
},
61 { "GL_EXT_texture_filter_anisotropic", NULL
},
62 { "GL_EXT_texture_lod_bias", NULL
},
63 { "GL_NV_blend_square", NULL
},
64 { "GL_NV_texture_env_combine4", NULL
},
69 nouveau_channel_flush_notify(struct nouveau_channel
*chan
)
71 struct nouveau_context
*nctx
= chan
->user_private
;
72 struct gl_context
*ctx
= &nctx
->base
;
74 if (nctx
->fallback
< SWRAST
)
75 nouveau_bo_state_emit(ctx
);
79 nouveau_context_create(gl_api api
,
80 const struct gl_config
*visual
, __DRIcontext
*dri_ctx
,
83 __DRIscreen
*dri_screen
= dri_ctx
->driScreenPriv
;
84 struct nouveau_screen
*screen
= dri_screen
->private;
85 struct nouveau_context
*nctx
;
86 struct gl_context
*ctx
;
88 ctx
= screen
->driver
->context_create(screen
, visual
, share_ctx
);
92 nctx
= to_nouveau_context(ctx
);
93 nctx
->dri_context
= dri_ctx
;
94 dri_ctx
->driverPrivate
= ctx
;
100 nouveau_context_init(struct gl_context
*ctx
, struct nouveau_screen
*screen
,
101 const struct gl_config
*visual
, struct gl_context
*share_ctx
)
103 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
104 struct dd_function_table functions
;
107 nctx
->screen
= screen
;
108 nctx
->fallback
= HWTNL
;
110 /* Initialize the function pointers. */
111 _mesa_init_driver_functions(&functions
);
112 nouveau_driver_functions_init(&functions
);
113 nouveau_bufferobj_functions_init(&functions
);
114 nouveau_texture_functions_init(&functions
);
115 nouveau_fbo_functions_init(&functions
);
117 /* Initialize the mesa context. */
118 _mesa_initialize_context(ctx
, visual
, share_ctx
, &functions
, NULL
);
120 nouveau_state_init(ctx
);
121 nouveau_bo_state_init(ctx
);
122 _mesa_meta_init(ctx
);
123 _swrast_CreateContext(ctx
);
124 _vbo_CreateContext(ctx
);
125 _tnl_CreateContext(ctx
);
126 nouveau_span_functions_init(ctx
);
127 _mesa_allow_light_in_model(ctx
, GL_FALSE
);
129 /* Allocate a hardware channel. */
130 ret
= nouveau_channel_alloc(context_dev(ctx
), 0xbeef0201, 0xbeef0202,
131 512*1024, &nctx
->hw
.chan
);
133 nouveau_error("Error initializing the FIFO.\n");
137 nctx
->hw
.chan
->flush_notify
= nouveau_channel_flush_notify
;
138 nctx
->hw
.chan
->user_private
= nctx
;
140 /* Enable any supported extensions. */
141 driInitExtensions(ctx
, nouveau_extensions
, GL_TRUE
);
147 nouveau_context_deinit(struct gl_context
*ctx
)
149 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
151 if (TNL_CONTEXT(ctx
))
152 _tnl_DestroyContext(ctx
);
154 if (vbo_context(ctx
))
155 _vbo_DestroyContext(ctx
);
157 if (SWRAST_CONTEXT(ctx
))
158 _swrast_DestroyContext(ctx
);
161 _mesa_meta_free(ctx
);
164 nouveau_channel_free(&nctx
->hw
.chan
);
166 nouveau_bo_state_destroy(ctx
);
167 _mesa_free_context_data(ctx
);
171 nouveau_context_destroy(__DRIcontext
*dri_ctx
)
173 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
174 struct gl_context
*ctx
= &nctx
->base
;
176 context_drv(ctx
)->context_destroy(ctx
);
180 nouveau_update_renderbuffers(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
)
182 struct gl_context
*ctx
= dri_ctx
->driverPrivate
;
183 __DRIscreen
*screen
= dri_ctx
->driScreenPriv
;
184 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
185 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
186 unsigned int attachments
[10];
187 __DRIbuffer
*buffers
= NULL
;
188 int i
= 0, count
, ret
;
190 if (draw
->lastStamp
== *draw
->pStamp
)
192 draw
->lastStamp
= *draw
->pStamp
;
195 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
196 if (fb
->Visual
.doubleBufferMode
)
197 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
198 if (fb
->Visual
.haveDepthBuffer
&& fb
->Visual
.haveStencilBuffer
)
199 attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
200 else if (fb
->Visual
.haveDepthBuffer
)
201 attachments
[i
++] = __DRI_BUFFER_DEPTH
;
202 else if (fb
->Visual
.haveStencilBuffer
)
203 attachments
[i
++] = __DRI_BUFFER_STENCIL
;
205 buffers
= (*screen
->dri2
.loader
->getBuffers
)(draw
, &draw
->w
, &draw
->h
,
206 attachments
, i
, &count
,
207 draw
->loaderPrivate
);
211 for (i
= 0; i
< count
; i
++) {
212 struct gl_renderbuffer
*rb
;
213 struct nouveau_surface
*s
;
216 switch (buffers
[i
].attachment
) {
217 case __DRI_BUFFER_FRONT_LEFT
:
218 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
219 index
= BUFFER_FRONT_LEFT
;
221 case __DRI_BUFFER_BACK_LEFT
:
222 index
= BUFFER_BACK_LEFT
;
224 case __DRI_BUFFER_DEPTH
:
225 case __DRI_BUFFER_DEPTH_STENCIL
:
226 index
= BUFFER_DEPTH
;
228 case __DRI_BUFFER_STENCIL
:
229 index
= BUFFER_STENCIL
;
235 rb
= fb
->Attachment
[index
].Renderbuffer
;
236 s
= &to_nouveau_renderbuffer(rb
)->surface
;
240 s
->pitch
= buffers
[i
].pitch
;
241 s
->cpp
= buffers
[i
].cpp
;
243 nouveau_bo_ref(NULL
, &s
->bo
);
244 ret
= nouveau_bo_handle_ref(context_dev(ctx
),
245 buffers
[i
].name
, &s
->bo
);
249 _mesa_resize_framebuffer(NULL
, fb
, draw
->w
, draw
->h
);
253 update_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
256 struct gl_context
*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 /* Clean up references to the old framebuffer objects. */
265 context_dirty(ctx
, FRAMEBUFFER
);
266 context_bctx(ctx
, FRAMEBUFFER
);
267 FIRE_RING(context_chan(ctx
));
271 nouveau_context_make_current(__DRIcontext
*dri_ctx
, __DRIdrawable
*dri_draw
,
272 __DRIdrawable
*dri_read
)
275 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
276 struct gl_context
*ctx
= &nctx
->base
;
278 /* Ask the X server for new renderbuffers. */
279 if (dri_draw
->driverPrivate
!= ctx
->WinSysDrawBuffer
)
280 update_framebuffer(dri_ctx
, dri_draw
,
281 &dri_ctx
->dri2
.draw_stamp
);
283 if (dri_draw
!= dri_read
&&
284 dri_read
->driverPrivate
!= ctx
->WinSysReadBuffer
)
285 update_framebuffer(dri_ctx
, dri_read
,
286 &dri_ctx
->dri2
.read_stamp
);
288 /* Pass it down to mesa. */
289 _mesa_make_current(ctx
, dri_draw
->driverPrivate
,
290 dri_read
->driverPrivate
);
291 _mesa_update_state(ctx
);
294 _mesa_make_current(NULL
, NULL
, NULL
);
301 nouveau_context_unbind(__DRIcontext
*dri_ctx
)
303 /* Unset current context and dispath table */
304 _mesa_make_current(NULL
, NULL
, NULL
);
310 nouveau_fallback(struct gl_context
*ctx
, enum nouveau_fallback mode
)
312 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
314 nctx
->fallback
= MAX2(HWTNL
, mode
);
317 nouveau_state_emit(ctx
);
319 FIRE_RING(context_chan(ctx
));
323 validate_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
326 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
327 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
328 GLboolean need_front
=
329 (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
||
330 fb
->_ColorReadBufferIndex
== BUFFER_FRONT_LEFT
);
332 if (nfb
->need_front
!= need_front
) {
333 nfb
->need_front
= need_front
;
334 dri2InvalidateDrawable(draw
);
337 if (*draw
->pStamp
!= *stamp
)
338 update_framebuffer(dri_ctx
, draw
, stamp
);
342 nouveau_validate_framebuffer(struct gl_context
*ctx
)
344 __DRIcontext
*dri_ctx
= to_nouveau_context(ctx
)->dri_context
;
345 __DRIdrawable
*dri_draw
= dri_ctx
->driDrawablePriv
;
346 __DRIdrawable
*dri_read
= dri_ctx
->driReadablePriv
;
348 if (ctx
->DrawBuffer
->Name
== 0)
349 validate_framebuffer(dri_ctx
, dri_draw
,
350 &dri_ctx
->dri2
.draw_stamp
);
352 if (ctx
->ReadBuffer
->Name
== 0)
353 validate_framebuffer(dri_ctx
, dri_read
,
354 &dri_ctx
->dri2
.read_stamp
);
356 nouveau_state_emit(ctx
);