Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[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/p_util.h"
37 #include "pipe/p_winsys.h"
38
39 #include "sp_context.h"
40 #include "sp_state.h"
41 #include "sp_texture.h"
42 #include "sp_tile_cache.h"
43
44
45 /* Simple, maximally packed layout.
46 */
47
48 static unsigned minify( unsigned d )
49 {
50 return MAX2(1, d>>1);
51 }
52
53
54 static void
55 softpipe_texture_layout(struct softpipe_texture * spt)
56 {
57 struct pipe_texture *pt = &spt->base;
58 unsigned level;
59 unsigned width = pt->width[0];
60 unsigned height = pt->height[0];
61 unsigned depth = pt->depth[0];
62
63 spt->buffer_size = 0;
64
65 for (level = 0; level <= pt->last_level; level++) {
66 pt->width[level] = width;
67 pt->height[level] = height;
68 pt->depth[level] = depth;
69
70 spt->level_offset[level] = spt->buffer_size;
71
72 spt->buffer_size += ((pt->compressed) ? MAX2(1, height/4) : height) *
73 ((pt->target == PIPE_TEXTURE_CUBE) ? 6 : depth) *
74 width * pt->cpp;
75
76 width = minify(width);
77 height = minify(height);
78 depth = minify(depth);
79 }
80 }
81
82
83 static struct pipe_texture *
84 softpipe_texture_create_screen(struct pipe_screen *screen,
85 const struct pipe_texture *templat)
86 {
87 struct pipe_winsys *ws = screen->winsys;
88 struct softpipe_texture *spt = CALLOC_STRUCT(softpipe_texture);
89 if (!spt)
90 return NULL;
91
92 spt->base = *templat;
93 spt->base.refcount = 1;
94 spt->base.screen = screen;
95
96 softpipe_texture_layout(spt);
97
98 spt->buffer = ws->buffer_create(ws, 32,
99 PIPE_BUFFER_USAGE_PIXEL,
100 spt->buffer_size);
101 if (!spt->buffer) {
102 FREE(spt);
103 return NULL;
104 }
105
106 assert(spt->base.refcount == 1);
107
108 return &spt->base;
109 }
110
111
112 static void
113 softpipe_texture_release_screen(struct pipe_screen *screen,
114 struct pipe_texture **pt)
115 {
116 if (!*pt)
117 return;
118
119 /*
120 DBG("%s %p refcount will be %d\n",
121 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
122 */
123 if (--(*pt)->refcount <= 0) {
124 struct softpipe_texture *spt = softpipe_texture(*pt);
125
126 /*
127 DBG("%s deleting %p\n", __FUNCTION__, (void *) spt);
128 */
129
130 pipe_buffer_reference(screen->winsys, &spt->buffer, NULL);
131
132 FREE(spt);
133 }
134 *pt = NULL;
135 }
136
137
138 static struct pipe_surface *
139 softpipe_get_tex_surface_screen(struct pipe_screen *screen,
140 struct pipe_texture *pt,
141 unsigned face, unsigned level, unsigned zslice)
142 {
143 struct pipe_winsys *ws = screen->winsys;
144 struct softpipe_texture *spt = softpipe_texture(pt);
145 struct pipe_surface *ps;
146
147 assert(level <= pt->last_level);
148
149 ps = ws->surface_alloc(ws);
150 if (ps) {
151 assert(ps->refcount);
152 assert(ps->winsys);
153 pipe_buffer_reference(ws, &ps->buffer, spt->buffer);
154 ps->format = pt->format;
155 ps->cpp = pt->cpp;
156 ps->width = pt->width[level];
157 ps->height = pt->height[level];
158 ps->pitch = ps->width;
159 ps->offset = spt->level_offset[level];
160
161 if (pt->target == PIPE_TEXTURE_CUBE || pt->target == PIPE_TEXTURE_3D) {
162 ps->offset += ((pt->target == PIPE_TEXTURE_CUBE) ? face : zslice) *
163 (pt->compressed ? ps->height/4 : ps->height) *
164 ps->width * ps->cpp;
165 }
166 else {
167 assert(face == 0);
168 assert(zslice == 0);
169 }
170 }
171 return ps;
172 }
173
174
175 static void
176 softpipe_texture_update(struct pipe_context *pipe,
177 struct pipe_texture *texture)
178 {
179 struct softpipe_context *softpipe = softpipe_context(pipe);
180 uint unit;
181 for (unit = 0; unit < softpipe->num_textures; unit++) {
182 if (softpipe->texture[unit] == texture) {
183 sp_flush_tile_cache(softpipe, softpipe->tex_cache[unit]);
184 }
185 }
186 }
187
188
189 void
190 softpipe_init_texture_funcs( struct softpipe_context *softpipe )
191 {
192 softpipe->pipe.texture_update = softpipe_texture_update;
193 }
194
195
196 void
197 softpipe_init_screen_texture_funcs(struct pipe_screen *screen)
198 {
199 screen->texture_create = softpipe_texture_create_screen;
200 screen->texture_release = softpipe_texture_release_screen;
201 screen->get_tex_surface = softpipe_get_tex_surface_screen;
202 }