Merge remote branch 'upstream/gallium-0.1' into gallium-0.1
[mesa.git] / src / gallium / drivers / nv30 / nv30_miptree.c
1 #include "pipe/p_state.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_util.h"
4 #include "pipe/p_inlines.h"
5
6 #include "nv30_context.h"
7
8 static void
9 nv30_miptree_layout(struct nv30_miptree *nv30mt)
10 {
11 struct pipe_texture *pt = &nv30mt->base;
12 boolean swizzled = FALSE;
13 uint width = pt->width[0], height = pt->height[0], depth = pt->depth[0];
14 uint offset = 0;
15 int nr_faces, l, f, pitch;
16
17 if (pt->target == PIPE_TEXTURE_CUBE) {
18 nr_faces = 6;
19 } else
20 if (pt->target == PIPE_TEXTURE_3D) {
21 nr_faces = pt->depth[0];
22 } else {
23 nr_faces = 1;
24 }
25
26 pitch = pt->width[0];
27 for (l = 0; l <= pt->last_level; l++) {
28 pt->width[l] = width;
29 pt->height[l] = height;
30 pt->depth[l] = depth;
31 pt->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
32 pt->nblocksy[l] = pf_get_nblocksy(&pt->block, height);
33
34 if (swizzled)
35 pitch = pt->width[l];
36 pitch = (pitch + 63) & ~63;
37
38 nv30mt->level[l].pitch = pitch * pt->block.size;
39 nv30mt->level[l].image_offset =
40 CALLOC(nr_faces, sizeof(unsigned));
41
42 width = MAX2(1, width >> 1);
43 height = MAX2(1, height >> 1);
44 depth = MAX2(1, depth >> 1);
45
46 }
47
48 for (f = 0; f < nr_faces; f++) {
49 for (l = 0; l <= pt->last_level; l++) {
50 nv30mt->level[l].image_offset[f] = offset;
51 offset += nv30mt->level[l].pitch * pt->height[l];
52 }
53 }
54
55 nv30mt->total_size = offset;
56 }
57
58 static struct pipe_texture *
59 nv30_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
60 {
61 struct pipe_winsys *ws = pscreen->winsys;
62 struct nv30_miptree *mt;
63
64 mt = MALLOC(sizeof(struct nv30_miptree));
65 if (!mt)
66 return NULL;
67 mt->base = *pt;
68 mt->base.refcount = 1;
69 mt->base.screen = pscreen;
70
71 nv30_miptree_layout(mt);
72
73 mt->buffer = ws->buffer_create(ws, 256,
74 PIPE_BUFFER_USAGE_PIXEL |
75 NOUVEAU_BUFFER_USAGE_TEXTURE,
76 mt->total_size);
77 if (!mt->buffer) {
78 FREE(mt);
79 return NULL;
80 }
81
82 return &mt->base;
83 }
84
85 static void
86 nv30_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **pt)
87 {
88 struct pipe_winsys *ws = pscreen->winsys;
89 struct pipe_texture *mt = *pt;
90
91 *pt = NULL;
92 if (--mt->refcount <= 0) {
93 struct nv30_miptree *nv30mt = (struct nv30_miptree *)mt;
94 int l;
95
96 pipe_buffer_reference(ws, &nv30mt->buffer, NULL);
97 for (l = 0; l <= mt->last_level; l++) {
98 if (nv30mt->level[l].image_offset)
99 FREE(nv30mt->level[l].image_offset);
100 }
101 FREE(nv30mt);
102 }
103 }
104
105 static struct pipe_surface *
106 nv30_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
107 unsigned face, unsigned level, unsigned zslice,
108 unsigned flags)
109 {
110 struct pipe_winsys *ws = pscreen->winsys;
111 struct nv30_miptree *nv30mt = (struct nv30_miptree *)pt;
112 struct pipe_surface *ps;
113
114 ps = ws->surface_alloc(ws);
115 if (!ps)
116 return NULL;
117 pipe_buffer_reference(ws, &ps->buffer, nv30mt->buffer);
118 ps->format = pt->format;
119 ps->width = pt->width[level];
120 ps->height = pt->height[level];
121 ps->nblocksx = pt->nblocksx[level];
122 ps->nblocksy = pt->nblocksy[level];
123 ps->block = pt->block;
124 ps->stride = nv30mt->level[level].pitch;
125
126 if (pt->target == PIPE_TEXTURE_CUBE) {
127 ps->offset = nv30mt->level[level].image_offset[face];
128 } else
129 if (pt->target == PIPE_TEXTURE_3D) {
130 ps->offset = nv30mt->level[level].image_offset[zslice];
131 } else {
132 ps->offset = nv30mt->level[level].image_offset[0];
133 }
134
135 return ps;
136 }
137
138 static void
139 nv30_miptree_surface_del(struct pipe_screen *pscreen,
140 struct pipe_surface **psurface)
141 {
142 }
143
144 void
145 nv30_screen_init_miptree_functions(struct pipe_screen *pscreen)
146 {
147 pscreen->texture_create = nv30_miptree_create;
148 pscreen->texture_release = nv30_miptree_release;
149 pscreen->get_tex_surface = nv30_miptree_surface_new;
150 pscreen->tex_surface_release = nv30_miptree_surface_del;
151 }
152