Skip to content

Commit

Permalink
[Misc] Removed further 'throw'-statements to include exception handli…
Browse files Browse the repository at this point in the history
…ng only when enabled.
  • Loading branch information
LukasBanana committed Sep 22, 2024
1 parent 3be9ddc commit 8fb5e46
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 64 deletions.
2 changes: 1 addition & 1 deletion include/LLGL/LLGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* Overview
* --------
*
* - **Version**: 0.03 Beta
* - **Version**: 0.04 Beta
* - **License**: [3-Clause BSD License](https://github.com/LukasBanana/LLGL/blob/master/LICENSE.txt)
*
*
Expand Down
5 changes: 2 additions & 3 deletions include/LLGL/RenderSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ class LLGL_EXPORT RenderSystem : public Interface
}
LLGL::RenderSystemPtr myRenderSystem = LLGL::RenderSystem::Load(myRendererDesc);
\endcode
\throws std::runtime_error If loading the render system from the specified module failed.
\see RenderSystemDescriptor::moduleName
*/
static RenderSystemPtr Load(const RenderSystemDescriptor& renderSystemDesc, Report* report = nullptr);
Expand Down Expand Up @@ -411,7 +410,7 @@ class LLGL_EXPORT RenderSystem : public Interface

/**
\brief Creates a new Sampler object.
\throws std::runtime_error If the renderer does not support Sampler objects (e.g. if OpenGL 3.1 or lower is used).
\remarks Samplers (aka. sampler states) define how to sample texture resources in shaders.
\see GetRenderingCaps
*/
virtual Sampler* CreateSampler(const SamplerDescriptor& samplerDesc) = 0;
Expand Down Expand Up @@ -475,7 +474,7 @@ class LLGL_EXPORT RenderSystem : public Interface

/**
\brief Creates a new RenderTarget object.
\throws std::runtime_error If the renderer does not support RenderTarget objects (e.g. if OpenGL 2.1 or lower is used).
\remarks Use render targets to render into a texture instead of a swap-chain (i.e. the screen).
*/
virtual RenderTarget* CreateRenderTarget(const RenderTargetDescriptor& renderTargetDesc) = 0;

Expand Down
22 changes: 18 additions & 4 deletions sources/Core/Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
#include <stdarg.h>
#include <stdlib.h>

#include <LLGL/Platform/Platform.h>
#ifdef LLGL_OS_ANDROID
# include <android/log.h>
#endif


namespace LLGL
{
Expand Down Expand Up @@ -43,7 +48,7 @@ LLGL_EXPORT void Trap(const char* origin, const char* format, ...)
/* Throw exception with report and optional origin */
throw std::runtime_error(report);

#else
#else // LLGL_ENABLE_EXCEPTIONS

# ifdef LLGL_DEBUG

Expand All @@ -55,17 +60,26 @@ LLGL_EXPORT void Trap(const char* origin, const char* format, ...)
/* Break execution if there's a debugger attached */
LLGL_DEBUG_BREAK();

# else
# else // LLGL_DEBUG

# ifdef LLGL_OS_ANDROID

/* Print report to Android specific error log */
(void)__android_log_print(ANDROID_LOG_ERROR, "LLGL", "%s\n", report.c_str());

# else

/* Print report to standard error output */
::fprintf(stderr, "%s\n", report.c_str());

# endif
# endif

# endif // /LLGL_DEBUG

/* Abort execution as LLGL is trapped in an unrecoverable state */
::abort();

#endif
#endif // /LLGL_ENABLE_EXCEPTIONS
}

[[noreturn]]
Expand Down
20 changes: 10 additions & 10 deletions sources/Core/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,43 @@ class Report;
[[noreturn]]
LLGL_EXPORT void Trap(const char* origin, const char* format, ...);

// Throws an std::runtime_error exception with the message, that the specified assertion that failed.
// Traps program execution with the message that the specified assertion that failed.
[[noreturn]]
LLGL_EXPORT void TrapAssertionFailed(const char* origin, const char* expr, const char* details = nullptr, ...);

// Throws an std::runtime_error exception with the message, that the specified feature is not supported.
// Traps program execution with the message that the specified feature is not supported.
[[noreturn]]
LLGL_EXPORT void TrapFeatureNotSupported(const char* origin, const char* featureName);

