Cogs.Core
BlueNoiseManager.cpp
1#include "BlueNoiseManager.h"
2
3#include "Resources/TextureManager.h"
4
5#include "Services/Random.h"
6#include "Services/Variables.h"
7
8#include "Foundation/Logging/Logger.h"
9
10namespace
11{
12 Cogs::Logging::Log logger = Cogs::Logging::getLogger("BlueNoiseManager");
13}
14
15namespace Cogs::Core
16{
17 BlueNoiseManager::BlueNoiseManager(Context *context):
18 ResourceManager(context)
19 {
20 for(auto &tmp : blueNoise) tmp = BlueNoiseHandle::NoHandle;
21 count = context->variables->get("renderer.blueNoiseTextures", 4);
22 assert(count <= sizeof(blueNoise)/sizeof(blueNoise[0]));
23 context->blueNoiseManager = this;
24 }
25 BlueNoiseManager::~BlueNoiseManager()
26 {
27 reportLeaks("BlueNoise");
28 }
29
30 void BlueNoiseManager::initialize()
31 {
32 ResourceManager::initialize();
33 }
34
35 void BlueNoiseManager::handleEnable()
36 {
37 enabled = true;
38 for (size_t i = 0; i<count; i++) {
39 std::string name = "Textures/LDR_RGBA_" + std::to_string(i) + ".png";
40 blueNoise[i] = loadBlueNoise(name.c_str(), getNextResourceId());
41 }
42 LOG_DEBUG(logger, "Enabled blue noise manager.");
43 }
44
45 void BlueNoiseManager::clear()
46 {
47 for(auto &tmp : blueNoise) tmp = BlueNoiseHandle::NoHandle;
48 ResourceManager::clear();
49 enabled = false;
50 }
51
52 uint32_t BlueNoiseManager::RandIndex()
53 {
54 auto & random = context->random;
55 static std::uniform_int_distribution<uint32_t> dist(0, uint32_t(count)-1);
56 return dist(random->mt);
57 }
58
59 BlueNoiseHandle BlueNoiseManager::loadBlueNoise(const std::string & name, const ResourceId resourceId)
60 {
61 auto textureManager = context->textureManager;
62
63 BlueNoiseHandle handle = getOrCreate(resourceId);
64
65 auto texture = textureManager->loadTexture(name, textureManager->getNextResourceId(), TextureLoadFlags::None);
66 if(HandleIsValid(texture)){
67 handle->texture = texture;
68 //texture->setName("BlueNoise:" + blueNoise->getName());
69 }
70
71 return handle;
72 }
73
74 BlueNoiseHandle BlueNoiseManager::getBlueNoiseHandle(bool stable) {
75 return blueNoise[stable ? 0 : RandIndex()];
76 }
77}
Log implementation class.
Definition: LogManager.h:139
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180