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