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