i915g: handle seperate stencil clear
[mesa.git] / src / gallium / drivers / i915 / i915_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 /* Authors:
29 * Brian Paul
30 */
31
32
33 #include "util/u_clear.h"
34 #include "util/u_format.h"
35 #include "util/u_pack_color.h"
36 #include "i915_context.h"
37 #include "i915_screen.h"
38 #include "i915_reg.h"
39 #include "i915_batch.h"
40 #include "i915_resource.h"
41 #include "i915_state.h"
42
43 void
44 i915_clear_emit(struct pipe_context *pipe, unsigned buffers,
45 const union pipe_color_union *color,
46 double depth, unsigned stencil,
47 unsigned destx, unsigned desty, unsigned width, unsigned height)
48 {
49 struct i915_context *i915 = i915_context(pipe);
50 uint32_t clear_params, clear_color, clear_depth, clear_stencil,
51 clear_color8888, packed_z_stencil;
52 union util_color u_color;
53 float f_depth = depth;
54 struct i915_texture *cbuf_tex, *depth_tex;
55 int depth_clear_bbp, color_clear_bbp;
56
57 cbuf_tex = depth_tex = NULL;
58 clear_params = 0;
59 depth_clear_bbp = color_clear_bbp = 0;
60
61 if (buffers & PIPE_CLEAR_COLOR) {
62 struct pipe_surface *cbuf = i915->framebuffer.cbufs[0];
63
64 clear_params |= CLEARPARAM_WRITE_COLOR;
65 cbuf_tex = i915_texture(cbuf->texture);
66
67 util_pack_color(color->f, cbuf->format, &u_color);
68 if (util_format_get_blocksize(cbuf_tex->b.b.format) == 4) {
69 clear_color = u_color.ui;
70 color_clear_bbp = 32;
71 } else {
72 clear_color = (u_color.ui & 0xffff) | (u_color.ui << 16);
73 color_clear_bbp = 16;
74 }
75
76 /* correctly swizzle clear value */
77 if (i915->current.need_target_fixup)
78 util_pack_color(color->f, cbuf->format, &u_color);
79 else
80 util_pack_color(color->f, PIPE_FORMAT_B8G8R8A8_UNORM, &u_color);
81 clear_color8888 = u_color.ui;
82 } else
83 clear_color = clear_color8888 = 0;
84
85 clear_depth = clear_stencil = 0;
86 if (buffers & PIPE_CLEAR_DEPTH) {
87 struct pipe_surface *zbuf = i915->framebuffer.zsbuf;
88
89 clear_params |= CLEARPARAM_WRITE_DEPTH;
90 depth_tex = i915_texture(zbuf->texture);
91 packed_z_stencil = util_pack_z_stencil(depth_tex->b.b.format, depth, stencil);
92
93 if (util_format_get_blocksize(depth_tex->b.b.format) == 4) {
94 /* Avoid read-modify-write if there's no stencil. */
95 if (buffers & PIPE_CLEAR_STENCIL
96 || depth_tex->b.b.format != PIPE_FORMAT_Z24_UNORM_S8_USCALED) {
97 clear_params |= CLEARPARAM_WRITE_STENCIL;
98 clear_stencil = packed_z_stencil & 0xff;
99 clear_depth = packed_z_stencil;
100 } else
101 clear_depth = packed_z_stencil & 0xffffff00;
102
103 depth_clear_bbp = 32;
104 } else {
105 clear_depth = (packed_z_stencil & 0xffff) | (packed_z_stencil << 16);
106 depth_clear_bbp = 16;
107 }
108 } else if (buffers & PIPE_CLEAR_DEPTH) {
109 struct pipe_surface *zbuf = i915->framebuffer.zsbuf;
110
111 clear_params |= CLEARPARAM_WRITE_STENCIL;
112 depth_tex = i915_texture(zbuf->texture);
113 assert(depth_tex->b.b.format == PIPE_FORMAT_Z24_UNORM_S8_USCALED);
114
115 packed_z_stencil = util_pack_z_stencil(depth_tex->b.b.format, depth, stencil);
116 depth_clear_bbp = 32;
117 clear_stencil = packed_z_stencil & 0xff;
118 }
119
120 /* hw can't fastclear both depth and color if their bbp mismatch. */
121 if (color_clear_bbp && depth_clear_bbp
122 && color_clear_bbp != depth_clear_bbp) {
123 if (i915->hardware_dirty)
124 i915_emit_hardware_state(i915);
125
126 if (!BEGIN_BATCH(1 + 2*(7 + 7))) {
127 FLUSH_BATCH(NULL);
128
129 i915_emit_hardware_state(i915);
130 i915->vbo_flushed = 1;
131
132 assert(BEGIN_BATCH(1 + 2*(7 + 7)));
133 }
134
135 OUT_BATCH(_3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT);
136
137 OUT_BATCH(_3DSTATE_CLEAR_PARAMETERS);
138 OUT_BATCH(CLEARPARAM_WRITE_COLOR | CLEARPARAM_CLEAR_RECT);
139 /* Used for zone init prim */
140 OUT_BATCH(clear_color);
141 OUT_BATCH(clear_depth);
142 /* Used for clear rect prim */
143 OUT_BATCH(clear_color8888);
144 OUT_BATCH_F(f_depth);
145 OUT_BATCH(clear_stencil);
146
147 OUT_BATCH(_3DPRIMITIVE | PRIM3D_CLEAR_RECT | 5);
148 OUT_BATCH_F(destx + width);
149 OUT_BATCH_F(desty + height);
150 OUT_BATCH_F(destx);
151 OUT_BATCH_F(desty + height);
152 OUT_BATCH_F(destx);
153 OUT_BATCH_F(desty);
154
155 OUT_BATCH(_3DSTATE_CLEAR_PARAMETERS);
156 OUT_BATCH((clear_params & ~CLEARPARAM_WRITE_COLOR) |
157 CLEARPARAM_CLEAR_RECT);
158 /* Used for zone init prim */
159 /* Used for zone init prim */
160 OUT_BATCH(clear_color);
161 OUT_BATCH(clear_depth);
162 /* Used for clear rect prim */
163 OUT_BATCH(clear_color8888);
164 OUT_BATCH_F(f_depth);
165 OUT_BATCH(clear_stencil);
166
167 OUT_BATCH(_3DPRIMITIVE | PRIM3D_CLEAR_RECT | 5);
168 OUT_BATCH_F(destx + width);
169 OUT_BATCH_F(desty + height);
170 OUT_BATCH_F(destx);
171 OUT_BATCH_F(desty + height);
172 OUT_BATCH_F(destx);
173 OUT_BATCH_F(desty);
174 } else {
175 if (i915->hardware_dirty)
176 i915_emit_hardware_state(i915);
177
178 if (!BEGIN_BATCH(1 + 7 + 7)) {
179 FLUSH_BATCH(NULL);
180
181 i915_emit_hardware_state(i915);
182 i915->vbo_flushed = 1;
183
184 assert(BEGIN_BATCH(1 + 7 + 7));
185 }
186
187 OUT_BATCH(_3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT);
188
189 OUT_BATCH(_3DSTATE_CLEAR_PARAMETERS);
190 OUT_BATCH(clear_params | CLEARPARAM_CLEAR_RECT);
191 /* Used for zone init prim */
192 OUT_BATCH(clear_color);
193 OUT_BATCH(clear_depth);
194 /* Used for clear rect prim */
195 OUT_BATCH(clear_color8888);
196 OUT_BATCH_F(f_depth);
197 OUT_BATCH(clear_stencil);
198
199 OUT_BATCH(_3DPRIMITIVE | PRIM3D_CLEAR_RECT | 5);
200 OUT_BATCH_F(destx + width);
201 OUT_BATCH_F(desty + height);
202 OUT_BATCH_F(destx);
203 OUT_BATCH_F(desty + height);
204 OUT_BATCH_F(destx);
205 OUT_BATCH_F(desty);
206 }
207
208 /* Flush after clear, its expected to be a costly operation.
209 * This is not required, just a heuristic, but without the flush we'd need to
210 * clobber the SCISSOR_ENABLE dynamic state. */
211 FLUSH_BATCH(NULL);
212
213 i915->last_fired_vertices = i915->fired_vertices;
214 i915->fired_vertices = 0;
215 }
216
217 /**
218 * Clear the given buffers to the specified values.
219 * No masking, no scissor (clear entire buffer).
220 */
221 void
222 i915_clear_blitter(struct pipe_context *pipe, unsigned buffers,
223 const union pipe_color_union *color,
224 double depth, unsigned stencil)
225 {
226 util_clear(pipe, &i915_context(pipe)->framebuffer, buffers, color, depth,
227 stencil);
228 }
229
230 void
231 i915_clear_render(struct pipe_context *pipe, unsigned buffers,
232 const union pipe_color_union *color,
233 double depth, unsigned stencil)
234 {
235 struct i915_context *i915 = i915_context(pipe);
236
237 if (i915->dirty)
238 i915_update_derived(i915);
239
240 i915_clear_emit(pipe, buffers, color, depth, stencil,
241 0, 0, i915->framebuffer.width, i915->framebuffer.height);
242 }