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.
52 /* Conventional allocation path for non-display textures:
55 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
56 struct llvmpipe_texture
* lpt
)
58 struct pipe_texture
*pt
= &lpt
->base
;
60 unsigned width
= pt
->width0
;
61 unsigned height
= pt
->height0
;
62 unsigned depth
= pt
->depth0
;
64 unsigned buffer_size
= 0;
66 pf_get_block(lpt
->base
.format
, &lpt
->base
.block
);
68 for (level
= 0; level
<= pt
->last_level
; level
++) {
69 unsigned nblocksx
, nblocksy
;
71 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, width
);
72 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, height
);
74 /* Allocate storage for whole quads. This is particularly important
75 * for depth surfaces, which are currently stored in a swizzled format. */
76 nblocksx
= pf_get_nblocksx(&pt
->block
, align(width
, 2));
77 nblocksy
= pf_get_nblocksy(&pt
->block
, align(height
, 2));
79 lpt
->stride
[level
] = align(nblocksx
*pt
->block
.size
, 16);
81 lpt
->level_offset
[level
] = buffer_size
;
83 buffer_size
+= (nblocksy
*
84 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
87 width
= u_minify(width
, 1);
88 height
= u_minify(height
, 1);
89 depth
= u_minify(depth
, 1);
92 lpt
->data
= align_malloc(buffer_size
, 16);
94 return lpt
->data
!= NULL
;
98 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
99 struct llvmpipe_texture
* lpt
)
101 struct llvmpipe_winsys
*winsys
= screen
->winsys
;
103 pf_get_block(lpt
->base
.format
, &lpt
->base
.block
);
104 lpt
->base
.nblocksx
[0] = pf_get_nblocksx(&lpt
->base
.block
, lpt
->base
.width0
);
105 lpt
->base
.nblocksy
[0] = pf_get_nblocksy(&lpt
->base
.block
, lpt
->base
.height0
);
107 lpt
->dt
= winsys
->displaytarget_create(winsys
,
114 return lpt
->dt
!= NULL
;
121 static struct pipe_texture
*
122 llvmpipe_texture_create(struct pipe_screen
*_screen
,
123 const struct pipe_texture
*templat
)
125 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
126 struct llvmpipe_texture
*lpt
= CALLOC_STRUCT(llvmpipe_texture
);
130 lpt
->base
= *templat
;
131 pipe_reference_init(&lpt
->base
.reference
, 1);
132 lpt
->base
.screen
= &screen
->base
;
134 /* XXX: The xlib state tracker is brain-dead and will request
135 * PIPE_FORMAT_Z16_UNORM no matter how much we tell it we don't support it.
137 if(lpt
->base
.format
== PIPE_FORMAT_Z16_UNORM
)
138 lpt
->base
.format
= PIPE_FORMAT_Z32_UNORM
;
140 if (lpt
->base
.tex_usage
& (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
141 PIPE_TEXTURE_USAGE_PRIMARY
)) {
142 if (!llvmpipe_displaytarget_layout(screen
, lpt
))
146 if (!llvmpipe_texture_layout(screen
, lpt
))
158 static struct pipe_texture
*
159 llvmpipe_texture_blanket(struct pipe_screen
* screen
,
160 const struct pipe_texture
*base
,
161 const unsigned *stride
,
162 struct pipe_buffer
*buffer
)
166 struct llvmpipe_texture
*lpt
;
169 /* Only supports one type */
170 if (base
->target
!= PIPE_TEXTURE_2D
||
171 base
->last_level
!= 0 ||
176 lpt
= CALLOC_STRUCT(llvmpipe_texture
);
181 pipe_reference_init(&lpt
->base
.reference
, 1);
182 lpt
->base
.screen
= screen
;
183 lpt
->base
.nblocksx
[0] = pf_get_nblocksx(&lpt
->base
.block
, lpt
->base
.width0
);
184 lpt
->base
.nblocksy
[0] = pf_get_nblocksy(&lpt
->base
.block
, lpt
->base
.height0
);
185 lpt
->stride
[0] = stride
[0];
187 pipe_buffer_reference(&lpt
->buffer
, buffer
);
197 llvmpipe_texture_destroy(struct pipe_texture
*pt
)
199 struct llvmpipe_screen
*screen
= llvmpipe_screen(pt
->screen
);
200 struct llvmpipe_texture
*lpt
= llvmpipe_texture(pt
);
203 struct llvmpipe_winsys
*winsys
= screen
->winsys
;
204 winsys
->displaytarget_destroy(winsys
, lpt
->dt
);
207 align_free(lpt
->data
);
213 static struct pipe_surface
*
214 llvmpipe_get_tex_surface(struct pipe_screen
*screen
,
215 struct pipe_texture
*pt
,
216 unsigned face
, unsigned level
, unsigned zslice
,
219 struct llvmpipe_texture
*lpt
= llvmpipe_texture(pt
);
220 struct pipe_surface
*ps
;
222 assert(level
<= pt
->last_level
);
224 ps
= CALLOC_STRUCT(pipe_surface
);
226 pipe_reference_init(&ps
->reference
, 1);
227 pipe_texture_reference(&ps
->texture
, pt
);
228 ps
->format
= pt
->format
;
229 ps
->width
= u_minify(pt
->width0
, level
);
230 ps
->height
= u_minify(pt
->height0
, level
);
231 ps
->offset
= lpt
->level_offset
[level
];
234 /* Because we are llvmpipe, anything that the state tracker
235 * thought was going to be done with the GPU will actually get
236 * done with the CPU. Let's adjust the flags to take that into
239 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
240 /* GPU_WRITE means "render" and that can involve reads (blending) */
241 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
244 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
245 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
247 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
248 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
249 /* Mark the surface as dirty. The tile cache will look for this. */
251 llvmpipe_screen(screen
)->timestamp
++;
258 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
259 ps
->offset
+= face
* pt
->nblocksy
[level
] * lpt
->stride
[level
];
261 else if (pt
->target
== PIPE_TEXTURE_3D
) {
262 ps
->offset
+= zslice
* pt
->nblocksy
[level
] * lpt
->stride
[level
];
274 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
276 /* Effectively do the texture_update work here - if texture images
277 * needed post-processing to put them into hardware layout, this is
278 * where it would happen. For llvmpipe, nothing to do.
280 assert(surf
->texture
);
281 pipe_texture_reference(&surf
->texture
, NULL
);
286 static struct pipe_transfer
*
287 llvmpipe_get_tex_transfer(struct pipe_screen
*screen
,
288 struct pipe_texture
*texture
,
289 unsigned face
, unsigned level
, unsigned zslice
,
290 enum pipe_transfer_usage usage
,
291 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
293 struct llvmpipe_texture
*lptex
= llvmpipe_texture(texture
);
294 struct llvmpipe_transfer
*lpt
;
297 assert(level
<= texture
->last_level
);
299 lpt
= CALLOC_STRUCT(llvmpipe_transfer
);
301 struct pipe_transfer
*pt
= &lpt
->base
;
302 pipe_texture_reference(&pt
->texture
, texture
);
303 pt
->format
= texture
->format
;
304 pt
->block
= texture
->block
;
309 pt
->nblocksx
= texture
->nblocksx
[level
];
310 pt
->nblocksy
= texture
->nblocksy
[level
];
311 pt
->stride
= lptex
->stride
[level
];
317 lpt
->offset
= lptex
->level_offset
[level
];
319 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
320 lpt
->offset
+= face
* pt
->nblocksy
* pt
->stride
;
322 else if (texture
->target
== PIPE_TEXTURE_3D
) {
323 lpt
->offset
+= zslice
* pt
->nblocksy
* pt
->stride
;
336 llvmpipe_tex_transfer_destroy(struct pipe_transfer
*transfer
)
338 /* Effectively do the texture_update work here - if texture images
339 * needed post-processing to put them into hardware layout, this is
340 * where it would happen. For llvmpipe, nothing to do.
342 assert (transfer
->texture
);
343 pipe_texture_reference(&transfer
->texture
, NULL
);
349 llvmpipe_transfer_map( struct pipe_screen
*_screen
,
350 struct pipe_transfer
*transfer
)
352 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
353 ubyte
*map
, *xfer_map
;
354 struct llvmpipe_texture
*lpt
;
356 assert(transfer
->texture
);
357 lpt
= llvmpipe_texture(transfer
->texture
);
360 struct llvmpipe_winsys
*winsys
= screen
->winsys
;
362 map
= winsys
->displaytarget_map(winsys
, lpt
->dt
,
363 pipe_transfer_buffer_flags(transfer
));
370 /* May want to different things here depending on read/write nature
373 if (transfer
->texture
&& (transfer
->usage
& PIPE_TRANSFER_WRITE
))
375 /* Do something to notify sharing contexts of a texture change.
376 * In llvmpipe, that would mean flushing the texture cache.
381 xfer_map
= map
+ llvmpipe_transfer(transfer
)->offset
+
382 transfer
->y
/ transfer
->block
.height
* transfer
->stride
+
383 transfer
->x
/ transfer
->block
.width
* transfer
->block
.size
;
384 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
390 llvmpipe_transfer_unmap(struct pipe_screen
*_screen
,
391 struct pipe_transfer
*transfer
)
393 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
394 struct llvmpipe_texture
*lpt
;
396 assert(transfer
->texture
);
397 lpt
= llvmpipe_texture(transfer
->texture
);
400 struct llvmpipe_winsys
*winsys
= screen
->winsys
;
401 winsys
->displaytarget_unmap(winsys
, lpt
->dt
);
407 llvmpipe_init_texture_funcs(struct llvmpipe_context
*lp
)
413 llvmpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
415 screen
->texture_create
= llvmpipe_texture_create
;
416 screen
->texture_blanket
= llvmpipe_texture_blanket
;
417 screen
->texture_destroy
= llvmpipe_texture_destroy
;
419 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
420 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
422 screen
->get_tex_transfer
= llvmpipe_get_tex_transfer
;
423 screen
->tex_transfer_destroy
= llvmpipe_tex_transfer_destroy
;
424 screen
->transfer_map
= llvmpipe_transfer_map
;
425 screen
->transfer_unmap
= llvmpipe_transfer_unmap
;