Skip to content

Commit

Permalink
IGL: Add Invalid backend type
Browse files Browse the repository at this point in the history
Summary:
This diff adds a new backend type: Invalid. This will be used to create a safe, sentinel device that returns Invalid as its backend type, which should prevent any code that validate the backend of a device it works with from using it.

Relevant code that is checking all possible backends is updated to reflect this change.

Reviewed By: corporateshark

Differential Revision: D49359044

fbshipit-source-id: 84115bede9e59e695e814909705523c215e30e1e
  • Loading branch information
Eric Griffith authored and facebook-github-bot committed Sep 19, 2023
1 parent af263ae commit f0a2d23
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 44 deletions.
5 changes: 4 additions & 1 deletion IGLU/imgui/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ void main() {
static std::unique_ptr<igl::IShaderStages> getShaderStagesForBackend(igl::IDevice& device) {
igl::Result result;
switch (device.getBackendType()) {
case igl::BackendType::Invalid:
IGL_ASSERT_NOT_REACHED();
return nullptr;
case igl::BackendType::Vulkan: {
return igl::ShaderStagesCreator::fromModuleStringInput(device,
getVulkanVertexShaderSource(),
Expand Down Expand Up @@ -158,7 +161,7 @@ static std::unique_ptr<igl::IShaderStages> getShaderStagesForBackend(igl::IDevic
break;
}
}
IGL_UNREACHABLE_RETURN(nullptr);
IGL_UNREACHABLE_RETURN(nullptr)
}

struct DrawableData {
Expand Down
2 changes: 1 addition & 1 deletion IGLU/simple_renderer/ShaderUniforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ShaderUniforms::ShaderUniforms(igl::IDevice& device,
createBuffer = iglDesc.isUniformBlock;
} else if (device_.getBackendType() == igl::BackendType::Vulkan) {
createBuffer = true;
} else {
} else if (device_.getBackendType() == igl::BackendType::Metal) {
// On Metal, need to create buffers only when data > 4kb
createBuffer = !hasBindBytesFeature || length > bindBytesLimit;
}
Expand Down
2 changes: 2 additions & 0 deletions IGLU/uniform/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ void Encoder::operator()(igl::IRenderCommandEncoder& encoder,
IGL_ASSERT_NOT_IMPLEMENTED();
// @fb-only
// @fb-only
} else {
IGL_ASSERT_NOT_REACHED();
}
}

Expand Down
4 changes: 4 additions & 0 deletions samples/ios/snapshot_test_support/TinyRenderable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ const char kGLSLShaderSourceFragment[] =

std::unique_ptr<igl::IShaderStages> getShaderStagesForBackend(igl::IDevice& device) {
switch (device.getBackendType()) {
case igl::BackendType::Invalid:
IGL_ASSERT_NOT_REACHED();
return nullptr;
case igl::BackendType::Vulkan:
IGL_ASSERT_MSG(0, "IGLSamples not set up for Vulkan");
return nullptr;
Expand Down Expand Up @@ -129,6 +132,7 @@ std::unique_ptr<igl::IShaderStages> getShaderStagesForBackend(igl::IDevice& devi
return stages;
}
}
IGL_UNREACHABLE_RETURN(nullptr)
}

const size_t kTextureUnit = 0;
Expand Down
3 changes: 3 additions & 0 deletions shell/ios/ViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ - (void)tick {

- (void)loadView {
switch (backendType_) {
case igl::BackendType::Invalid:
IGL_ASSERT_NOT_REACHED();
break;
case igl::BackendType::Metal: {
#if IGL_BACKEND_METAL
[self initShell];
Expand Down
5 changes: 4 additions & 1 deletion shell/renderSessions/ColorSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ static std::string getVulkanFragmentShaderSource() {

static std::unique_ptr<IShaderStages> getShaderStagesForBackend(igl::IDevice& device) {
switch (device.getBackendType()) {
case igl::BackendType::Invalid:
IGL_ASSERT_NOT_REACHED();
return nullptr;
case igl::BackendType::Vulkan:
return igl::ShaderStagesCreator::fromModuleStringInput(device,
getVulkanVertexShaderSource().c_str(),
Expand Down Expand Up @@ -171,7 +174,7 @@ static std::unique_ptr<IShaderStages> getShaderStagesForBackend(igl::IDevice& de
"",
nullptr);
}
IGL_UNREACHABLE_RETURN(nullptr);
IGL_UNREACHABLE_RETURN(nullptr)
}

ColorSession::ColorSession(std::shared_ptr<Platform> platform) :
Expand Down
5 changes: 4 additions & 1 deletion shell/renderSessions/HelloWorldSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ void main() {

static std::unique_ptr<IShaderStages> getShaderStagesForBackend(igl::IDevice& device) {
switch (device.getBackendType()) {
case igl::BackendType::Invalid:
IGL_ASSERT_NOT_REACHED();
return nullptr;
case igl::BackendType::Vulkan:
return igl::ShaderStagesCreator::fromModuleStringInput(device,
getVulkanVertexShaderSource(),
Expand Down Expand Up @@ -122,7 +125,7 @@ static std::unique_ptr<IShaderStages> getShaderStagesForBackend(igl::IDevice& de
return nullptr;
#endif // IGL_BACKEND_OPENGL
}
IGL_UNREACHABLE_RETURN(nullptr);
IGL_UNREACHABLE_RETURN(nullptr)
}

void HelloWorldSession::initialize() noexcept {
Expand Down
26 changes: 4 additions & 22 deletions shell/renderSessions/MRTSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ static std::string getVulkanFragmentShaderSource(int programIndex) {
static std::unique_ptr<IShaderStages> createShaderStagesForBackend(const IDevice& device,
int programIndex) {
switch (device.getBackendType()) {
case igl::BackendType::Invalid:
IGL_ASSERT_NOT_REACHED();
return nullptr;
case igl::BackendType::Vulkan:
return igl::ShaderStagesCreator::fromModuleStringInput(
device,
Expand Down Expand Up @@ -273,28 +276,7 @@ static std::unique_ptr<IShaderStages> createShaderStagesForBackend(const IDevice
}

static bool isDeviceCompatible(IDevice& device) noexcept {
const auto backendtype = device.getBackendType();
if (backendtype == BackendType::OpenGL) {
#if IGL_BACKEND_OPENGL
auto glVersion =
static_cast<opengl::Device&>(device).getContext().deviceFeatures().getGLVersion();
#if IGL_OPENGL_ES
if (glVersion < opengl::GLVersion::v3_0_ES) {
return false;
}
#else
if (glVersion < opengl::GLVersion::v4_1) {
return false;
}
#endif // IGL_OPENGL_ES
#endif // IGL_BACKEND_OPENGL
} else if (backendtype == BackendType::Vulkan) {
return true;
// @fb-only
// @fb-only
}

return true;
return device.hasFeature(DeviceFeatures::MultipleRenderTargets);
}

void MRTSession::initialize() noexcept {
Expand Down
5 changes: 4 additions & 1 deletion shell/renderSessions/TQMultiRenderPassSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ static std::string getOpenGLFragmentShaderSource() {

static std::unique_ptr<IShaderStages> getShaderStagesForBackend(igl::IDevice& device) {
switch (device.getBackendType()) {
case igl::BackendType::Invalid:
IGL_ASSERT_NOT_REACHED();
return nullptr;
case igl::BackendType::Vulkan:
IGL_ASSERT_MSG(0, "IGLSamples not set up for Vulkan");
return nullptr;
Expand All @@ -124,7 +127,7 @@ static std::unique_ptr<IShaderStages> getShaderStagesForBackend(igl::IDevice& de
"",
nullptr);
}
IGL_UNREACHABLE_RETURN(nullptr);
IGL_UNREACHABLE_RETURN(nullptr)
}

static void render(std::shared_ptr<ICommandBuffer>& buffer,
Expand Down
5 changes: 4 additions & 1 deletion shell/renderSessions/TQSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ static std::string getVulkanFragmentShaderSource() {

static std::unique_ptr<IShaderStages> getShaderStagesForBackend(igl::IDevice& device) {
switch (device.getBackendType()) {
case igl::BackendType::Invalid:
IGL_ASSERT_NOT_REACHED();
return nullptr;
case igl::BackendType::Vulkan:
return igl::ShaderStagesCreator::fromModuleStringInput(device,
getVulkanVertexShaderSource().c_str(),
Expand Down Expand Up @@ -173,7 +176,7 @@ static std::unique_ptr<IShaderStages> getShaderStagesForBackend(igl::IDevice& de
"",
nullptr);
}
IGL_UNREACHABLE_RETURN(nullptr);
IGL_UNREACHABLE_RETURN(nullptr)
}

void TQSession::initialize() noexcept {
Expand Down
20 changes: 6 additions & 14 deletions shell/renderSessions/Textured3DCubeSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static const char* getVulkanVertexShaderSource() {
mat4 mvpMatrix;
float scaleZ;
} perFrame;
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 uvw_in;
layout(location = 0) out vec3 uvw;
Expand All @@ -157,6 +157,9 @@ static const char* getVulkanVertexShaderSource() {

static std::unique_ptr<IShaderStages> getShaderStagesForBackend(igl::IDevice& device) {
switch (device.getBackendType()) {
case igl::BackendType::Invalid:
IGL_ASSERT_NOT_REACHED();
return nullptr;
case igl::BackendType::Vulkan:
return igl::ShaderStagesCreator::fromModuleStringInput(device,
getVulkanVertexShaderSource(),
Expand Down Expand Up @@ -184,22 +187,11 @@ static std::unique_ptr<IShaderStages> getShaderStagesForBackend(igl::IDevice& de
"",
nullptr);
}
IGL_UNREACHABLE_RETURN(nullptr);
IGL_UNREACHABLE_RETURN(nullptr)
}

static bool isDeviceCompatible(IDevice& device) noexcept {
const auto backendtype = device.getBackendType();
if (backendtype == BackendType::OpenGL) {
#if IGL_BACKEND_OPENGL
return device.hasFeature(DeviceFeatures::Texture3D);
#endif // IGL_BACKEND_OPENGL
} else if (backendtype == BackendType::Vulkan) {
return true;
// @fb-only
// @fb-only
}

return true;
return device.hasFeature(DeviceFeatures::Texture3D);
}

void Textured3DCubeSession::createSamplerAndTextures(const igl::IDevice& device) {
Expand Down
2 changes: 2 additions & 0 deletions src/igl/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ static_assert(sizeof(Color) == 4 * sizeof(float));

std::string BackendTypeToString(BackendType backendType) {
switch (backendType) {
case BackendType::Invalid:
return "Invalid";
case BackendType::OpenGL:
return "OpenGL";
case BackendType::Metal:
Expand Down
1 change: 1 addition & 0 deletions src/igl/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ struct Result {
};

enum class BackendType {
Invalid,
OpenGL,
Metal,
Vulkan,
Expand Down
2 changes: 2 additions & 0 deletions src/igl/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ TextureDesc IDevice::sanitize(const TextureDesc& desc) const {

Color IDevice::backendDebugColor() const noexcept {
switch (getBackendType()) {
case BackendType::Invalid:
return {0.f, 0.f, 0.f, 0.f};
case BackendType::OpenGL:
return {1.f, 1.f, 0.f, 1.f};
case BackendType::Metal:
Expand Down
6 changes: 4 additions & 2 deletions src/igl/tests/util/device/TestDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ namespace igl::tests::util::device {

bool isBackendTypeSupported(::igl::BackendType backendType) {
switch (backendType) {
case ::igl::BackendType::Invalid:
IGL_ASSERT_NOT_REACHED();
return false;
case ::igl::BackendType::Metal:
return IGL_METAL_SUPPORTED;
case ::igl::BackendType::OpenGL:
Expand All @@ -51,9 +54,8 @@ bool isBackendTypeSupported(::igl::BackendType backendType) {
return IGL_VULKAN_SUPPORTED;
// @fb-only
// @fb-only
default:
return false;
}
IGL_UNREACHABLE_RETURN(false)
}

std::shared_ptr<::igl::IDevice> createTestDevice(::igl::BackendType backendType,
Expand Down

0 comments on commit f0a2d23

Please sign in to comment.