Merge commit 'origin/master' into gallium-0.2
[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 const struct pipe_framebuffer_state *fb = &nv40->framebuffer;
42 unsigned i;
43
44 for (i = 0; i < fb->num_cbufs; i++)
45 fb->cbufs[i]->status = PIPE_SURFACE_STATUS_DEFINED;
46 if (fb->zsbuf)
47 fb->zsbuf->status = PIPE_SURFACE_STATUS_DEFINED;
48
49 while (*states) {
50 struct nv40_state_entry *e = *states;
51
52 if (nv40->dirty & e->dirty.pipe) {
53 if (e->validate(nv40))
54 nv40->state.dirty |= (1ULL << e->dirty.hw);
55 }
56
57 states++;
58 }
59 nv40->dirty = 0;
60 }
61
62 void
63 nv40_state_emit(struct nv40_context *nv40)
64 {
65 struct nv40_state *state = &nv40->state;
66 struct nv40_screen *screen = nv40->screen;
67 unsigned i, samplers;
68 uint64_t states;
69
70 if (nv40->pctx_id != screen->cur_pctx) {
71 for (i = 0; i < NV40_STATE_MAX; i++) {
72 if (state->hw[i] && screen->state[i] != state->hw[i])
73 state->dirty |= (1ULL << i);
74 }
75
76 screen->cur_pctx = nv40->pctx_id;
77 }
78
79 for (i = 0, states = state->dirty; states; i++) {
80 if (!(states & (1ULL << i)))
81 continue;
82 so_ref (state->hw[i], &nv40->screen->state[i]);
83 if (state->hw[i])
84 so_emit(nv40->nvws, nv40->screen->state[i]);
85 states &= ~(1ULL << i);
86 }
87
88 if (state->dirty & ((1ULL << NV40_STATE_FRAGPROG) |
89 (1ULL << NV40_STATE_FRAGTEX0))) {
90 BEGIN_RING(curie, NV40TCL_TEX_CACHE_CTL, 1);
91 OUT_RING (2);
92 BEGIN_RING(curie, NV40TCL_TEX_CACHE_CTL, 1);
93 OUT_RING (1);
94 }
95
96 state->dirty = 0;
97
98 so_emit_reloc_markers(nv40->nvws, state->hw[NV40_STATE_FB]);
99 for (i = 0, samplers = state->fp_samplers; i < 16 && samplers; i++) {
100 if (!(samplers & (1 << i)))
101 continue;
102 so_emit_reloc_markers(nv40->nvws,
103 state->hw[NV40_STATE_FRAGTEX0+i]);
104 samplers &= ~(1ULL << i);
105 }
106 so_emit_reloc_markers(nv40->nvws, state->hw[NV40_STATE_FRAGPROG]);
107 if (state->hw[NV40_STATE_VTXBUF] && nv40->render_mode == HW)
108 so_emit_reloc_markers(nv40->nvws, state->hw[NV40_STATE_VTXBUF]);
109 }
110
111 boolean
112 nv40_state_validate(struct nv40_context *nv40)
113 {
114 boolean was_sw = nv40->fallback_swtnl ? TRUE : FALSE;
115
116 if (nv40->render_mode != HW) {
117 /* Don't even bother trying to go back to hw if none
118 * of the states that caused swtnl previously have changed.
119 */
120 if ((nv40->fallback_swtnl & nv40->dirty)
121 != nv40->fallback_swtnl)
122 return FALSE;
123
124 /* Attempt to go to hwtnl again */
125 nv40->pipe.flush(&nv40->pipe, 0, NULL);
126 nv40->dirty |= (NV40_NEW_VIEWPORT |
127 NV40_NEW_VERTPROG |
128 NV40_NEW_ARRAYS);
129 nv40->render_mode = HW;
130 }
131
132 nv40_state_do_validate(nv40, render_states);
133 if (nv40->fallback_swtnl || nv40->fallback_swrast)
134 return FALSE;
135
136 if (was_sw)
137 NOUVEAU_ERR("swtnl->hw\n");
138
139 return TRUE;
140 }
141
142 boolean
143 nv40_state_validate_swtnl(struct nv40_context *nv40)
144 {
145 struct draw_context *draw = nv40->draw;
146
147 /* Setup for swtnl */
148 if (nv40->render_mode == HW) {
149 NOUVEAU_ERR("hw->swtnl 0x%08x\n", nv40->fallback_swtnl);
150 nv40->pipe.flush(&nv40->pipe, 0, NULL);
151 nv40->dirty |= (NV40_NEW_VIEWPORT |
152 NV40_NEW_VERTPROG |
153 NV40_NEW_ARRAYS);
154 nv40->render_mode = SWTNL;
155 }
156
157 if (nv40->draw_dirty & NV40_NEW_VERTPROG)
158 draw_bind_vertex_shader(draw, nv40->vertprog->draw);
159
160 if (nv40->draw_dirty & NV40_NEW_RAST)
161 draw_set_rasterizer_state(draw, &nv40->rasterizer->pipe);
162
163 if (nv40->draw_dirty & NV40_NEW_UCP)
164 draw_set_clip_state(draw, &nv40->clip);
165
166 if (nv40->draw_dirty & NV40_NEW_VIEWPORT)
167 draw_set_viewport_state(draw, &nv40->viewport);
168
169 if (nv40->draw_dirty & NV40_NEW_ARRAYS) {
170 draw_set_edgeflags(draw, nv40->edgeflags);
171 draw_set_vertex_buffers(draw, nv40->vtxbuf_nr, nv40->vtxbuf);
172 draw_set_vertex_elements(draw, nv40->vtxelt_nr, nv40->vtxelt);
173 }
174
175 nv40_state_do_validate(nv40, swtnl_states);
176 if (nv40->fallback_swrast) {
177 NOUVEAU_ERR("swtnl->swrast 0x%08x\n", nv40->fallback_swrast);
178 return FALSE;
179 }
180
181 nv40->draw_dirty = 0;
182 return TRUE;
183 }
184