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 "lp_context.h"
42 #include "lp_texture.h"
43 #include "lp_tex_cache.h"
44 #include "lp_screen.h"
45 #include "lp_winsys.h"
48 /* Simple, maximally packed layout.
51 static unsigned minify( unsigned d
)
57 /* Conventional allocation path for non-display textures:
60 llvmpipe_texture_layout(struct pipe_screen
*screen
,
61 struct llvmpipe_texture
* lpt
)
63 struct pipe_texture
*pt
= &lpt
->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 pf_get_block(lpt
->base
.format
, &lpt
->base
.block
);
73 for (level
= 0; level
<= pt
->last_level
; level
++) {
74 pt
->width
[level
] = width
;
75 pt
->height
[level
] = height
;
76 pt
->depth
[level
] = depth
;
77 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, width
);
78 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, height
);
79 lpt
->stride
[level
] = pt
->nblocksx
[level
]*pt
->block
.size
;
81 lpt
->level_offset
[level
] = buffer_size
;
83 buffer_size
+= (pt
->nblocksy
[level
] *
84 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
87 width
= minify(width
);
88 height
= minify(height
);
89 depth
= minify(depth
);
92 lpt
->buffer
= screen
->buffer_create(screen
, 32,
93 PIPE_BUFFER_USAGE_PIXEL
,
96 return lpt
->buffer
!= NULL
;
100 llvmpipe_displaytarget_layout(struct pipe_screen
*screen
,
101 struct llvmpipe_texture
* lpt
)
103 unsigned usage
= (PIPE_BUFFER_USAGE_CPU_READ_WRITE
|
104 PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
106 pf_get_block(lpt
->base
.format
, &lpt
->base
.block
);
107 lpt
->base
.nblocksx
[0] = pf_get_nblocksx(&lpt
->base
.block
, lpt
->base
.width
[0]);
108 lpt
->base
.nblocksy
[0] = pf_get_nblocksy(&lpt
->base
.block
, lpt
->base
.height
[0]);
110 lpt
->buffer
= screen
->surface_buffer_create( screen
,
117 return lpt
->buffer
!= NULL
;
124 static struct pipe_texture
*
125 llvmpipe_texture_create(struct pipe_screen
*screen
,
126 const struct pipe_texture
*templat
)
128 struct llvmpipe_texture
*lpt
= CALLOC_STRUCT(llvmpipe_texture
);
132 lpt
->base
= *templat
;
133 pipe_reference_init(&lpt
->base
.reference
, 1);
134 lpt
->base
.screen
= screen
;
136 /* XXX: The xlib state tracker is brain-dead and will request
137 * PIPE_FORMAT_Z16_UNORM no matter how much we tell it we don't support it.
139 if(lpt
->base
.format
== PIPE_FORMAT_Z16_UNORM
)
140 lpt
->base
.format
= PIPE_FORMAT_Z32_UNORM
;
142 if (lpt
->base
.tex_usage
& PIPE_TEXTURE_USAGE_DISPLAY_TARGET
) {
143 if (!llvmpipe_displaytarget_layout(screen
, lpt
))
147 if (!llvmpipe_texture_layout(screen
, lpt
))
159 static struct pipe_texture
*
160 llvmpipe_texture_blanket(struct pipe_screen
* screen
,
161 const struct pipe_texture
*base
,
162 const unsigned *stride
,
163 struct pipe_buffer
*buffer
)
165 struct llvmpipe_texture
*lpt
;
168 /* Only supports one type */
169 if (base
->target
!= PIPE_TEXTURE_2D
||
170 base
->last_level
!= 0 ||
171 base
->depth
[0] != 1) {
175 lpt
= CALLOC_STRUCT(llvmpipe_texture
);
180 pipe_reference_init(&lpt
->base
.reference
, 1);
181 lpt
->base
.screen
= screen
;
182 lpt
->base
.nblocksx
[0] = pf_get_nblocksx(&lpt
->base
.block
, lpt
->base
.width
[0]);
183 lpt
->base
.nblocksy
[0] = pf_get_nblocksy(&lpt
->base
.block
, lpt
->base
.height
[0]);
184 lpt
->stride
[0] = stride
[0];
186 pipe_buffer_reference(&lpt
->buffer
, buffer
);
193 llvmpipe_texture_destroy(struct pipe_texture
*pt
)
195 struct llvmpipe_texture
*lpt
= llvmpipe_texture(pt
);
197 pipe_buffer_reference(&lpt
->buffer
, NULL
);
202 static struct pipe_surface
*
203 llvmpipe_get_tex_surface(struct pipe_screen
*screen
,
204 struct pipe_texture
*pt
,
205 unsigned face
, unsigned level
, unsigned zslice
,
208 struct llvmpipe_texture
*lpt
= llvmpipe_texture(pt
);
209 struct pipe_surface
*ps
;
211 assert(level
<= pt
->last_level
);
213 ps
= CALLOC_STRUCT(pipe_surface
);
215 pipe_reference_init(&ps
->reference
, 1);
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
= lpt
->level_offset
[level
];
223 /* Because we are llvmpipe, 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. */
240 llvmpipe_screen(screen
)->timestamp
++;
247 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
248 ps
->offset
+= face
* pt
->nblocksy
[level
] * lpt
->stride
[level
];
250 else if (pt
->target
== PIPE_TEXTURE_3D
) {
251 ps
->offset
+= zslice
* pt
->nblocksy
[level
] * lpt
->stride
[level
];
263 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
265 /* Effectively do the texture_update work here - if texture images
266 * needed post-processing to put them into hardware layout, this is
267 * where it would happen. For llvmpipe, nothing to do.
269 assert(surf
->texture
);
270 pipe_texture_reference(&surf
->texture
, NULL
);
275 static struct pipe_transfer
*
276 llvmpipe_get_tex_transfer(struct pipe_screen
*screen
,
277 struct pipe_texture
*texture
,
278 unsigned face
, unsigned level
, unsigned zslice
,
279 enum pipe_transfer_usage usage
,
280 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
282 struct llvmpipe_texture
*lptex
= llvmpipe_texture(texture
);
283 struct llvmpipe_transfer
*lpt
;
286 assert(level
<= texture
->last_level
);
288 lpt
= CALLOC_STRUCT(llvmpipe_transfer
);
290 struct pipe_transfer
*pt
= &lpt
->base
;
291 pipe_texture_reference(&pt
->texture
, texture
);
292 pt
->format
= texture
->format
;
293 pt
->block
= texture
->block
;
298 pt
->nblocksx
= texture
->nblocksx
[level
];
299 pt
->nblocksy
= texture
->nblocksy
[level
];
300 pt
->stride
= lptex
->stride
[level
];
306 lpt
->offset
= lptex
->level_offset
[level
];
308 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
309 lpt
->offset
+= face
* pt
->nblocksy
* pt
->stride
;
311 else if (texture
->target
== PIPE_TEXTURE_3D
) {
312 lpt
->offset
+= zslice
* pt
->nblocksy
* pt
->stride
;
325 llvmpipe_tex_transfer_destroy(struct pipe_transfer
*transfer
)
327 /* Effectively do the texture_update work here - if texture images
328 * needed post-processing to put them into hardware layout, this is
329 * where it would happen. For llvmpipe, nothing to do.
331 assert (transfer
->texture
);
332 pipe_texture_reference(&transfer
->texture
, NULL
);
338 llvmpipe_transfer_map( struct pipe_screen
*screen
,
339 struct pipe_transfer
*transfer
)
341 ubyte
*map
, *xfer_map
;
342 struct llvmpipe_texture
*lpt
;
345 assert(transfer
->texture
);
346 lpt
= llvmpipe_texture(transfer
->texture
);
348 if (transfer
->usage
!= PIPE_TRANSFER_READ
) {
349 flags
|= PIPE_BUFFER_USAGE_CPU_WRITE
;
352 if (transfer
->usage
!= PIPE_TRANSFER_WRITE
) {
353 flags
|= PIPE_BUFFER_USAGE_CPU_READ
;
356 map
= pipe_buffer_map(screen
, lpt
->buffer
, flags
);
360 /* May want to different things here depending on read/write nature
363 if (transfer
->texture
&& transfer
->usage
!= PIPE_TRANSFER_READ
)
365 /* Do something to notify sharing contexts of a texture change.
366 * In llvmpipe, that would mean flushing the texture cache.
368 llvmpipe_screen(screen
)->timestamp
++;
371 xfer_map
= map
+ llvmpipe_transfer(transfer
)->offset
+
372 transfer
->y
/ transfer
->block
.height
* transfer
->stride
+
373 transfer
->x
/ transfer
->block
.width
* transfer
->block
.size
;
374 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
380 llvmpipe_transfer_unmap(struct pipe_screen
*screen
,
381 struct pipe_transfer
*transfer
)
383 struct llvmpipe_texture
*lpt
;
385 assert(transfer
->texture
);
386 lpt
= llvmpipe_texture(transfer
->texture
);
388 pipe_buffer_unmap( screen
, lpt
->buffer
);
393 llvmpipe_init_texture_funcs(struct llvmpipe_context
*lp
)
399 llvmpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
401 screen
->texture_create
= llvmpipe_texture_create
;
402 screen
->texture_blanket
= llvmpipe_texture_blanket
;
403 screen
->texture_destroy
= llvmpipe_texture_destroy
;
405 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
406 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
408 screen
->get_tex_transfer
= llvmpipe_get_tex_transfer
;
409 screen
->tex_transfer_destroy
= llvmpipe_tex_transfer_destroy
;
410 screen
->transfer_map
= llvmpipe_transfer_map
;
411 screen
->transfer_unmap
= llvmpipe_transfer_unmap
;
416 llvmpipe_get_texture_buffer( struct pipe_texture
*texture
,
417 struct pipe_buffer
**buf
,
420 struct llvmpipe_texture
*tex
= (struct llvmpipe_texture
*)texture
;
425 pipe_buffer_reference(buf
, tex
->buffer
);
428 *stride
= tex
->stride
[0];