<commit_before>/*
 * Copyright (c), <NAME>.
 *
 * MIT License (MIT), http://opensource.org/licenses/MIT
 * Full license can be found in the LICENSE file
 */

#include "../common.h"
#include "../../include/gk/gk.h"
#include "../../include/gk/pass.h"
#include "../../include/gk/scene.h"
#include "../gpu_state/common.h"

GK_EXPORT
GkPassOut*
gkAllocPassOut(void) {
  GkPassOut *pout;

  pout = calloc(1, sizeof(*pout));
  glGenFramebuffers(1, &pout->fbo);

  return pout;
}

GK_EXPORT
GkPassOut*
gkCurrentOutput(GkContext * __restrict ctx) {
  GkPassOut *output;

  if ((output = ctx->currState->outputState.renderOutput))
    return output;

  return gkDefaultRenderOut();
}

GK_EXPORT
void
gkBindPassOut(GkScene   *scene,
              GkPassOut *pout) {
  GkRenderOutState *state;
  GkContext        *ctx;

  ctx   = gkContextOf(scene);
  state = gkGetOrCreatState(ctx, GK_GPUSTATE_RENDER_OUT);
  if (ctx->currState->outputState.renderOutput == pout)
    return;

  state->renderOutput = pout;

  gkStateMakeCurrent(ctx, &state->base);
  glBindFramebuffer(GL_FRAMEBUFFER, pout->fbo);
}

GK_EXPORT
void
gkBindDefaultPassOut(GkScene *scene) {
  GkRenderOutState *state;
  GkContext        *ctx;

  ctx   = gkContextOf(scene);
  state = gkGetOrCreatState(ctx, GK_GPUSTATE_RENDER_OUT);
  if (ctx->currState->outputState.renderOutput == NULL)
    return;

  state->renderOutput = NULL;
  gkStateMakeCurrent(ctx, &state->base);
  glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

GK_EXPORT
void
gkPassEnableDepth(GkScene *scene,
                  GkPass  *pass) {
  GkPassOut *pout;

  if (!(pout = pass->output))
    pass->output = pout = gkAllocPassOut();

  if (pout->depth != 0)
    return;

  glGenRenderbuffers(1, &pout->depth);
  glBindRenderbuffer(GL_RENDERBUFFER, pout->depth);

  glRenderbufferStorage(GL_RENDERBUFFER,
                        GL_DEPTH_COMPONENT,
                        scene->vrect.size.w * scene->backingScale,
                        scene->vrect.size.h * scene->backingScale);

  glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                            GL_DEPTH_ATTACHMENT,
                            GL_RENDERBUFFER,
                            pout->depth);
}

GK_EXPORT
void
gkPassEnableDepthTex(GkScene *scene,
                     GkPass  *pass) {
  GkPassOut *pout, *currentOutput;

  if (!(pout = pass->output))
    pass->output = pout = gkAllocPassOut();

  if (pout->depth != 0)
    return;

  currentOutput = gkCurrentOutput(gkContextOf(scene));
  if (currentOutput != pout)
    gkBindPassOut(scene, pout);

  glGenTextures(1, &pout->depth);
  glBindTexture(GL_TEXTURE_2D, pout->depth);

  glTexImage2D(GL_TEXTURE_2D,
               0,
               GL_DEPTH_COMPONENT16, /* todo: add option for this */
               scene->vrect.size.w * scene->backingScale,
               scene->vrect.size.h * scene->backingScale,
               0,
               GL_DEPTH_COMPONENT,
               GL_FLOAT,
               NULL);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,   GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,   GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,       GL_CLAMP_TO_EDGE);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,       GL_CLAMP_TO_EDGE);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  glTexParameteri(GL_TEXTURE_2D,
                  GL_TEXTURE_COMPARE_MODE,
                  GL_COMPARE_REF_TO_TEXTURE);

  glFramebufferTexture2D(GL_FRAMEBUFFER,
                         GL_DEPTH_ATTACHMENT,
                         GL_TEXTURE_2D,
                         pout->depth,
                         0);

  if (currentOutput != pout)
    gkBindPassOut(scene, currentOutput);
}

