Merge branch 'gallium-0.2' of git+ssh://marcheu@git.freedesktop.org/git/mesa/mesa...
[mesa.git] / src / gallium / drivers / nv04 / nv04_miptree.c
1 #include "pipe/p_state.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_inlines.h"
4
5 #include "nv04_context.h"
6 #include "nv04_screen.h"
7
8 static void
9 nv04_miptree_layout(struct nv04_miptree *nv04mt)
10 {
11 struct pipe_texture *pt = &nv04mt->base;
12 uint width = pt->width[0], height = pt->height[0];
13 uint offset = 0;
14 int nr_faces, l;
15
16 nr_faces = 1;
17
18 for (l = 0; l <= pt->last_level; l++) {
19 pt->width[l] = width;
20 pt->height[l] = height;
21
22 pt->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
23 pt->nblocksy[l] = pf_get_nblocksy(&pt->block, height);
24
25 nv04mt->level[l].pitch = pt->width[0];
26 nv04mt->level[l].pitch = (nv04mt->level[l].pitch + 63) & ~63;
27
28 width = MAX2(1, width >> 1);
29 height = MAX2(1, height >> 1);
30 }
31
32 for (l = 0; l <= pt->last_level; l++) {
33
34 nv04mt->level[l].image_offset = offset;
35 offset += nv04mt->level[l].pitch * pt->height[l];
36 }
37
38 nv04mt->total_size = offset;
39 }
40
41 static struct pipe_texture *
42 nv04_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
43 {
44 struct pipe_winsys *ws = pscreen->winsys;
45 struct nv04_miptree *mt;
46
47 mt = MALLOC(sizeof(struct nv04_miptree));
48 if (!mt)
49 return NULL;
50 mt->base = *pt;
51 mt->base.refcount = 1;
52 mt->base.screen = pscreen;
53 mt->shadow_tex = NULL;
54 mt->shadow_surface = NULL;
55
56 //mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
57
58 nv04_miptree_layout(mt);
59
60 mt->buffer = ws->buffer_create(ws, 256, PIPE_BUFFER_USAGE_PIXEL |
61 NOUVEAU_BUFFER_USAGE_TEXTURE,
62 mt->total_size);
63 if (!mt->buffer) {
64 printf("failed %d byte alloc\n",mt->total_size);
65 FREE(mt);
66 return NULL;
67 }
68
69 return &mt->base;
70 }
71
72 static void
73 nv04_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **ppt)
74 {
75 struct pipe_texture *pt = *ppt;
76 struct nv04_miptree *mt = (struct nv04_miptree *)pt;
77 int l;
78
79 *ppt = NULL;
80 if (--pt->refcount)
81 return;
82
83 pipe_buffer_reference(pscreen, &mt->buffer, NULL);
84 for (l = 0; l <= pt->last_level; l++) {
85 if (mt->level[l].image_offset)
86 FREE(mt->level[l].image_offset);
87 }
88
89 if (mt->shadow_tex) {
90 assert(mt->shadow_surface);
91 pscreen->tex_surface_release(pscreen, &mt->shadow_surface);
92 nv04_miptree_release(pscreen, &mt->shadow_tex);
93 }
94
95 FREE(mt);
96 }
97
98 static struct pipe_surface *
99 nv04_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
100 unsigned face, unsigned level, unsigned zslice,
101 unsigned flags)
102 {
103 struct nv04_miptree *nv04mt = (struct nv04_miptree *)pt;
104 struct pipe_surface *ps;
105
106 ps = CALLOC_STRUCT(pipe_surface);
107 if (!ps)
108 return NULL;
109 pipe_texture_reference(&ps->texture, pt);
110 pipe_buffer_reference(pscreen, &ps->buffer, nv04mt->buffer);
111 ps->format = pt->format;
112 ps->width = pt->width[level];
113 ps->height = pt->height[level];
114 ps->block = pt->block;
115 ps->nblocksx = pt->nblocksx[level];
116 ps->nblocksy = pt->nblocksy[level];
117 ps->stride = nv04mt->level[level].pitch;
118 ps->usage = flags;
119 ps->status = PIPE_SURFACE_STATUS_DEFINED;
120 ps->refcount = 1;
121 ps->face = face;
122 ps->level = level;
123 ps->zslice = zslice;
124
125 ps->offset = nv04mt->level[level].image_offset;
126
127 return ps;
128 }
129
130 static void
131 nv04_miptree_surface_del(struct pipe_screen *pscreen,
132 struct pipe_surface **psurface)
133 {
134 struct pipe_surface *ps = *psurface;
135
136 *psurface = NULL;
137 if (--ps->refcount > 0)
138 return;
139
140 pipe_texture_reference(&ps->texture, NULL);
141 pipe_buffer_reference(pscreen->winsys, &ps->buffer, NULL);
142 FREE(ps);
143 }
144
145 void
146 nv04_screen_init_miptree_functions(struct pipe_screen *pscreen)
147 {
148 pscreen->texture_create = nv04_miptree_create;
149 pscreen->texture_release = nv04_miptree_release;
150 pscreen->get_tex_surface = nv04_miptree_surface_new;
151 pscreen->tex_surface_release = nv04_miptree_surface_del;
152 }
153