a02ad41885ba99935da161cbe4439e57c8876b8a
[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_util.h"
26 #include "pipe/p_inlines.h"
27
28 #include "nv50_context.h"
29
30 static struct pipe_texture *
31 nv50_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
32 {
33 struct pipe_winsys *ws = pscreen->winsys;
34 struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
35 unsigned usage, pitch;
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 pitch = ((pt->width[0] + 63) & ~63) * pt->block.size;
52
53 mt->buffer = ws->buffer_create(ws, 256, usage, pitch * pt->height[0]);
54 if (!mt->buffer) {
55 FREE(mt);
56 return NULL;
57 }
58
59 return &mt->base;
60 }
61
62 static void
63 nv50_miptree_release(struct pipe_screen *pscreen, struct pipe_texture **ppt)
64 {
65 struct pipe_winsys *ws = pscreen->winsys;
66 struct pipe_texture *pt = *ppt;
67
68 *ppt = NULL;
69
70 if (--pt->refcount <= 0) {
71 struct nv50_miptree *mt = nv50_miptree(pt);
72
73 pipe_buffer_reference(ws, &mt->buffer, NULL);
74 FREE(mt);
75 }
76 }
77
78 static struct pipe_surface *
79 nv50_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
80 unsigned face, unsigned level, unsigned zslice,
81 unsigned flags)
82 {
83 struct pipe_winsys *ws = pscreen->winsys;
84 struct nv50_miptree *mt = nv50_miptree(pt);
85 struct nv50_surface *s;
86 struct pipe_surface *ps;
87
88 s = CALLOC_STRUCT(nv50_surface);
89 if (!s)
90 return NULL;
91 ps = &s->base;
92
93 ps->refcount = 1;
94 ps->winsys = ws;
95 ps->format = pt->format;
96 ps->width = pt->width[level];
97 ps->height = pt->height[level];
98 ps->block = pt->block;
99 ps->nblocksx = pt->nblocksx[level];
100 ps->nblocksy = pt->nblocksy[level];
101 ps->stride = ps->width * ps->block.size;
102 ps->offset = 0;
103 ps->usage = flags;
104 ps->status = PIPE_SURFACE_STATUS_DEFINED;
105
106 pipe_texture_reference(&ps->texture, pt);
107 pipe_buffer_reference(ws, &ps->buffer, mt->buffer);
108
109 return ps;
110 }
111
112 static void
113 nv50_miptree_surface_del(struct pipe_screen *pscreen,
114 struct pipe_surface **psurface)
115 {
116 struct pipe_winsys *ws = pscreen->winsys;
117 struct pipe_surface *ps = *psurface;
118 struct nv50_surface *s = nv50_surface(ps);
119
120 *psurface = NULL;
121
122 if (--ps->refcount <= 0) {
123 pipe_texture_reference(&ps->texture, NULL);
124 pipe_buffer_reference(ws, &ps->buffer, NULL);
125 FREE(s);
126 }
127 }
128
129 void
130 nv50_screen_init_miptree_functions(struct pipe_screen *pscreen)
131 {
132 pscreen->texture_create = nv50_miptree_create;
133 pscreen->texture_release = nv50_miptree_release;
134 pscreen->get_tex_surface = nv50_miptree_surface_new;
135 pscreen->tex_surface_release = nv50_miptree_surface_del;
136 }
137