Merge branch 'gallium-0.2' of git://anongit.freedesktop.org/mesa/mesa into gallium-0.2
[mesa.git] / src / gallium / drivers / nv04 / nv04_miptree.c
1 #include "pipe/p_state.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_inlines.h"
4
5 #include "nv04_context.h"
6 #include "nv04_screen.h"
7
8 static void
9 nv04_miptree_layout(struct nv04_miptree *nv04mt)
10 {
11 struct pipe_texture *pt = &nv04mt->base;
12 uint width = pt->width[0], height = pt->height[0];
13 uint offset = 0;
14 int nr_faces, l;
15
16 nr_faces = 1;
17
18 for (l = 0; l <= pt->last_level; l++) {
19 pt->width[l] = width;
20 pt->height[l] = height;
21
22 pt->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
23 pt->nblocksy[l] = pf_get_nblocksy(&pt->block, height);
24
25 nv04mt->level[l].pitch = pt->width[0];
26 nv04mt->level[l].pitch = (nv04mt->level[l].pitch + 63) & ~63;
27
28 width = MAX2(1, width >> 1);
29 height = MAX2(1, height >> 1);
30 }
31
32 for (l = 0; l <= pt->last_level; l++) {
33
34 nv04mt->level[l].image_offset = offset;
35 offset += nv04mt->level[l].pitch * pt->height[l];
36 }
37
38 nv04mt->total_size = offset;
39 }
40
41 static struct pipe_texture *
42 nv04_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
43 {
44 struct pipe_winsys *ws = pscreen->winsys;
45 struct nv04_miptree *mt;
46
47 mt = MALLOC(sizeof(struct nv04_miptree));
48 if (!mt)
49 return NULL;
50 mt->base = *pt;
51 mt->base.refcount = 1;
52 mt->base.screen = pscreen;
53 mt->shadow_tex = NULL;
54 mt->shadow_surface = NULL;
55
56 //mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
57
58 nv04_miptree_layout(mt);
59
60 mt->buffer = ws->buffer_create(ws, 256, PIPE_BUFFER_USAGE_PIXEL |
61 NOUVEAU_BUFFER_USAGE_TEXTURE,
62 mt->total_size);
63 if (!mt->buffer) {
64 printf("failed %d byte alloc\n",mt->total_size);
65 FREE(mt);
66 return NULL;
67 }
68
69 return &mt->base;
70 }
71
72 static struct pipe_texture *
73 nv04_miptree_blanket(struct pipe_screen *pscreen, const struct pipe_texture *pt,
74 const unsigned *stride, struct pipe_buffer *pb)
75 {
76 struct nv04_miptree *mt;
77
78 /* Only supports 2D, non-mipmapped textures for the moment */
79 if (pt->target != PIPE_TEXTURE_2D || pt->last_level != 0 ||
80 pt->depth[0] != 1)
81 return NULL;
82
83 mt = CALLOC_STRUCT(nv04_miptree);
84 if (!mt)
85 return NULL;
86
87 mt->base = *pt;
88 mt->base.refcount = 1;
89 mt->base.screen = pscreen;
90 mt->level[0].pitch = stride[0];
91 mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
92
93 pipe_buffer_reference(pscreen, &mt->buffer, pb);
94 return &mt->base;
95 }
96
97 static void
98 nv04_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **ppt)
99 {
100 struct pipe_texture *pt = *ppt;
101 struct nv04_miptree *mt = (struct nv04_miptree *)pt;
102 int l;
103
104 *ppt = NULL;
105 if (--pt->refcount)
106 return;
107
108 pipe_buffer_reference(pscreen, &mt->buffer, NULL);
109 for (l = 0; l <= pt->last_level; l++) {
110 if (mt->level[l].image_offset)
111 FREE(mt->level[l].image_offset);
112 }
113
114 if (mt->shadow_tex) {
115 assert(mt->shadow_surface);
116 pscreen->tex_surface_release(pscreen, &mt->shadow_surface);
117 nv04_miptree_release(pscreen, &mt->shadow_tex);
118 }
119
120 FREE(mt);
121 }
122
123 static struct pipe_surface *
124 nv04_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
125 unsigned face, unsigned level, unsigned zslice,
126 unsigned flags)
127 {
128 struct nv04_miptree *nv04mt = (struct nv04_miptree *)pt;
129 struct pipe_surface *ps;
130
131 ps = CALLOC_STRUCT(pipe_surface);
132 if (!ps)
133 return NULL;
134 pipe_texture_reference(&ps->texture, pt);
135 ps->format = pt->format;
136 ps->width = pt->width[level];
137 ps->height = pt->height[level];
138 ps->block = pt->block;
139 ps->nblocksx = pt->nblocksx[level];
140 ps->nblocksy = pt->nblocksy[level];
141 ps->stride = nv04mt->level[level].pitch;
142 ps->usage = flags;
143 ps->status = PIPE_SURFACE_STATUS_DEFINED;
144 ps->refcount = 1;
145 ps->face = face;
146 ps->level = level;
147 ps->zslice = zslice;
148
149 ps->offset = nv04mt->level[level].image_offset;
150
151 return ps;
152 }
153
154 static void
155 nv04_miptree_surface_del(struct pipe_screen *pscreen,
156 struct pipe_surface **psurface)
157 {
158 struct pipe_surface *ps = *psurface;
159
160 *psurface = NULL;
161 if (--ps->refcount > 0)
162 return;
163
164 pipe_texture_reference(&ps->texture, NULL);
165 FREE(ps);
166 }
167
168 void
169 nv04_screen_init_miptree_functions(struct pipe_screen *pscreen)
170 {
171 pscreen->texture_create = nv04_miptree_create;
172 pscreen->texture_blanket = nv04_miptree_blanket;
173 pscreen->texture_release = nv04_miptree_release;
174 pscreen->get_tex_surface = nv04_miptree_surface_new;
175 pscreen->tex_surface_release = nv04_miptree_surface_del;
176 }
177