win32: Use simple vulkan output.

This commit is contained in:
BearOso 2023-02-10 16:03:05 -06:00
parent bb210dc1a1
commit 0bbecf042a
6 changed files with 25 additions and 262 deletions

View File

@ -23,6 +23,7 @@ bool Swapchain::set_vsync(bool new_setting)
if (new_setting == vsync)
return false;
vsync = new_setting;
return true;
}

View File

@ -64,7 +64,7 @@ bool COpenGL::Initialize(HWND hWnd)
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
16, // Select Our Color Depth
24, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
@ -84,8 +84,11 @@ bool COpenGL::Initialize(HWND hWnd)
return false;
}
if(!SetPixelFormat(hDC,pfdIndex,&pfd)) {
DeInitialize();
return false;
// SetPixelFormat can only be called once per window. Vulkan WSI will do
// this automatically and fall back if it's already been set. Since Vulkan
// has similar requirements, we don't need to set it any more anyway.
// DeInitialize();
// return false;
}
if(!(hRC=wglCreateContext(hDC))) {
DeInitialize();

View File

@ -1,5 +1,4 @@
#include "CVulkan.h"
#include "../vulkan/slang_shader.hpp"
#include "snes9x.h"
#include <win32_display.h>
#include <gfx.h>
@ -19,9 +18,6 @@ bool CVulkan::Initialize(HWND hWnd)
if (!context->init_win32(0, hWnd))
return false;
swapchain = context->swapchain.get();
device = context->device;
if (GUI.shaderEnabled && GUI.OGLshaderFileName && GUI.OGLshaderFileName[0])
{
shaderchain = std::make_unique<Vulkan::ShaderChain>(context.get());
@ -37,46 +33,7 @@ bool CVulkan::Initialize(HWND hWnd)
}
}
create_pipeline();
descriptors.clear();
for (size_t i = 0; i < swapchain->get_num_frames(); i++)
{
vk::DescriptorSetAllocateInfo dsai{};
dsai
.setDescriptorPool(context->descriptor_pool.get())
.setDescriptorSetCount(1)
.setSetLayouts(descriptor_set_layout.get());
auto descriptor = device.allocateDescriptorSetsUnique(dsai);
descriptors.push_back(std::move(descriptor[0]));
}
textures.clear();
textures.resize(swapchain->get_num_frames());
for (auto& t : textures)
{
t.init(context.get());
t.create(256, 224, vk::Format::eR5G6B5UnormPack16, vk::SamplerAddressMode::eClampToEdge, Settings.BilinearFilter, false);
}
vk::SamplerCreateInfo sci{};
sci.setAddressModeU(vk::SamplerAddressMode::eClampToEdge)
.setAddressModeV(vk::SamplerAddressMode::eClampToEdge)
.setAddressModeW(vk::SamplerAddressMode::eClampToEdge)
.setMipmapMode(vk::SamplerMipmapMode::eLinear)
.setAnisotropyEnable(false)
.setMinFilter(vk::Filter::eLinear)
.setMagFilter(vk::Filter::eLinear)
.setUnnormalizedCoordinates(false)
.setMinLod(1.0f)
.setMaxLod(1.0f)
.setMipLodBias(0.0)
.setCompareEnable(false);
linear_sampler = device.createSampler(sci);
sci.setMinFilter(vk::Filter::eNearest)
.setMagFilter(vk::Filter::eNearest);
nearest_sampler = device.createSampler(sci);
simple_output = std::make_unique<Vulkan::SimpleOutput>(context.get(), vk::Format::eR5G6B5UnormPack16);
return true;
}
@ -88,24 +45,7 @@ void CVulkan::DeInitialize()
if (context)
context->wait_idle();
shaderchain.reset();
textures.clear();
descriptors.clear();
if (linear_sampler)
{
device.destroySampler(linear_sampler);
linear_sampler = nullptr;
}
if (nearest_sampler)
{
device.destroySampler(nearest_sampler);
nearest_sampler = nullptr;
}
swapchain = nullptr;
descriptor_set_layout.reset();
pipeline_layout.reset();
pipeline.reset();
textures.clear();
descriptors.clear();
simple_output.reset();
filtered_image.clear();
context.reset();
}
@ -116,7 +56,7 @@ void CVulkan::Render(SSurface Src)
return;
if (GUI.ReduceInputLag)
device.waitIdle();
context->wait_idle();
SSurface Dst{};
@ -147,39 +87,8 @@ void CVulkan::Render(SSurface Src)
return;
}
if (!swapchain->begin_frame())
return;
auto& tex = textures[swapchain->get_current_frame()];
auto& cmd = swapchain->get_cmd();
auto extents = swapchain->get_extents();
auto& dstset = descriptors[swapchain->get_current_frame()].get();
tex.from_buffer(cmd, (uint8_t*)filtered_image.data(), Dst.Width, Dst.Height, Dst.Pitch);
swapchain->begin_render_pass();
vk::DescriptorImageInfo dii{};
dii.setImageView(tex.image_view)
.setSampler(Settings.BilinearFilter ? linear_sampler : nearest_sampler)
.setImageLayout(vk::ImageLayout::eShaderReadOnlyOptimal);
vk::WriteDescriptorSet wds{};
wds.setDescriptorCount(1)
.setDstBinding(0)
.setDstArrayElement(0)
.setDstSet(dstset)
.setDescriptorType(vk::DescriptorType::eCombinedImageSampler)
.setImageInfo(dii);
device.updateDescriptorSets(wds, {});
cmd.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline.get());
cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipeline_layout.get(), 0, dstset, {});
cmd.setViewport(0, vk::Viewport(displayRect.left, displayRect.top, displayRect.right - displayRect.left, displayRect.bottom - displayRect.top, 0.0f, 1.0f));
cmd.setScissor(0, vk::Rect2D({}, extents));
cmd.draw(3, 1, 0, 0);
swapchain->end_render_pass();
swapchain->end_frame();
simple_output->set_filter(Settings.BilinearFilter);
simple_output->do_frame(Dst.Surface, Dst.Width, Dst.Height, Dst.Pitch, displayRect.left, displayRect.top, displayRect.right - displayRect.left, displayRect.bottom - displayRect.top);
}
bool CVulkan::ChangeRenderSize(unsigned int newWidth, unsigned int newHeight)
@ -197,8 +106,6 @@ bool CVulkan::ChangeRenderSize(unsigned int newWidth, unsigned int newHeight)
current_height = newHeight;
}
context->swapchain->set_vsync(GUI.Vsync);
return true;
}
@ -226,9 +133,9 @@ bool CVulkan::ApplyDisplayChanges(void)
current_shadername = shadername;
}
if (swapchain->set_vsync(GUI.Vsync))
if (context->swapchain->set_vsync(GUI.Vsync))
{
swapchain->recreate();
context->recreate_swapchain();
}
return true;
@ -298,150 +205,4 @@ std::function<void(const char *)> CVulkan::GetShaderParametersSaveFunction()
if (shaderchain)
shaderchain->preset->save_to_file(filename);
};
}
static const char* vertex_shader = R"(
#version 450
layout(location = 0) out vec2 texcoord;
vec2 positions[3] = vec2[](vec2(-1.0, -3.0), vec2(3.0, 1.0), vec2(-1.0, 1.0));
vec2 texcoords[3] = vec2[](vec2(0.0, -1.0), vec2(2.0, 1.0), vec2(0.0, 1.0));
void main()
{
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
texcoord = texcoords[gl_VertexIndex];
}
)";
static const char* fragment_shader = R"(
#version 450
layout(location = 0) in vec2 texcoord;
layout(binding = 0) uniform sampler2D tsampler;
layout(location = 0) out vec4 fragcolor;
void main()
{
fragcolor = texture(tsampler, texcoord);
}
)";
void CVulkan::create_pipeline()
{
auto vertex_spirv = SlangShader::generate_spirv(vertex_shader, "vertex");
auto fragment_spirv = SlangShader::generate_spirv(fragment_shader, "fragment");
auto vertex_module = device.createShaderModuleUnique({ {}, vertex_spirv });
auto fragment_module = device.createShaderModuleUnique({ {}, fragment_spirv });
vk::PipelineShaderStageCreateInfo vertex_ci;
vertex_ci.setStage(vk::ShaderStageFlagBits::eVertex)
.setModule(vertex_module.get())
.setPName("main");
vk::PipelineShaderStageCreateInfo fragment_ci;
fragment_ci.setStage(vk::ShaderStageFlagBits::eFragment)
.setModule(fragment_module.get())
.setPName("main");
std::vector<vk::PipelineShaderStageCreateInfo> stages = { vertex_ci, fragment_ci };
vk::PipelineVertexInputStateCreateInfo vertex_input_info{};
vk::PipelineInputAssemblyStateCreateInfo pipeline_input_assembly_info{};
pipeline_input_assembly_info.setTopology(vk::PrimitiveTopology::eTriangleList)
.setPrimitiveRestartEnable(false);
std::vector<vk::Viewport> viewports(1);
viewports[0]
.setX(0.0f)
.setY(0.0f)
.setWidth(256)
.setHeight(256)
.setMinDepth(0.0f)
.setMaxDepth(1.0f);
std::vector<vk::Rect2D> scissors(1);
scissors[0].extent.width = 256;
scissors[0].extent.height = 256;
scissors[0].offset = vk::Offset2D(0, 0);
vk::PipelineViewportStateCreateInfo pipeline_viewport_info;
pipeline_viewport_info.setViewports(viewports)
.setScissors(scissors);
vk::PipelineRasterizationStateCreateInfo rasterizer_info;
rasterizer_info.setCullMode(vk::CullModeFlagBits::eBack)
.setFrontFace(vk::FrontFace::eClockwise)
.setLineWidth(1.0f)
.setDepthClampEnable(false)
.setRasterizerDiscardEnable(false)
.setPolygonMode(vk::PolygonMode::eFill)
.setDepthBiasEnable(false)
.setRasterizerDiscardEnable(false);
vk::PipelineMultisampleStateCreateInfo multisample_info;
multisample_info.setSampleShadingEnable(false)
.setRasterizationSamples(vk::SampleCountFlagBits::e1);
vk::PipelineDepthStencilStateCreateInfo depth_stencil_info;
depth_stencil_info.setDepthTestEnable(false);
vk::PipelineColorBlendAttachmentState blend_attachment_info;
blend_attachment_info
.setColorWriteMask(vk::ColorComponentFlagBits::eB |
vk::ColorComponentFlagBits::eG |
vk::ColorComponentFlagBits::eR |
vk::ColorComponentFlagBits::eA)
.setBlendEnable(true)
.setColorBlendOp(vk::BlendOp::eAdd)
.setSrcColorBlendFactor(vk::BlendFactor::eSrcAlpha)
.setDstColorBlendFactor(vk::BlendFactor::eOneMinusSrcAlpha)
.setAlphaBlendOp(vk::BlendOp::eAdd)
.setSrcAlphaBlendFactor(vk::BlendFactor::eOne)
.setSrcAlphaBlendFactor(vk::BlendFactor::eZero);
vk::PipelineColorBlendStateCreateInfo blend_state_info;
blend_state_info.setLogicOpEnable(false)
.setAttachments(blend_attachment_info);
std::vector<vk::DynamicState> states = { vk::DynamicState::eViewport, vk::DynamicState::eScissor };
vk::PipelineDynamicStateCreateInfo dynamic_state_info({}, states);
vk::DescriptorSetLayoutBinding dslb{};
dslb.setBinding(0)
.setStageFlags(vk::ShaderStageFlagBits::eFragment)
.setDescriptorCount(1)
.setDescriptorType(vk::DescriptorType::eCombinedImageSampler);
vk::DescriptorSetLayoutCreateInfo dslci{};
dslci.setBindings(dslb);
descriptor_set_layout = device.createDescriptorSetLayoutUnique(dslci);
vk::PipelineLayoutCreateInfo pipeline_layout_info;
pipeline_layout_info.setSetLayoutCount(0)
.setPushConstantRangeCount(0)
.setSetLayouts(descriptor_set_layout.get());
pipeline_layout = device.createPipelineLayoutUnique(pipeline_layout_info);
vk::GraphicsPipelineCreateInfo pipeline_create_info;
pipeline_create_info.setStageCount(2)
.setStages(stages)
.setPVertexInputState(&vertex_input_info)
.setPInputAssemblyState(&pipeline_input_assembly_info)
.setPViewportState(&pipeline_viewport_info)
.setPRasterizationState(&rasterizer_info)
.setPMultisampleState(&multisample_info)
.setPDepthStencilState(&depth_stencil_info)
.setPColorBlendState(&blend_state_info)
.setPDynamicState(&dynamic_state_info)
.setLayout(pipeline_layout.get())
.setRenderPass(swapchain->get_render_pass())
.setSubpass(0);
auto [result, pipeline] = device.createGraphicsPipelineUnique(nullptr, pipeline_create_info);
this->pipeline = std::move(pipeline);
}
}

