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_context.h"
34 #include "pipe/p_defines.h"
35 #include "pipe/p_inlines.h"
36 #include "pipe/p_util.h"
37 #include "pipe/p_winsys.h"
39 #include "sp_context.h"
41 #include "sp_texture.h"
42 #include "sp_tile_cache.h"
43 #include "sp_screen.h"
46 /* Simple, maximally packed layout.
49 static unsigned minify( unsigned d
)
55 /* Conventional allocation path for non-display textures:
58 softpipe_texture_layout(struct pipe_screen
*screen
,
59 struct softpipe_texture
* spt
)
61 struct pipe_winsys
*ws
= screen
->winsys
;
62 struct pipe_texture
*pt
= &spt
->base
;
64 unsigned width
= pt
->width
[0];
65 unsigned height
= pt
->height
[0];
66 unsigned depth
= pt
->depth
[0];
68 unsigned buffer_size
= 0;
70 for (level
= 0; level
<= pt
->last_level
; level
++) {
71 pt
->width
[level
] = width
;
72 pt
->height
[level
] = height
;
73 pt
->depth
[level
] = depth
;
74 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, width
);
75 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, height
);
76 spt
->stride
[level
] = pt
->nblocksx
[level
]*pt
->block
.size
;
78 spt
->level_offset
[level
] = buffer_size
;
80 buffer_size
+= (pt
->nblocksy
[level
] *
81 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
84 width
= minify(width
);
85 height
= minify(height
);
86 depth
= minify(depth
);
89 spt
->buffer
= ws
->buffer_create(ws
, 32,
90 PIPE_BUFFER_USAGE_PIXEL
,
93 return spt
->buffer
!= NULL
;
98 /* Hack it up to use the old winsys->surface_alloc_storage()
102 softpipe_displaytarget_layout(struct pipe_screen
*screen
,
103 struct softpipe_texture
* spt
)
105 struct pipe_winsys
*ws
= screen
->winsys
;
106 struct pipe_surface surf
;
107 unsigned flags
= (PIPE_BUFFER_USAGE_CPU_READ
|
108 PIPE_BUFFER_USAGE_CPU_WRITE
|
109 PIPE_BUFFER_USAGE_GPU_READ
|
110 PIPE_BUFFER_USAGE_GPU_WRITE
);
113 memset(&surf
, 0, sizeof(surf
));
115 ws
->surface_alloc_storage( ws
,
121 spt
->base
.tex_usage
);
123 /* Now extract the goodies:
125 spt
->buffer
= surf
.buffer
;
126 spt
->stride
[0] = surf
.stride
;
128 return spt
->buffer
!= NULL
;
135 static struct pipe_texture
*
136 softpipe_texture_create(struct pipe_screen
*screen
,
137 const struct pipe_texture
*templat
)
139 struct softpipe_texture
*spt
= CALLOC_STRUCT(softpipe_texture
);
143 spt
->base
= *templat
;
144 spt
->base
.refcount
= 1;
145 spt
->base
.screen
= screen
;
147 if (spt
->base
.tex_usage
& PIPE_TEXTURE_USAGE_DISPLAY_TARGET
) {
148 if (!softpipe_displaytarget_layout(screen
, spt
))
152 if (!softpipe_texture_layout(screen
, spt
))
156 assert(spt
->base
.refcount
== 1);
166 softpipe_texture_release(struct pipe_screen
*screen
,
167 struct pipe_texture
**pt
)
172 if (--(*pt
)->refcount
<= 0) {
173 struct softpipe_texture
*spt
= softpipe_texture(*pt
);
175 pipe_buffer_reference(screen
->winsys
, &spt
->buffer
, NULL
);
182 static struct pipe_surface
*
183 softpipe_get_tex_surface(struct pipe_screen
*screen
,
184 struct pipe_texture
*pt
,
185 unsigned face
, unsigned level
, unsigned zslice
,
188 struct pipe_winsys
*ws
= screen
->winsys
;
189 struct softpipe_texture
*spt
= softpipe_texture(pt
);
190 struct pipe_surface
*ps
;
192 assert(level
<= pt
->last_level
);
194 ps
= ws
->surface_alloc(ws
);
196 assert(ps
->refcount
);
198 pipe_buffer_reference(ws
, &ps
->buffer
, spt
->buffer
);
199 ps
->format
= pt
->format
;
200 ps
->block
= pt
->block
;
201 ps
->width
= pt
->width
[level
];
202 ps
->height
= pt
->height
[level
];
203 ps
->nblocksx
= pt
->nblocksx
[level
];
204 ps
->nblocksy
= pt
->nblocksy
[level
];
205 ps
->stride
= spt
->stride
[level
];
206 ps
->offset
= spt
->level_offset
[level
];
209 /* Because we are softpipe, anything that the state tracker
210 * thought was going to be done with the GPU will actually get
211 * done with the CPU. Let's adjust the flags to take that into
214 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
215 /* GPU_WRITE means "render" and that can involve reads (blending) */
216 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
219 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
220 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
222 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
223 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
224 /* Mark the surface as dirty. The tile cache will look for this. */
225 spt
->modified
= TRUE
;
228 pipe_texture_reference(&ps
->texture
, pt
);
233 if (pt
->target
== PIPE_TEXTURE_CUBE
|| pt
->target
== PIPE_TEXTURE_3D
) {
234 ps
->offset
+= ((pt
->target
== PIPE_TEXTURE_CUBE
) ? face
: zslice
) *
248 softpipe_tex_surface_release(struct pipe_screen
*screen
,
249 struct pipe_surface
**s
)
251 /* Effectively do the texture_update work here - if texture images
252 * needed post-processing to put them into hardware layout, this is
253 * where it would happen. For softpipe, nothing to do.
255 assert ((*s
)->texture
);
256 pipe_texture_reference(&(*s
)->texture
, NULL
);
258 screen
->winsys
->surface_release(screen
->winsys
, s
);
263 softpipe_surface_map( struct pipe_screen
*screen
,
264 struct pipe_surface
*surface
,
269 if (flags
& ~surface
->usage
) {
274 map
= screen
->winsys
->buffer_map( screen
->winsys
, surface
->buffer
, flags
);
278 /* May want to different things here depending on read/write nature
281 if (surface
->texture
&&
282 (flags
& PIPE_BUFFER_USAGE_CPU_WRITE
))
284 /* Do something to notify sharing contexts of a texture change.
285 * In softpipe, that would mean flushing the texture cache.
287 softpipe_screen(screen
)->timestamp
++;
290 return map
+ surface
->offset
;
295 softpipe_surface_unmap(struct pipe_screen
*screen
,
296 struct pipe_surface
*surface
)
298 screen
->winsys
->buffer_unmap( screen
->winsys
, surface
->buffer
);
303 softpipe_init_texture_funcs(struct softpipe_context
*sp
)
309 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
311 screen
->texture_create
= softpipe_texture_create
;
312 screen
->texture_release
= softpipe_texture_release
;
314 screen
->get_tex_surface
= softpipe_get_tex_surface
;
315 screen
->tex_surface_release
= softpipe_tex_surface_release
;
317 screen
->surface_map
= softpipe_surface_map
;
318 screen
->surface_unmap
= softpipe_surface_unmap
;