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