具有初始数据的DirectX 11 ID3DDevice :: CreateTexture2D失败 [英] DirectX 11 ID3DDevice::CreateTexture2D with initial data fail

查看:1388
本文介绍了具有初始数据的DirectX 11 ID3DDevice :: CreateTexture2D失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只要我遵循一个教程,其中 CreateTexture2D()的初始数据为NULL,然后
使用 UpdateSubresource()加载数据,一切正常。但是 UpdateSubResource()是设备上下文函数,因此我尝试将 CreateTexture2D()与初始数据结合使用,但失败。

so long I follow a tutorial that CreateTexture2D() with NULL initial data, then using UpdateSubresource() to load data,every thing works fine. But UpdateSubResource() is a device context function, so I try to use CreateTexture2D() with initial data in one shot,but it fail.

下面的代码显示了能正常工作的原始文件。

below code show the original that works fine.

tex_desc.Height = h;
tex_desc.Width = w;
tex_desc.MipLevels = 0;
tex_desc.ArraySize = 1;
tex_desc.Format= DXGI_FORMAT_R8G8B8A8_UNORM;
tex_desc.SampleDesc.Count = 1;
tex_desc.SampleDesc.Quality = 0;
tex_desc.Usage= D3D11_USAGE_DEFAULT;
tex_desc.BindFlags= D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET;
tex_desc.CPUAccessFlags= 0;
tex_desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;

row_pitch = (w * 4) * sizeof(unsigned char);
hres = _dv->CreateTexture2D(&tex_desc, NULL, &m_tex);
HR_FAIL(hres,  "create 2D texture");

_dc->UpdateSubresource(m_tex, 0, NULL, data, row_pitch, 0);

然后将其更改为

tex_desc.Height = h;
tex_desc.Width = w;
tex_desc.MipLevels = 0;
tex_desc.ArraySize = 1;
tex_desc.Format= DXGI_FORMAT_R8G8B8A8_UNORM;
tex_desc.SampleDesc.Count = 1;
tex_desc.SampleDesc.Quality = 0;
tex_desc.Usage= D3D11_USAGE_DEFAULT;
tex_desc.BindFlags= D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET;
tex_desc.CPUAccessFlags= 0;
tex_desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;

row_pitch = (w * 4) * sizeof(unsigned char);

D3D11_SUBRESOURCE_DATA sdata;            // <<===changed
sdata.pSysMem = data;                  // <<===changed
sdata.SysMemPitch = row_pitch;                 // <<===changed
sdata.SysMemSlicePitch = (w*h*4)*sizeof(unsigned char);       // <<===changed
hres = _dv->CreateTexture2D(&tex_desc, &sdata, &m_tex);       // <<===changed
HR_FAIL(hres, "testing create texture 2d");

然后会产生错误

testing create texture 2d fail
E_INVALIDARG : An invalid parameter was passed to the returning function.

它说某些参数无效,但我不知道为什么我传递的参数无效

it said some parameter is invalid but I don't see why the argument i passed is invalid

更新:

,所以我尝试按照以下链接进行安装调试DirectX的设备。这就是我得到的:

UPDATE:
so I try to follow the links to setup debug device for DirectX . Here's what I got:

D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[4].SysMemPitch cannot be 0 [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[6].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[7].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
D3D11 ERROR: ID3D11Device::CreateTexture2D: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #104: CREATETEXTURE2D_INVALIDARG_RETURN]
Exception thrown at 0x750BC54F in directx11_test.exe: Microsoft C++ exception: _com_error at memory location 0x002BF3D0.
D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[2].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[4].SysMemPitch cannot be 0 [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[6].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[7].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
D3D11 ERROR: ID3D11Device::CreateTexture2D: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #104: CREATETEXTURE2D_INVALIDARG_RETURN]

因此它说Create函数尝试以以下方式访问InitialData:一个数组,而我只有一个元素作为参数传递。
需要注意的一件事是,我仅在发布配置中获得调试输出(我在代码中手动定义了_DEBUG)。在调试配置中,我获得了访问冲突异常触发器,因此未收到D3D调试消息。

so it said that the Create function try to access the InitialData as an array while I only has one element as pass as argument. One thing to notice is that I only get the debug output in release config(I manually define _DEBUG in the code). In Debug config, I get access violation exception trigger so I don't get the D3D Debug Message.

所以我有一种解决方法,就是得到一个足够大的子资源数组,以存储所有类型的mipmap级别,以防止访问冲突。

so I get a way to work around it is get an array of subresource big enough to to store all kind of mipmap level to prevent access violation.

D3D11_SUBRESOURCE_DATA sdata[20];
    int idx = 0;
    while (row_pitch > 0)
    {

        sdata[idx].pSysMem = data;
        sdata[idx].SysMemPitch = row_pitch;
        row_pitch >>= 1;
        idx++;
    }

    hres = _dv->CreateTexture2D(&tex_desc, sdata, &m_tex);
    HR_FAIL(hres, "testing create texture 2d");

但是我认为这是不明智的,因为我仍然需要生成mipmap且此功能在设备上下文中

but I think it's unwise since I still need generate mipmap and this function is in device context

推荐答案

我认为失败的原因是您已请求自动生成Mipmap( D3D11_RESOURCE_MISC_GENERATE_MIPS ),您不能使用初始数据进行设置。您必须通过设备上下文加载顶层。

I believe the reason it is failing is that you have requested auto-gen mipmaps (D3D11_RESOURCE_MISC_GENERATE_MIPS) which you can't use initial data to set up. You have to load the top level through the device context instead.

有关自动生成mipmap的完整实现以及在其他情况下使用initData的信息,请参见 WICTextureLoader DirectX工具套件

For a full implementation of auto-gen mipmaps as well as using initData for other scenarios, see WICTextureLoader in the DirectX Tool Kit


Direct3D调试设备,当返回 E_INVALIDARG 时,它将在输出窗口中提供更多详细信息。

If you used the Direct3D Debug Device it would have provided a lot more detail in the output window when returning E_INVALIDARG.

这篇关于具有初始数据的DirectX 11 ID3DDevice :: CreateTexture2D失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