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