r300g: disable depth clamp for now
[mesa.git] / src / gallium / drivers / r300 / r300_texture_desc.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 #include "r300_texture_desc.h"
25
26 #include "r300_context.h"
27 #include "r300_winsys.h"
28
29 #include "util/u_format.h"
30
31 /* Returns the number of pixels that the texture should be aligned to
32 * in the given dimension. */
33 unsigned r300_get_pixel_alignment(enum pipe_format format,
34 unsigned num_samples,
35 enum r300_buffer_tiling microtile,
36 enum r300_buffer_tiling macrotile,
37 enum r300_dim dim)
38 {
39 static const unsigned table[2][5][3][2] =
40 {
41 {
42 /* Macro: linear linear linear
43 Micro: linear tiled square-tiled */
44 {{ 32, 1}, { 8, 4}, { 0, 0}}, /* 8 bits per pixel */
45 {{ 16, 1}, { 8, 2}, { 4, 4}}, /* 16 bits per pixel */
46 {{ 8, 1}, { 4, 2}, { 0, 0}}, /* 32 bits per pixel */
47 {{ 4, 1}, { 0, 0}, { 2, 2}}, /* 64 bits per pixel */
48 {{ 2, 1}, { 0, 0}, { 0, 0}} /* 128 bits per pixel */
49 },
50 {
51 /* Macro: tiled tiled tiled
52 Micro: linear tiled square-tiled */
53 {{256, 8}, {64, 32}, { 0, 0}}, /* 8 bits per pixel */
54 {{128, 8}, {64, 16}, {32, 32}}, /* 16 bits per pixel */
55 {{ 64, 8}, {32, 16}, { 0, 0}}, /* 32 bits per pixel */
56 {{ 32, 8}, { 0, 0}, {16, 16}}, /* 64 bits per pixel */
57 {{ 16, 8}, { 0, 0}, { 0, 0}} /* 128 bits per pixel */
58 }
59 };
60 static const unsigned aa_block[2] = {4, 8};
61 unsigned tile = 0;
62 unsigned pixsize = util_format_get_blocksize(format);
63
64 assert(macrotile <= R300_BUFFER_TILED);
65 assert(microtile <= R300_BUFFER_SQUARETILED);
66 assert(pixsize <= 16);
67 assert(dim <= DIM_HEIGHT);
68
69 if (num_samples > 1) {
70 /* Multisampled textures have their own alignment scheme. */
71 if (pixsize == 4)
72 tile = aa_block[dim];
73 /* XXX FP16 AA. */
74 } else {
75 /* Standard alignment. */
76 tile = table[macrotile][util_logbase2(pixsize)][microtile][dim];
77 }
78
79 assert(tile);
80 return tile;
81 }
82
83 /* Return true if macrotiling should be enabled on the miplevel. */
84 static boolean r300_texture_macro_switch(struct r300_texture_desc *desc,
85 unsigned level,
86 boolean rv350_mode,
87 enum r300_dim dim)
88 {
89 unsigned tile, texdim;
90
91 tile = r300_get_pixel_alignment(desc->b.b.format, desc->b.b.nr_samples,
92 desc->microtile, R300_BUFFER_TILED, dim);
93 if (dim == DIM_WIDTH) {
94 texdim = u_minify(desc->b.b.width0, level);
95 } else {
96 texdim = u_minify(desc->b.b.height0, level);
97 }
98
99 /* See TX_FILTER1_n.MACRO_SWITCH. */
100 if (rv350_mode) {
101 return texdim >= tile;
102 } else {
103 return texdim > tile;
104 }
105 }
106
107 /**
108 * Return the stride, in bytes, of the texture image of the given texture
109 * at the given level.
110 */
111 static unsigned r300_texture_get_stride(struct r300_screen *screen,
112 struct r300_texture_desc *desc,
113 unsigned level)
114 {
115 unsigned tile_width, width, stride;
116
117 if (desc->stride_in_bytes_override)
118 return desc->stride_in_bytes_override;
119
120 /* Check the level. */
121 if (level > desc->b.b.last_level) {
122 SCREEN_DBG(screen, DBG_TEX, "%s: level (%u) > last_level (%u)\n",
123 __FUNCTION__, level, desc->b.b.last_level);
124 return 0;
125 }
126
127 width = u_minify(desc->b.b.width0, level);
128
129 if (util_format_is_plain(desc->b.b.format)) {
130 tile_width = r300_get_pixel_alignment(desc->b.b.format,
131 desc->b.b.nr_samples,
132 desc->microtile,
133 desc->macrotile[level],
134 DIM_WIDTH);
135 width = align(width, tile_width);
136
137 stride = util_format_get_stride(desc->b.b.format, width);
138
139 /* Some IGPs need a minimum stride of 64 bytes, hmm... */
140 if (!desc->macrotile[level] &&
141 (screen->caps.family == CHIP_FAMILY_RS600 ||
142 screen->caps.family == CHIP_FAMILY_RS690 ||
143 screen->caps.family == CHIP_FAMILY_RS740)) {
144 unsigned min_stride;
145
146 if (desc->microtile) {
147 unsigned tile_height =
148 r300_get_pixel_alignment(desc->b.b.format,
149 desc->b.b.nr_samples,
150 desc->microtile,
151 desc->macrotile[level],
152 DIM_HEIGHT);
153
154 min_stride = 64 / tile_height;
155 } else {
156 min_stride = 64;
157 }
158
159 return stride < min_stride ? min_stride : stride;
160 }
161
162 /* The alignment to 32 bytes is sort of implied by the layout... */
163 return stride;
164 } else {
165 return align(util_format_get_stride(desc->b.b.format, width), 32);
166 }
167 }
168
169 static unsigned r300_texture_get_nblocksy(struct r300_texture_desc *desc,
170 unsigned level,
171 boolean *out_aligned_for_cbzb)
172 {
173 unsigned height, tile_height;
174
175 height = u_minify(desc->b.b.height0, level);
176
177 if (util_format_is_plain(desc->b.b.format)) {
178 tile_height = r300_get_pixel_alignment(desc->b.b.format,
179 desc->b.b.nr_samples,
180 desc->microtile,
181 desc->macrotile[level],
182 DIM_HEIGHT);
183 height = align(height, tile_height);
184
185 /* This is needed for the kernel checker, unfortunately. */
186 if ((desc->b.b.target != PIPE_TEXTURE_1D &&
187 desc->b.b.target != PIPE_TEXTURE_2D) ||
188 desc->b.b.last_level != 0) {
189 height = util_next_power_of_two(height);
190 }
191
192 /* See if the CBZB clear can be used on the buffer,
193 * taking the texture size into account. */
194 if (out_aligned_for_cbzb) {
195 if (desc->macrotile[level]) {
196 /* When clearing, the layer (width*height) is horizontally split
197 * into two, and the upper and lower halves are cleared by the CB
198 * and ZB units, respectively. Therefore, the number of macrotiles
199 * in the Y direction must be even. */
200
201 /* Align the height so that there is an even number of macrotiles.
202 * Do so for 3 or more macrotiles in the Y direction. */
203 if (level == 0 && desc->b.b.last_level == 0 &&
204 (desc->b.b.target == PIPE_TEXTURE_1D ||
205 desc->b.b.target == PIPE_TEXTURE_2D) &&
206 height >= tile_height * 3) {
207 height = align(height, tile_height * 2);
208 }
209
210 *out_aligned_for_cbzb = height % (tile_height * 2) == 0;
211 } else {
212 *out_aligned_for_cbzb = FALSE;
213 }
214 }
215 }
216
217 return util_format_get_nblocksy(desc->b.b.format, height);
218 }
219
220 static void r300_texture_3d_fix_mipmapping(struct r300_screen *screen,
221 struct r300_texture_desc *desc)
222 {
223 /* The kernels <= 2.6.34-rc4 compute the size of mipmapped 3D textures
224 * incorrectly. This is a workaround to prevent CS from being rejected. */
225
226 unsigned i, size;
227
228 if (!screen->rws->get_value(screen->rws, R300_VID_DRM_2_3_0) &&
229 desc->b.b.target == PIPE_TEXTURE_3D &&
230 desc->b.b.last_level > 0) {
231 size = 0;
232
233 for (i = 0; i <= desc->b.b.last_level; i++) {
234 size += desc->stride_in_bytes[i] *
235 r300_texture_get_nblocksy(desc, i, FALSE);
236 }
237
238 size *= desc->b.b.depth0;
239 desc->size_in_bytes = size;
240 }
241 }
242
243 /* Get a width in pixels from a stride in bytes. */
244 static unsigned stride_to_width(enum pipe_format format,
245 unsigned stride_in_bytes)
246 {
247 return (stride_in_bytes / util_format_get_blocksize(format)) *
248 util_format_get_blockwidth(format);
249 }
250
251 static void r300_setup_miptree(struct r300_screen *screen,
252 struct r300_texture_desc *desc,
253 boolean align_for_cbzb)
254 {
255 struct pipe_resource *base = &desc->b.b;
256 unsigned stride, size, layer_size, nblocksy, i;
257 boolean rv350_mode = screen->caps.is_rv350;
258 boolean aligned_for_cbzb;
259
260 desc->size_in_bytes = 0;
261
262 SCREEN_DBG(screen, DBG_TEXALLOC,
263 "r300: Making miptree for texture, format %s\n",
264 util_format_short_name(base->format));
265
266 for (i = 0; i <= base->last_level; i++) {
267 /* Let's see if this miplevel can be macrotiled. */
268 desc->macrotile[i] =
269 (desc->macrotile[0] == R300_BUFFER_TILED &&
270 r300_texture_macro_switch(desc, i, rv350_mode, DIM_WIDTH) &&
271 r300_texture_macro_switch(desc, i, rv350_mode, DIM_HEIGHT)) ?
272 R300_BUFFER_TILED : R300_BUFFER_LINEAR;
273
274 stride = r300_texture_get_stride(screen, desc, i);
275
276 /* Compute the number of blocks in Y, see if the CBZB clear can be
277 * used on the texture. */
278 aligned_for_cbzb = FALSE;
279 if (align_for_cbzb && desc->cbzb_allowed[i])
280 nblocksy = r300_texture_get_nblocksy(desc, i, &aligned_for_cbzb);
281 else
282 nblocksy = r300_texture_get_nblocksy(desc, i, NULL);
283
284 layer_size = stride * nblocksy;
285
286 if (base->nr_samples) {
287 layer_size *= base->nr_samples;
288 }
289
290 if (base->target == PIPE_TEXTURE_CUBE)
291 size = layer_size * 6;
292 else
293 size = layer_size * u_minify(base->depth0, i);
294
295 desc->offset_in_bytes[i] = desc->size_in_bytes;
296 desc->size_in_bytes = desc->offset_in_bytes[i] + size;
297 desc->layer_size_in_bytes[i] = layer_size;
298 desc->stride_in_bytes[i] = stride;
299 desc->stride_in_pixels[i] = stride_to_width(desc->b.b.format, stride);
300 desc->cbzb_allowed[i] = desc->cbzb_allowed[i] && aligned_for_cbzb;
301
302 SCREEN_DBG(screen, DBG_TEXALLOC, "r300: Texture miptree: Level %d "
303 "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
304 i, u_minify(base->width0, i), u_minify(base->height0, i),
305 u_minify(base->depth0, i), stride, desc->size_in_bytes,
306 desc->macrotile[i] ? "TRUE" : "FALSE");
307 }
308 }
309
310 static void r300_setup_flags(struct r300_texture_desc *desc)
311 {
312 desc->uses_stride_addressing =
313 !util_is_power_of_two(desc->b.b.width0) ||
314 !util_is_power_of_two(desc->b.b.height0) ||
315 (desc->stride_in_bytes_override &&
316 stride_to_width(desc->b.b.format,
317 desc->stride_in_bytes_override) != desc->b.b.width0);
318
319 desc->is_npot =
320 desc->uses_stride_addressing ||
321 !util_is_power_of_two(desc->b.b.height0);
322 }
323
324 static void r300_setup_cbzb_flags(struct r300_screen *rscreen,
325 struct r300_texture_desc *desc)
326 {
327 unsigned i, bpp;
328 boolean first_level_valid;
329
330 bpp = util_format_get_blocksizebits(desc->b.b.format);
331
332 /* 1) The texture must be point-sampled,
333 * 2) The depth must be 16 or 32 bits.
334 * 3) If the midpoint ZB offset is not aligned to 2048, it returns garbage
335 * with certain texture sizes. Macrotiling ensures the alignment. */
336 first_level_valid = desc->b.b.nr_samples <= 1 &&
337 (bpp == 16 || bpp == 32) &&
338 desc->macrotile[0];
339
340 for (i = 0; i <= desc->b.b.last_level; i++)
341 desc->cbzb_allowed[i] = first_level_valid && desc->macrotile[i];
342 }
343
344 static void r300_setup_tiling(struct r300_screen *screen,
345 struct r300_texture_desc *desc)
346 {
347 struct r300_winsys_screen *rws = screen->rws;
348 enum pipe_format format = desc->b.b.format;
349 boolean rv350_mode = screen->caps.is_rv350;
350 boolean is_zb = util_format_is_depth_or_stencil(format);
351 boolean dbg_no_tiling = SCREEN_DBG_ON(screen, DBG_NO_TILING);
352
353 if (!util_format_is_plain(format)) {
354 return;
355 }
356
357 /* If height == 1, disable microtiling except for zbuffer. */
358 if (!is_zb && (desc->b.b.height0 == 1 || dbg_no_tiling)) {
359 return;
360 }
361
362 /* Set microtiling. */
363 switch (util_format_get_blocksize(format)) {
364 case 1:
365 case 4:
366 desc->microtile = R300_BUFFER_TILED;
367 break;
368
369 case 2:
370 case 8:
371 if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
372 desc->microtile = R300_BUFFER_SQUARETILED;
373 }
374 break;
375 }
376
377 if (dbg_no_tiling) {
378 return;
379 }
380
381 /* Set macrotiling. */
382 if (r300_texture_macro_switch(desc, 0, rv350_mode, DIM_WIDTH) &&
383 r300_texture_macro_switch(desc, 0, rv350_mode, DIM_HEIGHT)) {
384 desc->macrotile[0] = R300_BUFFER_TILED;
385 }
386 }
387
388 static void r300_tex_print_info(struct r300_screen *rscreen,
389 struct r300_texture_desc *desc,
390 const char *func)
391 {
392 fprintf(stderr,
393 "r300: %s: Macro: %s, Micro: %s, Pitch: %i, Dim: %ix%ix%i, "
394 "LastLevel: %i, Size: %i, Format: %s\n",
395 func,
396 desc->macrotile[0] ? "YES" : " NO",
397 desc->microtile ? "YES" : " NO",
398 desc->stride_in_pixels[0],
399 desc->b.b.width0, desc->b.b.height0, desc->b.b.depth0,
400 desc->b.b.last_level, desc->size_in_bytes,
401 util_format_short_name(desc->b.b.format));
402 }
403
404 boolean r300_texture_desc_init(struct r300_screen *rscreen,
405 struct r300_texture_desc *desc,
406 const struct pipe_resource *base,
407 enum r300_buffer_tiling microtile,
408 enum r300_buffer_tiling macrotile,
409 unsigned stride_in_bytes_override,
410 unsigned max_buffer_size)
411 {
412 desc->b.b = *base;
413 desc->b.b.screen = &rscreen->screen;
414
415 desc->stride_in_bytes_override = stride_in_bytes_override;
416
417 if (microtile == R300_BUFFER_SELECT_LAYOUT ||
418 macrotile == R300_BUFFER_SELECT_LAYOUT) {
419 r300_setup_tiling(rscreen, desc);
420 } else {
421 desc->microtile = microtile;
422 desc->macrotile[0] = macrotile;
423 assert(desc->b.b.last_level == 0);
424 }
425
426 r300_setup_flags(desc);
427 r300_setup_cbzb_flags(rscreen, desc);
428
429 /* Setup the miptree description. */
430 r300_setup_miptree(rscreen, desc, TRUE);
431 /* If the required buffer size is larger the given max size,
432 * try again without the alignment for the CBZB clear. */
433 if (max_buffer_size && desc->size_in_bytes > max_buffer_size) {
434 r300_setup_miptree(rscreen, desc, FALSE);
435 }
436
437 r300_texture_3d_fix_mipmapping(rscreen, desc);
438
439 if (max_buffer_size) {
440 /* Make sure the buffer we got is large enough. */
441 if (desc->size_in_bytes > max_buffer_size) {
442 fprintf(stderr, "r300: texture_from_handle: The buffer is not "
443 "large enough. Got: %i, Need: %i, Info:\n",
444 max_buffer_size, desc->size_in_bytes);
445 r300_tex_print_info(rscreen, desc, "texture_from_handle");
446 return FALSE;
447 }
448
449 desc->buffer_size_in_bytes = max_buffer_size;
450 } else {
451 desc->buffer_size_in_bytes = desc->size_in_bytes;
452 }
453
454 if (SCREEN_DBG_ON(rscreen, DBG_TEX))
455 r300_tex_print_info(rscreen, desc, "texture_from_handle");
456
457 return TRUE;
458 }
459
460 unsigned r300_texture_get_offset(struct r300_texture_desc *desc,
461 unsigned level, unsigned zslice,
462 unsigned face)
463 {
464 unsigned offset = desc->offset_in_bytes[level];
465
466 switch (desc->b.b.target) {
467 case PIPE_TEXTURE_3D:
468 assert(face == 0);
469 return offset + zslice * desc->layer_size_in_bytes[level];
470
471 case PIPE_TEXTURE_CUBE:
472 assert(zslice == 0);
473 return offset + face * desc->layer_size_in_bytes[level];
474
475 default:
476 assert(zslice == 0 && face == 0);
477 return offset;
478 }
479 }