gallium: fix more statetrackers/drivers for not using texture width/height/depth...
[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 #include "util/u_math.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->width0, height = pt->height0;
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
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 = u_minify(width, 1);
39 height = u_minify(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 * u_minify(pt->height0, 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->depth0 != 1)
62 return NULL;
63
64 mt = CALLOC_STRUCT(nv10_miptree);
65 if (!mt)
66 return NULL;
67
68 mt->base = *pt;
69 pipe_reference_init(&mt->base.reference, 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(&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 nv10_miptree *mt;
82
83 mt = MALLOC(sizeof(struct nv10_miptree));
84 if (!mt)
85 return NULL;
86 mt->base = *pt;
87 pipe_reference_init(&mt->base.reference, 1);
88 mt->base.screen = screen;
89
90 nv10_miptree_layout(mt);
91
92 mt->buffer = screen->buffer_create(screen, 256, PIPE_BUFFER_USAGE_PIXEL,
93 mt->total_size);
94 if (!mt->buffer) {
95 FREE(mt);
96 return NULL;
97 }
98
99 return &mt->base;
100 }
101
102 static void
103 nv10_miptree_destroy(struct pipe_texture *pt)
104 {
105 struct nv10_miptree *nv10mt = (struct nv10_miptree *)pt;
106 int l;
107
108 pipe_buffer_reference(&nv10mt->buffer, NULL);
109 for (l = 0; l <= pt->last_level; l++) {
110 if (nv10mt->level[l].image_offset)
111 FREE(nv10mt->level[l].image_offset);
112 }
113 FREE(nv10mt);
114 }
115
116 static void
117 nv10_miptree_update(struct pipe_context *pipe, struct pipe_texture *mt,
118 uint face, uint levels)
119 {
120 }
121
122
123 static struct pipe_surface *
124 nv10_miptree_surface_get(struct pipe_screen *screen, struct pipe_texture *pt,
125 unsigned face, unsigned level, unsigned zslice,
126 unsigned flags)
127 {
128 struct nv10_miptree *nv10mt = (struct nv10_miptree *)pt;
129 struct nv04_surface *ns;
130
131 ns = CALLOC_STRUCT(nv04_surface);
132 if (!ns)
133 return NULL;
134 pipe_texture_reference(&ns->base.texture, pt);
135 ns->base.format = pt->format;
136 ns->base.width = u_minify(pt->width0, level);
137 ns->base.height = u_minify(pt->height0, level);
138 ns->base.usage = flags;
139 pipe_reference_init(&ns->base.reference, 1);
140 ns->base.face = face;
141 ns->base.level = level;
142 ns->base.zslice = zslice;
143 ns->pitch = nv10mt->level[level].pitch;
144
145 if (pt->target == PIPE_TEXTURE_CUBE) {
146 ns->base.offset = nv10mt->level[level].image_offset[face];
147 } else {
148 ns->base.offset = nv10mt->level[level].image_offset[0];
149 }
150
151 return &ns->base;
152 }
153
154 static void
155 nv10_miptree_surface_destroy(struct pipe_surface *surface)
156 {
157 }
158
159 void nv10_screen_init_miptree_functions(struct pipe_screen *pscreen)
160 {
161 pscreen->texture_create = nv10_miptree_create;
162 pscreen->texture_blanket = nv10_miptree_blanket;
163 pscreen->texture_destroy = nv10_miptree_destroy;
164 pscreen->get_tex_surface = nv10_miptree_surface_get;
165 pscreen->tex_surface_destroy = nv10_miptree_surface_destroy;
166 }
167