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