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