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
->width
[0];
56 unsigned height
= pt
->height
[0];
57 unsigned depth
= pt
->depth
[0];
59 unsigned buffer_size
= 0;
61 for (level
= 0; level
<= pt
->last_level
; level
++) {
62 pt
->width
[level
] = width
;
63 pt
->height
[level
] = height
;
64 pt
->depth
[level
] = depth
;
65 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, width
);
66 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, height
);
67 spt
->stride
[level
] = pt
->nblocksx
[level
]*pt
->block
.size
;
69 spt
->level_offset
[level
] = buffer_size
;
71 buffer_size
+= (pt
->nblocksy
[level
] *
72 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
75 width
= minify(width
);
76 height
= minify(height
);
77 depth
= minify(depth
);
80 spt
->buffer
= screen
->buffer_create(screen
, 32,
81 PIPE_BUFFER_USAGE_PIXEL
,
84 return spt
->buffer
!= NULL
;
89 * Texture layout for simple color buffers.
92 softpipe_displaytarget_layout(struct pipe_screen
*screen
,
93 struct softpipe_texture
* spt
)
95 unsigned usage
= (PIPE_BUFFER_USAGE_CPU_READ_WRITE
|
96 PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
97 unsigned tex_usage
= spt
->base
.tex_usage
;
99 spt
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width
[0]);
100 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height
[0]);
102 spt
->buffer
= screen
->surface_buffer_create( screen
,
110 return spt
->buffer
!= NULL
;
115 * Create new pipe_texture given the template information.
117 static struct pipe_texture
*
118 softpipe_texture_create(struct pipe_screen
*screen
,
119 const struct pipe_texture
*template)
121 struct softpipe_texture
*spt
= CALLOC_STRUCT(softpipe_texture
);
125 spt
->base
= *template;
126 pipe_reference_init(&spt
->base
.reference
, 1);
127 spt
->base
.screen
= screen
;
129 spt
->pot
= (util_is_power_of_two(template->width
[0]) &&
130 util_is_power_of_two(template->height
[0]) &&
131 util_is_power_of_two(template->depth
[0]));
133 if (spt
->base
.tex_usage
& (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
134 PIPE_TEXTURE_USAGE_PRIMARY
)) {
135 if (!softpipe_displaytarget_layout(screen
, spt
))
139 if (!softpipe_texture_layout(screen
, spt
))
152 * Create a new pipe_texture which wraps an existing buffer.
154 static struct pipe_texture
*
155 softpipe_texture_blanket(struct pipe_screen
* screen
,
156 const struct pipe_texture
*base
,
157 const unsigned *stride
,
158 struct pipe_buffer
*buffer
)
160 struct softpipe_texture
*spt
;
163 /* Only supports one type */
164 if (base
->target
!= PIPE_TEXTURE_2D
||
165 base
->last_level
!= 0 ||
166 base
->depth
[0] != 1) {
170 spt
= CALLOC_STRUCT(softpipe_texture
);
175 pipe_reference_init(&spt
->base
.reference
, 1);
176 spt
->base
.screen
= screen
;
177 spt
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width
[0]);
178 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height
[0]);
179 spt
->stride
[0] = stride
[0];
181 pipe_buffer_reference(&spt
->buffer
, buffer
);
188 softpipe_texture_destroy(struct pipe_texture
*pt
)
190 struct softpipe_texture
*spt
= softpipe_texture(pt
);
192 pipe_buffer_reference(&spt
->buffer
, NULL
);
198 * Get a pipe_surface "view" into a texture.
200 static struct pipe_surface
*
201 softpipe_get_tex_surface(struct pipe_screen
*screen
,
202 struct pipe_texture
*pt
,
203 unsigned face
, unsigned level
, unsigned zslice
,
206 struct softpipe_texture
*spt
= softpipe_texture(pt
);
207 struct pipe_surface
*ps
;
209 assert(level
<= pt
->last_level
);
211 ps
= CALLOC_STRUCT(pipe_surface
);
213 pipe_reference_init(&ps
->reference
, 1);
214 pipe_texture_reference(&ps
->texture
, pt
);
215 ps
->format
= pt
->format
;
216 ps
->width
= pt
->width
[level
];
217 ps
->height
= pt
->height
[level
];
218 ps
->offset
= spt
->level_offset
[level
];
221 /* Because we are softpipe, anything that the state tracker
222 * thought was going to be done with the GPU will actually get
223 * done with the CPU. Let's adjust the flags to take that into
226 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
227 /* GPU_WRITE means "render" and that can involve reads (blending) */
228 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
231 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
232 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
234 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
235 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
236 /* Mark the surface as dirty. The tile cache will look for this. */
238 softpipe_screen(screen
)->timestamp
++;
245 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
246 ps
->offset
+= face
* pt
->nblocksy
[level
] * spt
->stride
[level
];
248 else if (pt
->target
== PIPE_TEXTURE_3D
) {
249 ps
->offset
+= zslice
* pt
->nblocksy
[level
] * spt
->stride
[level
];
261 * Free a pipe_surface which was created with softpipe_get_tex_surface().
264 softpipe_tex_surface_destroy(struct pipe_surface
*surf
)
266 /* Effectively do the texture_update work here - if texture images
267 * needed post-processing to put them into hardware layout, this is
268 * where it would happen. For softpipe, nothing to do.
270 assert(surf
->texture
);
271 pipe_texture_reference(&surf
->texture
, NULL
);
277 * Geta pipe_transfer object which is used for moving data in/out of
279 * \param face one of PIPE_TEX_FACE_x or 0
280 * \param level texture mipmap level
281 * \param zslice 2D slice of a 3D texture
282 * \param usage one of PIPE_TRANSFER_READ/WRITE/READ_WRITE
283 * \param x X position of region to read/write
284 * \param y Y position of region to read/write
285 * \param width width of region to read/write
286 * \param height height of region to read/write
288 static struct pipe_transfer
*
289 softpipe_get_tex_transfer(struct pipe_screen
*screen
,
290 struct pipe_texture
*texture
,
291 unsigned face
, unsigned level
, unsigned zslice
,
292 enum pipe_transfer_usage usage
,
293 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
295 struct softpipe_texture
*sptex
= softpipe_texture(texture
);
296 struct softpipe_transfer
*spt
;
299 assert(level
<= texture
->last_level
);
301 spt
= CALLOC_STRUCT(softpipe_transfer
);
303 struct pipe_transfer
*pt
= &spt
->base
;
304 pipe_texture_reference(&pt
->texture
, texture
);
305 pt
->format
= texture
->format
;
306 pt
->block
= texture
->block
;
311 pt
->nblocksx
= texture
->nblocksx
[level
];
312 pt
->nblocksy
= texture
->nblocksy
[level
];
313 pt
->stride
= sptex
->stride
[level
];
319 spt
->offset
= sptex
->level_offset
[level
];
321 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
322 spt
->offset
+= face
* pt
->nblocksy
* pt
->stride
;
324 else if (texture
->target
== PIPE_TEXTURE_3D
) {
325 spt
->offset
+= zslice
* pt
->nblocksy
* pt
->stride
;
338 * Free a pipe_transfer object which was created with
339 * softpipe_get_tex_transfer().
342 softpipe_tex_transfer_destroy(struct pipe_transfer
*transfer
)
344 /* Effectively do the texture_update work here - if texture images
345 * needed post-processing to put them into hardware layout, this is
346 * where it would happen. For softpipe, nothing to do.
348 assert (transfer
->texture
);
349 pipe_texture_reference(&transfer
->texture
, NULL
);
355 * Create memory mapping for given pipe_transfer object.
358 softpipe_transfer_map( struct pipe_screen
*screen
,
359 struct pipe_transfer
*transfer
)
361 ubyte
*map
, *xfer_map
;
362 struct softpipe_texture
*spt
;
364 assert(transfer
->texture
);
365 spt
= softpipe_texture(transfer
->texture
);
367 map
= pipe_buffer_map(screen
, spt
->buffer
, pipe_transfer_buffer_flags(transfer
));
371 /* May want to different things here depending on read/write nature
374 if (transfer
->texture
&& (transfer
->usage
& PIPE_TRANSFER_WRITE
)) {
375 /* Do something to notify sharing contexts of a texture change.
376 * In softpipe, that would mean flushing the texture cache.
378 softpipe_screen(screen
)->timestamp
++;
381 xfer_map
= map
+ softpipe_transfer(transfer
)->offset
+
382 transfer
->y
/ transfer
->block
.height
* transfer
->stride
+
383 transfer
->x
/ transfer
->block
.width
* transfer
->block
.size
;
384 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
390 * Unmap memory mapping for given pipe_transfer object.
393 softpipe_transfer_unmap(struct pipe_screen
*screen
,
394 struct pipe_transfer
*transfer
)
396 struct softpipe_texture
*spt
;
398 assert(transfer
->texture
);
399 spt
= softpipe_texture(transfer
->texture
);
401 pipe_buffer_unmap( screen
, spt
->buffer
);
403 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
404 /* Mark the texture as dirty to expire the tile caches. */
410 static struct pipe_video_surface
*
411 softpipe_video_surface_create(struct pipe_screen
*screen
,
412 enum pipe_video_chroma_format chroma_format
,
413 unsigned width
, unsigned height
)
415 struct softpipe_video_surface
*sp_vsfc
;
416 struct pipe_texture
template;
419 assert(width
&& height
);
421 sp_vsfc
= CALLOC_STRUCT(softpipe_video_surface
);
425 pipe_reference_init(&sp_vsfc
->base
.reference
, 1);
426 sp_vsfc
->base
.screen
= screen
;
427 sp_vsfc
->base
.chroma_format
= chroma_format
;
428 /*sp_vsfc->base.surface_format = PIPE_VIDEO_SURFACE_FORMAT_VUYA;*/
429 sp_vsfc
->base
.width
= width
;
430 sp_vsfc
->base
.height
= height
;
432 memset(&template, 0, sizeof(struct pipe_texture
));
433 template.target
= PIPE_TEXTURE_2D
;
434 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
435 template.last_level
= 0;
436 /* vl_mpeg12_mc_renderer expects this when it's initialized with pot_buffers=true */
437 template.width
[0] = util_next_power_of_two(width
);
438 template.height
[0] = util_next_power_of_two(height
);
439 template.depth
[0] = 1;
440 pf_get_block(template.format
, &template.block
);
441 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
| PIPE_TEXTURE_USAGE_RENDER_TARGET
;
443 sp_vsfc
->tex
= screen
->texture_create(screen
, &template);
449 return &sp_vsfc
->base
;
454 softpipe_video_surface_destroy(struct pipe_video_surface
*vsfc
)
456 struct softpipe_video_surface
*sp_vsfc
= softpipe_video_surface(vsfc
);
458 pipe_texture_reference(&sp_vsfc
->tex
, NULL
);
464 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
466 screen
->texture_create
= softpipe_texture_create
;
467 screen
->texture_blanket
= softpipe_texture_blanket
;
468 screen
->texture_destroy
= softpipe_texture_destroy
;
470 screen
->get_tex_surface
= softpipe_get_tex_surface
;
471 screen
->tex_surface_destroy
= softpipe_tex_surface_destroy
;
473 screen
->get_tex_transfer
= softpipe_get_tex_transfer
;
474 screen
->tex_transfer_destroy
= softpipe_tex_transfer_destroy
;
475 screen
->transfer_map
= softpipe_transfer_map
;
476 screen
->transfer_unmap
= softpipe_transfer_unmap
;
478 screen
->video_surface_create
= softpipe_video_surface_create
;
479 screen
->video_surface_destroy
= softpipe_video_surface_destroy
;
484 * Return pipe_buffer handle and stride for given texture object.
488 softpipe_get_texture_buffer( struct pipe_texture
*texture
,
489 struct pipe_buffer
**buf
,
492 struct softpipe_texture
*tex
= (struct softpipe_texture
*) texture
;
497 pipe_buffer_reference(buf
, tex
->buffer
);
500 *stride
= tex
->stride
[0];