Skip to content

Commit

Permalink
v1.138:
Browse files Browse the repository at this point in the history
HIGHLIGHTS:
- improved memory allocation API (more friendly for render graph based systems)

BREAKING CHANGES:
- "GetAccelerationStructureMemoryInfo" renamed to "GetAccelerationStructureMemoryDesc"
- "GetBufferMemoryInfo(const Buffer& buffer, ...)" changed to "GetBufferMemoryDesc(const Device& device, const BufferDesc& bufferDesc, ...)"
- "GetTextureMemoryInfo(const Texture& texture, ...)" changed to "GetTextureMemoryDesc(const Device& device, const TextureDesc& textureDesc, ...)"
- no-brain migration: "GetXxxMemoryInfo(resource, ...)" => "GetXxxMemoryDesc(device, NRI.GetXxxDesc(resource), ..."

DETAILS:
- NRI: "GetXxxMemoryInfo" renamed to "GetXxxMemoryDesc"
- NRI: use "BufferDesc" and "TextureDesc" to get "MemoryDesc", avoiding beforehand resource creation
- NRI: exposed "DeviceDesc::memoryTier", which provides a hint about which resource categories can be put in one memory (valid for D3D12, just a hint for VK)
- VK: old-school "vkGetXxxMemoryRequirements2" replaced with modern "vkGetDeviceXxxMemoryRequirements" from "VK_KHR_maintenance4"
  • Loading branch information
dzhdanNV committed Jul 22, 2024
1 parent 5967fc3 commit 6b9ecf8
Show file tree
Hide file tree
Showing 57 changed files with 385 additions and 369 deletions.
2 changes: 1 addition & 1 deletion Include/Extensions/NRIRayTracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ NRI_STRUCT(DispatchRaysIndirectDesc)
NRI_STRUCT(RayTracingInterface)
{
// Get
void (NRI_CALL *GetAccelerationStructureMemoryInfo)(const NRI_NAME_REF(AccelerationStructure) accelerationStructure, NRI_NAME_REF(MemoryDesc) memoryDesc);
void (NRI_CALL *GetAccelerationStructureMemoryDesc)(const NRI_NAME_REF(AccelerationStructure) accelerationStructure, NRI_NAME_REF(MemoryDesc) memoryDesc);
uint64_t (NRI_CALL *GetAccelerationStructureUpdateScratchBufferSize)(const NRI_NAME_REF(AccelerationStructure) accelerationStructure);
uint64_t (NRI_CALL *GetAccelerationStructureBuildScratchBufferSize)(const NRI_NAME_REF(AccelerationStructure) accelerationStructure);
uint64_t (NRI_CALL *GetAccelerationStructureHandle)(const NRI_NAME_REF(AccelerationStructure) accelerationStructure);
Expand Down
8 changes: 4 additions & 4 deletions Include/NRI.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Non-goals:
#include <stddef.h>

#define NRI_VERSION_MAJOR 1
#define NRI_VERSION_MINOR 137
#define NRI_VERSION_DATE "21 July 2024"
#define NRI_VERSION_MINOR 138
#define NRI_VERSION_DATE "22 July 2024"

#ifdef _WIN32
#define NRI_CALL __fastcall
Expand Down Expand Up @@ -56,8 +56,8 @@ NRI_STRUCT(CoreInterface)
const NRI_NAME_REF(TextureDesc) (NRI_CALL *GetTextureDesc)(const NRI_NAME_REF(Texture) texture);
NRI_NAME(FormatSupportBits) (NRI_CALL *GetFormatSupport)(const NRI_NAME_REF(Device) device, NRI_NAME(Format) format);
uint32_t (NRI_CALL *GetQuerySize)(const NRI_NAME_REF(QueryPool) queryPool);
void (NRI_CALL *GetBufferMemoryInfo)(const NRI_NAME_REF(Buffer) buffer, NRI_NAME(MemoryLocation) memoryLocation, NRI_NAME_REF(MemoryDesc) memoryDesc);
void (NRI_CALL *GetTextureMemoryInfo)(const NRI_NAME_REF(Texture) texture, NRI_NAME(MemoryLocation) memoryLocation, NRI_NAME_REF(MemoryDesc) memoryDesc);
void (NRI_CALL *GetBufferMemoryDesc)(const NRI_NAME_REF(Device) device, const NRI_NAME_REF(BufferDesc) bufferDesc, NRI_NAME(MemoryLocation) memoryLocation, NRI_NAME_REF(MemoryDesc) memoryDesc);
void (NRI_CALL *GetTextureMemoryDesc)(const NRI_NAME_REF(Device) device, const NRI_NAME_REF(TextureDesc) textureDesc, NRI_NAME(MemoryLocation) memoryLocation, NRI_NAME_REF(MemoryDesc) memoryDesc);
NRI_NAME(Result) (NRI_CALL *GetCommandQueue)(NRI_NAME_REF(Device) device, NRI_NAME(CommandQueueType) commandQueueType, NRI_NAME_REF(CommandQueue*) commandQueue);

// Create
Expand Down
58 changes: 35 additions & 23 deletions Include/NRIDescs.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,6 @@ NRI_ENUM
MAX_NUM
);

