sync constants

This commit is contained in:
Joshua Ashton 2022-08-13 21:15:23 +00:00
parent 0b84e8b027
commit a9bb435d04
3 changed files with 46 additions and 17 deletions

View File

@ -24,6 +24,8 @@ namespace orange
VkFormat Format() const { return m_format; }
VkExtent2D Extent() const { return m_extent; }
uint32_t CurrentFrame() const { return m_currentFrame; }
void Present();
protected:
friend class Result<Swapchain>;

View File

@ -328,11 +328,25 @@ int main(int argc, char** argv)
VkDescriptorSet descriptorSet = VK_NULL_HANDLE;
vkAllocateDescriptorSets(r_renderContext->Device(), &descriptorSetAllocInfo, &descriptorSet);
struct PushConstants
{
uint32_t frameIdx;
};
VkPushConstantRange pushConstantRange =
{
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
.offset = 0,
.size = sizeof(PushConstants),
};
VkPipelineLayoutCreateInfo pipelineLayoutInfo =
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = 1,
.pSetLayouts = &descriptorSetLayout
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = 1,
.pSetLayouts = &descriptorSetLayout,
.pushConstantRangeCount = 1,
.pPushConstantRanges = &pushConstantRange,
};
VkPipelineLayout pipelineLayout = VK_NULL_HANDLE;
vkCreatePipelineLayout(r_renderContext->Device(), &pipelineLayoutInfo, nullptr, &pipelineLayout);
@ -428,19 +442,15 @@ int main(int argc, char** argv)
//camera.lookAt(vec3{40.0f, 40.0f, 0.0f});
camera.transform.orientation = eulerAnglesToQuaternion(EulerAngles{ 0_deg, 0_deg, 0_deg });
UniformData uniformData
{
.projection = camera.projection(),
.view = camera.view(),
};
auto meshData = r_mesh->vertexData.View();
auto vertexSlice = *pooler.AllocSlice(meshData.size, 16);
auto indexSlice = *pooler.AllocSlice(r_mesh->indices.Size() * sizeof(IndexType), 16);
auto uniformSlice = *pooler.AllocSlice(sizeof(UniformData), 16);
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);
auto uniformSlice = *pooler.AllocSlice(MaxFramesInFlight * sizeof(UniformData), 16);
UniformData* uniformData = reinterpret_cast<UniformData*>(uniformSlice.ptr);
VkDescriptorBufferInfo bufferInfo =
{
.buffer = r_buffer->buffer,
@ -464,8 +474,6 @@ int main(int argc, char** argv)
meshData.Copy ((uint8_t*)(r_buffer->ptr) + vertexSlice.offset);
r_mesh->indices.Copy((uint8_t*)(r_buffer->ptr) + indexSlice.offset);
memcpy((uint8_t*)(r_buffer->ptr) + uniformSlice.offset, &uniformData, sizeof(uniformData));
Input::InputHandler handler;
r_window->EnableRelativeMouse(true);
@ -496,7 +504,7 @@ int main(int argc, char** argv)
-sensitivity * dt * Radian( mouseDelta.x ),
sensitivity * dt * Radian( mouseDelta.y ));
UniformData uniformData
uniformData[r_swapchain->CurrentFrame()] = UniformData
{
.projection = camera.projection(),
.view = camera.view(),
@ -519,8 +527,6 @@ int main(int argc, char** argv)
camera.transform.position += camera.left() * movementSpeed * dt;
else if (handler.get(Input::ButtonCodes::Key_D))
camera.transform.position += camera.right() * movementSpeed * dt;
// NEED TO SYNCHRONIZE THIS
memcpy((uint8_t*)(r_buffer->ptr) + uniformSlice.offset, &uniformData, sizeof(uniformData));
}
VkCommandBuffer cmdBuf = r_swapchain->CommandBuffer();
@ -563,6 +569,12 @@ int main(int argc, char** argv)
vkCmdSetViewport(cmdBuf, 0, 1, &viewport);
vkCmdSetScissor(cmdBuf, 0, 1, &scissor);
PushConstants pushConstants =
{
.frameIdx = r_swapchain->CurrentFrame(),
};
vkCmdPushConstants(cmdBuf, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(PushConstants), &pushConstants);
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, VulkanIndexType);

View File

@ -1,11 +1,23 @@
#version 450
#extension GL_ARB_shader_draw_parameters : enable
layout(binding = 0) uniform UniformData {
struct UniformData
{
mat4 projection;
mat4 view;
};
layout(push_constant) uniform p_constants_t
{
uint frame;
} p_constants;
layout(binding = 0) uniform u_data_t
{
UniformData frame[4];
} u_data;
layout(location = 0) in vec3 i_pos;
layout(location = 1) in vec2 i_uv;
layout(location = 2) in vec3 i_normal;
@ -15,7 +27,10 @@ layout(location = 1) out vec2 o_uv;
void main()
{
gl_Position = u_data.projection * u_data.view * vec4(i_pos, 1.0);
gl_Position =
u_data.frame[p_constants.frame].projection *
u_data.frame[p_constants.frame].view *
vec4(i_pos, 1.0);
o_id = gl_DrawIDARB;
o_uv = i_uv;
}