Merge commit 'origin/master' into gallium-0.2
[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_winsys.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
39
40 #include "sp_context.h"
41 #include "sp_state.h"
42 #include "sp_texture.h"
43 #include "sp_tile_cache.h"
44 #include "sp_screen.h"
45
46
47 /* Simple, maximally packed layout.
48 */
49
50 static unsigned minify( unsigned d )
51 {
52 return MAX2(1, d>>1);
53 }
54
55
56 /* Conventional allocation path for non-display textures:
57 */
58 static boolean
59 softpipe_texture_layout(struct pipe_screen *screen,
60 struct softpipe_texture * spt)
61 {
62 struct pipe_winsys *ws = screen->winsys;
63 struct pipe_texture *pt = &spt->base;
64 unsigned level;
65 unsigned width = pt->width[0];
66 unsigned height = pt->height[0];
67 unsigned depth = pt->depth[0];
68
69 unsigned buffer_size = 0;
70
71 for (level = 0; level <= pt->last_level; level++) {
72 pt->width[level] = width;
73 pt->height[level] = height;
74 pt->depth[level] = depth;
75 pt->nblocksx[level] = pf_get_nblocksx(&pt->block, width);
76 pt->nblocksy[level] = pf_get_nblocksy(&pt->block, height);
77 spt->stride[level] = pt->nblocksx[level]*pt->block.size;
78
79 spt->level_offset[level] = buffer_size;
80
81 buffer_size += (pt->nblocksy[level] *
82 ((pt->target == PIPE_TEXTURE_CUBE) ? 6 : depth) *
83 spt->stride[level]);
84
85 width = minify(width);
86 height = minify(height);
87 depth = minify(depth);
88 }
89
90 spt->buffer = ws->buffer_create(ws, 32,
91 PIPE_BUFFER_USAGE_PIXEL,
92 buffer_size);
93
94 return spt->buffer != NULL;
95 }
96
97
98
99 /* Hack it up to use the old winsys->surface_alloc_storage()
100 * method for now:
101 */
102 static boolean
103 softpipe_displaytarget_layout(struct pipe_screen *screen,
104 struct softpipe_texture * spt)
105 {
106 struct pipe_winsys *ws = screen->winsys;
107 struct pipe_surface surf;
108 unsigned flags = (PIPE_BUFFER_USAGE_CPU_READ |
109 PIPE_BUFFER_USAGE_CPU_WRITE |
110 PIPE_BUFFER_USAGE_GPU_READ |
111 PIPE_BUFFER_USAGE_GPU_WRITE);
112
113
114 memset(&surf, 0, sizeof(surf));
115
116 ws->surface_alloc_storage( ws,
117 &surf,
118 spt->base.width[0],
119 spt->base.height[0],
120 spt->base.format,
121 flags,
122 spt->base.tex_usage);
123
124 /* Now extract the goodies:
125 */
126 spt->base.nblocksx[0] = pf_get_nblocksx(&spt->base.block, spt->base.width[0]);
127 spt->base.nblocksy[0] = pf_get_nblocksy(&spt->base.block, spt->base.height[0]);
128 spt->stride[0] = surf.stride;
129 spt->buffer = surf.buffer;
130
131 return spt->buffer != NULL;
132 }
133
134
135
136
137
138 static struct pipe_texture *
139 softpipe_texture_create(struct pipe_screen *screen,
140 const struct pipe_texture *templat)
141 {
142 struct softpipe_texture *spt = CALLOC_STRUCT(softpipe_texture);
143 if (!spt)
144 return NULL;
145
146 spt->base = *templat;
147 spt->base.refcount = 1;
148 spt->base.screen = screen;
149
150 if (spt->base.tex_usage & PIPE_TEXTURE_USAGE_DISPLAY_TARGET) {
151 if (!softpipe_displaytarget_layout(screen, spt))
152 goto fail;
153 }
154 else {
155 if (!softpipe_texture_layout(screen, spt))
156 goto fail;
157 }
158
159 assert(spt->base.refcount == 1);
160 return &spt->base;
161
162 fail:
163 FREE(spt);
164 return NULL;
165 }
166
167
168 static struct pipe_texture *
169 softpipe_texture_blanket(struct pipe_screen * screen,
170 const struct pipe_texture *base,
171 const unsigned *stride,
172 struct pipe_buffer *buffer)
173 {
174 struct softpipe_texture *spt;
175 assert(screen);
176
177 /* Only supports one type */
178 if (base->target != PIPE_TEXTURE_2D ||
179 base->last_level != 0 ||
180 base->depth[0] != 1) {
181 return NULL;
182 }
183
184 spt = CALLOC_STRUCT(softpipe_texture);
185 if (!spt)
186 return NULL;
187
188 spt->base = *base;
189 spt->base.refcount = 1;
190 spt->base.screen = screen;
191 spt->base.nblocksx[0] = pf_get_nblocksx(&spt->base.block, spt->base.width[0]);
192 spt->base.nblocksy[0] = pf_get_nblocksy(&spt->base.block, spt->base.height[0]);
193 spt->stride[0] = stride[0];
194
195 pipe_buffer_reference(screen, &spt->buffer, buffer);
196
197 return &spt->base;
198 }
199
200
201 static void
202 softpipe_texture_release(struct pipe_screen *screen,
203 struct pipe_texture **pt)
204 {
205 if (!*pt)
206 return;
207
208 if (--(*pt)->refcount <= 0) {
209 struct softpipe_texture *spt = softpipe_texture(*pt);
210
211 pipe_buffer_reference(screen, &spt->buffer, NULL);
212 FREE(spt);
213 }
214 *pt = NULL;
215 }
216
217
218 static struct pipe_surface *
219 softpipe_get_tex_surface(struct pipe_screen *screen,
220 struct pipe_texture *pt,
221 unsigned face, unsigned level, unsigned zslice,
222 unsigned usage)
223 {
224 struct pipe_winsys *ws = screen->winsys;
225 struct softpipe_texture *spt = softpipe_texture(pt);
226 struct pipe_surface *ps;
227
228 assert(level <= pt->last_level);
229
230 ps = ws->surface_alloc(ws);
231 if (ps) {
232 assert(ps->refcount);
233 assert(ps->winsys);
234 pipe_buffer_reference(screen, &ps->buffer, spt->buffer);
235 ps->format = pt->format;
236 ps->block = pt->block;
237 ps->width = pt->width[level];
238 ps->height = pt->height[level];
239 ps->nblocksx = pt->nblocksx[level];
240 ps->nblocksy = pt->nblocksy[level];
241 ps->stride = spt->stride[level];
242 ps->offset = spt->level_offset[level];
243 ps->usage = usage;
244
245 /* Because we are softpipe, anything that the state tracker
246 * thought was going to be done with the GPU will actually get
247 * done with the CPU. Let's adjust the flags to take that into
248 * account.
249 */
250 if (ps->usage & PIPE_BUFFER_USAGE_GPU_WRITE) {
251 /* GPU_WRITE means "render" and that can involve reads (blending) */
252 ps->usage |= PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_CPU_READ;
253 }
254
255 if (ps->usage & PIPE_BUFFER_USAGE_GPU_READ)
256 ps->usage |= PIPE_BUFFER_USAGE_CPU_READ;
257
258 if (ps->usage & (PIPE_BUFFER_USAGE_CPU_WRITE |
259 PIPE_BUFFER_USAGE_GPU_WRITE)) {
260 /* Mark the surface as dirty. The tile cache will look for this. */
261 spt->modified = TRUE;
262 }
263
264 pipe_texture_reference(&ps->texture, pt);
265 ps->face = face;
266 ps->level = level;
267 ps->zslice = zslice;
268
269 if (pt->target == PIPE_TEXTURE_CUBE || pt->target == PIPE_TEXTURE_3D) {
270 ps->offset += ((pt->target == PIPE_TEXTURE_CUBE) ? face : zslice) *
271 ps->nblocksy *
272 ps->stride;
273 }
274 else {
275 assert(face == 0);
276 assert(zslice == 0);
277 }
278 }
279 return ps;
280 }
281
282
283 static void
284 softpipe_tex_surface_release(struct pipe_screen *screen,
285 struct pipe_surface **s)
286 {
287 /* Effectively do the texture_update work here - if texture images
288 * needed post-processing to put them into hardware layout, this is
289 * where it would happen. For softpipe, nothing to do.
290 */
291 assert ((*s)->texture);
292 pipe_texture_reference(&(*s)->texture, NULL);
293
294 screen->winsys->surface_release(screen->winsys, s);
295 }
296
297
298 static void *
299 softpipe_surface_map( struct pipe_screen *screen,
300 struct pipe_surface *surface,
301 unsigned flags )
302 {
303 ubyte *map;
304
305 if (flags & ~surface->usage) {
306 assert(0);
307 return NULL;
308 }
309
310 map = pipe_buffer_map( screen, surface->buffer, flags );
311 if (map == NULL)
312 return NULL;
313
314 /* May want to different things here depending on read/write nature
315 * of the map:
316 */
317 if (surface->texture &&
318 (flags & PIPE_BUFFER_USAGE_CPU_WRITE))
319 {
320 /* Do something to notify sharing contexts of a texture change.
321 * In softpipe, that would mean flushing the texture cache.
322 */
323 softpipe_screen(screen)->timestamp++;
324 }
325
326 return map + surface->offset;
327 }
328
329
330 static void
331 softpipe_surface_unmap(struct pipe_screen *screen,
332 struct pipe_surface *surface)
333 {
334 pipe_buffer_unmap( screen, surface->buffer );
335 }
336
337
338 void
339 softpipe_init_texture_funcs(struct softpipe_context *sp)
340 {
341 }
342
343
344 void
345 softpipe_init_screen_texture_funcs(struct pipe_screen *screen)
346 {
347 screen->texture_create = softpipe_texture_create;
348 screen->texture_blanket = softpipe_texture_blanket;
349 screen->texture_release = softpipe_texture_release;
350
351 screen->get_tex_surface = softpipe_get_tex_surface;
352 screen->tex_surface_release = softpipe_tex_surface_release;
353
354 screen->surface_map = softpipe_surface_map;
355 screen->surface_unmap = softpipe_surface_unmap;
356 }