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