// Throws an std::runtime_error exception with the message, that the specified rendering feature is not supported by the renderer (see RenderingFeatures).
// Traps program execution with the message that the specified rendering feature is not supported by the renderer (see RenderingFeatures).
[[noreturn]]
LLGL_EXPORT void TrapRenderingFeatureNotSupported(const char* origin, const char* featureName);

// Throws an std::runtime_error exception with the message, that the specified OpenGL extension is not supported.
// Traps program execution with the message that the specified OpenGL extension is not supported.
[[noreturn]]
LLGL_EXPORT void TrapGLExtensionNotSupported(const char* origin, const char* extensionName, const char* useCase = nullptr);

// Throws an std::runtime_error exception with the message, that the specified Vulkan extension is not supported.
// Traps program execution with the message that the specified Vulkan extension is not supported.
[[noreturn]]
LLGL_EXPORT void TrapVKExtensionNotSupported(const char* origin, const char* extensionName, const char* useCase = nullptr);

// Throws an std::runtime_error exception with the message, that the specified interface function is not implemented yet.
// Traps program execution with the message that the specified interface function is not implemented yet.
[[noreturn]]
LLGL_EXPORT void TrapNotImplemented(const char* origin, const char* useCase = nullptr);

// Throws an std::runtime_error exception with the message, that a null pointer was passed.
// Traps program execution with the message that a null pointer was passed.
[[noreturn]]
LLGL_EXPORT void TrapNullPointer(const char* origin, const char* expr);

// Throws an std::runtime_error exception with the message, that a value has exceeded an upper bound, i.e. <value> is not in the half-open range [0, upperBound).
// Traps program execution with the message that a value has exceeded an upper bound, i.e. <value> is not in the half-open range [0, upperBound).
[[noreturn]]
LLGL_EXPORT void TrapParamExceededUpperBound(const char* origin, const char* paramName, int value, int upperBound);

// Throws an std::runtime_error exception with the message, that a value has exceeded its maximum, i.e. <value> is not in the closed range [0, maximum].
// Traps program execution with the message that a value has exceeded its maximum, i.e. <value> is not in the closed range [0, maximum].
[[noreturn]]
LLGL_EXPORT void TrapParamExceededMaximum(const char* origin, const char* paramName, int value, int maximum);

// THrows an std::runtime_error exception with the message from the specified report, cutting off any trailing new-line characters.
// Traps program execution with the message from the specified report, cutting off any trailing new-line characters.
[[noreturn]]
LLGL_EXPORT void TrapReport(const char* origin, const Report& report);

Expand Down
37 changes: 31 additions & 6 deletions sources/Renderer/CheckedCast.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,27 @@

#include "../Platform/Debug.h"

#ifdef LLGL_ENABLE_CHECKED_CAST
# include <typeinfo>
#if LLGL_ENABLE_CHECKED_CAST
# if LLGL_ENABLE_EXCEPTIONS
# include <typeinfo>
# else
# include <LLGL/TypeInfo.h>
# include "../Core/Assertion.h"
# endif
#endif


