r300-gallium: Moar vert shader emit.
[mesa.git] / src / gallium / drivers / r300 / r300_surface.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Joakim Sindholt <opensource@zhasha.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 #include "r300_surface.h"
25
26 static void r300_surface_setup(struct pipe_context* pipe,
27 struct pipe_surface* dest,
28 unsigned x, unsigned y,
29 unsigned w, unsigned h)
30 {
31 struct r300_context* r300 = r300_context(pipe);
32 CS_LOCALS(r300);
33 struct r300_capabilities* caps = r300_screen(pipe->screen)->caps;
34 struct r300_texture* tex = (struct r300_texture*)dest->texture;
35 unsigned pixpitch = tex->stride / tex->tex.block.size;
36
37 r300_emit_blend_state(r300, &blend_clear_state);
38 r300_emit_blend_color_state(r300, &blend_color_clear_state);
39 r300_emit_dsa_state(r300, &dsa_clear_state);
40 r300_emit_rs_state(r300, &rs_clear_state);
41
42 /* XXX these magic numbers should be explained when
43 * this becomes a cached state object */
44 if (caps->has_tcl) {
45 r300_emit_vertex_shader(r300, &r300_passthrough_vertex_shader);
46 } else {
47 OUT_CS_REG(R300_VAP_CNTL, R300_PVS_NUM_SLOTS(5) |
48 R300_PVS_NUM_CNTLRS(5) |
49 R300_PVS_NUM_FPUS(caps->num_vert_fpus) |
50 R300_PVS_VF_MAX_VTX_NUM(12));
51 }
52
53 BEGIN_CS(15);
54
55 /* Pixel scissors. */
56 OUT_CS_REG_SEQ(R300_SC_SCISSORS_TL, 2);
57 if (caps->is_r500) {
58 OUT_CS((x << R300_SCISSORS_X_SHIFT) | (y << R300_SCISSORS_Y_SHIFT));
59 OUT_CS((w << R300_SCISSORS_X_SHIFT) | (h << R300_SCISSORS_Y_SHIFT));
60 } else {
61 /* Non-R500 chipsets have an offset of 1440 in their scissors. */
62 OUT_CS(((x + 1440) << R300_SCISSORS_X_SHIFT) |
63 ((y + 1440) << R300_SCISSORS_Y_SHIFT));
64 OUT_CS(((w + 1440) << R300_SCISSORS_X_SHIFT) |
65 ((h + 1440) << R300_SCISSORS_Y_SHIFT));
66 }
67
68 /* Flush colorbuffer and blend caches. */
69 OUT_CS_REG(R300_RB3D_DSTCACHE_CTLSTAT,
70 R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D |
71 R300_RB3D_DSTCACHE_CTLSTAT_DC_FINISH_SIGNAL);
72 OUT_CS_REG(R300_ZB_ZCACHE_CTLSTAT,
73 R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE |
74 R300_ZB_ZCACHE_CTLSTAT_ZC_FREE_FREE);
75
76 /* Setup colorbuffer. */
77 OUT_CS_REG_SEQ(R300_RB3D_COLOROFFSET0, 1);
78 OUT_CS_RELOC(tex->buffer, 0, 0, RADEON_GEM_DOMAIN_VRAM, 0);
79 OUT_CS_REG(R300_RB3D_COLORPITCH0, pixpitch |
80 r300_translate_colorformat(tex->tex.format));
81 OUT_CS_REG(RB3D_COLOR_CHANNEL_MASK, 0xf);
82
83 END_CS;
84 }
85
86 /* Provides pipe_context's "surface_fill". Commonly used for clearing
87 * buffers. */
88 static void r300_surface_fill(struct pipe_context* pipe,
89 struct pipe_surface* dest,
90 unsigned x, unsigned y,
91 unsigned w, unsigned h,
92 unsigned color)
93 {
94 struct r300_context* r300 = r300_context(pipe);
95 CS_LOCALS(r300);
96 struct r300_capabilities* caps = r300_screen(pipe->screen)->caps;
97 struct r300_texture* tex = (struct r300_texture*)dest->texture;
98 int i;
99 float r, g, b, a, depth;
100 unsigned pixpitch = tex->stride / tex->tex.block.size;
101
102 a = (float)((color >> 24) & 0xff) / 255.0f;
103 r = (float)((color >> 16) & 0xff) / 255.0f;
104 g = (float)((color >> 8) & 0xff) / 255.0f;
105 b = (float)((color >> 0) & 0xff) / 255.0f;
106 debug_printf("r300: Filling surface %p at (%d,%d),"
107 " dimensions %dx%d (pixel pitch %d), color 0x%x\n",
108 dest, x, y, w, h, pixpitch, color);
109
110 /* Fallback? */
111 if (FALSE) {
112 debug_printf("r300: Falling back on surface clear...");
113 util_surface_fill(pipe, dest, x, y, w, h, color);
114 return;
115 }
116
117 r300_surface_setup(r300, dest, x, y, w, h);
118
119 /* Fragment shader setup */
120 if (caps->is_r500) {
121 r500_emit_fragment_shader(r300, &r500_passthrough_fragment_shader);
122 r300_emit_rs_block_state(r300, &r500_rs_block_clear_state);
123 } else {
124 r300_emit_fragment_shader(r300, &r300_passthrough_fragment_shader);
125 r300_emit_rs_block_state(r300, &r300_rs_block_clear_state);
126 }
127
128 BEGIN_CS(31);
129
130 /* VAP stream control, mapping from input memory to PVS/RS memory */
131 if (caps->has_tcl) {
132 OUT_CS_REG(R300_VAP_PROG_STREAM_CNTL_0,
133 (R300_DATA_TYPE_FLOAT_4 << R300_DATA_TYPE_0_SHIFT) |
134 ((R300_LAST_VEC | (1 << R300_DST_VEC_LOC_SHIFT) |
135 R300_DATA_TYPE_FLOAT_4) << R300_DATA_TYPE_1_SHIFT));
136 } else {
137 OUT_CS_REG(R300_VAP_PROG_STREAM_CNTL_0,
138 (R300_DATA_TYPE_FLOAT_4 << R300_DATA_TYPE_0_SHIFT) |
139 ((R300_LAST_VEC | (2 << R300_DST_VEC_LOC_SHIFT) |
140 R300_DATA_TYPE_FLOAT_4) << R300_DATA_TYPE_1_SHIFT));
141 }
142 OUT_CS_REG(R300_VAP_PROG_STREAM_CNTL_EXT_0,
143 (R300_VAP_SWIZZLE_XYZW << R300_SWIZZLE0_SHIFT) |
144 (R300_VAP_SWIZZLE_XYZW << R300_SWIZZLE1_SHIFT));
145
146 /* VAP format controls */
147 OUT_CS_REG(R300_VAP_OUTPUT_VTX_FMT_0,
148 R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT |
149 R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT);
150 OUT_CS_REG(R300_VAP_OUTPUT_VTX_FMT_1, 0x0);
151
152 /* Disable textures */
153 OUT_CS_REG(R300_TX_ENABLE, 0x0);
154
155 /* Viewport setup */
156 OUT_CS_REG_SEQ(R300_SE_VPORT_XSCALE, 6);
157 OUT_CS_32F(1.0);
158 OUT_CS_32F((float)x);
159 OUT_CS_32F(1.0);
160 OUT_CS_32F((float)y);
161 OUT_CS_32F(1.0);
162 OUT_CS_32F(0.0);
163
164 /* The size of the point we're about to draw, in sixths of pixels */
165 OUT_CS_REG(R300_GA_POINT_SIZE,
166 ((h * 6) & R300_POINTSIZE_Y_MASK) |
167 ((w * 6) << R300_POINTSIZE_X_SHIFT));
168
169 /* Packet3 with our point vertex */
170 OUT_CS_PKT3(R200_3D_DRAW_IMMD_2, 8);
171 OUT_CS(R300_PRIM_TYPE_POINT | R300_PRIM_WALK_RING |
172 (1 << R300_PRIM_NUM_VERTICES_SHIFT));
173 /* Position */
174 OUT_CS_32F(w / 2.0);
175 OUT_CS_32F(h / 2.0);
176 OUT_CS_32F(1.0);
177 OUT_CS_32F(1.0);
178 /* Color */
179 OUT_CS_32F(r);
180 OUT_CS_32F(g);
181 OUT_CS_32F(b);
182 OUT_CS_32F(a);
183
184 /* XXX figure out why this is 0xA and not 0x2 */
185 OUT_CS_REG(R300_RB3D_DSTCACHE_CTLSTAT, 0xA);
186 /* XXX OUT_CS_REG(R300_ZB_ZCACHE_CTLSTAT,
187 R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE |
188 R300_ZB_ZCACHE_CTLSTAT_ZC_FREE_FREE); */
189
190 END_CS;
191
192 r300->dirty_hw++;
193 }
194
195 static void r300_surface_copy(struct pipe_context* pipe,
196 struct pipe_surface* dest,
197 unsigned destx, unsigned desty,
198 struct pipe_surface* src,
199 unsigned srcx, unsigned srcy,
200 unsigned w, unsigned h)
201 {
202 struct r300_context* r300 = r300_context(pipe);
203 CS_LOCALS(r300);
204 struct r300_capabilities* caps = r300_screen(pipe->screen)->caps;
205 struct r300_texture* srctex = (struct r300_texture*)src->texture;
206 struct r300_texture* desttex = (struct r300_texture*)dest->texture;
207
208 unsigned pixpitch = srctex->stride / srctex->tex.block.size;
209 debug_printf("r300: Copying surface %p at (%d,%d) to %p at (%d, %d),"
210 " dimensions %dx%d (pixel pitch %d)\n",
211 src, srcx, srcy, dest, destx, desty, w, h, pixpitch);
212
213 if ((srctex == desttex) &&
214 ((destx < srcx + w) || (srcx < destx + w)) &&
215 ((desty < srcy + h) || (srcy < desty + h))) {
216 debug_printf("r300: Falling back on surface_copy\n");
217 util_surface_copy(pipe, FALSE, dest, destx, desty, src,
218 srcx, srcy, w, h);
219 }
220
221 r300_emit_sampler(r300, &r300_sampler_copy_state, 0);
222 r300_emit_texture(r300, srctex, 0);
223 r300_flush_textures(r300);
224
225 /* Fragment shader setup */
226 if (caps->is_r500) {
227 r500_emit_fragment_shader(r300, &r500_texture_fragment_shader);
228 r300_emit_rs_block_state(r300, &r500_rs_block_copy_state);
229 } else {
230 r300_emit_fragment_shader(r300, &r300_texture_fragment_shader);
231 r300_emit_rs_block_state(r300, &r300_rs_block_copy_state);
232 }
233
234 /* VAP stream control, mapping from input memory to PVS/RS memory */
235 if (caps->has_tcl) {
236 OUT_CS_REG(R300_VAP_PROG_STREAM_CNTL_0,
237 (R300_DATA_TYPE_FLOAT_2 << R300_DATA_TYPE_0_SHIFT) |
238 ((R300_LAST_VEC | (1 << R300_DST_VEC_LOC_SHIFT) |
239 R300_DATA_TYPE_FLOAT_2) << R300_DATA_TYPE_1_SHIFT));
240 } else {
241 OUT_CS_REG(R300_VAP_PROG_STREAM_CNTL_0,
242 (R300_DATA_TYPE_FLOAT_2 << R300_DATA_TYPE_0_SHIFT) |
243 ((R300_LAST_VEC | (6 << R300_DST_VEC_LOC_SHIFT) |
244 R300_DATA_TYPE_FLOAT_2) << R300_DATA_TYPE_1_SHIFT));
245 }
246 OUT_CS_REG(R300_VAP_PROG_STREAM_CNTL_EXT_0,
247 (R300_VAP_SWIZZLE_XYZW << R300_SWIZZLE0_SHIFT) |
248 (R300_VAP_SWIZZLE_XYZW << R300_SWIZZLE1_SHIFT));
249
250 /* VAP format controls */
251 OUT_CS_REG(R300_VAP_OUTPUT_VTX_FMT_0,
252 R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT);
253 /* Two components of texture 0 */
254 OUT_CS_REG(R300_VAP_OUTPUT_VTX_FMT_1, 0x2);
255
256 /* Packet3 with our texcoords */
257 OUT_CS_PKT3(R200_3D_DRAW_IMMD_2, 8);
258 OUT_CS(R300_PRIM_TYPE_QUADS | R300_PRIM_WALK_RING |
259 (4 << R300_PRIM_NUM_VERTICES_SHIFT));
260 /* (x , y ) */
261 OUT_CS_32F((float)destx);
262 OUT_CS_32F((float)desty);
263 OUT_CS_32F((float)srcx);
264 OUT_CS_32F((float)srcy);
265 /* (x , y + h) */
266 OUT_CS_32F((float)destx);
267 OUT_CS_32F((float)(desty + h));
268 OUT_CS_32F((float)srcx);
269 OUT_CS_32F((float)(srcy + h));
270 /* (x + w, y + h) */
271 OUT_CS_32F((float)(destx + w));
272 OUT_CS_32F((float)(desty + h));
273 OUT_CS_32F((float)(srcx + w));
274 OUT_CS_32F((float)(srcy + h));
275 /* (x + w, y ) */
276 OUT_CS_32F((float)(destx + w));
277 OUT_CS_32F((float)desty);
278 OUT_CS_32F((float)(srcx + w));
279 OUT_CS_32F((float)srcy);
280
281 OUT_CS_REG(R300_RB3D_DSTCACHE_CTLSTAT, 0xA);
282
283 r300->dirty_hw++;
284 }
285
286 void r300_init_surface_functions(struct r300_context* r300)
287 {
288 r300->context.surface_fill = r300_surface_fill;
289 r300->context.surface_copy = r300_surface_copy;
290 }