Skip to content

Commit

Permalink
wip: continue xr improvements + docs
Browse files Browse the repository at this point in the history
  • Loading branch information
knokko committed Sep 21, 2024
1 parent c455f33 commit a1c1ade
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 42 deletions.
95 changes: 55 additions & 40 deletions samples/src/main/java/com/github/knokko/boiler/samples/HelloXR.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.knokko.boiler.samples;

import com.github.knokko.boiler.buffers.MappedVkbBuffer;
import com.github.knokko.boiler.builders.BoilerBuilder;
import com.github.knokko.boiler.builders.xr.BoilerXrBuilder;
import com.github.knokko.boiler.commands.CommandRecorder;
Expand Down Expand Up @@ -28,6 +29,8 @@

public class HelloXR {

private static final int NUM_FRAMES_IN_FLIGHT = 2;

public static void main(String[] args) throws InterruptedException {
var boiler = new BoilerBuilder(
VK_API_VERSION_1_0, "HelloXR", 1
Expand Down Expand Up @@ -94,11 +97,12 @@ public static void main(String[] args) throws InterruptedException {
VK_IMAGE_ASPECT_DEPTH_BIT, VK_SAMPLE_COUNT_1_BIT, 1, 2, true, "DepthImage"
);

var commandPool = boiler.commands.createPool(
VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, boiler.queueFamilies().graphics().index(), "Drawing"
var commandPools = boiler.commands.createPools(
VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, boiler.queueFamilies().graphics().index(),
NUM_FRAMES_IN_FLIGHT, "Drawing"
);
var commandBuffer = boiler.commands.createPrimaryBuffers(commandPool, 1, "Drawing")[0];
var fence = boiler.sync.fenceBank.borrowFence(false, "Drawing");
var commandBuffers = boiler.commands.createPrimaryBufferPerPool("Drawing", commandPools);
var fences = boiler.sync.fenceBank.borrowFences(NUM_FRAMES_IN_FLIGHT, false, "Drawing");

int vertexSize = (3 + 3) * 4;
var vertexBuffer = boiler.buffers.createMapped(
Expand All @@ -124,13 +128,16 @@ public static void main(String[] args) throws InterruptedException {
hostIndexBuffer.put(1).put(2).put(3); // right of the hand triangle
hostIndexBuffer.put(2).put(0).put(3); // left of the hand triangle

var matrixBuffer = boiler.buffers.createMapped(
5 * 64, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, "MatrixBuffer"
);
var matrixBuffers = new MappedVkbBuffer[NUM_FRAMES_IN_FLIGHT];
for (int index = 0; index < NUM_FRAMES_IN_FLIGHT; index++) {
matrixBuffers[index] = boiler.buffers.createMapped(
5 * 64, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, "MatrixBuffer"
);
}

VkbDescriptorSetLayout descriptorSetLayout;
HomogeneousDescriptorPool descriptorPool;
long descriptorSet;
long[] descriptorSets;
long pipelineLayout;
long graphicsPipeline;
try (var stack = stackPush()) {
Expand All @@ -139,15 +146,17 @@ public static void main(String[] args) throws InterruptedException {
boiler.descriptors.binding(layoutBindings, 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT);

descriptorSetLayout = boiler.descriptors.createLayout(stack, layoutBindings, "MatricesLayout");
descriptorPool = descriptorSetLayout.createPool(1, 0, "MatricesPool");
descriptorSet = descriptorPool.allocate(1)[0];
descriptorPool = descriptorSetLayout.createPool(NUM_FRAMES_IN_FLIGHT, 0, "MatricesPool");
descriptorSets = descriptorPool.allocate(NUM_FRAMES_IN_FLIGHT);

var descriptorWrites = VkWriteDescriptorSet.calloc(1, stack);
boiler.descriptors.writeBuffer(
stack, descriptorWrites, descriptorSet,
0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, matrixBuffer.fullRange()
);
vkUpdateDescriptorSets(boiler.vkDevice(), descriptorWrites, null);
for (int index = 0; index < NUM_FRAMES_IN_FLIGHT; index++) {
boiler.descriptors.writeBuffer(
stack, descriptorWrites, descriptorSets[index],
0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, matrixBuffers[index].fullRange()
);
vkUpdateDescriptorSets(boiler.vkDevice(), descriptorWrites, null);
}

var pushConstants = VkPushConstantRange.calloc(1, stack);
pushConstants.stageFlags(VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT);
Expand Down Expand Up @@ -243,6 +252,7 @@ public static void main(String[] args) throws InterruptedException {

class HelloSessionLoop extends SessionLoop {

private int frameIndex;
private Matrix4f leftHandMatrix, rightHandMatrix;

public HelloSessionLoop(
Expand Down Expand Up @@ -272,6 +282,16 @@ protected void handleEvent(XrEventDataBuffer event) {
System.out.println("Handled event of type " + event.type() + ": new state is " + getState());
}

@Override
protected void waitForRenderResources(MemoryStack stack) {
frameIndex = (frameIndex + 1) % NUM_FRAMES_IN_FLIGHT;
fences[frameIndex].waitIfSubmitted();
fences[frameIndex].reset();
assertVkSuccess(vkResetCommandPool(
xr.boilerInstance.vkDevice(), commandPools[frameIndex], 0
), "ResetCommandPool", "Drawing");
}

@Override
protected void prepareRender(MemoryStack stack, XrFrameState frameState) {
leftHandMatrix = boiler.xr().locateSpace(
Expand All @@ -288,7 +308,7 @@ protected void prepareRender(MemoryStack stack, XrFrameState frameState) {
protected void recordRenderCommands(
MemoryStack stack, int swapchainImageIndex, Matrix4f[] cameraMatrices
) {
var commands = CommandRecorder.begin(commandBuffer, xr.boilerInstance, stack, "Drawing");
var commands = CommandRecorder.begin(commandBuffers[frameIndex], xr.boilerInstance, stack, "Drawing");

var colorAttachments = VkRenderingAttachmentInfoKHR.calloc(1, stack);
commands.simpleColorRenderingAttachment(
Expand All @@ -311,19 +331,19 @@ protected void recordRenderCommands(
dynamicRenderingInfo.pColorAttachments(colorAttachments);
dynamicRenderingInfo.pDepthAttachment(depthAttachment);

vkCmdBeginRenderingKHR(commandBuffer, dynamicRenderingInfo);
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
vkCmdBeginRenderingKHR(commandBuffers[frameIndex], dynamicRenderingInfo);
vkCmdBindPipeline(commandBuffers[frameIndex], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
vkCmdBindDescriptorSets(
commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
pipelineLayout, 0, stack.longs(descriptorSet), null
commandBuffers[frameIndex], VK_PIPELINE_BIND_POINT_GRAPHICS,
pipelineLayout, 0, stack.longs(descriptorSets[frameIndex]), null
);
vkCmdBindVertexBuffers(
commandBuffer, 0,
commandBuffers[frameIndex], 0,
stack.longs(vertexBuffer.vkBuffer()), stack.longs(0)
);
vkCmdBindIndexBuffer(commandBuffer, indexBuffer.vkBuffer(), 0, VK_INDEX_TYPE_UINT32);
vkCmdBindIndexBuffer(commandBuffers[frameIndex], indexBuffer.vkBuffer(), 0, VK_INDEX_TYPE_UINT32);

var hostMatrixBuffer = memFloatBuffer(matrixBuffer.hostAddress(), 5 * 16);
var hostMatrixBuffer = memFloatBuffer(matrixBuffers[frameIndex].hostAddress(), 5 * 16);
cameraMatrices[0].get(0, hostMatrixBuffer);
cameraMatrices[1].get(16, hostMatrixBuffer);

Expand All @@ -342,55 +362,50 @@ protected void recordRenderCommands(
pushConstants.put(0, 0);
pushConstants.put(1, 0);
vkCmdPushConstants(
commandBuffer, pipelineLayout,
commandBuffers[frameIndex], pipelineLayout,
VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_VERTEX_BIT, 0, pushConstants
);
vkCmdDrawIndexed(commandBuffer, 3, 1, 0, 0, 0);
vkCmdDrawIndexed(commandBuffers[frameIndex], 3, 1, 0, 0, 0);

if (leftHandMatrix != null) {
var giLeftClick = session.prepareSubactionState(stack, handClickAction, pathLeftHand);
var holdsLeft = session.getBooleanAction(stack, giLeftClick, "left click").currentState();
pushConstants.put(0, holdsLeft ? 0 : 1);
pushConstants.put(1, 1);
vkCmdPushConstants(
commandBuffer, pipelineLayout,
commandBuffers[frameIndex], pipelineLayout,
VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_VERTEX_BIT, 0, pushConstants
);
vkCmdDrawIndexed(commandBuffer, 12, 1, 3, 0, 0);
vkCmdDrawIndexed(commandBuffers[frameIndex], 12, 1, 3, 0, 0);
}
if (rightHandMatrix != null) {
var giRightClick = session.prepareSubactionState(stack, handClickAction, pathRightHand);
var holdsRight = session.getBooleanAction(stack, giRightClick, "right click").currentState();
pushConstants.put(0, holdsRight ? 0 : 1);
pushConstants.put(1, 2);
vkCmdPushConstants(
commandBuffer, pipelineLayout,
commandBuffers[frameIndex], pipelineLayout,
VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_VERTEX_BIT, 0, pushConstants
);
vkCmdDrawIndexed(commandBuffer, 12, 1, 3, 0, 0);
vkCmdDrawIndexed(commandBuffers[frameIndex], 12, 1, 3, 0, 0);
}

vkCmdEndRenderingKHR(commandBuffer);
vkCmdEndRenderingKHR(commandBuffers[frameIndex]);
commands.end();
}

@Override
protected void submitAndWaitRender() {
protected void submitRenderCommands() {
xr.boilerInstance.queueFamilies().graphics().first().submit(
commandBuffer, "Drawing", null, fence
commandBuffers[frameIndex], "Drawing", null, fences[frameIndex]
);

fence.waitAndReset();
assertVkSuccess(vkResetCommandPool(
xr.boilerInstance.vkDevice(), commandPool, 0
), "ResetCommandPool", "Drawing");
}
}

new HelloSessionLoop(session, renderSpace, swapchain, width, height).run();

boiler.sync.fenceBank.returnFence(fence);
vkDestroyCommandPool(boiler.vkDevice(), commandPool, null);
boiler.sync.fenceBank.returnFences(fences);
for (var commandPool : commandPools) vkDestroyCommandPool(boiler.vkDevice(), commandPool, null);
vkDestroyPipeline(boiler.vkDevice(), graphicsPipeline, null);
vkDestroyPipelineLayout(boiler.vkDevice(), pipelineLayout, null);
descriptorPool.destroy();
Expand All @@ -401,7 +416,7 @@ protected void submitAndWaitRender() {

vertexBuffer.destroy(boiler);
indexBuffer.destroy(boiler);
matrixBuffer.destroy(boiler);
for (var matrixBuffer : matrixBuffers) matrixBuffer.destroy(boiler);
vkDestroyImageView(boiler.vkDevice(), depthImage.vkImageView(), null);
vmaDestroyImage(boiler.vmaAllocator(), depthImage.vkImage(), depthImage.vmaAllocation());

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/github/knokko/boiler/xr/SessionLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public void run() {
if (this.state == XR_SESSION_STATE_SYNCHRONIZED || this.state == XR_SESSION_STATE_VISIBLE ||
this.state == XR_SESSION_STATE_FOCUSED || this.state == XR_SESSION_STATE_READY
) {
waitForRenderResources(stack);

var frameState = XrFrameState.calloc(stack);
frameState.type$Default();

Expand Down Expand Up @@ -170,7 +172,7 @@ stack, renderSpace, numViews, getViewConfigurationType(),
swapchain, wiSwapchain
), "WaitSwapchainImage", null);

submitAndWaitRender();
submitRenderCommands();

assertXrSuccess(xrReleaseSwapchainImage(
swapchain, null
Expand Down Expand Up @@ -273,9 +275,11 @@ protected long getSwapchainWaitTimeout() {

protected abstract void handleEvent(XrEventDataBuffer event);

protected abstract void waitForRenderResources(MemoryStack stack);

protected abstract void prepareRender(MemoryStack stack, XrFrameState frameState);

protected abstract void recordRenderCommands(MemoryStack stack, int swapchainImageIndex, Matrix4f[] cameraMatrices);

protected abstract void submitAndWaitRender();
protected abstract void submitRenderCommands();
}

0 comments on commit a1c1ade

Please sign in to comment.