Merge commit 'origin/gallium-0.1'
[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 *tmp)
31 {
32 struct pipe_winsys *ws = pscreen->winsys;
33 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
34 struct pipe_texture *pt = &mt->base;
35 unsigned usage, width = tmp->width[0], height = tmp->height[0];
36 unsigned depth = tmp->depth[0];
37 int i, l;
38
39 mt->base = *tmp;
40 mt->base.refcount = 1;
41 mt->base.screen = pscreen;
42
43 usage = PIPE_BUFFER_USAGE_PIXEL;
44 switch (pt->format) {
45 case PIPE_FORMAT_Z24S8_UNORM:
46 case PIPE_FORMAT_Z16_UNORM:
47 usage |= NOUVEAU_BUFFER_USAGE_ZETA;
48 break;
49 default:
50 break;
51 }
52
53 switch (pt->target) {
54 case PIPE_TEXTURE_3D:
55 mt->image_nr = pt->depth[0];
56 break;
57 case PIPE_TEXTURE_CUBE:
58 mt->image_nr = 6;
59 break;
60 default:
61 mt->image_nr = 1;
62 break;
63 }
64
65 for (l = 0; l <= pt->last_level; l++) {
66 struct nv50_miptree_level *lvl = &mt->level[l];
67
68 pt->width[l] = width;
69 pt->height[l] = height;
70 pt->depth[l] = depth;
71 pt->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
72 pt->nblocksy[l] = pf_get_nblocksy(&pt->block, height);
73
74 lvl->image_offset = CALLOC(mt->image_nr, sizeof(int));
75 lvl->pitch = align(pt->width[l] * pt->block.size, 64);
76
77 width = MAX2(1, width >> 1);
78 height = MAX2(1, height >> 1);
79 depth = MAX2(1, depth >> 1);
80 }
81
82 for (i = 0; i < mt->image_nr; i++) {
83 for (l = 0; l <= pt->last_level; l++) {
84 struct nv50_miptree_level *lvl = &mt->level[l];
85 int size;
86
87 size = align(pt->width[l], 8) * pt->block.size;
88 size = align(size, 64);
89 size *= align(pt->height[l], 8) * pt->block.size;
90
91 lvl->image_offset[i] = mt->total_size;
92
93 mt->total_size += size;
94 }
95 }
96
97 mt->buffer = ws->buffer_create(ws, 256, usage, mt->total_size);
98 if (!mt->buffer) {
99 FREE(mt);
100 return NULL;
101 }
102
103 return &mt->base;
104 }
105
106 static struct pipe_texture *
107 nv50_miptree_blanket(struct pipe_screen *pscreen, const struct pipe_texture *pt,
108 const unsigned *stride, struct pipe_buffer *pb)
109 {
110 struct nv50_miptree *mt;
111
112 /* Only supports 2D, non-mipmapped textures for the moment */
113 if (pt->target != PIPE_TEXTURE_2D || pt->last_level != 0 ||
114 pt->depth[0] != 1)
115 return NULL;
116
117 mt = CALLOC_STRUCT(nv50_miptree);
118 if (!mt)
119 return NULL;
120
121 mt->base = *pt;
122 mt->base.refcount = 1;
123 mt->base.screen = pscreen;
124 mt->image_nr = 1;
125 mt->level[0].pitch = *stride;
126 mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
127
128 pipe_buffer_reference(pscreen, &mt->buffer, pb);
129 return &mt->base;
130 }
131
132 static void
133 nv50_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **ppt)
134 {
135 struct pipe_texture *pt = *ppt;
136
137 *ppt = NULL;
138
139 if (--pt->refcount <= 0) {
140 struct nv50_miptree *mt = nv50_miptree(pt);
141
142 pipe_buffer_reference(pscreen, &mt->buffer, NULL);
143 FREE(mt);
144 }
145 }
146
147 static struct pipe_surface *
148 nv50_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
149 unsigned face, unsigned level, unsigned zslice,
150 unsigned flags)
151 {
152 struct nv50_miptree *mt = nv50_miptree(pt);
153 struct nv50_miptree_level *lvl = &mt->level[level];
154 struct pipe_surface *ps;
155 int img;
156
157 if (pt->target == PIPE_TEXTURE_CUBE)
158 img = face;
159 else
160 if (pt->target == PIPE_TEXTURE_3D)
161 img = zslice;
162 else
163 img = 0;
164
165 ps = CALLOC_STRUCT(pipe_surface);
166 if (!ps)
167 return NULL;
168 pipe_texture_reference(&ps->texture, pt);
169 ps->format = pt->format;
170 ps->width = pt->width[level];
171 ps->height = pt->height[level];
172 ps->usage = flags;
173 ps->status = PIPE_SURFACE_STATUS_DEFINED;
174 ps->refcount = 1;
175 ps->face = face;
176 ps->level = level;
177 ps->zslice = zslice;
178 ps->offset = lvl->image_offset[img];
179
180 return ps;
181 }
182
183 static void
184 nv50_miptree_surface_del(struct pipe_screen *pscreen,
185 struct pipe_surface **psurface)
186 {
187 struct pipe_surface *ps = *psurface;
188 struct nv50_surface *s = nv50_surface(ps);
189
190 *psurface = NULL;
191
192 if (--ps->refcount <= 0) {
193 pipe_texture_reference(&ps->texture, NULL);
194 FREE(s);
195 }
196 }
197
198 void
199 nv50_screen_init_miptree_functions(struct pipe_screen *pscreen)
200 {
201 pscreen->texture_create = nv50_miptree_create;
202 pscreen->texture_blanket = nv50_miptree_blanket;
203 pscreen->texture_release = nv50_miptree_release;
204 pscreen->get_tex_surface = nv50_miptree_surface_new;
205 pscreen->tex_surface_release = nv50_miptree_surface_del;
206 }
207