Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
/// Declaration of Diligent::DynamicMemoryManagerWebGPU class

#include <mutex>
#include <memory>
#include <vector>

#include "WebGPUObjectWrappers.hpp"
Expand Down Expand Up @@ -111,9 +112,9 @@ class DynamicMemoryManagerWebGPU
size_t m_CurrentOffset = 0;
WebGPUBufferWrapper m_wgpuBuffer;

std::mutex m_AvailablePagesMtx;
std::vector<Page> m_AvailablePages;
std::vector<Uint8> m_MappedData;
std::mutex m_AvailablePagesMtx;
std::vector<Page> m_AvailablePages;
std::unique_ptr<Uint8[]> m_MappedData;
};

} // namespace Diligent
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
/// Declaration of Diligent::UploadMemoryManagerWebGPU class

#include <mutex>
#include <vector>
#include <memory>
#include <atomic>
#include <vector>

#include "WebGPUObjectWrappers.hpp"
#include "BasicTypes.h"
Expand Down Expand Up @@ -83,13 +84,14 @@ class UploadMemoryManagerWebGPU

size_t GetSize() const
{
return m_Data.size();
return m_DataSize;
}

private:
UploadMemoryManagerWebGPU* m_pMgr = nullptr;
WebGPUBufferWrapper m_wgpuBuffer;
std::vector<Uint8> m_Data;
std::unique_ptr<Uint8[]> m_Data;
size_t m_DataSize = 0;
size_t m_CurrOffset = 0;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ void DynamicMemoryManagerWebGPU::Page::Recycle()
DynamicMemoryManagerWebGPU::DynamicMemoryManagerWebGPU(WGPUDevice wgpuDevice, size_t PageSize, size_t BufferSize) :
m_PageSize{PageSize},
m_BufferSize{BufferSize},
m_CurrentOffset{0}
m_CurrentOffset{0},
m_MappedData{std::make_unique_for_overwrite<Uint8[]>(BufferSize)}
{
WGPUBufferDescriptor wgpuBufferDesc{};
wgpuBufferDesc.label = GetWGPUStringView("Dynamic buffer");
Expand All @@ -133,7 +134,6 @@ DynamicMemoryManagerWebGPU::DynamicMemoryManagerWebGPU(WGPUDevice wgpuDevice, si
WGPUBufferUsage_Index |
WGPUBufferUsage_Indirect;
m_wgpuBuffer.Reset(wgpuDeviceCreateBuffer(wgpuDevice, &wgpuBufferDesc));
m_MappedData.resize(BufferSize);

LOG_INFO_MESSAGE("Created dynamic buffer: ", BufferSize >> 10, " KB");
}
Expand Down
8 changes: 2 additions & 6 deletions Graphics/GraphicsEngineWebGPU/src/EngineFactoryWebGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,10 @@ WebGPUDeviceWrapper CreateDeviceForAdapter(const DeviceFeatures& Features, WGPUI
if (wgpuAdapterHasFeature(wgpuAdapter, WGPUFeatureName_BGRA8UnormStorage))
wgpuFeatures.push_back(WGPUFeatureName_BGRA8UnormStorage);

if (wgpuAdapterHasFeature(wgpuAdapter, WGPUFeatureName_Unorm16TextureFormats))
wgpuFeatures.push_back(WGPUFeatureName_Unorm16TextureFormats);

if (wgpuAdapterHasFeature(wgpuAdapter, WGPUFeatureName_TextureFormatsTier1))
wgpuFeatures.push_back(WGPUFeatureName_TextureFormatsTier1);

if (wgpuAdapterHasFeature(wgpuAdapter, WGPUFeatureName_TextureFormatsTier2))
wgpuFeatures.push_back(WGPUFeatureName_TextureFormatsTier2);
else if (wgpuAdapterHasFeature(wgpuAdapter, WGPUFeatureName_TextureFormatsTier1))
wgpuFeatures.push_back(WGPUFeatureName_TextureFormatsTier1);
}

struct CallbackUserData
Expand Down
121 changes: 79 additions & 42 deletions Graphics/GraphicsEngineWebGPU/src/RenderDeviceWebGPUImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,11 @@ void RenderDeviceWebGPUImpl::FindSupportedTextureFormats()

constexpr BIND_FLAGS BIND_SRU = BIND_SHADER_RESOURCE | BIND_RENDER_TARGET | BIND_UNORDERED_ACCESS;
constexpr BIND_FLAGS BIND_SR = BIND_SHADER_RESOURCE | BIND_RENDER_TARGET;
constexpr BIND_FLAGS BIND_S = BIND_SHADER_RESOURCE;
constexpr BIND_FLAGS BIND_SU = BIND_SHADER_RESOURCE | BIND_UNORDERED_ACCESS;
constexpr BIND_FLAGS BIND_D = BIND_DEPTH_STENCIL;
constexpr BIND_FLAGS BIND_SD = BIND_SHADER_RESOURCE | BIND_DEPTH_STENCIL;
constexpr BIND_FLAGS BIND_S = BIND_SHADER_RESOURCE;
constexpr BIND_FLAGS BIND_R = BIND_RENDER_TARGET;
constexpr BIND_FLAGS BIND_U = BIND_UNORDERED_ACCESS;

SAMPLE_COUNT SupportedSampleCounts = SAMPLE_COUNT_1 | SAMPLE_COUNT_4; // We can't query supported sample counts in WebGPU

Expand Down Expand Up @@ -466,57 +468,63 @@ void RenderDeviceWebGPUImpl::FindSupportedTextureFormats()
FmtInfo.ComponentType == COMPONENT_TYPE_DEPTH_STENCIL))
{
FmtInfo.Dimensions |=
RESOURCE_DIMENSION_SUPPORT_TEX_1D |
RESOURCE_DIMENSION_SUPPORT_TEX_1D_ARRAY |
RESOURCE_DIMENSION_SUPPORT_TEX_1D |
RESOURCE_DIMENSION_SUPPORT_TEX_3D;
}
// clang-format on
FmtInfo.Filterable = (FmtFlags & FMT_FLAG_FILTER) != 0;
}
};

