r300g: add precalculating of pixel pitch, add a new NPOT flag
[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 #include "r300_screen.h"
31
32 static void r300_setup_texture_state(struct r300_texture* tex, boolean is_r500)
33 {
34 struct r300_texture_state* state = &tex->state;
35 struct pipe_texture *pt = &tex->tex;
36
37 state->format0 = R300_TX_WIDTH((pt->width[0] - 1) & 0x7ff) |
38 R300_TX_HEIGHT((pt->height[0] - 1) & 0x7ff);
39
40 if (tex->is_npot) {
41 /* rectangles love this */
42 state->format0 |= R300_TX_PITCH_EN;
43 state->format2 = (tex->pitch[0] - 1) & 0x1fff;
44 } else {
45 /* power of two textures (3D, mipmaps, and no pitch) */
46 state->format0 |= R300_TX_DEPTH(util_logbase2(pt->depth[0]) & 0xf) |
47 R300_TX_NUM_LEVELS(pt->last_level & 0xf);
48 }
49
50 state->format1 = r300_translate_texformat(pt->format);
51 if (pt->target == PIPE_TEXTURE_CUBE) {
52 state->format1 |= R300_TX_FORMAT_CUBIC_MAP;
53 }
54 if (pt->target == PIPE_TEXTURE_3D) {
55 state->format1 |= R300_TX_FORMAT_3D;
56 }
57
58 /* large textures on r500 */
59 if (is_r500)
60 {
61 if (pt->width[0] > 2048) {
62 state->format2 |= R500_TXWIDTH_BIT11;
63 }
64 if (pt->height[0] > 2048) {
65 state->format2 |= R500_TXHEIGHT_BIT11;
66 }
67 }
68 assert(is_r500 || (pt->width[0] <= 2048 && pt->height[0] <= 2048));
69
70 debug_printf("r300: Set texture state (%dx%d, %d levels)\n",
71 pt->width[0], pt->height[0], pt->last_level);
72 }
73
74 unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
75 unsigned zslice, unsigned face)
76 {
77 unsigned offset = tex->offset[level];
78
79 switch (tex->tex.target) {
80 case PIPE_TEXTURE_3D:
81 assert(face == 0);
82 return offset + zslice * tex->layer_size[level];
83
84 case PIPE_TEXTURE_CUBE:
85 assert(zslice == 0);
86 return offset + face * tex->layer_size[level];
87
88 default:
89 assert(zslice == 0 && face == 0);
90 return offset;
91 }
92 }
93
94 /**
95 * Return the stride, in bytes, of the texture images of the given texture
96 * at the given level.
97 */
98 unsigned r300_texture_get_stride(struct r300_texture* tex, unsigned level)
99 {
100 if (tex->stride_override)
101 return tex->stride_override;
102
103 if (level > tex->tex.last_level) {
104 debug_printf("%s: level (%u) > last_level (%u)\n", __FUNCTION__,
105 level, tex->tex.last_level);
106 return 0;
107 }
108
109 return align(pf_get_stride(&tex->tex.block, tex->tex.width[level]), 32);
110 }
111
112 static void r300_setup_miptree(struct r300_texture* tex)
113 {
114 struct pipe_texture* base = &tex->tex;
115 int stride, size, layer_size;
116 int i;
117
118 for (i = 0; i <= base->last_level; i++) {
119 if (i > 0) {
120 base->width[i] = minify(base->width[i-1]);
121 base->height[i] = minify(base->height[i-1]);
122 base->depth[i] = minify(base->depth[i-1]);
123 }
124
125 base->nblocksx[i] = pf_get_nblocksx(&base->block, base->width[i]);
126 base->nblocksy[i] = pf_get_nblocksy(&base->block, base->height[i]);
127
128 stride = r300_texture_get_stride(tex, i);
129 layer_size = stride * base->nblocksy[i];
130
131 if (base->target == PIPE_TEXTURE_CUBE)
132 size = layer_size * 6;
133 else
134 size = layer_size * base->depth[i];
135
136 tex->offset[i] = align(tex->size, 32);
137 tex->size = tex->offset[i] + size;
138 tex->layer_size[i] = layer_size;
139
140 if (tex->is_npot) {
141 tex->pitch[i] = stride / base->block.size;
142 } else {
143 tex->pitch[i] = base->width[i];
144 }
145
146 debug_printf("r300: Texture miptree: Level %d "
147 "(%dx%dx%d px, pitch %d bytes)\n",
148 i, base->width[i], base->height[i], base->depth[i],
149 stride);
150 }
151 }
152
153 static void r300_setup_flags(struct r300_texture* tex)
154 {
155 tex->is_npot = !util_is_power_of_two(tex->tex.width[0]) ||
156 !util_is_power_of_two(tex->tex.height[0]);
157 }
158
159 /* Create a new texture. */
160 static struct pipe_texture*
161 r300_texture_create(struct pipe_screen* screen,
162 const struct pipe_texture* template)
163 {
164 struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
165
166 if (!tex) {
167 return NULL;
168 }
169
170 tex->tex = *template;
171 pipe_reference_init(&tex->tex.reference, 1);
172 tex->tex.screen = screen;
173
174 r300_setup_flags(tex);
175 r300_setup_miptree(tex);
176 r300_setup_texture_state(tex, r300_screen(screen)->caps->is_r500);
177
178 tex->buffer = screen->buffer_create(screen, 1024,
179 PIPE_BUFFER_USAGE_PIXEL,
180 tex->size);
181
182 if (!tex->buffer) {
183 FREE(tex);
184 return NULL;
185 }
186
187 return (struct pipe_texture*)tex;
188 }
189
190 static void r300_texture_destroy(struct pipe_texture* texture)
191 {
192 struct r300_texture* tex = (struct r300_texture*)texture;
193
194 pipe_buffer_reference(&tex->buffer, NULL);
195
196 FREE(tex);
197 }
198
199 static struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
200 struct pipe_texture* texture,
201 unsigned face,
202 unsigned level,
203 unsigned zslice,
204 unsigned flags)
205 {
206 struct r300_texture* tex = (struct r300_texture*)texture;
207 struct pipe_surface* surface = CALLOC_STRUCT(pipe_surface);
208 unsigned offset;
209
210 offset = r300_texture_get_offset(tex, level, zslice, face);
211
212 if (surface) {
213 pipe_reference_init(&surface->reference, 1);
214 pipe_texture_reference(&surface->texture, texture);
215 surface->format = texture->format;
216 surface->width = texture->width[level];
217 surface->height = texture->height[level];
218 surface->offset = offset;
219 surface->usage = flags;
220 surface->zslice = zslice;
221 surface->texture = texture;
222 surface->face = face;
223 surface->level = level;
224 }
225
226 return surface;
227 }
228
229 static void r300_tex_surface_destroy(struct pipe_surface* s)
230 {
231 pipe_texture_reference(&s->texture, NULL);
232 FREE(s);
233 }
234
235 static struct pipe_texture*
236 r300_texture_blanket(struct pipe_screen* screen,
237 const struct pipe_texture* base,
238 const unsigned* stride,
239 struct pipe_buffer* buffer)
240 {
241 struct r300_texture* tex;
242
243 /* Support only 2D textures without mipmaps */
244 if (base->target != PIPE_TEXTURE_2D ||
245 base->depth[0] != 1 ||
246 base->last_level != 0) {
247 return NULL;
248 }
249
250 tex = CALLOC_STRUCT(r300_texture);
251 if (!tex) {
252 return NULL;
253 }
254
255 tex->tex = *base;
256 pipe_reference_init(&tex->tex.reference, 1);
257 tex->tex.screen = screen;
258
259 tex->stride_override = *stride;
260 tex->pitch[0] = *stride / base->block.size;
261
262 r300_setup_flags(tex);
263 r300_setup_texture_state(tex, r300_screen(screen)->caps->is_r500);
264
265 pipe_buffer_reference(&tex->buffer, buffer);
266
267 return (struct pipe_texture*)tex;
268 }
269
270 static struct pipe_video_surface *
271 r300_video_surface_create(struct pipe_screen *screen,
272 enum pipe_video_chroma_format chroma_format,
273 unsigned width, unsigned height)
274 {
275 struct r300_video_surface *r300_vsfc;
276 struct pipe_texture template;
277
278 assert(screen);
279 assert(width && height);
280
281 r300_vsfc = CALLOC_STRUCT(r300_video_surface);
282 if (!r300_vsfc)
283 return NULL;
284
285 pipe_reference_init(&r300_vsfc->base.reference, 1);
286 r300_vsfc->base.screen = screen;
287 r300_vsfc->base.chroma_format = chroma_format;
288 r300_vsfc->base.width = width;
289 r300_vsfc->base.height = height;
290
291 memset(&template, 0, sizeof(struct pipe_texture));
292 template.target = PIPE_TEXTURE_2D;
293 template.format = PIPE_FORMAT_X8R8G8B8_UNORM;
294 template.last_level = 0;
295 template.width[0] = util_next_power_of_two(width);
296 template.height[0] = util_next_power_of_two(height);
297 template.depth[0] = 1;
298 pf_get_block(template.format, &template.block);
299 template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER |
300 PIPE_TEXTURE_USAGE_RENDER_TARGET;
301
302 r300_vsfc->tex = screen->texture_create(screen, &template);
303 if (!r300_vsfc->tex)
304 {
305 FREE(r300_vsfc);
306 return NULL;
307 }
308
309 return &r300_vsfc->base;
310 }
311
312 static void r300_video_surface_destroy(struct pipe_video_surface *vsfc)
313 {
314 struct r300_video_surface *r300_vsfc = r300_video_surface(vsfc);
315 pipe_texture_reference(&r300_vsfc->tex, NULL);
316 FREE(r300_vsfc);
317 }
318
319 void r300_init_screen_texture_functions(struct pipe_screen* screen)
320 {
321 screen->texture_create = r300_texture_create;
322 screen->texture_destroy = r300_texture_destroy;
323 screen->get_tex_surface = r300_get_tex_surface;
324 screen->tex_surface_destroy = r300_tex_surface_destroy;
325 screen->texture_blanket = r300_texture_blanket;
326
327 screen->video_surface_create = r300_video_surface_create;
328 screen->video_surface_destroy= r300_video_surface_destroy;
329 }
330
331 boolean r300_get_texture_buffer(struct pipe_texture* texture,
332 struct pipe_buffer** buffer,
333 unsigned* stride)
334 {
335 struct r300_texture* tex = (struct r300_texture*)texture;
336 if (!tex) {
337 return FALSE;
338 }
339
340 pipe_buffer_reference(buffer, tex->buffer);
341
342 if (stride) {
343 *stride = r300_texture_get_stride(tex, 0);
344 }
345
346 return TRUE;
347 }