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