nvfx: rewrite draw code and buffer code
[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
49 struct nvfx_rasterizer_state {
50 struct pipe_rasterizer_state pipe;
51 unsigned sb_len;
52 uint32_t sb[32];
53 };
54
55 struct nvfx_zsa_state {
56 struct pipe_depth_stencil_alpha_state pipe;
57 unsigned sb_len;
58 uint32_t sb[26];
59 };
60
61 struct nvfx_blend_state {
62 struct pipe_blend_state pipe;
63 unsigned sb_len;
64 uint32_t sb[13];
65 };
66
67
68 struct nvfx_state {
69 unsigned scissor_enabled;
70 unsigned stipple_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_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 unsigned nr_samplers;
178 unsigned nr_textures;
179 unsigned dirty_samplers;
180 struct pipe_vertex_buffer vtxbuf[PIPE_MAX_ATTRIBS];
181 unsigned vtxbuf_nr;
182 struct nvfx_vtxelt_state *vtxelt;
183 int base_vertex;
184 boolean use_index_buffer;
185 /* -1 = hardware input setup is outdated
186 * 0 = hardware input setup is for inline vertices
187 * 1 = hardware input setup is for hardware vertices
188 */
189 int use_vertex_buffers;
190
191 unsigned hw_vtxelt_nr;
192 uint8_t hw_samplers;
193 uint32_t hw_txf[8];
194 struct nvfx_render_target hw_rt[4];
195 struct nvfx_render_target hw_zeta;
196 };
197
198 static INLINE struct nvfx_context *
199 nvfx_context(struct pipe_context *pipe)
200 {
201 return (struct nvfx_context *)pipe;
202 }
203
204 extern struct nvfx_state_entry nvfx_state_blend;
205 extern struct nvfx_state_entry nvfx_state_blend_colour;
206 extern struct nvfx_state_entry nvfx_state_fragprog;
207 extern struct nvfx_state_entry nvfx_state_fragtex;
208 extern struct nvfx_state_entry nvfx_state_framebuffer;
209 extern struct nvfx_state_entry nvfx_state_rasterizer;
210 extern struct nvfx_state_entry nvfx_state_scissor;
211 extern struct nvfx_state_entry nvfx_state_sr;
212 extern struct nvfx_state_entry nvfx_state_stipple;
213 extern struct nvfx_state_entry nvfx_state_vbo;
214 extern struct nvfx_state_entry nvfx_state_vertprog;
215 extern struct nvfx_state_entry nvfx_state_viewport;
216 extern struct nvfx_state_entry nvfx_state_vtxfmt;
217 extern struct nvfx_state_entry nvfx_state_zsa;
218
219 extern void nvfx_init_query_functions(struct nvfx_context *nvfx);
220 extern void nvfx_init_surface_functions(struct nvfx_context *nvfx);
221
222 /* nvfx_context.c */
223 struct pipe_context *
224 nvfx_create(struct pipe_screen *pscreen, void *priv);
225
226 /* nvfx_clear.c */
227 extern void nvfx_clear(struct pipe_context *pipe, unsigned buffers,
228 const float *rgba, double depth, unsigned stencil);
229
230 /* nvfx_draw.c */
231 extern struct draw_stage *nvfx_draw_render_stage(struct nvfx_context *nvfx);
232 extern void nvfx_draw_vbo_swtnl(struct pipe_context *pipe, const struct pipe_draw_info* info);
233 extern void nvfx_vtxfmt_validate(struct nvfx_context *nvfx);
234
235 /* nvfx_fb.c */
236 extern int nvfx_framebuffer_prepare(struct nvfx_context *nvfx);
237 extern void nvfx_framebuffer_validate(struct nvfx_context *nvfx, unsigned prepare_result);
238 void
239 nvfx_framebuffer_relocate(struct nvfx_context *nvfx);
240
241 /* nvfx_fragprog.c */
242 extern void nvfx_fragprog_destroy(struct nvfx_context *,
243 struct nvfx_fragment_program *);
244 extern void nvfx_fragprog_validate(struct nvfx_context *nvfx);
245 extern void
246 nvfx_fragprog_relocate(struct nvfx_context *nvfx);
247
248 /* nvfx_fragtex.c */
249 extern void nvfx_init_sampling_functions(struct nvfx_context *nvfx);
250 extern void nvfx_fragtex_validate(struct nvfx_context *nvfx);
251 extern void nvfx_fragtex_relocate(struct nvfx_context *nvfx);
252
253 struct nvfx_sampler_view;
254
255 /* nv30_fragtex.c */
256 extern void
257 nv30_sampler_state_init(struct pipe_context *pipe,
258 struct nvfx_sampler_state *ps,
259 const struct pipe_sampler_state *cso);
260 extern void
261 nv30_sampler_view_init(struct pipe_context *pipe,
262 struct nvfx_sampler_view *sv);
263 extern void nv30_fragtex_set(struct nvfx_context *nvfx, int unit);
264
265 /* nv40_fragtex.c */
266 extern void
267 nv40_sampler_state_init(struct pipe_context *pipe,
268 struct nvfx_sampler_state *ps,
269 const struct pipe_sampler_state *cso);
270 extern void
271 nv40_sampler_view_init(struct pipe_context *pipe,
272 struct nvfx_sampler_view *sv);
273 extern void nv40_fragtex_set(struct nvfx_context *nvfx, int unit);
274
275 /* nvfx_state.c */
276 extern void nvfx_init_state_functions(struct nvfx_context *nvfx);
277 extern void nvfx_state_scissor_validate(struct nvfx_context *nvfx);
278 extern void nvfx_state_stipple_validate(struct nvfx_context *nvfx);
279 extern void nvfx_state_blend_validate(struct nvfx_context *nvfx);
280 extern void nvfx_state_blend_colour_validate(struct nvfx_context *nvfx);
281 extern void nvfx_state_viewport_validate(struct nvfx_context *nvfx);
282 extern void nvfx_state_rasterizer_validate(struct nvfx_context *nvfx);
283 extern void nvfx_state_sr_validate(struct nvfx_context *nvfx);
284 extern void nvfx_state_zsa_validate(struct nvfx_context *nvfx);
285
286 /* nvfx_state_emit.c */
287 extern void nvfx_state_relocate(struct nvfx_context *nvfx);
288 extern boolean nvfx_state_validate(struct nvfx_context *nvfx);
289 extern boolean nvfx_state_validate_swtnl(struct nvfx_context *nvfx);
290 extern void nvfx_state_emit(struct nvfx_context *nvfx);
291
292 /* nvfx_transfer.c */
293 extern void nvfx_init_transfer_functions(struct pipe_context *pipe);
294
295 /* nvfx_vbo.c */
296 extern boolean nvfx_vbo_validate(struct nvfx_context *nvfx);
297 extern void nvfx_vbo_relocate(struct nvfx_context *nvfx);
298 extern void nvfx_idxbuf_validate(struct nvfx_context* nvfx);
299 extern void nvfx_idxbuf_relocate(struct nvfx_context* nvfx);
300 extern void nvfx_draw_vbo(struct pipe_context *pipe,
301 const struct pipe_draw_info *info);
302 extern void nvfx_init_vbo_functions(struct nvfx_context *nvfx);
303 extern unsigned nvfx_vertex_formats[];
304
305 /* nvfx_vertprog.c */
306 extern boolean nvfx_vertprog_validate(struct nvfx_context *nvfx);
307 extern void nvfx_vertprog_destroy(struct nvfx_context *,
308 struct nvfx_vertex_program *);
309
310 /* nvfx_push.c */
311 extern void nvfx_push_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info);
312
313 /* must WAIT_RING(chan, ncomp + 1) or equivalent beforehand! */
314 static inline void nvfx_emit_vtx_attr(struct nouveau_channel* chan, unsigned attrib, float* v, unsigned ncomp)
315 {
316 switch (ncomp) {
317 case 4:
318 OUT_RING(chan, RING_3D(NV34TCL_VTX_ATTR_4F_X(attrib), 4));
319 OUT_RING(chan, fui(v[0]));
320 OUT_RING(chan, fui(v[1]));
321 OUT_RING(chan, fui(v[2]));
322 OUT_RING(chan, fui(v[3]));
323 break;
324 case 3:
325 OUT_RING(chan, RING_3D(NV34TCL_VTX_ATTR_3F_X(attrib), 3));
326 OUT_RING(chan, fui(v[0]));
327 OUT_RING(chan, fui(v[1]));
328 OUT_RING(chan, fui(v[2]));
329 break;
330 case 2:
331 OUT_RING(chan, RING_3D(NV34TCL_VTX_ATTR_2F_X(attrib), 2));
332 OUT_RING(chan, fui(v[0]));
333 OUT_RING(chan, fui(v[1]));
334 break;
335 case 1:
336 OUT_RING(chan, RING_3D(NV34TCL_VTX_ATTR_1F(attrib), 1));
337 OUT_RING(chan, fui(v[0]));
338 break;
339 }
340 }
341
342 #endif