ilo: migrate to ilo_layout
[mesa.git] / src / gallium / drivers / ilo / ilo_resource.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2012-2013 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #ifndef ILO_RESOURCE_H
29 #define ILO_RESOURCE_H
30
31 #include "intel_winsys.h"
32
33 #include "ilo_common.h"
34 #include "ilo_layout.h"
35 #include "ilo_screen.h"
36
37 enum ilo_texture_flags {
38 /*
39 * Possible writers of a texture. There can be at most one writer at any
40 * time.
41 *
42 * Wine set in resolve flags (in ilo_blit_resolve_slices()), they indicate
43 * the new writer. When set in slice flags (ilo_texture_slice::flags),
44 * they indicate the writer since last resolve.
45 */
46 ILO_TEXTURE_RENDER_WRITE = 1 << 0,
47 ILO_TEXTURE_BLT_WRITE = 1 << 1,
48 ILO_TEXTURE_CPU_WRITE = 1 << 2,
49
50 /*
51 * Possible readers of a texture. There may be multiple readers at any
52 * time.
53 *
54 * When set in resolve flags, they indicate the new readers. They are
55 * never set in slice flags.
56 */
57 ILO_TEXTURE_RENDER_READ = 1 << 3,
58 ILO_TEXTURE_BLT_READ = 1 << 4,
59 ILO_TEXTURE_CPU_READ = 1 << 5,
60
61 /*
62 * Set when the texture is cleared.
63 *
64 * When set in resolve flags, the new writer will clear. When set in slice
65 * flags, the slice has been cleared to ilo_texture_slice::clear_value.
66 */
67 ILO_TEXTURE_CLEAR = 1 << 6,
68
69 /*
70 * Set when HiZ can be enabled.
71 *
72 * It is never set in resolve flags. When set in slice flags, the slice
73 * can have HiZ enabled. It is to be noted that this bit is always set for
74 * either all or none of the slices in a level, allowing quick check in
75 * case of layered rendering.
76 */
77 ILO_TEXTURE_HIZ = 1 << 7,
78 };
79
80 struct ilo_buffer {
81 struct pipe_resource base;
82
83 struct intel_bo *bo;
84 unsigned bo_size;
85 };
86
87 /**
88 * A 3D image slice, cube face, or array layer.
89 */
90 struct ilo_texture_slice {
91 unsigned flags;
92
93 /*
94 * Slice clear value. It is served for two purposes
95 *
96 * - the clear value used in commands such as 3DSTATE_CLEAR_PARAMS
97 * - the clear value when ILO_TEXTURE_CLEAR is set
98 *
99 * Since commands such as 3DSTATE_CLEAR_PARAMS expect a single clear value
100 * for all slices, ilo_blit_resolve_slices() will silently make all slices
101 * to have the same clear value.
102 */
103 uint32_t clear_value;
104 };
105
106 struct ilo_texture {
107 struct pipe_resource base;
108
109 bool imported;
110
111 struct ilo_layout layout;
112
113 /* XXX thread-safety */
114 struct intel_bo *bo;
115 struct ilo_texture_slice *slices[PIPE_MAX_TEXTURE_LEVELS];
116
117 struct intel_bo *aux_bo;
118
119 struct ilo_texture *separate_s8;
120 };
121
122 static inline struct ilo_buffer *
123 ilo_buffer(struct pipe_resource *res)
124 {
125 return (struct ilo_buffer *)
126 ((res && res->target == PIPE_BUFFER) ? res : NULL);
127 }
128
129 static inline struct ilo_texture *
130 ilo_texture(struct pipe_resource *res)
131 {
132 return (struct ilo_texture *)
133 ((res && res->target != PIPE_BUFFER) ? res : NULL);
134 }
135
136 void
137 ilo_init_resource_functions(struct ilo_screen *is);
138
139 bool
140 ilo_buffer_rename_bo(struct ilo_buffer *buf);
141
142 bool
143 ilo_texture_rename_bo(struct ilo_texture *tex);
144
145 /**
146 * Return the bo of the resource.
147 */
148 static inline struct intel_bo *
149 ilo_resource_get_bo(struct pipe_resource *res)
150 {
151 return (res->target == PIPE_BUFFER) ?
152 ilo_buffer(res)->bo : ilo_texture(res)->bo;
153 }
154
155 static inline struct ilo_texture_slice *
156 ilo_texture_get_slice(const struct ilo_texture *tex,
157 unsigned level, unsigned slice)
158 {
159 assert(level <= tex->base.last_level);
160 assert(slice < ((tex->base.target == PIPE_TEXTURE_3D) ?
161 u_minify(tex->base.depth0, level) : tex->base.array_size));
162
163 return &tex->slices[level][slice];
164 }
165
166 static inline void
167 ilo_texture_set_slice_flags(struct ilo_texture *tex, unsigned level,
168 unsigned first_slice, unsigned num_slices,
169 unsigned mask, unsigned value)
170 {
171 const struct ilo_texture_slice *last =
172 ilo_texture_get_slice(tex, level, first_slice + num_slices - 1);
173 struct ilo_texture_slice *slice =
174 ilo_texture_get_slice(tex, level, first_slice);
175
176 while (slice <= last) {
177 slice->flags = (slice->flags & ~mask) | (value & mask);
178 slice++;
179 }
180 }
181
182 static inline void
183 ilo_texture_set_slice_clear_value(struct ilo_texture *tex, unsigned level,
184 unsigned first_slice, unsigned num_slices,
185 uint32_t clear_value)
186 {
187 const struct ilo_texture_slice *last =
188 ilo_texture_get_slice(tex, level, first_slice + num_slices - 1);
189 struct ilo_texture_slice *slice =
190 ilo_texture_get_slice(tex, level, first_slice);
191
192 while (slice <= last) {
193 slice->clear_value = clear_value;
194 slice++;
195 }
196 }
197
198 static inline bool
199 ilo_texture_can_enable_hiz(const struct ilo_texture *tex, unsigned level,
200 unsigned first_slice, unsigned num_slices)
201 {
202 /*
203 * Either all or none of the slices in the same level have ILO_TEXTURE_HIZ
204 * set. It suffices to check only the first slice.
205 */
206 const struct ilo_texture_slice *slice =
207 ilo_texture_get_slice(tex, level, 0);
208
209 return (tex->aux_bo && (slice->flags & ILO_TEXTURE_HIZ));
210 }
211
212 #endif /* ILO_RESOURCE_H */