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