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