ae1fcc568db6cea82b42fbb9a33574ee57ab85b5
[mesa.git] / src / gallium / drivers / nouveau / 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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include "pipe/p_defines.h"
24 #include "util/u_framebuffer.h"
25
26 #ifdef NV50_WITH_DRAW_MODULE
27 #include "draw/draw_context.h"
28 #endif
29
30 #include "nv50/nv50_context.h"
31 #include "nv50/nv50_screen.h"
32 #include "nv50/nv50_resource.h"
33
34 static void
35 nv50_flush(struct pipe_context *pipe,
36 struct pipe_fence_handle **fence,
37 unsigned flags)
38 {
39 struct nouveau_screen *screen = nouveau_screen(pipe->screen);
40
41 if (fence)
42 nouveau_fence_ref(screen->fence.current, (struct nouveau_fence **)fence);
43
44 PUSH_KICK(screen->pushbuf);
45
46 nouveau_context_update_frame_stats(nouveau_context(pipe));
47 }
48
49 static void
50 nv50_texture_barrier(struct pipe_context *pipe)
51 {
52 struct nouveau_pushbuf *push = nv50_context(pipe)->base.pushbuf;
53
54 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
55 PUSH_DATA (push, 0);
56 BEGIN_NV04(push, NV50_3D(TEX_CACHE_CTL), 1);
57 PUSH_DATA (push, 0x20);
58 }
59
60 void
61 nv50_default_kick_notify(struct nouveau_pushbuf *push)
62 {
63 struct nv50_screen *screen = push->user_priv;
64
65 if (screen) {
66 nouveau_fence_next(&screen->base);
67 nouveau_fence_update(&screen->base, TRUE);
68 if (screen->cur_ctx)
69 screen->cur_ctx->state.flushed = TRUE;
70 }
71 }
72
73 static void
74 nv50_context_unreference_resources(struct nv50_context *nv50)
75 {
76 unsigned s, i;
77
78 nouveau_bufctx_del(&nv50->bufctx_3d);
79 nouveau_bufctx_del(&nv50->bufctx);
80
81 util_unreference_framebuffer_state(&nv50->framebuffer);
82
83 assert(nv50->num_vtxbufs <= PIPE_MAX_ATTRIBS);
84 for (i = 0; i < nv50->num_vtxbufs; ++i)
85 pipe_resource_reference(&nv50->vtxbuf[i].buffer, NULL);
86
87 pipe_resource_reference(&nv50->idxbuf.buffer, NULL);
88
89 for (s = 0; s < 3; ++s) {
90 assert(nv50->num_textures[s] <= PIPE_MAX_SAMPLERS);
91 for (i = 0; i < nv50->num_textures[s]; ++i)
92 pipe_sampler_view_reference(&nv50->textures[s][i], NULL);
93
94 for (i = 0; i < NV50_MAX_PIPE_CONSTBUFS; ++i)
95 if (!nv50->constbuf[s][i].user)
96 pipe_resource_reference(&nv50->constbuf[s][i].u.buf, NULL);
97 }
98 }
99
100 static void
101 nv50_destroy(struct pipe_context *pipe)
102 {
103 struct nv50_context *nv50 = nv50_context(pipe);
104
105 if (nv50_context_screen(nv50)->cur_ctx == nv50) {
106 nv50->base.pushbuf->kick_notify = NULL;
107 nv50_context_screen(nv50)->cur_ctx = NULL;
108 nouveau_pushbuf_bufctx(nv50->base.pushbuf, NULL);
109 }
110 /* need to flush before destroying the bufctx */
111 nouveau_pushbuf_kick(nv50->base.pushbuf, nv50->base.pushbuf->channel);
112
113 nv50_context_unreference_resources(nv50);
114
115 #ifdef NV50_WITH_DRAW_MODULE
116 draw_destroy(nv50->draw);
117 #endif
118
119 FREE(nv50->blit);
120
121 nouveau_context_destroy(&nv50->base);
122 }
123
124 static int
125 nv50_invalidate_resource_storage(struct nouveau_context *ctx,
126 struct pipe_resource *res,
127 int ref)
128 {
129 struct nv50_context *nv50 = nv50_context(&ctx->pipe);
130 unsigned s, i;
131
132 if (res->bind & PIPE_BIND_RENDER_TARGET) {
133 assert(nv50->framebuffer.nr_cbufs <= PIPE_MAX_COLOR_BUFS);
134 for (i = 0; i < nv50->framebuffer.nr_cbufs; ++i) {
135 if (nv50->framebuffer.cbufs[i] &&
136 nv50->framebuffer.cbufs[i]->texture == res) {
137 nv50->dirty |= NV50_NEW_FRAMEBUFFER;
138 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_FB);
139 if (!--ref)
140 return ref;
141 }
142 }
143 }
144 if (res->bind & PIPE_BIND_DEPTH_STENCIL) {
145 if (nv50->framebuffer.zsbuf &&
146 nv50->framebuffer.zsbuf->texture == res) {
147 nv50->dirty |= NV50_NEW_FRAMEBUFFER;
148 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_FB);
149 if (!--ref)
150 return ref;
151 }
152 }
153
154 if (res->bind & PIPE_BIND_VERTEX_BUFFER) {
155 assert(nv50->num_vtxbufs <= PIPE_MAX_ATTRIBS);
156 for (i = 0; i < nv50->num_vtxbufs; ++i) {
157 if (nv50->vtxbuf[i].buffer == res) {
158 nv50->dirty |= NV50_NEW_ARRAYS;
159 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_VERTEX);
160 if (!--ref)
161 return ref;
162 }
163 }
164 }
165 if (res->bind & PIPE_BIND_INDEX_BUFFER) {
166 if (nv50->idxbuf.buffer == res)
167 if (!--ref)
168 return ref;
169 }
170
171 if (res->bind & PIPE_BIND_SAMPLER_VIEW) {
172 for (s = 0; s < 3; ++s) {
173 assert(nv50->num_textures[s] <= PIPE_MAX_SAMPLERS);
174 for (i = 0; i < nv50->num_textures[s]; ++i) {
175 if (nv50->textures[s][i] &&
176 nv50->textures[s][i]->texture == res) {
177 nv50->dirty |= NV50_NEW_TEXTURES;
178 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_TEXTURES);
179 if (!--ref)
180 return ref;
181 }
182 }
183 }
184 }
185
186 if (res->bind & PIPE_BIND_CONSTANT_BUFFER) {
187 for (s = 0; s < 3; ++s) {
188 assert(nv50->num_vtxbufs <= NV50_MAX_PIPE_CONSTBUFS);
189 for (i = 0; i < nv50->num_vtxbufs; ++i) {
190 if (!nv50->constbuf[s][i].user &&
191 nv50->constbuf[s][i].u.buf == res) {
192 nv50->dirty |= NV50_NEW_CONSTBUF;
193 nv50->constbuf_dirty[s] |= 1 << i;
194 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_CB(s, i));
195 if (!--ref)
196 return ref;
197 }
198 }
199 }
200 }
201
202 return ref;
203 }
204
205 static void
206 nv50_context_get_sample_position(struct pipe_context *, unsigned, unsigned,
207 float *);
208
209 struct pipe_context *
210 nv50_create(struct pipe_screen *pscreen, void *priv)
211 {
212 struct nv50_screen *screen = nv50_screen(pscreen);
213 struct nv50_context *nv50;
214 struct pipe_context *pipe;
215 int ret;
216 uint32_t flags;
217
218 nv50 = CALLOC_STRUCT(nv50_context);
219 if (!nv50)
220 return NULL;
221 pipe = &nv50->base.pipe;
222
223 if (!nv50_blitctx_create(nv50))
224 goto out_err;
225
226 nv50->base.pushbuf = screen->base.pushbuf;
227 nv50->base.client = screen->base.client;
228
229 ret = nouveau_bufctx_new(screen->base.client, NV50_BIND_COUNT,
230 &nv50->bufctx_3d);
231 if (!ret)
232 ret = nouveau_bufctx_new(screen->base.client, 2, &nv50->bufctx);
233 if (ret)
234 goto out_err;
235
236 nv50->base.screen = &screen->base;
237 nv50->base.copy_data = nv50_m2mf_copy_linear;
238 nv50->base.push_data = nv50_sifc_linear_u8;
239 nv50->base.push_cb = nv50_cb_push;
240
241 nv50->screen = screen;
242 pipe->screen = pscreen;
243 pipe->priv = priv;
244
245 pipe->destroy = nv50_destroy;
246
247 pipe->draw_vbo = nv50_draw_vbo;
248 pipe->clear = nv50_clear;
249
250 pipe->flush = nv50_flush;
251 pipe->texture_barrier = nv50_texture_barrier;
252 pipe->get_sample_position = nv50_context_get_sample_position;
253
254 if (!screen->cur_ctx) {
255 screen->cur_ctx = nv50;
256 nouveau_pushbuf_bufctx(screen->base.pushbuf, nv50->bufctx);
257 }
258 nv50->base.pushbuf->kick_notify = nv50_default_kick_notify;
259
260 nv50_init_query_functions(nv50);
261 nv50_init_surface_functions(nv50);
262 nv50_init_state_functions(nv50);
263 nv50_init_resource_functions(pipe);
264
265 nv50->base.invalidate_resource_storage = nv50_invalidate_resource_storage;
266
267 #ifdef NV50_WITH_DRAW_MODULE
268 /* no software fallbacks implemented */
269 nv50->draw = draw_create(pipe);
270 assert(nv50->draw);
271 draw_set_rasterize_stage(nv50->draw, nv50_draw_render_stage(nv50));
272 #endif
273
274 if (screen->base.device->chipset < 0x84 ||
275 debug_get_bool_option("NOUVEAU_PMPEG", FALSE)) {
276 /* PMPEG */
277 nouveau_context_init_vdec(&nv50->base);
278 } else if (screen->base.device->chipset < 0x98 ||
279 screen->base.device->chipset == 0xa0) {
280 /* VP2 */
281 pipe->create_video_codec = nv84_create_decoder;
282 pipe->create_video_buffer = nv84_video_buffer_create;
283 } else {
284 /* VP3/4 */
285 pipe->create_video_codec = nv98_create_decoder;
286 pipe->create_video_buffer = nv98_video_buffer_create;
287 }
288
289 flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
290
291 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->code);
292 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->uniforms);
293 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->txc);
294 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->stack_bo);
295
296 flags = NOUVEAU_BO_GART | NOUVEAU_BO_WR;
297
298 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->fence.bo);
299 BCTX_REFN_bo(nv50->bufctx, FENCE, flags, screen->fence.bo);
300
301 nv50->base.scratch.bo_size = 2 << 20;
302
303 return pipe;
304
305 out_err:
306 if (nv50->bufctx_3d)
307 nouveau_bufctx_del(&nv50->bufctx_3d);
308 if (nv50->bufctx)
309 nouveau_bufctx_del(&nv50->bufctx);
310 if (nv50->blit)
311 FREE(nv50->blit);
312 FREE(nv50);
313 return NULL;
314 }
315
316 void
317 nv50_bufctx_fence(struct nouveau_bufctx *bufctx, boolean on_flush)
318 {
319 struct nouveau_list *list = on_flush ? &bufctx->current : &bufctx->pending;
320 struct nouveau_list *it;
321
322 for (it = list->next; it != list; it = it->next) {
323 struct nouveau_bufref *ref = (struct nouveau_bufref *)it;
324 struct nv04_resource *res = ref->priv;
325 if (res)
326 nv50_resource_validate(res, (unsigned)ref->priv_data);
327 }
328 }
329
330 static void
331 nv50_context_get_sample_position(struct pipe_context *pipe,
332 unsigned sample_count, unsigned sample_index,
333 float *xy)
334 {
335 static const uint8_t ms1[1][2] = { { 0x8, 0x8 } };
336 static const uint8_t ms2[2][2] = {
337 { 0x4, 0x4 }, { 0xc, 0xc } }; /* surface coords (0,0), (1,0) */
338 static const uint8_t ms4[4][2] = {
339 { 0x6, 0x2 }, { 0xe, 0x6 }, /* (0,0), (1,0) */
340 { 0x2, 0xa }, { 0xa, 0xe } }; /* (0,1), (1,1) */
341 static const uint8_t ms8[8][2] = {
342 { 0x1, 0x7 }, { 0x5, 0x3 }, /* (0,0), (1,0) */
343 { 0x3, 0xd }, { 0x7, 0xb }, /* (0,1), (1,1) */
344 { 0x9, 0x5 }, { 0xf, 0x1 }, /* (2,0), (3,0) */
345 { 0xb, 0xf }, { 0xd, 0x9 } }; /* (2,1), (3,1) */
346 #if 0
347 /* NOTE: there are alternative modes for MS2 and MS8, currently not used */
348 static const uint8_t ms8_alt[8][2] = {
349 { 0x9, 0x5 }, { 0x7, 0xb }, /* (2,0), (1,1) */
350 { 0xd, 0x9 }, { 0x5, 0x3 }, /* (3,1), (1,0) */
351 { 0x3, 0xd }, { 0x1, 0x7 }, /* (0,1), (0,0) */
352 { 0xb, 0xf }, { 0xf, 0x1 } }; /* (2,1), (3,0) */
353 #endif
354
355 const uint8_t (*ptr)[2];
356
357 switch (sample_count) {
358 case 0:
359 case 1: ptr = ms1; break;
360 case 2: ptr = ms2; break;
361 case 4: ptr = ms4; break;
362 case 8: ptr = ms8; break;
363 default:
364 assert(0);
365 return; /* bad sample count -> undefined locations */
366 }
367 xy[0] = ptr[sample_index][0] * 0.0625f;
368 xy[1] = ptr[sample_index][1] * 0.0625f;
369 }