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