Merge branch 'master' into pipe-video
[mesa.git] / src / gallium / auxiliary / util / u_surface.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
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 VMWARE 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 /**
28 * @file
29 * Surface utility functions.
30 *
31 * @author Brian Paul
32 */
33
34
35 #include "pipe/p_defines.h"
36 #include "pipe/p_screen.h"
37 #include "pipe/p_state.h"
38
39 #include "util/u_format.h"
40 #include "util/u_inlines.h"
41 #include "util/u_rect.h"
42 #include "util/u_surface.h"
43 #include "util/u_pack_color.h"
44
45
46 /**
47 * Helper to quickly create an RGBA rendering surface of a certain size.
48 * \param textureOut returns the new texture
49 * \param surfaceOut returns the new surface
50 * \return TRUE for success, FALSE if failure
51 */
52 boolean
53 util_create_rgba_surface(struct pipe_screen *screen,
54 uint width, uint height,
55 uint bind,
56 struct pipe_resource **textureOut,
57 struct pipe_surface **surfaceOut)
58 {
59 static const enum pipe_format rgbaFormats[] = {
60 PIPE_FORMAT_B8G8R8A8_UNORM,
61 PIPE_FORMAT_A8R8G8B8_UNORM,
62 PIPE_FORMAT_A8B8G8R8_UNORM,
63 PIPE_FORMAT_NONE
64 };
65 const uint target = PIPE_TEXTURE_2D;
66 enum pipe_format format = PIPE_FORMAT_NONE;
67 struct pipe_resource templ;
68 uint i;
69
70 /* Choose surface format */
71 for (i = 0; rgbaFormats[i]; i++) {
72 if (screen->is_format_supported(screen, rgbaFormats[i],
73 target, 0, bind, 0)) {
74 format = rgbaFormats[i];
75 break;
76 }
77 }
78 if (format == PIPE_FORMAT_NONE)
79 return FALSE; /* unable to get an rgba format!?! */
80
81 /* create texture */
82 memset(&templ, 0, sizeof(templ));
83 templ.target = target;
84 templ.format = format;
85 templ.last_level = 0;
86 templ.width0 = width;
87 templ.height0 = height;
88 templ.depth0 = 1;
89 templ.bind = bind;
90
91 *textureOut = screen->resource_create(screen, &templ);
92 if (!*textureOut)
93 return FALSE;
94
95 /* create surface / view into texture */
96 *surfaceOut = screen->get_tex_surface(screen,
97 *textureOut,
98 0, 0, 0,
99 bind);
100 if (!*surfaceOut) {
101 pipe_resource_reference(textureOut, NULL);
102 return FALSE;
103 }
104
105 return TRUE;
106 }
107
108
109 /**
110 * Release the surface and texture from util_create_rgba_surface().
111 */
112 void
113 util_destroy_rgba_surface(struct pipe_resource *texture,
114 struct pipe_surface *surface)
115 {
116 pipe_surface_reference(&surface, NULL);
117 pipe_resource_reference(&texture, NULL);
118 }
119
120
121
122 /**
123 * Fallback function for pipe->resource_copy_region().
124 * Note: (X,Y)=(0,0) is always the upper-left corner.
125 */
126 void
127 util_resource_copy_region(struct pipe_context *pipe,
128 struct pipe_resource *dst,
129 struct pipe_subresource subdst,
130 unsigned dst_x, unsigned dst_y, unsigned dst_z,
131 struct pipe_resource *src,
132 struct pipe_subresource subsrc,
133 unsigned src_x, unsigned src_y, unsigned src_z,
134 unsigned w, unsigned h)
135 {
136 struct pipe_transfer *src_trans, *dst_trans;
137 void *dst_map;
138 const void *src_map;
139 enum pipe_format src_format, dst_format;
140
141 assert(src && dst);
142 if (!src || !dst)
143 return;
144
145 src_format = src->format;
146 dst_format = dst->format;
147
148 src_trans = pipe_get_transfer(pipe,
149 src,
150 subsrc.face,
151 subsrc.level,
152 src_z,
153 PIPE_TRANSFER_READ,
154 src_x, src_y, w, h);
155
156 dst_trans = pipe_get_transfer(pipe,
157 dst,
158 subdst.face,
159 subdst.level,
160 src_z,
161 PIPE_TRANSFER_WRITE,
162 dst_x, dst_y, w, h);
163
164 assert(util_format_get_blocksize(dst_format) == util_format_get_blocksize(src_format));
165 assert(util_format_get_blockwidth(dst_format) == util_format_get_blockwidth(src_format));
166 assert(util_format_get_blockheight(dst_format) == util_format_get_blockheight(src_format));
167
168 src_map = pipe->transfer_map(pipe, src_trans);
169 dst_map = pipe->transfer_map(pipe, dst_trans);
170
171 assert(src_map);
172 assert(dst_map);
173
174 if (src_map && dst_map) {
175 util_copy_rect(dst_map,
176 dst_format,
177 dst_trans->stride,
178 0, 0,
179 w, h,
180 src_map,
181 src_trans->stride,
182 0,
183 0);
184 }
185
186 pipe->transfer_unmap(pipe, src_trans);
187 pipe->transfer_unmap(pipe, dst_trans);
188
189 pipe->transfer_destroy(pipe, src_trans);
190 pipe->transfer_destroy(pipe, dst_trans);
191 }
192
193
194
195 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
196
197
198 /**
199 * Fallback for pipe->clear_render_target() function.
200 * XXX this looks too hackish to be really useful.
201 * cpp > 4 looks like a gross hack at best...
202 * Plus can't use these transfer fallbacks when clearing
203 * multisampled surfaces for instance.
204 */
205 void
206 util_clear_render_target(struct pipe_context *pipe,
207 struct pipe_surface *dst,
208 const float *rgba,
209 unsigned dstx, unsigned dsty,
210 unsigned width, unsigned height)
211 {
212 struct pipe_transfer *dst_trans;
213 void *dst_map;
214 union util_color uc;
215
216 assert(dst->texture);
217 if (!dst->texture)
218 return;
219
220 dst_trans = pipe_get_transfer(pipe,
221 dst->texture,
222 dst->face,
223 dst->level,
224 dst->zslice,
225 PIPE_TRANSFER_WRITE,
226 dstx, dsty, width, height);
227
228 dst_map = pipe->transfer_map(pipe, dst_trans);
229
230 assert(dst_map);
231
232 if (dst_map) {
233 assert(dst_trans->stride > 0);
234
235 util_pack_color(rgba, dst->texture->format, &uc);
236 util_fill_rect(dst_map, dst->texture->format,
237 dst_trans->stride,
238 0, 0, width, height, &uc);
239 }
240
241 pipe->transfer_unmap(pipe, dst_trans);
242 pipe->transfer_destroy(pipe, dst_trans);
243 }
244
245 /**
246 * Fallback for pipe->clear_stencil() function.
247 * sw fallback doesn't look terribly useful here.
248 * Plus can't use these transfer fallbacks when clearing
249 * multisampled surfaces for instance.
250 */
251 void
252 util_clear_depth_stencil(struct pipe_context *pipe,
253 struct pipe_surface *dst,
254 unsigned clear_flags,
255 double depth,
256 unsigned stencil,
257 unsigned dstx, unsigned dsty,
258 unsigned width, unsigned height)
259 {
260 struct pipe_transfer *dst_trans;
261 ubyte *dst_map;
262 boolean need_rmw = FALSE;
263
264 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) &&
265 ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
266 util_format_is_depth_and_stencil(dst->format))
267 need_rmw = TRUE;
268
269 assert(dst->texture);
270 if (!dst->texture)
271 return;
272 dst_trans = pipe_get_transfer(pipe,
273 dst->texture,
274 dst->face,
275 dst->level,
276 dst->zslice,
277 (need_rmw ? PIPE_TRANSFER_READ_WRITE :
278 PIPE_TRANSFER_WRITE),
279 dstx, dsty, width, height);
280
281 dst_map = pipe->transfer_map(pipe, dst_trans);
282
283 assert(dst_map);
284
285 if (dst_map) {
286 unsigned dst_stride = dst_trans->stride;
287 unsigned zstencil = util_pack_z_stencil(dst->texture->format, depth, stencil);
288 unsigned i, j;
289 assert(dst_trans->stride > 0);
290
291 switch (util_format_get_blocksize(dst->format)) {
292 case 1:
293 assert(dst->format == PIPE_FORMAT_S8_USCALED);
294 if(dst_stride == width)
295 memset(dst_map, (ubyte) zstencil, height * width);
296 else {
297 for (i = 0; i < height; i++) {
298 memset(dst_map, (ubyte) zstencil, width);
299 dst_map += dst_stride;
300 }
301 }
302 break;
303 case 2:
304 assert(dst->format == PIPE_FORMAT_Z16_UNORM);
305 for (i = 0; i < height; i++) {
306 uint16_t *row = (uint16_t *)dst_map;
307 for (j = 0; j < width; j++)
308 *row++ = (uint16_t) zstencil;
309 dst_map += dst_stride;
310 }
311 break;
312 case 4:
313 if (!need_rmw) {
314 for (i = 0; i < height; i++) {
315 uint32_t *row = (uint32_t *)dst_map;
316 for (j = 0; j < width; j++)
317 *row++ = zstencil;
318 dst_map += dst_stride;
319 }
320 }
321 else {
322 uint32_t dst_mask;
323 if (dst->format == PIPE_FORMAT_Z24_UNORM_S8_USCALED)
324 dst_mask = 0xffffff00;
325 else {
326 assert(dst->format == PIPE_FORMAT_S8_USCALED_Z24_UNORM);
327 dst_mask = 0xffffff;
328 }
329 if (clear_flags & PIPE_CLEAR_DEPTH)
330 dst_mask = ~dst_mask;
331 for (i = 0; i < height; i++) {
332 uint32_t *row = (uint32_t *)dst_map;
333 for (j = 0; j < width; j++) {
334 uint32_t tmp = *row & dst_mask;
335 *row++ = tmp | (zstencil & ~dst_mask);
336 }
337 dst_map += dst_stride;
338 }
339 }
340 break;
341 case 8:
342 default:
343 assert(0);
344 break;
345 }
346 }
347
348 pipe->transfer_unmap(pipe, dst_trans);
349 pipe->transfer_destroy(pipe, dst_trans);
350 }