Merge remote branch 'vdpau/pipe-video' into pipe-video
[mesa.git] / src / gallium / drivers / softpipe / sp_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_defines.h"
34 #include "util/u_inlines.h"
35
36 #include "util/u_format.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
39 #include "util/u_transfer.h"
40
41 #include "sp_context.h"
42 #include "sp_flush.h"
43 #include "sp_texture.h"
44 #include "sp_screen.h"
45
46 #include "state_tracker/sw_winsys.h"
47
48
49 /**
50 * Conventional allocation path for non-display textures:
51 * Use a simple, maximally packed layout.
52 */
53 static boolean
54 softpipe_resource_layout(struct pipe_screen *screen,
55 struct softpipe_resource *spr)
56 {
57 struct pipe_resource *pt = &spr->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 spr->stride[level] = util_format_get_stride(pt->format, width);
66
67 spr->level_offset[level] = buffer_size;
68
69 buffer_size += (util_format_get_nblocksy(pt->format, height) *
70 ((pt->target == PIPE_TEXTURE_CUBE) ? 6 : depth) *
71 spr->stride[level]);
72
73 width = u_minify(width, 1);
74 height = u_minify(height, 1);
75 depth = u_minify(depth, 1);
76 }
77
78 spr->data = align_malloc(buffer_size, 16);
79
80 return spr->data != NULL;
81 }
82
83
84 /**
85 * Texture layout for simple color buffers.
86 */
87 static boolean
88 softpipe_displaytarget_layout(struct pipe_screen *screen,
89 struct softpipe_resource *spr)
90 {
91 struct sw_winsys *winsys = softpipe_screen(screen)->winsys;
92
93 /* Round up the surface size to a multiple of the tile size?
94 */
95 spr->dt = winsys->displaytarget_create(winsys,
96 spr->base.bind,
97 spr->base.format,
98 spr->base.width0,
99 spr->base.height0,
100 16,
101 &spr->stride[0] );
102
103 return spr->dt != NULL;
104 }
105
106
107 /**
108 * Create new pipe_resource given the template information.
109 */
110 static struct pipe_resource *
111 softpipe_resource_create(struct pipe_screen *screen,
112 const struct pipe_resource *templat)
113 {
114 struct softpipe_resource *spr = CALLOC_STRUCT(softpipe_resource);
115 if (!spr)
116 return NULL;
117
118 assert(templat->format != PIPE_FORMAT_NONE);
119
120 spr->base = *templat;
121 pipe_reference_init(&spr->base.reference, 1);
122 spr->base.screen = screen;
123
124 spr->pot = (util_is_power_of_two(templat->width0) &&
125 util_is_power_of_two(templat->height0) &&
126 util_is_power_of_two(templat->depth0));
127
128 if (spr->base.bind & (PIPE_BIND_DISPLAY_TARGET |
129 PIPE_BIND_SCANOUT |
130 PIPE_BIND_SHARED)) {
131 if (!softpipe_displaytarget_layout(screen, spr))
132 goto fail;
133 }
134 else {
135 if (!softpipe_resource_layout(screen, spr))
136 goto fail;
137 }
138
139 return &spr->base;
140
141 fail:
142 FREE(spr);
143 return NULL;
144 }
145
146
147 static void
148 softpipe_resource_destroy(struct pipe_screen *pscreen,
149 struct pipe_resource *pt)
150 {
151 struct softpipe_screen *screen = softpipe_screen(pscreen);
152 struct softpipe_resource *spr = softpipe_resource(pt);
153
154 if (spr->dt) {
155 /* display target */
156 struct sw_winsys *winsys = screen->winsys;
157 winsys->displaytarget_destroy(winsys, spr->dt);
158 }
159 else if (!spr->userBuffer) {
160 /* regular texture */
161 align_free(spr->data);
162 }
163
164 FREE(spr);
165 }
166
167
168 static struct pipe_resource *
169 softpipe_resource_from_handle(struct pipe_screen *screen,
170 const struct pipe_resource *templat,
171 struct winsys_handle *whandle)
172 {
173 struct sw_winsys *winsys = softpipe_screen(screen)->winsys;
174 struct softpipe_resource *spr = CALLOC_STRUCT(softpipe_resource);
175 if (!spr)
176 return NULL;
177
178 spr->base = *templat;
179 pipe_reference_init(&spr->base.reference, 1);
180 spr->base.screen = screen;
181
182 spr->pot = (util_is_power_of_two(templat->width0) &&
183 util_is_power_of_two(templat->height0) &&
184 util_is_power_of_two(templat->depth0));
185
186 spr->dt = winsys->displaytarget_from_handle(winsys,
187 templat,
188 whandle,
189 &spr->stride[0]);
190 if (!spr->dt)
191 goto fail;
192
193 return &spr->base;
194
195 fail:
196 FREE(spr);
197 return NULL;
198 }
199
200
201 static boolean
202 softpipe_resource_get_handle(struct pipe_screen *screen,
203 struct pipe_resource *pt,
204 struct winsys_handle *whandle)
205 {
206 struct sw_winsys *winsys = softpipe_screen(screen)->winsys;
207 struct softpipe_resource *spr = softpipe_resource(pt);
208
209 assert(spr->dt);
210 if (!spr->dt)
211 return FALSE;
212
213 return winsys->displaytarget_get_handle(winsys, spr->dt, whandle);
214 }
215
216
217 /**
218 * Helper function to compute offset (in bytes) for a particular
219 * texture level/face/slice from the start of the buffer.
220 */
221 static unsigned
222 sp_get_tex_image_offset(const struct softpipe_resource *spr,
223 unsigned level, unsigned layer)
224 {
225 const unsigned hgt = u_minify(spr->base.height0, level);
226 const unsigned nblocksy = util_format_get_nblocksy(spr->base.format, hgt);
227 unsigned offset = spr->level_offset[level];
228
229 if (spr->base.target == PIPE_TEXTURE_CUBE ||
230 spr->base.target == PIPE_TEXTURE_3D) {
231 offset += layer * nblocksy * spr->stride[level];
232 }
233 else {
234 assert(layer == 0);
235 }
236
237 return offset;
238 }
239
240
241 /**
242 * Get a pipe_surface "view" into a texture resource.
243 */
244 static struct pipe_surface *
245 softpipe_create_surface(struct pipe_context *pipe,
246 struct pipe_resource *pt,
247 const struct pipe_surface *surf_tmpl)
248 {
249 struct pipe_surface *ps;
250 unsigned level = surf_tmpl->u.tex.level;
251
252 assert(level <= pt->last_level);
253 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
254
255 ps = CALLOC_STRUCT(pipe_surface);
256 if (ps) {
257 pipe_reference_init(&ps->reference, 1);
258 pipe_resource_reference(&ps->texture, pt);
259 ps->context = pipe;
260 ps->format = surf_tmpl->format;
261 ps->width = u_minify(pt->width0, level);
262 ps->height = u_minify(pt->height0, level);
263 ps->usage = surf_tmpl->usage;
264
265 ps->u.tex.level = level;
266 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
267 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
268 }
269 return ps;
270 }
271
272
273 /**
274 * Free a pipe_surface which was created with softpipe_create_surface().
275 */
276 static void
277 softpipe_surface_destroy(struct pipe_context *pipe,
278 struct pipe_surface *surf)
279 {
280 /* Effectively do the texture_update work here - if texture images
281 * needed post-processing to put them into hardware layout, this is
282 * where it would happen. For softpipe, nothing to do.
283 */
284 assert(surf->texture);
285 pipe_resource_reference(&surf->texture, NULL);
286 FREE(surf);
287 }
288
289
290 /**
291 * Geta pipe_transfer object which is used for moving data in/out of
292 * a resource object.
293 * \param pipe rendering context
294 * \param resource the resource to transfer in/out of
295 * \param sr indicates cube face or 3D texture slice
296 * \param usage bitmask of PIPE_TRANSFER_x flags
297 * \param box the 1D/2D/3D region of interest
298 */
299 static struct pipe_transfer *
300 softpipe_get_transfer(struct pipe_context *pipe,
301 struct pipe_resource *resource,
302 unsigned level,
303 unsigned usage,
304 const struct pipe_box *box)
305 {
306 struct softpipe_resource *spr = softpipe_resource(resource);
307 struct softpipe_transfer *spt;
308
309 assert(resource);
310 assert(level <= resource->last_level);
311
312 /* make sure the requested region is in the image bounds */
313 assert(box->x + box->width <= u_minify(resource->width0, level));
314 assert(box->y + box->height <= u_minify(resource->height0, level));
315 assert(box->z + box->depth <= (u_minify(resource->depth0, level) + resource->array_size - 1));
316
317 /*
318 * Transfers, like other pipe operations, must happen in order, so flush the
319 * context if necessary.
320 */
321 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
322 boolean read_only = !(usage & PIPE_TRANSFER_WRITE);
323 boolean do_not_block = !!(usage & PIPE_TRANSFER_DONTBLOCK);
324 if (!softpipe_flush_resource(pipe, resource,
325 level, box->depth > 1 ? -1 : box->z,
326 0, /* flush_flags */
327 read_only,
328 TRUE, /* cpu_access */
329 do_not_block)) {
330 /*
331 * It would have blocked, but state tracker requested no to.
332 */
333 assert(do_not_block);
334 return NULL;
335 }
336 }
337
338 spt = CALLOC_STRUCT(softpipe_transfer);
339 if (spt) {
340 struct pipe_transfer *pt = &spt->base;
341 enum pipe_format format = resource->format;
342 const unsigned hgt = u_minify(spr->base.height0, level);
343 const unsigned nblocksy = util_format_get_nblocksy(format, hgt);
344
345 pipe_resource_reference(&pt->resource, resource);
346 pt->level = level;
347 pt->usage = usage;
348 pt->box = *box;
349 pt->stride = spr->stride[level];
350 pt->layer_stride = pt->stride * nblocksy;
351
352 spt->offset = sp_get_tex_image_offset(spr, level, box->z);
353
354 spt->offset +=
355 box->y / util_format_get_blockheight(format) * spt->base.stride +
356 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
357
358 return pt;
359 }
360 return NULL;
361 }
362
363
364 /**
365 * Free a pipe_transfer object which was created with
366 * softpipe_get_transfer().
367 */
368 static void
369 softpipe_transfer_destroy(struct pipe_context *pipe,
370 struct pipe_transfer *transfer)
371 {
372 pipe_resource_reference(&transfer->resource, NULL);
373 FREE(transfer);
374 }
375
376
377 /**
378 * Create memory mapping for given pipe_transfer object.
379 */
380 static void *
381 softpipe_transfer_map(struct pipe_context *pipe,
382 struct pipe_transfer *transfer)
383 {
384 struct softpipe_transfer *spt = softpipe_transfer(transfer);
385 struct softpipe_resource *spr = softpipe_resource(transfer->resource);
386 struct sw_winsys *winsys = softpipe_screen(pipe->screen)->winsys;
387 uint8_t *map;
388
389 /* resources backed by display target treated specially:
390 */
391 if (spr->dt) {
392 map = winsys->displaytarget_map(winsys, spr->dt, transfer->usage);
393 }
394 else {
395 map = spr->data;
396 }
397
398 if (map == NULL)
399 return NULL;
400 else
401 return map + spt->offset;
402 }
403
404
405 /**
406 * Unmap memory mapping for given pipe_transfer object.
407 */
408 static void
409 softpipe_transfer_unmap(struct pipe_context *pipe,
410 struct pipe_transfer *transfer)
411 {
412 struct softpipe_resource *spr;
413
414 assert(transfer->resource);
415 spr = softpipe_resource(transfer->resource);
416
417 if (spr->dt) {
418 /* display target */
419 struct sw_winsys *winsys = softpipe_screen(pipe->screen)->winsys;
420 winsys->displaytarget_unmap(winsys, spr->dt);
421 }
422
423 if (transfer->usage & PIPE_TRANSFER_WRITE) {
424 /* Mark the texture as dirty to expire the tile caches. */
425 spr->timestamp++;
426 }
427 }
428
429 /**
430 * Create buffer which wraps user-space data.
431 */
432 static struct pipe_resource *
433 softpipe_user_buffer_create(struct pipe_screen *screen,
434 void *ptr,
435 unsigned bytes,
436 unsigned bind_flags)
437 {
438 struct softpipe_resource *spr;
439
440 spr = CALLOC_STRUCT(softpipe_resource);
441 if (!spr)
442 return NULL;
443
444 pipe_reference_init(&spr->base.reference, 1);
445 spr->base.screen = screen;
446 spr->base.format = PIPE_FORMAT_R8_UNORM; /* ?? */
447 spr->base.bind = bind_flags;
448 spr->base.usage = PIPE_USAGE_IMMUTABLE;
449 spr->base.flags = 0;
450 spr->base.width0 = bytes;
451 spr->base.height0 = 1;
452 spr->base.depth0 = 1;
453 spr->base.array_size = 1;
454 spr->userBuffer = TRUE;
455 spr->data = ptr;
456
457 return &spr->base;
458 }
459
460
461 void
462 softpipe_init_texture_funcs(struct pipe_context *pipe)
463 {
464 pipe->get_transfer = softpipe_get_transfer;
465 pipe->transfer_destroy = softpipe_transfer_destroy;
466 pipe->transfer_map = softpipe_transfer_map;
467 pipe->transfer_unmap = softpipe_transfer_unmap;
468
469 pipe->transfer_flush_region = u_default_transfer_flush_region;
470 pipe->transfer_inline_write = u_default_transfer_inline_write;
471
472 pipe->create_surface = softpipe_create_surface;
473 pipe->surface_destroy = softpipe_surface_destroy;
474 }
475
476
477 void
478 softpipe_init_screen_texture_funcs(struct pipe_screen *screen)
479 {
480 screen->resource_create = softpipe_resource_create;
481 screen->resource_destroy = softpipe_resource_destroy;
482 screen->resource_from_handle = softpipe_resource_from_handle;
483 screen->resource_get_handle = softpipe_resource_get_handle;
484 screen->user_buffer_create = softpipe_user_buffer_create;
485
486 }