directx11-无法将Texture2D设置为ShaderResourceView吗? [英] directx11 - can't set Texture2D as ShaderResourceView?

查看:893
本文介绍了directx11-无法将Texture2D设置为ShaderResourceView吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些DirectX RenderEngine,用于渲染和纹理简单的多边形。

I have written a little DirectX RenderEngine, to render and texture simple Polygons. The texturing worked with CreateDDSTextureFromFile(.dds) very well.

现在,我尝试从内存缓冲区中设置一个Texture和一个相应的ShaderResourceView。创建Texture2D可以,但是 CreateShaderResourceView总是失败。还尝试将第二个参数设置为NULL来调用它,也失败了。我试图弄清楚整个星期这里出了什么问题。

Now, I try to set up a Texture and a corresponding ShaderResourceView from a Memory buffer. Creating the Texture2D works, but then, CreateShaderResourceView ALWAYS fails. Also tried to call it with 2nd param set to NULL, also fails. I was trying to figure out what's wrong here for the whole week.

我希望有人可以在这里为我提供帮助。

I hope somebody can help me here.

[代码]

    D3D11_TEXTURE2D_DESC tdesc;
ZeroMemory(&tdesc, sizeof(tdesc));
D3D11_SUBRESOURCE_DATA srInitData;
ZeroMemory(&srInitData, sizeof(srInitData));
ID3D11Texture2D* tex = 0;
D3D11_SHADER_RESOURCE_VIEW_DESC srDesc;
ZeroMemory(&srDesc, sizeof(srDesc));

// ------------------------- [CHECK MATE ] ----------------------------
int w = 512;
int h = 512;
int bpp = 4;
int *buf = new int[w*h];

// filling the image
for (int i = 0; i<h; i++)
    for (int j = 0; j<w; j++)
    {
        if ((i & 32) == (j & 32))
            buf[i*w + j] = 0x00000000;
        else
            buf[i*w + j] = 0xffffffff;
    }

// setting up D3D11_SUBRESOURCE_DATA 
srInitData.pSysMem = (void *)buf;
srInitData.SysMemPitch = w*bpp;
srInitData.SysMemSlicePitch = w*h*bpp; // Not needed since this is a 2d texture

// setting up D3D11_TEXTURE2D_DESC 
tdesc.Width = w;
tdesc.Height = h;
tdesc.MipLevels = 1;
tdesc.ArraySize = 1;
tdesc.SampleDesc.Count = 1;
tdesc.SampleDesc.Quality = 0;
tdesc.Usage = D3D11_USAGE_DEFAULT;
tdesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
tdesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
tdesc.CPUAccessFlags = 0;
tdesc.MiscFlags = 0;

// checking inputs
if (device->CreateTexture2D(&tdesc, &srInitData, NULL) == S_FALSE)
    std::cout << "Inputs correct" << std::endl;
else
    std::cout << "wrong inputs" << std::endl;

// create the texture
if (FAILED(device->CreateTexture2D(&tdesc, &srInitData, &tex)))
{
    std::cout << "Failed" << std::endl;
    return(0);
}
else
    std::cout << "Success" << std::endl;


// setup the Shader Resource Desc.
srDesc.Format = tdesc.Format;
srDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srDesc.Texture2D.MostDetailedMip = 0;
srDesc.Texture2D.MipLevels = 1;

// ------------------ [ Here it always Fails ...]
if (FAILED(device->CreateShaderResourceView(tex, &srDesc, &texture_)));
{
    std::cerr << "Can't create Shader Resource View" << std::endl;
    return false;
}

delete[] buf;

[侧问] :当我更改 D3D11_TEXTURE2D_DESC时,我注意到了从.USAGE到D3D11_USAGE_DYNAMIC ,CreateTexture2D也会失败。但是为什么?

[Sidequestion]: I noticed when I Change the D3D11_TEXTURE2D_DESC.USAGE to D3D11_USAGE_DYNAMIC, CreateTexture2D aswell fails. but why?

