Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / gallium / drivers / cell / ppu / cell_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 /**
29 * Authors
30 * Brian Paul
31 */
32
33 #include <stdio.h>
34 #include <assert.h>
35 #include <stdint.h>
36 #include "pipe/p_inlines.h"
37 #include "util/u_memory.h"
38 #include "util/u_pack_color.h"
39 #include "cell/common.h"
40 #include "cell_clear.h"
41 #include "cell_context.h"
42 #include "cell_batch.h"
43 #include "cell_flush.h"
44 #include "cell_spu.h"
45 #include "cell_state.h"
46
47
48 /**
49 * Convert packed pixel from one format to another.
50 */
51 static unsigned
52 convert_color(enum pipe_format srcFormat, unsigned srcColor,
53 enum pipe_format dstFormat)
54 {
55 ubyte r, g, b, a;
56 unsigned dstColor;
57
58 util_unpack_color_ub(srcFormat, &srcColor, &r, &g, &b, &a);
59 util_pack_color_ub(r, g, b, a, dstFormat, &dstColor);
60
61 return dstColor;
62 }
63
64
65
66 /**
67 * Called via pipe->clear()
68 */
69 void
70 cell_clear_surface(struct pipe_context *pipe, struct pipe_surface *ps,
71 unsigned clearValue)
72 {
73 struct pipe_screen *screen = pipe->screen;
74 struct cell_context *cell = cell_context(pipe);
75 uint surfIndex;
76
77 if (cell->dirty)
78 cell_update_derived(cell);
79
80
81 if (!cell->cbuf_map[0])
82 cell->cbuf_map[0] = screen->surface_map(screen, ps,
83 PIPE_BUFFER_USAGE_GPU_WRITE);
84
85 if (ps == cell->framebuffer.zsbuf) {
86 /* clear z/stencil buffer */
87 surfIndex = 1;
88 }
89 else {
90 /* clear color buffer */
91 surfIndex = 0;
92
93 if (ps->format != PIPE_FORMAT_A8R8G8B8_UNORM) {
94 clearValue = convert_color(PIPE_FORMAT_A8R8G8B8_UNORM, clearValue,
95 ps->format);
96 }
97 }
98
99
100 /* Build a CLEAR command and place it in the current batch buffer */
101 {
102 STATIC_ASSERT(sizeof(struct cell_command_clear_surface) % 16 == 0);
103 struct cell_command_clear_surface *clr
104 = (struct cell_command_clear_surface *)
105 cell_batch_alloc16(cell, sizeof(*clr));
106 clr->opcode[0] = CELL_CMD_CLEAR_SURFACE;
107 clr->surface = surfIndex;
108 clr->value = clearValue;
109 }
110
111 /* Technically, the surface's contents are now known and cleared,
112 * so we could set the status to PIPE_SURFACE_STATUS_CLEAR. But
113 * it turns out it's quite painful to recognize when any particular
114 * surface goes from PIPE_SURFACE_STATUS_CLEAR to
115 * PIPE_SURFACE_STATUS_DEFINED (i.e. with known contents), because
116 * the drawing commands could be operating on numerous draw buffers,
117 * which we'd have to iterate through to set all their stati...
118 * For now, we cheat a bit and set the surface's status to DEFINED
119 * right here. Later we should revisit this and set the status to
120 * CLEAR here, and find a better place to set the status to DEFINED.
121 */
122 ps->status = PIPE_SURFACE_STATUS_DEFINED;
123 }