Orange/src/Apps/Tools/CubeTest.cpp

679 lines
22 KiB
C++
Raw Normal View History

2022-08-07 00:15:47 +01:00
#include <Orange/Core/Array.h>
#include <Orange/Core/Vector.h>
2022-08-05 01:01:41 +01:00
#include "Orange/Core/Span.h"
2022-06-19 04:33:41 +01:00
#include <Orange/Core/Result.h>
2022-08-05 01:01:41 +01:00
#include <Orange/Core/FileSystem.h>
2022-08-07 00:15:47 +01:00
#include <Orange/Math/Vector.h>
2022-08-12 10:05:58 +01:00
#include <Orange/Math/Matrix.h>
2022-08-12 21:18:11 +01:00
#include <Orange/Math/Transformation.h>
2022-08-05 01:01:41 +01:00
#include <Orange/Core/Parse.h>
2022-08-12 13:26:36 +01:00
#include <Orange/Core/Variant.h>
2022-06-19 04:33:41 +01:00
#include <Orange/Render/Window.h>
2022-08-04 03:00:54 +01:00
#include <Orange/Render/RenderContext.h>
#include <Orange/Render/Swapchain.h>
2022-08-12 20:08:28 +01:00
#include <Orange/Render/VulkanHelpers.h>
2022-06-19 04:33:41 +01:00
2022-08-12 20:08:28 +01:00
#include <vs_Mesh.h>
2022-08-07 06:09:59 +01:00
#include <fs_DebugVertColor.h>
2022-06-19 04:33:41 +01:00
using namespace orange;
2022-08-07 00:59:25 +01:00
struct AABB
2022-08-05 01:01:41 +01:00
{
2022-08-07 00:59:25 +01:00
vec3 min;
vec3 max;
void Extend(vec3 pos)
{
min = Min(pos, min);
max = Max(pos, max);
}
};
2022-08-12 13:03:00 +01:00
enum class MeshVertexType
{
Static,
Skinned,
};
2022-08-12 20:08:28 +01:00
#ifndef offsetof2
#define offsetof2(type, member) size_t(uintptr_t(&((type*)0)->member))
#endif
2022-08-12 13:26:36 +01:00
struct StaticVertex
2022-08-07 00:59:25 +01:00
{
vec3 pos;
vec2 uv;
vec3 normal;
2022-08-12 20:08:28 +01:00
//vec3 tangent;
static constexpr VkVertexInputAttributeDescription Attributes[] =
{
{ .location = 0, .binding = 0, .format = VK_FORMAT_R32G32B32_SFLOAT, .offset = uint32_t(offsetof2(StaticVertex, pos)) },
{ .location = 1, .binding = 0, .format = VK_FORMAT_R32G32_SFLOAT, .offset = uint32_t(offsetof2(StaticVertex, uv)) },
{ .location = 2, .binding = 0, .format = VK_FORMAT_R32G32B32_SFLOAT, .offset = uint32_t(offsetof2(StaticVertex, normal)) },
};
2022-08-07 00:59:25 +01:00
2022-08-12 13:26:36 +01:00
bool operator == (const StaticVertex& other) const
2022-08-07 00:59:25 +01:00
{
2022-08-12 13:03:00 +01:00
return pos == other.pos &&
uv == other.uv &&
2022-08-12 20:08:28 +01:00
normal == other.normal;// &&
//tangent == other.tangent;
2022-08-07 00:59:25 +01:00
}
};
2022-08-12 13:26:36 +01:00
struct SkinnedVertex : public StaticVertex
2022-08-12 13:03:00 +01:00
{
static constexpr uint32_t MaxVertexWeights = 4;
bool operator == (const SkinnedVertex& other) const
{
return pos == other.pos &&
uv == other.uv &&
normal == other.normal &&
2022-08-12 20:08:28 +01:00
//tangent == other.tangent &&
2022-08-12 13:03:00 +01:00
boneIndices == other.boneIndices &&
boneWeights == other.boneWeights;
}
Array<uint8_t, MaxVertexWeights> boneIndices;
Array<uint8_t, MaxVertexWeights> boneWeights;
2022-08-12 13:26:36 +01:00
};
2022-08-12 13:03:00 +01:00
2022-08-12 13:28:36 +01:00
struct MeshVertexData
2022-08-07 00:59:25 +01:00
{
2022-08-12 13:28:36 +01:00
MeshVertexData(MeshVertexType type)
2022-08-12 13:26:36 +01:00
: vertexType(type)
{
2022-08-12 20:08:28 +01:00
switch(vertexType)
2022-08-12 13:26:36 +01:00
{
case MeshVertexType::Static:
vertices.Construct<Vector<StaticVertex>>();
break;
case MeshVertexType::Skinned:
vertices.Construct<Vector<SkinnedVertex>>();
break;
}
}
Vector<StaticVertex>& GetStaticVertices()
2022-08-12 13:03:00 +01:00
{
2022-08-12 13:26:36 +01:00
Assert(vertexType == MeshVertexType::Static);
return vertices.Get<Vector<StaticVertex>>();
}
Vector<SkinnedVertex>& GetSkinnedVertices()
{
Assert(vertexType == MeshVertexType::Skinned);
return vertices.Get<Vector<SkinnedVertex>>();
}
2022-08-12 20:08:28 +01:00
BufferView View()
{
switch(vertexType)
{
case MeshVertexType::Static:
return GetStaticVertices();
break;
case MeshVertexType::Skinned:
return GetSkinnedVertices();
break;
default:
return BufferView{ nullptr, 0 };
}
}
uint32_t VertexCount() const
{
// Type doesn't matter here, just want to grab m_size.
return uint32_t(vertices.Get<Vector<uint8_t>>().Size());
}
2022-08-12 13:26:36 +01:00
MeshVertexType vertexType;
Variant<Vector<StaticVertex>, Vector<SkinnedVertex>> vertices;
2022-08-12 13:28:36 +01:00
};
struct MeshData
{
MeshData(MeshVertexType type)
: vertexData{ type } {}
MeshVertexData vertexData;
2022-08-07 00:59:25 +01:00
Vector<uint16_t> indices;
AABB bounds;
};
Result<MeshData> ParseOBJ(StringView buffer)
{
2022-08-12 13:26:36 +01:00
MeshData data{ MeshVertexType::Static };
2022-08-07 00:59:25 +01:00
Vector<vec3> positions;
Vector<vec2> uvs;
Vector<vec3> normals;
2022-08-05 03:26:36 +01:00
const char* obj = buffer.data;
const char* end = buffer.data + buffer.size;
while (obj != end)
2022-08-05 01:01:41 +01:00
{
2022-08-07 00:15:47 +01:00
SmallVector<char, 8> element;
2022-08-05 03:26:36 +01:00
stream::ReadString(obj, end, " #\n", element);
2022-08-05 01:01:41 +01:00
2022-08-07 00:59:25 +01:00
if (element == "v" || element == "vt" || element == "vn")
2022-08-05 01:01:41 +01:00
{
float vtx[3]{};
for (int i = 0; i < 3; i++)
{
2022-08-05 03:26:36 +01:00
stream::ConsumeSpace(obj, end);
if (auto r_float = stream::Parse<float>(obj, end))
vtx[i] = *r_float;
2022-08-05 01:01:41 +01:00
}
2022-08-07 00:59:25 +01:00
if (element == "v")
positions.EmplaceBack(vtx[0], vtx[1], vtx[2]);
else if (element == "vt")
uvs.EmplaceBack(vtx[0], vtx[1]);
2022-08-12 20:08:28 +01:00
else if (element == "vn")
normals.EmplaceBack(vtx[0], vtx[1], vtx[2]);
2022-08-05 01:01:41 +01:00
}
else if (element == "g" || element == "o")
{
2022-08-05 03:26:36 +01:00
stream::ConsumeSpace(obj, end);
2022-08-07 00:15:47 +01:00
SmallVector<char, 32> name;
2022-08-05 03:26:36 +01:00
stream::ReadString(obj, end, " #\n", name);
2022-08-05 01:01:41 +01:00
name.PushBack('\0');
if (element == "g")
log::info("Group name: %s", name.Data());
else
log::info("Object name: %s", name.Data());
}
else if (element == "f")
{
int32_t indices[3][3]{};
for (int i = 0; i < 3; i++)
{
2022-08-05 03:26:36 +01:00
stream::ConsumeSpace(obj, end);
2022-08-05 01:01:41 +01:00
for (int j = 0; j < 3; j++)
{
indices[i][j] = -1;
2022-08-05 03:26:36 +01:00
if (j == 0 || stream::Consume(obj, end, "/"))
2022-08-05 01:01:41 +01:00
{
2022-08-05 03:26:36 +01:00
if (auto r_int = stream::Parse<uint32_t>(obj, end))
2022-08-12 21:18:11 +01:00
indices[i][j] = *r_int - 1; // OBJ indexing starts at one.
2022-08-05 01:01:41 +01:00
}
}
}
2022-08-07 00:59:25 +01:00
for (int i = 0; i < 3; i++)
{
2022-08-12 13:26:36 +01:00
StaticVertex vertex =
2022-08-07 00:59:25 +01:00
{
.pos = indices[i][0] != -1 ? positions[indices[i][0]] : vec3{},
.uv = indices[i][1] != -1 ? uvs [indices[i][1]] : vec2{},
.normal = indices[i][2] != -1 ? normals [indices[i][2]] : vec3{},
};
2022-08-12 13:28:36 +01:00
auto& vertices = data.vertexData.GetStaticVertices();
2022-08-12 13:26:36 +01:00
size_t vertexIdx = vertices.FindIdx(vertex);
if (vertexIdx == vertices.InvalidIdx)
2022-08-07 00:59:25 +01:00
{
data.bounds.Extend(vertex.pos);
2022-08-12 13:26:36 +01:00
vertexIdx = vertices.PushBack(vertex);
2022-08-07 00:59:25 +01:00
}
Assert(vertexIdx < UINT16_MAX);
data.indices.PushBack(uint16_t(vertexIdx));
}
2022-08-05 01:01:41 +01:00
}
2022-08-05 03:26:36 +01:00
else if (!element.Empty())
2022-08-05 01:01:41 +01:00
{
element.PushBack('\0');
log::info("Unknown element: %s", element.Data());
}
2022-08-05 03:26:36 +01:00
stream::AdvancePast(obj, end, "\n");
};
2022-08-05 01:01:41 +01:00
2022-08-07 00:59:25 +01:00
return Result<MeshData>::Success(data);
2022-08-05 01:01:41 +01:00
}
2022-06-19 04:33:41 +01:00
int main(int argc, char** argv)
{
(void)argc; (void)argv;
2022-08-04 18:23:08 +01:00
auto r_window = Window::Create();
2022-06-19 04:33:41 +01:00
if (!r_window)
return 1;
2022-08-04 18:23:08 +01:00
auto r_renderContext = RenderContext::Create("Cube Test");
2022-08-04 03:00:54 +01:00
if (!r_renderContext)
2022-08-02 22:27:01 +01:00
return 1;
2022-08-04 18:23:08 +01:00
auto r_surface = r_window->CreateSurface(r_renderContext->Instance());
2022-08-04 03:00:54 +01:00
if (!r_surface)
return 1;
2022-08-03 05:33:52 +01:00
2022-08-04 18:23:08 +01:00
auto r_swapchain = Swapchain::Create(*r_renderContext, *r_surface);
2022-08-04 03:00:54 +01:00
if (!r_swapchain)
2022-08-02 22:27:01 +01:00
return 1;
2022-06-19 04:33:41 +01:00
2022-08-12 20:08:28 +01:00
//auto r_objData = fs::OpenFileIntoTextBuffer("/home/joshua/chair.obj");
auto r_objData = fs::OpenFileIntoTextBuffer("/home/joshua/cube.obj");
2022-08-05 01:01:41 +01:00
if (!r_objData)
return 1;
2022-08-07 00:59:25 +01:00
auto r_mesh = ParseOBJ(*r_objData);
2022-08-05 01:01:41 +01:00
2022-08-12 20:08:28 +01:00
auto r_vs = r_renderContext->CreateShader(vs_Mesh);
2022-08-07 06:09:59 +01:00
if (!r_vs) return 1;
auto r_fs = r_renderContext->CreateShader(fs_DebugVertColor);
if (!r_fs) return 1;
2022-08-12 20:08:28 +01:00
//
VkSamplerCreateInfo samplerInfo =
{
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
.magFilter = VK_FILTER_LINEAR,
.minFilter = VK_FILTER_LINEAR,
.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR,
.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.mipLodBias = 0.0f,
.anisotropyEnable = VK_TRUE,
.maxAnisotropy = 16.0f,
.minLod = -FLT_MAX,
.maxLod = FLT_MAX,
};
VkSampler sampler = VK_NULL_HANDLE;
vkCreateSampler(r_renderContext->Device(), &samplerInfo, nullptr, &sampler);
constexpr uint32_t MaxBindlessResources = 32768;
VkDescriptorPoolSize bindlessPoolSizes[] =
{
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MaxBindlessResources }
};
VkDescriptorPoolCreateInfo poolInfo =
{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.flags = VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT,
.maxSets = MaxBindlessResources * Size(bindlessPoolSizes),
.poolSizeCount = uint32_t(Size(bindlessPoolSizes)),
.pPoolSizes = bindlessPoolSizes,
};
VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
vkCreateDescriptorPool(r_renderContext->Device(), &poolInfo, nullptr, &descriptorPool);
VkDescriptorSetLayoutBinding layoutBindings[] =
{
{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = MaxBindlessResources,
.stageFlags = VK_SHADER_STAGE_ALL,
},
{
.binding = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
.pImmutableSamplers = &sampler,
},
{
2022-08-12 21:18:11 +01:00
.binding = 2,
2022-08-12 20:08:28 +01:00
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.descriptorCount = MaxBindlessResources,
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
},
};
constexpr VkDescriptorBindingFlags bindingFlags[] =
{
0,
0,
VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT,
};
Assert(Size(bindingFlags) == Size(layoutBindings));
VkDescriptorSetLayoutBindingFlagsCreateInfo layoutBindingFlagsInfo =
{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT,
.bindingCount = Size(bindingFlags),
.pBindingFlags = bindingFlags,
};
VkDescriptorSetLayoutCreateInfo layoutInfo =
{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pNext = &layoutBindingFlagsInfo,
.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT,
.bindingCount = Size(layoutBindings),
.pBindings = layoutBindings,
};
VkDescriptorSetLayout descriptorSetLayout = VK_NULL_HANDLE;
vkCreateDescriptorSetLayout(r_renderContext->Device(), &layoutInfo, nullptr, &descriptorSetLayout);
VkDescriptorSetVariableDescriptorCountAllocateInfoEXT descriptorCountAllocInfo =
{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT,
.descriptorSetCount = 1,
.pDescriptorCounts = &MaxBindlessResources,
};
VkDescriptorSetAllocateInfo descriptorSetAllocInfo =
{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.pNext = &descriptorCountAllocInfo,
.descriptorPool = descriptorPool,
.descriptorSetCount = 1,
.pSetLayouts = &descriptorSetLayout,
};
VkDescriptorSet descriptorSet = VK_NULL_HANDLE;
vkAllocateDescriptorSets(r_renderContext->Device(), &descriptorSetAllocInfo, &descriptorSet);
//
2022-08-07 06:09:59 +01:00
VkPipelineShaderStageCreateInfo stages[] =
{
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_VERTEX_BIT,
.module = *r_vs,
.pName = "main",
},
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
.module = *r_fs,
.pName = "main",
},
};
Array<VkDynamicState, 2> dynamicStates =
{
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR,
};
VkPipelineDynamicStateCreateInfo dynamicStateInfo =
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
.dynamicStateCount = uint32_t(dynamicStates.Size()),
.pDynamicStates = dynamicStates.Data(),
};
2022-08-12 20:08:28 +01:00
VkVertexInputBindingDescription inputBindingDescription =
{
.binding = 0,
.stride = sizeof(StaticVertex),
.inputRate = VK_VERTEX_INPUT_RATE_VERTEX,
};
2022-08-07 06:09:59 +01:00
VkPipelineVertexInputStateCreateInfo vertexInputInfo =
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
2022-08-12 20:08:28 +01:00
.vertexBindingDescriptionCount = 1u,
.pVertexBindingDescriptions = &inputBindingDescription,
.vertexAttributeDescriptionCount = Size(StaticVertex::Attributes),
.pVertexAttributeDescriptions = StaticVertex::Attributes,
2022-08-07 06:09:59 +01:00
};
VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo =
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
.primitiveRestartEnable = VK_FALSE,
};
VkPipelineViewportStateCreateInfo viewportStateInfo =
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
.viewportCount = 1,
.scissorCount = 1,
};
VkPipelineRasterizationStateCreateInfo rasterizationInfo =
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
2022-08-12 13:34:04 +01:00
.cullMode = VK_CULL_MODE_NONE, //VK_CULL_MODE_BACK_BIT,
2022-08-12 21:18:11 +01:00
.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
2022-08-12 20:08:28 +01:00
.lineWidth = 1.0f,
2022-08-07 06:09:59 +01:00
};
VkPipelineMultisampleStateCreateInfo multisampleInfo =
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
};
2022-08-12 21:18:11 +01:00
VkPipelineDepthStencilStateCreateInfo depthStencilInfo =
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
.depthTestEnable = VK_TRUE,
.depthWriteEnable = VK_TRUE,
.depthCompareOp = VK_COMPARE_OP_LESS,
.minDepthBounds = 0.0f,
.maxDepthBounds = 1.0f,
};
2022-08-07 06:09:59 +01:00
VkPipelineColorBlendAttachmentState attachmentBlendState =
{
.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT,
};
VkPipelineColorBlendStateCreateInfo colorBlendStateInfo =
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
.attachmentCount = 1,
.pAttachments = &attachmentBlendState,
};
VkPipelineLayoutCreateInfo pipelineLayoutInfo =
{
2022-08-12 20:08:28 +01:00
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = 1,
.pSetLayouts = &descriptorSetLayout
2022-08-07 06:09:59 +01:00
};
VkPipelineLayout pipelineLayout = VK_NULL_HANDLE;
vkCreatePipelineLayout(r_renderContext->Device(), &pipelineLayoutInfo, nullptr, &pipelineLayout);
2022-08-12 20:08:28 +01:00
VkFormat format = FormatToSrgbFormat(r_swapchain->Format());
2022-08-07 06:09:59 +01:00
VkPipelineRenderingCreateInfo renderingInfo =
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO,
.viewMask = 0u,
.colorAttachmentCount = 1u,
.pColorAttachmentFormats = &format,
};
VkGraphicsPipelineCreateInfo pipelineInfo =
{
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
.pNext = &renderingInfo,
.stageCount = Size(stages),
.pStages = stages,
.pVertexInputState = &vertexInputInfo,
.pInputAssemblyState = &inputAssemblyInfo,
.pViewportState = &viewportStateInfo,
.pRasterizationState = &rasterizationInfo,
.pMultisampleState = &multisampleInfo,
2022-08-12 21:18:11 +01:00
.pDepthStencilState = &depthStencilInfo,
2022-08-07 06:09:59 +01:00
.pColorBlendState = &colorBlendStateInfo,
.pDynamicState = &dynamicStateInfo,
.layout = pipelineLayout,
};
VkPipeline pipeline = VK_NULL_HANDLE;
if (vkCreateGraphicsPipelines(r_renderContext->Device(), VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &pipeline) != VK_SUCCESS)
{
log::err("Blah");
return 1;
}
2022-08-12 20:08:28 +01:00
auto r_buffer = r_renderContext->CreateBuffer(256 * 1024 * 1024);
if (!r_buffer)
return 1;
VkBufferViewCreateInfo bufferViewInfo =
{
.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO,
.buffer = r_buffer->buffer,
.format = VK_FORMAT_R8G8B8A8_UNORM,
.offset = 0,
.range = r_buffer->size,
};
VkBufferView bufferView = VK_NULL_HANDLE;
vkCreateBufferView(r_renderContext->Device(), &bufferViewInfo, nullptr, &bufferView);
2022-08-12 21:18:11 +01:00
struct UniformData
{
mat4 projection;
mat4 view;
};
UniformData uniformData
{
.projection = perspective(Degree(90.0f), 16.0f / 9.0f, 0.01f, 1024.0f),
.view = translate(vec3{0.0f, 0.0f, -4.0f}),
};
auto pooler = BufferPooler{ *r_buffer };
auto meshData = r_mesh->vertexData.View();
auto vertexSlice = *pooler.AllocSlice(meshData.size);
auto indexSlice = *pooler.AllocSlice(r_mesh->indices.Size() * sizeof(uint16_t));
auto uniformSlice = *pooler.AllocSlice(sizeof(UniformData));
2022-08-12 20:08:28 +01:00
VkDescriptorBufferInfo bufferInfo =
{
.buffer = r_buffer->buffer,
2022-08-12 21:18:11 +01:00
.offset = uniformSlice.offset,
.range = uniformSlice.size,
2022-08-12 20:08:28 +01:00
};
VkWriteDescriptorSet writeDescriptorSet[] =
{
{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = descriptorSet,
.dstBinding = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.pBufferInfo = &bufferInfo,
},
};
vkUpdateDescriptorSets(r_renderContext->Device(), Size(writeDescriptorSet), writeDescriptorSet, 0, nullptr);
meshData.Copy ((uint8_t*)(r_buffer->ptr) + vertexSlice.offset);
r_mesh->indices.Copy((uint8_t*)(r_buffer->ptr) + indexSlice.offset);
2022-08-12 21:18:11 +01:00
memcpy((uint8_t*)(r_buffer->ptr) + uniformSlice.offset, &uniformData, sizeof(uniformData));
2022-08-04 22:18:11 +01:00
while (r_window->Update())
{
2022-08-12 13:34:04 +01:00
VkCommandBuffer cmdBuf = r_swapchain->CommandBuffer();
r_renderContext->BeginCommandBuffer(cmdBuf);
2022-08-04 22:18:11 +01:00
{
2022-08-07 06:09:59 +01:00
VkViewport viewport =
{
.x = 0.0f,
.y = 0.0f,
.width = float(r_swapchain->Extent().width),
.height = float(r_swapchain->Extent().height),
.minDepth = 0.0f,
.maxDepth = 1.0f,
};
VkRect2D scissor =
{
.offset = {0u, 0u},
.extent = r_swapchain->Extent(),
};
2022-08-12 13:34:04 +01:00
vkCmdSetViewport(cmdBuf, 0, 1, &viewport);
vkCmdSetScissor(cmdBuf, 0, 1, &scissor);
vkCmdBindPipeline(cmdBuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
2022-08-12 20:08:28 +01:00
vkCmdBindDescriptorSets(cmdBuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
vkCmdBindVertexBuffers2(cmdBuf, 0, 1, &vertexSlice.buffer, &vertexSlice.offset, &vertexSlice.size, nullptr);
vkCmdBindIndexBuffer(cmdBuf, r_buffer->buffer, indexSlice.offset, VK_INDEX_TYPE_UINT16);
const VkImageMemoryBarrier undefinedToColorBarrier =
{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.image = r_swapchain->Image(),
.subresourceRange = FirstMipSubresourceRange,
};
vkCmdPipelineBarrier(
cmdBuf,
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
0,
0, nullptr,
0, nullptr,
1, &undefinedToColorBarrier);
2022-08-07 06:09:59 +01:00
2022-08-04 22:18:11 +01:00
const VkRenderingAttachmentInfoKHR attachmentInfo =
{
2022-08-07 01:07:56 +01:00
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,
.imageView = r_swapchain->ImageView(),
2022-08-04 22:18:11 +01:00
.imageLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL_KHR,
2022-08-07 01:07:56 +01:00
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.clearValue = { .color = { .float32 = { 1.0f, 0.5f, 0.0f, 1.0f } } },
2022-08-04 22:18:11 +01:00
};
const VkRenderingInfo renderInfo =
{
.sType = VK_STRUCTURE_TYPE_RENDERING_INFO,
.renderArea = { {}, r_swapchain->Extent() },
.layerCount = 1,
.colorAttachmentCount = 1,
.pColorAttachments = &attachmentInfo,
};
2022-08-12 13:34:04 +01:00
vkCmdBeginRendering(cmdBuf, &renderInfo);
2022-08-12 21:18:11 +01:00
vkCmdDrawIndexed(cmdBuf, r_mesh->indices.Size(), 1, 0, 0, 0);
2022-08-12 13:34:04 +01:00
vkCmdEndRendering(cmdBuf);
2022-08-12 20:08:28 +01:00
const VkImageMemoryBarrier colorToPresentBarrier =
{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
.image = r_swapchain->Image(),
.subresourceRange = FirstMipSubresourceRange,
};
vkCmdPipelineBarrier(
cmdBuf,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
0,
0, nullptr,
0, nullptr,
1, &colorToPresentBarrier);
2022-08-04 22:18:11 +01:00
}
2022-08-12 13:34:04 +01:00
r_renderContext->EndCommandBuffer(cmdBuf);
2022-08-04 22:18:11 +01:00
r_swapchain->Present();
}
2022-06-19 04:33:41 +01:00
return 0;
}