Heap Overflew
Heap Overflew

Reputation: 35

Is there a way to use DirectXTex without Visual Studio?

So far, I tried to do this with my DirectX 11 project to load a texture:

I downloaded DirectXTex source, included source files into my project and tried to compile, but got errors. I got errors from these header files: WICTextureLoader.h, DDSTextureLoader.h, and DirectXTex.h. The errors were from header files, and not my own code. Then, I tried WICTextureLoader11.h and DDSTextureLoader11.h, and the error that I got was undefined reference to the function CreateWICTextureFromFile()... (or the same, but with DDS).

I think, the problem here could be that the compiler (I use Clang/GCC) cannot find the .lib file? However, some people here mentioned that you could just throw the source files into your folder and everything would be OK. Seems like, it's not OK for me. If this is the problem, is there a way to get the binaries (.lib) without going through VS installation and other stuff?

Upvotes: 2

Views: 1424

Answers (2)

Heap Overflew
Heap Overflew

Reputation: 35

So, I resolved the issue by giving up and installing the Visual Studio. However, I ended up compiling with Clang; but, instead of linking to GCC, I link to MSVC (otherwise, I'll get errors referring to vcruntime.h).

The original problem was that I wanted to use GCC in order to work with DirectX 11, but when it came to the point when I needed to load a texture, it was unfortunate that the D3DX (d3dx11.h) function that I wanted to use was deprecated in favor of D3D implementations: instead of using D3DX11CreateShaderResourceViewFromFile, I should consider using DirectXTex that has the function that I need (CreateWICTextureFromFile).

Well, I downloaded and did (kinda) everything that Chuck Walbourn suggested:

  1. Download DirectXTex.
  2. Move the DirectXTex subfolder somewhere (you will later link to it); my location example: I:\Projects\VS\Real_DirectX11\common\include\DirectXTex
  3. In your project's source (in my case, I just have main.cpp), #include <DirectXTex.h> (not in quotes, because I'm going to link to it).
  4. Take two files from the subfolder WICTextureLoader: WICTextureLoader11.cpp and WICTextureLoader11.h.
  5. And put them into my project's source (or maybe you want to link them).
  6. Inside my main.cpp, I #include both of those files (.h/.cpp):

Nothing works, I get link errors (many errors!). I decided to give up and install Visual Studio again (I'm saying again, because at one point I got into OpenGL and started preferring portable solutions; and much simplistic, since my project folder has only the source code and asset files).

Seems like, VS is getting faster in its loading speeds (not compilation!), so maybe it's not that bad after all. Nope. I download the DirectXTex through NuGet package manager and probably did everything for the thing to work. Well, it kinda works, except I get irrelevant errors this time that I didn't get when I was compiling with GCC: my shaders don't compile! Oh no, where did I screw up? I have no idea! Setting the preferences for each files (VS, target version, etc.), nothing helps.

OK, let's get back to my compiler, except this time I'm using Clang (LLVM 10.0 Windows 64-bit) and I link to MSVC (you have it after installing Visual Studio). How? I change my Environment Variable PATHs from GCC's to MSVC's:

include

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include

lib

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x64

lib32

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86

And, just in case, also included bin

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\Hostx64\x64

Then, I use a batch file to compile my thing:

build.bat

@echo off

set filename=%1

clang++ ^
%cd%\src\%1.cpp ^
-I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt" ^
-I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um" ^
-I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\winrt" ^
-I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\shared" ^
-I"I:\Projects\VS\Real_DirectX11\common\include\DirectXTex" ^
-L"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\ucrt\x64" ^
-L"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\um\x64" ^
-L"I:\Projects\VS\Real_DirectX11\common\lib" ^
-w -lkernel32 -luser32 -lole32 -ldxguid -luuid -ldxgi -ld3d11 -ld3dcompiler -ldirectxtex -o %cd%\bin\%1.exe

In the command line (assuming you're in the root of your project), I just simply write build main and hit the enter. Everything compiles just fine. I'm using the flag -w which is for suppressing all warnings. Why? Because if I don't use it, it will give hundreds of warnings related to the Windows SDK's files (though, I still check if my own code doesn't have warnings).

I loaded the texture with this code:

HRESULT result;
...
result = DirectX::CreateWICTextureFromFile(d3d11_device, L"../textures/mdx.png", NULL, &texture);
if(FAILED(result))
{
    // handle the error
}

I don't know if I'm going into any troubles later in my DirectX 11 rendering engine... but I'll just hope that everything is going to be fine.

enter image description here

Upvotes: 0

Chuck Walbourn
Chuck Walbourn

Reputation: 41057

The DirectXTex library can be built with clang/LLVM for Windows or Visual C++. The project includes a CMakeLists.txt for building the DirectXTex library, and optionally the command-line tools, using CMake. If you have your own make solution, you can work out the details by referencing that file.

To use the DirectXTex library functions (a.k.a. LoadWICFromFile, Compress, etc.), you need to link to the DirectXTex library, which you can do in CMake via add_subdirectory and target_link_libraries. You also need to add the include path:

To use it from your own CMake:

add_subdirectory(${CMAKE_SOURCE_DIR}/../DirectXTex ${CMAKE_BINARY_DIR}/bin/CMake/DirectXTex)

target_link_libraries(${PROJECT_NAME} DirectXTex)
target_include_directories(${PROJECT_NAME} PRIVATE ../DirectXTex/DirectXTex)

Keep in mind that the DirectXTex package also includes standalone modules (DDSTextureLoader, WICTextureLoader, and ScreenGrab) that are intended for use instead of DirectXTex if you want basic runtime loading without all the code required for conversion, resize, compression, etc.

To use stand-alone modules, you need to add both the header and the .cpp file to your project--they are not in the DirectXTex.lib.

So in your case, include from this directory both WICTextureLoader11.h and WICTextureLoader11.cpp to get the Direct3D 11 version of CreateWICTextureFromFile. The reason you got a link error is that you didn't add the cpp file to your project.

The DirectX Tool Kit also includes DDSTextureLoader, WICTextureLoader, and ScreenGrab. These versions are integrated in the DirectXTK.lib. Instead of using DirectXTex at all, you could use that project's CMakeLists.txt and reference it as:

add_subdirectory(${CMAKE_SOURCE_DIR}/../DirectXT ${CMAKE_BINARY_DIR}/bin/CMake/DirectXTK)

target_link_libraries(${PROJECT_NAME} DirectXTK)
target_include_directories(${PROJECT_NAME} PRIVATE ../DirectXTK/Inc)

See the WICTextureLoader documentation and this blog post.

Upvotes: 3

Related Questions