Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / drivers / softpipe / sp_clear.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /* Author:
29 * Brian Paul
30 */
31
32
33 #include "pipe/p_defines.h"
34 #include "util/u_pack_color.h"
35 #include "sp_clear.h"
36 #include "sp_context.h"
37 #include "sp_surface.h"
38 #include "sp_state.h"
39 #include "sp_tile_cache.h"
40
41
42 /**
43 * Convert packed pixel from one format to another.
44 */
45 static unsigned
46 convert_color(enum pipe_format srcFormat, unsigned srcColor,
47 enum pipe_format dstFormat)
48 {
49 ubyte r, g, b, a;
50 unsigned dstColor;
51
52 util_unpack_color_ub(srcFormat, &srcColor, &r, &g, &b, &a);
53 util_pack_color_ub(r, g, b, a, dstFormat, &dstColor);
54
55 return dstColor;
56 }
57
58
59
60 /**
61 * Clear the given surface to the specified value.
62 * No masking, no scissor (clear entire buffer).
63 * Note: when clearing a color buffer, the clearValue is always
64 * encoded as PIPE_FORMAT_A8R8G8B8_UNORM.
65 */
66 void
67 softpipe_clear(struct pipe_context *pipe, struct pipe_surface *ps,
68 unsigned clearValue)
69 {
70 struct softpipe_context *softpipe = softpipe_context(pipe);
71 uint i;
72
73 if (softpipe->no_rast)
74 return;
75
76 #if 0
77 softpipe_update_derived(softpipe); /* not needed?? */
78 #endif
79
80 if (ps == sp_tile_cache_get_surface(softpipe->zsbuf_cache)) {
81 sp_tile_cache_clear(softpipe->zsbuf_cache, clearValue);
82 softpipe->framebuffer.zsbuf->status = PIPE_SURFACE_STATUS_CLEAR;
83 #if TILE_CLEAR_OPTIMIZATION
84 return;
85 #endif
86 }
87
88 for (i = 0; i < softpipe->framebuffer.num_cbufs; i++) {
89 if (ps == sp_tile_cache_get_surface(softpipe->cbuf_cache[i])) {
90 unsigned cv;
91 if (ps->format != PIPE_FORMAT_A8R8G8B8_UNORM) {
92 cv = convert_color(PIPE_FORMAT_A8R8G8B8_UNORM, clearValue,
93 ps->format);
94 }
95 else {
96 cv = clearValue;
97 }
98 sp_tile_cache_clear(softpipe->cbuf_cache[i], cv);
99 softpipe->framebuffer.cbufs[i]->status = PIPE_SURFACE_STATUS_CLEAR;
100 }
101 }
102
103 #if !TILE_CLEAR_OPTIMIZATION
104 /* non-cached surface */
105 pipe->surface_fill(pipe, ps, 0, 0, ps->width, ps->height, clearValue);
106 #endif
107 }