Skip to content

Commit

Permalink
Fixed bugs identified by Xcode static code analysis.
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasBanana committed Aug 17, 2023
1 parent 6bf9a92 commit e2fa19e
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 39 deletions.
4 changes: 2 additions & 2 deletions sources/Renderer/DebugLayer/DbgRenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,8 +869,8 @@ static std::string BindingSlotToString(const BindingSlot& slot)

void DbgRenderSystem::ValidateBufferView(DbgBuffer& bufferDbg, const BufferViewDescriptor& viewDesc, const BindingDescriptor& bindingDesc)
{
const auto minAlignment = GetMinAlignmentForBufferBinding(bindingDesc, limits_);
if (viewDesc.offset % minAlignment != 0 || viewDesc.size % minAlignment != 0)
const std::uint64_t minAlignment = GetMinAlignmentForBufferBinding(bindingDesc, limits_);
if (minAlignment > 0 && (viewDesc.offset % minAlignment != 0 || viewDesc.size % minAlignment != 0))
{
LLGL_DBG_ERROR(
ErrorType::InvalidArgument,
Expand Down
5 changes: 3 additions & 2 deletions sources/Renderer/Metal/Command/MTCommandExecutor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@

/* Source and target texture formats must match for 'copyFromTexture', so create texture view on mismatch */
id<MTLTexture> sourceTexture;
if ([drawableTexture pixelFormat] != [cmd->destinationTexture pixelFormat])
const bool isTextureViewRequired = ([drawableTexture pixelFormat] != [cmd->destinationTexture pixelFormat]);
if (isTextureViewRequired)
sourceTexture = [drawableTexture newTextureViewWithPixelFormat:[cmd->destinationTexture pixelFormat]];
else
sourceTexture = drawableTexture;
Expand All @@ -136,7 +137,7 @@
];

/* Decrement reference counter for temporary texture */
if (sourceTexture != drawableTexture)
if (isTextureViewRequired)
[sourceTexture release];
return sizeof(*cmd);
}
Expand Down
5 changes: 3 additions & 2 deletions sources/Renderer/Metal/Command/MTDirectCommandBuffer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@

/* Source and target texture formats must match for 'copyFromTexture', so create texture view on mismatch */
id<MTLTexture> sourceTexture;
if ([drawableTexture pixelFormat] != [targetTexture pixelFormat])
const bool isTextureViewRequired = ([drawableTexture pixelFormat] != [targetTexture pixelFormat]);
if (isTextureViewRequired)
sourceTexture = [drawableTexture newTextureViewWithPixelFormat:[targetTexture pixelFormat]];
else
sourceTexture = drawableTexture;
Expand Down Expand Up @@ -389,7 +390,7 @@
context_.ResumeRenderEncoder();

/* Decrement reference counter for temporary texture */
if (sourceTexture != drawableTexture)
if (isTextureViewRequired)
[sourceTexture release];
}

Expand Down
1 change: 0 additions & 1 deletion sources/Renderer/Metal/RenderState/MTConstantsCache.mm
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@
{
NSString* uniformName = [NSString stringWithCString:uniformDesc.name.c_str() encoding:NSASCIIStringEncoding];
AppendUniformByName(reflection, uniformDesc, uniformIndex, uniformName, shaderBuffers);
[uniformName release];
}


Expand Down
3 changes: 0 additions & 3 deletions sources/Renderer/Metal/Shader/MTShader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,6 @@ static MTLLanguageVersion GetMTLLanguageVersion(const ShaderDescriptor& desc)
error: &error
];

if (source != nullptr)
[source release];

[dispatchData release];

/* Load shader function with entry point */
Expand Down
13 changes: 8 additions & 5 deletions sources/Renderer/Metal/Texture/MTRenderTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import <Metal/Metal.h>

#include <LLGL/RenderTarget.h>
#include <LLGL/Container/SmallVector.h>
#include "../RenderState/MTRenderPass.h"


Expand Down Expand Up @@ -67,14 +68,16 @@ class MTRenderTarget final : public RenderTarget
);

id<MTLTexture> CreateAttachmentTexture(id<MTLDevice> device, MTLPixelFormat pixelFormat);
id<MTLTexture> CreateAttachmentTextureView(id<MTLTexture> sourceTexture, MTLPixelFormat pixelFormat);

private:

Extent2D resolution_;
MTLRenderPassDescriptor* nativeRenderPass_ = nullptr; // Cannot be id<>
MTLRenderPassDescriptor* nativeMutableRenderPass_ = nullptr; // Cannot be id<>
std::uint32_t numColorAttachments_ = 0;
MTRenderPass renderPass_;
Extent2D resolution_;
MTLRenderPassDescriptor* nativeRenderPass_ = nullptr; // Cannot be id<>
MTLRenderPassDescriptor* nativeMutableRenderPass_ = nullptr; // Cannot be id<>
std::uint32_t numColorAttachments_ = 0;
MTRenderPass renderPass_;
SmallVector<id<MTLTexture>, 2> internalTextures_; // List of internally created MTLTexture views

};

