gallium: adapt nv drivers to interface cleanups
[mesa.git] / src / gallium / drivers / nv30 / nv30_miptree.c
1 #include "pipe/p_state.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_inlines.h"
4 #include "util/u_math.h"
5
6 #include "nv30_context.h"
7
8 static void
9 nv30_miptree_layout(struct nv30_miptree *nv30mt)
10 {
11 struct pipe_texture *pt = &nv30mt->base;
12 uint width = pt->width0;
13 uint offset = 0;
14 int nr_faces, l, f;
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);
20
21 if (pt->target == PIPE_TEXTURE_CUBE) {
22 nr_faces = 6;
23 } else
24 if (pt->target == PIPE_TEXTURE_3D) {
25 nr_faces = pt->depth0;
26 } else {
27 nr_faces = 1;
28 }
29
30 for (l = 0; l <= pt->last_level; l++) {
31 if (wide_pitch && (pt->tex_usage & NOUVEAU_TEXTURE_USAGE_LINEAR))
32 nv30mt->level[l].pitch = align(pf_get_stride(pt->format, pt->width0), 64);
33 else
34 nv30mt->level[l].pitch = pf_get_stride(pt->format, width);
35
36 nv30mt->level[l].image_offset =
37 CALLOC(nr_faces, sizeof(unsigned));
38
39 width = u_minify(width, 1);
40 }
41
42 for (f = 0; f < nr_faces; f++) {
43 for (l = 0; l < pt->last_level; l++) {
44 nv30mt->level[l].image_offset[f] = offset;
45
46 if (!(pt->tex_usage & NOUVEAU_TEXTURE_USAGE_LINEAR) &&
47 u_minify(pt->width0, l + 1) > 1 && u_minify(pt->height0, l + 1) > 1)
48 offset += align(nv30mt->level[l].pitch * u_minify(pt->height0, l), 64);
49 else
50 offset += nv30mt->level[l].pitch * u_minify(pt->height0, l);
51 }
52
53 nv30mt->level[l].image_offset[f] = offset;
54 offset += nv30mt->level[l].pitch * u_minify(pt->height0, l);
55 }
56
57 nv30mt->total_size = offset;
58 }
59
60 static struct pipe_texture *
61 nv30_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
62 {
63 struct nv30_miptree *mt;
64 unsigned buf_usage = PIPE_BUFFER_USAGE_PIXEL |
65 NOUVEAU_BUFFER_USAGE_TEXTURE;
66
67 mt = MALLOC(sizeof(struct nv30_miptree));
68 if (!mt)
69 return NULL;
70 mt->base = *pt;
71 pipe_reference_init(&mt->base.reference, 1);
72 mt->base.screen = pscreen;
73
74 /* Swizzled textures must be POT */
75 if (pt->width0 & (pt->width0 - 1) ||
76 pt->height0 & (pt->height0 - 1))
77 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
78 else
79 if (pt->tex_usage & (PIPE_TEXTURE_USAGE_PRIMARY |
80 PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
81 PIPE_TEXTURE_USAGE_DEPTH_STENCIL))
82 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
83 else
84 if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
85 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
86 else {
87 switch (pt->format) {
88 /* TODO: Figure out which formats can be swizzled */
89 case PIPE_FORMAT_A8R8G8B8_UNORM:
90 case PIPE_FORMAT_X8R8G8B8_UNORM:
91 case PIPE_FORMAT_R16_SNORM:
92 case PIPE_FORMAT_R5G6B5_UNORM:
93 case PIPE_FORMAT_A8L8_UNORM:
94 case PIPE_FORMAT_A8_UNORM:
95 case PIPE_FORMAT_L8_UNORM:
96 case PIPE_FORMAT_I8_UNORM:
97 {
98 if (debug_get_bool_option("NOUVEAU_NO_SWIZZLE", FALSE))
99 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
100 break;
101 }
102 default:
103 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
104 }
105 }
106
107 if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
108 buf_usage |= PIPE_BUFFER_USAGE_CPU_READ_WRITE;
109
110 nv30_miptree_layout(mt);
111
112 mt->buffer = pscreen->buffer_create(pscreen, 256, buf_usage,
113 mt->total_size);
114 if (!mt->buffer) {
115 FREE(mt);
116 return NULL;
117 }
118
119 return &mt->base;
120 }
121
122 static struct pipe_texture *
123 nv30_miptree_blanket(struct pipe_screen *pscreen, const struct pipe_texture *pt,
124 const unsigned *stride, struct pipe_buffer *pb)
125 {
126 struct nv30_miptree *mt;
127
128 /* Only supports 2D, non-mipmapped textures for the moment */
129 if (pt->target != PIPE_TEXTURE_2D || pt->last_level != 0 ||
130 pt->depth0 != 1)
131 return NULL;
132
133 mt = CALLOC_STRUCT(nv30_miptree);
134 if (!mt)
135 return NULL;
136
137 mt->base = *pt;
138 pipe_reference_init(&mt->base.reference, 1);
139 mt->base.screen = pscreen;
140 mt->level[0].pitch = stride[0];
141 mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
142
143 /* Assume whoever created this buffer expects it to be linear for now */
144 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
145
146 pipe_buffer_reference(&mt->buffer, pb);
147 return &mt->base;
148 }
149
150 static void
151 nv30_miptree_destroy(struct pipe_texture *pt)
152 {
153 struct nv30_miptree *mt = (struct nv30_miptree *)pt;
154 int l;
155
156 pipe_buffer_reference(&mt->buffer, NULL);
157 for (l = 0; l <= pt->last_level; l++) {
158 if (mt->level[l].image_offset)
159 FREE(mt->level[l].image_offset);
160 }
161
162 FREE(mt);
163 }
164
165 static struct pipe_surface *
166 nv30_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
167 unsigned face, unsigned level, unsigned zslice,
168 unsigned flags)
169 {
170 struct nv30_miptree *nv30mt = (struct nv30_miptree *)pt;
171 struct nv04_surface *ns;
172
173 ns = CALLOC_STRUCT(nv04_surface);
174 if (!ns)
175 return NULL;
176 pipe_texture_reference(&ns->base.texture, pt);
177 ns->base.format = pt->format;
178 ns->base.width = u_minify(pt->width0, level);
179 ns->base.height = u_minify(pt->height0, level);
180 ns->base.usage = flags;
181 pipe_reference_init(&ns->base.reference, 1);
182 ns->base.face = face;
183 ns->base.level = level;
184 ns->base.zslice = zslice;
185 ns->pitch = nv30mt->level[level].pitch;
186
187 if (pt->target == PIPE_TEXTURE_CUBE) {
188 ns->base.offset = nv30mt->level[level].image_offset[face];
189 } else
190 if (pt->target == PIPE_TEXTURE_3D) {
191 ns->base.offset = nv30mt->level[level].image_offset[zslice];
192 } else {
193 ns->base.offset = nv30mt->level[level].image_offset[0];
194 }
195
196 return &ns->base;
197 }
198
199 static void
200 nv30_miptree_surface_del(struct pipe_surface *ps)
201 {
202 pipe_texture_reference(&ps->texture, NULL);
203 FREE(ps);
204 }
205
206 void
207 nv30_screen_init_miptree_functions(struct pipe_screen *pscreen)
208 {
209 pscreen->texture_create = nv30_miptree_create;
210 pscreen->texture_blanket = nv30_miptree_blanket;
211 pscreen->texture_destroy = nv30_miptree_destroy;
212 pscreen->get_tex_surface = nv30_miptree_surface_new;
213 pscreen->tex_surface_destroy = nv30_miptree_surface_del;
214 }