Add RHI texture upload and descriptor set fixes

This commit is contained in:
2026-03-26 00:04:51 +08:00
parent 605ef56e16
commit 76c4c2ace2
15 changed files with 468 additions and 46 deletions

View File

@@ -60,6 +60,27 @@ bool CompileD3D12Shader(const ShaderCompileDesc& desc, D3D12Shader& shader) {
return false;
}
uint32_t GetFormatBytesPerPixel(Format format) {
switch (format) {
case Format::R8_UNorm:
return 1;
case Format::R8G8_UNorm:
return 2;
case Format::R8G8B8A8_UNorm:
return 4;
case Format::R16_Float:
return 2;
case Format::R16G16B16A16_Float:
return 8;
case Format::R32_Float:
return 4;
case Format::R32G32B32A32_Float:
return 16;
default:
return 0;
}
}
} // namespace
D3D12Device::D3D12Device()
@@ -326,6 +347,77 @@ RHITexture* D3D12Device::CreateTexture(const TextureDesc& desc) {
return nullptr;
}
RHITexture* D3D12Device::CreateTexture(const TextureDesc& desc, const void* initialData, size_t initialDataSize, uint32_t rowPitch) {
(void)initialDataSize;
if (initialData == nullptr) {
return CreateTexture(desc);
}
if (desc.textureType != static_cast<uint32_t>(TextureType::Texture2D)) {
return nullptr;
}
const Format format = static_cast<Format>(desc.format);
uint32_t resolvedRowPitch = rowPitch;
if (resolvedRowPitch == 0) {
const uint32_t bytesPerPixel = GetFormatBytesPerPixel(format);
if (bytesPerPixel == 0) {
return nullptr;
}
resolvedRowPitch = desc.width * bytesPerPixel;
}
D3D12CommandQueue uploadQueue;
if (!uploadQueue.Initialize(m_device.Get(), CommandQueueType::Direct)) {
return nullptr;
}
D3D12CommandAllocator uploadAllocator;
if (!uploadAllocator.Initialize(m_device.Get(), CommandQueueType::Direct)) {
uploadQueue.Shutdown();
return nullptr;
}
D3D12CommandList uploadCommandList;
if (!uploadCommandList.Initialize(m_device.Get(), CommandQueueType::Direct, uploadAllocator.GetCommandAllocator())) {
uploadAllocator.Shutdown();
uploadQueue.Shutdown();
return nullptr;
}
uploadAllocator.Reset();
uploadCommandList.Reset();
auto* texture = new D3D12Texture();
if (!texture->InitializeFromData(
m_device.Get(),
uploadCommandList.GetCommandList(),
initialData,
desc.width,
desc.height,
ToD3D12(format),
resolvedRowPitch)) {
delete texture;
uploadCommandList.Shutdown();
uploadAllocator.Shutdown();
uploadQueue.Shutdown();
return nullptr;
}
texture->SetState(ResourceStates::PixelShaderResource);
uploadCommandList.Close();
ID3D12CommandList* commandLists[] = { uploadCommandList.GetCommandList() };
uploadQueue.ExecuteCommandListsInternal(1, commandLists);
uploadQueue.WaitForIdle();
uploadCommandList.Shutdown();
uploadAllocator.Shutdown();
uploadQueue.Shutdown();
return texture;
}
RHIShader* D3D12Device::CreateShader(const ShaderCompileDesc& desc) {
auto* shader = new D3D12Shader();
const std::string entryPoint = NarrowAscii(desc.entryPoint);