This commit is contained in:
Jorijn van der Graaf 2025-05-08 01:13:59 +02:00
commit 0c6934023b
4 changed files with 59 additions and 64 deletions

View file

@ -29,10 +29,11 @@ export module Crafter.Graphics:Mesh;
import Crafter.Component;
import Crafter.Math;
import :VulkanBuffer;
import :Types;
namespace Crafter {
export template <typename VertexType>
class Mesh : public Component {
class Mesh {
public:
std::uint32_t vertexCount;
std::uint32_t indexCount;
@ -41,51 +42,46 @@ namespace Crafter {
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) {
}
static Mesh* FromAsset(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);
Mesh(const char* asset) requires(std::same_as<VertexType, Vertex_xf32_yf32_zf32_wf32>) : 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) {
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*3; i+=3) {
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].w = 1.0f;
this->verticies.value[counter].x = verticies[i];
this->verticies.value[counter].y = verticies[i+1];
this->verticies.value[counter].z = verticies[i+2];
this->verticies.value[counter].w = 1.0f;
counter++;
}
memcpy(mesh->indicies.value, asset+(sizeof(std::uint32_t)*2)+(vertexCount*sizeof(float)*3), indexCount*sizeof(std::uint32_t));
memcpy(indicies.value, asset+(sizeof(std::uint32_t)*2)+(vertexCount*sizeof(float)*3), 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
indicies.value[i] = 0;//pad indicies to nearest 64
}
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;
}
static Mesh* FromHeightMapUV(float* heights, uint32_t sizeX, uint32_t sizeZ, float spacing) {
Mesh* mesh = new Mesh(sizeX*sizeZ, ((sizeX-1)*(sizeZ-1))*6);
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) {
const float* verticies = reinterpret_cast<const float*>(asset+sizeof(std::uint32_t)*2);
std::cout << vertexCount << std::endl;
std::cout << indexCount << std::endl;
std::uint32_t counter = 0;
for(std::uint32_t i = 0; i < vertexCount*5; i+=5) {
this->verticies.value[counter].x = verticies[i];
this->verticies.value[counter].y = verticies[i+1];
this->verticies.value[counter].z = verticies[i+2];
this->verticies.value[counter].u = verticies[i+3];
this->verticies.value[counter].v = verticies[i+4];
this->verticies.value[counter].w = 1.0f;
counter++;
}
memcpy(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++) {
indicies.value[i] = 0;//pad indicies to nearest 64
}
}
Mesh(float* heights, uint32_t sizeX, uint32_t sizeZ, float spacing) : vertexCount(sizeX*sizeZ), indexCount(((sizeX-1)*(sizeZ-1))*6), 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) {
for (float x = 0; x < sizeX; x++)
{
for (float z = 0; z < sizeZ; z++)
@ -101,7 +97,7 @@ namespace Crafter {
vertex.w = 1.0f;
vertex.u = uv.x;
vertex.v = uv.y;
mesh->verticies.value[xInt * sizeX + zInt] = vertex;
verticies.value[xInt * sizeX + zInt] = vertex;
}
}
@ -116,16 +112,15 @@ namespace Crafter {
uint32_t index = (x * sizeX + z)*6;
mesh->indicies.value[index] = topRightIndex;
mesh->indicies.value[index + 1] = bottomLeftIndex;
mesh->indicies.value[index + 2] = topLeftIndex;
indicies.value[index] = topRightIndex;
indicies.value[index + 1] = bottomLeftIndex;
indicies.value[index + 2] = topLeftIndex;
mesh->indicies.value[index + 3] = bottomRightIndex;
mesh->indicies.value[index + 4] = bottomLeftIndex;
mesh->indicies.value[index + 5] = topRightIndex;
indicies.value[index + 3] = bottomRightIndex;
indicies.value[index + 4] = bottomLeftIndex;
indicies.value[index + 5] = topRightIndex;
}
}
return mesh;
}
};
}

View file

