}
}
+VkShaderModule
+zink_shader_compile(struct zink_screen *screen, struct zink_shader *zs)
+{
+ VkShaderModule mod = NULL;
+ void *streamout = zs->streamout.so_info_slots ? &zs->streamout : NULL;
+ struct spirv_shader *spirv = nir_to_spirv(zs->nir, streamout);
+ assert(spirv);
+
+ if (zink_debug & ZINK_DEBUG_SPIRV) {
+ char buf[256];
+ static int i;
+ snprintf(buf, sizeof(buf), "dump%02d.spv", i++);
+ FILE *fp = fopen(buf, "wb");
+ if (fp) {
+ fwrite(spirv->words, sizeof(uint32_t), spirv->num_words, fp);
+ fclose(fp);
+ fprintf(stderr, "wrote '%s'...\n", buf);
+ }
+ }
+
+ VkShaderModuleCreateInfo smci = {};
+ smci.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
+ smci.codeSize = spirv->num_words * sizeof(uint32_t);
+ smci.pCode = spirv->words;
+
+ if (vkCreateShaderModule(screen->dev, &smci, NULL, &mod) != VK_SUCCESS)
+ mod = NULL;
+
+ /* TODO: determine if there's any reason to cache spirv output? */
+ free(spirv->words);
+ free(spirv);
+ return mod;
+}
+
struct zink_shader *
-zink_compile_nir(struct zink_screen *screen, struct nir_shader *nir,
- const struct pipe_stream_output_info *so_info)
+zink_shader_create(struct zink_screen *screen, struct nir_shader *nir,
+ const struct pipe_stream_output_info *so_info)
{
struct zink_shader *ret = CALLOC_STRUCT(zink_shader);
bool have_psiz = false;
}
}
- ret->info = nir->info;
+ ret->nir = nir;
if (so_info) {
memcpy(&ret->streamout.so_info, so_info, sizeof(struct pipe_stream_output_info));
ret->streamout.so_info_slots = malloc(so_info->num_outputs * sizeof(unsigned int));
update_so_info(ret, nir->info.outputs_written, have_psiz);
}
- struct spirv_shader *spirv = nir_to_spirv(nir, so_info ? &ret->streamout : NULL);
- assert(spirv);
-
- if (zink_debug & ZINK_DEBUG_SPIRV) {
- char buf[256];
- static int i;
- snprintf(buf, sizeof(buf), "dump%02d.spv", i++);
- FILE *fp = fopen(buf, "wb");
- if (fp) {
- fwrite(spirv->words, sizeof(uint32_t), spirv->num_words, fp);
- fclose(fp);
- fprintf(stderr, "wrote '%s'...\n", buf);
- }
- }
-
- VkShaderModuleCreateInfo smci = {};
- smci.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
- smci.codeSize = spirv->num_words * sizeof(uint32_t);
- smci.pCode = spirv->words;
-
- if (vkCreateShaderModule(screen->dev, &smci, NULL, &ret->shader_module) != VK_SUCCESS)
- return NULL;
-
- free(spirv->words);
- free(spirv);
-
return ret;
}
zink_shader_free(struct zink_context *ctx, struct zink_shader *shader)
{
struct zink_screen *screen = zink_screen(ctx->base.screen);
- vkDestroyShaderModule(screen->dev, shader->shader_module, NULL);
set_foreach(shader->programs, entry) {
struct zink_gfx_program *prog = (void*)entry->key;
- _mesa_hash_table_remove_key(ctx->program_cache, prog->stages);
- prog->stages[pipe_shader_type_from_mesa(shader->info.stage)] = NULL;
+ _mesa_hash_table_remove_key(ctx->program_cache, prog->shaders);
+ prog->shaders[pipe_shader_type_from_mesa(shader->nir->info.stage)] = NULL;
zink_gfx_program_reference(screen, &prog, NULL);
}
- _mesa_set_destroy(shader->programs, NULL
+ _mesa_set_destroy(shader->programs, NULL);
free(shader->streamout.so_info_slots);
+ ralloc_free(shader->nir);
FREE(shader);
}
#include "pipe/p_defines.h"
#include "pipe/p_state.h"
+#include "compiler/nir/nir.h"
#include "compiler/shader_info.h"
#include <vulkan/vulkan.h>
zink_tgsi_to_nir(struct pipe_screen *screen, const struct tgsi_token *tokens);
struct zink_shader {
- VkShaderModule shader_module;
+ struct nir_shader *nir;
- shader_info info;
struct zink_so_info streamout;
struct {
struct set *programs;
};
+VkShaderModule
+zink_shader_compile(struct zink_screen *screen, struct zink_shader *zs);
+
struct zink_shader *
-zink_compile_nir(struct zink_screen *screen, struct nir_shader *nir,
+zink_shader_create(struct zink_screen *screen, struct nir_shader *nir,
const struct pipe_stream_output_info *so_info);
void
if (!entry) {
struct zink_gfx_program *prog;
prog = zink_create_gfx_program(ctx, ctx->gfx_stages);
- entry = _mesa_hash_table_insert(ctx->program_cache, prog->stages, prog);
+ entry = _mesa_hash_table_insert(ctx->program_cache, prog->shaders, prog);
if (!entry)
return NULL;
}
VkPipelineShaderStageCreateInfo stage = {};
stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stage.stage = zink_shader_stage(i);
- stage.module = prog->stages[i]->shader_module;
+ stage.module = prog->stages[i];
stage.pName = "main";
shader_stages[num_stages++] = stage;
}
return layout;
}
+static void
+update_shader_modules(struct zink_context *ctx, struct zink_shader *stages[PIPE_SHADER_TYPES - 1], struct zink_gfx_program *prog)
+{
+ for (int i = 0; i < PIPE_SHADER_TYPES - 1; ++i) {
+ if (stages[i]) {
+ prog->stages[i] = zink_shader_compile(zink_screen(ctx->base.screen), stages[i]);
+ prog->shaders[i] = stages[i];
+ }
+ }
+}
+
static uint32_t
hash_gfx_pipeline_state(const void *key)
{
pipe_reference_init(&prog->reference, 1);
+ update_shader_modules(ctx, stages, prog);
+
for (int i = 0; i < ARRAY_SIZE(prog->pipelines); ++i) {
prog->pipelines[i] = _mesa_hash_table_create(NULL,
hash_gfx_pipeline_state,
}
for (int i = 0; i < PIPE_SHADER_TYPES - 1; ++i) {
- prog->stages[i] = stages[i];
- if (stages[i]) {
+ if (prog->stages[i]) {
_mesa_set_add(stages[i]->programs, prog);
zink_gfx_program_reference(screen, NULL, prog);
}
static void
gfx_program_remove_shader(struct zink_gfx_program *prog, struct zink_shader *shader)
{
- enum pipe_shader_type p_stage = pipe_shader_type_from_mesa(shader->info.stage);
+ enum pipe_shader_type p_stage = pipe_shader_type_from_mesa(shader->nir->info.stage);
- assert(prog->stages[p_stage] == shader);
- prog->stages[p_stage] = NULL;
+ assert(prog->shaders[p_stage] == shader);
+ prog->shaders[p_stage] = NULL;
_mesa_set_remove_key(shader->programs, prog);
}
vkDestroyDescriptorSetLayout(screen->dev, prog->dsl, NULL);
for (int i = 0; i < PIPE_SHADER_TYPES - 1; ++i) {
+ if (prog->shaders[i])
+ gfx_program_remove_shader(prog, prog->shaders[i]);
if (prog->stages[i])
- gfx_program_remove_shader(prog, prog->stages[i]);
+ vkDestroyShaderModule(screen->dev, prog->stages[i], NULL);
}
/* unref all used render-passes */
else
nir = (struct nir_shader *)shader->ir.nir;
- return zink_compile_nir(zink_screen(pctx->screen), nir, &shader->stream_output);
+ return zink_shader_create(zink_screen(pctx->screen), nir, &shader->stream_output);
}
static void
else
nir = (struct nir_shader *)shader->ir.nir;
- return zink_compile_nir(zink_screen(pctx->screen), nir, NULL);
+ return zink_shader_create(zink_screen(pctx->screen), nir, NULL);
}
static void
struct zink_gfx_program {
struct pipe_reference reference;
- struct zink_shader *stages[PIPE_SHADER_TYPES - 1]; // compute stage doesn't belong here
+ VkShaderModule stages[PIPE_SHADER_TYPES - 1]; // compute stage doesn't belong here
+ struct zink_shader *shaders[PIPE_SHADER_TYPES - 1];
VkDescriptorSetLayout dsl;
VkPipelineLayout layout;
unsigned num_descriptors;