<commit_before>#ifdef MINSG_EXT_THESISSTANISLAW
#include <iostream>
#include <iomanip>
#define LIB_GL
#define LIB_GLEW

#include "PhotonSampler.h"

#include "../../Core/FrameContext.h"

#include "../../../Rendering/Shader/Uniform.h"
#include "../../../Rendering/GLHeader.h"
#include "../../../Rendering/Texture/TextureUtils.h"
#include "../../../Rendering/RenderingContext/RenderingParameters.h"

#include "../../../Util/Graphics/PixelAccessor.h"
#include "../../../Util/Graphics/Bitmap.h"
#include "../../../Util/Graphics/PixelFormat.h"

#include "../../../Rendering/Mesh/MeshVertexData.h"
#include "../../../Rendering/Mesh/MeshDataStrategy.h"
#include "../../../Rendering/Mesh/VertexDescription.h"
#include "../../../Rendering/Mesh/VertexAttributeAccessors.h"
#include "../../../Rendering/Mesh/VertexAttributeIds.h"
#include "../../../Rendering/MeshUtils/MeshUtils.h"

#include "SamplingPatterns/PoissonGenerator.h"
#include "PhotonRenderer.h"

#include "../../../Geometry/SRT.h"

namespace MinSG{
namespace ThesisStanislaw{
  
const std::string PhotonSampler::_shaderPath = "ThesisStanislaw/ShaderScenes/shader/";
  
PhotonSampler::PhotonSampler() :
  State(),
  _fbo(nullptr), _depthTexture(nullptr), _posTexture(nullptr), _normalTexture(nullptr),
  _fboChanged(true), _camera(nullptr), _mrtShader(nullptr), _photonMatrixShader(nullptr), 
  _approxScene(nullptr), _photonNumber(50), _samplingMesh(nullptr), _photonBufferGLId(0)
{
  _mrtShader = Rendering::Shader::loadShader(Util::FileName(_shaderPath + "MRT.vs"), Util::FileName(_shaderPath + "MRT.fs"), Rendering::Shader::USE_UNIFORMS);
  _photonMatrixShader = Rendering::Shader::loadShader(Util::FileName(_shaderPath + "photonMatrices.vs"), Util::FileName(_shaderPath + "photonMatrices.fs"), Rendering::Shader::USE_UNIFORMS);
  resample();
  initializeSamplePointMesh();
}

void PhotonSampler::initializeSamplePointMesh(){
  // create mesh
  Rendering::VertexDescription vertexDesc;
  vertexDesc.appendPosition3D();
  vertexDesc.appendTexCoord(0);
  vertexDesc.appendUnsignedIntAttribute(Util::StringIdentifier("sg_PhotonID"), static_cast<uint8_t>(1));
  
  _samplingMesh = new Rendering::Mesh(vertexDesc, _photonNumber, 0);
  _samplingMesh->setDataStrategy(Rendering::SimpleMeshDataStrategy::getPureLocalStrategy());
  _samplingMesh->setUseIndexData(false);
  _samplingMesh->setDrawMode(Rendering::Mesh::draw_mode_t::DRAW_POINTS);
  
  // init vertex data
  auto& vertexData = _samplingMesh->openVertexData();

  vertexData.allocate(_samplePoints.size(), vertexDesc);
  
  auto posAcc = Rendering::PositionAttributeAccessor::create(vertexData, Rendering::VertexAttributeIds::POSITION);
  auto texAcc = Rendering::TexCoordAttributeAccessor::create(vertexData, Rendering::VertexAttributeIds::TEXCOORD0);
  auto IDAcc = Rendering::UIntAttributeAccessor::create(vertexData, Util::StringIdentifier("sg_PhotonID"));
  auto mapPos = [](const Geometry::Vec2& p){ return Geometry::Vec3(p.x()*2.f - 1.f, p.y()*2.f - 1.f, 0.f);}; //map [0,1] to [-1,1]
  
  for(uint32_t i = 0; i < _samplePoints.size(); i++){
    posAcc->setPosition(i, mapPos(_samplePoints[i]));
    texAcc->setCoordinate(i, _samplePoints[i]);
    IDAcc->setValue(i, i);
  }
  
  // Initialize ssbo to store the photon matrices
  if(_photonBufferGLId != 0){
    glDeleteBuffers(1, &_photonBufferGLId);
  }

  glGenBuffers(1, &_photonBufferGLId);
  glBindBuffer(GL_SHADER_STORAGE_BUFFER, _photonBufferGLId);
  glBufferData(GL_SHADER_STORAGE_BUFFER, _photonNumber * sizeof(float) * 28 , NULL, GL_STATIC_READ);
  glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);

}

void PhotonSampler::computePhotonMatrices(Rendering::RenderingContext& rc){
  rc.pushAndSetFBO(_fbo2.get());
  rc.pushAndSetShader(_photonMatrixShader.get());
  rc.clearDepth(1.f);
  
  bindPhotonBuffer(1);
  rc.pushAndSetTexture(0, _posTexture.get());
  rc.pushAndSetTexture(1, _normalTexture.get());
  
  rc.pushAndSetDepthBuffer(Rendering::DepthBufferParameters(false, false, Rendering::Comparison::LESS));
  rc.pushAndSetLighting(Rendering::LightingParameters(false));
  
  rc.pushMatrix_cameraToClipping();
  rc.setMatrix_cameraToClipping(Geometry::Matrix4x4::orthographicProjection(0, getTextureWidth(), 0, getTextureHeight(), -1, 1));
  
  Geometry::Matrix4x4 identityMatrix;
  identityMatrix.setIdentity();

  rc.pushMatrix_modelToCamera();
  rc.setMatrix_modelToCamera(identityMatrix);
  
  rc.displayMesh(_samplingMesh.get());
  
  rc.popMatrix_modelToCamera();
  rc.popMatrix_cameraToClipping();
  rc.popLighting();
  rc.popDepthBuffer();
  rc.popTexture(1);
  rc.popTexture(0);
  unbindPhotonBuffer(1);
  rc.popShader();
  rc.popFBO();
  
}

void PhotonSampler::allocateSamplingTexture(std::vector<int>& samplingImage){<commit_msg>Don't use fbo for photon matrices.<commit_after>#ifdef MINSG_EXT_THESISSTANISLAW
#include <iostream>
#include <iomanip>
#define LIB_GL
#define LIB_GLEW

#include "PhotonSampler.h"

#include "../../Core/FrameContext.h"

#include "../../../Rendering/Shader/Uniform.h"
#include "../../../Rendering/GLHeader.h"
#include "../../../Rendering/Texture/TextureUtils.h"
#include "../../../Rendering/RenderingContext/RenderingParameters.h"

#include "../../../Util/Graphics/PixelAccessor.h"
#include "../../../Util/Graphics/Bitmap.h"
#include "../../../Util/Graphics/PixelFormat.h"

#include "../../../Rendering/Mesh/MeshVertexData.h"
#include "../../../Rendering/Mesh/MeshDataStrategy.h"
#include "../../../Rendering/Mesh/VertexDescription.h"
#include "../../../Rendering/Mesh/VertexAttributeAccessors.h"
#include "../../../Rendering/Mesh/VertexAttributeIds.h"
#include "../../../Rendering/MeshUtils/MeshUtils.h"

#include "SamplingPatterns/PoissonGenerator.h"
#include "PhotonRenderer.h"

#include "../../../Geometry/SRT.h"

namespace MinSG{
namespace ThesisStanislaw{
  
const std::string PhotonSampler::_shaderPath = "ThesisStanislaw/ShaderScenes/shader/";
  
PhotonSampler::PhotonSampler() :
  State(),
  _fbo(nullptr), _depthTexture(nullptr), _posTexture(nullptr), _normalTexture(nullptr),
  _fboChanged(true), _camera(nullptr), _mrtShader(nullptr), _photonMatrixShader(nullptr), 
  _approxScene(nullptr), _photonNumber(50), _samplingMesh(nullptr), _photonBufferGLId(0)
{
  _mrtShader = Rendering::Shader::loadShader(Util::FileName(_shaderPath + "MRT.vs"), Util::FileName(_shaderPath + "MRT.fs"), Rendering::Shader::USE_UNIFORMS);
  _photonMatrixShader = Rendering::Shader::loadShader(Util::FileName(_shaderPath + "photonMatrices.vs"), Util::FileName(_shaderPath + "photonMatrices.fs"), Rendering::Shader::USE_UNIFORMS);
  resample();
  initializeSamplePointMesh();
}

void PhotonSampler::initializeSamplePointMesh(){
  // create mesh
  Rendering::VertexDescription vertexDesc;
  vertexDesc.appendPosition3D();
  vertexDesc.appendTexCoord(0);
  vertexDesc.appendUnsignedIntAttribute(Util::StringIdentifier("sg_PhotonID"), static_cast<uint8_t>(1));
  
  _samplingMesh = new Rendering::Mesh(vertexDesc, _photonNumber, 0);
  _samplingMesh->setDataStrategy(Rendering::SimpleMeshDataStrategy::getPureLocalStrategy());
  _samplingMesh->setUseIndexData(false);
  _samplingMesh->setDrawMode(Rendering::Mesh::draw_mode_t::DRAW_POINTS);
  
  // init vertex data
  auto& vertexData = _samplingMesh->openVertexData();

  vertexData.allocate(_samplePoints.size(), vertexDesc);
  
  auto posAcc = Rendering::PositionAttributeAccessor::create(vertexData, Rendering::VertexAttributeIds::POSITION);
  auto texAcc = Rendering::TexCoordAttributeAccessor::create(vertexData, Rendering::VertexAttributeIds::TEXCOORD0);
  auto IDAcc = Rendering::UIntAttributeAccessor::create(vertexData, Util::StringIdentifier("sg_PhotonID"));
  auto mapPos = [](const Geometry::Vec2& p){ return Geometry::Vec3(p.x()*2.f - 1.f, p.y()*2.f - 1.f, 0.f);}; //map [0,1] to [-1,1]
  
  for(uint32_t i = 0; i < _samplePoints.size(); i++){
    posAcc->setPosition(i, mapPos(_samplePoints[i]));
    texAcc->setCoordinate(i, _samplePoints[i]);
    IDAcc->setValue(i, i);
  }
  
  // Initialize ssbo to store the photon matrices
  if(_photonBufferGLId != 0){
    glDeleteBuffers(1, &_photonBufferGLId);
  }

  glGenBuffers(1, &_photonBufferGLId);
  glBindBuffer(GL_SHADER_STORAGE_BUFFER, _photonBufferGLId);
  glBufferData(GL_SHADER_STORAGE_BUFFER, _photonNumber * sizeof(float) * 28 , NULL, GL_STATIC_READ);
  glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);

}

void PhotonSampler::computePhotonMatrices(Rendering::RenderingContext& rc){
  //rc.pushAndSetFBO(_fbo2.get());
  rc.pushAndSetShader(_photonMatrixShader.get());
  rc.clearDepth(1.f);
  
  bindPhotonBuffer(1);
  rc.pushAndSetTexture(0, _posTexture.get());
  rc.pushAndSetTexture(1, _normalTexture.get());
  
  rc.pushAndSetDepthBuffer(Rendering::DepthBufferParameters(false, false, Rendering::Comparison::LESS));
  rc.pushAndSetLighting(Rendering::LightingParameters(false));
  
  rc.pushMatrix_cameraToClipping();
  rc.setMatrix_cameraToClipping(Geometry::Matrix4x4::orthographicProjection(0, getTextureWidth(), 0, getTextureHeight(), -1, 1));
  
  Geometry::Matrix4x4 identityMatrix;
  identityMatrix.setIdentity();

  rc.pushMatrix_modelToCamera();
  rc.setMatrix_modelToCamera(identityMatrix);
  
  rc.displayMesh(_samplingMesh.get());
  
  rc.popMatrix_modelToCamera();
  rc.popMatrix_cameraToClipping();
  rc.popLighting();
  rc.popDepthBuffer();
  rc.popTexture(1);
  rc.popTexture(0);
  unbindPhotonBuffer(1);
  rc.popShader();
  //rc.popFBO();
  
}

void PhotonSampler::allocateSamplingTexture(std::vector<int>& samplingImage){
{'fraction_non_alphanumeric': 0.1635459642441553, 'fraction_numerical': 0.00952078705172961, 'mean_word_length': 5.977121771217712, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7232499', 'n_tokens_mistral': 3186, 'n_tokens_neox': 3171, 'n_words': 424}