This commit is contained in:
Jorijn van der Graaf 2025-05-03 06:51:33 +02:00
commit 98b775e33e
21 changed files with 361 additions and 5541 deletions

View file

@ -17,10 +17,10 @@ using namespace Crafter;
Camera::Camera() { Camera::Camera() {
old::Camera camera; old::Camera camera;
camera.type = old::Camera::CameraType::lookat; camera.type = old::Camera::CameraType::firstperson;
camera.setPerspective(60.0f, 16 / 9, 0.1f, 512.0f); camera.setPerspective(90.0f, 16 / 9, 0.1f, 512.0f);
camera.setRotation(glm::vec3(180.0f, 0.0f, 0.0f)); camera.setRotation(glm::vec3(180.0f, 150, 0));
camera.setTranslation(glm::vec3(0.0f, 0.0f, -10.0f)); camera.setTranslation(glm::vec3(-100.0f, -130.0f, -100.0f));
projectionView = camera.matrices.perspective*camera.matrices.view; projectionView = camera.matrices.perspective*camera.matrices.view;
} }

View file

@ -40,5 +40,27 @@ namespace Crafter {
} }
return mesh; return mesh;
} }
static Mesh* FromAssetUV(const char* asset) {
std::uint32_t vertexCount = reinterpret_cast<const std::uint32_t*>(asset)[0];
std::uint32_t indexCount = reinterpret_cast<const std::uint32_t*>(asset)[1];
Mesh* mesh = new Mesh(vertexCount, (indexCount+ 63) & ~63);
const float* verticies = reinterpret_cast<const float*>(asset+sizeof(std::uint32_t)*2);
std::uint32_t counter = 0;
for(std::uint32_t i = 0; i < vertexCount*5; i+=5) {
mesh->verticies.value[counter].x = verticies[i];
mesh->verticies.value[counter].y = verticies[i+1];
mesh->verticies.value[counter].z = verticies[i+2];
mesh->verticies.value[counter].u = verticies[i+3];
mesh->verticies.value[counter].v = verticies[i+4];
mesh->verticies.value[counter].w = 1.0f;
counter++;
}
memcpy(mesh->indicies.value, asset+(sizeof(std::uint32_t)*2)+(vertexCount*sizeof(float)*5), indexCount*sizeof(std::uint32_t));
for(std::uint32_t i = indexCount; i < indexCount+(indexCount%64); i++) {
mesh->indicies.value[i] = 0;//pad indicies to nearest 64
}
return mesh;
}
}; };
} }

View file

