Compare commits

..

20 Commits

Author SHA1 Message Date
t ce8bc3a96b Implemented Maximal MSAA 2025-04-07 18:01:31 +02:00
t 2f485d11e6 Implemented Mipmaps 2025-04-07 17:31:34 +02:00
Ano-sys a4a1847f95 Now Models can be loaded 2025-04-06 00:36:45 +02:00
Ano-sys edc4931b67 Removed transitionImageLayout call in createDepthResources because renderPass takes care of this implicitly 2025-04-05 23:41:22 +02:00
Ano-sys b458f432ff Implemented Depth Buffering 2025-04-05 23:37:38 +02:00
Ano-sys 6d986f496e Image is projected onto the sqaure now 2025-04-05 15:04:30 +02:00
Ano-sys 881a43177e Example image of percy 2025-04-05 15:03:36 +02:00
Ano-sys 80012fc7e7 Implemented Sampler with Clamp to Edge (because I like this) and anisotrpic filtering to the graphics cards max 2025-04-05 14:40:32 +02:00
Ano-sys 45b362be82 Texture mapping functionallity implemented 2025-04-05 14:01:42 +02:00
Ano-sys b1c47589bc Added library requirements info at the top 2025-04-05 14:00:50 +02:00
Ano-sys 997e32f9bf Added entries to copy textures to cmake output dir 2025-04-05 14:00:19 +02:00
Ano-sys 71be91d2b1 Added set = 0 to layout for ubo 2025-04-04 18:05:35 +02:00
Ano-sys 3562c0308a Altered readme 2025-04-04 18:05:08 +02:00
Ano-sys 49d32224af added alignas(16) for right 16 byte offsets 2025-04-04 18:04:38 +02:00
Ano-sys cf88fecf08 Altered glm::rotate parameters 2025-04-04 18:03:31 +02:00
Ano-sys 8e0234c1fb Printing programpath added 2025-04-04 18:02:53 +02:00
Ano-sys e2e84eb42c The Square is rotating, oh damn vulkan 2025-04-04 16:29:13 +02:00
Ano-sys aa91619805 Static index buffering 2025-04-04 01:03:05 +02:00
Ano-sys 24a6e0086b Dynamic 2025-04-04 00:46:15 +02:00
Ano-sys 5a241c546e Added vertex buffer and staging buffers 2025-04-04 00:45:58 +02:00
10 changed files with 17141 additions and 178 deletions
+17 -9
View File
@@ -7,26 +7,37 @@ set(GLFW_USE_WAYLAND ON)
find_package(Vulkan REQUIRED) find_package(Vulkan REQUIRED)
find_package(glfw3 3.3 REQUIRED) find_package(glfw3 3.3 REQUIRED)
# Suche nach dem glslc Compiler
find_program(GLSLC glslc) find_program(GLSLC glslc)
if(NOT GLSLC) if(NOT GLSLC)
message(FATAL_ERROR "glslc compiler not found. Please install the Vulkan SDK.") message(FATAL_ERROR "glslc compiler not found. Please install the Vulkan SDK.")
endif() endif()
# Alle Shader-Dateien im Verzeichnis ./shaders sammeln # Copy textures into cmake output
file(GLOB TEXTURE_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/textures/*"
)
foreach(TEXTURE ${TEXTURE_FILES})
file(COPY ${TEXTURE_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/textures/")
endforeach()
# Copy models into cmake output
file(GLOB MODEL_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/models/*"
)
foreach(MODEL ${MODEL_FILES})
file(COPY ${MODEL_FILES} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/models/")
endforeach()
# Copy shaders into cmake output
file(GLOB SHADER_FILES file(GLOB SHADER_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/*.vert" "${CMAKE_CURRENT_SOURCE_DIR}/shaders/*.vert"
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/*.frag" "${CMAKE_CURRENT_SOURCE_DIR}/shaders/*.frag"
) )
set(SPIRV_SHADERS "")
foreach(SHADER ${SHADER_FILES}) foreach(SHADER ${SHADER_FILES})
# Dateiname extrahieren
get_filename_component(SHADER_NAME ${SHADER} NAME) get_filename_component(SHADER_NAME ${SHADER} NAME)
# Zielpfad für die kompilierten Shader im Build-Verzeichnis
set(SPIRV "${CMAKE_CURRENT_BINARY_DIR}/shaders/${SHADER_NAME}.spv") set(SPIRV "${CMAKE_CURRENT_BINARY_DIR}/shaders/${SHADER_NAME}.spv")
# Custom Command zum Kompilieren des Shaders
add_custom_command( add_custom_command(
OUTPUT ${SPIRV} OUTPUT ${SPIRV}
COMMAND ${GLSLC} ${SHADER} -o ${SPIRV} COMMAND ${GLSLC} ${SHADER} -o ${SPIRV}
@@ -37,10 +48,8 @@ foreach(SHADER ${SHADER_FILES})
list(APPEND SPIRV_SHADERS ${SPIRV}) list(APPEND SPIRV_SHADERS ${SPIRV})
endforeach() endforeach()
# Custom Target, das alle Shader kompiliert
add_custom_target(compile_shaders ALL DEPENDS ${SPIRV_SHADERS}) add_custom_target(compile_shaders ALL DEPENDS ${SPIRV_SHADERS})
# Das eigentliche Executable
add_executable(vulkan_test add_executable(vulkan_test
main.cpp main.cpp
vulkan/vulkan_app.cpp vulkan/vulkan_app.cpp
@@ -49,5 +58,4 @@ add_executable(vulkan_test
target_link_libraries(vulkan_test PRIVATE Vulkan::Vulkan glfw) target_link_libraries(vulkan_test PRIVATE Vulkan::Vulkan glfw)
# Sicherstellen, dass die Shader vor dem Bauen des Executables kompiliert werden
add_dependencies(vulkan_test compile_shaders) add_dependencies(vulkan_test compile_shaders)
+1 -1
View File
@@ -1,3 +1,3 @@
# Vulkan_TestApp # Vulkan_TestApp
Triangle Test App with Vulkan API strictly following documentation. Test App with Vulkan API strictly following documentation.
+16 -3
View File
@@ -1,12 +1,25 @@
/**
* Required external Libraries:
* - Vulkan
* - glfw
* - glm
* - stb
* - tinyobjloader
*/
#define DEBUG 1 #define DEBUG 1
#include <iostream>
#include "vulkan/vulkan_app.hpp" #include "vulkan/vulkan_app.hpp"
int main(){ int main(int argc, char **argv){
std::cout << "Program path: " << argv[0] << std::endl;
try{ try{
vapp::Vulkan app; vapp::Vulkan app;
app.run("Vulkan", 1200, 900);
app.MODEL_PATH = "models/viking_room.obj";
app.TEXTURE_PATH = "textures/viking_room.png";
app.run("Vulkan", 800, 800);
} }
catch(const std::exception& e){ catch(const std::exception& e){
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
+16053
View File
File diff suppressed because it is too large Load Diff
+5 -1
View File
@@ -1,9 +1,13 @@
#version 450 #version 450
layout(location = 0) in vec3 fragColor; layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 fragTexCoord;
layout(location = 0) out vec4 outColor; layout(location = 0) out vec4 outColor;
layout(binding = 1) uniform sampler2D texSampler;
void main(){ void main(){
outColor = vec4(fragColor, 1.0); // outColor = vec4(fragTexCoord, 0.0, 1.0);
outColor = texture(texSampler, fragTexCoord);
} }
+14 -14
View File
@@ -1,20 +1,20 @@
#version 450 #version 450
layout(set = 0, binding = 0) uniform UniformBufferObject{
mat4 model;
mat4 view;
mat4 proj;
}ubo;
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inColor;
layout(location = 2) in vec2 inTexCoord;
layout(location = 0) out vec3 fragColor; layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec2 fragTexCoord;
vec2 positions[3] = vec2[](
vec2(0.0, -0.5),
vec2(0.5, 0.5),
vec2(-0.5, 0.5)
);
vec3 colors[3] = vec3[](
vec3(0.0, 1.0, 1.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, 1.0)
);
void main(){ void main(){
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
fragColor = colors[gl_VertexIndex]; fragColor = inColor;
fragTexCoord = inTexCoord;
} }
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 940 KiB

+799 -64
View File
File diff suppressed because it is too large Load Diff
+236 -86
View File
@@ -8,125 +8,275 @@
#define GLFW_INCLUDE_VULKAN #define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/hash.hpp>
#include <iostream> #include <iostream>
#include <stdexcept>
#include <cstdlib>
#include <cstring> #include <cstring>
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include <optional> #include <optional>
#include <set> #include <set>
#include <limits>
#include <algorithm>
#include <fstream> #include <fstream>
#include <array>
#include <chrono>
#include <unordered_map>
namespace vapp{ namespace vapp{
// Change here for other presentMode
const VkPresentModeKHR WISHED_PRESENT_MODE = VK_PRESENT_MODE_MAILBOX_KHR;
// Change here for other presentMode struct Vertex{
const VkPresentModeKHR WISHED_PRESENT_MODE = VK_PRESENT_MODE_MAILBOX_KHR; glm::vec3 pos;
glm::vec3 color;
glm::vec2 texCoord;
typedef struct _QueueFamilyIndices{ static VkVertexInputBindingDescription getBindingDescription(){
std::optional<uint32_t> graphicsFamily; VkVertexInputBindingDescription bindingDescription{};
std::optional<uint32_t> presentFamily;
bool isComplete(){ bindingDescription.binding = 0;
return graphicsFamily.has_value(); bindingDescription.stride = sizeof(Vertex);
} bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
} QueueFamilyIndices;
typedef struct{ return bindingDescription;
VkSurfaceCapabilitiesKHR capabilities; }
std::vector<VkSurfaceFormatKHR> formats;
std::vector<VkPresentModeKHR> presentModes;
} SwapChainSupportDetails;
static std::vector<char> readFile(const std::string &filename); static std::array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions(){
std::array<VkVertexInputAttributeDescription, 3> attributeDescriptions{};
class Vulkan{ attributeDescriptions[0].binding = 0;
private: attributeDescriptions[0].location = 0;
#pragma region Fields attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
GLFWwindow *window; attributeDescriptions[0].offset = offsetof(Vertex, pos);
uint32_t _width, _height;
VkInstance instance; attributeDescriptions[1].binding = 0;
VkDebugUtilsMessengerEXT debugMessenger; attributeDescriptions[1].location = 1;
attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
attributeDescriptions[1].offset = offsetof(Vertex, color);
VkSurfaceKHR surface; attributeDescriptions[2].binding = 0;
attributeDescriptions[2].location = 2;
attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
attributeDescriptions[2].offset = offsetof(Vertex, texCoord);
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; return attributeDescriptions;
VkDevice device; }
VkQueue graphicsQueue; bool operator==(const Vertex &other) const{
VkQueue presentQueue; return pos == other.pos && color == other.color && texCoord == other.texCoord;
}
};
VkSwapchainKHR swapChain; /*
std::vector<VkImage> swapChainImages; const std::vector<Vertex> vertices = {
VkFormat swapChainImageFormat; {{-0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}},
VkExtent2D swapChainExtent; {{0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
{{0.5f, 0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
{{-0.5f, 0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
std::vector<VkImageView> swapChainImageViews; {{-0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}},
{{0.5f, -0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
{{0.5f, 0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
{{-0.5f, 0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}
};
VkRenderPass renderPass; // uint16_t also possible -> change in vkCmdBindIndexBuffer also
VkPipelineLayout pipelineLayout; const std::vector<uint32_t> indices = {
0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4
};
*/
// END Remove
VkPipeline graphicsPipeline; struct UniformBufferObject{
alignas(16) glm::mat4 model;
alignas(16) glm::mat4 view;
alignas(16) glm::mat4 proj;
};
VkCommandPool commandPool; struct QueueFamilyIndices{
std::vector<VkCommandBuffer> commandBuffers; std::optional<uint32_t> graphicsFamily;
std::optional<uint32_t> presentFamily;
std::vector<VkSemaphore> imageAvailableSemaphores, renderFinishedSemaphores; bool isComplete(){
std::vector<VkFence> inFlightFences; return graphicsFamily.has_value();
}
};
std::vector<VkFramebuffer> swapChainFramebuffers; struct SwapChainSupportDetails{
VkSurfaceCapabilitiesKHR capabilities;
std::vector<VkSurfaceFormatKHR> formats;
std::vector<VkPresentModeKHR> presentModes;
};
const int MAX_FRAMES_IN_FLIGHT = 2; static std::vector<char> readFile(const std::string& filename);
uint32_t currentFrame = 0; static std::array<VkVertexInputAttributeDescription, 2> getAttributeDescription();
class Vulkan{
private:
#pragma region PrivateFields
GLFWwindow *window;
uint32_t _width, _height;
VkInstance instance;
VkDebugUtilsMessengerEXT debugMessenger;
VkSurfaceKHR surface;
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
VkDevice device;
VkQueue graphicsQueue;
VkQueue presentQueue;
VkSwapchainKHR swapChain;
std::vector<VkImage> swapChainImages;
VkFormat swapChainImageFormat;
VkExtent2D swapChainExtent;
std::vector<VkImageView> swapChainImageViews;
VkRenderPass renderPass;
VkDescriptorSetLayout descriptorSetLayout;
VkPipelineLayout pipelineLayout;
VkPipeline graphicsPipeline;
VkCommandPool commandPool;
std::vector<VkCommandBuffer> commandBuffers;
std::vector<VkSemaphore> imageAvailableSemaphores, renderFinishedSemaphores;
std::vector<VkFence> inFlightFences;
std::vector<VkFramebuffer> swapChainFramebuffers;
const int MAX_FRAMES_IN_FLIGHT = 2;
uint32_t currentFrame = 0;
std::vector<Vertex> vertices;
std::vector<uint32_t> indices;
VkBuffer vertexBuffer;
VkDeviceMemory vertexBufferMemory;
VkBuffer indexBuffer;
VkDeviceMemory indexBufferMemory;
std::vector<VkBuffer> uniformBuffers;
std::vector<VkDeviceMemory> uniformBuffersMemory;
std::vector<void*> uniformBuffersMapped;
VkDescriptorPool descriptorPool;
std::vector<VkDescriptorSet> descriptorSets;
int32_t mipLevels;
VkImage textureImage;
VkDeviceMemory textureImageMemory;
VkImageView textureImageView;
VkSampler textureSampler;
VkImage depthImage;
VkDeviceMemory depthImageMemory;
VkImageView depthImageView;
VkImage colorImage;
VkDeviceMemory colorImageMemory;
VkImageView colorImageView;
#pragma endregion #pragma endregion
#pragma region PrivateFunctions
bool checkValidationLayerSupport();
std::vector<const char*> getRequiredExtensions();
VkSampleCountFlagBits getMaxUsableSampleCount();
bool checkValidationLayerSupport(); void createInstance();
std::vector<const char*> getRequiredExtensions(); void setupDebugMessenger();
void pickPhysicalDevice();
void createInstance(); void createLogicalDevice();
void setupDebugMessenger(); QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
void pickPhysicalDevice(); bool checkDeviceExtensionSupport(VkPhysicalDevice device);
void createLogicalDevice(); bool isDeviceSuitable(VkPhysicalDevice device);
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device); void createSurface();
bool checkDeviceExtensionSupport(VkPhysicalDevice device); SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device);
bool isDeviceSuitable(VkPhysicalDevice device); VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
void createSurface(); VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR> availablePresentModes);
SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device); VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats); void createSwapChain();
VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR> availablePresentModes); void createImageViews();
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities); void createDescriptorSetLayout();
void createSwapChain(); void createGraphicsPipeline();
void createImageViews(); VkShaderModule createShaderModule(const std::vector<char>& code);
void createGraphicsPipeline(); void createRenderPass();
VkShaderModule createShaderModule(const std::vector<char> &code); void createFramebuffers();
void createRenderPass(); void createCommandPool();
void createFrameBuffers(); VkFormat findSupportedFormat(const std::vector<VkFormat> &candidates, VkImageTiling tiling, VkFormatFeatureFlags features);
void createCommandPool(); VkFormat findDepthFormat();
void createCommandBuffers(); void createColorResources();
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex); void createDepthResources();
void createSyncObjects(); VkCommandBuffer beginSingleTimeCommands();
void drawFrame(); void endSingleTimeCommands(VkCommandBuffer commandBuffer);
void cleanupSwapChain(); void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout, uint32_t mipLevels);
void recreateSwapChain(); void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height);
void initWindow(const char* windowName); void createImage(uint32_t width, uint32_t height, uint32_t mipLevels, VkSampleCountFlagBits numSamples, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory &imageMemory);
void initVulkan(); void generateMipmaps(VkImage image, VkFormat imageFormat, int32_t texWidth, int32_t texHeight, uint32_t mipLevels);
void mainLoop(); void createTextureImage();
void cleanup(); VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags, uint32_t mipLevels);
void createTextureImageView();
public: void createTextureSampler();
#ifdef DEBUG uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory);
void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
void copyBufferOld(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
void loadModel();
void createVertexBuffer();
void createIndexBuffer();
void createUniformBuffers();
void createDescriptorPool();
void createDescriptorSets();
void createCommandBuffers();
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
void createSyncObjects();
void updateUniformBuffer(uint32_t currentFrame);
void drawFrame();
void cleanupSwapChain();
void recreateSwapChain();
void initWindow(const char *windowName);
void initVulkan();
void mainLoop();
void cleanup();
#pragma endregion
public:
#pragma region PublicFields
#ifdef DEBUG
const bool enableValidationLayers = true; const bool enableValidationLayers = true;
#else #else
const bool enableValidationLayers = false; const bool enableValidationLayers = false;
#endif #endif
bool framebufferResized = false; bool framebufferResized = false;
std::string MODEL_PATH;
std::string TEXTURE_PATH;
void run(const char* windowName, const uint32_t width = 800, const uint32_t height = 600); VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT;
}; #pragma endregion
#pragma region PublicFunctions
void run(const char *windowName, const uint32_t width = 800, const uint32_t height = 600);
#pragma endregion
};
} // vapp } // vapp
namespace std{
template<> struct hash<vapp::Vertex>{
size_t operator()(vapp::Vertex const &vertex) const{
return ((hash<glm::vec3>()(vertex.pos) ^
(hash<glm::vec3>()(vertex.color) << 1)) >> 1) ^
(hash<glm::vec2>()(vertex.texCoord) << 1);
}
};
}
#endif //VULKAN_APP_H #endif //VULKAN_APP_H