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