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