forked from pupbrained/vulkan-test
IT WORKS YAYY
This commit is contained in:
parent
40af687b7f
commit
f99ff172db
12
flake.lock
12
flake.lock
|
@ -2,11 +2,11 @@
|
|||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1729128939,
|
||||
"narHash": "sha256-5UI1kh4pN+fOMj9q8Lbe0Vrpu3kqWcSSRdE4JKZjca0=",
|
||||
"lastModified": 1729293761,
|
||||
"narHash": "sha256-bkAI9eZGSR+RqM/ZpBV7eZAwHtek9oPC7Ma2TFN0FnQ=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "e3543c83677b94083d5957e091bba29f155a2ec9",
|
||||
"rev": "f1812eb562c2253492e7f2f0122b40177e46826c",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -58,11 +58,11 @@
|
|||
"nixpkgs": "nixpkgs_2"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1729077719,
|
||||
"narHash": "sha256-zayHqZO9gA1U85c4CPvVSnLV8/cBgc2yVrSKWaKeBUs=",
|
||||
"lastModified": 1729242555,
|
||||
"narHash": "sha256-6jWSWxv2crIXmYSEb3LEVsFkCkyVHNllk61X4uhqfCs=",
|
||||
"owner": "numtide",
|
||||
"repo": "treefmt-nix",
|
||||
"rev": "5307ba60125bb024d7e52d71d582eafd511f3fee",
|
||||
"rev": "d986489c1c757f6921a48c1439f19bfb9b8ecab5",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
|
|
@ -50,10 +50,8 @@
|
|||
glfw
|
||||
glm
|
||||
imgui
|
||||
shaderc.out
|
||||
shaderc.dev
|
||||
shaderc.lib
|
||||
shaderc.static
|
||||
vulkan-extension-layer
|
||||
vulkan-memory-allocator
|
||||
vulkan-utility-libraries
|
||||
|
|
72
src/main.cpp
72
src/main.cpp
|
@ -1,7 +1,8 @@
|
|||
// Include necessary headers
|
||||
#include <chrono> // For time-related functions
|
||||
#include <fmt/format.h> // For string formatting
|
||||
#include <unordered_set> // For unordered_set container
|
||||
#include <chrono> // For time-related functions
|
||||
#include <fmt/format.h> // For string formatting
|
||||
#include <shaderc/shaderc.hpp> // For shader compilation
|
||||
#include <unordered_set> // For unordered_set container
|
||||
|
||||
// GLM (OpenGL Mathematics) configuration
|
||||
#define GLM_FORCE_DEPTH_ZERO_TO_ONE // Use Vulkan's depth range (0 to 1) instead of OpenGL's (-1 to 1)
|
||||
|
@ -22,7 +23,6 @@
|
|||
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
|
||||
|
||||
// Include custom utility headers
|
||||
#include "util/shader.h" // Shader compilation utilities
|
||||
#include "util/types.h" // Custom type definitions
|
||||
#include "util/unique_image.h" // Custom image handling utilities
|
||||
#include "util/vertex.h" // Custom vertex structure definition
|
||||
|
@ -43,6 +43,46 @@ constexpr i32 HEIGHT = 600;
|
|||
// Maximum number of frames that can be processed concurrently
|
||||
constexpr i32 MAX_FRAMES_IN_FLIGHT = 2;
|
||||
|
||||
// Vertex shader
|
||||
constexpr const char* vertShaderSrc = R"glsl(
|
||||
#version 450
|
||||
|
||||
layout(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 = 1) out vec2 fragTexCoord;
|
||||
|
||||
void main() {
|
||||
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
|
||||
fragColor = inColor;
|
||||
fragTexCoord = inTexCoord;
|
||||
}
|
||||
)glsl";
|
||||
|
||||
// Fragment shader
|
||||
constexpr const char* fragShaderSrc = R"glsl(
|
||||
#version 450
|
||||
|
||||
layout(binding = 1) uniform sampler2D texSampler;
|
||||
|
||||
layout(location = 0) in vec3 fragColor;
|
||||
layout(location = 1) in vec2 fragTexCoord;
|
||||
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
void main() {
|
||||
outColor = texture(texSampler, fragTexCoord);
|
||||
}
|
||||
)glsl";
|
||||
|
||||
// Validation layers for debug builds
|
||||
#ifndef NDEBUG
|
||||
constexpr std::array<const char*, 1> validationLayers = { "VK_LAYER_KHRONOS_validation" };
|
||||
|
@ -66,12 +106,10 @@ class VulkanApp {
|
|||
* It also cleans up resources when the application is closed.
|
||||
*/
|
||||
fn run() -> void {
|
||||
InitGlslang();
|
||||
initWindow(); // Initialize the application window
|
||||
initVulkan(); // Initialize Vulkan
|
||||
mainLoop(); // Enter the main rendering loop
|
||||
|
||||
CleanupGlslang();
|
||||
cleanupSwapChain(); // Clean up swap chain resources
|
||||
|
||||
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
|
||||
|
@ -194,6 +232,22 @@ class VulkanApp {
|
|||
alignas(16) glm::mat4 proj; // Projection matrix
|
||||
};
|
||||
|
||||
static fn compileShader(const std::string& source, const shaderc_shader_kind& kind)
|
||||
-> std::vector<uint32_t> {
|
||||
shaderc::Compiler compiler;
|
||||
shaderc::CompileOptions options;
|
||||
|
||||
shaderc::CompilationResult<u32> result =
|
||||
compiler.CompileGlslToSpv(source, kind, "shader.glsl", "main", options);
|
||||
|
||||
if (result.GetCompilationStatus() != shaderc_compilation_status_success) {
|
||||
fmt::println(stderr, "Shader compilation failed: {}", result.GetErrorMessage());
|
||||
return {};
|
||||
}
|
||||
|
||||
return { result.cbegin(), result.cend() };
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reads the contents of a file into a vector of chars.
|
||||
*
|
||||
|
@ -843,8 +897,10 @@ class VulkanApp {
|
|||
* states.
|
||||
*/
|
||||
fn createGraphicsPipeline() -> void {
|
||||
std::vector<u32> vertShaderCode = CompileShader(vertShaderSrc, EShLangVertex);
|
||||
std::vector<u32> fragShaderCode = CompileShader(fragShaderSrc, EShLangFragment);
|
||||
std::vector<u32> vertShaderCode =
|
||||
compileShader(vertShaderSrc, shaderc_shader_kind::shaderc_vertex_shader);
|
||||
std::vector<u32> fragShaderCode =
|
||||
compileShader(fragShaderSrc, shaderc_shader_kind::shaderc_fragment_shader);
|
||||
|
||||
vk::UniqueShaderModule vertShaderModule = createShaderModule(vertShaderCode);
|
||||
vk::UniqueShaderModule fragShaderModule = createShaderModule(fragShaderCode);
|
||||
|
|
|
@ -1,196 +0,0 @@
|
|||
#include <glslang/Public/ShaderLang.h>
|
||||
#include <glslang/SPIRV/GlslangToSpv.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
const TBuiltInResource DefaultTBuiltInResource = {
|
||||
.maxLights = 32,
|
||||
.maxClipPlanes = 6,
|
||||
.maxTextureUnits = 32,
|
||||
.maxTextureCoords = 32,
|
||||
.maxVertexAttribs = 64,
|
||||
.maxVertexUniformComponents = 4096,
|
||||
.maxVaryingFloats = 64,
|
||||
.maxVertexTextureImageUnits = 32,
|
||||
.maxCombinedTextureImageUnits = 80,
|
||||
.maxTextureImageUnits = 32,
|
||||
.maxFragmentUniformComponents = 4096,
|
||||
.maxDrawBuffers = 32,
|
||||
.maxVertexUniformVectors = 128,
|
||||
.maxVaryingVectors = 8,
|
||||
.maxFragmentUniformVectors = 16,
|
||||
.maxVertexOutputVectors = 16,
|
||||
.maxFragmentInputVectors = 15,
|
||||
.minProgramTexelOffset = -8,
|
||||
.maxProgramTexelOffset = 7,
|
||||
.maxClipDistances = 8,
|
||||
.maxComputeWorkGroupCountX = 65535,
|
||||
.maxComputeWorkGroupCountY = 65535,
|
||||
.maxComputeWorkGroupCountZ = 65535,
|
||||
.maxComputeWorkGroupSizeX = 1024,
|
||||
.maxComputeWorkGroupSizeY = 1024,
|
||||
.maxComputeWorkGroupSizeZ = 64,
|
||||
.maxComputeUniformComponents = 1024,
|
||||
.maxComputeTextureImageUnits = 16,
|
||||
.maxComputeImageUniforms = 8,
|
||||
.maxComputeAtomicCounters = 8,
|
||||
.maxComputeAtomicCounterBuffers = 1,
|
||||
.maxVaryingComponents = 60,
|
||||
.maxVertexOutputComponents = 64,
|
||||
.maxGeometryInputComponents = 64,
|
||||
.maxGeometryOutputComponents = 128,
|
||||
.maxFragmentInputComponents = 128,
|
||||
.maxImageUnits = 8,
|
||||
.maxCombinedImageUnitsAndFragmentOutputs = 8,
|
||||
.maxCombinedShaderOutputResources = 8,
|
||||
.maxImageSamples = 0,
|
||||
.maxVertexImageUniforms = 0,
|
||||
.maxTessControlImageUniforms = 0,
|
||||
.maxTessEvaluationImageUniforms = 0,
|
||||
.maxGeometryImageUniforms = 0,
|
||||
.maxFragmentImageUniforms = 8,
|
||||
.maxCombinedImageUniforms = 8,
|
||||
.maxGeometryTextureImageUnits = 16,
|
||||
.maxGeometryOutputVertices = 256,
|
||||
.maxGeometryTotalOutputComponents = 1024,
|
||||
.maxGeometryUniformComponents = 1024,
|
||||
.maxGeometryVaryingComponents = 64,
|
||||
.maxTessControlInputComponents = 128,
|
||||
.maxTessControlOutputComponents = 128,
|
||||
.maxTessControlTextureImageUnits = 16,
|
||||
.maxTessControlUniformComponents = 1024,
|
||||
.maxTessControlTotalOutputComponents = 4096,
|
||||
.maxTessEvaluationInputComponents = 128,
|
||||
.maxTessEvaluationOutputComponents = 128,
|
||||
.maxTessEvaluationTextureImageUnits = 16,
|
||||
.maxTessEvaluationUniformComponents = 1024,
|
||||
.maxTessPatchComponents = 120,
|
||||
.maxPatchVertices = 32,
|
||||
.maxTessGenLevel = 64,
|
||||
.maxViewports = 16,
|
||||
.maxVertexAtomicCounters = 0,
|
||||
.maxTessControlAtomicCounters = 0,
|
||||
.maxTessEvaluationAtomicCounters = 0,
|
||||
.maxGeometryAtomicCounters = 0,
|
||||
.maxFragmentAtomicCounters = 8,
|
||||
.maxCombinedAtomicCounters = 8,
|
||||
.maxAtomicCounterBindings = 1,
|
||||
.maxVertexAtomicCounterBuffers = 0,
|
||||
.maxTessControlAtomicCounterBuffers = 0,
|
||||
.maxTessEvaluationAtomicCounterBuffers = 0,
|
||||
.maxGeometryAtomicCounterBuffers = 0,
|
||||
.maxFragmentAtomicCounterBuffers = 1,
|
||||
.maxCombinedAtomicCounterBuffers = 1,
|
||||
.maxAtomicCounterBufferSize = 16384,
|
||||
.maxTransformFeedbackBuffers = 4,
|
||||
.maxTransformFeedbackInterleavedComponents = 64,
|
||||
.maxCullDistances = 8,
|
||||
.maxCombinedClipAndCullDistances = 8,
|
||||
.maxSamples = 4,
|
||||
.maxMeshOutputVerticesNV = 256,
|
||||
.maxMeshOutputPrimitivesNV = 512,
|
||||
.maxMeshWorkGroupSizeX_NV = 32,
|
||||
.maxMeshWorkGroupSizeY_NV = 1,
|
||||
.maxMeshWorkGroupSizeZ_NV = 1,
|
||||
.maxTaskWorkGroupSizeX_NV = 32,
|
||||
.maxTaskWorkGroupSizeY_NV = 1,
|
||||
.maxTaskWorkGroupSizeZ_NV = 1,
|
||||
.maxMeshViewCountNV = 4,
|
||||
.maxMeshOutputVerticesEXT = 256,
|
||||
.maxMeshOutputPrimitivesEXT = 512,
|
||||
.maxMeshWorkGroupSizeX_EXT = 32,
|
||||
.maxMeshWorkGroupSizeY_EXT = 1,
|
||||
.maxMeshWorkGroupSizeZ_EXT = 1,
|
||||
.maxTaskWorkGroupSizeX_EXT = 32,
|
||||
.maxTaskWorkGroupSizeY_EXT = 1,
|
||||
.maxTaskWorkGroupSizeZ_EXT = 1,
|
||||
.maxMeshViewCountEXT = 4,
|
||||
.maxDualSourceDrawBuffersEXT = 1,
|
||||
.limits = {
|
||||
.nonInductiveForLoops = true,
|
||||
.whileLoops = true,
|
||||
.doWhileLoops = true,
|
||||
.generalUniformIndexing = true,
|
||||
.generalAttributeMatrixVectorIndexing = true,
|
||||
.generalVaryingIndexing = true,
|
||||
.generalSamplerIndexing = true,
|
||||
.generalVariableIndexing = true,
|
||||
.generalConstantMatrixVectorIndexing = true,
|
||||
},
|
||||
};
|
||||
|
||||
constexpr const char* vertShaderSrc = R"glsl(
|
||||
#version 450
|
||||
|
||||
layout(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 = 1) out vec2 fragTexCoord;
|
||||
|
||||
void main() {
|
||||
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
|
||||
fragColor = inColor;
|
||||
fragTexCoord = inTexCoord;
|
||||
}
|
||||
)glsl";
|
||||
|
||||
constexpr const char* fragShaderSrc = R"glsl(
|
||||
#version 450
|
||||
|
||||
layout(binding = 1) uniform sampler2D texSampler;
|
||||
|
||||
layout(location = 0) in vec3 fragColor;
|
||||
layout(location = 1) in vec2 fragTexCoord;
|
||||
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
void main() {
|
||||
outColor = texture(texSampler, fragTexCoord);
|
||||
}
|
||||
)glsl";
|
||||
|
||||
inline void InitGlslang() { glslang::InitializeProcess(); }
|
||||
|
||||
inline void CleanupGlslang() { glslang::FinalizeProcess(); }
|
||||
|
||||
inline fn CompileShader(const char* source, EShLanguage shaderType) -> std::vector<uint32_t> {
|
||||
glslang::TShader shader(shaderType);
|
||||
shader.setStrings(&source, 1);
|
||||
|
||||
// Set shader environment details (language version, profile)
|
||||
shader.setEnvInput(glslang::EShSourceGlsl, shaderType, glslang::EShClientVulkan, 450);
|
||||
shader.setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_2);
|
||||
shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_3);
|
||||
|
||||
// Parse GLSL to AST (abstract syntax tree)
|
||||
if (!shader.parse(&DefaultTBuiltInResource, 450, false, EShMsgDefault)) {
|
||||
std::cerr << "GLSL parsing failed: " << shader.getInfoLog() << std::endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
// Create a program to hold the compiled shader
|
||||
glslang::TProgram program;
|
||||
program.addShader(&shader);
|
||||
|
||||
// Link the program
|
||||
if (!program.link(EShMsgDefault)) {
|
||||
std::cerr << "Linking failed: " << program.getInfoLog() << std::endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
// Convert AST to SPIR-V
|
||||
std::vector<uint32_t> spirv;
|
||||
glslang::SpvOptions spvOptions;
|
||||
glslang::GlslangToSpv(*program.getIntermediate(shaderType), spirv, &spvOptions);
|
||||
|
||||
return spirv;
|
||||
}
|
Loading…
Reference in a new issue