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"
34 #include "main/api_exec.h"
36 #include "main/framebuffer.h"
37 #include "main/fbobject.h"
38 #include "main/light.h"
39 #include "main/state.h"
40 #include "main/version.h"
41 #include "main/vtxfmt.h"
42 #include "drivers/common/meta.h"
43 #include "drivers/common/driverfuncs.h"
44 #include "swrast/swrast.h"
45 #include "swrast/s_context.h"
48 #include "tnl/t_context.h"
51 nouveau_context_create(gl_api api
,
52 const struct gl_config
*visual
, __DRIcontext
*dri_ctx
,
53 unsigned major_version
,
54 unsigned minor_version
,
59 __DRIscreen
*dri_screen
= dri_ctx
->driScreenPriv
;
60 struct nouveau_screen
*screen
= dri_screen
->driverPrivate
;
61 struct nouveau_context
*nctx
;
62 struct gl_context
*ctx
;
65 case API_OPENGL_COMPAT
:
66 /* Do after-the-fact version checking (below).
70 /* NV10 and NV20 can support OpenGL ES 1.0 only. Older chips
71 * cannot do even that.
73 if ((screen
->device
->chipset
& 0xf0) == 0x00) {
74 *error
= __DRI_CTX_ERROR_BAD_API
;
76 } else if (minor_version
!= 0) {
77 *error
= __DRI_CTX_ERROR_BAD_VERSION
;
83 *error
= __DRI_CTX_ERROR_BAD_API
;
87 /* API and flag filtering is handled in dri2CreateContextAttribs.
91 ctx
= screen
->driver
->context_create(screen
, visual
, share_ctx
);
93 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
97 nctx
= to_nouveau_context(ctx
);
98 nctx
->dri_context
= dri_ctx
;
99 dri_ctx
->driverPrivate
= ctx
;
101 _mesa_compute_version(ctx
);
102 if (ctx
->Version
< major_version
* 10 + minor_version
) {
103 nouveau_context_destroy(dri_ctx
);
104 *error
= __DRI_CTX_ERROR_BAD_VERSION
;
108 /* Exec table initialization requires the version to be computed */
109 _mesa_initialize_dispatch_tables(ctx
);
110 _mesa_initialize_vbo_vtxfmt(ctx
);
112 if (nouveau_bo_new(context_dev(ctx
), NOUVEAU_BO_VRAM
, 0, 4096,
113 NULL
, &nctx
->fence
)) {
114 nouveau_context_destroy(dri_ctx
);
115 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
119 *error
= __DRI_CTX_ERROR_SUCCESS
;
124 nouveau_context_init(struct gl_context
*ctx
, struct nouveau_screen
*screen
,
125 const struct gl_config
*visual
, struct gl_context
*share_ctx
)
127 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
128 struct dd_function_table functions
;
131 nctx
->screen
= screen
;
132 nctx
->fallback
= HWTNL
;
134 /* Initialize the function pointers. */
135 _mesa_init_driver_functions(&functions
);
136 nouveau_driver_functions_init(&functions
);
137 nouveau_bufferobj_functions_init(&functions
);
138 nouveau_texture_functions_init(&functions
);
139 nouveau_fbo_functions_init(&functions
);
141 /* Initialize the mesa context. */
142 _mesa_initialize_context(ctx
, API_OPENGL_COMPAT
, visual
,
143 share_ctx
, &functions
);
145 nouveau_state_init(ctx
);
146 nouveau_scratch_init(ctx
);
147 _mesa_meta_init(ctx
);
148 _swrast_CreateContext(ctx
);
149 _vbo_CreateContext(ctx
);
150 _tnl_CreateContext(ctx
);
151 nouveau_span_functions_init(ctx
);
152 _mesa_allow_light_in_model(ctx
, GL_FALSE
);
154 /* Allocate a hardware channel. */
155 ret
= nouveau_object_new(&context_dev(ctx
)->object
, 0xbeef0000,
156 NOUVEAU_FIFO_CHANNEL_CLASS
,
160 }, sizeof(struct nv04_fifo
), &nctx
->hw
.chan
);
162 nouveau_error("Error initializing the FIFO.\n");
166 /* Allocate a client (thread data) */
167 ret
= nouveau_client_new(context_dev(ctx
), &nctx
->hw
.client
);
169 nouveau_error("Error creating thread data\n");
173 /* Allocate a push buffer */
174 ret
= nouveau_pushbuf_new(nctx
->hw
.client
, nctx
->hw
.chan
, 4,
175 512 * 1024, true, &nctx
->hw
.pushbuf
);
177 nouveau_error("Error allocating DMA push buffer\n");
181 /* Allocate buffer context */
182 ret
= nouveau_bufctx_new(nctx
->hw
.client
, 16, &nctx
->hw
.bufctx
);
184 nouveau_error("Error allocating buffer context\n");
188 nctx
->hw
.pushbuf
->user_priv
= nctx
->hw
.bufctx
;
190 /* Allocate NULL object */
191 ret
= nouveau_object_new(nctx
->hw
.chan
, 0x00000000, NV01_NULL_CLASS
,
192 NULL
, 0, &nctx
->hw
.null
);
194 nouveau_error("Error allocating NULL object\n");
198 /* Enable any supported extensions. */
199 ctx
->Extensions
.EXT_blend_color
= true;
200 ctx
->Extensions
.EXT_blend_minmax
= true;
201 ctx
->Extensions
.EXT_fog_coord
= true;
202 ctx
->Extensions
.EXT_framebuffer_blit
= true;
203 ctx
->Extensions
.EXT_framebuffer_object
= true;
204 ctx
->Extensions
.EXT_packed_depth_stencil
= true;
205 ctx
->Extensions
.EXT_secondary_color
= true;
206 ctx
->Extensions
.EXT_texture_filter_anisotropic
= true;
207 ctx
->Extensions
.NV_blend_square
= true;
208 ctx
->Extensions
.NV_texture_env_combine4
= true;
214 nouveau_context_deinit(struct gl_context
*ctx
)
216 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
218 if (TNL_CONTEXT(ctx
))
219 _tnl_DestroyContext(ctx
);
221 if (vbo_context(ctx
))
222 _vbo_DestroyContext(ctx
);
224 if (SWRAST_CONTEXT(ctx
))
225 _swrast_DestroyContext(ctx
);
228 _mesa_meta_free(ctx
);
230 nouveau_bufctx_del(&nctx
->hw
.bufctx
);
231 nouveau_pushbuf_del(&nctx
->hw
.pushbuf
);
232 nouveau_client_del(&nctx
->hw
.client
);
233 nouveau_object_del(&nctx
->hw
.chan
);
235 nouveau_scratch_destroy(ctx
);
236 _mesa_free_context_data(ctx
);
240 nouveau_context_destroy(__DRIcontext
*dri_ctx
)
242 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
243 struct gl_context
*ctx
= &nctx
->base
;
245 nouveau_bo_ref(NULL
, &nctx
->fence
);
246 context_drv(ctx
)->context_destroy(ctx
);
250 nouveau_update_renderbuffers(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
)
252 struct gl_context
*ctx
= dri_ctx
->driverPrivate
;
253 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
254 __DRIscreen
*screen
= dri_ctx
->driScreenPriv
;
255 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
256 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
257 unsigned int attachments
[10];
258 __DRIbuffer
*buffers
= NULL
;
259 int i
= 0, count
, ret
;
261 if (draw
->lastStamp
== draw
->dri2
.stamp
)
263 draw
->lastStamp
= draw
->dri2
.stamp
;
266 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
267 if (fb
->Visual
.doubleBufferMode
)
268 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
269 if (fb
->Visual
.haveDepthBuffer
&& fb
->Visual
.haveStencilBuffer
)
270 attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
271 else if (fb
->Visual
.haveDepthBuffer
)
272 attachments
[i
++] = __DRI_BUFFER_DEPTH
;
273 else if (fb
->Visual
.haveStencilBuffer
)
274 attachments
[i
++] = __DRI_BUFFER_STENCIL
;
276 buffers
= (*screen
->dri2
.loader
->getBuffers
)(draw
, &draw
->w
, &draw
->h
,
277 attachments
, i
, &count
,
278 draw
->loaderPrivate
);
282 for (i
= 0; i
< count
; i
++) {
283 struct gl_renderbuffer
*rb
;
284 struct nouveau_surface
*s
;
288 switch (buffers
[i
].attachment
) {
289 case __DRI_BUFFER_FRONT_LEFT
:
290 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
291 index
= BUFFER_FRONT_LEFT
;
293 case __DRI_BUFFER_BACK_LEFT
:
294 index
= BUFFER_BACK_LEFT
;
296 case __DRI_BUFFER_DEPTH
:
297 case __DRI_BUFFER_DEPTH_STENCIL
:
298 index
= BUFFER_DEPTH
;
300 case __DRI_BUFFER_STENCIL
:
301 index
= BUFFER_STENCIL
;
307 rb
= fb
->Attachment
[index
].Renderbuffer
;
308 s
= &to_nouveau_renderbuffer(rb
)->surface
;
312 s
->pitch
= buffers
[i
].pitch
;
313 s
->cpp
= buffers
[i
].cpp
;
315 if (index
== BUFFER_DEPTH
&& s
->bo
) {
316 ret
= nouveau_bo_name_get(s
->bo
, &old_name
);
318 * Disable fast Z clears in the next frame, the
319 * depth buffer contents are undefined.
321 if (!ret
&& old_name
!= buffers
[i
].name
)
322 nctx
->hierz
.clear_seq
= 0;
325 nouveau_bo_ref(NULL
, &s
->bo
);
326 ret
= nouveau_bo_name_ref(context_dev(ctx
),
327 buffers
[i
].name
, &s
->bo
);
331 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
335 update_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
338 struct gl_context
*ctx
= dri_ctx
->driverPrivate
;
339 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
341 *stamp
= draw
->dri2
.stamp
;
343 nouveau_update_renderbuffers(dri_ctx
, draw
);
344 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
346 /* Clean up references to the old framebuffer objects. */
347 context_dirty(ctx
, FRAMEBUFFER
);
348 nouveau_bufctx_reset(to_nouveau_context(ctx
)->hw
.bufctx
, BUFCTX_FB
);
349 PUSH_KICK(context_push(ctx
));
353 nouveau_context_make_current(__DRIcontext
*dri_ctx
, __DRIdrawable
*dri_draw
,
354 __DRIdrawable
*dri_read
)
357 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
358 struct gl_context
*ctx
= &nctx
->base
;
360 /* Ask the X server for new renderbuffers. */
361 if (dri_draw
->driverPrivate
!= ctx
->WinSysDrawBuffer
)
362 update_framebuffer(dri_ctx
, dri_draw
,
363 &dri_ctx
->dri2
.draw_stamp
);
365 if (dri_draw
!= dri_read
&&
366 dri_read
->driverPrivate
!= ctx
->WinSysReadBuffer
)
367 update_framebuffer(dri_ctx
, dri_read
,
368 &dri_ctx
->dri2
.read_stamp
);
370 /* Pass it down to mesa. */
371 _mesa_make_current(ctx
, dri_draw
->driverPrivate
,
372 dri_read
->driverPrivate
);
373 _mesa_update_state(ctx
);
376 _mesa_make_current(NULL
, NULL
, NULL
);
383 nouveau_context_unbind(__DRIcontext
*dri_ctx
)
385 /* Unset current context and dispatch table */
386 _mesa_make_current(NULL
, NULL
, NULL
);
392 nouveau_fallback(struct gl_context
*ctx
, enum nouveau_fallback mode
)
394 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
396 nctx
->fallback
= MAX2(HWTNL
, mode
);
399 nouveau_state_emit(ctx
);
401 nouveau_bo_state_emit(ctx
);
404 PUSH_KICK(context_push(ctx
));
409 validate_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
412 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
413 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
414 GLboolean need_front
=
415 (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
||
416 fb
->_ColorReadBufferIndex
== BUFFER_FRONT_LEFT
);
418 if (nfb
->need_front
!= need_front
) {
419 nfb
->need_front
= need_front
;
420 dri2InvalidateDrawable(draw
);
423 if (draw
->dri2
.stamp
!= *stamp
)
424 update_framebuffer(dri_ctx
, draw
, stamp
);
428 nouveau_validate_framebuffer(struct gl_context
*ctx
)
430 __DRIcontext
*dri_ctx
= to_nouveau_context(ctx
)->dri_context
;
431 __DRIdrawable
*dri_draw
= dri_ctx
->driDrawablePriv
;
432 __DRIdrawable
*dri_read
= dri_ctx
->driReadablePriv
;
434 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
))
435 validate_framebuffer(dri_ctx
, dri_draw
,
436 &dri_ctx
->dri2
.draw_stamp
);
438 if (_mesa_is_winsys_fbo(ctx
->ReadBuffer
))
439 validate_framebuffer(dri_ctx
, dri_read
,
440 &dri_ctx
->dri2
.read_stamp
);
442 if (ctx
->NewState
& _NEW_BUFFERS
)
443 _mesa_update_state(ctx
);