i965: fix bugs in projective texture coordinates
[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;
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];
26 nv04mt->level[l].pitch = (nv04mt->level[l].pitch + 63) & ~63;
27
28 width = MAX2(1, width >> 1);
29 height = MAX2(1, height >> 1);
30 }
31
32 for (l = 0; l <= pt->last_level; l++) {
33
34 nv04mt->level[l].image_offset = offset;
35 offset += nv04mt->level[l].pitch * pt->height[l];
36 }
37
38 nv04mt->total_size = offset;
39 }
40
41 static struct pipe_texture *
42 nv04_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
43 {
44 struct nv04_miptree *mt;
45
46 mt = MALLOC(sizeof(struct nv04_miptree));
47 if (!mt)
48 return NULL;
49 mt->base = *pt;
50 pipe_reference_init(&mt->base.reference, 1);
51 mt->base.screen = pscreen;
52
53 //mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
54
55 nv04_miptree_layout(mt);
56
57 mt->buffer = pscreen->buffer_create(pscreen, 256, PIPE_BUFFER_USAGE_PIXEL |
58 NOUVEAU_BUFFER_USAGE_TEXTURE,
59 mt->total_size);
60 if (!mt->buffer) {
61 printf("failed %d byte alloc\n",mt->total_size);
62 FREE(mt);
63 return NULL;
64 }
65
66 return &mt->base;
67 }
68
69 static struct pipe_texture *
70 nv04_miptree_blanket(struct pipe_screen *pscreen, const struct pipe_texture *pt,
71 const unsigned *stride, struct pipe_buffer *pb)
72 {
73 struct nv04_miptree *mt;
74
75 /* Only supports 2D, non-mipmapped textures for the moment */
76 if (pt->target != PIPE_TEXTURE_2D || pt->last_level != 0 ||
77 pt->depth[0] != 1)
78 return NULL;
79
80 mt = CALLOC_STRUCT(nv04_miptree);
81 if (!mt)
82 return NULL;
83
84 mt->base = *pt;
85 pipe_reference_init(&mt->base.reference, 1);
86 mt->base.screen = pscreen;
87 mt->level[0].pitch = stride[0];
88 mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
89
90 pipe_buffer_reference(&mt->buffer, pb);
91 return &mt->base;
92 }
93
94 static void
95 nv04_miptree_destroy(struct pipe_texture *pt)
96 {
97 struct nv04_miptree *mt = (struct nv04_miptree *)pt;
98 int l;
99
100 pipe_buffer_reference(&mt->buffer, NULL);
101 for (l = 0; l <= pt->last_level; l++) {
102 if (mt->level[l].image_offset)
103 FREE(mt->level[l].image_offset);
104 }
105
106 FREE(mt);
107 }
108
109 static struct pipe_surface *
110 nv04_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
111 unsigned face, unsigned level, unsigned zslice,
112 unsigned flags)
113 {
114 struct nv04_miptree *nv04mt = (struct nv04_miptree *)pt;
115 struct nv04_surface *ns;
116
117 ns = CALLOC_STRUCT(nv04_surface);
118 if (!ns)
119 return NULL;
120 pipe_texture_reference(&ns->base.texture, pt);
121 ns->base.format = pt->format;
122 ns->base.width = pt->width[level];
123 ns->base.height = pt->height[level];
124 ns->base.usage = flags;
125 pipe_reference_init(&ns->base.reference, 1);
126 ns->base.face = face;
127 ns->base.level = level;
128 ns->base.zslice = zslice;
129 ns->pitch = nv04mt->level[level].pitch;
130
131 ns->base.offset = nv04mt->level[level].image_offset;
132
133 return &ns->base;
134 }
135
136 static void
137 nv04_miptree_surface_del(struct pipe_surface *ps)
138 {
139 pipe_texture_reference(&ps->texture, NULL);
140 FREE(ps);
141 }
142
143 void
144 nv04_screen_init_miptree_functions(struct pipe_screen *pscreen)
145 {
146 pscreen->texture_create = nv04_miptree_create;
147 pscreen->texture_blanket = nv04_miptree_blanket;
148 pscreen->texture_destroy = nv04_miptree_destroy;
149 pscreen->get_tex_surface = nv04_miptree_surface_new;
150 pscreen->tex_surface_destroy = nv04_miptree_surface_del;
151 }
152