我写了一些DirectX RenderEngine来渲染和纹理简单的多边形。纹理与CreateDDSTextureFromFile(.dds)配合使用非常好。
现在,我尝试从Memory缓冲区设置Texture和相应的ShaderResourceView。创建Texture2D有效,但是, CreateShaderResourceView总是失败。还试图用第二个参数设置为NULL来调用它,也失败了。我整个星期都想弄清楚这里有什么问题。
我希望有人可以帮助我。
[代码] :
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;
[Sidequestion] :我注意到当我将 D3D11_TEXTURE2D_DESC.USAGE更改为D3D11_USAGE_DYNAMIC 时,CreateTexture2D也失败了。但为什么呢?
[调试] :... D3D11 INFO:创建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 Thread'(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]
..对我说不多,看不出任何错误提示。
[注意] :实际上我想使用相机输入的RGBA8纹理(存储在缓冲区中) - 但我无法实现。所以我尝试了这个例子,找出为什么 CreateShaderResourceView 即使使用普通的int *缓冲区也会失败。
我非常感谢任何帮助!谢谢!
[解决] 作为上面的代码,我总是达到输出“无法创建着色器资源视图”,假设CreateShaderResourceView失败。检查HRESULT,它的值是S_OK,这对我来说似乎很奇怪。所以我只是尝试改变条件:
if (SUCCEEDED(device->CreateShaderResourceView(tex, &srDesc, &texture_)));
{
std::cout << "Successfully created ShaderResourceView" << std::endl;
delete[] buf;
return true;
}
return false;
它有效。这种行为对我来说似乎很奇怪,但我终于很高兴它有效。所以相机流现在也可以工作,非常感谢Adam Miles的支持! ;)
答案 0 :(得分:0)
在设备创建标志上使用D3D11_CREATE_DEVICE_DEBUG,以获取函数失败时打印出的确切错误消息。
答案 1 :(得分:0)
这已经晚了几年,但是问题似乎是问题中的代码将像这样被解析:
if (FAILED(device->CreateShaderResourceView(tex, &srDesc, &texture_)))
;
// unconditionally executed:
{
std::cerr << "Can't create Shader Resource View" << std::endl;
return false;
}
由于if
行末尾的分号。