Merge commit 'origin/mesa_7_7_branch'
[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 #include "util/u_math.h"
36 #include "util/u_memory.h"
37
38 #include "sp_context.h"
39 #include "sp_state.h"
40 #include "sp_texture.h"
41 #include "sp_screen.h"
42 #include "sp_winsys.h"
43
44
45 /**
46 * Conventional allocation path for non-display textures:
47 * Use a simple, maximally packed layout.
48 */
49 static boolean
50 softpipe_texture_layout(struct pipe_screen *screen,
51 struct softpipe_texture * spt)
52 {
53 struct pipe_texture *pt = &spt->base;
54 unsigned level;
55 unsigned width = pt->width0;
56 unsigned height = pt->height0;
57 unsigned depth = pt->depth0;
58
59 unsigned buffer_size = 0;
60
61 pt->width0 = width;
62 pt->height0 = height;
63 pt->depth0 = depth;
64
65 for (level = 0; level <= pt->last_level; level++) {
66 pt->nblocksx[level] = pf_get_nblocksx(&pt->block, width);
67 pt->nblocksy[level] = pf_get_nblocksy(&pt->block, height);
68 spt->stride[level] = pt->nblocksx[level]*pt->block.size;
69
70 spt->level_offset[level] = buffer_size;
71
72 buffer_size += (pt->nblocksy[level] *
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->base.nblocksx[0] = pf_get_nblocksx(&spt->base.block, spt->base.width0);
101 spt->base.nblocksy[0] = pf_get_nblocksy(&spt->base.block, spt->base.height0);
102
103 spt->buffer = screen->surface_buffer_create( screen,
104 spt->base.width0,
105 spt->base.height0,
106 spt->base.format,
107 usage,
108 tex_usage,
109 &spt->stride[0]);
110
111 return spt->buffer != NULL;
112 }
113
114
115 /**
116 * Create new pipe_texture given the template information.
117 */
118 static struct pipe_texture *
119 softpipe_texture_create(struct pipe_screen *screen,
120 const struct pipe_texture *template)
121 {
122 struct softpipe_texture *spt = CALLOC_STRUCT(softpipe_texture);
123 if (!spt)
124 return NULL;
125
126 spt->base = *template;
127 pipe_reference_init(&spt->base.reference, 1);
128 spt->base.screen = screen;
129
130 spt->pot = (util_is_power_of_two(template->width0) &&
131 util_is_power_of_two(template->height0) &&
132 util_is_power_of_two(template->depth0));
133
134 if (spt->base.tex_usage & (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
135 PIPE_TEXTURE_USAGE_PRIMARY)) {
136 if (!softpipe_displaytarget_layout(screen, spt))
137 goto fail;
138 }
139 else {
140 if (!softpipe_texture_layout(screen, spt))
141 goto fail;
142 }
143
144 return &spt->base;
145
146 fail:
147 FREE(spt);
148 return NULL;
149 }
150
151
152 /**
153 * Create a new pipe_texture which wraps an existing buffer.
154 */
155 static struct pipe_texture *
156 softpipe_texture_blanket(struct pipe_screen * screen,
157 const struct pipe_texture *base,
158 const unsigned *stride,
159 struct pipe_buffer *buffer)
160 {
161 struct softpipe_texture *spt;
162 assert(screen);
163
164 /* Only supports one type */
165 if (base->target != PIPE_TEXTURE_2D ||
166 base->last_level != 0 ||
167 base->depth0 != 1) {
168 return NULL;
169 }
170
171 spt = CALLOC_STRUCT(softpipe_texture);
172 if (!spt)
173 return NULL;
174
175 spt->base = *base;
176 pipe_reference_init(&spt->base.reference, 1);
177 spt->base.screen = screen;
178 spt->base.nblocksx[0] = pf_get_nblocksx(&spt->base.block, spt->base.width0);
179 spt->base.nblocksy[0] = pf_get_nblocksy(&spt->base.block, spt->base.height0);
180 spt->stride[0] = stride[0];
181
182 pipe_buffer_reference(&spt->buffer, buffer);
183
184 return &spt->base;
185 }
186
187
188 static void
189 softpipe_texture_destroy(struct pipe_texture *pt)
190 {
191 struct softpipe_texture *spt = softpipe_texture(pt);
192
193 pipe_buffer_reference(&spt->buffer, NULL);
194 FREE(spt);
195 }
196
197
198 /**
199 * Get a pipe_surface "view" into a texture.
200 */
201 static struct pipe_surface *
202 softpipe_get_tex_surface(struct pipe_screen *screen,
203 struct pipe_texture *pt,
204 unsigned face, unsigned level, unsigned zslice,
205 unsigned usage)
206 {
207 struct softpipe_texture *spt = softpipe_texture(pt);
208 struct pipe_surface *ps;
209
210 assert(level <= pt->last_level);
211
212 ps = CALLOC_STRUCT(pipe_surface);
213 if (ps) {
214 pipe_reference_init(&ps->reference, 1);
215 pipe_texture_reference(&ps->texture, pt);
216 ps->format = pt->format;
217 ps->width = u_minify(pt->width0, level);
218 ps->height = u_minify(pt->height0, level);
219 ps->offset = spt->level_offset[level];
220 ps->usage = usage;
221
222 /* Because we are softpipe, anything that the state tracker
223 * thought was going to be done with the GPU will actually get
224 * done with the CPU. Let's adjust the flags to take that into
225 * account.
226 */
227 if (ps->usage & PIPE_BUFFER_USAGE_GPU_WRITE) {
228 /* GPU_WRITE means "render" and that can involve reads (blending) */
229 ps->usage |= PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_CPU_READ;
230 }
231
232 if (ps->usage & PIPE_BUFFER_USAGE_GPU_READ)
233 ps->usage |= PIPE_BUFFER_USAGE_CPU_READ;
234
235 if (ps->usage & (PIPE_BUFFER_USAGE_CPU_WRITE |
236 PIPE_BUFFER_USAGE_GPU_WRITE)) {
237 /* Mark the surface as dirty. The tile cache will look for this. */
238 spt->timestamp++;
239 softpipe_screen(screen)->timestamp++;
240 }
241
242 ps->face = face;
243 ps->level = level;
244 ps->zslice = zslice;
245
246 if (pt->target == PIPE_TEXTURE_CUBE) {
247 ps->offset += face * pt->nblocksy[level] * spt->stride[level];
248 }
249 else if (pt->target == PIPE_TEXTURE_3D) {
250 ps->offset += zslice * pt->nblocksy[level] * 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_texture_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_tex_transfer(struct pipe_screen *screen,
291 struct pipe_texture *texture,
292 unsigned face, unsigned level, unsigned zslice,
293 enum pipe_transfer_usage usage,
294 unsigned x, unsigned y, unsigned w, unsigned h)
295 {
296 struct softpipe_texture *sptex = softpipe_texture(texture);
297 struct softpipe_transfer *spt;
298
299 assert(texture);
300 assert(level <= texture->last_level);
301
302 spt = CALLOC_STRUCT(softpipe_transfer);
303 if (spt) {
304 struct pipe_transfer *pt = &spt->base;
305 pipe_texture_reference(&pt->texture, texture);
306 pt->format = texture->format;
307 pt->block = texture->block;
308 pt->x = x;
309 pt->y = y;
310 pt->width = w;
311 pt->height = h;
312 pt->nblocksx = texture->nblocksx[level];
313 pt->nblocksy = texture->nblocksy[level];
314 pt->stride = sptex->stride[level];
315 pt->usage = usage;
316 pt->face = face;
317 pt->level = level;
318 pt->zslice = zslice;
319
320 spt->offset = sptex->level_offset[level];
321
322 if (texture->target == PIPE_TEXTURE_CUBE) {
323 spt->offset += face * pt->nblocksy * pt->stride;
324 }
325 else if (texture->target == PIPE_TEXTURE_3D) {
326 spt->offset += zslice * pt->nblocksy * pt->stride;
327 }
328 else {
329 assert(face == 0);
330 assert(zslice == 0);
331 }
332 return pt;
333 }
334 return NULL;
335 }
336
337
338 /**
339 * Free a pipe_transfer object which was created with
340 * softpipe_get_tex_transfer().
341 */
342 static void
343 softpipe_tex_transfer_destroy(struct pipe_transfer *transfer)
344 {
345 /* Effectively do the texture_update work here - if texture images
346 * needed post-processing to put them into hardware layout, this is
347 * where it would happen. For softpipe, nothing to do.
348 */
349 assert (transfer->texture);
350 pipe_texture_reference(&transfer->texture, 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_screen *screen,
360 struct pipe_transfer *transfer )
361 {
362 ubyte *map, *xfer_map;
363 struct softpipe_texture *spt;
364
365 assert(transfer->texture);
366 spt = softpipe_texture(transfer->texture);
367
368 map = pipe_buffer_map(screen, spt->buffer, pipe_transfer_buffer_flags(transfer));
369 if (map == NULL)
370 return NULL;
371
372 /* May want to different things here depending on read/write nature
373 * of the map:
374 */
375 if (transfer->texture && (transfer->usage & PIPE_TRANSFER_WRITE)) {
376 /* Do something to notify sharing contexts of a texture change.
377 * In softpipe, that would mean flushing the texture cache.
378 */
379 softpipe_screen(screen)->timestamp++;
380 }
381
382 xfer_map = map + softpipe_transfer(transfer)->offset +
383 transfer->y / transfer->block.height * transfer->stride +
384 transfer->x / transfer->block.width * transfer->block.size;
385 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
386 return xfer_map;
387 }
388
389
390 /**
391 * Unmap memory mapping for given pipe_transfer object.
392 */
393 static void
394 softpipe_transfer_unmap(struct pipe_screen *screen,
395 struct pipe_transfer *transfer)
396 {
397 struct softpipe_texture *spt;
398
399 assert(transfer->texture);
400 spt = softpipe_texture(transfer->texture);
401
402 pipe_buffer_unmap( screen, spt->buffer );
403
404 if (transfer->usage & PIPE_TRANSFER_WRITE) {
405 /* Mark the texture as dirty to expire the tile caches. */
406 spt->timestamp++;
407 }
408 }
409
410
411 static struct pipe_video_surface*
412 softpipe_video_surface_create(struct pipe_screen *screen,
413 enum pipe_video_chroma_format chroma_format,
414 unsigned width, unsigned height)
415 {
416 struct softpipe_video_surface *sp_vsfc;
417 struct pipe_texture template;
418
419 assert(screen);
420 assert(width && height);
421
422 sp_vsfc = CALLOC_STRUCT(softpipe_video_surface);
423 if (!sp_vsfc)
424 return NULL;
425
426 pipe_reference_init(&sp_vsfc->base.reference, 1);
427 sp_vsfc->base.screen = screen;
428 sp_vsfc->base.chroma_format = chroma_format;
429 /*sp_vsfc->base.surface_format = PIPE_VIDEO_SURFACE_FORMAT_VUYA;*/
430 sp_vsfc->base.width = width;
431 sp_vsfc->base.height = height;
432
433 memset(&template, 0, sizeof(struct pipe_texture));
434 template.target = PIPE_TEXTURE_2D;
435 template.format = PIPE_FORMAT_X8R8G8B8_UNORM;
436 template.last_level = 0;
437 /* vl_mpeg12_mc_renderer expects this when it's initialized with pot_buffers=true */
438 template.width0 = util_next_power_of_two(width);
439 template.height0 = util_next_power_of_two(height);
440 template.depth0 = 1;
441 pf_get_block(template.format, &template.block);
442 template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER | PIPE_TEXTURE_USAGE_RENDER_TARGET;
443
444 sp_vsfc->tex = screen->texture_create(screen, &template);
445 if (!sp_vsfc->tex) {
446 FREE(sp_vsfc);
447 return NULL;
448 }
449
450 return &sp_vsfc->base;
451 }
452
453
454 static void
455 softpipe_video_surface_destroy(struct pipe_video_surface *vsfc)
456 {
457 struct softpipe_video_surface *sp_vsfc = softpipe_video_surface(vsfc);
458
459 pipe_texture_reference(&sp_vsfc->tex, NULL);
460 FREE(sp_vsfc);
461 }
462
463
464 void
465 softpipe_init_screen_texture_funcs(struct pipe_screen *screen)
466 {
467 screen->texture_create = softpipe_texture_create;
468 screen->texture_blanket = softpipe_texture_blanket;
469 screen->texture_destroy = softpipe_texture_destroy;
470
471 screen->get_tex_surface = softpipe_get_tex_surface;
472 screen->tex_surface_destroy = softpipe_tex_surface_destroy;
473
474 screen->get_tex_transfer = softpipe_get_tex_transfer;
475 screen->tex_transfer_destroy = softpipe_tex_transfer_destroy;
476 screen->transfer_map = softpipe_transfer_map;
477 screen->transfer_unmap = softpipe_transfer_unmap;
478
479 screen->video_surface_create = softpipe_video_surface_create;
480 screen->video_surface_destroy = softpipe_video_surface_destroy;
481 }
482
483
484 /**
485 * Return pipe_buffer handle and stride for given texture object.
486 * XXX used for???
487 */
488 boolean
489 softpipe_get_texture_buffer( struct pipe_texture *texture,
490 struct pipe_buffer **buf,
491 unsigned *stride )
492 {
493 struct softpipe_texture *tex = (struct softpipe_texture *) texture;
494
495 if (!tex)
496 return FALSE;
497
498 pipe_buffer_reference(buf, tex->buffer);
499
500 if (stride)
501 *stride = tex->stride[0];
502
503 return TRUE;
504 }