-1

I am pretty new to DirectX, and have trouble understanding some of the texture loading. I basically want to load a texture from a JPG file using D3D. Since the createTextureFromFile and createTextureFromMemory are deprecated, I tried the WIC Loader, but can't figure out how to use it.

Earlier, this was part of the code I had:

        ID3D11DeviceContext* ctx = NULL;
        g_D3D11Device->GetImmediateContext(&ctx);

        // update native texture from code
        if (unity_TexturePointer)
        {
            ID3D11Texture2D* d3dtex = (ID3D11Texture2D*)unity_TexturePointer;
            D3D11_TEXTURE2D_DESC desc;
            d3dtex->GetDesc(&desc);

            // filled data is an unsigned char* and has the data

            ctx->UpdateSubresource(d3dtex, 0, NULL, filledData, (width * 4), 0);
            delete[] filledData;
        }
        ctx->Release();

Now, coming to WIC loading, I followed this post. I now have:

        ID3D11DeviceContext* ctx = NULL;
        g_D3D11Device->GetImmediateContext(&ctx);

        // update native texture from code
        if (unity_TexturePointer)
        {
            ID3D11Texture2D* d3dtex = (ID3D11Texture2D*)unity_TexturePointer;
            D3D11_TEXTURE2D_DESC desc;
            d3dtex->GetDesc(&desc);

            ComPtr<ID3D11ShaderResourceView> srv;

            std::basic_ifstream<unsigned char> file("bridge.jpg", std::ios::binary);

            if (file.is_open())
            {
                file.seekg(0, std::ios::end);
                int length = file.tellg();
                file.seekg(0, std::ios::beg);

                unsigned char* buffer = new unsigned char[length];
                file.read(&buffer[0], length);
                file.close();

                HRESULT hr;
                hr = CreateWICTextureFromMemory(g_D3D11Device, ctx, &buffer[0], (size_t)length, nullptr, &srv, NULL);

                // What goes here??
            }
        }
        ctx->Release();

I am totally confused what now. I somehow want that texture that is loaded to reach my d3dtex Texture2D pointer.

I am referring to the header file and I see that we can pass the Texture pointer too, but that doesn't seem to work for me.

hr = CreateWICTextureFromMemory(g_D3D11Device, ctx, &buffer[0], (size_t)length, &d3dtex, nullptr, NULL);

It throws the error, there is no instance of overloaded function. I wanted to do this so directly my Texture pointer would be updated, since I am not quite sure how to work with the ShaderResourceView.

Similar problems for the CreateWICTextureFromFile methods.

Basically, I know I am doing something fundamentally wrong, and would appreciate any help or examples for the same, as the example in the Microsoft documentation doesn't really help me.

Thanks!

Community
  • 1
  • 1
Stuti Rastogi
  • 1,162
  • 2
  • 16
  • 26
  • If your question has been answered satisfactorily, it would be nice for you to mark the answer as "accepted", and upvote if you found it particularly helpful. Thanks! – Rook Jun 12 '17 at 09:12
  • And if there';s still a problem with the answer, please do say so, because that's the only way to get them improved. – Rook Jun 12 '17 at 09:12

1 Answers1

2

I suspect that the problem is that CreateWICTextureFromMemory creates an ID3D11Resource, and d3dtex is an ID3D11Texture2D. COM class and interface hierarchies can't always be trivially interconverted in the same way that instances of C++ classes can.

Look at the answers to this question here for how to use QueryInterface or GetResource to convert between ID3D11Resource and ID3D11Texture2D.

Down Casting ID3D11Texture2D

Rook
  • 5,734
  • 3
  • 34
  • 43
  • 1
    Downvoting without comment is useless and helps no-one. Furthermore, systematically downvoting the questions and answers of a user is the sort of thing that triggers automatic anti-abuse systems and moderator attention. – Rook May 22 '17 at 14:32
  • Thanks @Rook, this was helpful. I will try it out. I am relatively new to SO, and see that my question got downvoted. Any idea on how I could have improved my question to avoid that in the future? Thanks for your help. – Stuti Rastogi May 23 '17 at 04:36
  • I tried `hr = CreateWICTextureFromMemory(g_D3D11Device, ctx, &buffer[0], (size_t)length, nullptr, &srv, NULL); hr = ctx->QueryInterface(IID_ID3D11Texture2D, (void **)&d3dtex);` and my Unity just got unresponsive. When I tried `hr = CreateWICTextureFromMemory(g_D3D11Device, ctx, &buffer[0], (size_t)length, nullptr, &srv, NULL); ctx->PSSetShaderResources(0, 1, &srv);`, the plugin ran but no texture. Any ideas? – Stuti Rastogi May 23 '17 at 05:15
  • Well, `hr = CreateWICTextureFromMemory(g_D3D11Device, ctx, &buffer[0], (size_t)length, nullptr, &srv, NULL)` makes no mention of the resource you're writing the texture to. Do your `QueryInterface` first to get a suitable resource, then pass the resource into `CreateWICTextureFromMemory`. – Rook May 23 '17 at 05:18