auto UpdateTexFormatInfo = [&](std::initializer_list<TEXTURE_FORMAT> Formats, BIND_FLAGS BindFlags, Uint32 FmtFlags) {
for (TEXTURE_FORMAT Fmt : Formats)
{
TextureFormatInfoExt& FmtInfo = m_TextureFormatsInfo[Fmt];
VERIFY(FmtInfo.Supported, "The format hasn't been initialized");

FmtInfo.BindFlags |= BindFlags;
if ((FmtFlags & FMT_FLAG_MSAA) != 0)
{
VERIFY_EXPR((FmtInfo.BindFlags & (BIND_RENDER_TARGET | BIND_DEPTH_STENCIL)) != 0 || FmtInfo.IsTypeless);
FmtInfo.SampleCounts |= SupportedSampleCounts;
}
FmtInfo.Filterable |= (FmtFlags & FMT_FLAG_FILTER) != 0;
}
};

const bool BGRA8UnormStorageSupported = wgpuDeviceHasFeature(m_wgpuDevice, WGPUFeatureName_BGRA8UnormStorage);
const bool Float32FilterableSupported = wgpuDeviceHasFeature(m_wgpuDevice, WGPUFeatureName_Float32Filterable);
const bool RG11B10UfloatRenderableSupported = wgpuDeviceHasFeature(m_wgpuDevice, WGPUFeatureName_RG11B10UfloatRenderable);
const bool Depth32FloatStencil8Supported = wgpuDeviceHasFeature(m_wgpuDevice, WGPUFeatureName_Depth32FloatStencil8);
const bool TextureCompressionBCSupported = wgpuDeviceHasFeature(m_wgpuDevice, WGPUFeatureName_TextureCompressionBC);
const bool TextureCompressionETC2Supported = wgpuDeviceHasFeature(m_wgpuDevice, WGPUFeatureName_TextureCompressionETC2);
const bool TextureFormatsTier1Supported = wgpuDeviceHasFeature(m_wgpuDevice, WGPUFeatureName_TextureFormatsTier1);
const bool Unorm16TextureFormatsSupported = wgpuDeviceHasFeature(m_wgpuDevice, WGPUFeatureName_Unorm16TextureFormats);

