nouveau: gallium directory structure changed again..
[mesa.git] / src / gallium / drivers / nv50 / nv50_miptree.c
1 /*
2 * Copyright 2008 Ben Skeggs
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 "pipe/p_state.h"
24 #include "pipe/p_defines.h"
25 #include "pipe/p_inlines.h"
26
27 #include "nv50_context.h"
28
29 static struct pipe_texture *
30 nv50_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
31 {
32 struct pipe_winsys *ws = pscreen->winsys;
33 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
34 unsigned usage, pitch;
35
36 mt->base = *pt;
37 mt->base.refcount = 1;
38 mt->base.screen = pscreen;
39
40 usage = PIPE_BUFFER_USAGE_PIXEL;
41 switch (pt->format) {
42 case PIPE_FORMAT_Z24S8_UNORM:
43 case PIPE_FORMAT_Z16_UNORM:
44 usage |= NOUVEAU_BUFFER_USAGE_ZETA;
45 break;
46 default:
47 break;
48 }
49
50 pitch = ((pt->width[0] + 63) & ~63) * pt->block.size;
51
52 mt->buffer = ws->buffer_create(ws, 256, usage, pitch * pt->height[0]);
53 if (!mt->buffer) {
54 FREE(mt);
55 return NULL;
56 }
57
58 return &mt->base;
59 }
60
61 static void
62 nv50_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **ppt)
63 {
64 struct pipe_texture *pt = *ppt;
65
66 *ppt = NULL;
67
68 if (--pt->refcount <= 0) {
69 struct nv50_miptree *mt = nv50_miptree(pt);
70
71 pipe_buffer_reference(pscreen, &mt->buffer, NULL);
72 FREE(mt);
73 }
74 }
75
76 static struct pipe_surface *
77 nv50_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
78 unsigned face, unsigned level, unsigned zslice,
79 unsigned flags)
80 {
81 struct nv50_miptree *mt = nv50_miptree(pt);
82 struct nv50_surface *s;
83 struct pipe_surface *ps;
84
85 s = CALLOC_STRUCT(nv50_surface);
86 if (!s)
87 return NULL;
88 ps = &s->base;
89
90 ps->refcount = 1;
91 ps->winsys = pscreen->winsys;
92 ps->format = pt->format;
93 ps->width = pt->width[level];
94 ps->height = pt->height[level];
95 ps->block = pt->block;
96 ps->nblocksx = pt->nblocksx[level];
97 ps->nblocksy = pt->nblocksy[level];
98 ps->stride = ps->width * ps->block.size;
99 ps->offset = 0;
100 ps->usage = flags;
101 ps->status = PIPE_SURFACE_STATUS_DEFINED;
102
103 pipe_texture_reference(&ps->texture, pt);
104 pipe_buffer_reference(pscreen, &ps->buffer, mt->buffer);
105
106 return ps;
107 }
108
109 static void
110 nv50_miptree_surface_del(struct pipe_screen *pscreen,
111 struct pipe_surface **psurface)
112 {
113 struct pipe_surface *ps = *psurface;
114 struct nv50_surface *s = nv50_surface(ps);
115
116 *psurface = NULL;
117
118 if (--ps->refcount <= 0) {
119 pipe_texture_reference(&ps->texture, NULL);
120 pipe_buffer_reference(pscreen, &ps->buffer, NULL);
121 FREE(s);
122 }
123 }
124
125 void
126 nv50_screen_init_miptree_functions(struct pipe_screen *pscreen)
127 {
128 pscreen->texture_create = nv50_miptree_create;
129 pscreen->texture_release = nv50_miptree_release;
130 pscreen->get_tex_surface = nv50_miptree_surface_new;
131 pscreen->tex_surface_release = nv50_miptree_surface_del;
132 }
133