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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
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/nvc0_context.h"
29 #include "nvc0/nvc0_resource.h"
32 nvc0_tex_choose_tile_dims(unsigned nx
, unsigned ny
, unsigned nz
)
34 return nv50_tex_choose_tile_dims_helper(nx
, ny
, nz
);
38 nvc0_mt_choose_storage_type(struct nv50_miptree
*mt
, boolean compressed
)
40 const unsigned ms
= util_logbase2(mt
->base
.base
.nr_samples
);
44 if (unlikely(mt
->base
.base
.bind
& PIPE_BIND_CURSOR
))
46 if (unlikely(mt
->base
.base
.flags
& NOUVEAU_RESOURCE_FLAG_LINEAR
))
49 switch (mt
->base
.base
.format
) {
50 case PIPE_FORMAT_Z16_UNORM
:
52 tile_flags
= 0x02 + ms
;
56 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
58 tile_flags
= 0x51 + ms
;
62 case PIPE_FORMAT_Z24X8_UNORM
:
63 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
65 tile_flags
= 0x17 + ms
;
69 case PIPE_FORMAT_Z32_FLOAT
:
71 tile_flags
= 0x86 + ms
;
75 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
77 tile_flags
= 0xce + ms
;
82 switch (util_format_get_blocksizebits(mt
->base
.base
.format
)) {
85 tile_flags
= 0xf4 + ms
* 2;
92 case 0: tile_flags
= 0xe6; break;
93 case 1: tile_flags
= 0xeb; break;
94 case 2: tile_flags
= 0xed; break;
95 case 3: tile_flags
= 0xf2; break;
104 if (compressed
&& ms
) {
106 /* This one makes things blurry:
107 case 0: tile_flags = 0xdb; break;
109 case 1: tile_flags
= 0xdd; break;
110 case 2: tile_flags
= 0xdf; break;
111 case 3: tile_flags
= 0xe4; break;
132 static INLINE boolean
133 nvc0_miptree_init_ms_mode(struct nv50_miptree
*mt
)
135 switch (mt
->base
.base
.nr_samples
) {
137 mt
->ms_mode
= NVC0_3D_MULTISAMPLE_MODE_MS8
;
142 mt
->ms_mode
= NVC0_3D_MULTISAMPLE_MODE_MS4
;
147 mt
->ms_mode
= NVC0_3D_MULTISAMPLE_MODE_MS2
;
152 mt
->ms_mode
= NVC0_3D_MULTISAMPLE_MODE_MS1
;
155 NOUVEAU_ERR("invalid nr_samples: %u\n", mt
->base
.base
.nr_samples
);
162 nvc0_miptree_init_layout_video(struct nv50_miptree
*mt
)
164 const struct pipe_resource
*pt
= &mt
->base
.base
;
165 const unsigned blocksize
= util_format_get_blocksize(pt
->format
);
167 assert(pt
->last_level
== 0);
168 assert(mt
->ms_x
== 0 && mt
->ms_y
== 0);
169 assert(!util_format_is_compressed(pt
->format
));
171 mt
->layout_3d
= pt
->target
== PIPE_TEXTURE_3D
;
173 mt
->level
[0].tile_mode
= 0x10;
174 mt
->level
[0].pitch
= align(pt
->width0
* blocksize
, 64);
175 mt
->total_size
= align(pt
->height0
, 16) * mt
->level
[0].pitch
* (mt
->layout_3d
? pt
->depth0
: 1);
177 if (pt
->array_size
> 1) {
178 mt
->layer_stride
= align(mt
->total_size
, NVC0_TILE_SIZE(0x10));
179 mt
->total_size
= mt
->layer_stride
* pt
->array_size
;
184 nvc0_miptree_init_layout_tiled(struct nv50_miptree
*mt
)
186 struct pipe_resource
*pt
= &mt
->base
.base
;
188 const unsigned blocksize
= util_format_get_blocksize(pt
->format
);
190 mt
->layout_3d
= pt
->target
== PIPE_TEXTURE_3D
;
192 w
= pt
->width0
<< mt
->ms_x
;
193 h
= pt
->height0
<< mt
->ms_y
;
195 /* For 3D textures, a mipmap is spanned by all the layers, for array
196 * textures and cube maps, each layer contains its own mipmaps.
198 d
= mt
->layout_3d
? pt
->depth0
: 1;
200 assert(!mt
->ms_mode
|| !pt
->last_level
);
202 for (l
= 0; l
<= pt
->last_level
; ++l
) {
203 struct nv50_miptree_level
*lvl
= &mt
->level
[l
];
204 unsigned tsx
, tsy
, tsz
;
205 unsigned nbx
= util_format_get_nblocksx(pt
->format
, w
);
206 unsigned nby
= util_format_get_nblocksy(pt
->format
, h
);
208 lvl
->offset
= mt
->total_size
;
210 lvl
->tile_mode
= nvc0_tex_choose_tile_dims(nbx
, nby
, d
);
212 tsx
= NVC0_TILE_SIZE_X(lvl
->tile_mode
); /* x is tile row pitch in bytes */
213 tsy
= NVC0_TILE_SIZE_Y(lvl
->tile_mode
);
214 tsz
= NVC0_TILE_SIZE_Z(lvl
->tile_mode
);
216 lvl
->pitch
= align(nbx
* blocksize
, tsx
);
218 mt
->total_size
+= lvl
->pitch
* align(nby
, tsy
) * align(d
, tsz
);
225 if (pt
->array_size
> 1) {
226 mt
->layer_stride
= align(mt
->total_size
,
227 NVC0_TILE_SIZE(mt
->level
[0].tile_mode
));
228 mt
->total_size
= mt
->layer_stride
* pt
->array_size
;
232 const struct u_resource_vtbl nvc0_miptree_vtbl
=
234 nv50_miptree_get_handle
, /* get_handle */
235 nv50_miptree_destroy
, /* resource_destroy */
236 nvc0_miptree_transfer_map
, /* transfer_map */
237 u_default_transfer_flush_region
, /* transfer_flush_region */
238 nvc0_miptree_transfer_unmap
, /* transfer_unmap */
239 u_default_transfer_inline_write
/* transfer_inline_write */
242 struct pipe_resource
*
243 nvc0_miptree_create(struct pipe_screen
*pscreen
,
244 const struct pipe_resource
*templ
)
246 struct nouveau_device
*dev
= nouveau_screen(pscreen
)->device
;
247 struct nv50_miptree
*mt
= CALLOC_STRUCT(nv50_miptree
);
248 struct pipe_resource
*pt
= &mt
->base
.base
;
249 boolean compressed
= dev
->drm_version
>= 0x01000101;
251 union nouveau_bo_config bo_config
;
257 mt
->base
.vtbl
= &nvc0_miptree_vtbl
;
259 pipe_reference_init(&pt
->reference
, 1);
260 pt
->screen
= pscreen
;
262 if (pt
->usage
== PIPE_USAGE_STAGING
) {
263 switch (pt
->target
) {
264 case PIPE_TEXTURE_1D
:
265 case PIPE_TEXTURE_2D
:
266 case PIPE_TEXTURE_RECT
:
267 if (pt
->last_level
== 0 &&
268 !util_format_is_depth_or_stencil(pt
->format
) &&
270 pt
->flags
|= NOUVEAU_RESOURCE_FLAG_LINEAR
;
277 if (pt
->bind
& PIPE_BIND_LINEAR
)
278 pt
->flags
|= NOUVEAU_RESOURCE_FLAG_LINEAR
;
280 bo_config
.nvc0
.memtype
= nvc0_mt_choose_storage_type(mt
, compressed
);
282 if (!nvc0_miptree_init_ms_mode(mt
)) {
287 if (unlikely(pt
->flags
& NVC0_RESOURCE_FLAG_VIDEO
)) {
288 nvc0_miptree_init_layout_video(mt
);
290 if (likely(bo_config
.nvc0
.memtype
)) {
291 nvc0_miptree_init_layout_tiled(mt
);
293 if (!nv50_miptree_init_layout_linear(mt
, 128)) {
297 bo_config
.nvc0
.tile_mode
= mt
->level
[0].tile_mode
;
299 if (!bo_config
.nvc0
.memtype
&& pt
->usage
== PIPE_USAGE_STAGING
)
300 mt
->base
.domain
= NOUVEAU_BO_GART
;
302 mt
->base
.domain
= NOUVEAU_BO_VRAM
;
304 bo_flags
= mt
->base
.domain
| NOUVEAU_BO_NOSNOOP
;
306 if (mt
->base
.base
.bind
& (PIPE_BIND_CURSOR
| PIPE_BIND_DISPLAY_TARGET
))
307 bo_flags
|= NOUVEAU_BO_CONTIG
;
309 ret
= nouveau_bo_new(dev
, bo_flags
, 4096, mt
->total_size
, &bo_config
,
315 mt
->base
.address
= mt
->base
.bo
->offset
;
317 NOUVEAU_DRV_STAT(nouveau_screen(pscreen
), tex_obj_current_count
, 1);
318 NOUVEAU_DRV_STAT(nouveau_screen(pscreen
), tex_obj_current_bytes
,
324 /* Offset of zslice @z from start of level @l. */
326 nvc0_mt_zslice_offset(const struct nv50_miptree
*mt
, unsigned l
, unsigned z
)
328 const struct pipe_resource
*pt
= &mt
->base
.base
;
330 unsigned tds
= NVC0_TILE_SHIFT_Z(mt
->level
[l
].tile_mode
);
331 unsigned ths
= NVC0_TILE_SHIFT_Y(mt
->level
[l
].tile_mode
);
333 unsigned nby
= util_format_get_nblocksy(pt
->format
,
334 u_minify(pt
->height0
, l
));
336 /* to next 2D tile slice within a 3D tile */
337 unsigned stride_2d
= NVC0_TILE_SIZE_2D(mt
->level
[l
].tile_mode
);
339 /* to slice in the next (in z direction) 3D tile */
340 unsigned stride_3d
= (align(nby
, (1 << ths
)) * mt
->level
[l
].pitch
) << tds
;
342 return (z
& (1 << (tds
- 1))) * stride_2d
+ (z
>> tds
) * stride_3d
;
345 /* Surface functions.
348 struct pipe_surface
*
349 nvc0_miptree_surface_new(struct pipe_context
*pipe
,
350 struct pipe_resource
*pt
,
351 const struct pipe_surface
*templ
)
353 struct nv50_surface
*ns
= nv50_surface_from_miptree(nv50_miptree(pt
), templ
);
356 ns
->base
.context
= pipe
;