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