Skip to content

Commit

Permalink
update AS api
Browse files Browse the repository at this point in the history
  • Loading branch information
yknishidate committed Nov 23, 2024
1 parent d0d0a0f commit dfb4277
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 48 deletions.
12 changes: 0 additions & 12 deletions include/reactive/Graphics/Accel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@

namespace rv {
struct BottomAccelCreateInfo {
BufferHandle vertexBuffer;
BufferHandle indexBuffer;

uint32_t vertexStride;
uint32_t maxVertexCount;
uint32_t maxTriangleCount;
uint32_t triangleCount;

vk::GeometryFlagsKHR geometryFlags = vk::GeometryFlagBitsKHR::eOpaque;

Expand Down Expand Up @@ -67,12 +63,6 @@ class BottomAccel {

auto getBufferAddress() const -> uint64_t { return buffer->getAddress(); }

void update(const BufferHandle& vertexBuffer,
const BufferHandle& indexBuffer,
uint32_t triangleCount);

bool shouldRebuild() { return lastPrimitiveCount != primitiveCount; }

private:
const Context* context;

Expand All @@ -87,8 +77,6 @@ class BottomAccel {
vk::AccelerationStructureBuildTypeKHR buildType;

uint32_t maxPrimitiveCount;
uint32_t lastPrimitiveCount;
uint32_t primitiveCount;
};

class TopAccel {
Expand Down
10 changes: 8 additions & 2 deletions include/reactive/Graphics/CommandBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,17 @@ class CommandBuffer {
// accel struct
void updateTopAccel(TopAccelHandle topAccel) const;

void updateBottomAccel(BottomAccelHandle bottomAccel) const;
void updateBottomAccel(BottomAccelHandle bottomAccel,
const BufferHandle& vertexBuffer, const BufferHandle& indexBuffer,
uint32_t vertexCount, uint32_t triangleCount) const;

void buildTopAccel(TopAccelHandle topAccel) const;

void buildBottomAccel(BottomAccelHandle bottomAccel) const;
void buildBottomAccel(BottomAccelHandle bottomAccel,
const BufferHandle& vertexBuffer,
const BufferHandle& indexBuffer,
uint32_t vertexCount,
uint32_t triangleCount) const;

// timestamp
void beginTimestamp(GPUTimerHandle gpuTimer) const;
Expand Down
10 changes: 4 additions & 6 deletions sample/hello_raytracing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@ class HelloApp : public App {
camera = Camera{Camera::Type::Orbital,
static_cast<float>(Window::getWidth()) / Window::getHeight()};

mesh = Mesh{context, MeshUsage::RayTracing, MemoryUsage::Device, vertices, indices,
"Triangle"};
mesh = Mesh{context, MeshUsage::RayTracing,
MemoryUsage::Device, vertices, indices, "Triangle"};

bottomAccel = context.createBottomAccel({
.vertexBuffer = mesh.vertexBuffer,
.indexBuffer = mesh.indexBuffer,
.vertexStride = sizeof(Vertex),
.maxVertexCount = mesh.getVertexCount(),
.maxTriangleCount = mesh.getTriangleCount(),
.triangleCount = mesh.getTriangleCount(),
.debugName = "bottomAccel",
});
context.oneTimeSubmit([&](CommandBufferHandle commandBuffer) {
commandBuffer->buildBottomAccel(bottomAccel);
commandBuffer->buildBottomAccel(bottomAccel, mesh.vertexBuffer, mesh.indexBuffer,
mesh.getVertexCount(), mesh.getTriangleCount());
});

topAccel = context.createTopAccel({
Expand Down
14 changes: 0 additions & 14 deletions src/Graphics/Accel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ BottomAccel::BottomAccel(const Context& _context, const BottomAccelCreateInfo& c
buildFlags{createInfo.buildFlags},
buildType{createInfo.buildType} {
trianglesData.setVertexFormat(vk::Format::eR32G32B32Sfloat);
trianglesData.setVertexData(createInfo.vertexBuffer->getAddress());
trianglesData.setVertexStride(createInfo.vertexStride);
trianglesData.setMaxVertex(createInfo.maxVertexCount);
trianglesData.setIndexType(vk::IndexTypeValue<uint32_t>::value);
trianglesData.setIndexData(createInfo.indexBuffer->getAddress());

vk::AccelerationStructureGeometryDataKHR geometryData;
geometryData.setTriangles(trianglesData);
Expand All @@ -29,7 +27,6 @@ BottomAccel::BottomAccel(const Context& _context, const BottomAccelCreateInfo& c
buildGeometryInfo.setFlags(buildFlags);
buildGeometryInfo.setGeometries(geometry);

primitiveCount = createInfo.triangleCount;
maxPrimitiveCount = createInfo.maxTriangleCount;
auto buildSizesInfo = context->getDevice().getAccelerationStructureBuildSizesKHR(
buildType, buildGeometryInfo, maxPrimitiveCount);
Expand Down Expand Up @@ -119,17 +116,6 @@ TopAccel::TopAccel(const Context& _context, const TopAccelCreateInfo& createInfo
});
}

void BottomAccel::update(const BufferHandle& vertexBuffer,
const BufferHandle& indexBuffer,
uint32_t triangleCount) {
assert(triangleCount <= maxPrimitiveCount);

trianglesData.setVertexData(vertexBuffer->getAddress());
trianglesData.setIndexData(indexBuffer->getAddress());
lastPrimitiveCount = primitiveCount;
primitiveCount = triangleCount;
}

void TopAccel::updateInstances(ArrayProxy<AccelInstance> accelInstances) const {
RV_ASSERT(primitiveCount == accelInstances.size(), "Instance count was changed. {} == {}",
primitiveCount, accelInstances.size());
Expand Down
41 changes: 27 additions & 14 deletions src/Graphics/CommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,18 @@ void CommandBuffer::updateTopAccel(TopAccelHandle topAccel) const {
commandBuffer->buildAccelerationStructuresKHR(buildGeometryInfo, &buildRangeInfo);
}

void CommandBuffer::updateBottomAccel(BottomAccelHandle bottomAccel) const {
void CommandBuffer::updateBottomAccel(BottomAccelHandle bottomAccel,
const BufferHandle& vertexBuffer,
const BufferHandle& indexBuffer,
uint32_t vertexCount,
uint32_t triangleCount) const {
auto triangleData = bottomAccel->trianglesData;
triangleData.setVertexData(vertexBuffer->getAddress());
triangleData.setMaxVertex(vertexCount);
triangleData.setIndexData(indexBuffer->getAddress());

vk::AccelerationStructureGeometryDataKHR geometryData;
geometryData.setTriangles(bottomAccel->trianglesData);
geometryData.setTriangles(triangleData);

vk::AccelerationStructureGeometryKHR geometry;
geometry.setGeometryType(vk::GeometryTypeKHR::eTriangles);
Expand All @@ -435,17 +444,12 @@ void CommandBuffer::updateBottomAccel(BottomAccelHandle bottomAccel) const {
buildGeometryInfo.setGeometries(geometry);
buildGeometryInfo.setScratchData(bottomAccel->scratchBuffer->getAddress());

if (bottomAccel->shouldRebuild()) {
buildGeometryInfo.setMode(vk::BuildAccelerationStructureModeKHR::eBuild);
buildGeometryInfo.setDstAccelerationStructure(*bottomAccel->accel);
} else {
buildGeometryInfo.setMode(vk::BuildAccelerationStructureModeKHR::eUpdate);
buildGeometryInfo.setSrcAccelerationStructure(*bottomAccel->accel);
buildGeometryInfo.setDstAccelerationStructure(*bottomAccel->accel);
}
buildGeometryInfo.setMode(vk::BuildAccelerationStructureModeKHR::eUpdate);
buildGeometryInfo.setSrcAccelerationStructure(*bottomAccel->accel);
buildGeometryInfo.setDstAccelerationStructure(*bottomAccel->accel);

vk::AccelerationStructureBuildRangeInfoKHR buildRangeInfo{};
buildRangeInfo.setPrimitiveCount(bottomAccel->primitiveCount);
buildRangeInfo.setPrimitiveCount(triangleCount);
buildRangeInfo.setPrimitiveOffset(0);
buildRangeInfo.setFirstVertex(0);
buildRangeInfo.setTransformOffset(0);
Expand Down Expand Up @@ -475,9 +479,18 @@ void CommandBuffer::buildTopAccel(TopAccelHandle topAccel) const {
commandBuffer->buildAccelerationStructuresKHR(buildGeometryInfo, &buildRangeInfo);
}

void CommandBuffer::buildBottomAccel(BottomAccelHandle bottomAccel) const {
void CommandBuffer::buildBottomAccel(BottomAccelHandle bottomAccel,
const BufferHandle& vertexBuffer,
const BufferHandle& indexBuffer,
uint32_t vertexCount,
uint32_t triangleCount) const {
auto trianglesData = bottomAccel->trianglesData;
trianglesData.setVertexData(vertexBuffer->getAddress());
trianglesData.setMaxVertex(vertexCount);
trianglesData.setIndexData(indexBuffer->getAddress());

vk::AccelerationStructureGeometryDataKHR geometryData;
geometryData.setTriangles(bottomAccel->trianglesData);
geometryData.setTriangles(trianglesData);

vk::AccelerationStructureGeometryKHR geometry;
geometry.setGeometryType(vk::GeometryTypeKHR::eTriangles);
Expand All @@ -494,7 +507,7 @@ void CommandBuffer::buildBottomAccel(BottomAccelHandle bottomAccel) const {
buildGeometryInfo.setScratchData(bottomAccel->scratchBuffer->getAddress());

vk::AccelerationStructureBuildRangeInfoKHR buildRangeInfo{};
buildRangeInfo.setPrimitiveCount(bottomAccel->primitiveCount);
buildRangeInfo.setPrimitiveCount(triangleCount);
buildRangeInfo.setPrimitiveOffset(0);
buildRangeInfo.setFirstVertex(0);
buildRangeInfo.setTransformOffset(0);
Expand Down

0 comments on commit dfb4277

Please sign in to comment.