Skip to content

Commit

Permalink
Made several internal RenderSystem functions static and refactored Co…
Browse files Browse the repository at this point in the history
…pyTextureImageData() interface.
  • Loading branch information
LukasBanana committed Jun 24, 2023
1 parent ebb355f commit ef32bae
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 75 deletions.
17 changes: 10 additions & 7 deletions include/LLGL/RenderSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -615,27 +615,30 @@ class LLGL_EXPORT RenderSystem : public Interface
//! Sets the rendering capabilities.
void SetRenderingCaps(const RenderingCapabilities& caps);

protected:

//! Validates the specified buffer descriptor to be used for buffer creation.
void AssertCreateBuffer(const BufferDescriptor& bufferDesc, std::uint64_t maxSize);
static void AssertCreateBuffer(const BufferDescriptor& bufferDesc, std::uint64_t maxSize);

//! Validates the specified arguments to be used for buffer array creation.
void AssertCreateBufferArray(std::uint32_t numBuffers, Buffer* const * bufferArray);
static void AssertCreateBufferArray(std::uint32_t numBuffers, Buffer* const * bufferArray);

//! Validates the specified shader descriptor.
void AssertCreateShader(const ShaderDescriptor& shaderDesc);
static void AssertCreateShader(const ShaderDescriptor& shaderDesc);

//! Validates the specified image data size against the required size (in bytes).
void AssertImageDataSize(std::size_t dataSize, std::size_t requiredDataSize, const char* useCase = nullptr);
static void AssertImageDataSize(std::size_t dataSize, std::size_t requiredDataSize, const char* useCase = nullptr);

