util/u_surface: Support 3D/array textures in util_resource_copy_region().
[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 * Initialize a pipe_surface object. 'view' is considered to have
48 * uninitialized contents.
49 */
50 void
51 u_surface_default_template(struct pipe_surface *surf,
52 const struct pipe_resource *texture,
53 unsigned bind)
54 {
55 memset(surf, 0, sizeof(*surf));
56
57 surf->format = texture->format;
58 /* XXX should filter out all non-rt/ds bind flags ? */
59 surf->usage = bind;
60 }
61
62 /**
63 * Helper to quickly create an RGBA rendering surface of a certain size.
64 * \param textureOut returns the new texture
65 * \param surfaceOut returns the new surface
66 * \return TRUE for success, FALSE if failure
67 */
68 boolean
69 util_create_rgba_texture(struct pipe_context *pipe,
70 uint width, uint height, uint bind,
71 struct pipe_resource **textureOut)
72 {
73 static const enum pipe_format rgbaFormats[] = {
74 PIPE_FORMAT_B8G8R8A8_UNORM,
75 PIPE_FORMAT_A8R8G8B8_UNORM,
76 PIPE_FORMAT_A8B8G8R8_UNORM,
77 PIPE_FORMAT_NONE
78 };
79 const uint target = PIPE_TEXTURE_2D;
80 enum pipe_format format = PIPE_FORMAT_NONE;
81 struct pipe_resource templ;
82 struct pipe_surface surf_templ;
83 struct pipe_screen *screen = pipe->screen;
84 uint i;
85
86 /* Choose surface format */
87 for (i = 0; rgbaFormats[i]; i++) {
88 if (screen->is_format_supported(screen, rgbaFormats[i],
89 target, 0, bind)) {
90 format = rgbaFormats[i];
91 break;
92 }
93 }
94 if (format == PIPE_FORMAT_NONE)
95 return FALSE; /* unable to get an rgba format!?! */
96
97 /* create texture */
98 memset(&templ, 0, sizeof(templ));
99 templ.target = target;
100 templ.format = format;
101 templ.last_level = 0;
102 templ.width0 = width;
103 templ.height0 = height;
104 templ.depth0 = 1;
105 templ.array_size = 1;
106 templ.bind = bind;
107
108 *textureOut = screen->resource_create(screen, &templ);
109 if (!*textureOut)
110 return FALSE;
111
112 /* create surface */
113 u_surface_default_template(&surf_templ, *textureOut, bind);
114 return TRUE;
115 }
116
117
118 /**
119 * Fallback function for pipe->resource_copy_region().
120 * Note: (X,Y)=(0,0) is always the upper-left corner.
121 */
122 void
123 util_resource_copy_region(struct pipe_context *pipe,
124 struct pipe_resource *dst,
125 unsigned dst_level,
126 unsigned dst_x, unsigned dst_y, unsigned dst_z,
127 struct pipe_resource *src,
128 unsigned src_level,
129 const struct pipe_box *src_box)
130 {
131 struct pipe_transfer *src_trans, *dst_trans;
132 uint8_t *dst_map;
133 const uint8_t *src_map;
134 enum pipe_format src_format, dst_format;
135 struct pipe_box dst_box;
136 int z;
137
138 assert(src && dst);
139 if (!src || !dst)
140 return;
141
142 assert((src->target == PIPE_BUFFER && dst->target == PIPE_BUFFER) ||
143 (src->target != PIPE_BUFFER && dst->target != PIPE_BUFFER));
144
145 src_format = src->format;
146 dst_format = dst->format;
147
148 assert(util_format_get_blocksize(dst_format) == util_format_get_blocksize(src_format));
149 assert(util_format_get_blockwidth(dst_format) == util_format_get_blockwidth(src_format));
150 assert(util_format_get_blockheight(dst_format) == util_format_get_blockheight(src_format));
151
152 src_map = pipe->transfer_map(pipe,
153 src,
154 src_level,
155 PIPE_TRANSFER_READ,
156 src_box, &src_trans);
157 assert(src_map);
158 if (!src_map) {
159 goto no_src_map;
160 }
161
162 dst_box.x = dst_x;
163 dst_box.y = dst_y;
164 dst_box.z = dst_z;
165 dst_box.width = src_box->width;
166 dst_box.height = src_box->height;
167 dst_box.depth = src_box->depth;
168
169 dst_map = pipe->transfer_map(pipe,
170 dst,
171 dst_level,
172 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE,
173 &dst_box, &dst_trans);
174 assert(dst_map);
175 if (!dst_map) {
176 goto no_dst_map;
177 }
178
179 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
180 assert(src_box->height == 1);
181 assert(src_box->depth == 1);
182 memcpy(dst_map, src_map, src_box->width);
183 } else {
184 for (z = 0; z < src_box->depth; ++z) {
185 util_copy_rect(dst_map,
186 dst_format,
187 dst_trans->stride,
188 0, 0,
189 src_box->width, src_box->height,
190 src_map,
191 src_trans->stride,
192 0, 0);
193
194 dst_map += dst_trans->layer_stride;
195 src_map += src_trans->layer_stride;
196 }
197 }
198
199 pipe->transfer_unmap(pipe, dst_trans);
200 no_dst_map:
201 pipe->transfer_unmap(pipe, src_trans);
202 no_src_map:
203 ;
204 }
205
206
207
208 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
209
210
211 /**
212 * Fallback for pipe->clear_render_target() function.
213 * XXX this looks too hackish to be really useful.
214 * cpp > 4 looks like a gross hack at best...
215 * Plus can't use these transfer fallbacks when clearing
216 * multisampled surfaces for instance.
217 */
218 void
219 util_clear_render_target(struct pipe_context *pipe,
220 struct pipe_surface *dst,
221 const union pipe_color_union *color,
222 unsigned dstx, unsigned dsty,
223 unsigned width, unsigned height)
224 {
225 struct pipe_transfer *dst_trans;
226 void *dst_map;
227 union util_color uc;
228
229 assert(dst->texture);
230 if (!dst->texture)
231 return;
232 /* XXX: should handle multiple layers */
233 dst_map = pipe_transfer_map(pipe,
234 dst->texture,
235 dst->u.tex.level,
236 dst->u.tex.first_layer,
237 PIPE_TRANSFER_WRITE,
238 dstx, dsty, width, height, &dst_trans);
239
240 assert(dst_map);
241
242 if (dst_map) {
243 assert(dst_trans->stride > 0);
244
245 util_pack_color(color->f, dst->texture->format, &uc);
246 util_fill_rect(dst_map, dst->texture->format,
247 dst_trans->stride,
248 0, 0, width, height, &uc);
249
250 pipe->transfer_unmap(pipe, dst_trans);
251 }
252 }
253
254 /**
255 * Fallback for pipe->clear_stencil() function.
256 * sw fallback doesn't look terribly useful here.
257 * Plus can't use these transfer fallbacks when clearing
258 * multisampled surfaces for instance.
259 */
260 void
261 util_clear_depth_stencil(struct pipe_context *pipe,
262 struct pipe_surface *dst,
263 unsigned clear_flags,
264 double depth,
265 unsigned stencil,
266 unsigned dstx, unsigned dsty,
267 unsigned width, unsigned height)
268 {
269 struct pipe_transfer *dst_trans;
270 ubyte *dst_map;
271 boolean need_rmw = FALSE;
272
273 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) &&
274 ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
275 util_format_is_depth_and_stencil(dst->format))
276 need_rmw = TRUE;
277
278 assert(dst->texture);
279 if (!dst->texture)
280 return;
281 dst_map = pipe_transfer_map(pipe,
282 dst->texture,
283 dst->u.tex.level,
284 dst->u.tex.first_layer,
285 (need_rmw ? PIPE_TRANSFER_READ_WRITE :
286 PIPE_TRANSFER_WRITE),
287 dstx, dsty, width, height, &dst_trans);
288 assert(dst_map);
289
290 if (dst_map) {
291 unsigned dst_stride = dst_trans->stride;
292 uint64_t zstencil = util_pack64_z_stencil(dst->texture->format,
293 depth, stencil);
294 unsigned i, j;
295 assert(dst_trans->stride > 0);
296
297 switch (util_format_get_blocksize(dst->format)) {
298 case 1:
299 assert(dst->format == PIPE_FORMAT_S8_UINT);
300 if(dst_stride == width)
301 memset(dst_map, (uint8_t) zstencil, height * width);
302 else {
303 for (i = 0; i < height; i++) {
304 memset(dst_map, (uint8_t) zstencil, width);
305 dst_map += dst_stride;
306 }
307 }
308 break;
309 case 2:
310 assert(dst->format == PIPE_FORMAT_Z16_UNORM);
311 for (i = 0; i < height; i++) {
312 uint16_t *row = (uint16_t *)dst_map;
313 for (j = 0; j < width; j++)
314 *row++ = (uint16_t) zstencil;
315 dst_map += dst_stride;
316 }
317 break;
318 case 4:
319 if (!need_rmw) {
320 for (i = 0; i < height; i++) {
321 uint32_t *row = (uint32_t *)dst_map;
322 for (j = 0; j < width; j++)
323 *row++ = (uint32_t) zstencil;
324 dst_map += dst_stride;
325 }
326 }
327 else {
328 uint32_t dst_mask;
329 if (dst->format == PIPE_FORMAT_Z24_UNORM_S8_UINT)
330 dst_mask = 0xffffff00;
331 else {
332 assert(dst->format == PIPE_FORMAT_S8_UINT_Z24_UNORM);
333 dst_mask = 0xffffff;
334 }
335 if (clear_flags & PIPE_CLEAR_DEPTH)
336 dst_mask = ~dst_mask;
337 for (i = 0; i < height; i++) {
338 uint32_t *row = (uint32_t *)dst_map;
339 for (j = 0; j < width; j++) {
340 uint32_t tmp = *row & dst_mask;
341 *row++ = tmp | ((uint32_t) zstencil & ~dst_mask);
342 }
343 dst_map += dst_stride;
344 }
345 }
346 break;
347 case 8:
348 if (!need_rmw) {
349 for (i = 0; i < height; i++) {
350 uint64_t *row = (uint64_t *)dst_map;
351 for (j = 0; j < width; j++)
352 *row++ = zstencil;
353 dst_map += dst_stride;
354 }
355 }
356 else {
357 uint64_t src_mask;
358
359 if (clear_flags & PIPE_CLEAR_DEPTH)
360 src_mask = 0x00000000ffffffffull;
361 else
362 src_mask = 0x000000ff00000000ull;
363
364 for (i = 0; i < height; i++) {
365 uint64_t *row = (uint64_t *)dst_map;
366 for (j = 0; j < width; j++) {
367 uint64_t tmp = *row & ~src_mask;
368 *row++ = tmp | (zstencil & src_mask);
369 }
370 dst_map += dst_stride;
371 }
372 }
373 break;
374 default:
375 assert(0);
376 break;
377 }
378
379 pipe->transfer_unmap(pipe, dst_trans);
380 }
381 }