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