nvfx: properly unreference bound objects on context destruction
[mesa.git] / src / gallium / drivers / nvfx / nvfx_context.c
1 #include "draw/draw_context.h"
2 #include "pipe/p_defines.h"
3 #include "util/u_framebuffer.h"
4
5 #include "nvfx_context.h"
6 #include "nvfx_screen.h"
7 #include "nvfx_resource.h"
8
9 static void
10 nvfx_flush(struct pipe_context *pipe, unsigned flags,
11 struct pipe_fence_handle **fence)
12 {
13 struct nvfx_context *nvfx = nvfx_context(pipe);
14 struct nvfx_screen *screen = nvfx->screen;
15 struct nouveau_channel *chan = screen->base.channel;
16 struct nouveau_grobj *eng3d = screen->eng3d;
17
18 if (flags & PIPE_FLUSH_TEXTURE_CACHE) {
19 BEGIN_RING(chan, eng3d, 0x1fd8, 1);
20 OUT_RING (chan, 2);
21 BEGIN_RING(chan, eng3d, 0x1fd8, 1);
22 OUT_RING (chan, 1);
23 }
24
25 FIRE_RING(chan);
26 if (fence)
27 *fence = NULL;
28 }
29
30 static void
31 nvfx_destroy(struct pipe_context *pipe)
32 {
33 struct nvfx_context *nvfx = nvfx_context(pipe);
34
35 for(unsigned i = 0; i < nvfx->vtxbuf_nr; ++i)
36 pipe_resource_reference(&nvfx->vtxbuf[i].buffer, 0);
37 pipe_resource_reference(&nvfx->idxbuf.buffer, 0);
38 util_unreference_framebuffer_state(&nvfx->framebuffer);
39 for(unsigned i = 0; i < PIPE_MAX_SAMPLERS; ++i)
40 pipe_sampler_view_reference(&nvfx->fragment_sampler_views[i], 0);
41
42 if (nvfx->draw)
43 draw_destroy(nvfx->draw);
44 FREE(nvfx);
45 }
46
47 struct pipe_context *
48 nvfx_create(struct pipe_screen *pscreen, void *priv)
49 {
50 struct nvfx_screen *screen = nvfx_screen(pscreen);
51 struct pipe_winsys *ws = pscreen->winsys;
52 struct nvfx_context *nvfx;
53 struct nouveau_winsys *nvws = screen->nvws;
54
55 nvfx = CALLOC(1, sizeof(struct nvfx_context));
56 if (!nvfx)
57 return NULL;
58 nvfx->screen = screen;
59
60 nvfx->nvws = nvws;
61
62 nvfx->pipe.winsys = ws;
63 nvfx->pipe.screen = pscreen;
64 nvfx->pipe.priv = priv;
65 nvfx->pipe.destroy = nvfx_destroy;
66 nvfx->pipe.draw_vbo = nvfx_draw_vbo;
67 nvfx->pipe.clear = nvfx_clear;
68 nvfx->pipe.flush = nvfx_flush;
69
70 screen->base.channel->user_private = nvfx;
71
72 nvfx->is_nv4x = screen->is_nv4x;
73
74 nvfx_init_query_functions(nvfx);
75 nvfx_init_surface_functions(nvfx);
76 nvfx_init_state_functions(nvfx);
77 nvfx_init_resource_functions(&nvfx->pipe);
78
79 /* Create, configure, and install fallback swtnl path */
80 nvfx->draw = draw_create(&nvfx->pipe);
81 draw_wide_point_threshold(nvfx->draw, 9999999.0);
82 draw_wide_line_threshold(nvfx->draw, 9999999.0);
83 draw_enable_line_stipple(nvfx->draw, FALSE);
84 draw_enable_point_sprites(nvfx->draw, FALSE);
85 draw_set_rasterize_stage(nvfx->draw, nvfx_draw_render_stage(nvfx));
86
87 /* set these to that we init them on first validation */
88 nvfx->state.scissor_enabled = ~0;
89 nvfx->state.stipple_enabled = ~0;
90 return &nvfx->pipe;
91 }