r600g: hide radeon_ctx inside winsys.
[mesa.git] / src / gallium / drivers / r600 / r600_context.h
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #ifndef R600_CONTEXT_H
24 #define R600_CONTEXT_H
25
26 #include <stdio.h>
27 #include <pipe/p_state.h>
28 #include <pipe/p_context.h>
29 #include <tgsi/tgsi_scan.h>
30 #include <tgsi/tgsi_parse.h>
31 #include <tgsi/tgsi_util.h>
32 #include <util/u_blitter.h>
33 #include <util/u_double_list.h>
34 #include "radeon.h"
35 #include "r600_shader.h"
36
37 #define R600_QUERY_STATE_STARTED (1 << 0)
38 #define R600_QUERY_STATE_ENDED (1 << 1)
39 #define R600_QUERY_STATE_SUSPENDED (1 << 2)
40
41 struct r600_query {
42 u64 result;
43 /* The kind of query. Currently only OQ is supported. */
44 unsigned type;
45 /* How many results have been written, in dwords. It's incremented
46 * after end_query and flush. */
47 unsigned num_results;
48 /* if we've flushed the query */
49 boolean flushed;
50 unsigned state;
51 /* The buffer where query results are stored. */
52 struct radeon_bo *buffer;
53 unsigned buffer_size;
54 /* linked list of queries */
55 struct list_head list;
56 struct radeon_state rstate;
57 };
58
59 /* XXX move this to a more appropriate place */
60 union pipe_states {
61 struct pipe_rasterizer_state rasterizer;
62 struct pipe_poly_stipple poly_stipple;
63 struct pipe_scissor_state scissor;
64 struct pipe_clip_state clip;
65 struct pipe_shader_state shader;
66 struct pipe_depth_state depth;
67 struct pipe_stencil_state stencil;
68 struct pipe_alpha_state alpha;
69 struct pipe_depth_stencil_alpha_state dsa;
70 struct pipe_blend_state blend;
71 struct pipe_blend_color blend_color;
72 struct pipe_stencil_ref stencil_ref;
73 struct pipe_framebuffer_state framebuffer;
74 struct pipe_sampler_state sampler;
75 struct pipe_sampler_view sampler_view;
76 struct pipe_viewport_state viewport;
77 };
78
79 enum pipe_state_type {
80 pipe_rasterizer_type = 1,
81 pipe_poly_stipple_type,
82 pipe_scissor_type,
83 pipe_clip_type,
84 pipe_shader_type,
85 pipe_depth_type,
86 pipe_stencil_type,
87 pipe_alpha_type,
88 pipe_dsa_type,
89 pipe_blend_type,
90 pipe_stencil_ref_type,
91 pipe_framebuffer_type,
92 pipe_sampler_type,
93 pipe_sampler_view_type,
94 pipe_viewport_type,
95 pipe_type_count
96 };
97
98 #define R600_MAX_RSTATE 16
99
100 struct r600_context_state {
101 union pipe_states state;
102 unsigned refcount;
103 unsigned type;
104 struct radeon_state rstate[R600_MAX_RSTATE];
105 struct r600_shader shader;
106 struct radeon_bo *bo;
107 unsigned nrstate;
108 };
109
110 struct r600_vertex_element
111 {
112 unsigned refcount;
113 unsigned count;
114 struct pipe_vertex_element elements[32];
115 };
116
117 struct r600_draw {
118 struct pipe_context *ctx;
119 struct radeon_state draw;
120 struct radeon_state vgt;
121 unsigned mode;
122 unsigned start;
123 unsigned count;
124 unsigned index_size;
125 struct pipe_resource *index_buffer;
126 unsigned index_buffer_offset;
127 unsigned min_index, max_index;
128 };
129
130 struct r600_context_hw_states {
131 struct radeon_state rasterizer;
132 struct radeon_state scissor;
133 struct radeon_state dsa;
134 struct radeon_state cb_cntl;
135
136 struct radeon_state db_flush;
137 struct radeon_state cb_flush;
138 };
139
140 #define R600_MAX_CONSTANT 256 /* magic */
141 #define R600_MAX_RESOURCE 160 /* magic */
142
143 struct r600_shader_sampler_states {
144 unsigned nsampler;
145 unsigned nview;
146 unsigned nborder;
147 struct radeon_state *sampler[PIPE_MAX_ATTRIBS];
148 struct radeon_state *view[PIPE_MAX_ATTRIBS];
149 struct radeon_state *border[PIPE_MAX_ATTRIBS];
150 };
151
152 struct r600_context;
153 struct r600_screen;
154 struct r600_resource;
155 struct r600_resource_texture;
156
157 struct r600_context_hw_state_vtbl {
158 void (*blend)(struct r600_context *rctx,
159 struct radeon_state *rstate,
160 const struct pipe_blend_state *state);
161 void (*ucp)(struct r600_context *rctx, struct radeon_state *rstate,
162 const struct pipe_clip_state *state);
163 void (*cb)(struct r600_context *rctx, struct radeon_state *rstate,
164 const struct pipe_framebuffer_state *state, int cb);
165 void (*db)(struct r600_context *rctx, struct radeon_state *rstate,
166 const struct pipe_framebuffer_state *state);
167 void (*rasterizer)(struct r600_context *rctx, struct radeon_state *rstate);
168 void (*scissor)(struct r600_context *rctx, struct radeon_state *rstate);
169 void (*viewport)(struct r600_context *rctx, struct radeon_state *rstate, const struct pipe_viewport_state *state);
170 void (*dsa)(struct r600_context *rctx, struct radeon_state *rstate);
171 void (*sampler_border)(struct r600_context *rctx, struct radeon_state *rstate,
172 const struct pipe_sampler_state *state, unsigned id);
173 void (*sampler)(struct r600_context *rctx, struct radeon_state *rstate,
174 const struct pipe_sampler_state *state, unsigned id);
175 void (*resource)(struct pipe_context *ctx, struct radeon_state *rstate,
176 const struct pipe_sampler_view *view, unsigned id);
177 void (*cb_cntl)(struct r600_context *rctx, struct radeon_state *rstate);
178 int (*vs_resource)(struct r600_context *rctx, int id, struct r600_resource *rbuffer, uint32_t offset,
179 uint32_t stride, uint32_t format);
180 int (*vgt_init)(struct r600_draw *draw,
181 int vgt_draw_initiator);
182 int (*vgt_prim)(struct r600_draw *draw,
183 uint32_t prim, uint32_t vgt_dma_index_type);
184
185 int (*ps_shader)(struct r600_context *rctx, struct r600_context_state *rshader,
186 struct radeon_state *state);
187 int (*vs_shader)(struct r600_context *rctx, struct r600_context_state *rpshader,
188 struct radeon_state *state);
189 void (*init_config)(struct r600_context *rctx);
190
191
192 void (*texture_state_viewport)(struct r600_screen *rscreen,
193 struct r600_resource_texture *rtexture,
194 unsigned level);
195 void (*texture_state_cb)(struct r600_screen *rscreen,
196 struct r600_resource_texture *rtexture,
197 unsigned cb,
198 unsigned level);
199 void (*texture_state_db)(struct r600_screen *rscreen,
200 struct r600_resource_texture *rtexture,
201 unsigned level);
202 void (*texture_state_scissor)(struct r600_screen *rscreen,
203 struct r600_resource_texture *rtexture,
204 unsigned level);
205 };
206 extern struct r600_context_hw_state_vtbl r600_hw_state_vtbl;
207 extern struct r600_context_hw_state_vtbl eg_hw_state_vtbl;
208
209 struct r600_context {
210 struct pipe_context context;
211 struct r600_screen *screen;
212 struct radeon *rw;
213 struct radeon_ctx *ctx;
214 struct blitter_context *blitter;
215 struct radeon_draw draw;
216 struct r600_context_hw_state_vtbl *vtbl;
217 struct radeon_state config;
218 boolean use_mem_constant;
219 /* FIXME get rid of those vs_resource,vs/ps_constant */
220 struct radeon_state *vs_resource;
221 unsigned vs_nresource;
222 struct radeon_state *vs_constant;
223 struct radeon_state *ps_constant;
224 /* hw states */
225 struct r600_context_hw_states hw_states;
226 /* pipe states */
227 unsigned flat_shade;
228
229 unsigned nvertex_buffer;
230 struct r600_context_state *rasterizer;
231 struct r600_context_state *poly_stipple;
232 struct r600_context_state *scissor;
233 struct r600_context_state *clip;
234 struct r600_context_state *ps_shader;
235 struct r600_context_state *vs_shader;
236 struct r600_context_state *depth;
237 struct r600_context_state *stencil;
238 struct r600_context_state *alpha;
239 struct r600_context_state *dsa;
240 struct r600_context_state *blend;
241 struct r600_context_state *stencil_ref;
242 struct r600_context_state *viewport;
243 struct r600_context_state *framebuffer;
244 struct r600_shader_sampler_states vs_sampler;
245 struct r600_shader_sampler_states ps_sampler;
246 /* can add gs later */
247 struct r600_vertex_element *vertex_elements;
248 struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];
249 struct pipe_index_buffer index_buffer;
250 struct pipe_blend_color blend_color;
251 struct list_head query_list;
252 };
253
254 /* Convenience cast wrapper. */
255 static INLINE struct r600_context *r600_context(struct pipe_context *pipe)
256 {
257 return (struct r600_context*)pipe;
258 }
259
260 static INLINE struct r600_query* r600_query(struct pipe_query* q)
261 {
262 return (struct r600_query*)q;
263 }
264
265 struct r600_context_state *r600_context_state_incref(struct r600_context_state *rstate);
266 struct r600_context_state *r600_context_state_decref(struct r600_context_state *rstate);
267 void r600_flush(struct pipe_context *ctx, unsigned flags,
268 struct pipe_fence_handle **fence);
269
270 int r600_context_hw_states(struct pipe_context *ctx);
271
272 void r600_draw_vbo(struct pipe_context *ctx,
273 const struct pipe_draw_info *info);
274
275 void r600_init_blit_functions(struct r600_context *rctx);
276 void r600_init_state_functions(struct r600_context *rctx);
277 void r600_init_query_functions(struct r600_context* rctx);
278 struct pipe_context *r600_create_context(struct pipe_screen *screen, void *priv);
279
280 extern int r600_pipe_shader_create(struct pipe_context *ctx,
281 struct r600_context_state *rstate,
282 const struct tgsi_token *tokens);
283 extern int r600_pipe_shader_update(struct pipe_context *ctx,
284 struct r600_context_state *rstate);
285
286 #define R600_ERR(fmt, args...) \
287 fprintf(stderr, "EE %s/%s:%d - "fmt, __FILE__, __func__, __LINE__, ##args)
288
289 uint32_t r600_translate_texformat(enum pipe_format format,
290 const unsigned char *swizzle_view,
291 uint32_t *word4_p, uint32_t *yuv_format_p);
292
293 /* query */
294 extern void r600_queries_resume(struct pipe_context *ctx);
295 extern void r600_queries_suspend(struct pipe_context *ctx);
296
297 int eg_bc_cf_build(struct r600_bc *bc, struct r600_bc_cf *cf);
298
299 void r600_set_constant_buffer_file(struct pipe_context *ctx,
300 uint shader, uint index,
301 struct pipe_resource *buffer);
302 void r600_set_constant_buffer_mem(struct pipe_context *ctx,
303 uint shader, uint index,
304 struct pipe_resource *buffer);
305 void eg_set_constant_buffer(struct pipe_context *ctx,
306 uint shader, uint index,
307 struct pipe_resource *buffer);
308
309 #endif