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