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
;
114 static struct pipe_texture
*
115 softpipe_texture_create(struct pipe_screen
*screen
,
116 const struct pipe_texture
*template)
118 struct softpipe_texture
*spt
= CALLOC_STRUCT(softpipe_texture
);
122 spt
->base
= *template;
123 pipe_reference_init(&spt
->base
.reference
, 1);
124 spt
->base
.screen
= screen
;
126 spt
->pot
= (util_is_power_of_two(template->width
[0]) &&
127 util_is_power_of_two(template->height
[0]) &&
128 util_is_power_of_two(template->depth
[0]));
130 if (spt
->base
.tex_usage
& (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
131 PIPE_TEXTURE_USAGE_PRIMARY
)) {
132 if (!softpipe_displaytarget_layout(screen
, spt
))
136 if (!softpipe_texture_layout(screen
, spt
))
148 static struct pipe_texture
*
149 softpipe_texture_blanket(struct pipe_screen
* screen
,
150 const struct pipe_texture
*base
,
151 const unsigned *stride
,
152 struct pipe_buffer
*buffer
)
154 struct softpipe_texture
*spt
;
157 /* Only supports one type */
158 if (base
->target
!= PIPE_TEXTURE_2D
||
159 base
->last_level
!= 0 ||
160 base
->depth
[0] != 1) {
164 spt
= CALLOC_STRUCT(softpipe_texture
);
169 pipe_reference_init(&spt
->base
.reference
, 1);
170 spt
->base
.screen
= screen
;
171 spt
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width
[0]);
172 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height
[0]);
173 spt
->stride
[0] = stride
[0];
175 pipe_buffer_reference(&spt
->buffer
, buffer
);
182 softpipe_texture_destroy(struct pipe_texture
*pt
)
184 struct softpipe_texture
*spt
= softpipe_texture(pt
);
186 pipe_buffer_reference(&spt
->buffer
, NULL
);
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
= pt
->width
[level
];
208 ps
->height
= pt
->height
[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
* pt
->nblocksy
[level
] * spt
->stride
[level
];
239 else if (pt
->target
== PIPE_TEXTURE_3D
) {
240 ps
->offset
+= zslice
* pt
->nblocksy
[level
] * spt
->stride
[level
];
252 softpipe_tex_surface_destroy(struct pipe_surface
*surf
)
254 /* Effectively do the texture_update work here - if texture images
255 * needed post-processing to put them into hardware layout, this is
256 * where it would happen. For softpipe, nothing to do.
258 assert(surf
->texture
);
259 pipe_texture_reference(&surf
->texture
, NULL
);
264 static struct pipe_transfer
*
265 softpipe_get_tex_transfer(struct pipe_screen
*screen
,
266 struct pipe_texture
*texture
,
267 unsigned face
, unsigned level
, unsigned zslice
,
268 enum pipe_transfer_usage usage
,
269 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
271 struct softpipe_texture
*sptex
= softpipe_texture(texture
);
272 struct softpipe_transfer
*spt
;
275 assert(level
<= texture
->last_level
);
277 spt
= CALLOC_STRUCT(softpipe_transfer
);
279 struct pipe_transfer
*pt
= &spt
->base
;
280 pipe_texture_reference(&pt
->texture
, texture
);
281 pt
->format
= texture
->format
;
282 pt
->block
= texture
->block
;
287 pt
->nblocksx
= texture
->nblocksx
[level
];
288 pt
->nblocksy
= texture
->nblocksy
[level
];
289 pt
->stride
= sptex
->stride
[level
];
295 spt
->offset
= sptex
->level_offset
[level
];
297 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
298 spt
->offset
+= face
* pt
->nblocksy
* pt
->stride
;
300 else if (texture
->target
== PIPE_TEXTURE_3D
) {
301 spt
->offset
+= zslice
* pt
->nblocksy
* pt
->stride
;
314 softpipe_tex_transfer_destroy(struct pipe_transfer
*transfer
)
316 /* Effectively do the texture_update work here - if texture images
317 * needed post-processing to put them into hardware layout, this is
318 * where it would happen. For softpipe, nothing to do.
320 assert (transfer
->texture
);
321 pipe_texture_reference(&transfer
->texture
, NULL
);
327 softpipe_transfer_map( struct pipe_screen
*screen
,
328 struct pipe_transfer
*transfer
)
330 ubyte
*map
, *xfer_map
;
331 struct softpipe_texture
*spt
;
333 assert(transfer
->texture
);
334 spt
= softpipe_texture(transfer
->texture
);
336 map
= pipe_buffer_map(screen
, spt
->buffer
, pipe_transfer_buffer_flags(transfer
));
340 /* May want to different things here depending on read/write nature
343 if (transfer
->texture
&& (transfer
->usage
& PIPE_TRANSFER_WRITE
)) {
344 /* Do something to notify sharing contexts of a texture change.
345 * In softpipe, that would mean flushing the texture cache.
347 softpipe_screen(screen
)->timestamp
++;
350 xfer_map
= map
+ softpipe_transfer(transfer
)->offset
+
351 transfer
->y
/ transfer
->block
.height
* transfer
->stride
+
352 transfer
->x
/ transfer
->block
.width
* transfer
->block
.size
;
353 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
359 softpipe_transfer_unmap(struct pipe_screen
*screen
,
360 struct pipe_transfer
*transfer
)
362 struct softpipe_texture
*spt
;
364 assert(transfer
->texture
);
365 spt
= softpipe_texture(transfer
->texture
);
367 pipe_buffer_unmap( screen
, spt
->buffer
);
369 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
370 /* Mark the texture as dirty to expire the tile caches. */
375 static struct pipe_video_surface
*
376 softpipe_video_surface_create(struct pipe_screen
*screen
,
377 enum pipe_video_chroma_format chroma_format
,
378 unsigned width
, unsigned height
)
380 struct softpipe_video_surface
*sp_vsfc
;
381 struct pipe_texture
template;
384 assert(width
&& height
);
386 sp_vsfc
= CALLOC_STRUCT(softpipe_video_surface
);
390 pipe_reference_init(&sp_vsfc
->base
.reference
, 1);
391 sp_vsfc
->base
.screen
= screen
;
392 sp_vsfc
->base
.chroma_format
= chroma_format
;
393 /*sp_vsfc->base.surface_format = PIPE_VIDEO_SURFACE_FORMAT_VUYA;*/
394 sp_vsfc
->base
.width
= width
;
395 sp_vsfc
->base
.height
= height
;
397 memset(&template, 0, sizeof(struct pipe_texture
));
398 template.target
= PIPE_TEXTURE_2D
;
399 template.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
400 template.last_level
= 0;
401 /* vl_mpeg12_mc_renderer expects this when it's initialized with pot_buffers=true */
402 template.width
[0] = util_next_power_of_two(width
);
403 template.height
[0] = util_next_power_of_two(height
);
404 template.depth
[0] = 1;
405 pf_get_block(template.format
, &template.block
);
406 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
| PIPE_TEXTURE_USAGE_RENDER_TARGET
;
408 sp_vsfc
->tex
= screen
->texture_create(screen
, &template);
414 return &sp_vsfc
->base
;
419 softpipe_video_surface_destroy(struct pipe_video_surface
*vsfc
)
421 struct softpipe_video_surface
*sp_vsfc
= softpipe_video_surface(vsfc
);
423 pipe_texture_reference(&sp_vsfc
->tex
, NULL
);
429 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
431 screen
->texture_create
= softpipe_texture_create
;
432 screen
->texture_blanket
= softpipe_texture_blanket
;
433 screen
->texture_destroy
= softpipe_texture_destroy
;
435 screen
->get_tex_surface
= softpipe_get_tex_surface
;
436 screen
->tex_surface_destroy
= softpipe_tex_surface_destroy
;
438 screen
->get_tex_transfer
= softpipe_get_tex_transfer
;
439 screen
->tex_transfer_destroy
= softpipe_tex_transfer_destroy
;
440 screen
->transfer_map
= softpipe_transfer_map
;
441 screen
->transfer_unmap
= softpipe_transfer_unmap
;
443 screen
->video_surface_create
= softpipe_video_surface_create
;
444 screen
->video_surface_destroy
= softpipe_video_surface_destroy
;
449 softpipe_get_texture_buffer( struct pipe_texture
*texture
,
450 struct pipe_buffer
**buf
,
453 struct softpipe_texture
*tex
= (struct softpipe_texture
*) texture
;
458 pipe_buffer_reference(buf
, tex
->buffer
);
461 *stride
= tex
->stride
[0];