Merge commit 'origin/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 * spt)
55 {
56 struct pipe_resource *pt = &spt->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 spt->stride[level] = util_format_get_stride(pt->format, width);
65
66 spt->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 spt->stride[level]);
71
72 width = u_minify(width, 1);
73 height = u_minify(height, 1);
74 depth = u_minify(depth, 1);
75 }
76
77 spt->data = align_malloc(buffer_size, 16);
78
79 return spt->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 * spt)
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 spt->dt = winsys->displaytarget_create(winsys,
95 spt->base.bind,
96 spt->base.format,
97 spt->base.width0,
98 spt->base.height0,
99 16,
100 &spt->stride[0] );
101
102 return spt->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 *template)
112 {
113 struct softpipe_resource *spt = CALLOC_STRUCT(softpipe_resource);
114 if (!spt)
115 return NULL;
116
117 assert(template->format != PIPE_FORMAT_NONE);
118
119 spt->base = *template;
120 pipe_reference_init(&spt->base.reference, 1);
121 spt->base.screen = screen;
122
123 spt->pot = (util_is_power_of_two(template->width0) &&
124 util_is_power_of_two(template->height0) &&
125 util_is_power_of_two(template->depth0));
126
127 if (spt->base.bind & (PIPE_BIND_DISPLAY_TARGET |
128 PIPE_BIND_SCANOUT |
129 PIPE_BIND_SHARED)) {
130 if (!softpipe_displaytarget_layout(screen, spt))
131 goto fail;
132 }
133 else {
134 if (!softpipe_resource_layout(screen, spt))
135 goto fail;
136 }
137
138 return &spt->base;
139
140 fail:
141 FREE(spt);
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 *spt = softpipe_resource(pt);
152
153 if (spt->dt) {
154 /* display target */
155 struct sw_winsys *winsys = screen->winsys;
156 winsys->displaytarget_destroy(winsys, spt->dt);
157 }
158 else if (!spt->userBuffer) {
159 /* regular texture */
160 align_free(spt->data);
161 }
162
163 FREE(spt);
164 }
165
166
167 static struct pipe_resource *
168 softpipe_resource_from_handle(struct pipe_screen *screen,
169 const struct pipe_resource *template,
170 struct winsys_handle *whandle)
171 {
172 struct sw_winsys *winsys = softpipe_screen(screen)->winsys;
173 struct softpipe_resource *spt = CALLOC_STRUCT(softpipe_resource);
174 if (!spt)
175 return NULL;
176
177 spt->base = *template;
178 pipe_reference_init(&spt->base.reference, 1);
179 spt->base.screen = screen;
180
181 spt->pot = (util_is_power_of_two(template->width0) &&
182 util_is_power_of_two(template->height0) &&
183 util_is_power_of_two(template->depth0));
184
185 spt->dt = winsys->displaytarget_from_handle(winsys,
186 template,
187 whandle,
188 &spt->stride[0]);
189 if (!spt->dt)
190 goto fail;
191
192 return &spt->base;
193
194 fail:
195 FREE(spt);
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 *spt = softpipe_resource(pt);
207
208 assert(spt->dt);
209 if (!spt->dt)
210 return FALSE;
211
212 return winsys->displaytarget_get_handle(winsys, spt->dt, whandle);
213 }
214
215
216 /**
217 * Get a pipe_surface "view" into a texture.
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 *spt = 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 = spt->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 spt->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 spt->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 texture object.
280 * \param face one of PIPE_TEX_FACE_x or 0
281 * \param level texture mipmap level
282 * \param zslice 2D slice of a 3D texture
283 * \param usage one of PIPE_TRANSFER_READ/WRITE/READ_WRITE
284 * \param x X position of region to read/write
285 * \param y Y position of region to read/write
286 * \param width width of region to read/write
287 * \param height height of region to read/write
288 */
289 static struct pipe_transfer *
290 softpipe_get_transfer(struct pipe_context *pipe,
291 struct pipe_resource *resource,
292 struct pipe_subresource sr,
293 unsigned usage,
294 const struct pipe_box *box)
295 {
296 struct softpipe_resource *sptex = softpipe_resource(resource);
297 struct softpipe_transfer *spt;
298
299 assert(resource);
300 assert(sr.level <= resource->last_level);
301
302 /* make sure the requested region is in the image bounds */
303 assert(box->x + box->width <= u_minify(resource->width0, sr.level));
304 assert(box->y + box->height <= u_minify(resource->height0, sr.level));
305 assert(box->z + box->depth <= u_minify(resource->depth0, sr.level));
306
307 spt = CALLOC_STRUCT(softpipe_transfer);
308 if (spt) {
309 struct pipe_transfer *pt = &spt->base;
310 enum pipe_format format = resource->format;
311 int nblocksy = util_format_get_nblocksy(resource->format,
312 u_minify(resource->height0, sr.level));
313 pipe_resource_reference(&pt->resource, resource);
314 pt->sr = sr;
315 pt->usage = usage;
316 pt->box = *box;
317 pt->stride = sptex->stride[sr.level];
318
319 spt->offset = sptex->level_offset[sr.level];
320
321 if (resource->target == PIPE_TEXTURE_CUBE) {
322 spt->offset += sr.face * nblocksy * pt->stride;
323 }
324 else if (resource->target == PIPE_TEXTURE_3D) {
325 spt->offset += box->z * nblocksy * pt->stride;
326 }
327 else {
328 assert(sr.face == 0);
329 assert(box->z == 0);
330 }
331
332 spt->offset +=
333 box->y / util_format_get_blockheight(format) * spt->base.stride +
334 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
335
336 return pt;
337 }
338 return NULL;
339 }
340
341
342 /**
343 * Free a pipe_transfer object which was created with
344 * softpipe_get_transfer().
345 */
346 static void
347 softpipe_transfer_destroy(struct pipe_context *pipe,
348 struct pipe_transfer *transfer)
349 {
350 pipe_resource_reference(&transfer->resource, NULL);
351 FREE(transfer);
352 }
353
354
355 /**
356 * Create memory mapping for given pipe_transfer object.
357 */
358 static void *
359 softpipe_transfer_map( struct pipe_context *pipe,
360 struct pipe_transfer *transfer )
361 {
362 struct softpipe_transfer *sp_transfer = softpipe_transfer(transfer);
363 struct softpipe_resource *sp_resource = softpipe_resource(transfer->resource);
364 struct sw_winsys *winsys = softpipe_screen(pipe->screen)->winsys;
365 uint8_t *map;
366
367 /* resources backed by display target treated specially:
368 */
369 if (sp_resource->dt) {
370 map = winsys->displaytarget_map(winsys,
371 sp_resource->dt,
372 transfer->usage);
373 }
374 else {
375 map = sp_resource->data;
376 }
377
378 if (map == NULL)
379 return NULL;
380 else
381 return map + sp_transfer->offset;
382 }
383
384
385 /**
386 * Unmap memory mapping for given pipe_transfer object.
387 */
388 static void
389 softpipe_transfer_unmap(struct pipe_context *pipe,
390 struct pipe_transfer *transfer)
391 {
392 struct softpipe_resource *spt;
393
394 assert(transfer->resource);
395 spt = softpipe_resource(transfer->resource);
396
397 if (spt->dt) {
398 /* display target */
399 struct sw_winsys *winsys = softpipe_screen(pipe->screen)->winsys;
400 winsys->displaytarget_unmap(winsys, spt->dt);
401 }
402
403 if (transfer->usage & PIPE_TRANSFER_WRITE) {
404 /* Mark the texture as dirty to expire the tile caches. */
405 spt->timestamp++;
406 }
407 }
408
409 /**
410 * Create buffer which wraps user-space data.
411 */
412 static struct pipe_resource *
413 softpipe_user_buffer_create(struct pipe_screen *screen,
414 void *ptr,
415 unsigned bytes,
416 unsigned bind_flags)
417 {
418 struct softpipe_resource *buffer;
419
420 buffer = CALLOC_STRUCT(softpipe_resource);
421 if(!buffer)
422 return NULL;
423
424
425 pipe_reference_init(&buffer->base.reference, 1);
426 buffer->base.screen = screen;
427 buffer->base.format = PIPE_FORMAT_R8_UNORM; /* ?? */
428 buffer->base.bind = bind_flags;
429 buffer->base._usage = PIPE_USAGE_IMMUTABLE;
430 buffer->base.flags = 0;
431 buffer->base.width0 = bytes;
432 buffer->base.height0 = 1;
433 buffer->base.depth0 = 1;
434 buffer->userBuffer = TRUE;
435 buffer->data = ptr;
436
437 return &buffer->base;
438 }
439
440
441
442
443
444 void
445 softpipe_init_texture_funcs(struct pipe_context *pipe)
446 {
447 pipe->get_transfer = softpipe_get_transfer;
448 pipe->transfer_destroy = softpipe_transfer_destroy;
449 pipe->transfer_map = softpipe_transfer_map;
450 pipe->transfer_unmap = softpipe_transfer_unmap;
451
452 pipe->transfer_flush_region = u_default_transfer_flush_region;
453 pipe->transfer_inline_write = u_default_transfer_inline_write;
454 }
455
456 void
457 softpipe_init_screen_texture_funcs(struct pipe_screen *screen)
458 {
459 screen->resource_create = softpipe_resource_create;
460 screen->resource_destroy = softpipe_resource_destroy;
461 screen->resource_from_handle = softpipe_resource_from_handle;
462 screen->resource_get_handle = softpipe_resource_get_handle;
463 screen->user_buffer_create = softpipe_user_buffer_create;
464
465 screen->get_tex_surface = softpipe_get_tex_surface;
466 screen->tex_surface_destroy = softpipe_tex_surface_destroy;
467 }
468
469
470