r600g: fix height calcs for miptree
[mesa.git] / src / gallium / drivers / r600 / r600_texture.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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 * Authors:
24 * Jerome Glisse
25 * Corbin Simpson
26 */
27 #include <pipe/p_screen.h>
28 #include <util/u_format.h>
29 #include <util/u_math.h>
30 #include <util/u_inlines.h>
31 #include <util/u_memory.h>
32 #include "state_tracker/drm_driver.h"
33 #include "r600_screen.h"
34 #include "r600_context.h"
35 #include "r600_resource.h"
36 #include "r600d.h"
37
38 extern struct u_resource_vtbl r600_texture_vtbl;
39
40 static unsigned long r600_texture_get_offset(struct r600_resource_texture *rtex,
41 unsigned level, unsigned zslice,
42 unsigned face)
43 {
44 unsigned long offset = rtex->offset[level];
45
46 switch (rtex->resource.base.b.target) {
47 case PIPE_TEXTURE_3D:
48 assert(face == 0);
49 return offset + zslice * rtex->layer_size[level];
50 case PIPE_TEXTURE_CUBE:
51 assert(zslice == 0);
52 return offset + face * rtex->layer_size[level];
53 default:
54 assert(zslice == 0 && face == 0);
55 return offset;
56 }
57 }
58
59 static void r600_setup_miptree(struct r600_screen *rscreen, struct r600_resource_texture *rtex)
60 {
61 struct pipe_resource *ptex = &rtex->resource.base.b;
62 unsigned long w, h, pitch, size, layer_size, i, offset;
63
64 rtex->bpt = util_format_get_blocksize(ptex->format);
65 for (i = 0, offset = 0; i <= ptex->last_level; i++) {
66 w = u_minify(ptex->width0, i);
67 h = u_minify(ptex->height0, i);
68 h = util_next_power_of_two(h);
69 pitch = util_format_get_stride(ptex->format, align(w, 64));
70 layer_size = pitch * h;
71 if (ptex->target == PIPE_TEXTURE_CUBE)
72 size = layer_size * 6;
73 else
74 size = layer_size * u_minify(ptex->depth0, i);
75 rtex->offset[i] = offset;
76 rtex->layer_size[i] = layer_size;
77 rtex->pitch[i] = pitch;
78 offset += size;
79 }
80 rtex->size = offset;
81 }
82
83 struct pipe_resource *r600_texture_create(struct pipe_screen *screen,
84 const struct pipe_resource *templ)
85 {
86 struct r600_resource_texture *rtex;
87 struct r600_resource *resource;
88 struct r600_screen *rscreen = r600_screen(screen);
89
90 rtex = CALLOC_STRUCT(r600_resource_texture);
91 if (!rtex) {
92 return NULL;
93 }
94 resource = &rtex->resource;
95 resource->base.b = *templ;
96 resource->base.vtbl = &r600_texture_vtbl;
97 pipe_reference_init(&resource->base.b.reference, 1);
98 resource->base.b.screen = screen;
99 r600_setup_miptree(rscreen, rtex);
100
101 /* FIXME alignment 4096 enought ? too much ? */
102 resource->domain = r600_domain_from_usage(resource->base.b.bind);
103 resource->bo = radeon_bo(rscreen->rw, 0, rtex->size, 4096, NULL);
104 if (resource->bo == NULL) {
105 FREE(rtex);
106 return NULL;
107 }
108
109 return &resource->base.b;
110 }
111
112 static void r600_texture_destroy(struct pipe_screen *screen,
113 struct pipe_resource *ptex)
114 {
115 struct r600_resource_texture *rtex = (struct r600_resource_texture*)ptex;
116 struct r600_resource *resource = &rtex->resource;
117 struct r600_screen *rscreen = r600_screen(screen);
118
119 if (resource->bo) {
120 radeon_bo_decref(rscreen->rw, resource->bo);
121 }
122 FREE(rtex);
123 }
124
125 static struct pipe_surface *r600_get_tex_surface(struct pipe_screen *screen,
126 struct pipe_resource *texture,
127 unsigned face, unsigned level,
128 unsigned zslice, unsigned flags)
129 {
130 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
131 struct pipe_surface *surface = CALLOC_STRUCT(pipe_surface);
132 unsigned long offset;
133
134 if (surface == NULL)
135 return NULL;
136 offset = r600_texture_get_offset(rtex, level, zslice, face);
137 pipe_reference_init(&surface->reference, 1);
138 pipe_resource_reference(&surface->texture, texture);
139 surface->format = texture->format;
140 surface->width = u_minify(texture->width0, level);
141 surface->height = u_minify(texture->height0, level);
142 surface->offset = offset;
143 surface->usage = flags;
144 surface->zslice = zslice;
145 surface->texture = texture;
146 surface->face = face;
147 surface->level = level;
148 return surface;
149 }
150
151 static void r600_tex_surface_destroy(struct pipe_surface *surface)
152 {
153 pipe_resource_reference(&surface->texture, NULL);
154 FREE(surface);
155 }
156
157 struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen,
158 const struct pipe_resource *templ,
159 struct winsys_handle *whandle)
160 {
161 struct radeon *rw = (struct radeon*)screen->winsys;
162 struct r600_resource_texture *rtex;
163 struct r600_resource *resource;
164 struct radeon_bo *bo = NULL;
165
166 bo = radeon_bo(rw, whandle->handle, 0, 0, NULL);
167 if (bo == NULL) {
168 return NULL;
169 }
170
171 /* Support only 2D textures without mipmaps */
172 if (templ->target != PIPE_TEXTURE_2D || templ->depth0 != 1 || templ->last_level != 0)
173 return NULL;
174
175 rtex = CALLOC_STRUCT(r600_resource_texture);
176 if (rtex == NULL)
177 return NULL;
178
179 resource = &rtex->resource;
180 resource->base.b = *templ;
181 resource->base.vtbl = &r600_texture_vtbl;
182 pipe_reference_init(&resource->base.b.reference, 1);
183 resource->base.b.screen = screen;
184 resource->bo = bo;
185 rtex->pitch_override = whandle->stride;
186 rtex->bpt = util_format_get_blocksize(templ->format);
187 rtex->pitch[0] = whandle->stride;
188 rtex->offset[0] = 0;
189 rtex->size = align(rtex->pitch[0] * templ->height0, 64);
190
191 return &resource->base.b;
192 }
193
194 static unsigned int r600_texture_is_referenced(struct pipe_context *context,
195 struct pipe_resource *texture,
196 unsigned face, unsigned level)
197 {
198 /* FIXME */
199 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
200 }
201
202 struct pipe_transfer* r600_texture_get_transfer(struct pipe_context *ctx,
203 struct pipe_resource *texture,
204 struct pipe_subresource sr,
205 unsigned usage,
206 const struct pipe_box *box)
207 {
208 struct r600_resource_texture *rtex = (struct r600_resource_texture*)texture;
209 struct r600_transfer *trans;
210
211 trans = CALLOC_STRUCT(r600_transfer);
212 if (trans == NULL)
213 return NULL;
214 pipe_resource_reference(&trans->transfer.resource, texture);
215 trans->transfer.sr = sr;
216 trans->transfer.usage = usage;
217 trans->transfer.box = *box;
218 trans->transfer.stride = rtex->pitch[sr.level];
219 trans->offset = r600_texture_get_offset(rtex, sr.level, box->z, sr.face);
220 return &trans->transfer;
221 }
222
223 void r600_texture_transfer_destroy(struct pipe_context *ctx,
224 struct pipe_transfer *trans)
225 {
226 pipe_resource_reference(&trans->resource, NULL);
227 FREE(trans);
228 }
229
230 void* r600_texture_transfer_map(struct pipe_context *ctx,
231 struct pipe_transfer* transfer)
232 {
233 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
234 struct r600_resource *resource;
235 enum pipe_format format = transfer->resource->format;
236 struct r600_screen *rscreen = r600_screen(ctx->screen);
237 char *map;
238
239 r600_flush(ctx, 0, NULL);
240
241 resource = (struct r600_resource *)transfer->resource;
242 if (radeon_bo_map(rscreen->rw, resource->bo)) {
243 return NULL;
244 }
245 radeon_bo_wait(rscreen->rw, resource->bo);
246
247 map = resource->bo->data;
248
249 return map + rtransfer->offset +
250 transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
251 transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
252 }
253
254 void r600_texture_transfer_unmap(struct pipe_context *ctx,
255 struct pipe_transfer* transfer)
256 {
257 struct r600_screen *rscreen = r600_screen(ctx->screen);
258 struct r600_resource *resource;
259
260 resource = (struct r600_resource *)transfer->resource;
261 radeon_bo_unmap(rscreen->rw, resource->bo);
262 }
263
264 struct u_resource_vtbl r600_texture_vtbl =
265 {
266 u_default_resource_get_handle, /* get_handle */
267 r600_texture_destroy, /* resource_destroy */
268 r600_texture_is_referenced, /* is_resource_referenced */
269 r600_texture_get_transfer, /* get_transfer */
270 r600_texture_transfer_destroy, /* transfer_destroy */
271 r600_texture_transfer_map, /* transfer_map */
272 u_default_transfer_flush_region,/* transfer_flush_region */
273 r600_texture_transfer_unmap, /* transfer_unmap */
274 u_default_transfer_inline_write /* transfer_inline_write */
275 };
276
277 void r600_init_screen_texture_functions(struct pipe_screen *screen)
278 {
279 screen->get_tex_surface = r600_get_tex_surface;
280 screen->tex_surface_destroy = r600_tex_surface_destroy;
281 }
282
283 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format,
284 const unsigned char *swizzle_view)
285 {
286 unsigned i;
287 unsigned char swizzle[4];
288 unsigned result = 0;
289 const uint32_t swizzle_shift[4] = {
290 16, 19, 22, 25,
291 };
292 const uint32_t swizzle_bit[4] = {
293 0, 1, 2, 3,
294 };
295
296 if (swizzle_view) {
297 /* Combine two sets of swizzles. */
298 for (i = 0; i < 4; i++) {
299 swizzle[i] = swizzle_view[i] <= UTIL_FORMAT_SWIZZLE_W ?
300 swizzle_format[swizzle_view[i]] : swizzle_view[i];
301 }
302 } else {
303 memcpy(swizzle, swizzle_format, 4);
304 }
305
306 /* Get swizzle. */
307 for (i = 0; i < 4; i++) {
308 switch (swizzle[i]) {
309 case UTIL_FORMAT_SWIZZLE_Y:
310 result |= swizzle_bit[1] << swizzle_shift[i];
311 break;
312 case UTIL_FORMAT_SWIZZLE_Z:
313 result |= swizzle_bit[2] << swizzle_shift[i];
314 break;
315 case UTIL_FORMAT_SWIZZLE_W:
316 result |= swizzle_bit[3] << swizzle_shift[i];
317 break;
318 case UTIL_FORMAT_SWIZZLE_0:
319 result |= V_038010_SQ_SEL_0 << swizzle_shift[i];
320 break;
321 case UTIL_FORMAT_SWIZZLE_1:
322 result |= V_038010_SQ_SEL_1 << swizzle_shift[i];
323 break;
324 default: /* UTIL_FORMAT_SWIZZLE_X */
325 result |= swizzle_bit[0] << swizzle_shift[i];
326 }
327 }
328 return result;
329 }
330
331 /* texture format translate */
332 uint32_t r600_translate_texformat(enum pipe_format format,
333 const unsigned char *swizzle_view,
334 uint32_t *word4_p, uint32_t *yuv_format_p)
335 {
336 uint32_t result = 0, word4 = 0, yuv_format = 0;
337 const struct util_format_description *desc;
338 boolean uniform = TRUE;
339 int i;
340 const uint32_t sign_bit[4] = {
341 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED),
342 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED),
343 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED),
344 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED)
345 };
346 desc = util_format_description(format);
347
348 /* Colorspace (return non-RGB formats directly). */
349 switch (desc->colorspace) {
350 /* Depth stencil formats */
351 case UTIL_FORMAT_COLORSPACE_ZS:
352 switch (format) {
353 case PIPE_FORMAT_Z16_UNORM:
354 result = V_028010_DEPTH_16;
355 goto out_word4;
356 case PIPE_FORMAT_Z24X8_UNORM:
357 result = V_028010_DEPTH_X8_24;
358 goto out_word4;
359 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
360 result = V_028010_DEPTH_8_24;
361 goto out_word4;
362 default:
363 goto out_unknown;
364 }
365
366 case UTIL_FORMAT_COLORSPACE_YUV:
367 yuv_format |= (1 << 30);
368 switch (format) {
369 case PIPE_FORMAT_UYVY:
370 case PIPE_FORMAT_YUYV:
371 default:
372 break;
373 }
374 goto out_unknown; /* TODO */
375
376 case UTIL_FORMAT_COLORSPACE_SRGB:
377 word4 |= S_038010_FORCE_DEGAMMA(1);
378 if (format == PIPE_FORMAT_L8A8_SRGB || format == PIPE_FORMAT_L8_SRGB)
379 goto out_unknown; /* fails for some reason - TODO */
380 break;
381
382 default:
383 break;
384 }
385
386 word4 |= r600_get_swizzle_combined(desc->swizzle, swizzle_view);
387
388 /* S3TC formats. TODO */
389 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
390 goto out_unknown;
391 }
392
393
394 for (i = 0; i < desc->nr_channels; i++) {
395 if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
396 word4 |= sign_bit[i];
397 }
398 }
399
400 /* R8G8Bx_SNORM - TODO CxV8U8 */
401
402 /* RGTC - TODO */
403
404 /* See whether the components are of the same size. */
405 for (i = 1; i < desc->nr_channels; i++) {
406 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
407 }
408
409 /* Non-uniform formats. */
410 if (!uniform) {
411 switch(desc->nr_channels) {
412 case 3:
413 if (desc->channel[0].size == 5 &&
414 desc->channel[1].size == 6 &&
415 desc->channel[2].size == 5) {
416 result |= V_0280A0_COLOR_5_6_5;
417 goto out_word4;
418 }
419 goto out_unknown;
420 case 4:
421 if (desc->channel[0].size == 5 &&
422 desc->channel[1].size == 5 &&
423 desc->channel[2].size == 5 &&
424 desc->channel[3].size == 1) {
425 result |= V_0280A0_COLOR_1_5_5_5;
426 goto out_word4;
427 }
428 if (desc->channel[0].size == 10 &&
429 desc->channel[1].size == 10 &&
430 desc->channel[2].size == 10 &&
431 desc->channel[3].size == 2) {
432 result |= V_0280A0_COLOR_10_10_10_2;
433 goto out_word4;
434 }
435 goto out_unknown;
436 }
437 goto out_unknown;
438 }
439
440 /* uniform formats */
441 switch (desc->channel[0].type) {
442 case UTIL_FORMAT_TYPE_UNSIGNED:
443 case UTIL_FORMAT_TYPE_SIGNED:
444 if (!desc->channel[0].normalized &&
445 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
446 goto out_unknown;
447 }
448
449 switch (desc->channel[0].size) {
450 case 4:
451 switch (desc->nr_channels) {
452 case 2:
453 result |= V_0280A0_COLOR_4_4;
454 goto out_word4;
455 case 4:
456 result |= V_0280A0_COLOR_4_4_4_4;
457 goto out_word4;
458 }
459 goto out_unknown;
460 case 8:
461 switch (desc->nr_channels) {
462 case 1:
463 result |= V_0280A0_COLOR_8;
464 goto out_word4;
465 case 2:
466 result |= V_0280A0_COLOR_8_8;
467 goto out_word4;
468 case 4:
469 result |= V_0280A0_COLOR_8_8_8_8;
470 goto out_word4;
471 }
472 goto out_unknown;
473 case 16:
474 switch (desc->nr_channels) {
475 case 1:
476 result |= V_0280A0_COLOR_16;
477 goto out_word4;
478 case 2:
479 result |= V_0280A0_COLOR_16_16;
480 goto out_word4;
481 case 4:
482 result |= V_0280A0_COLOR_16_16_16_16;
483 goto out_word4;
484 }
485 }
486 goto out_unknown;
487
488 case UTIL_FORMAT_TYPE_FLOAT:
489 switch (desc->channel[0].size) {
490 case 16:
491 switch (desc->nr_channels) {
492 case 1:
493 result |= V_0280A0_COLOR_16_FLOAT;
494 goto out_word4;
495 case 2:
496 result |= V_0280A0_COLOR_16_16_FLOAT;
497 goto out_word4;
498 case 4:
499 result |= V_0280A0_COLOR_16_16_16_16_FLOAT;
500 goto out_word4;
501 }
502 goto out_unknown;
503 case 32:
504 switch (desc->nr_channels) {
505 case 1:
506 result |= V_0280A0_COLOR_32_FLOAT;
507 goto out_word4;
508 case 2:
509 result |= V_0280A0_COLOR_32_32_FLOAT;
510 goto out_word4;
511 case 4:
512 result |= V_0280A0_COLOR_32_32_32_32_FLOAT;
513 goto out_word4;
514 }
515 }
516
517 }
518 out_word4:
519 if (word4_p)
520 *word4_p = word4;
521 if (yuv_format_p)
522 *yuv_format_p = yuv_format;
523 // fprintf(stderr,"returning %08x %08x %08x\n", result, word4, yuv_format);
524 return result;
525 out_unknown:
526 // R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));
527 return ~0;
528 }