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"
35 #include "util/u_math.h"
36 #include "util/u_memory.h"
38 #include "sp_context.h"
40 #include "sp_texture.h"
41 #include "sp_screen.h"
42 #include "sp_winsys.h"
46 * Conventional allocation path for non-display textures:
47 * Use a simple, maximally packed layout.
50 softpipe_texture_layout(struct pipe_screen
*screen
,
51 struct softpipe_texture
* spt
)
53 struct pipe_texture
*pt
= &spt
->base
;
55 unsigned width
= pt
->width0
;
56 unsigned height
= pt
->height0
;
57 unsigned depth
= pt
->depth0
;
59 unsigned buffer_size
= 0;
65 for (level
= 0; level
<= pt
->last_level
; level
++) {
66 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, width
);
67 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, height
);
68 spt
->stride
[level
] = pt
->nblocksx
[level
]*pt
->block
.size
;
70 spt
->level_offset
[level
] = buffer_size
;
72 buffer_size
+= (pt
->nblocksy
[level
] *
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
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width0
);
101 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height0
);
103 spt
->buffer
= screen
->surface_buffer_create( screen
,
111 return spt
->buffer
!= NULL
;
116 * Create new pipe_texture given the template information.
118 static struct pipe_texture
*
119 softpipe_texture_create(struct pipe_screen
*screen
,
120 const struct pipe_texture
*template)
122 struct softpipe_texture
*spt
= CALLOC_STRUCT(softpipe_texture
);
126 spt
->base
= *template;
127 pipe_reference_init(&spt
->base
.reference
, 1);
128 spt
->base
.screen
= screen
;
130 spt
->pot
= (util_is_power_of_two(template->width0
) &&
131 util_is_power_of_two(template->height0
) &&
132 util_is_power_of_two(template->depth0
));
134 if (spt
->base
.tex_usage
& (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
135 PIPE_TEXTURE_USAGE_PRIMARY
)) {
136 if (!softpipe_displaytarget_layout(screen
, spt
))
140 if (!softpipe_texture_layout(screen
, spt
))
153 * Create a new pipe_texture which wraps an existing buffer.
155 static struct pipe_texture
*
156 softpipe_texture_blanket(struct pipe_screen
* screen
,
157 const struct pipe_texture
*base
,
158 const unsigned *stride
,
159 struct pipe_buffer
*buffer
)
161 struct softpipe_texture
*spt
;
164 /* Only supports one type */
165 if (base
->target
!= PIPE_TEXTURE_2D
||
166 base
->last_level
!= 0 ||
171 spt
= CALLOC_STRUCT(softpipe_texture
);
176 pipe_reference_init(&spt
->base
.reference
, 1);
177 spt
->base
.screen
= screen
;
178 spt
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width0
);
179 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height0
);
180 spt
->stride
[0] = stride
[0];
182 pipe_buffer_reference(&spt
->buffer
, buffer
);
189 softpipe_texture_destroy(struct pipe_texture
*pt
)
191 struct softpipe_texture
*spt
= softpipe_texture(pt
);
193 pipe_buffer_reference(&spt
->buffer
, NULL
);
199 * Get a pipe_surface "view" into a texture.
201 static struct pipe_surface
*
202 softpipe_get_tex_surface(struct pipe_screen
*screen
,
203 struct pipe_texture
*pt
,
204 unsigned face
, unsigned level
, unsigned zslice
,
207 struct softpipe_texture
*spt
= softpipe_texture(pt
);
208 struct pipe_surface
*ps
;
210 assert(level
<= pt
->last_level
);
212 ps
= CALLOC_STRUCT(pipe_surface
);
214 pipe_reference_init(&ps
->reference
, 1);
215 pipe_texture_reference(&ps
->texture
, pt
);
216 ps
->format
= pt
->format
;
217 ps
->width
= u_minify(pt
->width0
, level
);
218 ps
->height
= u_minify(pt
->height0
, level
);
219 ps
->offset
= spt
->level_offset
[level
];
222 /* Because we are softpipe, anything that the state tracker
223 * thought was going to be done with the GPU will actually get
224 * done with the CPU. Let's adjust the flags to take that into
227 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
228 /* GPU_WRITE means "render" and that can involve reads (blending) */
229 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
232 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
233 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
235 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
236 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
237 /* Mark the surface as dirty. The tile cache will look for this. */
239 softpipe_screen(screen
)->timestamp
++;
246 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
247 ps
->offset
+= face
* pt
->nblocksy
[level
] * spt
->stride
[level
];
249 else if (pt
->target
== PIPE_TEXTURE_3D
) {
250 ps
->offset
+= zslice
* pt
->nblocksy
[level
] * spt
->stride
[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_texture_reference(&surf
->texture
, NULL
);
278 * Geta pipe_transfer object which is used for moving data in/out of
280 * \param face one of PIPE_TEX_FACE_x or 0
281 * \param level texture mipmap level
282 * \param zslice 2D slice of a 3D texture
283 * \param usage one of PIPE_TRANSFER_READ/WRITE/READ_WRITE
284 * \param x X position of region to read/write
285 * \param y Y position of region to read/write
286 * \param width width of region to read/write
287 * \param height height of region to read/write
289 static struct pipe_transfer
*
290 softpipe_get_tex_transfer(struct pipe_screen
*screen
,
291 struct pipe_texture
*texture
,
292 unsigned face
, unsigned level
, unsigned zslice
,
293 enum pipe_transfer_usage usage
,
294 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
296 struct softpipe_texture
*sptex
= softpipe_texture(texture
);
297 struct softpipe_transfer
*spt
;
300 assert(level
<= texture
->last_level
);
302 spt
= CALLOC_STRUCT(softpipe_transfer
);
304 struct pipe_transfer
*pt
= &spt
->base
;
305 pipe_texture_reference(&pt
->texture
, texture
);
306 pt
->format
= texture
->format
;
307 pt
->block
= texture
->block
;
312 pt
->nblocksx
= texture
->nblocksx
[level
];
313 pt
->nblocksy
= texture
->nblocksy
[level
];
314 pt
->stride
= sptex
->stride
[level
];
320 spt
->offset
= sptex
->level_offset
[level
];
322 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
323 spt
->offset
+= face
* pt
->nblocksy
* pt
->stride
;
325 else if (texture
->target
== PIPE_TEXTURE_3D
) {
326 spt
->offset
+= zslice
* pt
->nblocksy
* pt
->stride
;
339 * Free a pipe_transfer object which was created with
340 * softpipe_get_tex_transfer().
343 softpipe_tex_transfer_destroy(struct pipe_transfer
*transfer
)
345 /* Effectively do the texture_update work here - if texture images
346 * needed post-processing to put them into hardware layout, this is
347 * where it would happen. For softpipe, nothing to do.
349 assert (transfer
->texture
);
350 pipe_texture_reference(&transfer
->texture
, NULL
);
356 * Create memory mapping for given pipe_transfer object.
359 softpipe_transfer_map( struct pipe_screen
*screen
,
360 struct pipe_transfer
*transfer
)
362 ubyte
*map
, *xfer_map
;
363 struct softpipe_texture
*spt
;
365 assert(transfer
->texture
);
366 spt
= softpipe_texture(transfer
->texture
);
368 map
= pipe_buffer_map(screen
, spt
->buffer
, pipe_transfer_buffer_flags(transfer
));
372 /* May want to different things here depending on read/write nature
375 if (transfer
->texture
&& (transfer
->usage
& PIPE_TRANSFER_WRITE
)) {
376 /* Do something to notify sharing contexts of a texture change.
377 * In softpipe, that would mean flushing the texture cache.
379 softpipe_screen(screen
)->timestamp
++;
382 xfer_map
= map
+ softpipe_transfer(transfer
)->offset
+
383 transfer
->y
/ transfer
->block
.height
* transfer
->stride
+
384 transfer
->x
/ transfer
->block
.width
* transfer
->block
.size
;
385 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
391 * Unmap memory mapping for given pipe_transfer object.
394 softpipe_transfer_unmap(struct pipe_screen
*screen
,
395 struct pipe_transfer
*transfer
)
397 struct softpipe_texture
*spt
;
399 assert(transfer
->texture
);
400 spt
= softpipe_texture(transfer
->texture
);
402 pipe_buffer_unmap( screen
, spt
->buffer
);
404 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
405 /* Mark the texture as dirty to expire the tile caches. */
411 static struct pipe_video_surface
*
412 softpipe_video_surface_create(struct pipe_screen
*screen
,
413 enum pipe_video_chroma_format chroma_format
,
414 unsigned width
, unsigned height
)
416 struct softpipe_video_surface
*sp_vsfc
;
417 struct pipe_texture
template;
420 assert(width
&& height
);
422 sp_vsfc
= CALLOC_STRUCT(softpipe_video_surface
);
426 pipe_reference_init(&sp_vsfc
->base
.reference
, 1);
427 sp_vsfc
->base
.screen
= screen
;
428 sp_vsfc
->base
.chroma_format
= chroma_format
;
429 /*sp_vsfc->base.surface_format = PIPE_VIDEO_SURFACE_FORMAT_VUYA;*/
430 sp_vsfc
->base
.width
= width
;
431 sp_vsfc
->base
.height
= height
;
433 memset(&template, 0, sizeof(struct pipe_texture
));
434 template.target
= PIPE_TEXTURE_2D
;
435 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
436 template.last_level
= 0;
437 /* vl_mpeg12_mc_renderer expects this when it's initialized with pot_buffers=true */
438 template.width0
= util_next_power_of_two(width
);
439 template.height0
= util_next_power_of_two(height
);
441 pf_get_block(template.format
, &template.block
);
442 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
| PIPE_TEXTURE_USAGE_RENDER_TARGET
;
444 sp_vsfc
->tex
= screen
->texture_create(screen
, &template);
450 return &sp_vsfc
->base
;
455 softpipe_video_surface_destroy(struct pipe_video_surface
*vsfc
)
457 struct softpipe_video_surface
*sp_vsfc
= softpipe_video_surface(vsfc
);
459 pipe_texture_reference(&sp_vsfc
->tex
, NULL
);
465 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
467 screen
->texture_create
= softpipe_texture_create
;
468 screen
->texture_blanket
= softpipe_texture_blanket
;
469 screen
->texture_destroy
= softpipe_texture_destroy
;
471 screen
->get_tex_surface
= softpipe_get_tex_surface
;
472 screen
->tex_surface_destroy
= softpipe_tex_surface_destroy
;
474 screen
->get_tex_transfer
= softpipe_get_tex_transfer
;
475 screen
->tex_transfer_destroy
= softpipe_tex_transfer_destroy
;
476 screen
->transfer_map
= softpipe_transfer_map
;
477 screen
->transfer_unmap
= softpipe_transfer_unmap
;
479 screen
->video_surface_create
= softpipe_video_surface_create
;
480 screen
->video_surface_destroy
= softpipe_video_surface_destroy
;
485 * Return pipe_buffer handle and stride for given texture object.
489 softpipe_get_texture_buffer( struct pipe_texture
*texture
,
490 struct pipe_buffer
**buf
,
493 struct softpipe_texture
*tex
= (struct softpipe_texture
*) texture
;
498 pipe_buffer_reference(buf
, tex
->buffer
);
501 *stride
= tex
->stride
[0];