Expand Down
38 changes: 18 additions & 20 deletions sources/Renderer/Metal/Texture/MTRenderTarget.mm
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,9 @@ static void CopyMTLAttachmentDesc(MTLRenderPassAttachmentDescriptor* dst, const

MTRenderTarget::~MTRenderTarget()
{
#if 0//TODO: code analysis warns about invalid reference counting
/* Release all textures */
for_range(i, numColorAttachments_)
[nativeRenderPass_.colorAttachments[i] release];
if (auto attachment = nativeRenderPass_.depthAttachment)
[attachment.texture release];
if (auto attachment = nativeRenderPass_.stencilAttachment)
[attachment.texture release];

//[nativeRenderPass_ release];
#endif
/* Release all internally created textures */
for (id<MTLTexture> tex : internalTextures_)
[tex release];
}

Extent2D MTRenderTarget::GetResolution() const
Expand Down Expand Up @@ -188,12 +180,11 @@ static void CopyMTLAttachmentDesc(MTLRenderPassAttachmentDescriptor* dst, const
{
/* Create texture view with format */
const MTLPixelFormat pixelFormat = MTTypes::ToMTLPixelFormat(inAttachment.format);
outAttachment.texture = [tex newTextureViewWithPixelFormat:pixelFormat];
outAttachment.texture = CreateAttachmentTextureView(tex, pixelFormat);
}
else
{
/* Increment reference counter of texture and use texture directly */
[tex retain];
/* Use input texture directly */
outAttachment.texture = tex;
}
}
Expand All @@ -214,12 +205,11 @@ static void CopyMTLAttachmentDesc(MTLRenderPassAttachmentDescriptor* dst, const
{
/* Create texture view with format */
const MTLPixelFormat pixelFormat = MTTypes::ToMTLPixelFormat(inAttachment.format);
outAttachment.resolveTexture = [tex newTextureViewWithPixelFormat:pixelFormat];
outAttachment.resolveTexture = CreateAttachmentTextureView(tex, pixelFormat);
}
else
{
/* Increment reference counter of texture and use texture directly */
[tex retain];
/* Use input texture directly */
outAttachment.resolveTexture = tex;
}

Expand Down Expand Up @@ -257,12 +247,20 @@ static void CopyMTLAttachmentDesc(MTLRenderPassAttachmentDescriptor* dst, const

id<MTLTexture> MTRenderTarget::CreateAttachmentTexture(id<MTLDevice> device, MTLPixelFormat pixelFormat)
{
auto texDesc = CreateTextureDesc(device, pixelFormat, renderPass_.GetSampleCount());
MTLTextureDescriptor* texDesc = CreateTextureDesc(device, pixelFormat, renderPass_.GetSampleCount());

id<MTLTexture> texture = [device newTextureWithDescriptor:texDesc];
id<MTLTexture> newTexture = [device newTextureWithDescriptor:texDesc];
internalTextures_.push_back(newTexture);
[texDesc release];

return texture;
return newTexture;
}

id<MTLTexture> MTRenderTarget::CreateAttachmentTextureView(id<MTLTexture> sourceTexture, MTLPixelFormat pixelFormat)
{
id<MTLTexture> newTextureView = [sourceTexture newTextureViewWithPixelFormat:pixelFormat];
internalTextures_.push_back(newTextureView);
return newTextureView;
}


Expand Down
2 changes: 0 additions & 2 deletions sources/Renderer/Metal/Texture/MTTexture.mm
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,6 @@ static void ConvertTextureDesc(id<MTLDevice> device, MTLTextureDescriptor* dst,
/* Copy bytes from intermediate shared buffer into output CPU buffer */
::memcpy(imageDesc.data, intermediateBuffer.GetBytes(), imageDesc.dataSize);
}

[cmdBuffer release];
}


Expand Down
5 changes: 3 additions & 2 deletions sources/Renderer/OpenGL/Shader/GLPipelineSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ static GLuint SortShaderArray(std::size_t numShaders, const Shader* const* shade
const GLShader* shadersOrderedByType[numShaderTypes] = {};
for_range(i, numShaders)
{
const auto shaderType = shaders[i]->GetType();
const auto shaderIndex = static_cast<int>(shaderType);
LLGL_ASSERT_PTR(shaders[i]);
const ShaderType shaderType = shaders[i]->GetType();
const int shaderIndex = static_cast<int>(shaderType);
LLGL_ASSERT(shadersOrderedByType[shaderIndex] == nullptr, "duplicate definitions of %s shader in one pipeline", ToString(shaderType));
shadersOrderedByType[shaderIndex] = LLGL_CAST(const GLShader*, shaders[i]);
}
Expand Down

0 comments on commit e2fa19e

Please sign in to comment.