Skip to content

Commit

Permalink
IGLU: Add new Sentinel module for sentinel implementations
Browse files Browse the repository at this point in the history
Summary: This diff adds a new sentintenl IGLU module. This module will hold dummy, sentinel implementations of IGL interfaces. These can be used for safety when otherwise accessing a potentially null pointer to an IGL object.

Reviewed By: pixelperfect3, corporateshark

Differential Revision: D49357729

fbshipit-source-id: 2c0d677c7503b8d3af147f36869db0f298d38b31
  • Loading branch information
Eric Griffith authored and facebook-github-bot committed Sep 20, 2023
1 parent cffacf8 commit ddc035a
Show file tree
Hide file tree
Showing 14 changed files with 690 additions and 0 deletions.
1 change: 1 addition & 0 deletions IGLU/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ endmacro()

add_iglu_module(imgui)
add_iglu_module(managedUniformBuffer)
add_iglu_module(sentinel)
add_iglu_module(simple_renderer)
add_iglu_module(state_pool)
add_iglu_module(texture_accessor)
Expand Down
13 changes: 13 additions & 0 deletions IGLU/sentinel/Assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <igl/Common.h>

#define IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert) \
IGL_ASSERT_MSG(!(shouldAssert), "Sentinel implementation should NOT be reached")
57 changes: 57 additions & 0 deletions IGLU/sentinel/Buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <IGLU/sentinel/Buffer.h>

#include <IGLU/sentinel/Assert.h>
#include <igl/IGL.h>

namespace iglu::sentinel {

Buffer::Buffer(bool shouldAssert) : shouldAssert_(shouldAssert) {}

igl::Result Buffer::upload(const void* IGL_NULLABLE /*data*/, const igl::BufferRange& /*range*/) {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
return igl::Result(igl::Result::Code::Unimplemented, "Not Implemented");
}

void* IGL_NULLABLE Buffer::map(const igl::BufferRange& /*range*/,
igl::Result* IGL_NULLABLE /*outResult*/) {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
return nullptr;
}

void Buffer::unmap() {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
}

igl::BufferDesc::BufferAPIHint Buffer::requestedApiHints() const noexcept {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
return 0;
}

igl::BufferDesc::BufferAPIHint Buffer::acceptedApiHints() const noexcept {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
return 0;
}

igl::ResourceStorage Buffer::storage() const noexcept {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
return igl::ResourceStorage::Invalid;
}

size_t Buffer::getSizeInBytes() const {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
return 0;
}

uint64_t Buffer::gpuAddress(size_t /*offset*/) const {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
return 0;
}

} // namespace iglu::sentinel
38 changes: 38 additions & 0 deletions IGLU/sentinel/Buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <igl/Buffer.h>

namespace iglu::sentinel {

/**
* Sentinel Buffer intended for safe use where access to a real buffer is not available.
* Use cases include returning a reference to a buffer from a raw pointer when a valid buffer is not
* available.
* All methods return nullptr, the default value or an error.
*/
class Buffer : public igl::IBuffer {
public:
explicit Buffer(bool shouldAssert = true);

[[nodiscard]] igl::Result upload(const void* IGL_NULLABLE data,
const igl::BufferRange& range) final;
void* IGL_NULLABLE map(const igl::BufferRange& range, igl::Result* IGL_NULLABLE outResult) final;
void unmap() final;
[[nodiscard]] igl::BufferDesc::BufferAPIHint requestedApiHints() const noexcept final;
[[nodiscard]] igl::BufferDesc::BufferAPIHint acceptedApiHints() const noexcept final;
[[nodiscard]] igl::ResourceStorage storage() const noexcept final;
[[nodiscard]] size_t getSizeInBytes() const final;
[[nodiscard]] uint64_t gpuAddress(size_t offset = 0) const final;

private:
[[maybe_unused]] bool shouldAssert_;
};

} // namespace iglu::sentinel
51 changes: 51 additions & 0 deletions IGLU/sentinel/CommandBuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <IGLU/sentinel/CommandBuffer.h>

#include <IGLU/sentinel/Assert.h>
#include <igl/IGL.h>

