llvmpipe: Remove unnecessary headers.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_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_context.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_inlines.h"
36
37 #include "util/u_format.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40
41 #include "lp_context.h"
42 #include "lp_screen.h"
43 #include "lp_texture.h"
44 #include "lp_tile_size.h"
45 #include "lp_winsys.h"
46
47
48 /**
49 * Conventional allocation path for non-display textures:
50 * Simple, maximally packed layout.
51 */
52 static boolean
53 llvmpipe_texture_layout(struct llvmpipe_screen *screen,
54 struct llvmpipe_texture *lpt)
55 {
56 struct pipe_texture *pt = &lpt->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 unsigned nblocksx, nblocksy;
65
66 /* Allocate storage for whole quads. This is particularly important
67 * for depth surfaces, which are currently stored in a swizzled format.
68 */
69 nblocksx = util_format_get_nblocksx(pt->format, align(width, TILE_SIZE));
70 nblocksy = util_format_get_nblocksy(pt->format, align(height, TILE_SIZE));
71
72 lpt->stride[level] = align(nblocksx * util_format_get_blocksize(pt->format), 16);
73
74 lpt->level_offset[level] = buffer_size;
75
76 buffer_size += (nblocksy *
77 ((pt->target == PIPE_TEXTURE_CUBE) ? 6 : depth) *
78 lpt->stride[level]);
79
80 width = u_minify(width, 1);
81 height = u_minify(height, 1);
82 depth = u_minify(depth, 1);
83 }
84
85 lpt->data = align_malloc(buffer_size, 16);
86
87 return lpt->data != NULL;
88 }
89
90
91
92 static boolean
93 llvmpipe_displaytarget_layout(struct llvmpipe_screen *screen,
94 struct llvmpipe_texture *lpt)
95 {
96 struct llvmpipe_winsys *winsys = screen->winsys;
97
98 /* Round up the surface size to a multiple of the tile size to
99 * avoid tile clipping.
100 */
101 unsigned width = align(lpt->base.width0, TILE_SIZE);
102 unsigned height = align(lpt->base.height0, TILE_SIZE);
103
104 lpt->dt = winsys->displaytarget_create(winsys,
105 lpt->base.format,
106 width, height,
107 16,
108 &lpt->stride[0] );
109
110 return lpt->dt != NULL;
111 }
112
113
114 static struct pipe_texture *
115 llvmpipe_texture_create(struct pipe_screen *_screen,
116 const struct pipe_texture *templat)
117 {
118 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
119 struct llvmpipe_texture *lpt = CALLOC_STRUCT(llvmpipe_texture);
120 if (!lpt)
121 return NULL;
122
123 lpt->base = *templat;
124 pipe_reference_init(&lpt->base.reference, 1);
125 lpt->base.screen = &screen->base;
126
127 /* XXX: The xlib state tracker is brain-dead and will request
128 * PIPE_FORMAT_Z16_UNORM no matter how much we tell it we don't support it.
129 */
130 if (lpt->base.format == PIPE_FORMAT_Z16_UNORM)
131 lpt->base.format = PIPE_FORMAT_Z32_UNORM;
132
133 if (lpt->base.tex_usage & (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
134 PIPE_TEXTURE_USAGE_PRIMARY)) {
135 if (!llvmpipe_displaytarget_layout(screen, lpt))
136 goto fail;
137 }
138 else {
139 if (!llvmpipe_texture_layout(screen, lpt))
140 goto fail;
141 }
142
143 return &lpt->base;
144
145 fail:
146 FREE(lpt);
147 return NULL;
148 }
149
150
151 static struct pipe_texture *
152 llvmpipe_texture_blanket(struct pipe_screen * screen,
153 const struct pipe_texture *base,
154 const unsigned *stride,
155 struct pipe_buffer *buffer)
156 {
157 /* FIXME */
158 #if 0
159 struct llvmpipe_texture *lpt;
160 assert(screen);
161
162 /* Only supports one type */
163 if (base->target != PIPE_TEXTURE_2D ||
164 base->last_level != 0 ||
165 base->depth0 != 1) {
166 return NULL;
167 }
168
169 lpt = CALLOC_STRUCT(llvmpipe_texture);
170 if (!lpt)
171 return NULL;
172
173 lpt->base = *base;
174 pipe_reference_init(&lpt->base.reference, 1);
175 lpt->base.screen = screen;
176 lpt->stride[0] = stride[0];
177
178 pipe_buffer_reference(&lpt->buffer, buffer);
179
180 return &lpt->base;
181 #else
182 debug_printf("llvmpipe_texture_blanket() not implemented!");
183 return NULL;
184 #endif
185 }
186
187
188 static void
189 llvmpipe_texture_destroy(struct pipe_texture *pt)
190 {
191 struct llvmpipe_screen *screen = llvmpipe_screen(pt->screen);
192 struct llvmpipe_texture *lpt = llvmpipe_texture(pt);
193
194 if (lpt->dt) {
195 /* display target */
196 struct llvmpipe_winsys *winsys = screen->winsys;
197 winsys->displaytarget_destroy(winsys, lpt->dt);
198 }
199 else {
200 /* regular texture */
201 align_free(lpt->data);
202 }
203
204 FREE(lpt);
205 }
206
207
208 static struct pipe_surface *
209 llvmpipe_get_tex_surface(struct pipe_screen *screen,
210 struct pipe_texture *pt,
211 unsigned face, unsigned level, unsigned zslice,
212 unsigned usage)
213 {
214 struct llvmpipe_texture *lpt = llvmpipe_texture(pt);
215 struct pipe_surface *ps;
216
217 assert(level <= pt->last_level);
218
219 ps = CALLOC_STRUCT(pipe_surface);
220 if (ps) {
221 pipe_reference_init(&ps->reference, 1);
222 pipe_texture_reference(&ps->texture, pt);
223 ps->format = pt->format;
224 ps->width = u_minify(pt->width0, level);
225 ps->height = u_minify(pt->height0, level);
226 ps->offset = lpt->level_offset[level];
227 ps->usage = usage;
228
229 /* Because we are llvmpipe, anything that the state tracker
230 * thought was going to be done with the GPU will actually get
231 * done with the CPU. Let's adjust the flags to take that into
232 * account.
233 */
234 if (ps->usage & PIPE_BUFFER_USAGE_GPU_WRITE) {
235 /* GPU_WRITE means "render" and that can involve reads (blending) */
236 ps->usage |= PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_CPU_READ;
237 }
238
239 if (ps->usage & PIPE_BUFFER_USAGE_GPU_READ)
240 ps->usage |= PIPE_BUFFER_USAGE_CPU_READ;
241
242 if (ps->usage & (PIPE_BUFFER_USAGE_CPU_WRITE |
243 PIPE_BUFFER_USAGE_GPU_WRITE)) {
244 /* Mark the surface as dirty. */
245 lpt->timestamp++;
246 llvmpipe_screen(screen)->timestamp++;
247 }
248
249 ps->face = face;
250 ps->level = level;
251 ps->zslice = zslice;
252
253 /* XXX shouldn't that rather be
254 tex_height = align(ps->height, 2);
255 to account for alignment done in llvmpipe_texture_layout ?
256 */
257 if (pt->target == PIPE_TEXTURE_CUBE) {
258 unsigned tex_height = ps->height;
259 ps->offset += face * util_format_get_nblocksy(pt->format, tex_height) * lpt->stride[level];
260 }
261 else if (pt->target == PIPE_TEXTURE_3D) {
262 unsigned tex_height = ps->height;
263 ps->offset += zslice * util_format_get_nblocksy(pt->format, tex_height) * lpt->stride[level];
264 }
265 else {
266 assert(face == 0);
267 assert(zslice == 0);
268 }
269 }
270 return ps;
271 }
272
273
274 static void
275 llvmpipe_tex_surface_destroy(struct pipe_surface *surf)
276 {
277 /* Effectively do the texture_update work here - if texture images
278 * needed post-processing to put them into hardware layout, this is
279 * where it would happen. For llvmpipe, nothing to do.
280 */
281 assert(surf->texture);
282 pipe_texture_reference(&surf->texture, NULL);
283 FREE(surf);
284 }
285
286
287 static struct pipe_transfer *
288 llvmpipe_get_tex_transfer(struct pipe_screen *screen,
289 struct pipe_texture *texture,
290 unsigned face, unsigned level, unsigned zslice,
291 enum pipe_transfer_usage usage,
292 unsigned x, unsigned y, unsigned w, unsigned h)
293 {
294 struct llvmpipe_texture *lptex = llvmpipe_texture(texture);
295 struct llvmpipe_transfer *lpt;
296
297 assert(texture);
298 assert(level <= texture->last_level);
299
300 lpt = CALLOC_STRUCT(llvmpipe_transfer);
301 if (lpt) {
302 struct pipe_transfer *pt = &lpt->base;
303 pipe_texture_reference(&pt->texture, texture);
304 pt->x = x;
305 pt->y = y;
306 pt->width = align(w, TILE_SIZE);
307 pt->height = align(h, TILE_SIZE);
308 pt->stride = lptex->stride[level];
309 pt->usage = usage;
310 pt->face = face;
311 pt->level = level;
312 pt->zslice = zslice;
313
314 lpt->offset = lptex->level_offset[level];
315
316 /* XXX shouldn't that rather be
317 tex_height = align(u_minify(texture->height0, level), 2)
318 to account for alignment done in llvmpipe_texture_layout ?
319 */
320 if (texture->target == PIPE_TEXTURE_CUBE) {
321 unsigned tex_height = u_minify(texture->height0, level);
322 lpt->offset += face * util_format_get_nblocksy(texture->format, tex_height) * pt->stride;
323 }
324 else if (texture->target == PIPE_TEXTURE_3D) {
325 unsigned tex_height = u_minify(texture->height0, level);
326 lpt->offset += zslice * util_format_get_nblocksy(texture->format, tex_height) * pt->stride;
327 }
328 else {
329 assert(face == 0);
330 assert(zslice == 0);
331 }
332 return pt;
333 }
334 return NULL;
335 }
336
337
338 static void
339 llvmpipe_tex_transfer_destroy(struct pipe_transfer *transfer)
340 {
341 /* Effectively do the texture_update work here - if texture images
342 * needed post-processing to put them into hardware layout, this is
343 * where it would happen. For llvmpipe, nothing to do.
344 */
345 assert (transfer->texture);
346 pipe_texture_reference(&transfer->texture, NULL);
347 FREE(transfer);
348 }
349
350
351 static void *
352 llvmpipe_transfer_map( struct pipe_screen *_screen,
353 struct pipe_transfer *transfer )
354 {
355 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
356 ubyte *map, *xfer_map;
357 struct llvmpipe_texture *lpt;
358 enum pipe_format format;
359
360 assert(transfer->texture);
361 lpt = llvmpipe_texture(transfer->texture);
362 format = lpt->base.format;
363
364 if (lpt->dt) {
365 /* display target */
366 struct llvmpipe_winsys *winsys = screen->winsys;
367
368 map = winsys->displaytarget_map(winsys, lpt->dt,
369 pipe_transfer_buffer_flags(transfer));
370 if (map == NULL)
371 return NULL;
372 }
373 else {
374 /* regular texture */
375 map = lpt->data;
376 }
377
378 /* May want to different things here depending on read/write nature
379 * of the map:
380 */
381 if (transfer->texture && (transfer->usage & PIPE_TRANSFER_WRITE)) {
382 /* Do something to notify sharing contexts of a texture change.
383 */
384 screen->timestamp++;
385 }
386
387 xfer_map = map + llvmpipe_transfer(transfer)->offset +
388 transfer->y / util_format_get_blockheight(format) * transfer->stride +
389 transfer->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
390 /*printf("map = %p xfer map = %p\n", map, xfer_map);*/
391 return xfer_map;
392 }
393
394
395 static void
396 llvmpipe_transfer_unmap(struct pipe_screen *screen,
397 struct pipe_transfer *transfer)
398 {
399 struct llvmpipe_screen *lp_screen = llvmpipe_screen(screen);
400 struct llvmpipe_texture *lpt;
401
402 assert(transfer->texture);
403 lpt = llvmpipe_texture(transfer->texture);
404
405 if (lpt->dt) {
406 /* display target */
407 struct llvmpipe_winsys *winsys = lp_screen->winsys;
408 winsys->displaytarget_unmap(winsys, lpt->dt);
409 }
410 }
411
412
413 void
414 llvmpipe_init_screen_texture_funcs(struct pipe_screen *screen)
415 {
416 screen->texture_create = llvmpipe_texture_create;
417 screen->texture_blanket = llvmpipe_texture_blanket;
418 screen->texture_destroy = llvmpipe_texture_destroy;
419
420 screen->get_tex_surface = llvmpipe_get_tex_surface;
421 screen->tex_surface_destroy = llvmpipe_tex_surface_destroy;
422
423 screen->get_tex_transfer = llvmpipe_get_tex_transfer;
424 screen->tex_transfer_destroy = llvmpipe_tex_transfer_destroy;
425 screen->transfer_map = llvmpipe_transfer_map;
426 screen->transfer_unmap = llvmpipe_transfer_unmap;
427 }