nvfx: refactor to support multiple fragment program versions
[mesa.git] / src / gallium / drivers / nvfx / nvfx_context.h
1 #ifndef __NVFX_CONTEXT_H__
2 #define __NVFX_CONTEXT_H__
3
4 #include <stdio.h>
5
6 #include "pipe/p_context.h"
7 #include "pipe/p_defines.h"
8 #include "pipe/p_state.h"
9 #include "pipe/p_compiler.h"
10
11 #include "util/u_memory.h"
12 #include "util/u_math.h"
13 #include "util/u_inlines.h"
14 #include "util/u_double_list.h"
15
16 #include "draw/draw_vertex.h"
17 #include "util/u_blitter.h"
18
19 #include "nouveau/nouveau_winsys.h"
20 #include "nouveau/nouveau_gldefs.h"
21
22 #include "nvfx_state.h"
23
24 #define NOUVEAU_ERR(fmt, args...) \
25 fprintf(stderr, "%s:%d - "fmt, __func__, __LINE__, ##args);
26 #define NOUVEAU_MSG(fmt, args...) \
27 fprintf(stderr, "nouveau: "fmt, ##args);
28
29 #include "nvfx_screen.h"
30
31 #define NVFX_NEW_BLEND (1 << 0)
32 #define NVFX_NEW_RAST (1 << 1)
33 #define NVFX_NEW_ZSA (1 << 2)
34 #define NVFX_NEW_SAMPLER (1 << 3)
35 #define NVFX_NEW_FB (1 << 4)
36 #define NVFX_NEW_STIPPLE (1 << 5)
37 #define NVFX_NEW_SCISSOR (1 << 6)
38 #define NVFX_NEW_VIEWPORT (1 << 7)
39 #define NVFX_NEW_BCOL (1 << 8)
40 #define NVFX_NEW_VERTPROG (1 << 9)
41 #define NVFX_NEW_FRAGPROG (1 << 10)
42 #define NVFX_NEW_ARRAYS (1 << 11)
43 #define NVFX_NEW_UCP (1 << 12)
44 #define NVFX_NEW_SR (1 << 13)
45 #define NVFX_NEW_VERTCONST (1 << 14)
46 #define NVFX_NEW_FRAGCONST (1 << 15)
47 #define NVFX_NEW_INDEX (1 << 16)
48 #define NVFX_NEW_SPRITE (1 << 17)
49
50 struct nvfx_rasterizer_state {
51 struct pipe_rasterizer_state pipe;
52 unsigned sb_len;
53 uint32_t sb[34];
54 };
55
56 struct nvfx_zsa_state {
57 struct pipe_depth_stencil_alpha_state pipe;
58 unsigned sb_len;
59 uint32_t sb[24];
60 };
61
62 struct nvfx_blend_state {
63 struct pipe_blend_state pipe;
64 unsigned sb_len;
65 uint32_t sb[13];
66 };
67
68
69 struct nvfx_state {
70 unsigned scissor_enabled;
71 unsigned fp_samplers;
72 unsigned render_temps;
73 };
74
75 struct nvfx_per_vertex_element {
76 unsigned idx;
77 unsigned vertex_buffer_index;
78 unsigned src_offset;
79 };
80
81 struct nvfx_low_frequency_element {
82 unsigned idx;
83 unsigned vertex_buffer_index;
84 unsigned src_offset;
85 void (*fetch_rgba_float)(float *dst, const uint8_t *src, unsigned i, unsigned j);
86 unsigned ncomp;
87 };
88
89 struct nvfx_per_instance_element {
90 struct nvfx_low_frequency_element base;
91 unsigned instance_divisor;
92 };
93
94 struct nvfx_per_vertex_buffer_info
95 {
96 unsigned vertex_buffer_index;
97 unsigned per_vertex_size;
98 };
99
100 struct nvfx_vtxelt_state {
101 struct pipe_vertex_element pipe[16];
102 unsigned num_elements;
103 unsigned vtxfmt[16];
104
105 unsigned num_per_vertex_buffer_infos;
106 struct nvfx_per_vertex_buffer_info per_vertex_buffer_info[16];
107
108 unsigned num_per_vertex;
109 struct nvfx_per_vertex_element per_vertex[16];
110
111 unsigned num_per_instance;
112 struct nvfx_per_instance_element per_instance[16];
113
114 unsigned num_constant;
115 struct nvfx_low_frequency_element constant[16];
116
117 boolean needs_translate;
118 struct translate* translate;
119
120 unsigned vertex_length;
121 unsigned max_vertices_per_packet;
122 };
123
124 struct nvfx_render_target {
125 struct nouveau_bo* bo;
126 unsigned offset;
127 unsigned pitch;
128 };
129
130 struct nvfx_context {
131 struct pipe_context pipe;
132
133 struct nouveau_winsys *nvws;
134 struct nvfx_screen *screen;
135
136 unsigned is_nv4x; /* either 0 or ~0 */
137
138 struct draw_context *draw;
139 struct blitter_context* blitter;
140 struct list_head render_cache;
141
142 /* HW state derived from pipe states */
143 struct nvfx_state state;
144 struct {
145 struct nvfx_vertex_program *vertprog;
146
147 unsigned nr_attribs;
148 unsigned hw[PIPE_MAX_SHADER_INPUTS];
149 unsigned draw[PIPE_MAX_SHADER_INPUTS];
150 unsigned emit[PIPE_MAX_SHADER_INPUTS];
151 } swtnl;
152
153 enum {
154 HW, SWTNL, SWRAST
155 } render_mode;
156 unsigned fallback_swtnl;
157
158 /* Context state */
159 unsigned dirty, draw_dirty;
160 struct pipe_scissor_state scissor;
161 unsigned stipple[32];
162 struct pipe_clip_state clip;
163 struct nvfx_vertex_program *vertprog;
164 struct nvfx_pipe_fragment_program *fragprog;
165 struct pipe_resource *constbuf[PIPE_SHADER_TYPES];
166 unsigned constbuf_nr[PIPE_SHADER_TYPES];
167 struct nvfx_rasterizer_state *rasterizer;
168 struct nvfx_zsa_state *zsa;
169 struct nvfx_blend_state *blend;
170 struct pipe_blend_color blend_colour;
171 struct pipe_stencil_ref stencil_ref;
172 struct pipe_viewport_state viewport;
173 struct pipe_framebuffer_state framebuffer;
174 struct pipe_index_buffer idxbuf;
175 struct nvfx_sampler_state *tex_sampler[PIPE_MAX_SAMPLERS];
176 struct pipe_sampler_view *fragment_sampler_views[PIPE_MAX_SAMPLERS];
177 struct nvfx_pipe_fragment_program* dummy_fs;
178
179 unsigned nr_samplers;
180 unsigned nr_textures;
181 unsigned dirty_samplers;
182 struct pipe_vertex_buffer vtxbuf[PIPE_MAX_ATTRIBS];
183 unsigned vtxbuf_nr;
184 struct nvfx_vtxelt_state *vtxelt;
185 int base_vertex;
186 boolean use_index_buffer;
187 /* -1 = hardware input setup is outdated
188 * 0 = hardware input setup is for inline vertices
189 * 1 = hardware input setup is for hardware vertices
190 */
191 int use_vertex_buffers;
192
193 unsigned hw_vtxelt_nr;
194 uint8_t hw_samplers;
195 uint32_t hw_txf[8];
196 struct nvfx_render_target hw_rt[4];
197 struct nvfx_render_target hw_zeta;
198 int hw_pointsprite_control;
199 int hw_vp_output;
200 struct nvfx_fragment_program* hw_fragprog;
201 };
202
203 static INLINE struct nvfx_context *
204 nvfx_context(struct pipe_context *pipe)
205 {
206 return (struct nvfx_context *)pipe;
207 }
208
209 extern struct nvfx_state_entry nvfx_state_blend;
210 extern struct nvfx_state_entry nvfx_state_blend_colour;
211 extern struct nvfx_state_entry nvfx_state_fragprog;
212 extern struct nvfx_state_entry nvfx_state_fragtex;
213 extern struct nvfx_state_entry nvfx_state_framebuffer;
214 extern struct nvfx_state_entry nvfx_state_rasterizer;
215 extern struct nvfx_state_entry nvfx_state_scissor;
216 extern struct nvfx_state_entry nvfx_state_sr;
217 extern struct nvfx_state_entry nvfx_state_stipple;
218 extern struct nvfx_state_entry nvfx_state_vbo;
219 extern struct nvfx_state_entry nvfx_state_vertprog;
220 extern struct nvfx_state_entry nvfx_state_viewport;
221 extern struct nvfx_state_entry nvfx_state_vtxfmt;
222 extern struct nvfx_state_entry nvfx_state_zsa;
223
224 extern void nvfx_init_query_functions(struct nvfx_context *nvfx);
225 extern void nvfx_init_surface_functions(struct nvfx_context *nvfx);
226
227 /* nvfx_context.c */
228 struct pipe_context *
229 nvfx_create(struct pipe_screen *pscreen, void *priv);
230
231 /* nvfx_clear.c */
232 extern void nvfx_clear(struct pipe_context *pipe, unsigned buffers,
233 const float *rgba, double depth, unsigned stencil);
234
235 /* nvfx_draw.c */
236 extern struct draw_stage *nvfx_draw_render_stage(struct nvfx_context *nvfx);
237 extern void nvfx_draw_vbo_swtnl(struct pipe_context *pipe, const struct pipe_draw_info* info);
238 extern void nvfx_vtxfmt_validate(struct nvfx_context *nvfx);
239
240 /* nvfx_fb.c */
241 extern int nvfx_framebuffer_prepare(struct nvfx_context *nvfx);
242 extern void nvfx_framebuffer_validate(struct nvfx_context *nvfx, unsigned prepare_result);
243 void
244 nvfx_framebuffer_relocate(struct nvfx_context *nvfx);
245
246 /* nvfx_fragprog.c */
247 extern void nvfx_fragprog_destroy(struct nvfx_context *,
248 struct nvfx_fragment_program *);
249 extern void nvfx_fragprog_validate(struct nvfx_context *nvfx);
250 extern void nvfx_fragprog_relocate(struct nvfx_context *nvfx);
251 extern void nvfx_init_fragprog_functions(struct nvfx_context *nvfx);
252
253 /* nvfx_fragtex.c */
254 extern void nvfx_init_sampling_functions(struct nvfx_context *nvfx);
255 extern void nvfx_fragtex_validate(struct nvfx_context *nvfx);
256 extern void nvfx_fragtex_relocate(struct nvfx_context *nvfx);
257
258 struct nvfx_sampler_view;
259
260 /* nv30_fragtex.c */
261 extern void
262 nv30_sampler_state_init(struct pipe_context *pipe,
263 struct nvfx_sampler_state *ps,
264 const struct pipe_sampler_state *cso);
265 extern void
266 nv30_sampler_view_init(struct pipe_context *pipe,
267 struct nvfx_sampler_view *sv);
268 extern void nv30_fragtex_set(struct nvfx_context *nvfx, int unit);
269
270 /* nv40_fragtex.c */
271 extern void
272 nv40_sampler_state_init(struct pipe_context *pipe,
273 struct nvfx_sampler_state *ps,
274 const struct pipe_sampler_state *cso);
275 extern void
276 nv40_sampler_view_init(struct pipe_context *pipe,
277 struct nvfx_sampler_view *sv);
278 extern void nv40_fragtex_set(struct nvfx_context *nvfx, int unit);
279
280 /* nvfx_state.c */
281 extern void nvfx_init_state_functions(struct nvfx_context *nvfx);
282 extern void nvfx_state_scissor_validate(struct nvfx_context *nvfx);
283 extern void nvfx_state_stipple_validate(struct nvfx_context *nvfx);
284 extern void nvfx_state_blend_validate(struct nvfx_context *nvfx);
285 extern void nvfx_state_blend_colour_validate(struct nvfx_context *nvfx);
286 extern void nvfx_state_viewport_validate(struct nvfx_context *nvfx);
287 extern void nvfx_state_rasterizer_validate(struct nvfx_context *nvfx);
288 extern void nvfx_state_sr_validate(struct nvfx_context *nvfx);
289 extern void nvfx_state_zsa_validate(struct nvfx_context *nvfx);
290
291 /* nvfx_state_emit.c */
292 extern void nvfx_state_relocate(struct nvfx_context *nvfx);
293 extern boolean nvfx_state_validate(struct nvfx_context *nvfx);
294 extern boolean nvfx_state_validate_swtnl(struct nvfx_context *nvfx);
295 extern void nvfx_state_emit(struct nvfx_context *nvfx);
296
297 /* nvfx_transfer.c */
298 extern void nvfx_init_transfer_functions(struct pipe_context *pipe);
299
300 /* nvfx_vbo.c */
301 extern boolean nvfx_vbo_validate(struct nvfx_context *nvfx);
302 extern void nvfx_vbo_relocate(struct nvfx_context *nvfx);
303 extern void nvfx_idxbuf_validate(struct nvfx_context* nvfx);
304 extern void nvfx_idxbuf_relocate(struct nvfx_context* nvfx);
305 extern void nvfx_draw_vbo(struct pipe_context *pipe,
306 const struct pipe_draw_info *info);
307 extern void nvfx_init_vbo_functions(struct nvfx_context *nvfx);
308 extern unsigned nvfx_vertex_formats[];
309
310 /* nvfx_vertprog.c */
311 extern boolean nvfx_vertprog_validate(struct nvfx_context *nvfx);
312 extern void nvfx_vertprog_destroy(struct nvfx_context *,
313 struct nvfx_vertex_program *);
314 extern void nvfx_init_vertprog_functions(struct nvfx_context *nvfx);
315
316 /* nvfx_push.c */
317 extern void nvfx_push_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info);
318
319 /* must WAIT_RING(chan, ncomp + 1) or equivalent beforehand! */
320 static inline void nvfx_emit_vtx_attr(struct nouveau_channel* chan, unsigned attrib, const float* v, unsigned ncomp)
321 {
322 switch (ncomp) {
323 case 4:
324 OUT_RING(chan, RING_3D(NV34TCL_VTX_ATTR_4F_X(attrib), 4));
325 OUT_RING(chan, fui(v[0]));
326 OUT_RING(chan, fui(v[1]));
327 OUT_RING(chan, fui(v[2]));
328 OUT_RING(chan, fui(v[3]));
329 break;
330 case 3:
331 OUT_RING(chan, RING_3D(NV34TCL_VTX_ATTR_3F_X(attrib), 3));
332 OUT_RING(chan, fui(v[0]));
333 OUT_RING(chan, fui(v[1]));
334 OUT_RING(chan, fui(v[2]));
335 break;
336 case 2:
337 OUT_RING(chan, RING_3D(NV34TCL_VTX_ATTR_2F_X(attrib), 2));
338 OUT_RING(chan, fui(v[0]));
339 OUT_RING(chan, fui(v[1]));
340 break;
341 case 1:
342 OUT_RING(chan, RING_3D(NV34TCL_VTX_ATTR_1F(attrib), 1));
343 OUT_RING(chan, fui(v[0]));
344 break;
345 }
346 }
347
348 #endif