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