vulkan: Create program in program_state_cache<>::build_pipeline

This commit is contained in:
Vincent Lejeune 2016-03-20 02:26:51 +01:00
parent 1f500aad53
commit 6f9f5f7918
5 changed files with 259 additions and 594 deletions

View file

@ -5,385 +5,20 @@ namespace vk
{
namespace glsl
{
program::program()
program::program(VkDevice dev, VkPipeline p, const std::vector<program_input> &vertex_input, const std::vector<program_input>& fragment_inputs)
: m_device(dev), pipeline(p)
{
memset(&pstate, 0, sizeof(pstate));
load_uniforms(glsl::program_domain::glsl_vertex_program, vertex_input);
load_uniforms(glsl::program_domain::glsl_vertex_program, fragment_inputs);
}
program::program(vk::render_device &renderer)
{
memset(&pstate, 0, sizeof(pstate));
init_pipeline();
device = &renderer;
}
program::program(program&& other)
{
//This object does not yet exist in a valid state. Clear the original
memset(&pstate, 0, sizeof(pstate));
pipeline_state tmp;
memcpy(&tmp, &pstate, sizeof pstate);
memcpy(&pstate, &other.pstate, sizeof pstate);
memcpy(&other.pstate, &tmp, sizeof pstate);
std::vector<program_input> tmp_uniforms = uniforms;
uniforms = other.uniforms;
other.uniforms = tmp_uniforms;
vk::render_device *tmp_dev = device;
device = other.device;
other.device = tmp_dev;
bool _uniforms_changed = uniforms_changed;
uniforms_changed = other.uniforms_changed;
other.uniforms_changed = _uniforms_changed;
}
program& program::operator = (program&& other)
{
pipeline_state tmp;
memcpy(&tmp, &pstate, sizeof pstate);
memcpy(&pstate, &other.pstate, sizeof pstate);
memcpy(&other.pstate, &tmp, sizeof pstate);
std::vector<program_input> tmp_uniforms = uniforms;
uniforms = other.uniforms;
other.uniforms = tmp_uniforms;
vk::render_device *tmp_dev = device;
device = other.device;
other.device = tmp_dev;
bool _uniforms_changed = uniforms_changed;
uniforms_changed = other.uniforms_changed;
other.uniforms_changed = _uniforms_changed;
return *this;
}
void program::init_pipeline()
{
pstate.dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
pstate.dynamic_state.pDynamicStates = pstate.dynamic_state_descriptors;
pstate.pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pstate.pipeline.layout = nullptr;
pstate.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
pstate.ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
pstate.ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
pstate.rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
pstate.rs.polygonMode = VK_POLYGON_MODE_FILL;
pstate.rs.cullMode = VK_CULL_MODE_NONE;
pstate.rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
pstate.rs.depthClampEnable = VK_FALSE;
pstate.rs.rasterizerDiscardEnable = VK_FALSE;
pstate.rs.depthBiasEnable = VK_FALSE;
pstate.cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
pstate.cb.attachmentCount = 1;
pstate.cb.pAttachments = pstate.att_state;
for (int i = 0; i < 4; ++i)
{
pstate.att_state[i].colorWriteMask = 0xf;
pstate.att_state[i].blendEnable = VK_FALSE;
}
pstate.vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
pstate.vp.viewportCount = 1;
pstate.dynamic_state_descriptors[pstate.dynamic_state.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
pstate.vp.scissorCount = 1;
pstate.dynamic_state_descriptors[pstate.dynamic_state.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
pstate.dynamic_state_descriptors[pstate.dynamic_state.dynamicStateCount++] = VK_DYNAMIC_STATE_LINE_WIDTH;
pstate.ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
pstate.ds.depthTestEnable = VK_FALSE;
pstate.ds.depthWriteEnable = VK_TRUE;
pstate.ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
pstate.ds.depthBoundsTestEnable = VK_FALSE;
pstate.ds.back.failOp = VK_STENCIL_OP_KEEP;
pstate.ds.back.passOp = VK_STENCIL_OP_KEEP;
pstate.ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
pstate.ds.stencilTestEnable = VK_FALSE;
pstate.ds.front = pstate.ds.back;
pstate.ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
pstate.ms.pSampleMask = NULL;
pstate.ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
pstate.fs = nullptr;
pstate.vs = nullptr;
pstate.dirty = true;
pstate.pipeline.stageCount = 2;
pstate.shader_stages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
pstate.shader_stages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
pstate.shader_stages[0].module = nullptr;
pstate.shader_stages[0].pName = "main";
pstate.shader_stages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
pstate.shader_stages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
pstate.shader_stages[1].module = nullptr;
pstate.shader_stages[1].pName = "main";
pstate.pipeline_cache_desc.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
}
program::~program()
program::~program()
{
LOG_ERROR(RSX, "Program destructor invoked!");
destroy();
vkDestroyPipeline(m_device, pipeline, nullptr);
}
program& program::attach_device(vk::render_device &dev)
{
if (!device)
init_pipeline();
device = &dev;
return *this;
}
program& program::attachFragmentProgram(VkShaderModule prog)
{
pstate.fs = prog;
return *this;
}
program& program::attachVertexProgram(VkShaderModule prog)
{
pstate.vs = prog;
return *this;
}
void program::make()
{
if (pstate.fs == nullptr || pstate.vs == nullptr)
throw EXCEPTION("Missing shader stage!");
pstate.shader_stages[0].module = pstate.vs;
pstate.shader_stages[1].module = pstate.fs;
CHECK_RESULT(vkCreatePipelineCache((*device), &pstate.pipeline_cache_desc, nullptr, &pstate.pipeline_cache));
}
void program::set_depth_compare_op(VkCompareOp op)
{
if (pstate.ds.depthCompareOp != op)
{
pstate.ds.depthCompareOp = op;
pstate.dirty = true;
}
}
void program::set_depth_write_mask(VkBool32 write_enable)
{
if (pstate.ds.depthWriteEnable != write_enable)
{
pstate.ds.depthWriteEnable = write_enable;
pstate.dirty = true;
}
}
void program::set_depth_test_enable(VkBool32 state)
{
if (pstate.ds.depthTestEnable != state)
{
pstate.ds.depthTestEnable = state;
pstate.dirty = true;
}
}
void program::set_primitive_topology(VkPrimitiveTopology topology)
{
if (pstate.ia.topology != topology)
{
pstate.ia.topology = topology;
pstate.dirty = true;
}
}
void program::set_color_mask(int num_targets, u8* targets, VkColorComponentFlags* flags)
{
if (num_targets)
{
for (u8 idx = 0; idx < num_targets; ++idx)
{
u8 &id = targets[idx];
if (pstate.att_state[id].colorWriteMask != flags[idx])
{
pstate.att_state[id].colorWriteMask = flags[idx];
pstate.dirty = true;
}
}
}
}
void program::set_blend_state(int num_targets, u8* targets, VkBool32* enable)
{
if (num_targets)
{
for (u8 idx = 0; idx < num_targets; ++idx)
{
u8 &id = targets[idx];
if (pstate.att_state[id].blendEnable != enable[idx])
{
pstate.att_state[id].blendEnable = enable[idx];
pstate.dirty = true;
}
}
}
}
void program::set_blend_state(int num_targets, u8 *targets, VkBool32 enable)
{
for (u8 idx = 0; idx < num_targets; ++idx)
{
u8 &id = targets[idx];
if (pstate.att_state[id].blendEnable != enable)
{
pstate.att_state[id].blendEnable = enable;
pstate.dirty = true;
}
}
}
void program::set_blend_func(int num_targets, u8* targets, VkBlendFactor* src_color, VkBlendFactor* dst_color, VkBlendFactor* src_alpha, VkBlendFactor* dst_alpha)
{
if (num_targets)
{
for (u8 idx = 0; idx < num_targets; ++idx)
{
u8 &id = targets[idx];
if (pstate.att_state[id].srcColorBlendFactor != src_color[idx])
{
pstate.att_state[id].srcColorBlendFactor = src_color[idx];
pstate.dirty = true;
}
if (pstate.att_state[id].dstColorBlendFactor != dst_color[idx])
{
pstate.att_state[id].dstColorBlendFactor = dst_color[idx];
pstate.dirty = true;
}
if (pstate.att_state[id].srcAlphaBlendFactor != src_alpha[idx])
{
pstate.att_state[id].srcAlphaBlendFactor = src_alpha[idx];
pstate.dirty = true;
}
if (pstate.att_state[id].dstAlphaBlendFactor != dst_alpha[idx])
{
pstate.att_state[id].dstAlphaBlendFactor = dst_alpha[idx];
pstate.dirty = true;
}
}
}
}
void program::set_blend_func(int num_targets, u8* targets, VkBlendFactor src_color, VkBlendFactor dst_color, VkBlendFactor src_alpha, VkBlendFactor dst_alpha)
{
if (num_targets)
{
for (u8 idx = 0; idx < num_targets; ++idx)
{
u8 &id = targets[idx];
if (pstate.att_state[id].srcColorBlendFactor != src_color)
{
pstate.att_state[id].srcColorBlendFactor = src_color;
pstate.dirty = true;
}
if (pstate.att_state[id].dstColorBlendFactor != dst_color)
{
pstate.att_state[id].dstColorBlendFactor = dst_color;
pstate.dirty = true;
}
if (pstate.att_state[id].srcAlphaBlendFactor != src_alpha)
{
pstate.att_state[id].srcAlphaBlendFactor = src_alpha;
pstate.dirty = true;
}
if (pstate.att_state[id].dstAlphaBlendFactor != dst_alpha)
{
pstate.att_state[id].dstAlphaBlendFactor = dst_alpha;
pstate.dirty = true;
}
}
}
}
void program::set_blend_op(int num_targets, u8* targets, VkBlendOp* color_ops, VkBlendOp* alpha_ops)
{
if (num_targets)
{
for (u8 idx = 0; idx < num_targets; ++idx)
{
u8 &id = targets[idx];
if (pstate.att_state[id].colorBlendOp != color_ops[idx])
{
pstate.att_state[id].colorBlendOp = color_ops[idx];
pstate.dirty = true;
}
if (pstate.att_state[id].alphaBlendOp != alpha_ops[idx])
{
pstate.att_state[id].alphaBlendOp = alpha_ops[idx];
pstate.dirty = true;
}
}
}
}
void program::set_blend_op(int num_targets, u8* targets, VkBlendOp color_op, VkBlendOp alpha_op)
{
if (num_targets)
{
for (u8 idx = 0; idx < num_targets; ++idx)
{
u8 &id = targets[idx];
if (pstate.att_state[id].colorBlendOp != color_op)
{
pstate.att_state[id].colorBlendOp = color_op;
pstate.dirty = true;
}
if (pstate.att_state[id].alphaBlendOp != alpha_op)
{
pstate.att_state[id].alphaBlendOp = alpha_op;
pstate.dirty = true;
}
}
}
}
void program::set_primitive_restart(VkBool32 state)
{
if (pstate.ia.primitiveRestartEnable != state)
{
pstate.ia.primitiveRestartEnable = state;
pstate.dirty = true;
}
}
void program::set_draw_buffer_count(u8 draw_buffers)
{
if (pstate.num_targets != draw_buffers)
{
pstate.num_targets = draw_buffers;
pstate.dirty = true;
}
}
program& program::load_uniforms(program_domain domain, std::vector<program_input>& inputs)
program& program::load_uniforms(program_domain domain, const std::vector<program_input>& inputs)
{
std::vector<program_input> store = uniforms;
uniforms.resize(0);
@ -399,46 +34,6 @@ namespace vk
return *this;
}
void program::use(vk::command_buffer& commands, VkRenderPass pass, VkPipelineLayout pipeline_layout, VkDescriptorSet descriptor_set)
{
if (/*uniforms_changed*/true)
{
uniforms_changed = false;
}
if (pstate.dirty)
{
if (pstate.pipeline_handle)
vkDestroyPipeline((*device), pstate.pipeline_handle, nullptr);
pstate.dynamic_state.pDynamicStates = pstate.dynamic_state_descriptors;
pstate.cb.pAttachments = pstate.att_state;
pstate.cb.attachmentCount = pstate.num_targets;
//Reconfigure this..
pstate.pipeline.pVertexInputState = &pstate.vi;
pstate.pipeline.pInputAssemblyState = &pstate.ia;
pstate.pipeline.pRasterizationState = &pstate.rs;
pstate.pipeline.pColorBlendState = &pstate.cb;
pstate.pipeline.pMultisampleState = &pstate.ms;
pstate.pipeline.pViewportState = &pstate.vp;
pstate.pipeline.pDepthStencilState = &pstate.ds;
pstate.pipeline.pStages = pstate.shader_stages;
pstate.pipeline.pDynamicState = &pstate.dynamic_state;
pstate.pipeline.layout = pipeline_layout;
pstate.pipeline.basePipelineIndex = -1;
pstate.pipeline.basePipelineHandle = VK_NULL_HANDLE;
pstate.pipeline.renderPass = pass;
CHECK_RESULT(vkCreateGraphicsPipelines((*device), nullptr, 1, &pstate.pipeline, NULL, &pstate.pipeline_handle));
pstate.dirty = false;
}
vkCmdBindPipeline(commands, VK_PIPELINE_BIND_POINT_GRAPHICS, pstate.pipeline_handle);
vkCmdBindDescriptorSets(commands, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout, 0, 1, &descriptor_set, 0, nullptr);
}
bool program::has_uniform(std::string uniform_name)
{
for (auto &uniform : uniforms)
@ -465,7 +60,7 @@ namespace vk
descriptor_writer.dstArrayElement = 0;
descriptor_writer.dstBinding = uniform.location + TEXTURES_FIRST_BIND_SLOT;
vkUpdateDescriptorSets((*device), 1, &descriptor_writer, 0, nullptr);
vkUpdateDescriptorSets(m_device, 1, &descriptor_writer, 0, nullptr);
return;
}
}
@ -484,7 +79,7 @@ namespace vk
descriptor_writer.dstArrayElement = 0;
descriptor_writer.dstBinding = binding_point;
vkUpdateDescriptorSets((*device), 1, &descriptor_writer, 0, nullptr);
vkUpdateDescriptorSets(m_device, 1, &descriptor_writer, 0, nullptr);
}
void program::bind_uniform(const VkBufferView &buffer_view, const std::string &binding_name, VkDescriptorSet &descriptor_set)
@ -502,28 +97,11 @@ namespace vk
descriptor_writer.dstArrayElement = 0;
descriptor_writer.dstBinding = uniform.location + VERTEX_BUFFERS_FIRST_BIND_SLOT;
vkUpdateDescriptorSets((*device), 1, &descriptor_writer, 0, nullptr);
vkUpdateDescriptorSets(m_device, 1, &descriptor_writer, 0, nullptr);
return;
}
}
throw EXCEPTION("vertex buffer not found");
}
void program::destroy()
{
if (device)
{
uniforms.resize(0);
if (pstate.pipeline_handle)
vkDestroyPipeline((*device), pstate.pipeline_handle, nullptr);
if (pstate.pipeline_cache)
vkDestroyPipelineCache((*device), pstate.pipeline_cache, nullptr);
}
memset(&pstate, 0, sizeof pstate);
device = nullptr;
}
}
}
}