wip
This commit is contained in:
parent
abc1d7da9f
commit
0c6934023b
4 changed files with 59 additions and 64 deletions
|
|
@ -29,10 +29,11 @@ export module Crafter.Graphics:Mesh;
|
||||||
import Crafter.Component;
|
import Crafter.Component;
|
||||||
import Crafter.Math;
|
import Crafter.Math;
|
||||||
import :VulkanBuffer;
|
import :VulkanBuffer;
|
||||||
|
import :Types;
|
||||||
|
|
||||||
namespace Crafter {
|
namespace Crafter {
|
||||||
export template <typename VertexType>
|
export template <typename VertexType>
|
||||||
class Mesh : public Component {
|
class Mesh {
|
||||||
public:
|
public:
|
||||||
std::uint32_t vertexCount;
|
std::uint32_t vertexCount;
|
||||||
std::uint32_t indexCount;
|
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) {
|
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);
|
const float* verticies = reinterpret_cast<const float*>(asset+sizeof(std::uint32_t)*2);
|
||||||
std::uint32_t counter = 0;
|
std::uint32_t counter = 0;
|
||||||
|
|
||||||
for(std::uint32_t i = 0; i < vertexCount*3; i+=3) {
|
for(std::uint32_t i = 0; i < vertexCount*3; i+=3) {
|
||||||
mesh->verticies.value[counter].x = verticies[i];
|
this->verticies.value[counter].x = verticies[i];
|
||||||
mesh->verticies.value[counter].y = verticies[i+1];
|
this->verticies.value[counter].y = verticies[i+1];
|
||||||
mesh->verticies.value[counter].z = verticies[i+2];
|
this->verticies.value[counter].z = verticies[i+2];
|
||||||
mesh->verticies.value[counter].w = 1.0f;
|
this->verticies.value[counter].w = 1.0f;
|
||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
memcpy(mesh->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
|
|
||||||
}
|
|
||||||
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);
|
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++) {
|
||||||
|
indicies.value[i] = 0;//pad indicies to nearest 64
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
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;
|
std::uint32_t counter = 0;
|
||||||
for(std::uint32_t i = 0; i < vertexCount*5; i+=5) {
|
for(std::uint32_t i = 0; i < vertexCount*5; i+=5) {
|
||||||
mesh->verticies.value[counter].x = verticies[i];
|
this->verticies.value[counter].x = verticies[i];
|
||||||
mesh->verticies.value[counter].y = verticies[i+1];
|
this->verticies.value[counter].y = verticies[i+1];
|
||||||
mesh->verticies.value[counter].z = verticies[i+2];
|
this->verticies.value[counter].z = verticies[i+2];
|
||||||
mesh->verticies.value[counter].u = verticies[i+3];
|
this->verticies.value[counter].u = verticies[i+3];
|
||||||
mesh->verticies.value[counter].v = verticies[i+4];
|
this->verticies.value[counter].v = verticies[i+4];
|
||||||
mesh->verticies.value[counter].w = 1.0f;
|
this->verticies.value[counter].w = 1.0f;
|
||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
memcpy(mesh->indicies.value, asset+(sizeof(std::uint32_t)*2)+(vertexCount*sizeof(float)*5), indexCount*sizeof(std::uint32_t));
|
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++) {
|
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* FromHeightMapUV(float* heights, uint32_t sizeX, uint32_t sizeZ, float spacing) {
|
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) {
|
||||||
Mesh* mesh = new Mesh(sizeX*sizeZ, ((sizeX-1)*(sizeZ-1))*6);
|
|
||||||
for (float x = 0; x < sizeX; x++)
|
for (float x = 0; x < sizeX; x++)
|
||||||
{
|
{
|
||||||
for (float z = 0; z < sizeZ; z++)
|
for (float z = 0; z < sizeZ; z++)
|
||||||
|
|
@ -101,7 +97,7 @@ namespace Crafter {
|
||||||
vertex.w = 1.0f;
|
vertex.w = 1.0f;
|
||||||
vertex.u = uv.x;
|
vertex.u = uv.x;
|
||||||
vertex.v = uv.y;
|
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;
|
uint32_t index = (x * sizeX + z)*6;
|
||||||
|
|
||||||
mesh->indicies.value[index] = topRightIndex;
|
indicies.value[index] = topRightIndex;
|
||||||
mesh->indicies.value[index + 1] = bottomLeftIndex;
|
indicies.value[index + 1] = bottomLeftIndex;
|
||||||
mesh->indicies.value[index + 2] = topLeftIndex;
|
indicies.value[index + 2] = topLeftIndex;
|
||||||
|
|
||||||
mesh->indicies.value[index + 3] = bottomRightIndex;
|
indicies.value[index + 3] = bottomRightIndex;
|
||||||
mesh->indicies.value[index + 4] = bottomLeftIndex;
|
indicies.value[index + 4] = bottomLeftIndex;
|
||||||
mesh->indicies.value[index + 5] = topRightIndex;
|
indicies.value[index + 5] = topRightIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return mesh;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,11 +37,11 @@ import Crafter.Math;
|
||||||
|
|
||||||
namespace Crafter {
|
namespace Crafter {
|
||||||
export template <typename VertexType>
|
export template <typename VertexType>
|
||||||
class MeshShader : public Component {
|
class MeshShader {
|
||||||
public:
|
public:
|
||||||
MatrixRowMajor<float, 4, 4, 1> transform;
|
MatrixRowMajor<float, 4, 4, 1> transform;
|
||||||
ComponentRefOwning<Mesh<VertexType>> mesh;
|
Mesh<VertexType>* mesh;
|
||||||
ComponentRefOwning<Camera> camera;
|
Camera* camera;
|
||||||
Buffer<MatrixRowMajor<float, 4, 4, 1>> mvp;
|
Buffer<MatrixRowMajor<float, 4, 4, 1>> mvp;
|
||||||
std::uint32_t threadCount;
|
std::uint32_t threadCount;
|
||||||
EventListener<void> cameraUpdate;
|
EventListener<void> cameraUpdate;
|
||||||
|
|
@ -55,13 +55,13 @@ namespace Crafter {
|
||||||
void WriteDescriptors(VkDescriptorSet* set) {
|
void WriteDescriptors(VkDescriptorSet* set) {
|
||||||
VkWriteDescriptorSet write[3] = {
|
VkWriteDescriptorSet write[3] = {
|
||||||
vks::initializers::writeDescriptorSet(set[0], VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &mvp.descriptor),
|
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, 1, &mesh->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, 2, &mesh->indicies.descriptor)
|
||||||
};
|
};
|
||||||
vkUpdateDescriptorSets(VulkanDevice::device, 3, &write[0], 0, nullptr);
|
vkUpdateDescriptorSets(VulkanDevice::device, 3, &write[0], 0, nullptr);
|
||||||
}
|
}
|
||||||
void Update() {
|
void Update() {
|
||||||
*mvp.value = camera.component->projectionView*transform;
|
*mvp.value = camera->projectionView*transform;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ namespace Crafter {
|
||||||
public:
|
public:
|
||||||
VkSampler textureSampler;
|
VkSampler textureSampler;
|
||||||
VkDescriptorImageInfo imageInfo;
|
VkDescriptorImageInfo imageInfo;
|
||||||
ComponentRefOwning<VulkanTexture<PixelType>> texture;
|
VulkanTexture<PixelType>* texture;
|
||||||
TextureShader(VulkanTexture<PixelType>* texture) : texture(texture) {
|
TextureShader(VulkanTexture<PixelType>* texture) : texture(texture) {
|
||||||
VkSamplerCreateInfo samplerInfo{};
|
VkSamplerCreateInfo samplerInfo{};
|
||||||
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
||||||
|
|
|
||||||
24
main.cpp
24
main.cpp
|
|
@ -47,8 +47,8 @@ int main() {
|
||||||
Camera camera(1.57079633, 16 / 9, 0.01, 512);
|
Camera camera(1.57079633, 16 / 9, 0.01, 512);
|
||||||
camera.Update();
|
camera.Update();
|
||||||
VkCommandBuffer cmd = window.StartInit();
|
VkCommandBuffer cmd = window.StartInit();
|
||||||
Mesh<VertexUV>* mesh = Mesh<VertexUV>::FromAssetUV(asset.entries[0].data.data());
|
Mesh<VertexUV> mesh = Mesh<VertexUV>(asset.entries[0].data.data());
|
||||||
MeshShader<VertexUV> meshShader(mesh, &camera);
|
MeshShader<VertexUV> meshShader(&mesh, &camera);
|
||||||
Asset asset2;
|
Asset asset2;
|
||||||
asset2.LoadFull("texture.cras");
|
asset2.LoadFull("texture.cras");
|
||||||
VulkanTexture<Pixel_RU8_GU8_BU8_AU8>* txt = VulkanTexture<Pixel_RU8_GU8_BU8_AU8>::FromAsset(asset2.entries[0].data.data(), cmd);
|
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]);
|
meshShader.WriteDescriptors(&descriptors.set[0]);
|
||||||
texShader.WriteDescriptors(&descriptors.set[0]);
|
texShader.WriteDescriptors(&descriptors.set[0]);
|
||||||
MeshShader<VertexUV> meshShader2(mesh, &camera);
|
// MeshShader<VertexUV> meshShader2(&mesh, &camera);
|
||||||
TextureShader texShader2(txt);
|
// TextureShader texShader2(txt);
|
||||||
DescriptorSet<MeshVulkanShader, FragmentShader> descriptors2;
|
// DescriptorSet<MeshVulkanShader, FragmentShader> descriptors2;
|
||||||
meshShader2.WriteDescriptors(&descriptors2.set[0]);
|
// meshShader2.WriteDescriptors(&descriptors2.set[0]);
|
||||||
texShader2.WriteDescriptors(&descriptors2.set[0]);
|
// texShader2.WriteDescriptors(&descriptors2.set[0]);
|
||||||
|
|
||||||
meshShader.Update();
|
meshShader.Update();
|
||||||
meshShader2.Update();
|
//meshShader2.Update();
|
||||||
|
|
||||||
window.FinishInit();
|
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);
|
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);
|
||||||
VulkanDevice::vkCmdDrawMeshTasksEXTProc(cmd, meshShader.threadCount, 1, 1);
|
VulkanDevice::vkCmdDrawMeshTasksEXTProc(cmd, meshShader.threadCount, 1, 1);
|
||||||
|
|
||||||
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipelineLayout, 0, 2, &descriptors2.set[0], 0, NULL);
|
// 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);
|
// vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipeline);
|
||||||
VulkanDevice::vkCmdDrawMeshTasksEXTProc(cmd, meshShader2.threadCount, 1, 1);
|
// VulkanDevice::vkCmdDrawMeshTasksEXTProc(cmd, meshShader2.threadCount, 1, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
window.Start();
|
window.Start();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue