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