Lugdunum  0.1.0
Light.cpp
Go to the documentation of this file.
2 
6 
7 namespace lug {
8 namespace Graphics {
9 namespace Vulkan {
10 namespace Render {
11 namespace BufferPool {
12 
13 Light::Light(Renderer& renderer) : BufferPool(renderer, {
14  renderer.getDevice().getQueue("queue_graphics")->getQueueFamily()->getIdx(),
15  renderer.getDevice().getQueue("queue_transfer")->getQueueFamily()->getIdx()
16 }) {}
17 
18 const SubBuffer* Light::allocate(uint32_t currentFrame, const API::CommandBuffer& cmdBuffer, const std::vector<::lug::Graphics::Scene::Node*> nodes) {
19  // Generate hash
20  size_t hash = nodes.size() * 2;
21  for (auto node : nodes) {
22  hash ^= reinterpret_cast<size_t>(node) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
23  hash ^= node->getLight()->getHandle().value + 0x9e3779b9 + (hash << 6) + (hash >> 2);
24  }
25 
26  const auto& result = BufferPool::allocate(hash, std::any_of(nodes.cbegin(), nodes.cend(), [&currentFrame](const ::lug::Graphics::Scene::Node* node) {
27  return node->getLight()->isDirty(currentFrame) || node->isDirty(currentFrame);
28  }));
29 
30  for (auto node : nodes) {
31  node->getLight()->clearDirty(currentFrame);
32  node->clearDirty(currentFrame);
33  }
34 
35  if (std::get<0>(result) && std::get<1>(result)) {
36  // Update the buffer if the BufferPool told us that we need to
37  for (uint32_t i = 0; i < nodes.size(); ++i) {
38  const auto& light = nodes[i]->getLight();
39 
41  light->getData(lightData, *nodes[i]);
42 
43  cmdBuffer.updateBuffer(
44  *std::get<1>(result)->getBuffer(),
45  &lightData, sizeof(lightData),
46  std::get<1>(result)->getOffset() + ::lug::Graphics::Render::Light::strideShader * i
47  );
48  }
49 
50  const uint32_t lightsNb = static_cast<uint32_t>(nodes.size());
51  cmdBuffer.updateBuffer(
52  *std::get<1>(result)->getBuffer(),
53  &lightsNb, sizeof(uint32_t),
54  std::get<1>(result)->getOffset() + ::lug::Graphics::Render::Light::strideShader * 50);
55  }
56 
57  return std::get<1>(result);
58 }
59 
60 } // BufferPool
61 } // Render
62 } // Vulkan
63 } // Graphics
64 } // lug
std::tuple< bool, const SubBuffer * > allocate(size_t hash, bool dirty)
Definition: BufferPool.inl:7
const API::Queue * getQueue(const std::string &queueName) const
Definition: Device.cpp:70
const QueueFamily * getQueueFamily() const
Definition: Queue.cpp:86
const SubBuffer * allocate(uint32_t currentFrame, const API::CommandBuffer &cmdBuffer, const std::vector<::lug::Graphics::Scene::Node *> nodes)
Definition: Light.cpp:18
static constexpr uint32_t strideShader
Definition: Light.hpp:51