View File

@ -2,29 +2,19 @@
#include "IS9xDisplayOutput.h"
#include "../vulkan/vulkan_context.hpp"
#include "../vulkan/vulkan_texture.hpp"
#include "../vulkan/vulkan_shader_chain.hpp"
#include "../vulkan/vulkan_simple_output.hpp"
#include <functional>
class CVulkan : public IS9xDisplayOutput
{
private:
void create_pipeline();
HWND hWnd;
std::unique_ptr<Vulkan::Context> context;
Vulkan::Swapchain* swapchain;
vk::Device device;
vk::UniqueDescriptorSetLayout descriptor_set_layout;
vk::UniquePipelineLayout pipeline_layout;
vk::UniquePipeline pipeline;
vk::Sampler linear_sampler;
vk::Sampler nearest_sampler;
std::vector<Vulkan::Texture> textures;
std::vector<vk::UniqueDescriptorSet> descriptors;
std::vector<uint16_t> filtered_image;
std::unique_ptr<Vulkan::ShaderChain> shaderchain;
std::string current_shadername;
std::unique_ptr<Vulkan::SimpleOutput> simple_output;
int current_width;
int current_height;

View File

@ -445,6 +445,7 @@
<ClInclude Include="..\vulkan\vulkan_context.hpp" />
<ClInclude Include="..\vulkan\vulkan_pipeline_image.hpp" />
<ClInclude Include="..\vulkan\vulkan_shader_chain.hpp" />
<ClInclude Include="..\vulkan\vulkan_simple_output.hpp" />
<ClInclude Include="..\vulkan\vulkan_slang_pipeline.hpp" />
<ClInclude Include="..\vulkan\vulkan_swapchain.hpp" />
<ClInclude Include="..\vulkan\vulkan_texture.hpp" />
@ -607,6 +608,7 @@
<ClCompile Include="..\vulkan\vulkan_hpp_storage.cpp" />
<ClCompile Include="..\vulkan\vulkan_pipeline_image.cpp" />
<ClCompile Include="..\vulkan\vulkan_shader_chain.cpp" />
<ClCompile Include="..\vulkan\vulkan_simple_output.cpp" />
<ClCompile Include="..\vulkan\vulkan_slang_pipeline.cpp" />
<ClCompile Include="..\vulkan\vulkan_swapchain.cpp" />
<ClCompile Include="..\vulkan\vulkan_texture.cpp" />

View File

@ -327,6 +327,9 @@
<ClInclude Include="..\vulkan\vulkan_texture.hpp">
<Filter>GUI\VideoDriver\Vulkan</Filter>
</ClInclude>
<ClInclude Include="..\vulkan\vulkan_simple_output.hpp">
<Filter>GUI\VideoDriver\Vulkan</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\bsx.cpp">
@ -719,6 +722,9 @@
<ClCompile Include="..\external\stb\stb_image_implementation.cpp">
<Filter>GUI\VideoDriver\Vulkan</Filter>
</ClCompile>
<ClCompile Include="..\vulkan\vulkan_simple_output.cpp">
<Filter>GUI\VideoDriver\Vulkan</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="rsrc\nodrop.cur">