1 #include "pipe/p_state.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_inlines.h"
5 #include "nv40_context.h"
8 nv40_miptree_layout(struct nv40_miptree
*mt
)
10 struct pipe_texture
*pt
= &mt
->base
;
11 uint width
= pt
->width
[0], height
= pt
->height
[0], depth
= pt
->depth
[0];
13 int nr_faces
, l
, f
, pitch
;
15 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
18 if (pt
->target
== PIPE_TEXTURE_3D
) {
19 nr_faces
= pt
->depth
[0];
25 for (l
= 0; l
<= pt
->last_level
; l
++) {
27 pt
->height
[l
] = height
;
29 pt
->nblocksx
[l
] = pf_get_nblocksx(&pt
->block
, width
);
30 pt
->nblocksy
[l
] = pf_get_nblocksy(&pt
->block
, height
);
32 if (!(pt
->tex_usage
& NOUVEAU_TEXTURE_USAGE_LINEAR
))
33 pitch
= pt
->nblocksx
[l
];
34 pitch
= align(pitch
, 64);
36 mt
->level
[l
].pitch
= pitch
* pt
->block
.size
;
37 mt
->level
[l
].image_offset
=
38 CALLOC(nr_faces
, sizeof(unsigned));
40 width
= MAX2(1, width
>> 1);
41 height
= MAX2(1, height
>> 1);
42 depth
= MAX2(1, depth
>> 1);
45 for (f
= 0; f
< nr_faces
; f
++) {
46 for (l
= 0; l
<= pt
->last_level
; l
++) {
47 mt
->level
[l
].image_offset
[f
] = offset
;
48 offset
+= mt
->level
[l
].pitch
* pt
->height
[l
];
52 mt
->total_size
= offset
;
55 static struct pipe_texture
*
56 nv40_miptree_create(struct pipe_screen
*pscreen
, const struct pipe_texture
*pt
)
58 struct pipe_winsys
*ws
= pscreen
->winsys
;
59 struct nv40_miptree
*mt
;
60 unsigned buf_usage
= PIPE_BUFFER_USAGE_PIXEL
|
61 NOUVEAU_BUFFER_USAGE_TEXTURE
;
63 mt
= MALLOC(sizeof(struct nv40_miptree
));
67 mt
->base
.refcount
= 1;
68 mt
->base
.screen
= pscreen
;
69 mt
->shadow_tex
= NULL
;
70 mt
->shadow_surface
= NULL
;
72 /* Swizzled textures must be POT */
73 if (pt
->width
[0] & (pt
->width
[0] - 1) ||
74 pt
->height
[0] & (pt
->height
[0] - 1))
75 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
77 if (pt
->tex_usage
& (PIPE_TEXTURE_USAGE_PRIMARY
|
78 PIPE_TEXTURE_USAGE_DISPLAY_TARGET
))
79 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
81 if (pt
->tex_usage
& PIPE_TEXTURE_USAGE_DYNAMIC
)
82 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
85 /* TODO: Figure out which formats can be swizzled */
86 case PIPE_FORMAT_A8R8G8B8_UNORM
:
87 case PIPE_FORMAT_X8R8G8B8_UNORM
:
88 case PIPE_FORMAT_R16_SNORM
:
91 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
95 if (pt
->tex_usage
& PIPE_TEXTURE_USAGE_DYNAMIC
)
96 buf_usage
|= PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
98 nv40_miptree_layout(mt
);
100 mt
->buffer
= ws
->buffer_create(ws
, 256, buf_usage
, mt
->total_size
);
109 static struct pipe_texture
*
110 nv40_miptree_blanket(struct pipe_screen
*pscreen
, const struct pipe_texture
*pt
,
111 const unsigned *stride
, struct pipe_buffer
*pb
)
113 struct nv40_miptree
*mt
;
115 /* Only supports 2D, non-mipmapped textures for the moment */
116 if (pt
->target
!= PIPE_TEXTURE_2D
|| pt
->last_level
!= 0 ||
120 mt
= CALLOC_STRUCT(nv40_miptree
);
125 mt
->base
.refcount
= 1;
126 mt
->base
.screen
= pscreen
;
127 mt
->level
[0].pitch
= stride
[0];
128 mt
->level
[0].image_offset
= CALLOC(1, sizeof(unsigned));
130 pipe_buffer_reference(pscreen
, &mt
->buffer
, pb
);
135 nv40_miptree_release(struct pipe_screen
*pscreen
, struct pipe_texture
**ppt
)
137 struct pipe_texture
*pt
= *ppt
;
138 struct nv40_miptree
*mt
= (struct nv40_miptree
*)pt
;
145 pipe_buffer_reference(pscreen
, &mt
->buffer
, NULL
);
146 for (l
= 0; l
<= pt
->last_level
; l
++) {
147 if (mt
->level
[l
].image_offset
)
148 FREE(mt
->level
[l
].image_offset
);
151 if (mt
->shadow_tex
) {
152 assert(mt
->shadow_surface
);
153 pscreen
->tex_surface_release(pscreen
, &mt
->shadow_surface
);
154 nv40_miptree_release(pscreen
, &mt
->shadow_tex
);
160 static struct pipe_surface
*
161 nv40_miptree_surface_new(struct pipe_screen
*pscreen
, struct pipe_texture
*pt
,
162 unsigned face
, unsigned level
, unsigned zslice
,
165 struct nv40_miptree
*mt
= (struct nv40_miptree
*)pt
;
166 struct pipe_surface
*ps
;
168 ps
= CALLOC_STRUCT(pipe_surface
);
171 pipe_texture_reference(&ps
->texture
, pt
);
172 ps
->format
= pt
->format
;
173 ps
->width
= pt
->width
[level
];
174 ps
->height
= pt
->height
[level
];
175 ps
->block
= pt
->block
;
176 ps
->nblocksx
= pt
->nblocksx
[level
];
177 ps
->nblocksy
= pt
->nblocksy
[level
];
178 ps
->stride
= mt
->level
[level
].pitch
;
180 ps
->status
= PIPE_SURFACE_STATUS_DEFINED
;
186 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
187 ps
->offset
= mt
->level
[level
].image_offset
[face
];
189 if (pt
->target
== PIPE_TEXTURE_3D
) {
190 ps
->offset
= mt
->level
[level
].image_offset
[zslice
];
192 ps
->offset
= mt
->level
[level
].image_offset
[0];
199 nv40_miptree_surface_del(struct pipe_screen
*pscreen
,
200 struct pipe_surface
**psurface
)
202 struct pipe_surface
*ps
= *psurface
;
205 if (--ps
->refcount
> 0)
208 pipe_texture_reference(&ps
->texture
, NULL
);
213 nv40_screen_init_miptree_functions(struct pipe_screen
*pscreen
)
215 pscreen
->texture_create
= nv40_miptree_create
;
216 pscreen
->texture_blanket
= nv40_miptree_blanket
;
217 pscreen
->texture_release
= nv40_miptree_release
;
218 pscreen
->get_tex_surface
= nv40_miptree_surface_new
;
219 pscreen
->tex_surface_release
= nv40_miptree_surface_del
;