freedreno: better manage our WFI's
[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_screen.h"
35
36 enum ilo_texture_flags {
37 ILO_TEXTURE_RENDER_WRITE = 1 << 0,
38 ILO_TEXTURE_BLT_WRITE = 1 << 1,
39 ILO_TEXTURE_CPU_WRITE = 1 << 2,
40 ILO_TEXTURE_RENDER_READ = 1 << 3,
41 ILO_TEXTURE_BLT_READ = 1 << 4,
42 ILO_TEXTURE_CPU_READ = 1 << 5,
43 ILO_TEXTURE_CLEAR = 1 << 6,
44 ILO_TEXTURE_HIZ = 1 << 7,
45 };
46
47 struct ilo_buffer {
48 struct pipe_resource base;
49
50 struct intel_bo *bo;
51 unsigned bo_size;
52 unsigned bo_flags;
53 };
54
55 /**
56 * A 3D image slice, cube face, or array layer.
57 */
58 struct ilo_texture_slice {
59 /* 2D offset to the slice */
60 unsigned x, y;
61 unsigned flags;
62 };
63
64 struct ilo_texture {
65 struct pipe_resource base;
66
67 bool imported;
68 unsigned bo_flags;
69
70 enum pipe_format bo_format;
71 struct intel_bo *bo;
72
73 /*
74 * These are the values passed to or returned from winsys for bo
75 * allocation. As such,
76 *
77 * - width and height are in blocks,
78 * - cpp is the block size in bytes, and
79 * - stride is the distance in bytes between two block rows.
80 */
81 int bo_width, bo_height, bo_cpp, bo_stride;
82 enum intel_tiling_mode tiling;
83
84 bool compressed;
85 unsigned block_width;
86 unsigned block_height;
87
88 /* true if the mip level alignments are stricter */
89 bool halign_8, valign_4;
90 /* true if space is reserved between layers */
91 bool array_spacing_full;
92 /* true if samples are interleaved */
93 bool interleaved;
94
95 struct ilo_texture_slice *slices[PIPE_MAX_TEXTURE_LEVELS];
96
97 struct ilo_texture *separate_s8;
98
99 struct {
100 struct intel_bo *bo;
101 int bo_stride;
102 } hiz;
103 };
104
105 static inline struct ilo_buffer *
106 ilo_buffer(struct pipe_resource *res)
107 {
108 return (struct ilo_buffer *)
109 ((res && res->target == PIPE_BUFFER) ? res : NULL);
110 }
111
112 static inline struct ilo_texture *
113 ilo_texture(struct pipe_resource *res)
114 {
115 return (struct ilo_texture *)
116 ((res && res->target != PIPE_BUFFER) ? res : NULL);
117 }
118
119 void
120 ilo_init_resource_functions(struct ilo_screen *is);
121
122 bool
123 ilo_buffer_alloc_bo(struct ilo_buffer *buf);
124
125 bool
126 ilo_texture_alloc_bo(struct ilo_texture *tex);
127
128 static inline struct ilo_texture_slice *
129 ilo_texture_get_slice(const struct ilo_texture *tex,
130 unsigned level, unsigned slice)
131 {
132 assert(level <= tex->base.last_level);
133 assert(slice < ((tex->base.target == PIPE_TEXTURE_3D) ?
134 u_minify(tex->base.depth0, level) : tex->base.array_size));
135
136 return &tex->slices[level][slice];
137 }
138
139 unsigned
140 ilo_texture_get_slice_offset(const struct ilo_texture *tex,
141 unsigned level, unsigned slice,
142 unsigned *x_offset, unsigned *y_offset);
143
144 static inline void
145 ilo_texture_set_slice_flags(struct ilo_texture *tex, unsigned level,
146 unsigned first_slice, unsigned num_slices,
147 unsigned mask, unsigned value)
148 {
149 const struct ilo_texture_slice *last =
150 ilo_texture_get_slice(tex, level, first_slice + num_slices - 1);
151 struct ilo_texture_slice *slice =
152 ilo_texture_get_slice(tex, level, first_slice);
153
154 while (slice <= last) {
155 slice->flags = (slice->flags & ~mask) | (value & mask);
156 slice++;
157 }
158 }
159
160 static inline bool
161 ilo_texture_can_enable_hiz(const struct ilo_texture *tex, unsigned level,
162 unsigned first_slice, unsigned num_slices)
163 {
164 const struct ilo_screen *is = ilo_screen(tex->base.screen);
165 const struct ilo_texture_slice *slice =
166 ilo_texture_get_slice(tex, level, first_slice);
167
168 if (!tex->hiz.bo)
169 return false;
170
171 /* we can adjust 3DSTATE_DEPTH_BUFFER for the first slice */
172 if (level == 0 && first_slice == 0 && num_slices == 1)
173 return true;
174
175 /* HiZ is non-mipmapped and non-array on GEN6 */
176 assert(is->dev.gen > ILO_GEN(6));
177
178 /*
179 * Either all or none of the slices in the same level have ILO_TEXTURE_HIZ
180 * set. It suffices to check only the first slice.
181 */
182 return (slice->flags & ILO_TEXTURE_HIZ);
183 }
184
185 #endif /* ILO_RESOURCE_H */