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