Merge remote branch 'upstream/gallium-0.1' into nouveau-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->nblocksx[l];
36 pitch = align_int(pitch, 64);
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 for (f = 0; f < nr_faces; f++) {
48 for (l = 0; l <= pt->last_level; l++) {
49 nv30mt->level[l].image_offset[f] = offset;
50 offset += nv30mt->level[l].pitch * pt->height[l];
51 }
52 }
53
54 nv30mt->total_size = offset;
55 }
56
57 static struct pipe_texture *
58 nv30_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
59 {
60 struct pipe_winsys *ws = pscreen->winsys;
61 struct nv30_miptree *mt;
62
63 mt = MALLOC(sizeof(struct nv30_miptree));
64 if (!mt)
65 return NULL;
66 mt->base = *pt;
67 mt->base.refcount = 1;
68 mt->base.screen = pscreen;
69
70 nv30_miptree_layout(mt);
71
72 mt->buffer = ws->buffer_create(ws, 256,
73 PIPE_BUFFER_USAGE_PIXEL |
74 NOUVEAU_BUFFER_USAGE_TEXTURE,
75 mt->total_size);
76 if (!mt->buffer) {
77 FREE(mt);
78 return NULL;
79 }
80
81 return &mt->base;
82 }
83
84 static void
85 nv30_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **pt)
86 {
87 struct pipe_winsys *ws = pscreen->winsys;
88 struct pipe_texture *mt = *pt;
89
90 *pt = NULL;
91 if (--mt->refcount <= 0) {
92 struct nv30_miptree *nv30mt = (struct nv30_miptree *)mt;
93 int l;
94
95 pipe_buffer_reference(ws, &nv30mt->buffer, NULL);
96 for (l = 0; l <= mt->last_level; l++) {
97 if (nv30mt->level[l].image_offset)
98 FREE(nv30mt->level[l].image_offset);
99 }
100 FREE(nv30mt);
101 }
102 }
103
104 static struct pipe_surface *
105 nv30_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
106 unsigned face, unsigned level, unsigned zslice,
107 unsigned flags)
108 {
109 struct pipe_winsys *ws = pscreen->winsys;
110 struct nv30_miptree *nv30mt = (struct nv30_miptree *)pt;
111 struct pipe_surface *ps;
112
113 ps = CALLOC_STRUCT(pipe_surface);
114 if (!ps)
115 return NULL;
116 pipe_texture_reference(&ps->texture, pt);
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->block = pt->block;
122 ps->nblocksx = pt->nblocksx[level];
123 ps->nblocksy = pt->nblocksy[level];
124 ps->stride = nv30mt->level[level].pitch;
125 ps->usage = flags;
126 ps->status = PIPE_SURFACE_STATUS_DEFINED;
127
128 if (pt->target == PIPE_TEXTURE_CUBE) {
129 ps->offset = nv30mt->level[level].image_offset[face];
130 } else
131 if (pt->target == PIPE_TEXTURE_3D) {
132 ps->offset = nv30mt->level[level].image_offset[zslice];
133 } else {
134 ps->offset = nv30mt->level[level].image_offset[0];
135 }
136
137 return ps;
138 }
139
140 static void
141 nv30_miptree_surface_del(struct pipe_screen *pscreen,
142 struct pipe_surface **psurface)
143 {
144 struct pipe_surface *ps = *psurface;
145
146 *psurface = NULL;
147 if (--ps->refcount > 0)
148 return;
149
150 pipe_texture_reference(&ps->texture, NULL);
151 pipe_buffer_reference(pscreen->winsys, &ps->buffer, NULL);
152 FREE(ps);
153 }
154
155 void
156 nv30_screen_init_miptree_functions(struct pipe_screen *pscreen)
157 {
158 pscreen->texture_create = nv30_miptree_create;
159 pscreen->texture_release = nv30_miptree_release;
160 pscreen->get_tex_surface = nv30_miptree_surface_new;
161 pscreen->tex_surface_release = nv30_miptree_surface_del;
162 }
163