Skip to content

Commit bd24a57

Browse files
committed
Fix: Make all examples work on Windows
1 parent ca43995 commit bd24a57

8 files changed

Lines changed: 48 additions & 32 deletions

File tree

cmake/CmakeMacros.cmake

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010
endmacro()
1111

1212
macro(COPY_SHADERS_TO_BIN targetName shaderFiles)
13-
if(UNIX)
14-
set(SHADER_OUTPUT_DIR "${CMAKE_SOURCE_DIR}/bin")
15-
else()
16-
set(SHADER_OUTPUT_DIR "$<TARGET_FILE_DIR:${targetName}>")
17-
endif()
13+
14+
set(SHADER_OUTPUT_DIR "${CMAKE_SOURCE_DIR}/bin")
1815

1916
# Unique name per target to avoid clashes between lib and examples
2017
set(COPY_TARGET_NAME "CopyShaders_${targetName}")
@@ -38,7 +35,7 @@ macro(SETUP_TARGET_DEFAULTS targetName)
3835
set_property(TARGET ${targetName} PROPERTY CXX_STANDARD_REQUIRED ON)
3936

4037
if(MSVC)
41-
set_property(TARGET ${targetName} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
38+
set_property(TARGET ${targetName} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")
4239
endif()
4340
endmacro()
4441

@@ -104,9 +101,7 @@ macro(CREATE_EXAMPLE name)
104101
SETUP_GROUPS("${SRC_FILES}")
105102
SOURCE_GROUP(shaders FILES "${SHADER_FILES}")
106103

107-
if(UNIX)
108-
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")
109-
endif()
104+
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")
110105

111106
SETUP_TARGET_DEFAULTS(${PROJECT_NAME})
112107
COPY_SHADERS_TO_BIN(${PROJECT_NAME} "${SHADER_FILES}")

examples/common.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,22 @@ struct Camera final
4848
, Far(cameraDescription.far)
4949
{}
5050

51-
glm::mat4 GetViewMatrix() const
51+
[[nodiscard]] glm::mat4 GetViewMatrix() const
5252
{
5353
return glm::lookAt(Position, Position + GetForward(), Up);
5454
}
5555

56-
glm::mat4 GetProjectionMatrix(float aspectRatio) const
56+
[[nodiscard]] glm::mat4 GetProjectionMatrix(float aspectRatio) const
5757
{
5858
return glm::perspective(glm::radians(Fov), aspectRatio, Near, Far);
5959
}
6060

61-
glm::mat4 GetViewProjectionMatrix(float aspectRatio) const
61+
[[nodiscard]] glm::mat4 GetViewProjectionMatrix(float aspectRatio) const
6262
{
6363
return GetProjectionMatrix(aspectRatio) * GetViewMatrix();
6464
}
6565

66-
glm::vec3 GetPosition() const
66+
[[nodiscard]] glm::vec3 GetPosition() const
6767
{
6868
return Position;
6969
}
@@ -146,7 +146,7 @@ class ExampleApp final
146146

147147
DELETE_COPY_MOVE(ExampleApp)
148148

149-
EOS::Holder<EOS::TextureHandle> CreateDepthTexture() const
149+
[[nodiscard]] EOS::Holder<EOS::TextureHandle> CreateDepthTexture() const
150150
{
151151
return Context->CreateTexture(
152152
{
@@ -174,7 +174,7 @@ class ExampleApp final
174174
while (!Window.ShouldClose())
175175
{
176176
Window.Poll();
177-
if (!Window.IsFocused()) std::this_thread::sleep_for(std::chrono::milliseconds(20));
177+
if (!Window.IsFocused()) continue;
178178

179179
//Update time
180180
const float currentTime = glfwGetTime();

src/vulkan/vkTools.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,12 +1563,12 @@ namespace VkSynchronization
15631563
return semaphore;
15641564
}
15651565

1566-
VkFence CreateFence(const VkDevice &device, const char *debugName)
1566+
VkFence CreateFence(const VkDevice &device, const char *debugName, bool createSignaled)
15671567
{
1568-
constexpr VkFenceCreateInfo createInfo =
1568+
const VkFenceCreateInfo createInfo =
15691569
{
15701570
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1571-
.flags = 0,
1571+
.flags = createSignaled ? VK_FENCE_CREATE_SIGNALED_BIT : 0u,
15721572
};
15731573

15741574
VkFence fence = VK_NULL_HANDLE;

src/vulkan/vkTools.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ namespace VkSynchronization
247247
{
248248
[[nodiscard]] VkSemaphore CreateSemaphore(const VkDevice& device, const char* debugName);
249249
[[nodiscard]] VkSemaphore CreateSemaphoreTimeline(const VkDevice& device, uint64_t initialValue, const char* debugName);
250-
[[nodiscard]] VkFence CreateFence(const VkDevice& device, const char* debugName);
250+
[[nodiscard]] VkFence CreateFence(const VkDevice& device, const char* debugName, bool createSignaled = false);
251251
VkPipelineStageFlags2 ConvertToVkPipelineStage2(const EOS::ResourceState& state);
252252
VkAccessFlags2 ConvertToVkAccessFlags2(const EOS::ResourceState& state);
253253
VkImageLayout ConvertToVkImageLayout(const EOS::ResourceState& state);

src/vulkan/vulkanClasses.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,9 @@ VulkanSwapChain::VulkanSwapChain(const VulkanSwapChainCreationDescription& vulka
850850
AcquireSemaphores.clear();
851851
AcquireSemaphores.reserve(NumberOfSwapChainImages);
852852

853+
AcquireFences.reserve(NumberOfSwapChainImages);
854+
AcquireFences.reserve(NumberOfSwapChainImages);
855+
853856
Textures.clear();
854857
Textures.reserve(NumberOfSwapChainImages);
855858

@@ -868,8 +871,9 @@ VulkanSwapChain::VulkanSwapChain(const VulkanSwapChainCreationDescription& vulka
868871
// create images, image views and framebuffers
869872
for (uint32_t i{}; i < NumberOfSwapChainImages; ++i)
870873
{
871-
//Create our Acquire Semaphore for this swapChain image
872-
AcquireSemaphores.emplace_back(VkSynchronization::CreateSemaphore(vulkanSwapChainDescription.vulkanContext->VulkanDevice, "SwapChain Acquire Semaphore: " + i));
874+
//Create our Acquire Semaphore and fences for this swapChain image
875+
AcquireSemaphores.emplace_back(VkSynchronization::CreateSemaphore(vulkanSwapChainDescription.vulkanContext->VulkanDevice, fmt::format("SwapChain Acquire Semaphore: {}", i).c_str()));
876+
AcquireFences.emplace_back(VkSynchronization::CreateFence(vulkanSwapChainDescription.vulkanContext->VulkanDevice, fmt::format("SwapChain Acquire Fence: {}", i).c_str(), true));
873877

874878
//Create a image
875879
swapChainImageDescription.Image = swapChainImages[i];
@@ -899,6 +903,11 @@ VulkanSwapChain::~VulkanSwapChain()
899903
{
900904
vkDestroySemaphore(VkContext->VulkanDevice, semaphore, nullptr);
901905
}
906+
907+
for (const VkFence& fence : AcquireFences)
908+
{
909+
vkDestroyFence(VkContext->VulkanDevice, fence, nullptr);
910+
}
902911
}
903912

904913
void VulkanSwapChain::Present(VkSemaphore waitSemaphore)
@@ -961,12 +970,16 @@ void VulkanSwapChain::GetAndWaitOnNextImage()
961970
.pSemaphores = &VkContext->TimelineSemaphore,
962971
.pValues = &TimelineWaitValues[CurrentImageIndex],
963972
};
964-
965-
// when timeout is set to UINT64_MAX, we wait until the next image has been acquired
966973
VK_ASSERT(vkWaitSemaphores(VkContext->VulkanDevice, &waitInfo, UINT64_MAX));
967974

975+
VK_ASSERT(vkWaitForFences(VkContext->VulkanDevice, 1, &AcquireFences[CurrentImageIndex], VK_TRUE, UINT64_MAX));
976+
VK_ASSERT(vkResetFences(VkContext->VulkanDevice, 1, &AcquireFences[CurrentImageIndex]));
977+
VkFence acquireFence = AcquireFences[CurrentImageIndex];
978+
968979
VkSemaphore& acquireSemaphore = AcquireSemaphores[CurrentImageIndex];
969-
const VkResult result = vkAcquireNextImageKHR(VkContext->VulkanDevice, SwapChain, UINT64_MAX, acquireSemaphore, VK_NULL_HANDLE, &CurrentImageIndex);
980+
981+
// when timeout is set to UINT64_MAX, we wait until the next image has been acquired
982+
const VkResult result = vkAcquireNextImageKHR(VkContext->VulkanDevice, SwapChain, UINT64_MAX, acquireSemaphore, acquireFence, &CurrentImageIndex);
970983
CHECK(result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR || result == VK_ERROR_OUT_OF_DATE_KHR, "vkAcquireNextImageKHR Failed");
971984

972985
GetNextImage = false;
@@ -1082,6 +1095,7 @@ CommandPool::~CommandPool()
10821095
void CommandPool::WaitSemaphore(const VkSemaphore& semaphore)
10831096
{
10841097
CHECK(WaitOnSemaphore.semaphore == VK_NULL_HANDLE, "The wait Semaphore is not Empty");
1098+
CHECK(semaphore != VK_NULL_HANDLE, "The wait Semaphore is Empty");
10851099
WaitOnSemaphore.semaphore = semaphore;
10861100
}
10871101

src/vulkan/vulkanClasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ struct VulkanSwapChain final
203203
bool GetNextImage{true};
204204

205205
std::vector<VkSemaphore> AcquireSemaphores{};
206+
std::vector<VkFence> AcquireFences{};
206207
std::vector<EOS::TextureHandle> Textures{};
207208
std::vector<uint64_t> TimelineWaitValues{};
208209

src/window.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ namespace EOS
88
/// Setup the error callback
99
glfwSetErrorCallback([](int error, const char* message)
1010
{
11-
Logger->error("GLFW: {}, {}", error, message);
11+
//Logger->error("GLFW: {}, {}", error, message);
12+
printf(fmt::format("GLFW: {}, {}", error, message).c_str());
1213
});
1314

1415
// Initialize GLFW
@@ -20,11 +21,12 @@ namespace EOS
2021
}
2122

2223
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
24+
2325
#if defined(EOS_PLATFORM_WAYLAND)
2426
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
2527
#elif defined(EOS_PLATFORM_X11)
2628
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
27-
#elif defined(EOS_PLATFORM_WIN32)
29+
#elif defined(EOS_PLATFORM_WINDOWS)
2830
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WIN32);
2931
#endif
3032

@@ -70,6 +72,10 @@ namespace EOS
7072
}
7173

7274
// Get the actual window size and store it
75+
#if defined(EOS_PLATFORM_WINDOWS)
76+
glfwGetFramebufferSize(GlfwWindow, &Width, &Height);
77+
#endif
78+
7379
contextDescription.Width = Width;
7480
contextDescription.Height = Height;
7581

@@ -90,7 +96,7 @@ namespace EOS
9096
#elif defined(EOS_PLATFORM_X11)
9197
contextDescription.Window = reinterpret_cast<void*>(glfwGetX11Window(GlfwWindow));
9298
contextDescription.Display = static_cast<void*>(glfwGetX11Display());
93-
#elif defined(EOS_PLATFORM_WIN32)
99+
#elif defined(EOS_PLATFORM_WINDOWS)
94100
contextDescription.Window = static_cast<void*>(glfwGetWin32Window(GlfwWindow));
95101
contextDescription.Display = nullptr; // Not used on Windows
96102
#endif
@@ -105,17 +111,17 @@ namespace EOS
105111
void Window::Poll()
106112
{
107113
glfwPollEvents();
114+
glfwGetFramebufferSize(GlfwWindow, &Width, &Height);
108115
}
109116

110117
bool Window::ShouldClose() const
111118
{
112119
return glfwWindowShouldClose(GlfwWindow);
113120
}
114121

115-
bool Window::IsFocused()
122+
bool Window::IsFocused() const
116123
{
117-
glfwGetFramebufferSize(GlfwWindow, &Width, &Height);
118-
return Width || Height;
124+
return glfwGetWindowAttrib(GlfwWindow, GLFW_FOCUSED);
119125
}
120126
}
121127

src/window.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ namespace EOS
3232
~Window();
3333
DELETE_COPY_MOVE(Window)
3434

35-
static void Poll();
35+
void Poll();
3636
[[nodiscard] ]bool ShouldClose() const;
37-
[[nodiscard] ]bool IsFocused();
37+
[[nodiscard] ]bool IsFocused() const;
3838

3939
GLFWwindow* GlfwWindow;
4040
int Width;

0 commit comments

Comments
 (0)