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
40 #include "sp_context.h"
41 #include "sp_texture.h"
42 #include "sp_screen.h"
43
44 #include "state_tracker/sw_winsys.h"
45
46
47 /**
48 * Conventional allocation path for non-display textures:
49 * Use a simple, maximally packed layout.
50 */
51 static boolean
52 softpipe_texture_layout(struct pipe_screen *screen,
53 struct softpipe_texture * spt)
54 {
55 struct pipe_texture *pt = &spt->base;
56 unsigned level;
57 unsigned width = pt->width0;
58 unsigned height = pt->height0;
59 unsigned depth = pt->depth0;
60 unsigned buffer_size = 0;
61
62 for (level = 0; level <= pt->last_level; level++) {
63 spt->stride[level] = util_format_get_stride(pt->format, width);
64
65 spt->level_offset[level] = buffer_size;
66
67 buffer_size += (util_format_get_nblocksy(pt->format, height) *
68 ((pt->target == PIPE_TEXTURE_CUBE) ? 6 : depth) *
69 spt->stride[level]);
70
71 width = u_minify(width, 1);
72 height = u_minify(height, 1);
73 depth = u_minify(depth, 1);
74 }
75
76 spt->data = align_malloc(buffer_size, 16);
77
78 return spt->data != NULL;
79 }
80
81
82 /**
83 * Texture layout for simple color buffers.
84 */
85 static boolean
86 softpipe_displaytarget_layout(struct pipe_screen *screen,
87 struct softpipe_texture * spt)
88 {
89 struct sw_winsys *winsys = softpipe_screen(screen)->winsys;
90
91 /* Round up the surface size to a multiple of the tile size?
92 */
93 spt->dt = winsys->displaytarget_create(winsys,
94 spt->base.tex_usage,
95 spt->base.format,
96 spt->base.width0,
97 spt->base.height0,
98 16,
99 &spt->stride[0] );
100
101 return spt->dt != NULL;
102 }
103
104
105 /**
106 * Create new pipe_texture given the template information.
107 */
108 static struct pipe_texture *
109 softpipe_texture_create(struct pipe_screen *screen,
110 const struct pipe_texture *template)
111 {
112 struct softpipe_texture *spt = CALLOC_STRUCT(softpipe_texture);
113 if (!spt)
114 return NULL;
115
116 spt->base = *template;
117 pipe_reference_init(&spt->base.reference, 1);
118 spt->base.screen = screen;
119
120 spt->pot = (util_is_power_of_two(template->width0) &&
121 util_is_power_of_two(template->height0) &&
122 util_is_power_of_two(template->depth0));
123
124 if (spt->base.tex_usage & (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
125 PIPE_TEXTURE_USAGE_SCANOUT |
126 PIPE_TEXTURE_USAGE_SHARED)) {
127 if (!softpipe_displaytarget_layout(screen, spt))
128 goto fail;
129 }
130 else {
131 if (!softpipe_texture_layout(screen, spt))
132 goto fail;
133 }
134
135 return &spt->base;
136
137 fail:
138 FREE(spt);
139 return NULL;
140 }
141
142
143 static void
144 softpipe_texture_destroy(struct pipe_texture *pt)
145 {
146 struct softpipe_screen *screen = softpipe_screen(pt->screen);
147 struct softpipe_texture *spt = softpipe_texture(pt);
148
149 if (spt->dt) {
150 /* display target */
151 struct sw_winsys *winsys = screen->winsys;
152 winsys->displaytarget_destroy(winsys, spt->dt);
153 }
154 else {
155 /* regular texture */
156 align_free(spt->data);
157 }
158
159 FREE(spt);
160 }
161
162
163 static struct pipe_texture *
164 softpipe_texture_from_handle(struct pipe_screen *screen,
165 const struct pipe_texture *template,
166 struct winsys_handle *whandle)
167 {
168 struct sw_winsys *winsys = softpipe_screen(screen)->winsys;
169 struct softpipe_texture *spt = CALLOC_STRUCT(softpipe_texture);
170 if (!spt)
171 return NULL;
172
173 spt->base = *template;
174 pipe_reference_init(&spt->base.reference, 1);
175 spt->base.screen = screen;
176
177 spt->pot = (util_is_power_of_two(template->width0) &&
178 util_is_power_of_two(template->height0) &&
179 util_is_power_of_two(template->depth0));
180
181 spt->dt = winsys->displaytarget_from_handle(winsys,
182 template,
183 whandle,
184 &spt->stride[0]);
185 if (!spt->dt)
186 goto fail;
187
188 return &spt->base;
189
190 fail:
191 FREE(spt);
192 return NULL;
193 }
194
195
196 static boolean
197 softpipe_texture_get_handle(struct pipe_screen *screen,
198 struct pipe_texture *pt,
199 struct winsys_handle *whandle)
200 {
201 struct sw_winsys *winsys = softpipe_screen(screen)->winsys;
202 struct softpipe_texture *spt = softpipe_texture(pt);
203
204 assert(spt->dt);
205 if (!spt->dt)
206 return FALSE;
207
208 return winsys->displaytarget_get_handle(winsys, spt->dt, whandle);
209 }
210
211
212 /**
213 * Get a pipe_surface "view" into a texture.
214 */
215 static struct pipe_surface *
216 softpipe_get_tex_surface(struct pipe_screen *screen,
217 struct pipe_texture *pt,
218 unsigned face, unsigned level, unsigned zslice,
219 unsigned usage)
220 {
221 struct softpipe_texture *spt = softpipe_texture(pt);
222 struct pipe_surface *ps;
223
224 assert(level <= pt->last_level);
225
226 ps = CALLOC_STRUCT(pipe_surface);
227 if (ps) {
228 pipe_reference_init(&ps->reference, 1);
229 pipe_texture_reference(&ps->texture, pt);
230 ps->format = pt->format;
231 ps->width = u_minify(pt->width0, level);
232 ps->height = u_minify(pt->height0, level);
233 ps->offset = spt->level_offset[level];
234 ps->usage = usage;
235
236 /* Because we are softpipe, anything that the state tracker
237 * thought was going to be done with the GPU will actually get
238 * done with the CPU. Let's adjust the flags to take that into
239 * account.
240 */
241 if (ps->usage & PIPE_BUFFER_USAGE_GPU_WRITE) {
242 /* GPU_WRITE means "render" and that can involve reads (blending) */
243 ps->usage |= PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_CPU_READ;
244 }
245
246 if (ps->usage & PIPE_BUFFER_USAGE_GPU_READ)
247 ps->usage |= PIPE_BUFFER_USAGE_CPU_READ;
248
249 if (ps->usage & (PIPE_BUFFER_USAGE_CPU_WRITE |
250 PIPE_BUFFER_USAGE_GPU_WRITE)) {
251 /* Mark the surface as dirty. The tile cache will look for this. */
252 spt->timestamp++;
253 softpipe_screen(screen)->timestamp++;
254 }
255
256 ps->face = face;
257 ps->level = level;
258 ps->zslice = zslice;
259
260 if (pt->target == PIPE_TEXTURE_CUBE) {
261 ps->offset += face * util_format_get_nblocksy(pt->format, u_minify(pt->height0, level)) *
262 spt->stride[level];
263 }
264 else if (pt->target == PIPE_TEXTURE_3D) {
265 ps->offset += zslice * util_format_get_nblocksy(pt->format, u_minify(pt->height0, level)) *
266 spt->stride[level];
267 }
268 else {
269 assert(face == 0);
270 assert(zslice == 0);
271 }
272 }
273 return ps;
274 }
275
276
277 /**
278 * Free a pipe_surface which was created with softpipe_get_tex_surface().
279 */
280 static void
281 softpipe_tex_surface_destroy(struct pipe_surface *surf)
282 {
283 /* Effectively do the texture_update work here - if texture images
284 * needed post-processing to put them into hardware layout, this is
285 * where it would happen. For softpipe, nothing to do.
286 */
287 assert(surf->texture);
288 pipe_texture_reference(&surf->texture, NULL);
289 FREE(surf);
290 }
291
292
293 /**
294 * Geta pipe_transfer object which is used for moving data in/out of
295 * a texture object.
296 * \param face one of PIPE_TEX_FACE_x or 0
297 * \param level texture mipmap level
298 * \param zslice 2D slice of a 3D texture
299 * \param usage one of PIPE_TRANSFER_READ/WRITE/READ_WRITE
300 * \param x X position of region to read/write
301 * \param y Y position of region to read/write
302 * \param width width of region to read/write
303 * \param height height of region to read/write
304 */
305 static struct pipe_transfer *
306 softpipe_get_tex_transfer(struct pipe_context *pipe,
307 struct pipe_texture *texture,
308 unsigned face, unsigned level, unsigned zslice,
309 enum pipe_transfer_usage usage,
310 unsigned x, unsigned y, unsigned w, unsigned h)
311 {
312 struct softpipe_texture *sptex = softpipe_texture(texture);
313 struct softpipe_transfer *spt;
314
315 assert(texture);
316 assert(level <= texture->last_level);
317
318 /* make sure the requested region is in the image bounds */
319 assert(x + w <= u_minify(texture->width0, level));
320 assert(y + h <= u_minify(texture->height0, level));
321
322 spt = CALLOC_STRUCT(softpipe_transfer);
323 if (spt) {
324 struct pipe_transfer *pt = &spt->base;
325 int nblocksy = util_format_get_nblocksy(texture->format, u_minify(texture->height0, level));
326 pipe_texture_reference(&pt->texture, texture);
327 pt->x = x;
328 pt->y = y;
329 pt->width = w;
330 pt->height = h;
331 pt->stride = sptex->stride[level];
332 pt->usage = usage;
333 pt->face = face;
334 pt->level = level;
335 pt->zslice = zslice;
336
337 spt->offset = sptex->level_offset[level];
338
339 if (texture->target == PIPE_TEXTURE_CUBE) {
340 spt->offset += face * nblocksy * pt->stride;
341 }
342 else if (texture->target == PIPE_TEXTURE_3D) {
343 spt->offset += zslice * nblocksy * pt->stride;
344 }
345 else {
346 assert(face == 0);
347 assert(zslice == 0);
348 }
349 return pt;
350 }
351 return NULL;
352 }
353
354
355 /**
356 * Free a pipe_transfer object which was created with
357 * softpipe_get_tex_transfer().
358 */
359 static void
360 softpipe_tex_transfer_destroy(struct pipe_context *pipe,
361 struct pipe_transfer *transfer)
362 {
363 /* Effectively do the texture_update work here - if texture images
364 * needed post-processing to put them into hardware layout, this is
365 * where it would happen. For softpipe, nothing to do.
366 */
367 assert (transfer->texture);
368 pipe_texture_reference(&transfer->texture, NULL);
369 FREE(transfer);
370 }
371
372
373 /**
374 * Create memory mapping for given pipe_transfer object.
375 */
376 static void *
377 softpipe_transfer_map( struct pipe_context *pipe,
378 struct pipe_transfer *transfer )
379 {
380 ubyte *map, *xfer_map;
381 struct softpipe_texture *spt;
382 enum pipe_format format;
383
384 assert(transfer->texture);
385 spt = softpipe_texture(transfer->texture);
386 format = transfer->texture->format;
387
388 if (spt->dt) {
389 /* display target */
390 struct sw_winsys *winsys = softpipe_screen(pipe->screen)->winsys;
391
392 map = winsys->displaytarget_map(winsys, spt->dt,
393 pipe_transfer_buffer_flags(transfer));
394 if (map == NULL)
395 return NULL;
396 }
397 else {
398 map = spt->data;
399 if (map == NULL)
400 return NULL;
401 }
402
403 /* May want to different things here depending on read/write nature
404 * of the map:
405 */
406 if (transfer->texture && (transfer->usage & PIPE_TRANSFER_WRITE)) {
407 /* Do something to notify sharing contexts of a texture change.
408 * In softpipe, that would mean flushing the texture cache.
409 */
410 softpipe_screen(pipe->screen)->timestamp++;
411 }
412
413 xfer_map = map + softpipe_transfer(transfer)->offset +
414 transfer->y / util_format_get_blockheight(format) * transfer->stride +
415 transfer->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
416 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
417 return xfer_map;
418 }
419
420
421 /**
422 * Unmap memory mapping for given pipe_transfer object.
423 */
424 static void
425 softpipe_transfer_unmap(struct pipe_context *pipe,
426 struct pipe_transfer *transfer)
427 {
428 struct softpipe_texture *spt;
429
430 assert(transfer->texture);
431 spt = softpipe_texture(transfer->texture);
432
433 if (spt->dt) {
434 /* display target */
435 struct sw_winsys *winsys = softpipe_screen(pipe->screen)->winsys;
436 winsys->displaytarget_unmap(winsys, spt->dt);
437 }
438
439 if (transfer->usage & PIPE_TRANSFER_WRITE) {
440 /* Mark the texture as dirty to expire the tile caches. */
441 spt->timestamp++;
442 }
443 }
444
445
446 static struct pipe_video_surface*
447 softpipe_video_surface_create(struct pipe_screen *screen,
448 enum pipe_video_chroma_format chroma_format,
449 unsigned width, unsigned height)
450 {
451 struct softpipe_video_surface *sp_vsfc;
452 struct pipe_texture template;
453
454 assert(screen);
455 assert(width && height);
456
457 sp_vsfc = CALLOC_STRUCT(softpipe_video_surface);
458 if (!sp_vsfc)
459 return NULL;
460
461 pipe_reference_init(&sp_vsfc->base.reference, 1);
462 sp_vsfc->base.screen = screen;
463 sp_vsfc->base.chroma_format = chroma_format;
464 /*sp_vsfc->base.surface_format = PIPE_VIDEO_SURFACE_FORMAT_VUYA;*/
465 sp_vsfc->base.width = width;
466 sp_vsfc->base.height = height;
467
468 memset(&template, 0, sizeof(struct pipe_texture));
469 template.target = PIPE_TEXTURE_2D;
470 template.format = PIPE_FORMAT_B8G8R8X8_UNORM;
471 template.last_level = 0;
472 /* vl_mpeg12_mc_renderer expects this when it's initialized with pot_buffers=true */
473 template.width0 = util_next_power_of_two(width);
474 template.height0 = util_next_power_of_two(height);
475 template.depth0 = 1;
476 template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER | PIPE_TEXTURE_USAGE_RENDER_TARGET;
477
478 sp_vsfc->tex = screen->texture_create(screen, &template);
479 if (!sp_vsfc->tex) {
480 FREE(sp_vsfc);
481 return NULL;
482 }
483
484 return &sp_vsfc->base;
485 }
486
487
488 static void
489 softpipe_video_surface_destroy(struct pipe_video_surface *vsfc)
490 {
491 struct softpipe_video_surface *sp_vsfc = softpipe_video_surface(vsfc);
492
493 pipe_texture_reference(&sp_vsfc->tex, NULL);
494 FREE(sp_vsfc);
495 }
496
497
498 void
499 softpipe_init_texture_funcs(struct pipe_context *pipe)
500 {
501 pipe->get_tex_transfer = softpipe_get_tex_transfer;
502 pipe->tex_transfer_destroy = softpipe_tex_transfer_destroy;
503 pipe->transfer_map = softpipe_transfer_map;
504 pipe->transfer_unmap = softpipe_transfer_unmap;
505 }
506
507 void
508 softpipe_init_screen_texture_funcs(struct pipe_screen *screen)
509 {
510 screen->texture_create = softpipe_texture_create;
511 screen->texture_destroy = softpipe_texture_destroy;
512 screen->texture_from_handle = softpipe_texture_from_handle;
513 screen->texture_get_handle = softpipe_texture_get_handle;
514
515 screen->get_tex_surface = softpipe_get_tex_surface;
516 screen->tex_surface_destroy = softpipe_tex_surface_destroy;
517
518 screen->video_surface_create = softpipe_video_surface_create;
519 screen->video_surface_destroy = softpipe_video_surface_destroy;
520 }
521
522
523