winsys/sw: Add a software winsys layered on a pipe
[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_flush.h"
44 #include "lp_texture.h"
45 #include "lp_tile_size.h"
46 #include "state_tracker/sw_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 sw_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.tex_usage,
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 if (lpt->base.tex_usage & (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
130 PIPE_TEXTURE_USAGE_SCANOUT |
131 PIPE_TEXTURE_USAGE_SHARED)) {
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 void
149 llvmpipe_texture_destroy(struct pipe_texture *pt)
150 {
151 struct llvmpipe_screen *screen = llvmpipe_screen(pt->screen);
152 struct llvmpipe_texture *lpt = llvmpipe_texture(pt);
153
154 if (lpt->dt) {
155 /* display target */
156 struct sw_winsys *winsys = screen->winsys;
157 winsys->displaytarget_destroy(winsys, lpt->dt);
158 }
159 else {
160 /* regular texture */
161 align_free(lpt->data);
162 }
163
164 FREE(lpt);
165 }
166
167
168 /**
169 * Map a texture. Without any synchronization.
170 */
171 void *
172 llvmpipe_texture_map(struct pipe_texture *texture,
173 unsigned face,
174 unsigned level,
175 unsigned zslice)
176 {
177 struct llvmpipe_texture *lpt = llvmpipe_texture(texture);
178 uint8_t *map;
179
180 if (lpt->dt) {
181 /* display target */
182 struct llvmpipe_screen *screen = llvmpipe_screen(texture->screen);
183 struct sw_winsys *winsys = screen->winsys;
184 const unsigned usage = PIPE_BUFFER_USAGE_CPU_READ_WRITE;
185
186 assert(face == 0);
187 assert(level == 0);
188 assert(zslice == 0);
189
190 /* FIXME: keep map count? */
191 map = winsys->displaytarget_map(winsys, lpt->dt, usage);
192 }
193 else {
194 /* regular texture */
195 unsigned offset;
196 unsigned stride;
197
198 map = lpt->data;
199
200 assert(level < LP_MAX_TEXTURE_2D_LEVELS);
201
202 offset = lpt->level_offset[level];
203 stride = lpt->stride[level];
204
205 /* XXX shouldn't that rather be
206 tex_height = align(u_minify(texture->height0, level), 2)
207 to account for alignment done in llvmpipe_texture_layout ?
208 */
209 if (texture->target == PIPE_TEXTURE_CUBE) {
210 unsigned tex_height = u_minify(texture->height0, level);
211 offset += face * util_format_get_nblocksy(texture->format, tex_height) * stride;
212 }
213 else if (texture->target == PIPE_TEXTURE_3D) {
214 unsigned tex_height = u_minify(texture->height0, level);
215 offset += zslice * util_format_get_nblocksy(texture->format, tex_height) * stride;
216 }
217 else {
218 assert(face == 0);
219 assert(zslice == 0);
220 }
221
222 map += offset;
223 }
224
225 return map;
226 }
227
228
229 /**
230 * Unmap a texture. Without any synchronization.
231 */
232 void
233 llvmpipe_texture_unmap(struct pipe_texture *texture,
234 unsigned face,
235 unsigned level,
236 unsigned zslice)
237 {
238 struct llvmpipe_texture *lpt = llvmpipe_texture(texture);
239
240 if (lpt->dt) {
241 /* display target */
242 struct llvmpipe_screen *lp_screen = llvmpipe_screen(texture->screen);
243 struct sw_winsys *winsys = lp_screen->winsys;
244
245 assert(face == 0);
246 assert(level == 0);
247 assert(zslice == 0);
248
249 winsys->displaytarget_unmap(winsys, lpt->dt);
250 }
251 }
252
253
254 static struct pipe_texture *
255 llvmpipe_texture_from_handle(struct pipe_screen *screen,
256 const struct pipe_texture *template,
257 struct winsys_handle *whandle)
258 {
259 struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
260 struct llvmpipe_texture *lpt = CALLOC_STRUCT(llvmpipe_texture);
261 if (!lpt)
262 return NULL;
263
264 lpt->base = *template;
265 pipe_reference_init(&lpt->base.reference, 1);
266 lpt->base.screen = screen;
267
268 lpt->pot = (util_is_power_of_two(template->width0) &&
269 util_is_power_of_two(template->height0) &&
270 util_is_power_of_two(template->depth0));
271
272 lpt->dt = winsys->displaytarget_from_handle(winsys,
273 template,
274 whandle,
275 &lpt->stride[0]);
276 if (!lpt->dt)
277 goto fail;
278
279 return &lpt->base;
280
281 fail:
282 FREE(lpt);
283 return NULL;
284 }
285
286
287 static boolean
288 llvmpipe_texture_get_handle(struct pipe_screen *screen,
289 struct pipe_texture *pt,
290 struct winsys_handle *whandle)
291 {
292 struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
293 struct llvmpipe_texture *lpt = llvmpipe_texture(pt);
294
295 assert(lpt->dt);
296 if (!lpt->dt)
297 return FALSE;
298
299 return winsys->displaytarget_get_handle(winsys, lpt->dt, whandle);
300 }
301
302
303 static struct pipe_surface *
304 llvmpipe_get_tex_surface(struct pipe_screen *screen,
305 struct pipe_texture *pt,
306 unsigned face, unsigned level, unsigned zslice,
307 unsigned usage)
308 {
309 struct llvmpipe_texture *lpt = llvmpipe_texture(pt);
310 struct pipe_surface *ps;
311
312 assert(level <= pt->last_level);
313
314 ps = CALLOC_STRUCT(pipe_surface);
315 if (ps) {
316 pipe_reference_init(&ps->reference, 1);
317 pipe_texture_reference(&ps->texture, pt);
318 ps->format = pt->format;
319 ps->width = u_minify(pt->width0, level);
320 ps->height = u_minify(pt->height0, level);
321 ps->usage = usage;
322
323 /* Because we are llvmpipe, anything that the state tracker
324 * thought was going to be done with the GPU will actually get
325 * done with the CPU. Let's adjust the flags to take that into
326 * account.
327 */
328 if (ps->usage & PIPE_BUFFER_USAGE_GPU_WRITE) {
329 /* GPU_WRITE means "render" and that can involve reads (blending) */
330 ps->usage |= PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_CPU_READ;
331 }
332
333 if (ps->usage & PIPE_BUFFER_USAGE_GPU_READ)
334 ps->usage |= PIPE_BUFFER_USAGE_CPU_READ;
335
336 if (ps->usage & (PIPE_BUFFER_USAGE_CPU_WRITE |
337 PIPE_BUFFER_USAGE_GPU_WRITE)) {
338 /* Mark the surface as dirty. */
339 lpt->timestamp++;
340 llvmpipe_screen(screen)->timestamp++;
341 }
342
343 ps->face = face;
344 ps->level = level;
345 ps->zslice = zslice;
346 }
347 return ps;
348 }
349
350
351 static void
352 llvmpipe_tex_surface_destroy(struct pipe_surface *surf)
353 {
354 /* Effectively do the texture_update work here - if texture images
355 * needed post-processing to put them into hardware layout, this is
356 * where it would happen. For llvmpipe, nothing to do.
357 */
358 assert(surf->texture);
359 pipe_texture_reference(&surf->texture, NULL);
360 FREE(surf);
361 }
362
363
364 static struct pipe_transfer *
365 llvmpipe_get_tex_transfer(struct pipe_context *pipe,
366 struct pipe_texture *texture,
367 unsigned face, unsigned level, unsigned zslice,
368 enum pipe_transfer_usage usage,
369 unsigned x, unsigned y, unsigned w, unsigned h)
370 {
371 struct llvmpipe_texture *lptex = llvmpipe_texture(texture);
372 struct llvmpipe_transfer *lpt;
373
374 assert(texture);
375 assert(level <= texture->last_level);
376
377 lpt = CALLOC_STRUCT(llvmpipe_transfer);
378 if (lpt) {
379 struct pipe_transfer *pt = &lpt->base;
380 pipe_texture_reference(&pt->texture, texture);
381 pt->x = x;
382 pt->y = y;
383 pt->width = align(w, TILE_SIZE);
384 pt->height = align(h, TILE_SIZE);
385 pt->stride = lptex->stride[level];
386 pt->usage = usage;
387 pt->face = face;
388 pt->level = level;
389 pt->zslice = zslice;
390
391 return pt;
392 }
393 return NULL;
394 }
395
396
397 static void
398 llvmpipe_tex_transfer_destroy(struct pipe_context *pipe,
399 struct pipe_transfer *transfer)
400 {
401 /* Effectively do the texture_update work here - if texture images
402 * needed post-processing to put them into hardware layout, this is
403 * where it would happen. For llvmpipe, nothing to do.
404 */
405 assert (transfer->texture);
406 pipe_texture_reference(&transfer->texture, NULL);
407 FREE(transfer);
408 }
409
410
411 static void *
412 llvmpipe_transfer_map( struct pipe_context *pipe,
413 struct pipe_transfer *transfer )
414 {
415 struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
416 ubyte *map;
417 struct llvmpipe_texture *lpt;
418 enum pipe_format format;
419
420 assert(transfer->texture);
421 lpt = llvmpipe_texture(transfer->texture);
422 format = lpt->base.format;
423
424 /*
425 * Transfers, like other pipe operations, must happen in order, so flush the
426 * context if necessary.
427 */
428 llvmpipe_flush_texture(pipe,
429 transfer->texture, transfer->face, transfer->level,
430 0, /* flush_flags */
431 !(transfer->usage & PIPE_TRANSFER_WRITE), /* read_only */
432 TRUE, /* cpu_access */
433 FALSE); /* do_not_flush */
434
435 map = llvmpipe_texture_map(transfer->texture,
436 transfer->face, transfer->level, transfer->zslice);
437
438 /* May want to different things here depending on read/write nature
439 * of the map:
440 */
441 if (transfer->usage & PIPE_TRANSFER_WRITE) {
442 /* Do something to notify sharing contexts of a texture change.
443 */
444 screen->timestamp++;
445 }
446
447 map +=
448 transfer->y / util_format_get_blockheight(format) * transfer->stride +
449 transfer->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
450
451 return map;
452 }
453
454
455 static void
456 llvmpipe_transfer_unmap(struct pipe_context *pipe,
457 struct pipe_transfer *transfer)
458 {
459 assert(transfer->texture);
460
461 llvmpipe_texture_unmap(transfer->texture,
462 transfer->face, transfer->level, transfer->zslice);
463 }
464
465
466 void
467 llvmpipe_init_screen_texture_funcs(struct pipe_screen *screen)
468 {
469 screen->texture_create = llvmpipe_texture_create;
470 screen->texture_destroy = llvmpipe_texture_destroy;
471 screen->texture_get_handle = llvmpipe_texture_get_handle;
472
473 screen->get_tex_surface = llvmpipe_get_tex_surface;
474 screen->tex_surface_destroy = llvmpipe_tex_surface_destroy;
475 }
476
477
478 void
479 llvmpipe_init_context_texture_funcs(struct pipe_context *pipe)
480 {
481 pipe->get_tex_transfer = llvmpipe_get_tex_transfer;
482 pipe->tex_transfer_destroy = llvmpipe_tex_transfer_destroy;
483 pipe->transfer_map = llvmpipe_transfer_map;
484 pipe->transfer_unmap = llvmpipe_transfer_unmap;
485 }