i915g: make fixup swizzle into a real hw state
[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
56 cbuf_tex = depth_tex = NULL;
57 clear_params = 0;
58
59 if (buffers & PIPE_CLEAR_COLOR) {
60 struct pipe_surface *cbuf = i915->framebuffer.cbufs[0];
61
62 clear_params |= CLEARPARAM_WRITE_COLOR;
63 cbuf_tex = i915_texture(cbuf->texture);
64 util_pack_color(color->f, cbuf->format, &u_color);
65 if (util_format_get_blocksize(cbuf_tex->b.b.format) == 4)
66 clear_color = u_color.ui;
67 else
68 clear_color = (u_color.ui & 0xffff) | (u_color.ui << 16);
69
70 util_pack_color(color->f, cbuf->format, &u_color);
71 clear_color8888 = u_color.ui;
72 } else
73 clear_color = clear_color8888 = 0;
74
75 clear_depth = clear_stencil = 0;
76 if (buffers & PIPE_CLEAR_DEPTH) {
77 struct pipe_surface *zbuf = i915->framebuffer.zsbuf;
78
79 clear_params |= CLEARPARAM_WRITE_DEPTH;
80 depth_tex = i915_texture(zbuf->texture);
81 packed_z_stencil = util_pack_z_stencil(depth_tex->b.b.format, depth, stencil);
82
83 if (util_format_get_blocksize(depth_tex->b.b.format) == 4) {
84 /* Avoid read-modify-write if there's no stencil. */
85 if (buffers & PIPE_CLEAR_STENCIL
86 || depth_tex->b.b.format != PIPE_FORMAT_Z24_UNORM_S8_USCALED) {
87 clear_params |= CLEARPARAM_WRITE_STENCIL;
88 clear_stencil = packed_z_stencil & 0xff;
89 clear_depth = packed_z_stencil;
90 } else
91 clear_depth = packed_z_stencil & 0xffffff00;
92 } else {
93 clear_depth = (clear_depth & 0xffff) | (clear_depth << 16);
94 }
95 }
96
97 if (i915->hardware_dirty)
98 i915_emit_hardware_state(i915);
99
100 if (!BEGIN_BATCH(7 + 7)) {
101 FLUSH_BATCH(NULL);
102
103 i915_emit_hardware_state(i915);
104 i915->vbo_flushed = 1;
105
106 assert(BEGIN_BATCH(7 + 7));
107 }
108
109 OUT_BATCH(_3DSTATE_CLEAR_PARAMETERS);
110 OUT_BATCH(clear_params | CLEARPARAM_CLEAR_RECT);
111 OUT_BATCH(clear_color);
112 OUT_BATCH(clear_depth);
113 OUT_BATCH(clear_color8888);
114 OUT_BATCH_F(f_depth);
115 OUT_BATCH(clear_stencil);
116
117 OUT_BATCH(_3DPRIMITIVE | PRIM3D_CLEAR_RECT | 5);
118 OUT_BATCH_F(destx + width);
119 OUT_BATCH_F(desty + height);
120 OUT_BATCH_F(destx);
121 OUT_BATCH_F(desty + height);
122 OUT_BATCH_F(destx);
123 OUT_BATCH_F(desty);
124
125 /* Flush after clear, its expected to be a costly operation.
126 * This is not required, just a heuristic
127 */
128 FLUSH_BATCH(NULL);
129
130 i915->last_fired_vertices = i915->fired_vertices;
131 i915->fired_vertices = 0;
132 }
133
134 /**
135 * Clear the given buffers to the specified values.
136 * No masking, no scissor (clear entire buffer).
137 */
138 void
139 i915_clear_blitter(struct pipe_context *pipe, unsigned buffers,
140 const union pipe_color_union *color,
141 double depth, unsigned stencil)
142 {
143 util_clear(pipe, &i915_context(pipe)->framebuffer, buffers, color, depth,
144 stencil);
145 }
146
147 void
148 i915_clear_render(struct pipe_context *pipe, unsigned buffers,
149 const union pipe_color_union *color,
150 double depth, unsigned stencil)
151 {
152 struct i915_context *i915 = i915_context(pipe);
153
154 if (i915->dirty)
155 i915_update_derived(i915);
156
157 i915_clear_emit(pipe, buffers, color, depth, stencil,
158 0, 0, i915->framebuffer.width, i915->framebuffer.height);
159 }