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