为什么 D3DX11CreateShaderResourceViewFromMemory 只上传纹理的部分副本?

发布于 2024-10-20 08:53:53 字数 1276 浏览 1 评论 0原文

我对 D3DX11CreateShaderResourceViewFromMemory 辅助函数有疑问。

我从文件或 ZIP 中读取一些纹理,并将原始字节和长度传递给辅助函数,但是仅上传了部分纹理(由 PIX 确认)。 我尝试手动调整长度但无济于事。 这是从文件加载纹理的代码:

struct FileDataLoader
{
    void Load()
    {
        std::ifstream file(mFileName);

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

            mBuffer.resize(length);
            file.read(&mBuffer[0],length);
            file.close();
        }
    }

    void Decompress(void*& data, std::size_t& numBytes)
    {
        data = &mBuffer[0];
        numBytes = mBuffer.size();
    }

    std::wstring mFileName;
    std::vector<char> mBuffer;
};

FileDataLoader fdl;
fdl.mFileName = L"Content\\Textures\\Smoke.dds";
fdl.Load();
void* bytes;
std::size_t size;
fdl.Decompress(bytes, size);
DXCall(D3DX11CreateShaderResourceViewFromMemory(device, bytes, size, NULL, NULL, &particleTexture, NULL));

这只是我用来调试此问题的示例代码,我将其范围缩小到文件加载和 D3DX 辅助函数。 现在,如果我这样做:

DXCall(D3DX11CreateShaderResourceViewFromFileW(device, L"Content\\Textures\\Smoke.dds", NULL, NULL, &particleTexture, NULL));

它工作得很好。

知道为什么它不会完全上传纹理吗?

I have a problem with the D3DX11CreateShaderResourceViewFromMemory helper function.

I read some texture from a file or ZIP and pass the raw bytes and length to the helper function, however only part of the texture is uploaded (as confirmed by PIX).
I tried fiddling with the length manually but to no avail.
Here is the code that loads the texture from file:

struct FileDataLoader
{
    void Load()
    {
        std::ifstream file(mFileName);

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

            mBuffer.resize(length);
            file.read(&mBuffer[0],length);
            file.close();
        }
    }

    void Decompress(void*& data, std::size_t& numBytes)
    {
        data = &mBuffer[0];
        numBytes = mBuffer.size();
    }

    std::wstring mFileName;
    std::vector<char> mBuffer;
};

FileDataLoader fdl;
fdl.mFileName = L"Content\\Textures\\Smoke.dds";
fdl.Load();
void* bytes;
std::size_t size;
fdl.Decompress(bytes, size);
DXCall(D3DX11CreateShaderResourceViewFromMemory(device, bytes, size, NULL, NULL, &particleTexture, NULL));

That is only a sample code that I am using to debug this problem, and I narrowed it down to the file loading and the D3DX helper function.
Now if I do this instead:

DXCall(D3DX11CreateShaderResourceViewFromFileW(device, L"Content\\Textures\\Smoke.dds", NULL, NULL, &particleTexture, NULL));

it works perfectly fine.

Any idea on why it would not upload the texture entirely ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ人海孤独症 2024-10-27 08:53:53

打开文件时,您需要指定该文件是二进制文件:

std::ifstream file( fileName, std::ios::in | std::ios::binary );

如果没有 std::ios::binary 标志,您默认会以纯文本形式读取,这不是 D3DX11CreateShaderResourceViewFromMemory 所期望的。

When opening the file, you need to specify that the file is binary:

std::ifstream file( fileName, std::ios::in | std::ios::binary );

Without the std::ios::binary flag you're reading in plain text by default, which is not what D3DX11CreateShaderResourceViewFromMemory expects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文