[Debug] :...
D3D11信息:创建ID3D11Buffer:Name = unnamed,Addr = 0x00C31374,ExtRef = 1,IntRef = 0 [STATE_CREATION INFO#2097228:CREATE_BUFFER]
D3D11 INFO:创建ID3D11Buffer:Name = unnamed,Addr = 0x00C319A4,ExtRef = 1,IntRef = 0 [STATE_CREATION INFO#2097228: CREATE_BUFFER]
线程'Win32线程'(0x9c8)已退出,代码为0(0x0)。
D3D11 INFO:创建ID3D11Texture2D:Name = unnamed,Addr = 0x00C41984,ExtRef = 1,IntRef = 0 [STATE_CREATION INFO#2097234:CREATE_TEXTURE2D]
D3D11 INFO:创建ID3D11ShaderResourceView:Name = unnamed ,Addr = 0x00C41F6C,ExtRef = 1,IntRef = 0 [STATE_CREATION INFO#2097240:CREATE_SHADERRESOURCEVIEW]

[Debug]: ... D3D11 INFO: Create ID3D11Buffer: Name="unnamed", Addr=0x00C31374, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097228: CREATE_BUFFER] D3D11 INFO: Create ID3D11Buffer: Name="unnamed", Addr=0x00C319A4, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097228: CREATE_BUFFER] The thread 'Win32 Thread' (0x9c8) has exited with code 0 (0x0). D3D11 INFO: Create ID3D11Texture2D: Name="unnamed", Addr=0x00C41984, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097234: CREATE_TEXTURE2D] D3D11 INFO: Create ID3D11ShaderResourceView: Name="unnamed", Addr=0x00C41F6C, ExtRef=1, IntRef=0 [ STATE_CREATION INFO #2097240: CREATE_SHADERRESOURCEVIEW]

..对我说的不多,什么也看不到错误提示。

.. doesn't say much to me, can't see anything of an error hint here.

[注意] :实际上,我想使用来自Camera Input(存储在缓冲区中)的RGBA8纹理-但我无法实现。因此,我尝试了这个示例,以找出即使使用普通的int *缓冲区, CreateShaderResourceView 为何仍然失败。

[Note]: actually I want to use a RGBA8 Texture from Camera Input (stored in a buffer) - but I could not achieve that. So I tried this example, to find out why CreateShaderResourceView fails even with a normal int* buffer.

我非常感谢您的帮助!谢谢!

I am very thankful for any help! thanks!

[已解决] 如上面的代码,假设CreateShaderResourceView失败,我始终会到达输出无法创建着色器资源视图。检查HRESULT,它的值为S_OK,这对我来说很奇怪。因此,我只是更改了尝试的条件:

[Solved] as the code above, I always reached the output "Can't create Shader Resource View" assuming CreateShaderResourceView failed. Checking HRESULT, it's value was S_OK, which seemed strange to me. So I just changed the condition for a try:

if (SUCCEEDED(device->CreateShaderResourceView(tex, &srDesc, &texture_)));
{
    std::cout << "Successfully created ShaderResourceView" << std::endl;
    delete[] buf;
    return true;
}
return false;

而且有效。这种行为对我来说似乎很奇怪,但我最终很高兴它能奏效。因此,摄像机流现在也可以工作了,这要感谢Adam Miles的支持! ;)

And it worked. This behavior seems really odd to me, but I am finally glad it works. So Also the camera stream works now, many thanks to Adam Miles for the support! ;)

推荐答案

在设备创建标志上使用D3D11_CREATE_DEVICE_DEBUG可获得函数失败时打印出的确切错误消息。

Use D3D11_CREATE_DEVICE_DEBUG on device creation flags to get exact error messages printed out when a function fails.

这篇关于directx11-无法将Texture2D设置为ShaderResourceView吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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