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"
32 #include "nv_object.xml.h"
35 #include "main/framebuffer.h"
36 #include "main/light.h"
37 #include "main/state.h"
38 #include "main/version.h"
39 #include "drivers/common/meta.h"
40 #include "drivers/common/driverfuncs.h"
41 #include "swrast/swrast.h"
42 #include "swrast/s_context.h"
45 #include "tnl/t_context.h"
48 nouveau_context_create(gl_api api
,
49 const struct gl_config
*visual
, __DRIcontext
*dri_ctx
,
50 unsigned major_version
,
51 unsigned minor_version
,
56 __DRIscreen
*dri_screen
= dri_ctx
->driScreenPriv
;
57 struct nouveau_screen
*screen
= dri_screen
->driverPrivate
;
58 struct nouveau_context
*nctx
;
59 struct gl_context
*ctx
;
61 /* API and flag filtering is handled in dri2CreateContextAttribs.
66 ctx
= screen
->driver
->context_create(screen
, visual
, share_ctx
);
68 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
72 nctx
= to_nouveau_context(ctx
);
73 nctx
->dri_context
= dri_ctx
;
74 dri_ctx
->driverPrivate
= ctx
;
76 _mesa_compute_version(ctx
);
77 if (ctx
->VersionMajor
< major_version
78 || (ctx
->VersionMajor
== major_version
79 && ctx
->VersionMinor
< minor_version
)) {
80 nouveau_context_destroy(dri_ctx
);
81 *error
= __DRI_CTX_ERROR_BAD_VERSION
;
85 *error
= __DRI_CTX_ERROR_SUCCESS
;
90 nouveau_context_init(struct gl_context
*ctx
, struct nouveau_screen
*screen
,
91 const struct gl_config
*visual
, struct gl_context
*share_ctx
)
93 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
94 struct dd_function_table functions
;
97 nctx
->screen
= screen
;
98 nctx
->fallback
= HWTNL
;
100 /* Initialize the function pointers. */
101 _mesa_init_driver_functions(&functions
);
102 nouveau_driver_functions_init(&functions
);
103 nouveau_bufferobj_functions_init(&functions
);
104 nouveau_texture_functions_init(&functions
);
105 nouveau_fbo_functions_init(&functions
);
107 /* Initialize the mesa context. */
108 _mesa_initialize_context(ctx
, API_OPENGL
, visual
,
109 share_ctx
, &functions
, NULL
);
111 nouveau_state_init(ctx
);
112 nouveau_scratch_init(ctx
);
113 _mesa_meta_init(ctx
);
114 _swrast_CreateContext(ctx
);
115 _vbo_CreateContext(ctx
);
116 _tnl_CreateContext(ctx
);
117 nouveau_span_functions_init(ctx
);
118 _mesa_allow_light_in_model(ctx
, GL_FALSE
);
120 /* Allocate a hardware channel. */
121 ret
= nouveau_object_new(&context_dev(ctx
)->object
, 0xbeef0000,
122 NOUVEAU_FIFO_CHANNEL_CLASS
,
126 }, sizeof(struct nv04_fifo
), &nctx
->hw
.chan
);
128 nouveau_error("Error initializing the FIFO.\n");
132 /* Allocate a client (thread data) */
133 ret
= nouveau_client_new(context_dev(ctx
), &nctx
->hw
.client
);
135 nouveau_error("Error creating thread data\n");
139 /* Allocate a push buffer */
140 ret
= nouveau_pushbuf_new(nctx
->hw
.client
, nctx
->hw
.chan
, 4,
141 512 * 1024, true, &nctx
->hw
.pushbuf
);
143 nouveau_error("Error allocating DMA push buffer\n");
147 /* Allocate buffer context */
148 ret
= nouveau_bufctx_new(nctx
->hw
.client
, 16, &nctx
->hw
.bufctx
);
150 nouveau_error("Error allocating buffer context\n");
154 nctx
->hw
.pushbuf
->user_priv
= nctx
->hw
.bufctx
;
156 /* Allocate NULL object */
157 ret
= nouveau_object_new(nctx
->hw
.chan
, 0x00000000, NV01_NULL_CLASS
,
158 NULL
, 0, &nctx
->hw
.null
);
160 nouveau_error("Error allocating NULL object\n");
164 /* Enable any supported extensions. */
165 ctx
->Extensions
.EXT_blend_color
= true;
166 ctx
->Extensions
.EXT_blend_minmax
= true;
167 ctx
->Extensions
.EXT_fog_coord
= true;
168 ctx
->Extensions
.EXT_framebuffer_blit
= true;
169 ctx
->Extensions
.EXT_framebuffer_object
= true;
170 ctx
->Extensions
.EXT_packed_depth_stencil
= true;
171 ctx
->Extensions
.EXT_secondary_color
= true;
172 ctx
->Extensions
.EXT_texture_filter_anisotropic
= true;
173 ctx
->Extensions
.NV_blend_square
= true;
174 ctx
->Extensions
.NV_texture_env_combine4
= true;
180 nouveau_context_deinit(struct gl_context
*ctx
)
182 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
184 if (TNL_CONTEXT(ctx
))
185 _tnl_DestroyContext(ctx
);
187 if (vbo_context(ctx
))
188 _vbo_DestroyContext(ctx
);
190 if (SWRAST_CONTEXT(ctx
))
191 _swrast_DestroyContext(ctx
);
194 _mesa_meta_free(ctx
);
196 nouveau_bufctx_del(&nctx
->hw
.bufctx
);
197 nouveau_pushbuf_del(&nctx
->hw
.pushbuf
);
198 nouveau_client_del(&nctx
->hw
.client
);
199 nouveau_object_del(&nctx
->hw
.chan
);
201 nouveau_scratch_destroy(ctx
);
202 _mesa_free_context_data(ctx
);
206 nouveau_context_destroy(__DRIcontext
*dri_ctx
)
208 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
209 struct gl_context
*ctx
= &nctx
->base
;
211 context_drv(ctx
)->context_destroy(ctx
);
215 nouveau_update_renderbuffers(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
)
217 struct gl_context
*ctx
= dri_ctx
->driverPrivate
;
218 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
219 __DRIscreen
*screen
= dri_ctx
->driScreenPriv
;
220 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
221 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
222 unsigned int attachments
[10];
223 __DRIbuffer
*buffers
= NULL
;
224 int i
= 0, count
, ret
;
226 if (draw
->lastStamp
== draw
->dri2
.stamp
)
228 draw
->lastStamp
= draw
->dri2
.stamp
;
231 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
232 if (fb
->Visual
.doubleBufferMode
)
233 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
234 if (fb
->Visual
.haveDepthBuffer
&& fb
->Visual
.haveStencilBuffer
)
235 attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
236 else if (fb
->Visual
.haveDepthBuffer
)
237 attachments
[i
++] = __DRI_BUFFER_DEPTH
;
238 else if (fb
->Visual
.haveStencilBuffer
)
239 attachments
[i
++] = __DRI_BUFFER_STENCIL
;
241 buffers
= (*screen
->dri2
.loader
->getBuffers
)(draw
, &draw
->w
, &draw
->h
,
242 attachments
, i
, &count
,
243 draw
->loaderPrivate
);
247 for (i
= 0; i
< count
; i
++) {
248 struct gl_renderbuffer
*rb
;
249 struct nouveau_surface
*s
;
253 switch (buffers
[i
].attachment
) {
254 case __DRI_BUFFER_FRONT_LEFT
:
255 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
256 index
= BUFFER_FRONT_LEFT
;
258 case __DRI_BUFFER_BACK_LEFT
:
259 index
= BUFFER_BACK_LEFT
;
261 case __DRI_BUFFER_DEPTH
:
262 case __DRI_BUFFER_DEPTH_STENCIL
:
263 index
= BUFFER_DEPTH
;
265 case __DRI_BUFFER_STENCIL
:
266 index
= BUFFER_STENCIL
;
272 rb
= fb
->Attachment
[index
].Renderbuffer
;
273 s
= &to_nouveau_renderbuffer(rb
)->surface
;
277 s
->pitch
= buffers
[i
].pitch
;
278 s
->cpp
= buffers
[i
].cpp
;
280 if (index
== BUFFER_DEPTH
&& s
->bo
) {
281 ret
= nouveau_bo_name_get(s
->bo
, &old_name
);
283 * Disable fast Z clears in the next frame, the
284 * depth buffer contents are undefined.
286 if (!ret
&& old_name
!= buffers
[i
].name
)
287 nctx
->hierz
.clear_seq
= 0;
290 nouveau_bo_ref(NULL
, &s
->bo
);
291 ret
= nouveau_bo_name_ref(context_dev(ctx
),
292 buffers
[i
].name
, &s
->bo
);
296 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
300 update_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
303 struct gl_context
*ctx
= dri_ctx
->driverPrivate
;
304 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
306 *stamp
= draw
->dri2
.stamp
;
308 nouveau_update_renderbuffers(dri_ctx
, draw
);
309 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
311 /* Clean up references to the old framebuffer objects. */
312 context_dirty(ctx
, FRAMEBUFFER
);
313 nouveau_bufctx_reset(to_nouveau_context(ctx
)->hw
.bufctx
, BUFCTX_FB
);
314 PUSH_KICK(context_push(ctx
));
318 nouveau_context_make_current(__DRIcontext
*dri_ctx
, __DRIdrawable
*dri_draw
,
319 __DRIdrawable
*dri_read
)
322 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
323 struct gl_context
*ctx
= &nctx
->base
;
325 /* Ask the X server for new renderbuffers. */
326 if (dri_draw
->driverPrivate
!= ctx
->WinSysDrawBuffer
)
327 update_framebuffer(dri_ctx
, dri_draw
,
328 &dri_ctx
->dri2
.draw_stamp
);
330 if (dri_draw
!= dri_read
&&
331 dri_read
->driverPrivate
!= ctx
->WinSysReadBuffer
)
332 update_framebuffer(dri_ctx
, dri_read
,
333 &dri_ctx
->dri2
.read_stamp
);
335 /* Pass it down to mesa. */
336 _mesa_make_current(ctx
, dri_draw
->driverPrivate
,
337 dri_read
->driverPrivate
);
338 _mesa_update_state(ctx
);
341 _mesa_make_current(NULL
, NULL
, NULL
);
348 nouveau_context_unbind(__DRIcontext
*dri_ctx
)
350 /* Unset current context and dispatch table */
351 _mesa_make_current(NULL
, NULL
, NULL
);
357 nouveau_fallback(struct gl_context
*ctx
, enum nouveau_fallback mode
)
359 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
361 nctx
->fallback
= MAX2(HWTNL
, mode
);
364 nouveau_state_emit(ctx
);
366 nouveau_bo_state_emit(ctx
);
369 PUSH_KICK(context_push(ctx
));
374 validate_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
377 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
378 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
379 GLboolean need_front
=
380 (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
||
381 fb
->_ColorReadBufferIndex
== BUFFER_FRONT_LEFT
);
383 if (nfb
->need_front
!= need_front
) {
384 nfb
->need_front
= need_front
;
385 dri2InvalidateDrawable(draw
);
388 if (draw
->dri2
.stamp
!= *stamp
)
389 update_framebuffer(dri_ctx
, draw
, stamp
);
393 nouveau_validate_framebuffer(struct gl_context
*ctx
)
395 __DRIcontext
*dri_ctx
= to_nouveau_context(ctx
)->dri_context
;
396 __DRIdrawable
*dri_draw
= dri_ctx
->driDrawablePriv
;
397 __DRIdrawable
*dri_read
= dri_ctx
->driReadablePriv
;
399 if (ctx
->DrawBuffer
->Name
== 0)
400 validate_framebuffer(dri_ctx
, dri_draw
,
401 &dri_ctx
->dri2
.draw_stamp
);
403 if (ctx
->ReadBuffer
->Name
== 0)
404 validate_framebuffer(dri_ctx
, dri_read
,
405 &dri_ctx
->dri2
.read_stamp
);
407 if (ctx
->NewState
& _NEW_BUFFERS
)
408 _mesa_update_state(ctx
);