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