@ -28,7 +28,7 @@ namespace Crafter {
ComponentRefOwning<Camera> camera; ComponentRefOwning<Camera> camera;
Buffer<glm::mat4> mvp; Buffer<glm::mat4> mvp;
std::uint32_t threadCount; std::uint32_t threadCount;
MeshShader(Mesh<VertexType>* mesh, Camera* camera) : threadCount(mesh->indexCount/64/3), mvp(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT), mesh(mesh), camera(camera) { MeshShader(Mesh<VertexType>* mesh, Camera* camera) : threadCount(std::ceil(static_cast<double>(mesh->indexCount)/64/3)), mvp(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT), mesh(mesh), camera(camera) {
} }
void WriteDescriptors(DescriptorSet& set) { void WriteDescriptors(DescriptorSet& set) {

View file

@ -0,0 +1,55 @@
module;
#include <cstdint>
#include <vulkan/vulkan.h>
#include <cstring>
#include <iostream>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/type_ptr.hpp>
export module Crafter.Graphics:TextureShader;
import Crafter.Component;
import :VulkanTexture;
import :VulkanPipeline;
namespace Crafter {
export template <typename PixelType>
class TextureShader {
public:
VkSampler textureSampler;
VkDescriptorImageInfo imageInfo;
ComponentRefOwning<VulkanTexture<PixelType>> texture;
TextureShader(VulkanTexture<PixelType>* texture) : texture(texture) {
VkSamplerCreateInfo samplerInfo{};
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerInfo.magFilter = VK_FILTER_LINEAR;
samplerInfo.minFilter = VK_FILTER_LINEAR;
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.anisotropyEnable = VK_FALSE;
samplerInfo.maxAnisotropy = 1;
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
samplerInfo.unnormalizedCoordinates = VK_FALSE;
samplerInfo.compareEnable = VK_FALSE;
samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
samplerInfo.mipLodBias = 0.0f;
samplerInfo.minLod = 0.0f;
samplerInfo.maxLod = 0.0f;
VulkanDevice::CHECK_VK_RESULT(vkCreateSampler(VulkanDevice::device, &samplerInfo, nullptr, &textureSampler));
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = texture->imageView;
imageInfo.sampler = textureSampler;
}
void WriteDescriptors(DescriptorSet& set) {
set.Write(1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, &imageInfo);
}
};
}

View file

@ -16,7 +16,7 @@ namespace Crafter {
MousePoint mouseDelta; MousePoint mouseDelta;
}; };
export struct Pixel { export struct Pixel_RU8_GU8_BU8_AU8 {
std::uint8_t r; std::uint8_t r;
std::uint8_t g; std::uint8_t g;
std::uint8_t b; std::uint8_t b;
@ -29,4 +29,15 @@ namespace Crafter {
float z; float z;
float w; float w;
}; };
export struct Vertex_xf32_yf32_zf32_wf32_uf32_vf32 {
float x;
float y;
float z;
float w;
float u;
float v;
float pad[2];
};
} }

View file

@ -33,7 +33,7 @@ export namespace Crafter {
float relativeHeight; float relativeHeight;
float anchorOffsetX; float anchorOffsetX;
float anchorOffsetY; float anchorOffsetY;
std::vector<Pixel> buffer; std::vector<Pixel_RU8_GU8_BU8_AU8> buffer;
ComponentRef<UiElement> parent; ComponentRef<UiElement> parent;
ComponentRefVectorOwning<UiElement> children; ComponentRefVectorOwning<UiElement> children;
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); 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);

View file

@ -13,6 +13,8 @@ namespace Crafter {
public: public:
T* value; T* value;
VkDescriptorBufferInfo descriptor; VkDescriptorBufferInfo descriptor;
VkBuffer buffer = VK_NULL_HANDLE;
VkDeviceMemory memory = VK_NULL_HANDLE;
Buffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, std::uint32_t count = 1) { Buffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, std::uint32_t count = 1) {
VkBufferCreateInfo bufferCreateInfo = vks::initializers::bufferCreateInfo(usageFlags, sizeof(T)*count); VkBufferCreateInfo bufferCreateInfo = vks::initializers::bufferCreateInfo(usageFlags, sizeof(T)*count);
VulkanDevice::CHECK_VK_RESULT(vkCreateBuffer(VulkanDevice::device, &bufferCreateInfo, nullptr, &buffer)); VulkanDevice::CHECK_VK_RESULT(vkCreateBuffer(VulkanDevice::device, &bufferCreateInfo, nullptr, &buffer));
@ -44,9 +46,12 @@ namespace Crafter {
VulkanDevice::CHECK_VK_RESULT(vkBindBufferMemory(VulkanDevice::device, buffer, memory, 0)); VulkanDevice::CHECK_VK_RESULT(vkBindBufferMemory(VulkanDevice::device, buffer, memory, 0));
VulkanDevice::CHECK_VK_RESULT(vkMapMemory(VulkanDevice::device, memory, 0, sizeof(T)*count, 0, reinterpret_cast<void**>(&value))); VulkanDevice::CHECK_VK_RESULT(vkMapMemory(VulkanDevice::device, memory, 0, sizeof(T)*count, 0, reinterpret_cast<void**>(&value)));
} }
~Buffer() {
vkUnmapMemory(VulkanDevice::device, memory);
vkDestroyBuffer(VulkanDevice::device, buffer, nullptr);
vkFreeMemory(VulkanDevice::device, memory, nullptr);
}
private: private:
VkBuffer buffer = VK_NULL_HANDLE;
VkDeviceMemory memory = VK_NULL_HANDLE;
VkDeviceSize alignment = 0; VkDeviceSize alignment = 0;
VkMemoryPropertyFlags memoryPropertyFlags; VkMemoryPropertyFlags memoryPropertyFlags;
VkBufferUsageFlags usageFlags; VkBufferUsageFlags usageFlags;

