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