Merge branch '7.8'
[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 * Get a pipe_surface "view" into a texture resource.
219 */
220 static struct pipe_surface *
221 softpipe_get_tex_surface(struct pipe_screen *screen,
222 struct pipe_resource *pt,
223 unsigned face, unsigned level, unsigned zslice,
224 unsigned usage)
225 {
226 struct softpipe_resource *spr = softpipe_resource(pt);
227 struct pipe_surface *ps;
228
229 assert(level <= pt->last_level);
230
231 ps = CALLOC_STRUCT(pipe_surface);
232 if (ps) {
233 pipe_reference_init(&ps->reference, 1);
234 pipe_resource_reference(&ps->texture, pt);
235 ps->format = pt->format;
236 ps->width = u_minify(pt->width0, level);
237 ps->height = u_minify(pt->height0, level);
238 ps->offset = spr->level_offset[level];
239 ps->usage = usage;
240
241 ps->face = face;
242 ps->level = level;
243 ps->zslice = zslice;
244
245 if (pt->target == PIPE_TEXTURE_CUBE) {
246 ps->offset += face * util_format_get_nblocksy(pt->format, u_minify(pt->height0, level)) *
247 spr->stride[level];
248 }
249 else if (pt->target == PIPE_TEXTURE_3D) {
250 ps->offset += zslice * util_format_get_nblocksy(pt->format, u_minify(pt->height0, level)) *
251 spr->stride[level];
252 }
253 else {
254 assert(face == 0);
255 assert(zslice == 0);
256 }
257 }
258 return ps;
259 }
260
261
262 /**
263 * Free a pipe_surface which was created with softpipe_get_tex_surface().
264 */
265 static void
266 softpipe_tex_surface_destroy(struct pipe_surface *surf)
267 {
268 /* Effectively do the texture_update work here - if texture images
269 * needed post-processing to put them into hardware layout, this is
270 * where it would happen. For softpipe, nothing to do.
271 */
272 assert(surf->texture);
273 pipe_resource_reference(&surf->texture, NULL);
274 FREE(surf);
275 }
276
277
278 /**
279 * Geta pipe_transfer object which is used for moving data in/out of
280 * a resource object.
281 * \param pipe rendering context
282 * \param resource the resource to transfer in/out of
283 * \param sr indicates cube face or 3D texture slice
284 * \param usage bitmask of PIPE_TRANSFER_x flags
285 * \param box the 1D/2D/3D region of interest
286 */
287 static struct pipe_transfer *
288 softpipe_get_transfer(struct pipe_context *pipe,
289 struct pipe_resource *resource,
290 struct pipe_subresource sr,
291 unsigned usage,
292 const struct pipe_box *box)
293 {
294 struct softpipe_resource *spr = softpipe_resource(resource);
295 struct softpipe_transfer *spt;
296
297 assert(resource);
298 assert(sr.level <= resource->last_level);
299
300 /* make sure the requested region is in the image bounds */
301 assert(box->x + box->width <= u_minify(resource->width0, sr.level));
302 assert(box->y + box->height <= u_minify(resource->height0, sr.level));
303 assert(box->z + box->depth <= u_minify(resource->depth0, sr.level));
304
305 /*
306 * Transfers, like other pipe operations, must happen in order, so flush the
307 * context if necessary.
308 */
309 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
310 boolean read_only = !(usage & PIPE_TRANSFER_WRITE);
311 boolean do_not_block = !!(usage & PIPE_TRANSFER_DONTBLOCK);
312 if (!softpipe_flush_resource(pipe, resource,
313 sr.face, sr.level,
314 0, /* flush_flags */
315 read_only,
316 TRUE, /* cpu_access */
317 do_not_block)) {
318 /*
319 * It would have blocked, but state tracker requested no to.
320 */
321 assert(do_not_block);
322 return NULL;
323 }
324 }
325
326 spt = CALLOC_STRUCT(softpipe_transfer);
327 if (spt) {
328 struct pipe_transfer *pt = &spt->base;
329 enum pipe_format format = resource->format;
330 int nblocksy = util_format_get_nblocksy(resource->format,
331 u_minify(resource->height0, sr.level));
332 pipe_resource_reference(&pt->resource, resource);
333 pt->sr = sr;
334 pt->usage = usage;
335 pt->box = *box;
336 pt->stride = spr->stride[sr.level];
337
338 spt->offset = spr->level_offset[sr.level];
339
340 if (resource->target == PIPE_TEXTURE_CUBE) {
341 spt->offset += sr.face * nblocksy * pt->stride;
342 }
343 else if (resource->target == PIPE_TEXTURE_3D) {
344 spt->offset += box->z * nblocksy * pt->stride;
345 }
346 else {
347 assert(sr.face == 0);
348 assert(box->z == 0);
349 }
350
351 spt->offset +=
352 box->y / util_format_get_blockheight(format) * spt->base.stride +
353 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
354
355 return pt;
356 }
357 return NULL;
358 }
359
360
361 /**
362 * Free a pipe_transfer object which was created with
363 * softpipe_get_transfer().
364 */
365 static void
366 softpipe_transfer_destroy(struct pipe_context *pipe,
367 struct pipe_transfer *transfer)
368 {
369 pipe_resource_reference(&transfer->resource, NULL);
370 FREE(transfer);
371 }
372
373
374 /**
375 * Create memory mapping for given pipe_transfer object.
376 */
377 static void *
378 softpipe_transfer_map(struct pipe_context *pipe,
379 struct pipe_transfer *transfer)
380 {
381 struct softpipe_transfer *spt = softpipe_transfer(transfer);
382 struct softpipe_resource *spr = softpipe_resource(transfer->resource);
383 struct sw_winsys *winsys = softpipe_screen(pipe->screen)->winsys;
384 uint8_t *map;
385
386 /* resources backed by display target treated specially:
387 */
388 if (spr->dt) {
389 map = winsys->displaytarget_map(winsys, spr->dt, transfer->usage);
390 }
391 else {
392 map = spr->data;
393 }
394
395 if (map == NULL)
396 return NULL;
397 else
398 return map + spt->offset;
399 }
400
401
402 /**
403 * Unmap memory mapping for given pipe_transfer object.
404 */
405 static void
406 softpipe_transfer_unmap(struct pipe_context *pipe,
407 struct pipe_transfer *transfer)
408 {
409 struct softpipe_resource *spr;
410
411 assert(transfer->resource);
412 spr = softpipe_resource(transfer->resource);
413
414 if (spr->dt) {
415 /* display target */
416 struct sw_winsys *winsys = softpipe_screen(pipe->screen)->winsys;
417 winsys->displaytarget_unmap(winsys, spr->dt);
418 }
419
420 if (transfer->usage & PIPE_TRANSFER_WRITE) {
421 /* Mark the texture as dirty to expire the tile caches. */
422 spr->timestamp++;
423 }
424 }
425
426 /**
427 * Create buffer which wraps user-space data.
428 */
429 static struct pipe_resource *
430 softpipe_user_buffer_create(struct pipe_screen *screen,
431 void *ptr,
432 unsigned bytes,
433 unsigned bind_flags)
434 {
435 struct softpipe_resource *spr;
436
437 spr = CALLOC_STRUCT(softpipe_resource);
438 if (!spr)
439 return NULL;
440
441 pipe_reference_init(&spr->base.reference, 1);
442 spr->base.screen = screen;
443 spr->base.format = PIPE_FORMAT_R8_UNORM; /* ?? */
444 spr->base.bind = bind_flags;
445 spr->base.usage = PIPE_USAGE_IMMUTABLE;
446 spr->base.flags = 0;
447 spr->base.width0 = bytes;
448 spr->base.height0 = 1;
449 spr->base.depth0 = 1;
450 spr->userBuffer = TRUE;
451 spr->data = ptr;
452
453 return &spr->base;
454 }
455
456
457 void
458 softpipe_init_texture_funcs(struct pipe_context *pipe)
459 {
460 pipe->get_transfer = softpipe_get_transfer;
461 pipe->transfer_destroy = softpipe_transfer_destroy;
462 pipe->transfer_map = softpipe_transfer_map;
463 pipe->transfer_unmap = softpipe_transfer_unmap;
464
465 pipe->transfer_flush_region = u_default_transfer_flush_region;
466 pipe->transfer_inline_write = u_default_transfer_inline_write;
467 }
468
469
470 void
471 softpipe_init_screen_texture_funcs(struct pipe_screen *screen)
472 {
473 screen->resource_create = softpipe_resource_create;
474 screen->resource_destroy = softpipe_resource_destroy;
475 screen->resource_from_handle = softpipe_resource_from_handle;
476 screen->resource_get_handle = softpipe_resource_get_handle;
477 screen->user_buffer_create = softpipe_user_buffer_create;
478
479 screen->get_tex_surface = softpipe_get_tex_surface;
480 screen->tex_surface_destroy = softpipe_tex_surface_destroy;
481 }