gallium: Add texture usage flags, special-case allocation of display targets
[mesa.git] / src / gallium / drivers / softpipe / sp_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
31 */
32
33 #include "pipe/p_context.h"
34 #include "pipe/p_defines.h"
35 #include "pipe/p_inlines.h"
36 #include "pipe/p_util.h"
37 #include "pipe/p_winsys.h"
38
39 #include "sp_context.h"
40 #include "sp_state.h"
41 #include "sp_texture.h"
42 #include "sp_tile_cache.h"
43 #include "sp_screen.h"
44
45
46 /* Simple, maximally packed layout.
47 */
48
49 static unsigned minify( unsigned d )
50 {
51 return MAX2(1, d>>1);
52 }
53
54
55 /* Conventional allocation path for non-display textures:
56 */
57 static boolean
58 softpipe_texture_layout(struct pipe_screen *screen,
59 struct softpipe_texture * spt)
60 {
61 struct pipe_winsys *ws = screen->winsys;
62 struct pipe_texture *pt = &spt->base;
63 unsigned level;
64 unsigned width = pt->width[0];
65 unsigned height = pt->height[0];
66 unsigned depth = pt->depth[0];
67
68 unsigned buffer_size = 0;
69
70 for (level = 0; level <= pt->last_level; level++) {
71 pt->width[level] = width;
72 pt->height[level] = height;
73 pt->depth[level] = depth;
74 spt->pitch[level] = width;
75
76 spt->level_offset[level] = buffer_size;
77
78 buffer_size += (((pt->compressed) ? MAX2(1, height/4) : height) *
79 ((pt->target == PIPE_TEXTURE_CUBE) ? 6 : depth) *
80 width * pt->cpp);
81
82 width = minify(width);
83 height = minify(height);
84 depth = minify(depth);
85 }
86
87 spt->buffer = ws->buffer_create(ws, 32,
88 PIPE_BUFFER_USAGE_PIXEL,
89 buffer_size);
90
91 return spt->buffer != NULL;
92 }
93
94
95
96 /* Hack it up to use the old winsys->surface_alloc_storage()
97 * method for now:
98 */
99 static boolean
100 softpipe_displaytarget_layout(struct pipe_screen *screen,
101 struct softpipe_texture * spt)
102 {
103 struct pipe_winsys *ws = screen->winsys;
104 struct pipe_surface surf;
105 unsigned flags = (PIPE_BUFFER_USAGE_CPU_READ |
106 PIPE_BUFFER_USAGE_CPU_WRITE |
107 PIPE_BUFFER_USAGE_GPU_READ |
108 PIPE_BUFFER_USAGE_GPU_WRITE);
109
110
111 memset(&surf, 0, sizeof(surf));
112
113 ws->surface_alloc_storage( ws,
114 &surf,
115 spt->base.width[0],
116 spt->base.height[0],
117 spt->base.format,
118 flags);
119
120 /* Now extract the goodies:
121 */
122 spt->buffer = surf.buffer;
123 spt->pitch[0] = surf.pitch;
124
125 return spt->buffer != NULL;
126 }
127
128
129
130
131
132 static struct pipe_texture *
133 softpipe_texture_create(struct pipe_screen *screen,
134 const struct pipe_texture *templat)
135 {
136 struct softpipe_texture *spt = CALLOC_STRUCT(softpipe_texture);
137 if (!spt)
138 return NULL;
139
140 spt->base = *templat;
141 spt->base.refcount = 1;
142 spt->base.screen = screen;
143
144 if (spt->base.tex_usage & PIPE_TEXTURE_USAGE_DISPLAY_TARGET) {
145 if (!softpipe_displaytarget_layout(screen, spt))
146 goto fail;
147 }
148 else {
149 if (!softpipe_texture_layout(screen, spt))
150 goto fail;
151 }
152
153 assert(spt->base.refcount == 1);
154 return &spt->base;
155
156 fail:
157 FREE(spt);
158 return NULL;
159 }
160
161
162 static void
163 softpipe_texture_release(struct pipe_screen *screen,
164 struct pipe_texture **pt)
165 {
166 if (!*pt)
167 return;
168
169 if (--(*pt)->refcount <= 0) {
170 struct softpipe_texture *spt = softpipe_texture(*pt);
171
172 pipe_buffer_reference(screen->winsys, &spt->buffer, NULL);
173 FREE(spt);
174 }
175 *pt = NULL;
176 }
177
178
179 static struct pipe_surface *
180 softpipe_get_tex_surface(struct pipe_screen *screen,
181 struct pipe_texture *pt,
182 unsigned face, unsigned level, unsigned zslice,
183 unsigned usage)
184 {
185 struct pipe_winsys *ws = screen->winsys;
186 struct softpipe_texture *spt = softpipe_texture(pt);
187 struct pipe_surface *ps;
188
189 assert(level <= pt->last_level);
190
191 ps = ws->surface_alloc(ws);
192 if (ps) {
193 assert(ps->refcount);
194 assert(ps->winsys);
195 pipe_buffer_reference(ws, &ps->buffer, spt->buffer);
196 ps->format = pt->format;
197 ps->cpp = pt->cpp;
198 ps->width = pt->width[level];
199 ps->height = pt->height[level];
200 ps->pitch = ps->width;
201 ps->offset = spt->level_offset[level];
202 ps->usage = usage;
203
204 /* Because we are softpipe, anything that the state tracker
205 * thought was going to be done with the GPU will actually get
206 * done with the CPU. Let's adjust the flags to take that into
207 * account.
208 */
209 if (ps->usage & PIPE_BUFFER_USAGE_GPU_WRITE)
210 ps->usage |= PIPE_BUFFER_USAGE_CPU_WRITE;
211
212 if (ps->usage & PIPE_BUFFER_USAGE_GPU_READ)
213 ps->usage |= PIPE_BUFFER_USAGE_CPU_READ;
214
215
216 pipe_texture_reference(&ps->texture, pt);
217 ps->face = face;
218 ps->level = level;
219 ps->zslice = zslice;
220
221 if (pt->target == PIPE_TEXTURE_CUBE || pt->target == PIPE_TEXTURE_3D) {
222 ps->offset += ((pt->target == PIPE_TEXTURE_CUBE) ? face : zslice) *
223 (pt->compressed ? ps->height/4 : ps->height) *
224 ps->width * ps->cpp;
225 }
226 else {
227 assert(face == 0);
228 assert(zslice == 0);
229 }
230 }
231 return ps;
232 }
233
234
235 static void
236 softpipe_tex_surface_release(struct pipe_screen *screen,
237 struct pipe_surface **s)
238 {
239 /* Effectively do the texture_update work here - if texture images
240 * needed post-processing to put them into hardware layout, this is
241 * where it would happen. For softpipe, nothing to do.
242 */
243 assert ((*s)->texture);
244 pipe_texture_reference(&(*s)->texture, NULL);
245
246 screen->winsys->surface_release(screen->winsys, s);
247 }
248
249
250 static void *
251 softpipe_surface_map( struct pipe_screen *screen,
252 struct pipe_surface *surface,
253 unsigned flags )
254 {
255 ubyte *map;
256
257 if (flags & ~surface->usage) {
258 assert(0);
259 return NULL;
260 }
261
262 map = screen->winsys->buffer_map( screen->winsys, surface->buffer, flags );
263 if (map == NULL)
264 return NULL;
265
266 /* May want to different things here depending on read/write nature
267 * of the map:
268 */
269 if (surface->texture &&
270 (flags & PIPE_BUFFER_USAGE_CPU_WRITE))
271 {
272 /* Do something to notify sharing contexts of a texture change.
273 * In softpipe, that would mean flushing the texture cache.
274 */
275 softpipe_screen(screen)->timestamp++;
276 }
277
278 return map + surface->offset;
279 }
280
281
282 static void
283 softpipe_surface_unmap(struct pipe_screen *screen,
284 struct pipe_surface *surface)
285 {
286 screen->winsys->buffer_unmap( screen->winsys, surface->buffer );
287 }
288
289
290 void
291 softpipe_init_texture_funcs(struct softpipe_context *sp)
292 {
293 }
294
295
296 void
297 softpipe_init_screen_texture_funcs(struct pipe_screen *screen)
298 {
299 screen->texture_create = softpipe_texture_create;
300 screen->texture_release = softpipe_texture_release;
301
302 screen->get_tex_surface = softpipe_get_tex_surface;
303 screen->tex_surface_release = softpipe_tex_surface_release;
304
305 screen->surface_map = softpipe_surface_map;
306 screen->surface_unmap = softpipe_surface_unmap;
307 }