Merge branch 'mesa_7_7_branch'
[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_memory.h"
31
32 #include "pipe/p_state.h"
33
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 struct pipe_context;
40
41 struct blitter_context
42 {
43 /* Private members, really. */
44 void *saved_blend_state; /**< blend state */
45 void *saved_dsa_state; /**< depth stencil alpha state */
46 void *saved_rs_state; /**< rasterizer state */
47 void *saved_fs, *saved_vs; /**< fragment shader, vertex shader */
48
49 struct pipe_framebuffer_state saved_fb_state; /**< framebuffer state */
50
51 int saved_num_sampler_states;
52 void *saved_sampler_states[32];
53
54 int saved_num_textures;
55 struct pipe_texture *saved_textures[32]; /* is 32 enough? */
56 };
57
58 /**
59 * Create a blitter context.
60 */
61 struct blitter_context *util_blitter_create(struct pipe_context *pipe);
62
63 /**
64 * Destroy a blitter context.
65 */
66 void util_blitter_destroy(struct blitter_context *blitter);
67
68 /*
69 * These CSOs must be saved before any of the following functions is called:
70 * - blend state
71 * - depth stencil alpha state
72 * - rasterizer state
73 * - vertex shader
74 * - fragment shader
75 */
76
77 /**
78 * Clear a specified set of currently bound buffers to specified values.
79 */
80 void util_blitter_clear(struct blitter_context *blitter,
81 unsigned width, unsigned height,
82 unsigned num_cbufs,
83 unsigned clear_buffers,
84 const float *rgba,
85 double depth, unsigned stencil);
86
87 /**
88 * Copy a block of pixels from one surface to another.
89 *
90 * You can copy from any color format to any other color format provided
91 * the former can be sampled and the latter can be rendered to. Otherwise,
92 * a software fallback path is taken and both surfaces must be of the same
93 * format.
94 *
95 * The same holds for depth-stencil formats with the exception that stencil
96 * cannot be copied unless you set ignore_stencil to FALSE. In that case,
97 * a software fallback path is taken and both surfaces must be of the same
98 * format.
99 *
100 * Use pipe_screen->is_format_supported to know your options.
101 *
102 * These states must be saved in the blitter in addition to the state objects
103 * already required to be saved:
104 * - framebuffer state
105 * - fragment sampler states
106 * - fragment sampler textures
107 */
108 void util_blitter_copy(struct blitter_context *blitter,
109 struct pipe_surface *dst,
110 unsigned dstx, unsigned dsty,
111 struct pipe_surface *src,
112 unsigned srcx, unsigned srcy,
113 unsigned width, unsigned height,
114 boolean ignore_stencil);
115
116 /**
117 * Fill a region of a surface with a constant value.
118 *
119 * If the surface cannot be rendered to or it's a depth-stencil format,
120 * a software fallback path is taken.
121 *
122 * These states must be saved in the blitter in addition to the state objects
123 * already required to be saved:
124 * - framebuffer state
125 */
126 void util_blitter_fill(struct blitter_context *blitter,
127 struct pipe_surface *dst,
128 unsigned dstx, unsigned dsty,
129 unsigned width, unsigned height,
130 unsigned value);
131
132 /**
133 * Copy all pixels from one surface to another.
134 *
135 * The rules are the same as in util_blitter_copy with the addition that
136 * surfaces must have the same size.
137 */
138 static INLINE
139 void util_blitter_copy_surface(struct blitter_context *blitter,
140 struct pipe_surface *dst,
141 struct pipe_surface *src,
142 boolean ignore_stencil)
143 {
144 assert(dst->width == src->width && dst->height == src->height);
145
146 util_blitter_copy(blitter, dst, 0, 0, src, 0, 0, src->width, src->height,
147 ignore_stencil);
148 }
149
150
151 /* The functions below should be used to save currently bound constant state
152 * objects inside a driver. The objects are automatically restored at the end
153 * of the util_blitter_{clear, fill, copy, copy_surface} functions and then
154 * forgotten.
155 *
156 * CSOs not listed here are not affected by util_blitter. */
157
158 static INLINE
159 void util_blitter_save_blend(struct blitter_context *blitter,
160 void *state)
161 {
162 blitter->saved_blend_state = state;
163 }
164
165 static INLINE
166 void util_blitter_save_depth_stencil_alpha(struct blitter_context *blitter,
167 void *state)
168 {
169 blitter->saved_dsa_state = state;
170 }
171
172 static INLINE
173 void util_blitter_save_rasterizer(struct blitter_context *blitter,
174 void *state)
175 {
176 blitter->saved_rs_state = state;
177 }
178
179 static INLINE
180 void util_blitter_save_fragment_shader(struct blitter_context *blitter,
181 void *fs)
182 {
183 blitter->saved_fs = fs;
184 }
185
186 static INLINE
187 void util_blitter_save_vertex_shader(struct blitter_context *blitter,
188 void *vs)
189 {
190 blitter->saved_vs = vs;
191 }
192
193 static INLINE
194 void util_blitter_save_framebuffer(struct blitter_context *blitter,
195 struct pipe_framebuffer_state *state)
196 {
197 blitter->saved_fb_state = *state;
198 }
199
200 static INLINE
201 void util_blitter_save_fragment_sampler_states(
202 struct blitter_context *blitter,
203 int num_sampler_states,
204 void **sampler_states)
205 {
206 assert(num_sampler_states <= Elements(blitter->saved_sampler_states));
207
208 blitter->saved_num_sampler_states = num_sampler_states;
209 memcpy(blitter->saved_sampler_states, sampler_states,
210 num_sampler_states * sizeof(void *));
211 }
212
213 static INLINE
214 void util_blitter_save_fragment_sampler_textures(
215 struct blitter_context *blitter,
216 int num_textures,
217 struct pipe_texture **textures)
218 {
219 assert(num_textures <= Elements(blitter->saved_textures));
220
221 blitter->saved_num_textures = num_textures;
222 memcpy(blitter->saved_textures, textures,
223 num_textures * sizeof(struct pipe_texture *));
224 }
225
226 #ifdef __cplusplus
227 }
228 #endif
229
230 #endif