Skip to content

Commit

Permalink
backend: Add linux as platform for tests (#7375)
Browse files Browse the repository at this point in the history
- Also fixed a leak in `test_FeedbackLoops`
  • Loading branch information
poweifeng authored Nov 27, 2023
1 parent 000bff5 commit b10d5c5
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 18 deletions.
44 changes: 28 additions & 16 deletions filament/backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ install(DIRECTORY ${PUBLIC_HDR_DIR}/backend DESTINATION include)
# ==================================================================================================
option(INSTALL_BACKEND_TEST "Install the backend test library so it can be consumed on iOS" OFF)

if (APPLE)
add_library(backend_test STATIC
if (APPLE OR LINUX)
set(BACKEND_TEST_SRC
test/BackendTest.cpp
test/ShaderGenerator.cpp
test/TrianglePrimitive.cpp
Expand All @@ -410,19 +410,26 @@ if (APPLE)
test/test_BufferUpdates.cpp
test/test_MRT.cpp
test/test_LoadImage.cpp
test/test_RenderExternalImage.cpp
test/test_StencilBuffer.cpp
test/test_Scissor.cpp
test/test_MipLevels.cpp
)

target_link_libraries(backend_test PRIVATE
)
set(BACKEND_TEST_LIBS
backend
getopt
gtest
imageio
filamat
SPIRV
spirv-cross-glsl)
endif()

if (APPLE)
# TODO: we should expand this test to Linux and other platforms.
list(APPEND BACKEND_TEST_SRC
test/test_RenderExternalImage.cpp)
add_library(backend_test STATIC ${BACKEND_TEST_SRC})
target_link_libraries(backend_test PRIVATE ${BACKEND_TEST_LIBS})

set(BACKEND_TEST_DEPS
OGLCompiler
Expand All @@ -436,12 +443,11 @@ if (APPLE)
glslang
spirv-cross-core
spirv-cross-glsl
spirv-cross-msl
)
spirv-cross-msl)

if (NOT IOS)
target_link_libraries(backend_test PRIVATE image imageio)
list(APPEND BACKEND_TEST_DEPS image imageio)
list(APPEND BACKEND_TEST_DEPS image)
endif()

set(BACKEND_TEST_COMBINED_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/libbackendtest_combined.a")
Expand All @@ -455,15 +461,21 @@ if (APPLE)
endif()

set_target_properties(backend_test PROPERTIES FOLDER Tests)

if (APPLE AND NOT IOS)
add_executable(backend_test_mac test/mac_runner.mm)
target_link_libraries(backend_test_mac PRIVATE "-framework Metal -framework AppKit -framework QuartzCore")
# Because each test case is a separate file, the -force_load flag is necessary to prevent the
# linker from removing "unused" symbols.
target_link_libraries(backend_test_mac PRIVATE -force_load backend_test)
set_target_properties(backend_test_mac PROPERTIES FOLDER Tests)
endif()
endif()

if (APPLE AND NOT IOS)
add_executable(backend_test_mac test/mac_runner.mm)
target_link_libraries(backend_test_mac PRIVATE "-framework Metal -framework AppKit -framework QuartzCore")
# Because each test case is a separate file, the -force_load flag is necessary to prevent the
# linker from removing "unused" symbols.
target_link_libraries(backend_test_mac PRIVATE -force_load backend_test)
set_target_properties(backend_test_mac PROPERTIES FOLDER Tests)
if (LINUX)
add_executable(backend_test_linux test/linux_runner.cpp ${BACKEND_TEST_SRC})
target_link_libraries(backend_test_linux PRIVATE ${BACKEND_TEST_LIBS})
set_target_properties(backend_test_linux PROPERTIES FOLDER Tests)
endif()

# ==================================================================================================
Expand Down
4 changes: 3 additions & 1 deletion filament/backend/test/BackendTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ void BackendTest::flushAndWait() {

Handle<HwSwapChain> BackendTest::createSwapChain() {
const NativeView& view = getNativeView();
if (!view.ptr) {
return getDriverApi().createSwapChainHeadless(view.width, view.height, 0);
}
return getDriverApi().createSwapChain(view.ptr, 0);
}

Expand Down Expand Up @@ -212,4 +215,3 @@ int runTests() {
}

} // namespace test

3 changes: 3 additions & 0 deletions filament/backend/test/PlatformRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

namespace test {

constexpr uint32_t const WINDOW_WIDTH = 512;
constexpr uint32_t const WINDOW_HEIGHT = 512;

// To avoid a dependency on filabridge, the Backend enum is replicated here.
enum class Backend : uint8_t {
OPENGL = 1,
Expand Down
56 changes: 56 additions & 0 deletions filament/backend/test/linux_runner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#include "BackendTest.h"
#include "PlatformRunner.h"

#include <array>
#include <iostream>

namespace test {

test::NativeView getNativeView() {
return {
.ptr = nullptr,
.width = WINDOW_WIDTH,
.height = WINDOW_HEIGHT,
};
}

}// namespace test

namespace {

std::array<test::Backend, 2> const VALID_BACKENDS{
test::Backend::OPENGL,
test::Backend::VULKAN,
};

}// namespace

int main(int argc, char* argv[]) {
auto backend = test::parseArgumentsForBackend(argc, argv);

if (!std::any_of(VALID_BACKENDS.begin(), VALID_BACKENDS.end(),
[backend](test::Backend validBackend) { return backend == validBackend; })) {
std::cerr << "Specified an invalid backend. Only GL and Vulkan are available" << std::endl;
return 1;
}

test::initTests(backend, false, argc, argv);
return test::runTests();
}
2 changes: 1 addition & 1 deletion filament/backend/test/mac_runner.mm
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}

- (NSView*)createView {
NSRect frame = NSMakeRect(0, 0, 512, 512);
NSRect frame = NSMakeRect(0, 0, test::WINDOW_WIDTH, test::WINDOW_HEIGHT);
NSWindow* window = [[NSWindow alloc] initWithContentRect:frame
styleMask:NSWindowStyleMaskBorderless
backing:NSBackingStoreBuffered
Expand Down
1 change: 1 addition & 0 deletions filament/backend/test/test_FeedbackLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ static void dumpScreenshot(DriverApi& dapi, Handle<HwRenderTarget> rt) {
std::ofstream pngstrm("feedback.png", std::ios::binary | std::ios::trunc);
ImageEncoder::encode(pngstrm, ImageEncoder::Format::PNG, image, "", "feedback.png");
#endif
free(buffer);
};
PixelBufferDescriptor pb(buffer, size, PixelDataFormat::RGBA, PixelDataType::UBYTE, cb);
dapi.readPixels(rt, 0, 0, kTexWidth, kTexHeight, std::move(pb));
Expand Down

0 comments on commit b10d5c5

Please sign in to comment.