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