1 /**************************************************************************
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
25 **************************************************************************/
30 #include "util/u_memory.h"
32 #include "pipe/p_state.h"
41 struct blitter_context
43 /* Private members, really. */
44 void *saved_blend_state
; /**< blend state */
45 void *saved_dsa_state
; /**< depth stencil alpha state */
46 void *saved_velem_state
; /**< vertex elements state */
47 void *saved_rs_state
; /**< rasterizer state */
48 void *saved_fs
, *saved_vs
; /**< fragment shader, vertex shader */
50 struct pipe_framebuffer_state saved_fb_state
; /**< framebuffer state */
51 struct pipe_stencil_ref saved_stencil_ref
; /**< stencil ref */
52 struct pipe_viewport_state saved_viewport
;
53 struct pipe_clip_state saved_clip
;
55 int saved_num_sampler_states
;
56 void *saved_sampler_states
[PIPE_MAX_SAMPLERS
];
58 int saved_num_sampler_views
;
59 struct pipe_sampler_view
*saved_sampler_views
[PIPE_MAX_SAMPLERS
];
61 int saved_num_vertex_buffers
;
62 struct pipe_vertex_buffer saved_vertex_buffers
[PIPE_MAX_ATTRIBS
];
66 * Create a blitter context.
68 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
);
71 * Destroy a blitter context.
73 void util_blitter_destroy(struct blitter_context
*blitter
);
76 * These CSOs must be saved before any of the following functions is called:
78 * - depth stencil alpha state
85 * Clear a specified set of currently bound buffers to specified values.
87 void util_blitter_clear(struct blitter_context
*blitter
,
88 unsigned width
, unsigned height
,
90 unsigned clear_buffers
,
92 double depth
, unsigned stencil
);
95 * Copy a block of pixels from one surface to another.
97 * You can copy from any color format to any other color format provided
98 * the former can be sampled and the latter can be rendered to. Otherwise,
99 * a software fallback path is taken and both surfaces must be of the same
102 * The same holds for depth-stencil formats with the exception that stencil
103 * cannot be copied unless you set ignore_stencil to FALSE. In that case,
104 * a software fallback path is taken and both surfaces must be of the same
107 * Use pipe_screen->is_format_supported to know your options.
109 * These states must be saved in the blitter in addition to the state objects
110 * already required to be saved:
111 * - framebuffer state
112 * - fragment sampler states
113 * - fragment sampler textures
115 void util_blitter_copy(struct blitter_context
*blitter
,
116 struct pipe_surface
*dst
,
117 unsigned dstx
, unsigned dsty
,
118 struct pipe_surface
*src
,
119 unsigned srcx
, unsigned srcy
,
120 unsigned width
, unsigned height
,
121 boolean ignore_stencil
);
124 * Fill a region of a surface with a constant value.
126 * If the surface cannot be rendered to or it's a depth-stencil format,
127 * a software fallback path is taken.
129 * These states must be saved in the blitter in addition to the state objects
130 * already required to be saved:
131 * - framebuffer state
133 void util_blitter_fill(struct blitter_context
*blitter
,
134 struct pipe_surface
*dst
,
135 unsigned dstx
, unsigned dsty
,
136 unsigned width
, unsigned height
,
140 * Copy all pixels from one surface to another.
142 * The rules are the same as in util_blitter_copy with the addition that
143 * surfaces must have the same size.
146 void util_blitter_copy_surface(struct blitter_context
*blitter
,
147 struct pipe_surface
*dst
,
148 struct pipe_surface
*src
,
149 boolean ignore_stencil
)
151 assert(dst
->width
== src
->width
&& dst
->height
== src
->height
);
153 util_blitter_copy(blitter
, dst
, 0, 0, src
, 0, 0, src
->width
, src
->height
,
158 /* The functions below should be used to save currently bound constant state
159 * objects inside a driver. The objects are automatically restored at the end
160 * of the util_blitter_{clear, fill, copy, copy_surface} functions and then
163 * CSOs not listed here are not affected by util_blitter. */
166 void util_blitter_save_blend(struct blitter_context
*blitter
,
169 blitter
->saved_blend_state
= state
;
173 void util_blitter_save_depth_stencil_alpha(struct blitter_context
*blitter
,
176 blitter
->saved_dsa_state
= state
;
180 void util_blitter_save_vertex_elements(struct blitter_context
*blitter
,
183 blitter
->saved_velem_state
= state
;
187 void util_blitter_save_stencil_ref(struct blitter_context
*blitter
,
188 const struct pipe_stencil_ref
*state
)
190 blitter
->saved_stencil_ref
= *state
;
194 void util_blitter_save_rasterizer(struct blitter_context
*blitter
,
197 blitter
->saved_rs_state
= state
;
201 void util_blitter_save_fragment_shader(struct blitter_context
*blitter
,
204 blitter
->saved_fs
= fs
;
208 void util_blitter_save_vertex_shader(struct blitter_context
*blitter
,
211 blitter
->saved_vs
= vs
;
215 void util_blitter_save_framebuffer(struct blitter_context
*blitter
,
216 struct pipe_framebuffer_state
*state
)
218 blitter
->saved_fb_state
= *state
;
222 void util_blitter_save_viewport(struct blitter_context
*blitter
,
223 struct pipe_viewport_state
*state
)
225 blitter
->saved_viewport
= *state
;
229 void util_blitter_save_clip(struct blitter_context
*blitter
,
230 struct pipe_clip_state
*state
)
232 blitter
->saved_clip
= *state
;
236 void util_blitter_save_fragment_sampler_states(
237 struct blitter_context
*blitter
,
238 int num_sampler_states
,
239 void **sampler_states
)
241 assert(num_sampler_states
<= Elements(blitter
->saved_sampler_states
));
243 blitter
->saved_num_sampler_states
= num_sampler_states
;
244 memcpy(blitter
->saved_sampler_states
, sampler_states
,
245 num_sampler_states
* sizeof(void *));
249 util_blitter_save_fragment_sampler_views(struct blitter_context
*blitter
,
251 struct pipe_sampler_view
**views
)
253 assert(num_views
<= Elements(blitter
->saved_sampler_views
));
255 blitter
->saved_num_sampler_views
= num_views
;
256 memcpy(blitter
->saved_sampler_views
,
258 num_views
* sizeof(struct pipe_sampler_view
*));
262 util_blitter_save_vertex_buffers(struct blitter_context
*blitter
,
263 int num_vertex_buffers
,
264 struct pipe_vertex_buffer
*vertex_buffers
)
266 assert(num_vertex_buffers
<= Elements(blitter
->saved_vertex_buffers
));
268 blitter
->saved_num_vertex_buffers
= num_vertex_buffers
;
269 memcpy(blitter
->saved_vertex_buffers
,
271 num_vertex_buffers
* sizeof(struct pipe_vertex_buffer
));