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 "pipe/p_inlines.h"
36 #include "util/u_format.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
40 #include "sp_context.h"
42 #include "sp_texture.h"
43 #include "sp_screen.h"
44 #include "sp_winsys.h"
48 * Conventional allocation path for non-display textures:
49 * Use a simple, maximally packed layout.
52 softpipe_texture_layout(struct pipe_screen
*screen
,
53 struct softpipe_texture
* spt
)
55 struct pipe_texture
*pt
= &spt
->base
;
57 unsigned width
= pt
->width0
;
58 unsigned height
= pt
->height0
;
59 unsigned depth
= pt
->depth0
;
61 unsigned buffer_size
= 0;
67 for (level
= 0; level
<= pt
->last_level
; level
++) {
68 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, width
);
69 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, height
);
70 spt
->stride
[level
] = pt
->nblocksx
[level
]*pt
->block
.size
;
72 spt
->level_offset
[level
] = buffer_size
;
74 buffer_size
+= (pt
->nblocksy
[level
] *
75 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
78 width
= u_minify(width
, 1);
79 height
= u_minify(height
, 1);
80 depth
= u_minify(depth
, 1);
83 spt
->buffer
= screen
->buffer_create(screen
, 32,
84 PIPE_BUFFER_USAGE_PIXEL
,
87 return spt
->buffer
!= NULL
;
92 * Texture layout for simple color buffers.
95 softpipe_displaytarget_layout(struct pipe_screen
*screen
,
96 struct softpipe_texture
* spt
)
98 unsigned usage
= (PIPE_BUFFER_USAGE_CPU_READ_WRITE
|
99 PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
100 unsigned tex_usage
= spt
->base
.tex_usage
;
102 spt
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width0
);
103 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height0
);
105 spt
->buffer
= screen
->surface_buffer_create( screen
,
113 return spt
->buffer
!= NULL
;
118 * Create new pipe_texture given the template information.
120 static struct pipe_texture
*
121 softpipe_texture_create(struct pipe_screen
*screen
,
122 const struct pipe_texture
*template)
124 struct softpipe_texture
*spt
= CALLOC_STRUCT(softpipe_texture
);
128 spt
->base
= *template;
129 pipe_reference_init(&spt
->base
.reference
, 1);
130 spt
->base
.screen
= screen
;
132 spt
->pot
= (util_is_power_of_two(template->width0
) &&
133 util_is_power_of_two(template->height0
) &&
134 util_is_power_of_two(template->depth0
));
136 if (spt
->base
.tex_usage
& (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
137 PIPE_TEXTURE_USAGE_PRIMARY
)) {
138 if (!softpipe_displaytarget_layout(screen
, spt
))
142 if (!softpipe_texture_layout(screen
, spt
))
155 * Create a new pipe_texture which wraps an existing buffer.
157 static struct pipe_texture
*
158 softpipe_texture_blanket(struct pipe_screen
* screen
,
159 const struct pipe_texture
*base
,
160 const unsigned *stride
,
161 struct pipe_buffer
*buffer
)
163 struct softpipe_texture
*spt
;
166 /* Only supports one type */
167 if (base
->target
!= PIPE_TEXTURE_2D
||
168 base
->last_level
!= 0 ||
173 spt
= CALLOC_STRUCT(softpipe_texture
);
178 pipe_reference_init(&spt
->base
.reference
, 1);
179 spt
->base
.screen
= screen
;
180 spt
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width0
);
181 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height0
);
182 spt
->stride
[0] = stride
[0];
184 pipe_buffer_reference(&spt
->buffer
, buffer
);
191 softpipe_texture_destroy(struct pipe_texture
*pt
)
193 struct softpipe_texture
*spt
= softpipe_texture(pt
);
195 pipe_buffer_reference(&spt
->buffer
, NULL
);
201 * Get a pipe_surface "view" into a texture.
203 static struct pipe_surface
*
204 softpipe_get_tex_surface(struct pipe_screen
*screen
,
205 struct pipe_texture
*pt
,
206 unsigned face
, unsigned level
, unsigned zslice
,
209 struct softpipe_texture
*spt
= softpipe_texture(pt
);
210 struct pipe_surface
*ps
;
212 assert(level
<= pt
->last_level
);
214 ps
= CALLOC_STRUCT(pipe_surface
);
216 pipe_reference_init(&ps
->reference
, 1);
217 pipe_texture_reference(&ps
->texture
, pt
);
218 ps
->format
= pt
->format
;
219 ps
->width
= u_minify(pt
->width0
, level
);
220 ps
->height
= u_minify(pt
->height0
, level
);
221 ps
->offset
= spt
->level_offset
[level
];
224 /* Because we are softpipe, anything that the state tracker
225 * thought was going to be done with the GPU will actually get
226 * done with the CPU. Let's adjust the flags to take that into
229 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
230 /* GPU_WRITE means "render" and that can involve reads (blending) */
231 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
234 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
235 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
237 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
238 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
239 /* Mark the surface as dirty. The tile cache will look for this. */
241 softpipe_screen(screen
)->timestamp
++;
248 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
249 ps
->offset
+= face
* pt
->nblocksy
[level
] * spt
->stride
[level
];
251 else if (pt
->target
== PIPE_TEXTURE_3D
) {
252 ps
->offset
+= zslice
* pt
->nblocksy
[level
] * spt
->stride
[level
];
264 * Free a pipe_surface which was created with softpipe_get_tex_surface().
267 softpipe_tex_surface_destroy(struct pipe_surface
*surf
)
269 /* Effectively do the texture_update work here - if texture images
270 * needed post-processing to put them into hardware layout, this is
271 * where it would happen. For softpipe, nothing to do.
273 assert(surf
->texture
);
274 pipe_texture_reference(&surf
->texture
, NULL
);
280 * Geta pipe_transfer object which is used for moving data in/out of
282 * \param face one of PIPE_TEX_FACE_x or 0
283 * \param level texture mipmap level
284 * \param zslice 2D slice of a 3D texture
285 * \param usage one of PIPE_TRANSFER_READ/WRITE/READ_WRITE
286 * \param x X position of region to read/write
287 * \param y Y position of region to read/write
288 * \param width width of region to read/write
289 * \param height height of region to read/write
291 static struct pipe_transfer
*
292 softpipe_get_tex_transfer(struct pipe_screen
*screen
,
293 struct pipe_texture
*texture
,
294 unsigned face
, unsigned level
, unsigned zslice
,
295 enum pipe_transfer_usage usage
,
296 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
298 struct softpipe_texture
*sptex
= softpipe_texture(texture
);
299 struct softpipe_transfer
*spt
;
302 assert(level
<= texture
->last_level
);
304 spt
= CALLOC_STRUCT(softpipe_transfer
);
306 struct pipe_transfer
*pt
= &spt
->base
;
307 pipe_texture_reference(&pt
->texture
, texture
);
308 pt
->format
= texture
->format
;
309 pt
->block
= texture
->block
;
314 pt
->nblocksx
= texture
->nblocksx
[level
];
315 pt
->nblocksy
= texture
->nblocksy
[level
];
316 pt
->stride
= sptex
->stride
[level
];
322 spt
->offset
= sptex
->level_offset
[level
];
324 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
325 spt
->offset
+= face
* pt
->nblocksy
* pt
->stride
;
327 else if (texture
->target
== PIPE_TEXTURE_3D
) {
328 spt
->offset
+= zslice
* pt
->nblocksy
* pt
->stride
;
341 * Free a pipe_transfer object which was created with
342 * softpipe_get_tex_transfer().
345 softpipe_tex_transfer_destroy(struct pipe_transfer
*transfer
)
347 /* Effectively do the texture_update work here - if texture images
348 * needed post-processing to put them into hardware layout, this is
349 * where it would happen. For softpipe, nothing to do.
351 assert (transfer
->texture
);
352 pipe_texture_reference(&transfer
->texture
, NULL
);
358 * Create memory mapping for given pipe_transfer object.
361 softpipe_transfer_map( struct pipe_screen
*screen
,
362 struct pipe_transfer
*transfer
)
364 ubyte
*map
, *xfer_map
;
365 struct softpipe_texture
*spt
;
367 assert(transfer
->texture
);
368 spt
= softpipe_texture(transfer
->texture
);
370 map
= pipe_buffer_map(screen
, spt
->buffer
, pipe_transfer_buffer_flags(transfer
));
374 /* May want to different things here depending on read/write nature
377 if (transfer
->texture
&& (transfer
->usage
& PIPE_TRANSFER_WRITE
)) {
378 /* Do something to notify sharing contexts of a texture change.
379 * In softpipe, that would mean flushing the texture cache.
381 softpipe_screen(screen
)->timestamp
++;
384 xfer_map
= map
+ softpipe_transfer(transfer
)->offset
+
385 transfer
->y
/ transfer
->block
.height
* transfer
->stride
+
386 transfer
->x
/ transfer
->block
.width
* transfer
->block
.size
;
387 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
393 * Unmap memory mapping for given pipe_transfer object.
396 softpipe_transfer_unmap(struct pipe_screen
*screen
,
397 struct pipe_transfer
*transfer
)
399 struct softpipe_texture
*spt
;
401 assert(transfer
->texture
);
402 spt
= softpipe_texture(transfer
->texture
);
404 pipe_buffer_unmap( screen
, spt
->buffer
);
406 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
407 /* Mark the texture as dirty to expire the tile caches. */
413 static struct pipe_video_surface
*
414 softpipe_video_surface_create(struct pipe_screen
*screen
,
415 enum pipe_video_chroma_format chroma_format
,
416 unsigned width
, unsigned height
)
418 struct softpipe_video_surface
*sp_vsfc
;
419 struct pipe_texture
template;
422 assert(width
&& height
);
424 sp_vsfc
= CALLOC_STRUCT(softpipe_video_surface
);
428 pipe_reference_init(&sp_vsfc
->base
.reference
, 1);
429 sp_vsfc
->base
.screen
= screen
;
430 sp_vsfc
->base
.chroma_format
= chroma_format
;
431 /*sp_vsfc->base.surface_format = PIPE_VIDEO_SURFACE_FORMAT_VUYA;*/
432 sp_vsfc
->base
.width
= width
;
433 sp_vsfc
->base
.height
= height
;
435 memset(&template, 0, sizeof(struct pipe_texture
));
436 template.target
= PIPE_TEXTURE_2D
;
437 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
438 template.last_level
= 0;
439 /* vl_mpeg12_mc_renderer expects this when it's initialized with pot_buffers=true */
440 template.width0
= util_next_power_of_two(width
);
441 template.height0
= util_next_power_of_two(height
);
443 util_format_get_block(template.format
, &template.block
);
444 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
| PIPE_TEXTURE_USAGE_RENDER_TARGET
;
446 sp_vsfc
->tex
= screen
->texture_create(screen
, &template);
452 return &sp_vsfc
->base
;
457 softpipe_video_surface_destroy(struct pipe_video_surface
*vsfc
)
459 struct softpipe_video_surface
*sp_vsfc
= softpipe_video_surface(vsfc
);
461 pipe_texture_reference(&sp_vsfc
->tex
, NULL
);
467 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
469 screen
->texture_create
= softpipe_texture_create
;
470 screen
->texture_blanket
= softpipe_texture_blanket
;
471 screen
->texture_destroy
= softpipe_texture_destroy
;
473 screen
->get_tex_surface
= softpipe_get_tex_surface
;
474 screen
->tex_surface_destroy
= softpipe_tex_surface_destroy
;
476 screen
->get_tex_transfer
= softpipe_get_tex_transfer
;
477 screen
->tex_transfer_destroy
= softpipe_tex_transfer_destroy
;
478 screen
->transfer_map
= softpipe_transfer_map
;
479 screen
->transfer_unmap
= softpipe_transfer_unmap
;
481 screen
->video_surface_create
= softpipe_video_surface_create
;
482 screen
->video_surface_destroy
= softpipe_video_surface_destroy
;
487 * Return pipe_buffer handle and stride for given texture object.
491 softpipe_get_texture_buffer( struct pipe_texture
*texture
,
492 struct pipe_buffer
**buf
,
495 struct softpipe_texture
*tex
= (struct softpipe_texture
*) texture
;
500 pipe_buffer_reference(buf
, tex
->buffer
);
503 *stride
= tex
->stride
[0];