<commit_before>#version 440
#include "HABufferImplementation.hglsl"
#include "bindlessTexture.hglsl"
#define MAX_SAMPLES 4096
#define STEP_FACTOR 2.5

in vec4 fragmentInputPosition;

layout(location = 0) out vec4 outputColor;
layout(location = 1) out vec4 position;
layout(location = 2) out vec4 outputColor2;
layout(location = 3) out vec4 position2;
layout(depth_any) out float gl_FragDepth;

uniform vec3 backgroundColor = vec3(1.0, 1.0, 1.0);
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 inverseViewMatrix;
uniform vec3 textureAtlasSize;
uniform vec3 sampleDistance;
uniform float alphaThresholdForDepth = 0.1;
uniform float depthCutOff = 0.0;

uniform sampler3D volumeSampler;

const float POSITION_NOT_SET = -2;

#define transferFunctionRowCount 64.0f

struct VolumeData
{
  Tex2DAddress textureAddress;
  mat4 textureMatrix;
  int volumeId;
  mat4 objectToDatasetMatrix;
  int transferFunctionRow;
};

layout(std430, binding = 1) buffer CB1
{
  VolumeData volumes[];
};

vec4 clampColor(in vec4 color)
{
  return clamp(color, vec4(0), vec4(1));
}

vec3 clampColor(in vec3 color)
{
  return clamp(color, vec3(0), vec3(1));
}

float getVolumeSampleDensity(in vec3 texturePos)
{
  return texture(volumeSampler, texturePos).r;
}

// gradient calculation based on dataset values using central differences
vec3 getVolumeSampleGradient(in int objectId, in vec3 texturePos)
{
  vec3 gradient = vec3(texture(volumeSampler, vec3(texturePos.x + sampleDistance.x, texturePos.yz)).x -
      texture(volumeSampler, vec3(texturePos.x - sampleDistance.x, texturePos.yz)).x,
      texture(volumeSampler, vec3(texturePos.x, texturePos.y + sampleDistance.y, texturePos.z)).x -
      texture(volumeSampler, vec3(texturePos.x, texturePos.y - sampleDistance.y, texturePos.z)).x,
      texture(volumeSampler, vec3(texturePos.xy, texturePos.z + sampleDistance.z)).x -
      texture(volumeSampler, vec3(texturePos.xy, texturePos.z - sampleDistance.z)).x);

  return normalize(mat3(viewMatrix) *
                   transpose(mat3(volumes[objectId].textureMatrix)) * -gradient);
}

// Blending equation for in-order traversal
vec4 blend(vec4 clr, vec4 srf)
{
  return clr + (1.0 - clr.a) * vec4(srf.rgb * srf.a, srf.a);
}

bool fetchFragment(in uvec2 ij, in uint age, out FragmentData fragment)
{
  uvec2 l = (ij + offsets[age]);
  uint64_t h = Haddr(l % uvec2(tableSize));

  uint64_t rec = records[h];

  uint32_t key = uint32_t(rec >> uint64_t(32));

  if (HA_AGE(key) == age)
  {
    fragment = fragmentData[uint32_t(rec)];
    return true;
  }

  return false;
}

void updateActiveObjects(inout int objectId, inout int activeObjects)
{
  if (objectId > 0)
  {
    activeObjects |= 1 << (objectId - 1);
  }
  else if (objectId < 0)
  {
    objectId = -objectId;
    activeObjects &= (~(1 << (objectId - 1)));
  }
}

vec3 calculateLighting(vec4 color, vec3 startPos_eye, vec3 gradient)
{
  const vec3 lightPos = vec3(0.0f, 0.0f, 0.0f);
  vec3 lightDir = normalize(lightPos - startPos_eye);
  vec3 viewDir = -1.0f * normalize(startPos_eye);
  vec3 normalizedGradient = normalize(gradient);

  float dotNL = max(dot(normalizedGradient, lightDir), 0.0f);
  vec3 H = normalize(lightDir + viewDir);
  float dotNH = max(dot(normalizedGradient, H), 0.0f);
  const float ambient = 0.3;
  const float diffuse = 0.5;
  const float specular = 0.5;
  const float shininess = 32.0f;
  const vec3 specularColor = vec3(1.0, 1.0, 1.0);

  vec3 specularTerm = (shininess + 2.0f) / (2.0f * 3.1415f) * specular *
                      color.a * specularColor * pow(dotNH, shininess);
  return ambient * color.rgb + diffuse * color.rgb * dotNL + specularTerm;
}

