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"
40 #include "sp_context.h"
41 #include "sp_texture.h"
42 #include "sp_screen.h"
43 #include "sp_winsys.h"
47 * Conventional allocation path for non-display textures:
48 * Use a simple, maximally packed layout.
51 softpipe_texture_layout(struct pipe_screen
*screen
,
52 struct softpipe_texture
* spt
)
54 struct pipe_texture
*pt
= &spt
->base
;
56 unsigned width
= pt
->width0
;
57 unsigned height
= pt
->height0
;
58 unsigned depth
= pt
->depth0
;
59 unsigned buffer_size
= 0;
61 for (level
= 0; level
<= pt
->last_level
; level
++) {
62 spt
->stride
[level
] = util_format_get_stride(pt
->format
, width
);
64 spt
->level_offset
[level
] = buffer_size
;
66 buffer_size
+= (util_format_get_nblocksy(pt
->format
, height
) *
67 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
70 width
= u_minify(width
, 1);
71 height
= u_minify(height
, 1);
72 depth
= u_minify(depth
, 1);
75 spt
->buffer
= screen
->buffer_create(screen
, 32,
76 PIPE_BUFFER_USAGE_PIXEL
,
79 return spt
->buffer
!= NULL
;
84 * Texture layout for simple color buffers.
87 softpipe_displaytarget_layout(struct pipe_screen
*screen
,
88 struct softpipe_texture
* spt
)
90 unsigned usage
= (PIPE_BUFFER_USAGE_CPU_READ_WRITE
|
91 PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
92 unsigned tex_usage
= spt
->base
.tex_usage
;
94 spt
->buffer
= screen
->surface_buffer_create( screen
,
102 return spt
->buffer
!= NULL
;
107 * Create new pipe_texture given the template information.
109 static struct pipe_texture
*
110 softpipe_texture_create(struct pipe_screen
*screen
,
111 const struct pipe_texture
*template)
113 struct softpipe_texture
*spt
= CALLOC_STRUCT(softpipe_texture
);
117 spt
->base
= *template;
118 pipe_reference_init(&spt
->base
.reference
, 1);
119 spt
->base
.screen
= screen
;
121 spt
->pot
= (util_is_power_of_two(template->width0
) &&
122 util_is_power_of_two(template->height0
) &&
123 util_is_power_of_two(template->depth0
));
125 if (spt
->base
.tex_usage
& (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
126 PIPE_TEXTURE_USAGE_SCANOUT
|
127 PIPE_TEXTURE_USAGE_SHARED
)) {
128 if (!softpipe_displaytarget_layout(screen
, spt
))
132 if (!softpipe_texture_layout(screen
, spt
))
145 * Create a new pipe_texture which wraps an existing buffer.
147 static struct pipe_texture
*
148 softpipe_texture_blanket(struct pipe_screen
* screen
,
149 const struct pipe_texture
*base
,
150 const unsigned *stride
,
151 struct pipe_buffer
*buffer
)
153 struct softpipe_texture
*spt
;
156 /* Only supports one type */
157 if (base
->target
!= PIPE_TEXTURE_2D
||
158 base
->last_level
!= 0 ||
163 spt
= CALLOC_STRUCT(softpipe_texture
);
168 pipe_reference_init(&spt
->base
.reference
, 1);
169 spt
->base
.screen
= screen
;
170 spt
->stride
[0] = stride
[0];
172 pipe_buffer_reference(&spt
->buffer
, buffer
);
179 softpipe_texture_destroy(struct pipe_texture
*pt
)
181 struct softpipe_texture
*spt
= softpipe_texture(pt
);
183 pipe_buffer_reference(&spt
->buffer
, NULL
);
189 * Get a pipe_surface "view" into a texture.
191 static struct pipe_surface
*
192 softpipe_get_tex_surface(struct pipe_screen
*screen
,
193 struct pipe_texture
*pt
,
194 unsigned face
, unsigned level
, unsigned zslice
,
197 struct softpipe_texture
*spt
= softpipe_texture(pt
);
198 struct pipe_surface
*ps
;
200 assert(level
<= pt
->last_level
);
202 ps
= CALLOC_STRUCT(pipe_surface
);
204 pipe_reference_init(&ps
->reference
, 1);
205 pipe_texture_reference(&ps
->texture
, pt
);
206 ps
->format
= pt
->format
;
207 ps
->width
= u_minify(pt
->width0
, level
);
208 ps
->height
= u_minify(pt
->height0
, level
);
209 ps
->offset
= spt
->level_offset
[level
];
212 /* Because we are softpipe, anything that the state tracker
213 * thought was going to be done with the GPU will actually get
214 * done with the CPU. Let's adjust the flags to take that into
217 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
218 /* GPU_WRITE means "render" and that can involve reads (blending) */
219 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
222 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
223 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
225 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
226 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
227 /* Mark the surface as dirty. The tile cache will look for this. */
229 softpipe_screen(screen
)->timestamp
++;
236 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
237 ps
->offset
+= face
* util_format_get_nblocksy(pt
->format
, u_minify(pt
->height0
, level
)) *
240 else if (pt
->target
== PIPE_TEXTURE_3D
) {
241 ps
->offset
+= zslice
* util_format_get_nblocksy(pt
->format
, u_minify(pt
->height0
, level
)) *
254 * Free a pipe_surface which was created with softpipe_get_tex_surface().
257 softpipe_tex_surface_destroy(struct pipe_surface
*surf
)
259 /* Effectively do the texture_update work here - if texture images
260 * needed post-processing to put them into hardware layout, this is
261 * where it would happen. For softpipe, nothing to do.
263 assert(surf
->texture
);
264 pipe_texture_reference(&surf
->texture
, NULL
);
270 * Geta pipe_transfer object which is used for moving data in/out of
272 * \param face one of PIPE_TEX_FACE_x or 0
273 * \param level texture mipmap level
274 * \param zslice 2D slice of a 3D texture
275 * \param usage one of PIPE_TRANSFER_READ/WRITE/READ_WRITE
276 * \param x X position of region to read/write
277 * \param y Y position of region to read/write
278 * \param width width of region to read/write
279 * \param height height of region to read/write
281 static struct pipe_transfer
*
282 softpipe_get_tex_transfer(struct pipe_screen
*screen
,
283 struct pipe_texture
*texture
,
284 unsigned face
, unsigned level
, unsigned zslice
,
285 enum pipe_transfer_usage usage
,
286 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
288 struct softpipe_texture
*sptex
= softpipe_texture(texture
);
289 struct softpipe_transfer
*spt
;
292 assert(level
<= texture
->last_level
);
294 /* make sure the requested region is in the image bounds */
295 assert(x
+ w
<= u_minify(texture
->width0
, level
));
296 assert(y
+ h
<= u_minify(texture
->height0
, level
));
298 spt
= CALLOC_STRUCT(softpipe_transfer
);
300 struct pipe_transfer
*pt
= &spt
->base
;
301 int nblocksy
= util_format_get_nblocksy(texture
->format
, u_minify(texture
->height0
, level
));
302 pipe_texture_reference(&pt
->texture
, texture
);
307 pt
->stride
= sptex
->stride
[level
];
313 spt
->offset
= sptex
->level_offset
[level
];
315 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
316 spt
->offset
+= face
* nblocksy
* pt
->stride
;
318 else if (texture
->target
== PIPE_TEXTURE_3D
) {
319 spt
->offset
+= zslice
* nblocksy
* pt
->stride
;
332 * Free a pipe_transfer object which was created with
333 * softpipe_get_tex_transfer().
336 softpipe_tex_transfer_destroy(struct pipe_transfer
*transfer
)
338 /* Effectively do the texture_update work here - if texture images
339 * needed post-processing to put them into hardware layout, this is
340 * where it would happen. For softpipe, nothing to do.
342 assert (transfer
->texture
);
343 pipe_texture_reference(&transfer
->texture
, NULL
);
349 * Create memory mapping for given pipe_transfer object.
352 softpipe_transfer_map( struct pipe_screen
*screen
,
353 struct pipe_transfer
*transfer
)
355 ubyte
*map
, *xfer_map
;
356 struct softpipe_texture
*spt
;
357 enum pipe_format format
;
359 assert(transfer
->texture
);
360 spt
= softpipe_texture(transfer
->texture
);
361 format
= transfer
->texture
->format
;
363 map
= pipe_buffer_map(screen
, spt
->buffer
, pipe_transfer_buffer_flags(transfer
));
367 /* May want to different things here depending on read/write nature
370 if (transfer
->texture
&& (transfer
->usage
& PIPE_TRANSFER_WRITE
)) {
371 /* Do something to notify sharing contexts of a texture change.
372 * In softpipe, that would mean flushing the texture cache.
374 softpipe_screen(screen
)->timestamp
++;
377 xfer_map
= map
+ softpipe_transfer(transfer
)->offset
+
378 transfer
->y
/ util_format_get_blockheight(format
) * transfer
->stride
+
379 transfer
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
380 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
386 * Unmap memory mapping for given pipe_transfer object.
389 softpipe_transfer_unmap(struct pipe_screen
*screen
,
390 struct pipe_transfer
*transfer
)
392 struct softpipe_texture
*spt
;
394 assert(transfer
->texture
);
395 spt
= softpipe_texture(transfer
->texture
);
397 pipe_buffer_unmap( screen
, spt
->buffer
);
399 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
400 /* Mark the texture as dirty to expire the tile caches. */
406 static struct pipe_video_surface
*
407 softpipe_video_surface_create(struct pipe_screen
*screen
,
408 enum pipe_video_chroma_format chroma_format
,
409 unsigned width
, unsigned height
)
411 struct softpipe_video_surface
*sp_vsfc
;
412 struct pipe_texture
template;
415 assert(width
&& height
);
417 sp_vsfc
= CALLOC_STRUCT(softpipe_video_surface
);
421 pipe_reference_init(&sp_vsfc
->base
.reference
, 1);
422 sp_vsfc
->base
.screen
= screen
;
423 sp_vsfc
->base
.chroma_format
= chroma_format
;
424 /*sp_vsfc->base.surface_format = PIPE_VIDEO_SURFACE_FORMAT_VUYA;*/
425 sp_vsfc
->base
.width
= width
;
426 sp_vsfc
->base
.height
= height
;
428 memset(&template, 0, sizeof(struct pipe_texture
));
429 template.target
= PIPE_TEXTURE_2D
;
430 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
431 template.last_level
= 0;
432 /* vl_mpeg12_mc_renderer expects this when it's initialized with pot_buffers=true */
433 template.width0
= util_next_power_of_two(width
);
434 template.height0
= util_next_power_of_two(height
);
436 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
| PIPE_TEXTURE_USAGE_RENDER_TARGET
;
438 sp_vsfc
->tex
= screen
->texture_create(screen
, &template);
444 return &sp_vsfc
->base
;
449 softpipe_video_surface_destroy(struct pipe_video_surface
*vsfc
)
451 struct softpipe_video_surface
*sp_vsfc
= softpipe_video_surface(vsfc
);
453 pipe_texture_reference(&sp_vsfc
->tex
, NULL
);
459 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
461 screen
->texture_create
= softpipe_texture_create
;
462 screen
->texture_blanket
= softpipe_texture_blanket
;
463 screen
->texture_destroy
= softpipe_texture_destroy
;
465 screen
->get_tex_surface
= softpipe_get_tex_surface
;
466 screen
->tex_surface_destroy
= softpipe_tex_surface_destroy
;
468 screen
->get_tex_transfer
= softpipe_get_tex_transfer
;
469 screen
->tex_transfer_destroy
= softpipe_tex_transfer_destroy
;
470 screen
->transfer_map
= softpipe_transfer_map
;
471 screen
->transfer_unmap
= softpipe_transfer_unmap
;
473 screen
->video_surface_create
= softpipe_video_surface_create
;
474 screen
->video_surface_destroy
= softpipe_video_surface_destroy
;