Merge commit 'origin/gallium-0.1'
[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
54 //mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
55
56 nv04_miptree_layout(mt);
57
58 mt->buffer = ws->buffer_create(ws, 256, PIPE_BUFFER_USAGE_PIXEL |
59 NOUVEAU_BUFFER_USAGE_TEXTURE,
60 mt->total_size);
61 if (!mt->buffer) {
62 printf("failed %d byte alloc\n",mt->total_size);
63 FREE(mt);
64 return NULL;
65 }
66
67 return &mt->base;
68 }
69
70 static struct pipe_texture *
71 nv04_miptree_blanket(struct pipe_screen *pscreen, const struct pipe_texture *pt,
72 const unsigned *stride, struct pipe_buffer *pb)
73 {
74 struct nv04_miptree *mt;
75
76 /* Only supports 2D, non-mipmapped textures for the moment */
77 if (pt->target != PIPE_TEXTURE_2D || pt->last_level != 0 ||
78 pt->depth[0] != 1)
79 return NULL;
80
81 mt = CALLOC_STRUCT(nv04_miptree);
82 if (!mt)
83 return NULL;
84
85 mt->base = *pt;
86 mt->base.refcount = 1;
87 mt->base.screen = pscreen;
88 mt->level[0].pitch = stride[0];
89 mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
90
91 pipe_buffer_reference(pscreen, &mt->buffer, pb);
92 return &mt->base;
93 }
94
95 static void
96 nv04_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **ppt)
97 {
98 struct pipe_texture *pt = *ppt;
99 struct nv04_miptree *mt = (struct nv04_miptree *)pt;
100 int l;
101
102 *ppt = NULL;
103 if (--pt->refcount)
104 return;
105
106 pipe_buffer_reference(pscreen, &mt->buffer, NULL);
107 for (l = 0; l <= pt->last_level; l++) {
108 if (mt->level[l].image_offset)
109 FREE(mt->level[l].image_offset);
110 }
111
112 FREE(mt);
113 }
114
115 static struct pipe_surface *
116 nv04_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
117 unsigned face, unsigned level, unsigned zslice,
118 unsigned flags)
119 {
120 struct nv04_miptree *nv04mt = (struct nv04_miptree *)pt;
121 struct nv04_surface *ns;
122
123 ns = CALLOC_STRUCT(nv04_surface);
124 if (!ns)
125 return NULL;
126 pipe_texture_reference(&ns->base.texture, pt);
127 ns->base.format = pt->format;
128 ns->base.width = pt->width[level];
129 ns->base.height = pt->height[level];
130 ns->base.usage = flags;
131 ns->base.status = PIPE_SURFACE_STATUS_DEFINED;
132 ns->base.refcount = 1;
133 ns->base.face = face;
134 ns->base.level = level;
135 ns->base.zslice = zslice;
136 ns->pitch = nv04mt->level[level].pitch;
137
138 ns->base.offset = nv04mt->level[level].image_offset;
139
140 return &ns->base;
141 }
142
143 static void
144 nv04_miptree_surface_del(struct pipe_screen *pscreen,
145 struct pipe_surface **psurface)
146 {
147 struct pipe_surface *ps = *psurface;
148
149 *psurface = NULL;
150 if (--ps->refcount > 0)
151 return;
152
153 pipe_texture_reference(&ps->texture, NULL);
154 FREE(ps);
155 }
156
157 void
158 nv04_screen_init_miptree_functions(struct pipe_screen *pscreen)
159 {
160 pscreen->texture_create = nv04_miptree_create;
161 pscreen->texture_blanket = nv04_miptree_blanket;
162 pscreen->texture_release = nv04_miptree_release;
163 pscreen->get_tex_surface = nv04_miptree_surface_new;
164 pscreen->tex_surface_release = nv04_miptree_surface_del;
165 }
166