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 spt
->stride
[level
] = util_format_get_stride(pt
->format
, width
);
70 spt
->level_offset
[level
] = buffer_size
;
72 buffer_size
+= (util_format_get_nblocksy(pt
->format
, height
) *
73 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
76 width
= u_minify(width
, 1);
77 height
= u_minify(height
, 1);
78 depth
= u_minify(depth
, 1);
81 spt
->buffer
= screen
->buffer_create(screen
, 32,
82 PIPE_BUFFER_USAGE_PIXEL
,
85 return spt
->buffer
!= NULL
;
90 * Texture layout for simple color buffers.
93 softpipe_displaytarget_layout(struct pipe_screen
*screen
,
94 struct softpipe_texture
* spt
)
96 unsigned usage
= (PIPE_BUFFER_USAGE_CPU_READ_WRITE
|
97 PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
98 unsigned tex_usage
= spt
->base
.tex_usage
;
100 spt
->buffer
= screen
->surface_buffer_create( screen
,
108 return spt
->buffer
!= NULL
;
113 * Create new pipe_texture given the template information.
115 static struct pipe_texture
*
116 softpipe_texture_create(struct pipe_screen
*screen
,
117 const struct pipe_texture
*template)
119 struct softpipe_texture
*spt
= CALLOC_STRUCT(softpipe_texture
);
123 spt
->base
= *template;
124 pipe_reference_init(&spt
->base
.reference
, 1);
125 spt
->base
.screen
= screen
;
127 spt
->pot
= (util_is_power_of_two(template->width0
) &&
128 util_is_power_of_two(template->height0
) &&
129 util_is_power_of_two(template->depth0
));
131 if (spt
->base
.tex_usage
& (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
132 PIPE_TEXTURE_USAGE_PRIMARY
)) {
133 if (!softpipe_displaytarget_layout(screen
, spt
))
137 if (!softpipe_texture_layout(screen
, spt
))
150 * Create a new pipe_texture which wraps an existing buffer.
152 static struct pipe_texture
*
153 softpipe_texture_blanket(struct pipe_screen
* screen
,
154 const struct pipe_texture
*base
,
155 const unsigned *stride
,
156 struct pipe_buffer
*buffer
)
158 struct softpipe_texture
*spt
;
161 /* Only supports one type */
162 if (base
->target
!= PIPE_TEXTURE_2D
||
163 base
->last_level
!= 0 ||
168 spt
= CALLOC_STRUCT(softpipe_texture
);
173 pipe_reference_init(&spt
->base
.reference
, 1);
174 spt
->base
.screen
= screen
;
175 spt
->stride
[0] = stride
[0];
177 pipe_buffer_reference(&spt
->buffer
, buffer
);
184 softpipe_texture_destroy(struct pipe_texture
*pt
)
186 struct softpipe_texture
*spt
= softpipe_texture(pt
);
188 pipe_buffer_reference(&spt
->buffer
, NULL
);
194 * Get a pipe_surface "view" into a texture.
196 static struct pipe_surface
*
197 softpipe_get_tex_surface(struct pipe_screen
*screen
,
198 struct pipe_texture
*pt
,
199 unsigned face
, unsigned level
, unsigned zslice
,
202 struct softpipe_texture
*spt
= softpipe_texture(pt
);
203 struct pipe_surface
*ps
;
205 assert(level
<= pt
->last_level
);
207 ps
= CALLOC_STRUCT(pipe_surface
);
209 pipe_reference_init(&ps
->reference
, 1);
210 pipe_texture_reference(&ps
->texture
, pt
);
211 ps
->format
= pt
->format
;
212 ps
->width
= u_minify(pt
->width0
, level
);
213 ps
->height
= u_minify(pt
->height0
, level
);
214 ps
->offset
= spt
->level_offset
[level
];
217 /* Because we are softpipe, anything that the state tracker
218 * thought was going to be done with the GPU will actually get
219 * done with the CPU. Let's adjust the flags to take that into
222 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
223 /* GPU_WRITE means "render" and that can involve reads (blending) */
224 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
227 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
228 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
230 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
231 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
232 /* Mark the surface as dirty. The tile cache will look for this. */
234 softpipe_screen(screen
)->timestamp
++;
241 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
242 ps
->offset
+= face
* util_format_get_nblocksy(pt
->format
, u_minify(pt
->height0
, level
)) *
245 else if (pt
->target
== PIPE_TEXTURE_3D
) {
246 ps
->offset
+= zslice
* util_format_get_nblocksy(pt
->format
, u_minify(pt
->height0
, level
)) *
259 * Free a pipe_surface which was created with softpipe_get_tex_surface().
262 softpipe_tex_surface_destroy(struct pipe_surface
*surf
)
264 /* Effectively do the texture_update work here - if texture images
265 * needed post-processing to put them into hardware layout, this is
266 * where it would happen. For softpipe, nothing to do.
268 assert(surf
->texture
);
269 pipe_texture_reference(&surf
->texture
, NULL
);
275 * Geta pipe_transfer object which is used for moving data in/out of
277 * \param face one of PIPE_TEX_FACE_x or 0
278 * \param level texture mipmap level
279 * \param zslice 2D slice of a 3D texture
280 * \param usage one of PIPE_TRANSFER_READ/WRITE/READ_WRITE
281 * \param x X position of region to read/write
282 * \param y Y position of region to read/write
283 * \param width width of region to read/write
284 * \param height height of region to read/write
286 static struct pipe_transfer
*
287 softpipe_get_tex_transfer(struct pipe_screen
*screen
,
288 struct pipe_texture
*texture
,
289 unsigned face
, unsigned level
, unsigned zslice
,
290 enum pipe_transfer_usage usage
,
291 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
293 struct softpipe_texture
*sptex
= softpipe_texture(texture
);
294 struct softpipe_transfer
*spt
;
297 assert(level
<= texture
->last_level
);
299 spt
= CALLOC_STRUCT(softpipe_transfer
);
301 struct pipe_transfer
*pt
= &spt
->base
;
302 int nblocksy
= util_format_get_nblocksy(texture
->format
, u_minify(texture
->height0
, level
));
303 pipe_texture_reference(&pt
->texture
, texture
);
308 pt
->stride
= sptex
->stride
[level
];
314 spt
->offset
= sptex
->level_offset
[level
];
316 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
317 spt
->offset
+= face
* nblocksy
* pt
->stride
;
319 else if (texture
->target
== PIPE_TEXTURE_3D
) {
320 spt
->offset
+= zslice
* nblocksy
* pt
->stride
;
333 * Free a pipe_transfer object which was created with
334 * softpipe_get_tex_transfer().
337 softpipe_tex_transfer_destroy(struct pipe_transfer
*transfer
)
339 /* Effectively do the texture_update work here - if texture images
340 * needed post-processing to put them into hardware layout, this is
341 * where it would happen. For softpipe, nothing to do.
343 assert (transfer
->texture
);
344 pipe_texture_reference(&transfer
->texture
, NULL
);
350 * Create memory mapping for given pipe_transfer object.
353 softpipe_transfer_map( struct pipe_screen
*screen
,
354 struct pipe_transfer
*transfer
)
356 ubyte
*map
, *xfer_map
;
357 struct softpipe_texture
*spt
;
358 enum pipe_format format
;
360 assert(transfer
->texture
);
361 spt
= softpipe_texture(transfer
->texture
);
362 format
= transfer
->texture
->format
;
364 map
= pipe_buffer_map(screen
, spt
->buffer
, pipe_transfer_buffer_flags(transfer
));
368 /* May want to different things here depending on read/write nature
371 if (transfer
->texture
&& (transfer
->usage
& PIPE_TRANSFER_WRITE
)) {
372 /* Do something to notify sharing contexts of a texture change.
373 * In softpipe, that would mean flushing the texture cache.
375 softpipe_screen(screen
)->timestamp
++;
378 xfer_map
= map
+ softpipe_transfer(transfer
)->offset
+
379 transfer
->y
/ util_format_get_blockheight(format
) * transfer
->stride
+
380 transfer
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
381 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
387 * Unmap memory mapping for given pipe_transfer object.
390 softpipe_transfer_unmap(struct pipe_screen
*screen
,
391 struct pipe_transfer
*transfer
)
393 struct softpipe_texture
*spt
;
395 assert(transfer
->texture
);
396 spt
= softpipe_texture(transfer
->texture
);
398 pipe_buffer_unmap( screen
, spt
->buffer
);
400 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
401 /* Mark the texture as dirty to expire the tile caches. */
407 static struct pipe_video_surface
*
408 softpipe_video_surface_create(struct pipe_screen
*screen
,
409 enum pipe_video_chroma_format chroma_format
,
410 unsigned width
, unsigned height
)
412 struct softpipe_video_surface
*sp_vsfc
;
413 struct pipe_texture
template;
416 assert(width
&& height
);
418 sp_vsfc
= CALLOC_STRUCT(softpipe_video_surface
);
422 pipe_reference_init(&sp_vsfc
->base
.reference
, 1);
423 sp_vsfc
->base
.screen
= screen
;
424 sp_vsfc
->base
.chroma_format
= chroma_format
;
425 /*sp_vsfc->base.surface_format = PIPE_VIDEO_SURFACE_FORMAT_VUYA;*/
426 sp_vsfc
->base
.width
= width
;
427 sp_vsfc
->base
.height
= height
;
429 memset(&template, 0, sizeof(struct pipe_texture
));
430 template.target
= PIPE_TEXTURE_2D
;
431 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
432 template.last_level
= 0;
433 /* vl_mpeg12_mc_renderer expects this when it's initialized with pot_buffers=true */
434 template.width0
= util_next_power_of_two(width
);
435 template.height0
= util_next_power_of_two(height
);
437 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
| PIPE_TEXTURE_USAGE_RENDER_TARGET
;
439 sp_vsfc
->tex
= screen
->texture_create(screen
, &template);
445 return &sp_vsfc
->base
;
450 softpipe_video_surface_destroy(struct pipe_video_surface
*vsfc
)
452 struct softpipe_video_surface
*sp_vsfc
= softpipe_video_surface(vsfc
);
454 pipe_texture_reference(&sp_vsfc
->tex
, NULL
);
460 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
462 screen
->texture_create
= softpipe_texture_create
;
463 screen
->texture_blanket
= softpipe_texture_blanket
;
464 screen
->texture_destroy
= softpipe_texture_destroy
;
466 screen
->get_tex_surface
= softpipe_get_tex_surface
;
467 screen
->tex_surface_destroy
= softpipe_tex_surface_destroy
;
469 screen
->get_tex_transfer
= softpipe_get_tex_transfer
;
470 screen
->tex_transfer_destroy
= softpipe_tex_transfer_destroy
;
471 screen
->transfer_map
= softpipe_transfer_map
;
472 screen
->transfer_unmap
= softpipe_transfer_unmap
;
474 screen
->video_surface_create
= softpipe_video_surface_create
;
475 screen
->video_surface_destroy
= softpipe_video_surface_destroy
;
480 * Return pipe_buffer handle and stride for given texture object.
484 softpipe_get_texture_buffer( struct pipe_texture
*texture
,
485 struct pipe_buffer
**buf
,
488 struct softpipe_texture
*tex
= (struct softpipe_texture
*) texture
;
493 pipe_buffer_reference(buf
, tex
->buffer
);
496 *stride
= tex
->stride
[0];