1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-22 18:53:28 +01:00

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

@ -14,4 +14,5 @@ namespace vk
VkSamplerAddressMode vk_wrap_mode(u32 gcm_wrap);
float max_aniso(u32 gcm_aniso);
VkComponentMapping get_component_mapping(u32 format, u8 swizzle_mask);
}
VkPrimitiveTopology get_appropriate_topology(rsx::primitive_type& mode, bool &requires_modification);
}

View File

@ -502,90 +502,17 @@ void VKGSRender::begin()
//TODO: Fence sync, ring-buffers, etc
//CHECK_RESULT(vkDeviceWaitIdle((*m_device)));
if (!load_program())
return;
if (!recording)
begin_command_buffer_recording();
init_buffers();
m_program->set_draw_buffer_count(m_draw_buffers_count);
u32 color_mask = rsx::method_registers[NV4097_SET_COLOR_MASK];
bool color_mask_b = !!(color_mask & 0xff);
bool color_mask_g = !!((color_mask >> 8) & 0xff);
bool color_mask_r = !!((color_mask >> 16) & 0xff);
bool color_mask_a = !!((color_mask >> 24) & 0xff);
VkColorComponentFlags mask = 0;
if (color_mask_a) mask |= VK_COLOR_COMPONENT_A_BIT;
if (color_mask_b) mask |= VK_COLOR_COMPONENT_B_BIT;
if (color_mask_g) mask |= VK_COLOR_COMPONENT_G_BIT;
if (color_mask_r) mask |= VK_COLOR_COMPONENT_R_BIT;
VkColorComponentFlags color_masks[4] = { mask };
u8 render_targets[] = { 0, 1, 2, 3 };
m_program->set_color_mask(m_draw_buffers_count, render_targets, color_masks);
//TODO stencil mask
m_program->set_depth_write_mask(rsx::method_registers[NV4097_SET_DEPTH_MASK]);
if (rsx::method_registers[NV4097_SET_DEPTH_TEST_ENABLE])
{
m_program->set_depth_test_enable(VK_TRUE);
m_program->set_depth_compare_op(vk::compare_op(rsx::method_registers[NV4097_SET_DEPTH_FUNC]));
}
else
m_program->set_depth_test_enable(VK_FALSE);
if (rsx::method_registers[NV4097_SET_BLEND_ENABLE])
{
u32 sfactor = rsx::method_registers[NV4097_SET_BLEND_FUNC_SFACTOR];
u32 dfactor = rsx::method_registers[NV4097_SET_BLEND_FUNC_DFACTOR];
VkBlendFactor sfactor_rgb = vk::get_blend_factor(sfactor);
VkBlendFactor sfactor_a = vk::get_blend_factor(sfactor >> 16);
VkBlendFactor dfactor_rgb = vk::get_blend_factor(dfactor);
VkBlendFactor dfactor_a = vk::get_blend_factor(dfactor >> 16);
//TODO: Separate target blending
VkBool32 blend_state = VK_TRUE;
m_program->set_blend_state(m_draw_buffers_count, render_targets, blend_state);
m_program->set_blend_func(m_draw_buffers_count, render_targets, sfactor_rgb, dfactor_rgb, sfactor_a, dfactor_a);
u32 equation = rsx::method_registers[NV4097_SET_BLEND_EQUATION];
VkBlendOp equation_rgb = vk::get_blend_op(equation);
VkBlendOp equation_a = vk::get_blend_op(equation >> 16);
m_program->set_blend_op(m_draw_buffers_count, render_targets, equation_rgb, equation_a);
}
else
{
VkBool32 blend_state = VK_FALSE;
m_program->set_blend_state(m_draw_buffers_count, render_targets, blend_state);
}
if (rsx::method_registers[NV4097_SET_RESTART_INDEX_ENABLE])
{
if (rsx::method_registers[NV4097_SET_RESTART_INDEX] != 0xFFFF &&
rsx::method_registers[NV4097_SET_RESTART_INDEX] != 0xFFFFFFFF)
{
LOG_ERROR(RSX, "Custom primitive restart index 0x%X. Should rewrite index buffer with proper value!", rsx::method_registers[NV4097_SET_RESTART_INDEX]);
}
LOG_ERROR(RSX, "Primitive restart enabled!");
m_program->set_primitive_restart(VK_TRUE);
}
else
m_program->set_primitive_restart(VK_FALSE);
if (!load_program())
return;
u32 line_width = rsx::method_registers[NV4097_SET_LINE_WIDTH];
float actual_line_width = (line_width >> 3) + (line_width & 7) / 8.f;
vkCmdSetLineWidth(m_command_buffer, actual_line_width);
//TODO: Set up other render-state parameters into the program pipeline
@ -613,6 +540,8 @@ namespace
}
}
void VKGSRender::end()
{
size_t idx = vk::get_render_pass_location(
@ -651,8 +580,8 @@ void VKGSRender::end()
auto upload_info = upload_vertex_data();
m_program->set_primitive_topology(std::get<0>(upload_info));
m_program->use(m_command_buffer, current_render_pass, pipeline_layout, descriptor_sets);
vkCmdBindPipeline(m_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_program->pipeline);
vkCmdBindDescriptorSets(m_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout, 0, 1, &descriptor_sets, 0, nullptr);
if (!std::get<1>(upload_info))
vkCmdDraw(m_command_buffer, vertex_draw_count, 1, 0, 0);
@ -841,8 +770,116 @@ bool VKGSRender::load_program()
RSXVertexProgram vertex_program = get_current_vertex_program();
RSXFragmentProgram fragment_program = get_current_fragment_program();
vk::pipeline_props properties = {};
properties.ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
bool unused;
properties.ia.topology = vk::get_appropriate_topology(draw_mode, unused);
if (rsx::method_registers[NV4097_SET_RESTART_INDEX_ENABLE])
{
if (rsx::method_registers[NV4097_SET_RESTART_INDEX] != 0xFFFF &&
rsx::method_registers[NV4097_SET_RESTART_INDEX] != 0xFFFFFFFF)
{
LOG_ERROR(RSX, "Custom primitive restart index 0x%X. Should rewrite index buffer with proper value!", rsx::method_registers[NV4097_SET_RESTART_INDEX]);
}
properties.ia.primitiveRestartEnable = VK_TRUE;
}
else
properties.ia.primitiveRestartEnable = VK_FALSE;
for (int i = 0; i < 4; ++i)
{
properties.att_state[i].colorWriteMask = 0xf;
properties.att_state[i].blendEnable = VK_FALSE;
}
u32 color_mask = rsx::method_registers[NV4097_SET_COLOR_MASK];
bool color_mask_b = !!(color_mask & 0xff);
bool color_mask_g = !!((color_mask >> 8) & 0xff);
bool color_mask_r = !!((color_mask >> 16) & 0xff);
bool color_mask_a = !!((color_mask >> 24) & 0xff);
VkColorComponentFlags mask = 0;
if (color_mask_a) mask |= VK_COLOR_COMPONENT_A_BIT;
if (color_mask_b) mask |= VK_COLOR_COMPONENT_B_BIT;
if (color_mask_g) mask |= VK_COLOR_COMPONENT_G_BIT;
if (color_mask_r) mask |= VK_COLOR_COMPONENT_R_BIT;
VkColorComponentFlags color_masks[4] = { mask };
u8 render_targets[] = { 0, 1, 2, 3 };
for (u8 idx = 0; idx < m_draw_buffers_count; ++idx)
{
properties.att_state[render_targets[idx]].colorWriteMask = mask;
}
if (rsx::method_registers[NV4097_SET_BLEND_ENABLE])
{
u32 sfactor = rsx::method_registers[NV4097_SET_BLEND_FUNC_SFACTOR];
u32 dfactor = rsx::method_registers[NV4097_SET_BLEND_FUNC_DFACTOR];
VkBlendFactor sfactor_rgb = vk::get_blend_factor(sfactor);
VkBlendFactor sfactor_a = vk::get_blend_factor(sfactor >> 16);
VkBlendFactor dfactor_rgb = vk::get_blend_factor(dfactor);
VkBlendFactor dfactor_a = vk::get_blend_factor(dfactor >> 16);
u32 equation = rsx::method_registers[NV4097_SET_BLEND_EQUATION];
VkBlendOp equation_rgb = vk::get_blend_op(equation);
VkBlendOp equation_a = vk::get_blend_op(equation >> 16);
//TODO: Separate target blending
for (u8 idx = 0; idx < m_draw_buffers_count; ++idx)
{
properties.att_state[render_targets[idx]].blendEnable = VK_TRUE;
properties.att_state[render_targets[idx]].srcColorBlendFactor = sfactor_rgb;
properties.att_state[render_targets[idx]].dstColorBlendFactor = dfactor_rgb;
properties.att_state[render_targets[idx]].srcAlphaBlendFactor = sfactor_a;
properties.att_state[render_targets[idx]].dstAlphaBlendFactor = dfactor_a;
properties.att_state[render_targets[idx]].colorBlendOp = equation_rgb;
properties.att_state[render_targets[idx]].alphaBlendOp = equation_a;
}
}
else
{
for (u8 idx = 0; idx < m_draw_buffers_count; ++idx)
{
properties.att_state[render_targets[idx]].blendEnable = VK_FALSE;
}
}
properties.ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
properties.ds.depthWriteEnable = (!!rsx::method_registers[NV4097_SET_DEPTH_MASK]) ? VK_TRUE : VK_FALSE;
properties.ds.depthBoundsTestEnable = VK_FALSE;
properties.ds.back.failOp = VK_STENCIL_OP_KEEP;
properties.ds.back.passOp = VK_STENCIL_OP_KEEP;
properties.ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
properties.ds.stencilTestEnable = VK_FALSE;
properties.ds.front = properties.ds.back;
if (!!rsx::method_registers[NV4097_SET_DEPTH_TEST_ENABLE])
{
properties.ds.depthTestEnable = VK_TRUE;
properties.ds.depthCompareOp = vk::compare_op(rsx::method_registers[NV4097_SET_DEPTH_FUNC]);
}
else
properties.ds.depthTestEnable = VK_FALSE;
size_t idx = vk::get_render_pass_location(
vk::get_compatible_surface_format(m_surface.color_format),
vk::get_compatible_depth_surface_format(m_optimal_tiling_supported_formats, m_surface.depth_format),
(u8)vk::get_draw_buffers(rsx::to_surface_target(rsx::method_registers[NV4097_SET_SURFACE_COLOR_TARGET])).size());
properties.render_pass = m_render_passes[idx];
properties.num_targets = m_draw_buffers_count;
//Load current program from buffer
m_program = &m_prog_buffer.getGraphicPipelineState(vertex_program, fragment_program, nullptr);
m_program = m_prog_buffer.getGraphicPipelineState(vertex_program, fragment_program, properties, *m_device, pipeline_layout).get();
//TODO: Update constant buffers..
//1. Update scale-offset matrix

View File

@ -1159,76 +1159,17 @@ namespace vk
class program
{
struct pipeline_state
{
VkGraphicsPipelineCreateInfo pipeline;
VkPipelineCacheCreateInfo pipeline_cache_desc;
VkPipelineCache pipeline_cache;
VkPipelineVertexInputStateCreateInfo vi;
VkPipelineInputAssemblyStateCreateInfo ia;
VkPipelineRasterizationStateCreateInfo rs;
VkPipelineColorBlendStateCreateInfo cb;
VkPipelineDepthStencilStateCreateInfo ds;
VkPipelineViewportStateCreateInfo vp;
VkPipelineMultisampleStateCreateInfo ms;
VkDynamicState dynamic_state_descriptors[VK_DYNAMIC_STATE_RANGE_SIZE];
VkPipelineDynamicStateCreateInfo dynamic_state;
VkPipelineColorBlendAttachmentState att_state[4];
VkPipelineShaderStageCreateInfo shader_stages[2];
VkRenderPass render_pass = nullptr;
VkShaderModule vs, fs;
VkPipeline pipeline_handle = nullptr;
int num_targets = 1;
bool dirty;
bool in_use;
}
pstate;
bool uniforms_changed = true;
vk::render_device *device = nullptr;
std::vector<program_input> uniforms;
void init_pipeline();
std::vector<program_input> uniforms;
VkDevice m_device;
public:
program();
program(const program&) = delete;
program(program&& other);
program(vk::render_device &renderer);
VkPipeline pipeline;
program(VkDevice dev, VkPipeline p, const std::vector<program_input> &vertex_input, const std::vector<program_input>& fragment_inputs);
program(const program&) = delete;
program(program&& other) = delete;
~program();
program& attach_device(vk::render_device &dev);
program& attachFragmentProgram(VkShaderModule prog);
program& attachVertexProgram(VkShaderModule prog);
void make();
void destroy();
//Render state stuff...
void set_depth_compare_op(VkCompareOp op);
void set_depth_write_mask(VkBool32 write_enable);
void set_depth_test_enable(VkBool32 state);
void set_primitive_topology(VkPrimitiveTopology topology);
void set_color_mask(int num_targets, u8* targets, VkColorComponentFlags *flags);
void set_blend_state(int num_targets, u8* targets, VkBool32 *enable);
void set_blend_state(int num_targets, u8* targets, VkBool32 enable);
void set_blend_func(int num_targets, u8* targets, VkBlendFactor *src_color, VkBlendFactor *dst_color, VkBlendFactor *src_alpha, VkBlendFactor *dst_alpha);
void set_blend_func(int num_targets, u8 * targets, VkBlendFactor src_color, VkBlendFactor dst_color, VkBlendFactor src_alpha, VkBlendFactor dst_alpha);
void set_blend_op(int num_targets, u8* targets, VkBlendOp* color_ops, VkBlendOp* alpha_ops);
void set_blend_op(int num_targets, u8 * targets, VkBlendOp color_op, VkBlendOp alpha_op);
void set_primitive_restart(VkBool32 state);
void set_draw_buffer_count(u8 draw_buffers);
program& load_uniforms(program_domain domain, std::vector<program_input>& inputs);
void use(vk::command_buffer& commands, VkRenderPass pass, VkPipelineLayout pipeline_layout, VkDescriptorSet descriptor_set);
program& load_uniforms(program_domain domain, const std::vector<program_input>& inputs);
bool has_uniform(std::string uniform_name);
#define VERTEX_BUFFERS_FIRST_BIND_SLOT 3
@ -1239,9 +1180,6 @@ namespace vk
void bind_uniform(VkDescriptorImageInfo image_descriptor, std::string uniform_name, VkDescriptorSet &descriptor_set);
void bind_uniform(VkDescriptorBufferInfo buffer_descriptor, uint32_t binding_point, VkDescriptorSet &descriptor_set);
void bind_uniform(const VkBufferView &buffer_view, const std::string &binding_name, VkDescriptorSet &descriptor_set);
program& operator = (const program&) = delete;
program& operator = (program&& other);
};
}
}

