Merge branch '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 static struct pipe_texture *
115 softpipe_texture_create(struct pipe_screen *screen,
116 const struct pipe_texture *template)
117 {
118 struct softpipe_texture *spt = CALLOC_STRUCT(softpipe_texture);
119 if (!spt)
120 return NULL;
121
122 spt->base = *template;
123 pipe_reference_init(&spt->base.reference, 1);
124 spt->base.screen = screen;
125
126 spt->pot = (util_is_power_of_two(template->width[0]) &&
127 util_is_power_of_two(template->height[0]) &&
128 util_is_power_of_two(template->depth[0]));
129
130 if (spt->base.tex_usage & (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
131 PIPE_TEXTURE_USAGE_PRIMARY)) {
132 if (!softpipe_displaytarget_layout(screen, spt))
133 goto fail;
134 }
135 else {
136 if (!softpipe_texture_layout(screen, spt))
137 goto fail;
138 }
139
140 return &spt->base;
141
142 fail:
143 FREE(spt);
144 return NULL;
145 }
146
147
148 static struct pipe_texture *
149 softpipe_texture_blanket(struct pipe_screen * screen,
150 const struct pipe_texture *base,
151 const unsigned *stride,
152 struct pipe_buffer *buffer)
153 {
154 struct softpipe_texture *spt;
155 assert(screen);
156
157 /* Only supports one type */
158 if (base->target != PIPE_TEXTURE_2D ||
159 base->last_level != 0 ||
160 base->depth[0] != 1) {
161 return NULL;
162 }
163
164 spt = CALLOC_STRUCT(softpipe_texture);
165 if (!spt)
166 return NULL;
167
168 spt->base = *base;
169 pipe_reference_init(&spt->base.reference, 1);
170 spt->base.screen = screen;
171 spt->base.nblocksx[0] = pf_get_nblocksx(&spt->base.block, spt->base.width[0]);
172 spt->base.nblocksy[0] = pf_get_nblocksy(&spt->base.block, spt->base.height[0]);
173 spt->stride[0] = stride[0];
174
175 pipe_buffer_reference(&spt->buffer, buffer);
176
177 return &spt->base;
178 }
179
180
181 static void
182 softpipe_texture_destroy(struct pipe_texture *pt)
183 {
184 struct softpipe_texture *spt = softpipe_texture(pt);
185
186 pipe_buffer_reference(&spt->buffer, NULL);
187 FREE(spt);
188 }
189
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 = pt->width[level];
208 ps->height = pt->height[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 * pt->nblocksy[level] * spt->stride[level];
238 }
239 else if (pt->target == PIPE_TEXTURE_3D) {
240 ps->offset += zslice * pt->nblocksy[level] * spt->stride[level];
241 }
242 else {
243 assert(face == 0);
244 assert(zslice == 0);
245 }
246 }
247 return ps;
248 }
249
250
251 static void
252 softpipe_tex_surface_destroy(struct pipe_surface *surf)
253 {
254 /* Effectively do the texture_update work here - if texture images
255 * needed post-processing to put them into hardware layout, this is
256 * where it would happen. For softpipe, nothing to do.
257 */
258 assert(surf->texture);
259 pipe_texture_reference(&surf->texture, NULL);
260 FREE(surf);
261 }
262
263
264 static struct pipe_transfer *
265 softpipe_get_tex_transfer(struct pipe_screen *screen,
266 struct pipe_texture *texture,
267 unsigned face, unsigned level, unsigned zslice,
268 enum pipe_transfer_usage usage,
269 unsigned x, unsigned y, unsigned w, unsigned h)
270 {
271 struct softpipe_texture *sptex = softpipe_texture(texture);
272 struct softpipe_transfer *spt;
273
274 assert(texture);
275 assert(level <= texture->last_level);
276
277 spt = CALLOC_STRUCT(softpipe_transfer);
278 if (spt) {
279 struct pipe_transfer *pt = &spt->base;
280 pipe_texture_reference(&pt->texture, texture);
281 pt->format = texture->format;
282 pt->block = texture->block;
283 pt->x = x;
284 pt->y = y;
285 pt->width = w;
286 pt->height = h;
287 pt->nblocksx = texture->nblocksx[level];
288 pt->nblocksy = texture->nblocksy[level];
289 pt->stride = sptex->stride[level];
290 pt->usage = usage;
291 pt->face = face;
292 pt->level = level;
293 pt->zslice = zslice;
294
295 spt->offset = sptex->level_offset[level];
296
297 if (texture->target == PIPE_TEXTURE_CUBE) {
298 spt->offset += face * pt->nblocksy * pt->stride;
299 }
300 else if (texture->target == PIPE_TEXTURE_3D) {
301 spt->offset += zslice * pt->nblocksy * pt->stride;
302 }
303 else {
304 assert(face == 0);
305 assert(zslice == 0);
306 }
307 return pt;
308 }
309 return NULL;
310 }
311
312
313 static void
314 softpipe_tex_transfer_destroy(struct pipe_transfer *transfer)
315 {
316 /* Effectively do the texture_update work here - if texture images
317 * needed post-processing to put them into hardware layout, this is
318 * where it would happen. For softpipe, nothing to do.
319 */
320 assert (transfer->texture);
321 pipe_texture_reference(&transfer->texture, NULL);
322 FREE(transfer);
323 }
324
325
326 static void *
327 softpipe_transfer_map( struct pipe_screen *screen,
328 struct pipe_transfer *transfer )
329 {
330 ubyte *map, *xfer_map;
331 struct softpipe_texture *spt;
332
333 assert(transfer->texture);
334 spt = softpipe_texture(transfer->texture);
335
336 map = pipe_buffer_map(screen, spt->buffer, pipe_transfer_buffer_flags(transfer));
337 if (map == NULL)
338 return NULL;
339
340 /* May want to different things here depending on read/write nature
341 * of the map:
342 */
343 if (transfer->texture && (transfer->usage & PIPE_TRANSFER_WRITE)) {
344 /* Do something to notify sharing contexts of a texture change.
345 * In softpipe, that would mean flushing the texture cache.
346 */
347 softpipe_screen(screen)->timestamp++;
348 }
349
350 xfer_map = map + softpipe_transfer(transfer)->offset +
351 transfer->y / transfer->block.height * transfer->stride +
352 transfer->x / transfer->block.width * transfer->block.size;
353 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
354 return xfer_map;
355 }
356
357
358 static void
359 softpipe_transfer_unmap(struct pipe_screen *screen,
360 struct pipe_transfer *transfer)
361 {
362 struct softpipe_texture *spt;
363
364 assert(transfer->texture);
365 spt = softpipe_texture(transfer->texture);
366
367 pipe_buffer_unmap( screen, spt->buffer );
368
369 if (transfer->usage & PIPE_TRANSFER_WRITE) {
370 /* Mark the texture as dirty to expire the tile caches. */
371 spt->timestamp++;
372 }
373 }
374
375 static struct pipe_video_surface*
376 softpipe_video_surface_create(struct pipe_screen *screen,
377 enum pipe_video_chroma_format chroma_format,
378 unsigned width, unsigned height)
379 {
380 struct softpipe_video_surface *sp_vsfc;
381 struct pipe_texture template;
382
383 assert(screen);
384 assert(width && height);
385
386 sp_vsfc = CALLOC_STRUCT(softpipe_video_surface);
387 if (!sp_vsfc)
388 return NULL;
389
390 pipe_reference_init(&sp_vsfc->base.reference, 1);
391 sp_vsfc->base.screen = screen;
392 sp_vsfc->base.chroma_format = chroma_format;
393 /*sp_vsfc->base.surface_format = PIPE_VIDEO_SURFACE_FORMAT_VUYA;*/
394 sp_vsfc->base.width = width;
395 sp_vsfc->base.height = height;
396
397 memset(&template, 0, sizeof(struct pipe_texture));
398 template.target = PIPE_TEXTURE_2D;
399 template.format = PIPE_FORMAT_X8R8G8B8_UNORM;
400 template.last_level = 0;
401 /* vl_mpeg12_mc_renderer expects this when it's initialized with pot_buffers=true */
402 template.width[0] = util_next_power_of_two(width);
403 template.height[0] = util_next_power_of_two(height);
404 template.depth[0] = 1;
405 pf_get_block(template.format, &template.block);
406 template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER | PIPE_TEXTURE_USAGE_RENDER_TARGET;
407
408 sp_vsfc->tex = screen->texture_create(screen, &template);
409 if (!sp_vsfc->tex) {
410 FREE(sp_vsfc);
411 return NULL;
412 }
413
414 return &sp_vsfc->base;
415 }
416
417
418 static void
419 softpipe_video_surface_destroy(struct pipe_video_surface *vsfc)
420 {
421 struct softpipe_video_surface *sp_vsfc = softpipe_video_surface(vsfc);
422
423 pipe_texture_reference(&sp_vsfc->tex, NULL);
424 FREE(sp_vsfc);
425 }
426
427
428 void
429 softpipe_init_screen_texture_funcs(struct pipe_screen *screen)
430 {
431 screen->texture_create = softpipe_texture_create;
432 screen->texture_blanket = softpipe_texture_blanket;
433 screen->texture_destroy = softpipe_texture_destroy;
434
435 screen->get_tex_surface = softpipe_get_tex_surface;
436 screen->tex_surface_destroy = softpipe_tex_surface_destroy;
437
438 screen->get_tex_transfer = softpipe_get_tex_transfer;
439 screen->tex_transfer_destroy = softpipe_tex_transfer_destroy;
440 screen->transfer_map = softpipe_transfer_map;
441 screen->transfer_unmap = softpipe_transfer_unmap;
442
443 screen->video_surface_create = softpipe_video_surface_create;
444 screen->video_surface_destroy = softpipe_video_surface_destroy;
445 }
446
447
448 boolean
449 softpipe_get_texture_buffer( struct pipe_texture *texture,
450 struct pipe_buffer **buf,
451 unsigned *stride )
452 {
453 struct softpipe_texture *tex = (struct softpipe_texture *) texture;
454
455 if (!tex)
456 return FALSE;
457
458 pipe_buffer_reference(buf, tex->buffer);
459
460 if (stride)
461 *stride = tex->stride[0];
462
463 return TRUE;
464 }