zink: refcount zink_gfx_program 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 uint32_t
111 hash_gfx_pipeline_state(const void *key)
112 {
113 return _mesa_hash_data(key, sizeof(struct zink_gfx_pipeline_state));
114 }
115
116 static bool
117 equals_gfx_pipeline_state(const void *a, const void *b)
118 {
119 return memcmp(a, b, sizeof(struct zink_gfx_pipeline_state)) == 0;
120 }
121
122 struct zink_gfx_program *
123 zink_create_gfx_program(struct zink_context *ctx,
124 struct zink_shader *stages[PIPE_SHADER_TYPES - 1])
125 {
126 struct zink_screen *screen = zink_screen(ctx->base.screen);
127 struct zink_gfx_program *prog = CALLOC_STRUCT(zink_gfx_program);
128 if (!prog)
129 goto fail;
130
131 pipe_reference_init(&prog->reference, 1);
132
133 for (int i = 0; i < ARRAY_SIZE(prog->pipelines); ++i) {
134 prog->pipelines[i] = _mesa_hash_table_create(NULL,
135 hash_gfx_pipeline_state,
136 equals_gfx_pipeline_state);
137 if (!prog->pipelines[i])
138 goto fail;
139 }
140
141 for (int i = 0; i < PIPE_SHADER_TYPES - 1; ++i) {
142 prog->stages[i] = stages[i];
143 if (stages[i]) {
144 _mesa_set_add(stages[i]->programs, prog);
145 zink_gfx_program_reference(screen, NULL, prog);
146 }
147 }
148
149 prog->dsl = create_desc_set_layout(screen->dev, stages,
150 &prog->num_descriptors);
151 if (!prog->dsl)
152 goto fail;
153
154 prog->layout = create_pipeline_layout(screen->dev, prog->dsl);
155 if (!prog->layout)
156 goto fail;
157
158 prog->render_passes = _mesa_set_create(NULL, _mesa_hash_pointer,
159 _mesa_key_pointer_equal);
160 if (!prog->render_passes)
161 goto fail;
162
163 return prog;
164
165 fail:
166 if (prog)
167 zink_destroy_gfx_program(screen, prog);
168 return NULL;
169 }
170
171 static void
172 gfx_program_remove_shader(struct zink_gfx_program *prog, struct zink_shader *shader)
173 {
174 enum pipe_shader_type p_stage = pipe_shader_type_from_mesa(shader->info.stage);
175
176 assert(prog->stages[p_stage] == shader);
177 prog->stages[p_stage] = NULL;
178 _mesa_set_remove_key(shader->programs, prog);
179 }
180
181 void
182 zink_destroy_gfx_program(struct zink_screen *screen,
183 struct zink_gfx_program *prog)
184 {
185 if (prog->layout)
186 vkDestroyPipelineLayout(screen->dev, prog->layout, NULL);
187
188 if (prog->dsl)
189 vkDestroyDescriptorSetLayout(screen->dev, prog->dsl, NULL);
190
191 for (int i = 0; i < PIPE_SHADER_TYPES - 1; ++i) {
192 if (prog->stages[i])
193 gfx_program_remove_shader(prog, prog->stages[i]);
194 }
195
196 /* unref all used render-passes */
197 if (prog->render_passes) {
198 set_foreach(prog->render_passes, entry) {
199 struct zink_render_pass *pres = (struct zink_render_pass *)entry->key;
200 zink_render_pass_reference(screen, &pres, NULL);
201 }
202 _mesa_set_destroy(prog->render_passes, NULL);
203 }
204
205 for (int i = 0; i < ARRAY_SIZE(prog->pipelines); ++i) {
206 hash_table_foreach(prog->pipelines[i], entry) {
207 struct pipeline_cache_entry *pc_entry = entry->data;
208
209 vkDestroyPipeline(screen->dev, pc_entry->pipeline, NULL);
210 free(pc_entry);
211 }
212 _mesa_hash_table_destroy(prog->pipelines[i], NULL);
213 }
214
215 FREE(prog);
216 }
217
218 static VkPrimitiveTopology
219 primitive_topology(enum pipe_prim_type mode)
220 {
221 switch (mode) {
222 case PIPE_PRIM_POINTS:
223 return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
224
225 case PIPE_PRIM_LINES:
226 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
227
228 case PIPE_PRIM_LINE_STRIP:
229 return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
230
231 case PIPE_PRIM_TRIANGLES:
232 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
233
234 case PIPE_PRIM_TRIANGLE_STRIP:
235 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
236
237 case PIPE_PRIM_TRIANGLE_FAN:
238 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN;
239
240 default:
241 unreachable("unexpected enum pipe_prim_type");
242 }
243 }
244
245 static void
246 reference_render_pass(struct zink_screen *screen,
247 struct zink_gfx_program *prog,
248 struct zink_render_pass *render_pass)
249 {
250 struct set_entry *entry = _mesa_set_search(prog->render_passes,
251 render_pass);
252 if (!entry) {
253 entry = _mesa_set_add(prog->render_passes, render_pass);
254 pipe_reference(NULL, &render_pass->reference);
255 }
256 }
257
258 VkPipeline
259 zink_get_gfx_pipeline(struct zink_screen *screen,
260 struct zink_gfx_program *prog,
261 struct zink_gfx_pipeline_state *state,
262 enum pipe_prim_type mode)
263 {
264 assert(mode <= ARRAY_SIZE(prog->pipelines));
265
266 /* TODO: use pre-hashed versions to save some time (can re-hash only when
267 state changes) */
268 struct hash_entry *entry = _mesa_hash_table_search(prog->pipelines[mode], state);
269 if (!entry) {
270 VkPrimitiveTopology vkmode = primitive_topology(mode);
271 VkPipeline pipeline = zink_create_gfx_pipeline(screen, prog,
272 state, vkmode);
273 if (pipeline == VK_NULL_HANDLE)
274 return VK_NULL_HANDLE;
275
276 struct pipeline_cache_entry *pc_entry = CALLOC_STRUCT(pipeline_cache_entry);
277 if (!pc_entry)
278 return VK_NULL_HANDLE;
279
280 memcpy(&pc_entry->state, state, sizeof(*state));
281 pc_entry->pipeline = pipeline;
282
283 entry = _mesa_hash_table_insert(prog->pipelines[mode], &pc_entry->state, pc_entry);
284 assert(entry);
285
286 reference_render_pass(screen, prog, state->render_pass);
287 }
288
289 return ((struct pipeline_cache_entry *)(entry->data))->pipeline;
290 }
291
292
293 static void *
294 zink_create_vs_state(struct pipe_context *pctx,
295 const struct pipe_shader_state *shader)
296 {
297 struct nir_shader *nir;
298 if (shader->type != PIPE_SHADER_IR_NIR)
299 nir = zink_tgsi_to_nir(pctx->screen, shader->tokens);
300 else
301 nir = (struct nir_shader *)shader->ir.nir;
302
303 return zink_compile_nir(zink_screen(pctx->screen), nir, &shader->stream_output);
304 }
305
306 static void
307 bind_stage(struct zink_context *ctx, enum pipe_shader_type stage,
308 struct zink_shader *shader)
309 {
310 assert(stage < PIPE_SHADER_COMPUTE);
311 ctx->gfx_stages[stage] = shader;
312 ctx->dirty_program = true;
313 }
314
315 static void
316 zink_bind_vs_state(struct pipe_context *pctx,
317 void *cso)
318 {
319 bind_stage(zink_context(pctx), PIPE_SHADER_VERTEX, cso);
320 }
321
322 static void
323 zink_delete_vs_state(struct pipe_context *pctx,
324 void *cso)
325 {
326 zink_shader_free(zink_context(pctx), cso);
327 }
328
329 static void *
330 zink_create_fs_state(struct pipe_context *pctx,
331 const struct pipe_shader_state *shader)
332 {
333 struct nir_shader *nir;
334 if (shader->type != PIPE_SHADER_IR_NIR)
335 nir = zink_tgsi_to_nir(pctx->screen, shader->tokens);
336 else
337 nir = (struct nir_shader *)shader->ir.nir;
338
339 return zink_compile_nir(zink_screen(pctx->screen), nir, NULL);
340 }
341
342 static void
343 zink_bind_fs_state(struct pipe_context *pctx,
344 void *cso)
345 {
346 bind_stage(zink_context(pctx), PIPE_SHADER_FRAGMENT, cso);
347 }
348
349 static void
350 zink_delete_fs_state(struct pipe_context *pctx,
351 void *cso)
352 {
353 zink_shader_free(zink_context(pctx), cso);
354 }
355
356
357 void
358 zink_program_init(struct zink_context *ctx)
359 {
360 ctx->base.create_vs_state = zink_create_vs_state;
361 ctx->base.bind_vs_state = zink_bind_vs_state;
362 ctx->base.delete_vs_state = zink_delete_vs_state;
363
364 ctx->base.create_fs_state = zink_create_fs_state;
365 ctx->base.bind_fs_state = zink_bind_fs_state;
366 ctx->base.delete_fs_state = zink_delete_fs_state;
367 }