Skip to content

Commit

Permalink
Fix handling of shouldRender flag
Browse files Browse the repository at this point in the history
  • Loading branch information
janhsimon committed Aug 26, 2024
1 parent b0ecee0 commit 230e476
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 47 deletions.
14 changes: 6 additions & 8 deletions src/Headset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,13 +496,6 @@ Headset::BeginFrameResult Headset::beginFrame(uint32_t& swapchainImageIndex)
return BeginFrameResult::Error;
}

if (!frameState.shouldRender)
{
// Let the host know that we don't want to render this frame
// We do still need to end the frame however
return BeginFrameResult::SkipRender;
}

// Update the eye poses
viewState.type = XR_TYPE_VIEW_STATE;
uint32_t viewCount;
Expand Down Expand Up @@ -558,7 +551,12 @@ Headset::BeginFrameResult Headset::beginFrame(uint32_t& swapchainImageIndex)
return BeginFrameResult::Error;
}

return BeginFrameResult::RenderFully; // Request full rendering of the frame
if (!frameState.shouldRender)
{
return BeginFrameResult::SkipRender;
}

return BeginFrameResult::RenderFully;
}

void Headset::endFrame() const
Expand Down
2 changes: 1 addition & 1 deletion src/Headset.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Headset final
{
Error, // An error occurred
RenderFully, // Render this frame normally
SkipRender, // Skip rendering the frame but end it
SkipRender, // Process this frame but skip rendering
SkipFully // Skip processing this frame entirely without ending it
};
BeginFrameResult beginFrame(uint32_t& swapchainImageIndex);
Expand Down
75 changes: 37 additions & 38 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,57 +130,56 @@ int main()
{
return EXIT_FAILURE;
}
else if (frameResult == Headset::BeginFrameResult::RenderFully)
else if (frameResult == Headset::BeginFrameResult::SkipFully)
{
if (!controllers.sync(headset.getXrSpace(), headset.getXrFrameState().predictedDisplayTime))
{
return EXIT_FAILURE;
}
continue;
}

if (!controllers.sync(headset.getXrSpace(), headset.getXrFrameState().predictedDisplayTime))
{
return EXIT_FAILURE;
}

static float time = 0.0f;
time += deltaTime;
static float time = 0.0f;
time += deltaTime;

// Update
for (size_t controllerIndex = 0u; controllerIndex < 2u; ++controllerIndex)
// Update
for (size_t controllerIndex = 0u; controllerIndex < 2u; ++controllerIndex)
{
const float flySpeed = controllers.getFlySpeed(controllerIndex);
if (flySpeed > 0.0f)
{
const float flySpeed = controllers.getFlySpeed(controllerIndex);
if (flySpeed > 0.0f)
{
const glm::vec3 forward = glm::normalize(controllers.getPose(controllerIndex)[2]);
cameraMatrix = glm::translate(cameraMatrix, forward * flySpeed * flySpeedMultiplier * deltaTime);
}
const glm::vec3 forward = glm::normalize(controllers.getPose(controllerIndex)[2]);
cameraMatrix = glm::translate(cameraMatrix, forward * flySpeed * flySpeedMultiplier * deltaTime);
}
}

const glm::mat4 inverseCameraMatrix = glm::inverse(cameraMatrix);
handModelLeft.worldMatrix = inverseCameraMatrix * controllers.getPose(0u);
handModelRight.worldMatrix = inverseCameraMatrix * controllers.getPose(1u);
handModelRight.worldMatrix = glm::scale(handModelRight.worldMatrix, { -1.0f, 1.0f, 1.0f });

bikeModel.worldMatrix =
glm::rotate(glm::translate(glm::mat4(1.0f), { 0.5f, 0.0f, -4.5f }), time * 0.2f, { 0.0f, 1.0f, 0.0f });

// Render
renderer.render(cameraMatrix, swapchainImageIndex, time);
const glm::mat4 inverseCameraMatrix = glm::inverse(cameraMatrix);
handModelLeft.worldMatrix = inverseCameraMatrix * controllers.getPose(0u);
handModelRight.worldMatrix = inverseCameraMatrix * controllers.getPose(1u);
handModelRight.worldMatrix = glm::scale(handModelRight.worldMatrix, { -1.0f, 1.0f, 1.0f });

const MirrorView::RenderResult mirrorResult = mirrorView.render(swapchainImageIndex);
if (mirrorResult == MirrorView::RenderResult::Error)
{
return EXIT_FAILURE;
}
bikeModel.worldMatrix =
glm::rotate(glm::translate(glm::mat4(1.0f), { 0.5f, 0.0f, -4.5f }), time * 0.2f, { 0.0f, 1.0f, 0.0f });

const bool mirrorViewVisible = (mirrorResult == MirrorView::RenderResult::Visible);
renderer.submit(mirrorViewVisible);
// Render
renderer.render(cameraMatrix, swapchainImageIndex, time);

if (mirrorViewVisible)
{
mirrorView.present();
}
const MirrorView::RenderResult mirrorResult = mirrorView.render(swapchainImageIndex);
if (mirrorResult == MirrorView::RenderResult::Error)
{
return EXIT_FAILURE;
}

if (frameResult == Headset::BeginFrameResult::RenderFully || frameResult == Headset::BeginFrameResult::SkipRender)
const bool mirrorViewVisible = (mirrorResult == MirrorView::RenderResult::Visible);
renderer.submit(mirrorViewVisible);

if (mirrorViewVisible)
{
headset.endFrame();
mirrorView.present();
}

headset.endFrame();
}

context.sync(); // Sync before destroying so that resources are free
Expand Down

0 comments on commit 230e476

Please sign in to comment.