1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
33 #include "pipe/p_context.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_inlines.h"
37 #include "util/u_format.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
41 #include "lp_context.h"
42 #include "lp_screen.h"
44 #include "lp_texture.h"
45 #include "lp_tile_size.h"
46 #include "state_tracker/sw_winsys.h"
50 * Conventional allocation path for non-display textures:
51 * Simple, maximally packed layout.
54 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
55 struct llvmpipe_texture
*lpt
)
57 struct pipe_texture
*pt
= &lpt
->base
;
59 unsigned width
= pt
->width0
;
60 unsigned height
= pt
->height0
;
61 unsigned depth
= pt
->depth0
;
62 unsigned buffer_size
= 0;
64 for (level
= 0; level
<= pt
->last_level
; level
++) {
65 unsigned nblocksx
, nblocksy
;
67 /* Allocate storage for whole quads. This is particularly important
68 * for depth surfaces, which are currently stored in a swizzled format.
70 nblocksx
= util_format_get_nblocksx(pt
->format
, align(width
, TILE_SIZE
));
71 nblocksy
= util_format_get_nblocksy(pt
->format
, align(height
, TILE_SIZE
));
73 lpt
->stride
[level
] = align(nblocksx
* util_format_get_blocksize(pt
->format
), 16);
75 lpt
->level_offset
[level
] = buffer_size
;
77 buffer_size
+= (nblocksy
*
78 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
81 width
= u_minify(width
, 1);
82 height
= u_minify(height
, 1);
83 depth
= u_minify(depth
, 1);
86 lpt
->data
= align_malloc(buffer_size
, 16);
88 return lpt
->data
!= NULL
;
94 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
95 struct llvmpipe_texture
*lpt
)
97 struct sw_winsys
*winsys
= screen
->winsys
;
99 /* Round up the surface size to a multiple of the tile size to
100 * avoid tile clipping.
102 unsigned width
= align(lpt
->base
.width0
, TILE_SIZE
);
103 unsigned height
= align(lpt
->base
.height0
, TILE_SIZE
);
105 lpt
->dt
= winsys
->displaytarget_create(winsys
,
112 return lpt
->dt
!= NULL
;
116 static struct pipe_texture
*
117 llvmpipe_texture_create(struct pipe_screen
*_screen
,
118 const struct pipe_texture
*templat
)
120 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
121 struct llvmpipe_texture
*lpt
= CALLOC_STRUCT(llvmpipe_texture
);
125 lpt
->base
= *templat
;
126 pipe_reference_init(&lpt
->base
.reference
, 1);
127 lpt
->base
.screen
= &screen
->base
;
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
))
136 if (!llvmpipe_texture_layout(screen
, lpt
))
149 llvmpipe_texture_destroy(struct pipe_texture
*pt
)
151 struct llvmpipe_screen
*screen
= llvmpipe_screen(pt
->screen
);
152 struct llvmpipe_texture
*lpt
= llvmpipe_texture(pt
);
156 struct sw_winsys
*winsys
= screen
->winsys
;
157 winsys
->displaytarget_destroy(winsys
, lpt
->dt
);
160 /* regular texture */
161 align_free(lpt
->data
);
169 * Map a texture. Without any synchronization.
172 llvmpipe_texture_map(struct pipe_texture
*texture
,
177 struct llvmpipe_texture
*lpt
= llvmpipe_texture(texture
);
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
;
190 /* FIXME: keep map count? */
191 map
= winsys
->displaytarget_map(winsys
, lpt
->dt
, usage
);
194 /* regular texture */
200 assert(level
< LP_MAX_TEXTURE_2D_LEVELS
);
202 offset
= lpt
->level_offset
[level
];
203 stride
= lpt
->stride
[level
];
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 ?
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
;
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
;
230 * Unmap a texture. Without any synchronization.
233 llvmpipe_texture_unmap(struct pipe_texture
*texture
,
238 struct llvmpipe_texture
*lpt
= llvmpipe_texture(texture
);
242 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(texture
->screen
);
243 struct sw_winsys
*winsys
= lp_screen
->winsys
;
249 winsys
->displaytarget_unmap(winsys
, lpt
->dt
);
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
)
259 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
260 struct llvmpipe_texture
*lpt
= CALLOC_STRUCT(llvmpipe_texture
);
264 lpt
->base
= *template;
265 pipe_reference_init(&lpt
->base
.reference
, 1);
266 lpt
->base
.screen
= screen
;
268 lpt
->dt
= winsys
->displaytarget_from_handle(winsys
,
284 llvmpipe_texture_get_handle(struct pipe_screen
*screen
,
285 struct pipe_texture
*pt
,
286 struct winsys_handle
*whandle
)
288 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
289 struct llvmpipe_texture
*lpt
= llvmpipe_texture(pt
);
295 return winsys
->displaytarget_get_handle(winsys
, lpt
->dt
, whandle
);
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
,
305 struct llvmpipe_texture
*lpt
= llvmpipe_texture(pt
);
306 struct pipe_surface
*ps
;
308 assert(level
<= pt
->last_level
);
310 ps
= CALLOC_STRUCT(pipe_surface
);
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
);
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
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
;
329 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
330 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
332 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
333 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
334 /* Mark the surface as dirty. */
336 llvmpipe_screen(screen
)->timestamp
++;
348 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
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.
354 assert(surf
->texture
);
355 pipe_texture_reference(&surf
->texture
, NULL
);
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
)
367 struct llvmpipe_texture
*lptex
= llvmpipe_texture(texture
);
368 struct llvmpipe_transfer
*lpt
;
371 assert(level
<= texture
->last_level
);
373 lpt
= CALLOC_STRUCT(llvmpipe_transfer
);
375 struct pipe_transfer
*pt
= &lpt
->base
;
376 pipe_texture_reference(&pt
->texture
, texture
);
379 pt
->width
= align(w
, TILE_SIZE
);
380 pt
->height
= align(h
, TILE_SIZE
);
381 pt
->stride
= lptex
->stride
[level
];
394 llvmpipe_tex_transfer_destroy(struct pipe_context
*pipe
,
395 struct pipe_transfer
*transfer
)
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.
401 assert (transfer
->texture
);
402 pipe_texture_reference(&transfer
->texture
, NULL
);
408 llvmpipe_transfer_map( struct pipe_context
*pipe
,
409 struct pipe_transfer
*transfer
)
411 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
413 struct llvmpipe_texture
*lpt
;
414 enum pipe_format format
;
416 assert(transfer
->texture
);
417 lpt
= llvmpipe_texture(transfer
->texture
);
418 format
= lpt
->base
.format
;
421 * Transfers, like other pipe operations, must happen in order, so flush the
422 * context if necessary.
424 llvmpipe_flush_texture(pipe
,
425 transfer
->texture
, transfer
->face
, transfer
->level
,
427 !(transfer
->usage
& PIPE_TRANSFER_WRITE
), /* read_only */
428 TRUE
, /* cpu_access */
429 FALSE
); /* do_not_flush */
431 map
= llvmpipe_texture_map(transfer
->texture
,
432 transfer
->face
, transfer
->level
, transfer
->zslice
);
434 /* May want to different things here depending on read/write nature
437 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
438 /* Do something to notify sharing contexts of a texture change.
444 transfer
->y
/ util_format_get_blockheight(format
) * transfer
->stride
+
445 transfer
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
452 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
453 struct pipe_transfer
*transfer
)
455 assert(transfer
->texture
);
457 llvmpipe_texture_unmap(transfer
->texture
,
458 transfer
->face
, transfer
->level
, transfer
->zslice
);
463 llvmpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
465 screen
->texture_create
= llvmpipe_texture_create
;
466 screen
->texture_destroy
= llvmpipe_texture_destroy
;
467 screen
->texture_get_handle
= llvmpipe_texture_get_handle
;
469 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
470 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
475 llvmpipe_init_context_texture_funcs(struct pipe_context
*pipe
)
477 pipe
->get_tex_transfer
= llvmpipe_get_tex_transfer
;
478 pipe
->tex_transfer_destroy
= llvmpipe_tex_transfer_destroy
;
479 pipe
->transfer_map
= llvmpipe_transfer_map
;
480 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;