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