r300-gallium: Clean up casts and indents.
[mesa.git] / src / gallium / drivers / r300 / r300_emit.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 /* r300_emit: Functions for emitting state. */
24
25 #include "r300_emit.h"
26
27 void r300_emit_blend_state(struct r300_context* r300,
28 struct r300_blend_state* blend)
29 {
30 CS_LOCALS(r300);
31 BEGIN_CS(7);
32 OUT_CS_REG_SEQ(R300_RB3D_CBLEND, 2);
33 OUT_CS(blend->blend_control);
34 OUT_CS(blend->alpha_blend_control);
35 OUT_CS_REG(R300_RB3D_ROPCNTL, blend->rop);
36 OUT_CS_REG(R300_RB3D_DITHER_CTL, blend->dither);
37 END_CS;
38 }
39
40 void r300_emit_blend_color_state(struct r300_context* r300,
41 struct r300_blend_color_state* bc)
42 {
43 struct r300_screen* r300screen = r300_screen(r300->context.screen);
44 CS_LOCALS(r300);
45
46 if (r300screen->caps->is_r500) {
47 BEGIN_CS(3);
48 OUT_CS_REG_SEQ(R500_RB3D_CONSTANT_COLOR_AR, 2);
49 OUT_CS(bc->blend_color_red_alpha);
50 OUT_CS(bc->blend_color_green_blue);
51 END_CS;
52 } else {
53 BEGIN_CS(2);
54 OUT_CS_REG(R300_RB3D_BLEND_COLOR, bc->blend_color);
55 END_CS;
56 }
57 }
58
59 void r300_emit_dsa_state(struct r300_context* r300,
60 struct r300_dsa_state* dsa)
61 {
62 struct r300_screen* r300screen = r300_screen(r300->context.screen);
63 CS_LOCALS(r300);
64
65 BEGIN_CS(r300screen->caps->is_r500 ? 8 : 8);
66 OUT_CS_REG(R300_FG_ALPHA_FUNC, dsa->alpha_function);
67 /* XXX figure out the r300 counterpart for this */
68 if (r300screen->caps->is_r500) {
69 /* OUT_CS_REG(R500_FG_ALPHA_VALUE, dsa->alpha_reference); */
70 }
71 OUT_CS_REG_SEQ(R300_ZB_CNTL, 3);
72 OUT_CS(dsa->z_buffer_control);
73 OUT_CS(dsa->z_stencil_control);
74 OUT_CS(dsa->stencil_ref_mask);
75 OUT_CS_REG(R300_ZB_ZTOP, dsa->z_buffer_top);
76 if (r300screen->caps->is_r500) {
77 /* OUT_CS_REG(R500_ZB_STENCILREFMASK_BF, dsa->stencil_ref_bf); */
78 }
79 END_CS;
80 }
81
82 void r300_emit_fragment_shader(struct r300_context* r300,
83 struct r300_fragment_shader* fs)
84 {
85 CS_LOCALS(r300);
86 int i;
87
88 BEGIN_CS(22);
89
90 OUT_CS_REG(R300_US_CONFIG, MAX2(fs->indirections - 1, 0));
91 OUT_CS_REG(R300_US_PIXSIZE, fs->shader.stack_size);
92 /* XXX figure out exactly how big the sizes are on this reg */
93 OUT_CS_REG(R300_US_CODE_OFFSET, 0x0);
94 /* XXX figure these ones out a bit better kthnx */
95 OUT_CS_REG(R300_US_CODE_ADDR_0, 0x0);
96 OUT_CS_REG(R300_US_CODE_ADDR_1, 0x0);
97 OUT_CS_REG(R300_US_CODE_ADDR_2, 0x0);
98 OUT_CS_REG(R300_US_CODE_ADDR_3, R300_RGBA_OUT);
99
100 for (i = 0; i < fs->alu_instruction_count; i++) {
101 OUT_CS_REG(R300_US_ALU_RGB_INST_0 + (4 * i),
102 fs->instructions[i].alu_rgb_inst);
103 OUT_CS_REG(R300_US_ALU_RGB_ADDR_0 + (4 * i),
104 fs->instructions[i].alu_rgb_addr);
105 OUT_CS_REG(R300_US_ALU_ALPHA_INST_0 + (4 * i),
106 fs->instructions[i].alu_alpha_inst);
107 OUT_CS_REG(R300_US_ALU_ALPHA_ADDR_0 + (4 * i),
108 fs->instructions[i].alu_alpha_addr);
109 }
110
111 END_CS;
112 }
113
114 void r500_emit_fragment_shader(struct r300_context* r300,
115 struct r500_fragment_shader* fs)
116 {
117 CS_LOCALS(r300);
118 int i;
119
120 BEGIN_CS(9 + (fs->instruction_count * 6));
121 OUT_CS_REG(R500_US_CONFIG, R500_ZERO_TIMES_ANYTHING_EQUALS_ZERO);
122 OUT_CS_REG(R500_US_PIXSIZE, fs->shader.stack_size);
123 OUT_CS_REG(R500_US_CODE_ADDR, R500_US_CODE_START_ADDR(0) |
124 R500_US_CODE_END_ADDR(fs->instruction_count));
125
126 OUT_CS_REG(R500_GA_US_VECTOR_INDEX, R500_GA_US_VECTOR_INDEX_TYPE_INSTR);
127 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA,
128 fs->instruction_count * 6);
129 for (i = 0; i < fs->instruction_count; i++) {
130 OUT_CS(fs->instructions[i].inst0);
131 OUT_CS(fs->instructions[i].inst1);
132 OUT_CS(fs->instructions[i].inst2);
133 OUT_CS(fs->instructions[i].inst3);
134 OUT_CS(fs->instructions[i].inst4);
135 OUT_CS(fs->instructions[i].inst5);
136 }
137 END_CS;
138 }
139
140 /* Translate pipe_format into US_OUT_FMT. Note that formats are stored from
141 * C3 to C0. */
142 uint32_t translate_out_fmt(enum pipe_format format)
143 {
144 switch (format) {
145 case PIPE_FORMAT_A8R8G8B8_UNORM:
146 return R300_US_OUT_FMT_C4_8 |
147 R300_C0_SEL_B | R300_C1_SEL_G |
148 R300_C2_SEL_R | R300_C3_SEL_A;
149 default:
150 return R300_US_OUT_FMT_UNUSED;
151 }
152 return 0;
153 }
154
155 /* XXX add pitch, stride */
156 void r300_emit_fb_state(struct r300_context* r300,
157 struct pipe_framebuffer_state* fb)
158 {
159 CS_LOCALS(r300);
160 struct r300_texture* tex;
161 int i;
162
163 BEGIN_CS((5 * fb->nr_cbufs) + (fb->zsbuf ? 5 : 0) + 4);
164 for (i = 0; i < fb->nr_cbufs; i++) {
165 tex = (struct r300_texture*)fb->cbufs[i]->texture;
166 OUT_CS_REG_SEQ(R300_RB3D_COLOROFFSET0 + (4 * i), 1);
167 OUT_CS_RELOC(tex->buffer, 0, 0, RADEON_GEM_DOMAIN_VRAM, 0);
168
169 OUT_CS_REG(R300_US_OUT_FMT_0 + (4 * i),
170 translate_out_fmt(fb->cbufs[i]->format));
171 }
172
173 if (fb->zsbuf) {
174 tex = (struct r300_texture*)fb->zsbuf->texture;
175 OUT_CS_REG_SEQ(R300_ZB_DEPTHOFFSET, 1);
176 OUT_CS_RELOC(tex->buffer, 0, 0, RADEON_GEM_DOMAIN_VRAM, 0);
177 if (fb->zsbuf->format == PIPE_FORMAT_Z24S8_UNORM) {
178 OUT_CS_REG(R300_ZB_FORMAT,
179 R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL);
180 } else {
181 OUT_CS_REG(R300_ZB_FORMAT, 0x0);
182 }
183 }
184
185 OUT_CS_REG(R300_RB3D_DSTCACHE_CTLSTAT,
186 R300_RB3D_DSTCACHE_CTLSTAT_DC_FREE_FREE_3D_TAGS |
187 R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D);
188 OUT_CS_REG(R300_ZB_ZCACHE_CTLSTAT,
189 R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE |
190 R300_ZB_ZCACHE_CTLSTAT_ZC_FREE_FREE);
191 END_CS;
192 }
193
194 void r300_emit_rs_state(struct r300_context* r300, struct r300_rs_state* rs)
195 {
196 struct r300_screen* r300screen = r300_screen(r300->context.screen);
197 CS_LOCALS(r300);
198
199 BEGIN_CS(13);
200 OUT_CS_REG(R300_VAP_CNTL_STATUS, rs->vap_control_status);
201 OUT_CS_REG_SEQ(R300_SU_POLY_OFFSET_FRONT_SCALE, 6);
202 OUT_CS(rs->depth_scale_front);
203 OUT_CS(rs->depth_offset_front);
204 OUT_CS(rs->depth_scale_back);
205 OUT_CS(rs->depth_offset_back);
206 OUT_CS(rs->polygon_offset_enable);
207 OUT_CS(rs->cull_mode);
208 OUT_CS_REG(R300_GA_LINE_STIPPLE_CONFIG, rs->line_stipple_config);
209 OUT_CS_REG(R300_GA_LINE_STIPPLE_VALUE, rs->line_stipple_value);
210 END_CS;
211 }
212
213 void r300_emit_rs_block_state(struct r300_context* r300,
214 struct r300_rs_block* rs)
215 {
216 struct r300_screen* r300screen = r300_screen(r300->context.screen);
217 CS_LOCALS(r300);
218 int i;
219
220 BEGIN_CS(21);
221 if (r300screen->caps->is_r500) {
222 OUT_CS_REG_SEQ(R500_RS_IP_0, 8);
223 } else {
224 OUT_CS_REG_SEQ(R300_RS_IP_0, 8);
225 }
226 for (i = 0; i < 8; i++) {
227 OUT_CS(rs->ip[i]);
228 }
229
230 OUT_CS_REG_SEQ(R300_RS_COUNT, 2);
231 OUT_CS(rs->count);
232 OUT_CS(rs->inst_count);
233
234 if (r300screen->caps->is_r500) {
235 OUT_CS_REG_SEQ(R500_RS_INST_0, 8);
236 } else {
237 OUT_CS_REG_SEQ(R300_RS_INST_0, 8);
238 }
239 for (i = 0; i < 8; i++) {
240 OUT_CS(rs->inst[i]);
241 }
242
243 END_CS;
244 }
245
246 void r300_emit_scissor_state(struct r300_context* r300,
247 struct r300_scissor_state* scissor)
248 {
249 CS_LOCALS(r300);
250
251 BEGIN_CS(3);
252 OUT_CS_REG_SEQ(R300_SC_SCISSORS_TL, 2);
253 OUT_CS(scissor->scissor_top_left);
254 OUT_CS(scissor->scissor_bottom_right);
255 END_CS;
256 }
257
258 void r300_emit_vertex_format_state(struct r300_context* r300)
259 {
260 CS_LOCALS(r300);
261 int i;
262
263 BEGIN_CS(6);
264 OUT_CS_REG_SEQ(R300_VAP_VTX_STATE_CNTL, 2);
265 OUT_CS(r300->vertex_info.vinfo.hwfmt[0]);
266 OUT_CS(r300->vertex_info.vinfo.hwfmt[1]);
267 OUT_CS_REG_SEQ(R300_VAP_OUTPUT_VTX_FMT_0, 2);
268 OUT_CS(r300->vertex_info.vinfo.hwfmt[2]);
269 OUT_CS(r300->vertex_info.vinfo.hwfmt[3]);
270 END_CS;
271
272 BEGIN_CS(18);
273 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_0, 8);
274 for (i = 0; i < 8; i++) {
275 OUT_CS(r300->vertex_info.vap_prog_stream_cntl[i]);
276 }
277 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_EXT_0, 8);
278 for (i = 0; i < 8; i++) {
279 OUT_CS(r300->vertex_info.vap_prog_stream_cntl_ext[i]);
280 }
281 END_CS;
282 }
283
284 /* Emit all dirty state. */
285 void r300_emit_dirty_state(struct r300_context* r300)
286 {
287 struct r300_screen* r300screen = r300_screen(r300->context.screen);
288 CS_LOCALS(r300);
289
290 if (!(r300->dirty_state) && !(r300->dirty_hw)) {
291 return;
292 }
293
294 r300_update_derived_state(r300);
295
296 /* XXX check size */
297
298 if (r300->dirty_state & R300_NEW_BLEND) {
299 r300_emit_blend_state(r300, r300->blend_state);
300 r300->dirty_state &= ~R300_NEW_BLEND;
301 }
302
303 if (r300->dirty_state & R300_NEW_BLEND_COLOR) {
304 r300_emit_blend_color_state(r300, r300->blend_color_state);
305 r300->dirty_state &= ~R300_NEW_BLEND_COLOR;
306 }
307
308 if (r300->dirty_state & R300_NEW_DSA) {
309 r300_emit_dsa_state(r300, r300->dsa_state);
310 r300->dirty_state &= ~R300_NEW_DSA;
311 }
312
313 if (r300->dirty_state & R300_NEW_FRAGMENT_SHADER) {
314 if (r300screen->caps->is_r500) {
315 r500_emit_fragment_shader(r300,
316 (struct r500_fragment_shader*)r300->fs);
317 } else {
318 r300_emit_fragment_shader(r300,
319 (struct r300_fragment_shader*)r300->fs);
320 }
321 r300->dirty_state &= ~R300_NEW_FRAGMENT_SHADER;
322 }
323
324 if (r300->dirty_state & R300_NEW_RASTERIZER) {
325 r300_emit_rs_state(r300, r300->rs_state);
326 r300->dirty_state &= ~R300_NEW_RASTERIZER;
327 }
328
329 if (r300->dirty_state & R300_NEW_SCISSOR) {
330 r300_emit_scissor_state(r300, r300->scissor_state);
331 r300->dirty_state &= ~R300_NEW_SCISSOR;
332 }
333
334 if (r300->dirty_state & R300_NEW_VERTEX_FORMAT) {
335 r300_emit_vertex_format_state(r300);
336 r300->dirty_state &= ~R300_NEW_VERTEX_FORMAT;
337 }
338 }