6f631b993c0af42331cfdd4d8e9fa05ee1bc10f2
[mesa.git] / src / gallium / drivers / nouveau / nvc0 / nvc0_context.h
1 #ifndef __NVC0_CONTEXT_H__
2 #define __NVC0_CONTEXT_H__
3
4 #include "pipe/p_context.h"
5 #include "pipe/p_defines.h"
6 #include "pipe/p_state.h"
7
8 #include "util/u_memory.h"
9 #include "util/u_math.h"
10 #include "util/u_inlines.h"
11 #include "util/u_dynarray.h"
12
13 #include "nvc0/nvc0_winsys.h"
14 #include "nvc0/nvc0_stateobj.h"
15 #include "nvc0/nvc0_screen.h"
16 #include "nvc0/nvc0_program.h"
17 #include "nvc0/nvc0_resource.h"
18 #include "nvc0/nvc0_query.h"
19
20 #include "nv50/nv50_transfer.h"
21
22 #include "nouveau_context.h"
23 #include "nouveau_debug.h"
24
25 #include "nv50/nv50_3ddefs.xml.h"
26 #include "nvc0/nvc0_3d.xml.h"
27 #include "nv50/nv50_2d.xml.h"
28 #include "nvc0/nvc0_m2mf.xml.h"
29 #include "nvc0/nve4_p2mf.xml.h"
30 #include "nvc0/nvc0_compute.xml.h"
31 #include "nvc0/nvc0_macros.h"
32
33 /* NOTE: must keep NVC0_NEW_3D_...PROG in consecutive bits in this order */
34 #define NVC0_NEW_3D_BLEND (1 << 0)
35 #define NVC0_NEW_3D_RASTERIZER (1 << 1)
36 #define NVC0_NEW_3D_ZSA (1 << 2)
37 #define NVC0_NEW_3D_VERTPROG (1 << 3)
38 #define NVC0_NEW_3D_TCTLPROG (1 << 4)
39 #define NVC0_NEW_3D_TEVLPROG (1 << 5)
40 #define NVC0_NEW_3D_GMTYPROG (1 << 6)
41 #define NVC0_NEW_3D_FRAGPROG (1 << 7)
42 #define NVC0_NEW_3D_BLEND_COLOUR (1 << 8)
43 #define NVC0_NEW_3D_STENCIL_REF (1 << 9)
44 #define NVC0_NEW_3D_CLIP (1 << 10)
45 #define NVC0_NEW_3D_SAMPLE_MASK (1 << 11)
46 #define NVC0_NEW_3D_FRAMEBUFFER (1 << 12)
47 #define NVC0_NEW_3D_STIPPLE (1 << 13)
48 #define NVC0_NEW_3D_SCISSOR (1 << 14)
49 #define NVC0_NEW_3D_VIEWPORT (1 << 15)
50 #define NVC0_NEW_3D_ARRAYS (1 << 16)
51 #define NVC0_NEW_3D_VERTEX (1 << 17)
52 #define NVC0_NEW_3D_CONSTBUF (1 << 18)
53 #define NVC0_NEW_3D_TEXTURES (1 << 19)
54 #define NVC0_NEW_3D_SAMPLERS (1 << 20)
55 #define NVC0_NEW_3D_TFB_TARGETS (1 << 21)
56
57 #define NVC0_NEW_3D_SURFACES (1 << 23)
58 #define NVC0_NEW_3D_MIN_SAMPLES (1 << 24)
59 #define NVC0_NEW_3D_TESSFACTOR (1 << 25)
60 #define NVC0_NEW_3D_BUFFERS (1 << 26)
61 #define NVC0_NEW_3D_DRIVERCONST (1 << 27)
62 #define NVC0_NEW_3D_WINDOW_RECTS (1 << 28)
63
64 #define NVC0_NEW_CP_PROGRAM (1 << 0)
65 #define NVC0_NEW_CP_SURFACES (1 << 1)
66 #define NVC0_NEW_CP_TEXTURES (1 << 2)
67 #define NVC0_NEW_CP_SAMPLERS (1 << 3)
68 #define NVC0_NEW_CP_CONSTBUF (1 << 4)
69 #define NVC0_NEW_CP_GLOBALS (1 << 5)
70 #define NVC0_NEW_CP_DRIVERCONST (1 << 6)
71 #define NVC0_NEW_CP_BUFFERS (1 << 7)
72
73 /* 3d bufctx (during draw_vbo, blit_3d) */
74 #define NVC0_BIND_3D_FB 0
75 #define NVC0_BIND_3D_VTX 1
76 #define NVC0_BIND_3D_VTX_TMP 2
77 #define NVC0_BIND_3D_IDX 3
78 #define NVC0_BIND_3D_TEX(s, i) ( 4 + 32 * (s) + (i))
79 #define NVC0_BIND_3D_CB(s, i) (164 + 16 * (s) + (i))
80 #define NVC0_BIND_3D_TFB 244
81 #define NVC0_BIND_3D_SUF 245
82 #define NVC0_BIND_3D_BUF 246
83 #define NVC0_BIND_3D_SCREEN 247
84 #define NVC0_BIND_3D_TLS 249
85 #define NVC0_BIND_3D_TEXT 250
86 #define NVC0_BIND_3D_COUNT 251
87
88 /* compute bufctx (during launch_grid) */
89 #define NVC0_BIND_CP_CB(i) ( 0 + (i))
90 #define NVC0_BIND_CP_TEX(i) ( 16 + (i))
91 #define NVC0_BIND_CP_SUF 48
92 #define NVC0_BIND_CP_GLOBAL 49
93 #define NVC0_BIND_CP_DESC 50
94 #define NVC0_BIND_CP_SCREEN 51
95 #define NVC0_BIND_CP_QUERY 52
96 #define NVC0_BIND_CP_BUF 53
97 #define NVC0_BIND_CP_TEXT 54
98 #define NVC0_BIND_CP_COUNT 55
99
100 /* bufctx for other operations */
101 #define NVC0_BIND_2D 0
102 #define NVC0_BIND_M2MF 0
103 #define NVC0_BIND_FENCE 1
104
105 /* 6 user uniform buffers, at 64K each */
106 #define NVC0_CB_USR_INFO(s) (s << 16)
107 #define NVC0_CB_USR_SIZE (6 << 16)
108 /* 6 driver constbuts, at 2K each */
109 #define NVC0_CB_AUX_INFO(s) NVC0_CB_USR_SIZE + (s << 11)
110 #define NVC0_CB_AUX_SIZE (1 << 11)
111 /* XXX: Figure out what this UNK data is. */
112 #define NVC0_CB_AUX_UNK_INFO 0x000
113 #define NVC0_CB_AUX_UNK_SIZE (8 * 4)
114 /* 40 textures handles (8 for GM107+ images only), at 1 32-bits integer each */
115 #define NVC0_CB_AUX_TEX_INFO(i) 0x020 + (i) * 4
116 #define NVC0_CB_AUX_TEX_SIZE (40 * 4)
117 /* 8 sets of 32-bits coordinate offsets */
118 #define NVC0_CB_AUX_MS_INFO 0x0c0
119 #define NVC0_CB_AUX_MS_SIZE (8 * 2 * 4)
120 /* block/grid size, at 3 32-bits integers each, gridid and work_dim */
121 #define NVC0_CB_AUX_GRID_INFO(i) 0x100 + (i) * 4 /* CP */
122 #define NVC0_CB_AUX_GRID_SIZE (8 * 4)
123 /* FB texture handle */
124 #define NVC0_CB_AUX_FB_TEX_INFO 0x100 /* FP */
125 #define NVC0_CB_AUX_FB_TEX_SIZE (4)
126 /* 8 user clip planes, at 4 32-bits floats each */
127 #define NVC0_CB_AUX_UCP_INFO 0x120
128 #define NVC0_CB_AUX_UCP_SIZE (PIPE_MAX_CLIP_PLANES * 4 * 4)
129 /* 13 ubos, at 4 32-bits integer each */
130 #define NVC0_CB_AUX_UBO_INFO(i) 0x120 + (i) * 4 * 4 /* CP */
131 #define NVC0_CB_AUX_UBO_SIZE ((NVC0_MAX_PIPE_CONSTBUFS - 1) * 4 * 4)
132 /* 8 sets of 32-bits integer pairs sample offsets */
133 #define NVC0_CB_AUX_SAMPLE_INFO 0x1a0 /* FP */
134 #define NVC0_CB_AUX_SAMPLE_SIZE (8 * 4 * 2)
135 /* draw parameters (index bais, base instance, drawid) */
136 #define NVC0_CB_AUX_DRAW_INFO 0x1a0 /* VP */
137 /* 32 user buffers, at 4 32-bits integers each */
138 #define NVC0_CB_AUX_BUF_INFO(i) 0x220 + (i) * 4 * 4
139 #define NVC0_CB_AUX_BUF_SIZE (NVC0_MAX_BUFFERS * 4 * 4)
140 /* 8 surfaces, at 16 32-bits integers each */
141 #define NVC0_CB_AUX_SU_INFO(i) 0x420 + (i) * 16 * 4
142 #define NVC0_CB_AUX_SU_SIZE (NVC0_MAX_IMAGES * 16 * 4)
143 /* 1 64-bits address and 1 32-bits sequence */
144 #define NVC0_CB_AUX_MP_INFO 0x620
145 #define NVC0_CB_AUX_MP_SIZE 3 * 4
146 /* 4 32-bits floats for the vertex runout, put at the end */
147 #define NVC0_CB_AUX_RUNOUT_INFO NVC0_CB_USR_SIZE + (NVC0_CB_AUX_SIZE * 6)
148
149 struct nvc0_blitctx;
150
151 bool nvc0_blitctx_create(struct nvc0_context *);
152 void nvc0_blitctx_destroy(struct nvc0_context *);
153
154 struct nvc0_context {
155 struct nouveau_context base;
156
157 struct nouveau_bufctx *bufctx_3d;
158 struct nouveau_bufctx *bufctx;
159 struct nouveau_bufctx *bufctx_cp;
160
161 struct nvc0_screen *screen;
162
163 void (*m2mf_copy_rect)(struct nvc0_context *,
164 const struct nv50_m2mf_rect *dst,
165 const struct nv50_m2mf_rect *src,
166 uint32_t nblocksx, uint32_t nblocksy);
167
168 uint32_t dirty_3d; /* dirty flags for 3d state */
169 uint32_t dirty_cp; /* dirty flags for compute state */
170
171 struct nvc0_graph_state state;
172
173 struct nvc0_blend_stateobj *blend;
174 struct nvc0_rasterizer_stateobj *rast;
175 struct nvc0_zsa_stateobj *zsa;
176 struct nvc0_vertex_stateobj *vertex;
177
178 struct nvc0_program *vertprog;
179 struct nvc0_program *tctlprog;
180 struct nvc0_program *tevlprog;
181 struct nvc0_program *gmtyprog;
182 struct nvc0_program *fragprog;
183 struct nvc0_program *compprog;
184
185 struct nvc0_program *tcp_empty;
186
187 struct nvc0_constbuf constbuf[6][NVC0_MAX_PIPE_CONSTBUFS];
188 uint16_t constbuf_dirty[6];
189 uint16_t constbuf_valid[6];
190 uint16_t constbuf_coherent[6];
191 bool cb_dirty;
192
193 struct pipe_vertex_buffer vtxbuf[PIPE_MAX_ATTRIBS];
194 unsigned num_vtxbufs;
195 uint32_t vtxbufs_coherent;
196 uint32_t constant_vbos;
197 uint32_t vbo_user; /* bitmask of vertex buffers pointing to user memory */
198 uint32_t vb_elt_first; /* from pipe_draw_info, for vertex upload */
199 uint32_t vb_elt_limit; /* max - min element (count - 1) */
200 uint32_t instance_off; /* current base vertex for instanced arrays */
201 uint32_t instance_max; /* last instance for current draw call */
202
203 struct pipe_sampler_view *textures[6][PIPE_MAX_SAMPLERS];
204 unsigned num_textures[6];
205 uint32_t textures_dirty[6];
206 uint32_t textures_coherent[6];
207 struct nv50_tsc_entry *samplers[6][PIPE_MAX_SAMPLERS];
208 unsigned num_samplers[6];
209 uint32_t samplers_dirty[6];
210 bool seamless_cube_map;
211 struct pipe_sampler_view *fbtexture;
212
213 uint32_t tex_handles[6][PIPE_MAX_SAMPLERS]; /* for nve4 */
214
215 struct pipe_framebuffer_state framebuffer;
216 struct pipe_blend_color blend_colour;
217 struct pipe_stencil_ref stencil_ref;
218 struct pipe_poly_stipple stipple;
219 struct pipe_scissor_state scissors[NVC0_MAX_VIEWPORTS];
220 unsigned scissors_dirty;
221 struct pipe_viewport_state viewports[NVC0_MAX_VIEWPORTS];
222 unsigned viewports_dirty;
223 struct pipe_clip_state clip;
224 struct nvc0_window_rect_stateobj window_rect;
225
226 unsigned sample_mask;
227 unsigned min_samples;
228
229 float default_tess_outer[4];
230 float default_tess_inner[2];
231
232 bool vbo_push_hint;
233
234 uint8_t tfbbuf_dirty;
235 struct pipe_stream_output_target *tfbbuf[4];
236 unsigned num_tfbbufs;
237
238 struct pipe_query *cond_query;
239 bool cond_cond; /* inverted rendering condition */
240 uint cond_mode;
241 uint32_t cond_condmode; /* the calculated condition */
242
243 struct nvc0_blitctx *blit;
244
245 /* NOTE: some of these surfaces may reference buffers */
246 struct pipe_surface *surfaces[2][NVC0_MAX_SURFACE_SLOTS];
247 uint16_t surfaces_dirty[2];
248 uint16_t surfaces_valid[2];
249
250 struct pipe_shader_buffer buffers[6][NVC0_MAX_BUFFERS];
251 uint32_t buffers_dirty[6];
252 uint32_t buffers_valid[6];
253
254 struct pipe_image_view images[6][NVC0_MAX_IMAGES];
255 struct pipe_sampler_view *images_tic[6][NVC0_MAX_IMAGES]; /* GM107+ */
256 uint16_t images_dirty[6];
257 uint16_t images_valid[6];
258
259 struct util_dynarray global_residents;
260 };
261
262 static inline struct nvc0_context *
263 nvc0_context(struct pipe_context *pipe)
264 {
265 return (struct nvc0_context *)pipe;
266 }
267
268 static inline unsigned
269 nvc0_shader_stage(unsigned pipe)
270 {
271 switch (pipe) {
272 case PIPE_SHADER_VERTEX: return 0;
273 case PIPE_SHADER_TESS_CTRL: return 1;
274 case PIPE_SHADER_TESS_EVAL: return 2;
275 case PIPE_SHADER_GEOMETRY: return 3;
276 case PIPE_SHADER_FRAGMENT: return 4;
277 case PIPE_SHADER_COMPUTE: return 5;
278 default:
279 assert(!"invalid PIPE_SHADER type");
280 return 0;
281 }
282 }
283
284
285 /* nvc0_context.c */
286 struct pipe_context *nvc0_create(struct pipe_screen *, void *, unsigned flags);
287 void nvc0_bufctx_fence(struct nvc0_context *, struct nouveau_bufctx *,
288 bool on_flush);
289 void nvc0_default_kick_notify(struct nouveau_pushbuf *);
290 const void *nvc0_get_sample_locations(unsigned);
291
292 /* nvc0_draw.c */
293 extern struct draw_stage *nvc0_draw_render_stage(struct nvc0_context *);
294
295 /* nvc0_program.c */
296 bool nvc0_program_translate(struct nvc0_program *, uint16_t chipset,
297 struct pipe_debug_callback *);
298 bool nvc0_program_upload(struct nvc0_context *, struct nvc0_program *);
299 void nvc0_program_destroy(struct nvc0_context *, struct nvc0_program *);
300 void nvc0_program_library_upload(struct nvc0_context *);
301 uint32_t nvc0_program_symbol_offset(const struct nvc0_program *,
302 uint32_t label);
303 void nvc0_program_init_tcp_empty(struct nvc0_context *);
304
305 /* nvc0_shader_state.c */
306 void nvc0_vertprog_validate(struct nvc0_context *);
307 void nvc0_tctlprog_validate(struct nvc0_context *);
308 void nvc0_tevlprog_validate(struct nvc0_context *);
309 void nvc0_gmtyprog_validate(struct nvc0_context *);
310 void nvc0_fragprog_validate(struct nvc0_context *);
311 void nvc0_compprog_validate(struct nvc0_context *);
312
313 void nvc0_tfb_validate(struct nvc0_context *);
314 void nvc0_layer_validate(struct nvc0_context *);
315
316 /* nvc0_state.c */
317 extern void nvc0_init_state_functions(struct nvc0_context *);
318
319 /* nvc0_state_validate.c */
320 struct nvc0_state_validate {
321 void (*func)(struct nvc0_context *);
322 uint32_t states;
323 };
324
325 bool nvc0_state_validate(struct nvc0_context *, uint32_t,
326 struct nvc0_state_validate *, int, uint32_t *,
327 struct nouveau_bufctx *);
328 bool nvc0_state_validate_3d(struct nvc0_context *, uint32_t);
329
330 /* nvc0_surface.c */
331 extern void nvc0_clear(struct pipe_context *, unsigned buffers,
332 const union pipe_color_union *color,
333 double depth, unsigned stencil);
334 extern void nvc0_init_surface_functions(struct nvc0_context *);
335
336 /* nvc0_tex.c */
337 bool nvc0_validate_tic(struct nvc0_context *nvc0, int s);
338 bool nvc0_validate_tsc(struct nvc0_context *nvc0, int s);
339 bool nve4_validate_tsc(struct nvc0_context *nvc0, int s);
340 void nvc0_validate_suf(struct nvc0_context *nvc0, int s);
341 void nvc0_validate_textures(struct nvc0_context *);
342 void nvc0_validate_samplers(struct nvc0_context *);
343 void nve4_set_tex_handles(struct nvc0_context *);
344 void nvc0_validate_surfaces(struct nvc0_context *);
345 void nve4_set_surface_info(struct nouveau_pushbuf *, struct pipe_image_view *,
346 struct nvc0_context *);
347 void nvc0_mark_image_range_valid(const struct pipe_image_view *);
348 void nvc0_update_tic(struct nvc0_context *, struct nv50_tic_entry *,
349 struct nv04_resource *);
350
351 struct pipe_sampler_view *
352 nvc0_create_texture_view(struct pipe_context *,
353 struct pipe_resource *,
354 const struct pipe_sampler_view *,
355 uint32_t flags,
356 enum pipe_texture_target);
357 struct pipe_sampler_view *
358 nvc0_create_sampler_view(struct pipe_context *,
359 struct pipe_resource *,
360 const struct pipe_sampler_view *);
361 struct pipe_sampler_view *
362 gm107_create_texture_view_from_image(struct pipe_context *,
363 const struct pipe_image_view *);
364
365 /* nvc0_transfer.c */
366 void
367 nvc0_init_transfer_functions(struct nvc0_context *);
368
369 void
370 nvc0_m2mf_push_linear(struct nouveau_context *nv,
371 struct nouveau_bo *dst, unsigned offset, unsigned domain,
372 unsigned size, const void *data);
373 void
374 nve4_p2mf_push_linear(struct nouveau_context *nv,
375 struct nouveau_bo *dst, unsigned offset, unsigned domain,
376 unsigned size, const void *data);
377 void
378 nvc0_cb_bo_push(struct nouveau_context *,
379 struct nouveau_bo *bo, unsigned domain,
380 unsigned base, unsigned size,
381 unsigned offset, unsigned words, const uint32_t *data);
382
383 /* nvc0_vbo.c */
384 void nvc0_draw_vbo(struct pipe_context *, const struct pipe_draw_info *);
385
386 void *
387 nvc0_vertex_state_create(struct pipe_context *pipe,
388 unsigned num_elements,
389 const struct pipe_vertex_element *elements);
390 void
391 nvc0_vertex_state_delete(struct pipe_context *pipe, void *hwcso);
392
393 void nvc0_vertex_arrays_validate(struct nvc0_context *);
394
395 void nvc0_idxbuf_validate(struct nvc0_context *);
396
397 /* nvc0_video.c */
398 struct pipe_video_codec *
399 nvc0_create_decoder(struct pipe_context *context,
400 const struct pipe_video_codec *templ);
401
402 struct pipe_video_buffer *
403 nvc0_video_buffer_create(struct pipe_context *pipe,
404 const struct pipe_video_buffer *templat);
405
406 /* nvc0_push.c */
407 void nvc0_push_vbo(struct nvc0_context *, const struct pipe_draw_info *);
408
409 /* nve4_compute.c */
410 void nve4_launch_grid(struct pipe_context *, const struct pipe_grid_info *);
411
412 /* nvc0_compute.c */
413 void nvc0_launch_grid(struct pipe_context *, const struct pipe_grid_info *);
414 void nvc0_compute_validate_globals(struct nvc0_context *);
415
416 #endif