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