GK_EXPORT
void
gkPassEnableDepthTexArray(GkScene *scene, GkPass *pass, GLsizei len) {
  GkPassOut *pout, *currentOutput;

  if (!(pout = pass->output))
    pass->output = pout = gkAllocPassOut();

  if (pout->depth != 0)
    return;

  currentOutput = gkCurrentOutput(gkContextOf(scene));
  if (currentOutput != pout)
    gkBindPassOut(scene, pout);

  glGenTextures(1, &pout->depth);
  glBindTexture(GL_TEXTURE_2D_ARRAY, pout->depth);

  glTexImage3D(GL_TEXTURE_2D_ARRAY,
               0,
               GL_DEPTH_COMPONENT24, /* todo: add option for this */
               scene->vrect.size.w * scene->backingScale,
               scene->vrect.size.h * scene->backingScale,
               len,
               0,
               GL_DEPTH_COMPONENT,
               GL_FLOAT,
               NULL);

  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  glTexParameteri(GL_TEXTURE_2D_ARRAY,
                  GL_TEXTURE_COMPARE_MODE,
                  GL_COMPARE_REF_TO_TEXTURE);

  glFramebufferTextureLayer(GL_FRAMEBUFFER,
                            GL_DEPTH_ATTACHMENT,
                            pout->depth,
                            0,
                            0);

  if (currentOutput != pout)
    gkBindPassOut(scene, currentOutput);
}

GK_EXPORT
GkPassOutColor*
gkGetRenderTarget(GkPass *pass, int32_t index) {
  GkPassOut      *pout;
  GkPassOutColor *poc;

  if (!(pout = pass->output) || !(poc = pout->color))
    return NULL;

  while (index > 0) {
    poc = poc->next;
    index--;
  }

  return poc;
}

GK_EXPORT<commit_msg>remove redundant setting layer and make depth 24<commit_after>/*
 * Copyright (c), <NAME>.
 *
 * MIT License (MIT), http://opensource.org/licenses/MIT
 * Full license can be found in the LICENSE file
 */

#include "../common.h"
#include "../../include/gk/gk.h"
#include "../../include/gk/pass.h"
#include "../../include/gk/scene.h"
#include "../gpu_state/common.h"

GK_EXPORT
GkPassOut*
gkAllocPassOut(void) {
  GkPassOut *pout;

  pout = calloc(1, sizeof(*pout));
  glGenFramebuffers(1, &pout->fbo);

  return pout;
}

GK_EXPORT
GkPassOut*
gkCurrentOutput(GkContext * __restrict ctx) {
  GkPassOut *output;

  if ((output = ctx->currState->outputState.renderOutput))
    return output;

  return gkDefaultRenderOut();
}

GK_EXPORT
void
gkBindPassOut(GkScene   *scene,
              GkPassOut *pout) {
  GkRenderOutState *state;
  GkContext        *ctx;

  ctx   = gkContextOf(scene);
  state = gkGetOrCreatState(ctx, GK_GPUSTATE_RENDER_OUT);
  if (ctx->currState->outputState.renderOutput == pout)
    return;

  state->renderOutput = pout;

  gkStateMakeCurrent(ctx, &state->base);
  glBindFramebuffer(GL_FRAMEBUFFER, pout->fbo);
}

GK_EXPORT
void
gkBindDefaultPassOut(GkScene *scene) {
  GkRenderOutState *state;
  GkContext        *ctx;

  ctx   = gkContextOf(scene);
  state = gkGetOrCreatState(ctx, GK_GPUSTATE_RENDER_OUT);
  if (ctx->currState->outputState.renderOutput == NULL)
    return;

  state->renderOutput = NULL;
  gkStateMakeCurrent(ctx, &state->base);
  glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

GK_EXPORT
void
gkPassEnableDepth(GkScene *scene,
                  GkPass  *pass) {
  GkPassOut *pout;

  if (!(pout = pass->output))
    pass->output = pout = gkAllocPassOut();

  if (pout->depth != 0)
    return;

  glGenRenderbuffers(1, &pout->depth);
  glBindRenderbuffer(GL_RENDERBUFFER, pout->depth);

  glRenderbufferStorage(GL_RENDERBUFFER,
                        GL_DEPTH_COMPONENT,
                        scene->vrect.size.w * scene->backingScale,
                        scene->vrect.size.h * scene->backingScale);

  glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                            GL_DEPTH_ATTACHMENT,
                            GL_RENDERBUFFER,
                            pout->depth);
}