NRI_ENUM
(
Vendor, uint8_t,

UNKNOWN,
NVIDIA,
AMD,
INTEL,

MAX_NUM
);

NRI_ENUM
(
GraphicsAPI, uint8_t,

D3D11,
D3D12,
VULKAN,

MAX_NUM
);

// left -> right : low -> high bits
// Expected (but not guaranteed) "FormatSupportBits" are provided, but "GetFormatSupport" should be used for querying real HW support
// To demote sRGB use the previous format, i.e. "format - 1"
Expand Down Expand Up @@ -1404,6 +1381,40 @@ NRI_STRUCT(PipelineStatisticsDesc)
//===============================================================================================================================
#pragma region [ Device desc ]

NRI_ENUM
(
GraphicsAPI, uint8_t,

D3D11,
D3D12,
VULKAN,

MAX_NUM
);

NRI_ENUM
(
Vendor, uint8_t,

UNKNOWN,
NVIDIA,
AMD,
INTEL,

MAX_NUM
);

// Provided for convinience, when a memory chunk needs to be allocated in advance and it's unclear which resource categories can be placed together
NRI_ENUM
(
MemoryTier, uint8_t,

ONE, // A memory can only support resources from a single resource category (buffers, color and depth-stencil attachments, all other textures)
TWO, // A memory can support resources from all 3 categories

MAX_NUM
);