// Tier 1 enables normalized 16-bit formats without guaranteeing filtering support.
bool Norm16FilterableSupported = Unorm16TextureFormatsSupported;
#if !PLATFORM_WEB
Norm16FilterableSupported = Norm16FilterableSupported || wgpuDeviceHasFeature(m_wgpuDevice, WGPUFeatureName_Unorm16Filterable);
#endif
const bool R16UnormSupported = TextureFormatsTier1Supported || Unorm16TextureFormatsSupported;
const bool R16SnormSupported = TextureFormatsTier1Supported;

// https://www.w3.org/TR/webgpu/#texture-format-caps

// Color formats with 8-bits per channel
SetTexFormatInfo({TEX_FORMAT_R8_TYPELESS, TEX_FORMAT_R8_UNORM}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SR, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_R8_SNORM}, TextureFormatsTier1Supported ? BIND_SRU : BIND_S, TextureFormatsTier1Supported ? FMT_FLAG_FILTER | FMT_FLAG_MSAA : FMT_FLAG_FILTER);
SetTexFormatInfo({TEX_FORMAT_R8_UINT, TEX_FORMAT_R8_SINT}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SR, FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_R8_TYPELESS, TEX_FORMAT_R8_UNORM}, BIND_SR, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_R8_SNORM}, BIND_S, FMT_FLAG_FILTER);
SetTexFormatInfo({TEX_FORMAT_R8_UINT, TEX_FORMAT_R8_SINT}, BIND_SR, FMT_FLAG_MSAA);

SetTexFormatInfo({TEX_FORMAT_RG8_TYPELESS, TEX_FORMAT_RG8_UNORM}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SR, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RG8_SNORM}, TextureFormatsTier1Supported ? BIND_SRU : BIND_S, TextureFormatsTier1Supported ? FMT_FLAG_FILTER | FMT_FLAG_MSAA : FMT_FLAG_FILTER);
SetTexFormatInfo({TEX_FORMAT_RG8_UINT, TEX_FORMAT_RG8_SINT}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SR, FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RG8_TYPELESS, TEX_FORMAT_RG8_UNORM}, BIND_SR, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RG8_SNORM}, BIND_S, FMT_FLAG_FILTER);
SetTexFormatInfo({TEX_FORMAT_RG8_UINT, TEX_FORMAT_RG8_SINT}, BIND_SR, FMT_FLAG_MSAA);

SetTexFormatInfo({TEX_FORMAT_RGBA8_TYPELESS, TEX_FORMAT_RGBA8_UNORM}, BIND_SRU, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RGBA8_UNORM_SRGB}, BIND_SR, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RGBA8_SNORM}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SU, TextureFormatsTier1Supported ? FMT_FLAG_FILTER | FMT_FLAG_MSAA : FMT_FLAG_FILTER);
SetTexFormatInfo({TEX_FORMAT_RGBA8_SNORM}, BIND_SU, FMT_FLAG_FILTER);
SetTexFormatInfo({TEX_FORMAT_RGBA8_UINT, TEX_FORMAT_RGBA8_SINT}, BIND_SRU, FMT_FLAG_MSAA);

SetTexFormatInfo({TEX_FORMAT_BGRA8_TYPELESS, TEX_FORMAT_BGRA8_UNORM}, BGRA8UnormStorageSupported ? BIND_SRU : BIND_SR, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_BGRA8_UNORM_SRGB}, BIND_SR, FMT_FLAG_FILTER | FMT_FLAG_MSAA);

// Color formats with 16-bits per channel
SetTexFormatInfo({TEX_FORMAT_R16_UINT, TEX_FORMAT_R16_SINT}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SR, FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_R16_FLOAT, TEX_FORMAT_R16_TYPELESS}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SR, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_R16_UINT, TEX_FORMAT_R16_SINT}, BIND_SR, FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_R16_FLOAT, TEX_FORMAT_R16_TYPELESS}, BIND_SR, FMT_FLAG_FILTER | FMT_FLAG_MSAA);

