imgui: fix blocking keyboard at startup (#1237)
Some checks are pending
Build and Release / reuse (push) Waiting to run
Build and Release / clang-format (push) Waiting to run
Build and Release / get-info (push) Waiting to run
Build and Release / windows-sdl (push) Blocked by required conditions
Build and Release / windows-qt (push) Blocked by required conditions
Build and Release / macos-sdl (push) Blocked by required conditions
Build and Release / macos-qt (push) Blocked by required conditions
Build and Release / linux-sdl (push) Blocked by required conditions
Build and Release / linux-qt (push) Blocked by required conditions
Build and Release / pre-release (push) Blocked by required conditions

* imgui: dont capture any input without an active nav window

fix keyboard not being available as soon as the emulator opens

* imgui: cleanup renderer assigning unnecessary sType to vulkan structures
This commit is contained in:
Vinicius Rangel 2024-10-04 13:06:08 -03:00 committed by GitHub
parent 76644a0169
commit a5968b630d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 57 deletions

View file

@ -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 {
@ -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]();
@ -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:
@ -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();
}
}
@ -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},
@ -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

View file

@ -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,
@ -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,
@ -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,
@ -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,
@ -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,
@ -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),
@ -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,
},
@ -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,
};
@ -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));
}
@ -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{
@ -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),
@ -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,
@ -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,
@ -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),
@ -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,
},
@ -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,
@ -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,
@ -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());
@ -965,7 +946,6 @@ 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,
};
@ -973,7 +953,6 @@ static void CreateShaderModules(vk::Device device, const vk::AllocationCallbacks
}
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,
};
@ -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",
@ -1033,7 +1010,6 @@ 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,
@ -1041,18 +1017,15 @@ static void CreatePipeline(vk::Device device, const vk::AllocationCallbacks* all
};
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,
@ -1060,7 +1033,6 @@ static void CreatePipeline(vk::Device device, const vk::AllocationCallbacks* all
};
vk::PipelineMultisampleStateCreateInfo ms_info{
.sType = vk::StructureType::ePipelineMultisampleStateCreateInfo,
.rasterizationSamples = vk::SampleCountFlagBits::e1,
};
@ -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,
};
@ -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,
@ -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),
@ -1162,7 +1128,6 @@ bool CreateDeviceObjects() {
},
};
vk::DescriptorSetLayoutCreateInfo info{
.sType = vk::StructureType::eDescriptorSetLayoutCreateInfo,
.bindingCount = 1,
.pBindings = binding,
};
@ -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,
@ -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,
};
@ -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,