vec4 transferFunctionLookUp(int volumeId, float density)
{
  float row = volumes[volumeId].transferFunctionRow;

  return Texture(volumes[volumeId].textureAddress,
                 vec2(density, row / (transferFunctionRowCount - 1.0f)));
}

int calculateNextObjectId(inout uint remainingActiveObjects)
{
  int currentObjectId = findLSB(remainingActiveObjects);
  remainingActiveObjects &= (~(1 << (currentObjectId)));

  return currentObjectId;
}

float calculateSegmentTextureLength(int activeObjectCount, uint activeObjects,
    vec4 currentFragmentPos_eye, vec4 nextFragmentPos_eye)
{
  float segmentTextureLength = 0.0f;
  for (int oi = 0; oi < activeObjectCount; oi++)
  {
    int currentObjectId = calculateNextObjectId(activeObjects);

    vec4 textureStartPos = volumes[currentObjectId].textureMatrix *
                           inverseViewMatrix * currentFragmentPos_eye;
    vec4 textureEndPos = volumes[currentObjectId].textureMatrix *
                         inverseViewMatrix * nextFragmentPos_eye;

    segmentTextureLength = max(distance(textureStartPos.xyz * textureAtlasSize,
                                        textureEndPos.xyz * textureAtlasSize),
                               segmentTextureLength);
  }

  return segmentTextureLength;
}

