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