SetTexFormatInfo({TEX_FORMAT_RG16_UINT, TEX_FORMAT_RG16_SINT}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SR, FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RG16_FLOAT, TEX_FORMAT_RG16_TYPELESS}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SR, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RG16_UINT, TEX_FORMAT_RG16_SINT}, BIND_SR, FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RG16_FLOAT, TEX_FORMAT_RG16_TYPELESS}, BIND_SR, FMT_FLAG_FILTER | FMT_FLAG_MSAA);

SetTexFormatInfo({TEX_FORMAT_RGBA16_UINT, TEX_FORMAT_RGBA16_SINT}, BIND_SRU, FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RGBA16_FLOAT, TEX_FORMAT_RGBA16_TYPELESS}, BIND_SRU, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
Expand All @@ -532,17 +540,17 @@ void RenderDeviceWebGPUImpl::FindSupportedTextureFormats()
SetTexFormatInfo({TEX_FORMAT_RGBA32_FLOAT}, BIND_SRU, Float32FilterableSupported ? FMT_FLAG_FILTER : FMT_FLAG_NONE);

// Color formats with mixed width
SetTexFormatInfo({TEX_FORMAT_RGB10A2_TYPELESS, TEX_FORMAT_RGB10A2_UNORM}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SR, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RGB10A2_UINT}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SR, FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RGB10A2_TYPELESS, TEX_FORMAT_RGB10A2_UNORM}, BIND_SR, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RGB10A2_UINT}, BIND_SR, FMT_FLAG_MSAA);

SetTexFormatInfo({TEX_FORMAT_R11G11B10_FLOAT}, TextureFormatsTier1Supported ? BIND_SRU : (RG11B10UfloatRenderableSupported ? BIND_SR : BIND_S), RG11B10UfloatRenderableSupported ? FMT_FLAG_FILTER | FMT_FLAG_MSAA : FMT_FLAG_FILTER);
SetTexFormatInfo({TEX_FORMAT_R11G11B10_FLOAT}, RG11B10UfloatRenderableSupported ? BIND_SR : BIND_S, RG11B10UfloatRenderableSupported ? FMT_FLAG_FILTER | FMT_FLAG_MSAA : FMT_FLAG_FILTER);

// Depth-stencil formats
SetTexFormatInfo({TEX_FORMAT_D16_UNORM}, BIND_D, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_D24_UNORM_S8_UINT}, BIND_D, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_D32_FLOAT}, BIND_D, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_D16_UNORM}, BIND_SD, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_D24_UNORM_S8_UINT}, BIND_SD, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_D32_FLOAT}, BIND_SD, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
if (Depth32FloatStencil8Supported)
SetTexFormatInfo({TEX_FORMAT_D32_FLOAT_S8X24_UINT}, BIND_D, FMT_FLAG_FILTER | FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_D32_FLOAT_S8X24_UINT}, BIND_SD, FMT_FLAG_FILTER | FMT_FLAG_MSAA);

// Packed formats
SetTexFormatInfo({TEX_FORMAT_RGB9E5_SHAREDEXP}, BIND_S, FMT_FLAG_FILTER);
Expand Down Expand Up @@ -573,18 +581,47 @@ void RenderDeviceWebGPUImpl::FindSupportedTextureFormats()
BIND_S, FMT_FLAG_FILTER);
}

if (R16UnormSupported)
{
SetTexFormatInfo({TEX_FORMAT_R16_UNORM}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SR, Norm16FilterableSupported ? FMT_FLAG_FILTER | FMT_FLAG_MSAA : FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RG16_UNORM}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SR, Norm16FilterableSupported ? FMT_FLAG_FILTER | FMT_FLAG_MSAA : FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RGBA16_UNORM}, TextureFormatsTier1Supported ? BIND_SRU : BIND_SR, Norm16FilterableSupported ? FMT_FLAG_FILTER | FMT_FLAG_MSAA : FMT_FLAG_MSAA);
}

