Merge pull request #1608 from vlj/vulkan

Vulkan
This commit is contained in:
vlj 2016-03-21 23:46:20 +01:00
commit efd52a0547
7 changed files with 389 additions and 701 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

@ -467,6 +467,7 @@ VKGSRender::~VKGSRender()
null_buffer.release();
null_buffer_view.release();
m_buffer_view_to_clean.clear();
m_framebuffer_to_clean.clear();
for (auto &render_pass : m_render_passes)
if (render_pass)
@ -502,90 +503,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 +541,8 @@ namespace
}
}
void VKGSRender::end()
{
size_t idx = vk::get_render_pass_location(
@ -633,7 +563,13 @@ void VKGSRender::end()
}
vk::texture &tex = (texture0)? (*texture0): m_texture_cache.upload_texture(m_command_buffer, textures[i], m_rtts);
m_program->bind_uniform({ tex, tex, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL }, "tex" + std::to_string(i), descriptor_sets);
vk::sampler sampler(*m_device,
vk::vk_wrap_mode(textures[i].wrap_s()), vk::vk_wrap_mode(textures[i].wrap_t()), vk::vk_wrap_mode(textures[i].wrap_r()),
!!(textures[i].format() & CELL_GCM_TEXTURE_UN),
textures[i].bias(), vk::max_aniso(textures[i].max_aniso()), textures[i].min_lod(), textures[i].max_lod(),
VK_FILTER_LINEAR, VK_FILTER_LINEAR, VK_SAMPLER_MIPMAP_MODE_NEAREST
);
m_program->bind_uniform({ sampler.value, tex, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL }, "tex" + std::to_string(i), descriptor_sets);
texture0 = &tex;
}
}
@ -641,7 +577,7 @@ void VKGSRender::end()
VkRenderPassBeginInfo rp_begin = {};
rp_begin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
rp_begin.renderPass = current_render_pass;
rp_begin.framebuffer = m_framebuffer;
rp_begin.framebuffer = m_framebuffer_to_clean.back()->value;
rp_begin.renderArea.offset.x = 0;
rp_begin.renderArea.offset.y = 0;
rp_begin.renderArea.extent.width = m_frame->client_size().width;
@ -651,8 +587,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 +777,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
@ -1023,30 +1067,42 @@ void VKGSRender::prepare_rtts()
//Bind created rtts as current fbo...
std::vector<u8> draw_buffers = vk::get_draw_buffers(rsx::to_surface_target(rsx::method_registers[NV4097_SET_SURFACE_COLOR_TARGET]));
m_framebuffer.destroy();
std::vector<VkImageView> fbo_images;
std::vector<std::unique_ptr<vk::image_view> > fbo_images;
for (u8 index: draw_buffers)
{
vk::texture *raw = std::get<1>(m_rtts.m_bound_render_targets[index]);
VkImageView as_image = (*raw);
fbo_images.push_back(as_image);
VkImageSubresourceRange subres = {};
subres.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
subres.baseArrayLayer = 0;
subres.baseMipLevel = 0;
subres.layerCount = 1;
subres.levelCount = 1;
fbo_images.push_back(std::make_unique<vk::image_view>(*m_device, *raw, VK_IMAGE_VIEW_TYPE_2D, raw->get_format(), vk::default_component_map(), subres));
}
m_draw_buffers_count = fbo_images.size();
if (std::get<1>(m_rtts.m_bound_depth_stencil) != nullptr)
{
vk::texture *raw = (std::get<1>(m_rtts.m_bound_depth_stencil));
VkImageView depth_image = (*raw);
fbo_images.push_back(depth_image);
VkImageSubresourceRange subres = {};
subres.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
subres.baseArrayLayer = 0;
subres.baseMipLevel = 0;
subres.layerCount = 1;
subres.levelCount = 1;
fbo_images.push_back(std::make_unique<vk::image_view>(*m_device, *raw, VK_IMAGE_VIEW_TYPE_2D, raw->get_format(), vk::default_component_map(), subres));
}
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)draw_buffers.size());
VkRenderPass current_render_pass = m_render_passes[idx];
m_framebuffer.create((*m_device), current_render_pass, fbo_images.data(), fbo_images.size(),
clip_width, clip_height);
m_draw_buffers_count = draw_buffers.size();
m_framebuffer_to_clean.push_back(std::make_unique<vk::framebuffer>(*m_device, current_render_pass, clip_width, clip_height, std::move(fbo_images)));
}
void VKGSRender::execute_command_buffer(bool wait)

View file

