Merge branch 'gallium-0.2-radeon' 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].image_offset = CALLOC(1, sizeof(unsigned));
72
73 pipe_buffer_reference(pscreen, &mt->buffer, pb);
74 return &mt->base;
75 }
76
77 static struct pipe_texture *
78 nv20_miptree_create(struct pipe_screen *screen, const struct pipe_texture *pt)
79 {
80 struct pipe_winsys *ws = screen->winsys;
81 struct nv20_miptree *mt;
82 unsigned buf_usage = PIPE_BUFFER_USAGE_PIXEL |
83 NOUVEAU_BUFFER_USAGE_TEXTURE;
84
85 mt = MALLOC(sizeof(struct nv20_miptree));
86 if (!mt)
87 return NULL;
88 mt->base = *pt;
89 mt->base.refcount = 1;
90 mt->base.screen = screen;
91
92 /* Swizzled textures must be POT */
93 if (pt->width[0] & (pt->width[0] - 1) ||
94 pt->height[0] & (pt->height[0] - 1))
95 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
96 else
97 if (pt->tex_usage & (PIPE_TEXTURE_USAGE_PRIMARY |
98 PIPE_TEXTURE_USAGE_DISPLAY_TARGET))
99 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
100 else
101 if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
102 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
103 else {
104 switch (pt->format) {
105 /* TODO: Figure out which formats can be swizzled */
106 case PIPE_FORMAT_A8R8G8B8_UNORM:
107 case PIPE_FORMAT_X8R8G8B8_UNORM:
108 case PIPE_FORMAT_R16_SNORM:
109 break;
110 default:
111 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
112 }
113 }
114
115 if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
116 buf_usage |= PIPE_BUFFER_USAGE_CPU_READ_WRITE;
117
118 nv20_miptree_layout(mt);
119
120 mt->buffer = ws->buffer_create(ws, 256, buf_usage, mt->total_size);
121 if (!mt->buffer) {
122 FREE(mt);
123 return NULL;
124 }
125
126 return &mt->base;
127 }
128
129 static void
130 nv20_miptree_release(struct pipe_screen *screen, struct pipe_texture **pt)
131 {
132 struct pipe_texture *mt = *pt;
133
134 *pt = NULL;
135 if (--mt->refcount <= 0) {
136 struct nv20_miptree *nv20mt = (struct nv20_miptree *)mt;
137 int l;
138
139 pipe_buffer_reference(screen, &nv20mt->buffer, NULL);
140 for (l = 0; l <= mt->last_level; l++) {
141 if (nv20mt->level[l].image_offset)
142 FREE(nv20mt->level[l].image_offset);
143 }
144 FREE(nv20mt);
145 }
146 }
147
148 static struct pipe_surface *
149 nv20_miptree_surface_get(struct pipe_screen *screen, struct pipe_texture *pt,
150 unsigned face, unsigned level, unsigned zslice,
151 unsigned flags)
152 {
153 struct nv20_miptree *nv20mt = (struct nv20_miptree *)pt;
154 struct pipe_surface *ps;
155
156 ps = CALLOC_STRUCT(pipe_surface);
157 if (!ps)
158 return NULL;
159 pipe_texture_reference(&ps->texture, pt);
160 ps->format = pt->format;
161 ps->width = pt->width[level];
162 ps->height = pt->height[level];
163 ps->block = pt->block;
164 ps->nblocksx = pt->nblocksx[level];
165 ps->nblocksy = pt->nblocksy[level];
166 ps->stride = nv20mt->level[level].pitch;
167 ps->usage = flags;
168 ps->status = PIPE_SURFACE_STATUS_DEFINED;
169 ps->refcount = 1;
170
171 if (pt->target == PIPE_TEXTURE_CUBE) {
172 ps->offset = nv20mt->level[level].image_offset[face];
173 } else
174 if (pt->target == PIPE_TEXTURE_3D) {
175 ps->offset = nv20mt->level[level].image_offset[zslice];
176 } else {
177 ps->offset = nv20mt->level[level].image_offset[0];
178 }
179
180 return ps;
181 }
182
183 static void
184 nv20_miptree_surface_release(struct pipe_screen *pscreen,
185 struct pipe_surface **psurface)
186 {
187 struct pipe_surface *ps = *psurface;
188
189 *psurface = NULL;
190 if (--ps->refcount > 0)
191 return;
192
193 pipe_texture_reference(&ps->texture, NULL);
194 FREE(ps);
195 }
196
197 void nv20_screen_init_miptree_functions(struct pipe_screen *pscreen)
198 {
199 pscreen->texture_create = nv20_miptree_create;
200 pscreen->texture_blanket = nv20_miptree_blanket;
201 pscreen->texture_release = nv20_miptree_release;
202 pscreen->get_tex_surface = nv20_miptree_surface_get;
203 pscreen->tex_surface_release = nv20_miptree_surface_release;
204 }
205