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
->pot
= (util_is_power_of_two(template->width0
) &&
269 util_is_power_of_two(template->height0
) &&
270 util_is_power_of_two(template->depth0
));
272 lpt
->dt
= winsys
->displaytarget_from_handle(winsys
,
288 llvmpipe_texture_get_handle(struct pipe_screen
*screen
,
289 struct pipe_texture
*pt
,
290 struct winsys_handle
*whandle
)
292 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
293 struct llvmpipe_texture
*lpt
= llvmpipe_texture(pt
);
299 return winsys
->displaytarget_get_handle(winsys
, lpt
->dt
, whandle
);
303 static struct pipe_surface
*
304 llvmpipe_get_tex_surface(struct pipe_screen
*screen
,
305 struct pipe_texture
*pt
,
306 unsigned face
, unsigned level
, unsigned zslice
,
309 struct llvmpipe_texture
*lpt
= llvmpipe_texture(pt
);
310 struct pipe_surface
*ps
;
312 assert(level
<= pt
->last_level
);
314 ps
= CALLOC_STRUCT(pipe_surface
);
316 pipe_reference_init(&ps
->reference
, 1);
317 pipe_texture_reference(&ps
->texture
, pt
);
318 ps
->format
= pt
->format
;
319 ps
->width
= u_minify(pt
->width0
, level
);
320 ps
->height
= u_minify(pt
->height0
, level
);
323 /* Because we are llvmpipe, anything that the state tracker
324 * thought was going to be done with the GPU will actually get
325 * done with the CPU. Let's adjust the flags to take that into
328 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
329 /* GPU_WRITE means "render" and that can involve reads (blending) */
330 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
333 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
334 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
336 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
337 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
338 /* Mark the surface as dirty. */
340 llvmpipe_screen(screen
)->timestamp
++;
352 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
354 /* Effectively do the texture_update work here - if texture images
355 * needed post-processing to put them into hardware layout, this is
356 * where it would happen. For llvmpipe, nothing to do.
358 assert(surf
->texture
);
359 pipe_texture_reference(&surf
->texture
, NULL
);
364 static struct pipe_transfer
*
365 llvmpipe_get_tex_transfer(struct pipe_context
*pipe
,
366 struct pipe_texture
*texture
,
367 unsigned face
, unsigned level
, unsigned zslice
,
368 enum pipe_transfer_usage usage
,
369 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
371 struct llvmpipe_texture
*lptex
= llvmpipe_texture(texture
);
372 struct llvmpipe_transfer
*lpt
;
375 assert(level
<= texture
->last_level
);
377 lpt
= CALLOC_STRUCT(llvmpipe_transfer
);
379 struct pipe_transfer
*pt
= &lpt
->base
;
380 pipe_texture_reference(&pt
->texture
, texture
);
383 pt
->width
= align(w
, TILE_SIZE
);
384 pt
->height
= align(h
, TILE_SIZE
);
385 pt
->stride
= lptex
->stride
[level
];
398 llvmpipe_tex_transfer_destroy(struct pipe_context
*pipe
,
399 struct pipe_transfer
*transfer
)
401 /* Effectively do the texture_update work here - if texture images
402 * needed post-processing to put them into hardware layout, this is
403 * where it would happen. For llvmpipe, nothing to do.
405 assert (transfer
->texture
);
406 pipe_texture_reference(&transfer
->texture
, NULL
);
412 llvmpipe_transfer_map( struct pipe_context
*pipe
,
413 struct pipe_transfer
*transfer
)
415 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
417 struct llvmpipe_texture
*lpt
;
418 enum pipe_format format
;
420 assert(transfer
->texture
);
421 lpt
= llvmpipe_texture(transfer
->texture
);
422 format
= lpt
->base
.format
;
425 * Transfers, like other pipe operations, must happen in order, so flush the
426 * context if necessary.
428 llvmpipe_flush_texture(pipe
,
429 transfer
->texture
, transfer
->face
, transfer
->level
,
431 !(transfer
->usage
& PIPE_TRANSFER_WRITE
), /* read_only */
432 TRUE
, /* cpu_access */
433 FALSE
); /* do_not_flush */
435 map
= llvmpipe_texture_map(transfer
->texture
,
436 transfer
->face
, transfer
->level
, transfer
->zslice
);
438 /* May want to different things here depending on read/write nature
441 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
442 /* Do something to notify sharing contexts of a texture change.
448 transfer
->y
/ util_format_get_blockheight(format
) * transfer
->stride
+
449 transfer
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
456 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
457 struct pipe_transfer
*transfer
)
459 assert(transfer
->texture
);
461 llvmpipe_texture_unmap(transfer
->texture
,
462 transfer
->face
, transfer
->level
, transfer
->zslice
);
467 llvmpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
469 screen
->texture_create
= llvmpipe_texture_create
;
470 screen
->texture_destroy
= llvmpipe_texture_destroy
;
471 screen
->texture_get_handle
= llvmpipe_texture_get_handle
;
473 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
474 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
479 llvmpipe_init_context_texture_funcs(struct pipe_context
*pipe
)
481 pipe
->get_tex_transfer
= llvmpipe_get_tex_transfer
;
482 pipe
->tex_transfer_destroy
= llvmpipe_tex_transfer_destroy
;
483 pipe
->transfer_map
= llvmpipe_transfer_map
;
484 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;