@ -151,11 +151,11 @@ private:
vk::descriptor_pool descriptor_pool;
std::vector<std::unique_ptr<vk::buffer_view> > m_buffer_view_to_clean;
std::vector<std::unique_ptr<vk::framebuffer> > m_framebuffer_to_clean;
u32 m_draw_calls = 0;
u8 m_draw_buffers_count = 0;
vk::framebuffer m_framebuffer;
public:
VKGSRender();

View file

@ -332,10 +332,38 @@ namespace vk
}
};
struct image_view
{
VkImageView value;
VkImageViewCreateInfo info = {};
image_view(VkDevice dev, VkImage image, VkImageViewType view_type, VkFormat format, VkComponentMapping mapping, VkImageSubresourceRange range)
: m_device(dev)
{
info.format = format;
info.image = image;
info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
info.viewType = view_type;
info.components = mapping;
info.subresourceRange = range;
CHECK_RESULT(vkCreateImageView(m_device, &info, nullptr, &value));
}
~image_view()
{
vkDestroyImageView(m_device, value, nullptr);
}
image_view(const image_view&) = delete;
image_view(image_view&&) = delete;
private:
VkDevice m_device;
};
class texture
{
VkImageView m_view = nullptr;
VkSampler m_sampler = nullptr;
VkImage m_image_contents = nullptr;
VkMemoryRequirements m_memory_layout;
VkFormat m_internal_format;
@ -355,7 +383,6 @@ namespace vk
vk::texture *staging_texture = nullptr;
bool ready = false;
void sampler_setup(rsx::texture& tex, VkImageViewType type, VkComponentMapping swizzle);
public:
texture(vk::swap_chain_image &img);
@ -381,7 +408,6 @@ namespace vk
const u16 mipmaps();
const VkFormat get_format();
operator VkSampler();
operator VkImageView();
operator VkImage();
};
@ -462,42 +488,85 @@ namespace vk
VkDevice m_device;
};
class framebuffer
struct sampler
{
VkFramebuffer m_vk_framebuffer = nullptr;
vk::render_device *owner = nullptr;
VkSampler value;
VkSamplerCreateInfo info = {};
sampler(VkDevice dev, VkSamplerAddressMode clamp_u, VkSamplerAddressMode clamp_v, VkSamplerAddressMode clamp_w,
bool unnormalized_coordinates, float mipLodBias, float max_anisotropy, float min_lod, float max_lod,
VkFilter min_filter, VkFilter mag_filter, VkSamplerMipmapMode mipmap_mode)
: m_device(dev)
{
VkSamplerCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
info.addressModeU = clamp_u;
info.addressModeV = clamp_v;
info.addressModeW = clamp_w;
info.anisotropyEnable = VK_TRUE;
info.compareEnable = VK_FALSE;
info.unnormalizedCoordinates = unnormalized_coordinates;
info.mipLodBias = mipLodBias;
info.maxAnisotropy = max_anisotropy;
info.maxLod = max_lod;
info.minLod = min_lod;
info.magFilter = mag_filter;
info.minFilter = min_filter;
info.mipmapMode = mipmap_mode;
info.compareOp = VK_COMPARE_OP_NEVER;
info.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
CHECK_RESULT(vkCreateSampler(m_device, &info, nullptr, &value));
}
~sampler()
{
vkDestroySampler(m_device, value, nullptr);
}
sampler(const sampler&) = delete;
sampler(sampler&&) = delete;
private:
VkDevice m_device;
};
struct framebuffer
{
VkFramebuffer value;
VkFramebufferCreateInfo info = {};
std::vector<std::unique_ptr<vk::image_view>> attachements;
public:
framebuffer() {}
~framebuffer() {}
void create(vk::render_device &dev, VkRenderPass pass, VkImageView *attachments, u32 nb_attachments, u32 width, u32 height)
framebuffer(VkDevice dev, VkRenderPass pass, u32 width, u32 height, std::vector<std::unique_ptr<vk::image_view>> &&atts)
: m_device(dev), attachements(std::move(atts))
{
VkFramebufferCreateInfo infos = {};
infos.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
infos.width = width;
infos.height = height;
infos.attachmentCount = nb_attachments;
infos.pAttachments = attachments;
infos.renderPass = pass;
infos.layers = 1;
std::vector<VkImageView> image_view_array(attachements.size());
size_t i = 0;
for (const auto &att : attachements)
{
image_view_array[i++] = att->value;
}
vkCreateFramebuffer(dev, &infos, nullptr, &m_vk_framebuffer);
owner = &dev;
info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
info.width = width;
info.height = height;
info.attachmentCount = image_view_array.size();
info.pAttachments = image_view_array.data();
info.renderPass = pass;
info.layers = 1;
CHECK_RESULT(vkCreateFramebuffer(dev, &info, nullptr, &value));
}
void destroy()
~framebuffer()
{
if (!owner) return;
vkDestroyFramebuffer((*owner), m_vk_framebuffer, nullptr);
owner = nullptr;
vkDestroyFramebuffer(m_device, value, nullptr);
}
operator VkFramebuffer() const
{
return m_vk_framebuffer;
}
framebuffer(const framebuffer&) = delete;
framebuffer(framebuffer&&) = delete;
private:
VkDevice m_device;
};
class swap_chain_image
@ -1159,76 +1228,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 +1249,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;
}
}
}
}

