Compare commits

..

24 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
Ano-sys 5944a08abc Added resizing + swap chain recreation 2025-04-03 23:00:32 +02:00
Ano-sys 811b5e0ae6 Added full sync for buffers 2025-04-03 22:00:21 +02:00
Ano-sys 4288c6fa08 Added shader compilation to cmake 2025-04-03 21:38:28 +02:00
Ano-sys b0d5f9014f Added shader compilation to cmake 2025-04-03 21:38:03 +02:00
14 changed files with 17939 additions and 866 deletions
+47 -1
View File
@@ -7,9 +7,55 @@ set(GLFW_USE_WAYLAND ON)
find_package(Vulkan REQUIRED)
find_package(glfw3 3.3 REQUIRED)
find_program(GLSLC glslc)
if(NOT GLSLC)
message(FATAL_ERROR "glslc compiler not found. Please install the Vulkan SDK.")
endif()
# 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
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/*.vert"
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/*.frag"
)
foreach(SHADER ${SHADER_FILES})
get_filename_component(SHADER_NAME ${SHADER} NAME)
set(SPIRV "${CMAKE_CURRENT_BINARY_DIR}/shaders/${SHADER_NAME}.spv")
add_custom_command(
OUTPUT ${SPIRV}
COMMAND ${GLSLC} ${SHADER} -o ${SPIRV}
DEPENDS ${SHADER}
COMMENT "Compiling ${SHADER_NAME}"
)
list(APPEND SPIRV_SHADERS ${SPIRV})
endforeach()
add_custom_target(compile_shaders ALL DEPENDS ${SPIRV_SHADERS})
add_executable(vulkan_test
main.cpp
vulkan/vulkan_app.cpp
vulkan/vulkan_app.hpp)
vulkan/vulkan_app.hpp
)
target_link_libraries(vulkan_test PRIVATE Vulkan::Vulkan glfw)
add_dependencies(vulkan_test compile_shaders)
+1 -1
View File
@@ -1,3 +1,3 @@
# Vulkan_TestApp
Triangle Test App with Vulkan API strictly following documentation.
Test App with Vulkan API strictly following documentation.
+17 -4
View File
@@ -1,12 +1,25 @@
#define DEBUG
/**
* Required external Libraries:
* - Vulkan
* - glfw
* - glm
* - stb
* - tinyobjloader
*/
#define DEBUG 1
#include <iostream>
#include "vulkan/vulkan_app.hpp"
int main(){
int main(int argc, char **argv){
std::cout << "Program path: " << argv[0] << std::endl;
try{
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){
std::cerr << e.what() << std::endl;
+16053
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -1,2 +1,2 @@
glslc shader.vert -o vert.spv
glslc shader.frag -o frag.spv
glslc shader.vert -o shader.vert.spv
glslc shader.frag -o shader.frag.spv
+5 -1
View File
@@ -1,9 +1,13 @@
#version 450
layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 fragTexCoord;
layout(location = 0) out vec4 outColor;
layout(binding = 1) uniform sampler2D texSampler;
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
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;
vec2 positions[3] = vec2[](
vec2(0.0, -0.5),
vec2(0.5, 0.5),
vec2(-0.5, 0.5)
);
vec3 colors[3] = vec3[](
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, 1.0)
);
layout(location = 1) out vec2 fragTexCoord;
void main(){
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
fragColor = colors[gl_VertexIndex];
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
fragColor = inColor;
fragTexCoord = inTexCoord;
}
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 940 KiB

+893 -93
View File
File diff suppressed because it is too large Load Diff
+174 -17
View File
@@ -8,43 +8,119 @@
#define GLFW_INCLUDE_VULKAN
#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 <stdexcept>
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <vector>
#include <optional>
#include <set>
#include <limits>
#include <algorithm>
#include <fstream>
#include <array>
#include <chrono>
#include <unordered_map>
namespace vapp{
// Change here for other presentMode
const VkPresentModeKHR WISHED_PRESENT_MODE = VK_PRESENT_MODE_MAILBOX_KHR;
typedef struct _QueueFamilyIndices{
struct Vertex{
glm::vec3 pos;
glm::vec3 color;
glm::vec2 texCoord;
static VkVertexInputBindingDescription getBindingDescription(){
VkVertexInputBindingDescription bindingDescription{};
bindingDescription.binding = 0;
bindingDescription.stride = sizeof(Vertex);
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
return bindingDescription;
}
static std::array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions(){
std::array<VkVertexInputAttributeDescription, 3> attributeDescriptions{};
attributeDescriptions[0].binding = 0;
attributeDescriptions[0].location = 0;
attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
attributeDescriptions[0].offset = offsetof(Vertex, pos);
attributeDescriptions[1].binding = 0;
attributeDescriptions[1].location = 1;
attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
attributeDescriptions[1].offset = offsetof(Vertex, color);
attributeDescriptions[2].binding = 0;
attributeDescriptions[2].location = 2;
attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
attributeDescriptions[2].offset = offsetof(Vertex, texCoord);
return attributeDescriptions;
}
bool operator==(const Vertex &other) const{
return pos == other.pos && color == other.color && texCoord == other.texCoord;
}
};
/*
const std::vector<Vertex> vertices = {
{{-0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f}},
{{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}},
{{-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}}
};
// uint16_t also possible -> change in vkCmdBindIndexBuffer also
const std::vector<uint32_t> indices = {
0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4
};
*/
// END Remove
struct UniformBufferObject{
alignas(16) glm::mat4 model;
alignas(16) glm::mat4 view;
alignas(16) glm::mat4 proj;
};
struct QueueFamilyIndices{
std::optional<uint32_t> graphicsFamily;
std::optional<uint32_t> presentFamily;
bool isComplete(){
return graphicsFamily.has_value();
}
} QueueFamilyIndices;
};
typedef struct{
struct SwapChainSupportDetails{
VkSurfaceCapabilitiesKHR capabilities;
std::vector<VkSurfaceFormatKHR> formats;
std::vector<VkPresentModeKHR> presentModes;
} SwapChainSupportDetails;
};
static std::vector<char> readFile(const std::string& filename);
static std::array<VkVertexInputAttributeDescription, 2> getAttributeDescription();
class Vulkan{
private:
#pragma region Fields
#pragma region PrivateFields
GLFWwindow *window;
uint32_t _width, _height;
@@ -67,21 +143,55 @@ private:
std::vector<VkImageView> swapChainImageViews;
VkRenderPass renderPass;
VkDescriptorSetLayout descriptorSetLayout;
VkPipelineLayout pipelineLayout;
VkPipeline graphicsPipeline;
VkCommandPool commandPool;
VkCommandBuffer commandBuffer;
std::vector<VkCommandBuffer> commandBuffers;
VkSemaphore imageAvailableSemaphore, renderFinishedSemaphore;
VkFence inFlightFence;
std::vector<VkSemaphore> imageAvailableSemaphores, renderFinishedSemaphores;
std::vector<VkFence> inFlightFences;
std::vector<VkFramebuffer> swapChainFramebuffers;
#pragma endregion
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 region PrivateFunctions
bool checkValidationLayerSupport();
std::vector<const char*> getRequiredExtensions();
VkSampleCountFlagBits getMaxUsableSampleCount();
void createInstance();
void setupDebugMessenger();
@@ -97,29 +207,76 @@ private:
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
void createSwapChain();
void createImageViews();
void createDescriptorSetLayout();
void createGraphicsPipeline();
VkShaderModule createShaderModule(const std::vector<char>& code);
void createRenderPass();
void createFrameBuffers();
void createFramebuffers();
void createCommandPool();
void createCommandBuffer();
VkFormat findSupportedFormat(const std::vector<VkFormat> &candidates, VkImageTiling tiling, VkFormatFeatureFlags features);
VkFormat findDepthFormat();
void createColorResources();
void createDepthResources();
VkCommandBuffer beginSingleTimeCommands();
void endSingleTimeCommands(VkCommandBuffer commandBuffer);
void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout, uint32_t mipLevels);
void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height);
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 generateMipmaps(VkImage image, VkFormat imageFormat, int32_t texWidth, int32_t texHeight, uint32_t mipLevels);
void createTextureImage();
VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags, uint32_t mipLevels);
void createTextureImageView();
void createTextureSampler();
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;
#else
const bool enableValidationLayers = false;
#endif
bool framebufferResized = false;
std::string MODEL_PATH;
std::string TEXTURE_PATH;
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
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