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