1ea0b4a0d1d82c0d30f1a1211f5b0767e0c7a907
[mesa.git] / src / gallium / drivers / ilo / ilo_shader.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2012-2013 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #ifndef ILO_SHADER_H
29 #define ILO_SHADER_H
30
31 #include "ilo_common.h"
32 #include "ilo_context.h"
33
34 /* XXX The interface needs to be reworked */
35
36 /**
37 * A shader variant. It consists of non-orthogonal states of the pipe context
38 * affecting the compilation of a shader.
39 */
40 struct ilo_shader_variant {
41 union {
42 struct {
43 bool rasterizer_discard;
44 int num_ucps;
45 } vs;
46
47 struct {
48 bool rasterizer_discard;
49 int num_inputs;
50 int semantic_names[PIPE_MAX_SHADER_INPUTS];
51 int semantic_indices[PIPE_MAX_SHADER_INPUTS];
52 } gs;
53
54 struct {
55 bool flatshade;
56 int fb_height;
57 int num_cbufs;
58 } fs;
59 } u;
60
61 int num_sampler_views;
62 struct {
63 unsigned r:3;
64 unsigned g:3;
65 unsigned b:3;
66 unsigned a:3;
67 } sampler_view_swizzles[ILO_MAX_SAMPLER_VIEWS];
68
69 uint32_t saturate_tex_coords[3];
70 };
71
72 /**
73 * A compiled shader.
74 */
75 struct ilo_shader {
76 struct ilo_shader_variant variant;
77
78 struct {
79 int semantic_names[PIPE_MAX_SHADER_INPUTS];
80 int semantic_indices[PIPE_MAX_SHADER_INPUTS];
81 int interp[PIPE_MAX_SHADER_INPUTS];
82 bool centroid[PIPE_MAX_SHADER_INPUTS];
83 int count;
84
85 int start_grf;
86 bool has_pos;
87 bool has_linear_interp;
88 int barycentric_interpolation_mode;
89 bool discard_adj;
90 } in;
91
92 struct {
93 int register_indices[PIPE_MAX_SHADER_OUTPUTS];
94 int semantic_names[PIPE_MAX_SHADER_OUTPUTS];
95 int semantic_indices[PIPE_MAX_SHADER_OUTPUTS];
96 int count;
97
98 bool has_pos;
99 } out;
100
101 bool has_kill;
102 bool dispatch_16;
103
104 bool stream_output;
105 int svbi_post_inc;
106 /* for VS stream output / rasterizer discard */
107 int gs_offsets[3];
108 int gs_start_grf;
109
110 void *kernel;
111 int kernel_size;
112
113 /* what does the push constant buffer consist of? */
114 struct {
115 int clip_state_size;
116 } pcb;
117
118 struct list_head list;
119
120 uint32_t cache_seqno;
121 uint32_t cache_offset;
122 };
123
124 /**
125 * Information about a shader state.
126 */
127 struct ilo_shader_info {
128 const struct ilo_dev_info *dev;
129 int type;
130
131 const struct tgsi_token *tokens;
132
133 struct pipe_stream_output_info stream_output;
134 struct {
135 unsigned req_local_mem;
136 unsigned req_private_mem;
137 unsigned req_input_mem;
138 } compute;
139
140 bool has_color_interp;
141 bool has_pos;
142 bool has_vertexid;
143 bool has_instanceid;
144 bool fs_color0_writes_all_cbufs;
145
146 int edgeflag_in;
147 int edgeflag_out;
148
149 uint32_t shadow_samplers;
150 int num_samplers;
151 };
152
153 /**
154 * A shader state.
155 */
156 struct ilo_shader_state {
157 struct ilo_shader_info info;
158
159 struct list_head variants;
160 int num_variants, total_size;
161
162 struct ilo_shader *shader;
163 };
164
165 struct ilo_shader_cache {
166 struct intel_winsys *winsys;
167 struct intel_bo *bo;
168 int cur, size;
169 bool busy;
170
171 /* starting from 1, incremented whenever a new bo is allocated */
172 uint32_t seqno;
173 };
174
175 void
176 ilo_shader_variant_init(struct ilo_shader_variant *variant,
177 const struct ilo_shader_info *info,
178 const struct ilo_context *ilo);
179
180 struct ilo_shader_state *
181 ilo_shader_state_create(const struct ilo_context *ilo,
182 int type, const void *templ);
183
184 void
185 ilo_shader_state_destroy(struct ilo_shader_state *state);
186
187 struct ilo_shader *
188 ilo_shader_state_add_variant(struct ilo_shader_state *state,
189 const struct ilo_shader_variant *variant);
190
191 bool
192 ilo_shader_state_use_variant(struct ilo_shader_state *state,
193 const struct ilo_shader_variant *variant);
194
195 struct ilo_shader_cache *
196 ilo_shader_cache_create(struct intel_winsys *winsys);
197
198 void
199 ilo_shader_cache_destroy(struct ilo_shader_cache *shc);
200
201 void
202 ilo_shader_cache_set(struct ilo_shader_cache *shc,
203 struct ilo_shader **shaders,
204 int num_shaders);
205
206 static inline void
207 ilo_shader_cache_mark_busy(struct ilo_shader_cache *shc)
208 {
209 if (shc->cur)
210 shc->busy = true;
211 }
212
213 struct ilo_shader *
214 ilo_shader_compile_vs(const struct ilo_shader_state *state,
215 const struct ilo_shader_variant *variant);
216
217 struct ilo_shader *
218 ilo_shader_compile_gs(const struct ilo_shader_state *state,
219 const struct ilo_shader_variant *variant);
220
221 bool
222 ilo_shader_compile_gs_passthrough(const struct ilo_shader_state *vs_state,
223 const struct ilo_shader_variant *vs_variant,
224 const int *so_mapping,
225 struct ilo_shader *vs);
226
227 struct ilo_shader *
228 ilo_shader_compile_fs(const struct ilo_shader_state *state,
229 const struct ilo_shader_variant *variant);
230
231 struct ilo_shader *
232 ilo_shader_compile_cs(const struct ilo_shader_state *state,
233 const struct ilo_shader_variant *variant);
234
235 static inline void
236 ilo_shader_destroy(struct ilo_shader *sh)
237 {
238 FREE(sh->kernel);
239 FREE(sh);
240 }
241
242 #endif /* ILO_SHADER_H */