Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / nv50 / nv50_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 "nv50_context.h"
27 #include "nv50_screen.h"
28 #include "nv50_resource.h"
29
30 #include "nouveau/nouveau_reloc.h"
31
32 static void
33 nv50_flush(struct pipe_context *pipe,
34            struct pipe_fence_handle **fence)
35 {
36    struct nouveau_screen *screen = &nv50_context(pipe)->screen->base;
37
38    if (fence)
39       nouveau_fence_ref(screen->fence.current, (struct nouveau_fence **)fence);
40
41    /* Try to emit before firing to avoid having to flush again right after
42     * in case we have to wait on this fence.
43     */
44    nouveau_fence_emit(screen->fence.current);
45
46    FIRE_RING(screen->channel);
47 }
48
49 static void
50 nv50_texture_barrier(struct pipe_context *pipe)
51 {
52    struct nouveau_channel *chan = nv50_context(pipe)->screen->base.channel;
53
54    BEGIN_RING(chan, RING_3D(SERIALIZE), 1);
55    OUT_RING  (chan, 0);
56    BEGIN_RING(chan, RING_3D(TEX_CACHE_CTL), 1);
57    OUT_RING  (chan, 0x20);
58 }
59
60 void
61 nv50_default_flush_notify(struct nouveau_channel *chan)
62 {
63    struct nv50_context *nv50 = chan->user_private;
64
65    if (!nv50)
66       return;
67
68    nouveau_fence_update(&nv50->screen->base, TRUE);
69    nouveau_fence_next(&nv50->screen->base);
70 }
71
72 static void
73 nv50_context_unreference_resources(struct nv50_context *nv50)
74 {
75    unsigned s, i;
76
77    for (i = 0; i < NV50_BUFCTX_COUNT; ++i)
78       nv50_bufctx_reset(nv50, i);
79
80    for (i = 0; i < nv50->num_vtxbufs; ++i)
81       pipe_resource_reference(&nv50->vtxbuf[i].buffer, NULL);
82
83    pipe_resource_reference(&nv50->idxbuf.buffer, NULL);
84
85    for (s = 0; s < 3; ++s) {
86       for (i = 0; i < nv50->num_textures[s]; ++i)
87          pipe_sampler_view_reference(&nv50->textures[s][i], NULL);
88
89       for (i = 0; i < 16; ++i)
90          pipe_resource_reference(&nv50->constbuf[s][i], NULL);
91    }
92 }
93
94 static void
95 nv50_destroy(struct pipe_context *pipe)
96 {
97    struct nv50_context *nv50 = nv50_context(pipe);
98
99    nv50_context_unreference_resources(nv50);
100
101    draw_destroy(nv50->draw);
102
103    if (nv50->screen->cur_ctx == nv50) {
104       nv50->screen->base.channel->user_private = NULL;
105       nv50->screen->cur_ctx = NULL;
106    }
107
108    FREE(nv50);
109 }
110
111 struct pipe_context *
112 nv50_create(struct pipe_screen *pscreen, void *priv)
113 {
114    struct pipe_winsys *pipe_winsys = pscreen->winsys;
115    struct nv50_screen *screen = nv50_screen(pscreen);
116    struct nv50_context *nv50;
117    struct pipe_context *pipe;
118
119    nv50 = CALLOC_STRUCT(nv50_context);
120    if (!nv50)
121       return NULL;
122    pipe = &nv50->base.pipe;
123
124    nv50->screen = screen;
125    nv50->base.screen    = &screen->base;
126    nv50->base.copy_data = nv50_m2mf_copy_linear;
127    nv50->base.push_data = nv50_sifc_linear_u8;
128
129    pipe->winsys = pipe_winsys;
130    pipe->screen = pscreen;
131    pipe->priv = priv;
132
133    pipe->destroy = nv50_destroy;
134
135    pipe->draw_vbo = nv50_draw_vbo;
136    pipe->clear = nv50_clear;
137
138    pipe->flush = nv50_flush;
139    pipe->texture_barrier = nv50_texture_barrier;
140
141    if (!screen->cur_ctx)
142       screen->cur_ctx = nv50;
143    screen->base.channel->user_private = nv50;
144    screen->base.channel->flush_notify = nv50_default_flush_notify;
145
146    nv50_init_query_functions(nv50);
147    nv50_init_surface_functions(nv50);
148    nv50_init_state_functions(nv50);
149    nv50_init_resource_functions(pipe);
150
151    nv50->draw = draw_create(pipe);
152    assert(nv50->draw);
153    draw_set_rasterize_stage(nv50->draw, nv50_draw_render_stage(nv50));
154
155    return pipe;
156 }
157
158 struct resident {
159    struct nv04_resource *res;
160    uint32_t flags;
161 };
162
163 void
164 nv50_bufctx_add_resident(struct nv50_context *nv50, int ctx,
165                          struct nv04_resource *resource, uint32_t flags)
166 {
167    struct resident rsd = { resource, flags };
168
169    if (!resource->bo)
170       return;
171    nv50->residents_size += sizeof(struct resident);
172
173    /* We don't need to reference the resource here, it will be referenced
174     * in the context/state, and bufctx will be reset when state changes.
175     */
176    util_dynarray_append(&nv50->residents[ctx], struct resident, rsd);
177 }
178
179 void
180 nv50_bufctx_del_resident(struct nv50_context *nv50, int ctx,
181                          struct nv04_resource *resource)
182 {
183    struct resident *rsd, *top;
184    unsigned i;
185
186    for (i = 0; i < nv50->residents[ctx].size / sizeof(struct resident); ++i) {
187       rsd = util_dynarray_element(&nv50->residents[ctx], struct resident, i);
188
189       if (rsd->res == resource) {
190          top = util_dynarray_pop_ptr(&nv50->residents[ctx], struct resident);
191          if (rsd != top)
192             *rsd = *top;
193          nv50->residents_size -= sizeof(struct resident);
194          break;
195       }
196    }
197 }
198
199 void
200 nv50_bufctx_emit_relocs(struct nv50_context *nv50)
201 {
202    struct resident *rsd;
203    struct util_dynarray *array;
204    unsigned ctx, i, n;
205
206    n  = nv50->residents_size / sizeof(struct resident);
207    n += NV50_SCREEN_RESIDENT_BO_COUNT;
208
209    MARK_RING(nv50->screen->base.channel, n, n);
210
211    for (ctx = 0; ctx < NV50_BUFCTX_COUNT; ++ctx) {
212       array = &nv50->residents[ctx];
213
214       n = array->size / sizeof(struct resident);
215       for (i = 0; i < n; ++i) {
216          rsd = util_dynarray_element(array, struct resident, i);
217
218          nv50_resource_validate(rsd->res, rsd->flags);
219       }
220    }
221
222    nv50_screen_make_buffers_resident(nv50->screen);
223 }