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