sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.69k stars 638 forks source link

[UWP] C# Interop question #754

Open oden3d opened 8 years ago

oden3d commented 8 years ago

Hi, I have a project with SharpDX render and now would like to call native DirectX methods. My first step is to load textures with WIC/DDSTextureLoader from DirectXTex so what I did:

1) Add Windows Runtime Component (Universal Windows) to the solution. 2) Include DDS/WICTextureLoader.h/cpp to the project 3) Create new component class:

public ref class TextureLoader sealed
{
public:
    TextureLoader();
    int64 TextureLoader::LoadWICTexture(int64 devicePtr, int64 contextPtr, Platform::String^ filename)
    {
        ID3D11Device* d3dDevice = (ID3D11Device*)devicePtr;
        ID3D11DeviceContext* d3dDeviceContext = (ID3D11DeviceContext*)contextPtr;
        ID3D11ShaderResourceView *texView = nullptr;

        auto hr = DirectX::CreateWICTextureFromFile(d3dDevice, d3dDeviceContext, filename->Data(), 0, &texView);
        if (hr != S_OK)
        {
            return 0;
        }

        return (int64)texView;
    }
};

4) In my C# project call it as below:

    public class TextureTest
    {
        public void LoadTexture()
        {
            DirectXTools.TextureLoader loader = new DirectXTools.TextureLoader();
            string filename = $"some_texture.png";
            ShaderResourceView m_textureView = null;

            long texturePtr = loader.LoadWICTexture((long)d3dDevice2.NativePointer, (long)d3dDeviceContext2.NativePointer, filename);
            if (texturePtr != 0)
            {
                m_textureView = SharpDX.ComObject.FromPointer<ShaderResourceView>(new IntPtr(texturePtr));
            }
            // m_textureView - now contain loaded WIC texture
        }
    }

And my question is I miss something or did something wrong call or use? Or is any better way to interop?

Thank you in advance.

Best regards, Denis

xoofx commented 8 years ago

Not sure to understand, what is the problem exactly?