Skip to content

Commit

Permalink
Merge pull request #75 from KTStephano/v0.10
Browse files Browse the repository at this point in the history
V0.10
  • Loading branch information
KTStephano authored Aug 4, 2023
2 parents f9421a9 + c77c1b5 commit 3dd19b1
Show file tree
Hide file tree
Showing 13 changed files with 577 additions and 344 deletions.
138 changes: 0 additions & 138 deletions Source/Engine/StratusGpuBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,142 +584,4 @@ namespace stratus {
}
return indices;
}

GpuCommandBuffer::GpuCommandBuffer() {

}

void GpuCommandBuffer::RemoveCommandsAt(const std::unordered_set<size_t>& indices) {
VerifyArraySizes_();
if (indices.size() == 0) return;

// std::vector<uint64_t> newHandles;
std::vector<uint32_t> newMaterialIndices;
std::vector<glm::mat4> newPrevFrameModelTransforms;
std::vector<glm::mat4> newModelTransforms;
std::vector<GpuDrawElementsIndirectCommand> newIndirectDrawCommands;
for (size_t i = 0; i < NumDrawCommands(); ++i) {
if (indices.find(i) == indices.end()) {
// handlesToIndicesMap.insert(std::make_pair(handles[i], newHandles.size()));
// newHandles.push_back(handles[i]);
newMaterialIndices.push_back(materialIndices[i]);
newPrevFrameModelTransforms.push_back(prevFrameModelTransforms[i]);
newModelTransforms.push_back(modelTransforms[i]);
newIndirectDrawCommands.push_back(indirectDrawCommands[i]);
}
// else {
// handlesToIndicesMap.erase(handles[i]);
// }
}

// handles = std::move(newHandles);
materialIndices = std::move(newMaterialIndices);
prevFrameModelTransforms = std::move(newPrevFrameModelTransforms);
modelTransforms = std::move(newModelTransforms);
indirectDrawCommands = std::move(newIndirectDrawCommands);
}

size_t GpuCommandBuffer::NumDrawCommands() const {
return indirectDrawCommands.size();
}

void GpuCommandBuffer::UploadDataToGpu() {
VerifyArraySizes_();

const size_t numElems = NumDrawCommands();
if (numElems == 0) return;

if (indirectDrawCommands_ == GpuBuffer() || indirectDrawCommands_.SizeBytes() < (numElems * sizeof(GpuDrawElementsIndirectCommand))) {
const Bitfield flags = GPU_DYNAMIC_DATA;

materialIndices_ = GpuBuffer((const void *)materialIndices.data(), numElems * sizeof(uint32_t), flags);
prevFrameModelTransforms_ = GpuBuffer((const void*)prevFrameModelTransforms.data(), numElems * sizeof(glm::mat4), flags);
modelTransforms_ = GpuBuffer((const void *)modelTransforms.data(), numElems * sizeof(glm::mat4), flags);
indirectDrawCommands_ = GpuBuffer((const void *)indirectDrawCommands.data(), numElems * sizeof(GpuDrawElementsIndirectCommand), flags);
if (aabbs.size() > 0) {
aabbs_ = GpuBuffer((const void *)aabbs.data(), numElems * sizeof(GpuAABB), flags);
}
}
else {
materialIndices_.CopyDataToBuffer(0, numElems * sizeof(uint32_t), (const void *)materialIndices.data());
prevFrameModelTransforms_.CopyDataToBuffer(0, numElems * sizeof(glm::mat4), (const void*)prevFrameModelTransforms.data());
modelTransforms_.CopyDataToBuffer(0, numElems * sizeof(glm::mat4), (const void *)modelTransforms.data());
indirectDrawCommands_.CopyDataToBuffer(0, numElems * sizeof(GpuDrawElementsIndirectCommand), (const void *)indirectDrawCommands.data());
if (aabbs.size() > 0) {
aabbs_.CopyDataToBuffer(0, numElems * sizeof(GpuAABB), (const void *)aabbs.data());
}
}
}

void GpuCommandBuffer::BindMaterialIndicesBuffer(uint32_t index) {
if (materialIndices_ == GpuBuffer()) {
throw std::runtime_error("Null material indices GpuBuffer");
}
materialIndices_.BindBase(GpuBaseBindingPoint::SHADER_STORAGE_BUFFER, index);
}

void GpuCommandBuffer::BindPrevFrameModelTransformBuffer(uint32_t index) {
if (prevFrameModelTransforms_ == GpuBuffer()) {
throw std::runtime_error("Null previous frame model transform GpuBuffer");
}
prevFrameModelTransforms_.BindBase(GpuBaseBindingPoint::SHADER_STORAGE_BUFFER, index);
}

