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