Merge branch 'gallium-0.2' of git://anongit.freedesktop.org/mesa/mesa into gallium-0.2
[mesa.git] / src / gallium / drivers / nv30 / nv30_miptree.c
1 #include "pipe/p_state.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_inlines.h"
4
5 #include "nv30_context.h"
6
7 static void
8 nv30_miptree_layout(struct nv30_miptree *nv30mt)
9 {
10 struct pipe_texture *pt = &nv30mt->base;
11 boolean swizzled = FALSE;
12 uint width = pt->width[0], height = pt->height[0], depth = pt->depth[0];
13 uint offset = 0;
14 int nr_faces, l, f, pitch;
15
16 if (pt->target == PIPE_TEXTURE_CUBE) {
17 nr_faces = 6;
18 } else
19 if (pt->target == PIPE_TEXTURE_3D) {
20 nr_faces = pt->depth[0];
21 } else {
22 nr_faces = 1;
23 }
24
25 pitch = pt->width[0];
26 for (l = 0; l <= pt->last_level; l++) {
27 pt->width[l] = width;
28 pt->height[l] = height;
29 pt->depth[l] = depth;
30 pt->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
31 pt->nblocksy[l] = pf_get_nblocksy(&pt->block, height);
32
33 if (swizzled)
34 pitch = pt->nblocksx[l];
35 pitch = align(pitch, 64);
36
37 nv30mt->level[l].pitch = pitch * pt->block.size;
38 nv30mt->level[l].image_offset =
39 CALLOC(nr_faces, sizeof(unsigned));
40
41 width = MAX2(1, width >> 1);
42 height = MAX2(1, height >> 1);
43 depth = MAX2(1, depth >> 1);
44 }
45
46 for (f = 0; f < nr_faces; f++) {
47 for (l = 0; l <= pt->last_level; l++) {
48 nv30mt->level[l].image_offset[f] = offset;
49 offset += nv30mt->level[l].pitch * pt->height[l];
50 }
51 }
52
53 nv30mt->total_size = offset;
54 }
55
56 static struct pipe_texture *
57 nv30_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
58 {
59 struct pipe_winsys *ws = pscreen->winsys;
60 struct nv30_miptree *mt;
61
62 mt = MALLOC(sizeof(struct nv30_miptree));
63 if (!mt)
64 return NULL;
65 mt->base = *pt;
66 mt->base.refcount = 1;
67 mt->base.screen = pscreen;
68 mt->shadow_tex = NULL;
69 mt->shadow_surface = NULL;
70
71 /* Swizzled textures must be POT */
72 if (pt->width[0] & (pt->width[0] - 1) ||
73 pt->height[0] & (pt->height[0] - 1))
74 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
75 else
76 if (pt->tex_usage & (PIPE_TEXTURE_USAGE_PRIMARY |
77 PIPE_TEXTURE_USAGE_DISPLAY_TARGET))
78 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
79 else
80 if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
81 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
82 else {
83 switch (pt->format) {
84 /* TODO: Figure out which formats can be swizzled */
85 case PIPE_FORMAT_A8R8G8B8_UNORM:
86 case PIPE_FORMAT_X8R8G8B8_UNORM:
87 case PIPE_FORMAT_R16_SNORM:
88 break;
89 default:
90 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
91 }
92 }
93
94 nv30_miptree_layout(mt);
95
96 mt->buffer = ws->buffer_create(ws, 256,
97 PIPE_BUFFER_USAGE_PIXEL |
98 NOUVEAU_BUFFER_USAGE_TEXTURE,
99 mt->total_size);
100 if (!mt->buffer) {
101 FREE(mt);
102 return NULL;
103 }
104
105 return &mt->base;
106 }
107
108 static struct pipe_texture *
109 nv30_miptree_blanket(struct pipe_screen *pscreen, const struct pipe_texture *pt,
110 const unsigned *stride, struct pipe_buffer *pb)
111 {
112 struct nv30_miptree *mt;
113
114 /* Only supports 2D, non-mipmapped textures for the moment */
115 if (pt->target != PIPE_TEXTURE_2D || pt->last_level != 0 ||
116 pt->depth[0] != 1)
117 return NULL;
118
119 mt = CALLOC_STRUCT(nv30_miptree);
120 if (!mt)
121 return NULL;
122
123 mt->base = *pt;
124 mt->base.refcount = 1;
125 mt->base.screen = pscreen;
126 mt->level[0].pitch = stride[0];
127 mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
128
129 pipe_buffer_reference(pscreen, &mt->buffer, pb);
130 return &mt->base;
131 }
132
133 static void
134 nv30_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **ppt)
135 {
136 struct pipe_texture *pt = *ppt;
137 struct nv30_miptree *mt = (struct nv30_miptree *)pt;
138 int l;
139
140 *ppt = NULL;
141 if (--pt->refcount)
142 return;
143
144 pipe_buffer_reference(pscreen, &mt->buffer, NULL);
145 for (l = 0; l <= pt->last_level; l++) {
146 if (mt->level[l].image_offset)
147 FREE(mt->level[l].image_offset);
148 }
149
150 if (mt->shadow_tex) {
151 assert(mt->shadow_surface);
152 pscreen->tex_surface_release(pscreen, &mt->shadow_surface);
153 nv30_miptree_release(pscreen, &mt->shadow_tex);
154 }
155
156 FREE(mt);
157 }
158
159 static struct pipe_surface *
160 nv30_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
161 unsigned face, unsigned level, unsigned zslice,
162 unsigned flags)
163 {
164 struct nv30_miptree *nv30mt = (struct nv30_miptree *)pt;
165 struct pipe_surface *ps;
166
167 ps = CALLOC_STRUCT(pipe_surface);
168 if (!ps)
169 return NULL;
170 pipe_texture_reference(&ps->texture, pt);
171 ps->format = pt->format;
172 ps->width = pt->width[level];
173 ps->height = pt->height[level];
174 ps->block = pt->block;
175 ps->nblocksx = pt->nblocksx[level];
176 ps->nblocksy = pt->nblocksy[level];
177 ps->stride = nv30mt->level[level].pitch;
178 ps->usage = flags;
179 ps->status = PIPE_SURFACE_STATUS_DEFINED;
180 ps->refcount = 1;
181 ps->face = face;
182 ps->level = level;
183 ps->zslice = zslice;
184
185 if (pt->target == PIPE_TEXTURE_CUBE) {
186 ps->offset = nv30mt->level[level].image_offset[face];
187 } else
188 if (pt->target == PIPE_TEXTURE_3D) {
189 ps->offset = nv30mt->level[level].image_offset[zslice];
190 } else {
191 ps->offset = nv30mt->level[level].image_offset[0];
192 }
193
194 return ps;
195 }
196
197 static void
198 nv30_miptree_surface_del(struct pipe_screen *pscreen,
199 struct pipe_surface **psurface)
200 {
201 struct pipe_surface *ps = *psurface;
202
203 *psurface = NULL;
204 if (--ps->refcount > 0)
205 return;
206
207 pipe_texture_reference(&ps->texture, NULL);
208 FREE(ps);
209 }
210
211 void
212 nv30_screen_init_miptree_functions(struct pipe_screen *pscreen)
213 {
214 pscreen->texture_create = nv30_miptree_create;
215 pscreen->texture_blanket = nv30_miptree_blanket;
216 pscreen->texture_release = nv30_miptree_release;
217 pscreen->get_tex_surface = nv30_miptree_surface_new;
218 pscreen->tex_surface_release = nv30_miptree_surface_del;
219 }