Formalize cubemap skybox pipeline across backends
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
#include "XCEngine/RHI/D3D12/D3D12Texture.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace RHI {
|
||||
|
||||
@@ -72,22 +75,12 @@ bool D3D12Texture::InitializeFromExisting(ID3D12Resource* resource, bool ownsRes
|
||||
return true;
|
||||
}
|
||||
|
||||
bool D3D12Texture::InitializeFromData(ID3D12Device* device, ID3D12GraphicsCommandList* commandList,
|
||||
const void* pixelData, uint32_t width, uint32_t height, DXGI_FORMAT format, uint32_t rowPitch,
|
||||
ComPtr<ID3D12Resource>* uploadBuffer) {
|
||||
|
||||
D3D12_RESOURCE_DESC textureDesc = {};
|
||||
textureDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
|
||||
textureDesc.Alignment = 0;
|
||||
textureDesc.Width = width;
|
||||
textureDesc.Height = height;
|
||||
textureDesc.DepthOrArraySize = 1;
|
||||
textureDesc.MipLevels = 1;
|
||||
textureDesc.Format = format;
|
||||
textureDesc.SampleDesc.Count = 1;
|
||||
textureDesc.SampleDesc.Quality = 0;
|
||||
textureDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
|
||||
textureDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
|
||||
bool D3D12Texture::InitializeFromData(ID3D12Device* device, ID3D12GraphicsCommandList* commandList,
|
||||
const D3D12_RESOURCE_DESC& textureDesc, TextureType textureType, const void* pixelData, size_t pixelDataSize,
|
||||
uint32_t rowPitch, ComPtr<ID3D12Resource>* uploadBuffer) {
|
||||
if (device == nullptr || commandList == nullptr || pixelData == nullptr || pixelDataSize == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
D3D12_HEAP_PROPERTIES heapProperties = {};
|
||||
heapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
|
||||
@@ -105,16 +98,24 @@ bool D3D12Texture::InitializeFromData(ID3D12Device* device, ID3D12GraphicsComman
|
||||
return false;
|
||||
}
|
||||
m_ownsResource = true;
|
||||
m_format = FromD3D12(format);
|
||||
m_textureType = TextureType::Texture2D;
|
||||
m_format = FromD3D12(textureDesc.Format);
|
||||
m_textureType = textureType;
|
||||
|
||||
textureDesc = m_resource->GetDesc();
|
||||
const UINT subresourceCount = textureDesc.MipLevels *
|
||||
(textureDesc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? 1u : textureDesc.DepthOrArraySize);
|
||||
std::vector<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> subresourceFootprints(subresourceCount);
|
||||
std::vector<UINT> subresourceRowCounts(subresourceCount);
|
||||
std::vector<UINT64> subresourceRowSizes(subresourceCount);
|
||||
UINT64 memorySizeUsed = 0;
|
||||
UINT64 rowSizeInBytes = 0;
|
||||
UINT rowUsed = 0;
|
||||
D3D12_PLACED_SUBRESOURCE_FOOTPRINT subresourceFootprint;
|
||||
device->GetCopyableFootprints(&textureDesc, 0, 1, 0,
|
||||
&subresourceFootprint, &rowUsed, &rowSizeInBytes, &memorySizeUsed);
|
||||
device->GetCopyableFootprints(
|
||||
&textureDesc,
|
||||
0,
|
||||
subresourceCount,
|
||||
0,
|
||||
subresourceFootprints.data(),
|
||||
subresourceRowCounts.data(),
|
||||
subresourceRowSizes.data(),
|
||||
&memorySizeUsed);
|
||||
|
||||
ComPtr<ID3D12Resource> tempBufferObject;
|
||||
D3D12_HEAP_PROPERTIES d3dTempHeapProperties = {};
|
||||
@@ -146,26 +147,49 @@ bool D3D12Texture::InitializeFromData(ID3D12Device* device, ID3D12GraphicsComman
|
||||
return false;
|
||||
}
|
||||
|
||||
BYTE* pData;
|
||||
BYTE* pData = nullptr;
|
||||
tempBufferObject->Map(0, nullptr, reinterpret_cast<void**>(&pData));
|
||||
BYTE* pDstTempBuffer = reinterpret_cast<BYTE*>(pData + subresourceFootprint.Offset);
|
||||
const BYTE* pSrcData = reinterpret_cast<const BYTE*>(pixelData);
|
||||
const UINT sourceRowPitch = rowPitch > 0 ? rowPitch : static_cast<UINT>(rowSizeInBytes);
|
||||
for (UINT i = 0; i < rowUsed; i++) {
|
||||
memcpy(pDstTempBuffer + subresourceFootprint.Footprint.RowPitch * i, pSrcData + sourceRowPitch * i, rowSizeInBytes);
|
||||
const UINT sourceRowPitch = rowPitch > 0 ? rowPitch : static_cast<UINT>(subresourceRowSizes[0]);
|
||||
|
||||
size_t sourceOffset = 0;
|
||||
for (UINT subresourceIndex = 0; subresourceIndex < subresourceCount; ++subresourceIndex) {
|
||||
const D3D12_PLACED_SUBRESOURCE_FOOTPRINT& footprint = subresourceFootprints[subresourceIndex];
|
||||
const UINT rowCount = subresourceRowCounts[subresourceIndex];
|
||||
const UINT64 rowSizeInBytes = subresourceRowSizes[subresourceIndex];
|
||||
BYTE* pDstTempBuffer = pData + footprint.Offset;
|
||||
|
||||
for (UINT depthSlice = 0; depthSlice < footprint.Footprint.Depth; ++depthSlice) {
|
||||
for (UINT rowIndex = 0; rowIndex < rowCount; ++rowIndex) {
|
||||
const size_t srcRowOffset =
|
||||
sourceOffset +
|
||||
static_cast<size_t>(depthSlice) * static_cast<size_t>(sourceRowPitch) * rowCount +
|
||||
static_cast<size_t>(rowIndex) * sourceRowPitch;
|
||||
memcpy(
|
||||
pDstTempBuffer +
|
||||
static_cast<size_t>(depthSlice) * footprint.Footprint.RowPitch * rowCount +
|
||||
static_cast<size_t>(rowIndex) * footprint.Footprint.RowPitch,
|
||||
pSrcData + srcRowOffset,
|
||||
static_cast<size_t>(std::min<UINT64>(rowSizeInBytes, sourceRowPitch)));
|
||||
}
|
||||
}
|
||||
|
||||
sourceOffset += static_cast<size_t>(sourceRowPitch) * rowCount * footprint.Footprint.Depth;
|
||||
}
|
||||
tempBufferObject->Unmap(0, nullptr);
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION dst = {};
|
||||
dst.pResource = m_resource.Get();
|
||||
dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
dst.SubresourceIndex = 0;
|
||||
for (UINT subresourceIndex = 0; subresourceIndex < subresourceCount; ++subresourceIndex) {
|
||||
D3D12_TEXTURE_COPY_LOCATION dst = {};
|
||||
dst.pResource = m_resource.Get();
|
||||
dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
dst.SubresourceIndex = subresourceIndex;
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION src = {};
|
||||
src.pResource = tempBufferObject.Get();
|
||||
src.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
src.PlacedFootprint = subresourceFootprint;
|
||||
commandList->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
|
||||
D3D12_TEXTURE_COPY_LOCATION src = {};
|
||||
src.pResource = tempBufferObject.Get();
|
||||
src.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
src.PlacedFootprint = subresourceFootprints[subresourceIndex];
|
||||
commandList->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
|
||||
}
|
||||
|
||||
D3D12_RESOURCE_BARRIER barrier = {};
|
||||
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
||||
|
||||
Reference in New Issue
Block a user