Merge remote branch 'origin/master' into radeon-rewrite
[mesa.git] / src / gallium / drivers / nv50 / nv50_miptree.c
1 /*
2 * Copyright 2008 Ben Skeggs
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "pipe/p_state.h"
24 #include "pipe/p_defines.h"
25 #include "pipe/p_inlines.h"
26
27 #include "nv50_context.h"
28
29 static struct pipe_texture *
30 nv50_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *tmp)
31 {
32 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
33 struct pipe_texture *pt = &mt->base;
34 unsigned usage, width = tmp->width[0], height = tmp->height[0];
35 unsigned depth = tmp->depth[0];
36 int i, l;
37
38 mt->base = *tmp;
39 pipe_reference_init(&mt->base.reference, 1);
40 mt->base.screen = pscreen;
41
42 usage = PIPE_BUFFER_USAGE_PIXEL;
43 switch (pt->format) {
44 case PIPE_FORMAT_Z24S8_UNORM:
45 case PIPE_FORMAT_Z16_UNORM:
46 usage |= NOUVEAU_BUFFER_USAGE_ZETA;
47 break;
48 default:
49 break;
50 }
51
52 switch (pt->target) {
53 case PIPE_TEXTURE_3D:
54 mt->image_nr = pt->depth[0];
55 break;
56 case PIPE_TEXTURE_CUBE:
57 mt->image_nr = 6;
58 break;
59 default:
60 mt->image_nr = 1;
61 break;
62 }
63
64 for (l = 0; l <= pt->last_level; l++) {
65 struct nv50_miptree_level *lvl = &mt->level[l];
66
67 pt->width[l] = width;
68 pt->height[l] = height;
69 pt->depth[l] = depth;
70 pt->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
71 pt->nblocksy[l] = pf_get_nblocksy(&pt->block, height);
72
73 lvl->image_offset = CALLOC(mt->image_nr, sizeof(int));
74 lvl->pitch = align(pt->width[l] * pt->block.size, 64);
75
76 width = MAX2(1, width >> 1);
77 height = MAX2(1, height >> 1);
78 depth = MAX2(1, depth >> 1);
79 }
80
81 for (i = 0; i < mt->image_nr; i++) {
82 for (l = 0; l <= pt->last_level; l++) {
83 struct nv50_miptree_level *lvl = &mt->level[l];
84 int size;
85
86 size = align(pt->width[l], 8) * pt->block.size;
87 size = align(size, 64);
88 size *= align(pt->height[l], 8) * pt->block.size;
89
90 lvl->image_offset[i] = mt->total_size;
91
92 mt->total_size += size;
93 }
94 }
95
96 mt->buffer = pscreen->buffer_create(pscreen, 256, usage, mt->total_size);
97 if (!mt->buffer) {
98 FREE(mt);
99 return NULL;
100 }
101
102 return &mt->base;
103 }
104
105 static struct pipe_texture *
106 nv50_miptree_blanket(struct pipe_screen *pscreen, const struct pipe_texture *pt,
107 const unsigned *stride, struct pipe_buffer *pb)
108 {
109 struct nv50_miptree *mt;
110
111 /* Only supports 2D, non-mipmapped textures for the moment */
112 if (pt->target != PIPE_TEXTURE_2D || pt->last_level != 0 ||
113 pt->depth[0] != 1)
114 return NULL;
115
116 mt = CALLOC_STRUCT(nv50_miptree);
117 if (!mt)
118 return NULL;
119
120 mt->base = *pt;
121 pipe_reference_init(&mt->base.reference, 1);
122 mt->base.screen = pscreen;
123 mt->image_nr = 1;
124 mt->level[0].pitch = *stride;
125 mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
126
127 pipe_buffer_reference(&mt->buffer, pb);
128 return &mt->base;
129 }
130
131 static void
132 nv50_miptree_destroy(struct pipe_texture *pt)
133 {
134 struct nv50_miptree *mt = nv50_miptree(pt);
135
136 pipe_buffer_reference(&mt->buffer, NULL);
137 FREE(mt);
138 }
139
140 static struct pipe_surface *
141 nv50_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
142 unsigned face, unsigned level, unsigned zslice,
143 unsigned flags)
144 {
145 struct nv50_miptree *mt = nv50_miptree(pt);
146 struct nv50_miptree_level *lvl = &mt->level[level];
147 struct pipe_surface *ps;
148 int img;
149
150 if (pt->target == PIPE_TEXTURE_CUBE)
151 img = face;
152 else
153 if (pt->target == PIPE_TEXTURE_3D)
154 img = zslice;
155 else
156 img = 0;
157
158 ps = CALLOC_STRUCT(pipe_surface);
159 if (!ps)
160 return NULL;
161 pipe_texture_reference(&ps->texture, pt);
162 ps->format = pt->format;
163 ps->width = pt->width[level];
164 ps->height = pt->height[level];
165 ps->usage = flags;
166 pipe_reference_init(&ps->reference, 1);
167 ps->face = face;
168 ps->level = level;
169 ps->zslice = zslice;
170 ps->offset = lvl->image_offset[img];
171
172 return ps;
173 }
174
175 static void
176 nv50_miptree_surface_del(struct pipe_surface *ps)
177 {
178 struct nv50_surface *s = nv50_surface(ps);
179
180 pipe_texture_reference(&ps->texture, NULL);
181 FREE(s);
182 }
183
184 void
185 nv50_screen_init_miptree_functions(struct pipe_screen *pscreen)
186 {
187 pscreen->texture_create = nv50_miptree_create;
188 pscreen->texture_blanket = nv50_miptree_blanket;
189 pscreen->texture_destroy = nv50_miptree_destroy;
190 pscreen->get_tex_surface = nv50_miptree_surface_new;
191 pscreen->tex_surface_destroy = nv50_miptree_surface_del;
192 }
193