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