2016-10-25 95 views
0

我嘗試用2d圖像填充texture3D切片。 我的結果只給我回的第一個6個圖像就像你可以在這個圖片中看到:填充texture3d切片明智

3D cube with the texture3D texture

,以確保它不像錯誤UVW渲染問題座標我也給你的圖片在UVW座標:

uvw coordinates of the 3d cube

這裏是創建texture3d的代碼:

if (ETextureType::Texture3D == TextureType) 
{ 
    ID3D11Texture3D* pTexture3D = nullptr; 
    D3D11_TEXTURE3D_DESC TextureDesc; 
    ZeroMemory(&TextureDesc, sizeof(TextureDesc)); 
    TextureDesc.Width = nWidth; 
    TextureDesc.Height = nHeight; 
    TextureDesc.MipLevels = nMipMaps; 
    TextureDesc.Depth = nDepth; 
    TextureDesc.Usage = D3D11_USAGE_DEFAULT; 
    TextureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; 

    switch (TextureFormat) 
    { 
     case ETextureFormat::R8G8B8A8: 
     { 
      TextureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; 
     } 
     break; 

     case ETextureFormat::R32FG32FB32FA32F: 
     { 
      TextureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; 
     } 
     break; 

     default: 
     DebugAssertOnce(UNKNOWN_TEXTURE_FORMAT); 
    } 

    HRESULT hr = m_pD3D11Device->CreateTexture3D(&TextureDesc, nullptr, &pTexture3D); 

    if (FAILED(hr)) 
    { 
     DebugAssertOnce(UNABLE_TO_CREATE_TEXTURE); 
     return false; 
    } 

    if (bCreateShaderResourceView) 
    { 
     D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc; 
     ZeroMemory(&SRVDesc, sizeof(SRVDesc)); 

     SRVDesc.Format = TextureDesc.Format; 
     SRVDesc.Texture3D.MipLevels = TextureDesc.MipLevels; 
     SRVDesc.Texture3D.MostDetailedMip = 0; 
     SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D; 
     hr = m_pD3D11Device->CreateShaderResourceView(pTexture3D, &SRVDesc, &pShaderResourceView); 

     if (FAILED(hr)) 
     { 
      pTexture3D->Release(); 

      DebugAssertOnce(UNABLE_TO_CREATE_SHADER_RESOURCE_VIEW); 
      return false; 
     } 
    } 

    else if (bCreateRenderTargetView) 
    { 
     ID3D11RenderTargetView* pRenderTargetView = nullptr; 

     hr = m_pD3D11Device->CreateRenderTargetView(pTexture3D, nullptr, &pRenderTargetView); 

     if (FAILED(hr)) 
     { 
      pShaderResourceView->Release(); 
      pTexture3D->Release(); 

      DebugAssertOnce(UNABLE_TO_CREATE_RENDERTARGET_VIEW); 
      return false; 
     } 

     pView = pRenderTargetView; 
    } 
    *ppTexture = new CTextureDX11(TextureType, pTexture3D, pShaderResourceView, pView); 
    return true; 
} 

也填充部分:

bool CGraphicsDriverDX11::CreateTexture3DFromImageBuffers(CTexture** ppTexture, const std::vector<CImageBuffer*>* pvecImageBuffers) 
{ 
    uint32_t nWidth = pvecImageBuffers->front()->GetWidth(); 
    uint32_t nHeight = pvecImageBuffers->front()->GetHeight(); 

    uint32_t nMipMapLevels = 1; 

    bool bRet = CreateTexture(ppTexture, nWidth, nHeight, ETextureType::Texture3D, ETextureFormat::R8G8B8A8, nMipMapLevels, false, true, false, static_cast<UINT>(pvecImageBuffers->size())); 

    if (bRet) 
    { 
     ID3D11Texture3D* pD3DTexture = static_cast<ID3D11Texture3D*>((*ppTexture)->GetTexture()); 

     for (size_t nImageBuffer = 0; nImageBuffer < pvecImageBuffers->size(); ++nImageBuffer) 
     { 
      uint32_t nIndex = D3D11CalcSubresource(static_cast<UINT>(nImageBuffer), 0, 1); 

      m_pD3D11DeviceContext->UpdateSubresource(pD3DTexture, nIndex, nullptr, pvecImageBuffers->at(nImageBuffer)->GetData(), nWidth * 4, 0); 

     } 
    } 

    return bRet; 
} 

我嘗試了很多......比如我改變了這種代碼texture2DArray它工作得很好。所以這個錯誤不是我的CImageBuffer類。也nDepth變量具有正確的值...我想我必須使用另一個命令UpdateSubresource或至少更改參數以某種方式。 我也沒有在互聯網上找到一些例子。 先進的謝謝

回答

2

切片中的所有深度貼圖都並排在一個子資源中。您還需要計算給定miplevel的切片中有多少深度圖像。

這給了你這包含了整個片子資源指數:

D3D11CalcSubresource(level, 0, mipLevels); 

這給你的圖像數量的切片對於給定的miplevel:

std::max(depth >> level, 1); 

每幀圖像的切片具有在子資源中一個接一個佈置的節距D3D11_MAPPED_SUBRESOURCE.RowPitch,切片的總大小(字節數)爲D3D11_MAPPED_SUBRESOURCE.DepthPitch

例如,這裏是一些代碼從DirectXTex修剪了一下,使其更容易閱讀。它正在從捕獲的3D卷紋理中讀取數據,但填充紋理時的邏輯是相同的。

if (metadata.IsVolumemap()) 
    { 
     assert(metadata.arraySize == 1); 

     size_t height = metadata.height; 
     size_t depth = metadata.depth; 

     for (size_t level = 0; level < metadata.mipLevels; ++level) 
     { 
      UINT dindex = D3D11CalcSubresource(level, 0, metadata.mipLevels); 

      D3D11_MAPPED_SUBRESOURCE mapped; 
      HRESULT hr = pContext->Map(pSource, dindex, D3D11_MAP_READ, 0, &mapped); 
      if (FAILED(hr)) 
       // error 

      auto pslice = reinterpret_cast<const uint8_t*>(mapped.pData); 

      size_t lines = ComputeScanlines(metadata.format, height); 
      // For uncompressed images, lines == height 

      for (size_t slice = 0; slice < depth; ++slice) 
      { 
       const Image* img = result.GetImage(level, 0, slice); 

       const uint8_t* sptr = pslice; 
       uint8_t* dptr = img->pixels; 
       for (size_t h = 0; h < lines; ++h) 
       { 
        size_t msize = std::min<size_t>(img->rowPitch, mapped.RowPitch); 
        memcpy_s(dptr, img->rowPitch, sptr, msize); 
        sptr += mapped.RowPitch; 
        dptr += img->rowPitch; 
       } 

       pslice += mapped.DepthPitch; 
      } 

      pContext->Unmap(pSource, dindex); 

      if (height > 1) 
       height >>= 1; 
      if (depth > 1) 
       depth >>= 1; 
     } 
    } 
+0

非常感謝!它是這樣工作的! – Thomas