我正在尝试做一个简单的测试来更好地理解 DirectX11 以及如何使用ID3D11Texture2D.
我愿意创建一个ID3D11Texture2Dfrom unsigned char[],但由于某种原因它给了我以下错误:
Exception thrown at 0x00007FFBBCB9C21E (nvwgf2umx.dll) in TestDX.exe: 0xC0000005: Access violation reading location 0x0000024B1A7A4000.
我已经创建了D3D11CreateDevice带有调试标志的,它没有显示任何更多有用的信息。
上网查了一下,这个题目提示我的D3D11_TEXTURE2D_DESC
Format属性与输入的数据格式不匹配。
要重现,这是我的代码:
const int width = 1920;
const int height = 1080;
unsigned char* pixels = new unsigned char[width * height * 4];
int numPixels = (int)(width * height);
// Setting up pixels
for (int i = 0; i < numPixels; i++)
{
*(pixels) = 255;
*(pixels + 1) = 255;
*(pixels + 2) = 255;
*(pixels + 3) = 255;
pixels += 4; // move the pointer along to the next rgba pixel
}
// Creating DirectX11 Device
UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_DEBUG;
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_1
};
ID3D11Texture2D* tex = nullptr;
ID3D11Device* device = nullptr;
ID3D11DeviceContext* context = nullptr;
if (FAILED(D3D11CreateDevice(
nullptr, // specify nullptr to use the default adapter
D3D_DRIVER_TYPE_HARDWARE,
nullptr, // specify nullptr because D3D_DRIVER_TYPE_HARDWARE
// indicates that this function uses hardware
creationFlags, // optionally set debug and Direct2D compatibility flags
featureLevels,
ARRAYSIZE(featureLevels),
D3D11_SDK_VERSION, // always set this to D3D11_SDK_VERSION
&device,
nullptr,
&context)))
{
cout << "Failed creating device" << endl;
return 0;
}
else
{
cout << "Created device" << endl;
}
D3D11_SUBRESOURCE_DATA initData = { 0 };
initData.pSysMem = (const void*)pixels;
initData.SysMemPitch = width * 4 * sizeof(unsigned char);
initData.SysMemSlicePitch = height * width * 4;
D3D11_TEXTURE2D_DESC tex_desc = {};
tex_desc.Height = height;
tex_desc.Width = width;
tex_desc.MipLevels = 1;
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;
HRESULT hres = device->CreateTexture2D(&tex_desc, &initData, &tex); // Causes exception
if (FAILED(hres))
{
cout << "Could not create texture" << endl;
return 0;
}
else
{
cout << "Created texture" << endl;
}
if (tex)
{
tex->Release();
}
我还尝试创建一个空纹理并按照互联网上的建议使用 UpdateSubresource 并创建了这个场景:
const int width = 1920;
const int height = 1080;
unsigned char* pixels = new unsigned char[width * height * 4];
int numPixels = (int)(width * height);
// Setting up pixels
for (int i = 0; i < numPixels; i++)
{
*(pixels) = 255;
*(pixels + 1) = 255;
*(pixels + 2) = 255;
*(pixels + 3) = 255;
pixels += 4; // move the pointer along to the next rgba pixel
}
// Creating DirectX11 Device
UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_DEBUG;
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_1
};
ID3D11Texture2D* tex = nullptr;
ID3D11Device* device = nullptr;
ID3D11DeviceContext* context = nullptr;
if (FAILED(D3D11CreateDevice(
nullptr, // specify nullptr to use the default adapter
D3D_DRIVER_TYPE_HARDWARE,
nullptr, // specify nullptr because D3D_DRIVER_TYPE_HARDWARE
// indicates that this function uses hardware
creationFlags, // optionally set debug and Direct2D compatibility flags
featureLevels,
ARRAYSIZE(featureLevels),
D3D11_SDK_VERSION, // always set this to D3D11_SDK_VERSION
&device,
nullptr,
&context)))
{
cout << "Failed creating device" << endl;
return 0;
}
else
{
cout << "Created device" << endl;
}
D3D11_TEXTURE2D_DESC tex_desc = {};
tex_desc.Height = height;
tex_desc.Width = width;
tex_desc.MipLevels = 1;
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;
HRESULT hres = device->CreateTexture2D(&tex_desc, NULL, &tex);
if (FAILED(hres))
{
cout << "Could not create texture" << endl;
return 0;
}
else
{
cout << "Created texture" << endl;
}
int row_pitch = (width * 4) * sizeof(unsigned char);
context->UpdateSubresource(tex, 0, NULL, pixels, row_pitch, 0); // Causes exception
if (tex)
{
tex->Release();
}
但是,这两种情况都会导致相同的异常,我认为这表明我的输入数据实际上是错误的,但我不明白为什么它是错误的。我pixels确实代表了一个 RGBA 图像,我设置了tex_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM对我来说看起来正确的图像,但我对此完全没有经验,所以我无法真正判断。
我将不胜感激任何帮助。