void setPositionAndDepth(vec4 positionInEyeSpace)
{
  if (position.w == POSITION_NOT_SET)
  {
    vec4 ndcPos = projectionMatrix * positionInEyeSpace;
    ndcPos = ndcPos / ndcPos.w;
    float depth = ndcPos.z;
    gl_FragDepth = depth;
    position.xyz = ndcPos.xyz;
    position.w = 1.0f;
  }
  else if (position2.w == POSITION_NOT_SET)
  {
    vec4 ndcPos = projectionMatrix * positionInEyeSpace;
    ndcPos = ndcPos / ndcPos.w;<commit_msg>Extract fromEyeToNdcSpace function.<commit_after>#version 440
#include "HABufferImplementation.hglsl"
#include "bindlessTexture.hglsl"
#define MAX_SAMPLES 4096
#define STEP_FACTOR 2.5

in vec4 fragmentInputPosition;

layout(location = 0) out vec4 outputColor;
layout(location = 1) out vec4 position;
layout(location = 2) out vec4 outputColor2;
layout(location = 3) out vec4 position2;
layout(depth_any) out float gl_FragDepth;

uniform vec3 backgroundColor = vec3(1.0, 1.0, 1.0);
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 inverseViewMatrix;
uniform vec3 textureAtlasSize;
uniform vec3 sampleDistance;
uniform float alphaThresholdForDepth = 0.1;
uniform float depthCutOff = 0.0;

uniform sampler3D volumeSampler;

const float POSITION_NOT_SET = -2;

#define transferFunctionRowCount 64.0f

struct VolumeData
{
  Tex2DAddress textureAddress;
  mat4 textureMatrix;
  int volumeId;
  mat4 objectToDatasetMatrix;
  int transferFunctionRow;
};

layout(std430, binding = 1) buffer CB1
{
  VolumeData volumes[];
};

vec4 clampColor(in vec4 color)
{
  return clamp(color, vec4(0), vec4(1));
}

vec3 clampColor(in vec3 color)
{
  return clamp(color, vec3(0), vec3(1));
}

float getVolumeSampleDensity(in vec3 texturePos)
{
  return texture(volumeSampler, texturePos).r;
}

// gradient calculation based on dataset values using central differences
vec3 getVolumeSampleGradient(in int objectId, in vec3 texturePos)
{
  vec3 gradient = vec3(texture(volumeSampler, vec3(texturePos.x + sampleDistance.x, texturePos.yz)).x -
      texture(volumeSampler, vec3(texturePos.x - sampleDistance.x, texturePos.yz)).x,
      texture(volumeSampler, vec3(texturePos.x, texturePos.y + sampleDistance.y, texturePos.z)).x -
      texture(volumeSampler, vec3(texturePos.x, texturePos.y - sampleDistance.y, texturePos.z)).x,
      texture(volumeSampler, vec3(texturePos.xy, texturePos.z + sampleDistance.z)).x -
      texture(volumeSampler, vec3(texturePos.xy, texturePos.z - sampleDistance.z)).x);

  return normalize(mat3(viewMatrix) *
                   transpose(mat3(volumes[objectId].textureMatrix)) * -gradient);
}

// Blending equation for in-order traversal
vec4 blend(vec4 clr, vec4 srf)
{
  return clr + (1.0 - clr.a) * vec4(srf.rgb * srf.a, srf.a);
}

bool fetchFragment(in uvec2 ij, in uint age, out FragmentData fragment)
{
  uvec2 l = (ij + offsets[age]);
  uint64_t h = Haddr(l % uvec2(tableSize));

  uint64_t rec = records[h];

  uint32_t key = uint32_t(rec >> uint64_t(32));

  if (HA_AGE(key) == age)
  {
    fragment = fragmentData[uint32_t(rec)];
    return true;
  }

  return false;
}

void updateActiveObjects(inout int objectId, inout int activeObjects)
{
  if (objectId > 0)
  {
    activeObjects |= 1 << (objectId - 1);
  }
  else if (objectId < 0)
  {
    objectId = -objectId;
    activeObjects &= (~(1 << (objectId - 1)));
  }
}

vec3 calculateLighting(vec4 color, vec3 startPos_eye, vec3 gradient)
{
  const vec3 lightPos = vec3(0.0f, 0.0f, 0.0f);
  vec3 lightDir = normalize(lightPos - startPos_eye);
  vec3 viewDir = -1.0f * normalize(startPos_eye);
  vec3 normalizedGradient = normalize(gradient);

  float dotNL = max(dot(normalizedGradient, lightDir), 0.0f);
  vec3 H = normalize(lightDir + viewDir);
  float dotNH = max(dot(normalizedGradient, H), 0.0f);
  const float ambient = 0.3;
  const float diffuse = 0.5;
  const float specular = 0.5;
  const float shininess = 32.0f;
  const vec3 specularColor = vec3(1.0, 1.0, 1.0);

  vec3 specularTerm = (shininess + 2.0f) / (2.0f * 3.1415f) * specular *
                      color.a * specularColor * pow(dotNH, shininess);
  return ambient * color.rgb + diffuse * color.rgb * dotNL + specularTerm;
}

vec4 transferFunctionLookUp(int volumeId, float density)
{
  float row = volumes[volumeId].transferFunctionRow;

  return Texture(volumes[volumeId].textureAddress,
                 vec2(density, row / (transferFunctionRowCount - 1.0f)));
}

int calculateNextObjectId(inout uint remainingActiveObjects)
{
  int currentObjectId = findLSB(remainingActiveObjects);
  remainingActiveObjects &= (~(1 << (currentObjectId)));

  return currentObjectId;
}

float calculateSegmentTextureLength(int activeObjectCount, uint activeObjects,
    vec4 currentFragmentPos_eye, vec4 nextFragmentPos_eye)
{
  float segmentTextureLength = 0.0f;
  for (int oi = 0; oi < activeObjectCount; oi++)
  {
    int currentObjectId = calculateNextObjectId(activeObjects);

    vec4 textureStartPos = volumes[currentObjectId].textureMatrix *
                           inverseViewMatrix * currentFragmentPos_eye;
    vec4 textureEndPos = volumes[currentObjectId].textureMatrix *
                         inverseViewMatrix * nextFragmentPos_eye;

    segmentTextureLength = max(distance(textureStartPos.xyz * textureAtlasSize,
                                        textureEndPos.xyz * textureAtlasSize),
                               segmentTextureLength);
  }

  return segmentTextureLength;
}

vec4 fromEyeToNdcSpace(vec4 positionInEyeSpace)
{
  vec4 ndcPos = projectionMatrix * positionInEyeSpace;
  return ndcPos / ndcPos.w;
}

void setPositionAndDepth(in vec4 positionInEyeSpace)
{
  if (position.w == POSITION_NOT_SET)
  {
    vec4 ndcPos = fromEyeToNdcSpace(positionInEyeSpace);
    float depth = ndcPos.z;
    gl_FragDepth = depth;
    position.xyz = ndcPos.xyz;
    position.w = 1.0f;
  }
  else if (position2.w == POSITION_NOT_SET)
  {
    vec4 ndcPos = fromEyeToNdcSpace(positionInEyeSpace);
{'fraction_non_alphanumeric': 0.10550161812297734, 'fraction_numerical': 0.0319001386962552, 'mean_word_length': 3.9591930307198533, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '100832', 'n_tokens_mistral': 3792, 'n_tokens_neox': 3571, 'n_words': 844}