Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imgui: fix blocking keyboard at startup #1237

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/imgui/renderer/imgui_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ static std::vector<ImGui::Layer*> layers;
// Update layers before rendering to allow layer changes to be applied during rendering.
// Using deque to keep the order of changes in case a Layer is removed then added again between
// frames.
std::deque<std::pair<bool, ImGui::Layer*>>& GetChangeLayers() {
static std::deque<std::pair<bool, ImGui::Layer*>>* change_layers =
new std::deque<std::pair<bool, ImGui::Layer*>>;
return *change_layers;
}

static std::deque<std::pair<bool, ImGui::Layer*>> change_layers{};
static std::mutex change_layers_mutex{};

static ImGuiID dock_id;

namespace ImGui {
Expand All @@ -54,7 +50,7 @@ void Initialize(const ::Vulkan::Instance& instance, const Frontend::WindowSDL& w
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.DisplaySize = ImVec2((float)window.getWidth(), (float)window.getHeight());
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 6.0f); // Makes the window edges rounded
PushStyleVar(ImGuiStyleVar_WindowRounding, 6.0f); // Makes the window edges rounded

auto path = config_path.u8string();
char* config_file_buf = new char[path.size() + 1]();
Expand Down Expand Up @@ -148,11 +144,17 @@ bool ProcessEvent(SDL_Event* event) {
// Don't block release/up events
case SDL_EVENT_MOUSE_MOTION:
case SDL_EVENT_MOUSE_WHEEL:
case SDL_EVENT_MOUSE_BUTTON_DOWN:
return GetIO().WantCaptureMouse;
case SDL_EVENT_MOUSE_BUTTON_DOWN: {
const auto& io = GetIO();
return io.WantCaptureMouse && io.Ctx->NavWindow != nullptr &&
io.Ctx->NavWindow->ID != dock_id;
}
case SDL_EVENT_TEXT_INPUT:
case SDL_EVENT_KEY_DOWN:
return GetIO().WantCaptureKeyboard;
case SDL_EVENT_KEY_DOWN: {
const auto& io = GetIO();
return io.WantCaptureKeyboard && io.Ctx->NavWindow != nullptr &&
io.Ctx->NavWindow->ID != dock_id;
}
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN:
Expand All @@ -168,15 +170,15 @@ bool ProcessEvent(SDL_Event* event) {
void NewFrame() {
{
std::scoped_lock lock{change_layers_mutex};
while (!GetChangeLayers().empty()) {
const auto [to_be_added, layer] = GetChangeLayers().front();
while (!change_layers.empty()) {
const auto [to_be_added, layer] = change_layers.front();
if (to_be_added) {
layers.push_back(layer);
} else {
const auto [begin, end] = std::ranges::remove(layers, layer);
layers.erase(begin, end);
}
GetChangeLayers().pop_front();
change_layers.pop_front();
}
}

Expand Down Expand Up @@ -211,7 +213,7 @@ void Render(const vk::CommandBuffer& cmdbuf, ::Vulkan::Frame* frame) {
.storeOp = vk::AttachmentStoreOp::eStore,
},
};
vk::RenderingInfo render_info = {};
vk::RenderingInfo render_info{};
render_info.renderArea = vk::Rect2D{
.offset = {0, 0},
.extent = {frame->width, frame->height},
Expand All @@ -231,12 +233,12 @@ void Render(const vk::CommandBuffer& cmdbuf, ::Vulkan::Frame* frame) {

void Layer::AddLayer(Layer* layer) {
std::scoped_lock lock{change_layers_mutex};
GetChangeLayers().emplace_back(true, layer);
change_layers.emplace_back(true, layer);
}

void Layer::RemoveLayer(Layer* layer) {
std::scoped_lock lock{change_layers_mutex};
GetChangeLayers().emplace_back(false, layer);
change_layers.emplace_back(false, layer);
}

} // namespace ImGui
42 changes: 2 additions & 40 deletions src/imgui/renderer/imgui_impl_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ vk::DescriptorSet AddTexture(vk::ImageView image_view, vk::ImageLayout image_lay
vk::DescriptorSet descriptor_set;
{
vk::DescriptorSetAllocateInfo alloc_info{
.sType = vk::StructureType::eDescriptorSetAllocateInfo,
.descriptorPool = bd->descriptor_pool,
.descriptorSetCount = 1,
.pSetLayouts = &bd->descriptor_set_layout,
Expand All @@ -296,7 +295,6 @@ vk::DescriptorSet AddTexture(vk::ImageView image_view, vk::ImageLayout image_lay
};
vk::WriteDescriptorSet write_desc[1]{
{
.sType = vk::StructureType::eWriteDescriptorSet,
.dstSet = descriptor_set,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
Expand Down Expand Up @@ -411,7 +409,6 @@ UploadTextureData UploadTexture(const void* data, vk::Format format, u32 width,
{
vk::ImageMemoryBarrier copy_barrier[1]{
{
.sType = vk::StructureType::eImageMemoryBarrier,
.dstAccessMask = vk::AccessFlagBits::eTransferWrite,
.oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eTransferDstOptimal,
Expand Down Expand Up @@ -444,7 +441,6 @@ UploadTextureData UploadTexture(const void* data, vk::Format format, u32 width,
vk::ImageLayout::eTransferDstOptimal, {region});

vk::ImageMemoryBarrier use_barrier[1]{{
.sType = vk::StructureType::eImageMemoryBarrier,
.srcAccessMask = vk::AccessFlagBits::eTransferWrite,
.dstAccessMask = vk::AccessFlagBits::eShaderRead,
.oldLayout = vk::ImageLayout::eTransferDstOptimal,
Expand Down Expand Up @@ -488,7 +484,6 @@ static void CreateOrResizeBuffer(RenderBuffer& rb, size_t new_size, vk::BufferUs
const vk::DeviceSize buffer_size_aligned =
AlignBufferSize(IM_MAX(v.min_allocation_size, new_size), bd->buffer_memory_alignment);
vk::BufferCreateInfo buffer_info{
.sType = vk::StructureType::eBufferCreateInfo,
.size = buffer_size_aligned,
.usage = usage,
.sharingMode = vk::SharingMode::eExclusive,
Expand All @@ -498,7 +493,6 @@ static void CreateOrResizeBuffer(RenderBuffer& rb, size_t new_size, vk::BufferUs
const vk::MemoryRequirements req = v.device.getBufferMemoryRequirements(rb.buffer);
bd->buffer_memory_alignment = IM_MAX(bd->buffer_memory_alignment, req.alignment);
vk::MemoryAllocateInfo alloc_info{
.sType = vk::StructureType::eMemoryAllocateInfo,
.allocationSize = req.size,
.memoryTypeIndex =
FindMemoryType(vk::MemoryPropertyFlagBits::eHostVisible, req.memoryTypeBits),
Expand Down Expand Up @@ -608,12 +602,10 @@ void RenderDrawData(ImDrawData& draw_data, vk::CommandBuffer command_buffer,
}
vk::MappedMemoryRange range[2]{
{
.sType = vk::StructureType::eMappedMemoryRange,
.memory = frb.vertex.buffer_memory,
.size = VK_WHOLE_SIZE,
},
{
.sType = vk::StructureType::eMappedMemoryRange,
.memory = frb.index.buffer_memory,
.size = VK_WHOLE_SIZE,
},
Expand Down Expand Up @@ -725,7 +717,6 @@ static bool CreateFontsTexture() {
// Create command buffer
if (bd->font_command_buffer == VK_NULL_HANDLE) {
vk::CommandBufferAllocateInfo info{
.sType = vk::StructureType::eCommandBufferAllocateInfo,
.commandPool = bd->command_pool,
.commandBufferCount = 1,
};
Expand All @@ -737,7 +728,6 @@ static bool CreateFontsTexture() {
{
CheckVkErr(bd->font_command_buffer.reset());
vk::CommandBufferBeginInfo begin_info{};
begin_info.sType = vk::StructureType::eCommandBufferBeginInfo;
begin_info.flags |= vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
CheckVkErr(bd->font_command_buffer.begin(&begin_info));
}
Expand All @@ -750,7 +740,6 @@ static bool CreateFontsTexture() {
// Create the Image:
{
vk::ImageCreateInfo info{
.sType = vk::StructureType::eImageCreateInfo,
.imageType = vk::ImageType::e2D,
.format = vk::Format::eR8G8B8A8Unorm,
.extent{
Expand All @@ -769,7 +758,6 @@ static bool CreateFontsTexture() {
bd->font_image = CheckVkResult(v.device.createImage(info, v.allocator));
vk::MemoryRequirements req = v.device.getImageMemoryRequirements(bd->font_image);
vk::MemoryAllocateInfo alloc_info{
.sType = vk::StructureType::eMemoryAllocateInfo,
.allocationSize = IM_MAX(v.min_allocation_size, req.size),
.memoryTypeIndex =
FindMemoryType(vk::MemoryPropertyFlagBits::eDeviceLocal, req.memoryTypeBits),
Expand All @@ -781,7 +769,6 @@ static bool CreateFontsTexture() {
// Create the Image View:
{
vk::ImageViewCreateInfo info{
.sType = vk::StructureType::eImageViewCreateInfo,
.image = bd->font_image,
.viewType = vk::ImageViewType::e2D,
.format = vk::Format::eR8G8B8A8Unorm,
Expand All @@ -802,7 +789,6 @@ static bool CreateFontsTexture() {
vk::Buffer upload_buffer{};
{
vk::BufferCreateInfo buffer_info{
.sType = vk::StructureType::eBufferCreateInfo,
.size = upload_size,
.usage = vk::BufferUsageFlagBits::eTransferSrc,
.sharingMode = vk::SharingMode::eExclusive,
Expand All @@ -811,7 +797,6 @@ static bool CreateFontsTexture() {
vk::MemoryRequirements req = v.device.getBufferMemoryRequirements(upload_buffer);
bd->buffer_memory_alignment = IM_MAX(bd->buffer_memory_alignment, req.alignment);
vk::MemoryAllocateInfo alloc_info{
.sType = vk::StructureType::eMemoryAllocateInfo,
.allocationSize = IM_MAX(v.min_allocation_size, req.size),
.memoryTypeIndex =
FindMemoryType(vk::MemoryPropertyFlagBits::eHostVisible, req.memoryTypeBits),
Expand All @@ -826,7 +811,6 @@ static bool CreateFontsTexture() {
memcpy(map, pixels, upload_size);
vk::MappedMemoryRange range[1]{
{
.sType = vk::StructureType::eMappedMemoryRange,
.memory = upload_buffer_memory,
.size = upload_size,
},
Expand All @@ -839,7 +823,6 @@ static bool CreateFontsTexture() {
{
vk::ImageMemoryBarrier copy_barrier[1]{
{
.sType = vk::StructureType::eImageMemoryBarrier,
.dstAccessMask = vk::AccessFlagBits::eTransferWrite,
.oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eTransferDstOptimal,
Expand Down Expand Up @@ -872,7 +855,6 @@ static bool CreateFontsTexture() {
vk::ImageLayout::eTransferDstOptimal, {region});

vk::ImageMemoryBarrier use_barrier[1]{{
.sType = vk::StructureType::eImageMemoryBarrier,
.srcAccessMask = vk::AccessFlagBits::eTransferWrite,
.dstAccessMask = vk::AccessFlagBits::eShaderRead,
.oldLayout = vk::ImageLayout::eTransferDstOptimal,
Expand All @@ -892,11 +874,10 @@ static bool CreateFontsTexture() {
}

// Store our identifier
io.Fonts->SetTexID((ImTextureID)bd->font_descriptor_set);
io.Fonts->SetTexID(bd->font_descriptor_set);

// End command buffer
vk::SubmitInfo end_info = {};
end_info.sType = vk::StructureType::eSubmitInfo;
end_info.commandBufferCount = 1;
end_info.pCommandBuffers = &bd->font_command_buffer;
CheckVkErr(bd->font_command_buffer.end());
Expand Down Expand Up @@ -965,15 +946,13 @@ static void CreateShaderModules(vk::Device device, const vk::AllocationCallbacks
VkData* bd = GetBackendData();
if (bd->shader_module_vert == VK_NULL_HANDLE) {
vk::ShaderModuleCreateInfo vert_info{
.sType = vk::StructureType::eShaderModuleCreateInfo,
.codeSize = sizeof(glsl_shader_vert_spv),
.pCode = (uint32_t*)glsl_shader_vert_spv,
};
bd->shader_module_vert = CheckVkResult(device.createShaderModule(vert_info, allocator));
}
if (bd->shader_module_frag == VK_NULL_HANDLE) {
vk::ShaderModuleCreateInfo frag_info{
.sType = vk::StructureType::eShaderModuleCreateInfo,
.codeSize = sizeof(glsl_shader_frag_spv),
.pCode = (uint32_t*)glsl_shader_frag_spv,
};
Expand All @@ -991,13 +970,11 @@ static void CreatePipeline(vk::Device device, const vk::AllocationCallbacks* all

vk::PipelineShaderStageCreateInfo stage[2]{
{
.sType = vk::StructureType::ePipelineShaderStageCreateInfo,
.stage = vk::ShaderStageFlagBits::eVertex,
.module = bd->shader_module_vert,
.pName = "main",
},
{
.sType = vk::StructureType::ePipelineShaderStageCreateInfo,
.stage = vk::ShaderStageFlagBits::eFragment,
.module = bd->shader_module_frag,
.pName = "main",
Expand Down Expand Up @@ -1033,34 +1010,29 @@ static void CreatePipeline(vk::Device device, const vk::AllocationCallbacks* all
};

vk::PipelineVertexInputStateCreateInfo vertex_info{
.sType = vk::StructureType::ePipelineVertexInputStateCreateInfo,
.vertexBindingDescriptionCount = 1,
.pVertexBindingDescriptions = binding_desc,
.vertexAttributeDescriptionCount = 3,
.pVertexAttributeDescriptions = attribute_desc,
};

vk::PipelineInputAssemblyStateCreateInfo ia_info{
.sType = vk::StructureType::ePipelineInputAssemblyStateCreateInfo,
.topology = vk::PrimitiveTopology::eTriangleList,
};

vk::PipelineViewportStateCreateInfo viewport_info{
.sType = vk::StructureType::ePipelineViewportStateCreateInfo,
.viewportCount = 1,
.scissorCount = 1,
};

vk::PipelineRasterizationStateCreateInfo raster_info{
.sType = vk::StructureType::ePipelineRasterizationStateCreateInfo,
.polygonMode = vk::PolygonMode::eFill,
.cullMode = vk::CullModeFlagBits::eNone,
.frontFace = vk::FrontFace::eCounterClockwise,
.lineWidth = 1.0f,
};

vk::PipelineMultisampleStateCreateInfo ms_info{
.sType = vk::StructureType::ePipelineMultisampleStateCreateInfo,
.rasterizationSamples = vk::SampleCountFlagBits::e1,
};

Expand All @@ -1078,12 +1050,9 @@ static void CreatePipeline(vk::Device device, const vk::AllocationCallbacks* all
},
};

vk::PipelineDepthStencilStateCreateInfo depth_info{
.sType = vk::StructureType::ePipelineDepthStencilStateCreateInfo,
};
vk::PipelineDepthStencilStateCreateInfo depth_info{};

vk::PipelineColorBlendStateCreateInfo blend_info{
.sType = vk::StructureType::ePipelineColorBlendStateCreateInfo,
.attachmentCount = 1,
.pAttachments = color_attachment,
};
Expand All @@ -1093,13 +1062,11 @@ static void CreatePipeline(vk::Device device, const vk::AllocationCallbacks* all
vk::DynamicState::eScissor,
};
vk::PipelineDynamicStateCreateInfo dynamic_state{
.sType = vk::StructureType::ePipelineDynamicStateCreateInfo,
.dynamicStateCount = (uint32_t)IM_ARRAYSIZE(dynamic_states),
.pDynamicStates = dynamic_states,
};

vk::GraphicsPipelineCreateInfo info{
.sType = vk::StructureType::eGraphicsPipelineCreateInfo,
.pNext = &v.pipeline_rendering_create_info,
.flags = bd->pipeline_create_flags,
.stageCount = 2,
Expand Down Expand Up @@ -1143,7 +1110,6 @@ bool CreateDeviceObjects() {
};

vk::DescriptorPoolCreateInfo pool_info{
.sType = vk::StructureType::eDescriptorPoolCreateInfo,
.flags = vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet,
.maxSets = 1000,
.poolSizeCount = std::size(pool_sizes),
Expand All @@ -1162,7 +1128,6 @@ bool CreateDeviceObjects() {
},
};
vk::DescriptorSetLayoutCreateInfo info{
.sType = vk::StructureType::eDescriptorSetLayoutCreateInfo,
.bindingCount = 1,
.pBindings = binding,
};
Expand All @@ -1182,7 +1147,6 @@ bool CreateDeviceObjects() {
};
vk::DescriptorSetLayout set_layout[1] = {bd->descriptor_set_layout};
vk::PipelineLayoutCreateInfo layout_info{
.sType = vk::StructureType::ePipelineLayoutCreateInfo,
.setLayoutCount = 1,
.pSetLayouts = set_layout,
.pushConstantRangeCount = 1,
Expand All @@ -1196,7 +1160,6 @@ bool CreateDeviceObjects() {

if (bd->command_pool == VK_NULL_HANDLE) {
vk::CommandPoolCreateInfo info{
.sType = vk::StructureType::eCommandPoolCreateInfo,
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer,
.queueFamilyIndex = v.queue_family,
};
Expand All @@ -1209,7 +1172,6 @@ bool CreateDeviceObjects() {
// ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow
// point/nearest sampling.
vk::SamplerCreateInfo info{
.sType = vk::StructureType::eSamplerCreateInfo,
.magFilter = vk::Filter::eLinear,
.minFilter = vk::Filter::eLinear,
.mipmapMode = vk::SamplerMipmapMode::eLinear,
Expand Down
Loading