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