问题描述
首先,我使用RasterTek DX10教程11:2D渲染中的DirectX,并能够使示例运行。 https://www.rastertek.com/dx10tut11.html
我的目标是通过共享内存将一个UINT32数组从一个应用程序传递到我的新DirectX应用程序,以在屏幕上为每个彩色图像显示每个像素10位,即dxgi_FORMAT_R10G10B10A2_UnorM。出于开发目的,我只是在我的DirectX应用程序中填充数据数组,其中所有像素都具有相同的值(一旦工作,我将不仅仅显示纯色,这就是我尝试这样做的原因)。我可以看到数据是有道理的,例如仅显示一个纯白色图像,我所有像素值=2147483647。这是设置数据的代码:
Cols = GetSystemMetrics(SM_CXSCREEN);
Rows = GetSystemMetrics(SM_CYSCREEN);
Data = new UINT32[static_cast<__int64>(Cols) * Rows];
DataSize = static_cast<__int64>(Cols) * Rows * sizeof(UINT32);
for (int row = 0; row < Rows; row++)
for (int col = 0; col < Cols; coL++)
{
int red = 1023;
int green = 1023;
int blue = 1023;
int alpha = 1;
Data[col + (row * col)] = UINT32(alpha << 30 | blue << 20 | green << 10 | red);
}
然后,我通过D3D10_SUBRESOURCE_DATA.pSysMem初始化纹理数据并绑定着色器资源来创建纹理:
D3D10_TEXTURE2D_DESC desc;
ZeroMemory(&desc,sizeof(D3D10_TEXTURE2D_DESC));
desc.Width = Cols;
desc.Height = Rows;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = dxgi_FORMAT_R10G10B10A2_UnorM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D10_USAGE_DEFAULT;
desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
desc.cpuAccessFlags = 0;
desc.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA initialData;
initialData.pSysMem = (void*)Data; // you want to use the texel array as the data
initialData.SysMemPitch = sizeof(UINT32) * Cols; // distance between two adjacent lines of texels in memory
initialData.SysMemSlicePitch = 0; // slice pitch is meaningless for 2d textures,but Could be width*height*sizeof(texel) if this was a volume
HRESULT hr1 = device->CreateTexture2D(&desc,&initialData,&m_Texture);
if (Failed(hr1))
{
m_Texture->Release();
return false;
}
// Create the shader-resource view
D3D10_SHADER_RESOURCE_VIEW_DESC srDesc;
srDesc.Format = desc.Format;
srDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
srDesc.Texture2D.MostDetailedMip = 0;
srDesc.Texture2D.MipLevels = 1;
hr1 = device->CreateShaderResourceView(m_Texture,&srDesc,&m_textureSRV);
if (Failed(hr1))
{
m_textureSRV->Release();
//m_rendertargetView->Release();
m_Texture->Release();
return false;
}
return true;
几乎所有内容都可以正常运行并显示图像。我希望屏幕上的所有像素都是白色,但有些是绿色Expect solid white image,but some pixels are green。
此外,我相当有把握地正确设置数据数组,因为例如,如果我将数据设置为显示所有红色像素,则白色像素现在是红色,而绿色像素仍然是绿色。
我已打开DX调试,并且在初始化设备时没有看到任何错误。然后,如果我“开始图形调试”,则会收到以下错误:
D3D11错误:ID3D10Device :: copyResource:当每个资源的格式不相同或彼此可转换时,无法调用copyResource,除非GetFeatureLevel()返回D3D_FEATURE_LEVEL_10_1或更大。 [RESOURCE_MANIPULATION错误#284:copYRESOURCE_INVALIDSOURCE]。
这对我来说似乎很奇怪,因为我也将swapChainDesc.BufferDesc.Format设置为dxgi_FORMAT_R10G10B10A2_UnorM
但是,如果我将D3D10_TEXTURE2D_DESC格式和swapChainDesc.BufferDesc.Format更改为dxgi_FORMAT_R8G8B8A8_UnorM并将我的数据设置为与新格式匹配,此错误就会消失,但是我在屏幕上仍然有同样的奇怪图像。
此外,如果我运行示例代码并仅显示原始随附的示例位图,则似乎正确显示了该位图。在这种情况下,唯一真正的区别是,我上面的所有代码均未使用,而是按如下方式加载纹理:
result = D3DX10CreateShaderResourceViewFromFile(device,filename,NULL,&m_textureSRV,NULL);
因此,结论:
- 似乎我创建纹理的方式不正确,或者可能无法与我的着色器很好地配合使用,请参见下面的代码?我不确定。
- 我很困惑为什么我在尝试使用dxgi_FORMAT_R10G10B10A10_UnorM时收到RESOURCE_MANIPULATION错误#284:copYRESOURCE_INVALIDSOURCE,这是我使用的其他更简单的示例,其中我确实想通过仅将屏幕设置为纯色来实现此目的具有选定纯色的ClearrendertargetView。
////////////////////////////////////////////////////////////////////////////////
// Filename: texture.fx
////////////////////////////////////////////////////////////////////////////////
/////////////
// GLOBALS //
/////////////
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
Texture2D shaderTexture;
///////////////////
// SAMPLE STATES //
///////////////////
SamplerState SampleType
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
//////////////
// TYPEDEFS //
//////////////
struct VertexInputType
{
float4 position : POSITION;
float2 tex : TEXCOORD0;
};
struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
};
////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PixelInputType TextureVertexShader(VertexInputType input)
{
PixelInputType output;
// Change the position vector to be 4 units for proper matrix calculations.
input.position.w = 1.0f;
// Calculate the position of the vertex against the world,view,and projection matrices.
output.position = mul(input.position,worldMatrix);
output.position = mul(output.position,viewMatrix);
output.position = mul(output.position,projectionMatrix);
// Store the texture coordinates for the pixel shader.
output.tex = input.tex;
return output;
}
////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 TexturePixelShader(PixelInputType input) : SV_Target
{
float4 textureColor;
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
textureColor = shaderTexture.Sample(SampleType,input.tex);
//textureColor = D3DX_R10G10B10A2_UnorM_to_FLOAT4(shaderTexture.Sample(SampleType,input.tex));
return textureColor;
}
////////////////////////////////////////////////////////////////////////////////
// Technique
////////////////////////////////////////////////////////////////////////////////
technique10 TextureTechnique
{
pass pass0
{
SetVertexShader(CompileShader(vs_4_0,TextureVertexShader()));
SetPixelShader(CompileShader(ps_4_0,TexturePixelShader()));
SetGeometryShader(NULL);
}
}
请让我知道,我会提供可能需要的其他东西。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)