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/internal/p_winsys_screen.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
;
102 unsigned usage
= (PIPE_BUFFER_USAGE_CPU_READ_WRITE
|
103 PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
105 spt
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width
[0]);
106 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height
[0]);
108 spt
->buffer
= ws
->surface_buffer_create( ws
,
115 return spt
->buffer
!= NULL
;
122 static struct pipe_texture
*
123 softpipe_texture_create(struct pipe_screen
*screen
,
124 const struct pipe_texture
*templat
)
126 struct softpipe_texture
*spt
= CALLOC_STRUCT(softpipe_texture
);
130 spt
->base
= *templat
;
131 spt
->base
.refcount
= 1;
132 spt
->base
.screen
= screen
;
134 if (spt
->base
.tex_usage
& PIPE_TEXTURE_USAGE_DISPLAY_TARGET
) {
135 if (!softpipe_displaytarget_layout(screen
, spt
))
139 if (!softpipe_texture_layout(screen
, spt
))
143 assert(spt
->base
.refcount
== 1);
152 static struct pipe_texture
*
153 softpipe_texture_blanket(struct pipe_screen
* screen
,
154 const struct pipe_texture
*base
,
155 const unsigned *stride
,
156 struct pipe_buffer
*buffer
)
158 struct softpipe_texture
*spt
;
161 /* Only supports one type */
162 if (base
->target
!= PIPE_TEXTURE_2D
||
163 base
->last_level
!= 0 ||
164 base
->depth
[0] != 1) {
168 spt
= CALLOC_STRUCT(softpipe_texture
);
173 spt
->base
.refcount
= 1;
174 spt
->base
.screen
= screen
;
175 spt
->base
.nblocksx
[0] = pf_get_nblocksx(&spt
->base
.block
, spt
->base
.width
[0]);
176 spt
->base
.nblocksy
[0] = pf_get_nblocksy(&spt
->base
.block
, spt
->base
.height
[0]);
177 spt
->stride
[0] = stride
[0];
179 pipe_buffer_reference(screen
, &spt
->buffer
, buffer
);
186 softpipe_texture_release(struct pipe_screen
*screen
,
187 struct pipe_texture
**pt
)
192 if (--(*pt
)->refcount
<= 0) {
193 struct softpipe_texture
*spt
= softpipe_texture(*pt
);
195 pipe_buffer_reference(screen
, &spt
->buffer
, NULL
);
202 static struct pipe_surface
*
203 softpipe_get_tex_surface(struct pipe_screen
*screen
,
204 struct pipe_texture
*pt
,
205 unsigned face
, unsigned level
, unsigned zslice
,
208 struct softpipe_texture
*spt
= softpipe_texture(pt
);
209 struct pipe_surface
*ps
;
211 assert(level
<= pt
->last_level
);
213 ps
= CALLOC_STRUCT(pipe_surface
);
216 pipe_texture_reference(&ps
->texture
, pt
);
217 ps
->format
= pt
->format
;
218 ps
->width
= pt
->width
[level
];
219 ps
->height
= pt
->height
[level
];
220 ps
->offset
= spt
->level_offset
[level
];
223 /* Because we are softpipe, anything that the state tracker
224 * thought was going to be done with the GPU will actually get
225 * done with the CPU. Let's adjust the flags to take that into
228 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
229 /* GPU_WRITE means "render" and that can involve reads (blending) */
230 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
233 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
234 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
236 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
237 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
238 /* Mark the surface as dirty. The tile cache will look for this. */
239 spt
->modified
= TRUE
;
246 if (pt
->target
== PIPE_TEXTURE_CUBE
|| pt
->target
== PIPE_TEXTURE_3D
) {
247 ps
->offset
+= ((pt
->target
== PIPE_TEXTURE_CUBE
) ? face
: zslice
) *
248 pt
->nblocksy
[level
] * spt
->stride
[level
];
260 softpipe_tex_surface_release(struct pipe_screen
*screen
,
261 struct pipe_surface
**s
)
263 struct pipe_surface
*surf
= *s
;
264 /* Effectively do the texture_update work here - if texture images
265 * needed post-processing to put them into hardware layout, this is
266 * where it would happen. For softpipe, nothing to do.
268 assert(surf
->texture
);
269 if (--surf
->refcount
== 0) {
270 pipe_texture_reference(&surf
->texture
, NULL
);
277 static struct pipe_transfer
*
278 softpipe_get_tex_transfer(struct pipe_screen
*screen
,
279 struct pipe_texture
*texture
,
280 unsigned face
, unsigned level
, unsigned zslice
,
281 enum pipe_transfer_usage usage
,
282 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
284 struct softpipe_texture
*sptex
= softpipe_texture(texture
);
285 struct softpipe_transfer
*spt
;
288 assert(level
<= texture
->last_level
);
290 spt
= CALLOC_STRUCT(softpipe_transfer
);
292 struct pipe_transfer
*pt
= &spt
->base
;
294 pipe_texture_reference(&pt
->texture
, texture
);
295 pt
->format
= texture
->format
;
296 pt
->block
= texture
->block
;
301 pt
->nblocksx
= texture
->nblocksx
[level
];
302 pt
->nblocksy
= texture
->nblocksy
[level
];
303 pt
->stride
= sptex
->stride
[level
];
309 spt
->offset
= sptex
->level_offset
[level
];
311 if (texture
->target
== PIPE_TEXTURE_CUBE
||
312 texture
->target
== PIPE_TEXTURE_3D
) {
313 spt
->offset
+= ((texture
->target
== PIPE_TEXTURE_CUBE
) ? face
:
314 zslice
) * pt
->nblocksy
* pt
->stride
;
327 softpipe_tex_transfer_release(struct pipe_screen
*screen
,
328 struct pipe_transfer
**t
)
330 struct softpipe_transfer
*transfer
= softpipe_transfer(*t
);
331 /* Effectively do the texture_update work here - if texture images
332 * needed post-processing to put them into hardware layout, this is
333 * where it would happen. For softpipe, nothing to do.
335 assert (transfer
->base
.texture
);
336 if (--transfer
->base
.refcount
== 0) {
337 pipe_texture_reference(&transfer
->base
.texture
, NULL
);
345 softpipe_transfer_map( struct pipe_screen
*screen
,
346 struct pipe_transfer
*transfer
)
349 struct softpipe_texture
*spt
;
352 assert(transfer
->texture
);
353 spt
= softpipe_texture(transfer
->texture
);
355 if (transfer
->usage
!= PIPE_TRANSFER_READ
) {
356 flags
|= PIPE_BUFFER_USAGE_CPU_WRITE
;
359 if (transfer
->usage
!= PIPE_TRANSFER_WRITE
) {
360 flags
|= PIPE_BUFFER_USAGE_CPU_READ
;
363 map
= pipe_buffer_map(screen
, spt
->buffer
, flags
);
367 /* May want to different things here depending on read/write nature
370 if (transfer
->texture
&& transfer
->usage
!= PIPE_TRANSFER_READ
)
372 /* Do something to notify sharing contexts of a texture change.
373 * In softpipe, that would mean flushing the texture cache.
375 softpipe_screen(screen
)->timestamp
++;
378 return map
+ softpipe_transfer(transfer
)->offset
+
379 transfer
->y
/ transfer
->block
.height
* transfer
->stride
+
380 transfer
->x
/ transfer
->block
.width
* transfer
->block
.size
;
385 softpipe_transfer_unmap(struct pipe_screen
*screen
,
386 struct pipe_transfer
*transfer
)
388 struct softpipe_texture
*spt
;
390 assert(transfer
->texture
);
391 spt
= softpipe_texture(transfer
->texture
);
393 pipe_buffer_unmap( screen
, spt
->buffer
);
398 softpipe_init_texture_funcs(struct softpipe_context
*sp
)
404 softpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
406 screen
->texture_create
= softpipe_texture_create
;
407 screen
->texture_blanket
= softpipe_texture_blanket
;
408 screen
->texture_release
= softpipe_texture_release
;
410 screen
->get_tex_surface
= softpipe_get_tex_surface
;
411 screen
->tex_surface_release
= softpipe_tex_surface_release
;
413 screen
->get_tex_transfer
= softpipe_get_tex_transfer
;
414 screen
->tex_transfer_release
= softpipe_tex_transfer_release
;
415 screen
->transfer_map
= softpipe_transfer_map
;
416 screen
->transfer_unmap
= softpipe_transfer_unmap
;