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