<commit_before>#pragma once

#include <higanbana/core/platform/definitions.hpp>
#if defined(HIGANBANA_PLATFORM_WINDOWS)
#include <wrl.h>
#include <Objbase.h>
#include <dxc/dxcapi.h>
#include <dxc/Support/microcom.h>
#endif
#include "higanbana/graphics/desc/shader_interface_descriptor.hpp"
#include <higanbana/core/filesystem/filesystem.hpp>
#include <higanbana/core/math/math.hpp>
#include <higanbana/core/global_debug.hpp>
#include <fstream>
#include <string>

#if defined(HIGANBANA_PLATFORM_WINDOWS)
template <typename T>
using ComPtr = Microsoft::WRL::ComPtr<T>;

class DXCIncludeHandler2 : public IDxcIncludeHandler
{
public:
  DXCIncludeHandler2(higanbana::FileSystem& fs, std::string sourcePath, std::string rootSignature, ComPtr<IDxcLibrary> lib, std::function<void(std::string)> func): m_fs(fs)
    , m_sourcePath(sourcePath)
    , m_rootSignatureFile(rootSignature)
    , m_lib(lib)
    , m_fileIncluded(func)
  {
    if (m_rootSignatureFile.empty())
    {
      m_rootSignatureFile = "\n\n";
    }
  }

  DXC_MICROCOM_ADDREF_RELEASE_IMPL(m_dwRef)
    virtual ~DXCIncludeHandler2() {}

  HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) {
    return DoBasicQueryInterface<::IDxcIncludeHandler>(this, riid, ppvObject);
  }

  HRESULT STDMETHODCALLTYPE LoadSource(
    _In_ LPCWSTR pFilename,                                   // Candidate filename.
    _COM_Outptr_result_maybenull_ IDxcBlob **ppIncludeSource  // Resultant source object for included file, nullptr if not found.
  )
  {
    //ppIncludeSource = nullptr;
    std::string filename = ws2s(pFilename);
    if (!filename.empty())
      filename = filename.substr(2);

    std::string finalPath;
    finalPath = m_sourcePath + filename;
    if (filename.compare("rootSig.h") == 0)
    {
      HIGAN_LOG("Shader include wanted RootSignature!!\n");

      ComPtr<IDxcBlobEncoding> asd;
      auto hr = m_lib->CreateBlobWithEncodingOnHeapCopy(m_rootSignatureFile.data(), static_cast<UINT32>(m_rootSignatureFile.size()), CP_ACP, asd.ReleaseAndGetAddressOf());

      if (SUCCEEDED(hr))
      {
        *ppIncludeSource = asd.Detach();
      }
      return hr;
    }
    else
    {
      HIGAN_ASSERT(m_fs.fileExists(finalPath), "Shader file doesn't exists in path %s\n", finalPath.c_str());
    }

    if (m_fileIncluded)
    {
      m_fileIncluded(finalPath);
    }

    auto shader = m_fs.viewToFile(finalPath);
    ComPtr<IDxcBlobEncoding> asd;
    auto hr = m_lib->CreateBlobWithEncodingOnHeapCopy(shader.data(), static_cast<uint32_t>(shader.size()), CP_ACP, asd.ReleaseAndGetAddressOf());

    if (SUCCEEDED(hr))
    {
      *ppIncludeSource = asd.Detach();
    }
    else
    {
      HIGAN_LOG("oh no\n");
    }

    return hr;
  }
private:
  higanbana::FileSystem& m_fs;
  std::string m_sourcePath;
  std::string m_rootSignatureFile;
  std::function<void(std::string)> m_fileIncluded;
  ComPtr<IDxcLibrary> m_lib;
  DXC_MICROCOM_REF_FIELD(m_dwRef)
};

#endif

namespace higanbana
{
  namespace backend
  {
    enum class ShaderType
    {
      Vertex,
      Pixel,
      Compute,
      Geometry,
      TessControl,
      TessEvaluation
    };

