nouveau: gallium directory structure changed again..
[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 *nv40mt)
9 {
10 struct pipe_texture *pt = &nv40mt->base;
11 boolean swizzled = FALSE;
12 uint width = pt->width[0], height = pt->height[0], depth = pt->depth[0];
13 uint offset = 0;
14 int nr_faces, l, f, pitch;
15
16 if (pt->target == PIPE_TEXTURE_CUBE) {
17 nr_faces = 6;
18 } else
19 if (pt->target == PIPE_TEXTURE_3D) {
20 nr_faces = pt->depth[0];
21 } else {
22 nr_faces = 1;
23 }
24
25 pitch = pt->width[0];
26 for (l = 0; l <= pt->last_level; l++) {
27 pt->width[l] = width;
28 pt->height[l] = height;
29 pt->depth[l] = depth;
30 pt->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
31 pt->nblocksy[l] = pf_get_nblocksy(&pt->block, height);
32
33 if (swizzled)
34 pitch = pt->nblocksx[l];
35 pitch = align(pitch, 64);
36
37 nv40mt->level[l].pitch = pitch * pt->block.size;
38 nv40mt->level[l].image_offset =
39 CALLOC(nr_faces, sizeof(unsigned));
40
41 width = MAX2(1, width >> 1);
42 height = MAX2(1, height >> 1);
43 depth = MAX2(1, depth >> 1);
44 }
45
46 for (f = 0; f < nr_faces; f++) {
47 for (l = 0; l <= pt->last_level; l++) {
48 nv40mt->level[l].image_offset[f] = offset;
49 offset += nv40mt->level[l].pitch * pt->height[l];
50 }
51 }
52
53 nv40mt->total_size = offset;
54 }
55
56 static struct pipe_texture *
57 nv40_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
58 {
59 struct pipe_winsys *ws = pscreen->winsys;
60 struct nv40_miptree *mt;
61
62 mt = MALLOC(sizeof(struct nv40_miptree));
63 if (!mt)
64 return NULL;
65 mt->base = *pt;
66 mt->base.refcount = 1;
67 mt->base.screen = pscreen;
68
69 nv40_miptree_layout(mt);
70
71 mt->buffer = ws->buffer_create(ws, 256,
72 PIPE_BUFFER_USAGE_PIXEL |
73 NOUVEAU_BUFFER_USAGE_TEXTURE,
74 mt->total_size);
75 if (!mt->buffer) {
76 FREE(mt);
77 return NULL;
78 }
79
80 return &mt->base;
81 }
82
83 static void
84 nv40_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **pt)
85 {
86 struct pipe_texture *mt = *pt;
87
88 *pt = NULL;
89 if (--mt->refcount <= 0) {
90 struct nv40_miptree *nv40mt = (struct nv40_miptree *)mt;
91 int l;
92
93 pipe_buffer_reference(pscreen, &nv40mt->buffer, NULL);
94 for (l = 0; l <= mt->last_level; l++) {
95 if (nv40mt->level[l].image_offset)
96 FREE(nv40mt->level[l].image_offset);
97 }
98 FREE(nv40mt);
99 }
100 }
101
102 static struct pipe_surface *
103 nv40_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
104 unsigned face, unsigned level, unsigned zslice,
105 unsigned flags)
106 {
107 struct nv40_miptree *nv40mt = (struct nv40_miptree *)pt;
108 struct pipe_surface *ps;
109
110 ps = CALLOC_STRUCT(pipe_surface);
111 if (!ps)
112 return NULL;
113 pipe_texture_reference(&ps->texture, pt);
114 pipe_buffer_reference(pscreen, &ps->buffer, nv40mt->buffer);
115 ps->format = pt->format;
116 ps->width = pt->width[level];
117 ps->height = pt->height[level];
118 ps->block = pt->block;
119 ps->nblocksx = pt->nblocksx[level];
120 ps->nblocksy = pt->nblocksy[level];
121 ps->stride = nv40mt->level[level].pitch;
122 ps->usage = flags;
123 ps->status = PIPE_SURFACE_STATUS_DEFINED;
124
125 if (pt->target == PIPE_TEXTURE_CUBE) {
126 ps->offset = nv40mt->level[level].image_offset[face];
127 } else
128 if (pt->target == PIPE_TEXTURE_3D) {
129 ps->offset = nv40mt->level[level].image_offset[zslice];
130 } else {
131 ps->offset = nv40mt->level[level].image_offset[0];
132 }
133
134 return ps;
135 }
136
137 static void
138 nv40_miptree_surface_del(struct pipe_screen *pscreen,
139 struct pipe_surface **psurface)
140 {
141 struct pipe_surface *ps = *psurface;
142
143 *psurface = NULL;
144 if (--ps->refcount > 0)
145 return;
146
147 pipe_texture_reference(&ps->texture, NULL);
148 pipe_buffer_reference(pscreen, &ps->buffer, NULL);
149 FREE(ps);
150 }
151
152 void
153 nv40_screen_init_miptree_functions(struct pipe_screen *pscreen)
154 {
155 pscreen->texture_create = nv40_miptree_create;
156 pscreen->texture_release = nv40_miptree_release;
157 pscreen->get_tex_surface = nv40_miptree_surface_new;
158 pscreen->tex_surface_release = nv40_miptree_surface_del;
159 }
160