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_COMPILER_H
25 #define ZINK_COMPILER_H
26 
27 #include "pipe/p_defines.h"
28 #include "pipe/p_state.h"
29 
30 #include "compiler/nir/nir.h"
31 #include "compiler/shader_info.h"
32 #include "util/u_live_shader_cache.h"
33 
34 #include <vulkan/vulkan.h>
35 #include "zink_descriptors.h"
36 
37 #define ZINK_WORKGROUP_SIZE_X 1
38 #define ZINK_WORKGROUP_SIZE_Y 2
39 #define ZINK_WORKGROUP_SIZE_Z 3
40 
41 struct pipe_screen;
42 struct zink_context;
43 struct zink_screen;
44 struct zink_shader_key;
45 struct zink_shader_module;
46 struct zink_gfx_program;
47 
48 struct nir_shader_compiler_options;
49 struct nir_shader;
50 
51 struct set;
52 
53 struct tgsi_token;
54 struct zink_so_info {
55    struct pipe_stream_output_info so_info;
56    unsigned so_info_slots[PIPE_MAX_SO_OUTPUTS];
57    bool have_xfb;
58 };
59 
60 
61 const void *
62 zink_get_compiler_options(struct pipe_screen *screen,
63                           enum pipe_shader_ir ir,
64                           enum pipe_shader_type shader);
65 
66 struct nir_shader *
67 zink_tgsi_to_nir(struct pipe_screen *screen, const struct tgsi_token *tokens);
68 
69 struct zink_shader {
70    struct util_live_shader base;
71    uint32_t hash;
72    struct nir_shader *nir;
73    enum pipe_prim_type reduced_prim; // PIPE_PRIM_MAX for vs
74 
75    struct zink_so_info streamout;
76 
77    struct {
78       int index;
79       int binding;
80       VkDescriptorType type;
81       unsigned char size;
82    } bindings[ZINK_DESCRIPTOR_TYPES][ZINK_MAX_DESCRIPTORS_PER_TYPE];
83    size_t num_bindings[ZINK_DESCRIPTOR_TYPES];
84    unsigned num_texel_buffers;
85    uint32_t ubos_used; // bitfield of which ubo indices are used
86    uint32_t ssbos_used; // bitfield of which ssbo indices are used
87    bool bindless;
88 
89    simple_mtx_t lock;
90    struct set *programs;
91 
92    union {
93       struct zink_shader *generated; // a generated shader that this shader "owns"
94       bool is_generated; // if this is a driver-created shader (e.g., tcs)
95       nir_variable *fbfetch; //for fs output
96    };
97 };
98 
99 void
100 zink_screen_init_compiler(struct zink_screen *screen);
101 void
102 zink_compiler_assign_io(nir_shader *producer, nir_shader *consumer);
103 VkShaderModule
104 zink_shader_compile(struct zink_screen *screen, struct zink_shader *zs, nir_shader *nir, const struct zink_shader_key *key);
105 
106 struct zink_shader *
107 zink_shader_create(struct zink_screen *screen, struct nir_shader *nir,
108                  const struct pipe_stream_output_info *so_info);
109 
110 char *
111 zink_shader_finalize(struct pipe_screen *pscreen, void *nirptr);
112 
113 void
114 zink_shader_free(struct zink_context *ctx, struct zink_shader *shader);
115 
116 struct zink_shader *
117 zink_shader_tcs_create(struct zink_screen *screen, struct zink_shader *vs, unsigned vertices_per_patch);
118 
119 static inline bool
zink_shader_descriptor_is_buffer(struct zink_shader * zs,enum zink_descriptor_type type,unsigned i)120 zink_shader_descriptor_is_buffer(struct zink_shader *zs, enum zink_descriptor_type type, unsigned i)
121 {
122    return zs->bindings[type][i].type == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER ||
123           zs->bindings[type][i].type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
124 }
125 
126 #endif
127