if (R16SnormSupported)
if (TextureFormatsTier1Supported)
{
SetTexFormatInfo({TEX_FORMAT_R16_SNORM}, BIND_SRU, Norm16FilterableSupported ? FMT_FLAG_FILTER | FMT_FLAG_MSAA : FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RG16_SNORM}, BIND_SRU, Norm16FilterableSupported ? FMT_FLAG_FILTER | FMT_FLAG_MSAA : FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_RGBA16_SNORM}, BIND_SRU, Norm16FilterableSupported ? FMT_FLAG_FILTER | FMT_FLAG_MSAA : FMT_FLAG_MSAA);
SetTexFormatInfo({TEX_FORMAT_R16_UNORM,
TEX_FORMAT_R16_SNORM,
TEX_FORMAT_RG16_UNORM,
TEX_FORMAT_RG16_SNORM,
TEX_FORMAT_RGBA16_UNORM,
TEX_FORMAT_RGBA16_SNORM},
BIND_SRU, FMT_FLAG_MSAA);

UpdateTexFormatInfo({TEX_FORMAT_R8_SNORM,
TEX_FORMAT_RG8_SNORM,
TEX_FORMAT_RGBA8_SNORM},
BIND_R, FMT_FLAG_MSAA);

UpdateTexFormatInfo({TEX_FORMAT_R8_TYPELESS,
TEX_FORMAT_R8_UNORM,
TEX_FORMAT_R8_SNORM,
TEX_FORMAT_R8_UINT,
TEX_FORMAT_R8_SINT,
TEX_FORMAT_RG8_TYPELESS,
TEX_FORMAT_RG8_UNORM,
TEX_FORMAT_RG8_SNORM,
TEX_FORMAT_RG8_UINT,
TEX_FORMAT_RG8_SINT,
TEX_FORMAT_R16_TYPELESS,
TEX_FORMAT_R16_UINT,
TEX_FORMAT_R16_SINT,
TEX_FORMAT_R16_FLOAT,
TEX_FORMAT_R16_UNORM,
TEX_FORMAT_RG16_TYPELESS,
TEX_FORMAT_RG16_UINT,
TEX_FORMAT_RG16_SINT,
TEX_FORMAT_RG16_FLOAT,
TEX_FORMAT_RG16_UNORM,
TEX_FORMAT_RGBA16_UNORM,
TEX_FORMAT_RGB10A2_TYPELESS,
TEX_FORMAT_RGB10A2_UNORM,
TEX_FORMAT_RGB10A2_UINT,
TEX_FORMAT_R11G11B10_FLOAT},
BIND_U, FMT_FLAG_NONE);
}

if (TextureCompressionETC2Supported)
Expand Down
14 changes: 10 additions & 4 deletions Graphics/GraphicsEngineWebGPU/src/UploadMemoryManagerWebGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ namespace Diligent

