r600g: first step into winsys/radeon
[profile/ivi/mesa.git] / src / gallium / drivers / r600 / r600_pipe.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  * Authors:
24  *      Jerome Glisse
25  */
26 #ifndef R600_PIPE_H
27 #define R600_PIPE_H
28
29 #include "../../winsys/radeon/drm/radeon_winsys.h"
30
31 #include <pipe/p_state.h>
32 #include <pipe/p_screen.h>
33 #include <pipe/p_context.h>
34 #include <util/u_math.h>
35 #include "util/u_slab.h"
36 #include "util/u_vbuf_mgr.h"
37 #include "r600.h"
38 #include "r600_public.h"
39 #include "r600_shader.h"
40 #include "r600_resource.h"
41
42 #define R600_MAX_CONST_BUFFERS 1
43 #define R600_MAX_CONST_BUFFER_SIZE 4096
44
45 #ifdef PIPE_ARCH_BIG_ENDIAN
46 #define R600_BIG_ENDIAN 1
47 #else
48 #define R600_BIG_ENDIAN 0
49 #endif
50
51 enum r600_pipe_state_id {
52         R600_PIPE_STATE_BLEND = 0,
53         R600_PIPE_STATE_BLEND_COLOR,
54         R600_PIPE_STATE_CONFIG,
55         R600_PIPE_STATE_SEAMLESS_CUBEMAP,
56         R600_PIPE_STATE_CLIP,
57         R600_PIPE_STATE_SCISSOR,
58         R600_PIPE_STATE_VIEWPORT,
59         R600_PIPE_STATE_RASTERIZER,
60         R600_PIPE_STATE_VGT,
61         R600_PIPE_STATE_FRAMEBUFFER,
62         R600_PIPE_STATE_DSA,
63         R600_PIPE_STATE_STENCIL_REF,
64         R600_PIPE_STATE_PS_SHADER,
65         R600_PIPE_STATE_VS_SHADER,
66         R600_PIPE_STATE_CONSTANT,
67         R600_PIPE_STATE_SAMPLER,
68         R600_PIPE_STATE_RESOURCE,
69         R600_PIPE_STATE_POLYGON_OFFSET,
70         R600_PIPE_STATE_FETCH_SHADER,
71         R600_PIPE_STATE_SPI,
72         R600_PIPE_NSTATES
73 };
74
75 struct r600_screen {
76         struct pipe_screen              screen;
77         struct radeon                   *radeon;
78         struct r600_tiling_info         *tiling_info;
79         struct util_slab_mempool        pool_buffers;
80         unsigned                        num_contexts;
81
82         /* for thread-safe write accessing to num_contexts */
83         pipe_mutex                      mutex_num_contexts;
84 };
85
86 struct r600_pipe_sampler_view {
87         struct pipe_sampler_view        base;
88         struct r600_pipe_resource_state         state;
89 };
90
91 struct r600_pipe_rasterizer {
92         struct r600_pipe_state          rstate;
93         boolean                         clamp_vertex_color;
94         boolean                         clamp_fragment_color;
95         boolean                         flatshade;
96         unsigned                        sprite_coord_enable;
97         float                           offset_units;
98         float                           offset_scale;
99 };
100
101 struct r600_pipe_blend {
102         struct r600_pipe_state          rstate;
103         unsigned                        cb_target_mask;
104 };
105
106 struct r600_pipe_dsa {
107         struct r600_pipe_state          rstate;
108         unsigned                        alpha_ref;
109 };
110
111 struct r600_vertex_element
112 {
113         unsigned                        count;
114         struct pipe_vertex_element      elements[PIPE_MAX_ATTRIBS];
115         struct u_vbuf_mgr_elements      *vmgr_elements;
116         struct r600_bo                  *fetch_shader;
117         unsigned                        fs_size;
118         struct r600_pipe_state          rstate;
119         /* if offset is to big for fetch instructio we need to alterate
120          * offset of vertex buffer, record here the offset need to add
121          */
122         unsigned                        vbuffer_need_offset;
123         unsigned                        vbuffer_offset[PIPE_MAX_ATTRIBS];
124 };
125
126 struct r600_pipe_shader {
127         struct r600_shader              shader;
128         struct r600_pipe_state          rstate;
129         struct r600_bo                  *bo;
130         struct r600_bo                  *bo_fetch;
131         struct r600_vertex_element      vertex_elements;
132         struct tgsi_token               *tokens;
133 };
134
135 struct r600_pipe_sampler_state {
136         struct r600_pipe_state          rstate;
137         boolean seamless_cube_map;
138 };
139
140 /* needed for blitter save */
141 #define NUM_TEX_UNITS 16
142
143 struct r600_textures_info {
144         struct r600_pipe_sampler_view   *views[NUM_TEX_UNITS];
145         unsigned                        n_views;
146         void                            *samplers[NUM_TEX_UNITS];
147         unsigned                        n_samplers;
148 };
149
150 struct r600_fence {
151         struct pipe_reference           reference;
152         struct r600_pipe_context        *ctx;
153         unsigned                        index; /* in the shared bo */
154         struct list_head                head;
155 };
156
157 #define FENCE_BLOCK_SIZE 16
158
159 struct r600_fence_block {
160         struct r600_fence               fences[FENCE_BLOCK_SIZE];
161         struct list_head                head;
162 };
163
164 struct r600_pipe_fences {
165         struct r600_bo                  *bo;
166         unsigned                        *data;
167         unsigned                        next_index;
168         /* linked list of preallocated blocks */
169         struct list_head                blocks;
170         /* linked list of freed fences */
171         struct list_head                pool;
172 };
173
174 #define R600_CONSTANT_ARRAY_SIZE 256
175 #define R600_RESOURCE_ARRAY_SIZE 160
176
177 struct r600_pipe_context {
178         struct pipe_context             context;
179         struct blitter_context          *blitter;
180         enum radeon_family              family;
181         enum chip_class                 chip_class;
182         void                            *custom_dsa_flush;
183         struct r600_screen              *screen;
184         struct radeon                   *radeon;
185         struct r600_pipe_state          *states[R600_PIPE_NSTATES];
186         struct r600_context             ctx;
187         struct r600_vertex_element      *vertex_elements;
188         struct r600_pipe_resource_state fs_resource[PIPE_MAX_ATTRIBS];
189         struct pipe_framebuffer_state   framebuffer;
190         struct pipe_index_buffer        index_buffer;
191         unsigned                        cb_target_mask;
192         /* for saving when using blitter */
193         struct pipe_stencil_ref         stencil_ref;
194         struct pipe_viewport_state      viewport;
195         struct pipe_clip_state          clip;
196         struct r600_pipe_state          config;
197         struct r600_pipe_shader         *ps_shader;
198         struct r600_pipe_shader         *vs_shader;
199         struct r600_pipe_state          vs_const_buffer;
200         struct r600_pipe_resource_state         vs_const_buffer_resource[R600_MAX_CONST_BUFFERS];
201         struct r600_pipe_state          ps_const_buffer;
202         struct r600_pipe_resource_state         ps_const_buffer_resource[R600_MAX_CONST_BUFFERS];
203         struct r600_pipe_rasterizer     *rasterizer;
204         struct r600_pipe_state          vgt;
205         struct r600_pipe_state          spi;
206         struct pipe_query               *current_render_cond;
207         unsigned                        current_render_cond_mode;
208         struct pipe_query               *saved_render_cond;
209         unsigned                        saved_render_cond_mode;
210         /* shader information */
211         boolean                         clamp_vertex_color;
212         boolean                         clamp_fragment_color;
213         boolean                         spi_dirty;
214         unsigned                        sprite_coord_enable;
215         boolean                         flatshade;
216         boolean                         export_16bpc;
217         unsigned                        alpha_ref;
218         boolean                         alpha_ref_dirty;
219         unsigned                        nr_cbufs;
220         struct r600_textures_info       ps_samplers;
221
222         struct r600_pipe_fences         fences;
223
224         struct u_vbuf_mgr               *vbuf_mgr;
225         struct util_slab_mempool        pool_transfers;
226         boolean                         blit;
227         boolean                         have_depth_texture, have_depth_fb;
228
229         unsigned default_ps_gprs, default_vs_gprs;
230 };
231
232 struct r600_drawl {
233         struct pipe_draw_info   info;
234         struct pipe_context     *ctx;
235         unsigned                index_size;
236         unsigned                index_buffer_offset;
237         struct pipe_resource    *index_buffer;
238 };
239
240 /* evergreen_state.c */
241 void evergreen_init_state_functions(struct r600_pipe_context *rctx);
242 void evergreen_init_config(struct r600_pipe_context *rctx);
243 void evergreen_pipe_shader_ps(struct pipe_context *ctx, struct r600_pipe_shader *shader);
244 void evergreen_pipe_shader_vs(struct pipe_context *ctx, struct r600_pipe_shader *shader);
245 void evergreen_fetch_shader(struct pipe_context *ctx, struct r600_vertex_element *ve);
246 void *evergreen_create_db_flush_dsa(struct r600_pipe_context *rctx);
247 void evergreen_polygon_offset_update(struct r600_pipe_context *rctx);
248 void evergreen_pipe_init_buffer_resource(struct r600_pipe_context *rctx,
249                                          struct r600_pipe_resource_state *rstate);
250 void evergreen_pipe_mod_buffer_resource(struct r600_pipe_resource_state *rstate,
251                                         struct r600_resource *rbuffer,
252                                         unsigned offset, unsigned stride);
253 boolean evergreen_is_format_supported(struct pipe_screen *screen,
254                                       enum pipe_format format,
255                                       enum pipe_texture_target target,
256                                       unsigned sample_count,
257                                       unsigned usage);
258
259 /* r600_blit.c */
260 void r600_init_blit_functions(struct r600_pipe_context *rctx);
261 void r600_blit_uncompress_depth(struct pipe_context *ctx, struct r600_resource_texture *texture);
262 void r600_blit_push_depth(struct pipe_context *ctx, struct r600_resource_texture *texture);
263 void r600_flush_depth_textures(struct r600_pipe_context *rctx);
264
265 /* r600_buffer.c */
266 struct pipe_resource *r600_buffer_create(struct pipe_screen *screen,
267                                          const struct pipe_resource *templ);
268 struct pipe_resource *r600_user_buffer_create(struct pipe_screen *screen,
269                                               void *ptr, unsigned bytes,
270                                               unsigned bind);
271 struct pipe_resource *r600_buffer_from_handle(struct pipe_screen *screen,
272                                               struct winsys_handle *whandle);
273 void r600_upload_index_buffer(struct r600_pipe_context *rctx, struct r600_drawl *draw);
274
275 /* r600_query.c */
276 void r600_init_query_functions(struct r600_pipe_context *rctx);
277
278 /* r600_resource.c */
279 void r600_init_context_resource_functions(struct r600_pipe_context *r600);
280
281 /* r600_shader.c */
282 int r600_pipe_shader_create(struct pipe_context *ctx, struct r600_pipe_shader *shader);
283 void r600_pipe_shader_destroy(struct pipe_context *ctx, struct r600_pipe_shader *shader);
284 int r600_find_vs_semantic_index(struct r600_shader *vs,
285                                 struct r600_shader *ps, int id);
286
287 /* r600_state.c */
288 void r600_init_state_functions(struct r600_pipe_context *rctx);
289 void r600_init_config(struct r600_pipe_context *rctx);
290 void r600_pipe_shader_ps(struct pipe_context *ctx, struct r600_pipe_shader *shader);
291 void r600_pipe_shader_vs(struct pipe_context *ctx, struct r600_pipe_shader *shader);
292 void r600_fetch_shader(struct pipe_context *ctx, struct r600_vertex_element *ve);
293 void *r600_create_db_flush_dsa(struct r600_pipe_context *rctx);
294 void r600_polygon_offset_update(struct r600_pipe_context *rctx);
295 void r600_pipe_init_buffer_resource(struct r600_pipe_context *rctx,
296                                     struct r600_pipe_resource_state *rstate);
297 void r600_pipe_mod_buffer_resource(struct r600_pipe_resource_state *rstate,
298                                    struct r600_resource *rbuffer,
299                                    unsigned offset, unsigned stride);
300 void r600_adjust_gprs(struct r600_pipe_context *rctx);
301 boolean r600_is_format_supported(struct pipe_screen *screen,
302                                  enum pipe_format format,
303                                  enum pipe_texture_target target,
304                                  unsigned sample_count,
305                                  unsigned usage);
306
307 /* r600_texture.c */
308 void r600_init_screen_texture_functions(struct pipe_screen *screen);
309 void r600_init_surface_functions(struct r600_pipe_context *r600);
310 uint32_t r600_translate_texformat(struct pipe_screen *screen, enum pipe_format format,
311                                   const unsigned char *swizzle_view,
312                                   uint32_t *word4_p, uint32_t *yuv_format_p);
313 unsigned r600_texture_get_offset(struct r600_resource_texture *rtex,
314                                         unsigned level, unsigned layer);
315
316 /* r600_translate.c */
317 void r600_translate_index_buffer(struct r600_pipe_context *r600,
318                                  struct pipe_resource **index_buffer,
319                                  unsigned *index_size,
320                                  unsigned *start, unsigned count);
321
322 /* r600_state_common.c */
323 void r600_set_index_buffer(struct pipe_context *ctx,
324                            const struct pipe_index_buffer *ib);
325 void r600_set_vertex_buffers(struct pipe_context *ctx, unsigned count,
326                              const struct pipe_vertex_buffer *buffers);
327 void *r600_create_vertex_elements(struct pipe_context *ctx,
328                                   unsigned count,
329                                   const struct pipe_vertex_element *elements);
330 void r600_delete_vertex_element(struct pipe_context *ctx, void *state);
331 void r600_bind_blend_state(struct pipe_context *ctx, void *state);
332 void r600_bind_dsa_state(struct pipe_context *ctx, void *state);
333 void r600_bind_rs_state(struct pipe_context *ctx, void *state);
334 void r600_delete_rs_state(struct pipe_context *ctx, void *state);
335 void r600_sampler_view_destroy(struct pipe_context *ctx,
336                                struct pipe_sampler_view *state);
337 void r600_delete_state(struct pipe_context *ctx, void *state);
338 void r600_bind_vertex_elements(struct pipe_context *ctx, void *state);
339 void *r600_create_shader_state(struct pipe_context *ctx,
340                                const struct pipe_shader_state *state);
341 void r600_bind_ps_shader(struct pipe_context *ctx, void *state);
342 void r600_bind_vs_shader(struct pipe_context *ctx, void *state);
343 void r600_delete_ps_shader(struct pipe_context *ctx, void *state);
344 void r600_delete_vs_shader(struct pipe_context *ctx, void *state);
345 void r600_set_constant_buffer(struct pipe_context *ctx, uint shader, uint index,
346                               struct pipe_resource *buffer);
347 void r600_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *info);
348
349 /*
350  * common helpers
351  */
352 static INLINE u32 S_FIXED(float value, u32 frac_bits)
353 {
354         return value * (1 << frac_bits);
355 }
356 #define ALIGN_DIVUP(x, y) (((x) + (y) - 1) / (y))
357
358 static inline unsigned r600_tex_aniso_filter(unsigned filter)
359 {
360         if (filter <= 1)   return 0;
361         if (filter <= 2)   return 1;
362         if (filter <= 4)   return 2;
363         if (filter <= 8)   return 3;
364          /* else */        return 4;
365 }
366
367 #endif