void GpuCommandBuffer::BindModelTransformBuffer(uint32_t index) {
if (modelTransforms_ == GpuBuffer()) {
throw std::runtime_error("Null model transform GpuBuffer");
}
modelTransforms_.BindBase(GpuBaseBindingPoint::SHADER_STORAGE_BUFFER, index);
}

void GpuCommandBuffer::BindAabbBuffer(uint32_t index) {
if (aabbs_ == GpuBuffer()) {
throw std::runtime_error("Null aabb GpuBuffer");
}
aabbs_.BindBase(GpuBaseBindingPoint::SHADER_STORAGE_BUFFER, index);
}

void GpuCommandBuffer::BindIndirectDrawCommands() {
if (indirectDrawCommands_ == GpuBuffer()) {
throw std::runtime_error("Null indirect draw command buffer");
}
indirectDrawCommands_.Bind(GpuBindingPoint::DRAW_INDIRECT_BUFFER);
}

void GpuCommandBuffer::UnbindIndirectDrawCommands() {
if (indirectDrawCommands_ == GpuBuffer()) {
throw std::runtime_error("Null indirect draw command buffer");
}
indirectDrawCommands_.Unbind(GpuBindingPoint::DRAW_INDIRECT_BUFFER);
}

void GpuCommandBuffer::VerifyArraySizes_() const {
if (//materialIndices.size() == handlesToIndicesMap.size() &&
//materialIndices.size() == handles.size() &&
materialIndices.size() != prevFrameModelTransforms.size() ||
materialIndices.size() != modelTransforms.size() ||
materialIndices.size() != indirectDrawCommands.size()) {
throw std::runtime_error("Sizes do not match up in GpuBuffer");
}

if (aabbs.size() > 0) {
assert(aabbs.size() == indirectDrawCommands.size());
}
}

const GpuBuffer& GpuCommandBuffer::GetIndirectDrawCommandsBuffer() const {
return indirectDrawCommands_;
}

GpuCommandBufferPtr GpuCommandBuffer::Copy() const {
GpuCommandBufferPtr copy = GpuCommandBufferPtr(new GpuCommandBuffer());
copy->materialIndices = materialIndices;
copy->prevFrameModelTransforms = prevFrameModelTransforms;
copy->modelTransforms = modelTransforms;
copy->indirectDrawCommands = indirectDrawCommands;
copy->aabbs = aabbs;

copy->UploadDataToGpu();
return copy;
}
}
52 changes: 0 additions & 52 deletions Source/Engine/StratusGpuBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ namespace stratus {

struct GpuBufferImpl;
struct GpuArrayBufferImpl;
struct GpuCommandBuffer;

typedef std::shared_ptr<GpuCommandBuffer> GpuCommandBufferPtr;

// A gpu buffer holds primitive data usually in the form of floats, ints and shorts
// TODO: Look into use cases for things other than STATIC_DRAW
Expand Down Expand Up @@ -422,53 +419,4 @@ namespace stratus {
static std::vector<_MeshData> freeIndices_;
static bool initialized_;
};

// Stores material indices, model transforms and indirect draw commands
class GpuCommandBuffer final {
GpuBuffer materialIndices_;
GpuBuffer prevFrameModelTransforms_;
GpuBuffer modelTransforms_;
GpuBuffer indirectDrawCommands_;
GpuBuffer aabbs_;

public:
GpuCommandBuffer();

// This is to allow for 64-bit handles to be used to identify an object
// with its location in the array
// std::unordered_map<uint64_t, size_t> handlesToIndicesMap;
// std::vector<uint64_t> handles;
// CPU side of the data
std::vector<uint32_t> materialIndices;
// Model transform is defined as global transform * mesh transform
std::vector<glm::mat4> prevFrameModelTransforms;
std::vector<glm::mat4> modelTransforms;
std::vector<GpuDrawElementsIndirectCommand> indirectDrawCommands;
std::vector<GpuAABB> aabbs;

GpuCommandBuffer(GpuCommandBuffer&&) = default;
GpuCommandBuffer(const GpuCommandBuffer&) = delete;

GpuCommandBuffer& operator=(GpuCommandBuffer&&) = delete;
GpuCommandBuffer& operator=(const GpuCommandBuffer&) = delete;

void RemoveCommandsAt(const std::unordered_set<size_t>& indices);
size_t NumDrawCommands() const;
void UploadDataToGpu();

void BindMaterialIndicesBuffer(uint32_t index);
void BindPrevFrameModelTransformBuffer(uint32_t index);
void BindModelTransformBuffer(uint32_t index);
void BindAabbBuffer(uint32_t index);

void BindIndirectDrawCommands();
void UnbindIndirectDrawCommands();

const GpuBuffer& GetIndirectDrawCommandsBuffer() const;

GpuCommandBufferPtr Copy() const;

private:
void VerifyArraySizes_() const;
};
}
Loading

0 comments on commit 3dd19b1

Please sign in to comment.