8137f29af52d2194c65c9a5bb0ee851bb98eb044
[profile/ivi/mesa.git] / src / gallium / drivers / llvmpipe / lp_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 "util/u_inlines.h"
36
37 #include "util/u_format.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40
41 #include "lp_context.h"
42 #include "lp_screen.h"
43 #include "lp_flush.h"
44 #include "lp_texture.h"
45 #include "lp_tile_size.h"
46 #include "state_tracker/sw_winsys.h"
47
48
49 /**
50  * Conventional allocation path for non-display textures:
51  * Simple, maximally packed layout.
52  */
53 static boolean
54 llvmpipe_texture_layout(struct llvmpipe_screen *screen,
55                         struct llvmpipe_texture *lpt)
56 {
57    struct pipe_texture *pt = &lpt->base;
58    unsigned level;
59    unsigned width = pt->width0;
60    unsigned height = pt->height0;
61    unsigned depth = pt->depth0;
62    unsigned buffer_size = 0;
63
64    for (level = 0; level <= pt->last_level; level++) {
65       unsigned nblocksx, nblocksy;
66
67       /* Allocate storage for whole quads. This is particularly important
68        * for depth surfaces, which are currently stored in a swizzled format.
69        */
70       nblocksx = util_format_get_nblocksx(pt->format, align(width, TILE_SIZE));
71       nblocksy = util_format_get_nblocksy(pt->format, align(height, TILE_SIZE));
72
73       lpt->stride[level] = align(nblocksx * util_format_get_blocksize(pt->format), 16);
74
75       lpt->level_offset[level] = buffer_size;
76
77       buffer_size += (nblocksy *
78                       ((pt->target == PIPE_TEXTURE_CUBE) ? 6 : depth) *
79                       lpt->stride[level]);
80
81       width = u_minify(width, 1);
82       height = u_minify(height, 1);
83       depth = u_minify(depth, 1);
84    }
85
86    lpt->data = align_malloc(buffer_size, 16);
87
88    return lpt->data != NULL;
89 }
90
91
92
93 static boolean
94 llvmpipe_displaytarget_layout(struct llvmpipe_screen *screen,
95                               struct llvmpipe_texture *lpt)
96 {
97    struct sw_winsys *winsys = screen->winsys;
98
99    /* Round up the surface size to a multiple of the tile size to
100     * avoid tile clipping.
101     */
102    unsigned width = align(lpt->base.width0, TILE_SIZE);
103    unsigned height = align(lpt->base.height0, TILE_SIZE);
104
105    lpt->dt = winsys->displaytarget_create(winsys,
106                                           lpt->base.tex_usage,
107                                           lpt->base.format,
108                                           width, height,
109                                           16,
110                                           &lpt->stride[0] );
111
112    return lpt->dt != NULL;
113 }
114
115
116 static struct pipe_texture *
117 llvmpipe_texture_create(struct pipe_screen *_screen,
118                         const struct pipe_texture *templat)
119 {
120    struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
121    struct llvmpipe_texture *lpt = CALLOC_STRUCT(llvmpipe_texture);
122    if (!lpt)
123       return NULL;
124
125    lpt->base = *templat;
126    pipe_reference_init(&lpt->base.reference, 1);
127    lpt->base.screen = &screen->base;
128
129    if (lpt->base.tex_usage & (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
130                               PIPE_TEXTURE_USAGE_SCANOUT |
131                               PIPE_TEXTURE_USAGE_SHARED)) {
132       if (!llvmpipe_displaytarget_layout(screen, lpt))
133          goto fail;
134    }
135    else {
136       if (!llvmpipe_texture_layout(screen, lpt))
137          goto fail;
138    }
139     
140    return &lpt->base;
141
142  fail:
143    FREE(lpt);
144    return NULL;
145 }
146
147
148 static void
149 llvmpipe_texture_destroy(struct pipe_texture *pt)
150 {
151    struct llvmpipe_screen *screen = llvmpipe_screen(pt->screen);
152    struct llvmpipe_texture *lpt = llvmpipe_texture(pt);
153
154    if (lpt->dt) {
155       /* display target */
156       struct sw_winsys *winsys = screen->winsys;
157       winsys->displaytarget_destroy(winsys, lpt->dt);
158    }
159    else {
160       /* regular texture */
161       align_free(lpt->data);
162    }
163
164    FREE(lpt);
165 }
166
167
168 /**
169  * Map a texture. Without any synchronization.
170  */
171 void *
172 llvmpipe_texture_map(struct pipe_texture *texture,
173                      unsigned face,
174                      unsigned level,
175                      unsigned zslice)
176 {
177    struct llvmpipe_texture *lpt = llvmpipe_texture(texture);
178    uint8_t *map;
179
180    if (lpt->dt) {
181       /* display target */
182       struct llvmpipe_screen *screen = llvmpipe_screen(texture->screen);
183       struct sw_winsys *winsys = screen->winsys;
184       const unsigned usage = PIPE_BUFFER_USAGE_CPU_READ_WRITE;
185
186       assert(face == 0);
187       assert(level == 0);
188       assert(zslice == 0);
189
190       /* FIXME: keep map count? */
191       map = winsys->displaytarget_map(winsys, lpt->dt, usage);
192    }
193    else {
194       /* regular texture */
195       unsigned offset;
196       unsigned stride;
197
198       map = lpt->data;
199
200       assert(level < LP_MAX_TEXTURE_2D_LEVELS);
201
202       offset = lpt->level_offset[level];
203       stride = lpt->stride[level];
204
205       /* XXX shouldn't that rather be
206          tex_height = align(u_minify(texture->height0, level), 2)
207          to account for alignment done in llvmpipe_texture_layout ?
208       */
209       if (texture->target == PIPE_TEXTURE_CUBE) {
210          unsigned tex_height = u_minify(texture->height0, level);
211          offset += face *  util_format_get_nblocksy(texture->format, tex_height) * stride;
212       }
213       else if (texture->target == PIPE_TEXTURE_3D) {
214          unsigned tex_height = u_minify(texture->height0, level);
215          offset += zslice * util_format_get_nblocksy(texture->format, tex_height) * stride;
216       }
217       else {
218          assert(face == 0);
219          assert(zslice == 0);
220       }
221
222       map += offset;
223    }
224
225    return map;
226 }
227
228
229 /**
230  * Unmap a texture. Without any synchronization.
231  */
232 void
233 llvmpipe_texture_unmap(struct pipe_texture *texture,
234                        unsigned face,
235                        unsigned level,
236                        unsigned zslice)
237 {
238    struct llvmpipe_texture *lpt = llvmpipe_texture(texture);
239
240    if (lpt->dt) {
241       /* display target */
242       struct llvmpipe_screen *lp_screen = llvmpipe_screen(texture->screen);
243       struct sw_winsys *winsys = lp_screen->winsys;
244
245       assert(face == 0);
246       assert(level == 0);
247       assert(zslice == 0);
248
249       winsys->displaytarget_unmap(winsys, lpt->dt);
250    }
251 }
252
253
254 static struct pipe_texture *
255 llvmpipe_texture_from_handle(struct pipe_screen *screen,
256                              const struct pipe_texture *template,
257                              struct winsys_handle *whandle)
258 {
259    struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
260    struct llvmpipe_texture *lpt = CALLOC_STRUCT(llvmpipe_texture);
261    if (!lpt)
262       return NULL;
263
264    lpt->base = *template;
265    pipe_reference_init(&lpt->base.reference, 1);
266    lpt->base.screen = screen;
267
268    lpt->dt = winsys->displaytarget_from_handle(winsys,
269                                                template,
270                                                whandle,
271                                                &lpt->stride[0]);
272    if (!lpt->dt)
273       goto fail;
274
275    return &lpt->base;
276
277  fail:
278    FREE(lpt);
279    return NULL;
280 }
281
282
283 static boolean
284 llvmpipe_texture_get_handle(struct pipe_screen *screen,
285                             struct pipe_texture *pt,
286                             struct winsys_handle *whandle)
287 {
288    struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
289    struct llvmpipe_texture *lpt = llvmpipe_texture(pt);
290
291    assert(lpt->dt);
292    if (!lpt->dt)
293       return FALSE;
294
295    return winsys->displaytarget_get_handle(winsys, lpt->dt, whandle);
296 }
297
298
299 static struct pipe_surface *
300 llvmpipe_get_tex_surface(struct pipe_screen *screen,
301                          struct pipe_texture *pt,
302                          unsigned face, unsigned level, unsigned zslice,
303                          unsigned usage)
304 {
305    struct llvmpipe_texture *lpt = llvmpipe_texture(pt);
306    struct pipe_surface *ps;
307
308    assert(level <= pt->last_level);
309
310    ps = CALLOC_STRUCT(pipe_surface);
311    if (ps) {
312       pipe_reference_init(&ps->reference, 1);
313       pipe_texture_reference(&ps->texture, pt);
314       ps->format = pt->format;
315       ps->width = u_minify(pt->width0, level);
316       ps->height = u_minify(pt->height0, level);
317       ps->usage = usage;
318
319       /* Because we are llvmpipe, anything that the state tracker
320        * thought was going to be done with the GPU will actually get
321        * done with the CPU.  Let's adjust the flags to take that into
322        * account.
323        */
324       if (ps->usage & PIPE_BUFFER_USAGE_GPU_WRITE) {
325          /* GPU_WRITE means "render" and that can involve reads (blending) */
326          ps->usage |= PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_CPU_READ;
327       }
328
329       if (ps->usage & PIPE_BUFFER_USAGE_GPU_READ)
330          ps->usage |= PIPE_BUFFER_USAGE_CPU_READ;
331
332       if (ps->usage & (PIPE_BUFFER_USAGE_CPU_WRITE |
333                        PIPE_BUFFER_USAGE_GPU_WRITE)) {
334          /* Mark the surface as dirty. */
335          lpt->timestamp++;
336          llvmpipe_screen(screen)->timestamp++;
337       }
338
339       ps->face = face;
340       ps->level = level;
341       ps->zslice = zslice;
342    }
343    return ps;
344 }
345
346
347 static void 
348 llvmpipe_tex_surface_destroy(struct pipe_surface *surf)
349 {
350    /* Effectively do the texture_update work here - if texture images
351     * needed post-processing to put them into hardware layout, this is
352     * where it would happen.  For llvmpipe, nothing to do.
353     */
354    assert(surf->texture);
355    pipe_texture_reference(&surf->texture, NULL);
356    FREE(surf);
357 }
358
359
360 static struct pipe_transfer *
361 llvmpipe_get_tex_transfer(struct pipe_context *pipe,
362                           struct pipe_texture *texture,
363                           unsigned face, unsigned level, unsigned zslice,
364                           enum pipe_transfer_usage usage,
365                           unsigned x, unsigned y, unsigned w, unsigned h)
366 {
367    struct llvmpipe_texture *lptex = llvmpipe_texture(texture);
368    struct llvmpipe_transfer *lpt;
369
370    assert(texture);
371    assert(level <= texture->last_level);
372
373    lpt = CALLOC_STRUCT(llvmpipe_transfer);
374    if (lpt) {
375       struct pipe_transfer *pt = &lpt->base;
376       pipe_texture_reference(&pt->texture, texture);
377       pt->x = x;
378       pt->y = y;
379       pt->width = align(w, TILE_SIZE);
380       pt->height = align(h, TILE_SIZE);
381       pt->stride = lptex->stride[level];
382       pt->usage = usage;
383       pt->face = face;
384       pt->level = level;
385       pt->zslice = zslice;
386
387       return pt;
388    }
389    return NULL;
390 }
391
392
393 static void 
394 llvmpipe_tex_transfer_destroy(struct pipe_context *pipe,
395                               struct pipe_transfer *transfer)
396 {
397    /* Effectively do the texture_update work here - if texture images
398     * needed post-processing to put them into hardware layout, this is
399     * where it would happen.  For llvmpipe, nothing to do.
400     */
401    assert (transfer->texture);
402    pipe_texture_reference(&transfer->texture, NULL);
403    FREE(transfer);
404 }
405
406
407 static void *
408 llvmpipe_transfer_map( struct pipe_context *pipe,
409                        struct pipe_transfer *transfer )
410 {
411    struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
412    ubyte *map;
413    struct llvmpipe_texture *lpt;
414    enum pipe_format format;
415
416    assert(transfer->texture);
417    lpt = llvmpipe_texture(transfer->texture);
418    format = lpt->base.format;
419
420    /*
421     * Transfers, like other pipe operations, must happen in order, so flush the
422     * context if necessary.
423     */
424    llvmpipe_flush_texture(pipe,
425                           transfer->texture, transfer->face, transfer->level,
426                           0, /* flush_flags */
427                           !(transfer->usage & PIPE_TRANSFER_WRITE), /* read_only */
428                           TRUE, /* cpu_access */
429                           FALSE); /* do_not_flush */
430
431    map = llvmpipe_texture_map(transfer->texture,
432                               transfer->face, transfer->level, transfer->zslice);
433
434    /* May want to different things here depending on read/write nature
435     * of the map:
436     */
437    if (transfer->usage & PIPE_TRANSFER_WRITE) {
438       /* Do something to notify sharing contexts of a texture change.
439        */
440       screen->timestamp++;
441    }
442    
443    map +=
444       transfer->y / util_format_get_blockheight(format) * transfer->stride +
445       transfer->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
446
447    return map;
448 }
449
450
451 static void
452 llvmpipe_transfer_unmap(struct pipe_context *pipe,
453                         struct pipe_transfer *transfer)
454 {
455    assert(transfer->texture);
456
457    llvmpipe_texture_unmap(transfer->texture,
458                           transfer->face, transfer->level, transfer->zslice);
459 }
460
461
462 void
463 llvmpipe_init_screen_texture_funcs(struct pipe_screen *screen)
464 {
465    screen->texture_create = llvmpipe_texture_create;
466    screen->texture_destroy = llvmpipe_texture_destroy;
467    screen->texture_from_handle = llvmpipe_texture_from_handle;
468    screen->texture_get_handle = llvmpipe_texture_get_handle;
469
470    screen->get_tex_surface = llvmpipe_get_tex_surface;
471    screen->tex_surface_destroy = llvmpipe_tex_surface_destroy;
472 }
473
474
475 void
476 llvmpipe_init_context_texture_funcs(struct pipe_context *pipe)
477 {
478    pipe->get_tex_transfer = llvmpipe_get_tex_transfer;
479    pipe->tex_transfer_destroy = llvmpipe_tex_transfer_destroy;
480    pipe->transfer_map = llvmpipe_transfer_map;
481    pipe->transfer_unmap = llvmpipe_transfer_unmap;
482 }