Question

I'm trying to create a texture using code, turn it into a shader resource view then apply it to a plane, however all I'm getting is a black square. I tried using the sample code on msdn to no avail, also tried using unsigned char and float (float is shown below as that is what I'll need to use for my end goal).

Here is the code trying to create the texture:

    bool TerrainClass::CreateTexture(ID3D11Device* _device)
{
    ID3D11Texture2D *texture;
    D3D11_TEXTURE2D_DESC tdesc;
    D3D11_SUBRESOURCE_DATA data;

    float *buf = (float *)malloc(m_terrainWidth * m_terrainHeight * 4 * sizeof(float));

    for(int i = 0; i < m_terrainWidth * m_terrainHeight * 4; i++)
        buf[i] = 1.0f;

    data.pSysMem = (void *)buf;
    data.SysMemPitch = m_terrainWidth * 4;
    data.SysMemSlicePitch = m_terrainWidth * m_terrainHeight * 4;

    tdesc.Width = m_terrainWidth;
    tdesc.Height = m_terrainHeight;
    tdesc.MipLevels = 1;
    tdesc.ArraySize = 1;
    tdesc.SampleDesc.Count = 1;
    tdesc.SampleDesc.Quality = 0;
    tdesc.Usage = D3D11_USAGE_DEFAULT;
    tdesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
    tdesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;

    tdesc.CPUAccessFlags = 0;
    tdesc.MiscFlags = 0;

    //attempt to create the 2d texture
    if(FAILED(_device->CreateTexture2D(&tdesc,&data,&texture)))
        return false;

    //assign the texture made from fbm to one of the textures for the terrain
    if(!(m_Textures->ChangeTexture(_device, texture, 2)))
        return false;

    delete[] buf;

    return true;
}

Unless I'm misunderstanding that should be a white texture made. The texture is then passed into the texture array to this function:

bool TextureArrayClass::ChangeTexture(ID3D11Device* _device, ID3D11Texture2D* _texture, int _i)
{
        if(FAILED(_device->CreateShaderResourceView(_texture,NULL, &m_textures[_i])))
        {
            return false;
        }
        return true;
}

Which should set the shader resource view to be the texture I just created. So I'm completely lost as to where I've gone wrong, any thoughts?

Was it helpful?

Solution

data.SysMemPitch should be in bytes, so m_terrainWidth * 4 * sizeof(float). Same with SysMemSlicePitch but you can set that to zero since you only need that for a texture cube, volume, or array. You should also verify that the D3D_FEATURE_LEVEL you're running on supports using R32G32B32A32 in the way you're using it (to Sample I assume). Verify support by reviewing this page or calling CheckFormatSupport. If your hardware doesn't support the pattern you're using, switch to a more broadly supported format like B8G8R8A8_UNORM (and be sure to change your initial data as well to match the format layout).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top