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