Merge remote branch 'upstream/gallium-0.2' into nouveau-gallium-0.2
[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_inlines.h"
4
5 #include "nv10_context.h"
6 #include "nv10_screen.h"
7
8 static void
9 nv10_miptree_layout(struct nv10_miptree *nv10mt)
10 {
11 struct pipe_texture *pt = &nv10mt->base;
12 boolean swizzled = FALSE;
13 uint width = pt->width[0], height = pt->height[0];
14 uint offset = 0;
15 int nr_faces, l, f;
16
17 if (pt->target == PIPE_TEXTURE_CUBE) {
18 nr_faces = 6;
19 } else {
20 nr_faces = 1;
21 }
22
23 for (l = 0; l <= pt->last_level; l++) {
24 pt->width[l] = width;
25 pt->height[l] = height;
26 pt->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
27 pt->nblocksy[l] = pf_get_nblocksy(&pt->block, height);
28
29 if (swizzled)
30 nv10mt->level[l].pitch = pt->nblocksx[l] * pt->block.size;
31 else
32 nv10mt->level[l].pitch = pt->nblocksx[0] * pt->block.size;
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
41 }
42
43 for (f = 0; f < nr_faces; f++) {
44 for (l = 0; l <= pt->last_level; l++) {
45 nv10mt->level[l].image_offset[f] = offset;
46 offset += nv10mt->level[l].pitch * pt->height[l];
47 }
48 }
49
50 nv10mt->total_size = offset;
51 }
52
53 static struct pipe_texture *
54 nv10_miptree_create(struct pipe_screen *screen, const struct pipe_texture *pt)
55 {
56 struct pipe_winsys *ws = screen->winsys;
57 struct nv10_miptree *mt;
58
59 mt = MALLOC(sizeof(struct nv10_miptree));
60 if (!mt)
61 return NULL;
62 mt->base = *pt;
63 mt->base.refcount = 1;
64 mt->base.screen = screen;
65
66 nv10_miptree_layout(mt);
67
68 mt->buffer = ws->buffer_create(ws, 256, PIPE_BUFFER_USAGE_PIXEL,
69 mt->total_size);
70 if (!mt->buffer) {
71 FREE(mt);
72 return NULL;
73 }
74
75 return &mt->base;
76 }
77
78 static void
79 nv10_miptree_release(struct pipe_screen *screen, struct pipe_texture **pt)
80 {
81 struct pipe_texture *mt = *pt;
82
83 *pt = NULL;
84 if (--mt->refcount <= 0) {
85 struct nv10_miptree *nv10mt = (struct nv10_miptree *)mt;
86 int l;
87
88 pipe_buffer_reference(screen, &nv10mt->buffer, NULL);
89 for (l = 0; l <= mt->last_level; l++) {
90 if (nv10mt->level[l].image_offset)
91 FREE(nv10mt->level[l].image_offset);
92 }
93 FREE(nv10mt);
94 }
95 }
96
97 static void
98 nv10_miptree_update(struct pipe_context *pipe, struct pipe_texture *mt,
99 uint face, uint levels)
100 {
101 }
102
103
104 static struct pipe_surface *
105 nv10_miptree_surface_get(struct pipe_screen *screen, struct pipe_texture *pt,
106 unsigned face, unsigned level, unsigned zslice,
107 unsigned flags)
108 {
109 struct pipe_winsys *ws = screen->winsys;
110 struct nv10_miptree *nv10mt = (struct nv10_miptree *)pt;
111 struct pipe_surface *ps;
112
113 ps = ws->surface_alloc(ws);
114 if (!ps)
115 return NULL;
116 pipe_buffer_reference(screen, &ps->buffer, nv10mt->buffer);
117 ps->format = pt->format;
118 ps->width = pt->width[level];
119 ps->height = pt->height[level];
120 ps->block = pt->block;
121 ps->nblocksx = pt->nblocksx[level];
122 ps->nblocksy = pt->nblocksy[level];
123 ps->stride = nv10mt->level[level].pitch;
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