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