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