#1 GDNet+ - Reputation: 747
Posted 29 October 2012 - 06:21 PM
This is the code that is suppose to fill the texture with the content in the XMFLOAT3 vector:
[source lang="cpp"]void Terrain::BuildBlendMapSRV(ID3D11Device* device){ // Fill out the texture description. D3D11_TEXTURE2D_DESC texDesc; texDesc.Width = mInfo.HeightmapWidth; texDesc.Height = mInfo.HeightmapHeight; texDesc.MipLevels = 1; texDesc.ArraySize = 1; texDesc.Format = DXGI_FORMAT_R32G32B32_FLOAT; // == XMFLOAT3 right? texDesc.SampleDesc.Count = 1; texDesc.SampleDesc.Quality = 0; texDesc.Usage = D3D11_USAGE_DYNAMIC; texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; texDesc.MiscFlags = 0; // Create the texture. ID3D11Texture2D* bmapTex = 0; HR(device->CreateTexture2D(&texDesc, 0, &bmapTex)); // Create the SRV to the texture. D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; srvDesc.Format = texDesc.Format; srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; srvDesc.Texture2D.MostDetailedMip = 0; srvDesc.Texture2D.MipLevels = -1; HR(device->CreateShaderResourceView(bmapTex, &srvDesc, &mBlendMapSRV)); // Get the texture. ID3D11Resource* resource = nullptr; mBlendMapSRV->GetResource(&resource); ID3D11Texture2D* texture = nullptr; resource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&texture); // Map the texture. D3D11_MAPPED_SUBRESOURCE data; GetD3DContext()->Map(texture, 0, D3D11_MAP_WRITE_DISCARD, 0, &data); data.pData = &mBlendMap[0]; // The std::vector<XMFLOAT3> GetD3DContext()->Unmap(texture, 0);}[/source]
The mapping doesn't affect the texture at all and I got no clue why it doesn't. I believe the format is correct, DXGI_FORMAT_R32G32B32_FLOAT corresponds to XMFLOAT3 right? Hopefully it's just a detail in my code, or maybe I'm doing it all wrong. I really appreciate any help, thanks.
#2 Moderators - Reputation: 5487
Posted 29 October 2012 - 08:37 PM
const uint32_t pixelSize = 12; // sizeof(DXGI_FORMAT_R32G32B32_FLOAT)
const uint32_t srcPitch = pixelSize * textureWidth;
uint8_t* textureData = reinterpret_cast<uint8_t*>(data.pData);
const uint8_t* srcData = reinterpret_cast<const uint8_t*>(mBlendMap.data());
for(uint32_t i = 0; i < textureHeight; ++i)
{
// Copy the texture data for a single row
memcpy(textureData, srcData, srcPitch);
// Advance the pointers
textureData += data.RowPitch;
srcData += srcPitch;
}
Edited by MJP, 29 October 2012 - 08:41 PM.
#3 GDNet+ - Reputation: 747
Posted 30 October 2012 - 04:17 AM
The code snippet don't want to work this time...
// Map the texture.
D3D11_MAPPED_SUBRESOURCE data;
GetD3DContext()->Map(texture, 0, D3D11_MAP_WRITE_DISCARD, 0, &data);
// All pixels red just for testing.
vector<XMFLOAT3> redVector;
for(int i = 0; i < mInfo.HeightmapWidth * mInfo.HeightmapHeight; i++)
redVector.push_back(XMFLOAT3(1, 0, 0));
const uint32_t pixelSize = sizeof(XMFLOAT3); // 12
const uint32_t srcPitch = pixelSize * mInfo.HeightmapWidth;
uint8_t* textureData = reinterpret_cast<uint8_t*>(data.pData);
const uint8_t* srcData = reinterpret_cast<uint8_t*>(redVector.data());
for(uint32_t i = 0; i < mInfo.HeightmapHeight; ++i)
{
memcpy(textureData, srcData, srcPitch);
textureData += data.RowPitch;
srcData += srcPitch;
}
GetD3DContext()->Unmap(texture, 0);
This is what the texture looks like after mapping and updating it:

As you can see there is a stripe pattern and the color of the texture is not red as it is suppose to be. I don't know why this is the case but since I'm memcpy()ing in a row fassion then it maybe has something to do with that?
Edited by simpler, 30 October 2012 - 04:22 AM.
#4 GDNet+ - Reputation: 747
Posted 30 October 2012 - 04:07 PM
[source lang="cpp"]vector<XMFLOAT3> redVector; for(int i = 0; i < mInfo.HeightmapWidth * mInfo.HeightmapHeight; i++) redVector.push_back(XMFLOAT3(1, 0, 0)); // Set the initial data to be the heightmap. D3D11_SUBRESOURCE_DATA data; data.pSysMem = redVector.data(); data.SysMemPitch = mInfo.HeightmapWidth*sizeof(XMFLOAT3); data.SysMemSlicePitch = 0; // Create the texture. ID3D11Texture2D* bmapTex = 0; HR(device->CreateTexture2D(&texDesc, &data, &bmapTex));[/source]
I set the format to DXGI_FORMAT_R32G32B32_FLOAT. I can set the data like this just fine in my heightmap, the difference is that it uses the format DXGI_FORMAT_R16_FLOAT and each member in the vector is a 16-bit float. But now that I'm using XMFLOAT3 and the format DXGI_FORMAT_R32G32B32_FLOAT I see no reason for it to not work...
Edited by simpler, 30 October 2012 - 04:08 PM.
#5 GDNet+ - Reputation: 747
Posted 31 October 2012 - 05:03 PM






