1 /*
2  * Copyright 2018 Collabora Ltd.
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 
24 #ifndef ZINK_PROGRAM_H
25 #define ZINK_PROGRAM_H
26 
27 #include <vulkan/vulkan.h>
28 
29 #include "compiler/shader_enums.h"
30 #include "pipe/p_state.h"
31 #include "util/u_inlines.h"
32 
33 #include "zink_context.h"
34 #include "zink_compiler.h"
35 #include "zink_shader_keys.h"
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 struct zink_screen;
41 struct zink_shader;
42 struct zink_gfx_pipeline_state;
43 struct zink_descriptor_set;
44 
45 struct hash_table;
46 struct set;
47 struct util_dynarray;
48 
49 struct zink_program;
50 
51 struct zink_gfx_push_constant {
52    unsigned draw_mode_is_indexed;
53    unsigned draw_id;
54    float default_inner_level[2];
55    float default_outer_level[4];
56 };
57 
58 struct zink_cs_push_constant {
59    unsigned work_dim;
60 };
61 
62 /* a shader module is used for directly reusing a shader module between programs,
63  * e.g., in the case where we're swapping out only one shader,
64  * allowing us to skip going through shader keys
65  */
66 struct zink_shader_module {
67    struct list_head list;
68    VkShaderModule shader;
69    uint32_t hash;
70    bool default_variant;
71    uint8_t num_uniforms;
72    uint8_t key_size;
73    uint8_t key[0]; /* | key | uniforms | */
74 };
75 
76 struct zink_program {
77    struct pipe_reference reference;
78    unsigned char sha1[20];
79    struct util_queue_fence cache_fence;
80    VkPipelineCache pipeline_cache;
81    size_t pipeline_cache_size;
82    struct zink_batch_usage *batch_uses;
83    bool is_compute;
84 
85    struct zink_program_descriptor_data *dd;
86 
87    uint32_t compat_id;
88    VkPipelineLayout layout;
89    VkDescriptorSetLayout dsl[ZINK_DESCRIPTOR_TYPES + 2]; // one for each type + push + bindless
90    unsigned num_dsl;
91 
92    bool removed;
93 };
94 
95 #define ZINK_MAX_INLINED_VARIANTS 5
96 
97 struct zink_gfx_program {
98    struct zink_program base;
99 
100    uint32_t stages_present; //mask of stages present in this program
101    struct nir_shader *nir[ZINK_SHADER_COUNT];
102 
103    struct zink_shader_module *modules[ZINK_SHADER_COUNT]; // compute stage doesn't belong here
104 
105    struct zink_shader *last_vertex_stage;
106 
107    struct list_head shader_cache[ZINK_SHADER_COUNT][2]; //normal, inline uniforms
108    unsigned inlined_variant_count[ZINK_SHADER_COUNT];
109 
110    struct zink_shader *shaders[ZINK_SHADER_COUNT];
111    struct hash_table pipelines[11]; // number of draw modes we support
112    uint32_t default_variant_hash;
113    uint32_t last_variant_hash;
114 };
115 
116 struct zink_compute_program {
117    struct zink_program base;
118 
119    struct zink_shader_module *module;
120    struct zink_shader *shader;
121    struct hash_table *pipelines;
122 };
123 
124 static inline enum zink_descriptor_type
zink_desc_type_from_vktype(VkDescriptorType type)125 zink_desc_type_from_vktype(VkDescriptorType type)
126 {
127    switch (type) {
128    case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
129    case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
130       return ZINK_DESCRIPTOR_TYPE_UBO;
131    case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
132    case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
133       return ZINK_DESCRIPTOR_TYPE_SAMPLER_VIEW;
134    case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
135       return ZINK_DESCRIPTOR_TYPE_SSBO;
136    case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
137    case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
138       return ZINK_DESCRIPTOR_TYPE_IMAGE;
139    default:
140       unreachable("unhandled descriptor type");
141    }
142 }
143 
144 static inline VkPrimitiveTopology
zink_primitive_topology(enum pipe_prim_type mode)145 zink_primitive_topology(enum pipe_prim_type mode)
146 {
147    switch (mode) {
148    case PIPE_PRIM_POINTS:
149       return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
150 
151    case PIPE_PRIM_LINES:
152       return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
153 
154    case PIPE_PRIM_LINE_STRIP:
155       return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
156 
157    case PIPE_PRIM_TRIANGLES:
158       return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
159 
160    case PIPE_PRIM_TRIANGLE_STRIP:
161       return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
162 
163    case PIPE_PRIM_TRIANGLE_FAN:
164       return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN;
165 
166    case PIPE_PRIM_LINE_STRIP_ADJACENCY:
167       return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY;
168 
169    case PIPE_PRIM_LINES_ADJACENCY:
170       return VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY;
171 
172    case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
173       return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY;
174 
175    case PIPE_PRIM_TRIANGLES_ADJACENCY:
176       return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY;
177 
178    case PIPE_PRIM_PATCHES:
179       return VK_PRIMITIVE_TOPOLOGY_PATCH_LIST;
180 
181    default:
182       unreachable("unexpected enum pipe_prim_type");
183    }
184 }
185 
186 void
187 zink_delete_shader_state(struct pipe_context *pctx, void *cso);
188 void *
189 zink_create_gfx_shader_state(struct pipe_context *pctx, const struct pipe_shader_state *shader);
190 
191 unsigned
192 zink_program_num_bindings_typed(const struct zink_program *pg, enum zink_descriptor_type type, bool is_compute);
193 
194 unsigned
195 zink_program_num_bindings(const struct zink_program *pg, bool is_compute);
196 
197 bool
198 zink_program_descriptor_is_buffer(struct zink_context *ctx, enum pipe_shader_type stage, enum zink_descriptor_type type, unsigned i);
199 
200 void
201 zink_update_gfx_program(struct zink_context *ctx, struct zink_gfx_program *prog);
202 
203 struct zink_gfx_program *
204 zink_create_gfx_program(struct zink_context *ctx,
205                         struct zink_shader *stages[ZINK_SHADER_COUNT],
206                         unsigned vertices_per_patch);
207 
208 void
209 zink_destroy_gfx_program(struct zink_screen *screen,
210                          struct zink_gfx_program *prog);
211 
212 VkPipeline
213 zink_get_gfx_pipeline(struct zink_context *ctx,
214                       struct zink_gfx_program *prog,
215                       struct zink_gfx_pipeline_state *state,
216                       enum pipe_prim_type mode);
217 
218 void
219 zink_program_init(struct zink_context *ctx);
220 
221 uint32_t
222 zink_program_get_descriptor_usage(struct zink_context *ctx, enum pipe_shader_type stage, enum zink_descriptor_type type);
223 
224 void
225 debug_describe_zink_gfx_program(char* buf, const struct zink_gfx_program *ptr);
226 
227 static inline bool
zink_gfx_program_reference(struct zink_screen * screen,struct zink_gfx_program ** dst,struct zink_gfx_program * src)228 zink_gfx_program_reference(struct zink_screen *screen,
229                            struct zink_gfx_program **dst,
230                            struct zink_gfx_program *src)
231 {
232    struct zink_gfx_program *old_dst = dst ? *dst : NULL;
233    bool ret = false;
234 
235    if (pipe_reference_described(old_dst ? &old_dst->base.reference : NULL, &src->base.reference,
236                                 (debug_reference_descriptor)debug_describe_zink_gfx_program)) {
237       zink_destroy_gfx_program(screen, old_dst);
238       ret = true;
239    }
240    if (dst) *dst = src;
241    return ret;
242 }
243 
244 struct zink_compute_program *
245 zink_create_compute_program(struct zink_context *ctx, struct zink_shader *shader);
246 void
247 zink_destroy_compute_program(struct zink_screen *screen,
248                          struct zink_compute_program *comp);
249 
250 void
251 debug_describe_zink_compute_program(char* buf, const struct zink_compute_program *ptr);
252 
253 static inline bool
zink_compute_program_reference(struct zink_screen * screen,struct zink_compute_program ** dst,struct zink_compute_program * src)254 zink_compute_program_reference(struct zink_screen *screen,
255                            struct zink_compute_program **dst,
256                            struct zink_compute_program *src)
257 {
258    struct zink_compute_program *old_dst = dst ? *dst : NULL;
259    bool ret = false;
260 
261    if (pipe_reference_described(old_dst ? &old_dst->base.reference : NULL, &src->base.reference,
262                                 (debug_reference_descriptor)debug_describe_zink_compute_program)) {
263       zink_destroy_compute_program(screen, old_dst);
264       ret = true;
265    }
266    if (dst) *dst = src;
267    return ret;
268 }
269 
270 VkPipelineLayout
271 zink_pipeline_layout_create(struct zink_screen *screen, struct zink_program *pg, uint32_t *compat);
272 
273 void
274 zink_program_update_compute_pipeline_state(struct zink_context *ctx, struct zink_compute_program *comp, const uint block[3]);
275 
276 VkPipeline
277 zink_get_compute_pipeline(struct zink_screen *screen,
278                       struct zink_compute_program *comp,
279                       struct zink_compute_pipeline_state *state);
280 
281 static inline bool
zink_program_has_descriptors(const struct zink_program * pg)282 zink_program_has_descriptors(const struct zink_program *pg)
283 {
284    return pg->num_dsl > 0;
285 }
286 
287 static inline struct zink_fs_key *
zink_set_fs_key(struct zink_context * ctx)288 zink_set_fs_key(struct zink_context *ctx)
289 {
290    ctx->dirty_shader_stages |= BITFIELD_BIT(PIPE_SHADER_FRAGMENT);
291    return (struct zink_fs_key *)&ctx->gfx_pipeline_state.shader_keys.key[PIPE_SHADER_FRAGMENT];
292 }
293 
294 static inline const struct zink_fs_key *
zink_get_fs_key(struct zink_context * ctx)295 zink_get_fs_key(struct zink_context *ctx)
296 {
297    return (const struct zink_fs_key *)&ctx->gfx_pipeline_state.shader_keys.key[PIPE_SHADER_FRAGMENT];
298 }
299 
300 void
301 zink_update_fs_key_samples(struct zink_context *ctx);
302 
303 static inline struct zink_vs_key *
zink_set_vs_key(struct zink_context * ctx)304 zink_set_vs_key(struct zink_context *ctx)
305 {
306    ctx->dirty_shader_stages |= BITFIELD_BIT(PIPE_SHADER_VERTEX);
307    return (struct zink_vs_key *)&ctx->gfx_pipeline_state.shader_keys.key[PIPE_SHADER_VERTEX];
308 }
309 
310 static inline const struct zink_vs_key *
zink_get_vs_key(struct zink_context * ctx)311 zink_get_vs_key(struct zink_context *ctx)
312 {
313    return (const struct zink_vs_key *)&ctx->gfx_pipeline_state.shader_keys.key[PIPE_SHADER_VERTEX];
314 }
315 
316 static inline struct zink_vs_key_base *
zink_set_last_vertex_key(struct zink_context * ctx)317 zink_set_last_vertex_key(struct zink_context *ctx)
318 {
319    ctx->last_vertex_stage_dirty = true;
320    return (struct zink_vs_key_base *)&ctx->gfx_pipeline_state.shader_keys.last_vertex;
321 }
322 
323 static inline const struct zink_vs_key_base *
zink_get_last_vertex_key(struct zink_context * ctx)324 zink_get_last_vertex_key(struct zink_context *ctx)
325 {
326    return (const struct zink_vs_key_base *)&ctx->gfx_pipeline_state.shader_keys.last_vertex;
327 }
328 
329 static inline void
zink_set_fs_point_coord_key(struct zink_context * ctx)330 zink_set_fs_point_coord_key(struct zink_context *ctx)
331 {
332    const struct zink_fs_key *fs = zink_get_fs_key(ctx);
333    bool disable = !ctx->gfx_pipeline_state.has_points || !ctx->rast_state->base.sprite_coord_enable;
334    uint8_t coord_replace_bits = disable ? 0 : ctx->rast_state->base.sprite_coord_enable;
335    bool coord_replace_yinvert = disable ? false : !!ctx->rast_state->base.sprite_coord_mode;
336    if (fs->coord_replace_bits != coord_replace_bits || fs->coord_replace_yinvert != coord_replace_yinvert) {
337       zink_set_fs_key(ctx)->coord_replace_bits = coord_replace_bits;
338       zink_set_fs_key(ctx)->coord_replace_yinvert = coord_replace_yinvert;
339    }
340 }
341 
342 #ifdef __cplusplus
343 }
344 #endif
345 
346 #endif
347