/* SPDX-FileCopyrightText: 2015 Blender Foundation
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * Author: Sergey Sharybin. */

#include "opensubdiv_evaluator_capi.hh"

#ifdef WITH_METAL_BACKEND
#  include <opensubdiv/osd/mtlPatchShaderSource.h>
#endif
#if defined(WITH_VULKAN_BACKEND) || defined(WITH_OPENGL_BACKEND)
#  include <opensubdiv/osd/glslPatchShaderSource.h>
#endif

#include "MEM_guardedalloc.h"

#include "GPU_context.hh"

#include "internal/evaluator/evaluator_cache_impl.h"

OpenSubdiv_EvaluatorCache *openSubdiv_createEvaluatorCache(eOpenSubdivEvaluator evaluator_type)
{
  OpenSubdiv_EvaluatorCache *evaluator_cache = MEM_new<OpenSubdiv_EvaluatorCache>(__func__);
  evaluator_cache->impl = openSubdiv_createEvaluatorCacheInternal(evaluator_type);
  return evaluator_cache;
}

void openSubdiv_deleteEvaluatorCache(OpenSubdiv_EvaluatorCache *evaluator_cache)
{
  if (!evaluator_cache) {
    return;
  }

  openSubdiv_deleteEvaluatorCacheInternal(evaluator_cache->impl);
  MEM_delete(evaluator_cache);
}

const char *openSubdiv_getGLSLPatchBasisSource()
{
  /* Using a global string to avoid dealing with memory allocation/ownership. */
  static std::string patch_basis_source;
  if (patch_basis_source.empty()) {
    patch_basis_source =
        "#define OsdPatchParam_host_shared_ OsdPatchParam\n"
        "#define OsdPatchArray_host_shared_ OsdPatchArray\n"
        "#define OsdPatchCoord_host_shared_ OsdPatchCoord\n";

#ifdef WITH_METAL_BACKEND
    patch_basis_source += OpenSubdiv::Osd::MTLPatchShaderSource::GetPatchBasisShaderSource();
#endif
#if defined(WITH_OPENGL_BACKEND) || defined(WITH_VULKAN_BACKEND)
    patch_basis_source += OpenSubdiv::Osd::GLSLPatchShaderSource::GetPatchBasisShaderSource();
#endif
  }
  return patch_basis_source.c_str();
}
