nouveau: rewrite winsys in terms of drm_api, support dri2 state tracker
[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 uint width = pt->width[0], height = pt->height[0];
13 uint offset = 0;
14 int nr_faces, l, f;
15 uint wide_pitch = pt->tex_usage & (PIPE_TEXTURE_USAGE_SAMPLER |
16 PIPE_TEXTURE_USAGE_DEPTH_STENCIL |
17 PIPE_TEXTURE_USAGE_RENDER_TARGET |
18 PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
19 PIPE_TEXTURE_USAGE_PRIMARY);
20
21 if (pt->target == PIPE_TEXTURE_CUBE) {
22 nr_faces = 6;
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->nblocksx[l] = pf_get_nblocksx(&pt->block, width);
31 pt->nblocksy[l] = pf_get_nblocksy(&pt->block, height);
32
33 if (wide_pitch && (pt->tex_usage & NOUVEAU_TEXTURE_USAGE_LINEAR))
34 nv20mt->level[l].pitch = align(pt->width[0] * pt->block.size, 64);
35 else
36 nv20mt->level[l].pitch = pt->width[l] * pt->block.size;
37
38 nv20mt->level[l].image_offset =
39 CALLOC(nr_faces, sizeof(unsigned));
40
41 width = MAX2(1, width >> 1);
42 height = MAX2(1, height >> 1);
43 }
44
45 for (f = 0; f < nr_faces; f++) {
46 for (l = 0; l < pt->last_level; l++) {
47 nv20mt->level[l].image_offset[f] = offset;
48
49 if (!(pt->tex_usage & NOUVEAU_TEXTURE_USAGE_LINEAR) &&
50 pt->width[l + 1] > 1 && pt->height[l + 1] > 1)
51 offset += align(nv20mt->level[l].pitch * pt->height[l], 64);
52 else
53 offset += nv20mt->level[l].pitch * pt->height[l];
54 }
55
56 nv20mt->level[l].image_offset[f] = offset;
57 offset += nv20mt->level[l].pitch * pt->height[l];
58 }
59
60 nv20mt->total_size = offset;
61 }
62
63 static struct pipe_texture *
64 nv20_miptree_blanket(struct pipe_screen *pscreen, const struct pipe_texture *pt,
65 const unsigned *stride, struct pipe_buffer *pb)
66 {
67 struct nv20_miptree *mt;
68
69 /* Only supports 2D, non-mipmapped textures for the moment */
70 if (pt->target != PIPE_TEXTURE_2D || pt->last_level != 0 ||
71 pt->depth[0] != 1)
72 return NULL;
73
74 mt = CALLOC_STRUCT(nv20_miptree);
75 if (!mt)
76 return NULL;
77
78 mt->base = *pt;
79 pipe_reference_init(&mt->base.reference, 1);
80 mt->base.screen = pscreen;
81 mt->level[0].pitch = stride[0];
82 mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
83
84 pipe_buffer_reference(&mt->buffer, pb);
85 return &mt->base;
86 }
87
88 static struct pipe_texture *
89 nv20_miptree_create(struct pipe_screen *screen, const struct pipe_texture *pt)
90 {
91 struct nv20_miptree *mt;
92 unsigned buf_usage = PIPE_BUFFER_USAGE_PIXEL |
93 NOUVEAU_BUFFER_USAGE_TEXTURE;
94
95 mt = MALLOC(sizeof(struct nv20_miptree));
96 if (!mt)
97 return NULL;
98 mt->base = *pt;
99 pipe_reference_init(&mt->base.reference, 1);
100 mt->base.screen = screen;
101
102 /* Swizzled textures must be POT */
103 if (pt->width[0] & (pt->width[0] - 1) ||
104 pt->height[0] & (pt->height[0] - 1))
105 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
106 else
107 if (pt->tex_usage & (PIPE_TEXTURE_USAGE_PRIMARY |
108 PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
109 PIPE_TEXTURE_USAGE_DEPTH_STENCIL))
110 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
111 else
112 if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
113 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
114 else {
115 switch (pt->format) {
116 /* TODO: Figure out which formats can be swizzled */
117 case PIPE_FORMAT_A8R8G8B8_UNORM:
118 case PIPE_FORMAT_X8R8G8B8_UNORM:
119 case PIPE_FORMAT_R16_SNORM:
120 {
121 if (debug_get_bool_option("NOUVEAU_NO_SWIZZLE", FALSE))
122 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
123 break;
124 }
125 default:
126 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
127 }
128 }
129
130 if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
131 buf_usage |= PIPE_BUFFER_USAGE_CPU_READ_WRITE;
132
133 nv20_miptree_layout(mt);
134
135 mt->buffer = screen->buffer_create(screen, 256, buf_usage, mt->total_size);
136 if (!mt->buffer) {
137 FREE(mt);
138 return NULL;
139 }
140
141 return &mt->base;
142 }
143
144 static void
145 nv20_miptree_destroy(struct pipe_texture *pt)
146 {
147 struct nv20_miptree *nv20mt = (struct nv20_miptree *)pt;
148 int l;
149
150 pipe_buffer_reference(&nv20mt->buffer, NULL);
151 for (l = 0; l <= pt->last_level; l++) {
152 if (nv20mt->level[l].image_offset)
153 FREE(nv20mt->level[l].image_offset);
154 }
155 }
156
157 static struct pipe_surface *
158 nv20_miptree_surface_get(struct pipe_screen *screen, struct pipe_texture *pt,
159 unsigned face, unsigned level, unsigned zslice,
160 unsigned flags)
161 {
162 struct nv20_miptree *nv20mt = (struct nv20_miptree *)pt;
163 struct nv04_surface *ns;
164
165 ns = CALLOC_STRUCT(nv04_surface);
166 if (!ns)
167 return NULL;
168 pipe_texture_reference(&ns->base.texture, pt);
169 ns->base.format = pt->format;
170 ns->base.width = pt->width[level];
171 ns->base.height = pt->height[level];
172 ns->base.usage = flags;
173 ns->base.status = PIPE_SURFACE_STATUS_DEFINED;
174 pipe_reference_init(&ns->base.reference, 1);
175 ns->base.face = face;
176 ns->base.level = level;
177 ns->base.zslice = zslice;
178 ns->pitch = nv20mt->level[level].pitch;
179
180 if (pt->target == PIPE_TEXTURE_CUBE) {
181 ns->base.offset = nv20mt->level[level].image_offset[face];
182 } else
183 if (pt->target == PIPE_TEXTURE_3D) {
184 ns->base.offset = nv20mt->level[level].image_offset[zslice];
185 } else {
186 ns->base.offset = nv20mt->level[level].image_offset[0];
187 }
188
189 return &ns->base;
190 }
191
192 static void
193 nv20_miptree_surface_destroy(struct pipe_surface *ps)
194 {
195 pipe_texture_reference(&ps->texture, NULL);
196 FREE(ps);
197 }
198
199 void nv20_screen_init_miptree_functions(struct pipe_screen *pscreen)
200 {
201 pscreen->texture_create = nv20_miptree_create;
202 pscreen->texture_blanket = nv20_miptree_blanket;
203 pscreen->texture_destroy = nv20_miptree_destroy;
204 pscreen->get_tex_surface = nv20_miptree_surface_get;
205 pscreen->tex_surface_destroy = nv20_miptree_surface_destroy;
206 }
207