1 #include "pipe/p_state.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_inlines.h"
5 #include "nv20_context.h"
6 #include "nv20_screen.h"
9 nv20_miptree_layout(struct nv20_miptree
*nv20mt
)
11 struct pipe_texture
*pt
= &nv20mt
->base
;
12 boolean swizzled
= FALSE
;
13 uint width
= pt
->width
[0], height
= pt
->height
[0];
17 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
23 for (l
= 0; l
<= pt
->last_level
; l
++) {
25 pt
->height
[l
] = height
;
26 pt
->nblocksx
[l
] = pf_get_nblocksx(&pt
->block
, width
);
27 pt
->nblocksy
[l
] = pf_get_nblocksy(&pt
->block
, height
);
30 nv20mt
->level
[l
].pitch
= pt
->nblocksx
[l
] * pt
->block
.size
;
32 nv20mt
->level
[l
].pitch
= pt
->nblocksx
[0] * pt
->block
.size
;
33 nv20mt
->level
[l
].pitch
= (nv20mt
->level
[l
].pitch
+ 63) & ~63;
35 nv20mt
->level
[l
].image_offset
=
36 CALLOC(nr_faces
, sizeof(unsigned));
38 width
= MAX2(1, width
>> 1);
39 height
= MAX2(1, height
>> 1);
43 for (f
= 0; f
< nr_faces
; f
++) {
44 for (l
= 0; l
<= pt
->last_level
; l
++) {
45 nv20mt
->level
[l
].image_offset
[f
] = offset
;
46 offset
+= nv20mt
->level
[l
].pitch
* pt
->height
[l
];
50 nv20mt
->total_size
= offset
;
53 static struct pipe_texture
*
54 nv20_miptree_blanket(struct pipe_screen
*pscreen
, const struct pipe_texture
*pt
,
55 const unsigned *stride
, struct pipe_buffer
*pb
)
57 struct nv20_miptree
*mt
;
59 /* Only supports 2D, non-mipmapped textures for the moment */
60 if (pt
->target
!= PIPE_TEXTURE_2D
|| pt
->last_level
!= 0 ||
64 mt
= CALLOC_STRUCT(nv20_miptree
);
69 mt
->base
.refcount
= 1;
70 mt
->base
.screen
= pscreen
;
71 mt
->level
[0].image_offset
= CALLOC(1, sizeof(unsigned));
73 pipe_buffer_reference(pscreen
, &mt
->buffer
, pb
);
77 static struct pipe_texture
*
78 nv20_miptree_create(struct pipe_screen
*screen
, const struct pipe_texture
*pt
)
80 struct pipe_winsys
*ws
= screen
->winsys
;
81 struct nv20_miptree
*mt
;
82 unsigned buf_usage
= PIPE_BUFFER_USAGE_PIXEL
|
83 NOUVEAU_BUFFER_USAGE_TEXTURE
;
85 mt
= MALLOC(sizeof(struct nv20_miptree
));
89 mt
->base
.refcount
= 1;
90 mt
->base
.screen
= screen
;
92 /* Swizzled textures must be POT */
93 if (pt
->width
[0] & (pt
->width
[0] - 1) ||
94 pt
->height
[0] & (pt
->height
[0] - 1))
95 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
97 if (pt
->tex_usage
& (PIPE_TEXTURE_USAGE_PRIMARY
|
98 PIPE_TEXTURE_USAGE_DISPLAY_TARGET
))
99 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
101 if (pt
->tex_usage
& PIPE_TEXTURE_USAGE_DYNAMIC
)
102 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
104 switch (pt
->format
) {
105 /* TODO: Figure out which formats can be swizzled */
106 case PIPE_FORMAT_A8R8G8B8_UNORM
:
107 case PIPE_FORMAT_X8R8G8B8_UNORM
:
108 case PIPE_FORMAT_R16_SNORM
:
111 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
115 if (pt
->tex_usage
& PIPE_TEXTURE_USAGE_DYNAMIC
)
116 buf_usage
|= PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
118 nv20_miptree_layout(mt
);
120 mt
->buffer
= ws
->buffer_create(ws
, 256, buf_usage
, mt
->total_size
);
130 nv20_miptree_release(struct pipe_screen
*screen
, struct pipe_texture
**pt
)
132 struct pipe_texture
*mt
= *pt
;
135 if (--mt
->refcount
<= 0) {
136 struct nv20_miptree
*nv20mt
= (struct nv20_miptree
*)mt
;
139 pipe_buffer_reference(screen
, &nv20mt
->buffer
, NULL
);
140 for (l
= 0; l
<= mt
->last_level
; l
++) {
141 if (nv20mt
->level
[l
].image_offset
)
142 FREE(nv20mt
->level
[l
].image_offset
);
148 static struct pipe_surface
*
149 nv20_miptree_surface_get(struct pipe_screen
*screen
, struct pipe_texture
*pt
,
150 unsigned face
, unsigned level
, unsigned zslice
,
153 struct nv20_miptree
*nv20mt
= (struct nv20_miptree
*)pt
;
154 struct pipe_surface
*ps
;
156 ps
= CALLOC_STRUCT(pipe_surface
);
159 pipe_texture_reference(&ps
->texture
, pt
);
160 ps
->format
= pt
->format
;
161 ps
->width
= pt
->width
[level
];
162 ps
->height
= pt
->height
[level
];
163 ps
->block
= pt
->block
;
164 ps
->nblocksx
= pt
->nblocksx
[level
];
165 ps
->nblocksy
= pt
->nblocksy
[level
];
166 ps
->stride
= nv20mt
->level
[level
].pitch
;
168 ps
->status
= PIPE_SURFACE_STATUS_DEFINED
;
171 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
172 ps
->offset
= nv20mt
->level
[level
].image_offset
[face
];
174 if (pt
->target
== PIPE_TEXTURE_3D
) {
175 ps
->offset
= nv20mt
->level
[level
].image_offset
[zslice
];
177 ps
->offset
= nv20mt
->level
[level
].image_offset
[0];
184 nv20_miptree_surface_release(struct pipe_screen
*pscreen
,
185 struct pipe_surface
**psurface
)
187 struct pipe_surface
*ps
= *psurface
;
190 if (--ps
->refcount
> 0)
193 pipe_texture_reference(&ps
->texture
, NULL
);
197 void nv20_screen_init_miptree_functions(struct pipe_screen
*pscreen
)
199 pscreen
->texture_create
= nv20_miptree_create
;
200 pscreen
->texture_blanket
= nv20_miptree_blanket
;
201 pscreen
->texture_release
= nv20_miptree_release
;
202 pscreen
->get_tex_surface
= nv20_miptree_surface_get
;
203 pscreen
->tex_surface_release
= nv20_miptree_surface_release
;