vulkan: Tidy a bit.

This commit is contained in:
BearOso 2024-09-03 16:32:20 -05:00
parent c8895c8cdb
commit fb89cbf4c4
4 changed files with 33 additions and 36 deletions

View File

@ -255,11 +255,11 @@ bool Context::init_device(int preferred_device)
{ {
for (auto &ext : present_wait_extensions) for (auto &ext : present_wait_extensions)
required_extensions.push_back(ext); required_extensions.push_back(ext);
have_VK_KHR_present_wait = true; supports_VK_KHR_present_wait = true;
} }
else else
{ {
have_VK_KHR_present_wait = false; supports_VK_KHR_present_wait = false;
} }
auto extension_properties = physical_device.enumerateDeviceExtensionProperties().value; auto extension_properties = physical_device.enumerateDeviceExtensionProperties().value;
@ -275,7 +275,7 @@ bool Context::init_device(int preferred_device)
vk::PhysicalDevicePresentWaitFeaturesKHR physical_device_present_wait_feature(true); vk::PhysicalDevicePresentWaitFeaturesKHR physical_device_present_wait_feature(true);
vk::PhysicalDevicePresentIdFeaturesKHR physical_device_present_id_feature(true); vk::PhysicalDevicePresentIdFeaturesKHR physical_device_present_id_feature(true);
if (have_VK_KHR_present_wait) if (supports_VK_KHR_present_wait)
{ {
dci.setPNext(&physical_device_present_wait_feature); dci.setPNext(&physical_device_present_wait_feature);
physical_device_present_wait_feature.setPNext(&physical_device_present_id_feature); physical_device_present_wait_feature.setPNext(&physical_device_present_id_feature);

View File

@ -51,7 +51,7 @@ class Context
vk::PhysicalDeviceProperties physical_device_props; vk::PhysicalDeviceProperties physical_device_props;
vk::UniqueSurfaceKHR surface; vk::UniqueSurfaceKHR surface;
bool have_VK_KHR_present_wait = false; bool supports_VK_KHR_present_wait = false;
private: private:
bool init_vma(); bool init_vma();

View File

@ -24,6 +24,7 @@ ShaderChain::~ShaderChain()
{ {
if (context && context->device) if (context && context->device)
{ {
context->wait_idle();
if (vertex_buffer) if (vertex_buffer)
context->allocator.destroyBuffer(vertex_buffer, vertex_buffer_allocation); context->allocator.destroyBuffer(vertex_buffer, vertex_buffer_allocation);
vertex_buffer = nullptr; vertex_buffer = nullptr;
@ -50,16 +51,13 @@ void ShaderChain::construct_buffer_objects()
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f }; 0.0f, 0.0f, 0.0f, 1.0f };
std::string block;
switch (uniform.block) switch (uniform.block)
{ {
case SlangShader::Uniform::UBO: case SlangShader::Uniform::UBO:
location = &ubo_memory[uniform.offset]; location = &ubo_memory[uniform.offset];
block = "uniform";
break; break;
case SlangShader::Uniform::PushConstant: case SlangShader::Uniform::PushConstant:
location = &pipeline.push_constants[uniform.offset]; location = &pipeline.push_constants[uniform.offset];
block = "push constant";
break; break;
} }
@ -416,6 +414,7 @@ bool ShaderChain::do_frame_without_swap(uint8_t *data, int width, int height, in
{ {
auto &pipe = *pipelines[i]; auto &pipe = *pipelines[i];
auto &frame = pipe.frame[current_frame_index]; auto &frame = pipe.frame[current_frame_index];
bool is_last_pass = (i == pipelines.size() - 1);
update_descriptor_set(cmd, i, current_frame_index); update_descriptor_set(cmd, i, current_frame_index);
@ -428,7 +427,7 @@ bool ShaderChain::do_frame_without_swap(uint8_t *data, int width, int height, in
.setRenderArea(vk::Rect2D({}, vk::Extent2D(frame.image.image_width, frame.image.image_height))) .setRenderArea(vk::Rect2D({}, vk::Extent2D(frame.image.image_width, frame.image.image_height)))
.setClearValues(value); .setClearValues(value);
if (i == pipelines.size() - 1) if (is_last_pass)
context->swapchain->begin_render_pass(); context->swapchain->begin_render_pass();
else else
cmd.beginRenderPass(render_pass_begin_info, vk::SubpassContents::eInline); cmd.beginRenderPass(render_pass_begin_info, vk::SubpassContents::eInline);
@ -439,32 +438,29 @@ bool ShaderChain::do_frame_without_swap(uint8_t *data, int width, int height, in
if (pipe.push_constants.size() > 0) if (pipe.push_constants.size() > 0)
cmd.pushConstants(pipe.pipeline_layout.get(), vk::ShaderStageFlagBits::eAllGraphics, 0, pipe.push_constants.size(), pipe.push_constants.data()); cmd.pushConstants(pipe.pipeline_layout.get(), vk::ShaderStageFlagBits::eAllGraphics, 0, pipe.push_constants.size(), pipe.push_constants.data());
if (i < pipelines.size() - 1) if (is_last_pass)
{
cmd.setViewport(0, vk::Viewport(0, 0, pipe.destination_width, pipe.destination_height, 0.0f, 1.0f));
cmd.setScissor(0, vk::Rect2D({}, vk::Extent2D(pipe.destination_width, pipe.destination_height)));
}
else
{ {
cmd.setViewport(0, vk::Viewport(viewport_x, viewport_y, viewport_width, viewport_height, 0.0f, 1.0f)); cmd.setViewport(0, vk::Viewport(viewport_x, viewport_y, viewport_width, viewport_height, 0.0f, 1.0f));
cmd.setScissor(0, vk::Rect2D(vk::Offset2D(viewport_x, viewport_y), vk::Extent2D(viewport_width, viewport_height))); cmd.setScissor(0, vk::Rect2D(vk::Offset2D(viewport_x, viewport_y), vk::Extent2D(viewport_width, viewport_height)));
} }
cmd.draw(3, 1, 0, 0);
if (i < pipelines.size() - 1)
{
cmd.endRenderPass();
}
else else
{ {
context->swapchain->end_render_pass(); cmd.setViewport(0, vk::Viewport(0, 0, pipe.destination_width, pipe.destination_height, 0.0f, 1.0f));
cmd.setScissor(0, vk::Rect2D({}, vk::Extent2D(pipe.destination_width, pipe.destination_height)));
} }
cmd.draw(3, 1, 0, 0);
if (is_last_pass)
context->swapchain->end_render_pass();
else
cmd.endRenderPass();
frame.image.barrier(cmd); frame.image.barrier(cmd);
if (i < pipelines.size() - 1) if (!is_last_pass)
frame.image.generate_mipmaps(cmd); frame.image.generate_mipmaps(cmd);
if (preset->last_pass_uses_feedback && i == pipelines.size() - 1) if (preset->last_pass_uses_feedback && is_last_pass)
{ {
std::array<vk::ImageMemoryBarrier, 2> image_memory_barrier{}; std::array<vk::ImageMemoryBarrier, 2> image_memory_barrier{};
image_memory_barrier[0] image_memory_barrier[0]
@ -525,33 +521,34 @@ bool ShaderChain::do_frame_without_swap(uint8_t *data, int width, int height, in
void ShaderChain::upload_original(vk::CommandBuffer cmd, uint8_t *data, int width, int height, int stride, vk::Format format) void ShaderChain::upload_original(vk::CommandBuffer cmd, uint8_t *data, int width, int height, int stride, vk::Format format)
{ {
std::unique_ptr<Texture> texture; std::unique_ptr<Texture> texture;
bool create_texture = false;
auto create_texture = [&]() {
texture->create(width,
height,
format,
wrap_mode_from_string(pipelines[0]->shader->wrap_mode),
pipelines[0]->shader->filter_linear,
pipelines[0]->shader->mipmap_input);
};
if (original.size() > original_history_size) if (original.size() > original_history_size)
{ {
texture = std::move(original.back()); texture = std::move(original.back());
original.pop_back(); original.pop_back();
if (texture->image_width != width || texture->image_height != height || texture->format != format) if (texture->image_width != width || texture->image_height != height || texture->format != format)
{ {
texture->destroy(); texture->destroy();
create_texture(); create_texture = true;
} }
} }
else else
{ {
texture = std::make_unique<Texture>(); texture = std::make_unique<Texture>();
texture->init(context); texture->init(context);
create_texture(); create_texture = true;
} }
if (create_texture)
texture->create(width,
height,
format,
wrap_mode_from_string(pipelines[0]->shader->wrap_mode),
pipelines[0]->shader->filter_linear,
pipelines[0]->shader->mipmap_input);
if (cmd) if (cmd)
texture->from_buffer(cmd, data, width, height, stride); texture->from_buffer(cmd, data, width, height, stride);
else else

View File

@ -362,7 +362,7 @@ bool Swapchain::swap()
present_mode_info.setPNext(&present_fence_info); present_mode_info.setPNext(&present_fence_info);
vk::PresentIdKHR present_id; vk::PresentIdKHR present_id;
if (context.have_VK_KHR_present_wait) if (context.supports_VK_KHR_present_wait)
{ {
presentation_id++; presentation_id++;
present_id.setPresentIds(presentation_id); present_id.setPresentIds(presentation_id);
@ -436,7 +436,7 @@ void Swapchain::wait_on_frames()
for (auto i = 0; i < image_data.size(); i++) for (auto i = 0; i < image_data.size(); i++)
wait_on_frame(i); wait_on_frame(i);
if (context.have_VK_KHR_present_wait) if (context.supports_VK_KHR_present_wait)
{ {
auto result = device.waitForPresentKHR(swapchain_object.get(), presentation_id, 16666666); auto result = device.waitForPresentKHR(swapchain_object.get(), presentation_id, 16666666);
if (result != vk::Result::eSuccess) if (result != vk::Result::eSuccess)