Merge branch 'gallium-docs'
[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 nouveau_channel *chan = nv40->screen->base.channel;
58 struct nv40_state *state = &nv40->state;
59 struct nv40_screen *screen = nv40->screen;
60 unsigned i;
61 uint64_t states;
62
63 if (nv40->pctx_id != screen->cur_pctx) {
64 for (i = 0; i < NV40_STATE_MAX; i++) {
65 if (state->hw[i] && screen->state[i] != state->hw[i])
66 state->dirty |= (1ULL << i);
67 }
68
69 screen->cur_pctx = nv40->pctx_id;
70 }
71
72 for (i = 0, states = state->dirty; states; i++) {
73 if (!(states & (1ULL << i)))
74 continue;
75 so_ref (state->hw[i], &nv40->screen->state[i]);
76 if (state->hw[i])
77 so_emit(chan, nv40->screen->state[i]);
78 states &= ~(1ULL << i);
79 }
80
81 if (state->dirty & ((1ULL << NV40_STATE_FRAGPROG) |
82 (1ULL << NV40_STATE_FRAGTEX0))) {
83 BEGIN_RING(curie, NV40TCL_TEX_CACHE_CTL, 1);
84 OUT_RING (2);
85 BEGIN_RING(curie, NV40TCL_TEX_CACHE_CTL, 1);
86 OUT_RING (1);
87 }
88
89 state->dirty = 0;
90 }
91
92 void
93 nv40_state_flush_notify(struct nouveau_channel *chan)
94 {
95 struct nv40_context *nv40 = chan->user_private;
96 struct nv40_state *state = &nv40->state;
97 unsigned i, samplers;
98
99 so_emit_reloc_markers(chan, state->hw[NV40_STATE_FB]);
100 for (i = 0, samplers = state->fp_samplers; i < 16 && samplers; i++) {
101 if (!(samplers & (1 << i)))
102 continue;
103 so_emit_reloc_markers(chan,
104 state->hw[NV40_STATE_FRAGTEX0+i]);
105 samplers &= ~(1ULL << i);
106 }
107 so_emit_reloc_markers(chan, state->hw[NV40_STATE_FRAGPROG]);
108 if (state->hw[NV40_STATE_VTXBUF] && nv40->render_mode == HW)
109 so_emit_reloc_markers(chan, state->hw[NV40_STATE_VTXBUF]);
110 }
111
112 boolean
113 nv40_state_validate(struct nv40_context *nv40)
114 {
115 boolean was_sw = nv40->fallback_swtnl ? TRUE : FALSE;
116
117 if (nv40->render_mode != HW) {
118 /* Don't even bother trying to go back to hw if none
119 * of the states that caused swtnl previously have changed.
120 */
121 if ((nv40->fallback_swtnl & nv40->dirty)
122 != nv40->fallback_swtnl)
123 return FALSE;
124
125 /* Attempt to go to hwtnl again */
126 nv40->pipe.flush(&nv40->pipe, 0, NULL);
127 nv40->dirty |= (NV40_NEW_VIEWPORT |
128 NV40_NEW_VERTPROG |
129 NV40_NEW_ARRAYS);
130 nv40->render_mode = HW;
131 }
132
133 nv40_state_do_validate(nv40, render_states);
134 if (nv40->fallback_swtnl || nv40->fallback_swrast)
135 return FALSE;
136
137 if (was_sw)
138 NOUVEAU_ERR("swtnl->hw\n");
139
140 return TRUE;
141 }
142
143 boolean
144 nv40_state_validate_swtnl(struct nv40_context *nv40)
145 {
146 struct draw_context *draw = nv40->draw;
147
148 /* Setup for swtnl */
149 if (nv40->render_mode == HW) {
150 NOUVEAU_ERR("hw->swtnl 0x%08x\n", nv40->fallback_swtnl);
151 nv40->pipe.flush(&nv40->pipe, 0, NULL);
152 nv40->dirty |= (NV40_NEW_VIEWPORT |
153 NV40_NEW_VERTPROG |
154 NV40_NEW_ARRAYS);
155 nv40->render_mode = SWTNL;
156 }
157
158 if (nv40->draw_dirty & NV40_NEW_VERTPROG)
159 draw_bind_vertex_shader(draw, nv40->vertprog->draw);
160
161 if (nv40->draw_dirty & NV40_NEW_RAST)
162 draw_set_rasterizer_state(draw, &nv40->rasterizer->pipe);
163
164 if (nv40->draw_dirty & NV40_NEW_UCP)
165 draw_set_clip_state(draw, &nv40->clip);
166
167 if (nv40->draw_dirty & NV40_NEW_VIEWPORT)
168 draw_set_viewport_state(draw, &nv40->viewport);
169
170 if (nv40->draw_dirty & NV40_NEW_ARRAYS) {
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