summaryrefslogtreecommitdiffstats
path: root/tools/gfx/vulkan/render-vk.cpp
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2021-03-11 13:08:21 -0800
committerGitHub <noreply@github.com>2021-03-11 13:08:21 -0800
commit4b74f994bf94217f174cf0fb02ed94abe62e9d7c (patch)
tree9c6f4c771d1e962059c84047ccf96e0d891a5c80 /tools/gfx/vulkan/render-vk.cpp
parenta07455c175db33d8d95bc5cd83738808d74cd105 (diff)
Change representation of initial data for textures (#1747)
* Change representation of initial data for textures Before this change, initial data for a texture has been provided with the `ITextureResource::Data` type, where a call to `IDevice::createTexture()` would take zero or one `Data` and, if present, use it to initialize all the subresources of a texture. The organization of `Data` was not actually quite how its own documentation comment described it (the implementations didn't agree with the comment), and while it aggressively factored out redundancies (e.g., only storing the stride for each mip level once, instead of once per subresource for large arrays), the result was that setting up a `Data` correcty was a bit confusing. This change makes the initial data for a texture using a `SubresourceData` type that is almost identical to what D3D11 uses, so that developers are more likely to be comfortable filling it in. All of the existing implementations were easily adapted to use the new type, so it seems like a net win. Note: Both Vulkan and D3D11 do away with the idea of initializing a texture with data as part of allocating it, and we might eventually want to do the same given the complexity that this system entails. The main reason to preserve this detail is for better compatibility with D3D11, where immutable textures/buffers need to have their data specified at creation time. It seems good to preserve the ability to have immutable resources on target APIs where this distinction could affect performance (e.g., immutable resources do not need state/transition tracking on APIs like D3D11). * fixup: CUDA
Diffstat (limited to 'tools/gfx/vulkan/render-vk.cpp')
-rw-r--r--tools/gfx/vulkan/render-vk.cpp33
1 files changed, 23 insertions, 10 deletions
diff --git a/tools/gfx/vulkan/render-vk.cpp b/tools/gfx/vulkan/render-vk.cpp
index ee494d4c8..1c263d505 100644
--- a/tools/gfx/vulkan/render-vk.cpp
+++ b/tools/gfx/vulkan/render-vk.cpp
@@ -65,7 +65,7 @@ public:
virtual SLANG_NO_THROW Result SLANG_MCALL createTextureResource(
IResource::Usage initialUsage,
const ITextureResource::Desc& desc,
- const ITextureResource::Data* initData,
+ const ITextureResource::SubresourceData* initData,
ITextureResource** outResource) override;
virtual SLANG_NO_THROW Result SLANG_MCALL createBufferResource(
IResource::Usage initialUsage,
@@ -3011,7 +3011,7 @@ size_t calcNumRows(Format format, int height)
return (size_t)height;
}
-Result VKDevice::createTextureResource(IResource::Usage initialUsage, const ITextureResource::Desc& descIn, const ITextureResource::Data* initData, ITextureResource** outResource)
+Result VKDevice::createTextureResource(IResource::Usage initialUsage, const ITextureResource::Desc& descIn, const ITextureResource::SubresourceData* initData, ITextureResource** outResource)
{
TextureResource::Desc desc(descIn);
desc.setDefaults(initialUsage);
@@ -3113,7 +3113,6 @@ Result VKDevice::createTextureResource(IResource::Usage initialUsage, const ITex
VkCommandBuffer commandBuffer = m_deviceQueue.getCommandBuffer();
const int numMipMaps = desc.numMipLevels;
- assert(initData->numMips == numMipMaps);
// Calculate how large the buffer has to be
size_t bufferSize = 0;
@@ -3140,35 +3139,49 @@ Result VKDevice::createTextureResource(IResource::Usage initialUsage, const ITex
// Copy into upload buffer
{
- int subResourceIndex = 0;
+ int subResourceCounter = 0;
uint8_t* dstData;
m_api.vkMapMemory(m_device, uploadBuffer.m_memory, 0, bufferSize, 0, (void**)&dstData);
+ size_t dstSubresourceOffset = 0;
for (int i = 0; i < arraySize; ++i)
{
for (Index j = 0; j < mipSizes.getCount(); ++j)
{
const auto& mipSize = mipSizes[j];
- const ptrdiff_t srcRowStride = initData->mipRowStrides[j];
+ int subResourceIndex = subResourceCounter++;
+ auto initSubresource = initData[subResourceIndex];
+
+ const ptrdiff_t srcRowStride = initSubresource.strideY;
+ const ptrdiff_t srcLayerStride = initSubresource.strideZ;
+
auto dstRowSizeInBytes = calcRowSize(desc.format, mipSize.width);
auto numRows = calcNumRows(desc.format, mipSize.height);
+ auto dstLayerSizeInBytes = dstRowSizeInBytes * numRows;
+
+ const uint8_t* srcLayer = (const uint8_t*) initSubresource.data;
+ uint8_t* dstLayer = dstData + dstSubresourceOffset;
for (int k = 0; k < mipSize.depth; k++)
{
- const uint8_t* srcData = (const uint8_t*)(initData->subResources[subResourceIndex]);
+ const uint8_t* srcRow = srcLayer;
+ uint8_t* dstRow = dstLayer;
for (uint32_t l = 0; l < numRows; l++)
{
- ::memcpy(dstData, srcData, dstRowSizeInBytes);
+ ::memcpy(dstRow, srcRow, dstRowSizeInBytes);
- dstData += dstRowSizeInBytes;
- srcData += srcRowStride;
+ dstRow += dstRowSizeInBytes;
+ srcRow += srcRowStride;
}
- subResourceIndex++;
+ dstLayer += dstLayerSizeInBytes;
+ srcLayer += srcLayerStride;
}
+
+ dstSubresourceOffset += dstLayerSizeInBytes * mipSize.depth;
}
}