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.
29 #include "nouveau_driver.h"
30 #include "nouveau_context.h"
31 #include "nouveau_bufferobj.h"
32 #include "nouveau_fbo.h"
33 #include "nv_object.xml.h"
35 #include "main/api_exec.h"
37 #include "main/framebuffer.h"
38 #include "main/fbobject.h"
39 #include "main/light.h"
40 #include "main/state.h"
41 #include "main/version.h"
42 #include "main/vtxfmt.h"
43 #include "drivers/common/meta.h"
44 #include "drivers/common/driverfuncs.h"
45 #include "swrast/swrast.h"
46 #include "swrast/s_context.h"
49 #include "tnl/t_context.h"
52 nouveau_context_create(gl_api api
,
53 const struct gl_config
*visual
, __DRIcontext
*dri_ctx
,
54 const struct __DriverContextConfig
*ctx_config
,
58 __DRIscreen
*dri_screen
= dri_ctx
->driScreenPriv
;
59 struct nouveau_screen
*screen
= dri_screen
->driverPrivate
;
60 struct nouveau_context
*nctx
;
61 struct gl_context
*ctx
;
63 if (ctx_config
->flags
& ~(__DRI_CTX_FLAG_DEBUG
| __DRI_CTX_FLAG_NO_ERROR
)) {
64 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
68 if (ctx_config
->attribute_mask
) {
69 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
73 ctx
= screen
->driver
->context_create(screen
, api
, visual
, share_ctx
);
75 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
79 driContextSetFlags(ctx
, ctx_config
->flags
);
81 nctx
= to_nouveau_context(ctx
);
82 nctx
->dri_context
= dri_ctx
;
83 dri_ctx
->driverPrivate
= ctx
;
85 _mesa_compute_version(ctx
);
86 if (ctx
->Version
< (ctx_config
->major_version
* 10 +
87 ctx_config
->minor_version
)) {
88 nouveau_context_destroy(dri_ctx
);
89 *error
= __DRI_CTX_ERROR_BAD_VERSION
;
93 /* Exec table initialization requires the version to be computed */
94 _mesa_initialize_dispatch_tables(ctx
);
95 _mesa_initialize_vbo_vtxfmt(ctx
);
97 if (nouveau_bo_new(context_dev(ctx
), NOUVEAU_BO_VRAM
, 0, 4096,
98 NULL
, &nctx
->fence
)) {
99 nouveau_context_destroy(dri_ctx
);
100 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
104 *error
= __DRI_CTX_ERROR_SUCCESS
;
109 nouveau_context_init(struct gl_context
*ctx
, gl_api api
,
110 struct nouveau_screen
*screen
,
111 const struct gl_config
*visual
, struct gl_context
*share_ctx
)
113 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
114 struct dd_function_table functions
;
117 nctx
->screen
= screen
;
118 nctx
->fallback
= HWTNL
;
120 /* Initialize the function pointers. */
121 _mesa_init_driver_functions(&functions
);
122 nouveau_driver_functions_init(&functions
);
123 nouveau_bufferobj_functions_init(&functions
);
124 nouveau_texture_functions_init(&functions
);
125 nouveau_fbo_functions_init(&functions
);
127 /* Initialize the mesa context. */
128 if (!_mesa_initialize_context(ctx
, api
, visual
, share_ctx
, &functions
))
131 nouveau_state_init(ctx
);
132 nouveau_scratch_init(ctx
);
133 _mesa_meta_init(ctx
);
134 _swrast_CreateContext(ctx
);
135 _vbo_CreateContext(ctx
);
136 _tnl_CreateContext(ctx
);
137 nouveau_span_functions_init(ctx
);
138 _mesa_allow_light_in_model(ctx
, GL_FALSE
);
140 /* Allocate a hardware channel. */
141 ret
= nouveau_object_new(&context_dev(ctx
)->object
, 0xbeef0000,
142 NOUVEAU_FIFO_CHANNEL_CLASS
,
146 }, sizeof(struct nv04_fifo
), &nctx
->hw
.chan
);
148 nouveau_error("Error initializing the FIFO.\n");
152 /* Allocate a client (thread data) */
153 ret
= nouveau_client_new(context_dev(ctx
), &nctx
->hw
.client
);
155 nouveau_error("Error creating thread data\n");
159 /* Allocate a push buffer */
160 ret
= nouveau_pushbuf_new(nctx
->hw
.client
, nctx
->hw
.chan
, 4,
161 512 * 1024, true, &nctx
->hw
.pushbuf
);
163 nouveau_error("Error allocating DMA push buffer\n");
167 /* Allocate buffer context */
168 ret
= nouveau_bufctx_new(nctx
->hw
.client
, 16, &nctx
->hw
.bufctx
);
170 nouveau_error("Error allocating buffer context\n");
174 nctx
->hw
.pushbuf
->user_priv
= nctx
->hw
.bufctx
;
176 /* Allocate NULL object */
177 ret
= nouveau_object_new(nctx
->hw
.chan
, 0x00000000, NV01_NULL_CLASS
,
178 NULL
, 0, &nctx
->hw
.null
);
180 nouveau_error("Error allocating NULL object\n");
184 /* Enable any supported extensions. */
185 ctx
->Extensions
.EXT_blend_color
= true;
186 ctx
->Extensions
.EXT_blend_minmax
= true;
187 ctx
->Extensions
.EXT_texture_filter_anisotropic
= true;
188 ctx
->Extensions
.NV_texture_env_combine4
= true;
189 ctx
->Const
.MaxDrawBuffers
= ctx
->Const
.MaxColorAttachments
= 1;
191 /* This effectively disables 3D textures */
192 ctx
->Const
.Max3DTextureLevels
= 1;
198 nouveau_context_deinit(struct gl_context
*ctx
)
200 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
202 if (TNL_CONTEXT(ctx
))
203 _tnl_DestroyContext(ctx
);
205 _vbo_DestroyContext(ctx
);
207 if (SWRAST_CONTEXT(ctx
))
208 _swrast_DestroyContext(ctx
);
211 _mesa_meta_free(ctx
);
213 nouveau_bufctx_del(&nctx
->hw
.bufctx
);
214 nouveau_pushbuf_del(&nctx
->hw
.pushbuf
);
215 nouveau_client_del(&nctx
->hw
.client
);
216 nouveau_object_del(&nctx
->hw
.chan
);
218 nouveau_scratch_destroy(ctx
);
219 _mesa_free_context_data(ctx
);
223 nouveau_context_destroy(__DRIcontext
*dri_ctx
)
225 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
226 struct gl_context
*ctx
= &nctx
->base
;
228 nouveau_bo_ref(NULL
, &nctx
->fence
);
229 context_drv(ctx
)->context_destroy(ctx
);
233 nouveau_update_renderbuffers(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
)
235 struct gl_context
*ctx
= dri_ctx
->driverPrivate
;
236 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
237 __DRIscreen
*screen
= dri_ctx
->driScreenPriv
;
238 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
239 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
240 unsigned int attachments
[10];
241 __DRIbuffer
*buffers
= NULL
;
242 int i
= 0, count
, ret
;
244 if (draw
->lastStamp
== draw
->dri2
.stamp
)
246 draw
->lastStamp
= draw
->dri2
.stamp
;
249 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
250 if (fb
->Visual
.doubleBufferMode
)
251 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
252 if (fb
->Visual
.haveDepthBuffer
&& fb
->Visual
.haveStencilBuffer
)
253 attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
254 else if (fb
->Visual
.haveDepthBuffer
)
255 attachments
[i
++] = __DRI_BUFFER_DEPTH
;
256 else if (fb
->Visual
.haveStencilBuffer
)
257 attachments
[i
++] = __DRI_BUFFER_STENCIL
;
259 buffers
= screen
->dri2
.loader
->getBuffers(draw
, &draw
->w
, &draw
->h
,
260 attachments
, i
, &count
,
261 draw
->loaderPrivate
);
265 for (i
= 0; i
< count
; i
++) {
266 struct gl_renderbuffer
*rb
;
267 struct nouveau_surface
*s
;
271 switch (buffers
[i
].attachment
) {
272 case __DRI_BUFFER_FRONT_LEFT
:
273 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
274 index
= BUFFER_FRONT_LEFT
;
276 case __DRI_BUFFER_BACK_LEFT
:
277 index
= BUFFER_BACK_LEFT
;
279 case __DRI_BUFFER_DEPTH
:
280 case __DRI_BUFFER_DEPTH_STENCIL
:
281 index
= BUFFER_DEPTH
;
283 case __DRI_BUFFER_STENCIL
:
284 index
= BUFFER_STENCIL
;
290 rb
= fb
->Attachment
[index
].Renderbuffer
;
291 s
= &to_nouveau_renderbuffer(rb
)->surface
;
295 s
->pitch
= buffers
[i
].pitch
;
296 s
->cpp
= buffers
[i
].cpp
;
298 if (index
== BUFFER_DEPTH
&& s
->bo
) {
299 ret
= nouveau_bo_name_get(s
->bo
, &old_name
);
301 * Disable fast Z clears in the next frame, the
302 * depth buffer contents are undefined.
304 if (!ret
&& old_name
!= buffers
[i
].name
)
305 nctx
->hierz
.clear_seq
= 0;
308 nouveau_bo_ref(NULL
, &s
->bo
);
309 ret
= nouveau_bo_name_ref(context_dev(ctx
),
310 buffers
[i
].name
, &s
->bo
);
314 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
318 update_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
321 struct gl_context
*ctx
= dri_ctx
->driverPrivate
;
322 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
324 *stamp
= draw
->dri2
.stamp
;
326 nouveau_update_renderbuffers(dri_ctx
, draw
);
327 _mesa_resize_framebuffer(ctx
, fb
, draw
->w
, draw
->h
);
329 /* Clean up references to the old framebuffer objects. */
330 context_dirty(ctx
, FRAMEBUFFER
);
331 nouveau_bufctx_reset(to_nouveau_context(ctx
)->hw
.bufctx
, BUFCTX_FB
);
332 PUSH_KICK(context_push(ctx
));
336 nouveau_context_make_current(__DRIcontext
*dri_ctx
, __DRIdrawable
*dri_draw
,
337 __DRIdrawable
*dri_read
)
340 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
341 struct gl_context
*ctx
= &nctx
->base
;
343 /* Ask the X server for new renderbuffers. */
344 if (dri_draw
->driverPrivate
!= ctx
->WinSysDrawBuffer
)
345 update_framebuffer(dri_ctx
, dri_draw
,
346 &dri_ctx
->dri2
.draw_stamp
);
348 if (dri_draw
!= dri_read
&&
349 dri_read
->driverPrivate
!= ctx
->WinSysReadBuffer
)
350 update_framebuffer(dri_ctx
, dri_read
,
351 &dri_ctx
->dri2
.read_stamp
);
353 /* Pass it down to mesa. */
354 _mesa_make_current(ctx
, dri_draw
->driverPrivate
,
355 dri_read
->driverPrivate
);
356 _mesa_update_state(ctx
);
359 _mesa_make_current(NULL
, NULL
, NULL
);
366 nouveau_context_unbind(__DRIcontext
*dri_ctx
)
368 /* Unset current context and dispatch table */
369 _mesa_make_current(NULL
, NULL
, NULL
);
375 nouveau_fallback(struct gl_context
*ctx
, enum nouveau_fallback mode
)
377 struct nouveau_context
*nctx
= to_nouveau_context(ctx
);
379 nctx
->fallback
= MAX2(HWTNL
, mode
);
382 nouveau_state_emit(ctx
);
384 nouveau_bo_state_emit(ctx
);
387 PUSH_KICK(context_push(ctx
));
392 validate_framebuffer(__DRIcontext
*dri_ctx
, __DRIdrawable
*draw
,
395 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
396 struct nouveau_framebuffer
*nfb
= to_nouveau_framebuffer(fb
);
397 GLboolean need_front
=
398 (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
||
399 fb
->_ColorReadBufferIndex
== BUFFER_FRONT_LEFT
);
401 if (nfb
->need_front
!= need_front
) {
402 nfb
->need_front
= need_front
;
403 dri2InvalidateDrawable(draw
);
406 if (draw
->dri2
.stamp
!= *stamp
)
407 update_framebuffer(dri_ctx
, draw
, stamp
);
411 nouveau_validate_framebuffer(struct gl_context
*ctx
)
413 __DRIcontext
*dri_ctx
= to_nouveau_context(ctx
)->dri_context
;
414 __DRIdrawable
*dri_draw
= dri_ctx
->driDrawablePriv
;
415 __DRIdrawable
*dri_read
= dri_ctx
->driReadablePriv
;
417 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
))
418 validate_framebuffer(dri_ctx
, dri_draw
,
419 &dri_ctx
->dri2
.draw_stamp
);
421 if (_mesa_is_winsys_fbo(ctx
->ReadBuffer
))
422 validate_framebuffer(dri_ctx
, dri_read
,
423 &dri_ctx
->dri2
.read_stamp
);
425 if (ctx
->NewState
& _NEW_BUFFERS
)
426 _mesa_update_state(ctx
);