nv50: any cpu access to a texture is done on its backing images
[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 pipe_winsys *ws = pscreen->winsys;
33 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
34 struct pipe_texture *pt = &mt->base;
35 unsigned usage, width = tmp->width[0], height = tmp->height[0];
36 unsigned depth = tmp->depth[0];
37 int i, l;
38
39 mt->base = *tmp;
40 mt->base.refcount = 1;
41 mt->base.screen = pscreen;
42
43 usage = PIPE_BUFFER_USAGE_PIXEL;
44 switch (pt->format) {
45 case PIPE_FORMAT_Z24S8_UNORM:
46 case PIPE_FORMAT_Z16_UNORM:
47 usage |= NOUVEAU_BUFFER_USAGE_ZETA;
48 break;
49 default:
50 break;
51 }
52
53 switch (pt->target) {
54 case PIPE_TEXTURE_3D:
55 mt->image_nr = pt->depth[0];
56 break;
57 case PIPE_TEXTURE_CUBE:
58 mt->image_nr = 6;
59 break;
60 default:
61 mt->image_nr = 1;
62 break;
63 }
64
65 for (l = 0; l <= pt->last_level; l++) {
66 struct nv50_miptree_level *lvl = &mt->level[l];
67
68 pt->width[l] = width;
69 pt->height[l] = height;
70 pt->depth[l] = depth;
71 pt->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
72 pt->nblocksy[l] = pf_get_nblocksy(&pt->block, width);
73
74 lvl->image_offset = CALLOC(mt->image_nr, sizeof(int));
75 lvl->image = CALLOC(mt->image_nr, sizeof(struct pipe_buffer *));
76 }
77
78 for (i = 0; i < mt->image_nr; i++) {
79 for (l = 0; l <= pt->last_level; l++) {
80 struct nv50_miptree_level *lvl = &mt->level[l];
81 int size;
82
83 size = align(pt->width[l], 8) * pt->block.size;
84 size = align(size, 64);
85 size *= align(pt->height[l], 8) * pt->block.size;
86
87 lvl->image[i] = ws->buffer_create(ws, 256, 0, size);
88 lvl->image_offset[i] = mt->total_size;
89
90 mt->total_size += size;
91 }
92 }
93
94 mt->buffer = ws->buffer_create(ws, 256, usage, mt->total_size);
95 if (!mt->buffer) {
96 FREE(mt);
97 return NULL;
98 }
99
100 return &mt->base;
101 }
102
103 static void
104 nv50_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **ppt)
105 {
106 struct pipe_texture *pt = *ppt;
107
108 *ppt = NULL;
109
110 if (--pt->refcount <= 0) {
111 struct nv50_miptree *mt = nv50_miptree(pt);
112
113 pipe_buffer_reference(pscreen, &mt->buffer, NULL);
114 FREE(mt);
115 }
116 }
117
118 void
119 nv50_miptree_sync(struct pipe_screen *pscreen, struct nv50_miptree *mt,
120 unsigned level, unsigned image)
121 {
122 struct nouveau_winsys *nvws = nv50_screen(pscreen)->nvws;
123 struct nv50_miptree_level *lvl = &mt->level[level];
124 struct pipe_surface *dst, *src;
125 unsigned face = 0, zslice = 0;
126
127 if (!lvl->image_dirty_cpu & (1 << image))
128 return;
129
130 if (mt->base.target == PIPE_TEXTURE_CUBE)
131 face = image;
132 else
133 if (mt->base.target == PIPE_TEXTURE_3D)
134 zslice = image;
135
136 /* Mark as clean already - so we don't continually call this function
137 * trying to get a GPU_WRITE pipe_surface!
138 */
139 lvl->image_dirty_cpu &= ~(1 << image);
140
141 dst = pscreen->get_tex_surface(pscreen, &mt->base, face, level, zslice,
142 PIPE_BUFFER_USAGE_GPU_WRITE);
143 /* Pretend we're doing CPU access so we get the backing pipe_surface
144 * and not a view into the larger miptree.
145 */
146 src = pscreen->get_tex_surface(pscreen, &mt->base, face, level, zslice,
147 PIPE_BUFFER_USAGE_CPU_READ);
148
149 nvws->surface_copy(nvws, dst, 0, 0, src, 0, 0, dst->width, dst->height);
150
151 pscreen->tex_surface_release(pscreen, &dst);
152 pscreen->tex_surface_release(pscreen, &src);
153 }
154
155 static struct pipe_surface *
156 nv50_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
157 unsigned face, unsigned level, unsigned zslice,
158 unsigned flags)
159 {
160 struct nv50_miptree *mt = nv50_miptree(pt);
161 struct nv50_miptree_level *lvl = &mt->level[level];
162 struct nv50_surface *s;
163 struct pipe_surface *ps;
164 int img;
165
166 if (pt->target == PIPE_TEXTURE_CUBE)
167 img = face;
168 else
169 if (pt->target == PIPE_TEXTURE_3D)
170 img = zslice;
171 else
172 img = 0;
173
174 s = CALLOC_STRUCT(nv50_surface);
175 if (!s)
176 return NULL;
177 ps = &s->base;
178
179 ps->refcount = 1;
180 ps->winsys = pscreen->winsys;
181 ps->format = pt->format;
182 ps->width = pt->width[level];
183 ps->height = pt->height[level];
184 ps->block = pt->block;
185 ps->nblocksx = pt->nblocksx[level];
186 ps->nblocksy = pt->nblocksy[level];
187 ps->stride = ps->width * ps->block.size;
188 ps->usage = flags;
189 ps->status = PIPE_SURFACE_STATUS_DEFINED;
190
191 if (flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE) {
192 assert(!(flags & PIPE_BUFFER_USAGE_GPU_READ_WRITE));
193 assert(!(lvl->image_dirty_cpu & (1 << img)));
194
195 ps->offset = 0;
196 pipe_texture_reference(&ps->texture, pt);
197 pipe_buffer_reference(pscreen, &ps->buffer, lvl->image[img]);
198
199 if (flags & PIPE_BUFFER_USAGE_CPU_WRITE)
200 lvl->image_dirty_cpu |= (1 << img);
201 } else {
202 nv50_miptree_sync(pscreen, mt, level, img);
203
204 ps->offset = lvl->image_offset[img];
205 pipe_texture_reference(&ps->texture, pt);
206 pipe_buffer_reference(pscreen, &ps->buffer, mt->buffer);
207
208 if (flags & PIPE_BUFFER_USAGE_GPU_WRITE)
209 lvl->image_dirty_gpu |= (1 << img);
210 }
211
212 return ps;
213 }
214
215 static void
216 nv50_miptree_surface_del(struct pipe_screen *pscreen,
217 struct pipe_surface **psurface)
218 {
219 struct pipe_surface *ps = *psurface;
220 struct nv50_surface *s = nv50_surface(ps);
221
222 *psurface = NULL;
223
224 if (--ps->refcount <= 0) {
225 pipe_texture_reference(&ps->texture, NULL);
226 pipe_buffer_reference(pscreen, &ps->buffer, NULL);
227 FREE(s);
228 }
229 }
230
231 void
232 nv50_screen_init_miptree_functions(struct pipe_screen *pscreen)
233 {
234 pscreen->texture_create = nv50_miptree_create;
235 pscreen->texture_release = nv50_miptree_release;
236 pscreen->get_tex_surface = nv50_miptree_surface_new;
237 pscreen->tex_surface_release = nv50_miptree_surface_del;
238 }
239