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"
44 #include "state_tracker/sw_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
;
60 unsigned buffer_size
= 0;
62 for (level
= 0; level
<= pt
->last_level
; level
++) {
63 spt
->stride
[level
] = util_format_get_stride(pt
->format
, width
);
65 spt
->level_offset
[level
] = buffer_size
;
67 buffer_size
+= (util_format_get_nblocksy(pt
->format
, height
) *
68 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
71 width
= u_minify(width
, 1);
72 height
= u_minify(height
, 1);
73 depth
= u_minify(depth
, 1);
76 spt
->data
= align_malloc(buffer_size
, 16);
78 return spt
->data
!= NULL
;
83 * Texture layout for simple color buffers.
86 softpipe_displaytarget_layout(struct pipe_screen
*screen
,
87 struct softpipe_texture
* spt
)
89 struct sw_winsys
*winsys
= softpipe_screen(screen
)->winsys
;
91 /* Round up the surface size to a multiple of the tile size?
93 spt
->dt
= winsys
->displaytarget_create(winsys
,
100 return spt
->dt
!= NULL
;
105 * Create new pipe_texture given the template information.
107 static struct pipe_texture
*
108 softpipe_texture_create(struct pipe_screen
*screen
,
109 const struct pipe_texture
*template)
111 struct softpipe_texture
*spt
= CALLOC_STRUCT(softpipe_texture
);
115 spt
->base
= *template;
116 pipe_reference_init(&spt
->base
.reference
, 1);
117 spt
->base
.screen
= screen
;
119 spt
->pot
= (util_is_power_of_two(template->width0
) &&
120 util_is_power_of_two(template->height0
) &&
121 util_is_power_of_two(template->depth0
));
123 if (spt
->base
.tex_usage
& (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
124 PIPE_TEXTURE_USAGE_SCANOUT
|
125 PIPE_TEXTURE_USAGE_SHARED
)) {
126 if (!softpipe_displaytarget_layout(screen
, spt
))
130 if (!softpipe_texture_layout(screen
, spt
))
145 softpipe_texture_destroy(struct pipe_texture
*pt
)
147 struct softpipe_screen
*screen
= softpipe_screen(pt
->screen
);
148 struct softpipe_texture
*spt
= softpipe_texture(pt
);
152 struct sw_winsys
*winsys
= screen
->winsys
;
153 winsys
->displaytarget_destroy(winsys
, spt
->dt
);
156 /* regular texture */
157 align_free(spt
->data
);
165 * Get a pipe_surface "view" into a texture.
167 static struct pipe_surface
*
168 softpipe_get_tex_surface(struct pipe_screen
*screen
,
169 struct pipe_texture
*pt
,
170 unsigned face
, unsigned level
, unsigned zslice
,
173 struct softpipe_texture
*spt
= softpipe_texture(pt
);
174 struct pipe_surface
*ps
;
176 assert(level
<= pt
->last_level
);
178 ps
= CALLOC_STRUCT(pipe_surface
);
180 pipe_reference_init(&ps
->reference
, 1);
181 pipe_texture_reference(&ps
->texture
, pt
);
182 ps
->format
= pt
->format
;
183 ps
->width
= u_minify(pt
->width0
, level
);
184 ps
->height
= u_minify(pt
->height0
, level
);
185 ps
->offset
= spt
->level_offset
[level
];
188 /* Because we are softpipe, anything that the state tracker
189 * thought was going to be done with the GPU will actually get
190 * done with the CPU. Let's adjust the flags to take that into
193 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
194 /* GPU_WRITE means "render" and that can involve reads (blending) */
195 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
198 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
199 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
201 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
202 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
203 /* Mark the surface as dirty. The tile cache will look for this. */
205 softpipe_screen(screen
)->timestamp
++;
212 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
213 ps
->offset
+= face
* util_format_get_nblocksy(pt
->format
, u_minify(pt
->height0
, level
)) *
216 else if (pt
->target
== PIPE_TEXTURE_3D
) {
217 ps
->offset
+= zslice
* util_format_get_nblocksy(pt
->format
, u_minify(pt
->height0
, level
)) *
230 * Free a pipe_surface which was created with softpipe_get_tex_surface().
233 softpipe_tex_surface_destroy(struct pipe_surface
*surf
)
235 /* Effectively do the texture_update work here - if texture images
236 * needed post-processing to put them into hardware layout, this is
237 * where it would happen. For softpipe, nothing to do.
239 assert(surf
->texture
);
240 pipe_texture_reference(&surf
->texture
, NULL
);
246 * Geta pipe_transfer object which is used for moving data in/out of
248 * \param face one of PIPE_TEX_FACE_x or 0
249 * \param level texture mipmap level
250 * \param zslice 2D slice of a 3D texture
251 * \param usage one of PIPE_TRANSFER_READ/WRITE/READ_WRITE
252 * \param x X position of region to read/write
253 * \param y Y position of region to read/write
254 * \param width width of region to read/write
255 * \param height height of region to read/write
257 static struct pipe_transfer
*
258 softpipe_get_tex_transfer(struct pipe_context
*pipe
,
259 struct pipe_texture
*texture
,
260 unsigned face
, unsigned level
, unsigned zslice
,
261 enum pipe_transfer_usage usage
,
262 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
264 struct softpipe_texture
*sptex
= softpipe_texture(texture
);
265 struct softpipe_transfer
*spt
;
268 assert(level
<= texture
->last_level
);
270 /* make sure the requested region is in the image bounds */
271 assert(x
+ w
<= u_minify(texture
->width0
, level
));
272 assert(y
+ h
<= u_minify(texture
->height0
, level
));
274 spt
= CALLOC_STRUCT(softpipe_transfer
);
276 struct pipe_transfer
*pt
= &spt
->base
;
277 int nblocksy
= util_format_get_nblocksy(texture
->format
, u_minify(texture
->height0
, level
));
278 pipe_texture_reference(&pt
->texture
, texture
);
283 pt
->stride
= sptex
->stride
[level
];
289 spt
->offset
= sptex
->level_offset
[level
];
291 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
292 spt
->offset
+= face
* nblocksy
* pt
->stride
;
294 else if (texture
->target
== PIPE_TEXTURE_3D
) {
295 spt
->offset
+= zslice
* nblocksy
* pt
->stride
;
308 * Free a pipe_transfer object which was created with
309 * softpipe_get_tex_transfer().
312 softpipe_tex_transfer_destroy(struct pipe_context
*pipe
,
313 struct pipe_transfer
*transfer
)
315 /* Effectively do the texture_update work here - if texture images
316 * needed post-processing to put them into hardware layout, this is
317 * where it would happen. For softpipe, nothing to do.
319 assert (transfer
->texture
);
320 pipe_texture_reference(&transfer
->texture
, NULL
);
326 * Create memory mapping for given pipe_transfer object.
329 softpipe_transfer_map( struct pipe_context
*pipe
,
330 struct pipe_transfer
*transfer
)
332 ubyte
*map
, *xfer_map
;
333 struct softpipe_texture
*spt
;
334 enum pipe_format format
;
336 assert(transfer
->texture
);
337 spt
= softpipe_texture(transfer
->texture
);
338 format
= transfer
->texture
->format
;
342 struct sw_winsys
*winsys
= softpipe_screen(pipe
->screen
)->winsys
;
344 map
= winsys
->displaytarget_map(winsys
, spt
->dt
,
345 pipe_transfer_buffer_flags(transfer
));
355 /* May want to different things here depending on read/write nature
358 if (transfer
->texture
&& (transfer
->usage
& PIPE_TRANSFER_WRITE
)) {
359 /* Do something to notify sharing contexts of a texture change.
360 * In softpipe, that would mean flushing the texture cache.
362 softpipe_screen(pipe
->screen
)->timestamp
++;
365 xfer_map
= map
+ softpipe_transfer(transfer
)->offset
+
366 transfer
->y
/ util_format_get_blockheight(format
) * transfer
->stride
+
367 transfer
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
368 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
374 * Unmap memory mapping for given pipe_transfer object.
377 softpipe_transfer_unmap(struct pipe_context
*pipe
,
378 struct pipe_transfer
*transfer
)
380 struct softpipe_texture
*spt
;
382 assert(transfer
->texture
);
383 spt
= softpipe_texture(transfer
->texture
);
387 struct sw_winsys
*winsys
= softpipe_screen(pipe
->screen
)->winsys
;
388 winsys
->displaytarget_unmap(winsys
, spt
->dt
);
391 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
392 /* Mark the texture as dirty to expire the tile caches. */
398 static struct pipe_video_surface
*
399 softpipe_video_surface_create(struct pipe_screen
*screen
,
400 enum pipe_video_chroma_format chroma_format
,
401 unsigned width
, unsigned height
)
403 struct softpipe_video_surface
*sp_vsfc
;
404 struct pipe_texture
template;
407 assert(width
&& height
);
409 sp_vsfc
= CALLOC_STRUCT(softpipe_video_surface
);
413 pipe_reference_init(&sp_vsfc
->base
.reference
, 1);
414 sp_vsfc
->base
.screen
= screen
;
415 sp_vsfc
->base
.chroma_format
= chroma_format
;
416 /*sp_vsfc->base.surface_format = PIPE_VIDEO_SURFACE_FORMAT_VUYA;*/
417 sp_vsfc
->base
.width
= width
;
418 sp_vsfc
->base
.height
= height
;
420 memset(&template, 0, sizeof(struct pipe_texture
));
421 template.target
= PIPE_TEXTURE_2D
;
422 template.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
423 template.last_level
= 0;
424 /* vl_mpeg12_mc_renderer expects this when it's initialized with pot_buffers=true */
425 template.width0
= util_next_power_of_two(width
);
426 template.height0
= util_next_power_of_two(height
);
428 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
| PIPE_TEXTURE_USAGE_RENDER_TARGET
;
430 sp_vsfc
->tex
= screen
->texture_create(screen
, &template);
436 return &sp_vsfc
->base
;
441 softpipe_video_surface_destroy(struct pipe_video_surface
*vsfc
)
443 struct softpipe_video_surface
*sp_vsfc
= softpipe_video_surface(vsfc
);
445 pipe_texture_reference(&sp_vsfc
->tex
, NULL
);
451 softpipe_init_texture_funcs(struct pipe_context
*pipe
)
453 pipe
->get_tex_transfer
= softpipe_get_tex_transfer
;
454 pipe
->tex_transfer_destroy
= softpipe_tex_transfer_destroy
;
455 pipe
->transfer_map
= softpipe_transfer_map
;
456 pipe
->transfer_unmap
= softpipe_transfer_unmap
;
460 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
462 screen
->texture_create
= softpipe_texture_create
;
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
->video_surface_create
= softpipe_video_surface_create
;
469 screen
->video_surface_destroy
= softpipe_video_surface_destroy
;