r300g: implement hyper-z support. (v4)
[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_inlines.h"
31 #include "util/u_memory.h"
32
33 #include "pipe/p_state.h"
34
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 struct pipe_context;
41
42 enum blitter_attrib_type {
43 UTIL_BLITTER_ATTRIB_NONE,
44 UTIL_BLITTER_ATTRIB_COLOR,
45 UTIL_BLITTER_ATTRIB_TEXCOORD
46 };
47
48 struct blitter_context
49 {
50 /**
51 * Draw a rectangle.
52 *
53 * \param x1 An X coordinate of the top-left corner.
54 * \param y1 A Y coordinate of the top-left corner.
55 * \param x2 An X coordinate of the bottom-right corner.
56 * \param y2 A Y coordinate of the bottom-right corner.
57 * \param depth A depth which the rectangle is rendered at.
58 *
59 * \param type Semantics of the attributes "attrib".
60 * If type is UTIL_BLITTER_ATTRIB_NONE, ignore them.
61 * If type is UTIL_BLITTER_ATTRIB_COLOR, the attributes
62 * make up a constant RGBA color, and should go to the COLOR0
63 * varying slot of a fragment shader.
64 * If type is UTIL_BLITTER_ATTRIB_TEXCOORD, {a1, a2} and
65 * {a3, a4} specify top-left and bottom-right texture
66 * coordinates of the rectangle, respectively, and should go
67 * to the GENERIC0 varying slot of a fragment shader.
68 *
69 * \param attrib See type.
70 *
71 * \note A driver may optionally override this callback to implement
72 * a specialized hardware path for drawing a rectangle, e.g. using
73 * a rectangular point sprite.
74 */
75 void (*draw_rectangle)(struct blitter_context *blitter,
76 unsigned x1, unsigned y1, unsigned x2, unsigned y2,
77 float depth,
78 enum blitter_attrib_type type,
79 const float attrib[4]);
80
81 /* Private members, really. */
82 struct pipe_context *pipe; /**< pipe context */
83
84 void *saved_blend_state; /**< blend state */
85 void *saved_dsa_state; /**< depth stencil alpha state */
86 void *saved_velem_state; /**< vertex elements state */
87 void *saved_rs_state; /**< rasterizer state */
88 void *saved_fs, *saved_vs; /**< fragment shader, vertex shader */
89
90 struct pipe_framebuffer_state saved_fb_state; /**< framebuffer state */
91 struct pipe_stencil_ref saved_stencil_ref; /**< stencil ref */
92 struct pipe_viewport_state saved_viewport;
93 struct pipe_clip_state saved_clip;
94
95 int saved_num_sampler_states;
96 void *saved_sampler_states[PIPE_MAX_SAMPLERS];
97
98 int saved_num_sampler_views;
99 struct pipe_sampler_view *saved_sampler_views[PIPE_MAX_SAMPLERS];
100
101 int saved_num_vertex_buffers;
102 struct pipe_vertex_buffer saved_vertex_buffers[PIPE_MAX_ATTRIBS];
103 };
104
105 /**
106 * Create a blitter context.
107 */
108 struct blitter_context *util_blitter_create(struct pipe_context *pipe);
109
110 /**
111 * Destroy a blitter context.
112 */
113 void util_blitter_destroy(struct blitter_context *blitter);
114
115 /**
116 * Return the pipe context associated with a blitter context.
117 */
118 static INLINE
119 struct pipe_context *util_blitter_get_pipe(struct blitter_context *blitter)
120 {
121 return blitter->pipe;
122 }
123
124 /*
125 * These CSOs must be saved before any of the following functions is called:
126 * - blend state
127 * - depth stencil alpha state
128 * - rasterizer state
129 * - vertex shader
130 * - fragment shader
131 */
132
133 /**
134 * Clear a specified set of currently bound buffers to specified values.
135 */
136 void util_blitter_clear(struct blitter_context *blitter,
137 unsigned width, unsigned height,
138 unsigned num_cbufs,
139 unsigned clear_buffers,
140 const float *rgba,
141 double depth, unsigned stencil);
142
143 /**
144 * Copy a block of pixels from one surface to another.
145 *
146 * You can copy from any color format to any other color format provided
147 * the former can be sampled and the latter can be rendered to. Otherwise,
148 * a software fallback path is taken and both surfaces must be of the same
149 * format.
150 *
151 * The same holds for depth-stencil formats with the exception that stencil
152 * cannot be copied unless you set ignore_stencil to FALSE. In that case,
153 * a software fallback path is taken and both surfaces must be of the same
154 * format.
155 *
156 * Use pipe_screen->is_format_supported to know your options.
157 *
158 * These states must be saved in the blitter in addition to the state objects
159 * already required to be saved:
160 * - framebuffer state
161 * - fragment sampler states
162 * - fragment sampler textures
163 */
164 void util_blitter_copy_region(struct blitter_context *blitter,
165 struct pipe_resource *dst,
166 struct pipe_subresource subdst,
167 unsigned dstx, unsigned dsty, unsigned dstz,
168 struct pipe_resource *src,
169 struct pipe_subresource subsrc,
170 unsigned srcx, unsigned srcy, unsigned srcz,
171 unsigned width, unsigned height,
172 boolean ignore_stencil);
173
174 /**
175 * Clear a region of a (color) surface to a constant value.
176 *
177 * These states must be saved in the blitter in addition to the state objects
178 * already required to be saved:
179 * - framebuffer state
180 */
181 void util_blitter_clear_render_target(struct blitter_context *blitter,
182 struct pipe_surface *dst,
183 const float *rgba,
184 unsigned dstx, unsigned dsty,
185 unsigned width, unsigned height);
186
187 /**
188 * Clear a region of a depth-stencil surface, both stencil and depth
189 * or only one of them if this is a combined depth-stencil surface.
190 *
191 * These states must be saved in the blitter in addition to the state objects
192 * already required to be saved:
193 * - framebuffer state
194 */
195 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
196 struct pipe_surface *dst,
197 unsigned clear_flags,
198 double depth,
199 unsigned stencil,
200 unsigned dstx, unsigned dsty,
201 unsigned width, unsigned height);
202
203 void util_blitter_flush_depth_stencil(struct blitter_context *blitter,
204 struct pipe_surface *dstsurf);
205 /* The functions below should be used to save currently bound constant state
206 * objects inside a driver. The objects are automatically restored at the end
207 * of the util_blitter_{clear, copy_region, fill_region} functions and then
208 * forgotten.
209 *
210 * CSOs not listed here are not affected by util_blitter. */
211
212 static INLINE
213 void util_blitter_save_blend(struct blitter_context *blitter,
214 void *state)
215 {
216 blitter->saved_blend_state = state;
217 }
218
219 static INLINE
220 void util_blitter_save_depth_stencil_alpha(struct blitter_context *blitter,
221 void *state)
222 {
223 blitter->saved_dsa_state = state;
224 }
225
226 static INLINE
227 void util_blitter_save_vertex_elements(struct blitter_context *blitter,
228 void *state)
229 {
230 blitter->saved_velem_state = state;
231 }
232
233 static INLINE
234 void util_blitter_save_stencil_ref(struct blitter_context *blitter,
235 const struct pipe_stencil_ref *state)
236 {
237 blitter->saved_stencil_ref = *state;
238 }
239
240 static INLINE
241 void util_blitter_save_rasterizer(struct blitter_context *blitter,
242 void *state)
243 {
244 blitter->saved_rs_state = state;
245 }
246
247 static INLINE
248 void util_blitter_save_fragment_shader(struct blitter_context *blitter,
249 void *fs)
250 {
251 blitter->saved_fs = fs;
252 }
253
254 static INLINE
255 void util_blitter_save_vertex_shader(struct blitter_context *blitter,
256 void *vs)
257 {
258 blitter->saved_vs = vs;
259 }
260
261 /* XXX This should probably be moved elsewhere. */
262 static INLINE
263 void util_assign_framebuffer_state(struct pipe_framebuffer_state *dst,
264 const struct pipe_framebuffer_state *src)
265 {
266 unsigned i;
267
268 if (src) {
269 /* Reference all surfaces. */
270 for (i = 0; i < src->nr_cbufs; i++) {
271 pipe_surface_reference(&dst->cbufs[i], src->cbufs[i]);
272 }
273 for (; i < dst->nr_cbufs; i++) {
274 pipe_surface_reference(&dst->cbufs[i], NULL);
275 }
276
277 pipe_surface_reference(&dst->zsbuf, src->zsbuf);
278
279 dst->nr_cbufs = src->nr_cbufs;
280 dst->width = src->width;
281 dst->height = src->height;
282 } else {
283 /* Set all surfaces to NULL. */
284 for (i = 0; i < dst->nr_cbufs; i++) {
285 pipe_surface_reference(&dst->cbufs[i], NULL);
286 }
287
288 pipe_surface_reference(&dst->zsbuf, NULL);
289
290 dst->nr_cbufs = 0;
291 }
292 }
293
294 static INLINE
295 void util_blitter_save_framebuffer(struct blitter_context *blitter,
296 const struct pipe_framebuffer_state *state)
297 {
298 blitter->saved_fb_state.nr_cbufs = 0; /* It's ~0 now, meaning it's unsaved. */
299 util_assign_framebuffer_state(&blitter->saved_fb_state, state);
300 }
301
302 static INLINE
303 void util_blitter_save_viewport(struct blitter_context *blitter,
304 struct pipe_viewport_state *state)
305 {
306 blitter->saved_viewport = *state;
307 }
308
309 static INLINE
310 void util_blitter_save_clip(struct blitter_context *blitter,
311 struct pipe_clip_state *state)
312 {
313 blitter->saved_clip = *state;
314 }
315
316 static INLINE
317 void util_blitter_save_fragment_sampler_states(
318 struct blitter_context *blitter,
319 int num_sampler_states,
320 void **sampler_states)
321 {
322 assert(num_sampler_states <= Elements(blitter->saved_sampler_states));
323
324 blitter->saved_num_sampler_states = num_sampler_states;
325 memcpy(blitter->saved_sampler_states, sampler_states,
326 num_sampler_states * sizeof(void *));
327 }
328
329 static INLINE void
330 util_blitter_save_fragment_sampler_views(struct blitter_context *blitter,
331 int num_views,
332 struct pipe_sampler_view **views)
333 {
334 unsigned i;
335 assert(num_views <= Elements(blitter->saved_sampler_views));
336
337 blitter->saved_num_sampler_views = num_views;
338 for (i = 0; i < num_views; i++)
339 pipe_sampler_view_reference(&blitter->saved_sampler_views[i],
340 views[i]);
341 }
342
343 static INLINE void
344 util_blitter_save_vertex_buffers(struct blitter_context *blitter,
345 int num_vertex_buffers,
346 struct pipe_vertex_buffer *vertex_buffers)
347 {
348 unsigned i;
349 assert(num_vertex_buffers <= Elements(blitter->saved_vertex_buffers));
350
351 blitter->saved_num_vertex_buffers = num_vertex_buffers;
352
353 for (i = 0; i < num_vertex_buffers; i++) {
354 if (vertex_buffers[i].buffer) {
355 pipe_resource_reference(&blitter->saved_vertex_buffers[i].buffer,
356 vertex_buffers[i].buffer);
357 }
358 }
359
360 memcpy(blitter->saved_vertex_buffers,
361 vertex_buffers,
362 num_vertex_buffers * sizeof(struct pipe_vertex_buffer));
363 }
364
365 #ifdef __cplusplus
366 }
367 #endif
368
369 #endif