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/fbobject.h"
37 #include "main/light.h"
38 #include "main/state.h"
39 #include "main/version.h"
40 #include "drivers/common/meta.h"
41 #include "drivers/common/driverfuncs.h"
42 #include "swrast/swrast.h"
43 #include "swrast/s_context.h"
46 #include "tnl/t_context.h"
49 nouveau_context_create(gl_api api
,
50 const struct gl_config
*visual
, __DRIcontext
*dri_ctx
,
51 unsigned major_version
,
52 unsigned minor_version
,
57 __DRIscreen
*dri_screen
= dri_ctx
->driScreenPriv
;
58 struct nouveau_screen
*screen
= dri_screen
->driverPrivate
;
59 struct nouveau_context
*nctx
;
60 struct gl_context
*ctx
;
62 /* API and flag filtering is handled in dri2CreateContextAttribs.
67 ctx
= screen
->driver
->context_create(screen
, visual
, share_ctx
);
69 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
73 nctx
= to_nouveau_context(ctx
);
74 nctx
->dri_context
= dri_ctx
;
75 dri_ctx
->driverPrivate
= ctx
;
77 _mesa_compute_version(ctx
);
78 if (ctx
->VersionMajor
< major_version
79 || (ctx
->VersionMajor
== major_version
80 && ctx
->VersionMinor
< minor_version
)) {
81 nouveau_context_destroy(dri_ctx
);
82 *error
= __DRI_CTX_ERROR_BAD_VERSION
;
86 *error
= __DRI_CTX_ERROR_SUCCESS
;
91 nouveau_context_init(struct gl_context
*ctx
, struct nouveau_screen
*screen
,
92 const struct gl_config
*visual
, struct gl_context
*share_ctx
)
94 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
95 struct dd_function_table functions
;
98 nctx
->screen
= screen
;
99 nctx
->fallback
= HWTNL
;
101 /* Initialize the function pointers. */
102 _mesa_init_driver_functions(&functions
);
103 nouveau_driver_functions_init(&functions
);
104 nouveau_bufferobj_functions_init(&functions
);
105 nouveau_texture_functions_init(&functions
);
106 nouveau_fbo_functions_init(&functions
);
108 /* Initialize the mesa context. */
109 _mesa_initialize_context(ctx
, API_OPENGL
, visual
,
110 share_ctx
, &functions
, NULL
);
112 nouveau_state_init(ctx
);
113 nouveau_scratch_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_object_new(&context_dev(ctx
)->object
, 0xbeef0000,
123 NOUVEAU_FIFO_CHANNEL_CLASS
,
127 }, sizeof(struct nv04_fifo
), &nctx
->hw
.chan
);
129 nouveau_error("Error initializing the FIFO.\n");
133 /* Allocate a client (thread data) */
134 ret
= nouveau_client_new(context_dev(ctx
), &nctx
->hw
.client
);
136 nouveau_error("Error creating thread data\n");
140 /* Allocate a push buffer */
141 ret
= nouveau_pushbuf_new(nctx
->hw
.client
, nctx
->hw
.chan
, 4,
142 512 * 1024, true, &nctx
->hw
.pushbuf
);
144 nouveau_error("Error allocating DMA push buffer\n");
148 /* Allocate buffer context */
149 ret
= nouveau_bufctx_new(nctx
->hw
.client
, 16, &nctx
->hw
.bufctx
);
151 nouveau_error("Error allocating buffer context\n");
155 nctx
->hw
.pushbuf
->user_priv
= nctx
->hw
.bufctx
;
157 /* Allocate NULL object */
158 ret
= nouveau_object_new(nctx
->hw
.chan
, 0x00000000, NV01_NULL_CLASS
,
159 NULL
, 0, &nctx
->hw
.null
);
161 nouveau_error("Error allocating NULL object\n");
165 /* Enable any supported extensions. */
166 ctx
->Extensions
.EXT_blend_color
= true;
167 ctx
->Extensions
.EXT_blend_minmax
= true;
168 ctx
->Extensions
.EXT_fog_coord
= true;
169 ctx
->Extensions
.EXT_framebuffer_blit
= true;
170 ctx
->Extensions
.EXT_framebuffer_object
= true;
171 ctx
->Extensions
.EXT_packed_depth_stencil
= true;
172 ctx
->Extensions
.EXT_secondary_color
= true;
173 ctx
->Extensions
.EXT_texture_filter_anisotropic
= true;
174 ctx
->Extensions
.NV_blend_square
= true;
175 ctx
->Extensions
.NV_texture_env_combine4
= true;
181 nouveau_context_deinit(struct gl_context
*ctx
)
183 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
185 if (TNL_CONTEXT(ctx
))
186 _tnl_DestroyContext(ctx
);
188 if (vbo_context(ctx
))
189 _vbo_DestroyContext(ctx
);
191 if (SWRAST_CONTEXT(ctx
))
192 _swrast_DestroyContext(ctx
);
195 _mesa_meta_free(ctx
);
197 nouveau_bufctx_del(&nctx
->hw
.bufctx
);
198 nouveau_pushbuf_del(&nctx
->hw
.pushbuf
);
199 nouveau_client_del(&nctx
->hw
.client
);
200 nouveau_object_del(&nctx
->hw
.chan
);
202 nouveau_scratch_destroy(ctx
);
203 _mesa_free_context_data(ctx
);
207 nouveau_context_destroy(__DRIcontext
*dri_ctx
)
209 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
210 struct gl_context
*ctx
= &nctx
->base
;
212 context_drv(ctx
)->context_destroy(ctx
);
216 nouveau_update_renderbuffers(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
)
218 struct gl_context
*ctx
= dri_ctx
->driverPrivate
;
219 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
220 __DRIscreen
*screen
= dri_ctx
->driScreenPriv
;
221 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
222 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
223 unsigned int attachments
[10];
224 __DRIbuffer
*buffers
= NULL
;
225 int i
= 0, count
, ret
;
227 if (draw
->lastStamp
== draw
->dri2
.stamp
)
229 draw
->lastStamp
= draw
->dri2
.stamp
;
232 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
233 if (fb
->Visual
.doubleBufferMode
)
234 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
235 if (fb
->Visual
.haveDepthBuffer
&& fb
->Visual
.haveStencilBuffer
)
236 attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
237 else if (fb
->Visual
.haveDepthBuffer
)
238 attachments
[i
++] = __DRI_BUFFER_DEPTH
;
239 else if (fb
->Visual
.haveStencilBuffer
)
240 attachments
[i
++] = __DRI_BUFFER_STENCIL
;
242 buffers
= (*screen
->dri2
.loader
->getBuffers
)(draw
, &draw
->w
, &draw
->h
,
243 attachments
, i
, &count
,
244 draw
->loaderPrivate
);
248 for (i
= 0; i
< count
; i
++) {
249 struct gl_renderbuffer
*rb
;
250 struct nouveau_surface
*s
;
254 switch (buffers
[i
].attachment
) {
255 case __DRI_BUFFER_FRONT_LEFT
:
256 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
257 index
= BUFFER_FRONT_LEFT
;
259 case __DRI_BUFFER_BACK_LEFT
:
260 index
= BUFFER_BACK_LEFT
;
262 case __DRI_BUFFER_DEPTH
:
263 case __DRI_BUFFER_DEPTH_STENCIL
:
264 index
= BUFFER_DEPTH
;
266 case __DRI_BUFFER_STENCIL
:
267 index
= BUFFER_STENCIL
;
273 rb
= fb
->Attachment
[index
].Renderbuffer
;
274 s
= &to_nouveau_renderbuffer(rb
)->surface
;
278 s
->pitch
= buffers
[i
].pitch
;
279 s
->cpp
= buffers
[i
].cpp
;
281 if (index
== BUFFER_DEPTH
&& s
->bo
) {
282 ret
= nouveau_bo_name_get(s
->bo
, &old_name
);
284 * Disable fast Z clears in the next frame, the
285 * depth buffer contents are undefined.
287 if (!ret
&& old_name
!= buffers
[i
].name
)
288 nctx
->hierz
.clear_seq
= 0;
291 nouveau_bo_ref(NULL
, &s
->bo
);
292 ret
= nouveau_bo_name_ref(context_dev(ctx
),
293 buffers
[i
].name
, &s
->bo
);
297 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
301 update_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
304 struct gl_context
*ctx
= dri_ctx
->driverPrivate
;
305 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
307 *stamp
= draw
->dri2
.stamp
;
309 nouveau_update_renderbuffers(dri_ctx
, draw
);
310 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
312 /* Clean up references to the old framebuffer objects. */
313 context_dirty(ctx
, FRAMEBUFFER
);
314 nouveau_bufctx_reset(to_nouveau_context(ctx
)->hw
.bufctx
, BUFCTX_FB
);
315 PUSH_KICK(context_push(ctx
));
319 nouveau_context_make_current(__DRIcontext
*dri_ctx
, __DRIdrawable
*dri_draw
,
320 __DRIdrawable
*dri_read
)
323 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
324 struct gl_context
*ctx
= &nctx
->base
;
326 /* Ask the X server for new renderbuffers. */
327 if (dri_draw
->driverPrivate
!= ctx
->WinSysDrawBuffer
)
328 update_framebuffer(dri_ctx
, dri_draw
,
329 &dri_ctx
->dri2
.draw_stamp
);
331 if (dri_draw
!= dri_read
&&
332 dri_read
->driverPrivate
!= ctx
->WinSysReadBuffer
)
333 update_framebuffer(dri_ctx
, dri_read
,
334 &dri_ctx
->dri2
.read_stamp
);
336 /* Pass it down to mesa. */
337 _mesa_make_current(ctx
, dri_draw
->driverPrivate
,
338 dri_read
->driverPrivate
);
339 _mesa_update_state(ctx
);
342 _mesa_make_current(NULL
, NULL
, NULL
);
349 nouveau_context_unbind(__DRIcontext
*dri_ctx
)
351 /* Unset current context and dispatch table */
352 _mesa_make_current(NULL
, NULL
, NULL
);
358 nouveau_fallback(struct gl_context
*ctx
, enum nouveau_fallback mode
)
360 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
362 nctx
->fallback
= MAX2(HWTNL
, mode
);
365 nouveau_state_emit(ctx
);
367 nouveau_bo_state_emit(ctx
);
370 PUSH_KICK(context_push(ctx
));
375 validate_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
378 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
379 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
380 GLboolean need_front
=
381 (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
||
382 fb
->_ColorReadBufferIndex
== BUFFER_FRONT_LEFT
);
384 if (nfb
->need_front
!= need_front
) {
385 nfb
->need_front
= need_front
;
386 dri2InvalidateDrawable(draw
);
389 if (draw
->dri2
.stamp
!= *stamp
)
390 update_framebuffer(dri_ctx
, draw
, stamp
);
394 nouveau_validate_framebuffer(struct gl_context
*ctx
)
396 __DRIcontext
*dri_ctx
= to_nouveau_context(ctx
)->dri_context
;
397 __DRIdrawable
*dri_draw
= dri_ctx
->driDrawablePriv
;
398 __DRIdrawable
*dri_read
= dri_ctx
->driReadablePriv
;
400 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
))
401 validate_framebuffer(dri_ctx
, dri_draw
,
402 &dri_ctx
->dri2
.draw_stamp
);
404 if (_mesa_is_winsys_fbo(ctx
->ReadBuffer
))
405 validate_framebuffer(dri_ctx
, dri_read
,
406 &dri_ctx
->dri2
.read_stamp
);
408 if (ctx
->NewState
& _NEW_BUFFERS
)
409 _mesa_update_state(ctx
);