a19f85e4b35279bce386f9127e7cbd5929bfe4b8
[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 struct ilo_shader_cache;
35
36 /* XXX The interface needs to be reworked */
37
38 /**
39 * A shader variant. It consists of non-orthogonal states of the pipe context
40 * affecting the compilation of a shader.
41 */
42 struct ilo_shader_variant {
43 union {
44 struct {
45 bool rasterizer_discard;
46 int num_ucps;
47 } vs;
48
49 struct {
50 bool rasterizer_discard;
51 int num_inputs;
52 int semantic_names[PIPE_MAX_SHADER_INPUTS];
53 int semantic_indices[PIPE_MAX_SHADER_INPUTS];
54 } gs;
55
56 struct {
57 bool flatshade;
58 int fb_height;
59 int num_cbufs;
60 } fs;
61 } u;
62
63 int num_sampler_views;
64 struct {
65 unsigned r:3;
66 unsigned g:3;
67 unsigned b:3;
68 unsigned a:3;
69 } sampler_view_swizzles[ILO_MAX_SAMPLER_VIEWS];
70
71 uint32_t saturate_tex_coords[3];
72 };
73
74 /**
75 * A compiled shader.
76 */
77 struct ilo_shader {
78 struct ilo_shader_variant variant;
79
80 struct {
81 int semantic_names[PIPE_MAX_SHADER_INPUTS];
82 int semantic_indices[PIPE_MAX_SHADER_INPUTS];
83 int interp[PIPE_MAX_SHADER_INPUTS];
84 bool centroid[PIPE_MAX_SHADER_INPUTS];
85 int count;
86
87 int start_grf;
88 bool has_pos;
89 bool has_linear_interp;
90 int barycentric_interpolation_mode;
91 bool discard_adj;
92 } in;
93
94 struct {
95 int register_indices[PIPE_MAX_SHADER_OUTPUTS];
96 int semantic_names[PIPE_MAX_SHADER_OUTPUTS];
97 int semantic_indices[PIPE_MAX_SHADER_OUTPUTS];
98 int count;
99
100 bool has_pos;
101 } out;
102
103 bool has_kill;
104 bool dispatch_16;
105
106 bool stream_output;
107 int svbi_post_inc;
108 /* for VS stream output / rasterizer discard */
109 int gs_offsets[3];
110 int gs_start_grf;
111
112 void *kernel;
113 int kernel_size;
114
115 /* what does the push constant buffer consist of? */
116 struct {
117 int clip_state_size;
118 } pcb;
119
120 struct list_head list;
121
122 /* managed by shader cache */
123 uint32_t cache_seqno;
124 uint32_t cache_offset;
125 };
126
127 /**
128 * Information about a shader state.
129 */
130 struct ilo_shader_info {
131 const struct ilo_dev_info *dev;
132 int type;
133
134 const struct tgsi_token *tokens;
135
136 struct pipe_stream_output_info stream_output;
137 struct {
138 unsigned req_local_mem;
139 unsigned req_private_mem;
140 unsigned req_input_mem;
141 } compute;
142
143 bool has_color_interp;
144 bool has_pos;
145 bool has_vertexid;
146 bool has_instanceid;
147 bool fs_color0_writes_all_cbufs;
148
149 int edgeflag_in;
150 int edgeflag_out;
151
152 uint32_t shadow_samplers;
153 int num_samplers;
154 };
155
156 /**
157 * A shader state.
158 */
159 struct ilo_shader_state {
160 struct ilo_shader_info info;
161
162 struct list_head variants;
163 int num_variants, total_size;
164
165 struct ilo_shader *shader;
166
167 /* managed by shader cache */
168 struct ilo_shader_cache *cache;
169 struct list_head list;
170 };
171
172 struct ilo_shader_cache *
173 ilo_shader_cache_create(void);
174
175 void
176 ilo_shader_cache_destroy(struct ilo_shader_cache *shc);
177
178 void
179 ilo_shader_cache_add(struct ilo_shader_cache *shc,
180 struct ilo_shader_state *shader);
181
182 void
183 ilo_shader_cache_remove(struct ilo_shader_cache *shc,
184 struct ilo_shader_state *shader);
185
186 int
187 ilo_shader_cache_upload(struct ilo_shader_cache *shc,
188 struct intel_bo *bo, unsigned offset,
189 bool incremental);
190
191 void
192 ilo_shader_variant_init(struct ilo_shader_variant *variant,
193 const struct ilo_shader_info *info,
194 const struct ilo_context *ilo);
195
196 struct ilo_shader_state *
197 ilo_shader_state_create(const struct ilo_context *ilo,
198 int type, const void *templ);
199
200 void
201 ilo_shader_state_destroy(struct ilo_shader_state *state);
202
203 struct ilo_shader *
204 ilo_shader_state_add_variant(struct ilo_shader_state *state,
205 const struct ilo_shader_variant *variant);
206
207 bool
208 ilo_shader_state_use_variant(struct ilo_shader_state *state,
209 const struct ilo_shader_variant *variant);
210
211 struct ilo_shader *
212 ilo_shader_compile_vs(const struct ilo_shader_state *state,
213 const struct ilo_shader_variant *variant);
214
215 struct ilo_shader *
216 ilo_shader_compile_gs(const struct ilo_shader_state *state,
217 const struct ilo_shader_variant *variant);
218
219 bool
220 ilo_shader_compile_gs_passthrough(const struct ilo_shader_state *vs_state,
221 const struct ilo_shader_variant *vs_variant,
222 const int *so_mapping,
223 struct ilo_shader *vs);
224
225 struct ilo_shader *
226 ilo_shader_compile_fs(const struct ilo_shader_state *state,
227 const struct ilo_shader_variant *variant);
228
229 struct ilo_shader *
230 ilo_shader_compile_cs(const struct ilo_shader_state *state,
231 const struct ilo_shader_variant *variant);
232
233 static inline void
234 ilo_shader_destroy(struct ilo_shader *sh)
235 {
236 FREE(sh->kernel);
237 FREE(sh);
238 }
239
240 #endif /* ILO_SHADER_H */