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 "util/u_inlines.h"
37 #include "util/u_format.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
41 #include "lp_context.h"
42 #include "lp_screen.h"
43 #include "lp_texture.h"
44 #include "lp_tile_size.h"
45 #include "state_tracker/sw_winsys.h"
49 * Conventional allocation path for non-display textures:
50 * Simple, maximally packed layout.
53 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
54 struct llvmpipe_texture
*lpt
)
56 struct pipe_texture
*pt
= &lpt
->base
;
58 unsigned width
= pt
->width0
;
59 unsigned height
= pt
->height0
;
60 unsigned depth
= pt
->depth0
;
61 unsigned buffer_size
= 0;
63 for (level
= 0; level
<= pt
->last_level
; level
++) {
64 unsigned nblocksx
, nblocksy
;
66 /* Allocate storage for whole quads. This is particularly important
67 * for depth surfaces, which are currently stored in a swizzled format.
69 nblocksx
= util_format_get_nblocksx(pt
->format
, align(width
, TILE_SIZE
));
70 nblocksy
= util_format_get_nblocksy(pt
->format
, align(height
, TILE_SIZE
));
72 lpt
->stride
[level
] = align(nblocksx
* util_format_get_blocksize(pt
->format
), 16);
74 lpt
->level_offset
[level
] = buffer_size
;
76 buffer_size
+= (nblocksy
*
77 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
80 width
= u_minify(width
, 1);
81 height
= u_minify(height
, 1);
82 depth
= u_minify(depth
, 1);
85 lpt
->data
= align_malloc(buffer_size
, 16);
87 return lpt
->data
!= NULL
;
93 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
94 struct llvmpipe_texture
*lpt
)
96 struct sw_winsys
*winsys
= screen
->winsys
;
98 /* Round up the surface size to a multiple of the tile size to
99 * avoid tile clipping.
101 unsigned width
= align(lpt
->base
.width0
, TILE_SIZE
);
102 unsigned height
= align(lpt
->base
.height0
, TILE_SIZE
);
104 lpt
->dt
= winsys
->displaytarget_create(winsys
,
110 return lpt
->dt
!= NULL
;
114 static struct pipe_texture
*
115 llvmpipe_texture_create(struct pipe_screen
*_screen
,
116 const struct pipe_texture
*templat
)
118 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
119 struct llvmpipe_texture
*lpt
= CALLOC_STRUCT(llvmpipe_texture
);
123 lpt
->base
= *templat
;
124 pipe_reference_init(&lpt
->base
.reference
, 1);
125 lpt
->base
.screen
= &screen
->base
;
127 if (lpt
->base
.tex_usage
& (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
128 PIPE_TEXTURE_USAGE_SCANOUT
|
129 PIPE_TEXTURE_USAGE_SHARED
)) {
130 if (!llvmpipe_displaytarget_layout(screen
, lpt
))
134 if (!llvmpipe_texture_layout(screen
, lpt
))
147 llvmpipe_texture_destroy(struct pipe_texture
*pt
)
149 struct llvmpipe_screen
*screen
= llvmpipe_screen(pt
->screen
);
150 struct llvmpipe_texture
*lpt
= llvmpipe_texture(pt
);
154 struct sw_winsys
*winsys
= screen
->winsys
;
155 winsys
->displaytarget_destroy(winsys
, lpt
->dt
);
158 /* regular texture */
159 align_free(lpt
->data
);
166 static struct pipe_surface
*
167 llvmpipe_get_tex_surface(struct pipe_screen
*screen
,
168 struct pipe_texture
*pt
,
169 unsigned face
, unsigned level
, unsigned zslice
,
172 struct llvmpipe_texture
*lpt
= llvmpipe_texture(pt
);
173 struct pipe_surface
*ps
;
175 assert(level
<= pt
->last_level
);
177 ps
= CALLOC_STRUCT(pipe_surface
);
179 pipe_reference_init(&ps
->reference
, 1);
180 pipe_texture_reference(&ps
->texture
, pt
);
181 ps
->format
= pt
->format
;
182 ps
->width
= u_minify(pt
->width0
, level
);
183 ps
->height
= u_minify(pt
->height0
, level
);
184 ps
->offset
= lpt
->level_offset
[level
];
187 /* Because we are llvmpipe, anything that the state tracker
188 * thought was going to be done with the GPU will actually get
189 * done with the CPU. Let's adjust the flags to take that into
192 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
193 /* GPU_WRITE means "render" and that can involve reads (blending) */
194 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
197 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
198 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
200 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
201 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
202 /* Mark the surface as dirty. */
204 llvmpipe_screen(screen
)->timestamp
++;
211 /* XXX shouldn't that rather be
212 tex_height = align(ps->height, 2);
213 to account for alignment done in llvmpipe_texture_layout ?
215 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
216 unsigned tex_height
= ps
->height
;
217 ps
->offset
+= face
* util_format_get_nblocksy(pt
->format
, tex_height
) * lpt
->stride
[level
];
219 else if (pt
->target
== PIPE_TEXTURE_3D
) {
220 unsigned tex_height
= ps
->height
;
221 ps
->offset
+= zslice
* util_format_get_nblocksy(pt
->format
, tex_height
) * lpt
->stride
[level
];
233 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
235 /* Effectively do the texture_update work here - if texture images
236 * needed post-processing to put them into hardware layout, this is
237 * where it would happen. For llvmpipe, nothing to do.
239 assert(surf
->texture
);
240 pipe_texture_reference(&surf
->texture
, NULL
);
245 static struct pipe_transfer
*
246 llvmpipe_get_tex_transfer(struct pipe_context
*pipe
,
247 struct pipe_texture
*texture
,
248 unsigned face
, unsigned level
, unsigned zslice
,
249 enum pipe_transfer_usage usage
,
250 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
252 struct llvmpipe_texture
*lptex
= llvmpipe_texture(texture
);
253 struct llvmpipe_transfer
*lpt
;
256 assert(level
<= texture
->last_level
);
258 lpt
= CALLOC_STRUCT(llvmpipe_transfer
);
260 struct pipe_transfer
*pt
= &lpt
->base
;
261 pipe_texture_reference(&pt
->texture
, texture
);
264 pt
->width
= align(w
, TILE_SIZE
);
265 pt
->height
= align(h
, TILE_SIZE
);
266 pt
->stride
= lptex
->stride
[level
];
272 lpt
->offset
= lptex
->level_offset
[level
];
274 /* XXX shouldn't that rather be
275 tex_height = align(u_minify(texture->height0, level), 2)
276 to account for alignment done in llvmpipe_texture_layout ?
278 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
279 unsigned tex_height
= u_minify(texture
->height0
, level
);
280 lpt
->offset
+= face
* util_format_get_nblocksy(texture
->format
, tex_height
) * pt
->stride
;
282 else if (texture
->target
== PIPE_TEXTURE_3D
) {
283 unsigned tex_height
= u_minify(texture
->height0
, level
);
284 lpt
->offset
+= zslice
* util_format_get_nblocksy(texture
->format
, tex_height
) * pt
->stride
;
297 llvmpipe_tex_transfer_destroy(struct pipe_context
*pipe
,
298 struct pipe_transfer
*transfer
)
300 /* Effectively do the texture_update work here - if texture images
301 * needed post-processing to put them into hardware layout, this is
302 * where it would happen. For llvmpipe, nothing to do.
304 assert (transfer
->texture
);
305 pipe_texture_reference(&transfer
->texture
, NULL
);
311 llvmpipe_transfer_map( struct pipe_context
*pipe
,
312 struct pipe_transfer
*transfer
)
314 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
315 ubyte
*map
, *xfer_map
;
316 struct llvmpipe_texture
*lpt
;
317 enum pipe_format format
;
319 assert(transfer
->texture
);
320 lpt
= llvmpipe_texture(transfer
->texture
);
321 format
= lpt
->base
.format
;
325 struct sw_winsys
*winsys
= screen
->winsys
;
327 map
= winsys
->displaytarget_map(winsys
, lpt
->dt
,
328 pipe_transfer_buffer_flags(transfer
));
333 /* regular texture */
337 /* May want to different things here depending on read/write nature
340 if (transfer
->texture
&& (transfer
->usage
& PIPE_TRANSFER_WRITE
)) {
341 /* Do something to notify sharing contexts of a texture change.
346 xfer_map
= map
+ llvmpipe_transfer(transfer
)->offset
+
347 transfer
->y
/ util_format_get_blockheight(format
) * transfer
->stride
+
348 transfer
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
349 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
355 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
356 struct pipe_transfer
*transfer
)
358 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(pipe
->screen
);
359 struct llvmpipe_texture
*lpt
;
361 assert(transfer
->texture
);
362 lpt
= llvmpipe_texture(transfer
->texture
);
366 struct sw_winsys
*winsys
= lp_screen
->winsys
;
367 winsys
->displaytarget_unmap(winsys
, lpt
->dt
);
373 llvmpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
375 screen
->texture_create
= llvmpipe_texture_create
;
376 screen
->texture_destroy
= llvmpipe_texture_destroy
;
378 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
379 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
384 llvmpipe_init_context_texture_funcs(struct pipe_context
*pipe
)
386 pipe
->get_tex_transfer
= llvmpipe_get_tex_transfer
;
387 pipe
->tex_transfer_destroy
= llvmpipe_tex_transfer_destroy
;
388 pipe
->transfer_map
= llvmpipe_transfer_map
;
389 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;