Merge commit 'origin/gallium-master-merge'
[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_blanket(struct pipe_screen *pscreen, const struct pipe_texture *pt,
55 const unsigned *stride, struct pipe_buffer *pb)
56 {
57 struct nv10_miptree *mt;
58
59 /* Only supports 2D, non-mipmapped textures for the moment */
60 if (pt->target != PIPE_TEXTURE_2D || pt->last_level != 0 ||
61 pt->depth[0] != 1)
62 return NULL;
63
64 mt = CALLOC_STRUCT(nv10_miptree);
65 if (!mt)
66 return NULL;
67
68 mt->base = *pt;
69 mt->base.refcount = 1;
70 mt->base.screen = pscreen;
71 mt->level[0].pitch = stride[0];
72 mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
73
74 pipe_buffer_reference(pscreen, &mt->buffer, pb);
75 return &mt->base;
76 }
77
78 static struct pipe_texture *
79 nv10_miptree_create(struct pipe_screen *screen, const struct pipe_texture *pt)
80 {
81 struct pipe_winsys *ws = screen->winsys;
82 struct nv10_miptree *mt;
83
84 mt = MALLOC(sizeof(struct nv10_miptree));
85 if (!mt)
86 return NULL;
87 mt->base = *pt;
88 mt->base.refcount = 1;
89 mt->base.screen = screen;
90
91 nv10_miptree_layout(mt);
92
93 mt->buffer = ws->buffer_create(ws, 256, PIPE_BUFFER_USAGE_PIXEL,
94 mt->total_size);
95 if (!mt->buffer) {
96 FREE(mt);
97 return NULL;
98 }
99
100 return &mt->base;
101 }
102
103 static void
104 nv10_miptree_release(struct pipe_screen *screen, struct pipe_texture **pt)
105 {
106 struct pipe_texture *mt = *pt;
107
108 *pt = NULL;
109 if (--mt->refcount <= 0) {
110 struct nv10_miptree *nv10mt = (struct nv10_miptree *)mt;
111 int l;
112
113 pipe_buffer_reference(screen, &nv10mt->buffer, NULL);
114 for (l = 0; l <= mt->last_level; l++) {
115 if (nv10mt->level[l].image_offset)
116 FREE(nv10mt->level[l].image_offset);
117 }
118 FREE(nv10mt);
119 }
120 }
121
122 static void
123 nv10_miptree_update(struct pipe_context *pipe, struct pipe_texture *mt,
124 uint face, uint levels)
125 {
126 }
127
128
129 static struct pipe_surface *
130 nv10_miptree_surface_get(struct pipe_screen *screen, struct pipe_texture *pt,
131 unsigned face, unsigned level, unsigned zslice,
132 unsigned flags)
133 {
134 struct pipe_winsys *ws = screen->winsys;
135 struct nv10_miptree *nv10mt = (struct nv10_miptree *)pt;
136 struct pipe_surface *ps;
137
138 ps = CALLOC_STRUCT(pipe_surface);
139 if (!ps)
140 return NULL;
141 pipe_texture_reference(&ps->texture, pt);
142 ps->format = pt->format;
143 ps->width = pt->width[level];
144 ps->height = pt->height[level];
145 ps->block = pt->block;
146 ps->nblocksx = pt->nblocksx[level];
147 ps->nblocksy = pt->nblocksy[level];
148 ps->stride = nv10mt->level[level].pitch;
149 ps->refcount = 1;
150
151 if (pt->target == PIPE_TEXTURE_CUBE) {
152 ps->offset = nv10mt->level[level].image_offset[face];
153 } else {
154 ps->offset = nv10mt->level[level].image_offset[0];
155 }
156
157 return ps;
158 }
159
160 static void
161 nv10_miptree_surface_release(struct pipe_screen *screen,
162 struct pipe_surface **surface)
163 {
164 }
165
166 void nv10_screen_init_miptree_functions(struct pipe_screen *pscreen)
167 {
168 pscreen->texture_create = nv10_miptree_create;
169 pscreen->texture_blanket = nv10_miptree_blanket;
170 pscreen->texture_release = nv10_miptree_release;
171 pscreen->get_tex_surface = nv10_miptree_surface_get;
172 pscreen->tex_surface_release = nv10_miptree_surface_release;
173 }
174