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