summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/nvfx/nvfx_state_emit.c
blob: 8e3c342179d516b354674927f4b22c982fcf7106 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "nvfx_context.h"
#include "nvfx_state.h"
#include "nvfx_resource.h"
#include "draw/draw_context.h"

static boolean
nvfx_state_validate_common(struct nvfx_context *nvfx)
{
	struct nouveau_channel* chan = nvfx->screen->base.channel;
	unsigned dirty;
	unsigned still_dirty = 0;
	int all_swizzled = -1;
	boolean flush_tex_cache = FALSE;
	unsigned render_temps;

	if(nvfx != nvfx->screen->cur_ctx)
	{
		nvfx->dirty = ~0;
		nvfx->hw_vtxelt_nr = 16;
		nvfx->screen->cur_ctx = nvfx;
	}

	/* These can trigger use the of 3D engine to copy temporaries.
	 * That will recurse here and thus dirty all 3D state, so we need to this before anything else, and in a loop..
	 * This converges to having clean temps, then binding both fragtexes and framebuffers.
	 */
	while(nvfx->dirty & (NVFX_NEW_FB | NVFX_NEW_SAMPLER))
	{
		if(nvfx->dirty & NVFX_NEW_SAMPLER)
		{
			nvfx->dirty &=~ NVFX_NEW_SAMPLER;
			nvfx_fragtex_validate(nvfx);

			// TODO: only set this if really necessary
			flush_tex_cache = TRUE;
		}

		if(nvfx->dirty & NVFX_NEW_FB)
		{
			nvfx->dirty &=~ NVFX_NEW_FB;
			all_swizzled = nvfx_framebuffer_prepare(nvfx);

			// TODO: make sure this doesn't happen, i.e. fbs have matching formats
			assert(all_swizzled >= 0);
		}
	}

	dirty = nvfx->dirty;

	if(nvfx->render_mode == HW)
	{
		if(dirty & (NVFX_NEW_VERTPROG | NVFX_NEW_VERTCONST | NVFX_NEW_UCP))
		{
			if(!nvfx_vertprog_validate(nvfx))
				return FALSE;
		}

		if(dirty & NVFX_NEW_ARRAYS)
		{
			if(!nvfx_vbo_validate(nvfx))
				return FALSE;
		}

		if(dirty & NVFX_NEW_INDEX)
		{
			if(nvfx->use_index_buffer)
				nvfx_idxbuf_validate(nvfx);
			else
				still_dirty = NVFX_NEW_INDEX;
		}
	}
	else
	{
		/* TODO: this looks a bit misdesigned */
		if(dirty & (NVFX_NEW_VERTPROG | NVFX_NEW_UCP))
			nvfx_vertprog_validate(nvfx);

		if(dirty & (NVFX_NEW_ARRAYS | NVFX_NEW_INDEX | NVFX_NEW_FRAGPROG))
			nvfx_vtxfmt_validate(nvfx);
	}

	if(dirty & NVFX_NEW_RAST)
		sb_emit(chan, nvfx->rasterizer->sb, nvfx->rasterizer->sb_len);

	if(dirty & NVFX_NEW_SCISSOR)
		nvfx_state_scissor_validate(nvfx);

	if(dirty & NVFX_NEW_STIPPLE)
		nvfx_state_stipple_validate(nvfx);

	if(dirty & (NVFX_NEW_FRAGPROG | NVFX_NEW_FRAGCONST | NVFX_NEW_VERTPROG | NVFX_NEW_SPRITE))
	{
		nvfx_fragprog_validate(nvfx);
		if(dirty & NVFX_NEW_FRAGPROG)
			flush_tex_cache = TRUE; // TODO: do we need this?
	}

	if(all_swizzled >= 0)
		nvfx_framebuffer_validate(nvfx, all_swizzled);

	if(dirty & NVFX_NEW_BLEND)
		sb_emit(chan, nvfx->blend->sb, nvfx->blend->sb_len);

	if(dirty & NVFX_NEW_BCOL)
		nvfx_state_blend_colour_validate(nvfx);

	if(dirty & NVFX_NEW_ZSA)
		sb_emit(chan, nvfx->zsa->sb, nvfx->zsa->sb_len);

	if(dirty & NVFX_NEW_SR)
		nvfx_state_sr_validate(nvfx);

/* All these dependencies are wrong, but otherwise
   etracer, neverball, foobillard, glest totally misrender
   TODO: find the right fix
*/
	if(dirty & (NVFX_NEW_VIEWPORT | NVFX_NEW_RAST | NVFX_NEW_ZSA) || (all_swizzled >= 0))
	{
		nvfx_state_viewport_validate(nvfx);
	}

	if(dirty & NVFX_NEW_ZSA || (all_swizzled >= 0))
	{
		WAIT_RING(chan, 3);
		OUT_RING(chan, RING_3D(NV34TCL_DEPTH_WRITE_ENABLE, 2));
		OUT_RING(chan, nvfx->framebuffer.zsbuf && nvfx->zsa->pipe.depth.writemask);
	        OUT_RING(chan, nvfx->framebuffer.zsbuf && nvfx->zsa->pipe.depth.enabled);
	}

	if(flush_tex_cache)
	{
		// TODO: what about nv30?
		if(nvfx->is_nv4x)
		{
			WAIT_RING(chan, 4);
			OUT_RING(chan, RING_3D(NV40TCL_TEX_CACHE_CTL, 1));
			OUT_RING(chan, 2);
			OUT_RING(chan, RING_3D(NV40TCL_TEX_CACHE_CTL, 1));
			OUT_RING(chan, 1);
		}
	}

	nvfx->dirty = dirty & still_dirty;

	render_temps = nvfx->state.render_temps;
	if(render_temps)
	{
		for(int i = 0; i < nvfx->framebuffer.nr_cbufs; ++i)
		{
			if(render_temps & (1 << i))
				util_dirty_surface_set_dirty(nvfx_surface_get_dirty_surfaces(nvfx->framebuffer.cbufs[i]),
						(struct util_dirty_surface*)nvfx->framebuffer.cbufs[i]);
		}

		if(render_temps & 0x80)
			util_dirty_surface_set_dirty(nvfx_surface_get_dirty_surfaces(nvfx->framebuffer.zsbuf),
					(struct util_dirty_surface*)nvfx->framebuffer.zsbuf);
	}

	return TRUE;
}

