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_winsys.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
40 #include "sp_context.h"
42 #include "sp_texture.h"
43 #include "sp_tile_cache.h"
44 #include "sp_screen.h"
47 /* Simple, maximally packed layout.
50 static unsigned minify( unsigned d
)
56 /* Conventional allocation path for non-display textures:
59 softpipe_texture_layout(struct pipe_screen
*screen
,
60 struct softpipe_texture
* spt
)
62 struct pipe_winsys
*ws
= screen
->winsys
;
63 struct pipe_texture
*pt
= &spt
->base
;
65 unsigned width
= pt
->width
[0];
66 unsigned height
= pt
->height
[0];
67 unsigned depth
= pt
->depth
[0];
69 unsigned buffer_size
= 0;
71 for (level
= 0; level
<= pt
->last_level
; level
++) {
72 pt
->width
[level
] = width
;
73 pt
->height
[level
] = height
;
74 pt
->depth
[level
] = depth
;
75 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, width
);
76 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, height
);
77 spt
->stride
[level
] = pt
->nblocksx
[level
]*pt
->block
.size
;
79 spt
->level_offset
[level
] = buffer_size
;
81 buffer_size
+= (pt
->nblocksy
[level
] *
82 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
85 width
= minify(width
);
86 height
= minify(height
);
87 depth
= minify(depth
);
90 spt
->buffer
= ws
->buffer_create(ws
, 32,
91 PIPE_BUFFER_USAGE_PIXEL
,
94 return spt
->buffer
!= NULL
;
98 softpipe_displaytarget_layout(struct pipe_screen
*screen
,
99 struct softpipe_texture
* spt
)
101 struct pipe_winsys
*ws
= screen
->winsys
;
105 switch (spt
->base
.format
) {
106 case PIPE_FORMAT_R5G6B5_UNORM
:
109 case PIPE_FORMAT_Z24S8_UNORM
:
110 case PIPE_FORMAT_A8R8G8B8_UNORM
:
115 tex_size
= spt
->base
.width
[0] * cpp
* spt
->base
.height
[0];
116 spt
->buffer
= ws
->buffer_create(ws
, 64, PIPE_BUFFER_USAGE_PIXEL
, tex_size
);
117 /* Now extract the goodies:
119 spt
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width
[0]);
120 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height
[0]);
121 spt
->stride
[0] = spt
->base
.width
[0] * cpp
;
122 return spt
->buffer
!= NULL
;
129 static struct pipe_texture
*
130 softpipe_texture_create(struct pipe_screen
*screen
,
131 const struct pipe_texture
*templat
)
133 struct softpipe_texture
*spt
= CALLOC_STRUCT(softpipe_texture
);
137 spt
->base
= *templat
;
138 spt
->base
.refcount
= 1;
139 spt
->base
.screen
= screen
;
141 if (spt
->base
.tex_usage
& PIPE_TEXTURE_USAGE_DISPLAY_TARGET
) {
142 if (!softpipe_displaytarget_layout(screen
, spt
))
146 if (!softpipe_texture_layout(screen
, spt
))
150 assert(spt
->base
.refcount
== 1);
159 static struct pipe_texture
*
160 softpipe_texture_blanket(struct pipe_screen
* screen
,
161 const struct pipe_texture
*base
,
162 const unsigned *stride
,
163 struct pipe_buffer
*buffer
)
165 struct softpipe_texture
*spt
;
168 /* Only supports one type */
169 if (base
->target
!= PIPE_TEXTURE_2D
||
170 base
->last_level
!= 0 ||
171 base
->depth
[0] != 1) {
175 spt
= CALLOC_STRUCT(softpipe_texture
);
180 spt
->base
.refcount
= 1;
181 spt
->base
.screen
= screen
;
182 spt
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width
[0]);
183 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height
[0]);
184 spt
->stride
[0] = stride
[0];
186 pipe_buffer_reference(screen
, &spt
->buffer
, buffer
);
193 softpipe_texture_release(struct pipe_screen
*screen
,
194 struct pipe_texture
**pt
)
199 if (--(*pt
)->refcount
<= 0) {
200 struct softpipe_texture
*spt
= softpipe_texture(*pt
);
202 pipe_buffer_reference(screen
, &spt
->buffer
, NULL
);
209 static struct pipe_surface
*
210 softpipe_get_tex_surface(struct pipe_screen
*screen
,
211 struct pipe_texture
*pt
,
212 unsigned face
, unsigned level
, unsigned zslice
,
215 struct pipe_winsys
*ws
= screen
->winsys
;
216 struct softpipe_texture
*spt
= softpipe_texture(pt
);
217 struct pipe_surface
*ps
;
219 assert(level
<= pt
->last_level
);
221 ps
= CALLOC_STRUCT(pipe_surface
);
225 assert(ps
->refcount
);
227 pipe_texture_reference(&ps
->texture
, pt
);
228 pipe_buffer_reference(screen
, &ps
->buffer
, spt
->buffer
);
229 ps
->format
= pt
->format
;
230 ps
->block
= pt
->block
;
231 ps
->width
= pt
->width
[level
];
232 ps
->height
= pt
->height
[level
];
233 ps
->nblocksx
= pt
->nblocksx
[level
];
234 ps
->nblocksy
= pt
->nblocksy
[level
];
235 ps
->stride
= spt
->stride
[level
];
236 ps
->offset
= spt
->level_offset
[level
];
239 /* Because we are softpipe, anything that the state tracker
240 * thought was going to be done with the GPU will actually get
241 * done with the CPU. Let's adjust the flags to take that into
244 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
245 /* GPU_WRITE means "render" and that can involve reads (blending) */
246 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
249 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
250 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
252 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
253 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
254 /* Mark the surface as dirty. The tile cache will look for this. */
255 spt
->modified
= TRUE
;
262 if (pt
->target
== PIPE_TEXTURE_CUBE
|| pt
->target
== PIPE_TEXTURE_3D
) {
263 ps
->offset
+= ((pt
->target
== PIPE_TEXTURE_CUBE
) ? face
: zslice
) *
277 softpipe_tex_surface_release(struct pipe_screen
*screen
,
278 struct pipe_surface
**s
)
280 struct pipe_surface
*surf
= *s
;
281 /* Effectively do the texture_update work here - if texture images
282 * needed post-processing to put them into hardware layout, this is
283 * where it would happen. For softpipe, nothing to do.
285 assert ((*s
)->texture
);
286 if (--surf
->refcount
== 0) {
287 pipe_texture_reference(&surf
->texture
, NULL
);
288 pipe_buffer_reference(screen
, &surf
->buffer
, NULL
);
296 softpipe_surface_map( struct pipe_screen
*screen
,
297 struct pipe_surface
*surface
,
302 if (flags
& ~surface
->usage
) {
307 map
= pipe_buffer_map( screen
, surface
->buffer
, flags
);
311 /* May want to different things here depending on read/write nature
314 if (surface
->texture
&&
315 (flags
& PIPE_BUFFER_USAGE_CPU_WRITE
))
317 /* Do something to notify sharing contexts of a texture change.
318 * In softpipe, that would mean flushing the texture cache.
320 softpipe_screen(screen
)->timestamp
++;
323 return map
+ surface
->offset
;
328 softpipe_surface_unmap(struct pipe_screen
*screen
,
329 struct pipe_surface
*surface
)
331 pipe_buffer_unmap( screen
, surface
->buffer
);
336 softpipe_init_texture_funcs(struct softpipe_context
*sp
)
342 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
344 screen
->texture_create
= softpipe_texture_create
;
345 screen
->texture_blanket
= softpipe_texture_blanket
;
346 screen
->texture_release
= softpipe_texture_release
;
348 screen
->get_tex_surface
= softpipe_get_tex_surface
;
349 screen
->tex_surface_release
= softpipe_tex_surface_release
;
351 screen
->surface_map
= softpipe_surface_map
;
352 screen
->surface_unmap
= softpipe_surface_unmap
;