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_inlines.h"
31 #include "util/u_memory.h"
33 #include "pipe/p_state.h"
42 struct blitter_context
44 /* Private members, really. */
45 struct pipe_context
*pipe
; /**< pipe context */
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 */
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
;
58 int saved_num_sampler_states
;
59 void *saved_sampler_states
[PIPE_MAX_SAMPLERS
];
61 int saved_num_sampler_views
;
62 struct pipe_sampler_view
*saved_sampler_views
[PIPE_MAX_SAMPLERS
];
64 int saved_num_vertex_buffers
;
65 struct pipe_vertex_buffer saved_vertex_buffers
[PIPE_MAX_ATTRIBS
];
69 * Create a blitter context.
71 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
);
74 * Destroy a blitter context.
76 void util_blitter_destroy(struct blitter_context
*blitter
);
79 * Return the pipe context associated with a blitter context.
82 struct pipe_context
*util_blitter_get_pipe(struct blitter_context
*blitter
)
88 * These CSOs must be saved before any of the following functions is called:
90 * - depth stencil alpha state
97 * Clear a specified set of currently bound buffers to specified values.
99 void util_blitter_clear(struct blitter_context
*blitter
,
100 unsigned width
, unsigned height
,
102 unsigned clear_buffers
,
104 double depth
, unsigned stencil
);
107 * Copy a block of pixels from one surface to another.
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
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
119 * Use pipe_screen->is_format_supported to know your options.
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
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
);
138 * Clear a region of a (color) surface to a constant value.
140 * These states must be saved in the blitter in addition to the state objects
141 * already required to be saved:
142 * - framebuffer state
144 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
145 struct pipe_surface
*dst
,
147 unsigned dstx
, unsigned dsty
,
148 unsigned width
, unsigned height
);
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.
154 * These states must be saved in the blitter in addition to the state objects
155 * already required to be saved:
156 * - framebuffer state
158 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
159 struct pipe_surface
*dst
,
160 unsigned clear_flags
,
163 unsigned dstx
, unsigned dsty
,
164 unsigned width
, unsigned height
);
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
171 * CSOs not listed here are not affected by util_blitter. */
174 void util_blitter_save_blend(struct blitter_context
*blitter
,
177 blitter
->saved_blend_state
= state
;
181 void util_blitter_save_depth_stencil_alpha(struct blitter_context
*blitter
,
184 blitter
->saved_dsa_state
= state
;
188 void util_blitter_save_vertex_elements(struct blitter_context
*blitter
,
191 blitter
->saved_velem_state
= state
;
195 void util_blitter_save_stencil_ref(struct blitter_context
*blitter
,
196 const struct pipe_stencil_ref
*state
)
198 blitter
->saved_stencil_ref
= *state
;
202 void util_blitter_save_rasterizer(struct blitter_context
*blitter
,
205 blitter
->saved_rs_state
= state
;
209 void util_blitter_save_fragment_shader(struct blitter_context
*blitter
,
212 blitter
->saved_fs
= fs
;
216 void util_blitter_save_vertex_shader(struct blitter_context
*blitter
,
219 blitter
->saved_vs
= vs
;
222 /* XXX This should probably be moved elsewhere. */
224 void util_assign_framebuffer_state(struct pipe_framebuffer_state
*dst
,
225 const struct pipe_framebuffer_state
*src
)
230 /* Reference all surfaces. */
231 for (i
= 0; i
< src
->nr_cbufs
; i
++) {
232 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
234 for (; i
< dst
->nr_cbufs
; i
++) {
235 pipe_surface_reference(&dst
->cbufs
[i
], NULL
);
238 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
240 dst
->nr_cbufs
= src
->nr_cbufs
;
241 dst
->width
= src
->width
;
242 dst
->height
= src
->height
;
244 /* Set all surfaces to NULL. */
245 for (i
= 0; i
< dst
->nr_cbufs
; i
++) {
246 pipe_surface_reference(&dst
->cbufs
[i
], NULL
);
249 pipe_surface_reference(&dst
->zsbuf
, NULL
);
256 void util_blitter_save_framebuffer(struct blitter_context
*blitter
,
257 const struct pipe_framebuffer_state
*state
)
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
);
264 void util_blitter_save_viewport(struct blitter_context
*blitter
,
265 struct pipe_viewport_state
*state
)
267 blitter
->saved_viewport
= *state
;
271 void util_blitter_save_clip(struct blitter_context
*blitter
,
272 struct pipe_clip_state
*state
)
274 blitter
->saved_clip
= *state
;
278 void util_blitter_save_fragment_sampler_states(
279 struct blitter_context
*blitter
,
280 int num_sampler_states
,
281 void **sampler_states
)
283 assert(num_sampler_states
<= Elements(blitter
->saved_sampler_states
));
285 blitter
->saved_num_sampler_states
= num_sampler_states
;
286 memcpy(blitter
->saved_sampler_states
, sampler_states
,
287 num_sampler_states
* sizeof(void *));
291 util_blitter_save_fragment_sampler_views(struct blitter_context
*blitter
,
293 struct pipe_sampler_view
**views
)
296 assert(num_views
<= Elements(blitter
->saved_sampler_views
));
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
],
305 util_blitter_save_vertex_buffers(struct blitter_context
*blitter
,
306 int num_vertex_buffers
,
307 struct pipe_vertex_buffer
*vertex_buffers
)
310 assert(num_vertex_buffers
<= Elements(blitter
->saved_vertex_buffers
));
312 blitter
->saved_num_vertex_buffers
= num_vertex_buffers
;
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
);
321 memcpy(blitter
->saved_vertex_buffers
,
323 num_vertex_buffers
* sizeof(struct pipe_vertex_buffer
));