Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / drivers / nv50 / nv50_miptree.c
1 /*
2 * Copyright 2008 Ben Skeggs
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "pipe/p_state.h"
24 #include "pipe/p_defines.h"
25 #include "pipe/p_inlines.h"
26
27 #include "nv50_context.h"
28
29 static struct pipe_texture *
30 nv50_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
31 {
32 struct pipe_winsys *ws = pscreen->winsys;
33 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
34 unsigned usage, width = pt->width[0], height = pt->height[0];
35 int i;
36
37 mt->base = *pt;
38 mt->base.refcount = 1;
39 mt->base.screen = pscreen;
40
41 usage = PIPE_BUFFER_USAGE_PIXEL;
42 switch (pt->format) {
43 case PIPE_FORMAT_Z24S8_UNORM:
44 case PIPE_FORMAT_Z16_UNORM:
45 usage |= NOUVEAU_BUFFER_USAGE_ZETA;
46 break;
47 default:
48 break;
49 }
50
51 switch (pt->target) {
52 case PIPE_TEXTURE_3D:
53 mt->image_nr = pt->depth[0];
54 break;
55 case PIPE_TEXTURE_CUBE:
56 mt->image_nr = 6;
57 break;
58 default:
59 mt->image_nr = 1;
60 break;
61 }
62 mt->image_offset = CALLOC(mt->image_nr, sizeof(int));
63
64 for (i = 0; i < mt->image_nr; i++) {
65 int image_size;
66
67 image_size = align(width, 8) * pt->block.size;
68 image_size = align(image_size, 64);
69 image_size *= align(height, 8) * pt->block.size;
70
71 mt->image_offset[i] = mt->total_size;
72 mt->total_size += image_size;
73 }
74
75 mt->buffer = ws->buffer_create(ws, 256, usage, mt->total_size);
76 if (!mt->buffer) {
77 FREE(mt);
78 return NULL;
79 }
80
81 return &mt->base;
82 }
83
84 static void
85 nv50_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **ppt)
86 {
87 struct pipe_texture *pt = *ppt;
88
89 *ppt = NULL;
90
91 if (--pt->refcount <= 0) {
92 struct nv50_miptree *mt = nv50_miptree(pt);
93
94 pipe_buffer_reference(pscreen, &mt->buffer, NULL);
95 FREE(mt);
96 }
97 }
98
99 static struct pipe_surface *
100 nv50_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
101 unsigned face, unsigned level, unsigned zslice,
102 unsigned flags)
103 {
104 struct nv50_miptree *mt = nv50_miptree(pt);
105 struct nv50_surface *s;
106 struct pipe_surface *ps;
107 int img;
108
109 if (pt->target == PIPE_TEXTURE_CUBE)
110 img = face;
111 else
112 if (pt->target == PIPE_TEXTURE_3D)
113 img = zslice;
114 else
115 img = 0;
116
117 s = CALLOC_STRUCT(nv50_surface);
118 if (!s)
119 return NULL;
120 ps = &s->base;
121
122 ps->refcount = 1;
123 ps->winsys = pscreen->winsys;
124 ps->format = pt->format;
125 ps->width = pt->width[level];
126 ps->height = pt->height[level];
127 ps->block = pt->block;
128 ps->nblocksx = pt->nblocksx[level];
129 ps->nblocksy = pt->nblocksy[level];
130 ps->stride = ps->width * ps->block.size;
131 ps->offset = mt->image_offset[img];
132 ps->usage = flags;
133 ps->status = PIPE_SURFACE_STATUS_DEFINED;
134
135 pipe_texture_reference(&ps->texture, pt);
136 pipe_buffer_reference(pscreen, &ps->buffer, mt->buffer);
137
138 return ps;
139 }
140
141 static void
142 nv50_miptree_surface_del(struct pipe_screen *pscreen,
143 struct pipe_surface **psurface)
144 {
145 struct pipe_surface *ps = *psurface;
146 struct nv50_surface *s = nv50_surface(ps);
147
148 *psurface = NULL;
149
150 if (--ps->refcount <= 0) {
151 pipe_texture_reference(&ps->texture, NULL);
152 pipe_buffer_reference(pscreen, &ps->buffer, NULL);
153 FREE(s);
154 }
155 }
156
157 void
158 nv50_screen_init_miptree_functions(struct pipe_screen *pscreen)
159 {
160 pscreen->texture_create = nv50_miptree_create;
161 pscreen->texture_release = nv50_miptree_release;
162 pscreen->get_tex_surface = nv50_miptree_surface_new;
163 pscreen->tex_surface_release = nv50_miptree_surface_del;
164 }
165