@ -37,11 +37,11 @@ import Crafter.Math;
namespace Crafter {
export template <typename VertexType>
class MeshShader : public Component {
class MeshShader {
public:
MatrixRowMajor<float, 4, 4, 1> transform;
ComponentRefOwning<Mesh<VertexType>> mesh;
ComponentRefOwning<Camera> camera;
Mesh<VertexType>* mesh;
Camera* camera;
Buffer<MatrixRowMajor<float, 4, 4, 1>> mvp;
std::uint32_t threadCount;
EventListener<void> cameraUpdate;
@ -55,13 +55,13 @@ namespace Crafter {
void WriteDescriptors(VkDescriptorSet* set) {
VkWriteDescriptorSet write[3] = {
vks::initializers::writeDescriptorSet(set[0], VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &mvp.descriptor),
vks::initializers::writeDescriptorSet(set[0], VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, &mesh.component->verticies.descriptor),
vks::initializers::writeDescriptorSet(set[0], VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2, &mesh.component->indicies.descriptor)
vks::initializers::writeDescriptorSet(set[0], VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, &mesh->verticies.descriptor),
vks::initializers::writeDescriptorSet(set[0], VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2, &mesh->indicies.descriptor)
};
vkUpdateDescriptorSets(VulkanDevice::device, 3, &write[0], 0, nullptr);
}
void Update() {
*mvp.value = camera.component->projectionView*transform;
*mvp.value = camera->projectionView*transform;
}
};
}

View file

@ -38,7 +38,7 @@ namespace Crafter {
public:
VkSampler textureSampler;
VkDescriptorImageInfo imageInfo;
ComponentRefOwning<VulkanTexture<PixelType>> texture;
VulkanTexture<PixelType>* texture;
TextureShader(VulkanTexture<PixelType>* texture) : texture(texture) {
VkSamplerCreateInfo samplerInfo{};
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;

View file

@ -47,8 +47,8 @@ int main() {
Camera camera(1.57079633, 16 / 9, 0.01, 512);
camera.Update();
VkCommandBuffer cmd = window.StartInit();
Mesh<VertexUV>* mesh = Mesh<VertexUV>::FromAssetUV(asset.entries[0].data.data());
MeshShader<VertexUV> meshShader(mesh, &camera);
Mesh<VertexUV> mesh = Mesh<VertexUV>(asset.entries[0].data.data());
MeshShader<VertexUV> meshShader(&mesh, &camera);
Asset asset2;
asset2.LoadFull("texture.cras");
VulkanTexture<Pixel_RU8_GU8_BU8_AU8>* txt = VulkanTexture<Pixel_RU8_GU8_BU8_AU8>::FromAsset(asset2.entries[0].data.data(), cmd);
@ -60,25 +60,25 @@ int main() {
});
meshShader.WriteDescriptors(&descriptors.set[0]);
texShader.WriteDescriptors(&descriptors.set[0]);
MeshShader<VertexUV> meshShader2(mesh, &camera);
TextureShader texShader2(txt);
DescriptorSet<MeshVulkanShader, FragmentShader> descriptors2;
meshShader2.WriteDescriptors(&descriptors2.set[0]);
texShader2.WriteDescriptors(&descriptors2.set[0]);
// MeshShader<VertexUV> meshShader2(&mesh, &camera);
// TextureShader texShader2(txt);
// DescriptorSet<MeshVulkanShader, FragmentShader> descriptors2;
// meshShader2.WriteDescriptors(&descriptors2.set[0]);
// texShader2.WriteDescriptors(&descriptors2.set[0]);
meshShader.Update();
meshShader2.Update();
//meshShader2.Update();
window.FinishInit();
EventListener<VkCommandBuffer> listener(&window.onDraw, [&descriptors, &meshShader, &descriptors2, &meshShader2](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);
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipeline);
VulkanDevice::vkCmdDrawMeshTasksEXTProc(cmd, meshShader.threadCount, 1, 1);
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipelineLayout, 0, 2, &descriptors2.set[0], 0, NULL);
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipeline);
VulkanDevice::vkCmdDrawMeshTasksEXTProc(cmd, meshShader2.threadCount, 1, 1);
// vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipelineLayout, 0, 2, &descriptors2.set[0], 0, NULL);
// vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipeline);
// VulkanDevice::vkCmdDrawMeshTasksEXTProc(cmd, meshShader2.threadCount, 1, 1);
});
window.Start();