Merge commit 'origin/gallium-0.2' 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 unsigned buf_usage = PIPE_BUFFER_USAGE_PIXEL |
61 NOUVEAU_BUFFER_USAGE_TEXTURE;
62
63 mt = MALLOC(sizeof(struct nv40_miptree));
64 if (!mt)
65 return NULL;
66 mt->base = *pt;
67 mt->base.refcount = 1;
68 mt->base.screen = pscreen;
69 mt->shadow_tex = NULL;
70 mt->shadow_surface = NULL;
71
72 /* Swizzled textures must be POT */
73 if (pt->width[0] & (pt->width[0] - 1) ||
74 pt->height[0] & (pt->height[0] - 1))
75 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
76 else
77 if (pt->tex_usage & (PIPE_TEXTURE_USAGE_PRIMARY |
78 PIPE_TEXTURE_USAGE_DISPLAY_TARGET))
79 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
80 else
81 if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
82 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
83 else {
84 switch (pt->format) {
85 /* TODO: Figure out which formats can be swizzled */
86 case PIPE_FORMAT_A8R8G8B8_UNORM:
87 case PIPE_FORMAT_X8R8G8B8_UNORM:
88 case PIPE_FORMAT_R16_SNORM:
89 break;
90 default:
91 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
92 }
93 }
94
95 if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
96 buf_usage |= PIPE_BUFFER_USAGE_CPU_READ_WRITE;
97
98 nv40_miptree_layout(mt);
99
100 mt->buffer = ws->buffer_create(ws, 256, buf_usage, mt->total_size);
101 if (!mt->buffer) {
102 FREE(mt);
103 return NULL;
104 }
105
106 return &mt->base;
107 }
108
109 static struct pipe_texture *
110 nv40_miptree_blanket(struct pipe_screen *pscreen, const struct pipe_texture *pt,
111 const unsigned *stride, struct pipe_buffer *pb)
112 {
113 struct nv40_miptree *mt;
114
115 /* Only supports 2D, non-mipmapped textures for the moment */
116 if (pt->target != PIPE_TEXTURE_2D || pt->last_level != 0 ||
117 pt->depth[0] != 1)
118 return NULL;
119
120 mt = CALLOC_STRUCT(nv40_miptree);
121 if (!mt)
122 return NULL;
123
124 mt->base = *pt;
125 mt->base.refcount = 1;
126 mt->base.screen = pscreen;
127 mt->level[0].pitch = stride[0];
128 mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
129
130 pipe_buffer_reference(pscreen, &mt->buffer, pb);
131 return &mt->base;
132 }
133
134 static void
135 nv40_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **ppt)
136 {
137 struct pipe_texture *pt = *ppt;
138 struct nv40_miptree *mt = (struct nv40_miptree *)pt;
139 int l;
140
141 *ppt = NULL;
142 if (--pt->refcount)
143 return;
144
145 pipe_buffer_reference(pscreen, &mt->buffer, NULL);
146 for (l = 0; l <= pt->last_level; l++) {
147 if (mt->level[l].image_offset)
148 FREE(mt->level[l].image_offset);
149 }
150
151 if (mt->shadow_tex) {
152 assert(mt->shadow_surface);
153 pscreen->tex_surface_release(pscreen, &mt->shadow_surface);
154 nv40_miptree_release(pscreen, &mt->shadow_tex);
155 }
156
157 FREE(mt);
158 }
159
160 static struct pipe_surface *
161 nv40_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
162 unsigned face, unsigned level, unsigned zslice,
163 unsigned flags)
164 {
165 struct nv40_miptree *mt = (struct nv40_miptree *)pt;
166 struct pipe_surface *ps;
167
168 ps = CALLOC_STRUCT(pipe_surface);
169 if (!ps)
170 return NULL;
171 pipe_texture_reference(&ps->texture, pt);
172 ps->format = pt->format;
173 ps->width = pt->width[level];
174 ps->height = pt->height[level];
175 ps->block = pt->block;
176 ps->nblocksx = pt->nblocksx[level];
177 ps->nblocksy = pt->nblocksy[level];
178 ps->stride = mt->level[level].pitch;
179 ps->usage = flags;
180 ps->status = PIPE_SURFACE_STATUS_DEFINED;
181 ps->refcount = 1;
182 ps->face = face;
183 ps->level = level;
184 ps->zslice = zslice;
185
186 if (pt->target == PIPE_TEXTURE_CUBE) {
187 ps->offset = mt->level[level].image_offset[face];
188 } else
189 if (pt->target == PIPE_TEXTURE_3D) {
190 ps->offset = mt->level[level].image_offset[zslice];
191 } else {
192 ps->offset = mt->level[level].image_offset[0];
193 }
194
195 return ps;
196 }
197
198 static void
199 nv40_miptree_surface_del(struct pipe_screen *pscreen,
200 struct pipe_surface **psurface)
201 {
202 struct pipe_surface *ps = *psurface;
203
204 *psurface = NULL;
205 if (--ps->refcount > 0)
206 return;
207
208 pipe_texture_reference(&ps->texture, NULL);
209 FREE(ps);
210 }
211
212 void
213 nv40_screen_init_miptree_functions(struct pipe_screen *pscreen)
214 {
215 pscreen->texture_create = nv40_miptree_create;
216 pscreen->texture_blanket = nv40_miptree_blanket;
217 pscreen->texture_release = nv40_miptree_release;
218 pscreen->get_tex_surface = nv40_miptree_surface_new;
219 pscreen->tex_surface_release = nv40_miptree_surface_del;
220 }
221