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"
40 #include "util/u_transfer.h"
42 #include "lp_context.h"
43 #include "lp_screen.h"
45 #include "lp_texture.h"
47 #include "lp_tile_size.h"
48 #include "state_tracker/sw_winsys.h"
52 * Conventional allocation path for non-display textures:
53 * Simple, maximally packed layout.
56 llvmpipe_resource_layout(struct llvmpipe_screen
*screen
,
57 struct llvmpipe_resource
*lpt
)
59 struct pipe_resource
*pt
= &lpt
->base
;
61 unsigned width
= pt
->width0
;
62 unsigned height
= pt
->height0
;
63 unsigned depth
= pt
->depth0
;
64 unsigned buffer_size
= 0;
66 for (level
= 0; level
<= pt
->last_level
; level
++) {
67 unsigned nblocksx
, nblocksy
;
69 /* Allocate storage for whole quads. This is particularly important
70 * for depth surfaces, which are currently stored in a swizzled format.
72 nblocksx
= util_format_get_nblocksx(pt
->format
, align(width
, TILE_SIZE
));
73 nblocksy
= util_format_get_nblocksy(pt
->format
, align(height
, TILE_SIZE
));
75 lpt
->stride
[level
] = align(nblocksx
* util_format_get_blocksize(pt
->format
), 16);
77 lpt
->level_offset
[level
] = buffer_size
;
79 buffer_size
+= (nblocksy
*
80 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
83 width
= u_minify(width
, 1);
84 height
= u_minify(height
, 1);
85 depth
= u_minify(depth
, 1);
88 lpt
->data
= align_malloc(buffer_size
, 16);
90 return lpt
->data
!= NULL
;
96 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
97 struct llvmpipe_resource
*lpt
)
99 struct sw_winsys
*winsys
= screen
->winsys
;
101 /* Round up the surface size to a multiple of the tile size to
102 * avoid tile clipping.
104 unsigned width
= align(lpt
->base
.width0
, TILE_SIZE
);
105 unsigned height
= align(lpt
->base
.height0
, TILE_SIZE
);
107 lpt
->dt
= winsys
->displaytarget_create(winsys
,
114 return lpt
->dt
!= NULL
;
118 static struct pipe_resource
*
119 llvmpipe_resource_create(struct pipe_screen
*_screen
,
120 const struct pipe_resource
*templat
)
122 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
123 struct llvmpipe_resource
*lpt
= CALLOC_STRUCT(llvmpipe_resource
);
127 lpt
->base
= *templat
;
128 pipe_reference_init(&lpt
->base
.reference
, 1);
129 lpt
->base
.screen
= &screen
->base
;
131 if (lpt
->base
.bind
& (PIPE_BIND_DISPLAY_TARGET
|
134 if (!llvmpipe_displaytarget_layout(screen
, lpt
))
138 if (!llvmpipe_resource_layout(screen
, lpt
))
151 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
152 struct pipe_resource
*pt
)
154 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
155 struct llvmpipe_resource
*lpt
= llvmpipe_resource(pt
);
159 struct sw_winsys
*winsys
= screen
->winsys
;
160 winsys
->displaytarget_destroy(winsys
, lpt
->dt
);
162 else if (!lpt
->userBuffer
) {
163 /* regular texture */
164 align_free(lpt
->data
);
172 * Map a texture. Without any synchronization.
175 llvmpipe_resource_map(struct pipe_resource
*texture
,
181 struct llvmpipe_resource
*lpt
= llvmpipe_resource(texture
);
186 struct llvmpipe_screen
*screen
= llvmpipe_screen(texture
->screen
);
187 struct sw_winsys
*winsys
= screen
->winsys
;
193 /* FIXME: keep map count? */
194 map
= winsys
->displaytarget_map(winsys
, lpt
->dt
, usage
);
197 /* regular texture */
203 assert(level
< LP_MAX_TEXTURE_2D_LEVELS
);
205 offset
= lpt
->level_offset
[level
];
206 stride
= lpt
->stride
[level
];
208 /* XXX shouldn't that rather be
209 tex_height = align(u_minify(texture->height0, level), 2)
210 to account for alignment done in llvmpipe_resource_layout ?
212 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
213 unsigned tex_height
= u_minify(texture
->height0
, level
);
214 offset
+= face
* util_format_get_nblocksy(texture
->format
, tex_height
) * stride
;
216 else if (texture
->target
== PIPE_TEXTURE_3D
) {
217 unsigned tex_height
= u_minify(texture
->height0
, level
);
218 offset
+= zslice
* util_format_get_nblocksy(texture
->format
, tex_height
) * stride
;
233 * Unmap a texture. Without any synchronization.
236 llvmpipe_resource_unmap(struct pipe_resource
*texture
,
241 struct llvmpipe_resource
*lpt
= llvmpipe_resource(texture
);
245 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(texture
->screen
);
246 struct sw_winsys
*winsys
= lp_screen
->winsys
;
252 winsys
->displaytarget_unmap(winsys
, lpt
->dt
);
257 static struct pipe_resource
*
258 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
259 const struct pipe_resource
*template,
260 struct winsys_handle
*whandle
)
262 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
263 struct llvmpipe_resource
*lpt
= CALLOC_STRUCT(llvmpipe_resource
);
267 lpt
->base
= *template;
268 pipe_reference_init(&lpt
->base
.reference
, 1);
269 lpt
->base
.screen
= screen
;
271 lpt
->dt
= winsys
->displaytarget_from_handle(winsys
,
287 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
288 struct pipe_resource
*pt
,
289 struct winsys_handle
*whandle
)
291 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
292 struct llvmpipe_resource
*lpt
= llvmpipe_resource(pt
);
298 return winsys
->displaytarget_get_handle(winsys
, lpt
->dt
, whandle
);
302 static struct pipe_surface
*
303 llvmpipe_get_tex_surface(struct pipe_screen
*screen
,
304 struct pipe_resource
*pt
,
305 unsigned face
, unsigned level
, unsigned zslice
,
308 struct pipe_surface
*ps
;
310 assert(level
<= pt
->last_level
);
312 ps
= CALLOC_STRUCT(pipe_surface
);
314 pipe_reference_init(&ps
->reference
, 1);
315 pipe_resource_reference(&ps
->texture
, pt
);
316 ps
->format
= pt
->format
;
317 ps
->width
= u_minify(pt
->width0
, level
);
318 ps
->height
= u_minify(pt
->height0
, level
);
330 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
332 /* Effectively do the texture_update work here - if texture images
333 * needed post-processing to put them into hardware layout, this is
334 * where it would happen. For llvmpipe, nothing to do.
336 assert(surf
->texture
);
337 pipe_resource_reference(&surf
->texture
, NULL
);
342 static struct pipe_transfer
*
343 llvmpipe_get_transfer(struct pipe_context
*pipe
,
344 struct pipe_resource
*resource
,
345 struct pipe_subresource sr
,
347 const struct pipe_box
*box
)
349 struct llvmpipe_resource
*lptex
= llvmpipe_resource(resource
);
350 struct llvmpipe_transfer
*lpt
;
353 assert(sr
.level
<= resource
->last_level
);
355 lpt
= CALLOC_STRUCT(llvmpipe_transfer
);
357 struct pipe_transfer
*pt
= &lpt
->base
;
358 pipe_resource_reference(&pt
->resource
, resource
);
360 pt
->stride
= lptex
->stride
[sr
.level
];
370 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
371 struct pipe_transfer
*transfer
)
373 /* Effectively do the texture_update work here - if texture images
374 * needed post-processing to put them into hardware layout, this is
375 * where it would happen. For llvmpipe, nothing to do.
377 assert (transfer
->resource
);
378 pipe_resource_reference(&transfer
->resource
, NULL
);
384 llvmpipe_transfer_map( struct pipe_context
*pipe
,
385 struct pipe_transfer
*transfer
)
387 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
389 struct llvmpipe_resource
*lpt
;
390 enum pipe_format format
;
392 assert(transfer
->resource
);
393 lpt
= llvmpipe_resource(transfer
->resource
);
394 format
= lpt
->base
.format
;
397 * Transfers, like other pipe operations, must happen in order, so flush the
398 * context if necessary.
400 llvmpipe_flush_texture(pipe
,
405 !(transfer
->usage
& PIPE_TRANSFER_WRITE
), /* read_only */
406 TRUE
, /* cpu_access */
407 FALSE
); /* do_not_flush */
409 map
= llvmpipe_resource_map(transfer
->resource
,
415 /* May want to different things here depending on read/write nature
418 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
419 /* Do something to notify sharing contexts of a texture change.
425 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
426 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
433 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
434 struct pipe_transfer
*transfer
)
436 assert(transfer
->resource
);
438 llvmpipe_resource_unmap(transfer
->resource
,
445 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
446 struct pipe_resource
*presource
,
447 unsigned face
, unsigned level
)
449 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
451 if (presource
->target
== PIPE_BUFFER
)
452 return PIPE_UNREFERENCED
;
454 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
460 * Create buffer which wraps user-space data.
462 static struct pipe_resource
*
463 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
468 struct llvmpipe_resource
*buffer
;
470 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
474 pipe_reference_init(&buffer
->base
.reference
, 1);
475 buffer
->base
.screen
= screen
;
476 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
477 buffer
->base
.bind
= bind_flags
;
478 buffer
->base
._usage
= PIPE_USAGE_IMMUTABLE
;
479 buffer
->base
.flags
= 0;
480 buffer
->base
.width0
= bytes
;
481 buffer
->base
.height0
= 1;
482 buffer
->base
.depth0
= 1;
483 buffer
->userBuffer
= TRUE
;
486 return &buffer
->base
;
491 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
493 screen
->resource_create
= llvmpipe_resource_create
;
494 screen
->resource_destroy
= llvmpipe_resource_destroy
;
495 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
496 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
497 screen
->user_buffer_create
= llvmpipe_user_buffer_create
;
499 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
500 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
505 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
507 pipe
->get_transfer
= llvmpipe_get_transfer
;
508 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
509 pipe
->transfer_map
= llvmpipe_transfer_map
;
510 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
511 pipe
->is_resource_referenced
= llvmpipe_is_resource_referenced
;
513 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
514 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;