View file

@ -35,6 +35,10 @@ namespace Crafter {
VkWriteDescriptorSet write = vks::initializers::writeDescriptorSet(set[stage], type, binding, buffer); VkWriteDescriptorSet write = vks::initializers::writeDescriptorSet(set[stage], type, binding, buffer);
vkUpdateDescriptorSets(VulkanDevice::device, 1, &write, 0, nullptr); vkUpdateDescriptorSets(VulkanDevice::device, 1, &write, 0, nullptr);
} }
void Write(std::uint32_t stage, VkDescriptorType type, std::uint32_t binding, VkDescriptorImageInfo* buffer) {
VkWriteDescriptorSet write = vks::initializers::writeDescriptorSet(set[stage], type, binding, buffer);
vkUpdateDescriptorSets(VulkanDevice::device, 1, &write, 0, nullptr);
}
}; };
export template <typename MeshShader, typename FragmentShader> export template <typename MeshShader, typename FragmentShader>
@ -153,7 +157,7 @@ namespace Crafter {
// Pipeline // Pipeline
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE); VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_CLOCKWISE, 0); VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE, 0);
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE); VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState); VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL); VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);

View file

@ -13,13 +13,22 @@ module;
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
export module Crafter.Graphics:VulkanTexture; export module Crafter.Graphics:VulkanTexture;
import Crafter.Component;
import :VulkanDevice; import :VulkanDevice;
import :VulkanBuffer;
namespace Crafter { namespace Crafter {
class VulkanTexture { export template <typename PixelType>
class VulkanTexture : public Component {
public: public:
static void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) { VkImage image;
VkDeviceMemory imageMemory;
Buffer<PixelType> buffer;
VkImageView imageView;
VulkanTexture(std::uint32_t width, std::uint32_t height, const PixelType* pixels, VkCommandBuffer cmd) : buffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, width*height) {
memcpy(buffer.value, pixels, width*height*sizeof(PixelType));
VkImageCreateInfo imageInfo{}; VkImageCreateInfo imageInfo{};
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.imageType = VK_IMAGE_TYPE_2D; imageInfo.imageType = VK_IMAGE_TYPE_2D;
@ -28,10 +37,10 @@ namespace Crafter {
imageInfo.extent.depth = 1; imageInfo.extent.depth = 1;
imageInfo.mipLevels = 1; imageInfo.mipLevels = 1;
imageInfo.arrayLayers = 1; imageInfo.arrayLayers = 1;
imageInfo.format = format; imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
imageInfo.tiling = tiling; imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageInfo.usage = usage; imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VulkanDevice::CHECK_VK_RESULT(vkCreateImage(VulkanDevice::device, &imageInfo, nullptr, &image)); VulkanDevice::CHECK_VK_RESULT(vkCreateImage(VulkanDevice::device, &imageInfo, nullptr, &image));
@ -42,11 +51,100 @@ namespace Crafter {
VkMemoryAllocateInfo allocInfo{}; VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size; allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties); allocInfo.memoryTypeIndex = VulkanDevice::GetMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VulkanDevice::CHECK_VK_RESULT(vkAllocateMemory(VulkanDevice::device, &allocInfo, nullptr, &imageMemory)); VulkanDevice::CHECK_VK_RESULT(vkAllocateMemory(VulkanDevice::device, &allocInfo, nullptr, &imageMemory));
vkBindImageMemory(VulkanDevice::device, image, imageMemory, 0); vkBindImageMemory(VulkanDevice::device, image, imageMemory, 0);
TransitionImageLayout(cmd, buffer, image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
VkBufferImageCopy region{};
region.bufferOffset = 0;
region.bufferRowLength = 0;
region.bufferImageHeight = 0;
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
region.imageSubresource.mipLevel = 0;
region.imageSubresource.baseArrayLayer = 0;
region.imageSubresource.layerCount = 1;
region.imageOffset = {0, 0, 0};
region.imageExtent = { width, height, 1};
vkCmdCopyBufferToImage(
cmd,
buffer.buffer,
image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1,
&region
);
TransitionImageLayout(cmd, buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
VkImageViewCreateInfo viewInfo{};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = image;
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = 1;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 1;
VulkanDevice::CHECK_VK_RESULT(vkCreateImageView(VulkanDevice::device, &viewInfo, nullptr, &imageView));
}
static VulkanTexture<PixelType>* FromAsset(const char* asset, VkCommandBuffer cmd) {
std::uint32_t width = reinterpret_cast<const std::uint32_t*>(asset)[0];
std::uint32_t height = reinterpret_cast<const std::uint32_t*>(asset)[1];
const PixelType* pixels = reinterpret_cast<const PixelType*>(reinterpret_cast<const std::uint32_t*>(asset)+2);
return new VulkanTexture<PixelType>(width, height, pixels, cmd);
}
private:
void TransitionImageLayout(VkCommandBuffer cmd, Buffer<PixelType>& buffer, VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout) {
VkImageMemoryBarrier barrier{};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.oldLayout = oldLayout;
barrier.newLayout = newLayout;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = image;
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
barrier.subresourceRange.baseMipLevel = 0;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.baseArrayLayer = 0;
barrier.subresourceRange.layerCount = 1;
barrier.srcAccessMask = 0;
barrier.dstAccessMask = 0;
VkPipelineStageFlags sourceStage;
VkPipelineStageFlags destinationStage;
if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
barrier.srcAccessMask = 0;
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
} else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
} else {
throw std::invalid_argument("unsupported layout transition!");
}
vkCmdPipelineBarrier(
cmd,
sourceStage, destinationStage,
0,
0, nullptr,
0, nullptr,
1, &barrier
);
} }
}; };
} }

