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