341c3293ac2a2dea0d8bd9a310603d92dfcdb6d5
[mesa.git] / src / gallium / drivers / zink / zink_program.c
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 #include "zink_program.h"
25
26 #include "zink_compiler.h"
27 #include "zink_context.h"
28 #include "zink_render_pass.h"
29 #include "zink_screen.h"
30
31 #include "util/hash_table.h"
32 #include "util/set.h"
33 #include "util/u_debug.h"
34 #include "util/u_memory.h"
35 #include "tgsi/tgsi_from_mesa.h"
36
37 struct pipeline_cache_entry {
38 struct zink_gfx_pipeline_state state;
39 VkPipeline pipeline;
40 };
41
42 static VkDescriptorSetLayout
43 create_desc_set_layout(VkDevice dev,
44 struct zink_shader *stages[PIPE_SHADER_TYPES - 1],
45 unsigned *num_descriptors)
46 {
47 VkDescriptorSetLayoutBinding bindings[PIPE_SHADER_TYPES * PIPE_MAX_CONSTANT_BUFFERS];
48 int num_bindings = 0;
49
50 for (int i = 0; i < PIPE_SHADER_TYPES - 1; i++) {
51 struct zink_shader *shader = stages[i];
52 if (!shader)
53 continue;
54
55 VkShaderStageFlagBits stage_flags = zink_shader_stage(i);
56 for (int j = 0; j < shader->num_bindings; j++) {
57 assert(num_bindings < ARRAY_SIZE(bindings));
58 bindings[num_bindings].binding = shader->bindings[j].binding;
59 bindings[num_bindings].descriptorType = shader->bindings[j].type;
60 bindings[num_bindings].descriptorCount = 1;
61 bindings[num_bindings].stageFlags = stage_flags;
62 bindings[num_bindings].pImmutableSamplers = NULL;
63 ++num_bindings;
64 }
65 }
66
67 VkDescriptorSetLayoutCreateInfo dcslci = {};
68 dcslci.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
69 dcslci.pNext = NULL;
70 dcslci.flags = 0;
71 dcslci.bindingCount = num_bindings;
72 dcslci.pBindings = bindings;
73
74 VkDescriptorSetLayout dsl;
75 if (vkCreateDescriptorSetLayout(dev, &dcslci, 0, &dsl) != VK_SUCCESS) {
76 debug_printf("vkCreateDescriptorSetLayout failed\n");
77 return VK_NULL_HANDLE;
78 }
79
80 *num_descriptors = num_bindings;
81 return dsl;
82 }
83
84 static VkPipelineLayout
85 create_pipeline_layout(VkDevice dev, VkDescriptorSetLayout dsl)
86 {
87 assert(dsl != VK_NULL_HANDLE);
88
89 VkPipelineLayoutCreateInfo plci = {};
90 plci.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
91
92 plci.pSetLayouts = &dsl;
93 plci.setLayoutCount = 1;
94
95 VkPipelineLayout layout;
96 if (vkCreatePipelineLayout(dev, &plci, NULL, &layout) != VK_SUCCESS) {
97 debug_printf("vkCreatePipelineLayout failed!\n");
98 return VK_NULL_HANDLE;
99 }
100
101 return layout;
102 }
103
104 static uint32_t
105 hash_gfx_pipeline_state(const void *key)
106 {
107 return _mesa_hash_data(key, sizeof(struct zink_gfx_pipeline_state));
108 }
109
110 static bool
111 equals_gfx_pipeline_state(const void *a, const void *b)
112 {
113 return memcmp(a, b, sizeof(struct zink_gfx_pipeline_state)) == 0;
114 }
115
116 struct zink_gfx_program *
117 zink_create_gfx_program(struct zink_context *ctx,
118 struct zink_shader *stages[PIPE_SHADER_TYPES - 1])
119 {
120 struct zink_screen *screen = zink_screen(ctx->base.screen);
121 struct zink_gfx_program *prog = CALLOC_STRUCT(zink_gfx_program);
122 if (!prog)
123 goto fail;
124
125 for (int i = 0; i < ARRAY_SIZE(prog->pipelines); ++i) {
126 prog->pipelines[i] = _mesa_hash_table_create(NULL,
127 hash_gfx_pipeline_state,
128 equals_gfx_pipeline_state);
129 if (!prog->pipelines[i])
130 goto fail;
131 }
132
133 for (int i = 0; i < PIPE_SHADER_TYPES - 1; ++i) {
134 prog->stages[i] = stages[i];
135 if (stages[i])
136 _mesa_set_add(stages[i]->programs, prog);
137 }
138
139 prog->dsl = create_desc_set_layout(screen->dev, stages,
140 &prog->num_descriptors);
141 if (!prog->dsl)
142 goto fail;
143
144 prog->layout = create_pipeline_layout(screen->dev, prog->dsl);
145 if (!prog->layout)
146 goto fail;
147
148 prog->render_passes = _mesa_set_create(NULL, _mesa_hash_pointer,
149 _mesa_key_pointer_equal);
150 if (!prog->render_passes)
151 goto fail;
152
153 return prog;
154
155 fail:
156 if (prog)
157 zink_destroy_gfx_program(screen, prog);
158 return NULL;
159 }
160
161 static void
162 gfx_program_remove_shader(struct zink_gfx_program *prog, struct zink_shader *shader)
163 {
164 enum pipe_shader_type p_stage = pipe_shader_type_from_mesa(shader->info.stage);
165
166 assert(prog->stages[p_stage] == shader);
167 prog->stages[p_stage] = NULL;
168 _mesa_set_remove_key(shader->programs, prog);
169 }
170
171 void
172 zink_destroy_gfx_program(struct zink_screen *screen,
173 struct zink_gfx_program *prog)
174 {
175 if (prog->layout)
176 vkDestroyPipelineLayout(screen->dev, prog->layout, NULL);
177
178 if (prog->dsl)
179 vkDestroyDescriptorSetLayout(screen->dev, prog->dsl, NULL);
180
181 for (int i = 0; i < PIPE_SHADER_TYPES - 1; ++i) {
182 if (prog->stages[i])
183 gfx_program_remove_shader(prog, prog->stages[i]);
184 }
185
186 /* unref all used render-passes */
187 if (prog->render_passes) {
188 set_foreach(prog->render_passes, entry) {
189 struct zink_render_pass *pres = (struct zink_render_pass *)entry->key;
190 zink_render_pass_reference(screen, &pres, NULL);
191 }
192 _mesa_set_destroy(prog->render_passes, NULL);
193 }
194
195 for (int i = 0; i < ARRAY_SIZE(prog->pipelines); ++i) {
196 hash_table_foreach(prog->pipelines[i], entry) {
197 struct pipeline_cache_entry *pc_entry = entry->data;
198
199 vkDestroyPipeline(screen->dev, pc_entry->pipeline, NULL);
200 free(pc_entry);
201 }
202 _mesa_hash_table_destroy(prog->pipelines[i], NULL);
203 }
204
205 FREE(prog);
206 }
207
208 static VkPrimitiveTopology
209 primitive_topology(enum pipe_prim_type mode)
210 {
211 switch (mode) {
212 case PIPE_PRIM_POINTS:
213 return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
214
215 case PIPE_PRIM_LINES:
216 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
217
218 case PIPE_PRIM_LINE_STRIP:
219 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
220
221 case PIPE_PRIM_TRIANGLES:
222 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
223
224 case PIPE_PRIM_TRIANGLE_STRIP:
225 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
226
227 case PIPE_PRIM_TRIANGLE_FAN:
228 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN;
229
230 default:
231 unreachable("unexpected enum pipe_prim_type");
232 }
233 }
234
235 static void
236 reference_render_pass(struct zink_screen *screen,
237 struct zink_gfx_program *prog,
238 struct zink_render_pass *render_pass)
239 {
240 struct set_entry *entry = _mesa_set_search(prog->render_passes,
241 render_pass);
242 if (!entry) {
243 entry = _mesa_set_add(prog->render_passes, render_pass);
244 pipe_reference(NULL, &render_pass->reference);
245 }
246 }
247
248 VkPipeline
249 zink_get_gfx_pipeline(struct zink_screen *screen,
250 struct zink_gfx_program *prog,
251 struct zink_gfx_pipeline_state *state,
252 enum pipe_prim_type mode)
253 {
254 assert(mode <= ARRAY_SIZE(prog->pipelines));
255
256 /* TODO: use pre-hashed versions to save some time (can re-hash only when
257 state changes) */
258 struct hash_entry *entry = _mesa_hash_table_search(prog->pipelines[mode], state);
259 if (!entry) {
260 VkPrimitiveTopology vkmode = primitive_topology(mode);
261 VkPipeline pipeline = zink_create_gfx_pipeline(screen, prog,
262 state, vkmode);
263 if (pipeline == VK_NULL_HANDLE)
264 return VK_NULL_HANDLE;
265
266 struct pipeline_cache_entry *pc_entry = CALLOC_STRUCT(pipeline_cache_entry);
267 if (!pc_entry)
268 return VK_NULL_HANDLE;
269
270 memcpy(&pc_entry->state, state, sizeof(*state));
271 pc_entry->pipeline = pipeline;
272
273 entry = _mesa_hash_table_insert(prog->pipelines[mode], &pc_entry->state, pc_entry);
274 assert(entry);
275
276 reference_render_pass(screen, prog, state->render_pass);
277 }
278
279 return ((struct pipeline_cache_entry *)(entry->data))->pipeline;
280 }