nouveau: very quick port to tex-surface changes.
[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
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_winsys *ws = pscreen->winsys;
87 struct pipe_texture *mt = *pt;
88
89 *pt = NULL;
90 if (--mt->refcount <= 0) {
91 struct nv40_miptree *nv40mt = (struct nv40_miptree *)mt;
92 int l;
93
94 pipe_buffer_reference(ws, &nv40mt->buffer, NULL);
95 for (l = 0; l <= mt->last_level; l++) {
96 if (nv40mt->level[l].image_offset)
97 FREE(nv40mt->level[l].image_offset);
98 }
99 FREE(nv40mt);
100 }
101 }
102
103 static struct pipe_surface *
104 nv40_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
105 unsigned face, unsigned level, unsigned zslice,
106 unsigned flags)
107 {
108 struct pipe_winsys *ws = pscreen->winsys;
109 struct nv40_miptree *nv40mt = (struct nv40_miptree *)pt;
110 struct pipe_surface *ps;
111
112 ps = ws->surface_alloc(ws);
113 if (!ps)
114 return NULL;
115 pipe_buffer_reference(ws, &ps->buffer, nv40mt->buffer);
116 ps->format = pt->format;
117 ps->cpp = pt->cpp;
118 ps->width = pt->width[level];
119 ps->height = pt->height[level];
120 ps->pitch = nv40mt->level[level].pitch / ps->cpp;
121
122 if (pt->target == PIPE_TEXTURE_CUBE) {
123 ps->offset = nv40mt->level[level].image_offset[face];
124 } else
125 if (pt->target == PIPE_TEXTURE_3D) {
126 ps->offset = nv40mt->level[level].image_offset[zslice];
127 } else {
128 ps->offset = nv40mt->level[level].image_offset[0];
129 }
130
131 return ps;
132 }
133
134 static void
135 nv40_miptree_surface_del(struct pipe_screen *pscreen,
136 struct pipe_surface **psurface)
137 {
138 }
139
140 void
141 nv40_screen_init_miptree_functions(struct pipe_screen *pscreen)
142 {
143 pscreen->texture_create = nv40_miptree_create;
144 pscreen->texture_release = nv40_miptree_release;
145 pscreen->get_tex_surface = nv40_miptree_surface_new;
146 pscreen->tex_surface_release = nv40_miptree_surface_del;
147 }
148