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