u_blitter: rename blitter->base, add a way to get a pipe context from blitter
[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 struct pipe_context *pipe; /**< pipe context */
46
47 void *saved_blend_state; /**< blend state */
48 void *saved_dsa_state; /**< depth stencil alpha state */
49 void *saved_velem_state; /**< vertex elements state */
50 void *saved_rs_state; /**< rasterizer state */
51 void *saved_fs, *saved_vs; /**< fragment shader, vertex shader */
52
53 struct pipe_framebuffer_state saved_fb_state; /**< framebuffer state */
54 struct pipe_stencil_ref saved_stencil_ref; /**< stencil ref */
55 struct pipe_viewport_state saved_viewport;
56 struct pipe_clip_state saved_clip;
57
58 int saved_num_sampler_states;
59 void *saved_sampler_states[PIPE_MAX_SAMPLERS];
60
61 int saved_num_sampler_views;
62 struct pipe_sampler_view *saved_sampler_views[PIPE_MAX_SAMPLERS];
63
64 int saved_num_vertex_buffers;
65 struct pipe_vertex_buffer saved_vertex_buffers[PIPE_MAX_ATTRIBS];
66 };
67
68 /**
69 * Create a blitter context.
70 */
71 struct blitter_context *util_blitter_create(struct pipe_context *pipe);
72
73 /**
74 * Destroy a blitter context.
75 */
76 void util_blitter_destroy(struct blitter_context *blitter);
77
78 /**
79 * Return the pipe context associated with a blitter context.
80 */
81 static INLINE
82 struct pipe_context *util_blitter_get_pipe(struct blitter_context *blitter)
83 {
84 return blitter->pipe;
85 }
86
87 /*
88 * These CSOs must be saved before any of the following functions is called:
89 * - blend state
90 * - depth stencil alpha state
91 * - rasterizer state
92 * - vertex shader
93 * - fragment shader
94 */
95
96 /**
97 * Clear a specified set of currently bound buffers to specified values.
98 */
99 void util_blitter_clear(struct blitter_context *blitter,
100 unsigned width, unsigned height,
101 unsigned num_cbufs,
102 unsigned clear_buffers,
103 const float *rgba,
104 double depth, unsigned stencil);
105
106 /**
107 * Copy a block of pixels from one surface to another.
108 *
109 * You can copy from any color format to any other color format provided
110 * the former can be sampled and the latter can be rendered to. Otherwise,
111 * a software fallback path is taken and both surfaces must be of the same
112 * format.
113 *
114 * The same holds for depth-stencil formats with the exception that stencil
115 * cannot be copied unless you set ignore_stencil to FALSE. In that case,
116 * a software fallback path is taken and both surfaces must be of the same
117 * format.
118 *
119 * Use pipe_screen->is_format_supported to know your options.
120 *
121 * These states must be saved in the blitter in addition to the state objects
122 * already required to be saved:
123 * - framebuffer state
124 * - fragment sampler states
125 * - fragment sampler textures
126 */
127 void util_blitter_copy_region(struct blitter_context *blitter,
128 struct pipe_resource *dst,
129 struct pipe_subresource subdst,
130 unsigned dstx, unsigned dsty, unsigned dstz,
131 struct pipe_resource *src,
132 struct pipe_subresource subsrc,
133 unsigned srcx, unsigned srcy, unsigned srcz,
134 unsigned width, unsigned height,
135 boolean ignore_stencil);
136
137 /**
138 * Clear a region of a (color) surface to a constant value.
139 *
140 * These states must be saved in the blitter in addition to the state objects
141 * already required to be saved:
142 * - framebuffer state
143 */
144 void util_blitter_clear_render_target(struct blitter_context *blitter,
145 struct pipe_surface *dst,
146 const float *rgba,
147 unsigned dstx, unsigned dsty,
148 unsigned width, unsigned height);
149
150 /**
151 * Clear a region of a depth-stencil surface, both stencil and depth
152 * or only one of them if this is a combined depth-stencil surface.
153 *
154 * These states must be saved in the blitter in addition to the state objects
155 * already required to be saved:
156 * - framebuffer state
157 */
158 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
159 struct pipe_surface *dst,
160 unsigned clear_flags,
161 double depth,
162 unsigned stencil,
163 unsigned dstx, unsigned dsty,
164 unsigned width, unsigned height);
165
166 /* The functions below should be used to save currently bound constant state
167 * objects inside a driver. The objects are automatically restored at the end
168 * of the util_blitter_{clear, copy_region, fill_region} functions and then
169 * forgotten.
170 *
171 * CSOs not listed here are not affected by util_blitter. */
172
173 static INLINE
174 void util_blitter_save_blend(struct blitter_context *blitter,
175 void *state)
176 {
177 blitter->saved_blend_state = state;
178 }
179
180 static INLINE
181 void util_blitter_save_depth_stencil_alpha(struct blitter_context *blitter,
182 void *state)
183 {
184 blitter->saved_dsa_state = state;
185 }
186
187 static INLINE
188 void util_blitter_save_vertex_elements(struct blitter_context *blitter,
189 void *state)
190 {
191 blitter->saved_velem_state = state;
192 }
193
194 static INLINE
195 void util_blitter_save_stencil_ref(struct blitter_context *blitter,
196 const struct pipe_stencil_ref *state)
197 {
198 blitter->saved_stencil_ref = *state;
199 }
200
201 static INLINE
202 void util_blitter_save_rasterizer(struct blitter_context *blitter,
203 void *state)
204 {
205 blitter->saved_rs_state = state;
206 }
207
208 static INLINE
209 void util_blitter_save_fragment_shader(struct blitter_context *blitter,
210 void *fs)
211 {
212 blitter->saved_fs = fs;
213 }
214
215 static INLINE
216 void util_blitter_save_vertex_shader(struct blitter_context *blitter,
217 void *vs)
218 {
219 blitter->saved_vs = vs;
220 }
221
222 /* XXX This should probably be moved elsewhere. */
223 static INLINE
224 void util_assign_framebuffer_state(struct pipe_framebuffer_state *dst,
225 const struct pipe_framebuffer_state *src)
226 {
227 unsigned i;
228
229 if (src) {
230 /* Reference all surfaces. */
231 for (i = 0; i < src->nr_cbufs; i++) {
232 pipe_surface_reference(&dst->cbufs[i], src->cbufs[i]);
233 }
234 for (; i < dst->nr_cbufs; i++) {
235 pipe_surface_reference(&dst->cbufs[i], NULL);
236 }
237
238 pipe_surface_reference(&dst->zsbuf, src->zsbuf);
239
240 dst->nr_cbufs = src->nr_cbufs;
241 dst->width = src->width;
242 dst->height = src->height;
243 } else {
244 /* Set all surfaces to NULL. */
245 for (i = 0; i < dst->nr_cbufs; i++) {
246 pipe_surface_reference(&dst->cbufs[i], NULL);
247 }
248
249 pipe_surface_reference(&dst->zsbuf, NULL);
250
251 dst->nr_cbufs = 0;
252 }
253 }
254
255 static INLINE
256 void util_blitter_save_framebuffer(struct blitter_context *blitter,
257 const struct pipe_framebuffer_state *state)
258 {
259 blitter->saved_fb_state.nr_cbufs = 0; /* It's ~0 now, meaning it's unsaved. */
260 util_assign_framebuffer_state(&blitter->saved_fb_state, state);
261 }
262
263 static INLINE
264 void util_blitter_save_viewport(struct blitter_context *blitter,
265 struct pipe_viewport_state *state)
266 {
267 blitter->saved_viewport = *state;
268 }
269
270 static INLINE
271 void util_blitter_save_clip(struct blitter_context *blitter,
272 struct pipe_clip_state *state)
273 {
274 blitter->saved_clip = *state;
275 }
276
277 static INLINE
278 void util_blitter_save_fragment_sampler_states(
279 struct blitter_context *blitter,
280 int num_sampler_states,
281 void **sampler_states)
282 {
283 assert(num_sampler_states <= Elements(blitter->saved_sampler_states));
284
285 blitter->saved_num_sampler_states = num_sampler_states;
286 memcpy(blitter->saved_sampler_states, sampler_states,
287 num_sampler_states * sizeof(void *));
288 }
289
290 static INLINE void
291 util_blitter_save_fragment_sampler_views(struct blitter_context *blitter,
292 int num_views,
293 struct pipe_sampler_view **views)
294 {
295 unsigned i;
296 assert(num_views <= Elements(blitter->saved_sampler_views));
297
298 blitter->saved_num_sampler_views = num_views;
299 for (i = 0; i < num_views; i++)
300 pipe_sampler_view_reference(&blitter->saved_sampler_views[i],
301 views[i]);
302 }
303
304 static INLINE void
305 util_blitter_save_vertex_buffers(struct blitter_context *blitter,
306 int num_vertex_buffers,
307 struct pipe_vertex_buffer *vertex_buffers)
308 {
309 unsigned i;
310 assert(num_vertex_buffers <= Elements(blitter->saved_vertex_buffers));
311
312 blitter->saved_num_vertex_buffers = num_vertex_buffers;
313
314 for (i = 0; i < num_vertex_buffers; i++) {
315 if (vertex_buffers[i].buffer) {
316 pipe_resource_reference(&blitter->saved_vertex_buffers[i].buffer,
317 vertex_buffers[i].buffer);
318 }
319 }
320
321 memcpy(blitter->saved_vertex_buffers,
322 vertex_buffers,
323 num_vertex_buffers * sizeof(struct pipe_vertex_buffer));
324 }
325
326 #ifdef __cplusplus
327 }
328 #endif
329
330 #endif