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