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
},
65 { "GL_SGIS_generate_mipmap", NULL
},
70 nouveau_channel_flush_notify(struct nouveau_channel
*chan
)
72 struct nouveau_context
*nctx
= chan
->user_private
;
73 GLcontext
*ctx
= &nctx
->base
;
75 if (nctx
->fallback
< SWRAST
)
76 nouveau_bo_state_emit(ctx
);
80 nouveau_context_create(gl_api api
,
81 const __GLcontextModes
*visual
, __DRIcontext
*dri_ctx
,
84 __DRIscreen
*dri_screen
= dri_ctx
->driScreenPriv
;
85 struct nouveau_screen
*screen
= dri_screen
->private;
86 struct nouveau_context
*nctx
;
89 ctx
= screen
->driver
->context_create(screen
, visual
, share_ctx
);
93 nctx
= to_nouveau_context(ctx
);
94 nctx
->dri_context
= dri_ctx
;
95 dri_ctx
->driverPrivate
= ctx
;
101 nouveau_context_init(GLcontext
*ctx
, struct nouveau_screen
*screen
,
102 const GLvisual
*visual
, GLcontext
*share_ctx
)
104 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
105 struct dd_function_table functions
;
108 nctx
->screen
= screen
;
109 nctx
->fallback
= HWTNL
;
111 /* Initialize the function pointers. */
112 _mesa_init_driver_functions(&functions
);
113 nouveau_driver_functions_init(&functions
);
114 nouveau_bufferobj_functions_init(&functions
);
115 nouveau_texture_functions_init(&functions
);
116 nouveau_fbo_functions_init(&functions
);
118 /* Initialize the mesa context. */
119 _mesa_initialize_context(ctx
, visual
, share_ctx
, &functions
, NULL
);
121 nouveau_state_init(ctx
);
122 nouveau_bo_state_init(ctx
);
123 _mesa_meta_init(ctx
);
124 _swrast_CreateContext(ctx
);
125 _vbo_CreateContext(ctx
);
126 _tnl_CreateContext(ctx
);
127 nouveau_span_functions_init(ctx
);
128 _mesa_allow_light_in_model(ctx
, GL_FALSE
);
130 /* Allocate a hardware channel. */
131 ret
= nouveau_channel_alloc(context_dev(ctx
), 0xbeef0201, 0xbeef0202,
134 nouveau_error("Error initializing the FIFO.\n");
138 nctx
->hw
.chan
->flush_notify
= nouveau_channel_flush_notify
;
139 nctx
->hw
.chan
->user_private
= nctx
;
141 /* Enable any supported extensions. */
142 driInitExtensions(ctx
, nouveau_extensions
, GL_TRUE
);
148 nouveau_context_deinit(GLcontext
*ctx
)
150 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
152 if (TNL_CONTEXT(ctx
))
153 _tnl_DestroyContext(ctx
);
155 if (vbo_context(ctx
))
156 _vbo_DestroyContext(ctx
);
158 if (SWRAST_CONTEXT(ctx
))
159 _swrast_DestroyContext(ctx
);
162 _mesa_meta_free(ctx
);
165 nouveau_channel_free(&nctx
->hw
.chan
);
167 nouveau_bo_state_destroy(ctx
);
168 _mesa_free_context_data(ctx
);
172 nouveau_context_destroy(__DRIcontext
*dri_ctx
)
174 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
175 GLcontext
*ctx
= &nctx
->base
;
177 context_drv(ctx
)->context_destroy(ctx
);
181 nouveau_update_renderbuffers(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
)
183 GLcontext
*ctx
= dri_ctx
->driverPrivate
;
184 __DRIscreen
*screen
= dri_ctx
->driScreenPriv
;
185 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
186 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
187 unsigned int attachments
[10];
188 __DRIbuffer
*buffers
= NULL
;
189 int i
= 0, count
, ret
;
191 if (draw
->lastStamp
== *draw
->pStamp
)
193 draw
->lastStamp
= *draw
->pStamp
;
196 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
197 if (fb
->Visual
.doubleBufferMode
)
198 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
199 if (fb
->Visual
.haveDepthBuffer
&& fb
->Visual
.haveStencilBuffer
)
200 attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
201 else if (fb
->Visual
.haveDepthBuffer
)
202 attachments
[i
++] = __DRI_BUFFER_DEPTH
;
203 else if (fb
->Visual
.haveStencilBuffer
)
204 attachments
[i
++] = __DRI_BUFFER_STENCIL
;
206 buffers
= (*screen
->dri2
.loader
->getBuffers
)(draw
, &draw
->w
, &draw
->h
,
207 attachments
, i
, &count
,
208 draw
->loaderPrivate
);
212 for (i
= 0; i
< count
; i
++) {
213 struct gl_renderbuffer
*rb
;
214 struct nouveau_surface
*s
;
217 switch (buffers
[i
].attachment
) {
218 case __DRI_BUFFER_FRONT_LEFT
:
219 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
220 index
= BUFFER_FRONT_LEFT
;
222 case __DRI_BUFFER_BACK_LEFT
:
223 index
= BUFFER_BACK_LEFT
;
225 case __DRI_BUFFER_DEPTH
:
226 case __DRI_BUFFER_DEPTH_STENCIL
:
227 index
= BUFFER_DEPTH
;
229 case __DRI_BUFFER_STENCIL
:
230 index
= BUFFER_STENCIL
;
236 rb
= fb
->Attachment
[index
].Renderbuffer
;
237 s
= &to_nouveau_renderbuffer(rb
)->surface
;
241 s
->pitch
= buffers
[i
].pitch
;
242 s
->cpp
= buffers
[i
].cpp
;
244 nouveau_bo_ref(NULL
, &s
->bo
);
245 ret
= nouveau_bo_handle_ref(context_dev(ctx
),
246 buffers
[i
].name
, &s
->bo
);
250 _mesa_resize_framebuffer(NULL
, fb
, draw
->w
, draw
->h
);
254 update_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
257 GLcontext
*ctx
= dri_ctx
->driverPrivate
;
258 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
260 *stamp
= *draw
->pStamp
;
262 nouveau_update_renderbuffers(dri_ctx
, draw
);
263 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
265 context_dirty(ctx
, FRAMEBUFFER
);
269 nouveau_context_make_current(__DRIcontext
*dri_ctx
, __DRIdrawable
*dri_draw
,
270 __DRIdrawable
*dri_read
)
273 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
274 GLcontext
*ctx
= &nctx
->base
;
276 /* Ask the X server for new renderbuffers. */
277 if (dri_draw
->driverPrivate
!= ctx
->WinSysDrawBuffer
)
278 update_framebuffer(dri_ctx
, dri_draw
,
279 &dri_ctx
->dri2
.draw_stamp
);
281 if (dri_draw
!= dri_read
&&
282 dri_read
->driverPrivate
!= ctx
->WinSysReadBuffer
)
283 update_framebuffer(dri_ctx
, dri_read
,
284 &dri_ctx
->dri2
.read_stamp
);
286 /* Clean up references to the old framebuffer objects. */
287 context_bctx(ctx
, FRAMEBUFFER
);
288 FIRE_RING(context_chan(ctx
));
290 /* Pass it down to mesa. */
291 _mesa_make_current(ctx
, dri_draw
->driverPrivate
,
292 dri_read
->driverPrivate
);
293 _mesa_update_state(ctx
);
296 _mesa_make_current(NULL
, NULL
, NULL
);
303 nouveau_context_unbind(__DRIcontext
*dri_ctx
)
305 /* Unset current context and dispath table */
306 _mesa_make_current(NULL
, NULL
, NULL
);
312 nouveau_fallback(GLcontext
*ctx
, enum nouveau_fallback mode
)
314 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
316 nctx
->fallback
= MAX2(HWTNL
, mode
);
319 nouveau_state_emit(ctx
);
321 FIRE_RING(context_chan(ctx
));
325 validate_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
328 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
329 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
330 GLboolean need_front
=
331 (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
||
332 fb
->_ColorReadBufferIndex
== BUFFER_FRONT_LEFT
);
334 if (nfb
->need_front
!= need_front
) {
335 nfb
->need_front
= need_front
;
336 dri2InvalidateDrawable(draw
);
339 if (*draw
->pStamp
!= *stamp
)
340 update_framebuffer(dri_ctx
, draw
, stamp
);
344 nouveau_validate_framebuffer(GLcontext
*ctx
)
346 __DRIcontext
*dri_ctx
= to_nouveau_context(ctx
)->dri_context
;
347 __DRIdrawable
*dri_draw
= dri_ctx
->driDrawablePriv
;
348 __DRIdrawable
*dri_read
= dri_ctx
->driReadablePriv
;
350 if (ctx
->DrawBuffer
->Name
== 0)
351 validate_framebuffer(dri_ctx
, dri_draw
,
352 &dri_ctx
->dri2
.draw_stamp
);
354 if (ctx
->ReadBuffer
->Name
== 0)
355 validate_framebuffer(dri_ctx
, dri_read
,
356 &dri_ctx
->dri2
.read_stamp
);
358 if (nouveau_next_dirty_state(ctx
) >= 0) {
359 nouveau_state_emit(ctx
);
360 FIRE_RING(context_chan(ctx
));