2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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:
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
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.
28 #include <pipe/p_screen.h>
29 #include <util/u_format.h>
30 #include <util/u_math.h>
31 #include <util/u_inlines.h>
32 #include <util/u_memory.h>
33 #include "state_tracker/drm_driver.h"
34 #include "r600_pipe.h"
35 #include "r600_resource.h"
36 #include "r600_state_inlines.h"
39 extern struct u_resource_vtbl r600_texture_vtbl
;
41 /* Copy from a tiled texture to a detiled one. */
42 static void r600_copy_from_tiled_texture(struct pipe_context
*ctx
, struct r600_transfer
*rtransfer
)
44 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)rtransfer
;
45 struct pipe_resource
*texture
= transfer
->resource
;
46 struct pipe_subresource subdst
;
50 ctx
->resource_copy_region(ctx
, rtransfer
->linear_texture
,
51 subdst
, 0, 0, 0, texture
, transfer
->sr
,
52 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
53 transfer
->box
.width
, transfer
->box
.height
);
56 static unsigned long r600_texture_get_offset(struct r600_resource_texture
*rtex
,
57 unsigned level
, unsigned zslice
,
60 unsigned long offset
= rtex
->offset
[level
];
62 switch (rtex
->resource
.base
.b
.target
) {
65 return offset
+ zslice
* rtex
->layer_size
[level
];
66 case PIPE_TEXTURE_CUBE
:
68 return offset
+ face
* rtex
->layer_size
[level
];
70 assert(zslice
== 0 && face
== 0);
75 static void r600_setup_miptree(struct r600_resource_texture
*rtex
, enum chip_class chipc
)
77 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
78 unsigned long w
, h
, pitch
, size
, layer_size
, i
, offset
;
80 rtex
->bpt
= util_format_get_blocksize(ptex
->format
);
81 for (i
= 0, offset
= 0; i
<= ptex
->last_level
; i
++) {
82 w
= u_minify(ptex
->width0
, i
);
83 h
= u_minify(ptex
->height0
, i
);
84 h
= util_next_power_of_two(h
);
85 pitch
= util_format_get_stride(ptex
->format
, align(w
, 64));
86 if (chipc
== EVERGREEN
)
87 pitch
= align(pitch
, 512);
89 pitch
= align(pitch
, 256);
90 layer_size
= pitch
* h
;
91 if (ptex
->target
== PIPE_TEXTURE_CUBE
) {
93 size
= layer_size
* 8;
95 size
= layer_size
* 6;
98 size
= layer_size
* u_minify(ptex
->depth0
, i
);
99 rtex
->offset
[i
] = offset
;
100 rtex
->layer_size
[i
] = layer_size
;
101 rtex
->pitch
[i
] = pitch
;
109 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
110 const struct pipe_resource
*templ
)
112 struct r600_resource_texture
*rtex
;
113 struct r600_resource
*resource
;
114 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
116 rtex
= CALLOC_STRUCT(r600_resource_texture
);
120 resource
= &rtex
->resource
;
121 resource
->base
.b
= *templ
;
122 resource
->base
.vtbl
= &r600_texture_vtbl
;
123 pipe_reference_init(&resource
->base
.b
.reference
, 1);
124 resource
->base
.b
.screen
= screen
;
125 r600_setup_miptree(rtex
, r600_get_family_class(radeon
));
127 /* FIXME alignment 4096 enought ? too much ? */
128 resource
->domain
= r600_domain_from_usage(resource
->base
.b
.bind
);
129 resource
->size
= rtex
->size
;
130 resource
->bo
= radeon_ws_bo(radeon
, rtex
->size
, 4096, 0);
131 if (resource
->bo
== NULL
) {
135 return &resource
->base
.b
;
138 static void r600_texture_destroy(struct pipe_screen
*screen
,
139 struct pipe_resource
*ptex
)
141 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)ptex
;
142 struct r600_resource
*resource
= &rtex
->resource
;
143 struct radeon
*radeon
= (struct radeon
*)screen
->winsys
;
145 if (rtex
->flushed_depth_texture
)
146 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
149 radeon_ws_bo_reference(radeon
, &resource
->bo
, NULL
);
154 static struct pipe_surface
*r600_get_tex_surface(struct pipe_screen
*screen
,
155 struct pipe_resource
*texture
,
156 unsigned face
, unsigned level
,
157 unsigned zslice
, unsigned flags
)
159 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
160 struct pipe_surface
*surface
= CALLOC_STRUCT(pipe_surface
);
161 unsigned long offset
;
165 offset
= r600_texture_get_offset(rtex
, level
, zslice
, face
);
166 pipe_reference_init(&surface
->reference
, 1);
167 pipe_resource_reference(&surface
->texture
, texture
);
168 surface
->format
= texture
->format
;
169 surface
->width
= u_minify(texture
->width0
, level
);
170 surface
->height
= u_minify(texture
->height0
, level
);
171 surface
->offset
= offset
;
172 surface
->usage
= flags
;
173 surface
->zslice
= zslice
;
174 surface
->texture
= texture
;
175 surface
->face
= face
;
176 surface
->level
= level
;
180 static void r600_tex_surface_destroy(struct pipe_surface
*surface
)
182 pipe_resource_reference(&surface
->texture
, NULL
);
186 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
187 const struct pipe_resource
*templ
,
188 struct winsys_handle
*whandle
)
190 struct radeon
*rw
= (struct radeon
*)screen
->winsys
;
191 struct r600_resource_texture
*rtex
;
192 struct r600_resource
*resource
;
193 struct radeon_ws_bo
*bo
= NULL
;
195 /* Support only 2D textures without mipmaps */
196 if ((templ
->target
!= PIPE_TEXTURE_2D
&& templ
->target
!= PIPE_TEXTURE_RECT
) ||
197 templ
->depth0
!= 1 || templ
->last_level
!= 0)
200 rtex
= CALLOC_STRUCT(r600_resource_texture
);
204 bo
= radeon_ws_bo_handle(rw
, whandle
->handle
);
210 resource
= &rtex
->resource
;
211 resource
->base
.b
= *templ
;
212 resource
->base
.vtbl
= &r600_texture_vtbl
;
213 pipe_reference_init(&resource
->base
.b
.reference
, 1);
214 resource
->base
.b
.screen
= screen
;
217 rtex
->pitch_override
= whandle
->stride
;
218 rtex
->bpt
= util_format_get_blocksize(templ
->format
);
219 rtex
->pitch
[0] = whandle
->stride
;
220 rtex
->width
[0] = templ
->width0
;
221 rtex
->height
[0] = templ
->height0
;
223 rtex
->size
= align(rtex
->pitch
[0] * templ
->height0
, 64);
225 return &resource
->base
.b
;
228 static unsigned int r600_texture_is_referenced(struct pipe_context
*context
,
229 struct pipe_resource
*texture
,
230 unsigned face
, unsigned level
)
233 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
236 int (*r600_blit_uncompress_depth_ptr
)(struct pipe_context
*ctx
, struct r600_resource_texture
*texture
);
238 int r600_texture_depth_flush(struct pipe_context
*ctx
,
239 struct pipe_resource
*texture
)
241 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
242 struct pipe_resource resource
;
244 if (rtex
->flushed_depth_texture
)
247 resource
.target
= PIPE_TEXTURE_2D
;
248 resource
.format
= texture
->format
;
249 resource
.width0
= texture
->width0
;
250 resource
.height0
= texture
->height0
;
252 resource
.last_level
= 0;
253 resource
.nr_samples
= 0;
254 resource
.usage
= PIPE_USAGE_DYNAMIC
;
258 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
260 rtex
->flushed_depth_texture
= (struct r600_resource_texture
*)ctx
->screen
->resource_create(ctx
->screen
, &resource
);
261 if (rtex
->flushed_depth_texture
== NULL
) {
262 R600_ERR("failed to create temporary texture to hold untiled copy\n");
267 r600_blit_uncompress_depth_ptr(ctx
, rtex
);
271 struct pipe_transfer
* r600_texture_get_transfer(struct pipe_context
*ctx
,
272 struct pipe_resource
*texture
,
273 struct pipe_subresource sr
,
275 const struct pipe_box
*box
)
277 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
278 struct pipe_resource resource
;
279 struct r600_transfer
*trans
;
282 trans
= CALLOC_STRUCT(r600_transfer
);
285 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
286 trans
->transfer
.sr
= sr
;
287 trans
->transfer
.usage
= usage
;
288 trans
->transfer
.box
= *box
;
289 trans
->transfer
.stride
= rtex
->pitch
[sr
.level
];
290 trans
->offset
= r600_texture_get_offset(rtex
, sr
.level
, box
->z
, sr
.face
);
292 r
= r600_texture_depth_flush(ctx
, texture
);
294 R600_ERR("failed to create temporary texture to hold untiled copy\n");
295 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
299 } else if (rtex
->tiled
) {
300 resource
.target
= PIPE_TEXTURE_2D
;
301 resource
.format
= texture
->format
;
302 resource
.width0
= box
->width
;
303 resource
.height0
= box
->height
;
305 resource
.last_level
= 0;
306 resource
.nr_samples
= 0;
307 resource
.usage
= PIPE_USAGE_DYNAMIC
;
310 /* For texture reading, the temporary (detiled) texture is used as
311 * a render target when blitting from a tiled texture. */
312 if (usage
& PIPE_TRANSFER_READ
) {
313 resource
.bind
|= PIPE_BIND_RENDER_TARGET
;
315 /* For texture writing, the temporary texture is used as a sampler
316 * when blitting into a tiled texture. */
317 if (usage
& PIPE_TRANSFER_WRITE
) {
318 resource
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
320 /* Create the temporary texture. */
321 trans
->linear_texture
= ctx
->screen
->resource_create(ctx
->screen
, &resource
);
322 if (trans
->linear_texture
== NULL
) {
323 R600_ERR("failed to create temporary texture to hold untiled copy\n");
324 pipe_resource_reference(&trans
->transfer
.resource
, NULL
);
328 if (usage
& PIPE_TRANSFER_READ
) {
329 /* We cannot map a tiled texture directly because the data is
330 * in a different order, therefore we do detiling using a blit. */
331 r600_copy_from_tiled_texture(ctx
, trans
);
332 /* Always referenced in the blit. */
333 ctx
->flush(ctx
, 0, NULL
);
336 return &trans
->transfer
;
339 void r600_texture_transfer_destroy(struct pipe_context
*ctx
,
340 struct pipe_transfer
*transfer
)
342 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
343 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
345 if (rtransfer
->linear_texture
) {
346 pipe_resource_reference(&rtransfer
->linear_texture
, NULL
);
348 if (rtex
->flushed_depth_texture
) {
349 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
352 pipe_resource_reference((struct pipe_resource
**)&rtex
->flushed_depth_texture
, NULL
);
354 pipe_resource_reference(&transfer
->resource
, NULL
);
358 void* r600_texture_transfer_map(struct pipe_context
*ctx
,
359 struct pipe_transfer
* transfer
)
361 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
362 struct radeon_ws_bo
*bo
;
363 enum pipe_format format
= transfer
->resource
->format
;
364 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
365 unsigned long offset
= 0;
368 if (rtransfer
->linear_texture
) {
369 bo
= ((struct r600_resource
*)rtransfer
->linear_texture
)->bo
;
371 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
373 if (rtex
->flushed_depth_texture
)
374 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
376 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
378 offset
= rtransfer
->offset
+
379 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
380 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
382 map
= radeon_ws_bo_map(radeon
, bo
, 0, ctx
);
390 void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
391 struct pipe_transfer
* transfer
)
393 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
394 struct radeon
*radeon
= (struct radeon
*)ctx
->screen
->winsys
;
395 struct radeon_ws_bo
*bo
;
397 if (rtransfer
->linear_texture
) {
398 bo
= ((struct r600_resource
*)rtransfer
->linear_texture
)->bo
;
400 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)transfer
->resource
;
402 if (rtex
->flushed_depth_texture
) {
403 bo
= ((struct r600_resource
*)rtex
->flushed_depth_texture
)->bo
;
405 bo
= ((struct r600_resource
*)transfer
->resource
)->bo
;
408 radeon_ws_bo_unmap(radeon
, bo
);
411 struct u_resource_vtbl r600_texture_vtbl
=
413 u_default_resource_get_handle
, /* get_handle */
414 r600_texture_destroy
, /* resource_destroy */
415 r600_texture_is_referenced
, /* is_resource_referenced */
416 r600_texture_get_transfer
, /* get_transfer */
417 r600_texture_transfer_destroy
, /* transfer_destroy */
418 r600_texture_transfer_map
, /* transfer_map */
419 u_default_transfer_flush_region
,/* transfer_flush_region */
420 r600_texture_transfer_unmap
, /* transfer_unmap */
421 u_default_transfer_inline_write
/* transfer_inline_write */
424 void r600_init_screen_texture_functions(struct pipe_screen
*screen
)
426 screen
->get_tex_surface
= r600_get_tex_surface
;
427 screen
->tex_surface_destroy
= r600_tex_surface_destroy
;
430 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
431 const unsigned char *swizzle_view
)
434 unsigned char swizzle
[4];
436 const uint32_t swizzle_shift
[4] = {
439 const uint32_t swizzle_bit
[4] = {
444 /* Combine two sets of swizzles. */
445 for (i
= 0; i
< 4; i
++) {
446 swizzle
[i
] = swizzle_view
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
447 swizzle_format
[swizzle_view
[i
]] : swizzle_view
[i
];
450 memcpy(swizzle
, swizzle_format
, 4);
454 for (i
= 0; i
< 4; i
++) {
455 switch (swizzle
[i
]) {
456 case UTIL_FORMAT_SWIZZLE_Y
:
457 result
|= swizzle_bit
[1] << swizzle_shift
[i
];
459 case UTIL_FORMAT_SWIZZLE_Z
:
460 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
462 case UTIL_FORMAT_SWIZZLE_W
:
463 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
465 case UTIL_FORMAT_SWIZZLE_0
:
466 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
468 case UTIL_FORMAT_SWIZZLE_1
:
469 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
471 default: /* UTIL_FORMAT_SWIZZLE_X */
472 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
478 /* texture format translate */
479 uint32_t r600_translate_texformat(enum pipe_format format
,
480 const unsigned char *swizzle_view
,
481 uint32_t *word4_p
, uint32_t *yuv_format_p
)
483 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
484 const struct util_format_description
*desc
;
485 boolean uniform
= TRUE
;
487 const uint32_t sign_bit
[4] = {
488 S_038010_FORMAT_COMP_X(V_038010_SQ_FORMAT_COMP_SIGNED
),
489 S_038010_FORMAT_COMP_Y(V_038010_SQ_FORMAT_COMP_SIGNED
),
490 S_038010_FORMAT_COMP_Z(V_038010_SQ_FORMAT_COMP_SIGNED
),
491 S_038010_FORMAT_COMP_W(V_038010_SQ_FORMAT_COMP_SIGNED
)
493 desc
= util_format_description(format
);
495 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
497 /* Colorspace (return non-RGB formats directly). */
498 switch (desc
->colorspace
) {
499 /* Depth stencil formats */
500 case UTIL_FORMAT_COLORSPACE_ZS
:
502 case PIPE_FORMAT_Z16_UNORM
:
503 result
= V_0280A0_COLOR_16
;
505 case PIPE_FORMAT_Z24X8_UNORM
:
506 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
507 result
= V_0280A0_COLOR_8_24
;
509 case PIPE_FORMAT_X8Z24_UNORM
:
510 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
511 result
= V_0280A0_COLOR_24_8
;
517 case UTIL_FORMAT_COLORSPACE_YUV
:
518 yuv_format
|= (1 << 30);
520 case PIPE_FORMAT_UYVY
:
521 case PIPE_FORMAT_YUYV
:
525 goto out_unknown
; /* TODO */
527 case UTIL_FORMAT_COLORSPACE_SRGB
:
528 word4
|= S_038010_FORCE_DEGAMMA(1);
529 if (format
== PIPE_FORMAT_L8A8_SRGB
|| format
== PIPE_FORMAT_L8_SRGB
)
530 goto out_unknown
; /* fails for some reason - TODO */
537 /* S3TC formats. TODO */
538 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
543 for (i
= 0; i
< desc
->nr_channels
; i
++) {
544 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
545 word4
|= sign_bit
[i
];
549 /* R8G8Bx_SNORM - TODO CxV8U8 */
553 /* See whether the components are of the same size. */
554 for (i
= 1; i
< desc
->nr_channels
; i
++) {
555 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
558 /* Non-uniform formats. */
560 switch(desc
->nr_channels
) {
562 if (desc
->channel
[0].size
== 5 &&
563 desc
->channel
[1].size
== 6 &&
564 desc
->channel
[2].size
== 5) {
565 result
= V_0280A0_COLOR_5_6_5
;
570 if (desc
->channel
[0].size
== 5 &&
571 desc
->channel
[1].size
== 5 &&
572 desc
->channel
[2].size
== 5 &&
573 desc
->channel
[3].size
== 1) {
574 result
= V_0280A0_COLOR_1_5_5_5
;
577 if (desc
->channel
[0].size
== 10 &&
578 desc
->channel
[1].size
== 10 &&
579 desc
->channel
[2].size
== 10 &&
580 desc
->channel
[3].size
== 2) {
581 result
= V_0280A0_COLOR_10_10_10_2
;
589 /* uniform formats */
590 switch (desc
->channel
[0].type
) {
591 case UTIL_FORMAT_TYPE_UNSIGNED
:
592 case UTIL_FORMAT_TYPE_SIGNED
:
593 if (!desc
->channel
[0].normalized
&&
594 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_SRGB
) {
598 switch (desc
->channel
[0].size
) {
600 switch (desc
->nr_channels
) {
602 result
= V_0280A0_COLOR_4_4
;
605 result
= V_0280A0_COLOR_4_4_4_4
;
610 switch (desc
->nr_channels
) {
612 result
= V_0280A0_COLOR_8
;
615 result
= V_0280A0_COLOR_8_8
;
618 result
= V_0280A0_COLOR_8_8_8_8
;
623 switch (desc
->nr_channels
) {
625 result
= V_0280A0_COLOR_16
;
628 result
= V_0280A0_COLOR_16_16
;
631 result
= V_0280A0_COLOR_16_16_16_16
;
637 case UTIL_FORMAT_TYPE_FLOAT
:
638 switch (desc
->channel
[0].size
) {
640 switch (desc
->nr_channels
) {
642 result
= V_0280A0_COLOR_16_FLOAT
;
645 result
= V_0280A0_COLOR_16_16_FLOAT
;
648 result
= V_0280A0_COLOR_16_16_16_16_FLOAT
;
653 switch (desc
->nr_channels
) {
655 result
= V_0280A0_COLOR_32_FLOAT
;
658 result
= V_0280A0_COLOR_32_32_FLOAT
;
661 result
= V_0280A0_COLOR_32_32_32_32_FLOAT
;
671 *yuv_format_p
= yuv_format
;
674 // R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));