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