Merge branch 'gallium-0.2' into gallium-winsys-private
[mesa.git] / src / gallium / drivers / nv40 / nv40_miptree.c
1 #include "pipe/p_state.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_inlines.h"
4
5 #include "nv40_context.h"
6
7 static void
8 nv40_miptree_layout(struct nv40_miptree *mt)
9 {
10 struct pipe_texture *pt = &mt->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, pitch;
14
15 if (pt->target == PIPE_TEXTURE_CUBE) {
16 nr_faces = 6;
17 } else
18 if (pt->target == PIPE_TEXTURE_3D) {
19 nr_faces = pt->depth[0];
20 } else {
21 nr_faces = 1;
22 }
23
24 pitch = pt->width[0];
25 for (l = 0; l <= pt->last_level; l++) {
26 pt->width[l] = width;
27 pt->height[l] = height;
28 pt->depth[l] = depth;
29 pt->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
30 pt->nblocksy[l] = pf_get_nblocksy(&pt->block, height);
31
32 if (!(pt->tex_usage & NOUVEAU_TEXTURE_USAGE_LINEAR))
33 pitch = pt->nblocksx[l];
34 pitch = align(pitch, 64);
35
36 mt->level[l].pitch = pitch * pt->block.size;
37 mt->level[l].image_offset =
38 CALLOC(nr_faces, sizeof(unsigned));
39
40 width = MAX2(1, width >> 1);
41 height = MAX2(1, height >> 1);
42 depth = MAX2(1, depth >> 1);
43 }
44
45 for (f = 0; f < nr_faces; f++) {
46 for (l = 0; l <= pt->last_level; l++) {
47 mt->level[l].image_offset[f] = offset;
48 offset += mt->level[l].pitch * pt->height[l];
49 }
50 }
51
52 mt->total_size = offset;
53 }
54
55 static struct pipe_texture *
56 nv40_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
57 {
58 struct pipe_winsys *ws = pscreen->winsys;
59 struct nv40_miptree *mt;
60 unsigned buf_usage = PIPE_BUFFER_USAGE_PIXEL |
61 NOUVEAU_BUFFER_USAGE_TEXTURE;
62
63 mt = MALLOC(sizeof(struct nv40_miptree));
64 if (!mt)
65 return NULL;
66 mt->base = *pt;
67 mt->base.refcount = 1;
68 mt->base.screen = pscreen;
69 mt->shadow_tex = NULL;
70 mt->shadow_surface = NULL;
71
72 /* Swizzled textures must be POT */
73 if (pt->width[0] & (pt->width[0] - 1) ||
74 pt->height[0] & (pt->height[0] - 1))
75 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
76 else
77 if (pt->tex_usage & (PIPE_TEXTURE_USAGE_PRIMARY |
78 PIPE_TEXTURE_USAGE_DISPLAY_TARGET))
79 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
80 else
81 if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
82 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
83 else {
84 switch (pt->format) {
85 /* TODO: Figure out which formats can be swizzled */
86 case PIPE_FORMAT_A8R8G8B8_UNORM:
87 case PIPE_FORMAT_X8R8G8B8_UNORM:
88 case PIPE_FORMAT_R16_SNORM:
89 break;
90 default:
91 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
92 }
93 }
94
95 if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
96 buf_usage |= PIPE_BUFFER_USAGE_CPU_READ_WRITE;
97
98 nv40_miptree_layout(mt);
99
100 mt->buffer = ws->_buffer_create(ws, 256, buf_usage, mt->total_size);
101 if (!mt->buffer) {
102 FREE(mt);
103 return NULL;
104 }
105
106 return &mt->base;
107 }
108
109 static void
110 nv40_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **ppt)
111 {
112 struct pipe_texture *pt = *ppt;
113 struct nv40_miptree *mt = (struct nv40_miptree *)pt;
114 int l;
115
116 *ppt = NULL;
117 if (--pt->refcount)
118 return;
119
120 pipe_buffer_reference(pscreen, &mt->buffer, NULL);
121 for (l = 0; l <= pt->last_level; l++) {
122 if (mt->level[l].image_offset)
123 FREE(mt->level[l].image_offset);
124 }
125
126 if (mt->shadow_tex) {
127 assert(mt->shadow_surface);
128 pscreen->tex_surface_release(pscreen, &mt->shadow_surface);
129 nv40_miptree_release(pscreen, &mt->shadow_tex);
130 }
131
132 FREE(mt);
133 }
134
135 static struct pipe_surface *
136 nv40_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
137 unsigned face, unsigned level, unsigned zslice,
138 unsigned flags)
139 {
140 struct nv40_miptree *mt = (struct nv40_miptree *)pt;
141 struct pipe_surface *ps;
142
143 ps = CALLOC_STRUCT(pipe_surface);
144 if (!ps)
145 return NULL;
146 pipe_texture_reference(&ps->texture, pt);
147 pipe_buffer_reference(pscreen, &ps->buffer, mt->buffer);
148 ps->format = pt->format;
149 ps->width = pt->width[level];
150 ps->height = pt->height[level];
151 ps->block = pt->block;
152 ps->nblocksx = pt->nblocksx[level];
153 ps->nblocksy = pt->nblocksy[level];
154 ps->stride = mt->level[level].pitch;
155 ps->usage = flags;
156 ps->status = PIPE_SURFACE_STATUS_DEFINED;
157 ps->refcount = 1;
158 ps->face = face;
159 ps->level = level;
160 ps->zslice = zslice;
161
162 if (pt->target == PIPE_TEXTURE_CUBE) {
163 ps->offset = mt->level[level].image_offset[face];
164 } else
165 if (pt->target == PIPE_TEXTURE_3D) {
166 ps->offset = mt->level[level].image_offset[zslice];
167 } else {
168 ps->offset = mt->level[level].image_offset[0];
169 }
170
171 return ps;
172 }
173
174 static void
175 nv40_miptree_surface_del(struct pipe_screen *pscreen,
176 struct pipe_surface **psurface)
177 {
178 struct pipe_surface *ps = *psurface;
179
180 *psurface = NULL;
181 if (--ps->refcount > 0)
182 return;
183
184 pipe_texture_reference(&ps->texture, NULL);
185 pipe_buffer_reference(pscreen, &ps->buffer, NULL);
186 FREE(ps);
187 }
188
189 void
190 nv40_screen_init_miptree_functions(struct pipe_screen *pscreen)
191 {
192 pscreen->texture_create = nv40_miptree_create;
193 pscreen->texture_release = nv40_miptree_release;
194 pscreen->get_tex_surface = nv40_miptree_surface_new;
195 pscreen->tex_surface_release = nv40_miptree_surface_del;
196 }
197