View File

@ -3,12 +3,63 @@
#include "VKFragmentProgram.h"
#include "../Common/ProgramStateCache.h"
namespace vk
{
struct pipeline_props
{
VkPipelineInputAssemblyStateCreateInfo ia;
VkPipelineDepthStencilStateCreateInfo ds;
VkPipelineColorBlendAttachmentState att_state[4];
VkRenderPass render_pass;
int num_targets;
bool operator==(const pipeline_props& other) const
{
if (memcmp(&ia, &other.ia, sizeof(VkPipelineInputAssemblyStateCreateInfo)))
return false;
if (memcmp(&ds, &other.ds, sizeof(VkPipelineDepthStencilStateCreateInfo)))
return false;
if (memcmp(&att_state[0], &other.att_state[0], sizeof(VkPipelineColorBlendAttachmentState)))
return false;
return num_targets == other.num_targets;
}
};
}
namespace
{
template<typename T>
size_t hash_struct(const T& structure)
{
char *data = (char*)(&structure);
size_t result = 0;
for (unsigned i = 0; i < sizeof(T); i++)
result ^= std::hash<char>()(data[i]);
return result;
}
}
namespace std
{
template <>
struct hash<vk::pipeline_props> {
size_t operator()(const vk::pipeline_props &pipelineProperties) const {
size_t seed = hash<unsigned>()(pipelineProperties.num_targets);
seed ^= hash_struct(pipelineProperties.ia);
seed ^= hash_struct(pipelineProperties.ds);
seed ^= hash_struct(pipelineProperties.att_state[0]);
return hash<size_t>()(seed);
}
};
}
struct VKTraits
{
using vertex_program_type = VKVertexProgram;
using fragment_program_type = VKFragmentProgram;
using pipeline_storage_type = vk::glsl::program;
using pipeline_properties = void*;
using pipeline_storage_type = std::unique_ptr<vk::glsl::program>;
using pipeline_properties = vk::pipeline_props;
static
void recompile_fragment_program(const RSXFragmentProgram &RSXFP, fragment_program_type& fragmentProgramData, size_t ID)
@ -25,18 +76,78 @@ struct VKTraits
}
static
pipeline_storage_type build_pipeline(const vertex_program_type &vertexProgramData, const fragment_program_type &fragmentProgramData, const pipeline_properties &pipelineProperties)
pipeline_storage_type build_pipeline(const vertex_program_type &vertexProgramData, const fragment_program_type &fragmentProgramData, const vk::pipeline_props &pipelineProperties, VkDevice dev, VkPipelineLayout common_pipeline_layout)
{
pipeline_storage_type result(*vk::get_current_renderer());
// pstate.dynamic_state.pDynamicStates = pstate.dynamic_state_descriptors;
// pstate.cb.pAttachments = pstate.att_state;
// pstate.cb.attachmentCount = pstate.num_targets;
std::vector<vk::glsl::program_input> vertex_uniforms = vertexProgramData.uniforms;
std::vector<vk::glsl::program_input> fragment_uniforms = fragmentProgramData.uniforms;
result.attachVertexProgram(vertexProgramData.handle)
.attachFragmentProgram(fragmentProgramData.handle)
.load_uniforms(vk::glsl::glsl_vertex_program, vertex_uniforms)
.load_uniforms(vk::glsl::glsl_fragment_program, fragment_uniforms)
.make();
VkPipelineShaderStageCreateInfo shader_stages[2] = {};
shader_stages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shader_stages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
shader_stages[0].module = vertexProgramData.handle;
shader_stages[0].pName = "main";
shader_stages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shader_stages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
shader_stages[1].module = fragmentProgramData.handle;
shader_stages[1].pName = "main";
VkDynamicState dynamic_state_descriptors[VK_DYNAMIC_STATE_RANGE_SIZE] = {};
VkPipelineDynamicStateCreateInfo dynamic_state_info = {};
dynamic_state_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
dynamic_state_descriptors[dynamic_state_info.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
dynamic_state_descriptors[dynamic_state_info.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
dynamic_state_descriptors[dynamic_state_info.dynamicStateCount++] = VK_DYNAMIC_STATE_LINE_WIDTH;
dynamic_state_info.pDynamicStates = dynamic_state_descriptors;
VkPipelineVertexInputStateCreateInfo vi = { VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO };
VkPipelineViewportStateCreateInfo vp = {};
vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
vp.viewportCount = 1;
vp.scissorCount = 1;
VkPipelineMultisampleStateCreateInfo ms = {};
ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
ms.pSampleMask = NULL;
ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineColorBlendStateCreateInfo cb = {};
cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
cb.attachmentCount = 1;
cb.pAttachments = pipelineProperties.att_state;
VkPipelineRasterizationStateCreateInfo rs = {};
rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rs.polygonMode = VK_POLYGON_MODE_FILL;
rs.cullMode = VK_CULL_MODE_NONE;
rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
rs.depthClampEnable = VK_FALSE;
rs.rasterizerDiscardEnable = VK_FALSE;
rs.depthBiasEnable = VK_FALSE;
VkPipeline pipeline;
VkGraphicsPipelineCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
info.pVertexInputState = &vi;
info.pInputAssemblyState = &pipelineProperties.ia;
info.pRasterizationState = &rs;
info.pColorBlendState = &cb;
info.pMultisampleState = &ms;
info.pViewportState = &vp;
info.pDepthStencilState = &pipelineProperties.ds;
info.stageCount = 2;
info.pStages = shader_stages;
info.pDynamicState = &dynamic_state_info;
info.layout = common_pipeline_layout;
info.basePipelineIndex = -1;
info.basePipelineHandle = VK_NULL_HANDLE;
info.renderPass = pipelineProperties.render_pass;
CHECK_RESULT(vkCreateGraphicsPipelines(dev, nullptr, 1, &info, NULL, &pipeline));
pipeline_storage_type result = std::make_unique<vk::glsl::program>(dev, pipeline, vertexProgramData.uniforms, fragmentProgramData.uniforms);
return result;
}

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;
}
}
}
}