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
->width
[0];
61 unsigned height
= pt
->height
[0];
62 unsigned depth
= pt
->depth
[0];
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 pt
->width
[level
] = width
;
70 pt
->height
[level
] = height
;
71 pt
->depth
[level
] = depth
;
72 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, width
);
73 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, height
);
74 lpt
->stride
[level
] = align(pt
->nblocksx
[level
]*pt
->block
.size
, 16);
76 lpt
->level_offset
[level
] = buffer_size
;
78 buffer_size
+= (pt
->nblocksy
[level
] *
79 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
82 width
= minify(width
);
83 height
= minify(height
);
84 depth
= minify(depth
);
87 lpt
->data
= align_malloc(buffer_size
, 16);
89 return lpt
->data
!= NULL
;
93 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
94 struct llvmpipe_texture
* lpt
)
96 struct llvmpipe_winsys
*winsys
= screen
->winsys
;
98 pf_get_block(lpt
->base
.format
, &lpt
->base
.block
);
99 lpt
->base
.nblocksx
[0] = pf_get_nblocksx(&lpt
->base
.block
, lpt
->base
.width
[0]);
100 lpt
->base
.nblocksy
[0] = pf_get_nblocksy(&lpt
->base
.block
, lpt
->base
.height
[0]);
102 lpt
->dt
= winsys
->displaytarget_create(winsys
,
109 return lpt
->dt
!= NULL
;
116 static struct pipe_texture
*
117 llvmpipe_texture_create(struct pipe_screen
*_screen
,
118 const struct pipe_texture
*templat
)
120 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
121 struct llvmpipe_texture
*lpt
= CALLOC_STRUCT(llvmpipe_texture
);
125 lpt
->base
= *templat
;
126 pipe_reference_init(&lpt
->base
.reference
, 1);
127 lpt
->base
.screen
= &screen
->base
;
129 /* XXX: The xlib state tracker is brain-dead and will request
130 * PIPE_FORMAT_Z16_UNORM no matter how much we tell it we don't support it.
132 if(lpt
->base
.format
== PIPE_FORMAT_Z16_UNORM
)
133 lpt
->base
.format
= PIPE_FORMAT_Z32_UNORM
;
135 if (lpt
->base
.tex_usage
& (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
136 PIPE_TEXTURE_USAGE_PRIMARY
)) {
137 if (!llvmpipe_displaytarget_layout(screen
, lpt
))
141 if (!llvmpipe_texture_layout(screen
, lpt
))
153 static struct pipe_texture
*
154 llvmpipe_texture_blanket(struct pipe_screen
* screen
,
155 const struct pipe_texture
*base
,
156 const unsigned *stride
,
157 struct pipe_buffer
*buffer
)
161 struct llvmpipe_texture
*lpt
;
164 /* Only supports one type */
165 if (base
->target
!= PIPE_TEXTURE_2D
||
166 base
->last_level
!= 0 ||
167 base
->depth
[0] != 1) {
171 lpt
= CALLOC_STRUCT(llvmpipe_texture
);
176 pipe_reference_init(&lpt
->base
.reference
, 1);
177 lpt
->base
.screen
= screen
;
178 lpt
->base
.nblocksx
[0] = pf_get_nblocksx(&lpt
->base
.block
, lpt
->base
.width
[0]);
179 lpt
->base
.nblocksy
[0] = pf_get_nblocksy(&lpt
->base
.block
, lpt
->base
.height
[0]);
180 lpt
->stride
[0] = stride
[0];
182 pipe_buffer_reference(&lpt
->buffer
, buffer
);
192 llvmpipe_texture_destroy(struct pipe_texture
*pt
)
194 struct llvmpipe_screen
*screen
= llvmpipe_screen(pt
->screen
);
195 struct llvmpipe_texture
*lpt
= llvmpipe_texture(pt
);
198 struct llvmpipe_winsys
*winsys
= screen
->winsys
;
199 winsys
->displaytarget_destroy(winsys
, lpt
->dt
);
202 align_free(lpt
->data
);
208 static struct pipe_surface
*
209 llvmpipe_get_tex_surface(struct pipe_screen
*screen
,
210 struct pipe_texture
*pt
,
211 unsigned face
, unsigned level
, unsigned zslice
,
214 struct llvmpipe_texture
*lpt
= llvmpipe_texture(pt
);
215 struct pipe_surface
*ps
;
217 assert(level
<= pt
->last_level
);
219 ps
= CALLOC_STRUCT(pipe_surface
);
221 pipe_reference_init(&ps
->reference
, 1);
222 pipe_texture_reference(&ps
->texture
, pt
);
223 ps
->format
= pt
->format
;
224 ps
->width
= pt
->width
[level
];
225 ps
->height
= pt
->height
[level
];
226 ps
->offset
= lpt
->level_offset
[level
];
229 /* Because we are llvmpipe, anything that the state tracker
230 * thought was going to be done with the GPU will actually get
231 * done with the CPU. Let's adjust the flags to take that into
234 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
235 /* GPU_WRITE means "render" and that can involve reads (blending) */
236 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_CPU_READ
;
239 if (ps
->usage
& PIPE_BUFFER_USAGE_GPU_READ
)
240 ps
->usage
|= PIPE_BUFFER_USAGE_CPU_READ
;
242 if (ps
->usage
& (PIPE_BUFFER_USAGE_CPU_WRITE
|
243 PIPE_BUFFER_USAGE_GPU_WRITE
)) {
244 /* Mark the surface as dirty. The tile cache will look for this. */
246 llvmpipe_screen(screen
)->timestamp
++;
253 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
254 ps
->offset
+= face
* pt
->nblocksy
[level
] * lpt
->stride
[level
];
256 else if (pt
->target
== PIPE_TEXTURE_3D
) {
257 ps
->offset
+= zslice
* pt
->nblocksy
[level
] * lpt
->stride
[level
];
269 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
271 /* Effectively do the texture_update work here - if texture images
272 * needed post-processing to put them into hardware layout, this is
273 * where it would happen. For llvmpipe, nothing to do.
275 assert(surf
->texture
);
276 pipe_texture_reference(&surf
->texture
, NULL
);
281 static struct pipe_transfer
*
282 llvmpipe_get_tex_transfer(struct pipe_screen
*screen
,
283 struct pipe_texture
*texture
,
284 unsigned face
, unsigned level
, unsigned zslice
,
285 enum pipe_transfer_usage usage
,
286 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
288 struct llvmpipe_texture
*lptex
= llvmpipe_texture(texture
);
289 struct llvmpipe_transfer
*lpt
;
292 assert(level
<= texture
->last_level
);
294 lpt
= CALLOC_STRUCT(llvmpipe_transfer
);
296 struct pipe_transfer
*pt
= &lpt
->base
;
297 pipe_texture_reference(&pt
->texture
, texture
);
298 pt
->format
= texture
->format
;
299 pt
->block
= texture
->block
;
304 pt
->nblocksx
= texture
->nblocksx
[level
];
305 pt
->nblocksy
= texture
->nblocksy
[level
];
306 pt
->stride
= lptex
->stride
[level
];
312 lpt
->offset
= lptex
->level_offset
[level
];
314 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
315 lpt
->offset
+= face
* pt
->nblocksy
* pt
->stride
;
317 else if (texture
->target
== PIPE_TEXTURE_3D
) {
318 lpt
->offset
+= zslice
* pt
->nblocksy
* pt
->stride
;
331 llvmpipe_tex_transfer_destroy(struct pipe_transfer
*transfer
)
333 /* Effectively do the texture_update work here - if texture images
334 * needed post-processing to put them into hardware layout, this is
335 * where it would happen. For llvmpipe, nothing to do.
337 assert (transfer
->texture
);
338 pipe_texture_reference(&transfer
->texture
, NULL
);
344 llvmpipe_transfer_map( struct pipe_screen
*_screen
,
345 struct pipe_transfer
*transfer
)
347 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
348 ubyte
*map
, *xfer_map
;
349 struct llvmpipe_texture
*lpt
;
351 assert(transfer
->texture
);
352 lpt
= llvmpipe_texture(transfer
->texture
);
355 struct llvmpipe_winsys
*winsys
= screen
->winsys
;
358 if (transfer
->usage
!= PIPE_TRANSFER_READ
) {
359 flags
|= PIPE_BUFFER_USAGE_CPU_WRITE
;
362 if (transfer
->usage
!= PIPE_TRANSFER_WRITE
) {
363 flags
|= PIPE_BUFFER_USAGE_CPU_READ
;
366 map
= winsys
->displaytarget_map(winsys
, lpt
->dt
, flags
);
373 /* May want to different things here depending on read/write nature
376 if (transfer
->texture
&& transfer
->usage
!= PIPE_TRANSFER_READ
)
378 /* Do something to notify sharing contexts of a texture change.
379 * In llvmpipe, that would mean flushing the texture cache.
384 xfer_map
= map
+ llvmpipe_transfer(transfer
)->offset
+
385 transfer
->y
/ transfer
->block
.height
* transfer
->stride
+
386 transfer
->x
/ transfer
->block
.width
* transfer
->block
.size
;
387 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
393 llvmpipe_transfer_unmap(struct pipe_screen
*_screen
,
394 struct pipe_transfer
*transfer
)
396 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
397 struct llvmpipe_texture
*lpt
;
399 assert(transfer
->texture
);
400 lpt
= llvmpipe_texture(transfer
->texture
);
403 struct llvmpipe_winsys
*winsys
= screen
->winsys
;
404 winsys
->displaytarget_unmap(winsys
, lpt
->dt
);
410 llvmpipe_init_texture_funcs(struct llvmpipe_context
*lp
)
416 llvmpipe_init_screen_texture_funcs(struct pipe_screen
*screen
)
418 screen
->texture_create
= llvmpipe_texture_create
;
419 screen
->texture_blanket
= llvmpipe_texture_blanket
;
420 screen
->texture_destroy
= llvmpipe_texture_destroy
;
422 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
423 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
425 screen
->get_tex_transfer
= llvmpipe_get_tex_transfer
;
426 screen
->tex_transfer_destroy
= llvmpipe_tex_transfer_destroy
;
427 screen
->transfer_map
= llvmpipe_transfer_map
;
428 screen
->transfer_unmap
= llvmpipe_transfer_unmap
;