<commit_before>#include "./ha_buffer.h"
#include <Eigen/Dense>
#include <QLoggingCategory>
#include <vector>
#include <algorithm>
#include "./shader_program.h"
#include "./screen_quad.h"
#include "./managers.h"
#include "./object_manager.h"
#include "./volume_manager.h"
#include "./volume_data.h"
#include "./texture_manager.h"
#include "./transfer_function_manager.h"
#include "./random_texture_generator.h"
#include "../math/eigen.h"

namespace Graphics
{

QLoggingCategory channel("Graphics.HABuffer");

HABuffer::HABuffer(Eigen::Vector2i size) : size(size)
{
  offsets = new unsigned int[512];
}

HABuffer::~HABuffer()
{
  qCInfo(channel) << "Destructor";
  delete[] offsets;
}

void HABuffer::initialize(Gl *gl, std::shared_ptr<Managers> managers)
{
  this->gl = gl;

  clearQuad = std::make_shared<ScreenQuad>(":/shader/clearHABuffer.vert",
                                           ":/shader/clearHABuffer.frag");
  clearQuad->initialize(gl, managers);

  renderQuad = std::make_shared<ScreenQuad>(":/shader/renderHABuffer.vert",
                                            ":/shader/renderHABuffer.frag");
  renderQuad->initialize(gl, managers);

  initializeBufferHash();

  clearTimer.initialize(gl);
  buildTimer.initialize(gl);
  renderTimer.initialize(gl);

  RandomTextureGenerator randomTextureGenerator(managers->getTextureManager());
  noiseTexture = randomTextureGenerator.create(256, 256);
}

void HABuffer::updateNearAndFarPlanes(float nearvalue, float farvalue)
{
  zNear = nearvalue;
  zFar = farvalue;
}

void HABuffer::initializeBufferHash()
{
  habufferScreenSize = std::max(size[0], size[1]);
  uint recordCount = habufferScreenSize * habufferScreenSize * 32;
  habufferTableSize =
      std::max(habufferScreenSize,
               static_cast<uint>(ceil(sqrt(static_cast<float>(recordCount)))));
  tableElementCount = habufferTableSize * habufferTableSize;
  habufferCountsSize = habufferScreenSize * habufferScreenSize + 1;
  qCDebug(channel, "Screen size: %d %d\n# records: %d (%d x %d)\n", size.x(),
          size.y(), tableElementCount, habufferTableSize, habufferTableSize);

  // HA-Buffer records
  if (!recordsBuffer.isInitialized())
    recordsBuffer.initialize(gl, tableElementCount * sizeof(uint) * 2);
  else
    recordsBuffer.resize(tableElementCount * sizeof(uint) * 2);

  if (!countsBuffer.isInitialized())
    countsBuffer.initialize(gl, habufferCountsSize * sizeof(uint));
  else
    countsBuffer.resize(habufferCountsSize * sizeof(uint));

  if (!fragmentDataBuffer.isInitialized())
    fragmentDataBuffer.initialize(gl, tableElementCount * FRAGMENT_DATA_SIZE);
  else
    fragmentDataBuffer.resize(tableElementCount * FRAGMENT_DATA_SIZE);

  // clear counts
  countsBuffer.clear(0);

  gl->glMemoryBarrier(GL_ALL_BARRIER_BITS);

  qCDebug(channel) << "Memory usage:"
                   << ((tableElementCount * sizeof(uint) * 2 +
                        tableElementCount * FRAGMENT_DATA_SIZE +
                        (habufferScreenSize * habufferScreenSize + 1) *
                            sizeof(uint)) /
                       1024) /
                          1024.0f << "MB";
}

void HABuffer::clearAndPrepare(std::shared_ptr<Graphics::Managers> managers)
{
  clearTimer.start();

  for (int i = 0; i < 512; i++)
  {
    offsets[i] = rand() ^ (rand() << 8) ^ (rand() << 16);
    offsets[i] = offsets[i] % habufferTableSize;
  }

  auto clearShader = clearQuad->getShaderProgram();<commit_msg>Minor refactoring and more logging in HABuffer.<commit_after>#include "./ha_buffer.h"
#include <Eigen/Dense>
#include <QLoggingCategory>
#include <vector>
#include <algorithm>
#include "./shader_program.h"
#include "./screen_quad.h"
#include "./managers.h"
#include "./object_manager.h"
#include "./volume_manager.h"
#include "./volume_data.h"
#include "./texture_manager.h"
#include "./transfer_function_manager.h"
#include "./random_texture_generator.h"
#include "../math/eigen.h"

namespace Graphics
{

QLoggingCategory channel("Graphics.HABuffer");

HABuffer::HABuffer(Eigen::Vector2i size) : size(size)
{
  offsets = new unsigned int[512];
}

HABuffer::~HABuffer()
{
  qCInfo(channel) << "Destructor";
  delete[] offsets;
}

void HABuffer::initialize(Gl *gl, std::shared_ptr<Managers> managers)
{
  this->gl = gl;

  clearQuad = std::make_shared<ScreenQuad>(":/shader/clearHABuffer.vert",
                                           ":/shader/clearHABuffer.frag");
  clearQuad->initialize(gl, managers);

  renderQuad = std::make_shared<ScreenQuad>(":/shader/renderHABuffer.vert",
                                            ":/shader/renderHABuffer.frag");
  renderQuad->initialize(gl, managers);

  initializeBufferHash();

  clearTimer.initialize(gl);
  buildTimer.initialize(gl);
  renderTimer.initialize(gl);

  RandomTextureGenerator randomTextureGenerator(managers->getTextureManager());
  noiseTexture = randomTextureGenerator.create(256, 256);
}

void HABuffer::updateNearAndFarPlanes(float nearvalue, float farvalue)
{
  zNear = nearvalue;
  zFar = farvalue;
}

void HABuffer::initializeBufferHash()
{
  habufferScreenSize = std::max(size[0], size[1]);
  uint recordCount = habufferScreenSize * habufferScreenSize * 32;
  habufferTableSize =
      std::max(habufferScreenSize,
               static_cast<uint>(ceil(sqrt(static_cast<float>(recordCount)))));
  tableElementCount = habufferTableSize * habufferTableSize;
  habufferCountsSize = habufferScreenSize * habufferScreenSize + 1;
  int bufferSize = tableElementCount * sizeof(uint) * 2;
  qCInfo(channel, "Screen size: %d %d\n# records: %d (%d x %d) %dMB\n",
         size.x(), size.y(), tableElementCount, habufferTableSize,
         habufferTableSize, bufferSize / 1024 / 1024);

  // HA-Buffer records
  if (!recordsBuffer.isInitialized())
    recordsBuffer.initialize(gl, bufferSize);
  else
    recordsBuffer.resize(bufferSize);

  if (!countsBuffer.isInitialized())
    countsBuffer.initialize(gl, habufferCountsSize * sizeof(uint));
  else
    countsBuffer.resize(habufferCountsSize * sizeof(uint));

  if (!fragmentDataBuffer.isInitialized())
    fragmentDataBuffer.initialize(gl, tableElementCount * FRAGMENT_DATA_SIZE);
  else
    fragmentDataBuffer.resize(tableElementCount * FRAGMENT_DATA_SIZE);

  // clear counts
  countsBuffer.clear(0);

  gl->glMemoryBarrier(GL_ALL_BARRIER_BITS);

  qCInfo(channel) << "Memory usage:"
                  << ((bufferSize + tableElementCount * FRAGMENT_DATA_SIZE +
                       (habufferScreenSize * habufferScreenSize + 1) *
                           sizeof(uint)) /
                      1024) /
                         1024.0f
                  << "MB";
}

void HABuffer::clearAndPrepare(std::shared_ptr<Graphics::Managers> managers)
{
  clearTimer.start();

  for (int i = 0; i < 512; i++)
  {
    offsets[i] = rand() ^ (rand() << 8) ^ (rand() << 16);
    offsets[i] = offsets[i] % habufferTableSize;
  }

  auto clearShader = clearQuad->getShaderProgram();
{'fraction_non_alphanumeric': 0.13701195793113385, 'fraction_numerical': 0.011237573836622964, 'mean_word_length': 3.7580534612748457, 'pattern_counts': {'":': 8, '<': 49, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '744001', 'n_tokens_mistral': 2250, 'n_tokens_neox': 2164, 'n_words': 380}