nouveau: match gallium API changes
[mesa.git] / src / gallium / drivers / nv30 / nv30_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 "nv30_context.h"
7 #include "nv30_screen.h"
8
9 static void
10 nv30_miptree_layout(struct nv30_miptree *nv30mt)
11 {
12 struct pipe_texture *pt = &nv30mt->base;
13 boolean swizzled = FALSE;
14 uint width = pt->width[0], height = pt->height[0], depth = pt->depth[0];
15 uint offset = 0;
16 int nr_faces, l, f;
17
18 if (pt->target == PIPE_TEXTURE_CUBE) {
19 nr_faces = 6;
20 } else
21 if (pt->target == PIPE_TEXTURE_3D) {
22 nr_faces = pt->depth[0];
23 } else {
24 nr_faces = 1;
25 }
26
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 nv30mt->level[l].pitch = pt->width[l] * pt->cpp;
34 else
35 nv30mt->level[l].pitch = pt->width[0] * pt->cpp;
36 nv30mt->level[l].pitch = (nv30mt->level[l].pitch + 63) & ~63;
37
38 nv30mt->level[l].image_offset =
39 CALLOC(nr_faces, sizeof(unsigned));
40
41 width = MAX2(1, width >> 1);
42 height = MAX2(1, height >> 1);
43 depth = MAX2(1, depth >> 1);
44
45 }
46
47 for (f = 0; f < nr_faces; f++) {
48 for (l = 0; l <= pt->last_level; l++) {
49 nv30mt->level[l].image_offset[f] = offset;
50 offset += nv30mt->level[l].pitch * pt->height[l];
51 }
52 }
53
54 nv30mt->total_size = offset;
55 }
56
57 static struct pipe_texture *
58 nv30_miptree_create(struct pipe_screen *screen, const struct pipe_texture *pt)
59 {
60 struct pipe_winsys *ws = screen->winsys;
61 struct nv30_miptree *mt;
62
63 mt = MALLOC(sizeof(struct nv30_miptree));
64 if (!mt)
65 return NULL;
66 mt->base = *pt;
67 mt->base.refcount = 1;
68 mt->base.screen = screen;
69
70 nv30_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 nv30_miptree_release(struct pipe_screen *screen, struct pipe_texture **pt)
84 {
85 struct pipe_winsys *ws = screen->winsys;
86 struct pipe_texture *mt = *pt;
87
88 *pt = NULL;
89 if (--mt->refcount <= 0) {
90 struct nv30_miptree *nv30mt = (struct nv30_miptree *)mt;
91 int l;
92
93 pipe_buffer_reference(ws, &nv30mt->buffer, NULL);
94 for (l = 0; l <= mt->last_level; l++) {
95 if (nv30mt->level[l].image_offset)
96 free(nv30mt->level[l].image_offset);
97 }
98 free(nv30mt);
99 }
100 }
101
102 static void
103 nv30_miptree_update(struct pipe_context *pipe, struct pipe_texture *mt,
104 uint face, uint levels)
105 {
106 }
107
108 static struct pipe_surface *
109 nv30_miptree_surface_get(struct pipe_screen *screen, struct pipe_texture *pt,
110 unsigned face, unsigned level, unsigned zslice)
111 {
112 struct pipe_winsys *ws = screen->winsys;
113 struct nv30_miptree *nv30mt = (struct nv30_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, nv30mt->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 = nv30mt->level[level].pitch / ps->cpp;
125
126 if (pt->target == PIPE_TEXTURE_CUBE) {
127 ps->offset = nv30mt->level[level].image_offset[face];
128 } else
129 if (pt->target == PIPE_TEXTURE_3D) {
130 ps->offset = nv30mt->level[level].image_offset[zslice];
131 } else {
132 ps->offset = nv30mt->level[level].image_offset[0];
133 }
134
135 return ps;
136 }
137
138 void
139 nv30_init_miptree_functions(struct nv30_context *nv30)
140 {
141 nv30->pipe.texture_update = nv30_miptree_update;
142 }
143
144 void
145 nv30_screen_init_miptree_functions(struct pipe_screen *pscreen)
146 {
147 pscreen->texture_create = nv30_miptree_create;
148 pscreen->texture_release = nv30_miptree_release;
149 pscreen->get_tex_surface = nv30_miptree_surface_get;
150 }
151