2 * Copyright 2008 Ben Skeggs
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include "pipe/p_state.h"
24 #include "pipe/p_defines.h"
25 #include "util/u_inlines.h"
26 #include "util/u_format.h"
28 #include "nvc0_context.h"
29 #include "nvc0_resource.h"
30 #include "nvc0_transfer.h"
32 static INLINE
uint32_t
33 get_tile_dims(unsigned nx
, unsigned ny
, unsigned nz
)
35 uint32_t tile_mode
= 0x000;
37 if (ny
> 64) tile_mode
= 0x040; /* height 128 tiles */
39 if (ny
> 32) tile_mode
= 0x030; /* height 64 tiles */
41 if (ny
> 16) tile_mode
= 0x020; /* height 32 tiles */
43 if (ny
> 8) tile_mode
= 0x010; /* height 16 tiles */
48 if (tile_mode
> 0x020)
51 if (nz
> 16 && tile_mode
< 0x020)
52 return tile_mode
| 0x500; /* depth 32 tiles */
53 if (nz
> 8) return tile_mode
| 0x400; /* depth 16 tiles */
54 if (nz
> 4) return tile_mode
| 0x300; /* depth 8 tiles */
55 if (nz
> 2) return tile_mode
| 0x200; /* depth 4 tiles */
57 return tile_mode
| 0x100;
61 nvc0_miptree_zslice_offset(struct nvc0_miptree
*mt
, unsigned l
, unsigned z
)
63 unsigned nblocksy
; /* height of texture level aligned to tile height */
65 unsigned stride_2d
; /* to next slice within a 3D tile */
66 unsigned stride_3d
; /* to slice in the next (in z direction !) 3D tile */
68 unsigned tile_d_shift
= NVC0_TILE_DIM_SHIFT(mt
->level
[l
].tile_mode
, 2);
69 unsigned tile_d
= 1 << tile_d_shift
;
71 nblocksy
= util_format_get_nblocksy(mt
->base
.base
.format
,
72 u_minify(mt
->base
.base
.height0
, l
));
74 nblocksy
= align(nblocksy
, NVC0_TILE_HEIGHT(mt
->level
[l
].tile_mode
));
76 stride_2d
= NVC0_TILE_SIZE_2D(mt
->level
[l
].tile_mode
);
78 stride_3d
= (nblocksy
* mt
->level
[l
].pitch
) << tile_d_shift
;
80 return (z
& (tile_d
- 1)) * stride_2d
+ (z
>> tile_d_shift
) * stride_3d
;
84 nvc0_miptree_destroy(struct pipe_screen
*pscreen
, struct pipe_resource
*pt
)
86 struct nvc0_miptree
*mt
= nvc0_miptree(pt
);
88 nouveau_screen_bo_release(pscreen
, mt
->base
.bo
);
94 nvc0_miptree_get_handle(struct pipe_screen
*pscreen
,
95 struct pipe_resource
*pt
,
96 struct winsys_handle
*whandle
)
98 struct nvc0_miptree
*mt
= nvc0_miptree(pt
);
101 if (!mt
|| !mt
->base
.bo
)
104 stride
= util_format_get_stride(mt
->base
.base
.format
,
105 mt
->base
.base
.width0
);
107 return nouveau_screen_bo_get_handle(pscreen
,
113 const struct u_resource_vtbl nvc0_miptree_vtbl
=
115 nvc0_miptree_get_handle
, /* get_handle */
116 nvc0_miptree_destroy
, /* resource_destroy */
117 nvc0_miptree_transfer_new
, /* get_transfer */
118 nvc0_miptree_transfer_del
, /* transfer_destroy */
119 nvc0_miptree_transfer_map
, /* transfer_map */
120 u_default_transfer_flush_region
, /* transfer_flush_region */
121 nvc0_miptree_transfer_unmap
, /* transfer_unmap */
122 u_default_transfer_inline_write
/* transfer_inline_write */
125 struct pipe_resource
*
126 nvc0_miptree_create(struct pipe_screen
*pscreen
,
127 const struct pipe_resource
*templ
)
129 struct nouveau_device
*dev
= nouveau_screen(pscreen
)->device
;
130 struct nvc0_miptree
*mt
= CALLOC_STRUCT(nvc0_miptree
);
131 struct pipe_resource
*pt
= &mt
->base
.base
;
133 unsigned w
, h
, d
, l
, alloc_size
;
139 mt
->base
.vtbl
= &nvc0_miptree_vtbl
;
141 pipe_reference_init(&pt
->reference
, 1);
142 pt
->screen
= pscreen
;
144 mt
->layout_3d
= pt
->target
== PIPE_TEXTURE_3D
;
148 d
= mt
->layout_3d
? pt
->depth0
: 1;
150 switch (pt
->format
) {
151 case PIPE_FORMAT_Z16_UNORM
:
152 tile_flags
= 0x0700; /* COMPRESSED */
153 tile_flags
= 0x0100; /* NORMAL */
155 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
156 tile_flags
= 0x5300; /* MSAA 4, COMPRESSED */
157 tile_flags
= 0x4600; /* NORMAL */
159 case PIPE_FORMAT_Z24X8_UNORM
:
160 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
161 tile_flags
= 0x1100; /* NORMAL */
162 if (w
* h
>= 128 * 128 && 0)
163 tile_flags
= 0x1700; /* COMPRESSED, requires magic */
165 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
166 tile_flags
= 0xf500; /* COMPRESSED */
167 tile_flags
= 0xf700; /* MSAA 2 */
168 tile_flags
= 0xf900; /* MSAA 4 */
169 tile_flags
= 0xfe00; /* NORMAL */
171 case PIPE_FORMAT_Z32_FLOAT
:
174 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
175 tile_flags
= 0xce00; /* COMPRESSED */
176 tile_flags
= 0xcf00; /* MSAA 2, COMPRESSED */
177 tile_flags
= 0xd000; /* MSAA 4, COMPRESSED */
178 tile_flags
= 0xc300; /* NORMAL */
180 case PIPE_FORMAT_R16G16B16A16_UNORM
:
181 tile_flags
= 0xe900; /* COMPRESSED */
182 tile_flags
= 0xfe00; /* NORMAL */
185 tile_flags
= 0xe000; /* MSAA 4, COMPRESSED 32 BIT */
186 tile_flags
= 0xfe00; /* NORMAL 32 BIT */
187 if (w
* h
>= 128 * 128 && 0)
188 tile_flags
= 0xdb00; /* COMPRESSED 32 BIT, requires magic */
192 /* For 3D textures, a mipmap is spanned by all the layers, for array
193 * textures and cube maps, each layer contains its own mipmaps.
195 for (l
= 0; l
<= pt
->last_level
; ++l
) {
196 struct nvc0_miptree_level
*lvl
= &mt
->level
[l
];
197 unsigned nbx
= util_format_get_nblocksx(pt
->format
, w
);
198 unsigned nby
= util_format_get_nblocksy(pt
->format
, h
);
199 unsigned blocksize
= util_format_get_blocksize(pt
->format
);
201 lvl
->offset
= mt
->total_size
;
202 lvl
->tile_mode
= get_tile_dims(nbx
, nby
, d
);
203 lvl
->pitch
= align(nbx
* blocksize
, NVC0_TILE_PITCH(lvl
->tile_mode
));
205 mt
->total_size
+= lvl
->pitch
*
206 align(nby
, NVC0_TILE_HEIGHT(lvl
->tile_mode
)) *
207 align(d
, NVC0_TILE_DEPTH(lvl
->tile_mode
));
214 if (pt
->array_size
> 1) {
215 mt
->layer_stride
= align(mt
->total_size
,
216 NVC0_TILE_SIZE(mt
->level
[0].tile_mode
));
217 mt
->total_size
= mt
->layer_stride
* pt
->array_size
;
220 alloc_size
= mt
->total_size
;
221 if (tile_flags
== 0x1700)
222 alloc_size
*= 3; /* HiZ, XXX: correct size */
224 ret
= nouveau_bo_new_tile(dev
, NOUVEAU_BO_VRAM
, 256, alloc_size
,
225 mt
->level
[0].tile_mode
, tile_flags
,
231 mt
->base
.domain
= NOUVEAU_BO_VRAM
;
236 struct pipe_resource
*
237 nvc0_miptree_from_handle(struct pipe_screen
*pscreen
,
238 const struct pipe_resource
*templ
,
239 struct winsys_handle
*whandle
)
241 struct nvc0_miptree
*mt
;
244 /* only supports 2D, non-mipmapped textures for the moment */
245 if ((templ
->target
!= PIPE_TEXTURE_2D
&&
246 templ
->target
!= PIPE_TEXTURE_RECT
) ||
247 templ
->last_level
!= 0 ||
248 templ
->depth0
!= 1 ||
249 templ
->array_size
> 1)
252 mt
= CALLOC_STRUCT(nvc0_miptree
);
256 mt
->base
.bo
= nouveau_screen_bo_from_handle(pscreen
, whandle
, &stride
);
257 if (mt
->base
.bo
== NULL
) {
262 mt
->base
.base
= *templ
;
263 mt
->base
.vtbl
= &nvc0_miptree_vtbl
;
264 pipe_reference_init(&mt
->base
.base
.reference
, 1);
265 mt
->base
.base
.screen
= pscreen
;
266 mt
->level
[0].pitch
= stride
;
267 mt
->level
[0].offset
= 0;
268 mt
->level
[0].tile_mode
= mt
->base
.bo
->tile_mode
;
270 /* no need to adjust bo reference count */
271 return &mt
->base
.base
;
275 /* Surface functions.
278 struct pipe_surface
*
279 nvc0_miptree_surface_new(struct pipe_context
*pipe
,
280 struct pipe_resource
*pt
,
281 const struct pipe_surface
*templ
)
283 struct nvc0_miptree
*mt
= nvc0_miptree(pt
); /* guaranteed */
284 struct nvc0_surface
*ns
;
285 struct pipe_surface
*ps
;
286 struct nvc0_miptree_level
*lvl
= &mt
->level
[templ
->u
.tex
.level
];
288 ns
= CALLOC_STRUCT(nvc0_surface
);
293 pipe_reference_init(&ps
->reference
, 1);
294 pipe_resource_reference(&ps
->texture
, pt
);
296 ps
->format
= templ
->format
;
297 ps
->usage
= templ
->usage
;
298 ps
->u
.tex
.level
= templ
->u
.tex
.level
;
299 ps
->u
.tex
.first_layer
= templ
->u
.tex
.first_layer
;
300 ps
->u
.tex
.last_layer
= templ
->u
.tex
.last_layer
;
302 ns
->width
= u_minify(pt
->width0
, ps
->u
.tex
.level
);
303 ns
->height
= u_minify(pt
->height0
, ps
->u
.tex
.level
);
304 ns
->depth
= ps
->u
.tex
.last_layer
- ps
->u
.tex
.first_layer
+ 1;
305 ns
->offset
= lvl
->offset
;
307 /* comment says there are going to be removed, but they're used by the st */
308 ps
->width
= ns
->width
;
309 ps
->height
= ns
->height
;
315 nvc0_miptree_surface_del(struct pipe_context
*pipe
, struct pipe_surface
*ps
)
317 struct nvc0_surface
*s
= nvc0_surface(ps
);
319 pipe_resource_reference(&ps
->texture
, NULL
);