9fe7952951d3557256f58e045b3dcf5235af9d63
[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_framebuffer.h"
31 #include "util/u_inlines.h"
32 #include "util/u_memory.h"
33
34 #include "pipe/p_state.h"
35
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 struct pipe_context;
42
43 enum blitter_attrib_type {
44 UTIL_BLITTER_ATTRIB_NONE,
45 UTIL_BLITTER_ATTRIB_COLOR,
46 UTIL_BLITTER_ATTRIB_TEXCOORD
47 };
48
49 struct blitter_context
50 {
51 /**
52 * Draw a rectangle.
53 *
54 * \param x1 An X coordinate of the top-left corner.
55 * \param y1 A Y coordinate of the top-left corner.
56 * \param x2 An X coordinate of the bottom-right corner.
57 * \param y2 A Y coordinate of the bottom-right corner.
58 * \param depth A depth which the rectangle is rendered at.
59 *
60 * \param type Semantics of the attributes "attrib".
61 * If type is UTIL_BLITTER_ATTRIB_NONE, ignore them.
62 * If type is UTIL_BLITTER_ATTRIB_COLOR, the attributes
63 * make up a constant RGBA color, and should go
64 * to the GENERIC0 varying slot of a fragment shader.
65 * If type is UTIL_BLITTER_ATTRIB_TEXCOORD, {a1, a2} and
66 * {a3, a4} specify top-left and bottom-right texture
67 * coordinates of the rectangle, respectively, and should go
68 * to the GENERIC0 varying slot of a fragment shader.
69 *
70 * \param attrib See type.
71 *
72 * \note A driver may optionally override this callback to implement
73 * a specialized hardware path for drawing a rectangle, e.g. using
74 * a rectangular point sprite.
75 */
76 void (*draw_rectangle)(struct blitter_context *blitter,
77 unsigned x1, unsigned y1, unsigned x2, unsigned y2,
78 float depth,
79 enum blitter_attrib_type type,
80 const union pipe_color_union *color);
81
82 /* Whether the blitter is running. */
83 boolean running;
84
85 /* Private members, really. */
86 struct pipe_context *pipe; /**< pipe context */
87
88 void *saved_blend_state; /**< blend state */
89 void *saved_dsa_state; /**< depth stencil alpha state */
90 void *saved_velem_state; /**< vertex elements state */
91 void *saved_rs_state; /**< rasterizer state */
92 void *saved_fs, *saved_vs; /**< fragment shader, vertex shader */
93
94 struct pipe_framebuffer_state saved_fb_state; /**< framebuffer state */
95 struct pipe_stencil_ref saved_stencil_ref; /**< stencil ref */
96 struct pipe_viewport_state saved_viewport;
97 struct pipe_clip_state saved_clip;
98
99 int saved_num_sampler_states;
100 void *saved_sampler_states[PIPE_MAX_SAMPLERS];
101
102 int saved_num_sampler_views;
103 struct pipe_sampler_view *saved_sampler_views[PIPE_MAX_SAMPLERS];
104
105 int saved_num_vertex_buffers;
106 struct pipe_vertex_buffer saved_vertex_buffers[PIPE_MAX_ATTRIBS];
107 };
108
109 /**
110 * Create a blitter context.
111 */
112 struct blitter_context *util_blitter_create(struct pipe_context *pipe);
113
114 /**
115 * Destroy a blitter context.
116 */
117 void util_blitter_destroy(struct blitter_context *blitter);
118
119 /**
120 * Return the pipe context associated with a blitter context.
121 */
122 static INLINE
123 struct pipe_context *util_blitter_get_pipe(struct blitter_context *blitter)
124 {
125 return blitter->pipe;
126 }
127
128 /*
129 * These states must be saved before any of the following functions are called:
130 * - vertex buffers
131 * - vertex elements
132 * - vertex shader
133 * - rasterizer state
134 */
135
136 /**
137 * Clear a specified set of currently bound buffers to specified values.
138 *
139 * These states must be saved in the blitter in addition to the state objects
140 * already required to be saved:
141 * - fragment shader
142 * - depth stencil alpha state
143 * - blend state
144 */
145 void util_blitter_clear(struct blitter_context *blitter,
146 unsigned width, unsigned height,
147 unsigned num_cbufs,
148 unsigned clear_buffers,
149 const union pipe_color_union *color,
150 double depth, unsigned stencil);
151
152 void util_blitter_clear_depth_custom(struct blitter_context *blitter,
153 unsigned width, unsigned height,
154 double depth, void *custom_dsa);
155
156 /**
157 * Copy a block of pixels from one surface to another.
158 *
159 * You can copy from any color format to any other color format provided
160 * the former can be sampled from and the latter can be rendered to. Otherwise,
161 * a software fallback path is taken and both surfaces must be of the same
162 * format.
163 *
164 * The same holds for depth-stencil formats with the exception that stencil
165 * cannot be copied unless you set ignore_stencil to FALSE. In that case,
166 * a software fallback path is taken and both surfaces must be of the same
167 * format.
168 * XXX implement hw-accel stencil copy using shader stencil export.
169 *
170 * Use pipe_screen->is_format_supported to know your options.
171 *
172 * These states must be saved in the blitter in addition to the state objects
173 * already required to be saved:
174 * - fragment shader
175 * - depth stencil alpha state
176 * - blend state
177 * - fragment sampler states
178 * - fragment sampler textures
179 * - framebuffer state
180 */
181 void util_blitter_copy_texture(struct blitter_context *blitter,
182 struct pipe_resource *dst,
183 unsigned dstlevel,
184 unsigned dstx, unsigned dsty, unsigned dstz,
185 struct pipe_resource *src,
186 unsigned srclevel,
187 const struct pipe_box *srcbox,
188 boolean ignore_stencil);
189
190 /**
191 * Clear a region of a (color) surface to a constant value.
192 *
193 * These states must be saved in the blitter in addition to the state objects
194 * already required to be saved:
195 * - fragment shader
196 * - depth stencil alpha state
197 * - blend state
198 * - framebuffer state
199 */
200 void util_blitter_clear_render_target(struct blitter_context *blitter,
201 struct pipe_surface *dst,
202 const union pipe_color_union *color,
203 unsigned dstx, unsigned dsty,
204 unsigned width, unsigned height);
205
206 /**
207 * Clear a region of a depth-stencil surface, both stencil and depth
208 * or only one of them if this is a combined depth-stencil surface.
209 *
210 * These states must be saved in the blitter in addition to the state objects
211 * already required to be saved:
212 * - fragment shader
213 * - depth stencil alpha state
214 * - blend state
215 * - framebuffer state
216 */
217 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
218 struct pipe_surface *dst,
219 unsigned clear_flags,
220 double depth,
221 unsigned stencil,
222 unsigned dstx, unsigned dsty,
223 unsigned width, unsigned height);
224
225 void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
226 struct pipe_surface *zsurf,
227 struct pipe_surface *cbsurf,
228 void *dsa_stage, float depth);
229
230 /* The functions below should be used to save currently bound constant state
231 * objects inside a driver. The objects are automatically restored at the end
232 * of the util_blitter_{clear, copy_region, fill_region} functions and then
233 * forgotten.
234 *
235 * States not listed here are not affected by util_blitter. */
236
237 static INLINE
238 void util_blitter_save_blend(struct blitter_context *blitter,
239 void *state)
240 {
241 blitter->saved_blend_state = state;
242 }
243
244 static INLINE
245 void util_blitter_save_depth_stencil_alpha(struct blitter_context *blitter,
246 void *state)
247 {
248 blitter->saved_dsa_state = state;
249 }
250
251 static INLINE
252 void util_blitter_save_vertex_elements(struct blitter_context *blitter,
253 void *state)
254 {
255 blitter->saved_velem_state = state;
256 }
257
258 static INLINE
259 void util_blitter_save_stencil_ref(struct blitter_context *blitter,
260 const struct pipe_stencil_ref *state)
261 {
262 blitter->saved_stencil_ref = *state;
263 }
264
265 static INLINE
266 void util_blitter_save_rasterizer(struct blitter_context *blitter,
267 void *state)
268 {
269 blitter->saved_rs_state = state;
270 }
271
272 static INLINE
273 void util_blitter_save_fragment_shader(struct blitter_context *blitter,
274 void *fs)
275 {
276 blitter->saved_fs = fs;
277 }
278
279 static INLINE
280 void util_blitter_save_vertex_shader(struct blitter_context *blitter,
281 void *vs)
282 {
283 blitter->saved_vs = vs;
284 }
285
286 static INLINE
287 void util_blitter_save_framebuffer(struct blitter_context *blitter,
288 const struct pipe_framebuffer_state *state)
289 {
290 blitter->saved_fb_state.nr_cbufs = 0; /* It's ~0 now, meaning it's unsaved. */
291 util_copy_framebuffer_state(&blitter->saved_fb_state, state);
292 }
293
294 static INLINE
295 void util_blitter_save_viewport(struct blitter_context *blitter,
296 struct pipe_viewport_state *state)
297 {
298 blitter->saved_viewport = *state;
299 }
300
301 static INLINE
302 void util_blitter_save_clip(struct blitter_context *blitter,
303 struct pipe_clip_state *state)
304 {
305 blitter->saved_clip = *state;
306 }
307
308 static INLINE
309 void util_blitter_save_fragment_sampler_states(
310 struct blitter_context *blitter,
311 int num_sampler_states,
312 void **sampler_states)
313 {
314 assert(num_sampler_states <= Elements(blitter->saved_sampler_states));
315
316 blitter->saved_num_sampler_states = num_sampler_states;
317 memcpy(blitter->saved_sampler_states, sampler_states,
318 num_sampler_states * sizeof(void *));
319 }
320
321 static INLINE void
322 util_blitter_save_fragment_sampler_views(struct blitter_context *blitter,
323 int num_views,
324 struct pipe_sampler_view **views)
325 {
326 unsigned i;
327 assert(num_views <= Elements(blitter->saved_sampler_views));
328
329 blitter->saved_num_sampler_views = num_views;
330 for (i = 0; i < num_views; i++)
331 pipe_sampler_view_reference(&blitter->saved_sampler_views[i],
332 views[i]);
333 }
334
335 static INLINE void
336 util_blitter_save_vertex_buffers(struct blitter_context *blitter,
337 int num_vertex_buffers,
338 struct pipe_vertex_buffer *vertex_buffers)
339 {
340 assert(num_vertex_buffers <= Elements(blitter->saved_vertex_buffers));
341
342 blitter->saved_num_vertex_buffers = 0;
343 util_copy_vertex_buffers(blitter->saved_vertex_buffers,
344 (unsigned*)&blitter->saved_num_vertex_buffers,
345 vertex_buffers,
346 num_vertex_buffers);
347 }
348
349 #ifdef __cplusplus
350 }
351 #endif
352
353 #endif