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"
42 #include "sp_texture.h"
43 #include "sp_screen.h"
45 #include "state_tracker/sw_winsys.h"
49 * Conventional allocation path for non-display textures:
50 * Use a simple, maximally packed layout.
53 softpipe_resource_layout(struct pipe_screen
*screen
,
54 struct softpipe_resource
*spr
)
56 struct pipe_resource
*pt
= &spr
->base
;
58 unsigned width
= pt
->width0
;
59 unsigned height
= pt
->height0
;
60 unsigned depth
= pt
->depth0
;
61 unsigned buffer_size
= 0;
63 for (level
= 0; level
<= pt
->last_level
; level
++) {
64 spr
->stride
[level
] = util_format_get_stride(pt
->format
, width
);
66 spr
->level_offset
[level
] = buffer_size
;
68 buffer_size
+= (util_format_get_nblocksy(pt
->format
, height
) *
69 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
72 width
= u_minify(width
, 1);
73 height
= u_minify(height
, 1);
74 depth
= u_minify(depth
, 1);
77 spr
->data
= align_malloc(buffer_size
, 16);
79 return spr
->data
!= NULL
;
84 * Texture layout for simple color buffers.
87 softpipe_displaytarget_layout(struct pipe_screen
*screen
,
88 struct softpipe_resource
*spr
)
90 struct sw_winsys
*winsys
= softpipe_screen(screen
)->winsys
;
92 /* Round up the surface size to a multiple of the tile size?
94 spr
->dt
= winsys
->displaytarget_create(winsys
,
102 return spr
->dt
!= NULL
;
107 * Create new pipe_resource given the template information.
109 static struct pipe_resource
*
110 softpipe_resource_create(struct pipe_screen
*screen
,
111 const struct pipe_resource
*templat
)
113 struct softpipe_resource
*spr
= CALLOC_STRUCT(softpipe_resource
);
117 assert(templat
->format
!= PIPE_FORMAT_NONE
);
119 spr
->base
= *templat
;
120 pipe_reference_init(&spr
->base
.reference
, 1);
121 spr
->base
.screen
= screen
;
123 spr
->pot
= (util_is_power_of_two(templat
->width0
) &&
124 util_is_power_of_two(templat
->height0
) &&
125 util_is_power_of_two(templat
->depth0
));
127 if (spr
->base
.bind
& (PIPE_BIND_DISPLAY_TARGET
|
130 if (!softpipe_displaytarget_layout(screen
, spr
))
134 if (!softpipe_resource_layout(screen
, spr
))
147 softpipe_resource_destroy(struct pipe_screen
*pscreen
,
148 struct pipe_resource
*pt
)
150 struct softpipe_screen
*screen
= softpipe_screen(pscreen
);
151 struct softpipe_resource
*spr
= softpipe_resource(pt
);
155 struct sw_winsys
*winsys
= screen
->winsys
;
156 winsys
->displaytarget_destroy(winsys
, spr
->dt
);
158 else if (!spr
->userBuffer
) {
159 /* regular texture */
160 align_free(spr
->data
);
167 static struct pipe_resource
*
168 softpipe_resource_from_handle(struct pipe_screen
*screen
,
169 const struct pipe_resource
*templat
,
170 struct winsys_handle
*whandle
)
172 struct sw_winsys
*winsys
= softpipe_screen(screen
)->winsys
;
173 struct softpipe_resource
*spr
= CALLOC_STRUCT(softpipe_resource
);
177 spr
->base
= *templat
;
178 pipe_reference_init(&spr
->base
.reference
, 1);
179 spr
->base
.screen
= screen
;
181 spr
->pot
= (util_is_power_of_two(templat
->width0
) &&
182 util_is_power_of_two(templat
->height0
) &&
183 util_is_power_of_two(templat
->depth0
));
185 spr
->dt
= winsys
->displaytarget_from_handle(winsys
,
201 softpipe_resource_get_handle(struct pipe_screen
*screen
,
202 struct pipe_resource
*pt
,
203 struct winsys_handle
*whandle
)
205 struct sw_winsys
*winsys
= softpipe_screen(screen
)->winsys
;
206 struct softpipe_resource
*spr
= softpipe_resource(pt
);
212 return winsys
->displaytarget_get_handle(winsys
, spr
->dt
, whandle
);
217 * Get a pipe_surface "view" into a texture resource.
219 static struct pipe_surface
*
220 softpipe_get_tex_surface(struct pipe_screen
*screen
,
221 struct pipe_resource
*pt
,
222 unsigned face
, unsigned level
, unsigned zslice
,
225 struct softpipe_resource
*spr
= softpipe_resource(pt
);
226 struct pipe_surface
*ps
;
228 assert(level
<= pt
->last_level
);
230 ps
= CALLOC_STRUCT(pipe_surface
);
232 pipe_reference_init(&ps
->reference
, 1);
233 pipe_resource_reference(&ps
->texture
, pt
);
234 ps
->format
= pt
->format
;
235 ps
->width
= u_minify(pt
->width0
, level
);
236 ps
->height
= u_minify(pt
->height0
, level
);
237 ps
->offset
= spr
->level_offset
[level
];
244 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
245 ps
->offset
+= face
* util_format_get_nblocksy(pt
->format
, u_minify(pt
->height0
, level
)) *
248 else if (pt
->target
== PIPE_TEXTURE_3D
) {
249 ps
->offset
+= zslice
* util_format_get_nblocksy(pt
->format
, u_minify(pt
->height0
, level
)) *
262 * Free a pipe_surface which was created with softpipe_get_tex_surface().
265 softpipe_tex_surface_destroy(struct pipe_surface
*surf
)
267 /* Effectively do the texture_update work here - if texture images
268 * needed post-processing to put them into hardware layout, this is
269 * where it would happen. For softpipe, nothing to do.
271 assert(surf
->texture
);
272 pipe_resource_reference(&surf
->texture
, NULL
);
278 * Geta pipe_transfer object which is used for moving data in/out of
280 * \param pipe rendering context
281 * \param resource the resource to transfer in/out of
282 * \param sr indicates cube face or 3D texture slice
283 * \param usage bitmask of PIPE_TRANSFER_x flags
284 * \param box the 1D/2D/3D region of interest
286 static struct pipe_transfer
*
287 softpipe_get_transfer(struct pipe_context
*pipe
,
288 struct pipe_resource
*resource
,
289 struct pipe_subresource sr
,
291 const struct pipe_box
*box
)
293 struct softpipe_resource
*sprex
= softpipe_resource(resource
);
294 struct softpipe_transfer
*spr
;
297 assert(sr
.level
<= resource
->last_level
);
299 /* make sure the requested region is in the image bounds */
300 assert(box
->x
+ box
->width
<= u_minify(resource
->width0
, sr
.level
));
301 assert(box
->y
+ box
->height
<= u_minify(resource
->height0
, sr
.level
));
302 assert(box
->z
+ box
->depth
<= u_minify(resource
->depth0
, sr
.level
));
304 spr
= CALLOC_STRUCT(softpipe_transfer
);
306 struct pipe_transfer
*pt
= &spr
->base
;
307 enum pipe_format format
= resource
->format
;
308 int nblocksy
= util_format_get_nblocksy(resource
->format
,
309 u_minify(resource
->height0
, sr
.level
));
310 pipe_resource_reference(&pt
->resource
, resource
);
314 pt
->stride
= sprex
->stride
[sr
.level
];
316 spr
->offset
= sprex
->level_offset
[sr
.level
];
318 if (resource
->target
== PIPE_TEXTURE_CUBE
) {
319 spr
->offset
+= sr
.face
* nblocksy
* pt
->stride
;
321 else if (resource
->target
== PIPE_TEXTURE_3D
) {
322 spr
->offset
+= box
->z
* nblocksy
* pt
->stride
;
325 assert(sr
.face
== 0);
330 box
->y
/ util_format_get_blockheight(format
) * spr
->base
.stride
+
331 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
340 * Free a pipe_transfer object which was created with
341 * softpipe_get_transfer().
344 softpipe_transfer_destroy(struct pipe_context
*pipe
,
345 struct pipe_transfer
*transfer
)
347 pipe_resource_reference(&transfer
->resource
, NULL
);
353 * Create memory mapping for given pipe_transfer object.
356 softpipe_transfer_map(struct pipe_context
*pipe
,
357 struct pipe_transfer
*transfer
)
359 struct softpipe_transfer
*sp_transfer
= softpipe_transfer(transfer
);
360 struct softpipe_resource
*sp_resource
= softpipe_resource(transfer
->resource
);
361 struct sw_winsys
*winsys
= softpipe_screen(pipe
->screen
)->winsys
;
364 /* resources backed by display target treated specially:
366 if (sp_resource
->dt
) {
367 map
= winsys
->displaytarget_map(winsys
,
372 map
= sp_resource
->data
;
378 return map
+ sp_transfer
->offset
;
383 * Unmap memory mapping for given pipe_transfer object.
386 softpipe_transfer_unmap(struct pipe_context
*pipe
,
387 struct pipe_transfer
*transfer
)
389 struct softpipe_resource
*spr
;
391 assert(transfer
->resource
);
392 spr
= softpipe_resource(transfer
->resource
);
396 struct sw_winsys
*winsys
= softpipe_screen(pipe
->screen
)->winsys
;
397 winsys
->displaytarget_unmap(winsys
, spr
->dt
);
400 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
401 /* Mark the texture as dirty to expire the tile caches. */
407 * Create buffer which wraps user-space data.
409 static struct pipe_resource
*
410 softpipe_user_buffer_create(struct pipe_screen
*screen
,
415 struct softpipe_resource
*buffer
;
417 buffer
= CALLOC_STRUCT(softpipe_resource
);
422 pipe_reference_init(&buffer
->base
.reference
, 1);
423 buffer
->base
.screen
= screen
;
424 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
425 buffer
->base
.bind
= bind_flags
;
426 buffer
->base
._usage
= PIPE_USAGE_IMMUTABLE
;
427 buffer
->base
.flags
= 0;
428 buffer
->base
.width0
= bytes
;
429 buffer
->base
.height0
= 1;
430 buffer
->base
.depth0
= 1;
431 buffer
->userBuffer
= TRUE
;
434 return &buffer
->base
;
439 softpipe_init_texture_funcs(struct pipe_context
*pipe
)
441 pipe
->get_transfer
= softpipe_get_transfer
;
442 pipe
->transfer_destroy
= softpipe_transfer_destroy
;
443 pipe
->transfer_map
= softpipe_transfer_map
;
444 pipe
->transfer_unmap
= softpipe_transfer_unmap
;
446 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
447 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;
452 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
454 screen
->resource_create
= softpipe_resource_create
;
455 screen
->resource_destroy
= softpipe_resource_destroy
;
456 screen
->resource_from_handle
= softpipe_resource_from_handle
;
457 screen
->resource_get_handle
= softpipe_resource_get_handle
;
458 screen
->user_buffer_create
= softpipe_user_buffer_create
;
460 screen
->get_tex_surface
= softpipe_get_tex_surface
;
461 screen
->tex_surface_destroy
= softpipe_tex_surface_destroy
;