llvmpipe: align display target size to multiple of tile size
[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_screen.h"
44 #include "lp_state.h"
45 #include "lp_texture.h"
46 #include "lp_tile_size.h"
47 #include "lp_winsys.h"
48
49
50 /**
51 * Conventional allocation path for non-display textures:
52 * Simple, maximally packed layout.
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 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 */
71 nblocksx = util_format_get_nblocksx(pt->format, align(width, TILE_SIZE));
72 nblocksy = util_format_get_nblocksy(pt->format, align(height, TILE_SIZE));
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
93
94 static boolean
95 llvmpipe_displaytarget_layout(struct llvmpipe_screen *screen,
96 struct llvmpipe_texture *lpt)
97 {
98 struct llvmpipe_winsys *winsys = screen->winsys;
99
100 /* Round up the surface size to a multiple of the tile size to
101 * avoid tile clipping.
102 */
103 unsigned width = align(lpt->base.width0, TILE_SIZE);
104 unsigned height = align(lpt->base.height0, TILE_SIZE);
105
106 lpt->dt = winsys->displaytarget_create(winsys,
107 lpt->base.format,
108 width, height,
109 16,
110 &lpt->stride[0] );
111
112 return lpt->dt != NULL;
113 }
114
115
116 static struct pipe_texture *
117 llvmpipe_texture_create(struct pipe_screen *_screen,
118 const struct pipe_texture *templat)
119 {
120 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
121 struct llvmpipe_texture *lpt = CALLOC_STRUCT(llvmpipe_texture);
122 if (!lpt)
123 return NULL;
124
125 lpt->base = *templat;
126 pipe_reference_init(&lpt->base.reference, 1);
127 lpt->base.screen = &screen->base;
128
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.
131 */
132 if (lpt->base.format == PIPE_FORMAT_Z16_UNORM)
133 lpt->base.format = PIPE_FORMAT_Z32_UNORM;
134
135 if (lpt->base.tex_usage & (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
136 PIPE_TEXTURE_USAGE_PRIMARY)) {
137 if (!llvmpipe_displaytarget_layout(screen, lpt))
138 goto fail;
139 }
140 else {
141 if (!llvmpipe_texture_layout(screen, lpt))
142 goto fail;
143 }
144
145 return &lpt->base;
146
147 fail:
148 FREE(lpt);
149 return NULL;
150 }
151
152
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)
158 {
159 /* FIXME */
160 #if 0
161 struct llvmpipe_texture *lpt;
162 assert(screen);
163
164 /* Only supports one type */
165 if (base->target != PIPE_TEXTURE_2D ||
166 base->last_level != 0 ||
167 base->depth0 != 1) {
168 return NULL;
169 }
170
171 lpt = CALLOC_STRUCT(llvmpipe_texture);
172 if (!lpt)
173 return NULL;
174
175 lpt->base = *base;
176 pipe_reference_init(&lpt->base.reference, 1);
177 lpt->base.screen = screen;
178 lpt->stride[0] = stride[0];
179
180 pipe_buffer_reference(&lpt->buffer, buffer);
181
182 return &lpt->base;
183 #else
184 debug_printf("llvmpipe_texture_blanket() not implemented!");
185 return NULL;
186 #endif
187 }
188
189
190 static void
191 llvmpipe_texture_destroy(struct pipe_texture *pt)
192 {
193 struct llvmpipe_screen *screen = llvmpipe_screen(pt->screen);
194 struct llvmpipe_texture *lpt = llvmpipe_texture(pt);
195
196 if (lpt->dt) {
197 /* display target */
198 struct llvmpipe_winsys *winsys = screen->winsys;
199 winsys->displaytarget_destroy(winsys, lpt->dt);
200 }
201 else {
202 /* regular texture */
203 align_free(lpt->data);
204 }
205
206 FREE(lpt);
207 }
208
209
210 static struct pipe_surface *
211 llvmpipe_get_tex_surface(struct pipe_screen *screen,
212 struct pipe_texture *pt,
213 unsigned face, unsigned level, unsigned zslice,
214 unsigned usage)
215 {
216 struct llvmpipe_texture *lpt = llvmpipe_texture(pt);
217 struct pipe_surface *ps;
218
219 assert(level <= pt->last_level);
220
221 ps = CALLOC_STRUCT(pipe_surface);
222 if (ps) {
223 pipe_reference_init(&ps->reference, 1);
224 pipe_texture_reference(&ps->texture, pt);
225 ps->format = pt->format;
226 ps->width = u_minify(pt->width0, level);
227 ps->height = u_minify(pt->height0, level);
228 ps->offset = lpt->level_offset[level];
229 ps->usage = usage;
230
231 /* Because we are llvmpipe, anything that the state tracker
232 * thought was going to be done with the GPU will actually get
233 * done with the CPU. Let's adjust the flags to take that into
234 * account.
235 */
236 if (ps->usage & PIPE_BUFFER_USAGE_GPU_WRITE) {
237 /* GPU_WRITE means "render" and that can involve reads (blending) */
238 ps->usage |= PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_CPU_READ;
239 }
240
241 if (ps->usage & PIPE_BUFFER_USAGE_GPU_READ)
242 ps->usage |= PIPE_BUFFER_USAGE_CPU_READ;
243
244 if (ps->usage & (PIPE_BUFFER_USAGE_CPU_WRITE |
245 PIPE_BUFFER_USAGE_GPU_WRITE)) {
246 /* Mark the surface as dirty. */
247 lpt->timestamp++;
248 llvmpipe_screen(screen)->timestamp++;
249 }
250
251 ps->face = face;
252 ps->level = level;
253 ps->zslice = zslice;
254
255 /* XXX shouldn't that rather be
256 tex_height = align(ps->height, 2);
257 to account for alignment done in llvmpipe_texture_layout ?
258 */
259 if (pt->target == PIPE_TEXTURE_CUBE) {
260 unsigned tex_height = ps->height;
261 ps->offset += face * util_format_get_nblocksy(pt->format, tex_height) * lpt->stride[level];
262 }
263 else if (pt->target == PIPE_TEXTURE_3D) {
264 unsigned tex_height = ps->height;
265 ps->offset += zslice * util_format_get_nblocksy(pt->format, tex_height) * 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->x = x;
307 pt->y = y;
308 pt->width = align(w, TILE_SIZE);
309 pt->height = align(h, TILE_SIZE);
310 pt->stride = lptex->stride[level];
311 pt->usage = usage;
312 pt->face = face;
313 pt->level = level;
314 pt->zslice = zslice;
315
316 lpt->offset = lptex->level_offset[level];
317
318 /* XXX shouldn't that rather be
319 tex_height = align(u_minify(texture->height0, level), 2)
320 to account for alignment done in llvmpipe_texture_layout ?
321 */
322 if (texture->target == PIPE_TEXTURE_CUBE) {
323 unsigned tex_height = u_minify(texture->height0, level);
324 lpt->offset += face * util_format_get_nblocksy(texture->format, tex_height) * pt->stride;
325 }
326 else if (texture->target == PIPE_TEXTURE_3D) {
327 unsigned tex_height = u_minify(texture->height0, level);
328 lpt->offset += zslice * util_format_get_nblocksy(texture->format, tex_height) * pt->stride;
329 }
330 else {
331 assert(face == 0);
332 assert(zslice == 0);
333 }
334 return pt;
335 }
336 return NULL;
337 }
338
339
340 static void
341 llvmpipe_tex_transfer_destroy(struct pipe_transfer *transfer)
342 {
343 /* Effectively do the texture_update work here - if texture images
344 * needed post-processing to put them into hardware layout, this is
345 * where it would happen. For llvmpipe, nothing to do.
346 */
347 assert (transfer->texture);
348 pipe_texture_reference(&transfer->texture, NULL);
349 FREE(transfer);
350 }
351
352
353 static void *
354 llvmpipe_transfer_map( struct pipe_screen *_screen,
355 struct pipe_transfer *transfer )
356 {
357 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
358 ubyte *map, *xfer_map;
359 struct llvmpipe_texture *lpt;
360 enum pipe_format format;
361
362 assert(transfer->texture);
363 lpt = llvmpipe_texture(transfer->texture);
364 format = lpt->base.format;
365
366 if (lpt->dt) {
367 /* display target */
368 struct llvmpipe_winsys *winsys = screen->winsys;
369
370 map = winsys->displaytarget_map(winsys, lpt->dt,
371 pipe_transfer_buffer_flags(transfer));
372 if (map == NULL)
373 return NULL;
374 }
375 else {
376 /* regular texture */
377 map = lpt->data;
378 }
379
380 /* May want to different things here depending on read/write nature
381 * of the map:
382 */
383 if (transfer->texture && (transfer->usage & PIPE_TRANSFER_WRITE)) {
384 /* Do something to notify sharing contexts of a texture change.
385 */
386 screen->timestamp++;
387 }
388
389 xfer_map = map + llvmpipe_transfer(transfer)->offset +
390 transfer->y / util_format_get_blockheight(format) * transfer->stride +
391 transfer->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
392 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
393 return xfer_map;
394 }
395
396
397 static void
398 llvmpipe_transfer_unmap(struct pipe_screen *screen,
399 struct pipe_transfer *transfer)
400 {
401 struct llvmpipe_screen *lp_screen = llvmpipe_screen(screen);
402 struct llvmpipe_texture *lpt;
403
404 assert(transfer->texture);
405 lpt = llvmpipe_texture(transfer->texture);
406
407 if (lpt->dt) {
408 /* display target */
409 struct llvmpipe_winsys *winsys = lp_screen->winsys;
410 winsys->displaytarget_unmap(winsys, lpt->dt);
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 }