Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / drivers / r300 / r300_texture.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "pipe/p_screen.h"
24
25 #include "util/u_math.h"
26 #include "util/u_memory.h"
27
28 #include "r300_context.h"
29 #include "r300_texture.h"
30
31 static void r300_setup_texture_state(struct r300_texture* tex)
32 {
33 struct r300_texture_state* state = &tex->state;
34 struct pipe_texture *pt = &tex->tex;
35
36 state->format0 = R300_TX_WIDTH((pt->width[0] - 1) & 0x7ff) |
37 R300_TX_HEIGHT((pt->height[0] - 1) & 0x7ff) |
38 R300_TX_DEPTH(util_logbase2(pt->depth[0]) & 0xf) |
39 R300_TX_NUM_LEVELS(pt->last_level) |
40 R300_TX_PITCH_EN;
41
42 /* XXX */
43 state->format1 = r300_translate_texformat(pt->format);
44 if (pt->target == PIPE_TEXTURE_CUBE) {
45 state->format1 |= R300_TX_FORMAT_CUBIC_MAP;
46 }
47 if (pt->target == PIPE_TEXTURE_3D) {
48 state->format1 |= R300_TX_FORMAT_3D;
49 }
50
51 state->format2 = (r300_texture_get_stride(tex, 0) / pt->block.size) - 1;
52
53 /* Don't worry about accidentally setting this bit on non-r500;
54 * the kernel should catch it. */
55 if (pt->width[0] > 2048) {
56 state->format2 |= R500_TXWIDTH_BIT11;
57 }
58 if (pt->height[0] > 2048) {
59 state->format2 |= R500_TXHEIGHT_BIT11;
60 }
61
62 debug_printf("r300: Set texture state (%dx%d, %d levels)\n",
63 pt->width[0], pt->height[0], pt->last_level);
64 }
65
66 /**
67 * Return the stride, in bytes, of the texture images of the given texture
68 * at the given level.
69 */
70 unsigned r300_texture_get_stride(struct r300_texture* tex, unsigned level)
71 {
72 if (tex->stride_override)
73 return tex->stride_override;
74
75 if (level > tex->tex.last_level) {
76 debug_printf("%s: level (%u) > last_level (%u)\n", __FUNCTION__,
77 level, tex->tex.last_level);
78 return 0;
79 }
80
81 return align(pf_get_stride(&tex->tex.block, tex->tex.width[level]), 32);
82 }
83
84 static void r300_setup_miptree(struct r300_texture* tex)
85 {
86 struct pipe_texture* base = &tex->tex;
87 int stride, size;
88 int i;
89
90 for (i = 0; i <= base->last_level; i++) {
91 if (i > 0) {
92 base->width[i] = minify(base->width[i-1]);
93 base->height[i] = minify(base->height[i-1]);
94 base->depth[i] = minify(base->depth[i-1]);
95 }
96
97 base->nblocksx[i] = pf_get_nblocksx(&base->block, base->width[i]);
98 base->nblocksy[i] = pf_get_nblocksy(&base->block, base->height[i]);
99
100 stride = r300_texture_get_stride(tex, i);
101 size = stride * base->nblocksy[i] * base->depth[i];
102
103 tex->offset[i] = align(tex->size, 32);
104 tex->size = tex->offset[i] + size;
105
106 debug_printf("r300: Texture miptree: Level %d "
107 "(%dx%dx%d px, pitch %d bytes)\n",
108 i, base->width[i], base->height[i], base->depth[i],
109 stride);
110 }
111 }
112
113 /* Create a new texture. */
114 static struct pipe_texture*
115 r300_texture_create(struct pipe_screen* screen,
116 const struct pipe_texture* template)
117 {
118 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
119
120 if (!tex) {
121 return NULL;
122 }
123
124 tex->tex = *template;
125 pipe_reference_init(&tex->tex.reference, 1);
126 tex->tex.screen = screen;
127
128 r300_setup_miptree(tex);
129
130 r300_setup_texture_state(tex);
131
132 tex->buffer = screen->buffer_create(screen, 1024,
133 PIPE_BUFFER_USAGE_PIXEL,
134 tex->size);
135
136 if (!tex->buffer) {
137 FREE(tex);
138 return NULL;
139 }
140
141 return (struct pipe_texture*)tex;
142 }
143
144 static void r300_texture_destroy(struct pipe_texture* texture)
145 {
146 struct r300_texture* tex = (struct r300_texture*)texture;
147
148 pipe_buffer_reference(&tex->buffer, NULL);
149
150 FREE(tex);
151 }
152
153 static struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
154 struct pipe_texture* texture,
155 unsigned face,
156 unsigned level,
157 unsigned zslice,
158 unsigned flags)
159 {
160 struct r300_texture* tex = (struct r300_texture*)texture;
161 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
162 unsigned offset;
163
164 /* XXX this is certainly dependent on tex target */
165 offset = tex->offset[level];
166
167 if (surface) {
168 pipe_reference_init(&surface->reference, 1);
169 pipe_texture_reference(&surface->texture, texture);
170 surface->format = texture->format;
171 surface->width = texture->width[level];
172 surface->height = texture->height[level];
173 surface->offset = offset;
174 surface->usage = flags;
175 }
176
177 return surface;
178 }
179
180 static void r300_tex_surface_destroy(struct pipe_surface* s)
181 {
182 pipe_texture_reference(&s->texture, NULL);
183 FREE(s);
184 }
185
186 static struct pipe_texture*
187 r300_texture_blanket(struct pipe_screen* screen,
188 const struct pipe_texture* base,
189 const unsigned* stride,
190 struct pipe_buffer* buffer)
191 {
192 struct r300_texture* tex;
193
194 if (base->target != PIPE_TEXTURE_2D ||
195 base->depth[0] != 1) {
196 return NULL;
197 }
198
199 tex = CALLOC_STRUCT(r300_texture);
200 if (!tex) {
201 return NULL;
202 }
203
204 tex->tex = *base;
205 pipe_reference_init(&tex->tex.reference, 1);
206 tex->tex.screen = screen;
207
208 tex->stride_override = *stride;
209
210 r300_setup_texture_state(tex);
211
212 pipe_buffer_reference(&tex->buffer, buffer);
213
214 return (struct pipe_texture*)tex;
215 }
216
217 static struct pipe_video_surface *
218 r300_video_surface_create(struct pipe_screen *screen,
219 enum pipe_video_chroma_format chroma_format,
220 unsigned width, unsigned height)
221 {
222 struct r300_video_surface *r300_vsfc;
223 struct pipe_texture template;
224
225 assert(screen);
226 assert(width && height);
227
228 r300_vsfc = CALLOC_STRUCT(r300_video_surface);
229 if (!r300_vsfc)
230 return NULL;
231
232 pipe_reference_init(&r300_vsfc->base.reference, 1);
233 r300_vsfc->base.screen = screen;
234 r300_vsfc->base.chroma_format = chroma_format;
235 r300_vsfc->base.width = width;
236 r300_vsfc->base.height = height;
237
238 memset(&template, 0, sizeof(struct pipe_texture));
239 template.target = PIPE_TEXTURE_2D;
240 template.format = PIPE_FORMAT_X8R8G8B8_UNORM;
241 template.last_level = 0;
242 template.width[0] = util_next_power_of_two(width);
243 template.height[0] = util_next_power_of_two(height);
244 template.depth[0] = 1;
245 pf_get_block(template.format, &template.block);
246 template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER |
247 PIPE_TEXTURE_USAGE_RENDER_TARGET;
248
249 r300_vsfc->tex = screen->texture_create(screen, &template);
250 if (!r300_vsfc->tex)
251 {
252 FREE(r300_vsfc);
253 return NULL;
254 }
255
256 return &r300_vsfc->base;
257 }
258
259 static void r300_video_surface_destroy(struct pipe_video_surface *vsfc)
260 {
261 struct r300_video_surface *r300_vsfc = r300_video_surface(vsfc);
262 pipe_texture_reference(&r300_vsfc->tex, NULL);
263 FREE(r300_vsfc);
264 }
265
266 void r300_init_screen_texture_functions(struct pipe_screen* screen)
267 {
268 screen->texture_create = r300_texture_create;
269 screen->texture_destroy = r300_texture_destroy;
270 screen->get_tex_surface = r300_get_tex_surface;
271 screen->tex_surface_destroy = r300_tex_surface_destroy;
272 screen->texture_blanket = r300_texture_blanket;
273
274 screen->video_surface_create = r300_video_surface_create;
275 screen->video_surface_destroy= r300_video_surface_destroy;
276 }
277
278 boolean r300_get_texture_buffer(struct pipe_texture* texture,
279 struct pipe_buffer** buffer,
280 unsigned* stride)
281 {
282 struct r300_texture* tex = (struct r300_texture*)texture;
283 if (!tex) {
284 return FALSE;
285 }
286
287 pipe_buffer_reference(buffer, tex->buffer);
288
289 if (stride) {
290 *stride = r300_texture_get_stride(tex, 0);
291 }
292
293 return TRUE;
294 }