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], depth = pt->depth[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->depth[l] = depth;
28
29 if (swizzled)
30 nv10mt->level[l].pitch = pt->width[l] * pt->cpp;
31 else
32 nv10mt->level[l].pitch = pt->width[0] * pt->cpp;
33 nv10mt->level[l].pitch = (nv10mt->level[l].pitch + 63) & ~63;
34
35 nv10mt->level[l].image_offset =
36 CALLOC(nr_faces, sizeof(unsigned));
37
38 width = MAX2(1, width >> 1);
39 height = MAX2(1, height >> 1);
40 depth = MAX2(1, depth >> 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->cpp = pt->cpp;
121 ps->width = pt->width[level];
122 ps->height = pt->height[level];
123 ps->pitch = nv10mt->level[level].pitch / ps->cpp;
124
125 if (pt->target == PIPE_TEXTURE_CUBE) {
126 ps->offset = nv10mt->level[level].image_offset[face];
127 } else {
128 ps->offset = nv10mt->level[level].image_offset[0];
129 }
130
131 return ps;
132 }
133
134 static void
135 nv10_miptree_surface_release(struct pipe_screen *screen,
136 struct pipe_surface **surface)
137 {
138 }
139
140 void nv10_screen_init_miptree_functions(struct pipe_screen *pscreen)
141 {
142 pscreen->texture_create = nv10_miptree_create;
143 pscreen->texture_release = nv10_miptree_release;
144 pscreen->get_tex_surface = nv10_miptree_surface_get;
145 pscreen->tex_surface_release = nv10_miptree_surface_release;
146 }
147