namespace iglu::sentinel {

CommandBuffer::CommandBuffer(bool shouldAssert) : shouldAssert_(shouldAssert) {}

std::unique_ptr<igl::IRenderCommandEncoder> CommandBuffer::createRenderCommandEncoder(
const igl::RenderPassDesc& /*renderPass*/,
std::shared_ptr<igl::IFramebuffer> /*framebuffer*/,
igl::Result* IGL_NULLABLE /*outResult*/) {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
return nullptr;
}

std::unique_ptr<igl::IComputeCommandEncoder> CommandBuffer::createComputeCommandEncoder() {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
return nullptr;
}

void CommandBuffer::present(std::shared_ptr<igl::ITexture> /*surface*/) const {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
}

void CommandBuffer::waitUntilScheduled() {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
}

void CommandBuffer::waitUntilCompleted() {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
}

void CommandBuffer::pushDebugGroupLabel(const std::string& /*label*/,
const igl::Color& /*color*/) const {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
}

void CommandBuffer::popDebugGroupLabel() const {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
}

} // namespace iglu::sentinel
41 changes: 41 additions & 0 deletions IGLU/sentinel/CommandBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <igl/CommandBuffer.h>

namespace iglu::sentinel {

/**
* Sentinel CommandBuffer intended for safe use where access to a real command buffer is not
* available.
* Use cases include returning a reference to a command buffer from a raw pointer when a
* valid command buffer is not available.
* All methods return nullptr, the default value or an error.
*/
class CommandBuffer final : public igl::ICommandBuffer {
public:
explicit CommandBuffer(bool shouldAssert = true);

[[nodiscard]] std::unique_ptr<igl::IRenderCommandEncoder> createRenderCommandEncoder(
const igl::RenderPassDesc& /*renderPass*/,
std::shared_ptr<igl::IFramebuffer> /*framebuffer*/,
igl::Result* IGL_NULLABLE /*outResult*/) final;
[[nodiscard]] std::unique_ptr<igl::IComputeCommandEncoder> createComputeCommandEncoder() final;
void present(std::shared_ptr<igl::ITexture> /*surface*/) const final;
void waitUntilScheduled() final;
void waitUntilCompleted() final;
void pushDebugGroupLabel(const std::string& /*label*/,
const igl::Color& /*color*/ = igl::Color(1, 1, 1, 1)) const final;
void popDebugGroupLabel() const final;

private:
[[maybe_unused]] bool shouldAssert_;
};

} // namespace iglu::sentinel
30 changes: 30 additions & 0 deletions IGLU/sentinel/CommandQueue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <IGLU/sentinel/CommandQueue.h>

#include <IGLU/sentinel/Assert.h>

namespace iglu::sentinel {

CommandQueue::CommandQueue(bool shouldAssert) : shouldAssert_(shouldAssert) {}

std::shared_ptr<igl::ICommandBuffer> CommandQueue::createCommandBuffer(
const igl::CommandBufferDesc& /*desc*/,
igl::Result* IGL_NULLABLE
/*outResult*/) {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
return nullptr;
}

igl::SubmitHandle CommandQueue::submit(const igl::ICommandBuffer& /*commandBuffer*/,
bool /*endOfFrame*/) {
IGLU_SENTINEL_ASSERT_IF_NOT(shouldAssert_);
return 0;
}

} // namespace iglu::sentinel
34 changes: 34 additions & 0 deletions IGLU/sentinel/CommandQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <igl/CommandQueue.h>

namespace iglu::sentinel {

/**
* Sentinel CommandQueue intended for safe use where access to a real command queue is not
* available.
* Use cases include returning a reference to a command queue from a raw pointer when a
* valid command queue is not available.
* All methods return nullptr, the default value or an error.
*/
class CommandQueue final : public igl::ICommandQueue {
public:
explicit CommandQueue(bool shouldAssert = true);

[[nodiscard]] std::shared_ptr<igl::ICommandBuffer> createCommandBuffer(
const igl::CommandBufferDesc& desc,
igl::Result* IGL_NULLABLE outResult) final;
igl::SubmitHandle submit(const igl::ICommandBuffer& commandBuffer, bool endOfFrame = false) final;

private:
[[maybe_unused]] bool shouldAssert_;
};

} // namespace iglu::sentinel
Loading

0 comments on commit ddc035a

Please sign in to comment.