2fbedb6779a0700a7442ced54f70a6173b46bf66
[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 nouveau_device *dev = nouveau_screen(pscreen)->device;
33 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
34 struct pipe_texture *pt = &mt->base;
35 unsigned width = tmp->width[0], height = tmp->height[0];
36 unsigned depth = tmp->depth[0];
37 uint32_t tile_mode = 0, tile_flags = 0;
38 int ret, i, l;
39
40 mt->base = *tmp;
41 pipe_reference_init(&mt->base.reference, 1);
42 mt->base.screen = pscreen;
43
44 switch (pt->format) {
45 case PIPE_FORMAT_Z24S8_UNORM:
46 case PIPE_FORMAT_Z16_UNORM:
47 tile_flags = 0x2800;
48 break;
49 default:
50 tile_flags = 0x7000;
51 break;
52 }
53
54 switch (pt->target) {
55 case PIPE_TEXTURE_3D:
56 mt->image_nr = pt->depth[0];
57 break;
58 case PIPE_TEXTURE_CUBE:
59 mt->image_nr = 6;
60 break;
61 default:
62 mt->image_nr = 1;
63 break;
64 }
65
66 for (l = 0; l <= pt->last_level; l++) {
67 struct nv50_miptree_level *lvl = &mt->level[l];
68
69 pt->width[l] = width;
70 pt->height[l] = height;
71 pt->depth[l] = depth;
72 pt->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
73 pt->nblocksy[l] = pf_get_nblocksy(&pt->block, height);
74
75 lvl->image_offset = CALLOC(mt->image_nr, sizeof(int));
76 lvl->pitch = align(pt->width[l] * pt->block.size, 64);
77
78 width = MAX2(1, width >> 1);
79 height = MAX2(1, height >> 1);
80 depth = MAX2(1, depth >> 1);
81 }
82
83 for (i = 0; i < mt->image_nr; i++) {
84 for (l = 0; l <= pt->last_level; l++) {
85 struct nv50_miptree_level *lvl = &mt->level[l];
86 int size;
87
88 size = align(pt->width[l], 8) * pt->block.size;
89 size = align(size, 64);
90 size *= align(pt->height[l], 8) * pt->block.size;
91
92 lvl->image_offset[i] = mt->total_size;
93
94 mt->total_size += size;
95 }
96 }
97
98 ret = nouveau_bo_new_tile(dev, NOUVEAU_BO_VRAM, 256, mt->total_size,
99 tile_mode, tile_flags, &mt->bo);
100 if (ret) {
101 FREE(mt);
102 return NULL;
103 }
104
105 return &mt->base;
106 }
107
108 static struct pipe_texture *
109 nv50_miptree_blanket(struct pipe_screen *pscreen, const struct pipe_texture *pt,
110 const unsigned *stride, struct pipe_buffer *pb)
111 {
112 struct nouveau_bo *bo = nouveau_bo(pb);
113 struct nv50_miptree *mt;
114
115 /* Only supports 2D, non-mipmapped textures for the moment */
116 if (pt->target != PIPE_TEXTURE_2D || pt->last_level != 0 ||
117 pt->depth[0] != 1)
118 return NULL;
119
120 mt = CALLOC_STRUCT(nv50_miptree);
121 if (!mt)
122 return NULL;
123
124 mt->base = *pt;
125 pipe_reference_init(&mt->base.reference, 1);
126 mt->base.screen = pscreen;
127 mt->image_nr = 1;
128 mt->level[0].pitch = *stride;
129 mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
130
131 nouveau_bo_ref(bo, &mt->bo);
132 return &mt->base;
133 }
134
135 static void
136 nv50_miptree_destroy(struct pipe_texture *pt)
137 {
138 struct nv50_miptree *mt = nv50_miptree(pt);
139
140 nouveau_bo_ref(NULL, &mt->bo);
141 FREE(mt);
142 }
143
144 static struct pipe_surface *
145 nv50_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
146 unsigned face, unsigned level, unsigned zslice,
147 unsigned flags)
148 {
149 struct nv50_miptree *mt = nv50_miptree(pt);
150 struct nv50_miptree_level *lvl = &mt->level[level];
151 struct pipe_surface *ps;
152 int img;
153
154 if (pt->target == PIPE_TEXTURE_CUBE)
155 img = face;
156 else
157 if (pt->target == PIPE_TEXTURE_3D)
158 img = zslice;
159 else
160 img = 0;
161
162 ps = CALLOC_STRUCT(pipe_surface);
163 if (!ps)
164 return NULL;
165 pipe_texture_reference(&ps->texture, pt);
166 ps->format = pt->format;
167 ps->width = pt->width[level];
168 ps->height = pt->height[level];
169 ps->usage = flags;
170 pipe_reference_init(&ps->reference, 1);
171 ps->face = face;
172 ps->level = level;
173 ps->zslice = zslice;
174 ps->offset = lvl->image_offset[img];
175
176 return ps;
177 }
178
179 static void
180 nv50_miptree_surface_del(struct pipe_surface *ps)
181 {
182 struct nv50_surface *s = nv50_surface(ps);
183
184 pipe_texture_reference(&ps->texture, NULL);
185 FREE(s);
186 }
187
188 void
189 nv50_screen_init_miptree_functions(struct pipe_screen *pscreen)
190 {
191 pscreen->texture_create = nv50_miptree_create;
192 pscreen->texture_blanket = nv50_miptree_blanket;
193 pscreen->texture_destroy = nv50_miptree_destroy;
194 pscreen->get_tex_surface = nv50_miptree_surface_new;
195 pscreen->tex_surface_destroy = nv50_miptree_surface_del;
196 }
197