f421ad5b9380f450e89a8a4565050972dbf399b2
[mesa.git] / src / gallium / auxiliary / util / u_blitter.h
1 /**************************************************************************
2 *
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 #ifndef U_BLITTER_H
28 #define U_BLITTER_H
29
30 #include "util/u_inlines.h"
31 #include "util/u_memory.h"
32
33 #include "pipe/p_state.h"
34
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 struct pipe_context;
41
42 struct blitter_context
43 {
44 /* Private members, really. */
45 void *saved_blend_state; /**< blend state */
46 void *saved_dsa_state; /**< depth stencil alpha state */
47 void *saved_velem_state; /**< vertex elements state */
48 void *saved_rs_state; /**< rasterizer state */
49 void *saved_fs, *saved_vs; /**< fragment shader, vertex shader */
50
51 struct pipe_framebuffer_state saved_fb_state; /**< framebuffer state */
52 struct pipe_stencil_ref saved_stencil_ref; /**< stencil ref */
53 struct pipe_viewport_state saved_viewport;
54 struct pipe_clip_state saved_clip;
55
56 int saved_num_sampler_states;
57 void *saved_sampler_states[PIPE_MAX_SAMPLERS];
58
59 int saved_num_sampler_views;
60 struct pipe_sampler_view *saved_sampler_views[PIPE_MAX_SAMPLERS];
61
62 int saved_num_vertex_buffers;
63 struct pipe_vertex_buffer saved_vertex_buffers[PIPE_MAX_ATTRIBS];
64 };
65
66 /**
67 * Create a blitter context.
68 */
69 struct blitter_context *util_blitter_create(struct pipe_context *pipe);
70
71 /**
72 * Destroy a blitter context.
73 */
74 void util_blitter_destroy(struct blitter_context *blitter);
75
76 /*
77 * These CSOs must be saved before any of the following functions is called:
78 * - blend state
79 * - depth stencil alpha state
80 * - rasterizer state
81 * - vertex shader
82 * - fragment shader
83 */
84
85 /**
86 * Clear a specified set of currently bound buffers to specified values.
87 */
88 void util_blitter_clear(struct blitter_context *blitter,
89 unsigned width, unsigned height,
90 unsigned num_cbufs,
91 unsigned clear_buffers,
92 const float *rgba,
93 double depth, unsigned stencil);
94
95 /**
96 * Copy a block of pixels from one surface to another.
97 *
98 * You can copy from any color format to any other color format provided
99 * the former can be sampled and the latter can be rendered to. Otherwise,
100 * a software fallback path is taken and both surfaces must be of the same
101 * format.
102 *
103 * The same holds for depth-stencil formats with the exception that stencil
104 * cannot be copied unless you set ignore_stencil to FALSE. In that case,
105 * a software fallback path is taken and both surfaces must be of the same
106 * format.
107 *
108 * Use pipe_screen->is_format_supported to know your options.
109 *
110 * These states must be saved in the blitter in addition to the state objects
111 * already required to be saved:
112 * - framebuffer state
113 * - fragment sampler states
114 * - fragment sampler textures
115 */
116 void util_blitter_copy_region(struct blitter_context *blitter,
117 struct pipe_resource *dst,
118 struct pipe_subresource subdst,
119 unsigned dstx, unsigned dsty, unsigned dstz,
120 struct pipe_resource *src,
121 struct pipe_subresource subsrc,
122 unsigned srcx, unsigned srcy, unsigned srcz,
123 unsigned width, unsigned height,
124 boolean ignore_stencil);
125
126 /**
127 * Clear a region of a (color) surface to a constant value.
128 *
129 * These states must be saved in the blitter in addition to the state objects
130 * already required to be saved:
131 * - framebuffer state
132 */
133 void util_blitter_clear_render_target(struct blitter_context *blitter,
134 struct pipe_surface *dst,
135 const float *rgba,
136 unsigned dstx, unsigned dsty,
137 unsigned width, unsigned height);
138
139 /**
140 * Clear a region of a depth-stencil surface, both stencil and depth
141 * or only one of them if this is a combined depth-stencil surface.
142 *
143 * These states must be saved in the blitter in addition to the state objects
144 * already required to be saved:
145 * - framebuffer state
146 */
147 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
148 struct pipe_surface *dst,
149 unsigned clear_flags,
150 double depth,
151 unsigned stencil,
152 unsigned dstx, unsigned dsty,
153 unsigned width, unsigned height);
154
155 /* The functions below should be used to save currently bound constant state
156 * objects inside a driver. The objects are automatically restored at the end
157 * of the util_blitter_{clear, copy_region, fill_region} functions and then
158 * forgotten.
159 *
160 * CSOs not listed here are not affected by util_blitter. */
161
162 static INLINE
163 void util_blitter_save_blend(struct blitter_context *blitter,
164 void *state)
165 {
166 blitter->saved_blend_state = state;
167 }
168
169 static INLINE
170 void util_blitter_save_depth_stencil_alpha(struct blitter_context *blitter,
171 void *state)
172 {
173 blitter->saved_dsa_state = state;
174 }
175
176 static INLINE
177 void util_blitter_save_vertex_elements(struct blitter_context *blitter,
178 void *state)
179 {
180 blitter->saved_velem_state = state;
181 }
182
183 static INLINE
184 void util_blitter_save_stencil_ref(struct blitter_context *blitter,
185 const struct pipe_stencil_ref *state)
186 {
187 blitter->saved_stencil_ref = *state;
188 }
189
190 static INLINE
191 void util_blitter_save_rasterizer(struct blitter_context *blitter,
192 void *state)
193 {
194 blitter->saved_rs_state = state;
195 }
196
197 static INLINE
198 void util_blitter_save_fragment_shader(struct blitter_context *blitter,
199 void *fs)
200 {
201 blitter->saved_fs = fs;
202 }
203
204 static INLINE
205 void util_blitter_save_vertex_shader(struct blitter_context *blitter,
206 void *vs)
207 {
208 blitter->saved_vs = vs;
209 }
210
211 /* XXX This should probably be moved elsewhere. */
212 static INLINE
213 void util_assign_framebuffer_state(struct pipe_framebuffer_state *dst,
214 const struct pipe_framebuffer_state *src)
215 {
216 unsigned i;
217
218 if (src) {
219 /* Reference all surfaces. */
220 for (i = 0; i < src->nr_cbufs; i++) {
221 pipe_surface_reference(&dst->cbufs[i], src->cbufs[i]);
222 }
223 for (; i < dst->nr_cbufs; i++) {
224 pipe_surface_reference(&dst->cbufs[i], NULL);
225 }
226
227 pipe_surface_reference(&dst->zsbuf, src->zsbuf);
228
229 dst->nr_cbufs = src->nr_cbufs;
230 dst->width = src->width;
231 dst->height = src->height;
232 } else {
233 /* Set all surfaces to NULL. */
234 for (i = 0; i < dst->nr_cbufs; i++) {
235 pipe_surface_reference(&dst->cbufs[i], NULL);
236 }
237
238 pipe_surface_reference(&dst->zsbuf, NULL);
239
240 dst->nr_cbufs = 0;
241 }
242 }
243
244 static INLINE
245 void util_blitter_save_framebuffer(struct blitter_context *blitter,
246 const struct pipe_framebuffer_state *state)
247 {
248 blitter->saved_fb_state.nr_cbufs = 0; /* It's ~0 now, meaning it's unsaved. */
249 util_assign_framebuffer_state(&blitter->saved_fb_state, state);
250 }
251
252 static INLINE
253 void util_blitter_save_viewport(struct blitter_context *blitter,
254 struct pipe_viewport_state *state)
255 {
256 blitter->saved_viewport = *state;
257 }
258
259 static INLINE
260 void util_blitter_save_clip(struct blitter_context *blitter,
261 struct pipe_clip_state *state)
262 {
263 blitter->saved_clip = *state;
264 }
265
266 static INLINE
267 void util_blitter_save_fragment_sampler_states(
268 struct blitter_context *blitter,
269 int num_sampler_states,
270 void **sampler_states)
271 {
272 assert(num_sampler_states <= Elements(blitter->saved_sampler_states));
273
274 blitter->saved_num_sampler_states = num_sampler_states;
275 memcpy(blitter->saved_sampler_states, sampler_states,
276 num_sampler_states * sizeof(void *));
277 }
278
279 static INLINE void
280 util_blitter_save_fragment_sampler_views(struct blitter_context *blitter,
281 int num_views,
282 struct pipe_sampler_view **views)
283 {
284 unsigned i;
285 assert(num_views <= Elements(blitter->saved_sampler_views));
286
287 blitter->saved_num_sampler_views = num_views;
288 for (i = 0; i < num_views; i++)
289 pipe_sampler_view_reference(&blitter->saved_sampler_views[i],
290 views[i]);
291 }
292
293 static INLINE void
294 util_blitter_save_vertex_buffers(struct blitter_context *blitter,
295 int num_vertex_buffers,
296 struct pipe_vertex_buffer *vertex_buffers)
297 {
298 unsigned i;
299 assert(num_vertex_buffers <= Elements(blitter->saved_vertex_buffers));
300
301 blitter->saved_num_vertex_buffers = num_vertex_buffers;
302
303 for (i = 0; i < num_vertex_buffers; i++) {
304 if (vertex_buffers[i].buffer) {
305 pipe_resource_reference(&blitter->saved_vertex_buffers[i].buffer,
306 vertex_buffers[i].buffer);
307 }
308 }
309
310 memcpy(blitter->saved_vertex_buffers,
311 vertex_buffers,
312 num_vertex_buffers * sizeof(struct pipe_vertex_buffer));
313 }
314
315 #ifdef __cplusplus
316 }
317 #endif
318
319 #endif