Merge branch 'mesa_7_6_branch' of git+ssh://agd5f@git.freedesktop.org/git/mesa/mesa
[mesa.git] / src / gallium / drivers / llvmpipe / lp_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
31 */
32
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"
39
40 #include "lp_context.h"
41 #include "lp_state.h"
42 #include "lp_texture.h"
43 #include "lp_tex_cache.h"
44 #include "lp_screen.h"
45 #include "lp_winsys.h"
46
47
48 /* Simple, maximally packed layout.
49 */
50
51
52 /* Conventional allocation path for non-display textures:
53 */
54 static boolean
55 llvmpipe_texture_layout(struct llvmpipe_screen *screen,
56 struct llvmpipe_texture * lpt)
57 {
58 struct pipe_texture *pt = &lpt->base;
59 unsigned level;
60 unsigned width = pt->width[0];
61 unsigned height = pt->height[0];
62 unsigned depth = pt->depth[0];
63
64 unsigned buffer_size = 0;
65
66 pf_get_block(lpt->base.format, &lpt->base.block);
67
68 for (level = 0; level <= pt->last_level; level++) {
69 unsigned nblocksx, nblocksy;
70
71 pt->width[level] = width;
72 pt->height[level] = height;
73 pt->depth[level] = depth;
74 pt->nblocksx[level] = pf_get_nblocksx(&pt->block, width);
75 pt->nblocksy[level] = pf_get_nblocksy(&pt->block, height);
76
77 /* Allocate storage for whole quads. This is particularly important
78 * for depth surfaces, which are currently stored in a swizzled format. */
79 nblocksx = pf_get_nblocksx(&pt->block, align(width, 2));
80 nblocksy = pf_get_nblocksy(&pt->block, align(height, 2));
81
82 lpt->stride[level] = align(nblocksx*pt->block.size, 16);
83
84 lpt->level_offset[level] = buffer_size;
85
86 buffer_size += (nblocksy *
87 ((pt->target == PIPE_TEXTURE_CUBE) ? 6 : depth) *
88 lpt->stride[level]);
89
90 width = minify(width);
91 height = minify(height);
92 depth = minify(depth);
93 }
94
95 lpt->data = align_malloc(buffer_size, 16);
96
97 return lpt->data != NULL;
98 }
99
100 static boolean
101 llvmpipe_displaytarget_layout(struct llvmpipe_screen *screen,
102 struct llvmpipe_texture * lpt)
103 {
104 struct llvmpipe_winsys *winsys = screen->winsys;
105
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]);
109
110 lpt->dt = winsys->displaytarget_create(winsys,
111 lpt->base.format,
112 lpt->base.width[0],
113 lpt->base.height[0],
114 16,
115 &lpt->stride[0] );
116
117 return lpt->dt != NULL;
118 }
119
120
121
122
123
124 static struct pipe_texture *
125 llvmpipe_texture_create(struct pipe_screen *_screen,
126 const struct pipe_texture *templat)
127 {
128 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
129 struct llvmpipe_texture *lpt = CALLOC_STRUCT(llvmpipe_texture);
130 if (!lpt)
131 return NULL;
132
133 lpt->base = *templat;
134 pipe_reference_init(&lpt->base.reference, 1);
135 lpt->base.screen = &screen->base;
136
137 /* XXX: The xlib state tracker is brain-dead and will request
138 * PIPE_FORMAT_Z16_UNORM no matter how much we tell it we don't support it.
139 */
140 if(lpt->base.format == PIPE_FORMAT_Z16_UNORM)
141 lpt->base.format = PIPE_FORMAT_Z32_UNORM;
142
143 if (lpt->base.tex_usage & (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
144 PIPE_TEXTURE_USAGE_PRIMARY)) {
145 if (!llvmpipe_displaytarget_layout(screen, lpt))
146 goto fail;
147 }
148 else {
149 if (!llvmpipe_texture_layout(screen, lpt))
150 goto fail;
151 }
152
153 return &lpt->base;
154
155 fail:
156 FREE(lpt);
157 return NULL;
158 }
159
160
161 static struct pipe_texture *
162 llvmpipe_texture_blanket(struct pipe_screen * screen,
163 const struct pipe_texture *base,
164 const unsigned *stride,
165 struct pipe_buffer *buffer)
166 {
167 /* FIXME */
168 #if 0
169 struct llvmpipe_texture *lpt;
170 assert(screen);
171
172 /* Only supports one type */
173 if (base->target != PIPE_TEXTURE_2D ||
174 base->last_level != 0 ||
175 base->depth[0] != 1) {
176 return NULL;
177 }
178
179 lpt = CALLOC_STRUCT(llvmpipe_texture);
180 if (!lpt)
181 return NULL;
182
183 lpt->base = *base;
184 pipe_reference_init(&lpt->base.reference, 1);
185 lpt->base.screen = screen;
186 lpt->base.nblocksx[0] = pf_get_nblocksx(&lpt->base.block, lpt->base.width[0]);
187 lpt->base.nblocksy[0] = pf_get_nblocksy(&lpt->base.block, lpt->base.height[0]);
188 lpt->stride[0] = stride[0];
189
190 pipe_buffer_reference(&lpt->buffer, buffer);
191
192 return &lpt->base;
193 #else
194 return NULL;
195 #endif
196 }
197
198
199 static void
200 llvmpipe_texture_destroy(struct pipe_texture *pt)
201 {
202 struct llvmpipe_screen *screen = llvmpipe_screen(pt->screen);
203 struct llvmpipe_texture *lpt = llvmpipe_texture(pt);
204
205 if(lpt->dt) {
206 struct llvmpipe_winsys *winsys = screen->winsys;
207 winsys->displaytarget_destroy(winsys, lpt->dt);
208 }
209 else
210 align_free(lpt->data);
211
212 FREE(lpt);
213 }
214
215
216 static struct pipe_surface *
217 llvmpipe_get_tex_surface(struct pipe_screen *screen,
218 struct pipe_texture *pt,
219 unsigned face, unsigned level, unsigned zslice,
220 unsigned usage)
221 {
222 struct llvmpipe_texture *lpt = llvmpipe_texture(pt);
223 struct pipe_surface *ps;
224
225 assert(level <= pt->last_level);
226
227 ps = CALLOC_STRUCT(pipe_surface);
228 if (ps) {
229 pipe_reference_init(&ps->reference, 1);
230 pipe_texture_reference(&ps->texture, pt);
231 ps->format = pt->format;
232 ps->width = pt->width[level];
233 ps->height = pt->height[level];
234 ps->offset = lpt->level_offset[level];
235 ps->usage = usage;
236
237 /* Because we are llvmpipe, anything that the state tracker
238 * thought was going to be done with the GPU will actually get
239 * done with the CPU. Let's adjust the flags to take that into
240 * account.
241 */
242 if (ps->usage & PIPE_BUFFER_USAGE_GPU_WRITE) {
243 /* GPU_WRITE means "render" and that can involve reads (blending) */
244 ps->usage |= PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_CPU_READ;
245 }
246
247 if (ps->usage & PIPE_BUFFER_USAGE_GPU_READ)
248 ps->usage |= PIPE_BUFFER_USAGE_CPU_READ;
249
250 if (ps->usage & (PIPE_BUFFER_USAGE_CPU_WRITE |
251 PIPE_BUFFER_USAGE_GPU_WRITE)) {
252 /* Mark the surface as dirty. The tile cache will look for this. */
253 lpt->timestamp++;
254 llvmpipe_screen(screen)->timestamp++;
255 }
256
257 ps->face = face;
258 ps->level = level;
259 ps->zslice = zslice;
260
261 if (pt->target == PIPE_TEXTURE_CUBE) {
262 ps->offset += face * pt->nblocksy[level] * lpt->stride[level];
263 }
264 else if (pt->target == PIPE_TEXTURE_3D) {
265 ps->offset += zslice * pt->nblocksy[level] * lpt->stride[level];
266 }
267 else {
268 assert(face == 0);
269 assert(zslice == 0);
270 }
271 }
272 return ps;
273 }
274
275
276 static void
277 llvmpipe_tex_surface_destroy(struct pipe_surface *surf)
278 {
279 /* Effectively do the texture_update work here - if texture images
280 * needed post-processing to put them into hardware layout, this is
281 * where it would happen. For llvmpipe, nothing to do.
282 */
283 assert(surf->texture);
284 pipe_texture_reference(&surf->texture, NULL);
285 FREE(surf);
286 }
287
288
289 static struct pipe_transfer *
290 llvmpipe_get_tex_transfer(struct pipe_screen *screen,
291 struct pipe_texture *texture,
292 unsigned face, unsigned level, unsigned zslice,
293 enum pipe_transfer_usage usage,
294 unsigned x, unsigned y, unsigned w, unsigned h)
295 {
296 struct llvmpipe_texture *lptex = llvmpipe_texture(texture);
297 struct llvmpipe_transfer *lpt;
298
299 assert(texture);
300 assert(level <= texture->last_level);
301
302 lpt = CALLOC_STRUCT(llvmpipe_transfer);
303 if (lpt) {
304 struct pipe_transfer *pt = &lpt->base;
305 pipe_texture_reference(&pt->texture, texture);
306 pt->format = texture->format;
307 pt->block = texture->block;
308 pt->x = x;
309 pt->y = y;
310 pt->width = w;
311 pt->height = h;
312 pt->nblocksx = texture->nblocksx[level];
313 pt->nblocksy = texture->nblocksy[level];
314 pt->stride = lptex->stride[level];
315 pt->usage = usage;
316 pt->face = face;
317 pt->level = level;
318 pt->zslice = zslice;
319
320 lpt->offset = lptex->level_offset[level];
321
322 if (texture->target == PIPE_TEXTURE_CUBE) {
323 lpt->offset += face * pt->nblocksy * pt->stride;
324 }
325 else if (texture->target == PIPE_TEXTURE_3D) {
326 lpt->offset += zslice * pt->nblocksy * pt->stride;
327 }
328 else {
329 assert(face == 0);
330 assert(zslice == 0);
331 }
332 return pt;
333 }
334 return NULL;
335 }
336
337
338 static void
339 llvmpipe_tex_transfer_destroy(struct pipe_transfer *transfer)
340 {
341 /* Effectively do the texture_update work here - if texture images
342 * needed post-processing to put them into hardware layout, this is
343 * where it would happen. For llvmpipe, nothing to do.
344 */
345 assert (transfer->texture);
346 pipe_texture_reference(&transfer->texture, NULL);
347 FREE(transfer);
348 }
349
350
351 static void *
352 llvmpipe_transfer_map( struct pipe_screen *_screen,
353 struct pipe_transfer *transfer )
354 {
355 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
356 ubyte *map, *xfer_map;
357 struct llvmpipe_texture *lpt;
358
359 assert(transfer->texture);
360 lpt = llvmpipe_texture(transfer->texture);
361
362 if(lpt->dt) {
363 struct llvmpipe_winsys *winsys = screen->winsys;
364
365 map = winsys->displaytarget_map(winsys, lpt->dt,
366 pipe_transfer_buffer_flags(transfer));
367 if (map == NULL)
368 return NULL;
369 }
370 else
371 map = lpt->data;
372
373 /* May want to different things here depending on read/write nature
374 * of the map:
375 */
376 if (transfer->texture && (transfer->usage & PIPE_TRANSFER_WRITE))
377 {
378 /* Do something to notify sharing contexts of a texture change.
379 * In llvmpipe, that would mean flushing the texture cache.
380 */
381 screen->timestamp++;
382 }
383
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);*/
388 return xfer_map;
389 }
390
391
392 static void
393 llvmpipe_transfer_unmap(struct pipe_screen *_screen,
394 struct pipe_transfer *transfer)
395 {
396 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
397 struct llvmpipe_texture *lpt;
398
399 assert(transfer->texture);
400 lpt = llvmpipe_texture(transfer->texture);
401
402 if(lpt->dt) {
403 struct llvmpipe_winsys *winsys = screen->winsys;
404 winsys->displaytarget_unmap(winsys, lpt->dt);
405 }
406 }
407
408
409 void
410 llvmpipe_init_texture_funcs(struct llvmpipe_context *lp)
411 {
412 }
413
414
415 void
416 llvmpipe_init_screen_texture_funcs(struct pipe_screen *screen)
417 {
418 screen->texture_create = llvmpipe_texture_create;
419 screen->texture_blanket = llvmpipe_texture_blanket;
420 screen->texture_destroy = llvmpipe_texture_destroy;
421
422 screen->get_tex_surface = llvmpipe_get_tex_surface;
423 screen->tex_surface_destroy = llvmpipe_tex_surface_destroy;
424
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;
429 }