svga: add helpers for tracking rendering to textures
[mesa.git] / src / gallium / drivers / svga / svga_resource_texture.h
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #ifndef SVGA_TEXTURE_H
27 #define SVGA_TEXTURE_H
28
29
30 #include "pipe/p_compiler.h"
31 #include "pipe/p_state.h"
32 #include "util/u_inlines.h"
33 #include "util/u_memory.h"
34 #include "util/u_transfer.h"
35 #include "svga_screen_cache.h"
36
37 struct pipe_context;
38 struct pipe_screen;
39 struct svga_context;
40 struct svga_winsys_surface;
41 enum SVGA3dSurfaceFormat;
42
43
44 #define SVGA_MAX_TEXTURE_LEVELS 16
45
46
47 extern struct u_resource_vtbl svga_texture_vtbl;
48
49
50 struct svga_texture
51 {
52 struct u_resource b;
53
54 boolean defined[6][SVGA_MAX_TEXTURE_LEVELS];
55
56 struct svga_sampler_view *cached_view;
57
58 unsigned view_age[SVGA_MAX_TEXTURE_LEVELS];
59 unsigned age;
60
61 boolean views_modified;
62
63 /**
64 * Creation key for the host surface handle.
65 *
66 * This structure describes all the host surface characteristics so that it
67 * can be looked up in cache, since creating a host surface is often a slow
68 * operation.
69 */
70 struct svga_host_surface_cache_key key;
71
72 /**
73 * Handle for the host side surface.
74 *
75 * This handle is owned by this texture. Views should hold on to a reference
76 * to this texture and never destroy this handle directly.
77 */
78 struct svga_winsys_surface *handle;
79
80 unsigned size; /**< Approximate size in bytes */
81
82 /** array indexed by cube face or 3D/array slice, one bit per mipmap level */
83 ushort *rendered_to;
84 };
85
86
87
88 /* Note this is only used for texture (not buffer) transfers:
89 */
90 struct svga_transfer
91 {
92 struct pipe_transfer base;
93
94 unsigned face;
95
96 struct svga_winsys_buffer *hwbuf;
97
98 /* Height of the hardware buffer in pixel blocks */
99 unsigned hw_nblocksy;
100
101 /* Temporary malloc buffer when we can't allocate a hardware buffer
102 * big enough */
103 void *swbuf;
104 };
105
106
107 static INLINE struct svga_texture *svga_texture( struct pipe_resource *resource )
108 {
109 struct svga_texture *tex = (struct svga_texture *)resource;
110 assert(tex == NULL || tex->b.vtbl == &svga_texture_vtbl);
111 return tex;
112 }
113
114
115 static INLINE struct svga_transfer *
116 svga_transfer(struct pipe_transfer *transfer)
117 {
118 assert(transfer);
119 return (struct svga_transfer *)transfer;
120 }
121
122
123 /**
124 * Increment the age of a view into a texture
125 * This is used to track updates to textures when we draw into
126 * them via a surface.
127 */
128 static INLINE void
129 svga_age_texture_view(struct svga_texture *tex, unsigned level)
130 {
131 assert(level < Elements(tex->view_age));
132 tex->view_age[level] = ++(tex->age);
133 }
134
135
136 /**
137 * Mark the given texture face/level as being defined.
138 */
139 static INLINE void
140 svga_define_texture_level(struct svga_texture *tex,
141 unsigned face,unsigned level)
142 {
143 assert(face < Elements(tex->defined));
144 assert(level < Elements(tex->defined[0]));
145 tex->defined[face][level] = TRUE;
146 }
147
148
149 static INLINE bool
150 svga_is_texture_level_defined(const struct svga_texture *tex,
151 unsigned face, unsigned level)
152 {
153 assert(face < Elements(tex->defined));
154 assert(level < Elements(tex->defined[0]));
155 return tex->defined[face][level];
156 }
157
158
159 /** For debugging, check that face and level are legal */
160 static inline void
161 check_face_level(const struct svga_texture *tex,
162 unsigned face, unsigned level)
163 {
164 if (tex->b.b.target == PIPE_TEXTURE_CUBE) {
165 assert(face < 6);
166 }
167 else if (tex->b.b.target == PIPE_TEXTURE_3D) {
168 assert(face < tex->b.b.depth0);
169 }
170 else {
171 assert(face < tex->b.b.array_size);
172 }
173
174 assert(level < 8 * sizeof(tex->rendered_to[0]));
175 }
176
177
178 static INLINE void
179 svga_set_texture_rendered_to(struct svga_texture *tex,
180 unsigned face, unsigned level)
181 {
182 check_face_level(tex, face, level);
183 tex->rendered_to[face] |= 1 << level;
184 }
185
186
187 static INLINE void
188 svga_clear_texture_rendered_to(struct svga_texture *tex,
189 unsigned face, unsigned level)
190 {
191 check_face_level(tex, face, level);
192 tex->rendered_to[face] &= ~(1 << level);
193 }
194
195
196 static INLINE boolean
197 svga_was_texture_rendered_to(const struct svga_texture *tex,
198 unsigned face, unsigned level)
199 {
200 check_face_level(tex, face, level);
201 return !!(tex->rendered_to[face] & (1 << level));
202 }
203
204
205 struct pipe_resource *
206 svga_texture_create(struct pipe_screen *screen,
207 const struct pipe_resource *template);
208
209 struct pipe_resource *
210 svga_texture_from_handle(struct pipe_screen * screen,
211 const struct pipe_resource *template,
212 struct winsys_handle *whandle);
213
214
215
216
217 #endif /* SVGA_TEXTURE_H */