NRI_STRUCT(AdapterDesc)
{
char description[128];
Expand Down Expand Up @@ -1464,6 +1475,7 @@ NRI_STRUCT(DeviceDesc)
uint32_t bufferTextureGranularity;
uint64_t bufferMaxSize;
uint32_t pushConstantsMaxSize;
NRI_NAME(MemoryTier) memoryTier;

// Shader resources
uint32_t boundDescriptorSetMaxNum;
Expand Down
2 changes: 1 addition & 1 deletion Resources/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define STR(x) STR_HELPER(x)

#define VERSION_MAJOR 1
#define VERSION_MINOR 137
#define VERSION_MINOR 138
#define VERSION_BUILD 0
#define VERSION_REVISION 0

Expand Down
17 changes: 0 additions & 17 deletions Source/D3D11/BufferD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,23 +234,6 @@ TextureD3D11& BufferD3D11::RecreateReadbackTexture(const TextureD3D11& srcTextur
// NRI
//================================================================================================================

inline void BufferD3D11::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const {
const bool isConstantBuffer = (m_Desc.usageMask & BufferUsageBits::CONSTANT_BUFFER) == (uint32_t)BufferUsageBits::CONSTANT_BUFFER;

uint32_t alignment = 65536;
if (isConstantBuffer)
alignment = 256;
else if (m_Desc.size <= 4096)
alignment = 4096;

uint64_t size = Align(m_Desc.size, alignment);

memoryDesc.type = (MemoryType)memoryLocation;
memoryDesc.size = size;
memoryDesc.alignment = alignment;
memoryDesc.mustBeDedicated = false;
}

inline void* BufferD3D11::Map(uint64_t offset, uint64_t size) {
MaybeUnused(size);

Expand Down
1 change: 0 additions & 1 deletion Source/D3D11/BufferD3D11.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ struct BufferD3D11 {
SET_D3D_DEBUG_OBJECT_NAME(m_Buffer, name);
}

void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const;
void* Map(uint64_t offset, uint64_t size);
void Unmap();

Expand Down
4 changes: 0 additions & 4 deletions Source/D3D11/BufferD3D11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ static uint64_t NRI_CALL GetBufferNativeObject(const Buffer& buffer) {
return uint64_t((ID3D11Buffer*)((BufferD3D11&)buffer));
}

static void NRI_CALL GetBufferMemoryInfo(const Buffer& buffer, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) {
((BufferD3D11&)buffer).GetMemoryInfo(memoryLocation, memoryDesc);
}

static void* NRI_CALL MapBuffer(Buffer& buffer, uint64_t offset, uint64_t size) {
return ((BufferD3D11&)buffer).Map(offset, size);
}
Expand Down
34 changes: 34 additions & 0 deletions Source/D3D11/DeviceD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ void DeviceD3D11::FillDesc() {
m_Desc.bufferTextureGranularity = 1;
m_Desc.bufferMaxSize = D3D11_REQ_RESOURCE_SIZE_IN_MEGABYTES_EXPRESSION_C_TERM * 1024ull * 1024ull;
m_Desc.pushConstantsMaxSize = D3D11_REQ_IMMEDIATE_CONSTANT_BUFFER_ELEMENT_COUNT * 16;
m_Desc.memoryTier = MemoryTier::ONE;

m_Desc.boundDescriptorSetMaxNum = D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT;
m_Desc.perStageDescriptorSamplerMaxNum = D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT;
Expand Down Expand Up @@ -446,6 +447,39 @@ Result DeviceD3D11::CreateImplementation(Interface*& entity, const Args&... args
return result;
}

void DeviceD3D11::GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const {
const bool isConstantBuffer = (bufferDesc.usageMask & BufferUsageBits::CONSTANT_BUFFER) == (uint32_t)BufferUsageBits::CONSTANT_BUFFER;

uint32_t alignment = 65536;
if (isConstantBuffer)
alignment = 256;
else if (bufferDesc.size <= 4096)
alignment = 4096;

memoryDesc.type = (MemoryType)memoryLocation;
memoryDesc.size = Align(bufferDesc.size, alignment);
memoryDesc.alignment = alignment;
memoryDesc.mustBeDedicated = false;
}

void DeviceD3D11::GetMemoryDesc(const TextureDesc& textureDesc,MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const {
bool isMultisampled = textureDesc.sampleNum > 1;
uint32_t size = TextureD3D11::GetMipmappedSize(textureDesc);

uint32_t alignment = 65536;
if (isMultisampled)
alignment = 4194304;
else if (size <= 65536)
alignment = 65536;

size = Align(size, alignment);

memoryDesc.type = (MemoryType)memoryLocation;
memoryDesc.size = size;
memoryDesc.alignment = alignment;
memoryDesc.mustBeDedicated = false;
}

//================================================================================================================
// DeviceBase
//================================================================================================================
Expand Down
6 changes: 4 additions & 2 deletions Source/D3D11/DeviceD3D11.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ struct DeviceD3D11 final : public DeviceBase {
return m_IsDeferredContextEmulated;
}

Result Create(const DeviceCreationDesc& deviceCreationDesc, ID3D11Device* precreatedDevice, AGSContext* agsContext, bool isNVAPILoadedInApp);

inline void EnterCriticalSection() {
if (m_Multithread)
m_Multithread->Enter();
Expand All @@ -65,6 +63,10 @@ struct DeviceD3D11 final : public DeviceBase {
::LeaveCriticalSection(&m_CriticalSection);
}

Result Create(const DeviceCreationDesc& deviceCreationDesc, ID3D11Device* precreatedDevice, AGSContext* agsContext, bool isNVAPILoadedInApp);
void GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const;
void GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const;

//================================================================================================================
// NRI
//================================================================================================================
Expand Down
18 changes: 14 additions & 4 deletions Source/D3D11/DeviceD3D11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ static const TextureDesc& NRI_CALL GetTextureDesc(const Texture& texture) {
return ((const TextureD3D11&)texture).GetDesc();
}

static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) {
return ((const DeviceD3D11&)device).GetFormatSupport(format);
}

static void NRI_CALL GetBufferMemoryDesc(const Device& device, const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) {
((const DeviceD3D11&)device).GetMemoryDesc(bufferDesc, memoryLocation, memoryDesc);
}

static void NRI_CALL GetTextureMemoryDesc(const Device& device, const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) {
((const DeviceD3D11&)device).GetMemoryDesc(textureDesc, memoryLocation, memoryDesc);
}

static Result NRI_CALL GetCommandQueue(Device& device, CommandQueueType commandQueueType, CommandQueue*& commandQueue) {
return ((DeviceD3D11&)device).GetCommandQueue(commandQueueType, commandQueue);
}
Expand Down Expand Up @@ -173,10 +185,6 @@ static void NRI_CALL FreeMemory(Memory& memory) {
device.FreeMemory(memory);
}

static FormatSupportBits NRI_CALL GetFormatSupport(const Device& device, Format format) {
return ((const DeviceD3D11&)device).GetFormatSupport(format);
}

static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) {
((DeviceD3D11&)device).SetDebugName(name);
}
Expand Down Expand Up @@ -208,6 +216,8 @@ Result DeviceD3D11::FillFunctionTable(CoreInterface& coreInterface) const {
coreInterface.GetBufferDesc = ::GetBufferDesc;
coreInterface.GetTextureDesc = ::GetTextureDesc;
coreInterface.GetFormatSupport = ::GetFormatSupport;
coreInterface.GetBufferMemoryDesc = ::GetBufferMemoryDesc;
coreInterface.GetTextureMemoryDesc = ::GetTextureMemoryDesc;
coreInterface.GetCommandQueue = ::GetCommandQueue;
coreInterface.CreateCommandAllocator = ::CreateCommandAllocator;
coreInterface.CreateDescriptorPool = ::CreateDescriptorPool;
Expand Down
50 changes: 10 additions & 40 deletions Source/D3D11/TextureD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Result TextureD3D11::Create(const MemoryD3D11* memory) {

RETURN_ON_BAD_HRESULT(&m_Device, hr, "ID3D11Device::CreateTextureXx()");

uint64_t size = GetMipmappedSize();
uint64_t size = GetMipmappedSize(m_Desc);
uint32_t priority = memory ? memory->GetResidencyPriority(size) : 0;
if (priority != 0)
m_Texture->SetEvictionPriority(priority);
Expand All @@ -94,22 +94,13 @@ Result TextureD3D11::Create(const TextureD3D11Desc& textureDesc) {
return Result::SUCCESS;
}

uint32_t TextureD3D11::GetMipmappedSize(uint32_t w, uint32_t h, uint32_t d, Mip_t mipNum, Mip_t mipOffset) const {
if (!mipNum)
mipNum = m_Desc.mipNum;

bool isCompressed = m_Desc.format >= Format::BC1_RGBA_UNORM && m_Desc.format <= Format::BC7_RGBA_SRGB;
bool isCustom = w || h || d;

if (!w)
w = GetSize(0, mipOffset);

if (!h)
h = GetSize(1, mipOffset);

if (!d)
d = GetSize(2, mipOffset);
uint32_t TextureD3D11::GetMipmappedSize(const TextureDesc& textureDesc) {
bool isCompressed = textureDesc.format >= Format::BC1_RGBA_UNORM && textureDesc.format <= Format::BC7_RGBA_SRGB;

uint32_t w = GetDimension(GraphicsAPI::D3D11, textureDesc, 0, 0);
uint32_t h = GetDimension(GraphicsAPI::D3D11, textureDesc, 1, 0);
uint32_t d = GetDimension(GraphicsAPI::D3D11, textureDesc, 2, 0);
uint32_t mipNum = textureDesc.mipNum;
uint32_t size = 0;

while (mipNum) {
Expand All @@ -133,13 +124,10 @@ uint32_t TextureD3D11::GetMipmappedSize(uint32_t w, uint32_t h, uint32_t d, Mip_
mipNum--;
}

const FormatProps& formatProps = GetFormatProps(m_Desc.format);
const FormatProps& formatProps = GetFormatProps(textureDesc.format);
size *= formatProps.stride;

if (!isCustom) {
size *= m_Desc.sampleNum;
size *= m_Desc.arraySize;
}
size *= std::max(textureDesc.sampleNum, (Sample_t)1);
size *= std::max(textureDesc.arraySize, (Dim_t)1);

return size;
}
Expand All @@ -148,22 +136,4 @@ uint32_t TextureD3D11::GetMipmappedSize(uint32_t w, uint32_t h, uint32_t d, Mip_
// NRI
//================================================================================================================

inline void TextureD3D11::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const {
bool isMultisampled = m_Desc.sampleNum > 1;
uint32_t size = GetMipmappedSize();

uint32_t alignment = 65536;
if (isMultisampled)
alignment = 4194304;
else if (size <= 65536)
alignment = 65536;

size = Align(size, alignment);

memoryDesc.type = (MemoryType)memoryLocation;
memoryDesc.size = size;
memoryDesc.alignment = alignment;
memoryDesc.mustBeDedicated = false;
}

#include "TextureD3D11.hpp"
5 changes: 2 additions & 3 deletions Source/D3D11/TextureD3D11.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ struct TextureD3D11 {

Result Create(const MemoryD3D11* memory);
Result Create(const TextureD3D11Desc& textureDesc);
uint32_t GetMipmappedSize(uint32_t w = 0, uint32_t h = 0, uint32_t d = 0, Mip_t mipNum = 0, Mip_t mipOffset = 0) const;

static uint32_t GetMipmappedSize(const TextureDesc& textureDesc);

//================================================================================================================
// NRI
Expand All @@ -61,8 +62,6 @@ struct TextureD3D11 {
SET_D3D_DEBUG_OBJECT_NAME(m_Texture, name);
}

void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const;

private:
DeviceD3D11& m_Device;
ComPtr<ID3D11Resource> m_Texture;
Expand Down
4 changes: 0 additions & 4 deletions Source/D3D11/TextureD3D11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ static uint64_t NRI_CALL GetTextureNativeObject(const Texture& texture) {
return uint64_t((ID3D11Resource*)((TextureD3D11&)texture));
}

static void NRI_CALL GetTextureMemoryInfo(const Texture& texture, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) {
((TextureD3D11&)texture).GetMemoryInfo(memoryLocation, memoryDesc);
}

#pragma endregion

Define_Core_Texture_PartiallyFillFunctionTable(D3D11);
4 changes: 2 additions & 2 deletions Source/D3D12/AccelerationStructureD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ Result AccelerationStructureD3D12::Create(const AccelerationStructureDesc& accel
return result;
}

void AccelerationStructureD3D12::GetMemoryInfo(MemoryDesc& memoryDesc) const {
m_Device.GetMemoryInfoForAccelerationStructure(m_PrebuildInfo.ResultDataMaxSizeInBytes, memoryDesc);
void AccelerationStructureD3D12::GetMemoryDesc(MemoryDesc& memoryDesc) const {
m_Device.GetAccelerationStructureMemoryDesc(m_PrebuildInfo.ResultDataMaxSizeInBytes, memoryDesc);
}

uint64_t AccelerationStructureD3D12::GetUpdateScratchBufferSize() const {
Expand Down
2 changes: 1 addition & 1 deletion Source/D3D12/AccelerationStructureD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct AccelerationStructureD3D12 {

Result Create(const AccelerationStructureDesc& accelerationStructureDesc);
Result Create(const AccelerationStructureD3D12Desc& accelerationStructureDesc);
void GetMemoryInfo(MemoryDesc& memoryDesc) const;
void GetMemoryDesc(MemoryDesc& memoryDesc) const;
uint64_t GetUpdateScratchBufferSize() const;
uint64_t GetBuildScratchBufferSize() const;
Result BindMemory(Memory* memory, uint64_t offset);
Expand Down
Loading

0 comments on commit 6b9ecf8

Please sign in to comment.