View file

@ -23,7 +23,6 @@ module;
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <print> #include <print>
#include "cat.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -261,7 +260,7 @@ WindowWayland::WindowWayland(std::string name, std::uint32_t width, std::uint32_
} }
// Map the shared memory file // Map the shared memory file
shm_data = reinterpret_cast<Pixel*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); shm_data = reinterpret_cast<Pixel_RU8_GU8_BU8_AU8*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
if (shm_data == MAP_FAILED) { if (shm_data == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n"); fprintf(stderr, "mmap failed: %m\n");
close(fd); close(fd);

View file

@ -19,7 +19,7 @@ export namespace Crafter {
WindowWayland(std::string name, std::uint32_t width, std::uint32_t height); WindowWayland(std::string name, std::uint32_t width, std::uint32_t height);
~WindowWayland(); ~WindowWayland();
protected: protected:
Pixel* shm_data = NULL; Pixel_RU8_GU8_BU8_AU8* shm_data = NULL;
bool configured = false; bool configured = false;
wl_shm* shm = NULL; wl_shm* shm = NULL;
wl_seat* seat = NULL; wl_seat* seat = NULL;

View file

@ -348,16 +348,36 @@ WindowWaylandVulkan::~WindowWaylandVulkan() {
} }
} }
VkCommandBuffer WindowWaylandVulkan::StartInit() {
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
VulkanDevice::CHECK_VK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[currentBuffer], &cmdBufInfo));
return drawCmdBuffers[currentBuffer];
}
void WindowWaylandVulkan::FinishInit() {
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
VulkanDevice::CHECK_VK_RESULT(vkEndCommandBuffer(drawCmdBuffers[currentBuffer]));
VulkanDevice::CHECK_VK_RESULT(vkQueueSubmit(VulkanDevice::queue, 1, &submitInfo, VK_NULL_HANDLE));
VulkanDevice::CHECK_VK_RESULT(vkQueueWaitIdle(VulkanDevice::queue));
}
void WindowWaylandVulkan::Start() { void WindowWaylandVulkan::Start() {
while (open && wl_display_dispatch(display) != -1) {
// Acquire the next image from the swap chain
VulkanDevice::CHECK_VK_RESULT(vkAcquireNextImageKHR(VulkanDevice::device, swapChain, UINT64_MAX, semaphores.presentComplete, (VkFence)nullptr, &currentBuffer));
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo(); VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
VkClearValue clearValues[2]; VkClearValue clearValues[2];
clearValues[0].color = { };; clearValues[0].color = { };;
clearValues[1].depthStencil = { 1.0f, 0 }; clearValues[1].depthStencil = { 1.0f, 0 };
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i) VulkanDevice::CHECK_VK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[currentBuffer], &cmdBufInfo));
{
VulkanDevice::CHECK_VK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
VkImageSubresourceRange range{}; VkImageSubresourceRange range{};
range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
@ -369,8 +389,8 @@ void WindowWaylandVulkan::Start() {
VkImageSubresourceRange depth_range{range}; VkImageSubresourceRange depth_range{range};
depth_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; depth_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
image_layout_transition(drawCmdBuffers[i], image_layout_transition(drawCmdBuffers[currentBuffer],
images[i], images[currentBuffer],
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
0, 0,
@ -379,14 +399,14 @@ void WindowWaylandVulkan::Start() {
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
range); range);
image_layout_transition(drawCmdBuffers[i], image_layout_transition(drawCmdBuffers[currentBuffer],
depthStencil.image, depthStencil.image,
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL,
depth_range); depth_range);
VkRenderingAttachmentInfoKHR color_attachment_info = {VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR, VK_NULL_HANDLE}; VkRenderingAttachmentInfoKHR color_attachment_info = {VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR, VK_NULL_HANDLE};
color_attachment_info.imageView = imageViews[i]; color_attachment_info.imageView = imageViews[currentBuffer];
color_attachment_info.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; color_attachment_info.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
color_attachment_info.resolveMode = VK_RESOLVE_MODE_NONE; color_attachment_info.resolveMode = VK_RESOLVE_MODE_NONE;
color_attachment_info.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; color_attachment_info.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
@ -410,32 +430,27 @@ void WindowWaylandVulkan::Start() {
render_info.pDepthAttachment = &depth_attachment_info; render_info.pDepthAttachment = &depth_attachment_info;
render_info.pStencilAttachment = VK_NULL_HANDLE; render_info.pStencilAttachment = VK_NULL_HANDLE;
VulkanDevice::vkCmdBeginRenderingKHRProc(drawCmdBuffers[i], &render_info); VulkanDevice::vkCmdBeginRenderingKHRProc(drawCmdBuffers[currentBuffer], &render_info);
VkViewport viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f); VkViewport viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); vkCmdSetViewport(drawCmdBuffers[currentBuffer], 0, 1, &viewport);
VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0); VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0);
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); vkCmdSetScissor(drawCmdBuffers[currentBuffer], 0, 1, &scissor);
onDraw.Invoke(drawCmdBuffers[i]); onDraw.Invoke(drawCmdBuffers[currentBuffer]);
VulkanDevice::vkCmdEndRenderingKHRProc(drawCmdBuffers[i]); VulkanDevice::vkCmdEndRenderingKHRProc(drawCmdBuffers[currentBuffer]);
image_layout_transition(drawCmdBuffers[i], image_layout_transition(drawCmdBuffers[currentBuffer],
images[i], images[currentBuffer],
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
range range
); );
VulkanDevice::CHECK_VK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i])); VulkanDevice::CHECK_VK_RESULT(vkEndCommandBuffer(drawCmdBuffers[currentBuffer]));
}
while (open && wl_display_dispatch(display) != -1) {
// Acquire the next image from the swap chain
VulkanDevice::CHECK_VK_RESULT(vkAcquireNextImageKHR(VulkanDevice::device, swapChain, UINT64_MAX, semaphores.presentComplete, (VkFence)nullptr, &currentBuffer));
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
VulkanDevice::CHECK_VK_RESULT(vkQueueSubmit(VulkanDevice::queue, 1, &submitInfo, VK_NULL_HANDLE)); VulkanDevice::CHECK_VK_RESULT(vkQueueSubmit(VulkanDevice::queue, 1, &submitInfo, VK_NULL_HANDLE));
VkPresentInfoKHR presentInfo = {}; VkPresentInfoKHR presentInfo = {};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;

View file

@ -31,6 +31,8 @@ namespace Crafter {
Event<VkCommandBuffer> onDraw; Event<VkCommandBuffer> onDraw;
WindowWaylandVulkan(std::string name, std::uint32_t width, std::uint32_t height); WindowWaylandVulkan(std::string name, std::uint32_t width, std::uint32_t height);
~WindowWaylandVulkan(); ~WindowWaylandVulkan();
VkCommandBuffer StartInit();
void FinishInit();
void Start(); void Start();
private: private:
void CreateSwapchain(); void CreateSwapchain();

View file

@ -23,7 +23,6 @@ module;
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <print> #include <print>
#include "cat.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -41,13 +40,13 @@ import Crafter.Event;
using namespace Crafter; using namespace Crafter;
void ScaleBitmapR8G8B8(Pixel* dst, const Pixel* src, std::uint32_t srcWidth, std::uint32_t srcHeight, std::uint32_t dstWidth, std::uint32_t dstHeight) { void ScaleBitmapR8G8B8(Pixel_RU8_GU8_BU8_AU8* dst, const Pixel_RU8_GU8_BU8_AU8* src, std::uint32_t srcWidth, std::uint32_t srcHeight, std::uint32_t dstWidth, std::uint32_t dstHeight) {
for (std::uint32_t y = 0; y < dstHeight; y++) { for (std::uint32_t y = 0; y < dstHeight; y++) {
std::uint32_t srcY = y * srcHeight / dstHeight; std::uint32_t srcY = y * srcHeight / dstHeight;
for (std::uint32_t x = 0; x < dstWidth; x++) { for (std::uint32_t x = 0; x < dstWidth; x++) {
std::uint32_t srcX = x * srcWidth / dstWidth; std::uint32_t srcX = x * srcWidth / dstWidth;
const Pixel* srcPixel = src + (srcY * srcWidth + srcX); const Pixel_RU8_GU8_BU8_AU8* srcPixel = src + (srcY * srcWidth + srcX);
Pixel* dstPixel = dst + (y * dstWidth + x); Pixel_RU8_GU8_BU8_AU8* dstPixel = dst + (y * dstWidth + x);
dstPixel[0] = srcPixel[0]; dstPixel[0] = srcPixel[0];
} }
} }
@ -85,7 +84,7 @@ void WindowWaylandWayland::Start() {
} }
realX = (element->anchorX*width)-(element->anchorOffsetX*elementWidth); realX = (element->anchorX*width)-(element->anchorOffsetX*elementWidth);
realY = (element->anchorY*height)-(element->anchorOffsetY*elementHeight); realY = (element->anchorY*height)-(element->anchorOffsetY*elementHeight);
std::vector<Pixel> scaled(elementWidth*elementHeight); std::vector<Pixel_RU8_GU8_BU8_AU8> scaled(elementWidth*elementHeight);
ScaleBitmapR8G8B8(scaled.data(), element->buffer.data(), element->bufferWidth, element->bufferHeight, elementWidth, elementHeight); ScaleBitmapR8G8B8(scaled.data(), element->buffer.data(), element->bufferWidth, element->bufferHeight, elementWidth, elementHeight);
for(std::int32_t x = realX; x-realX < elementWidth; x++) { for(std::int32_t x = realX; x-realX < elementWidth; x++) {
for(std::int32_t y = realY; y-realY < elementHeight; y++) { for(std::int32_t y = realY; y-realY < elementHeight; y++) {

View file

@ -13,3 +13,5 @@ export import :Camera;
export import :VulkanBuffer; export import :VulkanBuffer;
export import :Mesh; export import :Mesh;
export import :MeshShader; export import :MeshShader;
export import :VulkanTexture;
export import :TextureShader;

View file

@ -0,0 +1,10 @@
#version 450
layout(location = 0) in vec2 fragUV;
layout(location = 0) out vec4 outColor;
layout(set = 1, binding = 0) uniform sampler2D texSampler;
void main()
{
outColor = texture(texSampler, fragUV);
}

View file

@ -25,6 +25,6 @@ void main()
uint localID = gl_LocalInvocationIndex.x*3; uint localID = gl_LocalInvocationIndex.x*3;
gl_MeshVerticesEXT[localID].gl_Position = ubo.modelProjectionView * vertex.pos[index.index[triangleID]]; gl_MeshVerticesEXT[localID].gl_Position = ubo.modelProjectionView * vertex.pos[index.index[triangleID]];
gl_MeshVerticesEXT[localID+1].gl_Position = ubo.modelProjectionView * vertex.pos[index.index[triangleID+1]]; gl_MeshVerticesEXT[localID+1].gl_Position = ubo.modelProjectionView * vertex.pos[index.index[triangleID+1]];
gl_MeshVerticesEXT[localID+2].gl_Position =ubo.modelProjectionView * vertex.pos[index.index[triangleID+2]]; gl_MeshVerticesEXT[localID+2].gl_Position = ubo.modelProjectionView * vertex.pos[index.index[triangleID+2]];
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex.x] = uvec3(localID, localID+1, localID+2); gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex.x] = uvec3(localID, localID+1, localID+2);
} }

46
MeshShaderXYZUV.mesh Normal file
View file

@ -0,0 +1,46 @@
#version 450
#extension GL_EXT_mesh_shader : require
layout (binding = 0) uniform UBO
{
mat4 modelProjectionView;
} ubo;
struct VertexType
{
vec4 position;
vec2 uv;
vec2 pad;
};
layout (std140, binding = 1) buffer VERTEX
{
VertexType pos[];
} vertex;
layout (binding = 2) buffer INDEX {
uint index[];
} index;
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
layout(triangles, max_vertices = 192, max_primitives = 64) out;
layout (location = 0) out PerVertexData
{
vec2 uv;
} outVert[];
void main()
{
SetMeshOutputsEXT(192, 64);
uint triangleID = ((gl_WorkGroupID.x * gl_WorkGroupSize.x) + gl_LocalInvocationIndex.x)*3;
uint localID = gl_LocalInvocationIndex.x*3;
gl_MeshVerticesEXT[localID].gl_Position = ubo.modelProjectionView * vertex.pos[index.index[triangleID]].position;
gl_MeshVerticesEXT[localID+1].gl_Position = ubo.modelProjectionView * vertex.pos[index.index[triangleID+1]].position;
gl_MeshVerticesEXT[localID+2].gl_Position = ubo.modelProjectionView * vertex.pos[index.index[triangleID+2]].position;
outVert[localID].uv = vertex.pos[index.index[triangleID]].uv;
outVert[localID + 1].uv = vertex.pos[index.index[triangleID+1]].uv;
outVert[localID + 2].uv = vertex.pos[index.index[triangleID+2]].uv;
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex.x] = uvec3(localID, localID+1, localID+2);
}

5469
cat.h

File diff suppressed because it is too large Load diff

View file

@ -14,8 +14,8 @@ import Crafter.Asset;
import Crafter.Event; import Crafter.Event;
using namespace Crafter; using namespace Crafter;
typedef VulkanShader<"MeshShaderXYZ.spirv", "main", VK_SHADER_STAGE_MESH_BIT_EXT, 3, {{{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2}}}> MeshVulkanShader; typedef VulkanShader<"MeshShaderXYZUV.spirv", "main", VK_SHADER_STAGE_MESH_BIT_EXT, 3, {{{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2}}}> MeshVulkanShader;
typedef VulkanShader<"FragmentShaderSolidWhite.spirv", "main", VK_SHADER_STAGE_FRAGMENT_BIT, 0, {}> FragmentShader; typedef VulkanShader<"FragmentShaderTexture.spirv", "main", VK_SHADER_STAGE_FRAGMENT_BIT, 1, {{{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0}}}> FragmentShader;
typedef VulkanPipeline<MeshVulkanShader, FragmentShader> Pipeline; typedef VulkanPipeline<MeshVulkanShader, FragmentShader> Pipeline;
int main() { int main() {
@ -27,18 +27,29 @@ int main() {
WindowWaylandVulkan window("Crafter.Graphics", 1280, 720); WindowWaylandVulkan window("Crafter.Graphics", 1280, 720);
Asset asset; Asset asset;
asset.LoadFull("core.cras"); asset.LoadFull("gulch.cras");
Camera camera; Camera camera;
Mesh<Vertex_xf32_yf32_zf32_wf32>* mesh = Mesh<Vertex_xf32_yf32_zf32_wf32>::FromAsset(asset.entries[0].data.data()); Mesh<Vertex_xf32_yf32_zf32_wf32_uf32_vf32>* mesh = Mesh<Vertex_xf32_yf32_zf32_wf32_uf32_vf32>::FromAssetUV(asset.entries[0].data.data());
DescriptorSet descriptors; DescriptorSet descriptors;
Pipeline::GetDescriptorSet(descriptors); Pipeline::GetDescriptorSet(descriptors);
MeshShader<Vertex_xf32_yf32_zf32_wf32> meshShader(mesh, &camera); MeshShader<Vertex_xf32_yf32_zf32_wf32_uf32_vf32> meshShader(mesh, &camera);
meshShader.WriteDescriptors(descriptors); meshShader.WriteDescriptors(descriptors);
meshShader.transform = glm::mat4(1.0f); meshShader.transform = glm::mat4(1.0f);
meshShader.Update(); meshShader.Update();
Asset asset2;
asset2.LoadFull("gulchtex.cras");
VkCommandBuffer cmd = window.StartInit();
VulkanTexture<Pixel_RU8_GU8_BU8_AU8>* txt = VulkanTexture<Pixel_RU8_GU8_BU8_AU8>::FromAsset(asset2.entries[0].data.data(), cmd);
TextureShader texShader(txt);
texShader.WriteDescriptors(descriptors);
window.FinishInit();
EventListener<VkCommandBuffer> listener(&window.onDraw, [&descriptors, &meshShader](VkCommandBuffer cmd){ EventListener<VkCommandBuffer> listener(&window.onDraw, [&descriptors, &meshShader](VkCommandBuffer cmd){
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipelineLayout, 0, 2, &descriptors.set[0], 0, NULL); vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipelineLayout, 0, 2, &descriptors.set[0], 0, NULL);
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipeline); vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipeline);

View file

@ -6,7 +6,7 @@
"standard": "c++26", "standard": "c++26",
"source_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics-UiElement", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-WindowWaylandVulkan", "VulkanBuffer", "VulkanTools", "Crafter.Graphics-Camera"], "source_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics-UiElement", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-WindowWaylandVulkan", "VulkanBuffer", "VulkanTools", "Crafter.Graphics-Camera"],
"c_files": ["wayland-xdg-decoration-unstable-v1-client-protocol", "xdg-shell-protocol", "shm"], "c_files": ["wayland-xdg-decoration-unstable-v1-client-protocol", "xdg-shell-protocol", "shm"],
"module_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics", "Crafter.Graphics-UiElement", "Crafter.Graphics-Types", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-VulkanPipeline", "Crafter.Graphics-VulkanShader", "Crafter.Graphics-WindowWaylandVulkan", "Crafter.Graphics-Camera", "Crafter.Graphics-VulkanBuffer", "Crafter.Graphics-Mesh", "Crafter.Graphics-MeshShader"], "module_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics", "Crafter.Graphics-UiElement", "Crafter.Graphics-Types", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-VulkanPipeline", "Crafter.Graphics-VulkanShader", "Crafter.Graphics-WindowWaylandVulkan", "Crafter.Graphics-Camera", "Crafter.Graphics-VulkanBuffer", "Crafter.Graphics-Mesh", "Crafter.Graphics-MeshShader", "Crafter.Graphics-VulkanTexture", "Crafter.Graphics-TextureShader"],
"build_dir": "build", "build_dir": "build",
"output_dir": "bin", "output_dir": "bin",
"type":"library", "type":"library",
@ -18,10 +18,20 @@
"type":13, "type":13,
"entrypoint":"main" "entrypoint":"main"
}, },
{
"path":"MeshShaderXYZUV.mesh",
"type":13,
"entrypoint":"main"
},
{ {
"path":"FragmentShaderSolidWhite.frag", "path":"FragmentShaderSolidWhite.frag",
"type":4, "type":4,
"entrypoint":"main" "entrypoint":"main"
},
{
"path":"FragmentShaderTexture.frag",
"type":4,
"entrypoint":"main"
} }
] ]
}, },