This commit is contained in:
Jorijn van der Graaf 2025-06-14 01:45:33 +02:00
commit 53df70b4f1
70 changed files with 108 additions and 11285 deletions

View file

@ -28,22 +28,19 @@ import Crafter.Math;
import Crafter.Event;
namespace Crafter {
/**
* @brief 3D camera with projection and view matrices
* @example examples/VulkanCube/main.cpp
*/
export class Camera {
public:
MatrixRowMajor<float, 4, 4, 1> projection;
MatrixRowMajor<float, 4, 4, 1> view;
MatrixRowMajor<float, 4, 4, 1> projectionView;
Event<void> onUpdate;
/**
* @brief Constructs a camera.
* @param fov Field of view in radians.
* @param aspectRatio Aspect ratio of the camera (width / height).
* @param near Near clipping plane.
* @param far Far clipping plane.
* @example examples/VulkanCube/main.cpp
*/
Camera(float fov, float aspectRatio, float near, float far);

View file

@ -40,10 +40,16 @@ namespace Crafter {
bool occured = true;
};
/**
* @brief A holder for descriptor sets.
*/
export template <typename MeshShader, typename FragmentShader>
class DescriptorSet {
public:
VkDescriptorSet set[2];
/**
* @brief This event is triggered when a descriptor is aded to this static set which invalidates all previous descriptors, subscribe to this event to renew them.
*/
inline static Event<void> onDescriptorRefresh;
inline static std::vector<DescriptorSet*> sets;
inline static VkDescriptorPool descriptorPool = VK_NULL_HANDLE;

View file

@ -38,10 +38,20 @@ namespace Crafter {
std::uint32_t indexCount;
Buffer<VertexType> verticies;
Buffer<std::uint32_t> indicies;
/**
* @brief Constructs Mesh with empty vertex and index buffer.
* @param vertexCount count of the vertex buffer
* @param vertexCount count of the index buffer
*/
Mesh(std::uint32_t vertexCount, std::uint32_t indexCount) : vertexCount(vertexCount), indexCount(indexCount), verticies(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, vertexCount), indicies(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, indexCount) {
}
/**
* @brief Constructs Mesh from an in memory char buffer
* @param asset pointer to the char buffer
*/
Mesh(const char* asset) requires(std::same_as<VertexType, Vertex>) : vertexCount(reinterpret_cast<const std::uint32_t*>(asset)[0]), indexCount(((reinterpret_cast<const std::uint32_t*>(asset)[1]) + 63) & ~63), verticies(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, vertexCount), indicies(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, indexCount) {
uint32_t indexCountNoPadding = reinterpret_cast<const std::uint32_t*>(asset)[1];
const float* verticies = reinterpret_cast<const float*>(asset+sizeof(std::uint32_t)*2);
@ -62,6 +72,10 @@ namespace Crafter {
}
/**
* @brief Constructs UV Mesh from an in memory char buffer
* @param asset pointer to the char buffer
*/
Mesh(const char* asset) requires(std::same_as<VertexType, VertexUV>) : vertexCount(reinterpret_cast<const std::uint32_t*>(asset)[0]), indexCount(((reinterpret_cast<const std::uint32_t*>(asset)[1]) + 63) & ~63), verticies(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, vertexCount), indicies(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, indexCount) {
uint32_t indexCountNoPadding = reinterpret_cast<const std::uint32_t*>(asset)[1];
const float* verticies = reinterpret_cast<const float*>(asset+sizeof(std::uint32_t)*2);
@ -81,8 +95,13 @@ namespace Crafter {
}
}
/**
* @brief Constructs heightmap Mesh from an in memory char buffer
* @param heights pointer to heights
* @param sizeX size in the X dimension
* @param sizeZ size in the Y dimension
* @param spacing spacing between the points
*/
Mesh(float* heights, uint32_t sizeX, uint32_t sizeZ, float spacing) : vertexCount(sizeX*sizeZ), indexCount(((((sizeX-1)*(sizeZ-1))*6)+ 63) & ~63), verticies(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, vertexCount), indicies(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, indexCount) {
uint32_t indexCountNoPadding = ((sizeX-1)*(sizeZ-1))*6;
for (float x = 0; x < sizeX; x++)

View file

@ -63,6 +63,9 @@ namespace Crafter {
};
vkUpdateDescriptorSets(VulkanDevice::device, 3, &write[0], 0, nullptr);
}
/**
* @brief Must be called after every update to the camera or this transform
*/
void Update() {
*mvp.value = camera->projectionView*transform;
}

View file

@ -27,8 +27,8 @@ export module Crafter.Graphics:UiElement;
import Crafter.Event;
import :Types;
export namespace Crafter {
class UiElement {
namespace Crafter {
export class UiElement {
public:
Event<MouseMoveEvent> onMouseMove;
Event<MouseMoveEvent> onMouseEnter;
@ -54,7 +54,35 @@ export namespace Crafter {
float anchorOffsetY;
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
std::vector<UiElement> children;
/**
* @brief Constructs a UiElement with absolute dimensions
* @param anchorX Relative position where this elements x anchor (top-left) is placed to its parent x anchor
* @param anchorY Relative position where this elements y anchor (top-left) is placed to its parent y anchor
* @param bufferWidth The width of this elements pixel buffer
* @param bufferHeight The height of this elements pixel buffer
* @param absoluteWidth The absolute x size in pixels this element should be scaled to
* @param absoluteHeight The absolute y size in pixels this element should be scaled to
* @param anchorOffsetX The amount this element's anchor should be offset from the top left corner (0.5 to in the middle)
* @param anchorOffsetY The amount this element's anchor should be offset from the top left corner (0.5 to place it in the middle)
* @param z This elements Z position
* @param ignoreScaling Wether this element ignores the scaling of the window, if true its size will be scaled according to the window scale
*/
UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, std::uint32_t absoluteWidth, std::uint32_t absoluteHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false);
/**
* @brief Constructs a UiElement with relative dimensions
* @param anchorX Relative position where this elements x anchor (top-left) is placed to its parent x anchor
* @param anchorY Relative position where this elements y anchor (top-left) is placed to its parent y anchor
* @param bufferWidth The width of this elements pixel buffer
* @param bufferHeight The height of this elements pixel buffer
* @param relativeWidth The relative x size this element should be scaled to compared to its parent
* @param relativeHeight The relative y size this element should be scaled to compared to its parent
* @param anchorOffsetX The amount this element's anchor should be offset from the top left corner (0.5 to in the middle)
* @param anchorOffsetY The amount this element's anchor should be offset from the top left corner (0.5 to place it in the middle)
* @param z This elements Z position
* @param ignoreScaling Wether this element ignores the scaling of the window, if true its size will be scaled according to the window scale
*/
UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false);
};
}

View file

@ -42,6 +42,9 @@ export namespace Crafter {
inline static PFN_vkCmdEndRenderingKHR vkCmdEndRenderingKHRProc;
inline static VkPhysicalDeviceMemoryProperties memoryProperties;
inline static VkFormat depthFormat = VK_FORMAT_UNDEFINED;
/**
* @brief Creates the vulkan device, this must be called before any use of vulkan.
*/
static void CreateDevice();
static void CHECK_VK_RESULT(VkResult result);
static std::uint32_t GetMemoryType(std::uint32_t typeBits, VkMemoryPropertyFlags properties);

View file

@ -50,6 +50,10 @@ namespace Crafter {
inline static VkPipelineLayout pipelineLayout;
inline static VkDescriptorSetLayout descriptorSetLayout[2];
/**
* @brief Creates the vulkan pipeline, this must be called before any use of this pipeline and all shaders must be created before this pipeline is created.
*/
static void CreatePipeline() {
// Layout
constexpr std::array<VkDescriptorSetLayoutBinding, MeshShader::descriptorCount> setLayoutBindingsMesh = GetDescriptorSet<MeshShader, VK_SHADER_STAGE_MESH_BIT_EXT>();

View file

@ -60,6 +60,9 @@ namespace Crafter {
constexpr static std::array<DescriptorBinding, DescriptorCount> descriptors = Descriptors;
constexpr static StringLiteral _entrypoint = entrypoint;
constexpr static VkShaderStageFlagBits _stage = stage;
/**
* @brief Creates the vulkan shader, this must be called before any use of this shader.
*/
static void CreateShader() {
std::ifstream file(path.value, std::ios::binary);
if (!file) {

View file

@ -64,8 +64,5 @@ int main() {
VulkanDevice::vkCmdDrawMeshTasksEXTProc(cmd, meshShader.threadCount, 1, 1);
});
window.Start();
while(true) {
}
window.StartSync();
}