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