blob: ebdf5f2f9e8e2c6214a6928b117e2a4a8507163d [file] [log] [blame]
Christoph Bumiller4c224752010-11-12 15:17:40 +01001/*
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
32static void
33nvc0_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);
Christoph Bumillerb6e31302011-02-09 15:01:23 +010044 } else
45 if ((flags & PIPE_FLUSH_RENDER_CACHE) && !(flags & PIPE_FLUSH_FRAME)) {
46 BEGIN_RING(chan, RING_3D(SERIALIZE), 1);
47 OUT_RING (chan, 0);
Christoph Bumiller4c224752010-11-12 15:17:40 +010048 }
49
Christoph Bumillera6ea37d2011-02-20 17:57:47 +010050 if (fence)
51 nvc0_fence_reference((struct nvc0_fence **)fence,
52 nvc0->screen->fence.current);
Christoph Bumiller4c224752010-11-12 15:17:40 +010053
Christoph Bumillera6ea37d2011-02-20 17:57:47 +010054 if (flags & (PIPE_FLUSH_SWAPBUFFERS | PIPE_FLUSH_FRAME))
Christoph Bumiller4c224752010-11-12 15:17:40 +010055 FIRE_RING(chan);
Christoph Bumiller4c224752010-11-12 15:17:40 +010056}
57
58static void
59nvc0_destroy(struct pipe_context *pipe)
60{
61 struct nvc0_context *nvc0 = nvc0_context(pipe);
62
63 draw_destroy(nvc0->draw);
64
65 if (nvc0->screen->cur_ctx == nvc0)
66 nvc0->screen->cur_ctx = NULL;
67
68 FREE(nvc0);
69}
70
Christoph Bumillera6ea37d2011-02-20 17:57:47 +010071void
72nvc0_default_flush_notify(struct nouveau_channel *chan)
73{
74 struct nvc0_context *nvc0 = chan->user_private;
75
76 nvc0_screen_fence_update(nvc0->screen, TRUE);
77
78 nvc0_screen_fence_next(nvc0->screen);
79}
80
Christoph Bumiller4c224752010-11-12 15:17:40 +010081struct pipe_context *
82nvc0_create(struct pipe_screen *pscreen, void *priv)
83{
84 struct pipe_winsys *pipe_winsys = pscreen->winsys;
85 struct nvc0_screen *screen = nvc0_screen(pscreen);
86 struct nvc0_context *nvc0;
87
88 nvc0 = CALLOC_STRUCT(nvc0_context);
89 if (!nvc0)
90 return NULL;
91 nvc0->screen = screen;
92
93 nvc0->pipe.winsys = pipe_winsys;
94 nvc0->pipe.screen = pscreen;
95 nvc0->pipe.priv = priv;
96
97 nvc0->pipe.destroy = nvc0_destroy;
98
99 nvc0->pipe.draw_vbo = nvc0_draw_vbo;
100 nvc0->pipe.clear = nvc0_clear;
101
102 nvc0->pipe.flush = nvc0_flush;
103
Ben Skeggs48e191f2011-03-01 14:37:06 +1000104 if (!screen->cur_ctx)
105 screen->cur_ctx = nvc0;
Christoph Bumiller4c224752010-11-12 15:17:40 +0100106 screen->base.channel->user_private = nvc0;
Christoph Bumillera6ea37d2011-02-20 17:57:47 +0100107 screen->base.channel->flush_notify = nvc0_default_flush_notify;
Christoph Bumiller4c224752010-11-12 15:17:40 +0100108
Christoph Bumillerb3d8e1f2011-01-09 21:50:06 +0100109 nvc0_init_query_functions(nvc0);
Christoph Bumiller4c224752010-11-12 15:17:40 +0100110 nvc0_init_surface_functions(nvc0);
111 nvc0_init_state_functions(nvc0);
112 nvc0_init_resource_functions(&nvc0->pipe);
113
114 nvc0->draw = draw_create(&nvc0->pipe);
115 assert(nvc0->draw);
116 draw_set_rasterize_stage(nvc0->draw, nvc0_draw_render_stage(nvc0));
117
118 return &nvc0->pipe;
119}
120
121struct resident {
Christoph Bumillere4349022010-12-27 13:57:46 +0100122 struct nvc0_resource *res;
Christoph Bumiller4c224752010-11-12 15:17:40 +0100123 uint32_t flags;
124};
125
126void
127nvc0_bufctx_add_resident(struct nvc0_context *nvc0, int ctx,
128 struct nvc0_resource *resource, uint32_t flags)
129{
Christoph Bumillere4349022010-12-27 13:57:46 +0100130 struct resident rsd = { resource, flags };
Christoph Bumiller4c224752010-11-12 15:17:40 +0100131
132 if (!resource->bo)
133 return;
Christoph Bumiller4c224752010-11-12 15:17:40 +0100134
Christoph Bumillere4349022010-12-27 13:57:46 +0100135 /* We don't need to reference the resource here, it will be referenced
136 * in the context/state, and bufctx will be reset when state changes.
137 */
Christoph Bumiller4c224752010-11-12 15:17:40 +0100138 util_dynarray_append(&nvc0->residents[ctx], struct resident, rsd);
139}
140
141void
142nvc0_bufctx_del_resident(struct nvc0_context *nvc0, int ctx,
143 struct nvc0_resource *resource)
144{
Christoph Bumillere4349022010-12-27 13:57:46 +0100145 struct resident *rsd, *top;
Christoph Bumiller4c224752010-11-12 15:17:40 +0100146 unsigned i;
147
148 for (i = 0; i < nvc0->residents[ctx].size / sizeof(struct resident); ++i) {
149 rsd = util_dynarray_element(&nvc0->residents[ctx], struct resident, i);
150
Christoph Bumillere4349022010-12-27 13:57:46 +0100151 if (rsd->res == resource) {
152 top = util_dynarray_pop_ptr(&nvc0->residents[ctx], struct resident);
153 if (rsd != top)
154 *rsd = *top;
Christoph Bumiller4c224752010-11-12 15:17:40 +0100155 break;
156 }
157 }
158}
159
160void
Christoph Bumiller4c224752010-11-12 15:17:40 +0100161nvc0_bufctx_emit_relocs(struct nvc0_context *nvc0)
162{
163 struct resident *rsd;
164 struct util_dynarray *array;
Christoph Bumillerbf1df062011-01-21 16:52:17 +0100165 unsigned ctx, i, n;
Christoph Bumiller4c224752010-11-12 15:17:40 +0100166
167 for (ctx = 0; ctx < NVC0_BUFCTX_COUNT; ++ctx) {
168 array = &nvc0->residents[ctx];
169
Christoph Bumillerbf1df062011-01-21 16:52:17 +0100170 n = array->size / sizeof(struct resident);
171 MARK_RING(nvc0->screen->base.channel, n, n);
172 for (i = 0; i < n; ++i) {
Christoph Bumiller4c224752010-11-12 15:17:40 +0100173 rsd = util_dynarray_element(array, struct resident, i);
174
Christoph Bumillere4349022010-12-27 13:57:46 +0100175 nvc0_resource_validate(rsd->res, rsd->flags);
Christoph Bumiller4c224752010-11-12 15:17:40 +0100176 }
177 }
178
Christoph Bumillere4349022010-12-27 13:57:46 +0100179 nvc0_screen_make_buffers_resident(nvc0->screen);
Christoph Bumiller4c224752010-11-12 15:17:40 +0100180}