nvc0: implement queries
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_context.c
1 /*
2 * Copyright 2010 Christoph Bumiller
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "draw/draw_context.h"
24 #include "pipe/p_defines.h"
25
26 #include "nvc0_context.h"
27 #include "nvc0_screen.h"
28 #include "nvc0_resource.h"
29
30 #include "nouveau/nouveau_reloc.h"
31
32 static void
33 nvc0_flush(struct pipe_context *pipe, unsigned flags,
34 struct pipe_fence_handle **fence)
35 {
36 struct nvc0_context *nvc0 = nvc0_context(pipe);
37 struct nouveau_channel *chan = nvc0->screen->base.channel;
38
39 if (flags & PIPE_FLUSH_TEXTURE_CACHE) {
40 BEGIN_RING(chan, RING_3D(SERIALIZE), 1);
41 OUT_RING (chan, 0);
42 BEGIN_RING(chan, RING_3D(TEX_CACHE_CTL), 1);
43 OUT_RING (chan, 0x00);
44 }
45
46 if (fence) {
47 nvc0_screen_fence_new(nvc0->screen, (struct nvc0_fence **)fence, TRUE);
48 }
49
50 if (flags & (PIPE_FLUSH_SWAPBUFFERS | PIPE_FLUSH_FRAME)) {
51 FIRE_RING(chan);
52
53 nvc0_screen_fence_next(nvc0->screen);
54 }
55 }
56
57 static void
58 nvc0_destroy(struct pipe_context *pipe)
59 {
60 struct nvc0_context *nvc0 = nvc0_context(pipe);
61
62 draw_destroy(nvc0->draw);
63
64 if (nvc0->screen->cur_ctx == nvc0)
65 nvc0->screen->cur_ctx = NULL;
66
67 FREE(nvc0);
68 }
69
70 struct pipe_context *
71 nvc0_create(struct pipe_screen *pscreen, void *priv)
72 {
73 struct pipe_winsys *pipe_winsys = pscreen->winsys;
74 struct nvc0_screen *screen = nvc0_screen(pscreen);
75 struct nvc0_context *nvc0;
76
77 nvc0 = CALLOC_STRUCT(nvc0_context);
78 if (!nvc0)
79 return NULL;
80 nvc0->screen = screen;
81
82 nvc0->pipe.winsys = pipe_winsys;
83 nvc0->pipe.screen = pscreen;
84 nvc0->pipe.priv = priv;
85
86 nvc0->pipe.destroy = nvc0_destroy;
87
88 nvc0->pipe.draw_vbo = nvc0_draw_vbo;
89 nvc0->pipe.clear = nvc0_clear;
90
91 nvc0->pipe.flush = nvc0_flush;
92
93 screen->base.channel->user_private = nvc0;
94
95 nvc0_init_query_functions(nvc0);
96 nvc0_init_surface_functions(nvc0);
97 nvc0_init_state_functions(nvc0);
98 nvc0_init_resource_functions(&nvc0->pipe);
99
100 nvc0->draw = draw_create(&nvc0->pipe);
101 assert(nvc0->draw);
102 draw_set_rasterize_stage(nvc0->draw, nvc0_draw_render_stage(nvc0));
103
104 return &nvc0->pipe;
105 }
106
107 struct resident {
108 struct nvc0_resource *res;
109 uint32_t flags;
110 };
111
112 void
113 nvc0_bufctx_add_resident(struct nvc0_context *nvc0, int ctx,
114 struct nvc0_resource *resource, uint32_t flags)
115 {
116 struct resident rsd = { resource, flags };
117
118 if (!resource->bo)
119 return;
120
121 /* We don't need to reference the resource here, it will be referenced
122 * in the context/state, and bufctx will be reset when state changes.
123 */
124 util_dynarray_append(&nvc0->residents[ctx], struct resident, rsd);
125 }
126
127 void
128 nvc0_bufctx_del_resident(struct nvc0_context *nvc0, int ctx,
129 struct nvc0_resource *resource)
130 {
131 struct resident *rsd, *top;
132 unsigned i;
133
134 for (i = 0; i < nvc0->residents[ctx].size / sizeof(struct resident); ++i) {
135 rsd = util_dynarray_element(&nvc0->residents[ctx], struct resident, i);
136
137 if (rsd->res == resource) {
138 top = util_dynarray_pop_ptr(&nvc0->residents[ctx], struct resident);
139 if (rsd != top)
140 *rsd = *top;
141 break;
142 }
143 }
144 }
145
146 void
147 nvc0_bufctx_emit_relocs(struct nvc0_context *nvc0)
148 {
149 struct resident *rsd;
150 struct util_dynarray *array;
151 unsigned ctx, i;
152
153 for (ctx = 0; ctx < NVC0_BUFCTX_COUNT; ++ctx) {
154 array = &nvc0->residents[ctx];
155
156 for (i = 0; i < array->size / sizeof(struct resident); ++i) {
157 rsd = util_dynarray_element(array, struct resident, i);
158
159 nvc0_resource_validate(rsd->res, rsd->flags);
160 }
161 }
162
163 nvc0_screen_make_buffers_resident(nvc0->screen);
164 }