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