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 uint width
= pt
->width
[0], height
= pt
->height
[0];
15 uint wide_pitch
= pt
->tex_usage
& (PIPE_TEXTURE_USAGE_SAMPLER
|
16 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
|
17 PIPE_TEXTURE_USAGE_RENDER_TARGET
|
18 PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
19 PIPE_TEXTURE_USAGE_PRIMARY
);
21 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
27 for (l
= 0; l
<= pt
->last_level
; l
++) {
29 pt
->height
[l
] = height
;
30 pt
->nblocksx
[l
] = pf_get_nblocksx(&pt
->block
, width
);
31 pt
->nblocksy
[l
] = pf_get_nblocksy(&pt
->block
, height
);
33 if (wide_pitch
&& (pt
->tex_usage
& NOUVEAU_TEXTURE_USAGE_LINEAR
))
34 nv20mt
->level
[l
].pitch
= align(pt
->width
[0] * pt
->block
.size
, 64);
36 nv20mt
->level
[l
].pitch
= pt
->width
[l
] * pt
->block
.size
;
38 nv20mt
->level
[l
].image_offset
=
39 CALLOC(nr_faces
, sizeof(unsigned));
41 width
= MAX2(1, width
>> 1);
42 height
= MAX2(1, height
>> 1);
45 for (f
= 0; f
< nr_faces
; f
++) {
46 for (l
= 0; l
< pt
->last_level
; l
++) {
47 nv20mt
->level
[l
].image_offset
[f
] = offset
;
49 if (!(pt
->tex_usage
& NOUVEAU_TEXTURE_USAGE_LINEAR
) &&
50 pt
->width
[l
+ 1] > 1 && pt
->height
[l
+ 1] > 1)
51 offset
+= align(nv20mt
->level
[l
].pitch
* pt
->height
[l
], 64);
53 offset
+= nv20mt
->level
[l
].pitch
* pt
->height
[l
];
56 nv20mt
->level
[l
].image_offset
[f
] = offset
;
57 offset
+= nv20mt
->level
[l
].pitch
* pt
->height
[l
];
60 nv20mt
->total_size
= offset
;
63 static struct pipe_texture
*
64 nv20_miptree_blanket(struct pipe_screen
*pscreen
, const struct pipe_texture
*pt
,
65 const unsigned *stride
, struct pipe_buffer
*pb
)
67 struct nv20_miptree
*mt
;
69 /* Only supports 2D, non-mipmapped textures for the moment */
70 if (pt
->target
!= PIPE_TEXTURE_2D
|| pt
->last_level
!= 0 ||
74 mt
= CALLOC_STRUCT(nv20_miptree
);
79 pipe_reference_init(&mt
->base
.reference
, 1);
80 mt
->base
.screen
= pscreen
;
81 mt
->level
[0].pitch
= stride
[0];
82 mt
->level
[0].image_offset
= CALLOC(1, sizeof(unsigned));
84 pipe_buffer_reference(&mt
->buffer
, pb
);
88 static struct pipe_texture
*
89 nv20_miptree_create(struct pipe_screen
*screen
, const struct pipe_texture
*pt
)
91 struct nv20_miptree
*mt
;
92 unsigned buf_usage
= PIPE_BUFFER_USAGE_PIXEL
|
93 NOUVEAU_BUFFER_USAGE_TEXTURE
;
95 mt
= MALLOC(sizeof(struct nv20_miptree
));
99 pipe_reference_init(&mt
->base
.reference
, 1);
100 mt
->base
.screen
= screen
;
102 /* Swizzled textures must be POT */
103 if (pt
->width
[0] & (pt
->width
[0] - 1) ||
104 pt
->height
[0] & (pt
->height
[0] - 1))
105 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
107 if (pt
->tex_usage
& (PIPE_TEXTURE_USAGE_PRIMARY
|
108 PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
109 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
))
110 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
112 if (pt
->tex_usage
& PIPE_TEXTURE_USAGE_DYNAMIC
)
113 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
115 switch (pt
->format
) {
116 /* TODO: Figure out which formats can be swizzled */
117 case PIPE_FORMAT_A8R8G8B8_UNORM
:
118 case PIPE_FORMAT_X8R8G8B8_UNORM
:
119 case PIPE_FORMAT_R16_SNORM
:
121 if (debug_get_bool_option("NOUVEAU_NO_SWIZZLE", FALSE
))
122 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
126 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
130 if (pt
->tex_usage
& PIPE_TEXTURE_USAGE_DYNAMIC
)
131 buf_usage
|= PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
133 nv20_miptree_layout(mt
);
135 mt
->buffer
= screen
->buffer_create(screen
, 256, buf_usage
, mt
->total_size
);
145 nv20_miptree_destroy(struct pipe_texture
*pt
)
147 struct nv20_miptree
*nv20mt
= (struct nv20_miptree
*)pt
;
150 pipe_buffer_reference(&nv20mt
->buffer
, NULL
);
151 for (l
= 0; l
<= pt
->last_level
; l
++) {
152 if (nv20mt
->level
[l
].image_offset
)
153 FREE(nv20mt
->level
[l
].image_offset
);
157 static struct pipe_surface
*
158 nv20_miptree_surface_get(struct pipe_screen
*screen
, struct pipe_texture
*pt
,
159 unsigned face
, unsigned level
, unsigned zslice
,
162 struct nv20_miptree
*nv20mt
= (struct nv20_miptree
*)pt
;
163 struct nv04_surface
*ns
;
165 ns
= CALLOC_STRUCT(nv04_surface
);
168 pipe_texture_reference(&ns
->base
.texture
, pt
);
169 ns
->base
.format
= pt
->format
;
170 ns
->base
.width
= pt
->width
[level
];
171 ns
->base
.height
= pt
->height
[level
];
172 ns
->base
.usage
= flags
;
173 pipe_reference_init(&ns
->base
.reference
, 1);
174 ns
->base
.face
= face
;
175 ns
->base
.level
= level
;
176 ns
->base
.zslice
= zslice
;
177 ns
->pitch
= nv20mt
->level
[level
].pitch
;
179 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
180 ns
->base
.offset
= nv20mt
->level
[level
].image_offset
[face
];
182 if (pt
->target
== PIPE_TEXTURE_3D
) {
183 ns
->base
.offset
= nv20mt
->level
[level
].image_offset
[zslice
];
185 ns
->base
.offset
= nv20mt
->level
[level
].image_offset
[0];
192 nv20_miptree_surface_destroy(struct pipe_surface
*ps
)
194 pipe_texture_reference(&ps
->texture
, NULL
);
198 void nv20_screen_init_miptree_functions(struct pipe_screen
*pscreen
)
200 pscreen
->texture_create
= nv20_miptree_create
;
201 pscreen
->texture_blanket
= nv20_miptree_blanket
;
202 pscreen
->texture_destroy
= nv20_miptree_destroy
;
203 pscreen
->get_tex_surface
= nv20_miptree_surface_get
;
204 pscreen
->tex_surface_destroy
= nv20_miptree_surface_destroy
;