i965: fix bugs in projective texture coordinates
[mesa.git] / src / gallium / drivers / nv40 / nv40_state_emit.c
1 #include "nv40_context.h"
2 #include "nv40_state.h"
3 #include "draw/draw_context.h"
4
5 static struct nv40_state_entry *render_states[] = {
6 &nv40_state_framebuffer,
7 &nv40_state_rasterizer,
8 &nv40_state_scissor,
9 &nv40_state_stipple,
10 &nv40_state_fragprog,
11 &nv40_state_fragtex,
12 &nv40_state_vertprog,
13 &nv40_state_blend,
14 &nv40_state_blend_colour,
15 &nv40_state_zsa,
16 &nv40_state_viewport,
17 &nv40_state_vbo,
18 NULL
19 };
20
21 static struct nv40_state_entry *swtnl_states[] = {
22 &nv40_state_framebuffer,
23 &nv40_state_rasterizer,
24 &nv40_state_scissor,
25 &nv40_state_stipple,
26 &nv40_state_fragprog,
27 &nv40_state_fragtex,
28 &nv40_state_vertprog,
29 &nv40_state_blend,
30 &nv40_state_blend_colour,
31 &nv40_state_zsa,
32 &nv40_state_viewport,
33 &nv40_state_vtxfmt,
34 NULL
35 };
36
37 static void
38 nv40_state_do_validate(struct nv40_context *nv40,
39 struct nv40_state_entry **states)
40 {
41 while (*states) {
42 struct nv40_state_entry *e = *states;
43
44 if (nv40->dirty & e->dirty.pipe) {
45 if (e->validate(nv40))
46 nv40->state.dirty |= (1ULL << e->dirty.hw);
47 }
48
49 states++;
50 }
51 nv40->dirty = 0;
52 }
53
54 void
55 nv40_state_emit(struct nv40_context *nv40)
56 {
57 struct nv40_state *state = &nv40->state;
58 struct nv40_screen *screen = nv40->screen;
59 unsigned i, samplers;
60 uint64_t states;
61
62 if (nv40->pctx_id != screen->cur_pctx) {
63 for (i = 0; i < NV40_STATE_MAX; i++) {
64 if (state->hw[i] && screen->state[i] != state->hw[i])
65 state->dirty |= (1ULL << i);
66 }
67
68 screen->cur_pctx = nv40->pctx_id;
69 }
70
71 for (i = 0, states = state->dirty; states; i++) {
72 if (!(states & (1ULL << i)))
73 continue;
74 so_ref (state->hw[i], &nv40->screen->state[i]);
75 if (state->hw[i])
76 so_emit(nv40->nvws, nv40->screen->state[i]);
77 states &= ~(1ULL << i);
78 }
79
80 if (state->dirty & ((1ULL << NV40_STATE_FRAGPROG) |
81 (1ULL << NV40_STATE_FRAGTEX0))) {
82 BEGIN_RING(curie, NV40TCL_TEX_CACHE_CTL, 1);
83 OUT_RING (2);
84 BEGIN_RING(curie, NV40TCL_TEX_CACHE_CTL, 1);
85 OUT_RING (1);
86 }
87
88 state->dirty = 0;
89
90 so_emit_reloc_markers(nv40->nvws, state->hw[NV40_STATE_FB]);
91 for (i = 0, samplers = state->fp_samplers; i < 16 && samplers; i++) {
92 if (!(samplers & (1 << i)))
93 continue;
94 so_emit_reloc_markers(nv40->nvws,
95 state->hw[NV40_STATE_FRAGTEX0+i]);
96 samplers &= ~(1ULL << i);
97 }
98 so_emit_reloc_markers(nv40->nvws, state->hw[NV40_STATE_FRAGPROG]);
99 if (state->hw[NV40_STATE_VTXBUF] && nv40->render_mode == HW)
100 so_emit_reloc_markers(nv40->nvws, state->hw[NV40_STATE_VTXBUF]);
101 }
102
103 boolean
104 nv40_state_validate(struct nv40_context *nv40)
105 {
106 boolean was_sw = nv40->fallback_swtnl ? TRUE : FALSE;
107
108 if (nv40->render_mode != HW) {
109 /* Don't even bother trying to go back to hw if none
110 * of the states that caused swtnl previously have changed.
111 */
112 if ((nv40->fallback_swtnl & nv40->dirty)
113 != nv40->fallback_swtnl)
114 return FALSE;
115
116 /* Attempt to go to hwtnl again */
117 nv40->pipe.flush(&nv40->pipe, 0, NULL);
118 nv40->dirty |= (NV40_NEW_VIEWPORT |
119 NV40_NEW_VERTPROG |
120 NV40_NEW_ARRAYS);
121 nv40->render_mode = HW;
122 }
123
124 nv40_state_do_validate(nv40, render_states);
125 if (nv40->fallback_swtnl || nv40->fallback_swrast)
126 return FALSE;
127
128 if (was_sw)
129 NOUVEAU_ERR("swtnl->hw\n");
130
131 return TRUE;
132 }
133
134 boolean
135 nv40_state_validate_swtnl(struct nv40_context *nv40)
136 {
137 struct draw_context *draw = nv40->draw;
138
139 /* Setup for swtnl */
140 if (nv40->render_mode == HW) {
141 NOUVEAU_ERR("hw->swtnl 0x%08x\n", nv40->fallback_swtnl);
142 nv40->pipe.flush(&nv40->pipe, 0, NULL);
143 nv40->dirty |= (NV40_NEW_VIEWPORT |
144 NV40_NEW_VERTPROG |
145 NV40_NEW_ARRAYS);
146 nv40->render_mode = SWTNL;
147 }
148
149 if (nv40->draw_dirty & NV40_NEW_VERTPROG)
150 draw_bind_vertex_shader(draw, nv40->vertprog->draw);
151
152 if (nv40->draw_dirty & NV40_NEW_RAST)
153 draw_set_rasterizer_state(draw, &nv40->rasterizer->pipe);
154
155 if (nv40->draw_dirty & NV40_NEW_UCP)
156 draw_set_clip_state(draw, &nv40->clip);
157
158 if (nv40->draw_dirty & NV40_NEW_VIEWPORT)
159 draw_set_viewport_state(draw, &nv40->viewport);
160
161 if (nv40->draw_dirty & NV40_NEW_ARRAYS) {
162 draw_set_edgeflags(draw, nv40->edgeflags);
163 draw_set_vertex_buffers(draw, nv40->vtxbuf_nr, nv40->vtxbuf);
164 draw_set_vertex_elements(draw, nv40->vtxelt_nr, nv40->vtxelt);
165 }
166
167 nv40_state_do_validate(nv40, swtnl_states);
168 if (nv40->fallback_swrast) {
169 NOUVEAU_ERR("swtnl->swrast 0x%08x\n", nv40->fallback_swrast);
170 return FALSE;
171 }
172
173 nv40->draw_dirty = 0;
174 return TRUE;
175 }
176