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