D3DCompile default include interface

Started by
1 comment, last by Juliean 10 years, 7 months ago

Hello,

I'm trying to get rid of all D3DX11-functionality, and one of the first suspects is the D3DX11CompileFromMemory-function. However, I'm having troubles with the IID3DInclude-interface you got to specify. Documentation says, you can just use:


#define D3D_COMPILE_STANDARD_FILE_INCLUDE ((ID3DInclude*)(UINT_PTR)1)

for a standard include interface, however when I use that macro, I always get a "failed to read at memory 1" error. Now there is very little tutorials/complete source code that actually use D3DCompile. Is there anything I've missed so this fails, or some other thing I'll have to consider?

Advertisement

I looks like your DX SDK is to old. At least the June 2010 SDK is missing this function. So you either need to use a newer SDK or implement the interface yourself. Since I just hat to implement this interface a few days ago too this may help you to start:


class CDXInclude : public ID3DInclude
{
public:
  CDXInclude(const std::string& baseFile);

  HRESULT __stdcall Open(D3D_INCLUDE_TYPE IncludeType, LPCSTR pFileName,
                         LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes);

  HRESULT __stdcall Close(LPCVOID pData);

private:
  std::string m_baseDir;
  std::list<std::unique_ptr<char[]>> m_data;
};

CDXInclude::CDXInclude(const std::string& baseFile)
{
  size_t pos = baseFile.rfind('/');
  if(pos != std::string::npos)
    m_baseDir = baseFile.substr(0, pos) + '/';
}

HRESULT CDXInclude::Open(D3D_INCLUDE_TYPE IncludeType, LPCSTR pFileName,
                         LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes)
{
  // Open file
  std::ifstream file(m_baseDir + pFileName, std::ios::binary);
  if(!file.is_open())
  {
    *ppData = NULL;
    *pBytes = 0;
    return S_OK;
  }

  // Get filesize
  file.seekg(0, std::ios::end);
  UINT size = static_cast<UINT>(file.tellg());
  file.seekg(0, std::ios::beg);

  // Create buffer and read file
  std::unique_ptr<char[]> data(new char[size]);
  file.read(data.get(), size);
  *ppData = data.get();
  *pBytes = size;
  m_data.push_back(std::move(data));
  return S_OK;
}

HRESULT CDXInclude::Close(LPCVOID pData)
{
  m_data.remove_if(shared_equals_raw(pData));
  return S_OK;
}

Note: I'm using some C++11 functions but instead you can use simple pointers if you want.

Thanks, I'll use your implementation as a starting point. I don't plan on using the newer SDK unless I start with DX11.1 / 2 to target Windows8, so that should do fine for now.

This topic is closed to new replies.

Advertisement