void
nvfx_state_emit(struct nvfx_context *nvfx)
{
	struct nouveau_channel* chan = nvfx->screen->base.channel;
	/* we need to ensure there is enough space to output relocations in one go */
	unsigned max_relocs = 0
	      + 16 /* vertex buffers, incl. dma flag */
	      + 2 /* index buffer plus format+dma flag */
	      + 2 * 5 /* 4 cbufs + zsbuf, plus dma objects */
	      + 2 * 16 /* fragment textures plus format+dma flag */
	      + 2 * 4 /* vertex textures plus format+dma flag */
	      + 1 /* fragprog incl dma flag */
	      ;
	MARK_RING(chan, max_relocs * 2, max_relocs * 2);
	nvfx_state_relocate(nvfx);
}

void
nvfx_state_relocate(struct nvfx_context *nvfx)
{
	nvfx_framebuffer_relocate(nvfx);
	nvfx_fragtex_relocate(nvfx);
	nvfx_fragprog_relocate(nvfx);
	if (nvfx->render_mode == HW)
	{
		nvfx_vbo_relocate(nvfx);
		if(nvfx->use_index_buffer)
			nvfx_idxbuf_relocate(nvfx);
	}
}

boolean
nvfx_state_validate(struct nvfx_context *nvfx)
{
	boolean was_sw = nvfx->fallback_swtnl ? TRUE : FALSE;

	if (nvfx->render_mode != HW) {
		/* Don't even bother trying to go back to hw if none
		 * of the states that caused swtnl previously have changed.
		 */
		if ((nvfx->fallback_swtnl & nvfx->dirty)
				!= nvfx->fallback_swtnl)
			return FALSE;

		/* Attempt to go to hwtnl again */
		nvfx->dirty |= (NVFX_NEW_VIEWPORT |
				NVFX_NEW_VERTPROG |
				NVFX_NEW_ARRAYS);
		nvfx->render_mode = HW;
	}

	if(!nvfx_state_validate_common(nvfx))
		return FALSE;

	if (was_sw)
		NOUVEAU_ERR("swtnl->hw\n");

	return TRUE;
}

boolean
nvfx_state_validate_swtnl(struct nvfx_context *nvfx)
{
	struct draw_context *draw = nvfx->draw;

	/* Setup for swtnl */
	if (nvfx->render_mode == HW) {
		NOUVEAU_ERR("hw->swtnl 0x%08x\n", nvfx->fallback_swtnl);
		nvfx->pipe.flush(&nvfx->pipe, 0, NULL);
		nvfx->dirty |= (NVFX_NEW_VIEWPORT |
				NVFX_NEW_VERTPROG |
				NVFX_NEW_ARRAYS);
		nvfx->render_mode = SWTNL;
	}

	if (nvfx->draw_dirty & NVFX_NEW_VERTPROG)
		draw_bind_vertex_shader(draw, nvfx->vertprog->draw);

	if (nvfx->draw_dirty & NVFX_NEW_RAST)
           draw_set_rasterizer_state(draw, &nvfx->rasterizer->pipe,
                                     nvfx->rasterizer);

	if (nvfx->draw_dirty & NVFX_NEW_UCP)
		draw_set_clip_state(draw, &nvfx->clip);

	if (nvfx->draw_dirty & NVFX_NEW_VIEWPORT)
		draw_set_viewport_state(draw, &nvfx->viewport);

	if (nvfx->draw_dirty & NVFX_NEW_ARRAYS) {
		draw_set_vertex_buffers(draw, nvfx->vtxbuf_nr, nvfx->vtxbuf);
		draw_set_vertex_elements(draw, nvfx->vtxelt->num_elements, nvfx->vtxelt->pipe);
	}

	nvfx_state_validate_common(nvfx);

	nvfx->draw_dirty = 0;
	return TRUE;
}