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