    enum class ShaderBinaryType
    {<commit_msg>amplification/mesh shader enums<commit_after>#pragma once

#include <higanbana/core/platform/definitions.hpp>
#if defined(HIGANBANA_PLATFORM_WINDOWS)
#include <wrl.h>
#include <Objbase.h>
#include <dxc/dxcapi.h>
#include <dxc/Support/microcom.h>
#endif
#include "higanbana/graphics/desc/shader_interface_descriptor.hpp"
#include <higanbana/core/filesystem/filesystem.hpp>
#include <higanbana/core/math/math.hpp>
#include <higanbana/core/global_debug.hpp>
#include <fstream>
#include <string>

#if defined(HIGANBANA_PLATFORM_WINDOWS)
template <typename T>
using ComPtr = Microsoft::WRL::ComPtr<T>;

class DXCIncludeHandler2 : public IDxcIncludeHandler
{
public:
  DXCIncludeHandler2(higanbana::FileSystem& fs, std::string sourcePath, std::string rootSignature, ComPtr<IDxcLibrary> lib, std::function<void(std::string)> func): m_fs(fs)
    , m_sourcePath(sourcePath)
    , m_rootSignatureFile(rootSignature)
    , m_lib(lib)
    , m_fileIncluded(func)
  {
    if (m_rootSignatureFile.empty())
    {
      m_rootSignatureFile = "\n\n";
    }
  }

  DXC_MICROCOM_ADDREF_RELEASE_IMPL(m_dwRef)
    virtual ~DXCIncludeHandler2() {}

  HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) {
    return DoBasicQueryInterface<::IDxcIncludeHandler>(this, riid, ppvObject);
  }

  HRESULT STDMETHODCALLTYPE LoadSource(
    _In_ LPCWSTR pFilename,                                   // Candidate filename.
    _COM_Outptr_result_maybenull_ IDxcBlob **ppIncludeSource  // Resultant source object for included file, nullptr if not found.
  )
  {
    //ppIncludeSource = nullptr;
    std::string filename = ws2s(pFilename);
    if (!filename.empty())
      filename = filename.substr(2);

    std::string finalPath;
    finalPath = m_sourcePath + filename;
    if (filename.compare("rootSig.h") == 0)
    {
      HIGAN_LOG("Shader include wanted RootSignature!!\n");

      ComPtr<IDxcBlobEncoding> asd;
      auto hr = m_lib->CreateBlobWithEncodingOnHeapCopy(m_rootSignatureFile.data(), static_cast<UINT32>(m_rootSignatureFile.size()), CP_ACP, asd.ReleaseAndGetAddressOf());

      if (SUCCEEDED(hr))
      {
        *ppIncludeSource = asd.Detach();
      }
      return hr;
    }
    else
    {
      HIGAN_ASSERT(m_fs.fileExists(finalPath), "Shader file doesn't exists in path %s\n", finalPath.c_str());
    }

    if (m_fileIncluded)
    {
      m_fileIncluded(finalPath);
    }

    auto shader = m_fs.viewToFile(finalPath);
    ComPtr<IDxcBlobEncoding> asd;
    auto hr = m_lib->CreateBlobWithEncodingOnHeapCopy(shader.data(), static_cast<uint32_t>(shader.size()), CP_ACP, asd.ReleaseAndGetAddressOf());

    if (SUCCEEDED(hr))
    {
      *ppIncludeSource = asd.Detach();
    }
    else
    {
      HIGAN_LOG("oh no\n");
    }

    return hr;
  }
private:
  higanbana::FileSystem& m_fs;
  std::string m_sourcePath;
  std::string m_rootSignatureFile;
  std::function<void(std::string)> m_fileIncluded;
  ComPtr<IDxcLibrary> m_lib;
  DXC_MICROCOM_REF_FIELD(m_dwRef)
};

#endif

namespace higanbana
{
  namespace backend
  {
    enum class ShaderType
    {
      Vertex,
      Pixel,
      Compute,
      Geometry,
      TessControl, // hull
      TessEvaluation, // domain
      Amplification, // Task shader
      Mesh, // Mesh shader
    };

    enum class ShaderBinaryType
    {
{'fraction_non_alphanumeric': 0.11704756022235949, 'fraction_numerical': 0.0030883261272390363, 'mean_word_length': 3.748533724340176, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 49, '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': '3431012', 'n_tokens_mistral': 2323, 'n_tokens_neox': 2262, 'n_words': 390}