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