UploadMemoryManagerWebGPU::Page::Page(UploadMemoryManagerWebGPU& Mgr, size_t Size) :
m_pMgr{&Mgr},
m_Data(Size)
m_Data{std::make_unique_for_overwrite<Uint8[]>(Size)},
m_DataSize{Size}
{
WGPUBufferDescriptor wgpuBufferDesc{};
wgpuBufferDesc.label = GetWGPUStringView("Upload memory page");
Expand All @@ -58,10 +59,13 @@ UploadMemoryManagerWebGPU::Page::Page(Page&& RHS) noexcept :
m_pMgr{RHS.m_pMgr},
m_wgpuBuffer{std::move(RHS.m_wgpuBuffer)},
m_Data{std::move(RHS.m_Data)},
m_DataSize{RHS.m_DataSize},
m_CurrOffset{RHS.m_CurrOffset}
// clang-format on
{
RHS = Page{};
RHS.m_pMgr = nullptr;
RHS.m_DataSize = 0;
RHS.m_CurrOffset = 0;
}

UploadMemoryManagerWebGPU::Page& UploadMemoryManagerWebGPU::Page::operator=(Page&& RHS) noexcept
Expand All @@ -72,9 +76,11 @@ UploadMemoryManagerWebGPU::Page& UploadMemoryManagerWebGPU::Page::operator=(Page
m_pMgr = RHS.m_pMgr;
m_wgpuBuffer = std::move(RHS.m_wgpuBuffer);
m_Data = std::move(RHS.m_Data);
m_DataSize = RHS.m_DataSize;
m_CurrOffset = RHS.m_CurrOffset;

RHS.m_pMgr = nullptr;
RHS.m_DataSize = 0;
RHS.m_CurrOffset = 0;

return *this;
Expand All @@ -91,7 +97,7 @@ UploadMemoryManagerWebGPU::Allocation UploadMemoryManagerWebGPU::Page::Allocate(
Allocation Alloc;
Alloc.Offset = AlignUp(m_CurrOffset, Alignment);
Alloc.Size = AlignUp(Size, Alignment);
if (Alloc.Offset + Alloc.Size <= m_Data.size())
if (Alloc.Offset + Alloc.Size <= m_DataSize)
{
Alloc.wgpuBuffer = m_wgpuBuffer;
Alloc.pData = &m_Data[Alloc.Offset];
Expand All @@ -105,7 +111,7 @@ void UploadMemoryManagerWebGPU::Page::FlushWrites(WGPUQueue wgpuQueue)
{
if (m_CurrOffset > 0)
{
wgpuQueueWriteBuffer(wgpuQueue, m_wgpuBuffer, 0, m_Data.data(), m_CurrOffset);
wgpuQueueWriteBuffer(wgpuQueue, m_wgpuBuffer, 0, m_Data.get(), m_CurrOffset);
}
}

Expand Down
17 changes: 15 additions & 2 deletions Graphics/ShaderTools/src/SPIRVUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ std::vector<uint32_t> PatchImageFormats(const std::vector<uint32_t>&

diligent_spirv_cross::ShaderResources resources = Compiler.get_shader_resources();

constexpr size_t SPIRVHeaderSize = 5;
std::unordered_map<uint32_t, size_t> ImageTypeIdToFormat;
bool HasStorageImageExtendedFormatsCapability = false;
// Skip the header and walk instruction boundaries: operands may have the same
// low bits as OpTypeImage and must not be interpreted as instruction words.
constexpr size_t SPIRVHeaderSize = 5;
for (size_t i = SPIRVHeaderSize; i < SPIRV.size();)
{
const uint32_t WordCount = SPIRV[i] >> 16;
Expand All @@ -120,14 +121,20 @@ std::vector<uint32_t> PatchImageFormats(const std::vector<uint32_t>&
LOG_ERROR_MESSAGE("Invalid SPIR-V instruction size at word ", i);
return {};
}
// OpCapability
// 0 1
// | OpCode | Capability |
constexpr uint32_t CapabilityOffset = 1;

// OpTypeImage
// 0 1 2 3 4 5 6 7 8 9
// | OpCode | Result | Sampled Type | Dim | Depth | Arrayed | MS | Sampled | Image Format | Access Qualifier
constexpr uint32_t ImageFormatOffset = 8;

const uint32_t OpCode = SPIRV[i] & 0xFFFF;
if (OpCode == spv::OpTypeImage && WordCount > ImageFormatOffset)
if (OpCode == spv::OpCapability && WordCount > CapabilityOffset)
HasStorageImageExtendedFormatsCapability |= SPIRV[i + CapabilityOffset] == spv::CapabilityStorageImageExtendedFormats;
else if (OpCode == spv::OpTypeImage && WordCount > ImageFormatOffset)
{
const uint32_t ImageTypeId = SPIRV[i + 1];
ImageTypeIdToFormat[ImageTypeId] = i + ImageFormatOffset;
Expand Down Expand Up @@ -169,6 +176,12 @@ std::vector<uint32_t> PatchImageFormats(const std::vector<uint32_t>&
}
}

if (!HasStorageImageExtendedFormatsCapability)
{
constexpr uint32_t WordCount = 2;
PatchedSPIRV.insert(PatchedSPIRV.begin() + SPIRVHeaderSize, {(WordCount << 16) | spv::OpCapability, spv::CapabilityStorageImageExtendedFormats});
}

return PatchedSPIRV;
}

Expand Down
Loading
Loading