ilo: use a helper to determine if HiZ is enabled
[mesa.git] / src / gallium / drivers / ilo / ilo_blit.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_BLIT_H
29 #define ILO_BLIT_H
30
31 #include "ilo_common.h"
32 #include "ilo_context.h"
33 #include "ilo_gpe.h"
34 #include "ilo_resource.h"
35
36 struct ilo_context;
37
38 void
39 ilo_blit_resolve_slices_for_hiz(struct ilo_context *ilo,
40 struct pipe_resource *res, unsigned level,
41 unsigned first_slice, unsigned num_slices,
42 unsigned flags);
43
44 static inline void
45 ilo_blit_resolve_slices(struct ilo_context *ilo,
46 struct pipe_resource *res, unsigned level,
47 unsigned first_slice, unsigned num_slices,
48 unsigned flags)
49 {
50 struct ilo_texture *tex;
51 unsigned flag_mask;
52
53 if (res->target == PIPE_BUFFER)
54 return;
55
56 tex = ilo_texture(res);
57
58 /*
59 * This function is called frequently and we need to make it run faster.
60 * As it is only used to resolve HiZ right now, return early when there is
61 * no HiZ.
62 */
63 if (!ilo_texture_can_enable_hiz(tex, level, first_slice, num_slices))
64 return;
65
66 /*
67 * flags may be
68 *
69 * - ILO_TEXTURE_CPU_{READ,WRITE} (transfer)
70 * - ILO_TEXTURE_BLT_{READ,WRITE} (BLT copy or clear)
71 * - ILO_TEXTURE_RENDER_{READ,WRITE} (sample or render)
72 * - ILO_TEXTURE_CLEAR
73 *
74 * It is assumed there is at most one writer, and that readers read before
75 * writers write.
76 */
77 if (ilo_texture_can_enable_hiz(tex, level, first_slice, num_slices)) {
78 ilo_blit_resolve_slices_for_hiz(ilo, res, level,
79 first_slice, num_slices, flags);
80 }
81
82 /* clear writers and clear state that are not set */
83 flag_mask =
84 ILO_TEXTURE_CPU_WRITE |
85 ILO_TEXTURE_BLT_WRITE |
86 ILO_TEXTURE_RENDER_WRITE;
87 if (flags & flag_mask)
88 flag_mask |= ILO_TEXTURE_CLEAR;
89
90 ilo_texture_set_slice_flags(tex, level,
91 first_slice, num_slices, flag_mask, flags);
92 }
93
94 static inline void
95 ilo_blit_resolve_resource(struct ilo_context *ilo,
96 struct pipe_resource *res,
97 unsigned flags)
98 {
99 unsigned lv;
100
101 for (lv = 0; lv <= res->last_level; lv++) {
102 const unsigned num_slices = (res->target == PIPE_TEXTURE_3D) ?
103 u_minify(res->depth0, lv) : res->array_size;
104
105 ilo_blit_resolve_slices(ilo, res, lv, 0, num_slices, flags);
106 }
107 }
108
109 static inline void
110 ilo_blit_resolve_surface(struct ilo_context *ilo,
111 struct pipe_surface *surf,
112 unsigned flags)
113 {
114 if (surf->texture->target == PIPE_BUFFER)
115 return;
116
117 ilo_blit_resolve_slices(ilo, surf->texture,
118 surf->u.tex.level, surf->u.tex.first_layer,
119 surf->u.tex.last_layer - surf->u.tex.first_layer + 1,
120 flags);
121 }
122
123 static inline void
124 ilo_blit_resolve_transfer(struct ilo_context *ilo,
125 const struct pipe_transfer *xfer)
126 {
127 unsigned flags = 0;
128
129 if (xfer->resource->target == PIPE_BUFFER)
130 return;
131
132 if (xfer->usage & PIPE_TRANSFER_READ)
133 flags |= ILO_TEXTURE_CPU_READ;
134 if (xfer->usage & PIPE_TRANSFER_WRITE)
135 flags |= ILO_TEXTURE_CPU_WRITE;
136
137 ilo_blit_resolve_slices(ilo, xfer->resource, xfer->level,
138 xfer->box.z, xfer->box.depth, flags);
139 }
140
141 static inline void
142 ilo_blit_resolve_view(struct ilo_context *ilo,
143 const struct pipe_sampler_view *view)
144 {
145 const unsigned flags = ILO_TEXTURE_RENDER_READ;
146 unsigned lv;
147
148 if (view->texture->target == PIPE_BUFFER)
149 return;
150
151 for (lv = view->u.tex.first_level; lv <= view->u.tex.last_level; lv++) {
152 unsigned first_slice, num_slices;
153
154 if (view->texture->target == PIPE_TEXTURE_3D) {
155 first_slice = 0;
156 num_slices = u_minify(view->texture->depth0, lv);
157 }
158 else {
159 first_slice = view->u.tex.first_layer;
160 num_slices = view->u.tex.last_layer - view->u.tex.first_layer + 1;
161 }
162
163 ilo_blit_resolve_slices(ilo, view->texture,
164 lv, first_slice, num_slices, flags);
165 }
166 }
167
168 static inline void
169 ilo_blit_resolve_framebuffer(struct ilo_context *ilo)
170 {
171 const struct pipe_framebuffer_state *fb = &ilo->fb.state;
172 unsigned sh, i;
173
174 /* Not all bound views are sampled by the shaders. How do we tell? */
175 for (sh = 0; sh < Elements(ilo->view); sh++) {
176 for (i = 0; i < ilo->view[sh].count; i++) {
177 if (ilo->view[sh].states[i])
178 ilo_blit_resolve_view(ilo, ilo->view[sh].states[i]);
179 }
180 }
181
182 for (i = 0; i < fb->nr_cbufs; i++)
183 ilo_blit_resolve_surface(ilo, fb->cbufs[i], ILO_TEXTURE_RENDER_WRITE);
184
185 if (fb->zsbuf)
186 ilo_blit_resolve_surface(ilo, fb->zsbuf, ILO_TEXTURE_RENDER_WRITE);
187 }
188
189 void
190 ilo_init_blit_functions(struct ilo_context *ilo);
191
192 #endif /* ILO_BLIT_H */