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