作者damody (天亮damody)
看板C_and_CPP
標題Re: [問題] 關於Direct跑出來的畫面問題
時間Wed Oct 16 19:19:53 2013
※ 引述《SuperEel (尖頭鰻)》之銘言:
: 開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
: Visual Studio 2005
: 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
: DirectX 2007 April
: 問題(Question):
: 我看2D畫面的圖
: 右上邊的部分順著對角線往下偏了1~2個Pixel
: 讓圖看起來有點對角線對切的感覺
: 請教一下各位大大
: 甚麼函式可能會讓圖片跑起來這樣子呢?
: 我是DirectX的新手...不好意思
顯卡在創建動態材質時,會是 8 的倍數,
今天在把 directx 11 的材質轉成 opencv 時發現的,不足就放大,
ex.
1399 => 1400
1395 => 1400
1393 => 1400
1392 => 1392
附上把 directx texture 與 cv::mat 互轉的 code
static ID3D11Device* m_Device;
static ID3D11DeviceContext* m_DeviceContext;
cv::Mat m_Image;
ID3D11ShaderResourceView* GetDx11Texture()
{
if (m_Image.rows == 0 || m_Image.cols == 0)
{
return NULL;
}
D3D11_TEXTURE2D_DESC texDesc;
ZeroMemory(&texDesc, sizeof(texDesc));
texDesc.Width = m_Image.rows;
texDesc.Height = m_Image.cols;
texDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
texDesc.MipLevels = 1;
texDesc.ArraySize = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.SampleDesc.Count = 1;
texDesc.Usage = D3D11_USAGE_DEFAULT;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texDesc.CPUAccessFlags = 0;
texDesc.MiscFlags = 0;
D3D11_SHADER_RESOURCE_VIEW_DESC srDesc;
srDesc.Format = texDesc.Format;
srDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srDesc.Texture2D.MostDetailedMip = 0;
srDesc.Texture2D.MipLevels = 1;
float* characterImages = new float[m_Image.rows * m_Image.cols * 4];
for (int j = 0; j < m_Image.cols; ++j)
{
for (int i = 0; i < m_Image.rows; ++i)
{
int offset = (j * m_Image.rows + i) * 4;
cv::Vec3b intensity = m_Image.at<cv::Vec3b>(i, j);
characterImages[offset ] = intensity[2] / 255.0f;
characterImages[offset + 1] = intensity[1] / 255.0f;
characterImages[offset + 2] = intensity[0] / 255.0f;
characterImages[offset + 3] = 1.0f;
}
}
texDesc.Width = m_Image.rows;
texDesc.Height = m_Image.cols;
D3D11_SUBRESOURCE_DATA sSubData;
sSubData.SysMemPitch = (UINT)(m_Image.rows * 4 * 4);
sSubData.SysMemSlicePitch = (UINT)(m_Image.rows * m_Image.cols * 4 * 4);
sSubData.pSysMem = characterImages;
ID3D11Texture2D* pTextureDraw;
HRESULT d3dResult = m_Device->CreateTexture2D(&texDesc, &sSubData,
&pTextureDraw);
ID3D11ShaderResourceView* pShaderResView;
int x = 0;
delete[] characterImages;
if (FAILED(d3dResult))
{
DXTRACE_MSG(L"vavImage: Failed to create texture2D!");
return 0;
}
d3dResult = m_Device->CreateShaderResourceView(pTextureDraw, &srDesc,
&pShaderResView);
if (FAILED(d3dResult))
{
DXTRACE_MSG(L"vavImage: Failed to create ShaderResourceView!");
return 0;
}
if (0) // 這段把 texture 轉回 cv::Mat
{
// take draw texture
ID3D11Texture2D* pTextureRead;
D3D11_TEXTURE2D_DESC texDescCV;
ZeroMemory(&texDescCV, sizeof(texDescCV));
texDescCV.Width = m_Image.rows;
texDescCV.Height = m_Image.cols;
texDescCV.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
texDescCV.MipLevels = 1;
texDescCV.ArraySize = 1;
texDescCV.SampleDesc.Quality = 0;
texDescCV.SampleDesc.Count = 1;
texDescCV.MiscFlags = 0;
texDescCV.Usage = D3D11_USAGE_STAGING;
texDescCV.BindFlags = 0;
texDescCV.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
HR(m_Device->CreateTexture2D(&texDescCV, 0, &pTextureRead));
m_DeviceContext->CopyResource(pTextureRead, pTextureDraw);
D3D11_MAPPED_SUBRESOURCE MappedResource;
float* pimg;
//HR(m_DeviceContext->Map(pTextureRead, 0, D3D11_MAP_READ, 0, &MappedResource));
unsigned int subresource = D3D11CalcSubresource(0, 0, 0);
HR(m_DeviceContext->Map(pTextureRead, subresource, D3D11_MAP_READ, 0,
&MappedResource));
pimg = (float*)MappedResource.pData;
cv::Mat simg = m_Image.clone();
simg = cv::Scalar(0);
int addoffset = (8 - (simg.rows - (simg.rows / 8 * 8))) % 8;
printf("addoffset: %d \n", addoffset);
for (int j = 0; j < simg.cols; ++j)
{
for (int i = 0; i < simg.rows; ++i)
{
int offset = (j * (simg.rows + addoffset) + i) * 4;
cv::Vec3b& intensity = simg.at<cv::Vec3b>(i, j);
intensity[2] = pimg[offset ] * 255.0f;
intensity[1] = pimg[offset + 1] * 255.0f;
intensity[0] = pimg[offset + 2] * 255.0f;
}
}
cv::imshow("simg", simg);
}
return pShaderResView;
}
就這樣的感覺,注意:
int addoffset = (8 - (simg.rows - (simg.rows / 8 * 8))) % 8;
這邊是算 offset。
--
標題 [趣事] 說服老媽進電影院看少年PI
→ milkfox:我的老虎哪有這麼可愛 老虎也要談戀愛 人X虎 11/28 10:18
→ milkfox:鄰座的怪老虎 元氣少年漂流虎 漂流床的寵物老虎 11/28 10:19
→ milkfox:少年與老虎漂流 絕園的人與虎 PSYCHO_TIGER TIGER_NOTES 11/28 10:22
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.118.175.35
→ azureblaze:不見得是8的倍數,早期顯示卡還限制是2^n (POT) 10/16 20:05
→ azureblaze:應該直接用D3D11_MAPPED_SUBRESOURCE 的RowPitch 10/16 20:06
→ azureblaze:影像相關處理常常都會有這個叫做pitch的東西 10/16 20:10
→ azureblaze:告訴你每行差多少byte 10/16 20:10
→ damody:感謝指教,我再研究看看 10/16 21:53