2022-08-07 00:15:47 +01:00
|
|
|
#include <Orange/Core/Array.h>
|
2022-08-05 01:01:41 +01:00
|
|
|
#include <Orange/Core/FileSystem.h>
|
2022-08-13 17:22:19 +01:00
|
|
|
#include <Orange/Core/Result.h>
|
|
|
|
#include <Orange/Core/Span.h>
|
|
|
|
#include <Orange/Core/Time.h>
|
|
|
|
#include <Orange/Core/Variant.h>
|
|
|
|
#include <Orange/Core/Vector.h>
|
|
|
|
#include <Orange/Graphics/Camera.h>
|
|
|
|
#include <Orange/Graphics/Mesh.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-13 17:22:19 +01:00
|
|
|
#include <Orange/Math/Vector.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-08-13 17:22:19 +01:00
|
|
|
#include <Orange/Render/Window.h>
|
2022-08-13 12:07:07 +01:00
|
|
|
|
2022-08-12 20:08:28 +01:00
|
|
|
#include <vs_Mesh.h>
|
2022-08-13 17:22:19 +01:00
|
|
|
#include <vs_Fullscreen.h>
|
2022-08-07 06:09:59 +01:00
|
|
|
#include <fs_DebugVertColor.h>
|
2022-08-13 21:41:19 +01:00
|
|
|
#include <fs_Mesh.h>
|
2022-08-13 19:09:54 +01:00
|
|
|
#include <fs_SkyGradient.h>
|
|
|
|
#include <fs_Red.h>
|
2022-08-07 06:09:59 +01:00
|
|
|
|
2022-08-13 21:41:19 +01:00
|
|
|
#include <spng.h>
|
|
|
|
|
2022-06-19 04:33:41 +01:00
|
|
|
using namespace orange;
|
|
|
|
|
2022-08-13 19:09:54 +01:00
|
|
|
struct MiniPipelineInfo
|
|
|
|
{
|
|
|
|
VkShaderModule vs;
|
|
|
|
VkShaderModule fs;
|
|
|
|
|
|
|
|
VkPipelineLayout pipelineLayout;
|
|
|
|
|
|
|
|
VkFormat colorFormat;
|
|
|
|
VkFormat depthFormat;
|
|
|
|
|
|
|
|
bool enableDepthTest;
|
|
|
|
bool enableDepthWrites;
|
|
|
|
|
|
|
|
Span<const VkVertexInputAttributeDescription> vertexAttributes;
|
|
|
|
};
|
|
|
|
|
|
|
|
static VulkanResult<VkPipeline> CreateGraphicsPipeline(VkDevice device, const MiniPipelineInfo& info)
|
|
|
|
{
|
|
|
|
VkPipelineShaderStageCreateInfo stages[] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
|
|
|
.stage = VK_SHADER_STAGE_VERTEX_BIT,
|
|
|
|
.module = info.vs,
|
|
|
|
.pName = "main",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
|
|
|
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
|
|
.module = info.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,
|
2022-08-25 07:27:11 +01:00
|
|
|
.dynamicStateCount = uint32_t(dynamicStates.size()),
|
|
|
|
.pDynamicStates = dynamicStates.data(),
|
2022-08-13 19:09:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
VkVertexInputBindingDescription inputBindingDescription =
|
|
|
|
{
|
|
|
|
.binding = 0,
|
|
|
|
.stride = sizeof(StaticVertex),
|
|
|
|
.inputRate = VK_VERTEX_INPUT_RATE_VERTEX,
|
|
|
|
};
|
|
|
|
|
|
|
|
VkPipelineVertexInputStateCreateInfo vertexInputInfo =
|
|
|
|
{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
|
|
|
|
.vertexBindingDescriptionCount = info.vertexAttributes.size ? 1u : 0u,
|
|
|
|
.pVertexBindingDescriptions = &inputBindingDescription,
|
|
|
|
.vertexAttributeDescriptionCount = uint32_t(info.vertexAttributes.size),
|
|
|
|
.pVertexAttributeDescriptions = info.vertexAttributes.data,
|
|
|
|
};
|
|
|
|
|
|
|
|
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-13 21:41:19 +01:00
|
|
|
.cullMode = VK_CULL_MODE_BACK_BIT,
|
2022-08-13 19:09:54 +01:00
|
|
|
.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
|
|
|
|
.lineWidth = 1.0f,
|
|
|
|
};
|
|
|
|
|
|
|
|
VkPipelineMultisampleStateCreateInfo multisampleInfo =
|
|
|
|
{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
|
|
|
|
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
|
|
|
|
};
|
|
|
|
|
|
|
|
VkPipelineDepthStencilStateCreateInfo depthStencilInfo =
|
|
|
|
{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
|
|
|
|
.depthTestEnable = info.enableDepthTest,
|
|
|
|
.depthWriteEnable = info.enableDepthWrites,
|
|
|
|
.depthCompareOp = VK_COMPARE_OP_LESS,
|
|
|
|
.minDepthBounds = 0.0f,
|
|
|
|
.maxDepthBounds = 1.0f,
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
VkPipelineRenderingCreateInfo renderingInfo =
|
|
|
|
{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO,
|
|
|
|
.viewMask = 0u,
|
|
|
|
.colorAttachmentCount = 1u,
|
|
|
|
.pColorAttachmentFormats = &info.colorFormat,
|
|
|
|
.depthAttachmentFormat = info.depthFormat,
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
.pDepthStencilState = &depthStencilInfo,
|
|
|
|
.pColorBlendState = &colorBlendStateInfo,
|
|
|
|
.pDynamicState = &dynamicStateInfo,
|
|
|
|
.layout = info.pipelineLayout,
|
|
|
|
};
|
|
|
|
|
|
|
|
VkPipeline pipeline = VK_NULL_HANDLE;
|
|
|
|
VkResult res = VK_SUCCESS;
|
|
|
|
if ((res = vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &pipeline)) != VK_SUCCESS)
|
|
|
|
return VulkanResult<VkPipeline>::Error(res);
|
|
|
|
|
|
|
|
return VulkanResult<VkPipeline>::Success(pipeline);
|
|
|
|
}
|
|
|
|
|
2022-08-15 22:51:01 +01:00
|
|
|
struct alignas(16) Mesh
|
|
|
|
{
|
|
|
|
uint32_t textureIdx;
|
|
|
|
uint32_t vertexOffset; // in sizeof(Vertex)
|
|
|
|
uint32_t indexOffset; // in sizeof(IndexType)
|
|
|
|
uint32_t indexCount;
|
|
|
|
AABB bounds;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct alignas(16) Renderable
|
|
|
|
{
|
|
|
|
mat4 transform;
|
|
|
|
uint32_t meshIdx;
|
|
|
|
vec3 color; // should replace with something more interesting lmao
|
|
|
|
};
|
|
|
|
|
|
|
|
// look at moving me if binary is big
|
|
|
|
SmallVector<Mesh, 1024> g_meshes;
|
|
|
|
SmallVector<Renderable, 1024> g_renderables;
|
|
|
|
|
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-13 21:41:19 +01:00
|
|
|
//auto r_objData = fs::OpenFileIntoTextBuffer("/home/joshua/gman.obj");
|
2022-08-13 07:18:10 +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-13 21:41:19 +01:00
|
|
|
auto r_buffer = r_renderContext->CreateBuffer(256 * 1024 * 1024);
|
|
|
|
if (!r_buffer)
|
|
|
|
return 1;
|
|
|
|
auto pooler = MemoryPool{ *r_buffer };
|
|
|
|
|
2022-08-13 19:09:54 +01:00
|
|
|
auto r_vsMesh = r_renderContext->CreateShader(vs_Mesh);
|
|
|
|
if (!r_vsMesh) return 1;
|
|
|
|
auto r_vsFullscreen = r_renderContext->CreateShader(vs_Fullscreen);
|
|
|
|
if (!r_vsFullscreen) return 1;
|
2022-08-13 21:41:19 +01:00
|
|
|
auto r_fsMesh = r_renderContext->CreateShader(fs_Mesh);
|
2022-08-13 19:09:54 +01:00
|
|
|
if (!r_fsMesh) return 1;
|
|
|
|
auto r_fsRed = r_renderContext->CreateShader(fs_Red);
|
|
|
|
if (!r_fsRed) return 1;
|
|
|
|
auto r_fsSky = r_renderContext->CreateShader(fs_SkyGradient);
|
|
|
|
if (!r_fsSky) return 1;
|
2022-08-07 06:09:59 +01:00
|
|
|
|
2022-08-15 22:51:01 +01:00
|
|
|
auto r_mesh = ParseOBJ(*r_objData);
|
|
|
|
|
|
|
|
auto newVertexSlice = *pooler.AllocSlice(sizeof(StaticVertex) * r_mesh->vertexData.VertexCount(), sizeof(StaticVertex));
|
2022-08-25 07:27:11 +01:00
|
|
|
auto newIndexSlice = *pooler.AllocSlice(sizeof(IndexType) * r_mesh->indices.size(), sizeof(IndexType));
|
|
|
|
r_mesh->vertexData.GetStaticVertices().copy((uint8_t*)(r_buffer->ptr) + newVertexSlice.offset);
|
|
|
|
r_mesh->indices.copy ((uint8_t*)(r_buffer->ptr) + newIndexSlice.offset);
|
2022-08-15 22:51:01 +01:00
|
|
|
|
2022-08-25 07:27:11 +01:00
|
|
|
uint32_t meshIdx = uint32_t(g_meshes.emplace_back(Mesh
|
2022-08-15 22:51:01 +01:00
|
|
|
{
|
|
|
|
.textureIdx = 0,
|
|
|
|
.vertexOffset = uint32_t(newVertexSlice.offset / sizeof(StaticVertex)),
|
|
|
|
.indexOffset = uint32_t(newIndexSlice.offset / sizeof(IndexType)),
|
2022-08-25 07:27:11 +01:00
|
|
|
.indexCount = uint32_t(r_mesh->indices.size()),
|
2022-08-15 22:51:01 +01:00
|
|
|
.bounds = r_mesh->bounds,
|
|
|
|
}));
|
|
|
|
|
2022-08-25 07:27:11 +01:00
|
|
|
g_renderables.emplace_back(Renderable
|
2022-08-15 22:51:01 +01:00
|
|
|
{
|
|
|
|
.transform = mat4{},
|
|
|
|
.meshIdx = meshIdx,
|
2022-08-20 00:53:01 +01:00
|
|
|
.color = 16_vec3,
|
|
|
|
});
|
|
|
|
|
2022-08-25 07:27:11 +01:00
|
|
|
g_renderables.emplace_back(Renderable
|
2022-08-20 00:53:01 +01:00
|
|
|
{
|
|
|
|
.transform = Math::translate(32_vec3),
|
|
|
|
.meshIdx = meshIdx,
|
|
|
|
.color = 1_vec3,
|
|
|
|
});
|
|
|
|
|
2022-08-25 07:27:11 +01:00
|
|
|
g_renderables.emplace_back(Renderable
|
2022-08-20 00:53:01 +01:00
|
|
|
{
|
|
|
|
.transform = Math::translate(-32_vec3),
|
|
|
|
.meshIdx = meshIdx,
|
2022-08-15 22:51:01 +01:00
|
|
|
.color = 1_vec3,
|
|
|
|
});
|
|
|
|
|
2022-08-12 20:08:28 +01:00
|
|
|
//
|
|
|
|
|
2022-08-15 22:51:01 +01:00
|
|
|
#pragma region "make descriptor n shit"
|
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[] =
|
|
|
|
{
|
2022-08-20 00:53:01 +01:00
|
|
|
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2u},
|
2022-08-13 19:09:54 +01:00
|
|
|
{ VK_DESCRIPTOR_TYPE_SAMPLER, 1u},
|
|
|
|
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MaxBindlessResources },
|
2022-08-12 20:08:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
VkDescriptorPoolCreateInfo poolInfo =
|
|
|
|
{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
|
|
|
|
.flags = VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT,
|
2022-08-13 19:09:54 +01:00
|
|
|
.maxSets = 1u,
|
2022-08-12 20:08:28 +01:00
|
|
|
.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,
|
2022-08-13 19:09:54 +01:00
|
|
|
.descriptorCount = 1,
|
2022-08-12 20:08:28 +01:00
|
|
|
.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-20 00:53:01 +01:00
|
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
.descriptorCount = 1,
|
|
|
|
.stageFlags = VK_SHADER_STAGE_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.binding = 3,
|
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[] =
|
|
|
|
{
|
2022-08-20 00:53:01 +01:00
|
|
|
0,
|
2022-08-12 20:08:28 +01:00
|
|
|
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-13 22:15:23 +01:00
|
|
|
struct PushConstants
|
|
|
|
{
|
|
|
|
uint32_t frameIdx;
|
|
|
|
};
|
|
|
|
|
|
|
|
VkPushConstantRange pushConstantRange =
|
|
|
|
{
|
|
|
|
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
|
|
|
|
.offset = 0,
|
|
|
|
.size = sizeof(PushConstants),
|
|
|
|
};
|
|
|
|
|
2022-08-07 06:09:59 +01:00
|
|
|
VkPipelineLayoutCreateInfo pipelineLayoutInfo =
|
|
|
|
{
|
2022-08-13 22:15:23 +01:00
|
|
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
|
|
|
|
.setLayoutCount = 1,
|
|
|
|
.pSetLayouts = &descriptorSetLayout,
|
|
|
|
.pushConstantRangeCount = 1,
|
|
|
|
.pPushConstantRanges = &pushConstantRange,
|
2022-08-07 06:09:59 +01:00
|
|
|
};
|
|
|
|
VkPipelineLayout pipelineLayout = VK_NULL_HANDLE;
|
|
|
|
vkCreatePipelineLayout(r_renderContext->Device(), &pipelineLayoutInfo, nullptr, &pipelineLayout);
|
|
|
|
|
2022-08-13 19:09:54 +01:00
|
|
|
//
|
2022-08-13 07:18:10 +01:00
|
|
|
VkFormat depthFormat = *FindDepthFormat(r_renderContext->PhysicalDevice());
|
2022-08-13 19:09:54 +01:00
|
|
|
auto pipeline = *CreateGraphicsPipeline(r_renderContext->Device(), MiniPipelineInfo
|
|
|
|
{
|
|
|
|
.vs = *r_vsMesh,
|
|
|
|
.fs = *r_fsMesh,
|
|
|
|
.pipelineLayout = pipelineLayout,
|
|
|
|
.colorFormat = FormatToSrgbFormat(r_swapchain->Format()),
|
|
|
|
.depthFormat = depthFormat,
|
|
|
|
.enableDepthTest = true,
|
|
|
|
.enableDepthWrites = true,
|
|
|
|
.vertexAttributes = Span<const VkVertexInputAttributeDescription>(StaticVertex::Attributes),
|
|
|
|
});
|
|
|
|
|
|
|
|
auto skyPipeline = *CreateGraphicsPipeline(r_renderContext->Device(), MiniPipelineInfo
|
|
|
|
{
|
|
|
|
.vs = *r_vsFullscreen,
|
|
|
|
.fs = *r_fsSky,
|
|
|
|
.pipelineLayout = pipelineLayout,
|
|
|
|
.colorFormat = FormatToSrgbFormat(r_swapchain->Format()),
|
|
|
|
.depthFormat = depthFormat,
|
|
|
|
.enableDepthTest = false,
|
|
|
|
.enableDepthWrites = false,
|
|
|
|
.vertexAttributes = Span<const VkVertexInputAttributeDescription>(nullptr, 0),
|
|
|
|
});
|
2022-08-15 22:51:01 +01:00
|
|
|
#pragma endregion
|
2022-08-07 06:09:59 +01:00
|
|
|
|
2022-08-13 21:41:19 +01:00
|
|
|
struct ImageUpload
|
|
|
|
{
|
|
|
|
BufferSlice from;
|
|
|
|
VkImage to;
|
|
|
|
VkExtent3D extent;
|
|
|
|
};
|
|
|
|
Vector<ImageUpload> imageUploads;
|
|
|
|
|
|
|
|
auto r_pngData = fs::OpenFileIntoBuffer("/home/joshua/chair_color.png");
|
|
|
|
if (!r_pngData)
|
2022-08-12 20:08:28 +01:00
|
|
|
return 1;
|
|
|
|
|
2022-08-13 21:41:19 +01:00
|
|
|
spng_ctx *ctx = spng_ctx_new(0);
|
|
|
|
spng_set_png_buffer(ctx, r_pngData->data, r_pngData->size);
|
|
|
|
size_t pngOutSize = 0zu;
|
|
|
|
spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &pngOutSize);
|
|
|
|
auto pngLinearSlice = *pooler.AllocSlice(pngOutSize, 16);
|
|
|
|
spng_decode_image(ctx, pngLinearSlice.ptr, pngOutSize, SPNG_FMT_RGBA8, 0);
|
|
|
|
struct spng_ihdr ihdr;
|
|
|
|
spng_get_ihdr(ctx, &ihdr);
|
|
|
|
spng_ctx_free(ctx);
|
|
|
|
|
2022-08-13 22:19:29 +01:00
|
|
|
auto r_texture = r_renderContext->CreateImage(pooler, ihdr.width, ihdr.height, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT);
|
2022-08-13 21:41:19 +01:00
|
|
|
auto r_textureView = r_renderContext->CreateImageView(*r_texture, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT);
|
|
|
|
|
|
|
|
VkDescriptorImageInfo descriptorImageInfo =
|
|
|
|
{
|
|
|
|
.imageView = *r_textureView,
|
|
|
|
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
|
|
};
|
|
|
|
|
|
|
|
VkWriteDescriptorSet writeImageDescriptorSet[] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
|
|
|
.dstSet = descriptorSet,
|
2022-08-20 00:53:01 +01:00
|
|
|
.dstBinding = 3,
|
2022-08-13 21:41:19 +01:00
|
|
|
.dstArrayElement = 0,
|
|
|
|
.descriptorCount = 1,
|
|
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
|
|
|
.pImageInfo = &descriptorImageInfo,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
vkUpdateDescriptorSets(r_renderContext->Device(), Size(writeImageDescriptorSet), writeImageDescriptorSet, 0, nullptr);
|
|
|
|
|
2022-08-25 07:27:11 +01:00
|
|
|
imageUploads.push_back(ImageUpload
|
2022-08-13 21:41:19 +01:00
|
|
|
{
|
|
|
|
.from = pngLinearSlice,
|
|
|
|
.to = *r_texture,
|
|
|
|
.extent = VkExtent3D{ ihdr.width, ihdr.height, 1u },
|
|
|
|
});
|
|
|
|
|
2022-08-13 12:07:07 +01:00
|
|
|
Camera camera;
|
|
|
|
camera.transform.position = vec3{0.0f, 0.0f, 80.0f};
|
|
|
|
camera.viewportAspectRatio = 16.0f / 9.0f;
|
2022-08-13 12:33:38 +01:00
|
|
|
//camera.offsetOrientation(1_deg * Seconds(t2 - t1).count(), 0_deg);
|
2022-08-13 12:07:07 +01:00
|
|
|
//camera.lookAt(vec3{40.0f, 40.0f, 0.0f});
|
2022-08-13 12:33:38 +01:00
|
|
|
camera.transform.orientation = eulerAnglesToQuaternion(EulerAngles{ 0_deg, 0_deg, 0_deg });
|
2022-08-13 12:07:07 +01:00
|
|
|
|
2022-08-12 21:18:11 +01:00
|
|
|
auto meshData = r_mesh->vertexData.View();
|
2022-08-13 21:41:19 +01:00
|
|
|
auto r_depthImage = r_renderContext->CreateImage(pooler, 1280, 720, depthFormat, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
|
|
|
|
auto depthImageView = *r_renderContext->CreateImageView(*r_depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
|
2022-08-12 21:18:11 +01:00
|
|
|
|
2022-08-20 00:53:01 +01:00
|
|
|
struct FrameData
|
|
|
|
{
|
|
|
|
mat4 projection;
|
|
|
|
mat4 view;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto frameDataSlice = *pooler.AllocSlice(MaxFramesInFlight * sizeof(FrameData), 16);
|
|
|
|
FrameData* frameData = reinterpret_cast<FrameData*>(frameDataSlice.ptr);
|
2022-08-13 22:15:23 +01:00
|
|
|
|
2022-08-12 20:08:28 +01:00
|
|
|
VkDescriptorBufferInfo bufferInfo =
|
|
|
|
{
|
|
|
|
.buffer = r_buffer->buffer,
|
2022-08-20 00:53:01 +01:00
|
|
|
.offset = frameDataSlice.offset,
|
|
|
|
.range = frameDataSlice.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);
|
|
|
|
|
2022-08-20 00:53:01 +01:00
|
|
|
struct Renderables
|
|
|
|
{
|
|
|
|
Renderable renderables[1024];
|
|
|
|
};
|
|
|
|
|
|
|
|
auto renderablesSlice = *pooler.AllocSlice(MaxFramesInFlight * sizeof(Renderables), 16);
|
|
|
|
Renderables* gpuRenderables = reinterpret_cast<Renderables*>(renderablesSlice.ptr);
|
|
|
|
|
|
|
|
VkDescriptorBufferInfo renderblesBufferInfo =
|
|
|
|
{
|
|
|
|
.buffer = r_buffer->buffer,
|
|
|
|
.offset = renderablesSlice.offset,
|
|
|
|
.range = renderablesSlice.size,
|
|
|
|
};
|
|
|
|
|
|
|
|
VkWriteDescriptorSet renderablesWriteDescriptorSet[] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
|
|
|
.dstSet = descriptorSet,
|
|
|
|
.dstBinding = 2,
|
|
|
|
.descriptorCount = 1,
|
|
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
.pBufferInfo = &renderblesBufferInfo,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
vkUpdateDescriptorSets(r_renderContext->Device(), Size(renderablesWriteDescriptorSet), renderablesWriteDescriptorSet, 0, nullptr);
|
|
|
|
|
2022-08-13 14:20:40 +01:00
|
|
|
Input::InputHandler handler;
|
|
|
|
|
2022-08-13 07:37:36 +01:00
|
|
|
r_window->EnableRelativeMouse(true);
|
|
|
|
|
2022-08-20 00:53:01 +01:00
|
|
|
auto indirectBuffer = *pooler.AllocSlice(1024 * sizeof(VkDrawIndexedIndirectCommand), 256);
|
2022-08-15 22:51:01 +01:00
|
|
|
auto indirectPtr = reinterpret_cast<VkDrawIndexedIndirectCommand *>(indirectBuffer.ptr);
|
2022-08-25 07:27:11 +01:00
|
|
|
for (size_t i = 0; i < g_renderables.size(); i++) {
|
2022-08-20 00:53:01 +01:00
|
|
|
const auto& renderable = g_renderables[i];
|
2022-08-25 07:30:01 +01:00
|
|
|
const uint32_t meshIdx = renderable.meshIdx;
|
2022-08-20 00:53:01 +01:00
|
|
|
|
|
|
|
indirectPtr[i] = VkDrawIndexedIndirectCommand
|
|
|
|
{
|
|
|
|
.indexCount = g_meshes[meshIdx].indexCount,
|
|
|
|
.instanceCount = 1u,
|
|
|
|
.firstIndex = g_meshes[meshIdx].indexOffset,
|
|
|
|
.vertexOffset = int32_t(g_meshes[meshIdx].vertexOffset),
|
|
|
|
.firstInstance = 0u,
|
|
|
|
};
|
|
|
|
}
|
2022-08-15 22:51:01 +01:00
|
|
|
|
|
|
|
auto countBuffer = *pooler.AllocSlice(sizeof(uint32_t), 16);
|
|
|
|
auto countPtr = reinterpret_cast<uint32_t*>(countBuffer.ptr);
|
2022-08-25 07:27:11 +01:00
|
|
|
*countPtr = uint32_t(g_renderables.size());
|
2022-08-15 22:51:01 +01:00
|
|
|
|
2022-08-13 12:33:38 +01:00
|
|
|
auto t1 = Time::now();
|
2022-08-13 14:20:40 +01:00
|
|
|
while (r_window->Update(handler))
|
2022-08-04 22:18:11 +01:00
|
|
|
{
|
2022-08-13 12:33:38 +01:00
|
|
|
const auto t2 = Time::now();
|
2022-08-13 21:41:19 +01:00
|
|
|
auto delta = t2 - t1;
|
|
|
|
const float dt = Seconds(delta).count();
|
2022-08-13 12:33:38 +01:00
|
|
|
t1 = t2;
|
|
|
|
|
2022-08-13 21:41:19 +01:00
|
|
|
//char title[64];
|
|
|
|
//stbsp_snprintf(title, sizeof(title), "CubeTest - %gfps", 1.0f / dt);
|
|
|
|
//r_window->SetTitle(title);
|
|
|
|
|
2022-08-13 12:33:38 +01:00
|
|
|
{
|
2022-08-13 14:37:31 +01:00
|
|
|
auto mouseDelta = handler.getMouseMotionDelta();
|
|
|
|
auto scrollDelta = handler.getMouseScrollDelta();
|
2022-08-13 12:33:38 +01:00
|
|
|
//if (mouseDelta.x || mouseDelta.y)
|
|
|
|
//{
|
|
|
|
// log::info("mouse: %d %d", mouseDelta.x, mouseDelta.y);
|
|
|
|
//}
|
|
|
|
|
|
|
|
const float sensitivity = 6.0f;
|
|
|
|
|
|
|
|
camera.offsetOrientation(
|
|
|
|
-sensitivity * dt * Radian( mouseDelta.x ),
|
|
|
|
sensitivity * dt * Radian( mouseDelta.y ));
|
2022-08-13 14:20:40 +01:00
|
|
|
|
2022-08-20 00:53:01 +01:00
|
|
|
frameData[r_swapchain->CurrentFrame()] = FrameData
|
2022-08-13 12:33:38 +01:00
|
|
|
{
|
|
|
|
.projection = camera.projection(),
|
|
|
|
.view = camera.view(),
|
|
|
|
};
|
2022-08-13 14:20:40 +01:00
|
|
|
|
2022-08-25 07:27:11 +01:00
|
|
|
g_renderables.copy(gpuRenderables[r_swapchain->CurrentFrame()].renderables);
|
2022-08-20 00:53:01 +01:00
|
|
|
|
2022-08-13 14:20:40 +01:00
|
|
|
const float movementSpeed = handler.get(Input::ButtonCodes::Key_LShift) ? 64.0f : 16.0f;
|
|
|
|
|
2022-08-13 14:37:31 +01:00
|
|
|
if (scrollDelta.y)
|
|
|
|
{
|
|
|
|
camera.fieldOfView += Radian(5_deg) * -scrollDelta.y;
|
2022-08-13 14:42:36 +01:00
|
|
|
camera.fieldOfView = Clamp(camera.fieldOfView, Radian(5_deg), Radian(175_deg));
|
2022-08-13 14:37:31 +01:00
|
|
|
}
|
|
|
|
|
2022-08-13 14:20:40 +01:00
|
|
|
if (handler.get(Input::ButtonCodes::Key_W))
|
|
|
|
camera.transform.position += camera.forward() * movementSpeed * dt;
|
|
|
|
else if (handler.get(Input::ButtonCodes::Key_S))
|
|
|
|
camera.transform.position += camera.backward() * movementSpeed * dt;
|
|
|
|
|
|
|
|
if (handler.get(Input::ButtonCodes::Key_A))
|
|
|
|
camera.transform.position += camera.left() * movementSpeed * dt;
|
|
|
|
else if (handler.get(Input::ButtonCodes::Key_D))
|
|
|
|
camera.transform.position += camera.right() * movementSpeed * dt;
|
2022-08-13 12:33:38 +01:00
|
|
|
}
|
2022-08-13 07:37:36 +01:00
|
|
|
|
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-13 21:41:19 +01:00
|
|
|
for (auto& upload : imageUploads)
|
|
|
|
{
|
|
|
|
VkBufferImageCopy region =
|
|
|
|
{
|
|
|
|
.bufferOffset = upload.from.offset,
|
|
|
|
.imageSubresource =
|
|
|
|
{
|
|
|
|
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
.mipLevel = 0u,
|
|
|
|
.baseArrayLayer = 0u,
|
|
|
|
.layerCount = 1u,
|
|
|
|
},
|
|
|
|
.imageExtent = upload.extent,
|
|
|
|
};
|
|
|
|
vkCmdCopyBufferToImage(cmdBuf, upload.from.buffer, upload.to, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 1, ®ion);
|
|
|
|
}
|
2022-08-25 07:27:11 +01:00
|
|
|
imageUploads.clear();
|
2022-08-13 21:41:19 +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-13 07:18:10 +01:00
|
|
|
vkCmdSetViewport(cmdBuf, 0, 1, &viewport);
|
2022-08-12 13:34:04 +01:00
|
|
|
vkCmdSetScissor(cmdBuf, 0, 1, &scissor);
|
|
|
|
|
2022-08-13 22:15:23 +01:00
|
|
|
PushConstants pushConstants =
|
|
|
|
{
|
|
|
|
.frameIdx = r_swapchain->CurrentFrame(),
|
|
|
|
};
|
|
|
|
vkCmdPushConstants(cmdBuf, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(PushConstants), &pushConstants);
|
|
|
|
|
2022-08-12 20:08:28 +01:00
|
|
|
vkCmdBindDescriptorSets(cmdBuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
|
2022-08-15 22:51:01 +01:00
|
|
|
VkDeviceSize zero = 0;
|
|
|
|
VkDeviceSize wholeSize = VK_WHOLE_SIZE;
|
|
|
|
VkDeviceSize validationBruhMoment = r_buffer->size;
|
|
|
|
//vkCmdBindVertexBuffers2(cmdBuf, 0, 1, &r_buffer->buffer, &zero, &wholeSize, nullptr);
|
|
|
|
vkCmdBindVertexBuffers2(cmdBuf, 0, 1, &r_buffer->buffer, &zero, &validationBruhMoment, nullptr);
|
|
|
|
vkCmdBindIndexBuffer(cmdBuf, r_buffer->buffer, zero, VulkanIndexType);
|
2022-08-12 20:08:28 +01:00
|
|
|
|
|
|
|
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(),
|
2022-08-13 07:18:10 +01:00
|
|
|
.subresourceRange = FirstColorMipSubresourceRange,
|
2022-08-12 20:08:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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-13 07:18:10 +01:00
|
|
|
const VkImageMemoryBarrier undefinedToDepthBarrier =
|
|
|
|
{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
|
|
|
|
.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
|
|
|
|
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
|
|
|
|
.newLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
|
|
|
.image = *r_depthImage,
|
2022-08-13 19:09:54 +01:00
|
|
|
.subresourceRange = FirstDepthMipSubresourceRange,
|
2022-08-13 07:18:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
vkCmdPipelineBarrier(
|
|
|
|
cmdBuf,
|
|
|
|
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
|
|
|
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
|
|
|
|
0,
|
|
|
|
0, nullptr,
|
|
|
|
0, nullptr,
|
|
|
|
1, &undefinedToDepthBarrier);
|
|
|
|
|
|
|
|
const VkRenderingAttachmentInfoKHR colorAttachmentInfos[] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,
|
|
|
|
.imageView = r_swapchain->ImageView(),
|
|
|
|
.imageLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL_KHR,
|
|
|
|
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
|
|
|
|
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
|
2022-08-13 17:22:19 +01:00
|
|
|
.clearValue = { .color = { .float32 = { 1.0f, 1.0f, 1.0f, 1.0f } } },
|
2022-08-13 07:18:10 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const VkRenderingAttachmentInfoKHR depthAttachmentInfo =
|
2022-08-04 22:18:11 +01:00
|
|
|
{
|
2022-08-07 01:07:56 +01:00
|
|
|
.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,
|
2022-08-13 07:18:10 +01:00
|
|
|
.imageView = depthImageView,
|
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,
|
2022-08-13 07:18:10 +01:00
|
|
|
.clearValue = { .color = { .float32 = { 1.0f, 1.0f, 1.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,
|
2022-08-13 07:18:10 +01:00
|
|
|
.colorAttachmentCount = Size(colorAttachmentInfos),
|
|
|
|
.pColorAttachments = colorAttachmentInfos,
|
|
|
|
.pDepthAttachment = &depthAttachmentInfo,
|
2022-08-04 22:18:11 +01:00
|
|
|
};
|
2022-08-12 13:34:04 +01:00
|
|
|
vkCmdBeginRendering(cmdBuf, &renderInfo);
|
2022-08-13 19:09:54 +01:00
|
|
|
{
|
|
|
|
vkCmdBindPipeline(cmdBuf, VK_PIPELINE_BIND_POINT_GRAPHICS, skyPipeline);
|
|
|
|
vkCmdDraw(cmdBuf, 3, 1, 0, 0);
|
|
|
|
|
|
|
|
vkCmdBindPipeline(cmdBuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
2022-08-20 00:53:01 +01:00
|
|
|
vkCmdDrawIndexedIndirectCount(cmdBuf, indirectBuffer.buffer, indirectBuffer.offset, countBuffer.buffer, countBuffer.offset, 1024u, sizeof(VkDrawIndexedIndirectCommand));
|
2022-08-13 19:09:54 +01:00
|
|
|
}
|
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(),
|
2022-08-13 07:18:10 +01:00
|
|
|
.subresourceRange = FirstColorMipSubresourceRange,
|
2022-08-12 20:08:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|