namespace LLGL
{


#ifdef LLGL_ENABLE_CHECKED_CAST
#if LLGL_ENABLE_CHECKED_CAST

template <typename TDst, typename TSrc>
inline TDst& ObjectCast(TSrc& obj)
{
#if LLGL_ENABLE_EXCEPTIONS

try
{
return dynamic_cast<TDst&>(obj);
Expand All @@ -34,13 +41,22 @@ inline TDst& ObjectCast(TSrc& obj)
LLGL_DEBUG_BREAK();
throw;
}

#else // LLGL_ENABLE_EXCEPTIONS

return dynamic_cast<TDst&>(obj);

#endif
}

template <typename TDst, typename TSrc>
inline TDst ObjectCast(TSrc* obj)
{
if (obj == nullptr)
return nullptr;

#if LLGL_ENABLE_EXCEPTIONS

try
{
TDst objInstance = dynamic_cast<TDst>(obj);
Expand All @@ -53,19 +69,28 @@ inline TDst ObjectCast(TSrc* obj)
LLGL_DEBUG_BREAK();
throw;
}

#else // LLGL_ENABLE_EXCEPTIONS

TDst objInstance = dynamic_cast<TDst>(obj);
LLGL_ASSERT(objInstance != nullptr);
return objInstance;

#endif // /LLGL_ENABLE_EXCEPTIONS
}

#else
#else // LLGL_ENABLE_CHECKED_CAST

template <typename TDst, typename TSrc>
inline TDst ObjectCast(TSrc&& obj)
{
return static_cast<TDst>(obj);
}

#endif
#endif // /LLGL_ENABLE_CHECKED_CAST

#define LLGL_CAST(TYPE, OBJ) ObjectCast<TYPE>(OBJ)
#define LLGL_CAST(TYPE, OBJ) \
ObjectCast<TYPE>(OBJ)


} // /namespace LLGL
Expand Down
6 changes: 3 additions & 3 deletions sources/Renderer/Direct3D11/RenderState/D3D11ResourceHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "../../BufferUtils.h"
#include "../../StaticAssertions.h"
#include "../../../Core/CoreUtils.h"
#include "../../../Core/Assertion.h"
#include <LLGL/ResourceHeapFlags.h>
#include <LLGL/Utils/ForRange.h>
#include <algorithm>
Expand Down Expand Up @@ -118,7 +119,7 @@ D3D11ResourceHeap::D3D11ResourceHeap(
/* Get pipeline layout object */
auto* pipelineLayoutD3D = LLGL_CAST(D3D11PipelineLayout*, desc.pipelineLayout);
if (!pipelineLayoutD3D)
throw std::invalid_argument("failed to create resource heap due to missing pipeline layout");
LLGL_TRAP("failed to create resource heap due to missing pipeline layout");

/* Get and validate number of bindings and resource views */
const auto& bindings = pipelineLayoutD3D->GetHeapBindings();
Expand Down Expand Up @@ -1058,8 +1059,7 @@ static UINT GetFormatBufferStride(const Format format)
/* Get buffer stride by format */
const FormatAttributes& formatAttribs = GetFormatAttribs(format);
const UINT stride = (formatAttribs.bitSize / formatAttribs.blockWidth / formatAttribs.blockHeight / 8);
if (stride == 0)
throw std::runtime_error("cannot create buffer subresource with format stride of 0");
LLGL_ASSERT(stride > 0, "cannot create buffer subresource with format stride of 0");
return stride;
}

Expand Down
19 changes: 3 additions & 16 deletions sources/Renderer/Direct3D11/Shader/D3D11Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
#include "../../../Core/CoreUtils.h"
#include "../../../Core/StringUtils.h"
#include "../../../Core/ReportUtils.h"
#include "../../../Core/Assertion.h"
#include <LLGL/Utils/TypeNames.h>
#include <LLGL/Utils/ForRange.h>
#include <algorithm>
#include <stdexcept>
#include <d3dcompiler.h>


Expand Down Expand Up @@ -89,24 +89,12 @@ bool D3D11Shader::BuildShader(ID3D11Device* device, const ShaderDescriptor& shad
return LoadBinary(device, shaderDesc);
}

static DXGI_FORMAT GetInputElementFormat(const VertexAttribute& attrib)
{
try
{
return DXTypes::ToDXGIFormat(attrib.format);
}
catch (const std::exception& e)
{
throw std::invalid_argument(std::string(e.what()) + " for vertex attribute: " + std::string(attrib.name.c_str()));
}
}