View file

@ -133,7 +133,6 @@ namespace vk
{
m_image_contents = img;
m_view = img;
m_sampler = nullptr;
//We did not create this object, do not allow internal modification!
owner = nullptr;
@ -190,32 +189,6 @@ namespace vk
m_usage = usage;
m_tiling = tiling;
if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT ||
usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)
{
VkSamplerAddressMode clamp_s = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
VkSamplerAddressMode clamp_t = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
VkSamplerAddressMode clamp_r = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
VkSamplerCreateInfo sampler_info = {};
sampler_info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
sampler_info.addressModeU = clamp_s;
sampler_info.addressModeV = clamp_t;
sampler_info.addressModeW = clamp_r;
sampler_info.anisotropyEnable = VK_FALSE;
sampler_info.compareEnable = VK_FALSE;
sampler_info.unnormalizedCoordinates = VK_FALSE;
sampler_info.mipLodBias = 0;
sampler_info.maxAnisotropy = 0;
sampler_info.magFilter = VK_FILTER_LINEAR;
sampler_info.minFilter = VK_FILTER_LINEAR;
sampler_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
sampler_info.compareOp = VK_COMPARE_OP_NEVER;
sampler_info.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
CHECK_RESULT(vkCreateSampler((*owner), &sampler_info, nullptr, &m_sampler));
}
ready = true;
}
@ -269,33 +242,6 @@ namespace vk
create(device, format, usage, tiling, width, height, mipmaps, gpu_only, swizzle);
}
void texture::sampler_setup(rsx::texture &tex, VkImageViewType type, VkComponentMapping swizzle)
{
VkSamplerAddressMode clamp_s = vk::vk_wrap_mode(tex.wrap_s());
VkSamplerAddressMode clamp_t = vk::vk_wrap_mode(tex.wrap_t());
VkSamplerAddressMode clamp_r = vk::vk_wrap_mode(tex.wrap_r());
VkSamplerCreateInfo sampler_info = {};
sampler_info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
sampler_info.addressModeU = clamp_s;
sampler_info.addressModeV = clamp_t;
sampler_info.addressModeW = clamp_r;
sampler_info.anisotropyEnable = VK_TRUE;
sampler_info.compareEnable = VK_FALSE;
sampler_info.unnormalizedCoordinates = !!(tex.format() & CELL_GCM_TEXTURE_UN);
sampler_info.mipLodBias = tex.bias();
sampler_info.maxAnisotropy = vk::max_aniso(tex.max_aniso());
sampler_info.maxLod = tex.max_lod();
sampler_info.minLod = tex.min_lod();
sampler_info.magFilter = VK_FILTER_LINEAR;
sampler_info.minFilter = VK_FILTER_LINEAR;
sampler_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
sampler_info.compareOp = VK_COMPARE_OP_NEVER;
sampler_info.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
CHECK_RESULT(vkCreateSampler((*owner), &sampler_info, nullptr, &m_sampler));
}
void texture::init(rsx::texture& tex, vk::command_buffer &cmd, bool ignore_checks)
{
VkImageViewType best_type = VK_IMAGE_VIEW_TYPE_2D;
@ -324,9 +270,6 @@ namespace vk
create(dev, format, VK_IMAGE_TYPE_3D, VK_IMAGE_VIEW_TYPE_3D, 0, usage, tiling, tex.width(), tex.height(), tex.mipmap(), false, default_component_map());
}
if (!m_sampler)
sampler_setup(tex, best_type, default_component_map());
VkImageSubresource subres = {};
subres.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
subres.mipLevel = 0;
@ -495,9 +438,6 @@ namespace vk
{
if (!owner) return;
if (m_sampler)
vkDestroySampler((*owner), m_sampler, nullptr);
//Destroy all objects managed by this object
vkDestroyImageView((*owner), m_view, nullptr);
vkDestroyImage((*owner), m_image_contents, nullptr);
@ -505,7 +445,6 @@ namespace vk
vram_allocation.destroy();
owner = nullptr;
m_sampler = nullptr;
m_view = nullptr;
m_image_contents = nullptr;
@ -532,8 +471,4 @@ namespace vk
return m_view;
}
texture::operator VkSampler()
{
return m_sampler;
}
}