zink: split up creating zink_shader objects and VkShaderModule objects
[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 void
43 debug_describe_zink_gfx_program(char *buf, const struct zink_gfx_program *ptr)
44 {
45 sprintf(buf, "zink_gfx_program");
46 }
47
48 static VkDescriptorSetLayout
49 create_desc_set_layout(VkDevice dev,
50 struct zink_shader *stages[PIPE_SHADER_TYPES - 1],
51 unsigned *num_descriptors)
52 {
53 VkDescriptorSetLayoutBinding bindings[PIPE_SHADER_TYPES * PIPE_MAX_CONSTANT_BUFFERS];
54 int num_bindings = 0;
55
56 for (int i = 0; i < PIPE_SHADER_TYPES - 1; i++) {
57 struct zink_shader *shader = stages[i];
58 if (!shader)
59 continue;
60
61 VkShaderStageFlagBits stage_flags = zink_shader_stage(i);
62 for (int j = 0; j < shader->num_bindings; j++) {
63 assert(num_bindings < ARRAY_SIZE(bindings));
64 bindings[num_bindings].binding = shader->bindings[j].binding;
65 bindings[num_bindings].descriptorType = shader->bindings[j].type;
66 bindings[num_bindings].descriptorCount = 1;
67 bindings[num_bindings].stageFlags = stage_flags;
68 bindings[num_bindings].pImmutableSamplers = NULL;
69 ++num_bindings;
70 }
71 }
72
73 VkDescriptorSetLayoutCreateInfo dcslci = {};
74 dcslci.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
75 dcslci.pNext = NULL;
76 dcslci.flags = 0;
77 dcslci.bindingCount = num_bindings;
78 dcslci.pBindings = bindings;
79
80 VkDescriptorSetLayout dsl;
81 if (vkCreateDescriptorSetLayout(dev, &dcslci, 0, &dsl) != VK_SUCCESS) {
82 debug_printf("vkCreateDescriptorSetLayout failed\n");
83 return VK_NULL_HANDLE;
84 }
85
86 *num_descriptors = num_bindings;
87 return dsl;
88 }
89
90 static VkPipelineLayout
91 create_pipeline_layout(VkDevice dev, VkDescriptorSetLayout dsl)
92 {
93 assert(dsl != VK_NULL_HANDLE);
94
95 VkPipelineLayoutCreateInfo plci = {};
96 plci.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
97
98 plci.pSetLayouts = &dsl;
99 plci.setLayoutCount = 1;
100
101 VkPipelineLayout layout;
102 if (vkCreatePipelineLayout(dev, &plci, NULL, &layout) != VK_SUCCESS) {
103 debug_printf("vkCreatePipelineLayout failed!\n");
104 return VK_NULL_HANDLE;
105 }
106
107 return layout;
108 }
109
110 static void
111 update_shader_modules(struct zink_context *ctx, struct zink_shader *stages[PIPE_SHADER_TYPES - 1], struct zink_gfx_program *prog)
112 {
113 for (int i = 0; i < PIPE_SHADER_TYPES - 1; ++i) {
114 if (stages[i]) {
115 prog->stages[i] = zink_shader_compile(zink_screen(ctx->base.screen), stages[i]);
116 prog->shaders[i] = stages[i];
117 }
118 }
119 }
120
121 static uint32_t
122 hash_gfx_pipeline_state(const void *key)
123 {
124 return _mesa_hash_data(key, sizeof(struct zink_gfx_pipeline_state));
125 }
126
127 static bool
128 equals_gfx_pipeline_state(const void *a, const void *b)
129 {
130 return memcmp(a, b, sizeof(struct zink_gfx_pipeline_state)) == 0;
131 }
132
133 struct zink_gfx_program *
134 zink_create_gfx_program(struct zink_context *ctx,
135 struct zink_shader *stages[PIPE_SHADER_TYPES - 1])
136 {
137 struct zink_screen *screen = zink_screen(ctx->base.screen);
138 struct zink_gfx_program *prog = CALLOC_STRUCT(zink_gfx_program);
139 if (!prog)
140 goto fail;
141
142 pipe_reference_init(&prog->reference, 1);
143
144 update_shader_modules(ctx, stages, prog);
145
146 for (int i = 0; i < ARRAY_SIZE(prog->pipelines); ++i) {
147 prog->pipelines[i] = _mesa_hash_table_create(NULL,
148 hash_gfx_pipeline_state,
149 equals_gfx_pipeline_state);
150 if (!prog->pipelines[i])
151 goto fail;
152 }
153
154 for (int i = 0; i < PIPE_SHADER_TYPES - 1; ++i) {
155 if (prog->stages[i]) {
156 _mesa_set_add(stages[i]->programs, prog);
157 zink_gfx_program_reference(screen, NULL, prog);
158 }
159 }
160
161 prog->dsl = create_desc_set_layout(screen->dev, stages,
162 &prog->num_descriptors);
163 if (!prog->dsl)
164 goto fail;
165
166 prog->layout = create_pipeline_layout(screen->dev, prog->dsl);
167 if (!prog->layout)
168 goto fail;
169
170 prog->render_passes = _mesa_set_create(NULL, _mesa_hash_pointer,
171 _mesa_key_pointer_equal);
172 if (!prog->render_passes)
173 goto fail;
174
175 return prog;
176
177 fail:
178 if (prog)
179 zink_destroy_gfx_program(screen, prog);
180 return NULL;
181 }
182
183 static void
184 gfx_program_remove_shader(struct zink_gfx_program *prog, struct zink_shader *shader)
185 {
186 enum pipe_shader_type p_stage = pipe_shader_type_from_mesa(shader->nir->info.stage);
187
188 assert(prog->shaders[p_stage] == shader);
189 prog->shaders[p_stage] = NULL;
190 _mesa_set_remove_key(shader->programs, prog);
191 }
192
193 void
194 zink_destroy_gfx_program(struct zink_screen *screen,
195 struct zink_gfx_program *prog)
196 {
197 if (prog->layout)
198 vkDestroyPipelineLayout(screen->dev, prog->layout, NULL);
199
200 if (prog->dsl)
201 vkDestroyDescriptorSetLayout(screen->dev, prog->dsl, NULL);
202
203 for (int i = 0; i < PIPE_SHADER_TYPES - 1; ++i) {
204 if (prog->shaders[i])
205 gfx_program_remove_shader(prog, prog->shaders[i]);
206 if (prog->stages[i])
207 vkDestroyShaderModule(screen->dev, prog->stages[i], NULL);
208 }
209
210 /* unref all used render-passes */
211 if (prog->render_passes) {
212 set_foreach(prog->render_passes, entry) {
213 struct zink_render_pass *pres = (struct zink_render_pass *)entry->key;
214 zink_render_pass_reference(screen, &pres, NULL);
215 }
216 _mesa_set_destroy(prog->render_passes, NULL);
217 }
218
219 for (int i = 0; i < ARRAY_SIZE(prog->pipelines); ++i) {
220 hash_table_foreach(prog->pipelines[i], entry) {
221 struct pipeline_cache_entry *pc_entry = entry->data;
222
223 vkDestroyPipeline(screen->dev, pc_entry->pipeline, NULL);
224 free(pc_entry);
225 }
226 _mesa_hash_table_destroy(prog->pipelines[i], NULL);
227 }
228
229 FREE(prog);
230 }
231
232 static VkPrimitiveTopology
233 primitive_topology(enum pipe_prim_type mode)
234 {
235 switch (mode) {
236 case PIPE_PRIM_POINTS:
237 return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
238
239 case PIPE_PRIM_LINES:
240 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
241
242 case PIPE_PRIM_LINE_STRIP:
243 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
244
245 case PIPE_PRIM_TRIANGLES:
246 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
247
248 case PIPE_PRIM_TRIANGLE_STRIP:
249 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
250
251 case PIPE_PRIM_TRIANGLE_FAN:
252 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN;
253
254 default:
255 unreachable("unexpected enum pipe_prim_type");
256 }
257 }
258
259 static void
260 reference_render_pass(struct zink_screen *screen,
261 struct zink_gfx_program *prog,
262 struct zink_render_pass *render_pass)
263 {
264 struct set_entry *entry = _mesa_set_search(prog->render_passes,
265 render_pass);
266 if (!entry) {
267 entry = _mesa_set_add(prog->render_passes, render_pass);
268 pipe_reference(NULL, &render_pass->reference);
269 }
270 }
271
272 VkPipeline
273 zink_get_gfx_pipeline(struct zink_screen *screen,
274 struct zink_gfx_program *prog,
275 struct zink_gfx_pipeline_state *state,
276 enum pipe_prim_type mode)
277 {
278 assert(mode <= ARRAY_SIZE(prog->pipelines));
279
280 /* TODO: use pre-hashed versions to save some time (can re-hash only when
281 state changes) */
282 struct hash_entry *entry = _mesa_hash_table_search(prog->pipelines[mode], state);
283 if (!entry) {
284 VkPrimitiveTopology vkmode = primitive_topology(mode);
285 VkPipeline pipeline = zink_create_gfx_pipeline(screen, prog,
286 state, vkmode);
287 if (pipeline == VK_NULL_HANDLE)
288 return VK_NULL_HANDLE;
289
290 struct pipeline_cache_entry *pc_entry = CALLOC_STRUCT(pipeline_cache_entry);
291 if (!pc_entry)
292 return VK_NULL_HANDLE;
293
294 memcpy(&pc_entry->state, state, sizeof(*state));
295 pc_entry->pipeline = pipeline;
296
297 entry = _mesa_hash_table_insert(prog->pipelines[mode], &pc_entry->state, pc_entry);
298 assert(entry);
299
300 reference_render_pass(screen, prog, state->render_pass);
301 }
302
303 return ((struct pipeline_cache_entry *)(entry->data))->pipeline;
304 }
305
306
307 static void *
308 zink_create_vs_state(struct pipe_context *pctx,
309 const struct pipe_shader_state *shader)
310 {
311 struct nir_shader *nir;
312 if (shader->type != PIPE_SHADER_IR_NIR)
313 nir = zink_tgsi_to_nir(pctx->screen, shader->tokens);
314 else
315 nir = (struct nir_shader *)shader->ir.nir;
316
317 return zink_shader_create(zink_screen(pctx->screen), nir, &shader->stream_output);
318 }
319
320 static void
321 bind_stage(struct zink_context *ctx, enum pipe_shader_type stage,
322 struct zink_shader *shader)
323 {
324 assert(stage < PIPE_SHADER_COMPUTE);
325 ctx->gfx_stages[stage] = shader;
326 ctx->dirty_program = true;
327 }
328
329 static void
330 zink_bind_vs_state(struct pipe_context *pctx,
331 void *cso)
332 {
333 bind_stage(zink_context(pctx), PIPE_SHADER_VERTEX, cso);
334 }
335
336 static void
337 zink_delete_vs_state(struct pipe_context *pctx,
338 void *cso)
339 {
340 zink_shader_free(zink_context(pctx), cso);
341 }
342
343 static void *
344 zink_create_fs_state(struct pipe_context *pctx,
345 const struct pipe_shader_state *shader)
346 {
347 struct nir_shader *nir;
348 if (shader->type != PIPE_SHADER_IR_NIR)
349 nir = zink_tgsi_to_nir(pctx->screen, shader->tokens);
350 else
351 nir = (struct nir_shader *)shader->ir.nir;
352
353 return zink_shader_create(zink_screen(pctx->screen), nir, NULL);
354 }
355
356 static void
357 zink_bind_fs_state(struct pipe_context *pctx,
358 void *cso)
359 {
360 bind_stage(zink_context(pctx), PIPE_SHADER_FRAGMENT, cso);
361 }
362
363 static void
364 zink_delete_fs_state(struct pipe_context *pctx,
365 void *cso)
366 {
367 zink_shader_free(zink_context(pctx), cso);
368 }
369
370
371 void
372 zink_program_init(struct zink_context *ctx)
373 {
374 ctx->base.create_vs_state = zink_create_vs_state;
375 ctx->base.bind_vs_state = zink_bind_vs_state;
376 ctx->base.delete_vs_state = zink_delete_vs_state;
377
378 ctx->base.create_fs_state = zink_create_fs_state;
379 ctx->base.bind_fs_state = zink_bind_fs_state;
380 ctx->base.delete_fs_state = zink_delete_fs_state;
381 }