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