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_defines.h"
34 #include "util/u_inlines.h"
36 #include "util/u_format.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
39 #include "util/u_transfer.h"
41 #include "sp_context.h"
43 #include "sp_texture.h"
44 #include "sp_screen.h"
46 #include "state_tracker/sw_winsys.h"
50 * Conventional allocation path for non-display textures:
51 * Use a simple, maximally packed layout.
54 softpipe_resource_layout(struct pipe_screen
*screen
,
55 struct softpipe_resource
*spr
)
57 struct pipe_resource
*pt
= &spr
->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 spr
->stride
[level
] = util_format_get_stride(pt
->format
, width
);
67 spr
->level_offset
[level
] = buffer_size
;
69 buffer_size
+= (util_format_get_nblocksy(pt
->format
, height
) *
70 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
73 width
= u_minify(width
, 1);
74 height
= u_minify(height
, 1);
75 depth
= u_minify(depth
, 1);
78 spr
->data
= align_malloc(buffer_size
, 16);
80 return spr
->data
!= NULL
;
85 * Texture layout for simple color buffers.
88 softpipe_displaytarget_layout(struct pipe_screen
*screen
,
89 struct softpipe_resource
*spr
)
91 struct sw_winsys
*winsys
= softpipe_screen(screen
)->winsys
;
93 /* Round up the surface size to a multiple of the tile size?
95 spr
->dt
= winsys
->displaytarget_create(winsys
,
103 return spr
->dt
!= NULL
;
108 * Create new pipe_resource given the template information.
110 static struct pipe_resource
*
111 softpipe_resource_create(struct pipe_screen
*screen
,
112 const struct pipe_resource
*templat
)
114 struct softpipe_resource
*spr
= CALLOC_STRUCT(softpipe_resource
);
118 assert(templat
->format
!= PIPE_FORMAT_NONE
);
120 spr
->base
= *templat
;
121 pipe_reference_init(&spr
->base
.reference
, 1);
122 spr
->base
.screen
= screen
;
124 spr
->pot
= (util_is_power_of_two(templat
->width0
) &&
125 util_is_power_of_two(templat
->height0
) &&
126 util_is_power_of_two(templat
->depth0
));
128 if (spr
->base
.bind
& (PIPE_BIND_DISPLAY_TARGET
|
131 if (!softpipe_displaytarget_layout(screen
, spr
))
135 if (!softpipe_resource_layout(screen
, spr
))
148 softpipe_resource_destroy(struct pipe_screen
*pscreen
,
149 struct pipe_resource
*pt
)
151 struct softpipe_screen
*screen
= softpipe_screen(pscreen
);
152 struct softpipe_resource
*spr
= softpipe_resource(pt
);
156 struct sw_winsys
*winsys
= screen
->winsys
;
157 winsys
->displaytarget_destroy(winsys
, spr
->dt
);
159 else if (!spr
->userBuffer
) {
160 /* regular texture */
161 align_free(spr
->data
);
168 static struct pipe_resource
*
169 softpipe_resource_from_handle(struct pipe_screen
*screen
,
170 const struct pipe_resource
*templat
,
171 struct winsys_handle
*whandle
)
173 struct sw_winsys
*winsys
= softpipe_screen(screen
)->winsys
;
174 struct softpipe_resource
*spr
= CALLOC_STRUCT(softpipe_resource
);
178 spr
->base
= *templat
;
179 pipe_reference_init(&spr
->base
.reference
, 1);
180 spr
->base
.screen
= screen
;
182 spr
->pot
= (util_is_power_of_two(templat
->width0
) &&
183 util_is_power_of_two(templat
->height0
) &&
184 util_is_power_of_two(templat
->depth0
));
186 spr
->dt
= winsys
->displaytarget_from_handle(winsys
,
202 softpipe_resource_get_handle(struct pipe_screen
*screen
,
203 struct pipe_resource
*pt
,
204 struct winsys_handle
*whandle
)
206 struct sw_winsys
*winsys
= softpipe_screen(screen
)->winsys
;
207 struct softpipe_resource
*spr
= softpipe_resource(pt
);
213 return winsys
->displaytarget_get_handle(winsys
, spr
->dt
, whandle
);
218 * Get a pipe_surface "view" into a texture resource.
220 static struct pipe_surface
*
221 softpipe_get_tex_surface(struct pipe_screen
*screen
,
222 struct pipe_resource
*pt
,
223 unsigned face
, unsigned level
, unsigned zslice
,
226 struct softpipe_resource
*spr
= softpipe_resource(pt
);
227 struct pipe_surface
*ps
;
229 assert(level
<= pt
->last_level
);
231 ps
= CALLOC_STRUCT(pipe_surface
);
233 pipe_reference_init(&ps
->reference
, 1);
234 pipe_resource_reference(&ps
->texture
, pt
);
235 ps
->format
= pt
->format
;
236 ps
->width
= u_minify(pt
->width0
, level
);
237 ps
->height
= u_minify(pt
->height0
, level
);
238 ps
->offset
= spr
->level_offset
[level
];
245 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
246 ps
->offset
+= face
* util_format_get_nblocksy(pt
->format
, u_minify(pt
->height0
, level
)) *
249 else if (pt
->target
== PIPE_TEXTURE_3D
) {
250 ps
->offset
+= zslice
* util_format_get_nblocksy(pt
->format
, u_minify(pt
->height0
, level
)) *
263 * Free a pipe_surface which was created with softpipe_get_tex_surface().
266 softpipe_tex_surface_destroy(struct pipe_surface
*surf
)
268 /* Effectively do the texture_update work here - if texture images
269 * needed post-processing to put them into hardware layout, this is
270 * where it would happen. For softpipe, nothing to do.
272 assert(surf
->texture
);
273 pipe_resource_reference(&surf
->texture
, NULL
);
279 * Geta pipe_transfer object which is used for moving data in/out of
281 * \param pipe rendering context
282 * \param resource the resource to transfer in/out of
283 * \param sr indicates cube face or 3D texture slice
284 * \param usage bitmask of PIPE_TRANSFER_x flags
285 * \param box the 1D/2D/3D region of interest
287 static struct pipe_transfer
*
288 softpipe_get_transfer(struct pipe_context
*pipe
,
289 struct pipe_resource
*resource
,
290 struct pipe_subresource sr
,
292 const struct pipe_box
*box
)
294 struct softpipe_resource
*spr
= softpipe_resource(resource
);
295 struct softpipe_transfer
*spt
;
298 assert(sr
.level
<= resource
->last_level
);
300 /* make sure the requested region is in the image bounds */
301 assert(box
->x
+ box
->width
<= u_minify(resource
->width0
, sr
.level
));
302 assert(box
->y
+ box
->height
<= u_minify(resource
->height0
, sr
.level
));
303 assert(box
->z
+ box
->depth
<= u_minify(resource
->depth0
, sr
.level
));
306 * Transfers, like other pipe operations, must happen in order, so flush the
307 * context if necessary.
309 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
310 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
311 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
312 if (!softpipe_flush_resource(pipe
, resource
,
316 TRUE
, /* cpu_access */
319 * It would have blocked, but state tracker requested no to.
321 assert(do_not_block
);
326 spt
= CALLOC_STRUCT(softpipe_transfer
);
328 struct pipe_transfer
*pt
= &spt
->base
;
329 enum pipe_format format
= resource
->format
;
330 int nblocksy
= util_format_get_nblocksy(resource
->format
,
331 u_minify(resource
->height0
, sr
.level
));
332 pipe_resource_reference(&pt
->resource
, resource
);
336 pt
->stride
= spr
->stride
[sr
.level
];
338 spt
->offset
= spr
->level_offset
[sr
.level
];
340 if (resource
->target
== PIPE_TEXTURE_CUBE
) {
341 spt
->offset
+= sr
.face
* nblocksy
* pt
->stride
;
343 else if (resource
->target
== PIPE_TEXTURE_3D
) {
344 spt
->offset
+= box
->z
* nblocksy
* pt
->stride
;
347 assert(sr
.face
== 0);
352 box
->y
/ util_format_get_blockheight(format
) * spt
->base
.stride
+
353 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
362 * Free a pipe_transfer object which was created with
363 * softpipe_get_transfer().
366 softpipe_transfer_destroy(struct pipe_context
*pipe
,
367 struct pipe_transfer
*transfer
)
369 pipe_resource_reference(&transfer
->resource
, NULL
);
375 * Create memory mapping for given pipe_transfer object.
378 softpipe_transfer_map(struct pipe_context
*pipe
,
379 struct pipe_transfer
*transfer
)
381 struct softpipe_transfer
*spt
= softpipe_transfer(transfer
);
382 struct softpipe_resource
*spr
= softpipe_resource(transfer
->resource
);
383 struct sw_winsys
*winsys
= softpipe_screen(pipe
->screen
)->winsys
;
386 /* resources backed by display target treated specially:
389 map
= winsys
->displaytarget_map(winsys
, spr
->dt
, transfer
->usage
);
398 return map
+ spt
->offset
;
403 * Unmap memory mapping for given pipe_transfer object.
406 softpipe_transfer_unmap(struct pipe_context
*pipe
,
407 struct pipe_transfer
*transfer
)
409 struct softpipe_resource
*spr
;
411 assert(transfer
->resource
);
412 spr
= softpipe_resource(transfer
->resource
);
416 struct sw_winsys
*winsys
= softpipe_screen(pipe
->screen
)->winsys
;
417 winsys
->displaytarget_unmap(winsys
, spr
->dt
);
420 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
421 /* Mark the texture as dirty to expire the tile caches. */
427 * Create buffer which wraps user-space data.
429 static struct pipe_resource
*
430 softpipe_user_buffer_create(struct pipe_screen
*screen
,
435 struct softpipe_resource
*spr
;
437 spr
= CALLOC_STRUCT(softpipe_resource
);
441 pipe_reference_init(&spr
->base
.reference
, 1);
442 spr
->base
.screen
= screen
;
443 spr
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
444 spr
->base
.bind
= bind_flags
;
445 spr
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
447 spr
->base
.width0
= bytes
;
448 spr
->base
.height0
= 1;
449 spr
->base
.depth0
= 1;
450 spr
->userBuffer
= TRUE
;
458 softpipe_init_texture_funcs(struct pipe_context
*pipe
)
460 pipe
->get_transfer
= softpipe_get_transfer
;
461 pipe
->transfer_destroy
= softpipe_transfer_destroy
;
462 pipe
->transfer_map
= softpipe_transfer_map
;
463 pipe
->transfer_unmap
= softpipe_transfer_unmap
;
465 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
466 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;
471 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
473 screen
->resource_create
= softpipe_resource_create
;
474 screen
->resource_destroy
= softpipe_resource_destroy
;
475 screen
->resource_from_handle
= softpipe_resource_from_handle
;
476 screen
->resource_get_handle
= softpipe_resource_get_handle
;
477 screen
->user_buffer_create
= softpipe_user_buffer_create
;
479 screen
->get_tex_surface
= softpipe_get_tex_surface
;
480 screen
->tex_surface_destroy
= softpipe_tex_surface_destroy
;