GK_EXPORT
void
gkPassEnableDepthTex(GkScene *scene,
                     GkPass  *pass) {
  GkPassOut *pout, *currentOutput;

  if (!(pout = pass->output))
    pass->output = pout = gkAllocPassOut();

  if (pout->depth != 0)
    return;

  currentOutput = gkCurrentOutput(gkContextOf(scene));
  if (currentOutput != pout)
    gkBindPassOut(scene, pout);

  glGenTextures(1, &pout->depth);
  glBindTexture(GL_TEXTURE_2D, pout->depth);

  glTexImage2D(GL_TEXTURE_2D,
               0,
               GL_DEPTH_COMPONENT24, /* todo: add option for this */
               scene->vrect.size.w * scene->backingScale,
               scene->vrect.size.h * scene->backingScale,
               0,
               GL_DEPTH_COMPONENT,
               GL_FLOAT,
               NULL);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,   GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,   GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,       GL_CLAMP_TO_EDGE);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,       GL_CLAMP_TO_EDGE);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  glTexParameteri(GL_TEXTURE_2D,
                  GL_TEXTURE_COMPARE_MODE,
                  GL_COMPARE_REF_TO_TEXTURE);

  glFramebufferTexture2D(GL_FRAMEBUFFER,
                         GL_DEPTH_ATTACHMENT,
                         GL_TEXTURE_2D,
                         pout->depth,
                         0);

  if (currentOutput != pout)
    gkBindPassOut(scene, currentOutput);
}

GK_EXPORT
void
gkPassEnableDepthTexArray(GkScene *scene, GkPass *pass, GLsizei len) {
  GkPassOut *pout, *currentOutput;

  if (!(pout = pass->output))
    pass->output = pout = gkAllocPassOut();

  if (pout->depth != 0)
    return;

  currentOutput = gkCurrentOutput(gkContextOf(scene));
  if (currentOutput != pout)
    gkBindPassOut(scene, pout);

  glGenTextures(1, &pout->depth);
  glBindTexture(GL_TEXTURE_2D_ARRAY, pout->depth);

  glTexImage3D(GL_TEXTURE_2D_ARRAY,
               0,
               GL_DEPTH_COMPONENT24, /* todo: add option for this */
               scene->vrect.size.w * scene->backingScale,
               scene->vrect.size.h * scene->backingScale,
               len,
               0,
               GL_DEPTH_COMPONENT,
               GL_FLOAT,
               NULL);

  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  glTexParameteri(GL_TEXTURE_2D_ARRAY,
                  GL_TEXTURE_COMPARE_MODE,
                  GL_COMPARE_REF_TO_TEXTURE);

  if (currentOutput != pout)
    gkBindPassOut(scene, currentOutput);
}

GK_EXPORT
GkPassOutColor*
gkGetRenderTarget(GkPass *pass, int32_t index) {
  GkPassOut      *pout;
  GkPassOutColor *poc;

  if (!(pout = pass->output) || !(poc = pout->color))
    return NULL;

  while (index > 0) {
    poc = poc->next;
    index--;
  }

  return poc;
}

GK_EXPORT
{'fraction_non_alphanumeric': 0.1086422001639195, 'fraction_numerical': 0.00783170931609143, 'mean_word_length': 2.736645117386866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 96, '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': '1830959', 'n_tokens_mistral': 4067, 'n_tokens_neox': 3860, 'n_words': 621}