/**
\brief Copies the specified source data (i.e. \c data) to the destination image.
\remarks This function also performs image conversion if there is a mismatch between source and destination format.
\see ConvertImageBuffer
*/
void CopyTextureImageData(
static void CopyTextureImageData(
const DstImageDescriptor& dstImageDesc,
const Extent3D& extent,
const Format format,
std::uint32_t numTexels,
std::uint32_t numTexelsInRow,
Format format,
const void* data,
std::size_t rowStride = 0
);
Expand Down
2 changes: 1 addition & 1 deletion sources/Renderer/DebugLayer/DbgRenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Buffer* DbgRenderSystem::CreateBuffer(const BufferDescriptor& bufferDesc, const

BufferArray* DbgRenderSystem::CreateBufferArray(std::uint32_t numBuffers, Buffer* const * bufferArray)
{
AssertCreateBufferArray(numBuffers, bufferArray);
RenderSystem::AssertCreateBufferArray(numBuffers, bufferArray);

/* Create temporary buffer array with buffer instances */
std::vector<Buffer*> bufferInstanceArray(numBuffers);
Expand Down
16 changes: 9 additions & 7 deletions sources/Renderer/Direct3D11/D3D11RenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void D3D11RenderSystem::Release(CommandBuffer& commandBuffer)

Buffer* D3D11RenderSystem::CreateBuffer(const BufferDescriptor& bufferDesc, const void* initialData)
{
AssertCreateBuffer(bufferDesc, UINT_MAX);
RenderSystem::AssertCreateBuffer(bufferDesc, UINT_MAX);
if (DXBindFlagsNeedBufferWithRV(bufferDesc.bindFlags))
return buffers_.emplace<D3D11BufferWithRV>(device_.Get(), bufferDesc, initialData);
else
Expand All @@ -138,7 +138,7 @@ Buffer* D3D11RenderSystem::CreateBuffer(const BufferDescriptor& bufferDesc, cons

BufferArray* D3D11RenderSystem::CreateBufferArray(std::uint32_t numBuffers, Buffer* const * bufferArray)
{
AssertCreateBufferArray(numBuffers, bufferArray);
RenderSystem::AssertCreateBufferArray(numBuffers, bufferArray);
return bufferArrays_.emplace<D3D11BufferArray>(numBuffers, bufferArray);
}

Expand Down Expand Up @@ -284,9 +284,11 @@ void D3D11RenderSystem::ReadTexture(Texture& texture, const TextureRegion& textu
textureD3D.CreateSubresourceCopyWithCPUAccess(device_.Get(), context_.Get(), texCopy, D3D11_CPU_ACCESS_READ, textureRegion);

/* Map subresource for reading */
DstImageDescriptor dstImageDesc = imageDesc;
const Format format = textureD3D.GetFormat();
const SubresourceLayout layoutPerLayer = CalcSubresourceLayout(format, textureRegion.extent);
DstImageDescriptor dstImageDesc = imageDesc;
const Format format = textureD3D.GetFormat();
const Extent3D extent = CalcTextureExtent(textureD3D.GetType(), textureRegion.extent);
const std::uint32_t numTexelsPerLayer = extent.width * extent.height * extent.depth;
const SubresourceLayout layoutPerLayer = CalcSubresourceLayout(format, textureRegion.extent);

for_range(arrayLayer, textureRegion.subresource.numArrayLayers)
{
Expand All @@ -297,7 +299,7 @@ void D3D11RenderSystem::ReadTexture(Texture& texture, const TextureRegion& textu
DXThrowIfFailed(hr, "failed to map D3D11 texture copy resource");

/* Copy host visible resource to CPU accessible resource */
CopyTextureImageData(dstImageDesc, textureRegion.extent, format, mappedSubresource.pData, mappedSubresource.RowPitch);
RenderSystem::CopyTextureImageData(dstImageDesc, numTexelsPerLayer, extent.width, format, mappedSubresource.pData, mappedSubresource.RowPitch);

/* Unmap resource */
context_->Unmap(texCopy.resource.Get(), subresource);
Expand Down Expand Up @@ -365,7 +367,7 @@ void D3D11RenderSystem::Release(RenderTarget& renderTarget)

Shader* D3D11RenderSystem::CreateShader(const ShaderDescriptor& shaderDesc)
{
AssertCreateShader(shaderDesc);
RenderSystem::AssertCreateShader(shaderDesc);
return shaders_.emplace<D3D11Shader>(device_.Get(), shaderDesc);
}

Expand Down
15 changes: 8 additions & 7 deletions sources/Renderer/Direct3D11/Texture/D3D11Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,17 @@ void D3D11Texture::CreateSubresourceCopyWithCPUAccess(
}

/* Copy subresource */
const UINT mipLevel = region.subresource.baseMipLevel;
const Offset3D offset = CalcTextureOffset(GetType(), region.offset);
const Extent3D extent = CalcTextureExtent(GetType(), region.extent);

const D3D11_BOX srcBox
{
static_cast<UINT>(region.offset.x),
static_cast<UINT>(region.offset.y),
static_cast<UINT>(region.offset.z),
static_cast<UINT>(region.offset.x) + region.extent.width,
static_cast<UINT>(region.offset.y) + region.extent.height,
static_cast<UINT>(region.offset.z) + region.extent.depth,
static_cast<UINT>(offset.x),
static_cast<UINT>(offset.y),
static_cast<UINT>(offset.z),
static_cast<UINT>(offset.x) + extent.width,
static_cast<UINT>(offset.y) + extent.height,
static_cast<UINT>(offset.z) + extent.depth,
};

for_range(arrayLayer, region.subresource.numArrayLayers)
Expand Down
15 changes: 8 additions & 7 deletions sources/Renderer/Direct3D12/D3D12RenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void D3D12RenderSystem::Release(CommandBuffer& commandBuffer)

Buffer* D3D12RenderSystem::CreateBuffer(const BufferDescriptor& bufferDesc, const void* initialData)
{
AssertCreateBuffer(bufferDesc, ULLONG_MAX);
RenderSystem::AssertCreateBuffer(bufferDesc, ULLONG_MAX);
D3D12Buffer* bufferD3D = buffers_.emplace<D3D12Buffer>(device_.GetNative(), bufferDesc);
if (initialData != nullptr)
UpdateBufferAndSync(*bufferD3D, 0, initialData, bufferDesc.size, bufferD3D->GetAlignment());
Expand All @@ -125,7 +125,7 @@ Buffer* D3D12RenderSystem::CreateBuffer(const BufferDescriptor& bufferDesc, cons

BufferArray* D3D12RenderSystem::CreateBufferArray(std::uint32_t numBuffers, Buffer* const * bufferArray)
{
AssertCreateBufferArray(numBuffers, bufferArray);
RenderSystem::AssertCreateBufferArray(numBuffers, bufferArray);
return bufferArrays_.emplace<D3D12BufferArray>(numBuffers, bufferArray);
}

Expand Down Expand Up @@ -226,9 +226,10 @@ void D3D12RenderSystem::ReadTexture(Texture& texture, const TextureRegion& textu
}

/* Map readback buffer to CPU memory space */
DstImageDescriptor dstImageDesc = imageDesc;
const Format format = textureD3D.GetFormat();
const Extent3D extent = CalcTextureExtent(textureD3D.GetType(), textureRegion.extent);
DstImageDescriptor dstImageDesc = imageDesc;
const Format format = textureD3D.GetFormat();
const Extent3D extent = CalcTextureExtent(textureD3D.GetType(), textureRegion.extent);
const std::uint32_t numTexelsPerLayer = extent.width * extent.height * extent.depth;

void* mappedData = nullptr;
auto hr = readbackBuffer->Map(0, nullptr, &mappedData);
Expand All @@ -239,7 +240,7 @@ void D3D12RenderSystem::ReadTexture(Texture& texture, const TextureRegion& textu
for_range(arrayLayer, textureRegion.subresource.numArrayLayers)
{
/* Copy CPU accessible buffer to output data */
CopyTextureImageData(dstImageDesc, extent, format, srcData, rowStride);
RenderSystem::CopyTextureImageData(dstImageDesc, numTexelsPerLayer, extent.width, format, srcData, rowStride);

/* Move destination image pointer to next layer */
dstImageDesc.data = reinterpret_cast<char*>(dstImageDesc.data) + layerSize;
Expand Down Expand Up @@ -313,7 +314,7 @@ void D3D12RenderSystem::Release(RenderTarget& renderTarget)

Shader* D3D12RenderSystem::CreateShader(const ShaderDescriptor& shaderDesc)
{
AssertCreateShader(shaderDesc);
RenderSystem::AssertCreateShader(shaderDesc);
return shaders_.emplace<D3D12Shader>(shaderDesc);
}

Expand Down
5 changes: 3 additions & 2 deletions sources/Renderer/Metal/MTRenderSystem.mm
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@

Buffer* MTRenderSystem::CreateBuffer(const BufferDescriptor& bufferDesc, const void* initialData)
{
RenderSystem::AssertCreateBuffer(bufferDesc, device_.maxBufferLength);
return buffers_.emplace<MTBuffer>(device_, bufferDesc, initialData);
}

BufferArray* MTRenderSystem::CreateBufferArray(std::uint32_t numBuffers, Buffer* const * bufferArray)
{
AssertCreateBufferArray(numBuffers, bufferArray);
RenderSystem::AssertCreateBufferArray(numBuffers, bufferArray);
return bufferArrays_.emplace<MTBufferArray>(numBuffers, bufferArray);
}

Expand Down Expand Up @@ -237,7 +238,7 @@

Shader* MTRenderSystem::CreateShader(const ShaderDescriptor& shaderDesc)
{
AssertCreateShader(shaderDesc);
RenderSystem::AssertCreateShader(shaderDesc);
return shaders_.emplace<MTShader>(device_, shaderDesc);
}

Expand Down
5 changes: 2 additions & 3 deletions sources/Renderer/Null/NullRenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,13 @@ void NullRenderSystem::Release(CommandBuffer& commandBuffer)

Buffer* NullRenderSystem::CreateBuffer(const BufferDescriptor& bufferDesc, const void* initialData)
{
if (bufferDesc.size > GetRenderingCaps().limits.maxBufferSize)
throw std::invalid_argument("");
RenderSystem::AssertCreateBuffer(bufferDesc, GetRenderingCaps().limits.maxBufferSize);
return buffers_.emplace<NullBuffer>(bufferDesc, initialData);
}

BufferArray* NullRenderSystem::CreateBufferArray(std::uint32_t numBuffers, Buffer* const * bufferArray)
{
AssertCreateBufferArray(numBuffers, bufferArray);
RenderSystem::AssertCreateBufferArray(numBuffers, bufferArray);
return bufferArrays_.emplace<NullBufferArray>(numBuffers, bufferArray);
}

Expand Down
6 changes: 3 additions & 3 deletions sources/Renderer/OpenGL/GLRenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static void GLBufferStorage(GLBuffer& bufferGL, const BufferDescriptor& bufferDe

Buffer* GLRenderSystem::CreateBuffer(const BufferDescriptor& bufferDesc, const void* initialData)
{
AssertCreateBuffer(bufferDesc, static_cast<std::uint64_t>(std::numeric_limits<GLsizeiptr>::max()));
RenderSystem::AssertCreateBuffer(bufferDesc, static_cast<std::uint64_t>(std::numeric_limits<GLsizeiptr>::max()));

auto bufferGL = CreateGLBuffer(bufferDesc, initialData);

Expand Down Expand Up @@ -205,7 +205,7 @@ static bool IsBufferArrayWithVertexBufferBinding(std::uint32_t numBuffers, Buffe

BufferArray* GLRenderSystem::CreateBufferArray(std::uint32_t numBuffers, Buffer* const * bufferArray)
{
AssertCreateBufferArray(numBuffers, bufferArray);
RenderSystem::AssertCreateBufferArray(numBuffers, bufferArray);

/* Create vertex buffer array and build VAO if there is at least one buffer with VertexBuffer binding */
if (IsBufferArrayWithVertexBufferBinding(numBuffers, bufferArray))
Expand Down Expand Up @@ -422,7 +422,7 @@ void GLRenderSystem::Release(RenderTarget& renderTarget)

Shader* GLRenderSystem::CreateShader(const ShaderDescriptor& shaderDesc)
{
AssertCreateShader(shaderDesc);
RenderSystem::AssertCreateShader(shaderDesc);

/* Validate rendering capabilities for required shader type */
switch (shaderDesc.type)
Expand Down
20 changes: 10 additions & 10 deletions sources/Renderer/RenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,26 +431,26 @@ static void CopyRowAlignedData(void* dstData, const void* srcData, std::size_t d

void RenderSystem::CopyTextureImageData(
const DstImageDescriptor& dstImageDesc,
const Extent3D& extent,
const Format format,
std::uint32_t numTexels,
std::uint32_t numTexelsInRow,
Format format,
const void* data,
std::size_t rowStride)
{
/* Check if image buffer must be converted */
const auto numTexels = (extent.width * extent.height * extent.depth);
const auto& srcTexFormat = GetFormatAttribs(format);
const auto srcFormatSize = DataTypeSize(srcTexFormat.dataType) * ImageFormatSize(srcTexFormat.format);
const auto srcImageSize = (numTexels * srcFormatSize);
const auto dstPitch = (extent.width * srcFormatSize);
const auto dstStride = (numTexelsInRow * srcFormatSize);

if (srcTexFormat.format != dstImageDesc.format || srcTexFormat.dataType != dstImageDesc.dataType)
{
/* Check if padding must be removed */
ByteBuffer unpaddedData;
if (rowStride != 0 && dstPitch != rowStride)
if (rowStride != 0 && dstStride != rowStride)
{
unpaddedData = AllocateByteBuffer(srcImageSize, UninitializeTag{});
CopyRowAlignedData(unpaddedData.get(), data, srcImageSize, dstPitch, rowStride);
CopyRowAlignedData(unpaddedData.get(), data, srcImageSize, dstStride, rowStride);
data = unpaddedData.get();
}

Expand All @@ -459,7 +459,7 @@ void RenderSystem::CopyTextureImageData(
const auto dstImageSize = (numTexels * dstFormatSize);

/* Validate input size */
AssertImageDataSize(dstImageDesc.dataSize, dstImageSize);
RenderSystem::AssertImageDataSize(dstImageDesc.dataSize, dstImageSize);

/* Convert mapped data into requested format */
ByteBuffer formattedData = ConvertImageBuffer(
Expand All @@ -481,11 +481,11 @@ void RenderSystem::CopyTextureImageData(
else
{
/* Validate input size */
AssertImageDataSize(dstImageDesc.dataSize, srcImageSize);
RenderSystem::AssertImageDataSize(dstImageDesc.dataSize, srcImageSize);

/* Copy mapped data directly into the output buffer */
if (rowStride != 0 && dstPitch != rowStride)
CopyRowAlignedData(dstImageDesc.data, data, srcImageSize, dstPitch, rowStride);
if (rowStride != 0 && dstStride != rowStride)
CopyRowAlignedData(dstImageDesc.data, data, srcImageSize, dstStride, rowStride);
else
::memcpy(dstImageDesc.data, data, srcImageSize);
}
Expand Down
20 changes: 10 additions & 10 deletions sources/Renderer/Vulkan/VKRenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static VkBufferUsageFlags GetStagingVkBufferUsageFlags(long cpuAccessFlags)

Buffer* VKRenderSystem::CreateBuffer(const BufferDescriptor& bufferDesc, const void* initialData)
{
AssertCreateBuffer(bufferDesc, static_cast<uint64_t>(std::numeric_limits<VkDeviceSize>::max()));
RenderSystem::AssertCreateBuffer(bufferDesc, static_cast<uint64_t>(std::numeric_limits<VkDeviceSize>::max()));

/* Create staging buffer */
VkBufferCreateInfo stagingCreateInfo;
Expand Down Expand Up @@ -146,7 +146,7 @@ Buffer* VKRenderSystem::CreateBuffer(const BufferDescriptor& bufferDesc, const v

BufferArray* VKRenderSystem::CreateBufferArray(std::uint32_t numBuffers, Buffer* const * bufferArray)
{
AssertCreateBufferArray(numBuffers, bufferArray);
RenderSystem::AssertCreateBufferArray(numBuffers, bufferArray);
return bufferArrays_.emplace<VKBufferArray>(numBuffers, bufferArray);
}

Expand Down Expand Up @@ -278,7 +278,7 @@ Texture* VKRenderSystem::CreateTexture(const TextureDescriptor& textureDesc, con
then use temporary image buffer as source for initial data
*/
const auto srcImageDataSize = imageSize * ImageFormatSize(imageDesc->format) * DataTypeSize(imageDesc->dataType);
AssertImageDataSize(imageDesc->dataSize, static_cast<std::size_t>(srcImageDataSize));
RenderSystem::AssertImageDataSize(imageDesc->dataSize, static_cast<std::size_t>(srcImageDataSize));
initialData = intermediateData.get();
}
else
Expand All @@ -287,7 +287,7 @@ Texture* VKRenderSystem::CreateTexture(const TextureDescriptor& textureDesc, con
Validate that image data is large enough,
then use input data as source for initial data
*/
AssertImageDataSize(imageDesc->dataSize, static_cast<std::size_t>(initialDataSize));
RenderSystem::AssertImageDataSize(imageDesc->dataSize, static_cast<std::size_t>(initialDataSize));
initialData = imageDesc->data;
}
}
Expand Down Expand Up @@ -415,7 +415,7 @@ void VKRenderSystem::WriteTexture(Texture& texture, const TextureRegion& texture
then use temporary image buffer as source for initial data
*/
const auto srcImageDataSize = imageSize * ImageFormatSize(imageDesc.format) * DataTypeSize(imageDesc.dataType);
AssertImageDataSize(imageDesc.dataSize, static_cast<std::size_t>(srcImageDataSize));
RenderSystem::AssertImageDataSize(imageDesc.dataSize, static_cast<std::size_t>(srcImageDataSize));
imageData = intermediateData.get();
}
else
Expand All @@ -424,7 +424,7 @@ void VKRenderSystem::WriteTexture(Texture& texture, const TextureRegion& texture
Validate that image data is large enough,
then use input data as source for initial data
*/
AssertImageDataSize(imageDesc.dataSize, static_cast<std::size_t>(imageDataSize));
RenderSystem::AssertImageDataSize(imageDesc.dataSize, static_cast<std::size_t>(imageDataSize));
imageData = imageDesc.data;
}

Expand Down Expand Up @@ -480,8 +480,8 @@ void VKRenderSystem::ReadTexture(Texture& texture, const TextureRegion& textureR
auto& textureVK = LLGL_CAST(VKTexture&, texture);

/* Determine size of image for staging buffer */
const Offset3D& offset = textureRegion.offset;
const Extent3D& extent = textureRegion.extent;
const Offset3D offset = CalcTextureOffset(textureVK.GetType(), textureRegion.offset);
const Extent3D extent = CalcTextureExtent(textureVK.GetType(), textureRegion.extent);
const TextureSubresource& subresource = textureRegion.subresource;
const Format format = VKTypes::Unmap(textureVK.GetVkFormat());

Expand Down Expand Up @@ -535,7 +535,7 @@ void VKRenderSystem::ReadTexture(Texture& texture, const TextureRegion& textureR
if (auto memory = deviceMemory->Map(device_, region->GetOffset(), imageDataSize))
{
/* Copy data to buffer object */
CopyTextureImageData(imageDesc, Extent3D{ extent.width, extent.height, extent.depth * subresource.numArrayLayers }, format, memory);
RenderSystem::CopyTextureImageData(imageDesc, imageSize, extent.width, format, memory);
deviceMemory->Unmap(device_);
}
}
Expand Down Expand Up @@ -602,7 +602,7 @@ void VKRenderSystem::Release(RenderTarget& renderTarget)

Shader* VKRenderSystem::CreateShader(const ShaderDescriptor& shaderDesc)
{
AssertCreateShader(shaderDesc);
RenderSystem::AssertCreateShader(shaderDesc);
return shaders_.emplace<VKShader>(device_, shaderDesc);
}

Expand Down
Loading

0 comments on commit ef32bae

Please sign in to comment.