// Converts a vertex attribute to a D3D input element descriptor
static void ConvertInputElementDesc(D3D11_INPUT_ELEMENT_DESC& dst, const VertexAttribute& src)
{
dst.SemanticName = src.name.c_str();
dst.SemanticIndex = src.semanticIndex;
dst.Format = GetInputElementFormat(src);
dst.Format = DXTypes::ToDXGIFormat(src.format);
dst.InputSlot = src.slot;
dst.AlignedByteOffset = src.offset;
dst.InputSlotClass = (src.instanceDivisor > 0 ? D3D11_INPUT_PER_INSTANCE_DATA : D3D11_INPUT_PER_VERTEX_DATA);
Expand All @@ -130,8 +118,7 @@ void D3D11Shader::BuildInputLayout(ID3D11Device* device, UINT numVertexAttribs,
return;

/* Check if input layout is allowed */
if (GetType() != ShaderType::Vertex)
throw std::runtime_error("cannot build input layout for shader unless it is a vertex shader");
LLGL_ASSERT(GetType() == ShaderType::Vertex, "cannot build input layout for non-vertex-shader");

/* Setup input element descriptors */
std::vector<D3D11_INPUT_ELEMENT_DESC> inputElements;
Expand Down
6 changes: 4 additions & 2 deletions sources/Renderer/Direct3D12/RenderState/D3D12ComputePSO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "../../DXCommon/DXCore.h"
#include "../../CheckedCast.h"
#include "../../PipelineStateUtils.h"
#include "../../../Core/Assertion.h"
#include <LLGL/PipelineStateFlags.h>


Expand All @@ -32,7 +31,10 @@ D3D12ComputePSO::D3D12ComputePSO(
{
auto* computeShaderD3D = LLGL_CAST(const D3D12Shader*, desc.computeShader);
if (computeShaderD3D == nullptr)
throw std::runtime_error("cannot create D3D compute pipeline without compute shader");
{
ResetReport("cannot create D3D compute PSO without compute shader", true);
return;
}

/* Create native compute PSO */
if (pipelineCache != nullptr)
Expand Down
4 changes: 2 additions & 2 deletions sources/Renderer/OpenGL/GLCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace LLGL
{


// Throws an std::runtime_error exception if 'status' is not equal to 'statusRequired'.
// Traps program execution if 'status' is not equal to 'statusRequired'.
void GLThrowIfFailed(const GLenum status, const GLenum statusRequired, const char* info = nullptr);

// Converts the GL debug source into a string.
Expand All @@ -38,7 +38,7 @@ bool GLParseVersionString(const GLubyte* s, GLint& major, GLint& minor);
// Returns the GL profile version as a single number, e.g. 450 for OpenGL 4.5.
int GLGetVersion();

// Throws an std::runtime_error exception reporting a call to an unsupported OpenGL procedure.
// Traps program execution reporting a call to an unsupported OpenGL procedure.
[[noreturn]]
void ErrUnsupportedGLProc(const char* name);

Expand Down
2 changes: 1 addition & 1 deletion sources/Renderer/OpenGL/RenderState/GLQueryHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ GLQueryHeap::GLQueryHeap(const QueryHeapDescriptor& desc) :
#ifdef GL_ARB_pipeline_statistics_query
if (desc.type == QueryType::PipelineStatistics)
{
/* Allocate IDs for all pipeline statistics queries or throw error on failure */
/* Allocate IDs for all pipeline statistics queries or trap program execution on failure */
LLGL_ASSERT_GL_EXT(ARB_pipeline_statistics_query);
groupSize_ = static_cast<std::uint32_t>(sizeof(QueryPipelineStatistics) / sizeof(std::uint64_t));
}
Expand Down
3 changes: 2 additions & 1 deletion sources/Renderer/OpenGL/RenderState/GLRasterizerState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "../GLCore.h"
#include "../GLTypes.h"
#include "../../../Core/MacroUtils.h"
#include "../../../Core/Exception.h"
#include "GLStateManager.h"
#include <LLGL/PipelineStateFlags.h>

Expand All @@ -32,7 +33,7 @@ static GLState ToPolygonOffsetState(const PolygonMode mode)
case PolygonMode::Points: break;
#endif
}
throw std::invalid_argument("failed to map 'PolygonMode' to polygon offset mode (GL_POLYGON_OFFSET_FILL, GL_POLYGON_OFFSET_LINE, or GL_POLYGON_OFFSET_POINT)");
LLGL_TRAP("failed to map LLGL::PolygonMode(%d) to polygon offset mode (GL_POLYGON_OFFSET_FILL, GL_POLYGON_OFFSET_LINE, or GL_POLYGON_OFFSET_POINT)", static_cast<int>(mode));
}

static bool IsPolygonOffsetEnabled(const DepthBiasDescriptor& desc)
Expand Down
3 changes: 2 additions & 1 deletion sources/Renderer/OpenGL/Texture/GLTexImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "../Profile/GLProfile.h"
#include "../Ext/GLExtensions.h"
#include "../Ext/GLExtensionRegistry.h"
#include "../../../Core/Exception.h"
#include <LLGL/Utils/ColorRGBA.h>
#include <LLGL/Utils/ForRange.h>
#include <array>
Expand Down Expand Up @@ -74,7 +75,7 @@ static GLenum GetDefaultInitialGLImageFormat(Format format)
[[noreturn]]
static void ErrIllegalUseOfDepthFormat()
{
throw std::runtime_error("illegal use of depth-stencil format for texture");
LLGL_TRAP("illegal use of depth-stencil format for texture");
}

// Converts the internal format if necessary
Expand Down
Loading

0 comments on commit 8fb5e46

Please sign in to comment.