2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Joakim Sindholt <opensource@zhasha.com>
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:
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
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. */
24 #include "r300_surface.h"
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
)
31 struct r300_context
* r300
= r300_context(pipe
);
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
;
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
);
42 /* XXX these magic numbers should be explained when
43 * this becomes a cached state object */
45 r300_emit_vertex_shader(r300
, &r300_passthrough_vertex_shader
);
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));
56 OUT_CS_REG_SEQ(R300_SC_SCISSORS_TL
, 2);
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
));
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
));
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
);
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);
86 /* Provides pipe_context's "surface_fill". Commonly used for clearing
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
,
94 struct r300_context
* r300
= r300_context(pipe
);
96 struct r300_capabilities
* caps
= r300_screen(pipe
->screen
)->caps
;
97 struct r300_texture
* tex
= (struct r300_texture
*)dest
->texture
;
99 float r
, g
, b
, a
, depth
;
100 unsigned pixpitch
= tex
->stride
/ tex
->tex
.block
.size
;
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
);
112 debug_printf("r300: Falling back on surface clear...");
113 util_surface_fill(pipe
, dest
, x
, y
, w
, h
, color
);
117 r300_surface_setup(r300
, dest
, x
, y
, w
, h
);
119 /* Fragment shader setup */
121 r500_emit_fragment_shader(r300
, &r500_passthrough_fragment_shader
);
122 r300_emit_rs_block_state(r300
, &r500_rs_block_clear_state
);
124 r300_emit_fragment_shader(r300
, &r300_passthrough_fragment_shader
);
125 r300_emit_rs_block_state(r300
, &r300_rs_block_clear_state
);
130 /* VAP stream control, mapping from input memory to PVS/RS memory */
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
));
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
));
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
));
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);
152 /* Disable textures */
153 OUT_CS_REG(R300_TX_ENABLE
, 0x0);
156 OUT_CS_REG_SEQ(R300_SE_VPORT_XSCALE
, 6);
158 OUT_CS_32F((float)x
);
160 OUT_CS_32F((float)y
);
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
));
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
));
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); */
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
)
202 struct r300_context
* r300
= r300_context(pipe
);
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
;
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
);
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
,
221 r300_emit_sampler(r300
, &r300_sampler_copy_state
, 0);
222 r300_emit_texture(r300
, srctex
, 0);
223 r300_flush_textures(r300
);
225 /* Fragment shader setup */
227 r500_emit_fragment_shader(r300
, &r500_texture_fragment_shader
);
228 r300_emit_rs_block_state(r300
, &r500_rs_block_copy_state
);
230 r300_emit_fragment_shader(r300
, &r300_texture_fragment_shader
);
231 r300_emit_rs_block_state(r300
, &r300_rs_block_copy_state
);
234 /* VAP stream control, mapping from input memory to PVS/RS memory */
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
));
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
));
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
));
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);
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
));
261 OUT_CS_32F((float)destx
);
262 OUT_CS_32F((float)desty
);
263 OUT_CS_32F((float)srcx
);
264 OUT_CS_32F((float)srcy
);
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
));
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
));
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
);
281 OUT_CS_REG(R300_RB3D_DSTCACHE_CTLSTAT
, 0xA);
286 void r300_init_surface_functions(struct r300_context
* r300
)
288 r300
->context
.surface_fill
= r300_surface_fill
;
289 r300
->context
.surface_copy
= r300_surface_copy
;