nv40: s/free/FREE/
[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_util.h"
4 #include "pipe/p_inlines.h"
5
6 #include "nv40_context.h"
7
8 static void
9 nv40_miptree_layout(struct nv40_miptree *nv40mt)
10 {
11 struct pipe_texture *pt = &nv40mt->base;
12 boolean swizzled = FALSE;
13 uint width = pt->width[0], height = pt->height[0], depth = pt->depth[0];
14 uint offset = 0;
15 int nr_faces, l, f, pitch;
16
17 if (pt->target == PIPE_TEXTURE_CUBE) {
18 nr_faces = 6;
19 } else
20 if (pt->target == PIPE_TEXTURE_3D) {
21 nr_faces = pt->depth[0];
22 } else {
23 nr_faces = 1;
24 }
25
26 pitch = pt->width[0];
27 for (l = 0; l <= pt->last_level; l++) {
28 pt->width[l] = width;
29 pt->height[l] = height;
30 pt->depth[l] = depth;
31
32 if (swizzled)
33 pitch = pt->width[l];
34 pitch = (pitch + 63) & ~63;
35
36 nv40mt->level[l].pitch = pitch * pt->cpp;
37 nv40mt->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
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 NOUVEAU_ERR("\n");
53
54 nv40mt->total_size = offset;
55 }
56
57 static struct pipe_texture *
58 nv40_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
59 {
60 struct pipe_winsys *ws = pscreen->winsys;
61 struct nv40_miptree *mt;
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
70 nv40_miptree_layout(mt);
71
72 mt->buffer = ws->buffer_create(ws, 256, PIPE_BUFFER_USAGE_PIXEL,
73 mt->total_size);
74 if (!mt->buffer) {
75 FREE(mt);
76 return NULL;
77 }
78
79 return &mt->base;
80 }
81
82 static void
83 nv40_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **pt)
84 {
85 struct pipe_winsys *ws = pscreen->winsys;
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(ws, &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 void
103 nv40_miptree_update(struct pipe_context *pipe, struct pipe_texture *mt,
104 uint face, uint levels)
105 {
106 }
107
108 static struct pipe_surface *
109 nv40_miptree_surface(struct pipe_screen *pscreen, struct pipe_texture *pt,
110 unsigned face, unsigned level, unsigned zslice)
111 {
112 struct pipe_winsys *ws = pscreen->winsys;
113 struct nv40_miptree *nv40mt = (struct nv40_miptree *)pt;
114 struct pipe_surface *ps;
115
116 ps = ws->surface_alloc(ws);
117 if (!ps)
118 return NULL;
119 pipe_buffer_reference(ws, &ps->buffer, nv40mt->buffer);
120 ps->format = pt->format;
121 ps->cpp = pt->cpp;
122 ps->width = pt->width[level];
123 ps->height = pt->height[level];
124 ps->pitch = nv40mt->level[level].pitch / ps->cpp;
125
126 if (pt->target == PIPE_TEXTURE_CUBE) {
127 ps->offset = nv40mt->level[level].image_offset[face];
128 } else
129 if (pt->target == PIPE_TEXTURE_3D) {
130 ps->offset = nv40mt->level[level].image_offset[zslice];
131 } else {
132 ps->offset = nv40mt->level[level].image_offset[0];
133 }
134
135 return ps;
136 }
137
138 void
139 nv40_init_miptree_functions(struct nv40_context *nv40)
140 {
141 nv40->pipe.texture_update = nv40_miptree_update;
142 }
143
144 void
145 nv40_screen_init_miptree_functions(struct pipe_screen *pscreen)
146 {
147 pscreen->texture_create = nv40_miptree_create;
148 pscreen->texture_release = nv40_miptree_release;
149 pscreen->get_tex_surface = nv40_miptree_surface;
150 }
151