1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright 2012 Red Hat Inc.
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the "Software"),
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * to deal in the Software without restriction, including without limitation
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and/or sell copies of the Software, and to permit persons to whom the
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software is furnished to do so, subject to the following conditions:
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice shall be included in
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * all copies or substantial portions of the Software.
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * SOFTWARE.
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Authors: Ben Skeggs
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "draw/draw_context.h"
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "nouveau/nv_object.xml.h"
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "nv30-40_3d.xml.h"
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "nouveau/nouveau_fence.h"
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "nv30_context.h"
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "nv30_transfer.h"
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "nv30_state.h"
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnv30_context_kick_notify(struct nouveau_pushbuf *push)
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct nouveau_screen *screen;
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct nv30_context *nv30;
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!push->user_priv)
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return;
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30 = container_of(push->user_priv, nv30, bufctx);
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   screen = &nv30->screen->base;
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nouveau_fence_next(screen);
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nouveau_fence_update(screen, TRUE);
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (push->bufctx) {
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      struct nouveau_bufref *bref;
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      LIST_FOR_EACH_ENTRY(bref, &push->bufctx->current, thead) {
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         struct nv04_resource *res = bref->priv;
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         if (res && res->mm) {
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            nouveau_fence_ref(screen->fence.current, &res->fence);
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            if (bref->flags & NOUVEAU_BO_RD)
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org               res->status |= NOUVEAU_BUFFER_STATUS_GPU_READING;
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            if (bref->flags & NOUVEAU_BO_WR) {
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org               nouveau_fence_ref(screen->fence.current, &res->fence_wr);
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org               res->status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            }
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         }
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnv30_context_flush(struct pipe_context *pipe, struct pipe_fence_handle **fence)
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct nv30_context *nv30 = nv30_context(pipe);
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct nouveau_pushbuf *push = nv30->base.pushbuf;
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (fence)
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      nouveau_fence_ref(nv30->screen->base.fence.current,
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        (struct nouveau_fence **)fence);
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   PUSH_KICK(push);
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnv30_context_destroy(struct pipe_context *pipe)
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct nv30_context *nv30 = nv30_context(pipe);
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (nv30->draw)
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      draw_destroy(nv30->draw);
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nouveau_bufctx_del(&nv30->bufctx);
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (nv30->screen->cur_ctx == nv30)
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      nv30->screen->cur_ctx = NULL;
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nouveau_context_destroy(&nv30->base);
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define FAIL_CONTEXT_INIT(str, err)                   \
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   do {                                               \
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      NOUVEAU_ERR(str, err);                          \
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      nv30_context_destroy(pipe);                     \
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return NULL;                                    \
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   } while(0)
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct pipe_context *
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnv30_context_create(struct pipe_screen *pscreen, void *priv)
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct nv30_screen *screen = nv30_screen(pscreen);
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct nv30_context *nv30 = CALLOC_STRUCT(nv30_context);
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct nouveau_pushbuf *push;
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_context *pipe;
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int ret;
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!nv30)
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return NULL;
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30->screen = screen;
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30->base.screen = &screen->base;
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30->base.copy_data = nv30_transfer_copy_data;
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe = &nv30->base.pipe;
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe->screen = pscreen;
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe->priv = priv;
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe->destroy = nv30_context_destroy;
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe->flush = nv30_context_flush;
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*XXX: *cough* per-context client */
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30->base.client = screen->base.client;
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*XXX: *cough* per-context pushbufs */
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   push = screen->base.pushbuf;
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30->base.pushbuf = push;
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30->base.pushbuf->user_priv = push->user_priv; /* hack at validate time */
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30->base.pushbuf->rsvd_kick = 16; /* hack in screen before first space */
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30->base.pushbuf->kick_notify = nv30_context_kick_notify;
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ret = nouveau_bufctx_new(nv30->base.client, 64, &nv30->bufctx);
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ret) {
139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      nv30_context_destroy(pipe);
140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return NULL;
141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*XXX: make configurable with performance vs quality, these defaults
144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *     match the binary driver's defaults
145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (screen->eng3d->oclass < NV40_3D_CLASS)
147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      nv30->config.filter = 0x00000004;
148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else
149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      nv30->config.filter = 0x00002dc4;
150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30->config.aniso = NV40_3D_TEX_WRAP_ANISO_MIP_FILTER_OPTIMIZATION_OFF;
152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (debug_get_bool_option("NV30_SWTNL", FALSE))
154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      nv30->draw_flags |= NV30_NEW_SWTNL;
155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*XXX: nvfx... */
157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30->is_nv4x = (screen->eng3d->oclass >= NV40_3D_CLASS) ? ~0 : 0;
158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30->use_nv4x = (screen->eng3d->oclass >= NV40_3D_CLASS) ? ~0 : 0;
159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30->render_mode = HW;
160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30->sample_mask = 0xffff;
162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30_vbo_init(pipe);
163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30_query_init(pipe);
164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30_state_init(pipe);
165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30_resource_init(pipe);
166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30_clear_init(pipe);
167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30_fragprog_init(pipe);
168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30_vertprog_init(pipe);
169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30_texture_init(pipe);
170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30_fragtex_init(pipe);
171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv40_verttex_init(pipe);
172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   nv30_draw_init(pipe);
173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return pipe;
175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
176