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.
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"
38 extern struct u_resource_vtbl r600_texture_vtbl
;
40 static unsigned long r600_texture_get_offset(struct r600_resource_texture
*rtex
,
41 unsigned level
, unsigned zslice
,
44 unsigned long offset
= rtex
->offset
[level
];
46 switch (rtex
->resource
.base
.b
.target
) {
49 return offset
+ zslice
* rtex
->layer_size
[level
];
50 case PIPE_TEXTURE_CUBE
:
52 return offset
+ face
* rtex
->layer_size
[level
];
54 assert(zslice
== 0 && face
== 0);
59 static void r600_setup_miptree(struct r600_screen
*rscreen
, struct r600_resource_texture
*rtex
)
61 struct pipe_resource
*ptex
= &rtex
->resource
.base
.b
;
62 unsigned long w
, h
, pitch
, size
, layer_size
, i
, offset
;
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;
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
;
84 struct pipe_resource
*r600_texture_create(struct pipe_screen
*screen
,
85 const struct pipe_resource
*templ
)
87 struct r600_resource_texture
*rtex
;
88 struct r600_resource
*resource
;
89 struct r600_screen
*rscreen
= r600_screen(screen
);
91 rtex
= CALLOC_STRUCT(r600_resource_texture
);
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
);
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
) {
110 return &resource
->base
.b
;
113 static void r600_texture_destroy(struct pipe_screen
*screen
,
114 struct pipe_resource
*ptex
)
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
);
121 radeon_bo_decref(rscreen
->rw
, resource
->bo
);
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
)
131 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
132 struct pipe_surface
*surface
= CALLOC_STRUCT(pipe_surface
);
133 unsigned long offset
;
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
;
152 static void r600_tex_surface_destroy(struct pipe_surface
*surface
)
154 pipe_resource_reference(&surface
->texture
, NULL
);
158 struct pipe_resource
*r600_texture_from_handle(struct pipe_screen
*screen
,
159 const struct pipe_resource
*templ
,
160 struct winsys_handle
*whandle
)
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
;
167 bo
= radeon_bo(rw
, whandle
->handle
, 0, 0, NULL
);
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)
177 rtex
= CALLOC_STRUCT(r600_resource_texture
);
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
;
187 rtex
->pitch_override
= whandle
->stride
;
188 rtex
->bpt
= util_format_get_blocksize(templ
->format
);
189 rtex
->pitch
[0] = whandle
->stride
;
191 rtex
->size
= align(rtex
->pitch
[0] * templ
->height0
, 64);
193 return &resource
->base
.b
;
196 static unsigned int r600_texture_is_referenced(struct pipe_context
*context
,
197 struct pipe_resource
*texture
,
198 unsigned face
, unsigned level
)
201 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
204 struct pipe_transfer
* r600_texture_get_transfer(struct pipe_context
*ctx
,
205 struct pipe_resource
*texture
,
206 struct pipe_subresource sr
,
208 const struct pipe_box
*box
)
210 struct r600_resource_texture
*rtex
= (struct r600_resource_texture
*)texture
;
211 struct r600_transfer
*trans
;
213 trans
= CALLOC_STRUCT(r600_transfer
);
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
;
225 void r600_texture_transfer_destroy(struct pipe_context
*ctx
,
226 struct pipe_transfer
*trans
)
228 pipe_resource_reference(&trans
->resource
, NULL
);
232 void* r600_texture_transfer_map(struct pipe_context
*ctx
,
233 struct pipe_transfer
* transfer
)
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
);
241 r600_flush(ctx
, 0, NULL
);
243 resource
= (struct r600_resource
*)transfer
->resource
;
244 if (radeon_bo_map(rscreen
->rw
, resource
->bo
)) {
247 radeon_bo_wait(rscreen
->rw
, resource
->bo
);
249 map
= resource
->bo
->data
;
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
);
256 void r600_texture_transfer_unmap(struct pipe_context
*ctx
,
257 struct pipe_transfer
* transfer
)
259 struct r600_screen
*rscreen
= r600_screen(ctx
->screen
);
260 struct r600_resource
*resource
;
262 resource
= (struct r600_resource
*)transfer
->resource
;
263 radeon_bo_unmap(rscreen
->rw
, resource
->bo
);
266 struct u_resource_vtbl r600_texture_vtbl
=
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 */
279 void r600_init_screen_texture_functions(struct pipe_screen
*screen
)
281 screen
->get_tex_surface
= r600_get_tex_surface
;
282 screen
->tex_surface_destroy
= r600_tex_surface_destroy
;
285 static unsigned r600_get_swizzle_combined(const unsigned char *swizzle_format
,
286 const unsigned char *swizzle_view
)
289 unsigned char swizzle
[4];
291 const uint32_t swizzle_shift
[4] = {
294 const uint32_t swizzle_bit
[4] = {
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
];
305 memcpy(swizzle
, swizzle_format
, 4);
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
];
314 case UTIL_FORMAT_SWIZZLE_Z
:
315 result
|= swizzle_bit
[2] << swizzle_shift
[i
];
317 case UTIL_FORMAT_SWIZZLE_W
:
318 result
|= swizzle_bit
[3] << swizzle_shift
[i
];
320 case UTIL_FORMAT_SWIZZLE_0
:
321 result
|= V_038010_SQ_SEL_0
<< swizzle_shift
[i
];
323 case UTIL_FORMAT_SWIZZLE_1
:
324 result
|= V_038010_SQ_SEL_1
<< swizzle_shift
[i
];
326 default: /* UTIL_FORMAT_SWIZZLE_X */
327 result
|= swizzle_bit
[0] << swizzle_shift
[i
];
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
)
338 uint32_t result
= 0, word4
= 0, yuv_format
= 0;
339 const struct util_format_description
*desc
;
340 boolean uniform
= TRUE
;
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
)
348 desc
= util_format_description(format
);
350 /* Colorspace (return non-RGB formats directly). */
351 switch (desc
->colorspace
) {
352 /* Depth stencil formats */
353 case UTIL_FORMAT_COLORSPACE_ZS
:
355 case PIPE_FORMAT_Z16_UNORM
:
356 result
= V_028010_DEPTH_16
;
358 case PIPE_FORMAT_Z24X8_UNORM
:
359 result
= V_028010_DEPTH_X8_24
;
361 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
362 result
= V_028010_DEPTH_8_24
;
368 case UTIL_FORMAT_COLORSPACE_YUV
:
369 yuv_format
|= (1 << 30);
371 case PIPE_FORMAT_UYVY
:
372 case PIPE_FORMAT_YUYV
:
376 goto out_unknown
; /* TODO */
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 */
388 word4
|= r600_get_swizzle_combined(desc
->swizzle
, swizzle_view
);
390 /* S3TC formats. TODO */
391 if (desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
) {
396 for (i
= 0; i
< desc
->nr_channels
; i
++) {
397 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
398 word4
|= sign_bit
[i
];
402 /* R8G8Bx_SNORM - TODO CxV8U8 */
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
;
411 /* Non-uniform formats. */
413 switch(desc
->nr_channels
) {
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
;
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
;
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
;
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
) {
451 switch (desc
->channel
[0].size
) {
453 switch (desc
->nr_channels
) {
455 result
|= V_0280A0_COLOR_4_4
;
458 result
|= V_0280A0_COLOR_4_4_4_4
;
463 switch (desc
->nr_channels
) {
465 result
|= V_0280A0_COLOR_8
;
468 result
|= V_0280A0_COLOR_8_8
;
471 result
|= V_0280A0_COLOR_8_8_8_8
;
476 switch (desc
->nr_channels
) {
478 result
|= V_0280A0_COLOR_16
;
481 result
|= V_0280A0_COLOR_16_16
;
484 result
|= V_0280A0_COLOR_16_16_16_16
;
490 case UTIL_FORMAT_TYPE_FLOAT
:
491 switch (desc
->channel
[0].size
) {
493 switch (desc
->nr_channels
) {
495 result
|= V_0280A0_COLOR_16_FLOAT
;
498 result
|= V_0280A0_COLOR_16_16_FLOAT
;
501 result
|= V_0280A0_COLOR_16_16_16_16_FLOAT
;
506 switch (desc
->nr_channels
) {
508 result
|= V_0280A0_COLOR_32_FLOAT
;
511 result
|= V_0280A0_COLOR_32_32_FLOAT
;
514 result
|= V_0280A0_COLOR_32_32_32_32_FLOAT
;
524 *yuv_format_p
= yuv_format
;
525 // fprintf(stderr,"returning %08x %08x %08x\n", result, word4, yuv_format);
528 // R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));