Skip to content

Commit

Permalink
refactor render test to use latest slang-rhi (#5119)
Browse files Browse the repository at this point in the history
* refactor render test to use latest slang-rhi

* update slang-rhi

* update slang-rhi

* update slang-rhi

* update slang-rhi
  • Loading branch information
skallweitNV authored Sep 19, 2024
1 parent fe71eaf commit 3861be7
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 377 deletions.
2 changes: 1 addition & 1 deletion external/slang-rhi
Submodule slang-rhi updated 312 files
8 changes: 4 additions & 4 deletions tools/render-test/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static rhi::DeviceType _toRenderType(Slang::RenderApiType apiType)
case RenderApiType::CPU: return rhi::DeviceType::CPU;
case RenderApiType::CUDA: return rhi::DeviceType::CUDA;
default:
return rhi::DeviceType::Unknown;
return rhi::DeviceType::Default;
}
}

Expand Down Expand Up @@ -244,7 +244,7 @@ static rhi::DeviceType _toRenderType(Slang::RenderApiType apiType)
UnownedStringSlice argName = argSlice.tail(1);
DeviceType deviceType = _toRenderType(RenderApiUtil::findApiTypeByName(argName));

if (deviceType != DeviceType::Unknown)
if (deviceType != DeviceType::Default)
{
outOptions.deviceType = deviceType;
continue;
Expand All @@ -253,7 +253,7 @@ static rhi::DeviceType _toRenderType(Slang::RenderApiType apiType)
// Lookup the target language type
DeviceType targetLanguageDeviceType = _toRenderType(RenderApiUtil::findImplicitLanguageRenderApiType(argName));

if (targetLanguageDeviceType != DeviceType::Unknown || argName == "glsl")
if (targetLanguageDeviceType != DeviceType::Default || argName == "glsl")
{
outOptions.targetLanguageDeviceType = targetLanguageDeviceType;
outOptions.inputLanguageID = (argName == "hlsl" || argName == "glsl" || argName == "cpp" || argName == "cxx" || argName == "c") ? InputLanguageID::Native : InputLanguageID::Slang;
Expand All @@ -266,7 +266,7 @@ static rhi::DeviceType _toRenderType(Slang::RenderApiType apiType)
}

// If a render option isn't set use defaultRenderType
outOptions.deviceType = (outOptions.deviceType == DeviceType::Unknown)
outOptions.deviceType = (outOptions.deviceType == DeviceType::Default)
? outOptions.targetLanguageDeviceType
: outOptions.deviceType;

Expand Down
4 changes: 2 additions & 2 deletions tools/render-test/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ struct Options
ShaderProgramType shaderType = ShaderProgramType::Graphics;

/// The renderer type inferred from the target language type. Used if a rendererType is not explicitly set.
DeviceType targetLanguageDeviceType = DeviceType::Unknown;
DeviceType targetLanguageDeviceType = DeviceType::Default;
/// The set render type
DeviceType deviceType = DeviceType::Unknown;
DeviceType deviceType = DeviceType::Default;
InputLanguageID inputLanguageID = InputLanguageID::Slang;
SlangSourceLanguage sourceLanguage = SLANG_SOURCE_LANGUAGE_UNKNOWN;

Expand Down
351 changes: 137 additions & 214 deletions tools/render-test/render-test-main.cpp

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions tools/render-test/shader-input-layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ namespace renderer_test

Format _getFormatFromName(const UnownedStringSlice& slice)
{
#define SLANG_FORMAT_CASE(name, blockSizeInBytes, pixelsPerBlock) if (slice == #name) return Format::name; else

SLANG_RHI_FORMAT(SLANG_FORMAT_CASE)
for (int i = 0; i < int(Format::_Count); ++i)
{
FormatInfo info;
rhiGetFormatInfo(Format(i), &info);
if (slice == info.name)
{
return Format(i);
}
}
return Format::Unknown;
}

Expand Down
184 changes: 72 additions & 112 deletions tools/render-test/shader-renderer-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,26 @@ inline int calcMipSize(int size, int level)
return size > 0 ? size : 1;
}

