r300_state_derived.c revision c28298855bf5d5ef790d28bac2e77700625fa69a
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#include "r300_state_derived.h"
24
25/* r300_state_derived: Various bits of state which are dependent upon
26 * currently bound CSO data. */
27
28static uint32_t translate_vertex_data_type(int type) {
29    switch (type) {
30        case EMIT_1F:
31        case EMIT_1F_PSIZE:
32            return R300_DATA_TYPE_FLOAT_1;
33            break;
34        case EMIT_2F:
35            return R300_DATA_TYPE_FLOAT_2;
36            break;
37        case EMIT_3F:
38            return R300_DATA_TYPE_FLOAT_3;
39            break;
40        case EMIT_4F:
41            return R300_DATA_TYPE_FLOAT_4;
42            break;
43        default:
44            debug_printf("r300: Implementation error: "
45                    "Bad vertex data type!\n");
46            break;
47    }
48
49    return 0;
50}
51
52/* Update the vertex_info struct in our r300_context.
53 *
54 * The vertex_info struct describes the post-TCL format of vertices. It is
55 * required for Draw when doing SW TCL, and also for describing the
56 * dreaded RS block on R300 chipsets. */
57/* XXX this function should be able to handle vert shaders as well as draw */
58static void r300_update_vertex_layout(struct r300_context* r300)
59{
60    struct r300_vertex_format vformat;
61    struct vertex_info vinfo;
62    boolean pos = false, psize = false, fog = false;
63    int i, texs = 0, cols = 0;
64    int tab[16];
65
66    struct tgsi_shader_info* info = &r300->fs->info;
67
68    memset(&vinfo, 0, sizeof(vinfo));
69    for (i = 0; i < 16; i++) {
70        tab[i] = -1;
71    }
72
73    assert(info->num_inputs <= 16);
74
75    for (i = 0; i < info->num_inputs; i++) {
76        switch (info->input_semantic_name[i]) {
77            case TGSI_SEMANTIC_POSITION:
78                pos = true;
79                tab[i] = 0;
80                break;
81            case TGSI_SEMANTIC_COLOR:
82                tab[i] = 2 + cols++;
83                break;
84            case TGSI_SEMANTIC_FOG:
85                fog = true;
86                break;
87            case TGSI_SEMANTIC_PSIZE:
88                psize = true;
89                tab[i] = 1;
90                break;
91            case TGSI_SEMANTIC_GENERIC:
92                tab[i] = 6 + texs++;
93                break;
94            default:
95                debug_printf("r300: Unknown vertex input %d\n",
96                    info->input_semantic_name[i]);
97                break;
98        }
99    }
100
101    /* Do the actual vertex_info setup.
102     *
103     * vertex_info has four uints of hardware-specific data in it.
104     * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
105     * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
106     * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
107     * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
108
109    vinfo.hwfmt[0] = 0x5555; /* XXX this is classic Mesa bonghits */
110
111    if (!pos) {
112        debug_printf("r300: Forcing vertex position attribute emit...\n");
113        tab[0] = 0;
114    }
115
116    draw_emit_vertex_attr(&vinfo, EMIT_4F, INTERP_POS,
117        draw_find_vs_output(r300->draw, TGSI_SEMANTIC_POSITION, 0));
118    vinfo.hwfmt[1] |= R300_INPUT_CNTL_POS;
119    vinfo.hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT;
120
121    if (psize) {
122        draw_emit_vertex_attr(&vinfo, EMIT_1F_PSIZE, INTERP_LINEAR,
123            draw_find_vs_output(r300->draw, TGSI_SEMANTIC_PSIZE, 0));
124        vinfo.hwfmt[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT;
125    }
126
127    for (i = 0; i < cols; i++) {
128        draw_emit_vertex_attr(&vinfo, EMIT_4F, INTERP_LINEAR,
129            draw_find_vs_output(r300->draw, TGSI_SEMANTIC_COLOR, i));
130        vinfo.hwfmt[1] |= R300_INPUT_CNTL_COLOR;
131        vinfo.hwfmt[2] |= (R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << i);
132    }
133
134    if (fog) {
135        draw_emit_vertex_attr(&vinfo, EMIT_4F, INTERP_PERSPECTIVE,
136            draw_find_vs_output(r300->draw, TGSI_SEMANTIC_FOG, 0));
137        vinfo.hwfmt[2] |=
138            (R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << cols);
139    }
140
141    for (i = 0; i < texs; i++) {
142        draw_emit_vertex_attr(&vinfo, EMIT_4F, INTERP_PERSPECTIVE,
143            draw_find_vs_output(r300->draw, TGSI_SEMANTIC_GENERIC, i));
144        vinfo.hwfmt[1] |= (R300_INPUT_CNTL_TC0 << i);
145        vinfo.hwfmt[3] |= (4 << (3 * i));
146    }
147
148    draw_compute_vertex_size(&vinfo);
149
150    if (memcmp(&r300->vertex_info, &vinfo, sizeof(struct vertex_info))) {
151        uint32_t temp;
152
153#define BORING_SWIZZLE \
154        ((R300_SWIZZLE_SELECT_X << R300_SWIZZLE_SELECT_X_SHIFT) | \
155         (R300_SWIZZLE_SELECT_Y << R300_SWIZZLE_SELECT_Y_SHIFT) | \
156         (R300_SWIZZLE_SELECT_Z << R300_SWIZZLE_SELECT_Z_SHIFT) | \
157         (R300_SWIZZLE_SELECT_W << R300_SWIZZLE_SELECT_W_SHIFT) | \
158         (0xf << R300_WRITE_ENA_SHIFT))
159
160        for (i = 0; i < vinfo.num_attribs; i++) {
161            /* Make sure we have a proper destination for our attribute */
162            if (tab[i] != -1) {
163                assert(0);
164            }
165
166            temp = translate_vertex_data_type(vinfo.attrib[i].emit) |
167                (tab[i] << R300_DST_VEC_LOC_SHIFT) | R300_SIGNED;
168            if (i & 1) {
169                r300->vertex_info.vap_prog_stream_cntl[i >> 1] &= 0xffff0000;
170                r300->vertex_info.vap_prog_stream_cntl[i >> 1] |=
171                        temp << 16;
172            } else {
173                r300->vertex_info.vap_prog_stream_cntl[i >> 1] &= 0xffff;
174                r300->vertex_info.vap_prog_stream_cntl[i >> 1] |=
175                    temp;
176            }
177
178            r300->vertex_info.vap_prog_stream_cntl_ext[i >> 1] |=
179                (BORING_SWIZZLE << (i & 1 ? 16 : 0));
180        }
181        r300->vertex_info.vap_prog_stream_cntl[i >> 1] |= (R300_LAST_VEC <<
182                (i & 1 ? 16 : 0));
183
184        memcpy(&r300->vertex_info, &vinfo, sizeof(struct vertex_info));
185        r300->dirty_state |= R300_NEW_VERTEX_FORMAT;
186    }
187}
188
189/* Set up the RS block. This is the part of the chipset that actually does
190 * the rasterization of vertices into fragments. This is also the part of the
191 * chipset that locks up if any part of it is even slightly wrong. */
192static void r300_update_rs_block(struct r300_context* r300)
193{
194    struct r300_rs_block* rs = r300->rs_block;
195    struct vertex_info* vinfo = &r300->vertex_info.vinfo;
196    int col_count = 0, fp_offset = 0, i, tex_count = 0;
197
198    memset(rs, 0, sizeof(struct r300_rs_block));
199
200    for (i = 0; i < vinfo->num_attribs; i++) {
201        switch (vinfo->attrib[i].interp_mode) {
202            case INTERP_LINEAR:
203                rs->ip[col_count] |=
204                    R300_RS_COL_PTR(vinfo->attrib[i].src_index) |
205                    R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
206                col_count++;
207                break;
208            case INTERP_PERSPECTIVE:
209                rs->ip[tex_count] |=
210                    R300_RS_TEX_PTR(vinfo->attrib[i].src_index) |
211                    R300_RS_SEL_S(R300_RS_SEL_C0) |
212                    R300_RS_SEL_T(R300_RS_SEL_C1) |
213                    R300_RS_SEL_R(R300_RS_SEL_C2) |
214                    R300_RS_SEL_Q(R300_RS_SEL_C3);
215                tex_count += 4;
216                break;
217        }
218    }
219
220    for (i = 0; i < tex_count; i++) {
221        rs->inst[i] |= R300_RS_INST_TEX_ID(i) | R300_RS_INST_TEX_CN_WRITE |
222            R300_RS_INST_TEX_ADDR(fp_offset);
223        fp_offset++;
224    }
225
226    for (i = 0; i < col_count; i++) {
227        rs->inst[i] |= R300_RS_INST_COL_ID(i) | R300_RS_INST_COL_CN_WRITE |
228            R300_RS_INST_COL_ADDR(fp_offset);
229        fp_offset++;
230    }
231
232    rs->count = (tex_count * 4) | (col_count << R300_IC_COUNT_SHIFT) |
233        R300_HIRES_EN;
234
235    rs->inst_count = MAX2(col_count, tex_count);
236}
237
238void r300_update_derived_state(struct r300_context* r300)
239{
240    if (r300->dirty_state & R300_NEW_FRAGMENT_SHADER) {
241        r300_update_vertex_layout(r300);
242    }
243
244    if (r300->dirty_state & R300_NEW_VERTEX_FORMAT) {
245        r300_update_rs_block(r300);
246    }
247}
248