1/*
2 * Copyright 2012 Red Hat Inc.
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 * Authors: Ben Skeggs
23 *
24 */
25
26#include "draw/draw_context.h"
27
28#include "nouveau/nv_object.xml.h"
29#include "nv30-40_3d.xml.h"
30
31#include "nouveau/nouveau_fence.h"
32#include "nv30_context.h"
33#include "nv30_transfer.h"
34#include "nv30_state.h"
35
36static void
37nv30_context_kick_notify(struct nouveau_pushbuf *push)
38{
39   struct nouveau_screen *screen;
40   struct nv30_context *nv30;
41
42   if (!push->user_priv)
43      return;
44   nv30 = container_of(push->user_priv, nv30, bufctx);
45   screen = &nv30->screen->base;
46
47   nouveau_fence_next(screen);
48   nouveau_fence_update(screen, TRUE);
49
50   if (push->bufctx) {
51      struct nouveau_bufref *bref;
52      LIST_FOR_EACH_ENTRY(bref, &push->bufctx->current, thead) {
53         struct nv04_resource *res = bref->priv;
54         if (res && res->mm) {
55            nouveau_fence_ref(screen->fence.current, &res->fence);
56
57            if (bref->flags & NOUVEAU_BO_RD)
58               res->status |= NOUVEAU_BUFFER_STATUS_GPU_READING;
59
60            if (bref->flags & NOUVEAU_BO_WR) {
61               nouveau_fence_ref(screen->fence.current, &res->fence_wr);
62               res->status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
63            }
64         }
65      }
66   }
67}
68
69static void
70nv30_context_flush(struct pipe_context *pipe, struct pipe_fence_handle **fence)
71{
72   struct nv30_context *nv30 = nv30_context(pipe);
73   struct nouveau_pushbuf *push = nv30->base.pushbuf;
74
75   if (fence)
76      nouveau_fence_ref(nv30->screen->base.fence.current,
77                        (struct nouveau_fence **)fence);
78
79   PUSH_KICK(push);
80}
81
82static void
83nv30_context_destroy(struct pipe_context *pipe)
84{
85   struct nv30_context *nv30 = nv30_context(pipe);
86
87   if (nv30->draw)
88      draw_destroy(nv30->draw);
89
90   nouveau_bufctx_del(&nv30->bufctx);
91
92   if (nv30->screen->cur_ctx == nv30)
93      nv30->screen->cur_ctx = NULL;
94
95   nouveau_context_destroy(&nv30->base);
96}
97
98#define FAIL_CONTEXT_INIT(str, err)                   \
99   do {                                               \
100      NOUVEAU_ERR(str, err);                          \
101      nv30_context_destroy(pipe);                     \
102      return NULL;                                    \
103   } while(0)
104
105struct pipe_context *
106nv30_context_create(struct pipe_screen *pscreen, void *priv)
107{
108   struct nv30_screen *screen = nv30_screen(pscreen);
109   struct nv30_context *nv30 = CALLOC_STRUCT(nv30_context);
110   struct nouveau_pushbuf *push;
111   struct pipe_context *pipe;
112   int ret;
113
114   if (!nv30)
115      return NULL;
116
117   nv30->screen = screen;
118   nv30->base.screen = &screen->base;
119   nv30->base.copy_data = nv30_transfer_copy_data;
120
121   pipe = &nv30->base.pipe;
122   pipe->screen = pscreen;
123   pipe->priv = priv;
124   pipe->destroy = nv30_context_destroy;
125   pipe->flush = nv30_context_flush;
126
127   /*XXX: *cough* per-context client */
128   nv30->base.client = screen->base.client;
129
130   /*XXX: *cough* per-context pushbufs */
131   push = screen->base.pushbuf;
132   nv30->base.pushbuf = push;
133   nv30->base.pushbuf->user_priv = push->user_priv; /* hack at validate time */
134   nv30->base.pushbuf->rsvd_kick = 16; /* hack in screen before first space */
135   nv30->base.pushbuf->kick_notify = nv30_context_kick_notify;
136
137   ret = nouveau_bufctx_new(nv30->base.client, 64, &nv30->bufctx);
138   if (ret) {
139      nv30_context_destroy(pipe);
140      return NULL;
141   }
142
143   /*XXX: make configurable with performance vs quality, these defaults
144    *     match the binary driver's defaults
145    */
146   if (screen->eng3d->oclass < NV40_3D_CLASS)
147      nv30->config.filter = 0x00000004;
148   else
149      nv30->config.filter = 0x00002dc4;
150
151   nv30->config.aniso = NV40_3D_TEX_WRAP_ANISO_MIP_FILTER_OPTIMIZATION_OFF;
152
153   if (debug_get_bool_option("NV30_SWTNL", FALSE))
154      nv30->draw_flags |= NV30_NEW_SWTNL;
155
156   /*XXX: nvfx... */
157   nv30->is_nv4x = (screen->eng3d->oclass >= NV40_3D_CLASS) ? ~0 : 0;
158   nv30->use_nv4x = (screen->eng3d->oclass >= NV40_3D_CLASS) ? ~0 : 0;
159   nv30->render_mode = HW;
160
161   nv30->sample_mask = 0xffff;
162   nv30_vbo_init(pipe);
163   nv30_query_init(pipe);
164   nv30_state_init(pipe);
165   nv30_resource_init(pipe);
166   nv30_clear_init(pipe);
167   nv30_fragprog_init(pipe);
168   nv30_vertprog_init(pipe);
169   nv30_texture_init(pipe);
170   nv30_fragtex_init(pipe);
171   nv40_verttex_init(pipe);
172   nv30_draw_init(pipe);
173
174   return pipe;
175}
176