ilo: plug a potential index buffer leak
[mesa.git] / src / gallium / drivers / ilo / ilo_blitter_pipe.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 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 #include "util/u_blitter.h"
29 #include "util/u_surface.h"
30
31 #include "ilo_3d.h"
32 #include "ilo_context.h"
33 #include "ilo_blitter.h"
34
35 enum ilo_blitter_pipe_op {
36 ILO_BLITTER_PIPE_BLIT,
37 ILO_BLITTER_PIPE_COPY,
38 ILO_BLITTER_PIPE_CLEAR,
39 ILO_BLITTER_PIPE_CLEAR_FB,
40 };
41
42 static void
43 ilo_blitter_pipe_begin(struct ilo_blitter *blitter,
44 enum ilo_blitter_pipe_op op)
45 {
46 struct blitter_context *b = blitter->pipe_blitter;
47 struct ilo_context *ilo = blitter->ilo;
48
49 /* as documented in util/u_blitter.h */
50 util_blitter_save_vertex_buffer_slot(b, ilo->vb.states);
51 util_blitter_save_vertex_elements(b, (void *) ilo->ve);
52 util_blitter_save_vertex_shader(b, ilo->vs);
53 util_blitter_save_geometry_shader(b, ilo->gs);
54 util_blitter_save_so_targets(b, ilo->so.count, ilo->so.states);
55
56 util_blitter_save_fragment_shader(b, ilo->fs);
57 util_blitter_save_depth_stencil_alpha(b, (void *) ilo->dsa);
58 util_blitter_save_blend(b, (void *) ilo->blend);
59
60 /* undocumented? */
61 util_blitter_save_viewport(b, &ilo->viewport.viewport0);
62 util_blitter_save_stencil_ref(b, &ilo->stencil_ref);
63 util_blitter_save_sample_mask(b, ilo->sample_mask);
64
65 switch (op) {
66 case ILO_BLITTER_PIPE_BLIT:
67 case ILO_BLITTER_PIPE_COPY:
68 util_blitter_save_rasterizer(b, (void *) ilo->rasterizer);
69 util_blitter_save_framebuffer(b, &ilo->fb.state);
70
71 util_blitter_save_render_condition(b,
72 ilo->hw3d->render_condition.query,
73 ilo->hw3d->render_condition.cond,
74 ilo->hw3d->render_condition.mode);
75
76 util_blitter_save_fragment_sampler_states(b,
77 ilo->sampler[PIPE_SHADER_FRAGMENT].count,
78 (void **) ilo->sampler[PIPE_SHADER_FRAGMENT].cso);
79
80 util_blitter_save_fragment_sampler_views(b,
81 ilo->view[PIPE_SHADER_FRAGMENT].count,
82 ilo->view[PIPE_SHADER_FRAGMENT].states);
83
84 /* disable render condition? */
85 break;
86 case ILO_BLITTER_PIPE_CLEAR:
87 util_blitter_save_rasterizer(b, (void *) ilo->rasterizer);
88 util_blitter_save_framebuffer(b, &ilo->fb.state);
89
90 /* disable render condition? */
91 break;
92 case ILO_BLITTER_PIPE_CLEAR_FB:
93 util_blitter_save_rasterizer(b, (void *) ilo->rasterizer);
94 break;
95 default:
96 break;
97 }
98 }
99
100 static void
101 ilo_blitter_pipe_end(struct ilo_blitter *blitter)
102 {
103 }
104
105 bool
106 ilo_blitter_pipe_blit(struct ilo_blitter *blitter,
107 const struct pipe_blit_info *info)
108 {
109 struct blitter_context *b = blitter->pipe_blitter;
110 struct pipe_blit_info skip_stencil;
111
112 if (util_try_blit_via_copy_region(&blitter->ilo->base, info))
113 return true;
114
115 if (!util_blitter_is_blit_supported(b, info)) {
116 /* try without stencil */
117 if (info->mask & PIPE_MASK_S) {
118 skip_stencil = *info;
119 skip_stencil.mask = info->mask & ~PIPE_MASK_S;
120
121 if (util_blitter_is_blit_supported(blitter->pipe_blitter,
122 &skip_stencil)) {
123 ilo_warn("ignore stencil buffer blitting\n");
124 info = &skip_stencil;
125 }
126 }
127
128 if (info != &skip_stencil) {
129 ilo_warn("failed to blit with pipe blitter\n");
130 return false;
131 }
132 }
133
134 ilo_blitter_pipe_begin(blitter, ILO_BLITTER_PIPE_BLIT);
135 util_blitter_blit(b, info);
136 ilo_blitter_pipe_end(blitter);
137
138 return true;
139 }
140
141 bool
142 ilo_blitter_pipe_copy_resource(struct ilo_blitter *blitter,
143 struct pipe_resource *dst, unsigned dst_level,
144 unsigned dst_x, unsigned dst_y, unsigned dst_z,
145 struct pipe_resource *src, unsigned src_level,
146 const struct pipe_box *src_box)
147 {
148 const unsigned mask = PIPE_MASK_RGBAZS;
149 const bool copy_all_samples = true;
150
151 /* not until we allow rendertargets to be buffers */
152 if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER)
153 return false;
154
155 if (!util_blitter_is_copy_supported(blitter->pipe_blitter, dst, src, mask))
156 return false;
157
158 ilo_blitter_pipe_begin(blitter, ILO_BLITTER_PIPE_COPY);
159
160 util_blitter_copy_texture(blitter->pipe_blitter,
161 dst, dst_level, dst_x, dst_y, dst_z,
162 src, src_level, src_box,
163 mask, copy_all_samples);
164
165 ilo_blitter_pipe_end(blitter);
166
167 return true;
168 }
169
170 bool
171 ilo_blitter_pipe_clear_rt(struct ilo_blitter *blitter,
172 struct pipe_surface *rt,
173 const union pipe_color_union *color,
174 unsigned x, unsigned y,
175 unsigned width, unsigned height)
176 {
177 ilo_blitter_pipe_begin(blitter, ILO_BLITTER_PIPE_CLEAR);
178
179 util_blitter_clear_render_target(blitter->pipe_blitter,
180 rt, color, x, y, width, height);
181
182 ilo_blitter_pipe_end(blitter);
183
184 return true;
185 }
186
187 bool
188 ilo_blitter_pipe_clear_zs(struct ilo_blitter *blitter,
189 struct pipe_surface *zs,
190 unsigned clear_flags,
191 double depth, unsigned stencil,
192 unsigned x, unsigned y,
193 unsigned width, unsigned height)
194 {
195 ilo_blitter_pipe_begin(blitter, ILO_BLITTER_PIPE_CLEAR);
196
197 util_blitter_clear_depth_stencil(blitter->pipe_blitter,
198 zs, clear_flags, depth, stencil, x, y, width, height);
199
200 ilo_blitter_pipe_end(blitter);
201
202 return true;
203 }
204
205 bool
206 ilo_blitter_pipe_clear_fb(struct ilo_blitter *blitter,
207 unsigned buffers,
208 const union pipe_color_union *color,
209 double depth, unsigned stencil)
210 {
211 /* TODO we should pause/resume some queries */
212 ilo_blitter_pipe_begin(blitter, ILO_BLITTER_PIPE_CLEAR_FB);
213
214 util_blitter_clear(blitter->pipe_blitter,
215 blitter->ilo->fb.state.width, blitter->ilo->fb.state.height,
216 buffers, color, depth, stencil);
217
218 ilo_blitter_pipe_end(blitter);
219
220 return true;
221 }