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