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.
28 #include "nouveau_driver.h"
29 #include "nouveau_context.h"
30 #include "nouveau_bufferobj.h"
31 #include "nouveau_fbo.h"
34 #include "main/framebuffer.h"
35 #include "main/light.h"
36 #include "main/state.h"
37 #include "drivers/common/meta.h"
38 #include "drivers/common/driverfuncs.h"
39 #include "swrast/swrast.h"
40 #include "swrast/s_context.h"
43 #include "tnl/t_context.h"
46 nouveau_channel_flush_notify(struct nouveau_channel
*chan
)
48 struct nouveau_context
*nctx
= chan
->user_private
;
49 struct gl_context
*ctx
= &nctx
->base
;
51 if (nctx
->fallback
< SWRAST
)
52 nouveau_bo_state_emit(ctx
);
56 nouveau_context_create(gl_api api
,
57 const struct gl_config
*visual
, __DRIcontext
*dri_ctx
,
60 __DRIscreen
*dri_screen
= dri_ctx
->driScreenPriv
;
61 struct nouveau_screen
*screen
= dri_screen
->private;
62 struct nouveau_context
*nctx
;
63 struct gl_context
*ctx
;
65 ctx
= screen
->driver
->context_create(screen
, visual
, share_ctx
);
69 nctx
= to_nouveau_context(ctx
);
70 nctx
->dri_context
= dri_ctx
;
71 dri_ctx
->driverPrivate
= ctx
;
77 nouveau_context_init(struct gl_context
*ctx
, struct nouveau_screen
*screen
,
78 const struct gl_config
*visual
, struct gl_context
*share_ctx
)
80 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
81 struct dd_function_table functions
;
84 nctx
->screen
= screen
;
85 nctx
->fallback
= HWTNL
;
87 /* Initialize the function pointers. */
88 _mesa_init_driver_functions(&functions
);
89 nouveau_driver_functions_init(&functions
);
90 nouveau_bufferobj_functions_init(&functions
);
91 nouveau_texture_functions_init(&functions
);
92 nouveau_fbo_functions_init(&functions
);
94 /* Initialize the mesa context. */
95 _mesa_initialize_context(ctx
, API_OPENGL
, visual
,
96 share_ctx
, &functions
, NULL
);
98 nouveau_state_init(ctx
);
99 nouveau_bo_state_init(ctx
);
100 nouveau_scratch_init(ctx
);
101 _mesa_meta_init(ctx
);
102 _swrast_CreateContext(ctx
);
103 _vbo_CreateContext(ctx
);
104 _tnl_CreateContext(ctx
);
105 nouveau_span_functions_init(ctx
);
106 _mesa_allow_light_in_model(ctx
, GL_FALSE
);
108 /* Allocate a hardware channel. */
109 ret
= nouveau_channel_alloc(context_dev(ctx
), 0xbeef0201, 0xbeef0202,
110 512*1024, &nctx
->hw
.chan
);
112 nouveau_error("Error initializing the FIFO.\n");
116 nctx
->hw
.chan
->flush_notify
= nouveau_channel_flush_notify
;
117 nctx
->hw
.chan
->user_private
= nctx
;
119 /* Enable any supported extensions. */
120 ctx
->Extensions
.ARB_texture_mirrored_repeat
= true;
121 ctx
->Extensions
.EXT_blend_color
= true;
122 ctx
->Extensions
.EXT_blend_minmax
= true;
123 ctx
->Extensions
.EXT_blend_subtract
= true;
124 ctx
->Extensions
.EXT_fog_coord
= true;
125 ctx
->Extensions
.EXT_framebuffer_blit
= true;
126 ctx
->Extensions
.EXT_framebuffer_object
= true;
127 ctx
->Extensions
.EXT_packed_depth_stencil
= true;
128 ctx
->Extensions
.EXT_secondary_color
= true;
129 ctx
->Extensions
.EXT_texture_filter_anisotropic
= true;
130 ctx
->Extensions
.NV_blend_square
= true;
131 ctx
->Extensions
.NV_texture_env_combine4
= true;
137 nouveau_context_deinit(struct gl_context
*ctx
)
139 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
141 if (TNL_CONTEXT(ctx
))
142 _tnl_DestroyContext(ctx
);
144 if (vbo_context(ctx
))
145 _vbo_DestroyContext(ctx
);
147 if (SWRAST_CONTEXT(ctx
))
148 _swrast_DestroyContext(ctx
);
151 _mesa_meta_free(ctx
);
154 nouveau_channel_free(&nctx
->hw
.chan
);
156 nouveau_scratch_destroy(ctx
);
157 nouveau_bo_state_destroy(ctx
);
158 _mesa_free_context_data(ctx
);
162 nouveau_context_destroy(__DRIcontext
*dri_ctx
)
164 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
165 struct gl_context
*ctx
= &nctx
->base
;
167 context_drv(ctx
)->context_destroy(ctx
);
171 nouveau_update_renderbuffers(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
)
173 struct gl_context
*ctx
= dri_ctx
->driverPrivate
;
174 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
175 __DRIscreen
*screen
= dri_ctx
->driScreenPriv
;
176 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
177 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
178 unsigned int attachments
[10];
179 __DRIbuffer
*buffers
= NULL
;
180 int i
= 0, count
, ret
;
182 if (draw
->lastStamp
== *draw
->pStamp
)
184 draw
->lastStamp
= *draw
->pStamp
;
187 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
188 if (fb
->Visual
.doubleBufferMode
)
189 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
190 if (fb
->Visual
.haveDepthBuffer
&& fb
->Visual
.haveStencilBuffer
)
191 attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
192 else if (fb
->Visual
.haveDepthBuffer
)
193 attachments
[i
++] = __DRI_BUFFER_DEPTH
;
194 else if (fb
->Visual
.haveStencilBuffer
)
195 attachments
[i
++] = __DRI_BUFFER_STENCIL
;
197 buffers
= (*screen
->dri2
.loader
->getBuffers
)(draw
, &draw
->w
, &draw
->h
,
198 attachments
, i
, &count
,
199 draw
->loaderPrivate
);
203 for (i
= 0; i
< count
; i
++) {
204 struct gl_renderbuffer
*rb
;
205 struct nouveau_surface
*s
;
209 switch (buffers
[i
].attachment
) {
210 case __DRI_BUFFER_FRONT_LEFT
:
211 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
212 index
= BUFFER_FRONT_LEFT
;
214 case __DRI_BUFFER_BACK_LEFT
:
215 index
= BUFFER_BACK_LEFT
;
217 case __DRI_BUFFER_DEPTH
:
218 case __DRI_BUFFER_DEPTH_STENCIL
:
219 index
= BUFFER_DEPTH
;
221 case __DRI_BUFFER_STENCIL
:
222 index
= BUFFER_STENCIL
;
228 rb
= fb
->Attachment
[index
].Renderbuffer
;
229 s
= &to_nouveau_renderbuffer(rb
)->surface
;
233 s
->pitch
= buffers
[i
].pitch
;
234 s
->cpp
= buffers
[i
].cpp
;
236 if (index
== BUFFER_DEPTH
&& s
->bo
) {
237 ret
= nouveau_bo_handle_get(s
->bo
, &old_name
);
239 * Disable fast Z clears in the next frame, the
240 * depth buffer contents are undefined.
242 if (!ret
&& old_name
!= buffers
[i
].name
)
243 nctx
->hierz
.clear_seq
= 0;
246 nouveau_bo_ref(NULL
, &s
->bo
);
247 ret
= nouveau_bo_handle_ref(context_dev(ctx
),
248 buffers
[i
].name
, &s
->bo
);
252 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
256 update_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
259 struct gl_context
*ctx
= dri_ctx
->driverPrivate
;
260 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
262 *stamp
= *draw
->pStamp
;
264 nouveau_update_renderbuffers(dri_ctx
, draw
);
265 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
267 /* Clean up references to the old framebuffer objects. */
268 context_dirty(ctx
, FRAMEBUFFER
);
269 context_bctx(ctx
, FRAMEBUFFER
);
270 FIRE_RING(context_chan(ctx
));
274 nouveau_context_make_current(__DRIcontext
*dri_ctx
, __DRIdrawable
*dri_draw
,
275 __DRIdrawable
*dri_read
)
278 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
279 struct gl_context
*ctx
= &nctx
->base
;
281 /* Ask the X server for new renderbuffers. */
282 if (dri_draw
->driverPrivate
!= ctx
->WinSysDrawBuffer
)
283 update_framebuffer(dri_ctx
, dri_draw
,
284 &dri_ctx
->dri2
.draw_stamp
);
286 if (dri_draw
!= dri_read
&&
287 dri_read
->driverPrivate
!= ctx
->WinSysReadBuffer
)
288 update_framebuffer(dri_ctx
, dri_read
,
289 &dri_ctx
->dri2
.read_stamp
);
291 /* Pass it down to mesa. */
292 _mesa_make_current(ctx
, dri_draw
->driverPrivate
,
293 dri_read
->driverPrivate
);
294 _mesa_update_state(ctx
);
297 _mesa_make_current(NULL
, NULL
, NULL
);
304 nouveau_context_unbind(__DRIcontext
*dri_ctx
)
306 /* Unset current context and dispatch table */
307 _mesa_make_current(NULL
, NULL
, NULL
);
313 nouveau_fallback(struct gl_context
*ctx
, enum nouveau_fallback mode
)
315 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
317 nctx
->fallback
= MAX2(HWTNL
, mode
);
320 nouveau_state_emit(ctx
);
321 nouveau_bo_state_emit(ctx
);
323 FIRE_RING(context_chan(ctx
));
328 validate_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
331 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
332 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
333 GLboolean need_front
=
334 (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
||
335 fb
->_ColorReadBufferIndex
== BUFFER_FRONT_LEFT
);
337 if (nfb
->need_front
!= need_front
) {
338 nfb
->need_front
= need_front
;
339 dri2InvalidateDrawable(draw
);
342 if (*draw
->pStamp
!= *stamp
)
343 update_framebuffer(dri_ctx
, draw
, stamp
);
347 nouveau_validate_framebuffer(struct gl_context
*ctx
)
349 __DRIcontext
*dri_ctx
= to_nouveau_context(ctx
)->dri_context
;
350 __DRIdrawable
*dri_draw
= dri_ctx
->driDrawablePriv
;
351 __DRIdrawable
*dri_read
= dri_ctx
->driReadablePriv
;
353 if (ctx
->DrawBuffer
->Name
== 0)
354 validate_framebuffer(dri_ctx
, dri_draw
,
355 &dri_ctx
->dri2
.draw_stamp
);
357 if (ctx
->ReadBuffer
->Name
== 0)
358 validate_framebuffer(dri_ctx
, dri_read
,
359 &dri_ctx
->dri2
.read_stamp
);
361 if (ctx
->NewState
& _NEW_BUFFERS
)
362 _mesa_update_state(ctx
);