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
*templat
)
118 struct softpipe_texture
*spt
= CALLOC_STRUCT(softpipe_texture
);
122 spt
->base
= *templat
;
123 pipe_reference_init(&spt
->base
.reference
, 1);
124 spt
->base
.screen
= screen
;
126 if (spt
->base
.tex_usage
& (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
127 PIPE_TEXTURE_USAGE_PRIMARY
)) {
128 if (!softpipe_displaytarget_layout(screen
, spt
))
132 if (!softpipe_texture_layout(screen
, spt
))
144 static struct pipe_texture
*
145 softpipe_texture_blanket(struct pipe_screen
* screen
,
146 const struct pipe_texture
*base
,
147 const unsigned *stride
,
148 struct pipe_buffer
*buffer
)
150 struct softpipe_texture
*spt
;
153 /* Only supports one type */
154 if (base
->target
!= PIPE_TEXTURE_2D
||
155 base
->last_level
!= 0 ||
156 base
->depth
[0] != 1) {
160 spt
= CALLOC_STRUCT(softpipe_texture
);
165 pipe_reference_init(&spt
->base
.reference
, 1);
166 spt
->base
.screen
= screen
;
167 spt
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width
[0]);
168 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height
[0]);
169 spt
->stride
[0] = stride
[0];
171 pipe_buffer_reference(&spt
->buffer
, buffer
);
178 softpipe_texture_destroy(struct pipe_texture
*pt
)
180 struct softpipe_texture
*spt
= softpipe_texture(pt
);
182 pipe_buffer_reference(&spt
->buffer
, NULL
);
187 static struct pipe_surface
*
188 softpipe_get_tex_surface(struct pipe_screen
*screen
,
189 struct pipe_texture
*pt
,
190 unsigned face
, unsigned level
, unsigned zslice
,
193 struct softpipe_texture
*spt
= softpipe_texture(pt
);
194 struct pipe_surface
*ps
;
196 assert(level
<= pt
->last_level
);
198 ps
= CALLOC_STRUCT(pipe_surface
);
200 pipe_reference_init(&ps
->reference
, 1);
201 pipe_texture_reference(&ps
->texture
, pt
);
202 ps
->format
= pt
->format
;
203 ps
->width
= pt
->width
[level
];
204 ps
->height
= pt
->height
[level
];
205 ps
->offset
= spt
->level_offset
[level
];
208 /* Because we are softpipe, anything that the state tracker
209 * thought was going to be done with the GPU will actually get
210 * done with the CPU. Let's adjust the flags to take that into
213 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
214 /* GPU_WRITE means "render" and that can involve reads (blending) */
215 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
218 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
219 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
225 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
226 ps
->offset
+= face
* pt
->nblocksy
[level
] * spt
->stride
[level
];
228 else if (pt
->target
== PIPE_TEXTURE_3D
) {
229 ps
->offset
+= zslice
* pt
->nblocksy
[level
] * spt
->stride
[level
];
241 softpipe_tex_surface_destroy(struct pipe_surface
*surf
)
243 /* Effectively do the texture_update work here - if texture images
244 * needed post-processing to put them into hardware layout, this is
245 * where it would happen. For softpipe, nothing to do.
247 assert(surf
->texture
);
248 pipe_texture_reference(&surf
->texture
, NULL
);
253 static struct pipe_transfer
*
254 softpipe_get_tex_transfer(struct pipe_screen
*screen
,
255 struct pipe_texture
*texture
,
256 unsigned face
, unsigned level
, unsigned zslice
,
257 enum pipe_transfer_usage usage
,
258 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
260 struct softpipe_texture
*sptex
= softpipe_texture(texture
);
261 struct softpipe_transfer
*spt
;
264 assert(level
<= texture
->last_level
);
266 spt
= CALLOC_STRUCT(softpipe_transfer
);
268 struct pipe_transfer
*pt
= &spt
->base
;
269 pipe_texture_reference(&pt
->texture
, texture
);
270 pt
->format
= texture
->format
;
271 pt
->block
= texture
->block
;
276 pt
->nblocksx
= texture
->nblocksx
[level
];
277 pt
->nblocksy
= texture
->nblocksy
[level
];
278 pt
->stride
= sptex
->stride
[level
];
284 spt
->offset
= sptex
->level_offset
[level
];
286 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
287 spt
->offset
+= face
* pt
->nblocksy
* pt
->stride
;
289 else if (texture
->target
== PIPE_TEXTURE_3D
) {
290 spt
->offset
+= zslice
* pt
->nblocksy
* pt
->stride
;
303 softpipe_tex_transfer_destroy(struct pipe_transfer
*transfer
)
305 /* Effectively do the texture_update work here - if texture images
306 * needed post-processing to put them into hardware layout, this is
307 * where it would happen. For softpipe, nothing to do.
309 assert (transfer
->texture
);
310 pipe_texture_reference(&transfer
->texture
, NULL
);
316 softpipe_transfer_map( struct pipe_screen
*screen
,
317 struct pipe_transfer
*transfer
)
319 ubyte
*map
, *xfer_map
;
320 struct softpipe_texture
*spt
;
323 assert(transfer
->texture
);
324 spt
= softpipe_texture(transfer
->texture
);
326 if (transfer
->usage
!= PIPE_TRANSFER_READ
) {
327 flags
|= PIPE_BUFFER_USAGE_CPU_WRITE
;
330 if (transfer
->usage
!= PIPE_TRANSFER_WRITE
) {
331 flags
|= PIPE_BUFFER_USAGE_CPU_READ
;
334 map
= pipe_buffer_map(screen
, spt
->buffer
, flags
);
338 /* May want to different things here depending on read/write nature
341 if (transfer
->texture
&& transfer
->usage
!= PIPE_TRANSFER_READ
) {
342 /* Do something to notify sharing contexts of a texture change.
343 * In softpipe, that would mean flushing the texture cache.
345 softpipe_screen(screen
)->timestamp
++;
348 xfer_map
= map
+ softpipe_transfer(transfer
)->offset
+
349 transfer
->y
/ transfer
->block
.height
* transfer
->stride
+
350 transfer
->x
/ transfer
->block
.width
* transfer
->block
.size
;
351 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
357 softpipe_transfer_unmap(struct pipe_screen
*screen
,
358 struct pipe_transfer
*transfer
)
360 struct softpipe_texture
*spt
;
362 assert(transfer
->texture
);
363 spt
= softpipe_texture(transfer
->texture
);
365 pipe_buffer_unmap( screen
, spt
->buffer
);
367 if (transfer
->usage
!= PIPE_TRANSFER_READ
) {
368 /* Mark the texture as dirty to expire the tile caches. */
369 spt
->modified
= TRUE
;
375 softpipe_init_texture_funcs(struct softpipe_context
*sp
)
381 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
383 screen
->texture_create
= softpipe_texture_create
;
384 screen
->texture_blanket
= softpipe_texture_blanket
;
385 screen
->texture_destroy
= softpipe_texture_destroy
;
387 screen
->get_tex_surface
= softpipe_get_tex_surface
;
388 screen
->tex_surface_destroy
= softpipe_tex_surface_destroy
;
390 screen
->get_tex_transfer
= softpipe_get_tex_transfer
;
391 screen
->tex_transfer_destroy
= softpipe_tex_transfer_destroy
;
392 screen
->transfer_map
= softpipe_transfer_map
;
393 screen
->transfer_unmap
= softpipe_transfer_unmap
;
398 softpipe_get_texture_buffer( struct pipe_texture
*texture
,
399 struct pipe_buffer
**buf
,
402 struct softpipe_texture
*tex
= (struct softpipe_texture
*) texture
;
407 pipe_buffer_reference(buf
, tex
->buffer
);
410 *stride
= tex
->stride
[0];