nouveau: gallium directory structure changed again..
[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, f;
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] * pt->block.size;
26 nv04mt->level[l].pitch = (nv04mt->level[l].pitch + 63) & ~63;
27
28 nv04mt->level[l].image_offset =
29 CALLOC(nr_faces, sizeof(unsigned));
30
31 width = MAX2(1, width >> 1);
32 height = MAX2(1, height >> 1);
33
34 }
35
36 for (f = 0; f < nr_faces; f++) {
37 for (l = 0; l <= pt->last_level; l++) {
38 nv04mt->level[l].image_offset[f] = offset;
39 offset += nv04mt->level[l].pitch * pt->height[l];
40 }
41 }
42
43 nv04mt->total_size = offset;
44 }
45
46 static struct pipe_texture *
47 nv04_miptree_create(struct pipe_screen *screen, const struct pipe_texture *pt)
48 {
49 struct pipe_winsys *ws = screen->winsys;
50 struct nv04_miptree *mt;
51
52 mt = MALLOC(sizeof(struct nv04_miptree));
53 if (!mt)
54 return NULL;
55 mt->base = *pt;
56 mt->base.refcount = 1;
57 mt->base.screen = screen;
58
59 nv04_miptree_layout(mt);
60
61 mt->buffer = ws->buffer_create(ws, 256, PIPE_BUFFER_USAGE_PIXEL,
62 mt->total_size);
63 if (!mt->buffer) {
64 FREE(mt);
65 return NULL;
66 }
67
68 return &mt->base;
69 }
70
71 static void
72 nv04_miptree_release(struct pipe_screen *screen, struct pipe_texture **pt)
73 {
74 struct pipe_texture *mt = *pt;
75
76 *pt = NULL;
77 if (--mt->refcount <= 0) {
78 struct nv04_miptree *nv04mt = (struct nv04_miptree *)mt;
79 int l;
80
81 pipe_buffer_reference(screen, &nv04mt->buffer, NULL);
82 for (l = 0; l <= mt->last_level; l++) {
83 if (nv04mt->level[l].image_offset)
84 FREE(nv04mt->level[l].image_offset);
85 }
86 FREE(nv04mt);
87 }
88 }
89
90 static struct pipe_surface *
91 nv04_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
92 unsigned face, unsigned level, unsigned zslice,
93 unsigned flags)
94 {
95 struct pipe_winsys *ws = pscreen->winsys;
96 struct nv04_miptree *nv04mt = (struct nv04_miptree *)pt;
97 struct pipe_surface *ps;
98
99 ps = ws->surface_alloc(ws);
100 if (!ps)
101 return NULL;
102 pipe_buffer_reference(pscreen, &ps->buffer, nv04mt->buffer);
103 ps->format = pt->format;
104 ps->width = pt->width[level];
105 ps->height = pt->height[level];
106 ps->block = pt->block;
107 ps->width = pt->width[level];
108 ps->height = pt->height[level];
109 ps->nblocksx = pt->nblocksx[level];
110 ps->nblocksy = pt->nblocksy[level];
111 ps->stride = nv04mt->level[level].pitch;
112
113 if (pt->target == PIPE_TEXTURE_CUBE) {
114 ps->offset = nv04mt->level[level].image_offset[face];
115 } else {
116 ps->offset = nv04mt->level[level].image_offset[0];
117 }
118
119 return ps;
120 }
121
122 static void
123 nv04_miptree_surface_del(struct pipe_screen *pscreen,
124 struct pipe_surface **psurface)
125 {
126 }
127
128 void
129 nv04_init_miptree_functions(struct pipe_screen *pscreen)
130 {
131 pscreen->texture_create = nv04_miptree_create;
132 pscreen->texture_release = nv04_miptree_release;
133 pscreen->get_tex_surface = nv04_miptree_surface_new;
134 pscreen->tex_surface_release = nv04_miptree_surface_del;
135 }
136