softpipe: Also defere primary textures to backend
[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/internal/p_winsys_screen.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 #include "sp_winsys.h"
46
47
48 /* Simple, maximally packed layout.
49 */
50
51 static unsigned minify( unsigned d )
52 {
53 return MAX2(1, d>>1);
54 }
55
56
57 /* Conventional allocation path for non-display textures:
58 */
59 static boolean
60 softpipe_texture_layout(struct pipe_screen *screen,
61 struct softpipe_texture * spt)
62 {
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 = screen->buffer_create(screen, 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 unsigned usage = (PIPE_BUFFER_USAGE_CPU_READ_WRITE |
102 PIPE_BUFFER_USAGE_GPU_READ_WRITE);
103
104 spt->base.nblocksx[0] = pf_get_nblocksx(&spt->base.block, spt->base.width[0]);
105 spt->base.nblocksy[0] = pf_get_nblocksy(&spt->base.block, spt->base.height[0]);
106
107 spt->buffer = screen->surface_buffer_create( screen,
108 spt->base.width[0],
109 spt->base.height[0],
110 spt->base.format,
111 usage,
112 &spt->stride[0]);
113
114 return spt->buffer != NULL;
115 }
116
117
118
119
120
121 static struct pipe_texture *
122 softpipe_texture_create(struct pipe_screen *screen,
123 const struct pipe_texture *templat)
124 {
125 struct softpipe_texture *spt = CALLOC_STRUCT(softpipe_texture);
126 if (!spt)
127 return NULL;
128
129 spt->base = *templat;
130 pipe_reference_init(&spt->base.reference, 1);
131 spt->base.screen = screen;
132
133 if (spt->base.tex_usage & (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
134 PIPE_TEXTURE_USAGE_PRIMARY)) {
135 if (!softpipe_displaytarget_layout(screen, spt))
136 goto fail;
137 }
138 else {
139 if (!softpipe_texture_layout(screen, spt))
140 goto fail;
141 }
142
143 return &spt->base;
144
145 fail:
146 FREE(spt);
147 return NULL;
148 }
149
150
151 static struct pipe_texture *
152 softpipe_texture_blanket(struct pipe_screen * screen,
153 const struct pipe_texture *base,
154 const unsigned *stride,
155 struct pipe_buffer *buffer)
156 {
157 struct softpipe_texture *spt;
158 assert(screen);
159
160 /* Only supports one type */
161 if (base->target != PIPE_TEXTURE_2D ||
162 base->last_level != 0 ||
163 base->depth[0] != 1) {
164 return NULL;
165 }
166
167 spt = CALLOC_STRUCT(softpipe_texture);
168 if (!spt)
169 return NULL;
170
171 spt->base = *base;
172 pipe_reference_init(&spt->base.reference, 1);
173 spt->base.screen = screen;
174 spt->base.nblocksx[0] = pf_get_nblocksx(&spt->base.block, spt->base.width[0]);
175 spt->base.nblocksy[0] = pf_get_nblocksy(&spt->base.block, spt->base.height[0]);
176 spt->stride[0] = stride[0];
177
178 pipe_buffer_reference(&spt->buffer, buffer);
179
180 return &spt->base;
181 }
182
183
184 static void
185 softpipe_texture_destroy(struct pipe_texture *pt)
186 {
187 struct softpipe_texture *spt = softpipe_texture(pt);
188
189 pipe_buffer_reference(&spt->buffer, NULL);
190 FREE(spt);
191 }
192
193
194 static struct pipe_surface *
195 softpipe_get_tex_surface(struct pipe_screen *screen,
196 struct pipe_texture *pt,
197 unsigned face, unsigned level, unsigned zslice,
198 unsigned usage)
199 {
200 struct softpipe_texture *spt = softpipe_texture(pt);
201 struct pipe_surface *ps;
202
203 assert(level <= pt->last_level);
204
205 ps = CALLOC_STRUCT(pipe_surface);
206 if (ps) {
207 pipe_reference_init(&ps->reference, 1);
208 pipe_texture_reference(&ps->texture, pt);
209 ps->format = pt->format;
210 ps->width = pt->width[level];
211 ps->height = pt->height[level];
212 ps->offset = spt->level_offset[level];
213 ps->usage = usage;
214
215 /* Because we are softpipe, anything that the state tracker
216 * thought was going to be done with the GPU will actually get
217 * done with the CPU. Let's adjust the flags to take that into
218 * account.
219 */
220 if (ps->usage & PIPE_BUFFER_USAGE_GPU_WRITE) {
221 /* GPU_WRITE means "render" and that can involve reads (blending) */
222 ps->usage |= PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_CPU_READ;
223 }
224
225 if (ps->usage & PIPE_BUFFER_USAGE_GPU_READ)
226 ps->usage |= PIPE_BUFFER_USAGE_CPU_READ;
227
228 ps->face = face;
229 ps->level = level;
230 ps->zslice = zslice;
231
232 if (pt->target == PIPE_TEXTURE_CUBE) {
233 ps->offset += face * pt->nblocksy[level] * spt->stride[level];
234 }
235 else if (pt->target == PIPE_TEXTURE_3D) {
236 ps->offset += zslice * pt->nblocksy[level] * spt->stride[level];
237 }
238 else {
239 assert(face == 0);
240 assert(zslice == 0);
241 }
242 }
243 return ps;
244 }
245
246
247 static void
248 softpipe_tex_surface_destroy(struct pipe_surface *surf)
249 {
250 /* Effectively do the texture_update work here - if texture images
251 * needed post-processing to put them into hardware layout, this is
252 * where it would happen. For softpipe, nothing to do.
253 */
254 assert(surf->texture);
255 pipe_texture_reference(&surf->texture, NULL);
256 FREE(surf);
257 }
258
259
260 static struct pipe_transfer *
261 softpipe_get_tex_transfer(struct pipe_screen *screen,
262 struct pipe_texture *texture,
263 unsigned face, unsigned level, unsigned zslice,
264 enum pipe_transfer_usage usage,
265 unsigned x, unsigned y, unsigned w, unsigned h)
266 {
267 struct softpipe_texture *sptex = softpipe_texture(texture);
268 struct softpipe_transfer *spt;
269
270 assert(texture);
271 assert(level <= texture->last_level);
272
273 spt = CALLOC_STRUCT(softpipe_transfer);
274 if (spt) {
275 struct pipe_transfer *pt = &spt->base;
276 pipe_texture_reference(&pt->texture, texture);
277 pt->format = texture->format;
278 pt->block = texture->block;
279 pt->x = x;
280 pt->y = y;
281 pt->width = w;
282 pt->height = h;
283 pt->nblocksx = texture->nblocksx[level];
284 pt->nblocksy = texture->nblocksy[level];
285 pt->stride = sptex->stride[level];
286 pt->usage = usage;
287 pt->face = face;
288 pt->level = level;
289 pt->zslice = zslice;
290
291 spt->offset = sptex->level_offset[level];
292
293 if (texture->target == PIPE_TEXTURE_CUBE) {
294 spt->offset += face * pt->nblocksy * pt->stride;
295 }
296 else if (texture->target == PIPE_TEXTURE_3D) {
297 spt->offset += zslice * pt->nblocksy * pt->stride;
298 }
299 else {
300 assert(face == 0);
301 assert(zslice == 0);
302 }
303 return pt;
304 }
305 return NULL;
306 }
307
308
309 static void
310 softpipe_tex_transfer_destroy(struct pipe_transfer *transfer)
311 {
312 /* Effectively do the texture_update work here - if texture images
313 * needed post-processing to put them into hardware layout, this is
314 * where it would happen. For softpipe, nothing to do.
315 */
316 assert (transfer->texture);
317 pipe_texture_reference(&transfer->texture, NULL);
318 FREE(transfer);
319 }
320
321
322 static void *
323 softpipe_transfer_map( struct pipe_screen *screen,
324 struct pipe_transfer *transfer )
325 {
326 ubyte *map, *xfer_map;
327 struct softpipe_texture *spt;
328 unsigned flags = 0;
329
330 assert(transfer->texture);
331 spt = softpipe_texture(transfer->texture);
332
333 if (transfer->usage != PIPE_TRANSFER_READ) {
334 flags |= PIPE_BUFFER_USAGE_CPU_WRITE;
335 }
336
337 if (transfer->usage != PIPE_TRANSFER_WRITE) {
338 flags |= PIPE_BUFFER_USAGE_CPU_READ;
339 }
340
341 map = pipe_buffer_map(screen, spt->buffer, flags);
342 if (map == NULL)
343 return NULL;
344
345 /* May want to different things here depending on read/write nature
346 * of the map:
347 */
348 if (transfer->texture && transfer->usage != PIPE_TRANSFER_READ)
349 {
350 /* Do something to notify sharing contexts of a texture change.
351 * In softpipe, that would mean flushing the texture cache.
352 */
353 softpipe_screen(screen)->timestamp++;
354 }
355
356 xfer_map = map + softpipe_transfer(transfer)->offset +
357 transfer->y / transfer->block.height * transfer->stride +
358 transfer->x / transfer->block.width * transfer->block.size;
359 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
360 return xfer_map;
361 }
362
363
364 static void
365 softpipe_transfer_unmap(struct pipe_screen *screen,
366 struct pipe_transfer *transfer)
367 {
368 struct softpipe_texture *spt;
369
370 assert(transfer->texture);
371 spt = softpipe_texture(transfer->texture);
372
373 pipe_buffer_unmap( screen, spt->buffer );
374
375 if (transfer->usage != PIPE_TRANSFER_READ) {
376 /* Mark the texture as dirty to expire the tile caches. */
377 spt->modified = TRUE;
378 }
379 }
380
381
382 void
383 softpipe_init_texture_funcs(struct softpipe_context *sp)
384 {
385 }
386
387
388 void
389 softpipe_init_screen_texture_funcs(struct pipe_screen *screen)
390 {
391 screen->texture_create = softpipe_texture_create;
392 screen->texture_blanket = softpipe_texture_blanket;
393 screen->texture_destroy = softpipe_texture_destroy;
394
395 screen->get_tex_surface = softpipe_get_tex_surface;
396 screen->tex_surface_destroy = softpipe_tex_surface_destroy;
397
398 screen->get_tex_transfer = softpipe_get_tex_transfer;
399 screen->tex_transfer_destroy = softpipe_tex_transfer_destroy;
400 screen->transfer_map = softpipe_transfer_map;
401 screen->transfer_unmap = softpipe_transfer_unmap;
402 }
403
404
405 boolean
406 softpipe_get_texture_buffer( struct pipe_texture *texture,
407 struct pipe_buffer **buf,
408 unsigned *stride )
409 {
410 struct softpipe_texture *tex = (struct softpipe_texture *)texture;
411
412 if (!tex)
413 return FALSE;
414
415 pipe_buffer_reference(buf, tex->buffer);
416
417 if (stride)
418 *stride = tex->stride[0];
419
420 return TRUE;
421 }