inline ITextureResource::Extents calcMipSize(ITextureResource::Extents size, int mipLevel)
inline Extents calcMipSize(Extents size, int mipLevel)
{
ITextureResource::Extents rs;
Extents rs;
rs.width = calcMipSize(size.width, mipLevel);
rs.height = calcMipSize(size.height, mipLevel);
rs.depth = calcMipSize(size.depth, mipLevel);
return rs;
}

/// Calculate the effective array size - in essence the amount if mip map sets needed.
/// In practice takes into account if the arraySize is 0 (it's not an array, but it will still have
/// at least one mip set) and if the type is a cubemap (multiplies the amount of mip sets by 6)
inline int calcEffectiveArraySize(const ITextureResource::Desc& desc)
{
const int arrSize = (desc.arraySize > 0) ? desc.arraySize : 1;

switch (desc.type)
{
case IResource::Type::Texture1D: // fallthru
case IResource::Type::Texture2D:
{
return arrSize;
}
case IResource::Type::TextureCube:
return arrSize * 6;
case IResource::Type::Texture3D:
return 1;
default:
return 0;
}
}

/// Given the type works out the maximum dimension size
inline int calcMaxDimension(ITextureResource::Extents size, IResource::Type type)
inline int calcMaxDimension(Extents size, TextureType type)
{
switch (type)
{
case IResource::Type::Texture1D:
case TextureType::Texture1D:
return size.width;
case IResource::Type::Texture3D:
case TextureType::Texture3D:
return Math::Max(Math::Max(size.width, size.height), size.depth);
case IResource::Type::TextureCube: // fallthru
case IResource::Type::Texture2D:
case TextureType::TextureCube: // fallthru
case TextureType::Texture2D:
{
return Math::Max(size.width, size.height);
}
Expand All @@ -65,115 +42,103 @@ inline int calcMaxDimension(ITextureResource::Extents size, IResource::Type type
}

/// Given the type, calculates the number of mip maps. 0 on error
inline int calcNumMipLevels(IResource::Type type, ITextureResource::Extents size)
inline int calcNumMipLevels(TextureType type, Extents size)
{
const int maxDimensionSize = calcMaxDimension(size, type);
return (maxDimensionSize > 0) ? (Math::Log2Floor(maxDimensionSize) + 1) : 0;
}

/// Calculate the total number of sub resources. 0 on error.
inline int calcNumSubResources(const ITextureResource::Desc& desc)
{
const int numMipMaps =
(desc.numMipLevels > 0) ? desc.numMipLevels : calcNumMipLevels(desc.type, desc.size);
const int arrSize = (desc.arraySize > 0) ? desc.arraySize : 1;

switch (desc.type)
{
case IResource::Type::Texture1D:
case IResource::Type::Texture2D:
case IResource::Type::Texture3D:
{
return numMipMaps * arrSize;
}
case IResource::Type::TextureCube:
{
// There are 6 faces to a cubemap
return numMipMaps * arrSize * 6;
}
default:
return 0;
}
}

/* static */ Result ShaderRendererUtil::generateTextureResource(
/* static */ Result ShaderRendererUtil::generateTexture(
const InputTextureDesc& inputDesc,
ResourceState defaultState,
IDevice* device,
ComPtr<ITextureResource>& textureOut)
ComPtr<ITexture>& textureOut)
{
TextureData texData;
generateTextureData(texData, inputDesc);
return createTextureResource(inputDesc, texData, defaultState, device, textureOut);
return createTexture(inputDesc, texData, defaultState, device, textureOut);
}

/* static */ Result ShaderRendererUtil::createTextureResource(
/* static */ Result ShaderRendererUtil::createTexture(
const InputTextureDesc& inputDesc,
const TextureData& texData,
ResourceState defaultState,
IDevice* device,
ComPtr<ITextureResource>& textureOut)
ComPtr<ITexture>& textureOut)
{
ITextureResource::Desc textureResourceDesc = {};
TextureDesc textureDesc = {};

// Default to R8G8B8A8_UNORM
const Format format = (inputDesc.format == Format::Unknown) ? Format::R8G8B8A8_UNORM : inputDesc.format;

textureResourceDesc.sampleDesc = ITextureResource::SampleDesc{inputDesc.sampleCount, 0};
textureResourceDesc.format = format;
textureResourceDesc.numMipLevels = texData.m_mipLevels;
textureResourceDesc.arraySize = inputDesc.arrayLength;
textureResourceDesc.allowedStates =
ResourceStateSet(defaultState, ResourceState::CopyDestination, ResourceState::CopySource);
textureResourceDesc.defaultState = defaultState;
textureDesc.sampleCount = inputDesc.sampleCount;
textureDesc.format = format;
textureDesc.numMipLevels = texData.m_mipLevels;
textureDesc.arrayLength = inputDesc.arrayLength > 0 ? inputDesc.arrayLength : 1;
textureDesc.usage = TextureUsage::CopyDestination | TextureUsage::CopySource;
switch (defaultState)
{
case ResourceState::ShaderResource:
textureDesc.usage |= TextureUsage::ShaderResource;
break;
case ResourceState::UnorderedAccess:
textureDesc.usage |= TextureUsage::UnorderedAccess;
break;
default:
return SLANG_FAIL;
}
textureDesc.defaultState = defaultState;

// It's the same size in all dimensions
switch (inputDesc.dimension)
{
case 1:
{
textureResourceDesc.type = IResource::Type::Texture1D;
textureResourceDesc.size.width = inputDesc.size;
textureResourceDesc.size.height = 1;
textureResourceDesc.size.depth = 1;
textureDesc.type = TextureType::Texture1D;
textureDesc.size.width = inputDesc.size;
textureDesc.size.height = 1;
textureDesc.size.depth = 1;

break;
}
case 2:
{
textureResourceDesc.type = inputDesc.isCube ? IResource::Type::TextureCube : IResource::Type::Texture2D;
textureResourceDesc.size.width = inputDesc.size;
textureResourceDesc.size.height = inputDesc.size;
textureResourceDesc.size.depth = 1;
textureDesc.type = inputDesc.isCube ? TextureType::TextureCube : TextureType::Texture2D;
textureDesc.size.width = inputDesc.size;
textureDesc.size.height = inputDesc.size;
textureDesc.size.depth = 1;
break;
}
case 3:
{
textureResourceDesc.type = IResource::Type::Texture3D;
textureResourceDesc.size.width = inputDesc.size;
textureResourceDesc.size.height = inputDesc.size;
textureResourceDesc.size.depth = inputDesc.size;
textureDesc.type = TextureType::Texture3D;
textureDesc.size.width = inputDesc.size;
textureDesc.size.height = inputDesc.size;
textureDesc.size.depth = inputDesc.size;
break;
}
}

const int effectiveArraySize = calcEffectiveArraySize(textureResourceDesc);
const int numSubResources = calcNumSubResources(textureResourceDesc);
if (textureDesc.numMipLevels == 0)
{
textureDesc.numMipLevels = calcNumMipLevels(textureDesc.type, textureDesc.size);
}

List<ITextureResource::SubresourceData> initSubresources;
List<SubresourceData> initSubresources;
int arrayLayerCount = textureDesc.arrayLength * (textureDesc.type == TextureType::TextureCube ? 6 : 1);
int subResourceCounter = 0;
for( int a = 0; a < effectiveArraySize; ++a )
for( int a = 0; a < arrayLayerCount; ++a )
{
for( int m = 0; m < textureResourceDesc.numMipLevels; ++m )
for( int m = 0; m < textureDesc.numMipLevels; ++m )
{
int subResourceIndex = subResourceCounter++;
const int mipWidth = calcMipSize(textureResourceDesc.size.width, m);
const int mipHeight = calcMipSize(textureResourceDesc.size.height, m);
const int mipWidth = calcMipSize(textureDesc.size.width, m);
const int mipHeight = calcMipSize(textureDesc.size.height, m);

auto strideY = mipWidth * sizeof(uint32_t);
auto strideZ = mipHeight * strideY;

ITextureResource::SubresourceData subresourceData;
SubresourceData subresourceData;
subresourceData.data = texData.m_slices[subResourceIndex].values;
subresourceData.strideY = strideY;
subresourceData.strideZ = strideZ;
Expand All @@ -182,31 +147,26 @@ inline int calcNumSubResources(const ITextureResource::Desc& desc)
}
}

textureOut = device->createTextureResource(textureResourceDesc, initSubresources.getBuffer());
textureOut = device->createTexture(textureDesc, initSubresources.getBuffer());

return textureOut ? SLANG_OK : SLANG_FAIL;
}

/* static */ Result ShaderRendererUtil::createBufferResource(
/* static */ Result ShaderRendererUtil::createBuffer(
const InputBufferDesc& inputDesc,
size_t bufferSize,
const void* initData,
IDevice* device,
Slang::ComPtr<IBufferResource>& bufferOut)
Slang::ComPtr<IBuffer>& bufferOut)
{
IBufferResource::Desc srcDesc;
srcDesc.type = IResource::Type::Buffer;
srcDesc.sizeInBytes = bufferSize;
srcDesc.format = inputDesc.format;
srcDesc.elementSize = inputDesc.stride;
srcDesc.defaultState = ResourceState::UnorderedAccess;
srcDesc.allowedStates = ResourceStateSet(
ResourceState::CopyDestination,
ResourceState::CopySource,
ResourceState::UnorderedAccess,
ResourceState::ShaderResource);

ComPtr<IBufferResource> bufferResource = device->createBufferResource(srcDesc, initData);
BufferDesc bufferDesc;
bufferDesc.size = bufferSize;
bufferDesc.format = inputDesc.format;
bufferDesc.elementSize = inputDesc.stride;
bufferDesc.usage = BufferUsage::CopyDestination | BufferUsage::CopySource | BufferUsage::ShaderResource | BufferUsage::UnorderedAccess;
bufferDesc.defaultState = ResourceState::UnorderedAccess;

ComPtr<IBuffer> bufferResource = device->createBuffer(bufferDesc, initData);
if (!bufferResource)
{
return SLANG_FAIL;
Expand All @@ -216,21 +176,21 @@ inline int calcNumSubResources(const ITextureResource::Desc& desc)
return SLANG_OK;
}

static ISamplerState::Desc _calcSamplerDesc(const InputSamplerDesc& srcDesc)
static SamplerDesc _calcSamplerDesc(const InputSamplerDesc& srcDesc)
{
ISamplerState::Desc dstDesc;
SamplerDesc samplerDesc;
if (srcDesc.isCompareSampler)
{
dstDesc.reductionOp = TextureReductionOp::Comparison;
dstDesc.comparisonFunc = ComparisonFunc::Less;
samplerDesc.reductionOp = TextureReductionOp::Comparison;
samplerDesc.comparisonFunc = ComparisonFunc::Less;
}
return dstDesc;
return samplerDesc;
}

ComPtr<ISamplerState> _createSamplerState(IDevice* device,
ComPtr<ISampler> _createSampler(IDevice* device,
const InputSamplerDesc& srcDesc)
{
return device->createSamplerState(_calcSamplerDesc(srcDesc));
return device->createSampler(_calcSamplerDesc(srcDesc));
}

} // renderer_test
Loading

0 comments on commit 3861be7

Please sign in to comment.